15aefb655Srie /* 25aefb655Srie * CDDL HEADER START 35aefb655Srie * 45aefb655Srie * 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. 75aefb655Srie * 85aefb655Srie * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95aefb655Srie * or http://www.opensolaris.org/os/licensing. 105aefb655Srie * See the License for the specific language governing permissions 115aefb655Srie * and limitations under the License. 125aefb655Srie * 135aefb655Srie * When distributing Covered Code, include this CDDL HEADER in each 145aefb655Srie * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155aefb655Srie * If applicable, add the following below this CDDL HEADER, with the 165aefb655Srie * fields enclosed by brackets "[]" replaced with your own identifying 175aefb655Srie * information: Portions Copyright [yyyy] [name of copyright owner] 185aefb655Srie * 195aefb655Srie * CDDL HEADER END 205aefb655Srie */ 215aefb655Srie 225aefb655Srie /* 23*69112eddSAli Bahrami * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 245aefb655Srie * Use is subject to license terms. 255aefb655Srie */ 265aefb655Srie 27ba2be530Sab196087 /* Get the x86 version of the relocation engine */ 28ba2be530Sab196087 #define DO_RELOC_LIBLD_X86 29ba2be530Sab196087 305aefb655Srie #include <string.h> 315aefb655Srie #include <stdio.h> 325aefb655Srie #include <strings.h> 335aefb655Srie #include <sys/elf_amd64.h> 345aefb655Srie #include <debug.h> 355aefb655Srie #include <reloc.h> 36ba2be530Sab196087 #include <i386/machdep_x86.h> 375aefb655Srie #include "msg.h" 385aefb655Srie #include "_libld.h" 395aefb655Srie 4057ef7aa9SRod Evans /* 4157ef7aa9SRod Evans * Search the GOT index list for a GOT entry with a matching reference and the 4257ef7aa9SRod Evans * proper addend. 4357ef7aa9SRod Evans */ 4457ef7aa9SRod Evans static Gotndx * 4557ef7aa9SRod Evans ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc) 4657ef7aa9SRod Evans { 4757ef7aa9SRod Evans Aliste idx; 4857ef7aa9SRod Evans Gotndx *gnp; 49ba2be530Sab196087 5057ef7aa9SRod Evans assert(rdesc != 0); 51ba2be530Sab196087 5257ef7aa9SRod Evans if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx) 5357ef7aa9SRod Evans return (ofl->ofl_tlsldgotndx); 5457ef7aa9SRod Evans 5557ef7aa9SRod Evans for (ALIST_TRAVERSE(alp, idx, gnp)) { 5657ef7aa9SRod Evans if ((rdesc->rel_raddend == gnp->gn_addend) && 5757ef7aa9SRod Evans (gnp->gn_gotref == gref)) { 5857ef7aa9SRod Evans return (gnp); 5957ef7aa9SRod Evans } 6057ef7aa9SRod Evans } 6157ef7aa9SRod Evans return (NULL); 6257ef7aa9SRod Evans } 6357ef7aa9SRod Evans 6457ef7aa9SRod Evans static Xword 6557ef7aa9SRod Evans ld_calc_got_offset(Rel_desc *rdesc, Ofl_desc *ofl) 6657ef7aa9SRod Evans { 6757ef7aa9SRod Evans Os_desc *osp = ofl->ofl_osgot; 6857ef7aa9SRod Evans Sym_desc *sdp = rdesc->rel_sym; 6957ef7aa9SRod Evans Xword gotndx; 7057ef7aa9SRod Evans Gotref gref; 7157ef7aa9SRod Evans Gotndx *gnp; 7257ef7aa9SRod Evans 7357ef7aa9SRod Evans if (rdesc->rel_flags & FLG_REL_DTLS) 7457ef7aa9SRod Evans gref = GOT_REF_TLSGD; 7557ef7aa9SRod Evans else if (rdesc->rel_flags & FLG_REL_MTLS) 7657ef7aa9SRod Evans gref = GOT_REF_TLSLD; 7757ef7aa9SRod Evans else if (rdesc->rel_flags & FLG_REL_STLS) 7857ef7aa9SRod Evans gref = GOT_REF_TLSIE; 7957ef7aa9SRod Evans else 8057ef7aa9SRod Evans gref = GOT_REF_GENERIC; 8157ef7aa9SRod Evans 8257ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc); 8357ef7aa9SRod Evans assert(gnp); 8457ef7aa9SRod Evans 8557ef7aa9SRod Evans gotndx = (Xword)gnp->gn_gotndx; 8657ef7aa9SRod Evans 8757ef7aa9SRod Evans if ((rdesc->rel_flags & FLG_REL_DTLS) && 8857ef7aa9SRod Evans (rdesc->rel_rtype == R_AMD64_DTPOFF64)) 8957ef7aa9SRod Evans gotndx++; 9057ef7aa9SRod Evans 9157ef7aa9SRod Evans return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE))); 9257ef7aa9SRod Evans } 93ba2be530Sab196087 94ba2be530Sab196087 static Word 955aefb655Srie ld_init_rel(Rel_desc *reld, void *reloc) 965aefb655Srie { 975aefb655Srie Rela *rel = (Rela *)reloc; 985aefb655Srie 995aefb655Srie /* LINTED */ 100ba2be530Sab196087 reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH); 1015aefb655Srie reld->rel_roffset = rel->r_offset; 1025aefb655Srie reld->rel_raddend = rel->r_addend; 1035aefb655Srie reld->rel_typedata = 0; 1045aefb655Srie 1055aefb655Srie reld->rel_flags |= FLG_REL_RELA; 1065aefb655Srie 1075aefb655Srie return ((Word)ELF_R_SYM(rel->r_info)); 1085aefb655Srie } 1095aefb655Srie 110ba2be530Sab196087 static void 1115aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl) 1125aefb655Srie { 1135aefb655Srie ofl->ofl_dehdr->e_flags |= ehdr->e_flags; 1145aefb655Srie } 1155aefb655Srie 116ba2be530Sab196087 static void 1175aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt) 1185aefb655Srie { 1195aefb655Srie if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) { 1205aefb655Srie /* 1215aefb655Srie * Create this entry if we are going to create a PLT table. 1225aefb655Srie */ 1235aefb655Srie if (ofl->ofl_pltcnt) 1245aefb655Srie (*cnt)++; /* DT_PLTGOT */ 1255aefb655Srie } 1265aefb655Srie } 1275aefb655Srie 128ba2be530Sab196087 static void 1295aefb655Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn) 1305aefb655Srie { 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)++; 1385aefb655Srie } 1395aefb655Srie } 1405aefb655Srie 141ba2be530Sab196087 static Xword 1425aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl) 1435aefb655Srie { 1445aefb655Srie Xword value; 1455aefb655Srie 1465aefb655Srie value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) + 1475aefb655Srie M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE); 1485aefb655Srie return (value); 1495aefb655Srie } 1505aefb655Srie 1515aefb655Srie /* 1525aefb655Srie * Build a single plt entry - code is: 1535aefb655Srie * JMP *name1@GOTPCREL(%rip) 1545aefb655Srie * PUSHL $index 1555aefb655Srie * JMP .PLT0 1565aefb655Srie */ 1575aefb655Srie static uchar_t pltn_entry[M_PLT_ENTSIZE] = { 1585aefb655Srie /* 0x00 jmpq *name1@GOTPCREL(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 1595aefb655Srie /* 0x06 pushq $index */ 0x68, 0x00, 0x00, 0x00, 0x00, 1605aefb655Srie /* 0x0b jmpq .plt0(%rip) */ 0xe9, 0x00, 0x00, 0x00, 0x00 1615aefb655Srie /* 0x10 */ 1625aefb655Srie }; 1635aefb655Srie 1645aefb655Srie static uintptr_t 1655aefb655Srie plt_entry(Ofl_desc * ofl, Sym_desc * sdp) 1665aefb655Srie { 1675aefb655Srie uchar_t *plt0, *pltent, *gotent; 1685aefb655Srie Sword plt_off; 1695aefb655Srie Word got_off; 1705aefb655Srie Xword val1; 171ba2be530Sab196087 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 1725aefb655Srie 1735aefb655Srie got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE; 1745aefb655Srie plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * 1755aefb655Srie M_PLT_ENTSIZE); 1765aefb655Srie plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf); 1775aefb655Srie pltent = plt0 + plt_off; 1785aefb655Srie gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off; 1795aefb655Srie 1805aefb655Srie bcopy(pltn_entry, pltent, sizeof (pltn_entry)); 1815aefb655Srie /* 1825aefb655Srie * Fill in the got entry with the address of the next instruction. 1835aefb655Srie */ 1845aefb655Srie /* LINTED */ 1855aefb655Srie *(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off + 1865aefb655Srie M_PLT_INSSIZE; 187ba2be530Sab196087 if (bswap) 188ba2be530Sab196087 /* LINTED */ 189ba2be530Sab196087 *(Word *)gotent = ld_bswap_Word(*(Word *)gotent); 1905aefb655Srie 1915aefb655Srie /* 192f3324781Sab196087 * If '-z noreloc' is specified - skip the do_reloc_ld 193f3324781Sab196087 * stage. 194f3324781Sab196087 */ 195f3324781Sab196087 if (!OFL_DO_RELOC(ofl)) 196f3324781Sab196087 return (1); 197f3324781Sab196087 198f3324781Sab196087 /* 1995aefb655Srie * patchup: 2005aefb655Srie * jmpq *name1@gotpcrel(%rip) 2015aefb655Srie * 2025aefb655Srie * NOTE: 0x06 represents next instruction. 2035aefb655Srie */ 2045aefb655Srie val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) - 2055aefb655Srie (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06; 2065aefb655Srie 207f3324781Sab196087 if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02], 2085aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 209f3324781Sab196087 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 210f3324781Sab196087 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL), 211f3324781Sab196087 sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name)); 2125aefb655Srie return (S_ERROR); 2135aefb655Srie } 2145aefb655Srie 2155aefb655Srie /* 2165aefb655Srie * patchup: 2175aefb655Srie * pushq $pltndx 2185aefb655Srie */ 2195aefb655Srie val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1); 220f3324781Sab196087 221f3324781Sab196087 if (do_reloc_ld(R_AMD64_32, &pltent[0x07], 2225aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 223f3324781Sab196087 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 224f3324781Sab196087 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL), 225f3324781Sab196087 sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name)); 2265aefb655Srie return (S_ERROR); 2275aefb655Srie } 2285aefb655Srie 2295aefb655Srie /* 2305aefb655Srie * patchup: 2315aefb655Srie * jmpq .plt0(%rip) 232f3324781Sab196087 * NOTE: 0x10 represents next instruction. The rather complex 233f3324781Sab196087 * series of casts is necessary to sign extend an offset into 234f3324781Sab196087 * a 64-bit value while satisfying various compiler error 235f3324781Sab196087 * checks. Handle with care. 2365aefb655Srie */ 2375aefb655Srie val1 = (Xword)((intptr_t)((uintptr_t)plt0 - 2385aefb655Srie (uintptr_t)(&pltent[0x10]))); 2395aefb655Srie 240f3324781Sab196087 if (do_reloc_ld(R_AMD64_PC32, &pltent[0x0c], 2415aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 242f3324781Sab196087 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 243f3324781Sab196087 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL), 244f3324781Sab196087 sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name)); 2455aefb655Srie return (S_ERROR); 2465aefb655Srie } 247f3324781Sab196087 2485aefb655Srie return (1); 2495aefb655Srie } 2505aefb655Srie 251ba2be530Sab196087 static uintptr_t 2525aefb655Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl) 2535aefb655Srie { 2545aefb655Srie Os_desc * relosp, * osp = 0; 2555aefb655Srie Word ndx; 2565aefb655Srie Xword roffset, value; 2575aefb655Srie Sxword raddend; 2585aefb655Srie Rela rea; 2595aefb655Srie char *relbits; 2605aefb655Srie Sym_desc * sdp, * psym = (Sym_desc *)0; 2615aefb655Srie int sectmoved = 0; 2625aefb655Srie 2635aefb655Srie raddend = orsp->rel_raddend; 2645aefb655Srie sdp = orsp->rel_sym; 2655aefb655Srie 2665aefb655Srie /* 2675aefb655Srie * If the section this relocation is against has been discarded 2685aefb655Srie * (-zignore), then also discard (skip) the relocation itself. 2695aefb655Srie */ 2705aefb655Srie if (orsp->rel_isdesc && ((orsp->rel_flags & 2715aefb655Srie (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) && 2725aefb655Srie (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) { 2735aefb655Srie DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp)); 2745aefb655Srie return (1); 2755aefb655Srie } 2765aefb655Srie 2775aefb655Srie /* 2785aefb655Srie * If this is a relocation against a move table, or expanded move 2795aefb655Srie * table, adjust the relocation entries. 2805aefb655Srie */ 2815aefb655Srie if (orsp->rel_move) 2825aefb655Srie ld_adj_movereloc(ofl, orsp); 2835aefb655Srie 2845aefb655Srie /* 2855aefb655Srie * If this is a relocation against a section then we need to adjust the 2865aefb655Srie * raddend field to compensate for the new position of the input section 2875aefb655Srie * within the new output section. 2885aefb655Srie */ 2895aefb655Srie if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) { 29057ef7aa9SRod Evans if (ofl->ofl_parsyms && 2915aefb655Srie (sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 2925aefb655Srie /* LINTED */ 2935aefb655Srie (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) { 2945aefb655Srie DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym)); 2955aefb655Srie sectmoved = 1; 2965aefb655Srie if (ofl->ofl_flags & FLG_OF_RELOBJ) 2975aefb655Srie raddend = psym->sd_sym->st_value; 2985aefb655Srie else 2995aefb655Srie raddend = psym->sd_sym->st_value - 3005aefb655Srie psym->sd_isc->is_osdesc->os_shdr->sh_addr; 3015aefb655Srie /* LINTED */ 3025aefb655Srie raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata); 3035aefb655Srie if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 3045aefb655Srie raddend += 3055aefb655Srie psym->sd_isc->is_osdesc->os_shdr->sh_addr; 3065aefb655Srie } else { 3075aefb655Srie /* LINTED */ 3085aefb655Srie raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata); 3095aefb655Srie if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 3105aefb655Srie raddend += 3115aefb655Srie sdp->sd_isc->is_osdesc->os_shdr->sh_addr; 3125aefb655Srie } 3135aefb655Srie } 3145aefb655Srie 3155aefb655Srie value = sdp->sd_sym->st_value; 3165aefb655Srie 3175aefb655Srie if (orsp->rel_flags & FLG_REL_GOT) { 3185aefb655Srie /* 3195aefb655Srie * Note: for GOT relative relocations on amd64 3205aefb655Srie * we discard the addend. It was relevant 3215aefb655Srie * to the reference - not to the data item 3225aefb655Srie * being referenced (ie: that -4 thing). 3235aefb655Srie */ 3245aefb655Srie raddend = 0; 3255aefb655Srie osp = ofl->ofl_osgot; 3265aefb655Srie roffset = ld_calc_got_offset(orsp, ofl); 3275aefb655Srie 3285aefb655Srie } else if (orsp->rel_flags & FLG_REL_PLT) { 3295aefb655Srie /* 3305aefb655Srie * Note that relocations for PLT's actually 3315aefb655Srie * cause a relocation againt the GOT. 3325aefb655Srie */ 3335aefb655Srie osp = ofl->ofl_osplt; 3345aefb655Srie roffset = (ofl->ofl_osgot->os_shdr->sh_addr) + 3355aefb655Srie sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE; 3365aefb655Srie raddend = 0; 3375aefb655Srie if (plt_entry(ofl, sdp) == S_ERROR) 3385aefb655Srie return (S_ERROR); 3395aefb655Srie 3405aefb655Srie } else if (orsp->rel_flags & FLG_REL_BSS) { 3415aefb655Srie /* 3425aefb655Srie * This must be a R_AMD64_COPY. For these set the roffset to 3435aefb655Srie * point to the new symbols location. 3445aefb655Srie */ 3455aefb655Srie osp = ofl->ofl_isbss->is_osdesc; 3465aefb655Srie roffset = value; 3475aefb655Srie 3485aefb655Srie /* 3495aefb655Srie * The raddend doesn't mean anything in a R_SPARC_COPY 3505aefb655Srie * relocation. Null it out because it can confuse people. 3515aefb655Srie */ 3525aefb655Srie raddend = 0; 3535aefb655Srie } else { 3545aefb655Srie osp = orsp->rel_osdesc; 3555aefb655Srie 3565aefb655Srie /* 3575aefb655Srie * Calculate virtual offset of reference point; equals offset 3585aefb655Srie * into section + vaddr of section for loadable sections, or 3595aefb655Srie * offset plus section displacement for nonloadable sections. 3605aefb655Srie */ 3615aefb655Srie roffset = orsp->rel_roffset + 3625aefb655Srie (Off)_elf_getxoff(orsp->rel_isdesc->is_indata); 3635aefb655Srie if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) 3645aefb655Srie roffset += orsp->rel_isdesc->is_osdesc-> 3655aefb655Srie os_shdr->sh_addr; 3665aefb655Srie } 3675aefb655Srie 3685aefb655Srie if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0)) 3695aefb655Srie relosp = ofl->ofl_osrel; 3705aefb655Srie 3715aefb655Srie /* 3725aefb655Srie * Assign the symbols index for the output relocation. If the 3735aefb655Srie * relocation refers to a SECTION symbol then it's index is based upon 3745aefb655Srie * the output sections symbols index. Otherwise the index can be 3755aefb655Srie * derived from the symbols index itself. 3765aefb655Srie */ 3775aefb655Srie if (orsp->rel_rtype == R_AMD64_RELATIVE) 3785aefb655Srie ndx = STN_UNDEF; 3795aefb655Srie else if ((orsp->rel_flags & FLG_REL_SCNNDX) || 3805aefb655Srie (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) { 3815aefb655Srie if (sectmoved == 0) { 3825aefb655Srie /* 3835aefb655Srie * Check for a null input section. This can 3845aefb655Srie * occur if this relocation references a symbol 3855aefb655Srie * generated by sym_add_sym(). 3865aefb655Srie */ 38757ef7aa9SRod Evans if (sdp->sd_isc && sdp->sd_isc->is_osdesc) 38857ef7aa9SRod Evans ndx = sdp->sd_isc->is_osdesc->os_identndx; 3895aefb655Srie else 3905aefb655Srie ndx = sdp->sd_shndx; 3915aefb655Srie } else 39235450702SAli Bahrami ndx = ofl->ofl_parexpnndx; 3935aefb655Srie } else 3945aefb655Srie ndx = sdp->sd_symndx; 3955aefb655Srie 3965aefb655Srie /* 3975aefb655Srie * Add the symbols 'value' to the addend field. 3985aefb655Srie */ 3995aefb655Srie if (orsp->rel_flags & FLG_REL_ADVAL) 4005aefb655Srie raddend += value; 4015aefb655Srie 4025aefb655Srie /* 4037010c12aSrie * The addend field for R_AMD64_DTPMOD64 means nothing. The addend 4047010c12aSrie * is propagated in the corresponding R_AMD64_DTPOFF64 relocation. 4055aefb655Srie */ 4065aefb655Srie if (orsp->rel_rtype == R_AMD64_DTPMOD64) 4075aefb655Srie raddend = 0; 4085aefb655Srie 4095aefb655Srie relbits = (char *)relosp->os_outdata->d_buf; 4105aefb655Srie 4115aefb655Srie rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype); 4125aefb655Srie rea.r_offset = roffset; 4135aefb655Srie rea.r_addend = raddend; 4145aefb655Srie DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name, 4155aefb655Srie orsp->rel_sname)); 4165aefb655Srie 4175aefb655Srie /* 4185aefb655Srie * Assert we haven't walked off the end of our relocation table. 4195aefb655Srie */ 4205aefb655Srie assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size); 4215aefb655Srie 4225aefb655Srie (void) memcpy((relbits + relosp->os_szoutrels), 4235aefb655Srie (char *)&rea, sizeof (Rela)); 4245aefb655Srie relosp->os_szoutrels += (Xword)sizeof (Rela); 4255aefb655Srie 4265aefb655Srie /* 4275aefb655Srie * Determine if this relocation is against a non-writable, allocatable 4285aefb655Srie * section. If so we may need to provide a text relocation diagnostic. 4295aefb655Srie * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually 4305aefb655Srie * result in modifications to the .got. 4315aefb655Srie */ 4325aefb655Srie if (orsp->rel_rtype == R_AMD64_JUMP_SLOT) 4335aefb655Srie osp = ofl->ofl_osgot; 4345aefb655Srie 4355aefb655Srie ld_reloc_remain_entry(orsp, osp, ofl); 4365aefb655Srie return (1); 4375aefb655Srie } 4385aefb655Srie 4395aefb655Srie /* 4405aefb655Srie * amd64 Instructions for TLS processing 4415aefb655Srie */ 4425aefb655Srie static uchar_t tlsinstr_gd_ie[] = { 4435aefb655Srie /* 4445aefb655Srie * 0x00 movq %fs:0, %rax 4455aefb655Srie */ 4465aefb655Srie 0x64, 0x48, 0x8b, 0x04, 0x25, 4475aefb655Srie 0x00, 0x00, 0x00, 0x00, 4485aefb655Srie /* 4495aefb655Srie * 0x09 addq x@gottpoff(%rip), %rax 4505aefb655Srie */ 4515aefb655Srie 0x48, 0x03, 0x05, 0x00, 0x00, 4525aefb655Srie 0x00, 0x00 4535aefb655Srie }; 4545aefb655Srie 4555aefb655Srie static uchar_t tlsinstr_gd_le[] = { 4565aefb655Srie /* 4575aefb655Srie * 0x00 movq %fs:0, %rax 4585aefb655Srie */ 4595aefb655Srie 0x64, 0x48, 0x8b, 0x04, 0x25, 4605aefb655Srie 0x00, 0x00, 0x00, 0x00, 4615aefb655Srie /* 4625aefb655Srie * 0x09 leaq x@gottpoff(%rip), %rax 4635aefb655Srie */ 4645aefb655Srie 0x48, 0x8d, 0x80, 0x00, 0x00, 4655aefb655Srie 0x00, 0x00 4665aefb655Srie }; 4675aefb655Srie 4685aefb655Srie static uchar_t tlsinstr_ld_le[] = { 4695aefb655Srie /* 4705aefb655Srie * .byte 0x66 4715aefb655Srie */ 4725aefb655Srie 0x66, 4735aefb655Srie /* 4745aefb655Srie * .byte 0x66 4755aefb655Srie */ 4765aefb655Srie 0x66, 4775aefb655Srie /* 4785aefb655Srie * .byte 0x66 4795aefb655Srie */ 4805aefb655Srie 0x66, 4815aefb655Srie /* 4825aefb655Srie * movq %fs:0, %rax 4835aefb655Srie */ 4845aefb655Srie 0x64, 0x48, 0x8b, 0x04, 0x25, 4855aefb655Srie 0x00, 0x00, 0x00, 0x00 4865aefb655Srie }; 4875aefb655Srie 4885aefb655Srie 4895aefb655Srie static Fixupret 4905aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp) 4915aefb655Srie { 4925aefb655Srie Sym_desc *sdp = arsp->rel_sym; 4935aefb655Srie Word rtype = arsp->rel_rtype; 4945aefb655Srie uchar_t *offset; 4955aefb655Srie 4965aefb655Srie offset = (uchar_t *)((uintptr_t)arsp->rel_roffset + 4975aefb655Srie (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) + 4985aefb655Srie (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf); 4995aefb655Srie 5005aefb655Srie if (sdp->sd_ref == REF_DYN_NEED) { 5015aefb655Srie /* 5025aefb655Srie * IE reference model 5035aefb655Srie */ 5045aefb655Srie switch (rtype) { 5055aefb655Srie case R_AMD64_TLSGD: 5065aefb655Srie /* 5075aefb655Srie * GD -> IE 5085aefb655Srie * 5095aefb655Srie * Transition: 5105aefb655Srie * 0x00 .byte 0x66 5115aefb655Srie * 0x01 leaq x@tlsgd(%rip), %rdi 5125aefb655Srie * 0x08 .word 0x6666 5135aefb655Srie * 0x0a rex64 5145aefb655Srie * 0x0b call __tls_get_addr@plt 5155aefb655Srie * 0x10 5165aefb655Srie * To: 5175aefb655Srie * 0x00 movq %fs:0, %rax 5185aefb655Srie * 0x09 addq x@gottpoff(%rip), %rax 5195aefb655Srie * 0x10 5205aefb655Srie */ 5215aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 522051d39bbSrie R_AMD64_GOTTPOFF, arsp)); 5235aefb655Srie arsp->rel_rtype = R_AMD64_GOTTPOFF; 5245aefb655Srie arsp->rel_roffset += 8; 5255aefb655Srie arsp->rel_raddend = (Sxword)-4; 5265aefb655Srie 5275aefb655Srie /* 528051d39bbSrie * Adjust 'offset' to beginning of instruction 5295aefb655Srie * sequence. 5305aefb655Srie */ 5315aefb655Srie offset -= 4; 5325aefb655Srie (void) memcpy(offset, tlsinstr_gd_ie, 5335aefb655Srie sizeof (tlsinstr_gd_ie)); 5345aefb655Srie return (FIX_RELOC); 5355aefb655Srie 5365aefb655Srie case R_AMD64_PLT32: 5375aefb655Srie /* 538051d39bbSrie * Fixup done via the TLS_GD relocation. 5395aefb655Srie */ 5405aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 541051d39bbSrie R_AMD64_NONE, arsp)); 5425aefb655Srie return (FIX_DONE); 5435aefb655Srie } 5445aefb655Srie } 5455aefb655Srie 5465aefb655Srie /* 5475aefb655Srie * LE reference model 5485aefb655Srie */ 5495aefb655Srie switch (rtype) { 5505aefb655Srie case R_AMD64_TLSGD: 5515aefb655Srie /* 5525aefb655Srie * GD -> LE 5535aefb655Srie * 5545aefb655Srie * Transition: 5555aefb655Srie * 0x00 .byte 0x66 5565aefb655Srie * 0x01 leaq x@tlsgd(%rip), %rdi 5575aefb655Srie * 0x08 .word 0x6666 5585aefb655Srie * 0x0a rex64 5595aefb655Srie * 0x0b call __tls_get_addr@plt 5605aefb655Srie * 0x10 5615aefb655Srie * To: 5625aefb655Srie * 0x00 movq %fs:0, %rax 5635aefb655Srie * 0x09 leaq x@tpoff(%rax), %rax 5645aefb655Srie * 0x10 5655aefb655Srie */ 5665aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 567051d39bbSrie R_AMD64_TPOFF32, arsp)); 5685aefb655Srie arsp->rel_rtype = R_AMD64_TPOFF32; 5695aefb655Srie arsp->rel_roffset += 8; 5705aefb655Srie arsp->rel_raddend = 0; 5715aefb655Srie 5725aefb655Srie /* 573051d39bbSrie * Adjust 'offset' to beginning of instruction sequence. 5745aefb655Srie */ 5755aefb655Srie offset -= 4; 5765aefb655Srie (void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le)); 5775aefb655Srie return (FIX_RELOC); 5785aefb655Srie 5795aefb655Srie case R_AMD64_GOTTPOFF: 5805aefb655Srie /* 5815aefb655Srie * IE -> LE 5825aefb655Srie * 5835aefb655Srie * Transition: 5845aefb655Srie * 0x00 movq %fs:0, %rax 5855aefb655Srie * 0x09 addq x@gottopoff(%rip), %rax 5865aefb655Srie * 0x10 5875aefb655Srie * To: 5885aefb655Srie * 0x00 movq %fs:0, %rax 5895aefb655Srie * 0x09 leaq x@tpoff(%rax), %rax 5905aefb655Srie * 0x10 5915aefb655Srie */ 592051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 593051d39bbSrie R_AMD64_TPOFF32, arsp)); 5945aefb655Srie arsp->rel_rtype = R_AMD64_TPOFF32; 5955aefb655Srie arsp->rel_raddend = 0; 5965aefb655Srie 5975aefb655Srie /* 598051d39bbSrie * Adjust 'offset' to beginning of instruction sequence. 5995aefb655Srie */ 6005aefb655Srie offset -= 12; 6015aefb655Srie 6025aefb655Srie /* 603051d39bbSrie * Same code sequence used in the GD -> LE transition. 6045aefb655Srie */ 6055aefb655Srie (void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le)); 6065aefb655Srie return (FIX_RELOC); 6075aefb655Srie 6085aefb655Srie case R_AMD64_TLSLD: 6095aefb655Srie /* 6105aefb655Srie * LD -> LE 6115aefb655Srie * 6125aefb655Srie * Transition 6135aefb655Srie * 0x00 leaq x1@tlsgd(%rip), %rdi 6145aefb655Srie * 0x07 call __tls_get_addr@plt 6155aefb655Srie * 0x0c 6165aefb655Srie * To: 6175aefb655Srie * 0x00 .byte 0x66 6185aefb655Srie * 0x01 .byte 0x66 6195aefb655Srie * 0x02 .byte 0x66 6205aefb655Srie * 0x03 movq %fs:0, %rax 6215aefb655Srie */ 622051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 623051d39bbSrie R_AMD64_NONE, arsp)); 6245aefb655Srie offset -= 3; 6255aefb655Srie (void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le)); 6265aefb655Srie return (FIX_DONE); 6275aefb655Srie 6285aefb655Srie case R_AMD64_DTPOFF32: 6295aefb655Srie /* 6305aefb655Srie * LD->LE 6315aefb655Srie * 6325aefb655Srie * Transition: 6335aefb655Srie * 0x00 leaq x1@dtpoff(%rax), %rcx 6345aefb655Srie * To: 6355aefb655Srie * 0x00 leaq x1@tpoff(%rax), %rcx 6365aefb655Srie */ 637051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 638051d39bbSrie R_AMD64_TPOFF32, arsp)); 6395aefb655Srie arsp->rel_rtype = R_AMD64_TPOFF32; 6405aefb655Srie arsp->rel_raddend = 0; 6415aefb655Srie return (FIX_RELOC); 6425aefb655Srie } 6435aefb655Srie 6445aefb655Srie return (FIX_RELOC); 6455aefb655Srie } 6465aefb655Srie 647ba2be530Sab196087 static uintptr_t 6485aefb655Srie ld_do_activerelocs(Ofl_desc *ofl) 6495aefb655Srie { 6505aefb655Srie Rel_desc *arsp; 6515aefb655Srie Rel_cache *rcp; 65257ef7aa9SRod Evans Aliste idx; 6535aefb655Srie uintptr_t return_code = 1; 6541d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags; 6555aefb655Srie 65657ef7aa9SRod Evans if (ofl->ofl_actrels) 6575aefb655Srie DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml)); 6587010c12aSrie 6595aefb655Srie /* 6605aefb655Srie * Process active relocations. 6615aefb655Srie */ 66257ef7aa9SRod Evans for (APLIST_TRAVERSE(ofl->ofl_actrels, idx, rcp)) { 6635aefb655Srie /* LINTED */ 6645aefb655Srie for (arsp = (Rel_desc *)(rcp + 1); 6655aefb655Srie arsp < rcp->rc_free; arsp++) { 6665aefb655Srie uchar_t *addr; 6675aefb655Srie Xword value; 6685aefb655Srie Sym_desc *sdp; 6695aefb655Srie const char *ifl_name; 6705aefb655Srie Xword refaddr; 6715aefb655Srie int moved = 0; 6725aefb655Srie Gotref gref; 6735aefb655Srie 6745aefb655Srie /* 6755aefb655Srie * If the section this relocation is against has been 6765aefb655Srie * discarded (-zignore), then discard (skip) the 6775aefb655Srie * relocation itself. 6785aefb655Srie */ 6795aefb655Srie if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) && 6805aefb655Srie ((arsp->rel_flags & 6815aefb655Srie (FLG_REL_GOT | FLG_REL_BSS | 6825aefb655Srie FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) { 6835aefb655Srie DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, 6845aefb655Srie M_MACH, arsp)); 6855aefb655Srie continue; 6865aefb655Srie } 6875aefb655Srie 6885aefb655Srie /* 689d2d5cf7cSAli Bahrami * We determine what the 'got reference' 6905aefb655Srie * model (if required) is at this point. This 6915aefb655Srie * needs to be done before tls_fixup() since 6925aefb655Srie * it may 'transition' our instructions. 6935aefb655Srie * 6945aefb655Srie * The got table entries have already been assigned, 6955aefb655Srie * and we bind to those initial entries. 6965aefb655Srie */ 6975aefb655Srie if (arsp->rel_flags & FLG_REL_DTLS) 6985aefb655Srie gref = GOT_REF_TLSGD; 6995aefb655Srie else if (arsp->rel_flags & FLG_REL_MTLS) 7005aefb655Srie gref = GOT_REF_TLSLD; 7015aefb655Srie else if (arsp->rel_flags & FLG_REL_STLS) 7025aefb655Srie gref = GOT_REF_TLSIE; 7035aefb655Srie else 7045aefb655Srie gref = GOT_REF_GENERIC; 7055aefb655Srie 7065aefb655Srie /* 7075aefb655Srie * Perform any required TLS fixups. 7085aefb655Srie */ 7095aefb655Srie if (arsp->rel_flags & FLG_REL_TLSFIX) { 7105aefb655Srie Fixupret ret; 7115aefb655Srie 7125aefb655Srie if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR) 7135aefb655Srie return (S_ERROR); 7145aefb655Srie if (ret == FIX_DONE) 7155aefb655Srie continue; 7165aefb655Srie } 7175aefb655Srie 7185aefb655Srie /* 7195aefb655Srie * If this is a relocation against a move table, or 7205aefb655Srie * expanded move table, adjust the relocation entries. 7215aefb655Srie */ 7225aefb655Srie if (arsp->rel_move) 7235aefb655Srie ld_adj_movereloc(ofl, arsp); 7245aefb655Srie 7255aefb655Srie sdp = arsp->rel_sym; 7265aefb655Srie refaddr = arsp->rel_roffset + 7275aefb655Srie (Off)_elf_getxoff(arsp->rel_isdesc->is_indata); 7285aefb655Srie 7295aefb655Srie if ((arsp->rel_flags & FLG_REL_CLVAL) || 7305aefb655Srie (arsp->rel_flags & FLG_REL_GOTCL)) 7315aefb655Srie value = 0; 7325aefb655Srie else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == 7335aefb655Srie STT_SECTION) { 7345aefb655Srie Sym_desc *sym; 7355aefb655Srie 7365aefb655Srie /* 7375aefb655Srie * The value for a symbol pointing to a SECTION 7385aefb655Srie * is based off of that sections position. 7395aefb655Srie */ 7405aefb655Srie if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 7415aefb655Srie /* LINTED */ 742bf2f215aSAli Bahrami (sym = ld_am_I_partial(arsp, 743bf2f215aSAli Bahrami arsp->rel_raddend))) { 7445aefb655Srie /* 745b26cc8daSAli Bahrami * The symbol was moved, so adjust 746b26cc8daSAli Bahrami * the value relative to the new 747b26cc8daSAli Bahrami * section. 7485aefb655Srie */ 7495aefb655Srie value = sym->sd_sym->st_value; 7505aefb655Srie 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; 7665aefb655Srie } else { 7675aefb655Srie value = _elf_getxoff( 7685aefb655Srie sdp->sd_isc->is_indata); 7695aefb655Srie if (sdp->sd_isc->is_shdr->sh_flags & 7705aefb655Srie SHF_ALLOC) 771de777a60Sab196087 value += 772de777a60Sab196087 sdp->sd_isc->is_osdesc-> 7735aefb655Srie os_shdr->sh_addr; 7745aefb655Srie } 7755aefb655Srie if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS) 7765aefb655Srie 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; 7835aefb655Srie } else { 7845aefb655Srie /* 7857010c12aSrie * Else the value is the symbols value. 7865aefb655Srie */ 7875aefb655Srie value = sdp->sd_sym->st_value; 7885aefb655Srie } 7895aefb655Srie 7905aefb655Srie /* 7915aefb655Srie * Relocation against the GLOBAL_OFFSET_TABLE. 7925aefb655Srie */ 7935aefb655Srie if (arsp->rel_flags & FLG_REL_GOT) 7945aefb655Srie arsp->rel_osdesc = ofl->ofl_osgot; 7955aefb655Srie 7965aefb655Srie /* 7975aefb655Srie * If loadable and not producing a relocatable object 7985aefb655Srie * add the sections virtual address to the reference 7995aefb655Srie * address. 8005aefb655Srie */ 8015aefb655Srie if ((arsp->rel_flags & FLG_REL_LOAD) && 8025aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) 8035aefb655Srie refaddr += arsp->rel_isdesc->is_osdesc-> 8045aefb655Srie os_shdr->sh_addr; 8055aefb655Srie 8065aefb655Srie /* 8075aefb655Srie * If this entry has a PLT assigned to it, it's 8085aefb655Srie * value is actually the address of the PLT (and 8095aefb655Srie * not the address of the function). 8105aefb655Srie */ 8115aefb655Srie if (IS_PLT(arsp->rel_rtype)) { 8125aefb655Srie if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx) 8135aefb655Srie value = ld_calc_plt_addr(sdp, ofl); 8145aefb655Srie } 8155aefb655Srie 8165aefb655Srie /* 8175aefb655Srie * Add relocations addend to value. Add extra 8185aefb655Srie * relocation addend if needed. 8195aefb655Srie * 8205aefb655Srie * Note: for GOT relative relocations on amd64 8215aefb655Srie * we discard the addend. It was relevant 8225aefb655Srie * to the reference - not to the data item 8235aefb655Srie * being referenced (ie: that -4 thing). 8245aefb655Srie */ 8255aefb655Srie if ((arsp->rel_flags & FLG_REL_GOT) == 0) 826bf2f215aSAli Bahrami value += arsp->rel_raddend; 8275aefb655Srie 8285aefb655Srie /* 8295aefb655Srie * Determine whether the value needs further adjustment. 8305aefb655Srie * Filter through the attributes of the relocation to 8315aefb655Srie * determine what adjustment is required. Note, many 8325aefb655Srie * of the following cases are only applicable when a 8335aefb655Srie * .got is present. As a .got is not generated when a 8345aefb655Srie * relocatable object is being built, any adjustments 8355aefb655Srie * that require a .got need to be skipped. 8365aefb655Srie */ 8375aefb655Srie if ((arsp->rel_flags & FLG_REL_GOT) && 8385aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8395aefb655Srie Xword R1addr; 8405aefb655Srie uintptr_t R2addr; 8415aefb655Srie Word gotndx; 8425aefb655Srie Gotndx *gnp; 8435aefb655Srie 8445aefb655Srie /* 8455aefb655Srie * Perform relocation against GOT table. Since 8465aefb655Srie * this doesn't fit exactly into a relocation 8475aefb655Srie * we place the appropriate byte in the GOT 8485aefb655Srie * directly 8495aefb655Srie * 8505aefb655Srie * Calculate offset into GOT at which to apply 8515aefb655Srie * the relocation. 8525aefb655Srie */ 85357ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, 8545aefb655Srie ofl, arsp); 8555aefb655Srie assert(gnp); 8565aefb655Srie 8575aefb655Srie if (arsp->rel_rtype == R_AMD64_DTPOFF64) 8585aefb655Srie gotndx = gnp->gn_gotndx + 1; 8595aefb655Srie else 8605aefb655Srie gotndx = gnp->gn_gotndx; 8615aefb655Srie 8625aefb655Srie R1addr = (Xword)(gotndx * M_GOT_ENTSIZE); 8635aefb655Srie 8645aefb655Srie /* 8655aefb655Srie * Add the GOTs data's offset. 8665aefb655Srie */ 8675aefb655Srie R2addr = R1addr + (uintptr_t) 8685aefb655Srie arsp->rel_osdesc->os_outdata->d_buf; 8695aefb655Srie 8705aefb655Srie DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, 871635216b6SRod Evans ELF_DBG_LD_ACT, M_MACH, SHT_RELA, 8725aefb655Srie arsp->rel_rtype, R1addr, value, 8735aefb655Srie arsp->rel_sname, arsp->rel_osdesc)); 8745aefb655Srie 8755aefb655Srie /* 8765aefb655Srie * And do it. 8775aefb655Srie */ 878f3324781Sab196087 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) 879f3324781Sab196087 *(Xword *)R2addr = 880ba2be530Sab196087 ld_bswap_Xword(value); 881f3324781Sab196087 else 8825aefb655Srie *(Xword *)R2addr = value; 8835aefb655Srie continue; 8845aefb655Srie 8855aefb655Srie } else if (IS_GOT_BASED(arsp->rel_rtype) && 8865aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8875aefb655Srie value -= ofl->ofl_osgot->os_shdr->sh_addr; 8885aefb655Srie 8895aefb655Srie } else if (IS_GOTPCREL(arsp->rel_rtype) && 8905aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8915aefb655Srie Gotndx *gnp; 8925aefb655Srie 8935aefb655Srie /* 8945aefb655Srie * Calculation: 8955aefb655Srie * G + GOT + A - P 8965aefb655Srie */ 89757ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 8985aefb655Srie gref, ofl, arsp); 8995aefb655Srie assert(gnp); 9005aefb655Srie value = (Xword)(ofl->ofl_osgot->os_shdr-> 9015aefb655Srie sh_addr) + ((Xword)gnp->gn_gotndx * 9025aefb655Srie M_GOT_ENTSIZE) + arsp->rel_raddend - 9035aefb655Srie refaddr; 9045aefb655Srie 9055aefb655Srie } else if (IS_GOT_PC(arsp->rel_rtype) && 9065aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 9075aefb655Srie value = (Xword)(ofl->ofl_osgot->os_shdr-> 9085aefb655Srie sh_addr) - refaddr + arsp->rel_raddend; 9095aefb655Srie 9105aefb655Srie } else if ((IS_PC_RELATIVE(arsp->rel_rtype)) && 9115aefb655Srie (((flags & FLG_OF_RELOBJ) == 0) || 9125aefb655Srie (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) { 9135aefb655Srie value -= refaddr; 9145aefb655Srie 9155aefb655Srie } else if (IS_TLS_INS(arsp->rel_rtype) && 9165aefb655Srie IS_GOT_RELATIVE(arsp->rel_rtype) && 9175aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 9185aefb655Srie Gotndx *gnp; 9195aefb655Srie 92057ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, 9215aefb655Srie ofl, arsp); 9225aefb655Srie assert(gnp); 9235aefb655Srie value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 9245aefb655Srie 9255aefb655Srie } else if (IS_GOT_RELATIVE(arsp->rel_rtype) && 9265aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 9275aefb655Srie Gotndx *gnp; 9285aefb655Srie 92957ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 9305aefb655Srie gref, ofl, arsp); 9315aefb655Srie assert(gnp); 9325aefb655Srie value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 9335aefb655Srie 9345aefb655Srie } else if ((arsp->rel_flags & FLG_REL_STLS) && 9355aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 9365aefb655Srie Xword tlsstatsize; 9375aefb655Srie 9385aefb655Srie /* 9395aefb655Srie * This is the LE TLS reference model. Static 9405aefb655Srie * offset is hard-coded. 9415aefb655Srie */ 9425aefb655Srie tlsstatsize = 9435aefb655Srie S_ROUND(ofl->ofl_tlsphdr->p_memsz, 9445aefb655Srie M_TLSSTATALIGN); 9455aefb655Srie value = tlsstatsize - value; 9465aefb655Srie 9475aefb655Srie /* 9485aefb655Srie * Since this code is fixed up, it assumes a 9495aefb655Srie * negative offset that can be added to the 9505aefb655Srie * thread pointer. 9515aefb655Srie */ 9525aefb655Srie if (arsp->rel_rtype == R_AMD64_TPOFF32) 9535aefb655Srie value = -value; 9545aefb655Srie } 9555aefb655Srie 9565aefb655Srie if (arsp->rel_isdesc->is_file) 9575aefb655Srie ifl_name = arsp->rel_isdesc->is_file->ifl_name; 9585aefb655Srie else 9595aefb655Srie ifl_name = MSG_INTL(MSG_STR_NULL); 9605aefb655Srie 9615aefb655Srie /* 9625aefb655Srie * Make sure we have data to relocate. Compiler and 9635aefb655Srie * assembler developers have been known to generate 9645aefb655Srie * relocations against invalid sections (normally .bss), 9655aefb655Srie * so for their benefit give them sufficient information 9665aefb655Srie * to help analyze the problem. End users should never 9675aefb655Srie * see this. 9685aefb655Srie */ 9695aefb655Srie if (arsp->rel_isdesc->is_indata->d_buf == 0) { 970de777a60Sab196087 Conv_inv_buf_t inv_buf; 971de777a60Sab196087 9725aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 9735aefb655Srie MSG_INTL(MSG_REL_EMPTYSEC), 974de777a60Sab196087 conv_reloc_amd64_type(arsp->rel_rtype, 975de777a60Sab196087 0, &inv_buf), ifl_name, 976de777a60Sab196087 demangle(arsp->rel_sname), 9774a8d0ea7SAli Bahrami EC_WORD(arsp->rel_isdesc->is_scnndx), 9785aefb655Srie arsp->rel_isdesc->is_name); 9795aefb655Srie return (S_ERROR); 9805aefb655Srie } 9815aefb655Srie 9825aefb655Srie /* 9835aefb655Srie * Get the address of the data item we need to modify. 9845aefb655Srie */ 9855aefb655Srie addr = (uchar_t *)((uintptr_t)arsp->rel_roffset + 9865aefb655Srie (uintptr_t)_elf_getxoff(arsp->rel_isdesc-> 9875aefb655Srie is_indata)); 9885aefb655Srie 989635216b6SRod Evans DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT, 9905aefb655Srie M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr), 9915aefb655Srie value, arsp->rel_sname, arsp->rel_osdesc)); 9925aefb655Srie addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf; 9935aefb655Srie 9945aefb655Srie if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) > 9955aefb655Srie ofl->ofl_size) || (arsp->rel_roffset > 9965aefb655Srie arsp->rel_osdesc->os_shdr->sh_size)) { 9975aefb655Srie int class; 998de777a60Sab196087 Conv_inv_buf_t inv_buf; 9995aefb655Srie 10005aefb655Srie if (((uintptr_t)addr - 10015aefb655Srie (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size) 10025aefb655Srie class = ERR_FATAL; 10035aefb655Srie else 10045aefb655Srie class = ERR_WARNING; 10055aefb655Srie 10065aefb655Srie eprintf(ofl->ofl_lml, class, 10075aefb655Srie MSG_INTL(MSG_REL_INVALOFFSET), 1008de777a60Sab196087 conv_reloc_amd64_type(arsp->rel_rtype, 1009de777a60Sab196087 0, &inv_buf), ifl_name, 10104a8d0ea7SAli Bahrami EC_WORD(arsp->rel_isdesc->is_scnndx), 1011de777a60Sab196087 arsp->rel_isdesc->is_name, 10125aefb655Srie demangle(arsp->rel_sname), 10135aefb655Srie EC_ADDR((uintptr_t)addr - 10145aefb655Srie (uintptr_t)ofl->ofl_nehdr)); 10155aefb655Srie 10165aefb655Srie if (class == ERR_FATAL) { 10175aefb655Srie return_code = S_ERROR; 10185aefb655Srie continue; 10195aefb655Srie } 10205aefb655Srie } 10215aefb655Srie 10225aefb655Srie /* 10235aefb655Srie * The relocation is additive. Ignore the previous 10245aefb655Srie * symbol value if this local partial symbol is 10255aefb655Srie * expanded. 10265aefb655Srie */ 10275aefb655Srie if (moved) 10285aefb655Srie value -= *addr; 10295aefb655Srie 10305aefb655Srie /* 1031f3324781Sab196087 * If '-z noreloc' is specified - skip the do_reloc_ld 10325aefb655Srie * stage. 10335aefb655Srie */ 1034f3324781Sab196087 if (OFL_DO_RELOC(ofl)) { 1035f3324781Sab196087 /* 1036f3324781Sab196087 * If this is a PROGBITS section and the 1037f3324781Sab196087 * running linker has a different byte order 1038f3324781Sab196087 * than the target host, tell do_reloc_ld() 1039f3324781Sab196087 * to swap bytes. 1040f3324781Sab196087 */ 1041f3324781Sab196087 if (do_reloc_ld((uchar_t)arsp->rel_rtype, 10425aefb655Srie addr, &value, arsp->rel_sname, ifl_name, 1043f3324781Sab196087 OFL_SWAP_RELOC_DATA(ofl, arsp), 10445aefb655Srie ofl->ofl_lml) == 0) 10455aefb655Srie return_code = S_ERROR; 10465aefb655Srie } 10475aefb655Srie } 10485aefb655Srie } 10495aefb655Srie return (return_code); 10505aefb655Srie } 10515aefb655Srie 1052ba2be530Sab196087 static uintptr_t 10535aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl) 10545aefb655Srie { 10555aefb655Srie Rel_desc *orsp; 10565aefb655Srie Rel_cache *rcp; 10575aefb655Srie Sym_desc *sdp = rsp->rel_sym; 105857ef7aa9SRod Evans static size_t nextsize = 0; 10595aefb655Srie 10605aefb655Srie /* 10615aefb655Srie * Static executables *do not* want any relocations against them. 10625aefb655Srie * Since our engine still creates relocations against a WEAK UNDEFINED 10635aefb655Srie * symbol in a static executable, it's best to disable them here 10645aefb655Srie * instead of through out the relocation code. 10655aefb655Srie */ 1066635216b6SRod Evans if (OFL_IS_STATIC_EXEC(ofl)) 10675aefb655Srie return (1); 10685aefb655Srie 10695aefb655Srie /* 107057ef7aa9SRod Evans * Obtain the new available relocation cache entry. 10715aefb655Srie */ 107257ef7aa9SRod Evans if ((rcp = ld_add_rel_cache(ofl, &ofl->ofl_outrels, &nextsize, 107357ef7aa9SRod Evans REL_LOIDESCNO, REL_HOIDESCNO)) == (Rel_cache *)S_ERROR) 10745aefb655Srie return (S_ERROR); 10755aefb655Srie 107657ef7aa9SRod Evans orsp = rcp->rc_free; 10775aefb655Srie 10785aefb655Srie /* 10795aefb655Srie * If we are adding a output relocation against a section 10805aefb655Srie * symbol (non-RELATIVE) then mark that section. These sections 10815aefb655Srie * will be added to the .dynsym symbol table. 10825aefb655Srie */ 10835aefb655Srie if (sdp && (rsp->rel_rtype != M_R_RELATIVE) && 10845aefb655Srie ((flags & FLG_REL_SCNNDX) || 10855aefb655Srie (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) { 10865aefb655Srie 10875aefb655Srie /* 10885aefb655Srie * If this is a COMMON symbol - no output section 10895aefb655Srie * exists yet - (it's created as part of sym_validate()). 10905aefb655Srie * So - we mark here that when it's created it should 10915aefb655Srie * be tagged with the FLG_OS_OUTREL flag. 10925aefb655Srie */ 10935aefb655Srie if ((sdp->sd_flags & FLG_SY_SPECSEC) && 10940bc07c75Srie (sdp->sd_sym->st_shndx == SHN_COMMON)) { 10955aefb655Srie if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS) 10965aefb655Srie ofl->ofl_flags1 |= FLG_OF1_BSSOREL; 10975aefb655Srie else 10985aefb655Srie ofl->ofl_flags1 |= FLG_OF1_TLSOREL; 10995aefb655Srie } else { 11005aefb655Srie Os_desc *osp = sdp->sd_isc->is_osdesc; 11015aefb655Srie 1102c1c6f601Srie if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) { 11035aefb655Srie ofl->ofl_dynshdrcnt++; 11045aefb655Srie osp->os_flags |= FLG_OS_OUTREL; 11055aefb655Srie } 11065aefb655Srie } 11075aefb655Srie } 11085aefb655Srie 11095aefb655Srie *orsp = *rsp; 11105aefb655Srie orsp->rel_flags |= flags; 11115aefb655Srie 11125aefb655Srie rcp->rc_free++; 11135aefb655Srie ofl->ofl_outrelscnt++; 11145aefb655Srie 11155aefb655Srie if (flags & FLG_REL_GOT) 11165aefb655Srie ofl->ofl_relocgotsz += (Xword)sizeof (Rela); 11175aefb655Srie else if (flags & FLG_REL_PLT) 11185aefb655Srie ofl->ofl_relocpltsz += (Xword)sizeof (Rela); 11195aefb655Srie else if (flags & FLG_REL_BSS) 11205aefb655Srie ofl->ofl_relocbsssz += (Xword)sizeof (Rela); 11215aefb655Srie else if (flags & FLG_REL_NOINFO) 11225aefb655Srie ofl->ofl_relocrelsz += (Xword)sizeof (Rela); 11235aefb655Srie else 11245aefb655Srie orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela); 11255aefb655Srie 11265aefb655Srie if (orsp->rel_rtype == M_R_RELATIVE) 11275aefb655Srie ofl->ofl_relocrelcnt++; 11285aefb655Srie 11295aefb655Srie /* 11305aefb655Srie * We don't perform sorting on PLT relocations because 11315aefb655Srie * they have already been assigned a PLT index and if we 11325aefb655Srie * were to sort them we would have to re-assign the plt indexes. 11335aefb655Srie */ 11345aefb655Srie if (!(flags & FLG_REL_PLT)) 11355aefb655Srie ofl->ofl_reloccnt++; 11365aefb655Srie 11375aefb655Srie /* 11385aefb655Srie * Insure a GLOBAL_OFFSET_TABLE is generated if required. 11395aefb655Srie */ 11405aefb655Srie if (IS_GOT_REQUIRED(orsp->rel_rtype)) 11415aefb655Srie ofl->ofl_flags |= FLG_OF_BLDGOT; 11425aefb655Srie 11435aefb655Srie /* 11445aefb655Srie * Identify and possibly warn of a displacement relocation. 11455aefb655Srie */ 11465aefb655Srie if (orsp->rel_flags & FLG_REL_DISP) { 11475aefb655Srie ofl->ofl_dtflags_1 |= DF_1_DISPRELPND; 11485aefb655Srie 11495aefb655Srie if (ofl->ofl_flags & FLG_OF_VERBOSE) 11505aefb655Srie ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl); 11515aefb655Srie } 11525aefb655Srie DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA, 11535aefb655Srie M_MACH, orsp)); 11545aefb655Srie return (1); 11555aefb655Srie } 11565aefb655Srie 11575aefb655Srie /* 11585aefb655Srie * process relocation for a LOCAL symbol 11595aefb655Srie */ 1160ba2be530Sab196087 static uintptr_t 11615aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl) 11625aefb655Srie { 11631d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags; 11645aefb655Srie Sym_desc *sdp = rsp->rel_sym; 11650bc07c75Srie Word shndx = sdp->sd_sym->st_shndx; 11665aefb655Srie Word ortype = rsp->rel_rtype; 11675aefb655Srie 11685aefb655Srie /* 11695aefb655Srie * if ((shared object) and (not pc relative relocation) and 11705aefb655Srie * (not against ABS symbol)) 11715aefb655Srie * then 11725aefb655Srie * build R_AMD64_RELATIVE 11735aefb655Srie * fi 11745aefb655Srie */ 11755aefb655Srie if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) && 11762926dd2eSrie !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) && 11775aefb655Srie !(IS_GOT_BASED(rsp->rel_rtype)) && 11785aefb655Srie !(rsp->rel_isdesc != NULL && 11795aefb655Srie (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) && 11805aefb655Srie (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) || 11815aefb655Srie (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) { 11825aefb655Srie 11835aefb655Srie /* 11845aefb655Srie * R_AMD64_RELATIVE updates a 64bit address, if this 11855aefb655Srie * relocation isn't a 64bit binding then we can not 11865aefb655Srie * simplify it to a RELATIVE relocation. 11875aefb655Srie */ 11885aefb655Srie if (reloc_table[ortype].re_fsize != sizeof (Addr)) { 11891d9df23bSab196087 return (ld_add_outrel(0, rsp, ofl)); 11905aefb655Srie } 11915aefb655Srie 11925aefb655Srie rsp->rel_rtype = R_AMD64_RELATIVE; 11935aefb655Srie if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR) 11945aefb655Srie return (S_ERROR); 11955aefb655Srie rsp->rel_rtype = ortype; 11965aefb655Srie return (1); 11975aefb655Srie } 11985aefb655Srie 11995aefb655Srie /* 12005aefb655Srie * If the relocation is against a 'non-allocatable' section 12015aefb655Srie * and we can not resolve it now - then give a warning 12025aefb655Srie * message. 12035aefb655Srie * 12045aefb655Srie * We can not resolve the symbol if either: 12055aefb655Srie * a) it's undefined 12065aefb655Srie * b) it's defined in a shared library and a 12075aefb655Srie * COPY relocation hasn't moved it to the executable 12085aefb655Srie * 12095aefb655Srie * Note: because we process all of the relocations against the 12105aefb655Srie * text segment before any others - we know whether 12115aefb655Srie * or not a copy relocation will be generated before 12125aefb655Srie * we get here (see reloc_init()->reloc_segments()). 12135aefb655Srie */ 12145aefb655Srie if (!(rsp->rel_flags & FLG_REL_LOAD) && 12155aefb655Srie ((shndx == SHN_UNDEF) || 12165aefb655Srie ((sdp->sd_ref == REF_DYN_NEED) && 12175aefb655Srie ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) { 1218de777a60Sab196087 Conv_inv_buf_t inv_buf; 1219de777a60Sab196087 12205aefb655Srie /* 12215aefb655Srie * If the relocation is against a SHT_SUNW_ANNOTATE 12225aefb655Srie * section - then silently ignore that the relocation 12235aefb655Srie * can not be resolved. 12245aefb655Srie */ 12255aefb655Srie if (rsp->rel_osdesc && 12265aefb655Srie (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE)) 12275aefb655Srie return (0); 12285aefb655Srie (void) eprintf(ofl->ofl_lml, ERR_WARNING, 12295aefb655Srie MSG_INTL(MSG_REL_EXTERNSYM), 1230de777a60Sab196087 conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf), 12315aefb655Srie rsp->rel_isdesc->is_file->ifl_name, 12325aefb655Srie demangle(rsp->rel_sname), rsp->rel_osdesc->os_name); 12335aefb655Srie return (1); 12345aefb655Srie } 12355aefb655Srie 12365aefb655Srie /* 12375aefb655Srie * Perform relocation. 12385aefb655Srie */ 12395aefb655Srie return (ld_add_actrel(NULL, rsp, ofl)); 12405aefb655Srie } 12415aefb655Srie 12425aefb655Srie 1243ba2be530Sab196087 static uintptr_t 12445aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl) 12455aefb655Srie { 12465aefb655Srie Word rtype = rsp->rel_rtype; 12475aefb655Srie Sym_desc *sdp = rsp->rel_sym; 12481d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags; 12495aefb655Srie Gotndx *gnp; 12505aefb655Srie 12515aefb655Srie /* 1252d326b23bSrie * If we're building an executable - use either the IE or LE access 1253d326b23bSrie * model. If we're building a shared object process any IE model. 12545aefb655Srie */ 1255d326b23bSrie if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) { 12565aefb655Srie /* 1257d326b23bSrie * Set the DF_STATIC_TLS flag. 12585aefb655Srie */ 12595aefb655Srie ofl->ofl_dtflags |= DF_STATIC_TLS; 12605aefb655Srie 1261d326b23bSrie if (!local || ((flags & FLG_OF_EXEC) == 0)) { 12625aefb655Srie /* 1263d326b23bSrie * Assign a GOT entry for static TLS references. 12645aefb655Srie */ 126557ef7aa9SRod Evans if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 126657ef7aa9SRod Evans GOT_REF_TLSIE, ofl, rsp)) == NULL) { 1267d326b23bSrie 1268d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, 1269d326b23bSrie gnp, GOT_REF_TLSIE, FLG_REL_STLS, 1270d326b23bSrie rtype, R_AMD64_TPOFF64, 0) == S_ERROR) 12715aefb655Srie return (S_ERROR); 12725aefb655Srie } 1273d326b23bSrie 1274d326b23bSrie /* 1275d326b23bSrie * IE access model. 1276d326b23bSrie */ 12775aefb655Srie if (IS_TLS_IE(rtype)) 12785aefb655Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 12795aefb655Srie 12805aefb655Srie /* 1281d326b23bSrie * Fixups are required for other executable models. 12825aefb655Srie */ 12835aefb655Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 12845aefb655Srie rsp, ofl)); 12855aefb655Srie } 1286d326b23bSrie 12875aefb655Srie /* 1288d326b23bSrie * LE access model. 12895aefb655Srie */ 12905aefb655Srie if (IS_TLS_LE(rtype)) 12915aefb655Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 1292d326b23bSrie 12935aefb655Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 12945aefb655Srie rsp, ofl)); 12955aefb655Srie } 12965aefb655Srie 12975aefb655Srie /* 1298d326b23bSrie * Building a shared object. 1299d326b23bSrie * 1300d326b23bSrie * Assign a GOT entry for a dynamic TLS reference. 13015aefb655Srie */ 130257ef7aa9SRod Evans if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 130357ef7aa9SRod Evans GOT_REF_TLSLD, ofl, rsp)) == NULL)) { 1304d326b23bSrie 1305d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD, 130657ef7aa9SRod Evans FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, NULL) == S_ERROR) 13075aefb655Srie return (S_ERROR); 1308d326b23bSrie 13095aefb655Srie } else if (IS_TLS_GD(rtype) && 131057ef7aa9SRod Evans ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD, 131157ef7aa9SRod Evans ofl, rsp)) == NULL)) { 1312d326b23bSrie 1313d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD, 1314d326b23bSrie FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64, 1315d326b23bSrie R_AMD64_DTPOFF64) == S_ERROR) 13165aefb655Srie return (S_ERROR); 13175aefb655Srie } 13185aefb655Srie 13195aefb655Srie if (IS_TLS_LD(rtype)) 13205aefb655Srie return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl)); 13215aefb655Srie 13225aefb655Srie return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl)); 13235aefb655Srie } 13245aefb655Srie 13255aefb655Srie /* ARGSUSED5 */ 1326ba2be530Sab196087 static uintptr_t 132757ef7aa9SRod Evans ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl, 13285aefb655Srie Rel_desc *rsp, Sym_desc *sdp) 13295aefb655Srie { 13305aefb655Srie Xword raddend; 133157ef7aa9SRod Evans Gotndx gn, *gnp; 133257ef7aa9SRod Evans Aliste idx; 13335aefb655Srie uint_t gotents; 13345aefb655Srie 13355aefb655Srie raddend = rsp->rel_raddend; 133657ef7aa9SRod Evans if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) 13375aefb655Srie return (1); 13385aefb655Srie 13395aefb655Srie if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD)) 13405aefb655Srie gotents = 2; 13415aefb655Srie else 13425aefb655Srie gotents = 1; 13435aefb655Srie 134457ef7aa9SRod Evans gn.gn_addend = raddend; 134557ef7aa9SRod Evans gn.gn_gotndx = ofl->ofl_gotcnt; 134657ef7aa9SRod Evans gn.gn_gotref = gref; 13475aefb655Srie 13485aefb655Srie ofl->ofl_gotcnt += gotents; 13495aefb655Srie 13505aefb655Srie if (gref == GOT_REF_TLSLD) { 135157ef7aa9SRod Evans if (ofl->ofl_tlsldgotndx == NULL) { 135257ef7aa9SRod Evans if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL) 135357ef7aa9SRod Evans return (S_ERROR); 135457ef7aa9SRod Evans (void) memcpy(gnp, &gn, sizeof (Gotndx)); 13555aefb655Srie ofl->ofl_tlsldgotndx = gnp; 135657ef7aa9SRod Evans } 13575aefb655Srie return (1); 13585aefb655Srie } 13595aefb655Srie 136057ef7aa9SRod Evans idx = 0; 136157ef7aa9SRod Evans for (ALIST_TRAVERSE(*alpp, idx, gnp)) { 136257ef7aa9SRod Evans if (gnp->gn_addend > raddend) 136357ef7aa9SRod Evans break; 13645aefb655Srie } 136557ef7aa9SRod Evans 136657ef7aa9SRod Evans /* 136757ef7aa9SRod Evans * GOT indexes are maintained on an Alist, where there is typically 136857ef7aa9SRod Evans * only one index. The usage of this list is to scan the list to find 136957ef7aa9SRod Evans * an index, and then apply that index immediately to a relocation. 137057ef7aa9SRod Evans * Thus there are no external references to these GOT index structures 137157ef7aa9SRod Evans * that can be compromised by the Alist being reallocated. 137257ef7aa9SRod Evans */ 137357ef7aa9SRod Evans if (alist_insert(alpp, &gn, sizeof (Gotndx), 137457ef7aa9SRod Evans AL_CNT_SDP_GOT, idx) == NULL) 137557ef7aa9SRod Evans return (S_ERROR); 137657ef7aa9SRod Evans 13775aefb655Srie return (1); 13785aefb655Srie } 13795aefb655Srie 1380ba2be530Sab196087 static void 13815aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl) 13825aefb655Srie { 13835aefb655Srie sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++; 13845aefb655Srie sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++; 13855aefb655Srie ofl->ofl_flags |= FLG_OF_BLDGOT; 13865aefb655Srie } 13875aefb655Srie 13885aefb655Srie static uchar_t plt0_template[M_PLT_ENTSIZE] = { 13895aefb655Srie /* 0x00 PUSHQ GOT+8(%rip) */ 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, 13905aefb655Srie /* 0x06 JMP *GOT+16(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 13915aefb655Srie /* 0x0c NOP */ 0x90, 13925aefb655Srie /* 0x0d NOP */ 0x90, 13935aefb655Srie /* 0x0e NOP */ 0x90, 13945aefb655Srie /* 0x0f NOP */ 0x90 13955aefb655Srie }; 13965aefb655Srie 13975aefb655Srie /* 13985aefb655Srie * Initializes .got[0] with the _DYNAMIC symbol value. 13995aefb655Srie */ 1400ba2be530Sab196087 static uintptr_t 14015aefb655Srie ld_fillin_gotplt(Ofl_desc *ofl) 14025aefb655Srie { 1403ba2be530Sab196087 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 1404ba2be530Sab196087 14055aefb655Srie if (ofl->ofl_osgot) { 14065aefb655Srie Sym_desc *sdp; 14075aefb655Srie 14085aefb655Srie if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U), 1409635216b6SRod Evans SYM_NOHASH, NULL, ofl)) != NULL) { 1410d326b23bSrie uchar_t *genptr; 1411d326b23bSrie 1412d326b23bSrie genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf + 14135aefb655Srie (M_GOT_XDYNAMIC * M_GOT_ENTSIZE)); 14145aefb655Srie /* LINTED */ 14155aefb655Srie *(Xword *)genptr = sdp->sd_sym->st_value; 1416ba2be530Sab196087 if (bswap) 1417ba2be530Sab196087 /* LINTED */ 1418ba2be530Sab196087 *(Xword *)genptr = 1419ba2be530Sab196087 /* LINTED */ 1420ba2be530Sab196087 ld_bswap_Xword(*(Xword *)genptr); 14215aefb655Srie } 14225aefb655Srie } 14235aefb655Srie 14245aefb655Srie /* 14255aefb655Srie * Fill in the reserved slot in the procedure linkage table the first 14265aefb655Srie * entry is: 14275aefb655Srie * 0x00 PUSHQ GOT+8(%rip) # GOT[1] 14285aefb655Srie * 0x06 JMP *GOT+16(%rip) # GOT[2] 14295aefb655Srie * 0x0c NOP 14305aefb655Srie * 0x0d NOP 14315aefb655Srie * 0x0e NOP 14325aefb655Srie * 0x0f NOP 14335aefb655Srie */ 1434f3324781Sab196087 if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) { 14355aefb655Srie uchar_t *pltent; 14365aefb655Srie Xword val1; 14375aefb655Srie 14385aefb655Srie pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf; 14395aefb655Srie bcopy(plt0_template, pltent, sizeof (plt0_template)); 14405aefb655Srie 14415aefb655Srie /* 1442f3324781Sab196087 * If '-z noreloc' is specified - skip the do_reloc_ld 1443f3324781Sab196087 * stage. 1444f3324781Sab196087 */ 1445f3324781Sab196087 if (!OFL_DO_RELOC(ofl)) 1446f3324781Sab196087 return (1); 1447f3324781Sab196087 1448f3324781Sab196087 /* 14495aefb655Srie * filin: 14505aefb655Srie * PUSHQ GOT + 8(%rip) 14515aefb655Srie * 14525aefb655Srie * Note: 0x06 below represents the offset to the 14535aefb655Srie * next instruction - which is what %rip will 14545aefb655Srie * be pointing at. 14555aefb655Srie */ 14565aefb655Srie val1 = (ofl->ofl_osgot->os_shdr->sh_addr) + 14575aefb655Srie (M_GOT_XLINKMAP * M_GOT_ENTSIZE) - 14585aefb655Srie ofl->ofl_osplt->os_shdr->sh_addr - 0x06; 14595aefb655Srie 1460f3324781Sab196087 if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02], 14615aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 1462f3324781Sab196087 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 14635aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 14645aefb655Srie MSG_INTL(MSG_PLT_PLT0FAIL)); 14655aefb655Srie return (S_ERROR); 14665aefb655Srie } 14675aefb655Srie 14685aefb655Srie /* 14695aefb655Srie * filin: 14705aefb655Srie * JMP *GOT+16(%rip) 14715aefb655Srie */ 14725aefb655Srie val1 = (ofl->ofl_osgot->os_shdr->sh_addr) + 14735aefb655Srie (M_GOT_XRTLD * M_GOT_ENTSIZE) - 14745aefb655Srie ofl->ofl_osplt->os_shdr->sh_addr - 0x0c; 1475f3324781Sab196087 1476f3324781Sab196087 if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x08], 14775aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 1478f3324781Sab196087 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 14795aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 14805aefb655Srie MSG_INTL(MSG_PLT_PLT0FAIL)); 14815aefb655Srie return (S_ERROR); 14825aefb655Srie } 14835aefb655Srie } 1484f3324781Sab196087 14855aefb655Srie return (1); 14865aefb655Srie } 1487ba2be530Sab196087 1488ba2be530Sab196087 1489ba2be530Sab196087 1490ba2be530Sab196087 /* 1491ba2be530Sab196087 * Template for generating "void (*)(void)" function 1492ba2be530Sab196087 */ 1493ba2be530Sab196087 static const uchar_t nullfunc_tmpl[] = { /* amd64 */ 1494ba2be530Sab196087 /* 0x00 */ 0x55, /* pushq %rbp */ 1495ba2be530Sab196087 /* 0x01 */ 0x48, 0x8b, 0xec, /* movq %rsp,%rbp */ 1496ba2be530Sab196087 /* 0x04 */ 0x48, 0x8b, 0xe5, /* movq %rbp,%rsp */ 1497ba2be530Sab196087 /* 0x07 */ 0x5d, /* popq %rbp */ 1498ba2be530Sab196087 /* 0x08 */ 0xc3 /* ret */ 1499ba2be530Sab196087 }; 1500ba2be530Sab196087 1501ba2be530Sab196087 1502ba2be530Sab196087 /* 15033c573fccSAli Bahrami * Function used to provide fill padding in SHF_EXECINSTR sections 15043c573fccSAli Bahrami * 15053c573fccSAli Bahrami * entry: 15063c573fccSAli Bahrami * 15073c573fccSAli Bahrami * base - base address of section being filled 15083c573fccSAli Bahrami * offset - starting offset for fill within memory referenced by base 15093c573fccSAli Bahrami * cnt - # bytes to be filled 15103c573fccSAli Bahrami * 15113c573fccSAli Bahrami * exit: 15123c573fccSAli Bahrami * The fill has been completed. 15133c573fccSAli Bahrami */ 15143c573fccSAli Bahrami static void 15153c573fccSAli Bahrami execfill(void *base, off_t off, size_t cnt) 15163c573fccSAli Bahrami { 15173c573fccSAli Bahrami /* 15183c573fccSAli Bahrami * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds. 15193c573fccSAli Bahrami * There are no alignment constraints. 15203c573fccSAli Bahrami */ 15213c573fccSAli Bahrami (void) memset(off + (char *)base, 0x90, cnt); 15223c573fccSAli Bahrami } 15233c573fccSAli Bahrami 15243c573fccSAli Bahrami 15253c573fccSAli Bahrami /* 1526ba2be530Sab196087 * Return the ld_targ definition for this target. 1527ba2be530Sab196087 */ 1528ba2be530Sab196087 const Target * 1529ba2be530Sab196087 ld_targ_init_x86(void) 1530ba2be530Sab196087 { 1531ba2be530Sab196087 static const Target _ld_targ = { 1532ba2be530Sab196087 { /* Target_mach */ 1533ba2be530Sab196087 M_MACH, /* m_mach */ 1534ba2be530Sab196087 M_MACHPLUS, /* m_machplus */ 1535ba2be530Sab196087 M_FLAGSPLUS, /* m_flagsplus */ 1536ba2be530Sab196087 M_CLASS, /* m_class */ 1537ba2be530Sab196087 M_DATA, /* m_data */ 1538ba2be530Sab196087 1539ba2be530Sab196087 M_SEGM_ALIGN, /* m_segm_align */ 1540ba2be530Sab196087 M_SEGM_ORIGIN, /* m_segm_origin */ 1541bb3b4f6cSRod Evans M_SEGM_AORIGIN, /* m_segm_aorigin */ 1542ba2be530Sab196087 M_DATASEG_PERM, /* m_dataseg_perm */ 1543*69112eddSAli Bahrami M_STACK_PERM, /* m_stack_perm */ 1544ba2be530Sab196087 M_WORD_ALIGN, /* m_word_align */ 1545ba2be530Sab196087 MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */ 1546ba2be530Sab196087 1547ba2be530Sab196087 /* Relocation type codes */ 1548ba2be530Sab196087 M_R_ARRAYADDR, /* m_r_arrayaddr */ 1549ba2be530Sab196087 M_R_COPY, /* m_r_copy */ 1550ba2be530Sab196087 M_R_GLOB_DAT, /* m_r_glob_dat */ 1551ba2be530Sab196087 M_R_JMP_SLOT, /* m_r_jmp_slot */ 1552ba2be530Sab196087 M_R_NUM, /* m_r_num */ 1553ba2be530Sab196087 M_R_NONE, /* m_r_none */ 1554ba2be530Sab196087 M_R_RELATIVE, /* m_r_relative */ 1555ba2be530Sab196087 M_R_REGISTER, /* m_r_register */ 1556ba2be530Sab196087 1557ba2be530Sab196087 /* Relocation related constants */ 1558ba2be530Sab196087 M_REL_DT_COUNT, /* m_rel_dt_count */ 1559ba2be530Sab196087 M_REL_DT_ENT, /* m_rel_dt_ent */ 1560ba2be530Sab196087 M_REL_DT_SIZE, /* m_rel_dt_size */ 1561ba2be530Sab196087 M_REL_DT_TYPE, /* m_rel_dt_type */ 1562ba2be530Sab196087 M_REL_SHT_TYPE, /* m_rel_sht_type */ 1563ba2be530Sab196087 1564ba2be530Sab196087 /* GOT related constants */ 1565ba2be530Sab196087 M_GOT_ENTSIZE, /* m_got_entsize */ 1566ba2be530Sab196087 M_GOT_XNumber, /* m_got_xnumber */ 1567ba2be530Sab196087 1568ba2be530Sab196087 /* PLT related constants */ 1569ba2be530Sab196087 M_PLT_ALIGN, /* m_plt_align */ 1570ba2be530Sab196087 M_PLT_ENTSIZE, /* m_plt_entsize */ 1571ba2be530Sab196087 M_PLT_RESERVSZ, /* m_plt_reservsz */ 1572ba2be530Sab196087 M_PLT_SHF_FLAGS, /* m_plt_shf_flags */ 1573ba2be530Sab196087 15747e16fca0SAli Bahrami /* Section type of .eh_frame/.eh_frame_hdr sections */ 15757e16fca0SAli Bahrami SHT_AMD64_UNWIND, /* m_sht_unwind */ 15767e16fca0SAli Bahrami 1577ba2be530Sab196087 M_DT_REGISTER, /* m_dt_register */ 1578ba2be530Sab196087 }, 1579ba2be530Sab196087 { /* Target_machid */ 1580ba2be530Sab196087 M_ID_ARRAY, /* id_array */ 1581ba2be530Sab196087 M_ID_BSS, /* id_bss */ 1582ba2be530Sab196087 M_ID_CAP, /* id_cap */ 1583ba2be530Sab196087 M_ID_DATA, /* id_data */ 1584ba2be530Sab196087 M_ID_DYNAMIC, /* id_dynamic */ 1585ba2be530Sab196087 M_ID_DYNSORT, /* id_dynsort */ 1586ba2be530Sab196087 M_ID_DYNSTR, /* id_dynstr */ 1587ba2be530Sab196087 M_ID_DYNSYM, /* id_dynsym */ 1588ba2be530Sab196087 M_ID_DYNSYM_NDX, /* id_dynsym_ndx */ 1589ba2be530Sab196087 M_ID_GOT, /* id_got */ 1590ba2be530Sab196087 M_ID_UNKNOWN, /* id_gotdata (unused) */ 1591ba2be530Sab196087 M_ID_HASH, /* id_hash */ 1592ba2be530Sab196087 M_ID_INTERP, /* id_interp */ 1593ba2be530Sab196087 M_ID_LBSS, /* id_lbss */ 1594ba2be530Sab196087 M_ID_LDYNSYM, /* id_ldynsym */ 1595ba2be530Sab196087 M_ID_NOTE, /* id_note */ 1596ba2be530Sab196087 M_ID_NULL, /* id_null */ 1597ba2be530Sab196087 M_ID_PLT, /* id_plt */ 1598ba2be530Sab196087 M_ID_REL, /* id_rel */ 1599ba2be530Sab196087 M_ID_STRTAB, /* id_strtab */ 1600ba2be530Sab196087 M_ID_SYMINFO, /* id_syminfo */ 1601ba2be530Sab196087 M_ID_SYMTAB, /* id_symtab */ 1602ba2be530Sab196087 M_ID_SYMTAB_NDX, /* id_symtab_ndx */ 1603ba2be530Sab196087 M_ID_TEXT, /* id_text */ 1604ba2be530Sab196087 M_ID_TLS, /* id_tls */ 1605ba2be530Sab196087 M_ID_TLSBSS, /* id_tlsbss */ 1606ba2be530Sab196087 M_ID_UNKNOWN, /* id_unknown */ 1607ba2be530Sab196087 M_ID_UNWIND, /* id_unwind */ 16087e16fca0SAli Bahrami M_ID_UNWINDHDR, /* id_unwindhdr */ 1609ba2be530Sab196087 M_ID_USER, /* id_user */ 1610ba2be530Sab196087 M_ID_VERSION, /* id_version */ 1611ba2be530Sab196087 }, 1612ba2be530Sab196087 { /* Target_nullfunc */ 1613ba2be530Sab196087 nullfunc_tmpl, /* nf_template */ 1614ba2be530Sab196087 sizeof (nullfunc_tmpl), /* nf_size */ 1615ba2be530Sab196087 }, 16163c573fccSAli Bahrami { /* Target_fillfunc */ 16173c573fccSAli Bahrami execfill /* ff_execfill */ 16183c573fccSAli Bahrami }, 1619ba2be530Sab196087 { /* Target_machrel */ 1620ba2be530Sab196087 reloc_table, 1621ba2be530Sab196087 1622ba2be530Sab196087 ld_init_rel, /* mr_init_rel */ 1623ba2be530Sab196087 ld_mach_eflags, /* mr_mach_eflags */ 1624ba2be530Sab196087 ld_mach_make_dynamic, /* mr_mach_make_dynamic */ 1625ba2be530Sab196087 ld_mach_update_odynamic, /* mr_mach_update_odynamic */ 1626ba2be530Sab196087 ld_calc_plt_addr, /* mr_calc_plt_addr */ 1627ba2be530Sab196087 ld_perform_outreloc, /* mr_perform_outreloc */ 1628ba2be530Sab196087 ld_do_activerelocs, /* mr_do_activerelocs */ 1629ba2be530Sab196087 ld_add_outrel, /* mr_add_outrel */ 1630ba2be530Sab196087 NULL, /* mr_reloc_register */ 1631ba2be530Sab196087 ld_reloc_local, /* mr_reloc_local */ 1632ba2be530Sab196087 NULL, /* mr_reloc_GOTOP */ 1633ba2be530Sab196087 ld_reloc_TLS, /* mr_reloc_TLS */ 1634ba2be530Sab196087 NULL, /* mr_assign_got */ 163557ef7aa9SRod Evans ld_find_got_ndx, /* mr_find_got_ndx */ 1636ba2be530Sab196087 ld_calc_got_offset, /* mr_calc_got_offset */ 1637ba2be530Sab196087 ld_assign_got_ndx, /* mr_assign_got_ndx */ 1638ba2be530Sab196087 ld_assign_plt_ndx, /* mr_assign_plt_ndx */ 1639ba2be530Sab196087 NULL, /* mr_allocate_got */ 1640ba2be530Sab196087 ld_fillin_gotplt, /* mr_fillin_gotplt */ 1641ba2be530Sab196087 }, 1642ba2be530Sab196087 { /* Target_machsym */ 1643ba2be530Sab196087 NULL, /* ms_reg_check */ 1644ba2be530Sab196087 NULL, /* ms_mach_sym_typecheck */ 1645ba2be530Sab196087 NULL, /* ms_is_regsym */ 1646ba2be530Sab196087 NULL, /* ms_reg_find */ 1647ba2be530Sab196087 NULL /* ms_reg_enter */ 1648ba2be530Sab196087 } 1649ba2be530Sab196087 }; 1650ba2be530Sab196087 1651ba2be530Sab196087 return (&_ld_targ); 1652ba2be530Sab196087 } 1653