11618Srie /* 21618Srie * CDDL HEADER START 31618Srie * 41618Srie * The contents of this file are subject to the terms of the 51618Srie * Common Development and Distribution License (the "License"). 61618Srie * You may not use this file except in compliance with the License. 71618Srie * 81618Srie * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 91618Srie * or http://www.opensolaris.org/os/licensing. 101618Srie * See the License for the specific language governing permissions 111618Srie * and limitations under the License. 121618Srie * 131618Srie * When distributing Covered Code, include this CDDL HEADER in each 141618Srie * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 151618Srie * If applicable, add the following below this CDDL HEADER, with the 161618Srie * fields enclosed by brackets "[]" replaced with your own identifying 171618Srie * information: Portions Copyright [yyyy] [name of copyright owner] 181618Srie * 191618Srie * CDDL HEADER END 201618Srie */ 211618Srie 221618Srie /* 231618Srie * Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. 241618Srie * Copyright (c) 1988 AT&T 251618Srie * All Rights Reserved 261618Srie * 278501SRod.Evans@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 281618Srie * Use is subject to license terms. 291618Srie */ 301618Srie 316206Sab196087 /* Get the x86 version of the relocation engine */ 326206Sab196087 #define DO_RELOC_LIBLD_X86 336206Sab196087 341618Srie #include <string.h> 351618Srie #include <stdio.h> 361618Srie #include <sys/elf_386.h> 371618Srie #include <debug.h> 381618Srie #include <reloc.h> 396206Sab196087 #include <i386/machdep_x86.h> 401618Srie #include "msg.h" 411618Srie #include "_libld.h" 421618Srie 439131SRod.Evans@Sun.COM /* 449131SRod.Evans@Sun.COM * Search the GOT index list for a GOT entry with a matching reference. 459131SRod.Evans@Sun.COM */ 469131SRod.Evans@Sun.COM /* ARGSUSED3 */ 479131SRod.Evans@Sun.COM static Gotndx * 489131SRod.Evans@Sun.COM ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc) 499131SRod.Evans@Sun.COM { 509131SRod.Evans@Sun.COM Aliste idx; 519131SRod.Evans@Sun.COM Gotndx *gnp; 529131SRod.Evans@Sun.COM 539131SRod.Evans@Sun.COM if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx) 549131SRod.Evans@Sun.COM return (ofl->ofl_tlsldgotndx); 559131SRod.Evans@Sun.COM 569131SRod.Evans@Sun.COM for (ALIST_TRAVERSE(alp, idx, gnp)) { 579131SRod.Evans@Sun.COM if (gnp->gn_gotref == gref) 589131SRod.Evans@Sun.COM return (gnp); 599131SRod.Evans@Sun.COM } 609131SRod.Evans@Sun.COM return (NULL); 619131SRod.Evans@Sun.COM } 626206Sab196087 639131SRod.Evans@Sun.COM static Xword 649131SRod.Evans@Sun.COM ld_calc_got_offset(Rel_desc *rdesc, Ofl_desc *ofl) 659131SRod.Evans@Sun.COM { 669131SRod.Evans@Sun.COM Os_desc *osp = ofl->ofl_osgot; 679131SRod.Evans@Sun.COM Sym_desc *sdp = rdesc->rel_sym; 689131SRod.Evans@Sun.COM Xword gotndx; 699131SRod.Evans@Sun.COM Gotref gref; 709131SRod.Evans@Sun.COM Gotndx *gnp; 719131SRod.Evans@Sun.COM 729131SRod.Evans@Sun.COM if (rdesc->rel_flags & FLG_REL_DTLS) 739131SRod.Evans@Sun.COM gref = GOT_REF_TLSGD; 749131SRod.Evans@Sun.COM else if (rdesc->rel_flags & FLG_REL_MTLS) 759131SRod.Evans@Sun.COM gref = GOT_REF_TLSLD; 769131SRod.Evans@Sun.COM else if (rdesc->rel_flags & FLG_REL_STLS) 779131SRod.Evans@Sun.COM gref = GOT_REF_TLSIE; 789131SRod.Evans@Sun.COM else 799131SRod.Evans@Sun.COM gref = GOT_REF_GENERIC; 809131SRod.Evans@Sun.COM 819131SRod.Evans@Sun.COM gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL); 829131SRod.Evans@Sun.COM assert(gnp); 839131SRod.Evans@Sun.COM 849131SRod.Evans@Sun.COM gotndx = (Xword)gnp->gn_gotndx; 859131SRod.Evans@Sun.COM 869131SRod.Evans@Sun.COM if ((rdesc->rel_flags & FLG_REL_DTLS) && 879131SRod.Evans@Sun.COM (rdesc->rel_rtype == R_386_TLS_DTPOFF32)) 889131SRod.Evans@Sun.COM gotndx++; 899131SRod.Evans@Sun.COM 909131SRod.Evans@Sun.COM return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE))); 919131SRod.Evans@Sun.COM } 926206Sab196087 936206Sab196087 static Word 941618Srie ld_init_rel(Rel_desc *reld, void *reloc) 951618Srie { 969131SRod.Evans@Sun.COM Rel *rel = (Rel *)reloc; 971618Srie 981618Srie /* LINTED */ 996206Sab196087 reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH); 1001618Srie reld->rel_roffset = rel->r_offset; 1011618Srie reld->rel_raddend = 0; 1021618Srie reld->rel_typedata = 0; 1031618Srie 1041618Srie return ((Word)ELF_R_SYM(rel->r_info)); 1051618Srie } 1061618Srie 1076206Sab196087 static void 1081618Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl) 1091618Srie { 1101618Srie ofl->ofl_dehdr->e_flags |= ehdr->e_flags; 1111618Srie } 1121618Srie 1136206Sab196087 static void 1141618Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt) 1151618Srie { 1161618Srie if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) { 1171618Srie /* 1181618Srie * Create this entry if we are going to create a PLT table. 1191618Srie */ 1201618Srie if (ofl->ofl_pltcnt) 1211618Srie (*cnt)++; /* DT_PLTGOT */ 1221618Srie } 1231618Srie } 1241618Srie 1256206Sab196087 static void 1262145Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn) 1271618Srie { 1281618Srie if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) { 1291618Srie (*dyn)->d_tag = DT_PLTGOT; 1301618Srie if (ofl->ofl_osgot) 1311618Srie (*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr; 1321618Srie else 1331618Srie (*dyn)->d_un.d_ptr = 0; 1341618Srie (*dyn)++; 1351618Srie } 1361618Srie } 1371618Srie 1386206Sab196087 static Xword 1391618Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl) 1401618Srie { 1411618Srie Xword value; 1421618Srie 1431618Srie value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) + 1441618Srie M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE); 1451618Srie return (value); 1461618Srie } 1471618Srie 1481618Srie /* 1491618Srie * Build a single plt entry - code is: 1501618Srie * if (building a.out) 1511618Srie * JMP *got_off 1521618Srie * else 1531618Srie * JMP *got_off@GOT(%ebx) 1541618Srie * PUSHL &rel_off 1551618Srie * JMP -n(%pc) # -n is pcrel offset to first plt entry 1561618Srie * 1571618Srie * The got_off@GOT entry gets filled with the address of the PUSHL, 1581618Srie * so the first pass through the plt jumps back here, jumping 1591618Srie * in turn to the first plt entry, which jumps to the dynamic 1601618Srie * linker. The dynamic linker then patches the GOT, rerouting 1611618Srie * future plt calls to the proper destination. 1621618Srie */ 1631618Srie static void 1641618Srie plt_entry(Ofl_desc * ofl, Word rel_off, Sym_desc * sdp) 1651618Srie { 1661618Srie uchar_t *pltent, *gotent; 1671618Srie Sword plt_off; 1681618Srie Word got_off; 1696206Sab196087 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 1701618Srie 1711618Srie got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE; 1721618Srie plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * 1731618Srie M_PLT_ENTSIZE); 1741618Srie pltent = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf) + plt_off; 1751618Srie gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off; 1761618Srie 1771618Srie /* 1781618Srie * Fill in the got entry with the address of the next instruction. 1791618Srie */ 1801618Srie /* LINTED */ 1811618Srie *(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off + 1821618Srie M_PLT_INSSIZE; 1836206Sab196087 if (bswap) 1846206Sab196087 /* LINTED */ 1856206Sab196087 *(Word *)gotent = ld_bswap_Word(*(Word *)gotent); 1861618Srie 1871618Srie if (!(ofl->ofl_flags & FLG_OF_SHAROBJ)) { 1881618Srie pltent[0] = M_SPECIAL_INST; 1891618Srie pltent[1] = M_JMP_DISP_IND; 1901618Srie pltent += 2; 1911618Srie /* LINTED */ 1921618Srie *(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->sh_addr + 1934734Sab196087 got_off); 1941618Srie } else { 1951618Srie pltent[0] = M_SPECIAL_INST; 1961618Srie pltent[1] = M_JMP_REG_DISP_IND; 1971618Srie pltent += 2; 1981618Srie /* LINTED */ 1991618Srie *(Word *)pltent = (Word)got_off; 2001618Srie } 2016206Sab196087 if (bswap) 2026206Sab196087 /* LINTED */ 2036206Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 2041618Srie pltent += 4; 2051618Srie 2061618Srie pltent[0] = M_INST_PUSHL; 2071618Srie pltent++; 2081618Srie /* LINTED */ 2091618Srie *(Word *)pltent = (Word)rel_off; 2106206Sab196087 if (bswap) 2116206Sab196087 /* LINTED */ 2126206Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 2131618Srie pltent += 4; 2141618Srie 2151618Srie plt_off = -(plt_off + 16); /* JMP, PUSHL, JMP take 16 bytes */ 2161618Srie pltent[0] = M_INST_JMP; 2171618Srie pltent++; 2181618Srie /* LINTED */ 2191618Srie *(Word *)pltent = (Word)plt_off; 2206206Sab196087 if (bswap) 2216206Sab196087 /* LINTED */ 2226206Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 2231618Srie } 2241618Srie 2256206Sab196087 static uintptr_t 2261618Srie ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl) 2271618Srie { 2281618Srie Os_desc * relosp, * osp = 0; 2291618Srie Word ndx, roffset, value; 2301618Srie Rel rea; 2311618Srie char *relbits; 2321618Srie Sym_desc * sdp, * psym = (Sym_desc *)0; 2331618Srie int sectmoved = 0; 2341618Srie 2351618Srie sdp = orsp->rel_sym; 2361618Srie 2371618Srie /* 2381618Srie * If the section this relocation is against has been discarded 2391618Srie * (-zignore), then also discard (skip) the relocation itself. 2401618Srie */ 2411618Srie if (orsp->rel_isdesc && ((orsp->rel_flags & 2421618Srie (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) && 2431618Srie (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) { 2441618Srie DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp)); 2451618Srie return (1); 2461618Srie } 2471618Srie 2481618Srie /* 2491618Srie * If this is a relocation against a move table, or expanded move 2501618Srie * table, adjust the relocation entries. 2511618Srie */ 2521618Srie if (orsp->rel_move) 2531618Srie ld_adj_movereloc(ofl, orsp); 2541618Srie 2551618Srie /* 2561618Srie * If this is a relocation against a section using a partial initialized 2571618Srie * symbol, adjust the embedded symbol info. 2581618Srie * 2591618Srie * The second argument of the am_I_partial() is the value stored at the 2601618Srie * target address relocation is going to be applied. 2611618Srie */ 2621618Srie if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) { 2639131SRod.Evans@Sun.COM if (ofl->ofl_parsyms && 2641618Srie (sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 2651618Srie /* LINTED */ 2661618Srie (psym = ld_am_I_partial(orsp, *(Xword *) 2671618Srie ((uchar_t *)(orsp->rel_isdesc->is_indata->d_buf) + 2681618Srie orsp->rel_roffset)))) { 2691618Srie DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym)); 2701618Srie sectmoved = 1; 2714734Sab196087 } 2721618Srie } 2731618Srie 2741618Srie value = sdp->sd_sym->st_value; 2751618Srie 2761618Srie if (orsp->rel_flags & FLG_REL_GOT) { 2771618Srie osp = ofl->ofl_osgot; 2781618Srie roffset = (Word)ld_calc_got_offset(orsp, ofl); 2791618Srie 2801618Srie } else if (orsp->rel_flags & FLG_REL_PLT) { 2811618Srie /* 2821618Srie * Note that relocations for PLT's actually 2831618Srie * cause a relocation againt the GOT. 2841618Srie */ 2851618Srie osp = ofl->ofl_osplt; 2861618Srie roffset = (Word) (ofl->ofl_osgot->os_shdr->sh_addr) + 2871618Srie sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE; 2881618Srie 2891618Srie plt_entry(ofl, osp->os_relosdesc->os_szoutrels, sdp); 2901618Srie 2911618Srie } else if (orsp->rel_flags & FLG_REL_BSS) { 2921618Srie /* 2931618Srie * This must be a R_386_COPY. For these set the roffset to 2941618Srie * point to the new symbols location. 2951618Srie */ 2961618Srie osp = ofl->ofl_isbss->is_osdesc; 2971618Srie roffset = (Word)value; 2981618Srie } else { 2991618Srie osp = orsp->rel_osdesc; 3001618Srie 3011618Srie /* 3021618Srie * Calculate virtual offset of reference point; equals offset 3031618Srie * into section + vaddr of section for loadable sections, or 3041618Srie * offset plus section displacement for nonloadable sections. 3051618Srie */ 3061618Srie roffset = orsp->rel_roffset + 3071618Srie (Off)_elf_getxoff(orsp->rel_isdesc->is_indata); 3081618Srie if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) 3091618Srie roffset += orsp->rel_isdesc->is_osdesc-> 3101618Srie os_shdr->sh_addr; 3111618Srie } 3121618Srie 3131618Srie if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0)) 3141618Srie relosp = ofl->ofl_osrel; 3151618Srie 3161618Srie /* 3171618Srie * Assign the symbols index for the output relocation. If the 3181618Srie * relocation refers to a SECTION symbol then it's index is based upon 3191618Srie * the output sections symbols index. Otherwise the index can be 3201618Srie * derived from the symbols index itself. 3211618Srie */ 3221618Srie if (orsp->rel_rtype == R_386_RELATIVE) 3231618Srie ndx = STN_UNDEF; 3241618Srie else if ((orsp->rel_flags & FLG_REL_SCNNDX) || 3251618Srie (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) { 3261618Srie if (sectmoved == 0) { 3271618Srie /* 3281618Srie * Check for a null input section. This can 3291618Srie * occur if this relocation references a symbol 3301618Srie * generated by sym_add_sym(). 3311618Srie */ 3329131SRod.Evans@Sun.COM if (sdp->sd_isc && sdp->sd_isc->is_osdesc) 3339131SRod.Evans@Sun.COM ndx = sdp->sd_isc->is_osdesc->os_identndx; 3341618Srie else 3351618Srie ndx = sdp->sd_shndx; 3361618Srie } else 3378159SAli.Bahrami@Sun.COM ndx = ofl->ofl_parexpnndx; 3381618Srie } else 3391618Srie ndx = sdp->sd_symndx; 3401618Srie 3415892Sab196087 /* 3425892Sab196087 * If we have a replacement value for the relocation 3435892Sab196087 * target, put it in place now. 3445892Sab196087 */ 3455892Sab196087 if (orsp->rel_flags & FLG_REL_NADDEND) { 3465892Sab196087 Xword addend = orsp->rel_raddend; 3475892Sab196087 uchar_t *addr; 3485892Sab196087 3495892Sab196087 /* 3505892Sab196087 * Get the address of the data item we need to modify. 3515892Sab196087 */ 3525892Sab196087 addr = (uchar_t *)((uintptr_t)orsp->rel_roffset + 3535892Sab196087 (uintptr_t)_elf_getxoff(orsp->rel_isdesc->is_indata)); 3545892Sab196087 addr += (uintptr_t)orsp->rel_osdesc->os_outdata->d_buf; 3555892Sab196087 if (ld_reloc_targval_set(ofl, orsp, addr, addend) == 0) 3565892Sab196087 return (S_ERROR); 3575892Sab196087 } 3585892Sab196087 3591618Srie relbits = (char *)relosp->os_outdata->d_buf; 3601618Srie 3611618Srie rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype); 3621618Srie rea.r_offset = roffset; 3631618Srie DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_REL, &rea, relosp->os_name, 3641618Srie orsp->rel_sname)); 3651618Srie 3661618Srie /* 3671618Srie * Assert we haven't walked off the end of our relocation table. 3681618Srie */ 3691618Srie assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size); 3701618Srie 3711618Srie (void) memcpy((relbits + relosp->os_szoutrels), 3721618Srie (char *)&rea, sizeof (Rel)); 3731618Srie relosp->os_szoutrels += sizeof (Rel); 3741618Srie 3751618Srie /* 3761618Srie * Determine if this relocation is against a non-writable, allocatable 3771618Srie * section. If so we may need to provide a text relocation diagnostic. 3781618Srie * Note that relocations against the .plt (R_386_JMP_SLOT) actually 3791618Srie * result in modifications to the .got. 3801618Srie */ 3811618Srie if (orsp->rel_rtype == R_386_JMP_SLOT) 3821618Srie osp = ofl->ofl_osgot; 3831618Srie 3841618Srie ld_reloc_remain_entry(orsp, osp, ofl); 3851618Srie return (1); 3861618Srie } 3871618Srie 3881618Srie /* 3891618Srie * i386 Instructions for TLS processing 3901618Srie */ 3911618Srie static uchar_t tlsinstr_gd_ie[] = { 3921618Srie /* 3931618Srie * 0x00 movl %gs:0x0, %eax 3941618Srie */ 3951618Srie 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, 3961618Srie /* 3971618Srie * 0x06 addl x(%eax), %eax 3981618Srie * 0x0c ... 3991618Srie */ 4001618Srie 0x03, 0x80, 0x00, 0x00, 0x00, 0x00 4011618Srie }; 4021618Srie 4031618Srie static uchar_t tlsinstr_gd_le[] = { 4041618Srie /* 4051618Srie * 0x00 movl %gs:0x0, %eax 4061618Srie */ 4071618Srie 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00, 4081618Srie /* 4091618Srie * 0x06 addl $0x0, %eax 4101618Srie */ 4111618Srie 0x05, 0x00, 0x00, 0x00, 0x00, 4121618Srie /* 4131618Srie * 0x0b nop 4141618Srie * 0x0c 4151618Srie */ 4161618Srie 0x90 4171618Srie }; 4181618Srie 4191618Srie static uchar_t tlsinstr_gd_ie_movgs[] = { 4201618Srie /* 4211618Srie * movl %gs:0x0,%eax 4221618Srie */ 4231618Srie 0x65, 0xa1, 0x00, 0x00, 0x00, 00 4241618Srie }; 4251618Srie 4261618Srie #define TLS_GD_IE_MOV 0x8b /* movl opcode */ 4271618Srie #define TLS_GD_IE_POP 0x58 /* popl + reg */ 4281618Srie 4291618Srie #define TLS_GD_LE_MOVL 0xb8 /* movl + reg */ 4301618Srie 4311618Srie #define TLS_NOP 0x90 /* NOP instruction */ 4321618Srie 4331618Srie #define MODRM_MSK_MOD 0xc0 4341618Srie #define MODRM_MSK_RO 0x38 4351618Srie #define MODRM_MSK_RM 0x07 4361618Srie 4371618Srie #define SIB_MSK_SS 0xc0 4381618Srie #define SIB_MSK_IND 0x38 4391618Srie #define SIB_MSK_BS 0x07 4401618Srie 4411618Srie static Fixupret 4421618Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp) 4431618Srie { 4441618Srie Sym_desc *sdp = arsp->rel_sym; 4451618Srie Word rtype = arsp->rel_rtype; 4461618Srie uchar_t *offset, r1, r2; 4471618Srie 4481618Srie offset = (uchar_t *)((uintptr_t)arsp->rel_roffset + 4491618Srie (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) + 4501618Srie (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf); 4511618Srie 4521618Srie if (sdp->sd_ref == REF_DYN_NEED) { 4531618Srie /* 4541618Srie * IE reference model 4551618Srie */ 4561618Srie switch (rtype) { 4571618Srie case R_386_TLS_GD: 4581618Srie /* 4591618Srie * Transition: 4601618Srie * 0x0 leal x@tlsgd(,r1,1), %eax 4611618Srie * 0x7 call ___tls_get_addr 4621618Srie * 0xc 4631618Srie * To: 4641618Srie * 0x0 movl %gs:0, %eax 4651618Srie * 0x6 addl x@gotntpoff(r1), %eax 4661618Srie */ 4671618Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 4683304Srie R_386_TLS_GOTIE, arsp)); 4691618Srie arsp->rel_rtype = R_386_TLS_GOTIE; 4701618Srie arsp->rel_roffset += 5; 4711618Srie 4721618Srie /* 4733304Srie * Adjust 'offset' to beginning of instruction 4741618Srie * sequence. 4751618Srie */ 4761618Srie offset -= 3; 4771618Srie r1 = (offset[2] & SIB_MSK_IND) >> 3; 4781618Srie (void) memcpy(offset, tlsinstr_gd_ie, 4791618Srie sizeof (tlsinstr_gd_ie)); 4801618Srie 4811618Srie /* 4821618Srie * set register %r1 into the addl 4831618Srie * instruction. 4841618Srie */ 4851618Srie offset[0x7] |= r1; 4861618Srie return (FIX_RELOC); 4871618Srie 4881618Srie case R_386_TLS_GD_PLT: 4891618Srie /* 4901618Srie * Fixup done via the TLS_GD relocation 4911618Srie */ 4921618Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 4933304Srie R_386_NONE, arsp)); 4941618Srie return (FIX_DONE); 4951618Srie } 4961618Srie } 4971618Srie 4981618Srie /* 4991618Srie * LE reference model 5001618Srie */ 5011618Srie switch (rtype) { 5021618Srie case R_386_TLS_GD: 5031618Srie /* 5041618Srie * Transition: 5051618Srie * 0x0 leal x@tlsgd(,r1,1), %eax 5061618Srie * 0x7 call ___tls_get_addr 5071618Srie * 0xc 5081618Srie * To: 5091618Srie * 0x0 movl %gs:0, %eax 5101618Srie * 0x6 addl $x@ntpoff, %eax 5111618Srie * 0xb nop 5121618Srie * 0xc 5131618Srie */ 5141618Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 5153304Srie R_386_TLS_LE, arsp)); 5161618Srie 5171618Srie arsp->rel_rtype = R_386_TLS_LE; 5181618Srie arsp->rel_roffset += 4; 5191618Srie 5201618Srie /* 5213304Srie * Adjust 'offset' to beginning of instruction 5221618Srie * sequence. 5231618Srie */ 5241618Srie offset -= 3; 5251618Srie (void) memcpy(offset, tlsinstr_gd_le, 5261618Srie sizeof (tlsinstr_gd_le)); 5271618Srie return (FIX_RELOC); 5281618Srie 5291618Srie case R_386_TLS_GD_PLT: 5301618Srie case R_386_PLT32: 5311618Srie /* 5321618Srie * Fixup done via the TLS_GD relocation 5331618Srie */ 5341618Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 5353304Srie R_386_NONE, arsp)); 5361618Srie return (FIX_DONE); 5371618Srie 5381618Srie case R_386_TLS_LDM_PLT: 5391618Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 5403304Srie R_386_NONE, arsp)); 5411618Srie 5421618Srie /* 5431618Srie * Transition: 5441618Srie * call __tls_get_addr() 5451618Srie * to: 5461618Srie * nop 5471618Srie * nop 5481618Srie * nop 5491618Srie * nop 5501618Srie * nop 5511618Srie */ 5521618Srie *(offset - 1) = TLS_NOP; 5531618Srie *(offset) = TLS_NOP; 5541618Srie *(offset + 1) = TLS_NOP; 5551618Srie *(offset + 2) = TLS_NOP; 5561618Srie *(offset + 3) = TLS_NOP; 5571618Srie return (FIX_DONE); 5581618Srie 5591618Srie case R_386_TLS_LDM: 5601618Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 5613304Srie R_386_NONE, arsp)); 5621618Srie 5631618Srie /* 5641618Srie * Transition: 5651618Srie * 5661618Srie * 0x00 leal x1@tlsldm(%ebx), %eax 5671618Srie * 0x06 call ___tls_get_addr 5681618Srie * 5691618Srie * to: 5701618Srie * 5711618Srie * 0x00 movl %gs:0, %eax 5721618Srie */ 5731618Srie (void) memcpy(offset - 2, tlsinstr_gd_ie_movgs, 5741618Srie sizeof (tlsinstr_gd_ie_movgs)); 5751618Srie return (FIX_DONE); 5761618Srie 5771618Srie case R_386_TLS_LDO_32: 5781618Srie /* 5791618Srie * Instructions: 5801618Srie * 5811618Srie * 0x10 leal x1@dtpoff(%eax), %edx R_386_TLS_LDO_32 5821618Srie * to 5831618Srie * 0x10 leal x1@ntpoff(%eax), %edx R_386_TLS_LE 5841618Srie * 5851618Srie */ 5861618Srie offset -= 2; 5871618Srie 5881618Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 5893304Srie R_386_TLS_LE, arsp)); 5901618Srie arsp->rel_rtype = R_386_TLS_LE; 5911618Srie return (FIX_RELOC); 5921618Srie 5931618Srie case R_386_TLS_GOTIE: 5941618Srie /* 5951618Srie * These transitions are a little different than the 5961618Srie * others, in that we could have multiple instructions 5971618Srie * pointed to by a single relocation. Depending upon the 5981618Srie * instruction, we perform a different code transition. 5991618Srie * 6001618Srie * Here's the known transitions: 6011618Srie * 6021618Srie * 1) movl foo@gotntpoff(%reg1), %reg2 6031618Srie * 0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff 6041618Srie * 6051618Srie * 2) addl foo@gotntpoff(%reg1), %reg2 6061618Srie * 0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff 6071618Srie * 6081618Srie * Transitions IE -> LE 6091618Srie * 6101618Srie * 1) movl $foo@ntpoff, %reg2 6111618Srie * 0xc7, 0xc0 | reg2, foo@ntpoff 6121618Srie * 6131618Srie * 2) addl $foo@ntpoff, %reg2 6141618Srie * 0x81, 0xc0 | reg2, foo@ntpoff 6151618Srie * 6161618Srie * Note: reg1 != 4 (%esp) 6171618Srie */ 6181618Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 6193304Srie R_386_TLS_LE, arsp)); 6201618Srie arsp->rel_rtype = R_386_TLS_LE; 6211618Srie 6221618Srie offset -= 2; 6231618Srie r2 = (offset[1] & MODRM_MSK_RO) >> 3; 6241618Srie if (offset[0] == 0x8b) { 6251618Srie /* case 1 above */ 6261618Srie offset[0] = 0xc7; /* movl */ 6271618Srie offset[1] = 0xc0 | r2; 6281618Srie return (FIX_RELOC); 6291618Srie } 6301618Srie 6311618Srie if (offset[0] == 0x03) { 6321618Srie /* case 2 above */ 6331618Srie assert(offset[0] == 0x03); 6341618Srie offset[0] = 0x81; /* addl */ 6351618Srie offset[1] = 0xc0 | r2; 6361618Srie return (FIX_RELOC); 6371618Srie } 6381618Srie 6391618Srie /* 6401618Srie * Unexpected instruction sequence - fatal error. 6411618Srie */ 6424734Sab196087 { 6434734Sab196087 Conv_inv_buf_t inv_buf; 6444734Sab196087 6454734Sab196087 eprintf(ofl->ofl_lml, ERR_FATAL, 6464734Sab196087 MSG_INTL(MSG_REL_BADTLSINS), 6474734Sab196087 conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf), 6484734Sab196087 arsp->rel_isdesc->is_file->ifl_name, 6494734Sab196087 demangle(arsp->rel_sname), 6504734Sab196087 arsp->rel_isdesc->is_name, 6514734Sab196087 EC_OFF(arsp->rel_roffset)); 6524734Sab196087 } 6531618Srie return (FIX_ERROR); 6541618Srie 6551618Srie case R_386_TLS_IE: 6561618Srie /* 6571618Srie * These transitions are a little different than the 6581618Srie * others, in that we could have multiple instructions 6591618Srie * pointed to by a single relocation. Depending upon the 6601618Srie * instruction, we perform a different code transition. 6611618Srie * 6621618Srie * Here's the known transitions: 6631618Srie * 1) movl foo@indntpoff, %eax 6641618Srie * 0xa1, foo@indntpoff 6651618Srie * 6661618Srie * 2) movl foo@indntpoff, %eax 6671618Srie * 0x8b, 0x05 | (reg << 3), foo@gotntpoff 6681618Srie * 6691618Srie * 3) addl foo@indntpoff, %eax 6701618Srie * 0x03, 0x05 | (reg << 3), foo@gotntpoff 6711618Srie * 6721618Srie * Transitions IE -> LE 6731618Srie * 6741618Srie * 1) movl $foo@ntpoff, %eax 6751618Srie * 0xb8, foo@ntpoff 6761618Srie * 6771618Srie * 2) movl $foo@ntpoff, %reg 6781618Srie * 0xc7, 0xc0 | reg, foo@ntpoff 6791618Srie * 6801618Srie * 3) addl $foo@ntpoff, %reg 6811618Srie * 0x81, 0xc0 | reg, foo@ntpoff 6821618Srie */ 6831618Srie arsp->rel_rtype = R_386_TLS_LE; 6841618Srie offset--; 6851618Srie if (offset[0] == 0xa1) { 6861618Srie /* case 1 above */ 6871618Srie offset[0] = 0xb8; /* movl */ 6881618Srie return (FIX_RELOC); 6891618Srie } 6901618Srie 6911618Srie offset--; 6921618Srie if (offset[0] == 0x8b) { 6931618Srie /* case 2 above */ 6941618Srie r2 = (offset[1] & MODRM_MSK_RO) >> 3; 6951618Srie offset[0] = 0xc7; /* movl */ 6961618Srie offset[1] = 0xc0 | r2; 6971618Srie return (FIX_RELOC); 6981618Srie } 6991618Srie if (offset[0] == 0x03) { 7001618Srie /* case 3 above */ 7011618Srie r2 = (offset[1] & MODRM_MSK_RO) >> 3; 7021618Srie offset[0] = 0x81; /* addl */ 7031618Srie offset[1] = 0xc0 | r2; 7041618Srie return (FIX_RELOC); 7051618Srie } 7061618Srie /* 7071618Srie * Unexpected instruction sequence - fatal error. 7081618Srie */ 7094734Sab196087 { 7104734Sab196087 Conv_inv_buf_t inv_buf; 7114734Sab196087 7124734Sab196087 eprintf(ofl->ofl_lml, ERR_FATAL, 7134734Sab196087 MSG_INTL(MSG_REL_BADTLSINS), 7144734Sab196087 conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf), 7154734Sab196087 arsp->rel_isdesc->is_file->ifl_name, 7164734Sab196087 demangle(arsp->rel_sname), 7174734Sab196087 arsp->rel_isdesc->is_name, 7184734Sab196087 EC_OFF(arsp->rel_roffset)); 7194734Sab196087 } 7201618Srie return (FIX_ERROR); 7211618Srie } 7221618Srie return (FIX_RELOC); 7231618Srie } 7241618Srie 7256206Sab196087 static uintptr_t 7261618Srie ld_do_activerelocs(Ofl_desc *ofl) 7271618Srie { 7281618Srie Rel_desc *arsp; 7291618Srie Rel_cache *rcp; 7309131SRod.Evans@Sun.COM Aliste idx; 7311618Srie uintptr_t return_code = 1; 7326299Sab196087 ofl_flag_t flags = ofl->ofl_flags; 7331618Srie 7349131SRod.Evans@Sun.COM if (ofl->ofl_actrels) 7352647Srie DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml)); 7362647Srie 7371618Srie /* 7381618Srie * Process active relocations. 7391618Srie */ 7409131SRod.Evans@Sun.COM for (APLIST_TRAVERSE(ofl->ofl_actrels, idx, rcp)) { 7411618Srie /* LINTED */ 7421618Srie for (arsp = (Rel_desc *)(rcp + 1); 7431618Srie arsp < rcp->rc_free; arsp++) { 7441618Srie uchar_t *addr; 7451618Srie Xword value; 7461618Srie Sym_desc *sdp; 7471618Srie const char *ifl_name; 7481618Srie Xword refaddr; 7491618Srie int moved = 0; 7501618Srie Gotref gref; 7511618Srie 7521618Srie /* 7531618Srie * If the section this relocation is against has been 7541618Srie * discarded (-zignore), then discard (skip) the 7551618Srie * relocation itself. 7561618Srie */ 7571618Srie if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) && 7581618Srie ((arsp->rel_flags & 7591618Srie (FLG_REL_GOT | FLG_REL_BSS | 7601618Srie FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) { 7611618Srie DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, 7621618Srie M_MACH, arsp)); 7631618Srie continue; 7641618Srie } 7651618Srie 7661618Srie /* 7678324SAli.Bahrami@Sun.COM * We determine what the 'got reference' 7681618Srie * model (if required) is at this point. This 7691618Srie * needs to be done before tls_fixup() since 7701618Srie * it may 'transition' our instructions. 7711618Srie * 7721618Srie * The got table entries have already been assigned, 7731618Srie * and we bind to those initial entries. 7741618Srie */ 7751618Srie if (arsp->rel_flags & FLG_REL_DTLS) 7761618Srie gref = GOT_REF_TLSGD; 7771618Srie else if (arsp->rel_flags & FLG_REL_MTLS) 7781618Srie gref = GOT_REF_TLSLD; 7791618Srie else if (arsp->rel_flags & FLG_REL_STLS) 7801618Srie gref = GOT_REF_TLSIE; 7811618Srie else 7821618Srie gref = GOT_REF_GENERIC; 7831618Srie 7841618Srie /* 7851618Srie * Perform any required TLS fixups. 7861618Srie */ 7871618Srie if (arsp->rel_flags & FLG_REL_TLSFIX) { 7881618Srie Fixupret ret; 7891618Srie 7901618Srie if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR) 7911618Srie return (S_ERROR); 7921618Srie if (ret == FIX_DONE) 7931618Srie continue; 7941618Srie } 7951618Srie 7961618Srie /* 7971618Srie * If this is a relocation against a move table, or 7981618Srie * expanded move table, adjust the relocation entries. 7991618Srie */ 8001618Srie if (arsp->rel_move) 8011618Srie ld_adj_movereloc(ofl, arsp); 8021618Srie 8031618Srie sdp = arsp->rel_sym; 8041618Srie refaddr = arsp->rel_roffset + 8051618Srie (Off)_elf_getxoff(arsp->rel_isdesc->is_indata); 8061618Srie 8071618Srie if (arsp->rel_flags & FLG_REL_CLVAL) 8081618Srie value = 0; 8091618Srie else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == 8101618Srie STT_SECTION) { 8111618Srie /* 8121618Srie * The value for a symbol pointing to a SECTION 8131618Srie * is based off of that sections position. 8141618Srie */ 8158369SAli.Bahrami@Sun.COM if (sdp->sd_isc->is_flags & FLG_IS_RELUPD) { 8168369SAli.Bahrami@Sun.COM Sym_desc *sym; 8178369SAli.Bahrami@Sun.COM Xword radd; 8188369SAli.Bahrami@Sun.COM uchar_t *raddr = (uchar_t *) 8198369SAli.Bahrami@Sun.COM arsp->rel_isdesc->is_indata->d_buf + 8208369SAli.Bahrami@Sun.COM arsp->rel_roffset; 8218369SAli.Bahrami@Sun.COM 8221618Srie /* 8238369SAli.Bahrami@Sun.COM * This is a REL platform. Hence, the 8248369SAli.Bahrami@Sun.COM * second argument of ld_am_I_partial() 8258369SAli.Bahrami@Sun.COM * is the value stored at the target 8268369SAli.Bahrami@Sun.COM * address where the relocation is 8278369SAli.Bahrami@Sun.COM * going to be applied. 8281618Srie */ 8298369SAli.Bahrami@Sun.COM if (ld_reloc_targval_get(ofl, arsp, 8308369SAli.Bahrami@Sun.COM raddr, &radd) == 0) 8318369SAli.Bahrami@Sun.COM return (S_ERROR); 8328369SAli.Bahrami@Sun.COM sym = ld_am_I_partial(arsp, radd); 8338369SAli.Bahrami@Sun.COM if (sym) { 8348369SAli.Bahrami@Sun.COM Sym *osym = sym->sd_osym; 8358369SAli.Bahrami@Sun.COM 8368369SAli.Bahrami@Sun.COM /* 8378369SAli.Bahrami@Sun.COM * The symbol was moved, so 8388369SAli.Bahrami@Sun.COM * adjust the value relative 8398369SAli.Bahrami@Sun.COM * to the new section. 8408369SAli.Bahrami@Sun.COM */ 8418369SAli.Bahrami@Sun.COM value = sym->sd_sym->st_value; 8428369SAli.Bahrami@Sun.COM moved = 1; 8438369SAli.Bahrami@Sun.COM 8448369SAli.Bahrami@Sun.COM /* 8458369SAli.Bahrami@Sun.COM * The original raddend covers 8468369SAli.Bahrami@Sun.COM * the displacement from the 8478369SAli.Bahrami@Sun.COM * section start to the desired 8488369SAli.Bahrami@Sun.COM * address. The value computed 8498369SAli.Bahrami@Sun.COM * above gets us from the 8508369SAli.Bahrami@Sun.COM * section start to the start 8518369SAli.Bahrami@Sun.COM * of the symbol range. Adjust 8528369SAli.Bahrami@Sun.COM * the old raddend to remove the 8538369SAli.Bahrami@Sun.COM * offset from section start to 8548369SAli.Bahrami@Sun.COM * symbol start, leaving the 8558369SAli.Bahrami@Sun.COM * displacement within the 8568369SAli.Bahrami@Sun.COM * range of the symbol. 8578369SAli.Bahrami@Sun.COM */ 8588369SAli.Bahrami@Sun.COM if (osym->st_value != 0) { 8598369SAli.Bahrami@Sun.COM radd -= osym->st_value; 8608369SAli.Bahrami@Sun.COM if (ld_reloc_targval_set 8618369SAli.Bahrami@Sun.COM (ofl, arsp, raddr, 8628369SAli.Bahrami@Sun.COM radd) == 0) 8638369SAli.Bahrami@Sun.COM return ( 8648369SAli.Bahrami@Sun.COM S_ERROR); 8658369SAli.Bahrami@Sun.COM } 8668369SAli.Bahrami@Sun.COM } 8678369SAli.Bahrami@Sun.COM } 8688369SAli.Bahrami@Sun.COM if (!moved) { 8691618Srie value = _elf_getxoff( 8701618Srie sdp->sd_isc->is_indata); 8711618Srie if (sdp->sd_isc->is_shdr->sh_flags & 8721618Srie SHF_ALLOC) 8731618Srie value += sdp->sd_isc-> 8741618Srie is_osdesc->os_shdr->sh_addr; 8751618Srie } 8761618Srie if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS) 8771618Srie value -= ofl->ofl_tlsphdr->p_vaddr; 8782850Srie 8792850Srie } else if (IS_SIZE(arsp->rel_rtype)) { 8802850Srie /* 8812850Srie * Size relocations require the symbols size. 8822850Srie */ 8832850Srie value = sdp->sd_sym->st_size; 8841618Srie } else { 8851618Srie /* 8862647Srie * Else the value is the symbols value. 8871618Srie */ 8881618Srie value = sdp->sd_sym->st_value; 8891618Srie } 8901618Srie 8911618Srie /* 8921618Srie * Relocation against the GLOBAL_OFFSET_TABLE. 8931618Srie */ 8941618Srie if (arsp->rel_flags & FLG_REL_GOT) 8951618Srie arsp->rel_osdesc = ofl->ofl_osgot; 8961618Srie 8971618Srie /* 8981618Srie * If loadable and not producing a relocatable object 8991618Srie * add the sections virtual address to the reference 9001618Srie * address. 9011618Srie */ 9021618Srie if ((arsp->rel_flags & FLG_REL_LOAD) && 9031618Srie ((flags & FLG_OF_RELOBJ) == 0)) 9041618Srie refaddr += arsp->rel_isdesc->is_osdesc-> 9051618Srie os_shdr->sh_addr; 9061618Srie 9071618Srie /* 9081618Srie * If this entry has a PLT assigned to it, it's 9091618Srie * value is actually the address of the PLT (and 9101618Srie * not the address of the function). 9111618Srie */ 9121618Srie if (IS_PLT(arsp->rel_rtype)) { 9131618Srie if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx) 9141618Srie value = ld_calc_plt_addr(sdp, ofl); 9151618Srie } 9161618Srie 9171618Srie /* 9181618Srie * Determine whether the value needs further adjustment. 9191618Srie * Filter through the attributes of the relocation to 9201618Srie * determine what adjustment is required. Note, many 9211618Srie * of the following cases are only applicable when a 9221618Srie * .got is present. As a .got is not generated when a 9231618Srie * relocatable object is being built, any adjustments 9241618Srie * that require a .got need to be skipped. 9251618Srie */ 9261618Srie if ((arsp->rel_flags & FLG_REL_GOT) && 9271618Srie ((flags & FLG_OF_RELOBJ) == 0)) { 9281618Srie Xword R1addr; 9291618Srie uintptr_t R2addr; 9301618Srie Word gotndx; 9311618Srie Gotndx *gnp; 9321618Srie 9331618Srie /* 9341618Srie * Perform relocation against GOT table. Since 9351618Srie * this doesn't fit exactly into a relocation 9361618Srie * we place the appropriate byte in the GOT 9371618Srie * directly 9381618Srie * 9391618Srie * Calculate offset into GOT at which to apply 9401618Srie * the relocation. 9411618Srie */ 9429131SRod.Evans@Sun.COM gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, 9439131SRod.Evans@Sun.COM ofl, NULL); 9441618Srie assert(gnp); 9451618Srie 9461618Srie if (arsp->rel_rtype == R_386_TLS_DTPOFF32) 9471618Srie gotndx = gnp->gn_gotndx + 1; 9481618Srie else 9491618Srie gotndx = gnp->gn_gotndx; 9501618Srie 9511618Srie R1addr = (Xword)(gotndx * M_GOT_ENTSIZE); 9521618Srie 9531618Srie /* 9541618Srie * Add the GOTs data's offset. 9551618Srie */ 9561618Srie R2addr = R1addr + (uintptr_t) 9571618Srie arsp->rel_osdesc->os_outdata->d_buf; 9581618Srie 9591618Srie DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, 96010792SRod.Evans@Sun.COM ELF_DBG_LD_ACT, M_MACH, SHT_REL, 9611618Srie arsp->rel_rtype, R1addr, value, 9621618Srie arsp->rel_sname, arsp->rel_osdesc)); 9631618Srie 9641618Srie /* 9651618Srie * And do it. 9661618Srie */ 9675189Sab196087 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) 9685189Sab196087 *(Xword *)R2addr = 9696206Sab196087 ld_bswap_Xword(value); 9705189Sab196087 else 9715189Sab196087 *(Xword *)R2addr = value; 9721618Srie continue; 9731618Srie 9741618Srie } else if (IS_GOT_BASED(arsp->rel_rtype) && 9751618Srie ((flags & FLG_OF_RELOBJ) == 0)) { 9761618Srie value -= ofl->ofl_osgot->os_shdr->sh_addr; 9771618Srie 9781618Srie } else if (IS_GOT_PC(arsp->rel_rtype) && 9791618Srie ((flags & FLG_OF_RELOBJ) == 0)) { 9801618Srie value = 9811618Srie (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) - 9821618Srie refaddr; 9831618Srie 9841618Srie } else if ((IS_PC_RELATIVE(arsp->rel_rtype)) && 9851618Srie (((flags & FLG_OF_RELOBJ) == 0) || 9861618Srie (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) { 9871618Srie value -= refaddr; 9881618Srie 9891618Srie } else if (IS_TLS_INS(arsp->rel_rtype) && 9901618Srie IS_GOT_RELATIVE(arsp->rel_rtype) && 9911618Srie ((flags & FLG_OF_RELOBJ) == 0)) { 9921618Srie Gotndx *gnp; 9931618Srie 9949131SRod.Evans@Sun.COM gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, 9959131SRod.Evans@Sun.COM ofl, NULL); 9961618Srie assert(gnp); 9971618Srie value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 9981618Srie if (arsp->rel_rtype == R_386_TLS_IE) { 9991618Srie value += 10001618Srie ofl->ofl_osgot->os_shdr->sh_addr; 10011618Srie } 10021618Srie 10031618Srie } else if (IS_GOT_RELATIVE(arsp->rel_rtype) && 10041618Srie ((flags & FLG_OF_RELOBJ) == 0)) { 10051618Srie Gotndx *gnp; 10061618Srie 10079131SRod.Evans@Sun.COM gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 10089131SRod.Evans@Sun.COM GOT_REF_GENERIC, ofl, NULL); 10091618Srie assert(gnp); 10101618Srie value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 10111618Srie 10121618Srie } else if ((arsp->rel_flags & FLG_REL_STLS) && 10131618Srie ((flags & FLG_OF_RELOBJ) == 0)) { 10141618Srie Xword tlsstatsize; 10151618Srie 10161618Srie /* 10171618Srie * This is the LE TLS reference model. Static 10181618Srie * offset is hard-coded. 10191618Srie */ 10201618Srie tlsstatsize = 10211618Srie S_ROUND(ofl->ofl_tlsphdr->p_memsz, 10221618Srie M_TLSSTATALIGN); 10231618Srie value = tlsstatsize - value; 10241618Srie 10251618Srie /* 10261618Srie * Since this code is fixed up, it assumes a 10271618Srie * negative offset that can be added to the 10281618Srie * thread pointer. 10291618Srie */ 10301618Srie if ((arsp->rel_rtype == R_386_TLS_LDO_32) || 10311618Srie (arsp->rel_rtype == R_386_TLS_LE)) 10321618Srie value = -value; 10331618Srie } 10341618Srie 10351618Srie if (arsp->rel_isdesc->is_file) 10361618Srie ifl_name = arsp->rel_isdesc->is_file->ifl_name; 10371618Srie else 10381618Srie ifl_name = MSG_INTL(MSG_STR_NULL); 10391618Srie 10401618Srie /* 10411618Srie * Make sure we have data to relocate. Compiler and 10421618Srie * assembler developers have been known to generate 10431618Srie * relocations against invalid sections (normally .bss), 10441618Srie * so for their benefit give them sufficient information 10451618Srie * to help analyze the problem. End users should never 10461618Srie * see this. 10471618Srie */ 10481618Srie if (arsp->rel_isdesc->is_indata->d_buf == 0) { 10494734Sab196087 Conv_inv_buf_t inv_buf; 10504734Sab196087 10511618Srie eprintf(ofl->ofl_lml, ERR_FATAL, 10521618Srie MSG_INTL(MSG_REL_EMPTYSEC), 10534734Sab196087 conv_reloc_386_type(arsp->rel_rtype, 10544734Sab196087 0, &inv_buf), 10551618Srie ifl_name, demangle(arsp->rel_sname), 10569878SAli.Bahrami@Sun.COM EC_WORD(arsp->rel_isdesc->is_scnndx), 10571618Srie arsp->rel_isdesc->is_name); 10581618Srie return (S_ERROR); 10591618Srie } 10601618Srie 10611618Srie /* 10621618Srie * Get the address of the data item we need to modify. 10631618Srie */ 10641618Srie addr = (uchar_t *)((uintptr_t)arsp->rel_roffset + 10651618Srie (uintptr_t)_elf_getxoff(arsp->rel_isdesc-> 10661618Srie is_indata)); 10671618Srie 106810792SRod.Evans@Sun.COM DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT, 10691618Srie M_MACH, SHT_REL, arsp->rel_rtype, EC_NATPTR(addr), 10701618Srie value, arsp->rel_sname, arsp->rel_osdesc)); 10711618Srie addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf; 10721618Srie 10731618Srie if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) > 10741618Srie ofl->ofl_size) || (arsp->rel_roffset > 10751618Srie arsp->rel_osdesc->os_shdr->sh_size)) { 10764734Sab196087 Conv_inv_buf_t inv_buf; 10774734Sab196087 int class; 10781618Srie 10791618Srie if (((uintptr_t)addr - 10801618Srie (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size) 10811618Srie class = ERR_FATAL; 10821618Srie else 10831618Srie class = ERR_WARNING; 10841618Srie 10851618Srie eprintf(ofl->ofl_lml, class, 10861618Srie MSG_INTL(MSG_REL_INVALOFFSET), 10874734Sab196087 conv_reloc_386_type(arsp->rel_rtype, 10889878SAli.Bahrami@Sun.COM 0, &inv_buf), ifl_name, 10899878SAli.Bahrami@Sun.COM EC_WORD(arsp->rel_isdesc->is_scnndx), 10909878SAli.Bahrami@Sun.COM arsp->rel_isdesc->is_name, 10911618Srie demangle(arsp->rel_sname), 10921618Srie EC_ADDR((uintptr_t)addr - 10931618Srie (uintptr_t)ofl->ofl_nehdr)); 10941618Srie 10951618Srie if (class == ERR_FATAL) { 10961618Srie return_code = S_ERROR; 10971618Srie continue; 10981618Srie } 10991618Srie } 11001618Srie 11011618Srie /* 11021618Srie * The relocation is additive. Ignore the previous 11031618Srie * symbol value if this local partial symbol is 11041618Srie * expanded. 11051618Srie */ 11061618Srie if (moved) 11071618Srie value -= *addr; 11081618Srie 11091618Srie /* 11105892Sab196087 * If we have a replacement value for the relocation 11115892Sab196087 * target, put it in place now. 11125892Sab196087 */ 11135892Sab196087 if (arsp->rel_flags & FLG_REL_NADDEND) { 11145892Sab196087 Xword addend = arsp->rel_raddend; 11155892Sab196087 11165892Sab196087 if (ld_reloc_targval_set(ofl, arsp, 11175892Sab196087 addr, addend) == 0) 11185892Sab196087 return (S_ERROR); 11195892Sab196087 } 11205892Sab196087 11215892Sab196087 /* 11225189Sab196087 * If '-z noreloc' is specified - skip the do_reloc_ld 11231618Srie * stage. 11241618Srie */ 11255189Sab196087 if (OFL_DO_RELOC(ofl)) { 11265189Sab196087 if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr, 11271618Srie &value, arsp->rel_sname, ifl_name, 11285189Sab196087 OFL_SWAP_RELOC_DATA(ofl, arsp), 11291618Srie ofl->ofl_lml) == 0) 11301618Srie return_code = S_ERROR; 11311618Srie } 11321618Srie } 11331618Srie } 11341618Srie return (return_code); 11351618Srie } 11361618Srie 11371618Srie /* 11381618Srie * Add an output relocation record. 11391618Srie */ 11406206Sab196087 static uintptr_t 11411618Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl) 11421618Srie { 11431618Srie Rel_desc *orsp; 11441618Srie Rel_cache *rcp; 11451618Srie Sym_desc *sdp = rsp->rel_sym; 11469131SRod.Evans@Sun.COM static size_t nextsize = 0; 11471618Srie 11481618Srie /* 11491618Srie * Static executables *do not* want any relocations against them. 11501618Srie * Since our engine still creates relocations against a WEAK UNDEFINED 11511618Srie * symbol in a static executable, it's best to disable them here 11521618Srie * instead of through out the relocation code. 11531618Srie */ 115410792SRod.Evans@Sun.COM if (OFL_IS_STATIC_EXEC(ofl)) 11551618Srie return (1); 11561618Srie 11571618Srie /* 11589131SRod.Evans@Sun.COM * Obtain the new available relocation cache entry. 11591618Srie */ 11609131SRod.Evans@Sun.COM if ((rcp = ld_add_rel_cache(ofl, &ofl->ofl_outrels, &nextsize, 11619131SRod.Evans@Sun.COM REL_LOIDESCNO, REL_HOIDESCNO)) == (Rel_cache *)S_ERROR) 11629131SRod.Evans@Sun.COM return (S_ERROR); 11631618Srie 11649131SRod.Evans@Sun.COM orsp = rcp->rc_free; 11651618Srie 11661618Srie /* 11671618Srie * If we are adding a output relocation against a section 11681618Srie * symbol (non-RELATIVE) then mark that section. These sections 11691618Srie * will be added to the .dynsym symbol table. 11701618Srie */ 11711618Srie if (sdp && (rsp->rel_rtype != M_R_RELATIVE) && 11721618Srie ((flags & FLG_REL_SCNNDX) || 11731618Srie (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) { 11741618Srie 11751618Srie /* 11761618Srie * If this is a COMMON symbol - no output section 11771618Srie * exists yet - (it's created as part of sym_validate()). 11781618Srie * So - we mark here that when it's created it should 11791618Srie * be tagged with the FLG_OS_OUTREL flag. 11801618Srie */ 11811618Srie if ((sdp->sd_flags & FLG_SY_SPECSEC) && 11821682Srie (sdp->sd_sym->st_shndx == SHN_COMMON)) { 11831618Srie if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS) 11841618Srie ofl->ofl_flags1 |= FLG_OF1_BSSOREL; 11851618Srie else 11861618Srie ofl->ofl_flags1 |= FLG_OF1_TLSOREL; 11871618Srie } else { 11881618Srie Os_desc *osp = sdp->sd_isc->is_osdesc; 11891618Srie 11902347Srie if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) { 11911618Srie ofl->ofl_dynshdrcnt++; 11921618Srie osp->os_flags |= FLG_OS_OUTREL; 11931618Srie } 11941618Srie } 11951618Srie } 11961618Srie 11971618Srie *orsp = *rsp; 11981618Srie orsp->rel_flags |= flags; 11991618Srie 12001618Srie rcp->rc_free++; 12011618Srie ofl->ofl_outrelscnt++; 12021618Srie 12031618Srie if (flags & FLG_REL_GOT) 12041618Srie ofl->ofl_relocgotsz += (Xword)sizeof (Rel); 12051618Srie else if (flags & FLG_REL_PLT) 12061618Srie ofl->ofl_relocpltsz += (Xword)sizeof (Rel); 12071618Srie else if (flags & FLG_REL_BSS) 12081618Srie ofl->ofl_relocbsssz += (Xword)sizeof (Rel); 12091618Srie else if (flags & FLG_REL_NOINFO) 12101618Srie ofl->ofl_relocrelsz += (Xword)sizeof (Rel); 12111618Srie else 12121618Srie orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rel); 12131618Srie 12141618Srie if (orsp->rel_rtype == M_R_RELATIVE) 12151618Srie ofl->ofl_relocrelcnt++; 12161618Srie 12171618Srie /* 12181618Srie * We don't perform sorting on PLT relocations because 12191618Srie * they have already been assigned a PLT index and if we 12201618Srie * were to sort them we would have to re-assign the plt indexes. 12211618Srie */ 12221618Srie if (!(flags & FLG_REL_PLT)) 12231618Srie ofl->ofl_reloccnt++; 12241618Srie 12251618Srie /* 12261618Srie * Insure a GLOBAL_OFFSET_TABLE is generated if required. 12271618Srie */ 12281618Srie if (IS_GOT_REQUIRED(orsp->rel_rtype)) 12291618Srie ofl->ofl_flags |= FLG_OF_BLDGOT; 12301618Srie 12311618Srie /* 12321618Srie * Identify and possibly warn of a displacement relocation. 12331618Srie */ 12341618Srie if (orsp->rel_flags & FLG_REL_DISP) { 12351618Srie ofl->ofl_dtflags_1 |= DF_1_DISPRELPND; 12361618Srie 12371618Srie if (ofl->ofl_flags & FLG_OF_VERBOSE) 12381618Srie ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl); 12391618Srie } 12401618Srie DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL, 12411618Srie M_MACH, orsp)); 12421618Srie return (1); 12431618Srie } 12441618Srie 12451618Srie /* 12461618Srie * process relocation for a LOCAL symbol 12471618Srie */ 12486206Sab196087 static uintptr_t 12491618Srie ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl) 12501618Srie { 12516299Sab196087 ofl_flag_t flags = ofl->ofl_flags; 12521618Srie Sym_desc *sdp = rsp->rel_sym; 12531682Srie Word shndx = sdp->sd_sym->st_shndx; 12541618Srie 12551618Srie /* 12561618Srie * if ((shared object) and (not pc relative relocation) and 12571618Srie * (not against ABS symbol)) 12581618Srie * then 12591618Srie * build R_386_RELATIVE 12601618Srie * fi 12611618Srie */ 12621618Srie if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) && 12632850Srie !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) && 12641618Srie !(IS_GOT_BASED(rsp->rel_rtype)) && 12651618Srie !(rsp->rel_isdesc != NULL && 12661618Srie (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) && 12671618Srie (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) || 12681618Srie (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) { 12691618Srie Word ortype = rsp->rel_rtype; 12701618Srie 12711618Srie rsp->rel_rtype = R_386_RELATIVE; 12721618Srie if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR) 12731618Srie return (S_ERROR); 12741618Srie rsp->rel_rtype = ortype; 12751618Srie } 12761618Srie 12771618Srie /* 12781618Srie * If the relocation is against a 'non-allocatable' section 12791618Srie * and we can not resolve it now - then give a warning 12801618Srie * message. 12811618Srie * 12821618Srie * We can not resolve the symbol if either: 12831618Srie * a) it's undefined 12841618Srie * b) it's defined in a shared library and a 12851618Srie * COPY relocation hasn't moved it to the executable 12861618Srie * 12871618Srie * Note: because we process all of the relocations against the 12881618Srie * text segment before any others - we know whether 12891618Srie * or not a copy relocation will be generated before 12901618Srie * we get here (see reloc_init()->reloc_segments()). 12911618Srie */ 12921618Srie if (!(rsp->rel_flags & FLG_REL_LOAD) && 12931618Srie ((shndx == SHN_UNDEF) || 12941618Srie ((sdp->sd_ref == REF_DYN_NEED) && 12951618Srie ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) { 12964734Sab196087 Conv_inv_buf_t inv_buf; 12974734Sab196087 12981618Srie /* 12991618Srie * If the relocation is against a SHT_SUNW_ANNOTATE 13001618Srie * section - then silently ignore that the relocation 13011618Srie * can not be resolved. 13021618Srie */ 13031618Srie if (rsp->rel_osdesc && 13041618Srie (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE)) 13051618Srie return (0); 13061618Srie eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM), 13074734Sab196087 conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf), 13081618Srie rsp->rel_isdesc->is_file->ifl_name, 13091618Srie demangle(rsp->rel_sname), rsp->rel_osdesc->os_name); 13101618Srie return (1); 13111618Srie } 13121618Srie 13131618Srie /* 13141618Srie * Perform relocation. 13151618Srie */ 13161618Srie return (ld_add_actrel(NULL, rsp, ofl)); 13171618Srie } 13181618Srie 13196206Sab196087 static uintptr_t 13201618Srie ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl) 13211618Srie { 13221618Srie Word rtype = rsp->rel_rtype; 13231618Srie Sym_desc *sdp = rsp->rel_sym; 13246299Sab196087 ofl_flag_t flags = ofl->ofl_flags; 13251618Srie Gotndx *gnp; 13261618Srie 13271618Srie /* 13282145Srie * If we're building an executable - use either the IE or LE access 13292145Srie * model. If we're building a shared object process any IE model. 13301618Srie */ 13312145Srie if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) { 13321618Srie /* 13332145Srie * Set the DF_STATIC_TLS flag. 13341618Srie */ 13351618Srie ofl->ofl_dtflags |= DF_STATIC_TLS; 13361618Srie 13372145Srie if (!local || ((flags & FLG_OF_EXEC) == 0)) { 13381618Srie /* 13392145Srie * Assign a GOT entry for static TLS references. 13401618Srie */ 13419131SRod.Evans@Sun.COM if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 13429131SRod.Evans@Sun.COM GOT_REF_TLSIE, ofl, NULL)) == NULL) { 13432145Srie 13442145Srie if (ld_assign_got_TLS(local, rsp, ofl, sdp, 13452145Srie gnp, GOT_REF_TLSIE, FLG_REL_STLS, 13469131SRod.Evans@Sun.COM rtype, R_386_TLS_TPOFF, NULL) == S_ERROR) 13472145Srie return (S_ERROR); 13481618Srie } 13491618Srie 13502145Srie /* 13512145Srie * IE access model. 13522145Srie */ 13532145Srie if (IS_TLS_IE(rtype)) { 13542145Srie if (ld_add_actrel(FLG_REL_STLS, 13552145Srie rsp, ofl) == S_ERROR) 13562145Srie return (S_ERROR); 13572145Srie 13582145Srie /* 13592145Srie * A non-pic shared object needs to adjust the 13602145Srie * active relocation (indntpoff). 13612145Srie */ 13622145Srie if (((flags & FLG_OF_EXEC) == 0) && 13632145Srie (rtype == R_386_TLS_IE)) { 13642145Srie rsp->rel_rtype = R_386_RELATIVE; 13652145Srie return (ld_add_outrel(NULL, rsp, ofl)); 13662145Srie } 13672145Srie return (1); 13682145Srie } 13691618Srie 13701618Srie /* 13712145Srie * Fixups are required for other executable models. 13721618Srie */ 13731618Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 13741618Srie rsp, ofl)); 13751618Srie } 13762145Srie 13771618Srie /* 13782145Srie * LE access model. 13791618Srie */ 13801618Srie if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32)) 13811618Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 13821618Srie 13831618Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 13841618Srie rsp, ofl)); 13851618Srie } 13861618Srie 13871618Srie /* 13882145Srie * Building a shared object. 13892145Srie * 13902145Srie * Assign a GOT entry for a dynamic TLS reference. 13911618Srie */ 13929131SRod.Evans@Sun.COM if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 13939131SRod.Evans@Sun.COM GOT_REF_TLSLD, ofl, NULL)) == NULL)) { 13942145Srie 13952145Srie if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD, 13969131SRod.Evans@Sun.COM FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, NULL) == S_ERROR) 13972145Srie return (S_ERROR); 13982145Srie 13999131SRod.Evans@Sun.COM } else if (IS_TLS_GD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 14009131SRod.Evans@Sun.COM GOT_REF_TLSGD, ofl, NULL)) == NULL)) { 14012145Srie 14022145Srie if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD, 14032145Srie FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32, 14042145Srie R_386_TLS_DTPOFF32) == S_ERROR) 14052145Srie return (S_ERROR); 14061618Srie } 14071618Srie 14081618Srie /* 14091618Srie * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually 14102145Srie * cause a call to __tls_get_addr(). Convert this relocation to that 14112145Srie * symbol now, and prepare for the PLT magic. 14121618Srie */ 14131618Srie if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) { 14143862Srie Sym_desc *tlsgetsym; 14151618Srie 14161618Srie if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU), 14173862Srie ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR) 14181618Srie return (S_ERROR); 14192145Srie 14201618Srie rsp->rel_sym = tlsgetsym; 14211618Srie rsp->rel_sname = tlsgetsym->sd_name; 14221618Srie rsp->rel_rtype = R_386_PLT32; 14232145Srie 14241618Srie if (ld_reloc_plt(rsp, ofl) == S_ERROR) 14251618Srie return (S_ERROR); 14262145Srie 14271618Srie rsp->rel_sym = sdp; 14281618Srie rsp->rel_sname = sdp->sd_name; 14291618Srie rsp->rel_rtype = rtype; 14301618Srie return (1); 14311618Srie } 14321618Srie 14331618Srie if (IS_TLS_LD(rtype)) 14341618Srie return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl)); 14351618Srie 14361618Srie return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl)); 14371618Srie } 14381618Srie 14391618Srie /* ARGSUSED4 */ 14406206Sab196087 static uintptr_t 14419131SRod.Evans@Sun.COM ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl, 14429131SRod.Evans@Sun.COM Rel_desc *rsp, Sym_desc *sdp) 14431618Srie { 14449131SRod.Evans@Sun.COM Gotndx gn, *gnp; 14451618Srie uint_t gotents; 14461618Srie 14471618Srie if (pgnp) 14481618Srie return (1); 14491618Srie 14501618Srie if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD)) 14511618Srie gotents = 2; 14521618Srie else 14531618Srie gotents = 1; 14541618Srie 14559131SRod.Evans@Sun.COM gn.gn_addend = 0; 14569131SRod.Evans@Sun.COM gn.gn_gotndx = ofl->ofl_gotcnt; 14579131SRod.Evans@Sun.COM gn.gn_gotref = gref; 14581618Srie 14591618Srie ofl->ofl_gotcnt += gotents; 14601618Srie 14611618Srie if (gref == GOT_REF_TLSLD) { 14629131SRod.Evans@Sun.COM if (ofl->ofl_tlsldgotndx == NULL) { 14639131SRod.Evans@Sun.COM if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL) 14649131SRod.Evans@Sun.COM return (S_ERROR); 14659131SRod.Evans@Sun.COM (void) memcpy(gnp, &gn, sizeof (Gotndx)); 14669131SRod.Evans@Sun.COM ofl->ofl_tlsldgotndx = gnp; 14679131SRod.Evans@Sun.COM } 14681618Srie return (1); 14691618Srie } 14701618Srie 14719131SRod.Evans@Sun.COM /* 14729131SRod.Evans@Sun.COM * GOT indexes are maintained on an Alist, where there is typically 14739131SRod.Evans@Sun.COM * only one index. The usage of this list is to scan the list to find 14749131SRod.Evans@Sun.COM * an index, and then apply that index immediately to a relocation. 14759131SRod.Evans@Sun.COM * Thus there are no external references to these GOT index structures 14769131SRod.Evans@Sun.COM * that can be compromised by the Alist being reallocated. 14779131SRod.Evans@Sun.COM */ 14789131SRod.Evans@Sun.COM if (alist_append(alpp, &gn, sizeof (Gotndx), AL_CNT_SDP_GOT) == NULL) 14791618Srie return (S_ERROR); 14801618Srie 14811618Srie return (1); 14821618Srie } 14831618Srie 14846206Sab196087 static void 14851618Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl) 14861618Srie { 14871618Srie sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++; 14881618Srie sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++; 14891618Srie ofl->ofl_flags |= FLG_OF_BLDGOT; 14901618Srie } 14911618Srie 14921618Srie /* 14931618Srie * Initializes .got[0] with the _DYNAMIC symbol value. 14941618Srie */ 14956206Sab196087 static uintptr_t 14962145Srie ld_fillin_gotplt(Ofl_desc *ofl) 14971618Srie { 14986299Sab196087 ofl_flag_t flags = ofl->ofl_flags; 14996299Sab196087 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 15002145Srie 15011618Srie if (ofl->ofl_osgot) { 15022145Srie Sym_desc *sdp; 15031618Srie 15041618Srie if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U), 150510792SRod.Evans@Sun.COM SYM_NOHASH, NULL, ofl)) != NULL) { 15062145Srie uchar_t *genptr; 15072145Srie 15082145Srie genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf + 15091618Srie (M_GOT_XDYNAMIC * M_GOT_ENTSIZE)); 15101618Srie /* LINTED */ 15111618Srie *(Word *)genptr = (Word)sdp->sd_sym->st_value; 15126206Sab196087 if (bswap) 15136206Sab196087 /* LINTED */ 15146206Sab196087 *(Word *)genptr = 15156206Sab196087 /* LINTED */ 15166206Sab196087 ld_bswap_Word(*(Word *)genptr); 15171618Srie } 15181618Srie } 15191618Srie 15201618Srie /* 15211618Srie * Fill in the reserved slot in the procedure linkage table the first 15221618Srie * entry is: 15231618Srie * if (building a.out) { 15241618Srie * PUSHL got[1] # the address of the link map entry 15251618Srie * JMP * got[2] # the address of rtbinder 15261618Srie * } else { 15271618Srie * PUSHL got[1]@GOT(%ebx) # the address of the link map entry 15281618Srie * JMP * got[2]@GOT(%ebx) # the address of rtbinder 15291618Srie * } 15301618Srie */ 15312145Srie if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) { 15321618Srie uchar_t *pltent; 15331618Srie 15341618Srie pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf; 15352145Srie if (!(flags & FLG_OF_SHAROBJ)) { 15361618Srie pltent[0] = M_SPECIAL_INST; 15371618Srie pltent[1] = M_PUSHL_DISP; 15381618Srie pltent += 2; 15391618Srie /* LINTED */ 15401618Srie *(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr-> 15414734Sab196087 sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE); 15426206Sab196087 if (bswap) 15436206Sab196087 /* LINTED */ 15446206Sab196087 *(Word *)pltent = 15456206Sab196087 /* LINTED */ 15466206Sab196087 ld_bswap_Word(*(Word *)pltent); 15471618Srie pltent += 4; 15481618Srie pltent[0] = M_SPECIAL_INST; 15491618Srie pltent[1] = M_JMP_DISP_IND; 15501618Srie pltent += 2; 15511618Srie /* LINTED */ 15521618Srie *(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr-> 15534734Sab196087 sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE); 15546206Sab196087 if (bswap) 15556206Sab196087 /* LINTED */ 15566206Sab196087 *(Word *)pltent = 15576206Sab196087 /* LINTED */ 15586206Sab196087 ld_bswap_Word(*(Word *)pltent); 15591618Srie } else { 15601618Srie pltent[0] = M_SPECIAL_INST; 15611618Srie pltent[1] = M_PUSHL_REG_DISP; 15621618Srie pltent += 2; 15631618Srie /* LINTED */ 15641618Srie *(Word *)pltent = (Word)(M_GOT_XLINKMAP * 15654734Sab196087 M_GOT_ENTSIZE); 15666206Sab196087 if (bswap) 15676206Sab196087 /* LINTED */ 15686206Sab196087 *(Word *)pltent = 15696206Sab196087 /* LINTED */ 15706206Sab196087 ld_bswap_Word(*(Word *)pltent); 15711618Srie pltent += 4; 15721618Srie pltent[0] = M_SPECIAL_INST; 15731618Srie pltent[1] = M_JMP_REG_DISP_IND; 15741618Srie pltent += 2; 15751618Srie /* LINTED */ 15761618Srie *(Word *)pltent = (Word)(M_GOT_XRTLD * 15774734Sab196087 M_GOT_ENTSIZE); 15786206Sab196087 if (bswap) 15796206Sab196087 /* LINTED */ 15806206Sab196087 *(Word *)pltent = 15816206Sab196087 /* LINTED */ 15826206Sab196087 ld_bswap_Word(*(Word *)pltent); 15831618Srie } 15841618Srie } 15851618Srie return (1); 15861618Srie } 15876206Sab196087 15886206Sab196087 15896206Sab196087 15906206Sab196087 /* 15916206Sab196087 * Template for generating "void (*)(void)" function 15926206Sab196087 */ 15936206Sab196087 static const uchar_t nullfunc_tmpl[] = { /* IA32 */ 15946206Sab196087 /* 0x00 */ 0xc3 /* ret */ 15956206Sab196087 }; 15966206Sab196087 15976206Sab196087 15986206Sab196087 15996206Sab196087 /* 1600*10809SAli.Bahrami@Sun.COM * Function used to provide fill padding in SHF_EXECINSTR sections 1601*10809SAli.Bahrami@Sun.COM * 1602*10809SAli.Bahrami@Sun.COM * entry: 1603*10809SAli.Bahrami@Sun.COM * 1604*10809SAli.Bahrami@Sun.COM * base - base address of section being filled 1605*10809SAli.Bahrami@Sun.COM * offset - starting offset for fill within memory referenced by base 1606*10809SAli.Bahrami@Sun.COM * cnt - # bytes to be filled 1607*10809SAli.Bahrami@Sun.COM * 1608*10809SAli.Bahrami@Sun.COM * exit: 1609*10809SAli.Bahrami@Sun.COM * The fill has been completed. 1610*10809SAli.Bahrami@Sun.COM */ 1611*10809SAli.Bahrami@Sun.COM static void 1612*10809SAli.Bahrami@Sun.COM execfill(void *base, off_t off, size_t cnt) 1613*10809SAli.Bahrami@Sun.COM { 1614*10809SAli.Bahrami@Sun.COM /* 1615*10809SAli.Bahrami@Sun.COM * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds. 1616*10809SAli.Bahrami@Sun.COM * There are no alignment constraints. 1617*10809SAli.Bahrami@Sun.COM */ 1618*10809SAli.Bahrami@Sun.COM (void) memset(off + (char *)base, 0x90, cnt); 1619*10809SAli.Bahrami@Sun.COM } 1620*10809SAli.Bahrami@Sun.COM 1621*10809SAli.Bahrami@Sun.COM 1622*10809SAli.Bahrami@Sun.COM /* 16236206Sab196087 * Return the ld_targ definition for this target. 16246206Sab196087 */ 16256206Sab196087 const Target * 16266206Sab196087 ld_targ_init_x86(void) 16276206Sab196087 { 16286206Sab196087 static const Target _ld_targ = { 16296206Sab196087 { /* Target_mach */ 16306206Sab196087 M_MACH, /* m_mach */ 16316206Sab196087 M_MACHPLUS, /* m_machplus */ 16326206Sab196087 M_FLAGSPLUS, /* m_flagsplus */ 16336206Sab196087 M_CLASS, /* m_class */ 16346206Sab196087 M_DATA, /* m_data */ 16356206Sab196087 16366206Sab196087 M_SEGM_ALIGN, /* m_segm_align */ 16376206Sab196087 M_SEGM_ORIGIN, /* m_segm_origin */ 16388501SRod.Evans@Sun.COM M_SEGM_AORIGIN, /* m_segm_aorigin */ 16396206Sab196087 M_DATASEG_PERM, /* m_dataseg_perm */ 16406206Sab196087 M_WORD_ALIGN, /* m_word_align */ 16416206Sab196087 MSG_ORIG(MSG_PTH_RTLD), /* m_def_interp */ 16426206Sab196087 16436206Sab196087 /* Relocation type codes */ 16446206Sab196087 M_R_ARRAYADDR, /* m_r_arrayaddr */ 16456206Sab196087 M_R_COPY, /* m_r_copy */ 16466206Sab196087 M_R_GLOB_DAT, /* m_r_glob_dat */ 16476206Sab196087 M_R_JMP_SLOT, /* m_r_jmp_slot */ 16486206Sab196087 M_R_NUM, /* m_r_num */ 16496206Sab196087 M_R_NONE, /* m_r_none */ 16506206Sab196087 M_R_RELATIVE, /* m_r_relative */ 16516206Sab196087 M_R_REGISTER, /* m_r_register */ 16526206Sab196087 16536206Sab196087 /* Relocation related constants */ 16546206Sab196087 M_REL_DT_COUNT, /* m_rel_dt_count */ 16556206Sab196087 M_REL_DT_ENT, /* m_rel_dt_ent */ 16566206Sab196087 M_REL_DT_SIZE, /* m_rel_dt_size */ 16576206Sab196087 M_REL_DT_TYPE, /* m_rel_dt_type */ 16586206Sab196087 M_REL_SHT_TYPE, /* m_rel_sht_type */ 16596206Sab196087 16606206Sab196087 /* GOT related constants */ 16616206Sab196087 M_GOT_ENTSIZE, /* m_got_entsize */ 16626206Sab196087 M_GOT_XNumber, /* m_got_xnumber */ 16636206Sab196087 16646206Sab196087 /* PLT related constants */ 16656206Sab196087 M_PLT_ALIGN, /* m_plt_align */ 16666206Sab196087 M_PLT_ENTSIZE, /* m_plt_entsize */ 16676206Sab196087 M_PLT_RESERVSZ, /* m_plt_reservsz */ 16686206Sab196087 M_PLT_SHF_FLAGS, /* m_plt_shf_flags */ 16696206Sab196087 16709085SAli.Bahrami@Sun.COM /* Section type of .eh_frame/.eh_frame_hdr sections */ 16719085SAli.Bahrami@Sun.COM SHT_PROGBITS, /* m_sht_unwind */ 16729085SAli.Bahrami@Sun.COM 16736206Sab196087 M_DT_REGISTER, /* m_dt_register */ 16746206Sab196087 }, 16756206Sab196087 { /* Target_machid */ 16766206Sab196087 M_ID_ARRAY, /* id_array */ 16776206Sab196087 M_ID_BSS, /* id_bss */ 16786206Sab196087 M_ID_CAP, /* id_cap */ 16796206Sab196087 M_ID_DATA, /* id_data */ 16806206Sab196087 M_ID_DYNAMIC, /* id_dynamic */ 16816206Sab196087 M_ID_DYNSORT, /* id_dynsort */ 16826206Sab196087 M_ID_DYNSTR, /* id_dynstr */ 16836206Sab196087 M_ID_DYNSYM, /* id_dynsym */ 16846206Sab196087 M_ID_DYNSYM_NDX, /* id_dynsym_ndx */ 16856206Sab196087 M_ID_GOT, /* id_got */ 16866206Sab196087 M_ID_UNKNOWN, /* id_gotdata (unused) */ 16876206Sab196087 M_ID_HASH, /* id_hash */ 16886206Sab196087 M_ID_INTERP, /* id_interp */ 16896206Sab196087 M_ID_LBSS, /* id_lbss */ 16906206Sab196087 M_ID_LDYNSYM, /* id_ldynsym */ 16916206Sab196087 M_ID_NOTE, /* id_note */ 16926206Sab196087 M_ID_NULL, /* id_null */ 16936206Sab196087 M_ID_PLT, /* id_plt */ 16946206Sab196087 M_ID_REL, /* id_rel */ 16956206Sab196087 M_ID_STRTAB, /* id_strtab */ 16966206Sab196087 M_ID_SYMINFO, /* id_syminfo */ 16976206Sab196087 M_ID_SYMTAB, /* id_symtab */ 16986206Sab196087 M_ID_SYMTAB_NDX, /* id_symtab_ndx */ 16996206Sab196087 M_ID_TEXT, /* id_text */ 17006206Sab196087 M_ID_TLS, /* id_tls */ 17016206Sab196087 M_ID_TLSBSS, /* id_tlsbss */ 17026206Sab196087 M_ID_UNKNOWN, /* id_unknown */ 17036206Sab196087 M_ID_UNWIND, /* id_unwind */ 17049085SAli.Bahrami@Sun.COM M_ID_UNWINDHDR, /* id_unwindhdr */ 17056206Sab196087 M_ID_USER, /* id_user */ 17066206Sab196087 M_ID_VERSION, /* id_version */ 17076206Sab196087 }, 17086206Sab196087 { /* Target_nullfunc */ 17096206Sab196087 nullfunc_tmpl, /* nf_template */ 17106206Sab196087 sizeof (nullfunc_tmpl), /* nf_size */ 17116206Sab196087 }, 1712*10809SAli.Bahrami@Sun.COM { /* Target_fillfunc */ 1713*10809SAli.Bahrami@Sun.COM execfill /* ff_execfill */ 1714*10809SAli.Bahrami@Sun.COM }, 17156206Sab196087 { /* Target_machrel */ 17166206Sab196087 reloc_table, 17176206Sab196087 17186206Sab196087 ld_init_rel, /* mr_init_rel */ 17196206Sab196087 ld_mach_eflags, /* mr_mach_eflags */ 17206206Sab196087 ld_mach_make_dynamic, /* mr_mach_make_dynamic */ 17216206Sab196087 ld_mach_update_odynamic, /* mr_mach_update_odynamic */ 17226206Sab196087 ld_calc_plt_addr, /* mr_calc_plt_addr */ 17236206Sab196087 ld_perform_outreloc, /* mr_perform_outreloc */ 17246206Sab196087 ld_do_activerelocs, /* mr_do_activerelocs */ 17256206Sab196087 ld_add_outrel, /* mr_add_outrel */ 17266206Sab196087 NULL, /* mr_reloc_register */ 17276206Sab196087 ld_reloc_local, /* mr_reloc_local */ 17286206Sab196087 NULL, /* mr_reloc_GOTOP */ 17296206Sab196087 ld_reloc_TLS, /* mr_reloc_TLS */ 17306206Sab196087 NULL, /* mr_assign_got */ 17319131SRod.Evans@Sun.COM ld_find_got_ndx, /* mr_find_got_ndx */ 17326206Sab196087 ld_calc_got_offset, /* mr_calc_got_offset */ 17336206Sab196087 ld_assign_got_ndx, /* mr_assign_got_ndx */ 17346206Sab196087 ld_assign_plt_ndx, /* mr_assign_plt_ndx */ 17356206Sab196087 NULL, /* mr_allocate_got */ 17366206Sab196087 ld_fillin_gotplt, /* mr_fillin_gotplt */ 17376206Sab196087 }, 17386206Sab196087 { /* Target_machsym */ 17396206Sab196087 NULL, /* ms_reg_check */ 17406206Sab196087 NULL, /* ms_mach_sym_typecheck */ 17416206Sab196087 NULL, /* ms_is_regsym */ 17426206Sab196087 NULL, /* ms_reg_find */ 17436206Sab196087 NULL /* ms_reg_enter */ 17446206Sab196087 } 17456206Sab196087 }; 17466206Sab196087 17476206Sab196087 return (&_ld_targ); 17486206Sab196087 } 1749