17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 55aefb655Srie * Common Development and Distribution License (the "License"). 65aefb655Srie * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21141040e8Srie 227c478bd9Sstevel@tonic-gate /* 23bb3b4f6cSRod Evans * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24b3fbe5e6Sseizo * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 27ba2be530Sab /* Get the x86 version of the relocation engine */ 28ba2be530Sab #define DO_RELOC_LIBLD_X86 29ba2be530Sab 307c478bd9Sstevel@tonic-gate #include <string.h> 317c478bd9Sstevel@tonic-gate #include <stdio.h> 327c478bd9Sstevel@tonic-gate #include <strings.h> 337c478bd9Sstevel@tonic-gate #include <sys/elf_amd64.h> 347c478bd9Sstevel@tonic-gate #include <debug.h> 357c478bd9Sstevel@tonic-gate #include <reloc.h> 36ba2be530Sab #include <i386/machdep_x86.h> 375aefb655Srie #include "msg.h" 385aefb655Srie #include "_libld.h" 39ba2be530Sab 40*57ef7aa9SRod Evans /* 41*57ef7aa9SRod Evans * Search the GOT index list for a GOT entry with a matching reference and the 42*57ef7aa9SRod Evans * proper addend. 43*57ef7aa9SRod Evans */ 44*57ef7aa9SRod Evans static Gotndx * 45*57ef7aa9SRod Evans ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc) 46*57ef7aa9SRod Evans { 47*57ef7aa9SRod Evans Aliste idx; 48*57ef7aa9SRod Evans Gotndx *gnp; 49*57ef7aa9SRod Evans 50*57ef7aa9SRod Evans assert(rdesc != 0); 51*57ef7aa9SRod Evans 52*57ef7aa9SRod Evans if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx) 53*57ef7aa9SRod Evans return (ofl->ofl_tlsldgotndx); 54ba2be530Sab 55*57ef7aa9SRod Evans for (ALIST_TRAVERSE(alp, idx, gnp)) { 56*57ef7aa9SRod Evans if ((rdesc->rel_raddend == gnp->gn_addend) && 57*57ef7aa9SRod Evans (gnp->gn_gotref == gref)) { 58*57ef7aa9SRod Evans return (gnp); 59*57ef7aa9SRod Evans } 60*57ef7aa9SRod Evans } 61*57ef7aa9SRod Evans return (NULL); 62*57ef7aa9SRod Evans } 637c478bd9Sstevel@tonic-gate 64*57ef7aa9SRod Evans static Xword 65*57ef7aa9SRod Evans ld_calc_got_offset(Rel_desc *rdesc, Ofl_desc *ofl) 66*57ef7aa9SRod Evans { 67*57ef7aa9SRod Evans Os_desc *osp = ofl->ofl_osgot; 68*57ef7aa9SRod Evans Sym_desc *sdp = rdesc->rel_sym; 69*57ef7aa9SRod Evans Xword gotndx; 70*57ef7aa9SRod Evans Gotref gref; 71*57ef7aa9SRod Evans Gotndx *gnp; 72*57ef7aa9SRod Evans 73*57ef7aa9SRod Evans if (rdesc->rel_flags & FLG_REL_DTLS) 74*57ef7aa9SRod Evans gref = GOT_REF_TLSGD; 75*57ef7aa9SRod Evans else if (rdesc->rel_flags & FLG_REL_MTLS) 76*57ef7aa9SRod Evans gref = GOT_REF_TLSLD; 77*57ef7aa9SRod Evans else if (rdesc->rel_flags & FLG_REL_STLS) 78*57ef7aa9SRod Evans gref = GOT_REF_TLSIE; 79*57ef7aa9SRod Evans else 80*57ef7aa9SRod Evans gref = GOT_REF_GENERIC; 81*57ef7aa9SRod Evans 82*57ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc); 83*57ef7aa9SRod Evans assert(gnp); 84*57ef7aa9SRod Evans 85*57ef7aa9SRod Evans gotndx = (Xword)gnp->gn_gotndx; 86*57ef7aa9SRod Evans 87*57ef7aa9SRod Evans if ((rdesc->rel_flags & FLG_REL_DTLS) && 88*57ef7aa9SRod Evans (rdesc->rel_rtype == R_AMD64_DTPOFF64)) 89*57ef7aa9SRod Evans gotndx++; 90*57ef7aa9SRod Evans 91*57ef7aa9SRod Evans return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE))); 92*57ef7aa9SRod Evans } 93ba2be530Sab 94ba2be530Sab static Word 955aefb655Srie ld_init_rel(Rel_desc *reld, void *reloc) 967c478bd9Sstevel@tonic-gate { 97*57ef7aa9SRod Evans Rela *rel = (Rela *)reloc; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* LINTED */ 100ba2be530Sab reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH); 1017c478bd9Sstevel@tonic-gate reld->rel_roffset = rel->r_offset; 1027c478bd9Sstevel@tonic-gate reld->rel_raddend = rel->r_addend; 1037c478bd9Sstevel@tonic-gate reld->rel_typedata = 0; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate reld->rel_flags |= FLG_REL_RELA; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate return ((Word)ELF_R_SYM(rel->r_info)); 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate 110ba2be530Sab static void 1115aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl) 1127c478bd9Sstevel@tonic-gate { 1135aefb655Srie ofl->ofl_dehdr->e_flags |= ehdr->e_flags; 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 116ba2be530Sab static void 1175aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt) 1187c478bd9Sstevel@tonic-gate { 1197c478bd9Sstevel@tonic-gate if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) { 1207c478bd9Sstevel@tonic-gate /* 1217c478bd9Sstevel@tonic-gate * Create this entry if we are going to create a PLT table. 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate if (ofl->ofl_pltcnt) 1247c478bd9Sstevel@tonic-gate (*cnt)++; /* DT_PLTGOT */ 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 128ba2be530Sab static void 1295aefb655Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn) 1307c478bd9Sstevel@tonic-gate { 1315aefb655Srie if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) { 1325aefb655Srie (*dyn)->d_tag = DT_PLTGOT; 1335aefb655Srie if (ofl->ofl_osgot) 1345aefb655Srie (*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr; 1355aefb655Srie else 1365aefb655Srie (*dyn)->d_un.d_ptr = 0; 1375aefb655Srie (*dyn)++; 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 141ba2be530Sab static Xword 1425aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl) 1437c478bd9Sstevel@tonic-gate { 1447c478bd9Sstevel@tonic-gate Xword value; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) + 1477c478bd9Sstevel@tonic-gate M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE); 1487c478bd9Sstevel@tonic-gate return (value); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* 1527c478bd9Sstevel@tonic-gate * Build a single plt entry - code is: 1537c478bd9Sstevel@tonic-gate * JMP *name1@GOTPCREL(%rip) 1547c478bd9Sstevel@tonic-gate * PUSHL $index 1557c478bd9Sstevel@tonic-gate * JMP .PLT0 1567c478bd9Sstevel@tonic-gate */ 157b3fbe5e6Sseizo static uchar_t pltn_entry[M_PLT_ENTSIZE] = { 1587c478bd9Sstevel@tonic-gate /* 0x00 jmpq *name1@GOTPCREL(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 1597c478bd9Sstevel@tonic-gate /* 0x06 pushq $index */ 0x68, 0x00, 0x00, 0x00, 0x00, 1607c478bd9Sstevel@tonic-gate /* 0x0b jmpq .plt0(%rip) */ 0xe9, 0x00, 0x00, 0x00, 0x00 1617c478bd9Sstevel@tonic-gate /* 0x10 */ 1627c478bd9Sstevel@tonic-gate }; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate static uintptr_t 1657c478bd9Sstevel@tonic-gate plt_entry(Ofl_desc * ofl, Sym_desc * sdp) 1667c478bd9Sstevel@tonic-gate { 167b3fbe5e6Sseizo uchar_t *plt0, *pltent, *gotent; 1687c478bd9Sstevel@tonic-gate Sword plt_off; 1697c478bd9Sstevel@tonic-gate Word got_off; 1707c478bd9Sstevel@tonic-gate Xword val1; 171ba2be530Sab int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE; 1747c478bd9Sstevel@tonic-gate plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * 1757c478bd9Sstevel@tonic-gate M_PLT_ENTSIZE); 176b3fbe5e6Sseizo plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf); 1777c478bd9Sstevel@tonic-gate pltent = plt0 + plt_off; 178b3fbe5e6Sseizo gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off; 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate bcopy(pltn_entry, pltent, sizeof (pltn_entry)); 1817c478bd9Sstevel@tonic-gate /* 1827c478bd9Sstevel@tonic-gate * Fill in the got entry with the address of the next instruction. 1837c478bd9Sstevel@tonic-gate */ 1847c478bd9Sstevel@tonic-gate /* LINTED */ 185b3fbe5e6Sseizo *(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off + 186b3fbe5e6Sseizo M_PLT_INSSIZE; 187ba2be530Sab if (bswap) 188ba2be530Sab /* LINTED */ 189ba2be530Sab *(Word *)gotent = ld_bswap_Word(*(Word *)gotent); 1907c478bd9Sstevel@tonic-gate 191f3324781Sab /* 192f3324781Sab * If '-z noreloc' is specified - skip the do_reloc_ld 193f3324781Sab * stage. 194f3324781Sab */ 195f3324781Sab if (!OFL_DO_RELOC(ofl)) 196f3324781Sab return (1); 197f3324781Sab 1987c478bd9Sstevel@tonic-gate /* 1997c478bd9Sstevel@tonic-gate * patchup: 2007c478bd9Sstevel@tonic-gate * jmpq *name1@gotpcrel(%rip) 2017c478bd9Sstevel@tonic-gate * 2027c478bd9Sstevel@tonic-gate * NOTE: 0x06 represents next instruction. 2037c478bd9Sstevel@tonic-gate */ 2047c478bd9Sstevel@tonic-gate val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) - 205de777a60Sab (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06; 2067c478bd9Sstevel@tonic-gate 207f3324781Sab if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02], 208f3324781Sab &val1, MSG_ORIG(MSG_SYM_PLTENT), 209f3324781Sab MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 210f3324781Sab eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL), 211f3324781Sab sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name)); 212f3324781Sab return (S_ERROR); 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate /* 2167c478bd9Sstevel@tonic-gate * patchup: 2177c478bd9Sstevel@tonic-gate * pushq $pltndx 2187c478bd9Sstevel@tonic-gate */ 2197c478bd9Sstevel@tonic-gate val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1); 220f3324781Sab 221f3324781Sab if (do_reloc_ld(R_AMD64_32, &pltent[0x07], 222f3324781Sab &val1, MSG_ORIG(MSG_SYM_PLTENT), 223f3324781Sab MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 224f3324781Sab eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL), 225f3324781Sab sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name)); 226f3324781Sab return (S_ERROR); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* 2307c478bd9Sstevel@tonic-gate * patchup: 2317c478bd9Sstevel@tonic-gate * jmpq .plt0(%rip) 232f3324781Sab * NOTE: 0x10 represents next instruction. The rather complex 233f3324781Sab * series of casts is necessary to sign extend an offset into 234f3324781Sab * a 64-bit value while satisfying various compiler error 235f3324781Sab * checks. Handle with care. 2367c478bd9Sstevel@tonic-gate */ 237b3fbe5e6Sseizo val1 = (Xword)((intptr_t)((uintptr_t)plt0 - 238b3fbe5e6Sseizo (uintptr_t)(&pltent[0x10]))); 239b3fbe5e6Sseizo 240f3324781Sab if (do_reloc_ld(R_AMD64_PC32, &pltent[0x0c], 241f3324781Sab &val1, MSG_ORIG(MSG_SYM_PLTENT), 242f3324781Sab MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 243f3324781Sab eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL), 244f3324781Sab sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name)); 245f3324781Sab return (S_ERROR); 2467c478bd9Sstevel@tonic-gate } 247f3324781Sab 2487c478bd9Sstevel@tonic-gate return (1); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 251ba2be530Sab static uintptr_t 2525aefb655Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl) 2537c478bd9Sstevel@tonic-gate { 2547c478bd9Sstevel@tonic-gate Os_desc * relosp, * osp = 0; 2557c478bd9Sstevel@tonic-gate Word ndx; 2567c478bd9Sstevel@tonic-gate Xword roffset, value; 2577c478bd9Sstevel@tonic-gate Sxword raddend; 2587c478bd9Sstevel@tonic-gate Rela rea; 2597c478bd9Sstevel@tonic-gate char *relbits; 2607c478bd9Sstevel@tonic-gate Sym_desc * sdp, * psym = (Sym_desc *)0; 2617c478bd9Sstevel@tonic-gate int sectmoved = 0; 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate raddend = orsp->rel_raddend; 2647c478bd9Sstevel@tonic-gate sdp = orsp->rel_sym; 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate /* 2677c478bd9Sstevel@tonic-gate * If the section this relocation is against has been discarded 2687c478bd9Sstevel@tonic-gate * (-zignore), then also discard (skip) the relocation itself. 2697c478bd9Sstevel@tonic-gate */ 2707c478bd9Sstevel@tonic-gate if (orsp->rel_isdesc && ((orsp->rel_flags & 2717c478bd9Sstevel@tonic-gate (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) && 2727c478bd9Sstevel@tonic-gate (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) { 2735aefb655Srie DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp)); 2747c478bd9Sstevel@tonic-gate return (1); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate /* 2787c478bd9Sstevel@tonic-gate * If this is a relocation against a move table, or expanded move 2797c478bd9Sstevel@tonic-gate * table, adjust the relocation entries. 2807c478bd9Sstevel@tonic-gate */ 2817c478bd9Sstevel@tonic-gate if (orsp->rel_move) 2825aefb655Srie ld_adj_movereloc(ofl, orsp); 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate /* 2857c478bd9Sstevel@tonic-gate * If this is a relocation against a section then we need to adjust the 2867c478bd9Sstevel@tonic-gate * raddend field to compensate for the new position of the input section 2877c478bd9Sstevel@tonic-gate * within the new output section. 2887c478bd9Sstevel@tonic-gate */ 2897c478bd9Sstevel@tonic-gate if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) { 290*57ef7aa9SRod Evans if (ofl->ofl_parsyms && 2917c478bd9Sstevel@tonic-gate (sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 2927c478bd9Sstevel@tonic-gate /* LINTED */ 2935aefb655Srie (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) { 2945aefb655Srie DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym)); 2957c478bd9Sstevel@tonic-gate sectmoved = 1; 2967c478bd9Sstevel@tonic-gate if (ofl->ofl_flags & FLG_OF_RELOBJ) 2977c478bd9Sstevel@tonic-gate raddend = psym->sd_sym->st_value; 2987c478bd9Sstevel@tonic-gate else 2997c478bd9Sstevel@tonic-gate raddend = psym->sd_sym->st_value - 3007c478bd9Sstevel@tonic-gate psym->sd_isc->is_osdesc->os_shdr->sh_addr; 3017c478bd9Sstevel@tonic-gate /* LINTED */ 3027c478bd9Sstevel@tonic-gate raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata); 3037c478bd9Sstevel@tonic-gate if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 3047c478bd9Sstevel@tonic-gate raddend += 305de777a60Sab psym->sd_isc->is_osdesc->os_shdr->sh_addr; 3067c478bd9Sstevel@tonic-gate } else { 3077c478bd9Sstevel@tonic-gate /* LINTED */ 3087c478bd9Sstevel@tonic-gate raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata); 3097c478bd9Sstevel@tonic-gate if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 3107c478bd9Sstevel@tonic-gate raddend += 311de777a60Sab sdp->sd_isc->is_osdesc->os_shdr->sh_addr; 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate } 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate value = sdp->sd_sym->st_value; 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate if (orsp->rel_flags & FLG_REL_GOT) { 3187c478bd9Sstevel@tonic-gate /* 3197c478bd9Sstevel@tonic-gate * Note: for GOT relative relocations on amd64 3207c478bd9Sstevel@tonic-gate * we discard the addend. It was relevant 3217c478bd9Sstevel@tonic-gate * to the reference - not to the data item 3227c478bd9Sstevel@tonic-gate * being referenced (ie: that -4 thing). 3237c478bd9Sstevel@tonic-gate */ 3247c478bd9Sstevel@tonic-gate raddend = 0; 3257c478bd9Sstevel@tonic-gate osp = ofl->ofl_osgot; 3265aefb655Srie roffset = ld_calc_got_offset(orsp, ofl); 3275aefb655Srie 3287c478bd9Sstevel@tonic-gate } else if (orsp->rel_flags & FLG_REL_PLT) { 3297c478bd9Sstevel@tonic-gate /* 3307c478bd9Sstevel@tonic-gate * Note that relocations for PLT's actually 3317c478bd9Sstevel@tonic-gate * cause a relocation againt the GOT. 3327c478bd9Sstevel@tonic-gate */ 3337c478bd9Sstevel@tonic-gate osp = ofl->ofl_osplt; 3347c478bd9Sstevel@tonic-gate roffset = (ofl->ofl_osgot->os_shdr->sh_addr) + 3357c478bd9Sstevel@tonic-gate sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE; 3367c478bd9Sstevel@tonic-gate raddend = 0; 3377c478bd9Sstevel@tonic-gate if (plt_entry(ofl, sdp) == S_ERROR) 3387c478bd9Sstevel@tonic-gate return (S_ERROR); 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate } else if (orsp->rel_flags & FLG_REL_BSS) { 3417c478bd9Sstevel@tonic-gate /* 3427c478bd9Sstevel@tonic-gate * This must be a R_AMD64_COPY. For these set the roffset to 3437c478bd9Sstevel@tonic-gate * point to the new symbols location. 3447c478bd9Sstevel@tonic-gate */ 3457c478bd9Sstevel@tonic-gate osp = ofl->ofl_isbss->is_osdesc; 3467c478bd9Sstevel@tonic-gate roffset = value; 3477c478bd9Sstevel@tonic-gate 3487c478bd9Sstevel@tonic-gate /* 3497c478bd9Sstevel@tonic-gate * The raddend doesn't mean anything in a R_SPARC_COPY 3507c478bd9Sstevel@tonic-gate * relocation. Null it out because it can confuse people. 3517c478bd9Sstevel@tonic-gate */ 3527c478bd9Sstevel@tonic-gate raddend = 0; 3537c478bd9Sstevel@tonic-gate } else { 3547c478bd9Sstevel@tonic-gate osp = orsp->rel_osdesc; 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate /* 3577c478bd9Sstevel@tonic-gate * Calculate virtual offset of reference point; equals offset 3587c478bd9Sstevel@tonic-gate * into section + vaddr of section for loadable sections, or 3597c478bd9Sstevel@tonic-gate * offset plus section displacement for nonloadable sections. 3607c478bd9Sstevel@tonic-gate */ 3617c478bd9Sstevel@tonic-gate roffset = orsp->rel_roffset + 3627c478bd9Sstevel@tonic-gate (Off)_elf_getxoff(orsp->rel_isdesc->is_indata); 3637c478bd9Sstevel@tonic-gate if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) 3647c478bd9Sstevel@tonic-gate roffset += orsp->rel_isdesc->is_osdesc-> 3657c478bd9Sstevel@tonic-gate os_shdr->sh_addr; 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0)) 3697c478bd9Sstevel@tonic-gate relosp = ofl->ofl_osrel; 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate /* 3727c478bd9Sstevel@tonic-gate * Assign the symbols index for the output relocation. If the 3737c478bd9Sstevel@tonic-gate * relocation refers to a SECTION symbol then it's index is based upon 3747c478bd9Sstevel@tonic-gate * the output sections symbols index. Otherwise the index can be 3757c478bd9Sstevel@tonic-gate * derived from the symbols index itself. 3767c478bd9Sstevel@tonic-gate */ 3777c478bd9Sstevel@tonic-gate if (orsp->rel_rtype == R_AMD64_RELATIVE) 3787c478bd9Sstevel@tonic-gate ndx = STN_UNDEF; 3797c478bd9Sstevel@tonic-gate else if ((orsp->rel_flags & FLG_REL_SCNNDX) || 3807c478bd9Sstevel@tonic-gate (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) { 3817c478bd9Sstevel@tonic-gate if (sectmoved == 0) { 3827c478bd9Sstevel@tonic-gate /* 3837c478bd9Sstevel@tonic-gate * Check for a null input section. This can 3847c478bd9Sstevel@tonic-gate * occur if this relocation references a symbol 3857c478bd9Sstevel@tonic-gate * generated by sym_add_sym(). 3867c478bd9Sstevel@tonic-gate */ 387*57ef7aa9SRod Evans if (sdp->sd_isc && sdp->sd_isc->is_osdesc) 388*57ef7aa9SRod Evans ndx = sdp->sd_isc->is_osdesc->os_identndx; 3897c478bd9Sstevel@tonic-gate else 3907c478bd9Sstevel@tonic-gate ndx = sdp->sd_shndx; 3917c478bd9Sstevel@tonic-gate } else 39235450702SAli Bahrami ndx = ofl->ofl_parexpnndx; 3937c478bd9Sstevel@tonic-gate } else 3947c478bd9Sstevel@tonic-gate ndx = sdp->sd_symndx; 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate /* 3977c478bd9Sstevel@tonic-gate * Add the symbols 'value' to the addend field. 3987c478bd9Sstevel@tonic-gate */ 3997c478bd9Sstevel@tonic-gate if (orsp->rel_flags & FLG_REL_ADVAL) 4007c478bd9Sstevel@tonic-gate raddend += value; 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate /* 4037010c12aSrie * The addend field for R_AMD64_DTPMOD64 means nothing. The addend 4047010c12aSrie * is propagated in the corresponding R_AMD64_DTPOFF64 relocation. 4057c478bd9Sstevel@tonic-gate */ 4067c478bd9Sstevel@tonic-gate if (orsp->rel_rtype == R_AMD64_DTPMOD64) 4077c478bd9Sstevel@tonic-gate raddend = 0; 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate relbits = (char *)relosp->os_outdata->d_buf; 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype); 4127c478bd9Sstevel@tonic-gate rea.r_offset = roffset; 4137c478bd9Sstevel@tonic-gate rea.r_addend = raddend; 4145aefb655Srie DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name, 4155aefb655Srie orsp->rel_sname)); 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate /* 4187c478bd9Sstevel@tonic-gate * Assert we haven't walked off the end of our relocation table. 4197c478bd9Sstevel@tonic-gate */ 4207c478bd9Sstevel@tonic-gate assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size); 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate (void) memcpy((relbits + relosp->os_szoutrels), 4237c478bd9Sstevel@tonic-gate (char *)&rea, sizeof (Rela)); 4247c478bd9Sstevel@tonic-gate relosp->os_szoutrels += (Xword)sizeof (Rela); 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate /* 4277c478bd9Sstevel@tonic-gate * Determine if this relocation is against a non-writable, allocatable 4287c478bd9Sstevel@tonic-gate * section. If so we may need to provide a text relocation diagnostic. 4297c478bd9Sstevel@tonic-gate * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually 4307c478bd9Sstevel@tonic-gate * result in modifications to the .got. 4317c478bd9Sstevel@tonic-gate */ 4327c478bd9Sstevel@tonic-gate if (orsp->rel_rtype == R_AMD64_JUMP_SLOT) 4337c478bd9Sstevel@tonic-gate osp = ofl->ofl_osgot; 4347c478bd9Sstevel@tonic-gate 4355aefb655Srie ld_reloc_remain_entry(orsp, osp, ofl); 4367c478bd9Sstevel@tonic-gate return (1); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate /* 4407c478bd9Sstevel@tonic-gate * amd64 Instructions for TLS processing 4417c478bd9Sstevel@tonic-gate */ 442b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie[] = { 4437c478bd9Sstevel@tonic-gate /* 4447c478bd9Sstevel@tonic-gate * 0x00 movq %fs:0, %rax 4457c478bd9Sstevel@tonic-gate */ 4467c478bd9Sstevel@tonic-gate 0x64, 0x48, 0x8b, 0x04, 0x25, 4477c478bd9Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 4487c478bd9Sstevel@tonic-gate /* 4497c478bd9Sstevel@tonic-gate * 0x09 addq x@gottpoff(%rip), %rax 4507c478bd9Sstevel@tonic-gate */ 4517c478bd9Sstevel@tonic-gate 0x48, 0x03, 0x05, 0x00, 0x00, 4527c478bd9Sstevel@tonic-gate 0x00, 0x00 4537c478bd9Sstevel@tonic-gate }; 4547c478bd9Sstevel@tonic-gate 455b3fbe5e6Sseizo static uchar_t tlsinstr_gd_le[] = { 4567c478bd9Sstevel@tonic-gate /* 4577c478bd9Sstevel@tonic-gate * 0x00 movq %fs:0, %rax 4587c478bd9Sstevel@tonic-gate */ 4597c478bd9Sstevel@tonic-gate 0x64, 0x48, 0x8b, 0x04, 0x25, 4607c478bd9Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 4617c478bd9Sstevel@tonic-gate /* 4627c478bd9Sstevel@tonic-gate * 0x09 leaq x@gottpoff(%rip), %rax 4637c478bd9Sstevel@tonic-gate */ 4647c478bd9Sstevel@tonic-gate 0x48, 0x8d, 0x80, 0x00, 0x00, 4657c478bd9Sstevel@tonic-gate 0x00, 0x00 4667c478bd9Sstevel@tonic-gate }; 4677c478bd9Sstevel@tonic-gate 468b3fbe5e6Sseizo static uchar_t tlsinstr_ld_le[] = { 4697c478bd9Sstevel@tonic-gate /* 4707c478bd9Sstevel@tonic-gate * .byte 0x66 4717c478bd9Sstevel@tonic-gate */ 4727c478bd9Sstevel@tonic-gate 0x66, 4737c478bd9Sstevel@tonic-gate /* 4747c478bd9Sstevel@tonic-gate * .byte 0x66 4757c478bd9Sstevel@tonic-gate */ 4767c478bd9Sstevel@tonic-gate 0x66, 4777c478bd9Sstevel@tonic-gate /* 4787c478bd9Sstevel@tonic-gate * .byte 0x66 4797c478bd9Sstevel@tonic-gate */ 4807c478bd9Sstevel@tonic-gate 0x66, 4817c478bd9Sstevel@tonic-gate /* 4827c478bd9Sstevel@tonic-gate * movq %fs:0, %rax 4837c478bd9Sstevel@tonic-gate */ 4847c478bd9Sstevel@tonic-gate 0x64, 0x48, 0x8b, 0x04, 0x25, 4857c478bd9Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00 4867c478bd9Sstevel@tonic-gate }; 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate 4895aefb655Srie static Fixupret 4905aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp) 4917c478bd9Sstevel@tonic-gate { 4927c478bd9Sstevel@tonic-gate Sym_desc *sdp = arsp->rel_sym; 4937c478bd9Sstevel@tonic-gate Word rtype = arsp->rel_rtype; 494b3fbe5e6Sseizo uchar_t *offset; 4957c478bd9Sstevel@tonic-gate 496b3fbe5e6Sseizo offset = (uchar_t *)((uintptr_t)arsp->rel_roffset + 497b3fbe5e6Sseizo (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) + 498b3fbe5e6Sseizo (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf); 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate if (sdp->sd_ref == REF_DYN_NEED) { 5017c478bd9Sstevel@tonic-gate /* 5027c478bd9Sstevel@tonic-gate * IE reference model 5037c478bd9Sstevel@tonic-gate */ 5047c478bd9Sstevel@tonic-gate switch (rtype) { 5057c478bd9Sstevel@tonic-gate case R_AMD64_TLSGD: 5067c478bd9Sstevel@tonic-gate /* 5077c478bd9Sstevel@tonic-gate * GD -> IE 5087c478bd9Sstevel@tonic-gate * 5097c478bd9Sstevel@tonic-gate * Transition: 5107c478bd9Sstevel@tonic-gate * 0x00 .byte 0x66 5117c478bd9Sstevel@tonic-gate * 0x01 leaq x@tlsgd(%rip), %rdi 5127c478bd9Sstevel@tonic-gate * 0x08 .word 0x6666 5137c478bd9Sstevel@tonic-gate * 0x0a rex64 5147c478bd9Sstevel@tonic-gate * 0x0b call __tls_get_addr@plt 5157c478bd9Sstevel@tonic-gate * 0x10 5167c478bd9Sstevel@tonic-gate * To: 5177c478bd9Sstevel@tonic-gate * 0x00 movq %fs:0, %rax 5187c478bd9Sstevel@tonic-gate * 0x09 addq x@gottpoff(%rip), %rax 5197c478bd9Sstevel@tonic-gate * 0x10 5207c478bd9Sstevel@tonic-gate */ 5215aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 522051d39bbSrie R_AMD64_GOTTPOFF, arsp)); 5237c478bd9Sstevel@tonic-gate arsp->rel_rtype = R_AMD64_GOTTPOFF; 5247c478bd9Sstevel@tonic-gate arsp->rel_roffset += 8; 5257c478bd9Sstevel@tonic-gate arsp->rel_raddend = (Sxword)-4; 5265aefb655Srie 5277c478bd9Sstevel@tonic-gate /* 528051d39bbSrie * Adjust 'offset' to beginning of instruction 5297c478bd9Sstevel@tonic-gate * sequence. 5307c478bd9Sstevel@tonic-gate */ 5317c478bd9Sstevel@tonic-gate offset -= 4; 5327c478bd9Sstevel@tonic-gate (void) memcpy(offset, tlsinstr_gd_ie, 5335aefb655Srie sizeof (tlsinstr_gd_ie)); 5347c478bd9Sstevel@tonic-gate return (FIX_RELOC); 5355aefb655Srie 5367c478bd9Sstevel@tonic-gate case R_AMD64_PLT32: 5377c478bd9Sstevel@tonic-gate /* 538051d39bbSrie * Fixup done via the TLS_GD relocation. 5397c478bd9Sstevel@tonic-gate */ 5405aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 541051d39bbSrie R_AMD64_NONE, arsp)); 5427c478bd9Sstevel@tonic-gate return (FIX_DONE); 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate /* 5477c478bd9Sstevel@tonic-gate * LE reference model 5487c478bd9Sstevel@tonic-gate */ 5497c478bd9Sstevel@tonic-gate switch (rtype) { 5507c478bd9Sstevel@tonic-gate case R_AMD64_TLSGD: 5517c478bd9Sstevel@tonic-gate /* 5527c478bd9Sstevel@tonic-gate * GD -> LE 5537c478bd9Sstevel@tonic-gate * 5547c478bd9Sstevel@tonic-gate * Transition: 5557c478bd9Sstevel@tonic-gate * 0x00 .byte 0x66 5567c478bd9Sstevel@tonic-gate * 0x01 leaq x@tlsgd(%rip), %rdi 5577c478bd9Sstevel@tonic-gate * 0x08 .word 0x6666 5587c478bd9Sstevel@tonic-gate * 0x0a rex64 5597c478bd9Sstevel@tonic-gate * 0x0b call __tls_get_addr@plt 5607c478bd9Sstevel@tonic-gate * 0x10 5617c478bd9Sstevel@tonic-gate * To: 5627c478bd9Sstevel@tonic-gate * 0x00 movq %fs:0, %rax 5637c478bd9Sstevel@tonic-gate * 0x09 leaq x@tpoff(%rax), %rax 5647c478bd9Sstevel@tonic-gate * 0x10 5657c478bd9Sstevel@tonic-gate */ 5665aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 567051d39bbSrie R_AMD64_TPOFF32, arsp)); 5687c478bd9Sstevel@tonic-gate arsp->rel_rtype = R_AMD64_TPOFF32; 5697c478bd9Sstevel@tonic-gate arsp->rel_roffset += 8; 5707c478bd9Sstevel@tonic-gate arsp->rel_raddend = 0; 5715aefb655Srie 5727c478bd9Sstevel@tonic-gate /* 573051d39bbSrie * Adjust 'offset' to beginning of instruction sequence. 5747c478bd9Sstevel@tonic-gate */ 5757c478bd9Sstevel@tonic-gate offset -= 4; 5765aefb655Srie (void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le)); 5777c478bd9Sstevel@tonic-gate return (FIX_RELOC); 5785aefb655Srie 5797c478bd9Sstevel@tonic-gate case R_AMD64_GOTTPOFF: 5807c478bd9Sstevel@tonic-gate /* 5817c478bd9Sstevel@tonic-gate * IE -> LE 5827c478bd9Sstevel@tonic-gate * 5837c478bd9Sstevel@tonic-gate * Transition: 5847c478bd9Sstevel@tonic-gate * 0x00 movq %fs:0, %rax 5857c478bd9Sstevel@tonic-gate * 0x09 addq x@gottopoff(%rip), %rax 5867c478bd9Sstevel@tonic-gate * 0x10 5877c478bd9Sstevel@tonic-gate * To: 5887c478bd9Sstevel@tonic-gate * 0x00 movq %fs:0, %rax 5897c478bd9Sstevel@tonic-gate * 0x09 leaq x@tpoff(%rax), %rax 5907c478bd9Sstevel@tonic-gate * 0x10 5917c478bd9Sstevel@tonic-gate */ 592051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 593051d39bbSrie R_AMD64_TPOFF32, arsp)); 5947c478bd9Sstevel@tonic-gate arsp->rel_rtype = R_AMD64_TPOFF32; 5957c478bd9Sstevel@tonic-gate arsp->rel_raddend = 0; 5965aefb655Srie 5977c478bd9Sstevel@tonic-gate /* 598051d39bbSrie * Adjust 'offset' to beginning of instruction sequence. 5997c478bd9Sstevel@tonic-gate */ 6007c478bd9Sstevel@tonic-gate offset -= 12; 6015aefb655Srie 6027c478bd9Sstevel@tonic-gate /* 603051d39bbSrie * Same code sequence used in the GD -> LE transition. 6047c478bd9Sstevel@tonic-gate */ 6055aefb655Srie (void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le)); 6067c478bd9Sstevel@tonic-gate return (FIX_RELOC); 6075aefb655Srie 6087c478bd9Sstevel@tonic-gate case R_AMD64_TLSLD: 6097c478bd9Sstevel@tonic-gate /* 6107c478bd9Sstevel@tonic-gate * LD -> LE 6117c478bd9Sstevel@tonic-gate * 6127c478bd9Sstevel@tonic-gate * Transition 6137c478bd9Sstevel@tonic-gate * 0x00 leaq x1@tlsgd(%rip), %rdi 6147c478bd9Sstevel@tonic-gate * 0x07 call __tls_get_addr@plt 6157c478bd9Sstevel@tonic-gate * 0x0c 6167c478bd9Sstevel@tonic-gate * To: 6177c478bd9Sstevel@tonic-gate * 0x00 .byte 0x66 6187c478bd9Sstevel@tonic-gate * 0x01 .byte 0x66 6197c478bd9Sstevel@tonic-gate * 0x02 .byte 0x66 6207c478bd9Sstevel@tonic-gate * 0x03 movq %fs:0, %rax 6217c478bd9Sstevel@tonic-gate */ 622051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 623051d39bbSrie R_AMD64_NONE, arsp)); 6247c478bd9Sstevel@tonic-gate offset -= 3; 6255aefb655Srie (void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le)); 6267c478bd9Sstevel@tonic-gate return (FIX_DONE); 6275aefb655Srie 6287c478bd9Sstevel@tonic-gate case R_AMD64_DTPOFF32: 6297c478bd9Sstevel@tonic-gate /* 6307c478bd9Sstevel@tonic-gate * LD->LE 6317c478bd9Sstevel@tonic-gate * 6327c478bd9Sstevel@tonic-gate * Transition: 6337c478bd9Sstevel@tonic-gate * 0x00 leaq x1@dtpoff(%rax), %rcx 6347c478bd9Sstevel@tonic-gate * To: 6357c478bd9Sstevel@tonic-gate * 0x00 leaq x1@tpoff(%rax), %rcx 6367c478bd9Sstevel@tonic-gate */ 637051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 638051d39bbSrie R_AMD64_TPOFF32, arsp)); 6397c478bd9Sstevel@tonic-gate arsp->rel_rtype = R_AMD64_TPOFF32; 6407c478bd9Sstevel@tonic-gate arsp->rel_raddend = 0; 6417c478bd9Sstevel@tonic-gate return (FIX_RELOC); 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate 6447c478bd9Sstevel@tonic-gate return (FIX_RELOC); 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate 647ba2be530Sab static uintptr_t 6485aefb655Srie ld_do_activerelocs(Ofl_desc *ofl) 6497c478bd9Sstevel@tonic-gate { 650141040e8Srie Rel_desc *arsp; 651141040e8Srie Rel_cache *rcp; 652*57ef7aa9SRod Evans Aliste idx; 6537c478bd9Sstevel@tonic-gate uintptr_t return_code = 1; 6541d9df23bSab ofl_flag_t flags = ofl->ofl_flags; 6557c478bd9Sstevel@tonic-gate 656*57ef7aa9SRod Evans if (ofl->ofl_actrels) 6577010c12aSrie DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml)); 6587010c12aSrie 6597c478bd9Sstevel@tonic-gate /* 660141040e8Srie * Process active relocations. 6617c478bd9Sstevel@tonic-gate */ 662*57ef7aa9SRod Evans for (APLIST_TRAVERSE(ofl->ofl_actrels, idx, rcp)) { 6637c478bd9Sstevel@tonic-gate /* LINTED */ 6647c478bd9Sstevel@tonic-gate for (arsp = (Rel_desc *)(rcp + 1); 6657c478bd9Sstevel@tonic-gate arsp < rcp->rc_free; arsp++) { 666b3fbe5e6Sseizo uchar_t *addr; 6677c478bd9Sstevel@tonic-gate Xword value; 6687c478bd9Sstevel@tonic-gate Sym_desc *sdp; 6697c478bd9Sstevel@tonic-gate const char *ifl_name; 6707c478bd9Sstevel@tonic-gate Xword refaddr; 6717c478bd9Sstevel@tonic-gate int moved = 0; 6727c478bd9Sstevel@tonic-gate Gotref gref; 6737c478bd9Sstevel@tonic-gate 6747c478bd9Sstevel@tonic-gate /* 6757c478bd9Sstevel@tonic-gate * If the section this relocation is against has been 6767c478bd9Sstevel@tonic-gate * discarded (-zignore), then discard (skip) the 6777c478bd9Sstevel@tonic-gate * relocation itself. 6787c478bd9Sstevel@tonic-gate */ 6797c478bd9Sstevel@tonic-gate if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) && 6807c478bd9Sstevel@tonic-gate ((arsp->rel_flags & 6817c478bd9Sstevel@tonic-gate (FLG_REL_GOT | FLG_REL_BSS | 6827c478bd9Sstevel@tonic-gate FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) { 6835aefb655Srie DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, 6845aefb655Srie M_MACH, arsp)); 6857c478bd9Sstevel@tonic-gate continue; 6867c478bd9Sstevel@tonic-gate } 6877c478bd9Sstevel@tonic-gate 6887c478bd9Sstevel@tonic-gate /* 689d2d5cf7cSAli Bahrami * We determine what the 'got reference' 6907c478bd9Sstevel@tonic-gate * model (if required) is at this point. This 6917c478bd9Sstevel@tonic-gate * needs to be done before tls_fixup() since 6927c478bd9Sstevel@tonic-gate * it may 'transition' our instructions. 6937c478bd9Sstevel@tonic-gate * 6947c478bd9Sstevel@tonic-gate * The got table entries have already been assigned, 6957c478bd9Sstevel@tonic-gate * and we bind to those initial entries. 6967c478bd9Sstevel@tonic-gate */ 6977c478bd9Sstevel@tonic-gate if (arsp->rel_flags & FLG_REL_DTLS) 6987c478bd9Sstevel@tonic-gate gref = GOT_REF_TLSGD; 6997c478bd9Sstevel@tonic-gate else if (arsp->rel_flags & FLG_REL_MTLS) 7007c478bd9Sstevel@tonic-gate gref = GOT_REF_TLSLD; 7017c478bd9Sstevel@tonic-gate else if (arsp->rel_flags & FLG_REL_STLS) 7027c478bd9Sstevel@tonic-gate gref = GOT_REF_TLSIE; 7037c478bd9Sstevel@tonic-gate else 7047c478bd9Sstevel@tonic-gate gref = GOT_REF_GENERIC; 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate /* 7077c478bd9Sstevel@tonic-gate * Perform any required TLS fixups. 7087c478bd9Sstevel@tonic-gate */ 7097c478bd9Sstevel@tonic-gate if (arsp->rel_flags & FLG_REL_TLSFIX) { 7107c478bd9Sstevel@tonic-gate Fixupret ret; 711141040e8Srie 7125aefb655Srie if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR) 7137c478bd9Sstevel@tonic-gate return (S_ERROR); 7147c478bd9Sstevel@tonic-gate if (ret == FIX_DONE) 7157c478bd9Sstevel@tonic-gate continue; 7167c478bd9Sstevel@tonic-gate } 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate /* 7197c478bd9Sstevel@tonic-gate * If this is a relocation against a move table, or 7207c478bd9Sstevel@tonic-gate * expanded move table, adjust the relocation entries. 7217c478bd9Sstevel@tonic-gate */ 7227c478bd9Sstevel@tonic-gate if (arsp->rel_move) 7235aefb655Srie ld_adj_movereloc(ofl, arsp); 7247c478bd9Sstevel@tonic-gate 7257c478bd9Sstevel@tonic-gate sdp = arsp->rel_sym; 7267c478bd9Sstevel@tonic-gate refaddr = arsp->rel_roffset + 7277c478bd9Sstevel@tonic-gate (Off)_elf_getxoff(arsp->rel_isdesc->is_indata); 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate if ((arsp->rel_flags & FLG_REL_CLVAL) || 7307c478bd9Sstevel@tonic-gate (arsp->rel_flags & FLG_REL_GOTCL)) 7317c478bd9Sstevel@tonic-gate value = 0; 7327c478bd9Sstevel@tonic-gate else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == 7337c478bd9Sstevel@tonic-gate STT_SECTION) { 734141040e8Srie Sym_desc *sym; 7357c478bd9Sstevel@tonic-gate 7367c478bd9Sstevel@tonic-gate /* 7377c478bd9Sstevel@tonic-gate * The value for a symbol pointing to a SECTION 7387c478bd9Sstevel@tonic-gate * is based off of that sections position. 7397c478bd9Sstevel@tonic-gate */ 7407c478bd9Sstevel@tonic-gate if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 7417c478bd9Sstevel@tonic-gate /* LINTED */ 742bf2f215aSAli Bahrami (sym = ld_am_I_partial(arsp, 743bf2f215aSAli Bahrami arsp->rel_raddend))) { 7447c478bd9Sstevel@tonic-gate /* 745b26cc8daSAli Bahrami * The symbol was moved, so adjust 746b26cc8daSAli Bahrami * the value relative to the new 747b26cc8daSAli Bahrami * section. 7487c478bd9Sstevel@tonic-gate */ 7497c478bd9Sstevel@tonic-gate value = sym->sd_sym->st_value; 7507c478bd9Sstevel@tonic-gate moved = 1; 751b26cc8daSAli Bahrami 752b26cc8daSAli Bahrami /* 753b26cc8daSAli Bahrami * The original raddend covers the 754b26cc8daSAli Bahrami * displacement from the section start 755b26cc8daSAli Bahrami * to the desired address. The value 756b26cc8daSAli Bahrami * computed above gets us from the 757b26cc8daSAli Bahrami * section start to the start of the 758b26cc8daSAli Bahrami * symbol range. Adjust the old raddend 759b26cc8daSAli Bahrami * to remove the offset from section 760b26cc8daSAli Bahrami * start to symbol start, leaving the 761b26cc8daSAli Bahrami * displacement within the range of 762b26cc8daSAli Bahrami * the symbol. 763b26cc8daSAli Bahrami */ 764bf2f215aSAli Bahrami arsp->rel_raddend -= 765bf2f215aSAli Bahrami sym->sd_osym->st_value; 7667c478bd9Sstevel@tonic-gate } else { 767141040e8Srie value = _elf_getxoff( 768141040e8Srie sdp->sd_isc->is_indata); 7697c478bd9Sstevel@tonic-gate if (sdp->sd_isc->is_shdr->sh_flags & 7707c478bd9Sstevel@tonic-gate SHF_ALLOC) 771de777a60Sab value += 772de777a60Sab sdp->sd_isc->is_osdesc-> 773de777a60Sab os_shdr->sh_addr; 7747c478bd9Sstevel@tonic-gate } 7757c478bd9Sstevel@tonic-gate if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS) 7767c478bd9Sstevel@tonic-gate value -= ofl->ofl_tlsphdr->p_vaddr; 7772926dd2eSrie 7782926dd2eSrie } else if (IS_SIZE(arsp->rel_rtype)) { 7792926dd2eSrie /* 7802926dd2eSrie * Size relocations require the symbols size. 7812926dd2eSrie */ 7822926dd2eSrie value = sdp->sd_sym->st_size; 783141040e8Srie } else { 7847c478bd9Sstevel@tonic-gate /* 7857010c12aSrie * Else the value is the symbols value. 7867c478bd9Sstevel@tonic-gate */ 7877c478bd9Sstevel@tonic-gate value = sdp->sd_sym->st_value; 788141040e8Srie } 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate /* 7917c478bd9Sstevel@tonic-gate * Relocation against the GLOBAL_OFFSET_TABLE. 7927c478bd9Sstevel@tonic-gate */ 7937c478bd9Sstevel@tonic-gate if (arsp->rel_flags & FLG_REL_GOT) 7947c478bd9Sstevel@tonic-gate arsp->rel_osdesc = ofl->ofl_osgot; 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate /* 7977c478bd9Sstevel@tonic-gate * If loadable and not producing a relocatable object 7987c478bd9Sstevel@tonic-gate * add the sections virtual address to the reference 7997c478bd9Sstevel@tonic-gate * address. 8007c478bd9Sstevel@tonic-gate */ 8017c478bd9Sstevel@tonic-gate if ((arsp->rel_flags & FLG_REL_LOAD) && 802141040e8Srie ((flags & FLG_OF_RELOBJ) == 0)) 8037c478bd9Sstevel@tonic-gate refaddr += arsp->rel_isdesc->is_osdesc-> 8047c478bd9Sstevel@tonic-gate os_shdr->sh_addr; 8057c478bd9Sstevel@tonic-gate 8067c478bd9Sstevel@tonic-gate /* 8077c478bd9Sstevel@tonic-gate * If this entry has a PLT assigned to it, it's 8087c478bd9Sstevel@tonic-gate * value is actually the address of the PLT (and 8097c478bd9Sstevel@tonic-gate * not the address of the function). 8107c478bd9Sstevel@tonic-gate */ 8117c478bd9Sstevel@tonic-gate if (IS_PLT(arsp->rel_rtype)) { 8127c478bd9Sstevel@tonic-gate if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx) 8135aefb655Srie value = ld_calc_plt_addr(sdp, ofl); 8147c478bd9Sstevel@tonic-gate } 8157c478bd9Sstevel@tonic-gate 8167c478bd9Sstevel@tonic-gate /* 8177c478bd9Sstevel@tonic-gate * Add relocations addend to value. Add extra 8187c478bd9Sstevel@tonic-gate * relocation addend if needed. 8197c478bd9Sstevel@tonic-gate * 8207c478bd9Sstevel@tonic-gate * Note: for GOT relative relocations on amd64 8217c478bd9Sstevel@tonic-gate * we discard the addend. It was relevant 8227c478bd9Sstevel@tonic-gate * to the reference - not to the data item 8237c478bd9Sstevel@tonic-gate * being referenced (ie: that -4 thing). 8247c478bd9Sstevel@tonic-gate */ 8257c478bd9Sstevel@tonic-gate if ((arsp->rel_flags & FLG_REL_GOT) == 0) 826bf2f215aSAli Bahrami value += arsp->rel_raddend; 8277c478bd9Sstevel@tonic-gate 828141040e8Srie /* 829141040e8Srie * Determine whether the value needs further adjustment. 830141040e8Srie * Filter through the attributes of the relocation to 831141040e8Srie * determine what adjustment is required. Note, many 832141040e8Srie * of the following cases are only applicable when a 833141040e8Srie * .got is present. As a .got is not generated when a 834141040e8Srie * relocatable object is being built, any adjustments 835141040e8Srie * that require a .got need to be skipped. 836141040e8Srie */ 837141040e8Srie if ((arsp->rel_flags & FLG_REL_GOT) && 838141040e8Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8397c478bd9Sstevel@tonic-gate Xword R1addr; 8407c478bd9Sstevel@tonic-gate uintptr_t R2addr; 8417c478bd9Sstevel@tonic-gate Word gotndx; 8427c478bd9Sstevel@tonic-gate Gotndx *gnp; 8437c478bd9Sstevel@tonic-gate 8447c478bd9Sstevel@tonic-gate /* 8457c478bd9Sstevel@tonic-gate * Perform relocation against GOT table. Since 8467c478bd9Sstevel@tonic-gate * this doesn't fit exactly into a relocation 8477c478bd9Sstevel@tonic-gate * we place the appropriate byte in the GOT 8487c478bd9Sstevel@tonic-gate * directly 8497c478bd9Sstevel@tonic-gate * 8507c478bd9Sstevel@tonic-gate * Calculate offset into GOT at which to apply 8517c478bd9Sstevel@tonic-gate * the relocation. 8527c478bd9Sstevel@tonic-gate */ 853*57ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, 8547c478bd9Sstevel@tonic-gate ofl, arsp); 8557c478bd9Sstevel@tonic-gate assert(gnp); 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate if (arsp->rel_rtype == R_AMD64_DTPOFF64) 8587c478bd9Sstevel@tonic-gate gotndx = gnp->gn_gotndx + 1; 8597c478bd9Sstevel@tonic-gate else 8607c478bd9Sstevel@tonic-gate gotndx = gnp->gn_gotndx; 8617c478bd9Sstevel@tonic-gate 8627c478bd9Sstevel@tonic-gate R1addr = (Xword)(gotndx * M_GOT_ENTSIZE); 8637c478bd9Sstevel@tonic-gate 8647c478bd9Sstevel@tonic-gate /* 8657c478bd9Sstevel@tonic-gate * Add the GOTs data's offset. 8667c478bd9Sstevel@tonic-gate */ 8677c478bd9Sstevel@tonic-gate R2addr = R1addr + (uintptr_t) 8687c478bd9Sstevel@tonic-gate arsp->rel_osdesc->os_outdata->d_buf; 8697c478bd9Sstevel@tonic-gate 8705aefb655Srie DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, 8715aefb655Srie ELF_DBG_LD, M_MACH, SHT_RELA, 8727c478bd9Sstevel@tonic-gate arsp->rel_rtype, R1addr, value, 8737c478bd9Sstevel@tonic-gate arsp->rel_sname, arsp->rel_osdesc)); 8747c478bd9Sstevel@tonic-gate 8757c478bd9Sstevel@tonic-gate /* 8767c478bd9Sstevel@tonic-gate * And do it. 8777c478bd9Sstevel@tonic-gate */ 878f3324781Sab if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) 879f3324781Sab *(Xword *)R2addr = 880ba2be530Sab ld_bswap_Xword(value); 881f3324781Sab else 882f3324781Sab *(Xword *)R2addr = value; 8837c478bd9Sstevel@tonic-gate continue; 8847c478bd9Sstevel@tonic-gate 885141040e8Srie } else if (IS_GOT_BASED(arsp->rel_rtype) && 886141040e8Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8877c478bd9Sstevel@tonic-gate value -= ofl->ofl_osgot->os_shdr->sh_addr; 888141040e8Srie 889141040e8Srie } else if (IS_GOTPCREL(arsp->rel_rtype) && 890141040e8Srie ((flags & FLG_OF_RELOBJ) == 0)) { 891141040e8Srie Gotndx *gnp; 892141040e8Srie 8937c478bd9Sstevel@tonic-gate /* 8947c478bd9Sstevel@tonic-gate * Calculation: 8957c478bd9Sstevel@tonic-gate * G + GOT + A - P 8967c478bd9Sstevel@tonic-gate */ 897*57ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 8987c478bd9Sstevel@tonic-gate gref, ofl, arsp); 899141040e8Srie assert(gnp); 9007c478bd9Sstevel@tonic-gate value = (Xword)(ofl->ofl_osgot->os_shdr-> 9017c478bd9Sstevel@tonic-gate sh_addr) + ((Xword)gnp->gn_gotndx * 9027c478bd9Sstevel@tonic-gate M_GOT_ENTSIZE) + arsp->rel_raddend - 9037c478bd9Sstevel@tonic-gate refaddr; 904141040e8Srie 905141040e8Srie } else if (IS_GOT_PC(arsp->rel_rtype) && 906141040e8Srie ((flags & FLG_OF_RELOBJ) == 0)) { 907141040e8Srie value = (Xword)(ofl->ofl_osgot->os_shdr-> 90854d82594Sseizo sh_addr) - refaddr + arsp->rel_raddend; 909141040e8Srie 9107c478bd9Sstevel@tonic-gate } else if ((IS_PC_RELATIVE(arsp->rel_rtype)) && 911141040e8Srie (((flags & FLG_OF_RELOBJ) == 0) || 9127c478bd9Sstevel@tonic-gate (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) { 9137c478bd9Sstevel@tonic-gate value -= refaddr; 914141040e8Srie 9157c478bd9Sstevel@tonic-gate } else if (IS_TLS_INS(arsp->rel_rtype) && 916141040e8Srie IS_GOT_RELATIVE(arsp->rel_rtype) && 917141040e8Srie ((flags & FLG_OF_RELOBJ) == 0)) { 9187c478bd9Sstevel@tonic-gate Gotndx *gnp; 9197c478bd9Sstevel@tonic-gate 920*57ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, 9217c478bd9Sstevel@tonic-gate ofl, arsp); 922141040e8Srie assert(gnp); 9237c478bd9Sstevel@tonic-gate value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 924141040e8Srie 925141040e8Srie } else if (IS_GOT_RELATIVE(arsp->rel_rtype) && 926141040e8Srie ((flags & FLG_OF_RELOBJ) == 0)) { 927141040e8Srie Gotndx *gnp; 9287c478bd9Sstevel@tonic-gate 929*57ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 9307c478bd9Sstevel@tonic-gate gref, ofl, arsp); 9317c478bd9Sstevel@tonic-gate assert(gnp); 9327c478bd9Sstevel@tonic-gate value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 933141040e8Srie 934141040e8Srie } else if ((arsp->rel_flags & FLG_REL_STLS) && 935141040e8Srie ((flags & FLG_OF_RELOBJ) == 0)) { 9367c478bd9Sstevel@tonic-gate Xword tlsstatsize; 937141040e8Srie 9387c478bd9Sstevel@tonic-gate /* 9397c478bd9Sstevel@tonic-gate * This is the LE TLS reference model. Static 9407c478bd9Sstevel@tonic-gate * offset is hard-coded. 9417c478bd9Sstevel@tonic-gate */ 942141040e8Srie tlsstatsize = 943141040e8Srie S_ROUND(ofl->ofl_tlsphdr->p_memsz, 9447c478bd9Sstevel@tonic-gate M_TLSSTATALIGN); 9457c478bd9Sstevel@tonic-gate value = tlsstatsize - value; 946141040e8Srie 9477c478bd9Sstevel@tonic-gate /* 948141040e8Srie * Since this code is fixed up, it assumes a 949141040e8Srie * negative offset that can be added to the 950141040e8Srie * thread pointer. 9517c478bd9Sstevel@tonic-gate */ 9527c478bd9Sstevel@tonic-gate if (arsp->rel_rtype == R_AMD64_TPOFF32) 9537c478bd9Sstevel@tonic-gate value = -value; 9547c478bd9Sstevel@tonic-gate } 9557c478bd9Sstevel@tonic-gate 9567c478bd9Sstevel@tonic-gate if (arsp->rel_isdesc->is_file) 9577c478bd9Sstevel@tonic-gate ifl_name = arsp->rel_isdesc->is_file->ifl_name; 9587c478bd9Sstevel@tonic-gate else 9597c478bd9Sstevel@tonic-gate ifl_name = MSG_INTL(MSG_STR_NULL); 9607c478bd9Sstevel@tonic-gate 9617c478bd9Sstevel@tonic-gate /* 9627c478bd9Sstevel@tonic-gate * Make sure we have data to relocate. Compiler and 9637c478bd9Sstevel@tonic-gate * assembler developers have been known to generate 9647c478bd9Sstevel@tonic-gate * relocations against invalid sections (normally .bss), 9657c478bd9Sstevel@tonic-gate * so for their benefit give them sufficient information 9667c478bd9Sstevel@tonic-gate * to help analyze the problem. End users should never 9677c478bd9Sstevel@tonic-gate * see this. 9687c478bd9Sstevel@tonic-gate */ 9697c478bd9Sstevel@tonic-gate if (arsp->rel_isdesc->is_indata->d_buf == 0) { 970de777a60Sab Conv_inv_buf_t inv_buf; 971de777a60Sab 9725aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 9735aefb655Srie MSG_INTL(MSG_REL_EMPTYSEC), 974de777a60Sab conv_reloc_amd64_type(arsp->rel_rtype, 975de777a60Sab 0, &inv_buf), ifl_name, 976de777a60Sab demangle(arsp->rel_sname), 9777c478bd9Sstevel@tonic-gate arsp->rel_isdesc->is_name); 9787c478bd9Sstevel@tonic-gate return (S_ERROR); 9797c478bd9Sstevel@tonic-gate } 9807c478bd9Sstevel@tonic-gate 9817c478bd9Sstevel@tonic-gate /* 9827c478bd9Sstevel@tonic-gate * Get the address of the data item we need to modify. 9837c478bd9Sstevel@tonic-gate */ 984b3fbe5e6Sseizo addr = (uchar_t *)((uintptr_t)arsp->rel_roffset + 985b3fbe5e6Sseizo (uintptr_t)_elf_getxoff(arsp->rel_isdesc-> 986b3fbe5e6Sseizo is_indata)); 9877c478bd9Sstevel@tonic-gate 9885aefb655Srie DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD, 9895aefb655Srie M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr), 9905aefb655Srie value, arsp->rel_sname, arsp->rel_osdesc)); 9917c478bd9Sstevel@tonic-gate addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf; 9927c478bd9Sstevel@tonic-gate 9935aefb655Srie if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) > 9947c478bd9Sstevel@tonic-gate ofl->ofl_size) || (arsp->rel_roffset > 9957c478bd9Sstevel@tonic-gate arsp->rel_osdesc->os_shdr->sh_size)) { 996de777a60Sab int class; 997de777a60Sab Conv_inv_buf_t inv_buf; 9987c478bd9Sstevel@tonic-gate 999b3fbe5e6Sseizo if (((uintptr_t)addr - 10005aefb655Srie (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size) 10017c478bd9Sstevel@tonic-gate class = ERR_FATAL; 10027c478bd9Sstevel@tonic-gate else 10037c478bd9Sstevel@tonic-gate class = ERR_WARNING; 10047c478bd9Sstevel@tonic-gate 10055aefb655Srie eprintf(ofl->ofl_lml, class, 10065aefb655Srie MSG_INTL(MSG_REL_INVALOFFSET), 1007de777a60Sab conv_reloc_amd64_type(arsp->rel_rtype, 1008de777a60Sab 0, &inv_buf), ifl_name, 1009de777a60Sab arsp->rel_isdesc->is_name, 10107c478bd9Sstevel@tonic-gate demangle(arsp->rel_sname), 10117c478bd9Sstevel@tonic-gate EC_ADDR((uintptr_t)addr - 10125aefb655Srie (uintptr_t)ofl->ofl_nehdr)); 10137c478bd9Sstevel@tonic-gate 10147c478bd9Sstevel@tonic-gate if (class == ERR_FATAL) { 10157c478bd9Sstevel@tonic-gate return_code = S_ERROR; 10167c478bd9Sstevel@tonic-gate continue; 10177c478bd9Sstevel@tonic-gate } 10187c478bd9Sstevel@tonic-gate } 10197c478bd9Sstevel@tonic-gate 10207c478bd9Sstevel@tonic-gate /* 10217c478bd9Sstevel@tonic-gate * The relocation is additive. Ignore the previous 10227c478bd9Sstevel@tonic-gate * symbol value if this local partial symbol is 10237c478bd9Sstevel@tonic-gate * expanded. 10247c478bd9Sstevel@tonic-gate */ 10257c478bd9Sstevel@tonic-gate if (moved) 10267c478bd9Sstevel@tonic-gate value -= *addr; 10277c478bd9Sstevel@tonic-gate 10287c478bd9Sstevel@tonic-gate /* 1029f3324781Sab * If '-z noreloc' is specified - skip the do_reloc_ld 10307c478bd9Sstevel@tonic-gate * stage. 10317c478bd9Sstevel@tonic-gate */ 1032f3324781Sab if (OFL_DO_RELOC(ofl)) { 1033f3324781Sab /* 1034f3324781Sab * If this is a PROGBITS section and the 1035f3324781Sab * running linker has a different byte order 1036f3324781Sab * than the target host, tell do_reloc_ld() 1037f3324781Sab * to swap bytes. 1038f3324781Sab */ 1039f3324781Sab if (do_reloc_ld((uchar_t)arsp->rel_rtype, 10405aefb655Srie addr, &value, arsp->rel_sname, ifl_name, 1041f3324781Sab OFL_SWAP_RELOC_DATA(ofl, arsp), 10425aefb655Srie ofl->ofl_lml) == 0) 10437c478bd9Sstevel@tonic-gate return_code = S_ERROR; 10447c478bd9Sstevel@tonic-gate } 10457c478bd9Sstevel@tonic-gate } 10467c478bd9Sstevel@tonic-gate } 10477c478bd9Sstevel@tonic-gate return (return_code); 10487c478bd9Sstevel@tonic-gate } 10497c478bd9Sstevel@tonic-gate 1050ba2be530Sab static uintptr_t 10515aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl) 10527c478bd9Sstevel@tonic-gate { 1053141040e8Srie Rel_desc *orsp; 1054141040e8Srie Rel_cache *rcp; 1055141040e8Srie Sym_desc *sdp = rsp->rel_sym; 1056*57ef7aa9SRod Evans static size_t nextsize = 0; 10577c478bd9Sstevel@tonic-gate 10587c478bd9Sstevel@tonic-gate /* 10597c478bd9Sstevel@tonic-gate * Static executables *do not* want any relocations against them. 10607c478bd9Sstevel@tonic-gate * Since our engine still creates relocations against a WEAK UNDEFINED 10617c478bd9Sstevel@tonic-gate * symbol in a static executable, it's best to disable them here 10627c478bd9Sstevel@tonic-gate * instead of through out the relocation code. 10637c478bd9Sstevel@tonic-gate */ 10647c478bd9Sstevel@tonic-gate if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) == 10657c478bd9Sstevel@tonic-gate (FLG_OF_STATIC | FLG_OF_EXEC)) 10667c478bd9Sstevel@tonic-gate return (1); 10677c478bd9Sstevel@tonic-gate 10687c478bd9Sstevel@tonic-gate /* 1069*57ef7aa9SRod Evans * Obtain the new available relocation cache entry. 10707c478bd9Sstevel@tonic-gate */ 1071*57ef7aa9SRod Evans if ((rcp = ld_add_rel_cache(ofl, &ofl->ofl_outrels, &nextsize, 1072*57ef7aa9SRod Evans REL_LOIDESCNO, REL_HOIDESCNO)) == (Rel_cache *)S_ERROR) 1073*57ef7aa9SRod Evans return (S_ERROR); 10747c478bd9Sstevel@tonic-gate 1075*57ef7aa9SRod Evans orsp = rcp->rc_free; 10767c478bd9Sstevel@tonic-gate 10777c478bd9Sstevel@tonic-gate /* 10787c478bd9Sstevel@tonic-gate * If we are adding a output relocation against a section 10797c478bd9Sstevel@tonic-gate * symbol (non-RELATIVE) then mark that section. These sections 10807c478bd9Sstevel@tonic-gate * will be added to the .dynsym symbol table. 10817c478bd9Sstevel@tonic-gate */ 10827c478bd9Sstevel@tonic-gate if (sdp && (rsp->rel_rtype != M_R_RELATIVE) && 10837c478bd9Sstevel@tonic-gate ((flags & FLG_REL_SCNNDX) || 10847c478bd9Sstevel@tonic-gate (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) { 10857c478bd9Sstevel@tonic-gate 10867c478bd9Sstevel@tonic-gate /* 10877c478bd9Sstevel@tonic-gate * If this is a COMMON symbol - no output section 10887c478bd9Sstevel@tonic-gate * exists yet - (it's created as part of sym_validate()). 10897c478bd9Sstevel@tonic-gate * So - we mark here that when it's created it should 10907c478bd9Sstevel@tonic-gate * be tagged with the FLG_OS_OUTREL flag. 10917c478bd9Sstevel@tonic-gate */ 10927c478bd9Sstevel@tonic-gate if ((sdp->sd_flags & FLG_SY_SPECSEC) && 10930bc07c75Srie (sdp->sd_sym->st_shndx == SHN_COMMON)) { 10947c478bd9Sstevel@tonic-gate if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS) 10957c478bd9Sstevel@tonic-gate ofl->ofl_flags1 |= FLG_OF1_BSSOREL; 10967c478bd9Sstevel@tonic-gate else 10977c478bd9Sstevel@tonic-gate ofl->ofl_flags1 |= FLG_OF1_TLSOREL; 1098141040e8Srie } else { 1099141040e8Srie Os_desc *osp = sdp->sd_isc->is_osdesc; 11007c478bd9Sstevel@tonic-gate 1101c1c6f601Srie if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) { 11027c478bd9Sstevel@tonic-gate ofl->ofl_dynshdrcnt++; 11037c478bd9Sstevel@tonic-gate osp->os_flags |= FLG_OS_OUTREL; 11047c478bd9Sstevel@tonic-gate } 11057c478bd9Sstevel@tonic-gate } 11067c478bd9Sstevel@tonic-gate } 11077c478bd9Sstevel@tonic-gate 11087c478bd9Sstevel@tonic-gate *orsp = *rsp; 11097c478bd9Sstevel@tonic-gate orsp->rel_flags |= flags; 11107c478bd9Sstevel@tonic-gate 11117c478bd9Sstevel@tonic-gate rcp->rc_free++; 11127c478bd9Sstevel@tonic-gate ofl->ofl_outrelscnt++; 11137c478bd9Sstevel@tonic-gate 11147c478bd9Sstevel@tonic-gate if (flags & FLG_REL_GOT) 11157c478bd9Sstevel@tonic-gate ofl->ofl_relocgotsz += (Xword)sizeof (Rela); 11167c478bd9Sstevel@tonic-gate else if (flags & FLG_REL_PLT) 11177c478bd9Sstevel@tonic-gate ofl->ofl_relocpltsz += (Xword)sizeof (Rela); 11187c478bd9Sstevel@tonic-gate else if (flags & FLG_REL_BSS) 11197c478bd9Sstevel@tonic-gate ofl->ofl_relocbsssz += (Xword)sizeof (Rela); 11207c478bd9Sstevel@tonic-gate else if (flags & FLG_REL_NOINFO) 11217c478bd9Sstevel@tonic-gate ofl->ofl_relocrelsz += (Xword)sizeof (Rela); 11227c478bd9Sstevel@tonic-gate else 11237c478bd9Sstevel@tonic-gate orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela); 11247c478bd9Sstevel@tonic-gate 11257c478bd9Sstevel@tonic-gate if (orsp->rel_rtype == M_R_RELATIVE) 11267c478bd9Sstevel@tonic-gate ofl->ofl_relocrelcnt++; 11277c478bd9Sstevel@tonic-gate 11287c478bd9Sstevel@tonic-gate /* 11297c478bd9Sstevel@tonic-gate * We don't perform sorting on PLT relocations because 11307c478bd9Sstevel@tonic-gate * they have already been assigned a PLT index and if we 11317c478bd9Sstevel@tonic-gate * were to sort them we would have to re-assign the plt indexes. 11327c478bd9Sstevel@tonic-gate */ 11337c478bd9Sstevel@tonic-gate if (!(flags & FLG_REL_PLT)) 11347c478bd9Sstevel@tonic-gate ofl->ofl_reloccnt++; 11357c478bd9Sstevel@tonic-gate 1136141040e8Srie /* 1137141040e8Srie * Insure a GLOBAL_OFFSET_TABLE is generated if required. 1138141040e8Srie */ 1139141040e8Srie if (IS_GOT_REQUIRED(orsp->rel_rtype)) 1140141040e8Srie ofl->ofl_flags |= FLG_OF_BLDGOT; 1141141040e8Srie 11427c478bd9Sstevel@tonic-gate /* 11437c478bd9Sstevel@tonic-gate * Identify and possibly warn of a displacement relocation. 11447c478bd9Sstevel@tonic-gate */ 11457c478bd9Sstevel@tonic-gate if (orsp->rel_flags & FLG_REL_DISP) { 11467c478bd9Sstevel@tonic-gate ofl->ofl_dtflags_1 |= DF_1_DISPRELPND; 11477c478bd9Sstevel@tonic-gate 11487c478bd9Sstevel@tonic-gate if (ofl->ofl_flags & FLG_OF_VERBOSE) 11495aefb655Srie ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl); 11507c478bd9Sstevel@tonic-gate } 11515aefb655Srie DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA, 11525aefb655Srie M_MACH, orsp)); 11537c478bd9Sstevel@tonic-gate return (1); 11547c478bd9Sstevel@tonic-gate } 11557c478bd9Sstevel@tonic-gate 11567c478bd9Sstevel@tonic-gate /* 11577c478bd9Sstevel@tonic-gate * process relocation for a LOCAL symbol 11587c478bd9Sstevel@tonic-gate */ 1159ba2be530Sab static uintptr_t 11605aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl) 11617c478bd9Sstevel@tonic-gate { 11621d9df23bSab ofl_flag_t flags = ofl->ofl_flags; 11637c478bd9Sstevel@tonic-gate Sym_desc *sdp = rsp->rel_sym; 11640bc07c75Srie Word shndx = sdp->sd_sym->st_shndx; 11657c478bd9Sstevel@tonic-gate Word ortype = rsp->rel_rtype; 11667c478bd9Sstevel@tonic-gate 11677c478bd9Sstevel@tonic-gate /* 11687c478bd9Sstevel@tonic-gate * if ((shared object) and (not pc relative relocation) and 11697c478bd9Sstevel@tonic-gate * (not against ABS symbol)) 11707c478bd9Sstevel@tonic-gate * then 11717c478bd9Sstevel@tonic-gate * build R_AMD64_RELATIVE 11727c478bd9Sstevel@tonic-gate * fi 11737c478bd9Sstevel@tonic-gate */ 11747c478bd9Sstevel@tonic-gate if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) && 11752926dd2eSrie !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) && 11767c478bd9Sstevel@tonic-gate !(IS_GOT_BASED(rsp->rel_rtype)) && 11777c478bd9Sstevel@tonic-gate !(rsp->rel_isdesc != NULL && 11787c478bd9Sstevel@tonic-gate (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) && 11797c478bd9Sstevel@tonic-gate (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) || 11807c478bd9Sstevel@tonic-gate (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) { 11817c478bd9Sstevel@tonic-gate 11827c478bd9Sstevel@tonic-gate /* 11837c478bd9Sstevel@tonic-gate * R_AMD64_RELATIVE updates a 64bit address, if this 11847c478bd9Sstevel@tonic-gate * relocation isn't a 64bit binding then we can not 11857c478bd9Sstevel@tonic-gate * simplify it to a RELATIVE relocation. 11867c478bd9Sstevel@tonic-gate */ 11877c478bd9Sstevel@tonic-gate if (reloc_table[ortype].re_fsize != sizeof (Addr)) { 11881d9df23bSab return (ld_add_outrel(0, rsp, ofl)); 11897c478bd9Sstevel@tonic-gate } 11907c478bd9Sstevel@tonic-gate 11917c478bd9Sstevel@tonic-gate rsp->rel_rtype = R_AMD64_RELATIVE; 11925aefb655Srie if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR) 11937c478bd9Sstevel@tonic-gate return (S_ERROR); 11947c478bd9Sstevel@tonic-gate rsp->rel_rtype = ortype; 11957c478bd9Sstevel@tonic-gate return (1); 11967c478bd9Sstevel@tonic-gate } 11977c478bd9Sstevel@tonic-gate 1198b3fbe5e6Sseizo /* 1199b3fbe5e6Sseizo * If the relocation is against a 'non-allocatable' section 1200b3fbe5e6Sseizo * and we can not resolve it now - then give a warning 1201b3fbe5e6Sseizo * message. 1202b3fbe5e6Sseizo * 1203b3fbe5e6Sseizo * We can not resolve the symbol if either: 1204b3fbe5e6Sseizo * a) it's undefined 1205b3fbe5e6Sseizo * b) it's defined in a shared library and a 1206b3fbe5e6Sseizo * COPY relocation hasn't moved it to the executable 1207b3fbe5e6Sseizo * 1208b3fbe5e6Sseizo * Note: because we process all of the relocations against the 1209b3fbe5e6Sseizo * text segment before any others - we know whether 1210b3fbe5e6Sseizo * or not a copy relocation will be generated before 1211b3fbe5e6Sseizo * we get here (see reloc_init()->reloc_segments()). 1212b3fbe5e6Sseizo */ 12137c478bd9Sstevel@tonic-gate if (!(rsp->rel_flags & FLG_REL_LOAD) && 1214b3fbe5e6Sseizo ((shndx == SHN_UNDEF) || 1215b3fbe5e6Sseizo ((sdp->sd_ref == REF_DYN_NEED) && 1216b3fbe5e6Sseizo ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) { 1217de777a60Sab Conv_inv_buf_t inv_buf; 1218de777a60Sab 1219b3fbe5e6Sseizo /* 1220b3fbe5e6Sseizo * If the relocation is against a SHT_SUNW_ANNOTATE 1221b3fbe5e6Sseizo * section - then silently ignore that the relocation 1222b3fbe5e6Sseizo * can not be resolved. 1223b3fbe5e6Sseizo */ 1224b3fbe5e6Sseizo if (rsp->rel_osdesc && 1225b3fbe5e6Sseizo (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE)) 1226b3fbe5e6Sseizo return (0); 12275aefb655Srie (void) eprintf(ofl->ofl_lml, ERR_WARNING, 12285aefb655Srie MSG_INTL(MSG_REL_EXTERNSYM), 1229de777a60Sab conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf), 12307c478bd9Sstevel@tonic-gate rsp->rel_isdesc->is_file->ifl_name, 12317c478bd9Sstevel@tonic-gate demangle(rsp->rel_sname), rsp->rel_osdesc->os_name); 12327c478bd9Sstevel@tonic-gate return (1); 12337c478bd9Sstevel@tonic-gate } 12347c478bd9Sstevel@tonic-gate 12357c478bd9Sstevel@tonic-gate /* 12367c478bd9Sstevel@tonic-gate * Perform relocation. 12377c478bd9Sstevel@tonic-gate */ 12385aefb655Srie return (ld_add_actrel(NULL, rsp, ofl)); 12397c478bd9Sstevel@tonic-gate } 12407c478bd9Sstevel@tonic-gate 12417c478bd9Sstevel@tonic-gate 1242ba2be530Sab static uintptr_t 12435aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl) 12447c478bd9Sstevel@tonic-gate { 12457c478bd9Sstevel@tonic-gate Word rtype = rsp->rel_rtype; 12467c478bd9Sstevel@tonic-gate Sym_desc *sdp = rsp->rel_sym; 12471d9df23bSab ofl_flag_t flags = ofl->ofl_flags; 12487c478bd9Sstevel@tonic-gate Gotndx *gnp; 12497c478bd9Sstevel@tonic-gate 12507c478bd9Sstevel@tonic-gate /* 1251d326b23bSrie * If we're building an executable - use either the IE or LE access 1252d326b23bSrie * model. If we're building a shared object process any IE model. 12537c478bd9Sstevel@tonic-gate */ 1254d326b23bSrie if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) { 12557c478bd9Sstevel@tonic-gate /* 1256d326b23bSrie * Set the DF_STATIC_TLS flag. 12577c478bd9Sstevel@tonic-gate */ 12587c478bd9Sstevel@tonic-gate ofl->ofl_dtflags |= DF_STATIC_TLS; 12597c478bd9Sstevel@tonic-gate 1260d326b23bSrie if (!local || ((flags & FLG_OF_EXEC) == 0)) { 12617c478bd9Sstevel@tonic-gate /* 1262d326b23bSrie * Assign a GOT entry for static TLS references. 12637c478bd9Sstevel@tonic-gate */ 1264*57ef7aa9SRod Evans if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 1265*57ef7aa9SRod Evans GOT_REF_TLSIE, ofl, rsp)) == NULL) { 12667c478bd9Sstevel@tonic-gate 1267d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, 1268d326b23bSrie gnp, GOT_REF_TLSIE, FLG_REL_STLS, 1269d326b23bSrie rtype, R_AMD64_TPOFF64, 0) == S_ERROR) 1270d326b23bSrie return (S_ERROR); 1271d326b23bSrie } 12727c478bd9Sstevel@tonic-gate 12737c478bd9Sstevel@tonic-gate /* 1274d326b23bSrie * IE access model. 12757c478bd9Sstevel@tonic-gate */ 12767c478bd9Sstevel@tonic-gate if (IS_TLS_IE(rtype)) 12775aefb655Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 12787c478bd9Sstevel@tonic-gate 12797c478bd9Sstevel@tonic-gate /* 1280d326b23bSrie * Fixups are required for other executable models. 12817c478bd9Sstevel@tonic-gate */ 12825aefb655Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 12837c478bd9Sstevel@tonic-gate rsp, ofl)); 12847c478bd9Sstevel@tonic-gate } 1285d326b23bSrie 12867c478bd9Sstevel@tonic-gate /* 1287d326b23bSrie * LE access model. 12887c478bd9Sstevel@tonic-gate */ 12897c478bd9Sstevel@tonic-gate if (IS_TLS_LE(rtype)) 12905aefb655Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 1291d326b23bSrie 12925aefb655Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 12935aefb655Srie rsp, ofl)); 12947c478bd9Sstevel@tonic-gate } 12957c478bd9Sstevel@tonic-gate 12967c478bd9Sstevel@tonic-gate /* 1297d326b23bSrie * Building a shared object. 1298d326b23bSrie * 1299d326b23bSrie * Assign a GOT entry for a dynamic TLS reference. 13007c478bd9Sstevel@tonic-gate */ 1301*57ef7aa9SRod Evans if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 1302*57ef7aa9SRod Evans GOT_REF_TLSLD, ofl, rsp)) == NULL)) { 1303d326b23bSrie 1304d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD, 1305*57ef7aa9SRod Evans FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, NULL) == S_ERROR) 13067c478bd9Sstevel@tonic-gate return (S_ERROR); 1307d326b23bSrie 13085aefb655Srie } else if (IS_TLS_GD(rtype) && 1309*57ef7aa9SRod Evans ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD, 1310*57ef7aa9SRod Evans ofl, rsp)) == NULL)) { 1311d326b23bSrie 1312d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD, 1313d326b23bSrie FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64, 1314d326b23bSrie R_AMD64_DTPOFF64) == S_ERROR) 13157c478bd9Sstevel@tonic-gate return (S_ERROR); 13167c478bd9Sstevel@tonic-gate } 13177c478bd9Sstevel@tonic-gate 13187c478bd9Sstevel@tonic-gate if (IS_TLS_LD(rtype)) 13195aefb655Srie return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl)); 13207c478bd9Sstevel@tonic-gate 13215aefb655Srie return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl)); 13227c478bd9Sstevel@tonic-gate } 13237c478bd9Sstevel@tonic-gate 13247c478bd9Sstevel@tonic-gate /* ARGSUSED5 */ 1325ba2be530Sab static uintptr_t 1326*57ef7aa9SRod Evans ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl, 1327*57ef7aa9SRod Evans Rel_desc *rsp, Sym_desc *sdp) 13287c478bd9Sstevel@tonic-gate { 13297c478bd9Sstevel@tonic-gate Xword raddend; 1330*57ef7aa9SRod Evans Gotndx gn, *gnp; 1331*57ef7aa9SRod Evans Aliste idx; 13327c478bd9Sstevel@tonic-gate uint_t gotents; 13337c478bd9Sstevel@tonic-gate 13347c478bd9Sstevel@tonic-gate raddend = rsp->rel_raddend; 1335*57ef7aa9SRod Evans if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) 13367c478bd9Sstevel@tonic-gate return (1); 13377c478bd9Sstevel@tonic-gate 13387c478bd9Sstevel@tonic-gate if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD)) 13397c478bd9Sstevel@tonic-gate gotents = 2; 13407c478bd9Sstevel@tonic-gate else 13417c478bd9Sstevel@tonic-gate gotents = 1; 13427c478bd9Sstevel@tonic-gate 1343*57ef7aa9SRod Evans gn.gn_addend = raddend; 1344*57ef7aa9SRod Evans gn.gn_gotndx = ofl->ofl_gotcnt; 1345*57ef7aa9SRod Evans gn.gn_gotref = gref; 13467c478bd9Sstevel@tonic-gate 13477c478bd9Sstevel@tonic-gate ofl->ofl_gotcnt += gotents; 13487c478bd9Sstevel@tonic-gate 13497c478bd9Sstevel@tonic-gate if (gref == GOT_REF_TLSLD) { 1350*57ef7aa9SRod Evans if (ofl->ofl_tlsldgotndx == NULL) { 1351*57ef7aa9SRod Evans if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL) 1352*57ef7aa9SRod Evans return (S_ERROR); 1353*57ef7aa9SRod Evans (void) memcpy(gnp, &gn, sizeof (Gotndx)); 1354*57ef7aa9SRod Evans ofl->ofl_tlsldgotndx = gnp; 1355*57ef7aa9SRod Evans } 13567c478bd9Sstevel@tonic-gate return (1); 13577c478bd9Sstevel@tonic-gate } 13587c478bd9Sstevel@tonic-gate 1359*57ef7aa9SRod Evans idx = 0; 1360*57ef7aa9SRod Evans for (ALIST_TRAVERSE(*alpp, idx, gnp)) { 1361*57ef7aa9SRod Evans if (gnp->gn_addend > raddend) 1362*57ef7aa9SRod Evans break; 13637c478bd9Sstevel@tonic-gate } 1364*57ef7aa9SRod Evans 1365*57ef7aa9SRod Evans /* 1366*57ef7aa9SRod Evans * GOT indexes are maintained on an Alist, where there is typically 1367*57ef7aa9SRod Evans * only one index. The usage of this list is to scan the list to find 1368*57ef7aa9SRod Evans * an index, and then apply that index immediately to a relocation. 1369*57ef7aa9SRod Evans * Thus there are no external references to these GOT index structures 1370*57ef7aa9SRod Evans * that can be compromised by the Alist being reallocated. 1371*57ef7aa9SRod Evans */ 1372*57ef7aa9SRod Evans if (alist_insert(alpp, &gn, sizeof (Gotndx), 1373*57ef7aa9SRod Evans AL_CNT_SDP_GOT, idx) == NULL) 1374*57ef7aa9SRod Evans return (S_ERROR); 1375*57ef7aa9SRod Evans 13767c478bd9Sstevel@tonic-gate return (1); 13777c478bd9Sstevel@tonic-gate } 13787c478bd9Sstevel@tonic-gate 1379ba2be530Sab static void 13805aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl) 13817c478bd9Sstevel@tonic-gate { 13827c478bd9Sstevel@tonic-gate sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++; 13837c478bd9Sstevel@tonic-gate sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++; 1384141040e8Srie ofl->ofl_flags |= FLG_OF_BLDGOT; 13857c478bd9Sstevel@tonic-gate } 13867c478bd9Sstevel@tonic-gate 1387b3fbe5e6Sseizo static uchar_t plt0_template[M_PLT_ENTSIZE] = { 13887c478bd9Sstevel@tonic-gate /* 0x00 PUSHQ GOT+8(%rip) */ 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, 13897c478bd9Sstevel@tonic-gate /* 0x06 JMP *GOT+16(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 13907c478bd9Sstevel@tonic-gate /* 0x0c NOP */ 0x90, 13917c478bd9Sstevel@tonic-gate /* 0x0d NOP */ 0x90, 13927c478bd9Sstevel@tonic-gate /* 0x0e NOP */ 0x90, 13937c478bd9Sstevel@tonic-gate /* 0x0f NOP */ 0x90 13947c478bd9Sstevel@tonic-gate }; 13957c478bd9Sstevel@tonic-gate 13967c478bd9Sstevel@tonic-gate /* 13977c478bd9Sstevel@tonic-gate * Initializes .got[0] with the _DYNAMIC symbol value. 13987c478bd9Sstevel@tonic-gate */ 1399ba2be530Sab static uintptr_t 1400d326b23bSrie ld_fillin_gotplt(Ofl_desc *ofl) 14017c478bd9Sstevel@tonic-gate { 1402ba2be530Sab int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 1403ba2be530Sab 14047c478bd9Sstevel@tonic-gate if (ofl->ofl_osgot) { 1405d326b23bSrie Sym_desc *sdp; 14067c478bd9Sstevel@tonic-gate 14075aefb655Srie if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U), 14087c478bd9Sstevel@tonic-gate SYM_NOHASH, 0, ofl)) != NULL) { 1409d326b23bSrie uchar_t *genptr; 1410d326b23bSrie 1411d326b23bSrie genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf + 14127c478bd9Sstevel@tonic-gate (M_GOT_XDYNAMIC * M_GOT_ENTSIZE)); 14137c478bd9Sstevel@tonic-gate /* LINTED */ 14147c478bd9Sstevel@tonic-gate *(Xword *)genptr = sdp->sd_sym->st_value; 1415ba2be530Sab if (bswap) 1416ba2be530Sab /* LINTED */ 1417ba2be530Sab *(Xword *)genptr = 1418ba2be530Sab /* LINTED */ 1419ba2be530Sab ld_bswap_Xword(*(Xword *)genptr); 14207c478bd9Sstevel@tonic-gate } 14217c478bd9Sstevel@tonic-gate } 14227c478bd9Sstevel@tonic-gate 14237c478bd9Sstevel@tonic-gate /* 14247c478bd9Sstevel@tonic-gate * Fill in the reserved slot in the procedure linkage table the first 14257c478bd9Sstevel@tonic-gate * entry is: 14267c478bd9Sstevel@tonic-gate * 0x00 PUSHQ GOT+8(%rip) # GOT[1] 14277c478bd9Sstevel@tonic-gate * 0x06 JMP *GOT+16(%rip) # GOT[2] 14287c478bd9Sstevel@tonic-gate * 0x0c NOP 14297c478bd9Sstevel@tonic-gate * 0x0d NOP 14307c478bd9Sstevel@tonic-gate * 0x0e NOP 14317c478bd9Sstevel@tonic-gate * 0x0f NOP 14327c478bd9Sstevel@tonic-gate */ 1433f3324781Sab if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) { 1434d326b23bSrie uchar_t *pltent; 1435d326b23bSrie Xword val1; 14367c478bd9Sstevel@tonic-gate 1437b3fbe5e6Sseizo pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf; 14387c478bd9Sstevel@tonic-gate bcopy(plt0_template, pltent, sizeof (plt0_template)); 14397c478bd9Sstevel@tonic-gate 1440f3324781Sab /* 1441f3324781Sab * If '-z noreloc' is specified - skip the do_reloc_ld 1442f3324781Sab * stage. 1443f3324781Sab */ 1444f3324781Sab if (!OFL_DO_RELOC(ofl)) 1445f3324781Sab return (1); 1446f3324781Sab 14477c478bd9Sstevel@tonic-gate /* 14487c478bd9Sstevel@tonic-gate * filin: 14497c478bd9Sstevel@tonic-gate * PUSHQ GOT + 8(%rip) 14507c478bd9Sstevel@tonic-gate * 14517c478bd9Sstevel@tonic-gate * Note: 0x06 below represents the offset to the 14527c478bd9Sstevel@tonic-gate * next instruction - which is what %rip will 14537c478bd9Sstevel@tonic-gate * be pointing at. 14547c478bd9Sstevel@tonic-gate */ 14557c478bd9Sstevel@tonic-gate val1 = (ofl->ofl_osgot->os_shdr->sh_addr) + 1456de777a60Sab (M_GOT_XLINKMAP * M_GOT_ENTSIZE) - 1457de777a60Sab ofl->ofl_osplt->os_shdr->sh_addr - 0x06; 14587c478bd9Sstevel@tonic-gate 1459f3324781Sab if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02], 1460f3324781Sab &val1, MSG_ORIG(MSG_SYM_PLTENT), 1461f3324781Sab MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 1462f3324781Sab eprintf(ofl->ofl_lml, ERR_FATAL, 1463f3324781Sab MSG_INTL(MSG_PLT_PLT0FAIL)); 1464f3324781Sab return (S_ERROR); 14657c478bd9Sstevel@tonic-gate } 14667c478bd9Sstevel@tonic-gate 14677c478bd9Sstevel@tonic-gate /* 14687c478bd9Sstevel@tonic-gate * filin: 14697c478bd9Sstevel@tonic-gate * JMP *GOT+16(%rip) 14707c478bd9Sstevel@tonic-gate */ 14717c478bd9Sstevel@tonic-gate val1 = (ofl->ofl_osgot->os_shdr->sh_addr) + 1472de777a60Sab (M_GOT_XRTLD * M_GOT_ENTSIZE) - 1473de777a60Sab ofl->ofl_osplt->os_shdr->sh_addr - 0x0c; 1474f3324781Sab 1475f3324781Sab if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x08], 1476f3324781Sab &val1, MSG_ORIG(MSG_SYM_PLTENT), 1477f3324781Sab MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 1478f3324781Sab eprintf(ofl->ofl_lml, ERR_FATAL, 1479f3324781Sab MSG_INTL(MSG_PLT_PLT0FAIL)); 1480f3324781Sab return (S_ERROR); 14817c478bd9Sstevel@tonic-gate } 14827c478bd9Sstevel@tonic-gate } 1483f3324781Sab 14847c478bd9Sstevel@tonic-gate return (1); 14857c478bd9Sstevel@tonic-gate } 1486ba2be530Sab 1487ba2be530Sab 1488ba2be530Sab 1489ba2be530Sab /* 1490ba2be530Sab * Template for generating "void (*)(void)" function 1491ba2be530Sab */ 1492ba2be530Sab static const uchar_t nullfunc_tmpl[] = { /* amd64 */ 1493ba2be530Sab /* 0x00 */ 0x55, /* pushq %rbp */ 1494ba2be530Sab /* 0x01 */ 0x48, 0x8b, 0xec, /* movq %rsp,%rbp */ 1495ba2be530Sab /* 0x04 */ 0x48, 0x8b, 0xe5, /* movq %rbp,%rsp */ 1496ba2be530Sab /* 0x07 */ 0x5d, /* popq %rbp */ 1497ba2be530Sab /* 0x08 */ 0xc3 /* ret */ 1498ba2be530Sab }; 1499ba2be530Sab 1500ba2be530Sab 1501ba2be530Sab /* 1502ba2be530Sab * Return the ld_targ definition for this target. 1503ba2be530Sab */ 1504ba2be530Sab const Target * 1505ba2be530Sab ld_targ_init_x86(void) 1506ba2be530Sab { 1507ba2be530Sab static const Target _ld_targ = { 1508ba2be530Sab { /* Target_mach */ 1509ba2be530Sab M_MACH, /* m_mach */ 1510ba2be530Sab M_MACHPLUS, /* m_machplus */ 1511ba2be530Sab M_FLAGSPLUS, /* m_flagsplus */ 1512ba2be530Sab M_CLASS, /* m_class */ 1513ba2be530Sab M_DATA, /* m_data */ 1514ba2be530Sab 1515ba2be530Sab M_SEGM_ALIGN, /* m_segm_align */ 1516ba2be530Sab M_SEGM_ORIGIN, /* m_segm_origin */ 1517bb3b4f6cSRod Evans M_SEGM_AORIGIN, /* m_segm_aorigin */ 1518ba2be530Sab M_DATASEG_PERM, /* m_dataseg_perm */ 1519ba2be530Sab M_WORD_ALIGN, /* m_word_align */ 1520ba2be530Sab MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */ 1521ba2be530Sab 1522ba2be530Sab /* Relocation type codes */ 1523ba2be530Sab M_R_ARRAYADDR, /* m_r_arrayaddr */ 1524ba2be530Sab M_R_COPY, /* m_r_copy */ 1525ba2be530Sab M_R_GLOB_DAT, /* m_r_glob_dat */ 1526ba2be530Sab M_R_JMP_SLOT, /* m_r_jmp_slot */ 1527ba2be530Sab M_R_NUM, /* m_r_num */ 1528ba2be530Sab M_R_NONE, /* m_r_none */ 1529ba2be530Sab M_R_RELATIVE, /* m_r_relative */ 1530ba2be530Sab M_R_REGISTER, /* m_r_register */ 1531ba2be530Sab 1532ba2be530Sab /* Relocation related constants */ 1533ba2be530Sab M_REL_DT_COUNT, /* m_rel_dt_count */ 1534ba2be530Sab M_REL_DT_ENT, /* m_rel_dt_ent */ 1535ba2be530Sab M_REL_DT_SIZE, /* m_rel_dt_size */ 1536ba2be530Sab M_REL_DT_TYPE, /* m_rel_dt_type */ 1537ba2be530Sab M_REL_SHT_TYPE, /* m_rel_sht_type */ 1538ba2be530Sab 1539ba2be530Sab /* GOT related constants */ 1540ba2be530Sab M_GOT_ENTSIZE, /* m_got_entsize */ 1541ba2be530Sab M_GOT_XNumber, /* m_got_xnumber */ 1542ba2be530Sab 1543ba2be530Sab /* PLT related constants */ 1544ba2be530Sab M_PLT_ALIGN, /* m_plt_align */ 1545ba2be530Sab M_PLT_ENTSIZE, /* m_plt_entsize */ 1546ba2be530Sab M_PLT_RESERVSZ, /* m_plt_reservsz */ 1547ba2be530Sab M_PLT_SHF_FLAGS, /* m_plt_shf_flags */ 1548ba2be530Sab 15497e16fca0SAli Bahrami /* Section type of .eh_frame/.eh_frame_hdr sections */ 15507e16fca0SAli Bahrami SHT_AMD64_UNWIND, /* m_sht_unwind */ 15517e16fca0SAli Bahrami 1552ba2be530Sab M_DT_REGISTER, /* m_dt_register */ 1553ba2be530Sab }, 1554ba2be530Sab { /* Target_machid */ 1555ba2be530Sab M_ID_ARRAY, /* id_array */ 1556ba2be530Sab M_ID_BSS, /* id_bss */ 1557ba2be530Sab M_ID_CAP, /* id_cap */ 1558ba2be530Sab M_ID_DATA, /* id_data */ 1559ba2be530Sab M_ID_DYNAMIC, /* id_dynamic */ 1560ba2be530Sab M_ID_DYNSORT, /* id_dynsort */ 1561ba2be530Sab M_ID_DYNSTR, /* id_dynstr */ 1562ba2be530Sab M_ID_DYNSYM, /* id_dynsym */ 1563ba2be530Sab M_ID_DYNSYM_NDX, /* id_dynsym_ndx */ 1564ba2be530Sab M_ID_GOT, /* id_got */ 1565ba2be530Sab M_ID_UNKNOWN, /* id_gotdata (unused) */ 1566ba2be530Sab M_ID_HASH, /* id_hash */ 1567ba2be530Sab M_ID_INTERP, /* id_interp */ 1568ba2be530Sab M_ID_LBSS, /* id_lbss */ 1569ba2be530Sab M_ID_LDYNSYM, /* id_ldynsym */ 1570ba2be530Sab M_ID_NOTE, /* id_note */ 1571ba2be530Sab M_ID_NULL, /* id_null */ 1572ba2be530Sab M_ID_PLT, /* id_plt */ 1573ba2be530Sab M_ID_REL, /* id_rel */ 1574ba2be530Sab M_ID_STRTAB, /* id_strtab */ 1575ba2be530Sab M_ID_SYMINFO, /* id_syminfo */ 1576ba2be530Sab M_ID_SYMTAB, /* id_symtab */ 1577ba2be530Sab M_ID_SYMTAB_NDX, /* id_symtab_ndx */ 1578ba2be530Sab M_ID_TEXT, /* id_text */ 1579ba2be530Sab M_ID_TLS, /* id_tls */ 1580ba2be530Sab M_ID_TLSBSS, /* id_tlsbss */ 1581ba2be530Sab M_ID_UNKNOWN, /* id_unknown */ 1582ba2be530Sab M_ID_UNWIND, /* id_unwind */ 15837e16fca0SAli Bahrami M_ID_UNWINDHDR, /* id_unwindhdr */ 1584ba2be530Sab M_ID_USER, /* id_user */ 1585ba2be530Sab M_ID_VERSION, /* id_version */ 1586ba2be530Sab }, 1587ba2be530Sab { /* Target_nullfunc */ 1588ba2be530Sab nullfunc_tmpl, /* nf_template */ 1589ba2be530Sab sizeof (nullfunc_tmpl), /* nf_size */ 1590ba2be530Sab }, 1591ba2be530Sab { /* Target_machrel */ 1592ba2be530Sab reloc_table, 1593ba2be530Sab 1594ba2be530Sab ld_init_rel, /* mr_init_rel */ 1595ba2be530Sab ld_mach_eflags, /* mr_mach_eflags */ 1596ba2be530Sab ld_mach_make_dynamic, /* mr_mach_make_dynamic */ 1597ba2be530Sab ld_mach_update_odynamic, /* mr_mach_update_odynamic */ 1598ba2be530Sab ld_calc_plt_addr, /* mr_calc_plt_addr */ 1599ba2be530Sab ld_perform_outreloc, /* mr_perform_outreloc */ 1600ba2be530Sab ld_do_activerelocs, /* mr_do_activerelocs */ 1601ba2be530Sab ld_add_outrel, /* mr_add_outrel */ 1602ba2be530Sab NULL, /* mr_reloc_register */ 1603ba2be530Sab ld_reloc_local, /* mr_reloc_local */ 1604ba2be530Sab NULL, /* mr_reloc_GOTOP */ 1605ba2be530Sab ld_reloc_TLS, /* mr_reloc_TLS */ 1606ba2be530Sab NULL, /* mr_assign_got */ 1607*57ef7aa9SRod Evans ld_find_got_ndx, /* mr_find_got_ndx */ 1608ba2be530Sab ld_calc_got_offset, /* mr_calc_got_offset */ 1609ba2be530Sab ld_assign_got_ndx, /* mr_assign_got_ndx */ 1610ba2be530Sab ld_assign_plt_ndx, /* mr_assign_plt_ndx */ 1611ba2be530Sab NULL, /* mr_allocate_got */ 1612ba2be530Sab ld_fillin_gotplt, /* mr_fillin_gotplt */ 1613ba2be530Sab }, 1614ba2be530Sab { /* Target_machsym */ 1615ba2be530Sab NULL, /* ms_reg_check */ 1616ba2be530Sab NULL, /* ms_mach_sym_typecheck */ 1617ba2be530Sab NULL, /* ms_is_regsym */ 1618ba2be530Sab NULL, /* ms_reg_find */ 1619ba2be530Sab NULL /* ms_reg_enter */ 1620ba2be530Sab } 1621ba2be530Sab }; 1622ba2be530Sab 1623ba2be530Sab return (&_ld_targ); 1624ba2be530Sab } 1625