17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55aefb655Srie * Common Development and Distribution License (the "License").
65aefb655Srie * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21141040e8Srie
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.
247c478bd9Sstevel@tonic-gate * Copyright (c) 1988 AT&T
257c478bd9Sstevel@tonic-gate * All Rights Reserved
267c478bd9Sstevel@tonic-gate *
27bf994817SAli Bahrami * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
30ba2be530Sab /* Get the x86 version of the relocation engine */
31ba2be530Sab #define DO_RELOC_LIBLD_X86
32ba2be530Sab
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <sys/elf_386.h>
365aefb655Srie #include <debug.h>
375aefb655Srie #include <reloc.h>
38ba2be530Sab #include <i386/machdep_x86.h>
397c478bd9Sstevel@tonic-gate #include "msg.h"
407c478bd9Sstevel@tonic-gate #include "_libld.h"
417c478bd9Sstevel@tonic-gate
4257ef7aa9SRod Evans /*
4357ef7aa9SRod Evans * Search the GOT index list for a GOT entry with a matching reference.
4457ef7aa9SRod Evans */
4557ef7aa9SRod Evans /* ARGSUSED3 */
4657ef7aa9SRod Evans static Gotndx *
ld_find_got_ndx(Alist * alp,Gotref gref,Ofl_desc * ofl,Rel_desc * rdesc)4757ef7aa9SRod Evans ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
4857ef7aa9SRod Evans {
4957ef7aa9SRod Evans Aliste idx;
5057ef7aa9SRod Evans Gotndx *gnp;
5157ef7aa9SRod Evans
5257ef7aa9SRod Evans if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
5357ef7aa9SRod Evans return (ofl->ofl_tlsldgotndx);
5457ef7aa9SRod Evans
5557ef7aa9SRod Evans for (ALIST_TRAVERSE(alp, idx, gnp)) {
5657ef7aa9SRod Evans if (gnp->gn_gotref == gref)
5757ef7aa9SRod Evans return (gnp);
5857ef7aa9SRod Evans }
5957ef7aa9SRod Evans return (NULL);
6057ef7aa9SRod Evans }
6157ef7aa9SRod Evans
6257ef7aa9SRod Evans static Xword
ld_calc_got_offset(Rel_desc * rdesc,Ofl_desc * ofl)6357ef7aa9SRod Evans ld_calc_got_offset(Rel_desc *rdesc, Ofl_desc *ofl)
6457ef7aa9SRod Evans {
6557ef7aa9SRod Evans Os_desc *osp = ofl->ofl_osgot;
6657ef7aa9SRod Evans Sym_desc *sdp = rdesc->rel_sym;
6757ef7aa9SRod Evans Xword gotndx;
6857ef7aa9SRod Evans Gotref gref;
6957ef7aa9SRod Evans Gotndx *gnp;
7057ef7aa9SRod Evans
7157ef7aa9SRod Evans if (rdesc->rel_flags & FLG_REL_DTLS)
7257ef7aa9SRod Evans gref = GOT_REF_TLSGD;
7357ef7aa9SRod Evans else if (rdesc->rel_flags & FLG_REL_MTLS)
7457ef7aa9SRod Evans gref = GOT_REF_TLSLD;
7557ef7aa9SRod Evans else if (rdesc->rel_flags & FLG_REL_STLS)
7657ef7aa9SRod Evans gref = GOT_REF_TLSIE;
7757ef7aa9SRod Evans else
7857ef7aa9SRod Evans gref = GOT_REF_GENERIC;
79ba2be530Sab
8057ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
8157ef7aa9SRod Evans assert(gnp);
8257ef7aa9SRod Evans
8357ef7aa9SRod Evans gotndx = (Xword)gnp->gn_gotndx;
8457ef7aa9SRod Evans
8557ef7aa9SRod Evans if ((rdesc->rel_flags & FLG_REL_DTLS) &&
8657ef7aa9SRod Evans (rdesc->rel_rtype == R_386_TLS_DTPOFF32))
8757ef7aa9SRod Evans gotndx++;
8857ef7aa9SRod Evans
8957ef7aa9SRod Evans return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE)));
9057ef7aa9SRod Evans }
91ba2be530Sab
92ba2be530Sab static Word
ld_init_rel(Rel_desc * reld,Word * typedata,void * reloc)93bf994817SAli Bahrami ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
947c478bd9Sstevel@tonic-gate {
9557ef7aa9SRod Evans Rel *rel = (Rel *)reloc;
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate /* LINTED */
98ba2be530Sab reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH);
997c478bd9Sstevel@tonic-gate reld->rel_roffset = rel->r_offset;
1007c478bd9Sstevel@tonic-gate reld->rel_raddend = 0;
101bf994817SAli Bahrami *typedata = 0;
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate return ((Word)ELF_R_SYM(rel->r_info));
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
106ba2be530Sab static void
ld_mach_eflags(Ehdr * ehdr,Ofl_desc * ofl)1075aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
1087c478bd9Sstevel@tonic-gate {
1095aefb655Srie ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate
112ba2be530Sab static void
ld_mach_make_dynamic(Ofl_desc * ofl,size_t * cnt)1135aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate * Create this entry if we are going to create a PLT table.
1187c478bd9Sstevel@tonic-gate */
1197c478bd9Sstevel@tonic-gate if (ofl->ofl_pltcnt)
1207c478bd9Sstevel@tonic-gate (*cnt)++; /* DT_PLTGOT */
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate
124ba2be530Sab static void
ld_mach_update_odynamic(Ofl_desc * ofl,Dyn ** dyn)125d326b23bSrie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
1267c478bd9Sstevel@tonic-gate {
1275aefb655Srie if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
1285aefb655Srie (*dyn)->d_tag = DT_PLTGOT;
1295aefb655Srie if (ofl->ofl_osgot)
1305aefb655Srie (*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr;
1315aefb655Srie else
1325aefb655Srie (*dyn)->d_un.d_ptr = 0;
1335aefb655Srie (*dyn)++;
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
137ba2be530Sab static Xword
ld_calc_plt_addr(Sym_desc * sdp,Ofl_desc * ofl)1385aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate Xword value;
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
1437c478bd9Sstevel@tonic-gate M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE);
1447c478bd9Sstevel@tonic-gate return (value);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate * Build a single plt entry - code is:
1497c478bd9Sstevel@tonic-gate * if (building a.out)
1507c478bd9Sstevel@tonic-gate * JMP *got_off
1517c478bd9Sstevel@tonic-gate * else
1527c478bd9Sstevel@tonic-gate * JMP *got_off@GOT(%ebx)
1537c478bd9Sstevel@tonic-gate * PUSHL &rel_off
1547c478bd9Sstevel@tonic-gate * JMP -n(%pc) # -n is pcrel offset to first plt entry
1557c478bd9Sstevel@tonic-gate *
1567c478bd9Sstevel@tonic-gate * The got_off@GOT entry gets filled with the address of the PUSHL,
1577c478bd9Sstevel@tonic-gate * so the first pass through the plt jumps back here, jumping
1587c478bd9Sstevel@tonic-gate * in turn to the first plt entry, which jumps to the dynamic
1597c478bd9Sstevel@tonic-gate * linker. The dynamic linker then patches the GOT, rerouting
1607c478bd9Sstevel@tonic-gate * future plt calls to the proper destination.
1617c478bd9Sstevel@tonic-gate */
1627c478bd9Sstevel@tonic-gate static void
plt_entry(Ofl_desc * ofl,Word rel_off,Sym_desc * sdp)1637c478bd9Sstevel@tonic-gate plt_entry(Ofl_desc * ofl, Word rel_off, Sym_desc * sdp)
1647c478bd9Sstevel@tonic-gate {
165b3fbe5e6Sseizo uchar_t *pltent, *gotent;
1667c478bd9Sstevel@tonic-gate Sword plt_off;
1677c478bd9Sstevel@tonic-gate Word got_off;
168ba2be530Sab int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
1717c478bd9Sstevel@tonic-gate plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) *
1727c478bd9Sstevel@tonic-gate M_PLT_ENTSIZE);
173b3fbe5e6Sseizo pltent = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf) + plt_off;
174b3fbe5e6Sseizo gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off;
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate * Fill in the got entry with the address of the next instruction.
1787c478bd9Sstevel@tonic-gate */
1797c478bd9Sstevel@tonic-gate /* LINTED */
1807c478bd9Sstevel@tonic-gate *(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off +
181b3fbe5e6Sseizo M_PLT_INSSIZE;
182ba2be530Sab if (bswap)
183ba2be530Sab /* LINTED */
184ba2be530Sab *(Word *)gotent = ld_bswap_Word(*(Word *)gotent);
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate if (!(ofl->ofl_flags & FLG_OF_SHAROBJ)) {
1877c478bd9Sstevel@tonic-gate pltent[0] = M_SPECIAL_INST;
1887c478bd9Sstevel@tonic-gate pltent[1] = M_JMP_DISP_IND;
1897c478bd9Sstevel@tonic-gate pltent += 2;
1907c478bd9Sstevel@tonic-gate /* LINTED */
1917c478bd9Sstevel@tonic-gate *(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr->sh_addr +
192de777a60Sab got_off);
1937c478bd9Sstevel@tonic-gate } else {
1947c478bd9Sstevel@tonic-gate pltent[0] = M_SPECIAL_INST;
1957c478bd9Sstevel@tonic-gate pltent[1] = M_JMP_REG_DISP_IND;
1967c478bd9Sstevel@tonic-gate pltent += 2;
1977c478bd9Sstevel@tonic-gate /* LINTED */
1987c478bd9Sstevel@tonic-gate *(Word *)pltent = (Word)got_off;
1997c478bd9Sstevel@tonic-gate }
200ba2be530Sab if (bswap)
201ba2be530Sab /* LINTED */
202ba2be530Sab *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2037c478bd9Sstevel@tonic-gate pltent += 4;
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate pltent[0] = M_INST_PUSHL;
2067c478bd9Sstevel@tonic-gate pltent++;
2077c478bd9Sstevel@tonic-gate /* LINTED */
2087c478bd9Sstevel@tonic-gate *(Word *)pltent = (Word)rel_off;
209ba2be530Sab if (bswap)
210ba2be530Sab /* LINTED */
211ba2be530Sab *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2127c478bd9Sstevel@tonic-gate pltent += 4;
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate plt_off = -(plt_off + 16); /* JMP, PUSHL, JMP take 16 bytes */
2157c478bd9Sstevel@tonic-gate pltent[0] = M_INST_JMP;
2167c478bd9Sstevel@tonic-gate pltent++;
2177c478bd9Sstevel@tonic-gate /* LINTED */
2187c478bd9Sstevel@tonic-gate *(Word *)pltent = (Word)plt_off;
219ba2be530Sab if (bswap)
220ba2be530Sab /* LINTED */
221ba2be530Sab *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
224ba2be530Sab static uintptr_t
ld_perform_outreloc(Rel_desc * orsp,Ofl_desc * ofl,Boolean * remain_seen)2251007fd6fSAli Bahrami ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl, Boolean *remain_seen)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate Os_desc * relosp, * osp = 0;
2287c478bd9Sstevel@tonic-gate Word ndx, roffset, value;
2297c478bd9Sstevel@tonic-gate Rel rea;
2307c478bd9Sstevel@tonic-gate char *relbits;
2317c478bd9Sstevel@tonic-gate Sym_desc * sdp, * psym = (Sym_desc *)0;
2327c478bd9Sstevel@tonic-gate int sectmoved = 0;
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate sdp = orsp->rel_sym;
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate * If the section this relocation is against has been discarded
2387c478bd9Sstevel@tonic-gate * (-zignore), then also discard (skip) the relocation itself.
2397c478bd9Sstevel@tonic-gate */
2407c478bd9Sstevel@tonic-gate if (orsp->rel_isdesc && ((orsp->rel_flags &
2417c478bd9Sstevel@tonic-gate (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
2427c478bd9Sstevel@tonic-gate (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
2435aefb655Srie DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
2447c478bd9Sstevel@tonic-gate return (1);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate * If this is a relocation against a move table, or expanded move
2497c478bd9Sstevel@tonic-gate * table, adjust the relocation entries.
2507c478bd9Sstevel@tonic-gate */
251bf994817SAli Bahrami if (RELAUX_GET_MOVE(orsp))
2525aefb655Srie ld_adj_movereloc(ofl, orsp);
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate * If this is a relocation against a section using a partial initialized
2567c478bd9Sstevel@tonic-gate * symbol, adjust the embedded symbol info.
2577c478bd9Sstevel@tonic-gate *
2587c478bd9Sstevel@tonic-gate * The second argument of the am_I_partial() is the value stored at the
2597c478bd9Sstevel@tonic-gate * target address relocation is going to be applied.
2607c478bd9Sstevel@tonic-gate */
2617c478bd9Sstevel@tonic-gate if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
26257ef7aa9SRod Evans if (ofl->ofl_parsyms &&
2637c478bd9Sstevel@tonic-gate (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
2647c478bd9Sstevel@tonic-gate /* LINTED */
2655aefb655Srie (psym = ld_am_I_partial(orsp, *(Xword *)
266b3fbe5e6Sseizo ((uchar_t *)(orsp->rel_isdesc->is_indata->d_buf) +
2677c478bd9Sstevel@tonic-gate orsp->rel_roffset)))) {
2685aefb655Srie DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
2697c478bd9Sstevel@tonic-gate sectmoved = 1;
270de777a60Sab }
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate value = sdp->sd_sym->st_value;
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate if (orsp->rel_flags & FLG_REL_GOT) {
2767c478bd9Sstevel@tonic-gate osp = ofl->ofl_osgot;
2775aefb655Srie roffset = (Word)ld_calc_got_offset(orsp, ofl);
2785aefb655Srie
2797c478bd9Sstevel@tonic-gate } else if (orsp->rel_flags & FLG_REL_PLT) {
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate * Note that relocations for PLT's actually
2827c478bd9Sstevel@tonic-gate * cause a relocation againt the GOT.
2837c478bd9Sstevel@tonic-gate */
2847c478bd9Sstevel@tonic-gate osp = ofl->ofl_osplt;
2857c478bd9Sstevel@tonic-gate roffset = (Word) (ofl->ofl_osgot->os_shdr->sh_addr) +
2867c478bd9Sstevel@tonic-gate sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE;
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate plt_entry(ofl, osp->os_relosdesc->os_szoutrels, sdp);
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate } else if (orsp->rel_flags & FLG_REL_BSS) {
2917c478bd9Sstevel@tonic-gate /*
2927c478bd9Sstevel@tonic-gate * This must be a R_386_COPY. For these set the roffset to
2937c478bd9Sstevel@tonic-gate * point to the new symbols location.
2947c478bd9Sstevel@tonic-gate */
2957c478bd9Sstevel@tonic-gate osp = ofl->ofl_isbss->is_osdesc;
2967c478bd9Sstevel@tonic-gate roffset = (Word)value;
2977c478bd9Sstevel@tonic-gate } else {
298bf994817SAli Bahrami osp = RELAUX_GET_OSDESC(orsp);
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate * Calculate virtual offset of reference point; equals offset
3027c478bd9Sstevel@tonic-gate * into section + vaddr of section for loadable sections, or
3037c478bd9Sstevel@tonic-gate * offset plus section displacement for nonloadable sections.
3047c478bd9Sstevel@tonic-gate */
3057c478bd9Sstevel@tonic-gate roffset = orsp->rel_roffset +
3067c478bd9Sstevel@tonic-gate (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
3077c478bd9Sstevel@tonic-gate if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
3087c478bd9Sstevel@tonic-gate roffset += orsp->rel_isdesc->is_osdesc->
3097c478bd9Sstevel@tonic-gate os_shdr->sh_addr;
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
3137c478bd9Sstevel@tonic-gate relosp = ofl->ofl_osrel;
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate /*
3167c478bd9Sstevel@tonic-gate * Assign the symbols index for the output relocation. If the
3177c478bd9Sstevel@tonic-gate * relocation refers to a SECTION symbol then it's index is based upon
3187c478bd9Sstevel@tonic-gate * the output sections symbols index. Otherwise the index can be
3197c478bd9Sstevel@tonic-gate * derived from the symbols index itself.
3207c478bd9Sstevel@tonic-gate */
3217c478bd9Sstevel@tonic-gate if (orsp->rel_rtype == R_386_RELATIVE)
3227c478bd9Sstevel@tonic-gate ndx = STN_UNDEF;
3237c478bd9Sstevel@tonic-gate else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
3247c478bd9Sstevel@tonic-gate (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
3257c478bd9Sstevel@tonic-gate if (sectmoved == 0) {
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate * Check for a null input section. This can
3287c478bd9Sstevel@tonic-gate * occur if this relocation references a symbol
3297c478bd9Sstevel@tonic-gate * generated by sym_add_sym().
3307c478bd9Sstevel@tonic-gate */
33157ef7aa9SRod Evans if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
33257ef7aa9SRod Evans ndx = sdp->sd_isc->is_osdesc->os_identndx;
3337c478bd9Sstevel@tonic-gate else
3347c478bd9Sstevel@tonic-gate ndx = sdp->sd_shndx;
3357c478bd9Sstevel@tonic-gate } else
33635450702SAli Bahrami ndx = ofl->ofl_parexpnndx;
3377c478bd9Sstevel@tonic-gate } else
3387c478bd9Sstevel@tonic-gate ndx = sdp->sd_symndx;
3397c478bd9Sstevel@tonic-gate
340cce0e03bSab /*
341cce0e03bSab * If we have a replacement value for the relocation
342cce0e03bSab * target, put it in place now.
343cce0e03bSab */
344cce0e03bSab if (orsp->rel_flags & FLG_REL_NADDEND) {
345cce0e03bSab Xword addend = orsp->rel_raddend;
346cce0e03bSab uchar_t *addr;
347cce0e03bSab
348cce0e03bSab /*
349cce0e03bSab * Get the address of the data item we need to modify.
350cce0e03bSab */
351cce0e03bSab addr = (uchar_t *)((uintptr_t)orsp->rel_roffset +
352cce0e03bSab (uintptr_t)_elf_getxoff(orsp->rel_isdesc->is_indata));
353bf994817SAli Bahrami addr += (uintptr_t)RELAUX_GET_OSDESC(orsp)->os_outdata->d_buf;
354cce0e03bSab if (ld_reloc_targval_set(ofl, orsp, addr, addend) == 0)
355cce0e03bSab return (S_ERROR);
356cce0e03bSab }
357cce0e03bSab
3580bc0887eSRichard Lowe if ((orsp->rel_rtype != M_R_NONE) &&
3590bc0887eSRichard Lowe (orsp->rel_rtype != M_R_RELATIVE)) {
3600bc0887eSRichard Lowe if (ndx == 0) {
3610bc0887eSRichard Lowe Conv_inv_buf_t inv_buf;
3620bc0887eSRichard Lowe Is_desc *isp = orsp->rel_isdesc;
3630bc0887eSRichard Lowe
3640bc0887eSRichard Lowe ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NOSYMBOL),
3650bc0887eSRichard Lowe conv_reloc_type(ofl->ofl_nehdr->e_machine,
3660bc0887eSRichard Lowe orsp->rel_rtype, 0, &inv_buf),
3670bc0887eSRichard Lowe isp->is_file->ifl_name, EC_WORD(isp->is_scnndx),
3680bc0887eSRichard Lowe isp->is_name, EC_XWORD(roffset));
3690bc0887eSRichard Lowe return (S_ERROR);
3700bc0887eSRichard Lowe }
3710bc0887eSRichard Lowe }
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype);
3747c478bd9Sstevel@tonic-gate rea.r_offset = roffset;
3755aefb655Srie DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_REL, &rea, relosp->os_name,
376bf994817SAli Bahrami ld_reloc_sym_name(orsp)));
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate * Assert we haven't walked off the end of our relocation table.
3807c478bd9Sstevel@tonic-gate */
3817c478bd9Sstevel@tonic-gate assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
3827c478bd9Sstevel@tonic-gate
3830bc0887eSRichard Lowe relbits = (char *)relosp->os_outdata->d_buf;
3840bc0887eSRichard Lowe
3857c478bd9Sstevel@tonic-gate (void) memcpy((relbits + relosp->os_szoutrels),
3867c478bd9Sstevel@tonic-gate (char *)&rea, sizeof (Rel));
3877c478bd9Sstevel@tonic-gate relosp->os_szoutrels += sizeof (Rel);
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate * Determine if this relocation is against a non-writable, allocatable
3917c478bd9Sstevel@tonic-gate * section. If so we may need to provide a text relocation diagnostic.
3927c478bd9Sstevel@tonic-gate * Note that relocations against the .plt (R_386_JMP_SLOT) actually
3937c478bd9Sstevel@tonic-gate * result in modifications to the .got.
3947c478bd9Sstevel@tonic-gate */
3957c478bd9Sstevel@tonic-gate if (orsp->rel_rtype == R_386_JMP_SLOT)
3967c478bd9Sstevel@tonic-gate osp = ofl->ofl_osgot;
3977c478bd9Sstevel@tonic-gate
3981007fd6fSAli Bahrami ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
3997c478bd9Sstevel@tonic-gate return (1);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate * i386 Instructions for TLS processing
4047c478bd9Sstevel@tonic-gate */
405b3fbe5e6Sseizo static uchar_t tlsinstr_gd_ie[] = {
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate * 0x00 movl %gs:0x0, %eax
4087c478bd9Sstevel@tonic-gate */
4097c478bd9Sstevel@tonic-gate 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate * 0x06 addl x(%eax), %eax
4127c478bd9Sstevel@tonic-gate * 0x0c ...
4137c478bd9Sstevel@tonic-gate */
4147c478bd9Sstevel@tonic-gate 0x03, 0x80, 0x00, 0x00, 0x00, 0x00
4157c478bd9Sstevel@tonic-gate };
4167c478bd9Sstevel@tonic-gate
417b3fbe5e6Sseizo static uchar_t tlsinstr_gd_le[] = {
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate * 0x00 movl %gs:0x0, %eax
4207c478bd9Sstevel@tonic-gate */
4217c478bd9Sstevel@tonic-gate 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate * 0x06 addl $0x0, %eax
4247c478bd9Sstevel@tonic-gate */
4257c478bd9Sstevel@tonic-gate 0x05, 0x00, 0x00, 0x00, 0x00,
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate * 0x0b nop
4287c478bd9Sstevel@tonic-gate * 0x0c
4297c478bd9Sstevel@tonic-gate */
4307c478bd9Sstevel@tonic-gate 0x90
4317c478bd9Sstevel@tonic-gate };
4327c478bd9Sstevel@tonic-gate
433096c97d6SRichard Lowe static uchar_t tlsinstr_ld_le_movgs[] = {
4347c478bd9Sstevel@tonic-gate /*
435096c97d6SRichard Lowe * 0x00 movl %gs:0x0,%eax
4367c478bd9Sstevel@tonic-gate */
437096c97d6SRichard Lowe 0x65, 0xa1, 0x00, 0x00, 0x00, 0x00,
438096c97d6SRichard Lowe };
439096c97d6SRichard Lowe
440096c97d6SRichard Lowe /*
441096c97d6SRichard Lowe * 0x00 nopl 0(%eax,%eax) -- the intel recommended 5-byte nop
442096c97d6SRichard Lowe * See Intel® 64 and IA-32 Architectures Software Developer’s Manual
443096c97d6SRichard Lowe * Volume 2B: Instruction Set Reference, M-U
444096c97d6SRichard Lowe * Table 4-12, Recommended Multi-Byte Sequence of NOP Instruction
445096c97d6SRichard Lowe */
446096c97d6SRichard Lowe static uchar_t tlsinstr_nop5[] = {
447096c97d6SRichard Lowe
448096c97d6SRichard Lowe 0x0f, 0x1f, 0x44, 0x00, 0x00
4497c478bd9Sstevel@tonic-gate };
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate #define TLS_GD_IE_MOV 0x8b /* movl opcode */
4527c478bd9Sstevel@tonic-gate #define TLS_GD_IE_POP 0x58 /* popl + reg */
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate #define TLS_GD_LE_MOVL 0xb8 /* movl + reg */
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate #define TLS_NOP 0x90 /* NOP instruction */
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate #define MODRM_MSK_MOD 0xc0
4597c478bd9Sstevel@tonic-gate #define MODRM_MSK_RO 0x38
4607c478bd9Sstevel@tonic-gate #define MODRM_MSK_RM 0x07
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate #define SIB_MSK_SS 0xc0
4637c478bd9Sstevel@tonic-gate #define SIB_MSK_IND 0x38
4647c478bd9Sstevel@tonic-gate #define SIB_MSK_BS 0x07
4657c478bd9Sstevel@tonic-gate
4665aefb655Srie static Fixupret
tls_fixups(Ofl_desc * ofl,Rel_desc * arsp)4675aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate Sym_desc *sdp = arsp->rel_sym;
4707c478bd9Sstevel@tonic-gate Word rtype = arsp->rel_rtype;
471b3fbe5e6Sseizo uchar_t *offset, r1, r2;
4727c478bd9Sstevel@tonic-gate
473b3fbe5e6Sseizo offset = (uchar_t *)((uintptr_t)arsp->rel_roffset +
474b3fbe5e6Sseizo (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
475bf994817SAli Bahrami (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
4767c478bd9Sstevel@tonic-gate
4777c478bd9Sstevel@tonic-gate if (sdp->sd_ref == REF_DYN_NEED) {
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate * IE reference model
4807c478bd9Sstevel@tonic-gate */
4817c478bd9Sstevel@tonic-gate switch (rtype) {
4827c478bd9Sstevel@tonic-gate case R_386_TLS_GD:
4837c478bd9Sstevel@tonic-gate /*
4847c478bd9Sstevel@tonic-gate * Transition:
4857c478bd9Sstevel@tonic-gate * 0x0 leal x@tlsgd(,r1,1), %eax
4867c478bd9Sstevel@tonic-gate * 0x7 call ___tls_get_addr
4877c478bd9Sstevel@tonic-gate * 0xc
4887c478bd9Sstevel@tonic-gate * To:
4897c478bd9Sstevel@tonic-gate * 0x0 movl %gs:0, %eax
4907c478bd9Sstevel@tonic-gate * 0x6 addl x@gotntpoff(r1), %eax
4917c478bd9Sstevel@tonic-gate */
4925aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
493bf994817SAli Bahrami R_386_TLS_GOTIE, arsp, ld_reloc_sym_name));
4947c478bd9Sstevel@tonic-gate arsp->rel_rtype = R_386_TLS_GOTIE;
4957c478bd9Sstevel@tonic-gate arsp->rel_roffset += 5;
4965aefb655Srie
4977c478bd9Sstevel@tonic-gate /*
498051d39bbSrie * Adjust 'offset' to beginning of instruction
4997c478bd9Sstevel@tonic-gate * sequence.
5007c478bd9Sstevel@tonic-gate */
5017c478bd9Sstevel@tonic-gate offset -= 3;
5027c478bd9Sstevel@tonic-gate r1 = (offset[2] & SIB_MSK_IND) >> 3;
5037c478bd9Sstevel@tonic-gate (void) memcpy(offset, tlsinstr_gd_ie,
5045aefb655Srie sizeof (tlsinstr_gd_ie));
5055aefb655Srie
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate * set register %r1 into the addl
5087c478bd9Sstevel@tonic-gate * instruction.
5097c478bd9Sstevel@tonic-gate */
5107c478bd9Sstevel@tonic-gate offset[0x7] |= r1;
5117c478bd9Sstevel@tonic-gate return (FIX_RELOC);
5125aefb655Srie
5137c478bd9Sstevel@tonic-gate case R_386_TLS_GD_PLT:
5147c478bd9Sstevel@tonic-gate /*
5157c478bd9Sstevel@tonic-gate * Fixup done via the TLS_GD relocation
5167c478bd9Sstevel@tonic-gate */
5175aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
518bf994817SAli Bahrami R_386_NONE, arsp, ld_reloc_sym_name));
5197c478bd9Sstevel@tonic-gate return (FIX_DONE);
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate * LE reference model
5257c478bd9Sstevel@tonic-gate */
5267c478bd9Sstevel@tonic-gate switch (rtype) {
5277c478bd9Sstevel@tonic-gate case R_386_TLS_GD:
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate * Transition:
5307c478bd9Sstevel@tonic-gate * 0x0 leal x@tlsgd(,r1,1), %eax
5317c478bd9Sstevel@tonic-gate * 0x7 call ___tls_get_addr
5327c478bd9Sstevel@tonic-gate * 0xc
5337c478bd9Sstevel@tonic-gate * To:
5347c478bd9Sstevel@tonic-gate * 0x0 movl %gs:0, %eax
5357c478bd9Sstevel@tonic-gate * 0x6 addl $x@ntpoff, %eax
5367c478bd9Sstevel@tonic-gate * 0xb nop
5377c478bd9Sstevel@tonic-gate * 0xc
5387c478bd9Sstevel@tonic-gate */
5395aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
540bf994817SAli Bahrami R_386_TLS_LE, arsp, ld_reloc_sym_name));
5417c478bd9Sstevel@tonic-gate
5427c478bd9Sstevel@tonic-gate arsp->rel_rtype = R_386_TLS_LE;
5437c478bd9Sstevel@tonic-gate arsp->rel_roffset += 4;
5445aefb655Srie
5457c478bd9Sstevel@tonic-gate /*
546051d39bbSrie * Adjust 'offset' to beginning of instruction
5477c478bd9Sstevel@tonic-gate * sequence.
5487c478bd9Sstevel@tonic-gate */
5497c478bd9Sstevel@tonic-gate offset -= 3;
5507c478bd9Sstevel@tonic-gate (void) memcpy(offset, tlsinstr_gd_le,
5515aefb655Srie sizeof (tlsinstr_gd_le));
5527c478bd9Sstevel@tonic-gate return (FIX_RELOC);
5535aefb655Srie
5547c478bd9Sstevel@tonic-gate case R_386_TLS_GD_PLT:
5557c478bd9Sstevel@tonic-gate case R_386_PLT32:
5567c478bd9Sstevel@tonic-gate /*
557096c97d6SRichard Lowe * Fixup done via the TLS_GD/TLS_LDM relocation processing
558096c97d6SRichard Lowe * and ld_reloc_plt() handling __tls_get_addr().
5597c478bd9Sstevel@tonic-gate */
5605aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
561bf994817SAli Bahrami R_386_NONE, arsp, ld_reloc_sym_name));
5627c478bd9Sstevel@tonic-gate return (FIX_DONE);
5635aefb655Srie
5647c478bd9Sstevel@tonic-gate case R_386_TLS_LDM_PLT:
5655aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
566bf994817SAli Bahrami R_386_NONE, arsp, ld_reloc_sym_name));
5675aefb655Srie
5687c478bd9Sstevel@tonic-gate /*
5697c478bd9Sstevel@tonic-gate * Transition:
5707c478bd9Sstevel@tonic-gate * call __tls_get_addr()
5717c478bd9Sstevel@tonic-gate * to:
572096c97d6SRichard Lowe * nopl 0x0(%eax,%eax)
5737c478bd9Sstevel@tonic-gate */
574096c97d6SRichard Lowe (void) memcpy(offset - 1, tlsinstr_nop5,
575096c97d6SRichard Lowe sizeof (tlsinstr_nop5));
5767c478bd9Sstevel@tonic-gate return (FIX_DONE);
5775aefb655Srie
5787c478bd9Sstevel@tonic-gate case R_386_TLS_LDM:
5795aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
580bf994817SAli Bahrami R_386_NONE, arsp, ld_reloc_sym_name));
5815aefb655Srie
5827c478bd9Sstevel@tonic-gate /*
5837c478bd9Sstevel@tonic-gate * Transition:
5847c478bd9Sstevel@tonic-gate *
5857c478bd9Sstevel@tonic-gate * 0x00 leal x1@tlsldm(%ebx), %eax
5867c478bd9Sstevel@tonic-gate * 0x06 call ___tls_get_addr
5877c478bd9Sstevel@tonic-gate *
5887c478bd9Sstevel@tonic-gate * to:
5897c478bd9Sstevel@tonic-gate *
5907c478bd9Sstevel@tonic-gate * 0x00 movl %gs:0, %eax
5917c478bd9Sstevel@tonic-gate */
592096c97d6SRichard Lowe (void) memcpy(offset - 2, tlsinstr_ld_le_movgs,
593096c97d6SRichard Lowe sizeof (tlsinstr_ld_le_movgs));
594096c97d6SRichard Lowe
595096c97d6SRichard Lowe /*
596096c97d6SRichard Lowe * We implicitly treat this as if a R_386_TLS_LDM_PLT for the
597096c97d6SRichard Lowe * __tls_get_addr call followed it as the GNU compiler
598096c97d6SRichard Lowe * doesn't generate one. This is safe, because if one _does_
599096c97d6SRichard Lowe * exist we'll just write the nop again.
600096c97d6SRichard Lowe */
601096c97d6SRichard Lowe (void) memcpy(offset + 4, tlsinstr_nop5,
602096c97d6SRichard Lowe sizeof (tlsinstr_nop5));
6037c478bd9Sstevel@tonic-gate return (FIX_DONE);
6045aefb655Srie
6057c478bd9Sstevel@tonic-gate case R_386_TLS_LDO_32:
6067c478bd9Sstevel@tonic-gate /*
6077c478bd9Sstevel@tonic-gate * Instructions:
6087c478bd9Sstevel@tonic-gate *
6097c478bd9Sstevel@tonic-gate * 0x10 leal x1@dtpoff(%eax), %edx R_386_TLS_LDO_32
6107c478bd9Sstevel@tonic-gate * to
6117c478bd9Sstevel@tonic-gate * 0x10 leal x1@ntpoff(%eax), %edx R_386_TLS_LE
6127c478bd9Sstevel@tonic-gate *
6137c478bd9Sstevel@tonic-gate */
6147c478bd9Sstevel@tonic-gate offset -= 2;
6157c478bd9Sstevel@tonic-gate
6165aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
617bf994817SAli Bahrami R_386_TLS_LE, arsp, ld_reloc_sym_name));
6187c478bd9Sstevel@tonic-gate arsp->rel_rtype = R_386_TLS_LE;
6197c478bd9Sstevel@tonic-gate return (FIX_RELOC);
6205aefb655Srie
6217c478bd9Sstevel@tonic-gate case R_386_TLS_GOTIE:
6227c478bd9Sstevel@tonic-gate /*
6237c478bd9Sstevel@tonic-gate * These transitions are a little different than the
6247c478bd9Sstevel@tonic-gate * others, in that we could have multiple instructions
6257c478bd9Sstevel@tonic-gate * pointed to by a single relocation. Depending upon the
6267c478bd9Sstevel@tonic-gate * instruction, we perform a different code transition.
6277c478bd9Sstevel@tonic-gate *
6287c478bd9Sstevel@tonic-gate * Here's the known transitions:
6297c478bd9Sstevel@tonic-gate *
6307c478bd9Sstevel@tonic-gate * 1) movl foo@gotntpoff(%reg1), %reg2
6317c478bd9Sstevel@tonic-gate * 0x8b, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
6327c478bd9Sstevel@tonic-gate *
6337c478bd9Sstevel@tonic-gate * 2) addl foo@gotntpoff(%reg1), %reg2
6347c478bd9Sstevel@tonic-gate * 0x03, 0x80 | (reg2 << 3) | reg1, foo@gotntpoff
6357c478bd9Sstevel@tonic-gate *
6367c478bd9Sstevel@tonic-gate * Transitions IE -> LE
6377c478bd9Sstevel@tonic-gate *
6387c478bd9Sstevel@tonic-gate * 1) movl $foo@ntpoff, %reg2
6397c478bd9Sstevel@tonic-gate * 0xc7, 0xc0 | reg2, foo@ntpoff
6407c478bd9Sstevel@tonic-gate *
6417c478bd9Sstevel@tonic-gate * 2) addl $foo@ntpoff, %reg2
6427c478bd9Sstevel@tonic-gate * 0x81, 0xc0 | reg2, foo@ntpoff
6437c478bd9Sstevel@tonic-gate *
6447c478bd9Sstevel@tonic-gate * Note: reg1 != 4 (%esp)
6457c478bd9Sstevel@tonic-gate */
6465aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
647bf994817SAli Bahrami R_386_TLS_LE, arsp, ld_reloc_sym_name));
6487c478bd9Sstevel@tonic-gate arsp->rel_rtype = R_386_TLS_LE;
6495aefb655Srie
6507c478bd9Sstevel@tonic-gate offset -= 2;
6517c478bd9Sstevel@tonic-gate r2 = (offset[1] & MODRM_MSK_RO) >> 3;
6527c478bd9Sstevel@tonic-gate if (offset[0] == 0x8b) {
6537c478bd9Sstevel@tonic-gate /* case 1 above */
6547c478bd9Sstevel@tonic-gate offset[0] = 0xc7; /* movl */
6557c478bd9Sstevel@tonic-gate offset[1] = 0xc0 | r2;
6567c478bd9Sstevel@tonic-gate return (FIX_RELOC);
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate
6597c478bd9Sstevel@tonic-gate if (offset[0] == 0x03) {
6607c478bd9Sstevel@tonic-gate /* case 2 above */
6617c478bd9Sstevel@tonic-gate assert(offset[0] == 0x03);
6627c478bd9Sstevel@tonic-gate offset[0] = 0x81; /* addl */
6637c478bd9Sstevel@tonic-gate offset[1] = 0xc0 | r2;
6647c478bd9Sstevel@tonic-gate return (FIX_RELOC);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate
6677c478bd9Sstevel@tonic-gate /*
6687c478bd9Sstevel@tonic-gate * Unexpected instruction sequence - fatal error.
6697c478bd9Sstevel@tonic-gate */
670de777a60Sab {
671de777a60Sab Conv_inv_buf_t inv_buf;
672de777a60Sab
6731007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
674de777a60Sab conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
675de777a60Sab arsp->rel_isdesc->is_file->ifl_name,
676bf994817SAli Bahrami ld_reloc_sym_name(arsp),
677de777a60Sab arsp->rel_isdesc->is_name,
678de777a60Sab EC_OFF(arsp->rel_roffset));
679de777a60Sab }
6807c478bd9Sstevel@tonic-gate return (FIX_ERROR);
6815aefb655Srie
6827c478bd9Sstevel@tonic-gate case R_386_TLS_IE:
6837c478bd9Sstevel@tonic-gate /*
6847c478bd9Sstevel@tonic-gate * These transitions are a little different than the
6857c478bd9Sstevel@tonic-gate * others, in that we could have multiple instructions
6867c478bd9Sstevel@tonic-gate * pointed to by a single relocation. Depending upon the
6877c478bd9Sstevel@tonic-gate * instruction, we perform a different code transition.
6887c478bd9Sstevel@tonic-gate *
6897c478bd9Sstevel@tonic-gate * Here's the known transitions:
6907c478bd9Sstevel@tonic-gate * 1) movl foo@indntpoff, %eax
6917c478bd9Sstevel@tonic-gate * 0xa1, foo@indntpoff
6927c478bd9Sstevel@tonic-gate *
6937c478bd9Sstevel@tonic-gate * 2) movl foo@indntpoff, %eax
6947c478bd9Sstevel@tonic-gate * 0x8b, 0x05 | (reg << 3), foo@gotntpoff
6957c478bd9Sstevel@tonic-gate *
6967c478bd9Sstevel@tonic-gate * 3) addl foo@indntpoff, %eax
6977c478bd9Sstevel@tonic-gate * 0x03, 0x05 | (reg << 3), foo@gotntpoff
6987c478bd9Sstevel@tonic-gate *
6997c478bd9Sstevel@tonic-gate * Transitions IE -> LE
7007c478bd9Sstevel@tonic-gate *
7017c478bd9Sstevel@tonic-gate * 1) movl $foo@ntpoff, %eax
7027c478bd9Sstevel@tonic-gate * 0xb8, foo@ntpoff
7037c478bd9Sstevel@tonic-gate *
7047c478bd9Sstevel@tonic-gate * 2) movl $foo@ntpoff, %reg
7057c478bd9Sstevel@tonic-gate * 0xc7, 0xc0 | reg, foo@ntpoff
7067c478bd9Sstevel@tonic-gate *
7077c478bd9Sstevel@tonic-gate * 3) addl $foo@ntpoff, %reg
7087c478bd9Sstevel@tonic-gate * 0x81, 0xc0 | reg, foo@ntpoff
7097c478bd9Sstevel@tonic-gate */
7107c478bd9Sstevel@tonic-gate arsp->rel_rtype = R_386_TLS_LE;
7117c478bd9Sstevel@tonic-gate offset--;
7127c478bd9Sstevel@tonic-gate if (offset[0] == 0xa1) {
7137c478bd9Sstevel@tonic-gate /* case 1 above */
7147c478bd9Sstevel@tonic-gate offset[0] = 0xb8; /* movl */
7157c478bd9Sstevel@tonic-gate return (FIX_RELOC);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate
7187c478bd9Sstevel@tonic-gate offset--;
7197c478bd9Sstevel@tonic-gate if (offset[0] == 0x8b) {
7207c478bd9Sstevel@tonic-gate /* case 2 above */
7217c478bd9Sstevel@tonic-gate r2 = (offset[1] & MODRM_MSK_RO) >> 3;
7227c478bd9Sstevel@tonic-gate offset[0] = 0xc7; /* movl */
7237c478bd9Sstevel@tonic-gate offset[1] = 0xc0 | r2;
7247c478bd9Sstevel@tonic-gate return (FIX_RELOC);
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate if (offset[0] == 0x03) {
7277c478bd9Sstevel@tonic-gate /* case 3 above */
7287c478bd9Sstevel@tonic-gate r2 = (offset[1] & MODRM_MSK_RO) >> 3;
7297c478bd9Sstevel@tonic-gate offset[0] = 0x81; /* addl */
7307c478bd9Sstevel@tonic-gate offset[1] = 0xc0 | r2;
7317c478bd9Sstevel@tonic-gate return (FIX_RELOC);
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate * Unexpected instruction sequence - fatal error.
7357c478bd9Sstevel@tonic-gate */
736de777a60Sab {
737de777a60Sab Conv_inv_buf_t inv_buf;
738de777a60Sab
7391007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLSINS),
740de777a60Sab conv_reloc_386_type(arsp->rel_rtype, 0, &inv_buf),
741de777a60Sab arsp->rel_isdesc->is_file->ifl_name,
742bf994817SAli Bahrami ld_reloc_sym_name(arsp),
743de777a60Sab arsp->rel_isdesc->is_name,
744de777a60Sab EC_OFF(arsp->rel_roffset));
745de777a60Sab }
7467c478bd9Sstevel@tonic-gate return (FIX_ERROR);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate return (FIX_RELOC);
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate
751ba2be530Sab static uintptr_t
ld_do_activerelocs(Ofl_desc * ofl)7525aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
7537c478bd9Sstevel@tonic-gate {
754141040e8Srie Rel_desc *arsp;
755bf994817SAli Bahrami Rel_cachebuf *rcbp;
75657ef7aa9SRod Evans Aliste idx;
7577c478bd9Sstevel@tonic-gate uintptr_t return_code = 1;
7581d9df23bSab ofl_flag_t flags = ofl->ofl_flags;
7597c478bd9Sstevel@tonic-gate
760bf994817SAli Bahrami if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
7617010c12aSrie DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
7627010c12aSrie
7637c478bd9Sstevel@tonic-gate /*
764141040e8Srie * Process active relocations.
7657c478bd9Sstevel@tonic-gate */
766bf994817SAli Bahrami REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
767bf994817SAli Bahrami uchar_t *addr;
768096c97d6SRichard Lowe Xword value;
769bf994817SAli Bahrami Sym_desc *sdp;
770bf994817SAli Bahrami const char *ifl_name;
771bf994817SAli Bahrami Xword refaddr;
772bf994817SAli Bahrami int moved = 0;
773bf994817SAli Bahrami Gotref gref;
774bf994817SAli Bahrami Os_desc *osp;
7757c478bd9Sstevel@tonic-gate
776bf994817SAli Bahrami /*
777bf994817SAli Bahrami * If the section this relocation is against has been discarded
778bf994817SAli Bahrami * (-zignore), then discard (skip) the relocation itself.
779bf994817SAli Bahrami */
780bf994817SAli Bahrami if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
781bf994817SAli Bahrami ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
782bf994817SAli Bahrami FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
783bf994817SAli Bahrami DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
784bf994817SAli Bahrami continue;
785bf994817SAli Bahrami }
7867c478bd9Sstevel@tonic-gate
787bf994817SAli Bahrami /*
788bf994817SAli Bahrami * We determine what the 'got reference' model (if required)
789bf994817SAli Bahrami * is at this point. This needs to be done before tls_fixup()
790bf994817SAli Bahrami * since it may 'transition' our instructions.
791bf994817SAli Bahrami *
792bf994817SAli Bahrami * The got table entries have already been assigned,
793bf994817SAli Bahrami * and we bind to those initial entries.
794bf994817SAli Bahrami */
795bf994817SAli Bahrami if (arsp->rel_flags & FLG_REL_DTLS)
796bf994817SAli Bahrami gref = GOT_REF_TLSGD;
797bf994817SAli Bahrami else if (arsp->rel_flags & FLG_REL_MTLS)
798bf994817SAli Bahrami gref = GOT_REF_TLSLD;
799bf994817SAli Bahrami else if (arsp->rel_flags & FLG_REL_STLS)
800bf994817SAli Bahrami gref = GOT_REF_TLSIE;
801bf994817SAli Bahrami else
802bf994817SAli Bahrami gref = GOT_REF_GENERIC;
8037c478bd9Sstevel@tonic-gate
804bf994817SAli Bahrami /*
805bf994817SAli Bahrami * Perform any required TLS fixups.
806bf994817SAli Bahrami */
807bf994817SAli Bahrami if (arsp->rel_flags & FLG_REL_TLSFIX) {
808bf994817SAli Bahrami Fixupret ret;
8097c478bd9Sstevel@tonic-gate
810bf994817SAli Bahrami if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
811bf994817SAli Bahrami return (S_ERROR);
812bf994817SAli Bahrami if (ret == FIX_DONE)
813bf994817SAli Bahrami continue;
814bf994817SAli Bahrami }
8157c478bd9Sstevel@tonic-gate
816bf994817SAli Bahrami /*
817bf994817SAli Bahrami * If this is a relocation against a move table, or
818bf994817SAli Bahrami * expanded move table, adjust the relocation entries.
819bf994817SAli Bahrami */
820bf994817SAli Bahrami if (RELAUX_GET_MOVE(arsp))
821bf994817SAli Bahrami ld_adj_movereloc(ofl, arsp);
822bf994817SAli Bahrami
823bf994817SAli Bahrami sdp = arsp->rel_sym;
824bf994817SAli Bahrami refaddr = arsp->rel_roffset +
825bf994817SAli Bahrami (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
826bf994817SAli Bahrami
827bf994817SAli Bahrami if (arsp->rel_flags & FLG_REL_CLVAL)
828bf994817SAli Bahrami value = 0;
829bf994817SAli Bahrami else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
8307c478bd9Sstevel@tonic-gate /*
831bf994817SAli Bahrami * The value for a symbol pointing to a SECTION
832bf994817SAli Bahrami * is based off of that sections position.
8337c478bd9Sstevel@tonic-gate */
834bf994817SAli Bahrami if (sdp->sd_isc->is_flags & FLG_IS_RELUPD) {
835bf994817SAli Bahrami Sym_desc *sym;
836bf994817SAli Bahrami Xword radd;
837bf994817SAli Bahrami uchar_t *raddr = (uchar_t *)
838bf994817SAli Bahrami arsp->rel_isdesc->is_indata->d_buf +
839bf994817SAli Bahrami arsp->rel_roffset;
8407c478bd9Sstevel@tonic-gate
8417c478bd9Sstevel@tonic-gate /*
842bf994817SAli Bahrami * This is a REL platform. Hence, the second
843bf994817SAli Bahrami * argument of ld_am_I_partial() is the value
844bf994817SAli Bahrami * stored at the target address where the
845bf994817SAli Bahrami * relocation is going to be applied.
8467c478bd9Sstevel@tonic-gate */
847bf994817SAli Bahrami if (ld_reloc_targval_get(ofl, arsp, raddr,
848bf994817SAli Bahrami &radd) == 0)
849bf994817SAli Bahrami return (S_ERROR);
850bf994817SAli Bahrami sym = ld_am_I_partial(arsp, radd);
851bf994817SAli Bahrami if (sym) {
852bf994817SAli Bahrami Sym *osym = sym->sd_osym;
853b26cc8daSAli Bahrami
8547c478bd9Sstevel@tonic-gate /*
855bf994817SAli Bahrami * The symbol was moved, so adjust the
856bf994817SAli Bahrami * value relative to the new section.
8577c478bd9Sstevel@tonic-gate */
858bf994817SAli Bahrami value = sym->sd_sym->st_value;
859bf994817SAli Bahrami moved = 1;
860bf994817SAli Bahrami
861bf994817SAli Bahrami /*
862bf994817SAli Bahrami * The original raddend covers the
863bf994817SAli Bahrami * displacement from the section start
864bf994817SAli Bahrami * to the desired address. The value
865bf994817SAli Bahrami * computed above gets us from the
866bf994817SAli Bahrami * section start to the start of the
867bf994817SAli Bahrami * symbol range. Adjust the old raddend
868bf994817SAli Bahrami * to remove the offset from section
869bf994817SAli Bahrami * start to symbol start, leaving the
870bf994817SAli Bahrami * displacement within the range of
871bf994817SAli Bahrami * the symbol.
872bf994817SAli Bahrami */
873bf994817SAli Bahrami if (osym->st_value != 0) {
874bf994817SAli Bahrami radd -= osym->st_value;
875bf994817SAli Bahrami if (ld_reloc_targval_set(ofl,
876bf994817SAli Bahrami arsp, raddr, radd) == 0)
877bf994817SAli Bahrami return (S_ERROR);
878b26cc8daSAli Bahrami }
879b26cc8daSAli Bahrami }
880bf994817SAli Bahrami }
881bf994817SAli Bahrami if (!moved) {
882bf994817SAli Bahrami value = _elf_getxoff(sdp->sd_isc->is_indata);
883bf994817SAli Bahrami if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
884bf994817SAli Bahrami value += sdp->sd_isc->
885bf994817SAli Bahrami is_osdesc->os_shdr->sh_addr;
886bf994817SAli Bahrami }
887bf994817SAli Bahrami if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
888bf994817SAli Bahrami value -= ofl->ofl_tlsphdr->p_vaddr;
8892926dd2eSrie
890bf994817SAli Bahrami } else if (IS_SIZE(arsp->rel_rtype)) {
891bf994817SAli Bahrami /*
892bf994817SAli Bahrami * Size relocations require the symbols size.
893bf994817SAli Bahrami */
894bf994817SAli Bahrami value = sdp->sd_sym->st_size;
89508278a5eSRod Evans
896bf994817SAli Bahrami } else if ((sdp->sd_flags & FLG_SY_CAP) &&
897bf994817SAli Bahrami sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
898bf994817SAli Bahrami /*
899bf994817SAli Bahrami * If relocation is against a capabilities symbol, we
900bf994817SAli Bahrami * need to jump to an associated PLT, so that at runtime
901bf994817SAli Bahrami * ld.so.1 is involved to determine the best binding
902bf994817SAli Bahrami * choice. Otherwise, the value is the symbols value.
903bf994817SAli Bahrami */
904bf994817SAli Bahrami value = ld_calc_plt_addr(sdp, ofl);
905bf994817SAli Bahrami
906bf994817SAli Bahrami } else
907bf994817SAli Bahrami value = sdp->sd_sym->st_value;
908bf994817SAli Bahrami
909bf994817SAli Bahrami /*
910bf994817SAli Bahrami * Relocation against the GLOBAL_OFFSET_TABLE.
911bf994817SAli Bahrami */
912bf994817SAli Bahrami if ((arsp->rel_flags & FLG_REL_GOT) &&
913bf994817SAli Bahrami !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
914bf994817SAli Bahrami return (S_ERROR);
915bf994817SAli Bahrami osp = RELAUX_GET_OSDESC(arsp);
916bf994817SAli Bahrami
917bf994817SAli Bahrami /*
918bf994817SAli Bahrami * If loadable and not producing a relocatable object add the
919bf994817SAli Bahrami * sections virtual address to the reference address.
920bf994817SAli Bahrami */
921bf994817SAli Bahrami if ((arsp->rel_flags & FLG_REL_LOAD) &&
922bf994817SAli Bahrami ((flags & FLG_OF_RELOBJ) == 0))
923bf994817SAli Bahrami refaddr +=
924bf994817SAli Bahrami arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr;
925bf994817SAli Bahrami
926bf994817SAli Bahrami /*
927bf994817SAli Bahrami * If this entry has a PLT assigned to it, its value is actually
928bf994817SAli Bahrami * the address of the PLT (and not the address of the function).
929bf994817SAli Bahrami */
930bf994817SAli Bahrami if (IS_PLT(arsp->rel_rtype)) {
931bf994817SAli Bahrami if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
93208278a5eSRod Evans value = ld_calc_plt_addr(sdp, ofl);
933bf994817SAli Bahrami }
93408278a5eSRod Evans
935bf994817SAli Bahrami /*
936bf994817SAli Bahrami * Determine whether the value needs further adjustment. Filter
937bf994817SAli Bahrami * through the attributes of the relocation to determine what
938bf994817SAli Bahrami * adjustment is required. Note, many of the following cases
939bf994817SAli Bahrami * are only applicable when a .got is present. As a .got is
940bf994817SAli Bahrami * not generated when a relocatable object is being built,
941bf994817SAli Bahrami * any adjustments that require a .got need to be skipped.
942bf994817SAli Bahrami */
943bf994817SAli Bahrami if ((arsp->rel_flags & FLG_REL_GOT) &&
944bf994817SAli Bahrami ((flags & FLG_OF_RELOBJ) == 0)) {
945bf994817SAli Bahrami Xword R1addr;
946bf994817SAli Bahrami uintptr_t R2addr;
947bf994817SAli Bahrami Word gotndx;
948bf994817SAli Bahrami Gotndx *gnp;
9497c478bd9Sstevel@tonic-gate
9507c478bd9Sstevel@tonic-gate /*
951bf994817SAli Bahrami * Perform relocation against GOT table. Since this
952bf994817SAli Bahrami * doesn't fit exactly into a relocation we place the
953bf994817SAli Bahrami * appropriate byte in the GOT directly
954bf994817SAli Bahrami *
955bf994817SAli Bahrami * Calculate offset into GOT at which to apply
956bf994817SAli Bahrami * the relocation.
9577c478bd9Sstevel@tonic-gate */
958bf994817SAli Bahrami gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
959bf994817SAli Bahrami assert(gnp);
9607c478bd9Sstevel@tonic-gate
961bf994817SAli Bahrami if (arsp->rel_rtype == R_386_TLS_DTPOFF32)
962bf994817SAli Bahrami gotndx = gnp->gn_gotndx + 1;
963bf994817SAli Bahrami else
964bf994817SAli Bahrami gotndx = gnp->gn_gotndx;
965bf994817SAli Bahrami
966bf994817SAli Bahrami R1addr = (Xword)(gotndx * M_GOT_ENTSIZE);
9677c478bd9Sstevel@tonic-gate
9687c478bd9Sstevel@tonic-gate /*
969bf994817SAli Bahrami * Add the GOTs data's offset.
9707c478bd9Sstevel@tonic-gate */
971bf994817SAli Bahrami R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
972bf994817SAli Bahrami
973bf994817SAli Bahrami DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
974bf994817SAli Bahrami M_MACH, SHT_REL, arsp, R1addr, value,
975bf994817SAli Bahrami ld_reloc_sym_name));
9767c478bd9Sstevel@tonic-gate
977141040e8Srie /*
978bf994817SAli Bahrami * And do it.
979141040e8Srie */
980bf994817SAli Bahrami if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
981bf994817SAli Bahrami *(Xword *)R2addr = ld_bswap_Xword(value);
982bf994817SAli Bahrami else
983bf994817SAli Bahrami *(Xword *)R2addr = value;
984bf994817SAli Bahrami continue;
985bf994817SAli Bahrami
986bf994817SAli Bahrami } else if (IS_GOT_BASED(arsp->rel_rtype) &&
987bf994817SAli Bahrami ((flags & FLG_OF_RELOBJ) == 0)) {
988bf994817SAli Bahrami value -= ofl->ofl_osgot->os_shdr->sh_addr;
989bf994817SAli Bahrami
990bf994817SAli Bahrami } else if (IS_GOT_PC(arsp->rel_rtype) &&
991bf994817SAli Bahrami ((flags & FLG_OF_RELOBJ) == 0)) {
992bf994817SAli Bahrami value = (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) -
993bf994817SAli Bahrami refaddr;
994bf994817SAli Bahrami
995bf994817SAli Bahrami } else if ((IS_PC_RELATIVE(arsp->rel_rtype)) &&
996bf994817SAli Bahrami (((flags & FLG_OF_RELOBJ) == 0) ||
997bf994817SAli Bahrami (osp == sdp->sd_isc->is_osdesc))) {
998bf994817SAli Bahrami value -= refaddr;
999bf994817SAli Bahrami
1000bf994817SAli Bahrami } else if (IS_TLS_INS(arsp->rel_rtype) &&
1001bf994817SAli Bahrami IS_GOT_RELATIVE(arsp->rel_rtype) &&
1002bf994817SAli Bahrami ((flags & FLG_OF_RELOBJ) == 0)) {
1003bf994817SAli Bahrami Gotndx *gnp;
1004bf994817SAli Bahrami
1005bf994817SAli Bahrami gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, NULL);
1006bf994817SAli Bahrami assert(gnp);
1007bf994817SAli Bahrami value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE;
1008bf994817SAli Bahrami if (arsp->rel_rtype == R_386_TLS_IE) {
1009bf994817SAli Bahrami value += ofl->ofl_osgot->os_shdr->sh_addr;
1010bf994817SAli Bahrami }
10117c478bd9Sstevel@tonic-gate
1012bf994817SAli Bahrami } else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
1013