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 /* 23ba2be530Sab196087 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 245aefb655Srie * Use is subject to license terms. 255aefb655Srie */ 265aefb655Srie #pragma ident "%Z%%M% %I% %E% SMI" 275aefb655Srie 28ba2be530Sab196087 /* Get the x86 version of the relocation engine */ 29ba2be530Sab196087 #define DO_RELOC_LIBLD_X86 30ba2be530Sab196087 315aefb655Srie #include <string.h> 325aefb655Srie #include <stdio.h> 335aefb655Srie #include <strings.h> 345aefb655Srie #include <sys/elf_amd64.h> 355aefb655Srie #include <debug.h> 365aefb655Srie #include <reloc.h> 37ba2be530Sab196087 #include <i386/machdep_x86.h> 385aefb655Srie #include "msg.h" 395aefb655Srie #include "_libld.h" 40ba2be530Sab196087 #include "unwind.amd.h" 415aefb655Srie 42ba2be530Sab196087 43ba2be530Sab196087 /* Forward declarations */ 44ba2be530Sab196087 static Gotndx *ld_find_gotndx(List *, Gotref, Ofl_desc *, Rel_desc *); 45ba2be530Sab196087 static Xword ld_calc_got_offset(Rel_desc *, Ofl_desc *); 46ba2be530Sab196087 47ba2be530Sab196087 48ba2be530Sab196087 static Word 495aefb655Srie ld_init_rel(Rel_desc *reld, void *reloc) 505aefb655Srie { 515aefb655Srie Rela * rel = (Rela *)reloc; 525aefb655Srie 535aefb655Srie /* LINTED */ 54ba2be530Sab196087 reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH); 555aefb655Srie reld->rel_roffset = rel->r_offset; 565aefb655Srie reld->rel_raddend = rel->r_addend; 575aefb655Srie reld->rel_typedata = 0; 585aefb655Srie 595aefb655Srie reld->rel_flags |= FLG_REL_RELA; 605aefb655Srie 615aefb655Srie return ((Word)ELF_R_SYM(rel->r_info)); 625aefb655Srie } 635aefb655Srie 64ba2be530Sab196087 static void 655aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl) 665aefb655Srie { 675aefb655Srie ofl->ofl_dehdr->e_flags |= ehdr->e_flags; 685aefb655Srie } 695aefb655Srie 70ba2be530Sab196087 static void 715aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt) 725aefb655Srie { 735aefb655Srie if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) { 745aefb655Srie /* 755aefb655Srie * Create this entry if we are going to create a PLT table. 765aefb655Srie */ 775aefb655Srie if (ofl->ofl_pltcnt) 785aefb655Srie (*cnt)++; /* DT_PLTGOT */ 795aefb655Srie } 805aefb655Srie } 815aefb655Srie 82ba2be530Sab196087 static void 835aefb655Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn) 845aefb655Srie { 855aefb655Srie if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) { 865aefb655Srie (*dyn)->d_tag = DT_PLTGOT; 875aefb655Srie if (ofl->ofl_osgot) 885aefb655Srie (*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr; 895aefb655Srie else 905aefb655Srie (*dyn)->d_un.d_ptr = 0; 915aefb655Srie (*dyn)++; 925aefb655Srie } 935aefb655Srie } 945aefb655Srie 95ba2be530Sab196087 static Xword 965aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl) 975aefb655Srie { 985aefb655Srie Xword value; 995aefb655Srie 1005aefb655Srie value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) + 1015aefb655Srie M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE); 1025aefb655Srie return (value); 1035aefb655Srie } 1045aefb655Srie 1055aefb655Srie /* 1065aefb655Srie * Build a single plt entry - code is: 1075aefb655Srie * JMP *name1@GOTPCREL(%rip) 1085aefb655Srie * PUSHL $index 1095aefb655Srie * JMP .PLT0 1105aefb655Srie */ 1115aefb655Srie static uchar_t pltn_entry[M_PLT_ENTSIZE] = { 1125aefb655Srie /* 0x00 jmpq *name1@GOTPCREL(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 1135aefb655Srie /* 0x06 pushq $index */ 0x68, 0x00, 0x00, 0x00, 0x00, 1145aefb655Srie /* 0x0b jmpq .plt0(%rip) */ 0xe9, 0x00, 0x00, 0x00, 0x00 1155aefb655Srie /* 0x10 */ 1165aefb655Srie }; 1175aefb655Srie 1185aefb655Srie static uintptr_t 1195aefb655Srie plt_entry(Ofl_desc * ofl, Sym_desc * sdp) 1205aefb655Srie { 1215aefb655Srie uchar_t *plt0, *pltent, *gotent; 1225aefb655Srie Sword plt_off; 1235aefb655Srie Word got_off; 1245aefb655Srie Xword val1; 125ba2be530Sab196087 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 1265aefb655Srie 1275aefb655Srie got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE; 1285aefb655Srie plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * 1295aefb655Srie M_PLT_ENTSIZE); 1305aefb655Srie plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf); 1315aefb655Srie pltent = plt0 + plt_off; 1325aefb655Srie gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off; 1335aefb655Srie 1345aefb655Srie bcopy(pltn_entry, pltent, sizeof (pltn_entry)); 1355aefb655Srie /* 1365aefb655Srie * Fill in the got entry with the address of the next instruction. 1375aefb655Srie */ 1385aefb655Srie /* LINTED */ 1395aefb655Srie *(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off + 1405aefb655Srie M_PLT_INSSIZE; 141ba2be530Sab196087 if (bswap) 142ba2be530Sab196087 /* LINTED */ 143ba2be530Sab196087 *(Word *)gotent = ld_bswap_Word(*(Word *)gotent); 1445aefb655Srie 1455aefb655Srie /* 146f3324781Sab196087 * If '-z noreloc' is specified - skip the do_reloc_ld 147f3324781Sab196087 * stage. 148f3324781Sab196087 */ 149f3324781Sab196087 if (!OFL_DO_RELOC(ofl)) 150f3324781Sab196087 return (1); 151f3324781Sab196087 152f3324781Sab196087 /* 1535aefb655Srie * patchup: 1545aefb655Srie * jmpq *name1@gotpcrel(%rip) 1555aefb655Srie * 1565aefb655Srie * NOTE: 0x06 represents next instruction. 1575aefb655Srie */ 1585aefb655Srie val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) - 1595aefb655Srie (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06; 1605aefb655Srie 161f3324781Sab196087 if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02], 1625aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 163f3324781Sab196087 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 164f3324781Sab196087 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL), 165f3324781Sab196087 sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name)); 1665aefb655Srie return (S_ERROR); 1675aefb655Srie } 1685aefb655Srie 1695aefb655Srie /* 1705aefb655Srie * patchup: 1715aefb655Srie * pushq $pltndx 1725aefb655Srie */ 1735aefb655Srie val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1); 174f3324781Sab196087 175f3324781Sab196087 if (do_reloc_ld(R_AMD64_32, &pltent[0x07], 1765aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 177f3324781Sab196087 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 178f3324781Sab196087 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL), 179f3324781Sab196087 sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name)); 1805aefb655Srie return (S_ERROR); 1815aefb655Srie } 1825aefb655Srie 1835aefb655Srie /* 1845aefb655Srie * patchup: 1855aefb655Srie * jmpq .plt0(%rip) 186f3324781Sab196087 * NOTE: 0x10 represents next instruction. The rather complex 187f3324781Sab196087 * series of casts is necessary to sign extend an offset into 188f3324781Sab196087 * a 64-bit value while satisfying various compiler error 189f3324781Sab196087 * checks. Handle with care. 1905aefb655Srie */ 1915aefb655Srie val1 = (Xword)((intptr_t)((uintptr_t)plt0 - 1925aefb655Srie (uintptr_t)(&pltent[0x10]))); 1935aefb655Srie 194f3324781Sab196087 if (do_reloc_ld(R_AMD64_PC32, &pltent[0x0c], 1955aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 196f3324781Sab196087 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 197f3324781Sab196087 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL), 198f3324781Sab196087 sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name)); 1995aefb655Srie return (S_ERROR); 2005aefb655Srie } 201f3324781Sab196087 2025aefb655Srie return (1); 2035aefb655Srie } 2045aefb655Srie 205ba2be530Sab196087 static uintptr_t 2065aefb655Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl) 2075aefb655Srie { 2085aefb655Srie Os_desc * relosp, * osp = 0; 2095aefb655Srie Word ndx; 2105aefb655Srie Xword roffset, value; 2115aefb655Srie Sxword raddend; 2125aefb655Srie Rela rea; 2135aefb655Srie char *relbits; 2145aefb655Srie Sym_desc * sdp, * psym = (Sym_desc *)0; 2155aefb655Srie int sectmoved = 0; 2165aefb655Srie 2175aefb655Srie raddend = orsp->rel_raddend; 2185aefb655Srie sdp = orsp->rel_sym; 2195aefb655Srie 2205aefb655Srie /* 2215aefb655Srie * If the section this relocation is against has been discarded 2225aefb655Srie * (-zignore), then also discard (skip) the relocation itself. 2235aefb655Srie */ 2245aefb655Srie if (orsp->rel_isdesc && ((orsp->rel_flags & 2255aefb655Srie (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) && 2265aefb655Srie (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) { 2275aefb655Srie DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp)); 2285aefb655Srie return (1); 2295aefb655Srie } 2305aefb655Srie 2315aefb655Srie /* 2325aefb655Srie * If this is a relocation against a move table, or expanded move 2335aefb655Srie * table, adjust the relocation entries. 2345aefb655Srie */ 2355aefb655Srie if (orsp->rel_move) 2365aefb655Srie ld_adj_movereloc(ofl, orsp); 2375aefb655Srie 2385aefb655Srie /* 2395aefb655Srie * If this is a relocation against a section then we need to adjust the 2405aefb655Srie * raddend field to compensate for the new position of the input section 2415aefb655Srie * within the new output section. 2425aefb655Srie */ 2435aefb655Srie if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) { 2445aefb655Srie if (ofl->ofl_parsym.head && 2455aefb655Srie (sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 2465aefb655Srie /* LINTED */ 2475aefb655Srie (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) { 2485aefb655Srie DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym)); 2495aefb655Srie sectmoved = 1; 2505aefb655Srie if (ofl->ofl_flags & FLG_OF_RELOBJ) 2515aefb655Srie raddend = psym->sd_sym->st_value; 2525aefb655Srie else 2535aefb655Srie raddend = psym->sd_sym->st_value - 2545aefb655Srie psym->sd_isc->is_osdesc->os_shdr->sh_addr; 2555aefb655Srie /* LINTED */ 2565aefb655Srie raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata); 2575aefb655Srie if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 2585aefb655Srie raddend += 2595aefb655Srie psym->sd_isc->is_osdesc->os_shdr->sh_addr; 2605aefb655Srie } else { 2615aefb655Srie /* LINTED */ 2625aefb655Srie raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata); 2635aefb655Srie if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 2645aefb655Srie raddend += 2655aefb655Srie sdp->sd_isc->is_osdesc->os_shdr->sh_addr; 2665aefb655Srie } 2675aefb655Srie } 2685aefb655Srie 2695aefb655Srie value = sdp->sd_sym->st_value; 2705aefb655Srie 2715aefb655Srie if (orsp->rel_flags & FLG_REL_GOT) { 2725aefb655Srie /* 2735aefb655Srie * Note: for GOT relative relocations on amd64 2745aefb655Srie * we discard the addend. It was relevant 2755aefb655Srie * to the reference - not to the data item 2765aefb655Srie * being referenced (ie: that -4 thing). 2775aefb655Srie */ 2785aefb655Srie raddend = 0; 2795aefb655Srie osp = ofl->ofl_osgot; 2805aefb655Srie roffset = ld_calc_got_offset(orsp, ofl); 2815aefb655Srie 2825aefb655Srie } else if (orsp->rel_flags & FLG_REL_PLT) { 2835aefb655Srie /* 2845aefb655Srie * Note that relocations for PLT's actually 2855aefb655Srie * cause a relocation againt the GOT. 2865aefb655Srie */ 2875aefb655Srie osp = ofl->ofl_osplt; 2885aefb655Srie roffset = (ofl->ofl_osgot->os_shdr->sh_addr) + 2895aefb655Srie sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE; 2905aefb655Srie raddend = 0; 2915aefb655Srie if (plt_entry(ofl, sdp) == S_ERROR) 2925aefb655Srie return (S_ERROR); 2935aefb655Srie 2945aefb655Srie } else if (orsp->rel_flags & FLG_REL_BSS) { 2955aefb655Srie /* 2965aefb655Srie * This must be a R_AMD64_COPY. For these set the roffset to 2975aefb655Srie * point to the new symbols location. 2985aefb655Srie */ 2995aefb655Srie osp = ofl->ofl_isbss->is_osdesc; 3005aefb655Srie roffset = value; 3015aefb655Srie 3025aefb655Srie /* 3035aefb655Srie * The raddend doesn't mean anything in a R_SPARC_COPY 3045aefb655Srie * relocation. Null it out because it can confuse people. 3055aefb655Srie */ 3065aefb655Srie raddend = 0; 3075aefb655Srie } else { 3085aefb655Srie osp = orsp->rel_osdesc; 3095aefb655Srie 3105aefb655Srie /* 3115aefb655Srie * Calculate virtual offset of reference point; equals offset 3125aefb655Srie * into section + vaddr of section for loadable sections, or 3135aefb655Srie * offset plus section displacement for nonloadable sections. 3145aefb655Srie */ 3155aefb655Srie roffset = orsp->rel_roffset + 3165aefb655Srie (Off)_elf_getxoff(orsp->rel_isdesc->is_indata); 3175aefb655Srie if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) 3185aefb655Srie roffset += orsp->rel_isdesc->is_osdesc-> 3195aefb655Srie os_shdr->sh_addr; 3205aefb655Srie } 3215aefb655Srie 3225aefb655Srie if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0)) 3235aefb655Srie relosp = ofl->ofl_osrel; 3245aefb655Srie 3255aefb655Srie /* 3265aefb655Srie * Assign the symbols index for the output relocation. If the 3275aefb655Srie * relocation refers to a SECTION symbol then it's index is based upon 3285aefb655Srie * the output sections symbols index. Otherwise the index can be 3295aefb655Srie * derived from the symbols index itself. 3305aefb655Srie */ 3315aefb655Srie if (orsp->rel_rtype == R_AMD64_RELATIVE) 3325aefb655Srie ndx = STN_UNDEF; 3335aefb655Srie else if ((orsp->rel_flags & FLG_REL_SCNNDX) || 3345aefb655Srie (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) { 3355aefb655Srie if (sectmoved == 0) { 3365aefb655Srie /* 3375aefb655Srie * Check for a null input section. This can 3385aefb655Srie * occur if this relocation references a symbol 3395aefb655Srie * generated by sym_add_sym(). 3405aefb655Srie */ 3415aefb655Srie if ((sdp->sd_isc != 0) && 3425aefb655Srie (sdp->sd_isc->is_osdesc != 0)) 3435aefb655Srie ndx = sdp->sd_isc->is_osdesc->os_scnsymndx; 3445aefb655Srie else 3455aefb655Srie ndx = sdp->sd_shndx; 3465aefb655Srie } else 3475aefb655Srie ndx = ofl->ofl_sunwdata1ndx; 3485aefb655Srie } else 3495aefb655Srie ndx = sdp->sd_symndx; 3505aefb655Srie 3515aefb655Srie /* 3525aefb655Srie * Add the symbols 'value' to the addend field. 3535aefb655Srie */ 3545aefb655Srie if (orsp->rel_flags & FLG_REL_ADVAL) 3555aefb655Srie raddend += value; 3565aefb655Srie 3575aefb655Srie /* 3587010c12aSrie * The addend field for R_AMD64_DTPMOD64 means nothing. The addend 3597010c12aSrie * is propagated in the corresponding R_AMD64_DTPOFF64 relocation. 3605aefb655Srie */ 3615aefb655Srie if (orsp->rel_rtype == R_AMD64_DTPMOD64) 3625aefb655Srie raddend = 0; 3635aefb655Srie 3645aefb655Srie relbits = (char *)relosp->os_outdata->d_buf; 3655aefb655Srie 3665aefb655Srie rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype); 3675aefb655Srie rea.r_offset = roffset; 3685aefb655Srie rea.r_addend = raddend; 3695aefb655Srie DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name, 3705aefb655Srie orsp->rel_sname)); 3715aefb655Srie 3725aefb655Srie /* 3735aefb655Srie * Assert we haven't walked off the end of our relocation table. 3745aefb655Srie */ 3755aefb655Srie assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size); 3765aefb655Srie 3775aefb655Srie (void) memcpy((relbits + relosp->os_szoutrels), 3785aefb655Srie (char *)&rea, sizeof (Rela)); 3795aefb655Srie relosp->os_szoutrels += (Xword)sizeof (Rela); 3805aefb655Srie 3815aefb655Srie /* 3825aefb655Srie * Determine if this relocation is against a non-writable, allocatable 3835aefb655Srie * section. If so we may need to provide a text relocation diagnostic. 3845aefb655Srie * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually 3855aefb655Srie * result in modifications to the .got. 3865aefb655Srie */ 3875aefb655Srie if (orsp->rel_rtype == R_AMD64_JUMP_SLOT) 3885aefb655Srie osp = ofl->ofl_osgot; 3895aefb655Srie 3905aefb655Srie ld_reloc_remain_entry(orsp, osp, ofl); 3915aefb655Srie return (1); 3925aefb655Srie } 3935aefb655Srie 3945aefb655Srie /* 3955aefb655Srie * amd64 Instructions for TLS processing 3965aefb655Srie */ 3975aefb655Srie static uchar_t tlsinstr_gd_ie[] = { 3985aefb655Srie /* 3995aefb655Srie * 0x00 movq %fs:0, %rax 4005aefb655Srie */ 4015aefb655Srie 0x64, 0x48, 0x8b, 0x04, 0x25, 4025aefb655Srie 0x00, 0x00, 0x00, 0x00, 4035aefb655Srie /* 4045aefb655Srie * 0x09 addq x@gottpoff(%rip), %rax 4055aefb655Srie */ 4065aefb655Srie 0x48, 0x03, 0x05, 0x00, 0x00, 4075aefb655Srie 0x00, 0x00 4085aefb655Srie }; 4095aefb655Srie 4105aefb655Srie static uchar_t tlsinstr_gd_le[] = { 4115aefb655Srie /* 4125aefb655Srie * 0x00 movq %fs:0, %rax 4135aefb655Srie */ 4145aefb655Srie 0x64, 0x48, 0x8b, 0x04, 0x25, 4155aefb655Srie 0x00, 0x00, 0x00, 0x00, 4165aefb655Srie /* 4175aefb655Srie * 0x09 leaq x@gottpoff(%rip), %rax 4185aefb655Srie */ 4195aefb655Srie 0x48, 0x8d, 0x80, 0x00, 0x00, 4205aefb655Srie 0x00, 0x00 4215aefb655Srie }; 4225aefb655Srie 4235aefb655Srie static uchar_t tlsinstr_ld_le[] = { 4245aefb655Srie /* 4255aefb655Srie * .byte 0x66 4265aefb655Srie */ 4275aefb655Srie 0x66, 4285aefb655Srie /* 4295aefb655Srie * .byte 0x66 4305aefb655Srie */ 4315aefb655Srie 0x66, 4325aefb655Srie /* 4335aefb655Srie * .byte 0x66 4345aefb655Srie */ 4355aefb655Srie 0x66, 4365aefb655Srie /* 4375aefb655Srie * movq %fs:0, %rax 4385aefb655Srie */ 4395aefb655Srie 0x64, 0x48, 0x8b, 0x04, 0x25, 4405aefb655Srie 0x00, 0x00, 0x00, 0x00 4415aefb655Srie }; 4425aefb655Srie 4435aefb655Srie 4445aefb655Srie static Fixupret 4455aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp) 4465aefb655Srie { 4475aefb655Srie Sym_desc *sdp = arsp->rel_sym; 4485aefb655Srie Word rtype = arsp->rel_rtype; 4495aefb655Srie uchar_t *offset; 4505aefb655Srie 4515aefb655Srie offset = (uchar_t *)((uintptr_t)arsp->rel_roffset + 4525aefb655Srie (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) + 4535aefb655Srie (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf); 4545aefb655Srie 4555aefb655Srie if (sdp->sd_ref == REF_DYN_NEED) { 4565aefb655Srie /* 4575aefb655Srie * IE reference model 4585aefb655Srie */ 4595aefb655Srie switch (rtype) { 4605aefb655Srie case R_AMD64_TLSGD: 4615aefb655Srie /* 4625aefb655Srie * GD -> IE 4635aefb655Srie * 4645aefb655Srie * Transition: 4655aefb655Srie * 0x00 .byte 0x66 4665aefb655Srie * 0x01 leaq x@tlsgd(%rip), %rdi 4675aefb655Srie * 0x08 .word 0x6666 4685aefb655Srie * 0x0a rex64 4695aefb655Srie * 0x0b call __tls_get_addr@plt 4705aefb655Srie * 0x10 4715aefb655Srie * To: 4725aefb655Srie * 0x00 movq %fs:0, %rax 4735aefb655Srie * 0x09 addq x@gottpoff(%rip), %rax 4745aefb655Srie * 0x10 4755aefb655Srie */ 4765aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 477051d39bbSrie R_AMD64_GOTTPOFF, arsp)); 4785aefb655Srie arsp->rel_rtype = R_AMD64_GOTTPOFF; 4795aefb655Srie arsp->rel_roffset += 8; 4805aefb655Srie arsp->rel_raddend = (Sxword)-4; 4815aefb655Srie 4825aefb655Srie /* 483051d39bbSrie * Adjust 'offset' to beginning of instruction 4845aefb655Srie * sequence. 4855aefb655Srie */ 4865aefb655Srie offset -= 4; 4875aefb655Srie (void) memcpy(offset, tlsinstr_gd_ie, 4885aefb655Srie sizeof (tlsinstr_gd_ie)); 4895aefb655Srie return (FIX_RELOC); 4905aefb655Srie 4915aefb655Srie case R_AMD64_PLT32: 4925aefb655Srie /* 493051d39bbSrie * Fixup done via the TLS_GD relocation. 4945aefb655Srie */ 4955aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 496051d39bbSrie R_AMD64_NONE, arsp)); 4975aefb655Srie return (FIX_DONE); 4985aefb655Srie } 4995aefb655Srie } 5005aefb655Srie 5015aefb655Srie /* 5025aefb655Srie * LE reference model 5035aefb655Srie */ 5045aefb655Srie switch (rtype) { 5055aefb655Srie case R_AMD64_TLSGD: 5065aefb655Srie /* 5075aefb655Srie * GD -> LE 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 leaq x@tpoff(%rax), %rax 5195aefb655Srie * 0x10 5205aefb655Srie */ 5215aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 522051d39bbSrie R_AMD64_TPOFF32, arsp)); 5235aefb655Srie arsp->rel_rtype = R_AMD64_TPOFF32; 5245aefb655Srie arsp->rel_roffset += 8; 5255aefb655Srie arsp->rel_raddend = 0; 5265aefb655Srie 5275aefb655Srie /* 528051d39bbSrie * Adjust 'offset' to beginning of instruction sequence. 5295aefb655Srie */ 5305aefb655Srie offset -= 4; 5315aefb655Srie (void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le)); 5325aefb655Srie return (FIX_RELOC); 5335aefb655Srie 5345aefb655Srie case R_AMD64_GOTTPOFF: 5355aefb655Srie /* 5365aefb655Srie * IE -> LE 5375aefb655Srie * 5385aefb655Srie * Transition: 5395aefb655Srie * 0x00 movq %fs:0, %rax 5405aefb655Srie * 0x09 addq x@gottopoff(%rip), %rax 5415aefb655Srie * 0x10 5425aefb655Srie * To: 5435aefb655Srie * 0x00 movq %fs:0, %rax 5445aefb655Srie * 0x09 leaq x@tpoff(%rax), %rax 5455aefb655Srie * 0x10 5465aefb655Srie */ 547051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 548051d39bbSrie R_AMD64_TPOFF32, arsp)); 5495aefb655Srie arsp->rel_rtype = R_AMD64_TPOFF32; 5505aefb655Srie arsp->rel_raddend = 0; 5515aefb655Srie 5525aefb655Srie /* 553051d39bbSrie * Adjust 'offset' to beginning of instruction sequence. 5545aefb655Srie */ 5555aefb655Srie offset -= 12; 5565aefb655Srie 5575aefb655Srie /* 558051d39bbSrie * Same code sequence used in the GD -> LE transition. 5595aefb655Srie */ 5605aefb655Srie (void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le)); 5615aefb655Srie return (FIX_RELOC); 5625aefb655Srie 5635aefb655Srie case R_AMD64_TLSLD: 5645aefb655Srie /* 5655aefb655Srie * LD -> LE 5665aefb655Srie * 5675aefb655Srie * Transition 5685aefb655Srie * 0x00 leaq x1@tlsgd(%rip), %rdi 5695aefb655Srie * 0x07 call __tls_get_addr@plt 5705aefb655Srie * 0x0c 5715aefb655Srie * To: 5725aefb655Srie * 0x00 .byte 0x66 5735aefb655Srie * 0x01 .byte 0x66 5745aefb655Srie * 0x02 .byte 0x66 5755aefb655Srie * 0x03 movq %fs:0, %rax 5765aefb655Srie */ 577051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 578051d39bbSrie R_AMD64_NONE, arsp)); 5795aefb655Srie offset -= 3; 5805aefb655Srie (void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le)); 5815aefb655Srie return (FIX_DONE); 5825aefb655Srie 5835aefb655Srie case R_AMD64_DTPOFF32: 5845aefb655Srie /* 5855aefb655Srie * LD->LE 5865aefb655Srie * 5875aefb655Srie * Transition: 5885aefb655Srie * 0x00 leaq x1@dtpoff(%rax), %rcx 5895aefb655Srie * To: 5905aefb655Srie * 0x00 leaq x1@tpoff(%rax), %rcx 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 return (FIX_RELOC); 5975aefb655Srie } 5985aefb655Srie 5995aefb655Srie return (FIX_RELOC); 6005aefb655Srie } 6015aefb655Srie 602ba2be530Sab196087 static uintptr_t 6035aefb655Srie ld_do_activerelocs(Ofl_desc *ofl) 6045aefb655Srie { 6055aefb655Srie Rel_desc *arsp; 6065aefb655Srie Rel_cache *rcp; 6075aefb655Srie Listnode *lnp; 6085aefb655Srie uintptr_t return_code = 1; 609*1d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags; 6105aefb655Srie 6117010c12aSrie if (ofl->ofl_actrels.head) 6125aefb655Srie DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml)); 6137010c12aSrie 6145aefb655Srie /* 6155aefb655Srie * Process active relocations. 6165aefb655Srie */ 6175aefb655Srie for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) { 6185aefb655Srie /* LINTED */ 6195aefb655Srie for (arsp = (Rel_desc *)(rcp + 1); 6205aefb655Srie arsp < rcp->rc_free; arsp++) { 6215aefb655Srie uchar_t *addr; 6225aefb655Srie Xword value; 6235aefb655Srie Sym_desc *sdp; 6245aefb655Srie const char *ifl_name; 6255aefb655Srie Xword refaddr; 6265aefb655Srie int moved = 0; 6275aefb655Srie Gotref gref; 6285aefb655Srie 6295aefb655Srie /* 6305aefb655Srie * If the section this relocation is against has been 6315aefb655Srie * discarded (-zignore), then discard (skip) the 6325aefb655Srie * relocation itself. 6335aefb655Srie */ 6345aefb655Srie if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) && 6355aefb655Srie ((arsp->rel_flags & 6365aefb655Srie (FLG_REL_GOT | FLG_REL_BSS | 6375aefb655Srie FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) { 6385aefb655Srie DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, 6395aefb655Srie M_MACH, arsp)); 6405aefb655Srie continue; 6415aefb655Srie } 6425aefb655Srie 6435aefb655Srie /* 6445aefb655Srie * We deteremine what the 'got reference' 6455aefb655Srie * model (if required) is at this point. This 6465aefb655Srie * needs to be done before tls_fixup() since 6475aefb655Srie * it may 'transition' our instructions. 6485aefb655Srie * 6495aefb655Srie * The got table entries have already been assigned, 6505aefb655Srie * and we bind to those initial entries. 6515aefb655Srie */ 6525aefb655Srie if (arsp->rel_flags & FLG_REL_DTLS) 6535aefb655Srie gref = GOT_REF_TLSGD; 6545aefb655Srie else if (arsp->rel_flags & FLG_REL_MTLS) 6555aefb655Srie gref = GOT_REF_TLSLD; 6565aefb655Srie else if (arsp->rel_flags & FLG_REL_STLS) 6575aefb655Srie gref = GOT_REF_TLSIE; 6585aefb655Srie else 6595aefb655Srie gref = GOT_REF_GENERIC; 6605aefb655Srie 6615aefb655Srie /* 6625aefb655Srie * Perform any required TLS fixups. 6635aefb655Srie */ 6645aefb655Srie if (arsp->rel_flags & FLG_REL_TLSFIX) { 6655aefb655Srie Fixupret ret; 6665aefb655Srie 6675aefb655Srie if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR) 6685aefb655Srie return (S_ERROR); 6695aefb655Srie if (ret == FIX_DONE) 6705aefb655Srie continue; 6715aefb655Srie } 6725aefb655Srie 6735aefb655Srie /* 6745aefb655Srie * If this is a relocation against a move table, or 6755aefb655Srie * expanded move table, adjust the relocation entries. 6765aefb655Srie */ 6775aefb655Srie if (arsp->rel_move) 6785aefb655Srie ld_adj_movereloc(ofl, arsp); 6795aefb655Srie 6805aefb655Srie sdp = arsp->rel_sym; 6815aefb655Srie refaddr = arsp->rel_roffset + 6825aefb655Srie (Off)_elf_getxoff(arsp->rel_isdesc->is_indata); 6835aefb655Srie 6845aefb655Srie if ((arsp->rel_flags & FLG_REL_CLVAL) || 6855aefb655Srie (arsp->rel_flags & FLG_REL_GOTCL)) 6865aefb655Srie value = 0; 6875aefb655Srie else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == 6885aefb655Srie STT_SECTION) { 6895aefb655Srie Sym_desc *sym; 6905aefb655Srie 6915aefb655Srie /* 6925aefb655Srie * The value for a symbol pointing to a SECTION 6935aefb655Srie * is based off of that sections position. 6945aefb655Srie * 6955aefb655Srie * The second argument of the ld_am_I_partial() 6965aefb655Srie * is the value stored at the target address 6975aefb655Srie * relocation is going to be applied. 6985aefb655Srie */ 6995aefb655Srie if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 7005aefb655Srie /* LINTED */ 7015aefb655Srie (sym = ld_am_I_partial(arsp, *(Xword *) 7025aefb655Srie ((uchar_t *) 7035aefb655Srie arsp->rel_isdesc->is_indata->d_buf + 7045aefb655Srie arsp->rel_roffset)))) { 7055aefb655Srie /* 7065aefb655Srie * If the symbol is moved, 7075aefb655Srie * adjust the value 7085aefb655Srie */ 7095aefb655Srie value = sym->sd_sym->st_value; 7105aefb655Srie moved = 1; 7115aefb655Srie } else { 7125aefb655Srie value = _elf_getxoff( 7135aefb655Srie sdp->sd_isc->is_indata); 7145aefb655Srie if (sdp->sd_isc->is_shdr->sh_flags & 7155aefb655Srie SHF_ALLOC) 716de777a60Sab196087 value += 717de777a60Sab196087 sdp->sd_isc->is_osdesc-> 7185aefb655Srie os_shdr->sh_addr; 7195aefb655Srie } 7205aefb655Srie if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS) 7215aefb655Srie value -= ofl->ofl_tlsphdr->p_vaddr; 7222926dd2eSrie 7232926dd2eSrie } else if (IS_SIZE(arsp->rel_rtype)) { 7242926dd2eSrie /* 7252926dd2eSrie * Size relocations require the symbols size. 7262926dd2eSrie */ 7272926dd2eSrie value = sdp->sd_sym->st_size; 7285aefb655Srie } else { 7295aefb655Srie /* 7307010c12aSrie * Else the value is the symbols value. 7315aefb655Srie */ 7325aefb655Srie value = sdp->sd_sym->st_value; 7335aefb655Srie } 7345aefb655Srie 7355aefb655Srie /* 7365aefb655Srie * Relocation against the GLOBAL_OFFSET_TABLE. 7375aefb655Srie */ 7385aefb655Srie if (arsp->rel_flags & FLG_REL_GOT) 7395aefb655Srie arsp->rel_osdesc = ofl->ofl_osgot; 7405aefb655Srie 7415aefb655Srie /* 7425aefb655Srie * If loadable and not producing a relocatable object 7435aefb655Srie * add the sections virtual address to the reference 7445aefb655Srie * address. 7455aefb655Srie */ 7465aefb655Srie if ((arsp->rel_flags & FLG_REL_LOAD) && 7475aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) 7485aefb655Srie refaddr += arsp->rel_isdesc->is_osdesc-> 7495aefb655Srie os_shdr->sh_addr; 7505aefb655Srie 7515aefb655Srie /* 7525aefb655Srie * If this entry has a PLT assigned to it, it's 7535aefb655Srie * value is actually the address of the PLT (and 7545aefb655Srie * not the address of the function). 7555aefb655Srie */ 7565aefb655Srie if (IS_PLT(arsp->rel_rtype)) { 7575aefb655Srie if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx) 7585aefb655Srie value = ld_calc_plt_addr(sdp, ofl); 7595aefb655Srie } 7605aefb655Srie 7615aefb655Srie /* 7625aefb655Srie * Add relocations addend to value. Add extra 7635aefb655Srie * relocation addend if needed. 7645aefb655Srie * 7655aefb655Srie * Note: for GOT relative relocations on amd64 7665aefb655Srie * we discard the addend. It was relevant 7675aefb655Srie * to the reference - not to the data item 7685aefb655Srie * being referenced (ie: that -4 thing). 7695aefb655Srie */ 7705aefb655Srie if ((arsp->rel_flags & FLG_REL_GOT) == 0) 7715aefb655Srie value += arsp->rel_raddend; 7725aefb655Srie 7735aefb655Srie /* 7745aefb655Srie * Determine whether the value needs further adjustment. 7755aefb655Srie * Filter through the attributes of the relocation to 7765aefb655Srie * determine what adjustment is required. Note, many 7775aefb655Srie * of the following cases are only applicable when a 7785aefb655Srie * .got is present. As a .got is not generated when a 7795aefb655Srie * relocatable object is being built, any adjustments 7805aefb655Srie * that require a .got need to be skipped. 7815aefb655Srie */ 7825aefb655Srie if ((arsp->rel_flags & FLG_REL_GOT) && 7835aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 7845aefb655Srie Xword R1addr; 7855aefb655Srie uintptr_t R2addr; 7865aefb655Srie Word gotndx; 7875aefb655Srie Gotndx *gnp; 7885aefb655Srie 7895aefb655Srie /* 7905aefb655Srie * Perform relocation against GOT table. Since 7915aefb655Srie * this doesn't fit exactly into a relocation 7925aefb655Srie * we place the appropriate byte in the GOT 7935aefb655Srie * directly 7945aefb655Srie * 7955aefb655Srie * Calculate offset into GOT at which to apply 7965aefb655Srie * the relocation. 7975aefb655Srie */ 7985aefb655Srie gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, 7995aefb655Srie ofl, arsp); 8005aefb655Srie assert(gnp); 8015aefb655Srie 8025aefb655Srie if (arsp->rel_rtype == R_AMD64_DTPOFF64) 8035aefb655Srie gotndx = gnp->gn_gotndx + 1; 8045aefb655Srie else 8055aefb655Srie gotndx = gnp->gn_gotndx; 8065aefb655Srie 8075aefb655Srie R1addr = (Xword)(gotndx * M_GOT_ENTSIZE); 8085aefb655Srie 8095aefb655Srie /* 8105aefb655Srie * Add the GOTs data's offset. 8115aefb655Srie */ 8125aefb655Srie R2addr = R1addr + (uintptr_t) 8135aefb655Srie arsp->rel_osdesc->os_outdata->d_buf; 8145aefb655Srie 8155aefb655Srie DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, 8165aefb655Srie ELF_DBG_LD, M_MACH, SHT_RELA, 8175aefb655Srie arsp->rel_rtype, R1addr, value, 8185aefb655Srie arsp->rel_sname, arsp->rel_osdesc)); 8195aefb655Srie 8205aefb655Srie /* 8215aefb655Srie * And do it. 8225aefb655Srie */ 823f3324781Sab196087 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) 824f3324781Sab196087 *(Xword *)R2addr = 825ba2be530Sab196087 ld_bswap_Xword(value); 826f3324781Sab196087 else 8275aefb655Srie *(Xword *)R2addr = value; 8285aefb655Srie continue; 8295aefb655Srie 8305aefb655Srie } else if (IS_GOT_BASED(arsp->rel_rtype) && 8315aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8325aefb655Srie value -= ofl->ofl_osgot->os_shdr->sh_addr; 8335aefb655Srie 8345aefb655Srie } else if (IS_GOTPCREL(arsp->rel_rtype) && 8355aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8365aefb655Srie Gotndx *gnp; 8375aefb655Srie 8385aefb655Srie /* 8395aefb655Srie * Calculation: 8405aefb655Srie * G + GOT + A - P 8415aefb655Srie */ 8425aefb655Srie gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 8435aefb655Srie gref, ofl, arsp); 8445aefb655Srie assert(gnp); 8455aefb655Srie value = (Xword)(ofl->ofl_osgot->os_shdr-> 8465aefb655Srie sh_addr) + ((Xword)gnp->gn_gotndx * 8475aefb655Srie M_GOT_ENTSIZE) + arsp->rel_raddend - 8485aefb655Srie refaddr; 8495aefb655Srie 8505aefb655Srie } else if (IS_GOT_PC(arsp->rel_rtype) && 8515aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8525aefb655Srie value = (Xword)(ofl->ofl_osgot->os_shdr-> 8535aefb655Srie sh_addr) - refaddr + arsp->rel_raddend; 8545aefb655Srie 8555aefb655Srie } else if ((IS_PC_RELATIVE(arsp->rel_rtype)) && 8565aefb655Srie (((flags & FLG_OF_RELOBJ) == 0) || 8575aefb655Srie (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) { 8585aefb655Srie value -= refaddr; 8595aefb655Srie 8605aefb655Srie } else if (IS_TLS_INS(arsp->rel_rtype) && 8615aefb655Srie IS_GOT_RELATIVE(arsp->rel_rtype) && 8625aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8635aefb655Srie Gotndx *gnp; 8645aefb655Srie 8655aefb655Srie gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, 8665aefb655Srie ofl, arsp); 8675aefb655Srie assert(gnp); 8685aefb655Srie value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 8695aefb655Srie 8705aefb655Srie } else if (IS_GOT_RELATIVE(arsp->rel_rtype) && 8715aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8725aefb655Srie Gotndx *gnp; 8735aefb655Srie 8745aefb655Srie gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 8755aefb655Srie gref, ofl, arsp); 8765aefb655Srie assert(gnp); 8775aefb655Srie value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 8785aefb655Srie 8795aefb655Srie } else if ((arsp->rel_flags & FLG_REL_STLS) && 8805aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8815aefb655Srie Xword tlsstatsize; 8825aefb655Srie 8835aefb655Srie /* 8845aefb655Srie * This is the LE TLS reference model. Static 8855aefb655Srie * offset is hard-coded. 8865aefb655Srie */ 8875aefb655Srie tlsstatsize = 8885aefb655Srie S_ROUND(ofl->ofl_tlsphdr->p_memsz, 8895aefb655Srie M_TLSSTATALIGN); 8905aefb655Srie value = tlsstatsize - value; 8915aefb655Srie 8925aefb655Srie /* 8935aefb655Srie * Since this code is fixed up, it assumes a 8945aefb655Srie * negative offset that can be added to the 8955aefb655Srie * thread pointer. 8965aefb655Srie */ 8975aefb655Srie if (arsp->rel_rtype == R_AMD64_TPOFF32) 8985aefb655Srie value = -value; 8995aefb655Srie } 9005aefb655Srie 9015aefb655Srie if (arsp->rel_isdesc->is_file) 9025aefb655Srie ifl_name = arsp->rel_isdesc->is_file->ifl_name; 9035aefb655Srie else 9045aefb655Srie ifl_name = MSG_INTL(MSG_STR_NULL); 9055aefb655Srie 9065aefb655Srie /* 9075aefb655Srie * Make sure we have data to relocate. Compiler and 9085aefb655Srie * assembler developers have been known to generate 9095aefb655Srie * relocations against invalid sections (normally .bss), 9105aefb655Srie * so for their benefit give them sufficient information 9115aefb655Srie * to help analyze the problem. End users should never 9125aefb655Srie * see this. 9135aefb655Srie */ 9145aefb655Srie if (arsp->rel_isdesc->is_indata->d_buf == 0) { 915de777a60Sab196087 Conv_inv_buf_t inv_buf; 916de777a60Sab196087 9175aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 9185aefb655Srie MSG_INTL(MSG_REL_EMPTYSEC), 919de777a60Sab196087 conv_reloc_amd64_type(arsp->rel_rtype, 920de777a60Sab196087 0, &inv_buf), ifl_name, 921de777a60Sab196087 demangle(arsp->rel_sname), 9225aefb655Srie arsp->rel_isdesc->is_name); 9235aefb655Srie return (S_ERROR); 9245aefb655Srie } 9255aefb655Srie 9265aefb655Srie /* 9275aefb655Srie * Get the address of the data item we need to modify. 9285aefb655Srie */ 9295aefb655Srie addr = (uchar_t *)((uintptr_t)arsp->rel_roffset + 9305aefb655Srie (uintptr_t)_elf_getxoff(arsp->rel_isdesc-> 9315aefb655Srie is_indata)); 9325aefb655Srie 9335aefb655Srie DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD, 9345aefb655Srie M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr), 9355aefb655Srie value, arsp->rel_sname, arsp->rel_osdesc)); 9365aefb655Srie addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf; 9375aefb655Srie 9385aefb655Srie if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) > 9395aefb655Srie ofl->ofl_size) || (arsp->rel_roffset > 9405aefb655Srie arsp->rel_osdesc->os_shdr->sh_size)) { 9415aefb655Srie int class; 942de777a60Sab196087 Conv_inv_buf_t inv_buf; 9435aefb655Srie 9445aefb655Srie if (((uintptr_t)addr - 9455aefb655Srie (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size) 9465aefb655Srie class = ERR_FATAL; 9475aefb655Srie else 9485aefb655Srie class = ERR_WARNING; 9495aefb655Srie 9505aefb655Srie eprintf(ofl->ofl_lml, class, 9515aefb655Srie MSG_INTL(MSG_REL_INVALOFFSET), 952de777a60Sab196087 conv_reloc_amd64_type(arsp->rel_rtype, 953de777a60Sab196087 0, &inv_buf), ifl_name, 954de777a60Sab196087 arsp->rel_isdesc->is_name, 9555aefb655Srie demangle(arsp->rel_sname), 9565aefb655Srie EC_ADDR((uintptr_t)addr - 9575aefb655Srie (uintptr_t)ofl->ofl_nehdr)); 9585aefb655Srie 9595aefb655Srie if (class == ERR_FATAL) { 9605aefb655Srie return_code = S_ERROR; 9615aefb655Srie continue; 9625aefb655Srie } 9635aefb655Srie } 9645aefb655Srie 9655aefb655Srie /* 9665aefb655Srie * The relocation is additive. Ignore the previous 9675aefb655Srie * symbol value if this local partial symbol is 9685aefb655Srie * expanded. 9695aefb655Srie */ 9705aefb655Srie if (moved) 9715aefb655Srie value -= *addr; 9725aefb655Srie 9735aefb655Srie /* 974f3324781Sab196087 * If '-z noreloc' is specified - skip the do_reloc_ld 9755aefb655Srie * stage. 9765aefb655Srie */ 977f3324781Sab196087 if (OFL_DO_RELOC(ofl)) { 978f3324781Sab196087 /* 979f3324781Sab196087 * If this is a PROGBITS section and the 980f3324781Sab196087 * running linker has a different byte order 981f3324781Sab196087 * than the target host, tell do_reloc_ld() 982f3324781Sab196087 * to swap bytes. 983f3324781Sab196087 */ 984f3324781Sab196087 if (do_reloc_ld((uchar_t)arsp->rel_rtype, 9855aefb655Srie addr, &value, arsp->rel_sname, ifl_name, 986f3324781Sab196087 OFL_SWAP_RELOC_DATA(ofl, arsp), 9875aefb655Srie ofl->ofl_lml) == 0) 9885aefb655Srie return_code = S_ERROR; 9895aefb655Srie } 9905aefb655Srie } 9915aefb655Srie } 9925aefb655Srie return (return_code); 9935aefb655Srie } 9945aefb655Srie 995ba2be530Sab196087 static uintptr_t 9965aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl) 9975aefb655Srie { 9985aefb655Srie Rel_desc *orsp; 9995aefb655Srie Rel_cache *rcp; 10005aefb655Srie Sym_desc *sdp = rsp->rel_sym; 10015aefb655Srie 10025aefb655Srie /* 10035aefb655Srie * Static executables *do not* want any relocations against them. 10045aefb655Srie * Since our engine still creates relocations against a WEAK UNDEFINED 10055aefb655Srie * symbol in a static executable, it's best to disable them here 10065aefb655Srie * instead of through out the relocation code. 10075aefb655Srie */ 10085aefb655Srie if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) == 10095aefb655Srie (FLG_OF_STATIC | FLG_OF_EXEC)) 10105aefb655Srie return (1); 10115aefb655Srie 10125aefb655Srie /* 10135aefb655Srie * If no relocation cache structures are available allocate 10145aefb655Srie * a new one and link it into the cache list. 10155aefb655Srie */ 10165aefb655Srie if ((ofl->ofl_outrels.tail == 0) || 10175aefb655Srie ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) || 10185aefb655Srie ((orsp = rcp->rc_free) == rcp->rc_end)) { 10195aefb655Srie static size_t nextsize = 0; 10205aefb655Srie size_t size; 10215aefb655Srie 10225aefb655Srie /* 10235aefb655Srie * Output relocation numbers can vary considerably between 10245aefb655Srie * building executables or shared objects (pic vs. non-pic), 10255aefb655Srie * etc. But, they typically aren't very large, so for these 10265aefb655Srie * objects use a standard bucket size. For building relocatable 10275aefb655Srie * objects, typically there will be an output relocation for 10285aefb655Srie * every input relocation. 10295aefb655Srie */ 10305aefb655Srie if (nextsize == 0) { 10315aefb655Srie if (ofl->ofl_flags & FLG_OF_RELOBJ) { 10325aefb655Srie if ((size = ofl->ofl_relocincnt) == 0) 10335aefb655Srie size = REL_LOIDESCNO; 10345aefb655Srie if (size > REL_HOIDESCNO) 10355aefb655Srie nextsize = REL_HOIDESCNO; 10365aefb655Srie else 10375aefb655Srie nextsize = REL_LOIDESCNO; 10385aefb655Srie } else 10395aefb655Srie nextsize = size = REL_HOIDESCNO; 10405aefb655Srie } else 10415aefb655Srie size = nextsize; 10425aefb655Srie 10435aefb655Srie size = size * sizeof (Rel_desc); 10445aefb655Srie 10455aefb655Srie if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) || 10465aefb655Srie (list_appendc(&ofl->ofl_outrels, rcp) == 0)) 10475aefb655Srie return (S_ERROR); 10485aefb655Srie 10495aefb655Srie /* LINTED */ 10505aefb655Srie rcp->rc_free = orsp = (Rel_desc *)(rcp + 1); 10515aefb655Srie /* LINTED */ 10525aefb655Srie rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size); 10535aefb655Srie } 10545aefb655Srie 10555aefb655Srie /* 10565aefb655Srie * If we are adding a output relocation against a section 10575aefb655Srie * symbol (non-RELATIVE) then mark that section. These sections 10585aefb655Srie * will be added to the .dynsym symbol table. 10595aefb655Srie */ 10605aefb655Srie if (sdp && (rsp->rel_rtype != M_R_RELATIVE) && 10615aefb655Srie ((flags & FLG_REL_SCNNDX) || 10625aefb655Srie (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) { 10635aefb655Srie 10645aefb655Srie /* 10655aefb655Srie * If this is a COMMON symbol - no output section 10665aefb655Srie * exists yet - (it's created as part of sym_validate()). 10675aefb655Srie * So - we mark here that when it's created it should 10685aefb655Srie * be tagged with the FLG_OS_OUTREL flag. 10695aefb655Srie */ 10705aefb655Srie if ((sdp->sd_flags & FLG_SY_SPECSEC) && 10710bc07c75Srie (sdp->sd_sym->st_shndx == SHN_COMMON)) { 10725aefb655Srie if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS) 10735aefb655Srie ofl->ofl_flags1 |= FLG_OF1_BSSOREL; 10745aefb655Srie else 10755aefb655Srie ofl->ofl_flags1 |= FLG_OF1_TLSOREL; 10765aefb655Srie } else { 10775aefb655Srie Os_desc *osp = sdp->sd_isc->is_osdesc; 10785aefb655Srie 1079c1c6f601Srie if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) { 10805aefb655Srie ofl->ofl_dynshdrcnt++; 10815aefb655Srie osp->os_flags |= FLG_OS_OUTREL; 10825aefb655Srie } 10835aefb655Srie } 10845aefb655Srie } 10855aefb655Srie 10865aefb655Srie *orsp = *rsp; 10875aefb655Srie orsp->rel_flags |= flags; 10885aefb655Srie 10895aefb655Srie rcp->rc_free++; 10905aefb655Srie ofl->ofl_outrelscnt++; 10915aefb655Srie 10925aefb655Srie if (flags & FLG_REL_GOT) 10935aefb655Srie ofl->ofl_relocgotsz += (Xword)sizeof (Rela); 10945aefb655Srie else if (flags & FLG_REL_PLT) 10955aefb655Srie ofl->ofl_relocpltsz += (Xword)sizeof (Rela); 10965aefb655Srie else if (flags & FLG_REL_BSS) 10975aefb655Srie ofl->ofl_relocbsssz += (Xword)sizeof (Rela); 10985aefb655Srie else if (flags & FLG_REL_NOINFO) 10995aefb655Srie ofl->ofl_relocrelsz += (Xword)sizeof (Rela); 11005aefb655Srie else 11015aefb655Srie orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela); 11025aefb655Srie 11035aefb655Srie if (orsp->rel_rtype == M_R_RELATIVE) 11045aefb655Srie ofl->ofl_relocrelcnt++; 11055aefb655Srie 11065aefb655Srie /* 11075aefb655Srie * We don't perform sorting on PLT relocations because 11085aefb655Srie * they have already been assigned a PLT index and if we 11095aefb655Srie * were to sort them we would have to re-assign the plt indexes. 11105aefb655Srie */ 11115aefb655Srie if (!(flags & FLG_REL_PLT)) 11125aefb655Srie ofl->ofl_reloccnt++; 11135aefb655Srie 11145aefb655Srie /* 11155aefb655Srie * Insure a GLOBAL_OFFSET_TABLE is generated if required. 11165aefb655Srie */ 11175aefb655Srie if (IS_GOT_REQUIRED(orsp->rel_rtype)) 11185aefb655Srie ofl->ofl_flags |= FLG_OF_BLDGOT; 11195aefb655Srie 11205aefb655Srie /* 11215aefb655Srie * Identify and possibly warn of a displacement relocation. 11225aefb655Srie */ 11235aefb655Srie if (orsp->rel_flags & FLG_REL_DISP) { 11245aefb655Srie ofl->ofl_dtflags_1 |= DF_1_DISPRELPND; 11255aefb655Srie 11265aefb655Srie if (ofl->ofl_flags & FLG_OF_VERBOSE) 11275aefb655Srie ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl); 11285aefb655Srie } 11295aefb655Srie DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA, 11305aefb655Srie M_MACH, orsp)); 11315aefb655Srie return (1); 11325aefb655Srie } 11335aefb655Srie 11345aefb655Srie /* 11355aefb655Srie * process relocation for a LOCAL symbol 11365aefb655Srie */ 1137ba2be530Sab196087 static uintptr_t 11385aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl) 11395aefb655Srie { 1140*1d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags; 11415aefb655Srie Sym_desc *sdp = rsp->rel_sym; 11420bc07c75Srie Word shndx = sdp->sd_sym->st_shndx; 11435aefb655Srie Word ortype = rsp->rel_rtype; 11445aefb655Srie 11455aefb655Srie /* 11465aefb655Srie * if ((shared object) and (not pc relative relocation) and 11475aefb655Srie * (not against ABS symbol)) 11485aefb655Srie * then 11495aefb655Srie * build R_AMD64_RELATIVE 11505aefb655Srie * fi 11515aefb655Srie */ 11525aefb655Srie if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) && 11532926dd2eSrie !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) && 11545aefb655Srie !(IS_GOT_BASED(rsp->rel_rtype)) && 11555aefb655Srie !(rsp->rel_isdesc != NULL && 11565aefb655Srie (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) && 11575aefb655Srie (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) || 11585aefb655Srie (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) { 11595aefb655Srie 11605aefb655Srie /* 11615aefb655Srie * R_AMD64_RELATIVE updates a 64bit address, if this 11625aefb655Srie * relocation isn't a 64bit binding then we can not 11635aefb655Srie * simplify it to a RELATIVE relocation. 11645aefb655Srie */ 11655aefb655Srie if (reloc_table[ortype].re_fsize != sizeof (Addr)) { 1166*1d9df23bSab196087 return (ld_add_outrel(0, rsp, ofl)); 11675aefb655Srie } 11685aefb655Srie 11695aefb655Srie rsp->rel_rtype = R_AMD64_RELATIVE; 11705aefb655Srie if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR) 11715aefb655Srie return (S_ERROR); 11725aefb655Srie rsp->rel_rtype = ortype; 11735aefb655Srie return (1); 11745aefb655Srie } 11755aefb655Srie 11765aefb655Srie /* 11775aefb655Srie * If the relocation is against a 'non-allocatable' section 11785aefb655Srie * and we can not resolve it now - then give a warning 11795aefb655Srie * message. 11805aefb655Srie * 11815aefb655Srie * We can not resolve the symbol if either: 11825aefb655Srie * a) it's undefined 11835aefb655Srie * b) it's defined in a shared library and a 11845aefb655Srie * COPY relocation hasn't moved it to the executable 11855aefb655Srie * 11865aefb655Srie * Note: because we process all of the relocations against the 11875aefb655Srie * text segment before any others - we know whether 11885aefb655Srie * or not a copy relocation will be generated before 11895aefb655Srie * we get here (see reloc_init()->reloc_segments()). 11905aefb655Srie */ 11915aefb655Srie if (!(rsp->rel_flags & FLG_REL_LOAD) && 11925aefb655Srie ((shndx == SHN_UNDEF) || 11935aefb655Srie ((sdp->sd_ref == REF_DYN_NEED) && 11945aefb655Srie ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) { 1195de777a60Sab196087 Conv_inv_buf_t inv_buf; 1196de777a60Sab196087 11975aefb655Srie /* 11985aefb655Srie * If the relocation is against a SHT_SUNW_ANNOTATE 11995aefb655Srie * section - then silently ignore that the relocation 12005aefb655Srie * can not be resolved. 12015aefb655Srie */ 12025aefb655Srie if (rsp->rel_osdesc && 12035aefb655Srie (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE)) 12045aefb655Srie return (0); 12055aefb655Srie (void) eprintf(ofl->ofl_lml, ERR_WARNING, 12065aefb655Srie MSG_INTL(MSG_REL_EXTERNSYM), 1207de777a60Sab196087 conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf), 12085aefb655Srie rsp->rel_isdesc->is_file->ifl_name, 12095aefb655Srie demangle(rsp->rel_sname), rsp->rel_osdesc->os_name); 12105aefb655Srie return (1); 12115aefb655Srie } 12125aefb655Srie 12135aefb655Srie /* 12145aefb655Srie * Perform relocation. 12155aefb655Srie */ 12165aefb655Srie return (ld_add_actrel(NULL, rsp, ofl)); 12175aefb655Srie } 12185aefb655Srie 12195aefb655Srie 1220ba2be530Sab196087 static uintptr_t 12215aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl) 12225aefb655Srie { 12235aefb655Srie Word rtype = rsp->rel_rtype; 12245aefb655Srie Sym_desc *sdp = rsp->rel_sym; 1225*1d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags; 12265aefb655Srie Gotndx *gnp; 12275aefb655Srie 12285aefb655Srie /* 1229d326b23bSrie * If we're building an executable - use either the IE or LE access 1230d326b23bSrie * model. If we're building a shared object process any IE model. 12315aefb655Srie */ 1232d326b23bSrie if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) { 12335aefb655Srie /* 1234d326b23bSrie * Set the DF_STATIC_TLS flag. 12355aefb655Srie */ 12365aefb655Srie ofl->ofl_dtflags |= DF_STATIC_TLS; 12375aefb655Srie 1238d326b23bSrie if (!local || ((flags & FLG_OF_EXEC) == 0)) { 12395aefb655Srie /* 1240d326b23bSrie * Assign a GOT entry for static TLS references. 12415aefb655Srie */ 12425aefb655Srie if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 1243d326b23bSrie GOT_REF_TLSIE, ofl, rsp)) == 0) { 1244d326b23bSrie 1245d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, 1246d326b23bSrie gnp, GOT_REF_TLSIE, FLG_REL_STLS, 1247d326b23bSrie rtype, R_AMD64_TPOFF64, 0) == S_ERROR) 12485aefb655Srie return (S_ERROR); 12495aefb655Srie } 1250d326b23bSrie 1251d326b23bSrie /* 1252d326b23bSrie * IE access model. 1253d326b23bSrie */ 12545aefb655Srie if (IS_TLS_IE(rtype)) 12555aefb655Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 12565aefb655Srie 12575aefb655Srie /* 1258d326b23bSrie * Fixups are required for other executable models. 12595aefb655Srie */ 12605aefb655Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 12615aefb655Srie rsp, ofl)); 12625aefb655Srie } 1263d326b23bSrie 12645aefb655Srie /* 1265d326b23bSrie * LE access model. 12665aefb655Srie */ 12675aefb655Srie if (IS_TLS_LE(rtype)) 12685aefb655Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 1269d326b23bSrie 12705aefb655Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 12715aefb655Srie rsp, ofl)); 12725aefb655Srie } 12735aefb655Srie 12745aefb655Srie /* 1275d326b23bSrie * Building a shared object. 1276d326b23bSrie * 1277d326b23bSrie * Assign a GOT entry for a dynamic TLS reference. 12785aefb655Srie */ 12795aefb655Srie if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 12805aefb655Srie GOT_REF_TLSLD, ofl, rsp)) == 0)) { 1281d326b23bSrie 1282d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD, 1283d326b23bSrie FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, 0) == S_ERROR) 12845aefb655Srie return (S_ERROR); 1285d326b23bSrie 12865aefb655Srie } else if (IS_TLS_GD(rtype) && 1287d326b23bSrie ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD, 1288d326b23bSrie ofl, rsp)) == 0)) { 1289d326b23bSrie 1290d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD, 1291d326b23bSrie FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64, 1292d326b23bSrie R_AMD64_DTPOFF64) == S_ERROR) 12935aefb655Srie return (S_ERROR); 12945aefb655Srie } 12955aefb655Srie 12965aefb655Srie if (IS_TLS_LD(rtype)) 12975aefb655Srie return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl)); 12985aefb655Srie 12995aefb655Srie return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl)); 13005aefb655Srie } 13015aefb655Srie 13025aefb655Srie /* ARGSUSED3 */ 1303ba2be530Sab196087 static Gotndx * 13045aefb655Srie ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc) 13055aefb655Srie { 13065aefb655Srie Listnode * lnp; 13075aefb655Srie Gotndx * gnp; 13085aefb655Srie 13095aefb655Srie assert(rdesc != 0); 13105aefb655Srie 13115aefb655Srie if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx) 13125aefb655Srie return (ofl->ofl_tlsldgotndx); 13135aefb655Srie 13145aefb655Srie for (LIST_TRAVERSE(lst, lnp, gnp)) { 13155aefb655Srie if ((rdesc->rel_raddend == gnp->gn_addend) && 13165aefb655Srie (gnp->gn_gotref == gref)) { 13175aefb655Srie return (gnp); 13185aefb655Srie } 13195aefb655Srie } 13205aefb655Srie return ((Gotndx *)0); 13215aefb655Srie } 13225aefb655Srie 1323ba2be530Sab196087 static Xword 13245aefb655Srie ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl) 13255aefb655Srie { 13265aefb655Srie Os_desc *osp = ofl->ofl_osgot; 13275aefb655Srie Sym_desc *sdp = rdesc->rel_sym; 13285aefb655Srie Xword gotndx; 13295aefb655Srie Gotref gref; 13305aefb655Srie Gotndx *gnp; 13315aefb655Srie 13325aefb655Srie if (rdesc->rel_flags & FLG_REL_DTLS) 13335aefb655Srie gref = GOT_REF_TLSGD; 13345aefb655Srie else if (rdesc->rel_flags & FLG_REL_MTLS) 13355aefb655Srie gref = GOT_REF_TLSLD; 13365aefb655Srie else if (rdesc->rel_flags & FLG_REL_STLS) 13375aefb655Srie gref = GOT_REF_TLSIE; 13385aefb655Srie else 13395aefb655Srie gref = GOT_REF_GENERIC; 13405aefb655Srie 13415aefb655Srie gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, rdesc); 13425aefb655Srie assert(gnp); 13435aefb655Srie 13445aefb655Srie gotndx = (Xword)gnp->gn_gotndx; 13455aefb655Srie 13465aefb655Srie if ((rdesc->rel_flags & FLG_REL_DTLS) && 13475aefb655Srie (rdesc->rel_rtype == R_AMD64_DTPOFF64)) 13485aefb655Srie gotndx++; 13495aefb655Srie 13505aefb655Srie return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE))); 13515aefb655Srie } 13525aefb655Srie 13535aefb655Srie 13545aefb655Srie /* ARGSUSED5 */ 1355ba2be530Sab196087 static uintptr_t 1356d326b23bSrie ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl, 13575aefb655Srie Rel_desc * rsp, Sym_desc * sdp) 13585aefb655Srie { 13595aefb655Srie Xword raddend; 13605aefb655Srie Gotndx *gnp, *_gnp; 13615aefb655Srie Listnode *lnp, *plnp; 13625aefb655Srie uint_t gotents; 13635aefb655Srie 13645aefb655Srie raddend = rsp->rel_raddend; 13655aefb655Srie if (pgnp && (pgnp->gn_addend == raddend) && 13665aefb655Srie (pgnp->gn_gotref == gref)) 13675aefb655Srie return (1); 13685aefb655Srie 13695aefb655Srie if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD)) 13705aefb655Srie gotents = 2; 13715aefb655Srie else 13725aefb655Srie gotents = 1; 13735aefb655Srie 13745aefb655Srie plnp = 0; 13755aefb655Srie for (LIST_TRAVERSE(lst, lnp, _gnp)) { 13765aefb655Srie if (_gnp->gn_addend > raddend) 13775aefb655Srie break; 13785aefb655Srie plnp = lnp; 13795aefb655Srie } 13805aefb655Srie 13815aefb655Srie /* 13825aefb655Srie * Allocate a new entry. 13835aefb655Srie */ 13845aefb655Srie if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0) 13855aefb655Srie return (S_ERROR); 13865aefb655Srie gnp->gn_addend = raddend; 13875aefb655Srie gnp->gn_gotndx = ofl->ofl_gotcnt; 13885aefb655Srie gnp->gn_gotref = gref; 13895aefb655Srie 13905aefb655Srie ofl->ofl_gotcnt += gotents; 13915aefb655Srie 13925aefb655Srie if (gref == GOT_REF_TLSLD) { 13935aefb655Srie ofl->ofl_tlsldgotndx = gnp; 13945aefb655Srie return (1); 13955aefb655Srie } 13965aefb655Srie 13975aefb655Srie if (plnp == 0) { 13985aefb655Srie /* 13995aefb655Srie * Insert at head of list 14005aefb655Srie */ 14015aefb655Srie if (list_prependc(lst, (void *)gnp) == 0) 14025aefb655Srie return (S_ERROR); 14035aefb655Srie } else if (_gnp->gn_addend > raddend) { 14045aefb655Srie /* 14055aefb655Srie * Insert in middle of lest 14065aefb655Srie */ 14075aefb655Srie if (list_insertc(lst, (void *)gnp, plnp) == 0) 14085aefb655Srie return (S_ERROR); 14095aefb655Srie } else { 14105aefb655Srie /* 14115aefb655Srie * Append to tail of list 14125aefb655Srie */ 14135aefb655Srie if (list_appendc(lst, (void *)gnp) == 0) 14145aefb655Srie return (S_ERROR); 14155aefb655Srie } 14165aefb655Srie return (1); 14175aefb655Srie } 14185aefb655Srie 1419ba2be530Sab196087 static void 14205aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl) 14215aefb655Srie { 14225aefb655Srie sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++; 14235aefb655Srie sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++; 14245aefb655Srie ofl->ofl_flags |= FLG_OF_BLDGOT; 14255aefb655Srie } 14265aefb655Srie 14275aefb655Srie static uchar_t plt0_template[M_PLT_ENTSIZE] = { 14285aefb655Srie /* 0x00 PUSHQ GOT+8(%rip) */ 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, 14295aefb655Srie /* 0x06 JMP *GOT+16(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 14305aefb655Srie /* 0x0c NOP */ 0x90, 14315aefb655Srie /* 0x0d NOP */ 0x90, 14325aefb655Srie /* 0x0e NOP */ 0x90, 14335aefb655Srie /* 0x0f NOP */ 0x90 14345aefb655Srie }; 14355aefb655Srie 14365aefb655Srie /* 14375aefb655Srie * Initializes .got[0] with the _DYNAMIC symbol value. 14385aefb655Srie */ 1439ba2be530Sab196087 static uintptr_t 14405aefb655Srie ld_fillin_gotplt(Ofl_desc *ofl) 14415aefb655Srie { 1442ba2be530Sab196087 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 1443ba2be530Sab196087 14445aefb655Srie if (ofl->ofl_osgot) { 14455aefb655Srie Sym_desc *sdp; 14465aefb655Srie 14475aefb655Srie if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U), 14485aefb655Srie SYM_NOHASH, 0, ofl)) != NULL) { 1449d326b23bSrie uchar_t *genptr; 1450d326b23bSrie 1451d326b23bSrie genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf + 14525aefb655Srie (M_GOT_XDYNAMIC * M_GOT_ENTSIZE)); 14535aefb655Srie /* LINTED */ 14545aefb655Srie *(Xword *)genptr = sdp->sd_sym->st_value; 1455ba2be530Sab196087 if (bswap) 1456ba2be530Sab196087 /* LINTED */ 1457ba2be530Sab196087 *(Xword *)genptr = 1458ba2be530Sab196087 /* LINTED */ 1459ba2be530Sab196087 ld_bswap_Xword(*(Xword *)genptr); 14605aefb655Srie } 14615aefb655Srie } 14625aefb655Srie 14635aefb655Srie /* 14645aefb655Srie * Fill in the reserved slot in the procedure linkage table the first 14655aefb655Srie * entry is: 14665aefb655Srie * 0x00 PUSHQ GOT+8(%rip) # GOT[1] 14675aefb655Srie * 0x06 JMP *GOT+16(%rip) # GOT[2] 14685aefb655Srie * 0x0c NOP 14695aefb655Srie * 0x0d NOP 14705aefb655Srie * 0x0e NOP 14715aefb655Srie * 0x0f NOP 14725aefb655Srie */ 1473f3324781Sab196087 if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) { 14745aefb655Srie uchar_t *pltent; 14755aefb655Srie Xword val1; 14765aefb655Srie 14775aefb655Srie pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf; 14785aefb655Srie bcopy(plt0_template, pltent, sizeof (plt0_template)); 14795aefb655Srie 14805aefb655Srie /* 1481f3324781Sab196087 * If '-z noreloc' is specified - skip the do_reloc_ld 1482f3324781Sab196087 * stage. 1483f3324781Sab196087 */ 1484f3324781Sab196087 if (!OFL_DO_RELOC(ofl)) 1485f3324781Sab196087 return (1); 1486f3324781Sab196087 1487f3324781Sab196087 /* 14885aefb655Srie * filin: 14895aefb655Srie * PUSHQ GOT + 8(%rip) 14905aefb655Srie * 14915aefb655Srie * Note: 0x06 below represents the offset to the 14925aefb655Srie * next instruction - which is what %rip will 14935aefb655Srie * be pointing at. 14945aefb655Srie */ 14955aefb655Srie val1 = (ofl->ofl_osgot->os_shdr->sh_addr) + 14965aefb655Srie (M_GOT_XLINKMAP * M_GOT_ENTSIZE) - 14975aefb655Srie ofl->ofl_osplt->os_shdr->sh_addr - 0x06; 14985aefb655Srie 1499f3324781Sab196087 if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02], 15005aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 1501f3324781Sab196087 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 15025aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 15035aefb655Srie MSG_INTL(MSG_PLT_PLT0FAIL)); 15045aefb655Srie return (S_ERROR); 15055aefb655Srie } 15065aefb655Srie 15075aefb655Srie /* 15085aefb655Srie * filin: 15095aefb655Srie * JMP *GOT+16(%rip) 15105aefb655Srie */ 15115aefb655Srie val1 = (ofl->ofl_osgot->os_shdr->sh_addr) + 15125aefb655Srie (M_GOT_XRTLD * M_GOT_ENTSIZE) - 15135aefb655Srie ofl->ofl_osplt->os_shdr->sh_addr - 0x0c; 1514f3324781Sab196087 1515f3324781Sab196087 if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x08], 15165aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 1517f3324781Sab196087 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 15185aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 15195aefb655Srie MSG_INTL(MSG_PLT_PLT0FAIL)); 15205aefb655Srie return (S_ERROR); 15215aefb655Srie } 15225aefb655Srie } 1523f3324781Sab196087 15245aefb655Srie return (1); 15255aefb655Srie } 1526ba2be530Sab196087 1527ba2be530Sab196087 1528ba2be530Sab196087 1529ba2be530Sab196087 /* 1530ba2be530Sab196087 * Template for generating "void (*)(void)" function 1531ba2be530Sab196087 */ 1532ba2be530Sab196087 static const uchar_t nullfunc_tmpl[] = { /* amd64 */ 1533ba2be530Sab196087 /* 0x00 */ 0x55, /* pushq %rbp */ 1534ba2be530Sab196087 /* 0x01 */ 0x48, 0x8b, 0xec, /* movq %rsp,%rbp */ 1535ba2be530Sab196087 /* 0x04 */ 0x48, 0x8b, 0xe5, /* movq %rbp,%rsp */ 1536ba2be530Sab196087 /* 0x07 */ 0x5d, /* popq %rbp */ 1537ba2be530Sab196087 /* 0x08 */ 0xc3 /* ret */ 1538ba2be530Sab196087 }; 1539ba2be530Sab196087 1540ba2be530Sab196087 1541ba2be530Sab196087 /* 1542ba2be530Sab196087 * Return the ld_targ definition for this target. 1543ba2be530Sab196087 */ 1544ba2be530Sab196087 const Target * 1545ba2be530Sab196087 ld_targ_init_x86(void) 1546ba2be530Sab196087 { 1547ba2be530Sab196087 static const Target _ld_targ = { 1548ba2be530Sab196087 { /* Target_mach */ 1549ba2be530Sab196087 M_MACH, /* m_mach */ 1550ba2be530Sab196087 M_MACHPLUS, /* m_machplus */ 1551ba2be530Sab196087 M_FLAGSPLUS, /* m_flagsplus */ 1552ba2be530Sab196087 M_CLASS, /* m_class */ 1553ba2be530Sab196087 M_DATA, /* m_data */ 1554ba2be530Sab196087 1555ba2be530Sab196087 M_SEGM_ALIGN, /* m_segm_align */ 1556ba2be530Sab196087 M_SEGM_ORIGIN, /* m_segm_origin */ 1557ba2be530Sab196087 M_DATASEG_PERM, /* m_dataseg_perm */ 1558ba2be530Sab196087 M_WORD_ALIGN, /* m_word_align */ 1559ba2be530Sab196087 MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */ 1560ba2be530Sab196087 1561ba2be530Sab196087 /* Relocation type codes */ 1562ba2be530Sab196087 M_R_ARRAYADDR, /* m_r_arrayaddr */ 1563ba2be530Sab196087 M_R_COPY, /* m_r_copy */ 1564ba2be530Sab196087 M_R_GLOB_DAT, /* m_r_glob_dat */ 1565ba2be530Sab196087 M_R_JMP_SLOT, /* m_r_jmp_slot */ 1566ba2be530Sab196087 M_R_NUM, /* m_r_num */ 1567ba2be530Sab196087 M_R_NONE, /* m_r_none */ 1568ba2be530Sab196087 M_R_RELATIVE, /* m_r_relative */ 1569ba2be530Sab196087 M_R_REGISTER, /* m_r_register */ 1570ba2be530Sab196087 1571ba2be530Sab196087 /* Relocation related constants */ 1572ba2be530Sab196087 M_REL_DT_COUNT, /* m_rel_dt_count */ 1573ba2be530Sab196087 M_REL_DT_ENT, /* m_rel_dt_ent */ 1574ba2be530Sab196087 M_REL_DT_SIZE, /* m_rel_dt_size */ 1575ba2be530Sab196087 M_REL_DT_TYPE, /* m_rel_dt_type */ 1576ba2be530Sab196087 M_REL_SHT_TYPE, /* m_rel_sht_type */ 1577ba2be530Sab196087 1578ba2be530Sab196087 /* GOT related constants */ 1579ba2be530Sab196087 M_GOT_ENTSIZE, /* m_got_entsize */ 1580ba2be530Sab196087 M_GOT_XNumber, /* m_got_xnumber */ 1581ba2be530Sab196087 1582ba2be530Sab196087 /* PLT related constants */ 1583ba2be530Sab196087 M_PLT_ALIGN, /* m_plt_align */ 1584ba2be530Sab196087 M_PLT_ENTSIZE, /* m_plt_entsize */ 1585ba2be530Sab196087 M_PLT_RESERVSZ, /* m_plt_reservsz */ 1586ba2be530Sab196087 M_PLT_SHF_FLAGS, /* m_plt_shf_flags */ 1587ba2be530Sab196087 1588ba2be530Sab196087 M_DT_REGISTER, /* m_dt_register */ 1589ba2be530Sab196087 }, 1590ba2be530Sab196087 { /* Target_machid */ 1591ba2be530Sab196087 M_ID_ARRAY, /* id_array */ 1592ba2be530Sab196087 M_ID_BSS, /* id_bss */ 1593ba2be530Sab196087 M_ID_CAP, /* id_cap */ 1594ba2be530Sab196087 M_ID_DATA, /* id_data */ 1595ba2be530Sab196087 M_ID_DYNAMIC, /* id_dynamic */ 1596ba2be530Sab196087 M_ID_DYNSORT, /* id_dynsort */ 1597ba2be530Sab196087 M_ID_DYNSTR, /* id_dynstr */ 1598ba2be530Sab196087 M_ID_DYNSYM, /* id_dynsym */ 1599ba2be530Sab196087 M_ID_DYNSYM_NDX, /* id_dynsym_ndx */ 1600ba2be530Sab196087 M_ID_GOT, /* id_got */ 1601ba2be530Sab196087 M_ID_UNKNOWN, /* id_gotdata (unused) */ 1602ba2be530Sab196087 M_ID_HASH, /* id_hash */ 1603ba2be530Sab196087 M_ID_INTERP, /* id_interp */ 1604ba2be530Sab196087 M_ID_LBSS, /* id_lbss */ 1605ba2be530Sab196087 M_ID_LDYNSYM, /* id_ldynsym */ 1606ba2be530Sab196087 M_ID_NOTE, /* id_note */ 1607ba2be530Sab196087 M_ID_NULL, /* id_null */ 1608ba2be530Sab196087 M_ID_PLT, /* id_plt */ 1609ba2be530Sab196087 M_ID_REL, /* id_rel */ 1610ba2be530Sab196087 M_ID_STRTAB, /* id_strtab */ 1611ba2be530Sab196087 M_ID_SYMINFO, /* id_syminfo */ 1612ba2be530Sab196087 M_ID_SYMTAB, /* id_symtab */ 1613ba2be530Sab196087 M_ID_SYMTAB_NDX, /* id_symtab_ndx */ 1614ba2be530Sab196087 M_ID_TEXT, /* id_text */ 1615ba2be530Sab196087 M_ID_TLS, /* id_tls */ 1616ba2be530Sab196087 M_ID_TLSBSS, /* id_tlsbss */ 1617ba2be530Sab196087 M_ID_UNKNOWN, /* id_unknown */ 1618ba2be530Sab196087 M_ID_UNWIND, /* id_unwind */ 1619ba2be530Sab196087 M_ID_USER, /* id_user */ 1620ba2be530Sab196087 M_ID_VERSION, /* id_version */ 1621ba2be530Sab196087 }, 1622ba2be530Sab196087 { /* Target_nullfunc */ 1623ba2be530Sab196087 nullfunc_tmpl, /* nf_template */ 1624ba2be530Sab196087 sizeof (nullfunc_tmpl), /* nf_size */ 1625ba2be530Sab196087 }, 1626ba2be530Sab196087 { /* Target_machrel */ 1627ba2be530Sab196087 reloc_table, 1628ba2be530Sab196087 1629ba2be530Sab196087 ld_init_rel, /* mr_init_rel */ 1630ba2be530Sab196087 ld_mach_eflags, /* mr_mach_eflags */ 1631ba2be530Sab196087 ld_mach_make_dynamic, /* mr_mach_make_dynamic */ 1632ba2be530Sab196087 ld_mach_update_odynamic, /* mr_mach_update_odynamic */ 1633ba2be530Sab196087 ld_calc_plt_addr, /* mr_calc_plt_addr */ 1634ba2be530Sab196087 ld_perform_outreloc, /* mr_perform_outreloc */ 1635ba2be530Sab196087 ld_do_activerelocs, /* mr_do_activerelocs */ 1636ba2be530Sab196087 ld_add_outrel, /* mr_add_outrel */ 1637ba2be530Sab196087 NULL, /* mr_reloc_register */ 1638ba2be530Sab196087 ld_reloc_local, /* mr_reloc_local */ 1639ba2be530Sab196087 NULL, /* mr_reloc_GOTOP */ 1640ba2be530Sab196087 ld_reloc_TLS, /* mr_reloc_TLS */ 1641ba2be530Sab196087 NULL, /* mr_assign_got */ 1642ba2be530Sab196087 ld_find_gotndx, /* mr_find_gotndx */ 1643ba2be530Sab196087 ld_calc_got_offset, /* mr_calc_got_offset */ 1644ba2be530Sab196087 ld_assign_got_ndx, /* mr_assign_got_ndx */ 1645ba2be530Sab196087 ld_assign_plt_ndx, /* mr_assign_plt_ndx */ 1646ba2be530Sab196087 NULL, /* mr_allocate_got */ 1647ba2be530Sab196087 ld_fillin_gotplt, /* mr_fillin_gotplt */ 1648ba2be530Sab196087 }, 1649ba2be530Sab196087 { /* Target_machsym */ 1650ba2be530Sab196087 NULL, /* ms_reg_check */ 1651ba2be530Sab196087 NULL, /* ms_mach_sym_typecheck */ 1652ba2be530Sab196087 NULL, /* ms_is_regsym */ 1653ba2be530Sab196087 NULL, /* ms_reg_find */ 1654ba2be530Sab196087 NULL /* ms_reg_enter */ 1655ba2be530Sab196087 }, 1656ba2be530Sab196087 { /* Target_unwind */ 1657ba2be530Sab196087 make_amd64_unwindhdr, /* uw_make_unwindhdr */ 1658ba2be530Sab196087 populate_amd64_unwindhdr, /* uw_populate_unwindhdr */ 1659ba2be530Sab196087 append_amd64_unwind, /* uw_append_unwind */ 1660ba2be530Sab196087 } 1661ba2be530Sab196087 }; 1662ba2be530Sab196087 1663ba2be530Sab196087 return (&_ld_targ); 1664ba2be530Sab196087 } 1665