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 /* 235aefb655Srie * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 245aefb655Srie * Use is subject to license terms. 255aefb655Srie */ 265aefb655Srie #pragma ident "%Z%%M% %I% %E% SMI" 275aefb655Srie 285aefb655Srie #include <string.h> 295aefb655Srie #include <stdio.h> 305aefb655Srie #include <strings.h> 315aefb655Srie #include <sys/elf_amd64.h> 325aefb655Srie #include <debug.h> 335aefb655Srie #include <reloc.h> 345aefb655Srie #include "msg.h" 355aefb655Srie #include "_libld.h" 365aefb655Srie 375aefb655Srie Word 385aefb655Srie ld_init_rel(Rel_desc *reld, void *reloc) 395aefb655Srie { 405aefb655Srie Rela * rel = (Rela *)reloc; 415aefb655Srie 425aefb655Srie /* LINTED */ 435aefb655Srie reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info); 445aefb655Srie reld->rel_roffset = rel->r_offset; 455aefb655Srie reld->rel_raddend = rel->r_addend; 465aefb655Srie reld->rel_typedata = 0; 475aefb655Srie 485aefb655Srie reld->rel_flags |= FLG_REL_RELA; 495aefb655Srie 505aefb655Srie return ((Word)ELF_R_SYM(rel->r_info)); 515aefb655Srie } 525aefb655Srie 535aefb655Srie void 545aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl) 555aefb655Srie { 565aefb655Srie ofl->ofl_dehdr->e_flags |= ehdr->e_flags; 575aefb655Srie } 585aefb655Srie 595aefb655Srie void 605aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt) 615aefb655Srie { 625aefb655Srie if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) { 635aefb655Srie /* 645aefb655Srie * Create this entry if we are going to create a PLT table. 655aefb655Srie */ 665aefb655Srie if (ofl->ofl_pltcnt) 675aefb655Srie (*cnt)++; /* DT_PLTGOT */ 685aefb655Srie } 695aefb655Srie } 705aefb655Srie 715aefb655Srie void 725aefb655Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn) 735aefb655Srie { 745aefb655Srie if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) { 755aefb655Srie (*dyn)->d_tag = DT_PLTGOT; 765aefb655Srie if (ofl->ofl_osgot) 775aefb655Srie (*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr; 785aefb655Srie else 795aefb655Srie (*dyn)->d_un.d_ptr = 0; 805aefb655Srie (*dyn)++; 815aefb655Srie } 825aefb655Srie } 835aefb655Srie 845aefb655Srie Xword 855aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl) 865aefb655Srie { 875aefb655Srie Xword value; 885aefb655Srie 895aefb655Srie value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) + 905aefb655Srie M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE); 915aefb655Srie return (value); 925aefb655Srie } 935aefb655Srie 945aefb655Srie /* 955aefb655Srie * Build a single plt entry - code is: 965aefb655Srie * JMP *name1@GOTPCREL(%rip) 975aefb655Srie * PUSHL $index 985aefb655Srie * JMP .PLT0 995aefb655Srie */ 1005aefb655Srie static uchar_t pltn_entry[M_PLT_ENTSIZE] = { 1015aefb655Srie /* 0x00 jmpq *name1@GOTPCREL(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 1025aefb655Srie /* 0x06 pushq $index */ 0x68, 0x00, 0x00, 0x00, 0x00, 1035aefb655Srie /* 0x0b jmpq .plt0(%rip) */ 0xe9, 0x00, 0x00, 0x00, 0x00 1045aefb655Srie /* 0x10 */ 1055aefb655Srie }; 1065aefb655Srie 1075aefb655Srie static uintptr_t 1085aefb655Srie plt_entry(Ofl_desc * ofl, Sym_desc * sdp) 1095aefb655Srie { 1105aefb655Srie uchar_t *plt0, *pltent, *gotent; 1115aefb655Srie Sword plt_off; 1125aefb655Srie Word got_off; 1135aefb655Srie Xword val1; 1145aefb655Srie Word flags = ofl->ofl_flags; 1155aefb655Srie Word dtflags1 = ofl->ofl_dtflags_1; 1165aefb655Srie 1175aefb655Srie got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE; 1185aefb655Srie plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * 1195aefb655Srie M_PLT_ENTSIZE); 1205aefb655Srie plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf); 1215aefb655Srie pltent = plt0 + plt_off; 1225aefb655Srie gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off; 1235aefb655Srie 1245aefb655Srie bcopy(pltn_entry, pltent, sizeof (pltn_entry)); 1255aefb655Srie /* 1265aefb655Srie * Fill in the got entry with the address of the next instruction. 1275aefb655Srie */ 1285aefb655Srie /* LINTED */ 1295aefb655Srie *(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off + 1305aefb655Srie M_PLT_INSSIZE; 1315aefb655Srie 1325aefb655Srie /* 1335aefb655Srie * patchup: 1345aefb655Srie * jmpq *name1@gotpcrel(%rip) 1355aefb655Srie * 1365aefb655Srie * NOTE: 0x06 represents next instruction. 1375aefb655Srie */ 1385aefb655Srie val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) - 1395aefb655Srie (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06; 1405aefb655Srie 1415aefb655Srie /* 1425aefb655Srie * If '-z noreloc' is specified - skip the do_reloc 1435aefb655Srie * stage. 1445aefb655Srie */ 1455aefb655Srie if ((flags & FLG_OF_RELOBJ) || 1465aefb655Srie !(dtflags1 & DF_1_NORELOC)) { 1475aefb655Srie if (do_reloc(R_AMD64_GOTPCREL, &pltent[0x02], 1485aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 1495aefb655Srie MSG_ORIG(MSG_SPECFIL_PLTENT), ofl->ofl_lml) == 0) { 1505aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 1515aefb655Srie MSG_INTL(MSG_PLT_PLTNFAIL), sdp->sd_aux->sa_PLTndx, 1525aefb655Srie demangle(sdp->sd_name)); 1535aefb655Srie return (S_ERROR); 1545aefb655Srie } 1555aefb655Srie } 1565aefb655Srie 1575aefb655Srie /* 1585aefb655Srie * patchup: 1595aefb655Srie * pushq $pltndx 1605aefb655Srie */ 1615aefb655Srie val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1); 1625aefb655Srie /* 1635aefb655Srie * If '-z noreloc' is specified - skip the do_reloc 1645aefb655Srie * stage. 1655aefb655Srie */ 1665aefb655Srie if ((flags & FLG_OF_RELOBJ) || 1675aefb655Srie !(dtflags1 & DF_1_NORELOC)) { 1685aefb655Srie if (do_reloc(R_AMD64_32, &pltent[0x07], 1695aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 1705aefb655Srie MSG_ORIG(MSG_SPECFIL_PLTENT), ofl->ofl_lml) == 0) { 1715aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 1725aefb655Srie MSG_INTL(MSG_PLT_PLTNFAIL), sdp->sd_aux->sa_PLTndx, 1735aefb655Srie demangle(sdp->sd_name)); 1745aefb655Srie return (S_ERROR); 1755aefb655Srie } 1765aefb655Srie } 1775aefb655Srie 1785aefb655Srie /* 1795aefb655Srie * patchup: 1805aefb655Srie * jmpq .plt0(%rip) 1815aefb655Srie * NOTE: 0x10 represents next instruction. The rather complex series 1825aefb655Srie * of casts is necessary to sign extend an offset into a 64-bit value 1835aefb655Srie * while satisfying various compiler error checks. Handle with care. 1845aefb655Srie */ 1855aefb655Srie val1 = (Xword)((intptr_t)((uintptr_t)plt0 - 1865aefb655Srie (uintptr_t)(&pltent[0x10]))); 1875aefb655Srie 1885aefb655Srie /* 1895aefb655Srie * If '-z noreloc' is specified - skip the do_reloc 1905aefb655Srie * stage. 1915aefb655Srie */ 1925aefb655Srie if ((flags & FLG_OF_RELOBJ) || 1935aefb655Srie !(dtflags1 & DF_1_NORELOC)) { 1945aefb655Srie if (do_reloc(R_AMD64_PC32, &pltent[0x0c], 1955aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 1965aefb655Srie MSG_ORIG(MSG_SPECFIL_PLTENT), ofl->ofl_lml) == 0) { 1975aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 1985aefb655Srie MSG_INTL(MSG_PLT_PLTNFAIL), sdp->sd_aux->sa_PLTndx, 1995aefb655Srie demangle(sdp->sd_name)); 2005aefb655Srie return (S_ERROR); 2015aefb655Srie } 2025aefb655Srie } 2035aefb655Srie return (1); 2045aefb655Srie } 2055aefb655Srie 2065aefb655Srie uintptr_t 2075aefb655Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl) 2085aefb655Srie { 2095aefb655Srie Os_desc * relosp, * osp = 0; 2105aefb655Srie Word ndx; 2115aefb655Srie Xword roffset, value; 2125aefb655Srie Sxword raddend; 2135aefb655Srie Rela rea; 2145aefb655Srie char *relbits; 2155aefb655Srie Sym_desc * sdp, * psym = (Sym_desc *)0; 2165aefb655Srie int sectmoved = 0; 2175aefb655Srie 2185aefb655Srie raddend = orsp->rel_raddend; 2195aefb655Srie sdp = orsp->rel_sym; 2205aefb655Srie 2215aefb655Srie /* 2225aefb655Srie * If the section this relocation is against has been discarded 2235aefb655Srie * (-zignore), then also discard (skip) the relocation itself. 2245aefb655Srie */ 2255aefb655Srie if (orsp->rel_isdesc && ((orsp->rel_flags & 2265aefb655Srie (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) && 2275aefb655Srie (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) { 2285aefb655Srie DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp)); 2295aefb655Srie return (1); 2305aefb655Srie } 2315aefb655Srie 2325aefb655Srie /* 2335aefb655Srie * If this is a relocation against a move table, or expanded move 2345aefb655Srie * table, adjust the relocation entries. 2355aefb655Srie */ 2365aefb655Srie if (orsp->rel_move) 2375aefb655Srie ld_adj_movereloc(ofl, orsp); 2385aefb655Srie 2395aefb655Srie /* 2405aefb655Srie * If this is a relocation against a section then we need to adjust the 2415aefb655Srie * raddend field to compensate for the new position of the input section 2425aefb655Srie * within the new output section. 2435aefb655Srie */ 2445aefb655Srie if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) { 2455aefb655Srie if (ofl->ofl_parsym.head && 2465aefb655Srie (sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 2475aefb655Srie /* LINTED */ 2485aefb655Srie (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) { 2495aefb655Srie DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym)); 2505aefb655Srie sectmoved = 1; 2515aefb655Srie if (ofl->ofl_flags & FLG_OF_RELOBJ) 2525aefb655Srie raddend = psym->sd_sym->st_value; 2535aefb655Srie else 2545aefb655Srie raddend = psym->sd_sym->st_value - 2555aefb655Srie psym->sd_isc->is_osdesc->os_shdr->sh_addr; 2565aefb655Srie /* LINTED */ 2575aefb655Srie raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata); 2585aefb655Srie if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 2595aefb655Srie raddend += 2605aefb655Srie psym->sd_isc->is_osdesc->os_shdr->sh_addr; 2615aefb655Srie } else { 2625aefb655Srie /* LINTED */ 2635aefb655Srie raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata); 2645aefb655Srie if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 2655aefb655Srie raddend += 2665aefb655Srie sdp->sd_isc->is_osdesc->os_shdr->sh_addr; 2675aefb655Srie } 2685aefb655Srie } 2695aefb655Srie 2705aefb655Srie value = sdp->sd_sym->st_value; 2715aefb655Srie 2725aefb655Srie if (orsp->rel_flags & FLG_REL_GOT) { 2735aefb655Srie /* 2745aefb655Srie * Note: for GOT relative relocations on amd64 2755aefb655Srie * we discard the addend. It was relevant 2765aefb655Srie * to the reference - not to the data item 2775aefb655Srie * being referenced (ie: that -4 thing). 2785aefb655Srie */ 2795aefb655Srie raddend = 0; 2805aefb655Srie osp = ofl->ofl_osgot; 2815aefb655Srie roffset = ld_calc_got_offset(orsp, ofl); 2825aefb655Srie 2835aefb655Srie } else if (orsp->rel_flags & FLG_REL_PLT) { 2845aefb655Srie /* 2855aefb655Srie * Note that relocations for PLT's actually 2865aefb655Srie * cause a relocation againt the GOT. 2875aefb655Srie */ 2885aefb655Srie osp = ofl->ofl_osplt; 2895aefb655Srie roffset = (ofl->ofl_osgot->os_shdr->sh_addr) + 2905aefb655Srie sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE; 2915aefb655Srie raddend = 0; 2925aefb655Srie if (plt_entry(ofl, sdp) == S_ERROR) 2935aefb655Srie return (S_ERROR); 2945aefb655Srie 2955aefb655Srie } else if (orsp->rel_flags & FLG_REL_BSS) { 2965aefb655Srie /* 2975aefb655Srie * This must be a R_AMD64_COPY. For these set the roffset to 2985aefb655Srie * point to the new symbols location. 2995aefb655Srie */ 3005aefb655Srie osp = ofl->ofl_isbss->is_osdesc; 3015aefb655Srie roffset = value; 3025aefb655Srie 3035aefb655Srie /* 3045aefb655Srie * The raddend doesn't mean anything in a R_SPARC_COPY 3055aefb655Srie * relocation. Null it out because it can confuse people. 3065aefb655Srie */ 3075aefb655Srie raddend = 0; 3085aefb655Srie } else { 3095aefb655Srie osp = orsp->rel_osdesc; 3105aefb655Srie 3115aefb655Srie /* 3125aefb655Srie * Calculate virtual offset of reference point; equals offset 3135aefb655Srie * into section + vaddr of section for loadable sections, or 3145aefb655Srie * offset plus section displacement for nonloadable sections. 3155aefb655Srie */ 3165aefb655Srie roffset = orsp->rel_roffset + 3175aefb655Srie (Off)_elf_getxoff(orsp->rel_isdesc->is_indata); 3185aefb655Srie if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) 3195aefb655Srie roffset += orsp->rel_isdesc->is_osdesc-> 3205aefb655Srie os_shdr->sh_addr; 3215aefb655Srie } 3225aefb655Srie 3235aefb655Srie if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0)) 3245aefb655Srie relosp = ofl->ofl_osrel; 3255aefb655Srie 3265aefb655Srie /* 3275aefb655Srie * Assign the symbols index for the output relocation. If the 3285aefb655Srie * relocation refers to a SECTION symbol then it's index is based upon 3295aefb655Srie * the output sections symbols index. Otherwise the index can be 3305aefb655Srie * derived from the symbols index itself. 3315aefb655Srie */ 3325aefb655Srie if (orsp->rel_rtype == R_AMD64_RELATIVE) 3335aefb655Srie ndx = STN_UNDEF; 3345aefb655Srie else if ((orsp->rel_flags & FLG_REL_SCNNDX) || 3355aefb655Srie (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) { 3365aefb655Srie if (sectmoved == 0) { 3375aefb655Srie /* 3385aefb655Srie * Check for a null input section. This can 3395aefb655Srie * occur if this relocation references a symbol 3405aefb655Srie * generated by sym_add_sym(). 3415aefb655Srie */ 3425aefb655Srie if ((sdp->sd_isc != 0) && 3435aefb655Srie (sdp->sd_isc->is_osdesc != 0)) 3445aefb655Srie ndx = sdp->sd_isc->is_osdesc->os_scnsymndx; 3455aefb655Srie else 3465aefb655Srie ndx = sdp->sd_shndx; 3475aefb655Srie } else 3485aefb655Srie ndx = ofl->ofl_sunwdata1ndx; 3495aefb655Srie } else 3505aefb655Srie ndx = sdp->sd_symndx; 3515aefb655Srie 3525aefb655Srie /* 3535aefb655Srie * Add the symbols 'value' to the addend field. 3545aefb655Srie */ 3555aefb655Srie if (orsp->rel_flags & FLG_REL_ADVAL) 3565aefb655Srie raddend += value; 3575aefb655Srie 3585aefb655Srie /* 3595aefb655Srie * addend field for R_AMD64_DTPMOD64 means nothing. The addend 3605aefb655Srie * is propogated in the corresponding R_AMD64_DTPOFF64 3615aefb655Srie * relocation. 3625aefb655Srie */ 3635aefb655Srie if (orsp->rel_rtype == R_AMD64_DTPMOD64) 3645aefb655Srie raddend = 0; 3655aefb655Srie 3665aefb655Srie relbits = (char *)relosp->os_outdata->d_buf; 3675aefb655Srie 3685aefb655Srie rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype); 3695aefb655Srie rea.r_offset = roffset; 3705aefb655Srie rea.r_addend = raddend; 3715aefb655Srie DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name, 3725aefb655Srie orsp->rel_sname)); 3735aefb655Srie 3745aefb655Srie /* 3755aefb655Srie * Assert we haven't walked off the end of our relocation table. 3765aefb655Srie */ 3775aefb655Srie assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size); 3785aefb655Srie 3795aefb655Srie (void) memcpy((relbits + relosp->os_szoutrels), 3805aefb655Srie (char *)&rea, sizeof (Rela)); 3815aefb655Srie relosp->os_szoutrels += (Xword)sizeof (Rela); 3825aefb655Srie 3835aefb655Srie /* 3845aefb655Srie * Determine if this relocation is against a non-writable, allocatable 3855aefb655Srie * section. If so we may need to provide a text relocation diagnostic. 3865aefb655Srie * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually 3875aefb655Srie * result in modifications to the .got. 3885aefb655Srie */ 3895aefb655Srie if (orsp->rel_rtype == R_AMD64_JUMP_SLOT) 3905aefb655Srie osp = ofl->ofl_osgot; 3915aefb655Srie 3925aefb655Srie ld_reloc_remain_entry(orsp, osp, ofl); 3935aefb655Srie return (1); 3945aefb655Srie } 3955aefb655Srie 3965aefb655Srie /* 3975aefb655Srie * amd64 Instructions for TLS processing 3985aefb655Srie */ 3995aefb655Srie static uchar_t tlsinstr_gd_ie[] = { 4005aefb655Srie /* 4015aefb655Srie * 0x00 movq %fs:0, %rax 4025aefb655Srie */ 4035aefb655Srie 0x64, 0x48, 0x8b, 0x04, 0x25, 4045aefb655Srie 0x00, 0x00, 0x00, 0x00, 4055aefb655Srie /* 4065aefb655Srie * 0x09 addq x@gottpoff(%rip), %rax 4075aefb655Srie */ 4085aefb655Srie 0x48, 0x03, 0x05, 0x00, 0x00, 4095aefb655Srie 0x00, 0x00 4105aefb655Srie }; 4115aefb655Srie 4125aefb655Srie static uchar_t tlsinstr_gd_le[] = { 4135aefb655Srie /* 4145aefb655Srie * 0x00 movq %fs:0, %rax 4155aefb655Srie */ 4165aefb655Srie 0x64, 0x48, 0x8b, 0x04, 0x25, 4175aefb655Srie 0x00, 0x00, 0x00, 0x00, 4185aefb655Srie /* 4195aefb655Srie * 0x09 leaq x@gottpoff(%rip), %rax 4205aefb655Srie */ 4215aefb655Srie 0x48, 0x8d, 0x80, 0x00, 0x00, 4225aefb655Srie 0x00, 0x00 4235aefb655Srie }; 4245aefb655Srie 4255aefb655Srie static uchar_t tlsinstr_ld_le[] = { 4265aefb655Srie /* 4275aefb655Srie * .byte 0x66 4285aefb655Srie */ 4295aefb655Srie 0x66, 4305aefb655Srie /* 4315aefb655Srie * .byte 0x66 4325aefb655Srie */ 4335aefb655Srie 0x66, 4345aefb655Srie /* 4355aefb655Srie * .byte 0x66 4365aefb655Srie */ 4375aefb655Srie 0x66, 4385aefb655Srie /* 4395aefb655Srie * movq %fs:0, %rax 4405aefb655Srie */ 4415aefb655Srie 0x64, 0x48, 0x8b, 0x04, 0x25, 4425aefb655Srie 0x00, 0x00, 0x00, 0x00 4435aefb655Srie }; 4445aefb655Srie 4455aefb655Srie 4465aefb655Srie static Fixupret 4475aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp) 4485aefb655Srie { 4495aefb655Srie Sym_desc *sdp = arsp->rel_sym; 4505aefb655Srie Word rtype = arsp->rel_rtype; 4515aefb655Srie uchar_t *offset; 4525aefb655Srie 4535aefb655Srie offset = (uchar_t *)((uintptr_t)arsp->rel_roffset + 4545aefb655Srie (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) + 4555aefb655Srie (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf); 4565aefb655Srie 4575aefb655Srie if (sdp->sd_ref == REF_DYN_NEED) { 4585aefb655Srie /* 4595aefb655Srie * IE reference model 4605aefb655Srie */ 4615aefb655Srie switch (rtype) { 4625aefb655Srie case R_AMD64_TLSGD: 4635aefb655Srie /* 4645aefb655Srie * GD -> IE 4655aefb655Srie * 4665aefb655Srie * Transition: 4675aefb655Srie * 0x00 .byte 0x66 4685aefb655Srie * 0x01 leaq x@tlsgd(%rip), %rdi 4695aefb655Srie * 0x08 .word 0x6666 4705aefb655Srie * 0x0a rex64 4715aefb655Srie * 0x0b call __tls_get_addr@plt 4725aefb655Srie * 0x10 4735aefb655Srie * To: 4745aefb655Srie * 0x00 movq %fs:0, %rax 4755aefb655Srie * 0x09 addq x@gottpoff(%rip), %rax 4765aefb655Srie * 0x10 4775aefb655Srie */ 4785aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 4795aefb655Srie rtype, R_AMD64_GOTTPOFF, arsp->rel_roffset, 4805aefb655Srie sdp->sd_name)); 4815aefb655Srie arsp->rel_rtype = R_AMD64_GOTTPOFF; 4825aefb655Srie arsp->rel_roffset += 8; 4835aefb655Srie arsp->rel_raddend = (Sxword)-4; 4845aefb655Srie 4855aefb655Srie /* 4865aefb655Srie * Addjust 'offset' to beginning of instruction 4875aefb655Srie * sequence. 4885aefb655Srie */ 4895aefb655Srie offset -= 4; 4905aefb655Srie (void) memcpy(offset, tlsinstr_gd_ie, 4915aefb655Srie sizeof (tlsinstr_gd_ie)); 4925aefb655Srie return (FIX_RELOC); 4935aefb655Srie 4945aefb655Srie case R_AMD64_PLT32: 4955aefb655Srie /* 4965aefb655Srie * Fixup done via the TLS_GD relocation 4975aefb655Srie */ 4985aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 4995aefb655Srie rtype, R_AMD64_NONE, arsp->rel_roffset, 5005aefb655Srie sdp->sd_name)); 5015aefb655Srie return (FIX_DONE); 5025aefb655Srie } 5035aefb655Srie } 5045aefb655Srie 5055aefb655Srie /* 5065aefb655Srie * LE reference model 5075aefb655Srie */ 5085aefb655Srie switch (rtype) { 5095aefb655Srie case R_AMD64_TLSGD: 5105aefb655Srie /* 5115aefb655Srie * GD -> LE 5125aefb655Srie * 5135aefb655Srie * Transition: 5145aefb655Srie * 0x00 .byte 0x66 5155aefb655Srie * 0x01 leaq x@tlsgd(%rip), %rdi 5165aefb655Srie * 0x08 .word 0x6666 5175aefb655Srie * 0x0a rex64 5185aefb655Srie * 0x0b call __tls_get_addr@plt 5195aefb655Srie * 0x10 5205aefb655Srie * To: 5215aefb655Srie * 0x00 movq %fs:0, %rax 5225aefb655Srie * 0x09 leaq x@tpoff(%rax), %rax 5235aefb655Srie * 0x10 5245aefb655Srie */ 5255aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 5265aefb655Srie rtype, R_AMD64_TPOFF32, arsp->rel_roffset, sdp->sd_name)); 5275aefb655Srie 5285aefb655Srie arsp->rel_rtype = R_AMD64_TPOFF32; 5295aefb655Srie arsp->rel_roffset += 8; 5305aefb655Srie arsp->rel_raddend = 0; 5315aefb655Srie 5325aefb655Srie /* 5335aefb655Srie * Addjust 'offset' to beginning of instruction 5345aefb655Srie * sequence. 5355aefb655Srie */ 5365aefb655Srie offset -= 4; 5375aefb655Srie (void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le)); 5385aefb655Srie return (FIX_RELOC); 5395aefb655Srie 5405aefb655Srie case R_AMD64_GOTTPOFF: 5415aefb655Srie /* 5425aefb655Srie * IE -> LE 5435aefb655Srie * 5445aefb655Srie * Transition: 5455aefb655Srie * 0x00 movq %fs:0, %rax 5465aefb655Srie * 0x09 addq x@gottopoff(%rip), %rax 5475aefb655Srie * 0x10 5485aefb655Srie * To: 5495aefb655Srie * 0x00 movq %fs:0, %rax 5505aefb655Srie * 0x09 leaq x@tpoff(%rax), %rax 5515aefb655Srie * 0x10 5525aefb655Srie */ 5535aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype, 5545aefb655Srie R_AMD64_TPOFF32, arsp->rel_roffset, sdp->sd_name)); 5555aefb655Srie 5565aefb655Srie arsp->rel_rtype = R_AMD64_TPOFF32; 5575aefb655Srie arsp->rel_raddend = 0; 5585aefb655Srie 5595aefb655Srie /* 5605aefb655Srie * Addjust 'offset' to beginning of instruction 5615aefb655Srie * sequence. 5625aefb655Srie */ 5635aefb655Srie offset -= 12; 5645aefb655Srie 5655aefb655Srie /* 5665aefb655Srie * Same code sequence used in the GD -> LE 5675aefb655Srie * transition. 5685aefb655Srie */ 5695aefb655Srie (void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le)); 5705aefb655Srie return (FIX_RELOC); 5715aefb655Srie 5725aefb655Srie case R_AMD64_TLSLD: 5735aefb655Srie /* 5745aefb655Srie * LD -> LE 5755aefb655Srie * 5765aefb655Srie * Transition 5775aefb655Srie * 0x00 leaq x1@tlsgd(%rip), %rdi 5785aefb655Srie * 0x07 call __tls_get_addr@plt 5795aefb655Srie * 0x0c 5805aefb655Srie * To: 5815aefb655Srie * 0x00 .byte 0x66 5825aefb655Srie * 0x01 .byte 0x66 5835aefb655Srie * 0x02 .byte 0x66 5845aefb655Srie * 0x03 movq %fs:0, %rax 5855aefb655Srie */ 5865aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype, 5875aefb655Srie R_AMD64_NONE, arsp->rel_roffset, sdp->sd_name)); 5885aefb655Srie offset -= 3; 5895aefb655Srie (void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le)); 5905aefb655Srie return (FIX_DONE); 5915aefb655Srie 5925aefb655Srie case R_AMD64_DTPOFF32: 5935aefb655Srie /* 5945aefb655Srie * LD->LE 5955aefb655Srie * 5965aefb655Srie * Transition: 5975aefb655Srie * 0x00 leaq x1@dtpoff(%rax), %rcx 5985aefb655Srie * To: 5995aefb655Srie * 0x00 leaq x1@tpoff(%rax), %rcx 6005aefb655Srie */ 6015aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, rtype, 6025aefb655Srie R_AMD64_TPOFF32, arsp->rel_roffset, sdp->sd_name)); 6035aefb655Srie arsp->rel_rtype = R_AMD64_TPOFF32; 6045aefb655Srie arsp->rel_raddend = 0; 6055aefb655Srie return (FIX_RELOC); 6065aefb655Srie } 6075aefb655Srie 6085aefb655Srie return (FIX_RELOC); 6095aefb655Srie } 6105aefb655Srie 6115aefb655Srie uintptr_t 6125aefb655Srie ld_do_activerelocs(Ofl_desc *ofl) 6135aefb655Srie { 6145aefb655Srie Rel_desc *arsp; 6155aefb655Srie Rel_cache *rcp; 6165aefb655Srie Listnode *lnp; 6175aefb655Srie uintptr_t return_code = 1; 6185aefb655Srie Word flags = ofl->ofl_flags; 6195aefb655Srie Word dtflags1 = ofl->ofl_dtflags_1; 6205aefb655Srie 6215aefb655Srie DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml)); 6225aefb655Srie /* 6235aefb655Srie * Process active relocations. 6245aefb655Srie */ 6255aefb655Srie for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) { 6265aefb655Srie /* LINTED */ 6275aefb655Srie for (arsp = (Rel_desc *)(rcp + 1); 6285aefb655Srie arsp < rcp->rc_free; arsp++) { 6295aefb655Srie uchar_t *addr; 6305aefb655Srie Xword value; 6315aefb655Srie Sym_desc *sdp; 6325aefb655Srie const char *ifl_name; 6335aefb655Srie Xword refaddr; 6345aefb655Srie int moved = 0; 6355aefb655Srie Gotref gref; 6365aefb655Srie 6375aefb655Srie /* 6385aefb655Srie * If the section this relocation is against has been 6395aefb655Srie * discarded (-zignore), then discard (skip) the 6405aefb655Srie * relocation itself. 6415aefb655Srie */ 6425aefb655Srie if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) && 6435aefb655Srie ((arsp->rel_flags & 6445aefb655Srie (FLG_REL_GOT | FLG_REL_BSS | 6455aefb655Srie FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) { 6465aefb655Srie DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, 6475aefb655Srie M_MACH, arsp)); 6485aefb655Srie continue; 6495aefb655Srie } 6505aefb655Srie 6515aefb655Srie /* 6525aefb655Srie * We deteremine what the 'got reference' 6535aefb655Srie * model (if required) is at this point. This 6545aefb655Srie * needs to be done before tls_fixup() since 6555aefb655Srie * it may 'transition' our instructions. 6565aefb655Srie * 6575aefb655Srie * The got table entries have already been assigned, 6585aefb655Srie * and we bind to those initial entries. 6595aefb655Srie */ 6605aefb655Srie if (arsp->rel_flags & FLG_REL_DTLS) 6615aefb655Srie gref = GOT_REF_TLSGD; 6625aefb655Srie else if (arsp->rel_flags & FLG_REL_MTLS) 6635aefb655Srie gref = GOT_REF_TLSLD; 6645aefb655Srie else if (arsp->rel_flags & FLG_REL_STLS) 6655aefb655Srie gref = GOT_REF_TLSIE; 6665aefb655Srie else 6675aefb655Srie gref = GOT_REF_GENERIC; 6685aefb655Srie 6695aefb655Srie /* 6705aefb655Srie * Perform any required TLS fixups. 6715aefb655Srie */ 6725aefb655Srie if (arsp->rel_flags & FLG_REL_TLSFIX) { 6735aefb655Srie Fixupret ret; 6745aefb655Srie 6755aefb655Srie if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR) 6765aefb655Srie return (S_ERROR); 6775aefb655Srie if (ret == FIX_DONE) 6785aefb655Srie continue; 6795aefb655Srie } 6805aefb655Srie 6815aefb655Srie /* 6825aefb655Srie * If this is a relocation against a move table, or 6835aefb655Srie * expanded move table, adjust the relocation entries. 6845aefb655Srie */ 6855aefb655Srie if (arsp->rel_move) 6865aefb655Srie ld_adj_movereloc(ofl, arsp); 6875aefb655Srie 6885aefb655Srie sdp = arsp->rel_sym; 6895aefb655Srie refaddr = arsp->rel_roffset + 6905aefb655Srie (Off)_elf_getxoff(arsp->rel_isdesc->is_indata); 6915aefb655Srie 6925aefb655Srie if ((arsp->rel_flags & FLG_REL_CLVAL) || 6935aefb655Srie (arsp->rel_flags & FLG_REL_GOTCL)) 6945aefb655Srie value = 0; 6955aefb655Srie else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == 6965aefb655Srie STT_SECTION) { 6975aefb655Srie Sym_desc *sym; 6985aefb655Srie 6995aefb655Srie /* 7005aefb655Srie * The value for a symbol pointing to a SECTION 7015aefb655Srie * is based off of that sections position. 7025aefb655Srie * 7035aefb655Srie * The second argument of the ld_am_I_partial() 7045aefb655Srie * is the value stored at the target address 7055aefb655Srie * relocation is going to be applied. 7065aefb655Srie */ 7075aefb655Srie if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 7085aefb655Srie /* LINTED */ 7095aefb655Srie (sym = ld_am_I_partial(arsp, *(Xword *) 7105aefb655Srie ((uchar_t *) 7115aefb655Srie arsp->rel_isdesc->is_indata->d_buf + 7125aefb655Srie arsp->rel_roffset)))) { 7135aefb655Srie /* 7145aefb655Srie * If the symbol is moved, 7155aefb655Srie * adjust the value 7165aefb655Srie */ 7175aefb655Srie value = sym->sd_sym->st_value; 7185aefb655Srie moved = 1; 7195aefb655Srie } else { 7205aefb655Srie value = _elf_getxoff( 7215aefb655Srie sdp->sd_isc->is_indata); 7225aefb655Srie if (sdp->sd_isc->is_shdr->sh_flags & 7235aefb655Srie SHF_ALLOC) 7245aefb655Srie value += sdp->sd_isc->is_osdesc-> 7255aefb655Srie os_shdr->sh_addr; 7265aefb655Srie } 7275aefb655Srie if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS) 7285aefb655Srie value -= ofl->ofl_tlsphdr->p_vaddr; 7295aefb655Srie } else { 7305aefb655Srie /* 7315aefb655Srie * else the value is the symbols value 7325aefb655Srie */ 7335aefb655Srie value = sdp->sd_sym->st_value; 7345aefb655Srie } 7355aefb655Srie 7365aefb655Srie /* 7375aefb655Srie * Relocation against the GLOBAL_OFFSET_TABLE. 7385aefb655Srie */ 7395aefb655Srie if (arsp->rel_flags & FLG_REL_GOT) 7405aefb655Srie arsp->rel_osdesc = ofl->ofl_osgot; 7415aefb655Srie 7425aefb655Srie /* 7435aefb655Srie * If loadable and not producing a relocatable object 7445aefb655Srie * add the sections virtual address to the reference 7455aefb655Srie * address. 7465aefb655Srie */ 7475aefb655Srie if ((arsp->rel_flags & FLG_REL_LOAD) && 7485aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) 7495aefb655Srie refaddr += arsp->rel_isdesc->is_osdesc-> 7505aefb655Srie os_shdr->sh_addr; 7515aefb655Srie 7525aefb655Srie /* 7535aefb655Srie * If this entry has a PLT assigned to it, it's 7545aefb655Srie * value is actually the address of the PLT (and 7555aefb655Srie * not the address of the function). 7565aefb655Srie */ 7575aefb655Srie if (IS_PLT(arsp->rel_rtype)) { 7585aefb655Srie if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx) 7595aefb655Srie value = ld_calc_plt_addr(sdp, ofl); 7605aefb655Srie } 7615aefb655Srie 7625aefb655Srie /* 7635aefb655Srie * Add relocations addend to value. Add extra 7645aefb655Srie * relocation addend if needed. 7655aefb655Srie * 7665aefb655Srie * Note: for GOT relative relocations on amd64 7675aefb655Srie * we discard the addend. It was relevant 7685aefb655Srie * to the reference - not to the data item 7695aefb655Srie * being referenced (ie: that -4 thing). 7705aefb655Srie */ 7715aefb655Srie if ((arsp->rel_flags & FLG_REL_GOT) == 0) 7725aefb655Srie value += arsp->rel_raddend; 7735aefb655Srie 7745aefb655Srie /* 7755aefb655Srie * Determine whether the value needs further adjustment. 7765aefb655Srie * Filter through the attributes of the relocation to 7775aefb655Srie * determine what adjustment is required. Note, many 7785aefb655Srie * of the following cases are only applicable when a 7795aefb655Srie * .got is present. As a .got is not generated when a 7805aefb655Srie * relocatable object is being built, any adjustments 7815aefb655Srie * that require a .got need to be skipped. 7825aefb655Srie */ 7835aefb655Srie if ((arsp->rel_flags & FLG_REL_GOT) && 7845aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 7855aefb655Srie Xword R1addr; 7865aefb655Srie uintptr_t R2addr; 7875aefb655Srie Word gotndx; 7885aefb655Srie Gotndx *gnp; 7895aefb655Srie 7905aefb655Srie /* 7915aefb655Srie * Perform relocation against GOT table. Since 7925aefb655Srie * this doesn't fit exactly into a relocation 7935aefb655Srie * we place the appropriate byte in the GOT 7945aefb655Srie * directly 7955aefb655Srie * 7965aefb655Srie * Calculate offset into GOT at which to apply 7975aefb655Srie * the relocation. 7985aefb655Srie */ 7995aefb655Srie gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, 8005aefb655Srie ofl, arsp); 8015aefb655Srie assert(gnp); 8025aefb655Srie 8035aefb655Srie if (arsp->rel_rtype == R_AMD64_DTPOFF64) 8045aefb655Srie gotndx = gnp->gn_gotndx + 1; 8055aefb655Srie else 8065aefb655Srie gotndx = gnp->gn_gotndx; 8075aefb655Srie 8085aefb655Srie R1addr = (Xword)(gotndx * M_GOT_ENTSIZE); 8095aefb655Srie 8105aefb655Srie /* 8115aefb655Srie * Add the GOTs data's offset. 8125aefb655Srie */ 8135aefb655Srie R2addr = R1addr + (uintptr_t) 8145aefb655Srie arsp->rel_osdesc->os_outdata->d_buf; 8155aefb655Srie 8165aefb655Srie DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, 8175aefb655Srie ELF_DBG_LD, M_MACH, SHT_RELA, 8185aefb655Srie arsp->rel_rtype, R1addr, value, 8195aefb655Srie arsp->rel_sname, arsp->rel_osdesc)); 8205aefb655Srie 8215aefb655Srie /* 8225aefb655Srie * And do it. 8235aefb655Srie */ 8245aefb655Srie *(Xword *)R2addr = value; 8255aefb655Srie continue; 8265aefb655Srie 8275aefb655Srie } else if (IS_GOT_BASED(arsp->rel_rtype) && 8285aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8295aefb655Srie value -= ofl->ofl_osgot->os_shdr->sh_addr; 8305aefb655Srie 8315aefb655Srie } else if (IS_GOTPCREL(arsp->rel_rtype) && 8325aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8335aefb655Srie Gotndx *gnp; 8345aefb655Srie 8355aefb655Srie /* 8365aefb655Srie * Calculation: 8375aefb655Srie * G + GOT + A - P 8385aefb655Srie */ 8395aefb655Srie gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 8405aefb655Srie gref, ofl, arsp); 8415aefb655Srie assert(gnp); 8425aefb655Srie value = (Xword)(ofl->ofl_osgot->os_shdr-> 8435aefb655Srie sh_addr) + ((Xword)gnp->gn_gotndx * 8445aefb655Srie M_GOT_ENTSIZE) + arsp->rel_raddend - 8455aefb655Srie refaddr; 8465aefb655Srie 8475aefb655Srie } else if (IS_GOT_PC(arsp->rel_rtype) && 8485aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8495aefb655Srie value = (Xword)(ofl->ofl_osgot->os_shdr-> 8505aefb655Srie sh_addr) - refaddr + arsp->rel_raddend; 8515aefb655Srie 8525aefb655Srie } else if ((IS_PC_RELATIVE(arsp->rel_rtype)) && 8535aefb655Srie (((flags & FLG_OF_RELOBJ) == 0) || 8545aefb655Srie (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) { 8555aefb655Srie value -= refaddr; 8565aefb655Srie 8575aefb655Srie } else if (IS_TLS_INS(arsp->rel_rtype) && 8585aefb655Srie IS_GOT_RELATIVE(arsp->rel_rtype) && 8595aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8605aefb655Srie Gotndx *gnp; 8615aefb655Srie 8625aefb655Srie gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, 8635aefb655Srie ofl, arsp); 8645aefb655Srie assert(gnp); 8655aefb655Srie value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 8665aefb655Srie 8675aefb655Srie } else if (IS_GOT_RELATIVE(arsp->rel_rtype) && 8685aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8695aefb655Srie Gotndx *gnp; 8705aefb655Srie 8715aefb655Srie gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 8725aefb655Srie gref, ofl, arsp); 8735aefb655Srie assert(gnp); 8745aefb655Srie value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 8755aefb655Srie 8765aefb655Srie } else if ((arsp->rel_flags & FLG_REL_STLS) && 8775aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) { 8785aefb655Srie Xword tlsstatsize; 8795aefb655Srie 8805aefb655Srie /* 8815aefb655Srie * This is the LE TLS reference model. Static 8825aefb655Srie * offset is hard-coded. 8835aefb655Srie */ 8845aefb655Srie tlsstatsize = 8855aefb655Srie S_ROUND(ofl->ofl_tlsphdr->p_memsz, 8865aefb655Srie M_TLSSTATALIGN); 8875aefb655Srie value = tlsstatsize - value; 8885aefb655Srie 8895aefb655Srie /* 8905aefb655Srie * Since this code is fixed up, it assumes a 8915aefb655Srie * negative offset that can be added to the 8925aefb655Srie * thread pointer. 8935aefb655Srie */ 8945aefb655Srie if (arsp->rel_rtype == R_AMD64_TPOFF32) 8955aefb655Srie value = -value; 8965aefb655Srie } 8975aefb655Srie 8985aefb655Srie if (arsp->rel_isdesc->is_file) 8995aefb655Srie ifl_name = arsp->rel_isdesc->is_file->ifl_name; 9005aefb655Srie else 9015aefb655Srie ifl_name = MSG_INTL(MSG_STR_NULL); 9025aefb655Srie 9035aefb655Srie /* 9045aefb655Srie * Make sure we have data to relocate. Compiler and 9055aefb655Srie * assembler developers have been known to generate 9065aefb655Srie * relocations against invalid sections (normally .bss), 9075aefb655Srie * so for their benefit give them sufficient information 9085aefb655Srie * to help analyze the problem. End users should never 9095aefb655Srie * see this. 9105aefb655Srie */ 9115aefb655Srie if (arsp->rel_isdesc->is_indata->d_buf == 0) { 9125aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 9135aefb655Srie MSG_INTL(MSG_REL_EMPTYSEC), 9145aefb655Srie conv_reloc_amd64_type(arsp->rel_rtype), 9155aefb655Srie ifl_name, demangle(arsp->rel_sname), 9165aefb655Srie arsp->rel_isdesc->is_name); 9175aefb655Srie return (S_ERROR); 9185aefb655Srie } 9195aefb655Srie 9205aefb655Srie /* 9215aefb655Srie * Get the address of the data item we need to modify. 9225aefb655Srie */ 9235aefb655Srie addr = (uchar_t *)((uintptr_t)arsp->rel_roffset + 9245aefb655Srie (uintptr_t)_elf_getxoff(arsp->rel_isdesc-> 9255aefb655Srie is_indata)); 9265aefb655Srie 9275aefb655Srie DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD, 9285aefb655Srie M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr), 9295aefb655Srie value, arsp->rel_sname, arsp->rel_osdesc)); 9305aefb655Srie addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf; 9315aefb655Srie 9325aefb655Srie if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) > 9335aefb655Srie ofl->ofl_size) || (arsp->rel_roffset > 9345aefb655Srie arsp->rel_osdesc->os_shdr->sh_size)) { 9355aefb655Srie int class; 9365aefb655Srie 9375aefb655Srie if (((uintptr_t)addr - 9385aefb655Srie (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size) 9395aefb655Srie class = ERR_FATAL; 9405aefb655Srie else 9415aefb655Srie class = ERR_WARNING; 9425aefb655Srie 9435aefb655Srie eprintf(ofl->ofl_lml, class, 9445aefb655Srie MSG_INTL(MSG_REL_INVALOFFSET), 9455aefb655Srie conv_reloc_amd64_type(arsp->rel_rtype), 9465aefb655Srie ifl_name, arsp->rel_isdesc->is_name, 9475aefb655Srie demangle(arsp->rel_sname), 9485aefb655Srie EC_ADDR((uintptr_t)addr - 9495aefb655Srie (uintptr_t)ofl->ofl_nehdr)); 9505aefb655Srie 9515aefb655Srie if (class == ERR_FATAL) { 9525aefb655Srie return_code = S_ERROR; 9535aefb655Srie continue; 9545aefb655Srie } 9555aefb655Srie } 9565aefb655Srie 9575aefb655Srie /* 9585aefb655Srie * The relocation is additive. Ignore the previous 9595aefb655Srie * symbol value if this local partial symbol is 9605aefb655Srie * expanded. 9615aefb655Srie */ 9625aefb655Srie if (moved) 9635aefb655Srie value -= *addr; 9645aefb655Srie 9655aefb655Srie /* 9665aefb655Srie * If '-z noreloc' is specified - skip the do_reloc 9675aefb655Srie * stage. 9685aefb655Srie */ 9695aefb655Srie if ((flags & FLG_OF_RELOBJ) || 9705aefb655Srie !(dtflags1 & DF_1_NORELOC)) { 9715aefb655Srie if (do_reloc((uchar_t)arsp->rel_rtype, 9725aefb655Srie addr, &value, arsp->rel_sname, ifl_name, 9735aefb655Srie ofl->ofl_lml) == 0) 9745aefb655Srie return_code = S_ERROR; 9755aefb655Srie } 9765aefb655Srie } 9775aefb655Srie } 9785aefb655Srie return (return_code); 9795aefb655Srie } 9805aefb655Srie 9815aefb655Srie uintptr_t 9825aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl) 9835aefb655Srie { 9845aefb655Srie Rel_desc *orsp; 9855aefb655Srie Rel_cache *rcp; 9865aefb655Srie Sym_desc *sdp = rsp->rel_sym; 9875aefb655Srie 9885aefb655Srie /* 9895aefb655Srie * Static executables *do not* want any relocations against them. 9905aefb655Srie * Since our engine still creates relocations against a WEAK UNDEFINED 9915aefb655Srie * symbol in a static executable, it's best to disable them here 9925aefb655Srie * instead of through out the relocation code. 9935aefb655Srie */ 9945aefb655Srie if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) == 9955aefb655Srie (FLG_OF_STATIC | FLG_OF_EXEC)) 9965aefb655Srie return (1); 9975aefb655Srie 9985aefb655Srie /* 9995aefb655Srie * If no relocation cache structures are available allocate 10005aefb655Srie * a new one and link it into the cache list. 10015aefb655Srie */ 10025aefb655Srie if ((ofl->ofl_outrels.tail == 0) || 10035aefb655Srie ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) || 10045aefb655Srie ((orsp = rcp->rc_free) == rcp->rc_end)) { 10055aefb655Srie static size_t nextsize = 0; 10065aefb655Srie size_t size; 10075aefb655Srie 10085aefb655Srie /* 10095aefb655Srie * Output relocation numbers can vary considerably between 10105aefb655Srie * building executables or shared objects (pic vs. non-pic), 10115aefb655Srie * etc. But, they typically aren't very large, so for these 10125aefb655Srie * objects use a standard bucket size. For building relocatable 10135aefb655Srie * objects, typically there will be an output relocation for 10145aefb655Srie * every input relocation. 10155aefb655Srie */ 10165aefb655Srie if (nextsize == 0) { 10175aefb655Srie if (ofl->ofl_flags & FLG_OF_RELOBJ) { 10185aefb655Srie if ((size = ofl->ofl_relocincnt) == 0) 10195aefb655Srie size = REL_LOIDESCNO; 10205aefb655Srie if (size > REL_HOIDESCNO) 10215aefb655Srie nextsize = REL_HOIDESCNO; 10225aefb655Srie else 10235aefb655Srie nextsize = REL_LOIDESCNO; 10245aefb655Srie } else 10255aefb655Srie nextsize = size = REL_HOIDESCNO; 10265aefb655Srie } else 10275aefb655Srie size = nextsize; 10285aefb655Srie 10295aefb655Srie size = size * sizeof (Rel_desc); 10305aefb655Srie 10315aefb655Srie if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) || 10325aefb655Srie (list_appendc(&ofl->ofl_outrels, rcp) == 0)) 10335aefb655Srie return (S_ERROR); 10345aefb655Srie 10355aefb655Srie /* LINTED */ 10365aefb655Srie rcp->rc_free = orsp = (Rel_desc *)(rcp + 1); 10375aefb655Srie /* LINTED */ 10385aefb655Srie rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size); 10395aefb655Srie } 10405aefb655Srie 10415aefb655Srie /* 10425aefb655Srie * If we are adding a output relocation against a section 10435aefb655Srie * symbol (non-RELATIVE) then mark that section. These sections 10445aefb655Srie * will be added to the .dynsym symbol table. 10455aefb655Srie */ 10465aefb655Srie if (sdp && (rsp->rel_rtype != M_R_RELATIVE) && 10475aefb655Srie ((flags & FLG_REL_SCNNDX) || 10485aefb655Srie (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) { 10495aefb655Srie 10505aefb655Srie /* 10515aefb655Srie * If this is a COMMON symbol - no output section 10525aefb655Srie * exists yet - (it's created as part of sym_validate()). 10535aefb655Srie * So - we mark here that when it's created it should 10545aefb655Srie * be tagged with the FLG_OS_OUTREL flag. 10555aefb655Srie */ 10565aefb655Srie if ((sdp->sd_flags & FLG_SY_SPECSEC) && 1057*0bc07c75Srie (sdp->sd_sym->st_shndx == SHN_COMMON)) { 10585aefb655Srie if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS) 10595aefb655Srie ofl->ofl_flags1 |= FLG_OF1_BSSOREL; 10605aefb655Srie else 10615aefb655Srie ofl->ofl_flags1 |= FLG_OF1_TLSOREL; 10625aefb655Srie } else { 10635aefb655Srie Os_desc *osp = sdp->sd_isc->is_osdesc; 10645aefb655Srie 10655aefb655Srie if ((osp->os_flags & FLG_OS_OUTREL) == 0) { 10665aefb655Srie ofl->ofl_dynshdrcnt++; 10675aefb655Srie osp->os_flags |= FLG_OS_OUTREL; 10685aefb655Srie } 10695aefb655Srie } 10705aefb655Srie } 10715aefb655Srie 10725aefb655Srie *orsp = *rsp; 10735aefb655Srie orsp->rel_flags |= flags; 10745aefb655Srie 10755aefb655Srie rcp->rc_free++; 10765aefb655Srie ofl->ofl_outrelscnt++; 10775aefb655Srie 10785aefb655Srie if (flags & FLG_REL_GOT) 10795aefb655Srie ofl->ofl_relocgotsz += (Xword)sizeof (Rela); 10805aefb655Srie else if (flags & FLG_REL_PLT) 10815aefb655Srie ofl->ofl_relocpltsz += (Xword)sizeof (Rela); 10825aefb655Srie else if (flags & FLG_REL_BSS) 10835aefb655Srie ofl->ofl_relocbsssz += (Xword)sizeof (Rela); 10845aefb655Srie else if (flags & FLG_REL_NOINFO) 10855aefb655Srie ofl->ofl_relocrelsz += (Xword)sizeof (Rela); 10865aefb655Srie else 10875aefb655Srie orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela); 10885aefb655Srie 10895aefb655Srie if (orsp->rel_rtype == M_R_RELATIVE) 10905aefb655Srie ofl->ofl_relocrelcnt++; 10915aefb655Srie 10925aefb655Srie /* 10935aefb655Srie * We don't perform sorting on PLT relocations because 10945aefb655Srie * they have already been assigned a PLT index and if we 10955aefb655Srie * were to sort them we would have to re-assign the plt indexes. 10965aefb655Srie */ 10975aefb655Srie if (!(flags & FLG_REL_PLT)) 10985aefb655Srie ofl->ofl_reloccnt++; 10995aefb655Srie 11005aefb655Srie /* 11015aefb655Srie * Insure a GLOBAL_OFFSET_TABLE is generated if required. 11025aefb655Srie */ 11035aefb655Srie if (IS_GOT_REQUIRED(orsp->rel_rtype)) 11045aefb655Srie ofl->ofl_flags |= FLG_OF_BLDGOT; 11055aefb655Srie 11065aefb655Srie /* 11075aefb655Srie * Identify and possibly warn of a displacement relocation. 11085aefb655Srie */ 11095aefb655Srie if (orsp->rel_flags & FLG_REL_DISP) { 11105aefb655Srie ofl->ofl_dtflags_1 |= DF_1_DISPRELPND; 11115aefb655Srie 11125aefb655Srie if (ofl->ofl_flags & FLG_OF_VERBOSE) 11135aefb655Srie ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl); 11145aefb655Srie } 11155aefb655Srie DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA, 11165aefb655Srie M_MACH, orsp)); 11175aefb655Srie return (1); 11185aefb655Srie } 11195aefb655Srie 11205aefb655Srie /* 11215aefb655Srie * Stub routine since register symbols are not supported on amd64. 11225aefb655Srie */ 11235aefb655Srie /* ARGSUSED */ 11245aefb655Srie uintptr_t 11255aefb655Srie ld_reloc_register(Rel_desc * rsp, Is_desc * isp, Ofl_desc * ofl) 11265aefb655Srie { 11275aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_NOREG)); 11285aefb655Srie return (S_ERROR); 11295aefb655Srie } 11305aefb655Srie 11315aefb655Srie /* 11325aefb655Srie * process relocation for a LOCAL symbol 11335aefb655Srie */ 11345aefb655Srie uintptr_t 11355aefb655Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl) 11365aefb655Srie { 11375aefb655Srie Word flags = ofl->ofl_flags; 11385aefb655Srie Sym_desc *sdp = rsp->rel_sym; 1139*0bc07c75Srie Word shndx = sdp->sd_sym->st_shndx; 11405aefb655Srie Word ortype = rsp->rel_rtype; 11415aefb655Srie 11425aefb655Srie /* 11435aefb655Srie * if ((shared object) and (not pc relative relocation) and 11445aefb655Srie * (not against ABS symbol)) 11455aefb655Srie * then 11465aefb655Srie * build R_AMD64_RELATIVE 11475aefb655Srie * fi 11485aefb655Srie */ 11495aefb655Srie if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) && 11505aefb655Srie !(IS_PC_RELATIVE(rsp->rel_rtype)) && 11515aefb655Srie !(IS_GOT_BASED(rsp->rel_rtype)) && 11525aefb655Srie !(rsp->rel_isdesc != NULL && 11535aefb655Srie (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) && 11545aefb655Srie (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) || 11555aefb655Srie (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) { 11565aefb655Srie 11575aefb655Srie /* 11585aefb655Srie * R_AMD64_RELATIVE updates a 64bit address, if this 11595aefb655Srie * relocation isn't a 64bit binding then we can not 11605aefb655Srie * simplify it to a RELATIVE relocation. 11615aefb655Srie */ 11625aefb655Srie if (reloc_table[ortype].re_fsize != sizeof (Addr)) { 11635aefb655Srie return (ld_add_outrel(NULL, rsp, ofl)); 11645aefb655Srie } 11655aefb655Srie 11665aefb655Srie rsp->rel_rtype = R_AMD64_RELATIVE; 11675aefb655Srie if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR) 11685aefb655Srie return (S_ERROR); 11695aefb655Srie rsp->rel_rtype = ortype; 11705aefb655Srie return (1); 11715aefb655Srie } 11725aefb655Srie 11735aefb655Srie /* 11745aefb655Srie * If the relocation is against a 'non-allocatable' section 11755aefb655Srie * and we can not resolve it now - then give a warning 11765aefb655Srie * message. 11775aefb655Srie * 11785aefb655Srie * We can not resolve the symbol if either: 11795aefb655Srie * a) it's undefined 11805aefb655Srie * b) it's defined in a shared library and a 11815aefb655Srie * COPY relocation hasn't moved it to the executable 11825aefb655Srie * 11835aefb655Srie * Note: because we process all of the relocations against the 11845aefb655Srie * text segment before any others - we know whether 11855aefb655Srie * or not a copy relocation will be generated before 11865aefb655Srie * we get here (see reloc_init()->reloc_segments()). 11875aefb655Srie */ 11885aefb655Srie if (!(rsp->rel_flags & FLG_REL_LOAD) && 11895aefb655Srie ((shndx == SHN_UNDEF) || 11905aefb655Srie ((sdp->sd_ref == REF_DYN_NEED) && 11915aefb655Srie ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) { 11925aefb655Srie /* 11935aefb655Srie * If the relocation is against a SHT_SUNW_ANNOTATE 11945aefb655Srie * section - then silently ignore that the relocation 11955aefb655Srie * can not be resolved. 11965aefb655Srie */ 11975aefb655Srie if (rsp->rel_osdesc && 11985aefb655Srie (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE)) 11995aefb655Srie return (0); 12005aefb655Srie (void) eprintf(ofl->ofl_lml, ERR_WARNING, 12015aefb655Srie MSG_INTL(MSG_REL_EXTERNSYM), 12025aefb655Srie conv_reloc_amd64_type(rsp->rel_rtype), 12035aefb655Srie rsp->rel_isdesc->is_file->ifl_name, 12045aefb655Srie demangle(rsp->rel_sname), rsp->rel_osdesc->os_name); 12055aefb655Srie return (1); 12065aefb655Srie } 12075aefb655Srie 12085aefb655Srie /* 12095aefb655Srie * Perform relocation. 12105aefb655Srie */ 12115aefb655Srie return (ld_add_actrel(NULL, rsp, ofl)); 12125aefb655Srie } 12135aefb655Srie 12145aefb655Srie 12155aefb655Srie uintptr_t 12165aefb655Srie /* ARGSUSED */ 12175aefb655Srie ld_reloc_GOTOP(Boolean local, Rel_desc * rsp, Ofl_desc * ofl) 12185aefb655Srie { 12195aefb655Srie /* 12205aefb655Srie * Stub routine for common code compatibility, we shouldn't 12215aefb655Srie * actually get here on amd64. 12225aefb655Srie */ 12235aefb655Srie return (S_ERROR); 12245aefb655Srie } 12255aefb655Srie 12265aefb655Srie uintptr_t 12275aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl) 12285aefb655Srie { 12295aefb655Srie Word rtype = rsp->rel_rtype; 12305aefb655Srie Sym_desc *sdp = rsp->rel_sym; 12315aefb655Srie Word flags = ofl->ofl_flags; 12325aefb655Srie Word rflags; 12335aefb655Srie Gotndx *gnp; 12345aefb655Srie 12355aefb655Srie /* 12365aefb655Srie * all TLS relocations are illegal in a static executable. 12375aefb655Srie */ 12385aefb655Srie if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) == 12395aefb655Srie (FLG_OF_STATIC | FLG_OF_EXEC)) { 12405aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSSTAT), 12415aefb655Srie conv_reloc_amd64_type(rsp->rel_rtype), 12425aefb655Srie rsp->rel_isdesc->is_file->ifl_name, 12435aefb655Srie demangle(rsp->rel_sname)); 12445aefb655Srie return (S_ERROR); 12455aefb655Srie } 12465aefb655Srie 12475aefb655Srie /* 12485aefb655Srie * Any TLS relocation must be against a STT_TLS symbol, all others 12495aefb655Srie * are illegal. 12505aefb655Srie */ 12515aefb655Srie if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS) { 12525aefb655Srie Ifl_desc *ifl = rsp->rel_isdesc->is_file; 12535aefb655Srie 12545aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSBADSYM), 12555aefb655Srie conv_reloc_amd64_type(rsp->rel_rtype), 12565aefb655Srie ifl->ifl_name, demangle(rsp->rel_sname), 12575aefb655Srie conv_sym_info_type(ifl->ifl_ehdr->e_machine, 12585aefb655Srie ELF_ST_TYPE(sdp->sd_sym->st_info))); 12595aefb655Srie return (S_ERROR); 12605aefb655Srie } 12615aefb655Srie 12625aefb655Srie /* 12635aefb655Srie * We're a executable - use either the IE or LE 12645aefb655Srie * access model. 12655aefb655Srie */ 12665aefb655Srie if (flags & FLG_OF_EXEC) { 12675aefb655Srie /* 12685aefb655Srie * If we are using either IE or LE reference 12695aefb655Srie * model set the DF_STATIC_TLS flag. 12705aefb655Srie */ 12715aefb655Srie ofl->ofl_dtflags |= DF_STATIC_TLS; 12725aefb655Srie 12735aefb655Srie if (!local) { 12745aefb655Srie Gotref gref; 12755aefb655Srie /* 12765aefb655Srie * IE access model 12775aefb655Srie */ 12785aefb655Srie /* 12795aefb655Srie * It's not possible for LD or LE reference 12805aefb655Srie * models to reference a symbol external to 12815aefb655Srie * the current object. 12825aefb655Srie */ 12835aefb655Srie if (IS_TLS_LD(rtype) || IS_TLS_LE(rtype)) { 12845aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 12855aefb655Srie MSG_INTL(MSG_REL_TLSBND), 12865aefb655Srie conv_reloc_amd64_type(rsp->rel_rtype), 12875aefb655Srie rsp->rel_isdesc->is_file->ifl_name, 12885aefb655Srie demangle(rsp->rel_sname), 12895aefb655Srie sdp->sd_file->ifl_name); 12905aefb655Srie return (S_ERROR); 12915aefb655Srie } 12925aefb655Srie 12935aefb655Srie gref = GOT_REF_TLSIE; 12945aefb655Srie 12955aefb655Srie /* 12965aefb655Srie * Assign a GOT entry for static TLS references 12975aefb655Srie */ 12985aefb655Srie if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 12995aefb655Srie gref, ofl, rsp)) == 0) { 13005aefb655Srie if (ld_assign_gotndx(&(sdp->sd_GOTndxs), 13015aefb655Srie gnp, gref, ofl, rsp, sdp) == S_ERROR) 13025aefb655Srie return (S_ERROR); 13035aefb655Srie rsp->rel_rtype = R_AMD64_TPOFF64; 13045aefb655Srie if (ld_add_outrel((FLG_REL_GOT | FLG_REL_STLS), 13055aefb655Srie rsp, ofl) == S_ERROR) 13065aefb655Srie return (S_ERROR); 13075aefb655Srie rsp->rel_rtype = rtype; 13085aefb655Srie } 13095aefb655Srie if (IS_TLS_IE(rtype)) 13105aefb655Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 13115aefb655Srie 13125aefb655Srie /* 13135aefb655Srie * If (GD or LD) reference models - fixups 13145aefb655Srie * are required. 13155aefb655Srie */ 13165aefb655Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 13175aefb655Srie rsp, ofl)); 13185aefb655Srie } 13195aefb655Srie /* 13205aefb655Srie * LE access model 13215aefb655Srie */ 13225aefb655Srie if (IS_TLS_LE(rtype)) 13235aefb655Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 13245aefb655Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 13255aefb655Srie rsp, ofl)); 13265aefb655Srie } 13275aefb655Srie 13285aefb655Srie /* 13295aefb655Srie * Building a shared object 13305aefb655Srie */ 13315aefb655Srie 13325aefb655Srie /* 13335aefb655Srie * Building a shared object - only GD & LD access models 13345aefb655Srie * will work here. 13355aefb655Srie */ 13365aefb655Srie if (IS_TLS_IE(rtype) || IS_TLS_LE(rtype)) { 13375aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSIE), 13385aefb655Srie conv_reloc_amd64_type(rsp->rel_rtype), 13395aefb655Srie rsp->rel_isdesc->is_file->ifl_name, 13405aefb655Srie demangle(rsp->rel_sname)); 13415aefb655Srie return (S_ERROR); 13425aefb655Srie } 13435aefb655Srie 13445aefb655Srie /* 13455aefb655Srie * LD access mode can only bind to local symbols. 13465aefb655Srie */ 13475aefb655Srie if (!local && IS_TLS_LD(rtype)) { 13485aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSBND), 13495aefb655Srie conv_reloc_amd64_type(rsp->rel_rtype), 13505aefb655Srie rsp->rel_isdesc->is_file->ifl_name, 13515aefb655Srie demangle(rsp->rel_sname), 13525aefb655Srie sdp->sd_file->ifl_name); 13535aefb655Srie return (S_ERROR); 13545aefb655Srie } 13555aefb655Srie 13565aefb655Srie 13575aefb655Srie if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 13585aefb655Srie GOT_REF_TLSLD, ofl, rsp)) == 0)) { 13595aefb655Srie if (ld_assign_gotndx(&(sdp->sd_GOTndxs), gnp, GOT_REF_TLSLD, 13605aefb655Srie ofl, rsp, sdp) == S_ERROR) 13615aefb655Srie return (S_ERROR); 13625aefb655Srie rflags = FLG_REL_GOT | FLG_REL_MTLS; 13635aefb655Srie if (local) 13645aefb655Srie rflags |= FLG_REL_SCNNDX; 13655aefb655Srie rsp->rel_rtype = R_AMD64_DTPMOD64; 13665aefb655Srie if (ld_add_outrel(rflags, rsp, ofl) == S_ERROR) 13675aefb655Srie return (S_ERROR); 13685aefb655Srie rsp->rel_rtype = rtype; 13695aefb655Srie } else if (IS_TLS_GD(rtype) && 13705aefb655Srie ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD, ofl, 13715aefb655Srie rsp)) == 0)) { 13725aefb655Srie if (ld_assign_gotndx(&(sdp->sd_GOTndxs), gnp, GOT_REF_TLSGD, 13735aefb655Srie ofl, rsp, sdp) == S_ERROR) 13745aefb655Srie return (S_ERROR); 13755aefb655Srie rflags = FLG_REL_GOT | FLG_REL_DTLS; 13765aefb655Srie if (local) 13775aefb655Srie rflags |= FLG_REL_SCNNDX; 13785aefb655Srie rsp->rel_rtype = R_AMD64_DTPMOD64; 13795aefb655Srie if (ld_add_outrel(rflags, rsp, ofl) == S_ERROR) 13805aefb655Srie return (S_ERROR); 13815aefb655Srie if (local == TRUE) { 13825aefb655Srie rsp->rel_rtype = R_AMD64_DTPOFF64; 13835aefb655Srie if (ld_add_actrel((FLG_REL_GOT | FLG_REL_DTLS), rsp, 13845aefb655Srie ofl) == S_ERROR) 13855aefb655Srie return (S_ERROR); 13865aefb655Srie } else { 13875aefb655Srie rsp->rel_rtype = R_AMD64_DTPOFF64; 13885aefb655Srie if (ld_add_outrel((FLG_REL_GOT | FLG_REL_DTLS), rsp, 13895aefb655Srie ofl) == S_ERROR) 13905aefb655Srie return (S_ERROR); 13915aefb655Srie } 13925aefb655Srie rsp->rel_rtype = rtype; 13935aefb655Srie } 13945aefb655Srie 13955aefb655Srie if (IS_TLS_LD(rtype)) 13965aefb655Srie return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl)); 13975aefb655Srie 13985aefb655Srie return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl)); 13995aefb655Srie } 14005aefb655Srie 14015aefb655Srie /* ARGSUSED3 */ 14025aefb655Srie Gotndx * 14035aefb655Srie ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc) 14045aefb655Srie { 14055aefb655Srie Listnode * lnp; 14065aefb655Srie Gotndx * gnp; 14075aefb655Srie 14085aefb655Srie assert(rdesc != 0); 14095aefb655Srie 14105aefb655Srie if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx) 14115aefb655Srie return (ofl->ofl_tlsldgotndx); 14125aefb655Srie 14135aefb655Srie for (LIST_TRAVERSE(lst, lnp, gnp)) { 14145aefb655Srie if ((rdesc->rel_raddend == gnp->gn_addend) && 14155aefb655Srie (gnp->gn_gotref == gref)) { 14165aefb655Srie return (gnp); 14175aefb655Srie } 14185aefb655Srie } 14195aefb655Srie return ((Gotndx *)0); 14205aefb655Srie } 14215aefb655Srie 14225aefb655Srie Xword 14235aefb655Srie ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl) 14245aefb655Srie { 14255aefb655Srie Os_desc *osp = ofl->ofl_osgot; 14265aefb655Srie Sym_desc *sdp = rdesc->rel_sym; 14275aefb655Srie Xword gotndx; 14285aefb655Srie Gotref gref; 14295aefb655Srie Gotndx *gnp; 14305aefb655Srie 14315aefb655Srie if (rdesc->rel_flags & FLG_REL_DTLS) 14325aefb655Srie gref = GOT_REF_TLSGD; 14335aefb655Srie else if (rdesc->rel_flags & FLG_REL_MTLS) 14345aefb655Srie gref = GOT_REF_TLSLD; 14355aefb655Srie else if (rdesc->rel_flags & FLG_REL_STLS) 14365aefb655Srie gref = GOT_REF_TLSIE; 14375aefb655Srie else 14385aefb655Srie gref = GOT_REF_GENERIC; 14395aefb655Srie 14405aefb655Srie gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, rdesc); 14415aefb655Srie assert(gnp); 14425aefb655Srie 14435aefb655Srie gotndx = (Xword)gnp->gn_gotndx; 14445aefb655Srie 14455aefb655Srie if ((rdesc->rel_flags & FLG_REL_DTLS) && 14465aefb655Srie (rdesc->rel_rtype == R_AMD64_DTPOFF64)) 14475aefb655Srie gotndx++; 14485aefb655Srie 14495aefb655Srie return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE))); 14505aefb655Srie } 14515aefb655Srie 14525aefb655Srie 14535aefb655Srie /* ARGSUSED5 */ 14545aefb655Srie uintptr_t 14555aefb655Srie ld_assign_gotndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl, 14565aefb655Srie Rel_desc * rsp, Sym_desc * sdp) 14575aefb655Srie { 14585aefb655Srie Xword raddend; 14595aefb655Srie Gotndx *gnp, *_gnp; 14605aefb655Srie Listnode *lnp, *plnp; 14615aefb655Srie uint_t gotents; 14625aefb655Srie 14635aefb655Srie raddend = rsp->rel_raddend; 14645aefb655Srie if (pgnp && (pgnp->gn_addend == raddend) && 14655aefb655Srie (pgnp->gn_gotref == gref)) 14665aefb655Srie return (1); 14675aefb655Srie 14685aefb655Srie if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD)) 14695aefb655Srie gotents = 2; 14705aefb655Srie else 14715aefb655Srie gotents = 1; 14725aefb655Srie 14735aefb655Srie plnp = 0; 14745aefb655Srie for (LIST_TRAVERSE(lst, lnp, _gnp)) { 14755aefb655Srie if (_gnp->gn_addend > raddend) 14765aefb655Srie break; 14775aefb655Srie plnp = lnp; 14785aefb655Srie } 14795aefb655Srie 14805aefb655Srie /* 14815aefb655Srie * Allocate a new entry. 14825aefb655Srie */ 14835aefb655Srie if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0) 14845aefb655Srie return (S_ERROR); 14855aefb655Srie gnp->gn_addend = raddend; 14865aefb655Srie gnp->gn_gotndx = ofl->ofl_gotcnt; 14875aefb655Srie gnp->gn_gotref = gref; 14885aefb655Srie 14895aefb655Srie ofl->ofl_gotcnt += gotents; 14905aefb655Srie 14915aefb655Srie if (gref == GOT_REF_TLSLD) { 14925aefb655Srie ofl->ofl_tlsldgotndx = gnp; 14935aefb655Srie return (1); 14945aefb655Srie } 14955aefb655Srie 14965aefb655Srie if (plnp == 0) { 14975aefb655Srie /* 14985aefb655Srie * Insert at head of list 14995aefb655Srie */ 15005aefb655Srie if (list_prependc(lst, (void *)gnp) == 0) 15015aefb655Srie return (S_ERROR); 15025aefb655Srie } else if (_gnp->gn_addend > raddend) { 15035aefb655Srie /* 15045aefb655Srie * Insert in middle of lest 15055aefb655Srie */ 15065aefb655Srie if (list_insertc(lst, (void *)gnp, plnp) == 0) 15075aefb655Srie return (S_ERROR); 15085aefb655Srie } else { 15095aefb655Srie /* 15105aefb655Srie * Append to tail of list 15115aefb655Srie */ 15125aefb655Srie if (list_appendc(lst, (void *)gnp) == 0) 15135aefb655Srie return (S_ERROR); 15145aefb655Srie } 15155aefb655Srie return (1); 15165aefb655Srie } 15175aefb655Srie 15185aefb655Srie void 15195aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl) 15205aefb655Srie { 15215aefb655Srie sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++; 15225aefb655Srie sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++; 15235aefb655Srie ofl->ofl_flags |= FLG_OF_BLDGOT; 15245aefb655Srie } 15255aefb655Srie 15265aefb655Srie static uchar_t plt0_template[M_PLT_ENTSIZE] = { 15275aefb655Srie /* 0x00 PUSHQ GOT+8(%rip) */ 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, 15285aefb655Srie /* 0x06 JMP *GOT+16(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 15295aefb655Srie /* 0x0c NOP */ 0x90, 15305aefb655Srie /* 0x0d NOP */ 0x90, 15315aefb655Srie /* 0x0e NOP */ 0x90, 15325aefb655Srie /* 0x0f NOP */ 0x90 15335aefb655Srie }; 15345aefb655Srie 15355aefb655Srie /* 15365aefb655Srie * Initializes .got[0] with the _DYNAMIC symbol value. 15375aefb655Srie */ 15385aefb655Srie uintptr_t 15395aefb655Srie ld_fillin_gotplt(Ofl_desc * ofl) 15405aefb655Srie { 15415aefb655Srie Word flags = ofl->ofl_flags; 15425aefb655Srie Word dtflags1 = ofl->ofl_dtflags_1; 15435aefb655Srie 15445aefb655Srie if (ofl->ofl_osgot) { 15455aefb655Srie Sym_desc * sdp; 15465aefb655Srie 15475aefb655Srie if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U), 15485aefb655Srie SYM_NOHASH, 0, ofl)) != NULL) { 15495aefb655Srie uchar_t *genptr = 15505aefb655Srie ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf + 15515aefb655Srie (M_GOT_XDYNAMIC * M_GOT_ENTSIZE)); 15525aefb655Srie /* LINTED */ 15535aefb655Srie *(Xword *)genptr = sdp->sd_sym->st_value; 15545aefb655Srie } 15555aefb655Srie } 15565aefb655Srie 15575aefb655Srie /* 15585aefb655Srie * Fill in the reserved slot in the procedure linkage table the first 15595aefb655Srie * entry is: 15605aefb655Srie * 0x00 PUSHQ GOT+8(%rip) # GOT[1] 15615aefb655Srie * 0x06 JMP *GOT+16(%rip) # GOT[2] 15625aefb655Srie * 0x0c NOP 15635aefb655Srie * 0x0d NOP 15645aefb655Srie * 0x0e NOP 15655aefb655Srie * 0x0f NOP 15665aefb655Srie */ 15675aefb655Srie if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) { 15685aefb655Srie uchar_t *pltent; 15695aefb655Srie Xword val1; 15705aefb655Srie 15715aefb655Srie pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf; 15725aefb655Srie bcopy(plt0_template, pltent, sizeof (plt0_template)); 15735aefb655Srie 15745aefb655Srie /* 15755aefb655Srie * filin: 15765aefb655Srie * PUSHQ GOT + 8(%rip) 15775aefb655Srie * 15785aefb655Srie * Note: 0x06 below represents the offset to the 15795aefb655Srie * next instruction - which is what %rip will 15805aefb655Srie * be pointing at. 15815aefb655Srie */ 15825aefb655Srie val1 = (ofl->ofl_osgot->os_shdr->sh_addr) + 15835aefb655Srie (M_GOT_XLINKMAP * M_GOT_ENTSIZE) - 15845aefb655Srie ofl->ofl_osplt->os_shdr->sh_addr - 0x06; 15855aefb655Srie 15865aefb655Srie /* 15875aefb655Srie * If '-z noreloc' is specified - skip the do_reloc 15885aefb655Srie * stage. 15895aefb655Srie */ 15905aefb655Srie if ((flags & FLG_OF_RELOBJ) || 15915aefb655Srie !(dtflags1 & DF_1_NORELOC)) { 15925aefb655Srie if (do_reloc(R_AMD64_GOTPCREL, &pltent[0x02], 15935aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 15945aefb655Srie MSG_ORIG(MSG_SPECFIL_PLTENT), ofl->ofl_lml) == 0) { 15955aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 15965aefb655Srie MSG_INTL(MSG_PLT_PLT0FAIL)); 15975aefb655Srie return (S_ERROR); 15985aefb655Srie } 15995aefb655Srie } 16005aefb655Srie 16015aefb655Srie /* 16025aefb655Srie * filin: 16035aefb655Srie * JMP *GOT+16(%rip) 16045aefb655Srie */ 16055aefb655Srie val1 = (ofl->ofl_osgot->os_shdr->sh_addr) + 16065aefb655Srie (M_GOT_XRTLD * M_GOT_ENTSIZE) - 16075aefb655Srie ofl->ofl_osplt->os_shdr->sh_addr - 0x0c; 16085aefb655Srie /* 16095aefb655Srie * If '-z noreloc' is specified - skip the do_reloc 16105aefb655Srie * stage. 16115aefb655Srie */ 16125aefb655Srie if ((flags & FLG_OF_RELOBJ) || 16135aefb655Srie !(dtflags1 & DF_1_NORELOC)) { 16145aefb655Srie if (do_reloc(R_AMD64_GOTPCREL, &pltent[0x08], 16155aefb655Srie &val1, MSG_ORIG(MSG_SYM_PLTENT), 16165aefb655Srie MSG_ORIG(MSG_SPECFIL_PLTENT), ofl->ofl_lml) == 0) { 16175aefb655Srie eprintf(ofl->ofl_lml, ERR_FATAL, 16185aefb655Srie MSG_INTL(MSG_PLT_PLT0FAIL)); 16195aefb655Srie return (S_ERROR); 16205aefb655Srie } 16215aefb655Srie } 16225aefb655Srie } 16235aefb655Srie return (1); 16245aefb655Srie } 1625