15aefb655Srie /*
25aefb655Srie * CDDL HEADER START
35aefb655Srie *
45aefb655Srie * The contents of this file are subject to the terms of the
55aefb655Srie * Common Development and Distribution License (the "License").
65aefb655Srie * You may not use this file except in compliance with the License.
75aefb655Srie *
85aefb655Srie * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95aefb655Srie * or http://www.opensolaris.org/os/licensing.
105aefb655Srie * See the License for the specific language governing permissions
115aefb655Srie * and limitations under the License.
125aefb655Srie *
135aefb655Srie * When distributing Covered Code, include this CDDL HEADER in each
145aefb655Srie * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155aefb655Srie * If applicable, add the following below this CDDL HEADER, with the
165aefb655Srie * fields enclosed by brackets "[]" replaced with your own identifying
175aefb655Srie * information: Portions Copyright [yyyy] [name of copyright owner]
185aefb655Srie *
195aefb655Srie * CDDL HEADER END
205aefb655Srie */
215aefb655Srie
225aefb655Srie /*
235aefb655Srie * Copyright (c) 1988 AT&T
245aefb655Srie * All Rights Reserved
255aefb655Srie *
26bf994817SAli Bahrami * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
275aefb655Srie */
285aefb655Srie
29ba2be530Sab196087 /* Get the sparc version of the relocation engine */
30ba2be530Sab196087 #define DO_RELOC_LIBLD_SPARC
31ba2be530Sab196087
325aefb655Srie #include <string.h>
335aefb655Srie #include <stdio.h>
345aefb655Srie #include <sys/elf_SPARC.h>
355aefb655Srie #include <debug.h>
365aefb655Srie #include <reloc.h>
37ba2be530Sab196087 #include <sparc/machdep_sparc.h>
385aefb655Srie #include "msg.h"
395aefb655Srie #include "_libld.h"
40ba2be530Sab196087 #include "machsym.sparc.h"
41ba2be530Sab196087
425aefb655Srie /*
435aefb655Srie * Local Variable Definitions
445aefb655Srie */
455aefb655Srie static Sword neggotoffset = 0; /* off. of GOT table from GOT symbol */
465aefb655Srie static Sword smlgotcnt = M_GOT_XNumber; /* no. of small GOT symbols */
476a074c93Sab196087 static Sword mixgotcnt = 0; /* # syms with both large/small GOT */
485aefb655Srie
4957ef7aa9SRod Evans /*
5057ef7aa9SRod Evans * Search the GOT index list for a GOT entry with a matching reference and the
5157ef7aa9SRod Evans * proper addend.
5257ef7aa9SRod Evans */
5357ef7aa9SRod Evans static Gotndx *
ld_find_got_ndx(Alist * alp,Gotref gref,Ofl_desc * ofl,Rel_desc * rdesc)5457ef7aa9SRod Evans ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
5557ef7aa9SRod Evans {
5657ef7aa9SRod Evans Aliste idx;
5757ef7aa9SRod Evans Gotndx *gnp;
5857ef7aa9SRod Evans
5957ef7aa9SRod Evans assert(rdesc != 0);
6057ef7aa9SRod Evans
6157ef7aa9SRod Evans if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
6257ef7aa9SRod Evans return (ofl->ofl_tlsldgotndx);
6357ef7aa9SRod Evans
6457ef7aa9SRod Evans for (ALIST_TRAVERSE(alp, idx, gnp)) {
6557ef7aa9SRod Evans if ((rdesc->rel_raddend == gnp->gn_addend) &&
6657ef7aa9SRod Evans (gref == gnp->gn_gotref))
6757ef7aa9SRod Evans return (gnp);
6857ef7aa9SRod Evans }
6957ef7aa9SRod Evans return (NULL);
7057ef7aa9SRod Evans }
7157ef7aa9SRod Evans
7257ef7aa9SRod Evans static Xword
ld_calc_got_offset(Rel_desc * rdesc,Ofl_desc * ofl)7357ef7aa9SRod Evans ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
7457ef7aa9SRod Evans {
7557ef7aa9SRod Evans Os_desc *osp = ofl->ofl_osgot;
7657ef7aa9SRod Evans Sym_desc *sdp = rdesc->rel_sym;
7757ef7aa9SRod Evans Xword gotndx;
7857ef7aa9SRod Evans Gotref gref;
7957ef7aa9SRod Evans Gotndx *gnp;
8057ef7aa9SRod Evans
8157ef7aa9SRod Evans if (rdesc->rel_flags & FLG_REL_DTLS)
8257ef7aa9SRod Evans gref = GOT_REF_TLSGD;
8357ef7aa9SRod Evans else if (rdesc->rel_flags & FLG_REL_MTLS)
8457ef7aa9SRod Evans gref = GOT_REF_TLSLD;
8557ef7aa9SRod Evans else if (rdesc->rel_flags & FLG_REL_STLS)
8657ef7aa9SRod Evans gref = GOT_REF_TLSIE;
8757ef7aa9SRod Evans else
8857ef7aa9SRod Evans gref = GOT_REF_GENERIC;
8957ef7aa9SRod Evans
9057ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc);
9157ef7aa9SRod Evans assert(gnp);
9257ef7aa9SRod Evans
9357ef7aa9SRod Evans gotndx = (Xword)gnp->gn_gotndx;
9457ef7aa9SRod Evans
9557ef7aa9SRod Evans if ((rdesc->rel_flags & FLG_REL_DTLS) &&
9657ef7aa9SRod Evans (rdesc->rel_rtype == M_R_DTPOFF))
9757ef7aa9SRod Evans gotndx++;
9857ef7aa9SRod Evans
9957ef7aa9SRod Evans return ((Xword)((osp->os_shdr->sh_addr) + (gotndx * M_GOT_ENTSIZE) +
10057ef7aa9SRod Evans (-neggotoffset * M_GOT_ENTSIZE)));
10157ef7aa9SRod Evans }
10257ef7aa9SRod Evans
103ba2be530Sab196087 static Word
ld_init_rel(Rel_desc * reld,Word * typedata,void * reloc)104bf994817SAli Bahrami ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
1055aefb655Srie {
1065aefb655Srie Rela *rela = (Rela *)reloc;
1075aefb655Srie
1085aefb655Srie /* LINTED */
109ba2be530Sab196087 reld->rel_rtype = (Word)ELF_R_TYPE(rela->r_info, M_MACH);
1105aefb655Srie reld->rel_roffset = rela->r_offset;
1115aefb655Srie reld->rel_raddend = rela->r_addend;
112bf994817SAli Bahrami *typedata = (Word)ELF_R_TYPE_DATA(rela->r_info);
1135aefb655Srie
1145aefb655Srie reld->rel_flags |= FLG_REL_RELA;
1155aefb655Srie
1165aefb655Srie return ((Word)ELF_R_SYM(rela->r_info));
1175aefb655Srie }
1185aefb655Srie
119ba2be530Sab196087 static void
ld_mach_eflags(Ehdr * ehdr,Ofl_desc * ofl)1205aefb655Srie ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
1215aefb655Srie {
1225aefb655Srie Word eflags = ofl->ofl_dehdr->e_flags;
1235aefb655Srie Word memopt1, memopt2;
1245aefb655Srie static int firstpass;
1255aefb655Srie
1265aefb655Srie /*
1275aefb655Srie * If a *PLUS relocatable is included, the output object is type *PLUS.
1285aefb655Srie */
1295aefb655Srie if ((ehdr->e_machine == EM_SPARC32PLUS) &&
1305aefb655Srie (ehdr->e_flags & EF_SPARC_32PLUS))
1315aefb655Srie ofl->ofl_dehdr->e_machine = EM_SPARC32PLUS;
1325aefb655Srie
1335aefb655Srie /*
1345aefb655Srie * On the first pass, we don't yet have a memory model to compare
1355aefb655Srie * against, therefore the initial file becomes our baseline. Subsequent
1365aefb655Srie * passes will do the comparison described below.
1375aefb655Srie */
1385aefb655Srie if (firstpass == 0) {
1395aefb655Srie ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
1405aefb655Srie firstpass++;
1415aefb655Srie return;
1425aefb655Srie }
1435aefb655Srie
1445aefb655Srie /*
1455aefb655Srie * Determine which memory model to mark the binary with. The options
1465aefb655Srie * are (most restrictive to least):
1475aefb655Srie *
1485aefb655Srie * EF_SPARCV9_TSO 0x0 Total Store Order
1495aefb655Srie * EF_SPARCV9_PSO 0x1 Partial Store Order
1505aefb655Srie * EF_SPARCV9_RMO 0x2 Relaxed Memory Order
1515aefb655Srie *
1525aefb655Srie * Mark the binary with the most restrictive option encountered from a
1535aefb655Srie * relocatable object included in the link.
1545aefb655Srie */
1555aefb655Srie eflags |= (ehdr->e_flags & ~EF_SPARCV9_MM);
1565aefb655Srie memopt1 = eflags & EF_SPARCV9_MM;
1575aefb655Srie memopt2 = ehdr->e_flags & EF_SPARCV9_MM;
1585aefb655Srie eflags &= ~EF_SPARCV9_MM;
1595aefb655Srie
1605aefb655Srie if ((memopt1 == EF_SPARCV9_TSO) || (memopt2 == EF_SPARCV9_TSO))
1615aefb655Srie /* EMPTY */
1625aefb655Srie ;
1635aefb655Srie else if ((memopt1 == EF_SPARCV9_PSO) || (memopt2 == EF_SPARCV9_PSO))
1645aefb655Srie eflags |= EF_SPARCV9_PSO;
1655aefb655Srie else
1665aefb655Srie eflags |= EF_SPARCV9_RMO;
1675aefb655Srie
1685aefb655Srie ofl->ofl_dehdr->e_flags = eflags;
1695aefb655Srie }
1705aefb655Srie
171ba2be530Sab196087 static void
ld_mach_make_dynamic(Ofl_desc * ofl,size_t * cnt)1725aefb655Srie ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
1735aefb655Srie {
1745aefb655Srie if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1755aefb655Srie /*
1765aefb655Srie * Create this entry if we are going to create a PLT table.
1775aefb655Srie */
1785aefb655Srie if (ofl->ofl_pltcnt)
1795aefb655Srie (*cnt)++; /* DT_PLTGOT */
1805aefb655Srie }
1815aefb655Srie }
1825aefb655Srie
183ba2be530Sab196087 static void
ld_mach_update_odynamic(Ofl_desc * ofl,Dyn ** dyn)1845aefb655Srie ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
1855aefb655Srie {
1865aefb655Srie if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
1875aefb655Srie (*dyn)->d_tag = DT_PLTGOT;
1885aefb655Srie if (ofl->ofl_osplt)
1895aefb655Srie (*dyn)->d_un.d_ptr = ofl->ofl_osplt->os_shdr->sh_addr;
1905aefb655Srie else
1915aefb655Srie (*dyn)->d_un.d_ptr = 0;
1925aefb655Srie (*dyn)++;
1935aefb655Srie }
1945aefb655Srie }
1955aefb655Srie
1965aefb655Srie #if defined(_ELF64)
1975aefb655Srie
198ba2be530Sab196087 static Xword
ld_calc_plt_addr(Sym_desc * sdp,Ofl_desc * ofl)1995aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
2005aefb655Srie {
2015aefb655Srie Xword value, pltndx, farpltndx;
2025aefb655Srie
2035aefb655Srie pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1;
2045aefb655Srie
2055aefb655Srie if ((pltndx) < M64_PLT_NEARPLTS) {
2065aefb655Srie value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
2075aefb655Srie (pltndx * M_PLT_ENTSIZE);
2085aefb655Srie return (value);
2095aefb655Srie }
2105aefb655Srie
2115aefb655Srie farpltndx = pltndx - M64_PLT_NEARPLTS;
2125aefb655Srie
2135aefb655Srie /*
2145aefb655Srie * pltoffset of a far plt is calculated by:
2155aefb655Srie *
2165aefb655Srie * <size of near plt table> +
2175aefb655Srie * <size of preceding far plt blocks> +
2185aefb655Srie * <blockndx * sizeof (far plt entsize)>
2195aefb655Srie */
2205aefb655Srie value =
2215aefb655Srie /* size of near plt table */
2225aefb655Srie (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) +
2235aefb655Srie /* size of preceding far plt blocks */
2245aefb655Srie ((farpltndx / M64_PLT_FBLKCNTS) *
2255aefb655Srie ((M64_PLT_FENTSIZE + sizeof (Addr)) *
2265aefb655Srie M64_PLT_FBLKCNTS)) +
2275aefb655Srie /* pltblockendx * fentsize */
2285aefb655Srie ((farpltndx % M64_PLT_FBLKCNTS) * M64_PLT_FENTSIZE);
2295aefb655Srie
2305aefb655Srie value += (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
2315aefb655Srie return (value);
2325aefb655Srie }
2335aefb655Srie
2345aefb655Srie /*
2355aefb655Srie * Instructions required for Far PLT's
2365aefb655Srie */
237ba2be530Sab196087 static uchar_t farplt_instrs[24] = {
238ba2be530Sab196087 0x8a, 0x10, 0x00, 0x0f, /* mov %o7, %g5 */
239ba2be530Sab196087 0x40, 0x00, 0x00, 0x02, /* call . + 0x8 */
240ba2be530Sab196087 0x01, 0x00, 0x00, 0x00, /* nop */
241ba2be530Sab196087 0xc2, 0x5b, 0xe0, 0x00, /* ldx [%o7 + 0], %g1 */
242ba2be530Sab196087 0x83, 0xc3, 0xc0, 0x01, /* jmpl %o7 + %g1, %g1 */
243ba2be530Sab196087 0x9e, 0x10, 0x00, 0x05 /* mov %g5, %o7 */
2445aefb655Srie };
2455aefb655Srie
2465aefb655Srie /*
2475aefb655Srie * Far PLT'S:
2485aefb655Srie *
2495aefb655Srie * Far PLT's are established in blocks of '160' at a time. These
2505aefb655Srie * PLT's consist of 6 instructions (24 bytes) and 1 pointer (8 bytes).
2515aefb655Srie * The instructions are collected together in blocks of 160 entries
2525aefb655Srie * followed by 160 pointers. The last group of entries and pointers
2535aefb655Srie * may contain less then 160 items. No padding is required.
2545aefb655Srie *
2555aefb655Srie * .PLT32768:
2565aefb655Srie * mov %o7, %g5
2575aefb655Srie * call . + 8
2585aefb655Srie * nop
2595aefb655Srie * ldx [%o7 + .PLTP32768 - (.PLT32768 + 4)], %g1
2605aefb655Srie * jmpl %o7 + %g1, %g1
2615aefb655Srie * mov %g5, %o7
2625aefb655Srie * ................................
2635aefb655Srie * .PLT32927:
2645aefb655Srie * mov %o7, %g5
2655aefb655Srie * call . + 8
2665aefb655Srie * nop
2675aefb655Srie * ldx [%o7 + .PLTP32927 - (.PLT32927 + 4)], %g1
2685aefb655Srie * jmpl %o7 + %g1, %g1
2695aefb655Srie * mov %g5, %o7
2705aefb655Srie * .PLTP32768:
2715aefb655Srie * .xword .PLT0-(.PLT32768+4)
2725aefb655Srie * ................................
2735aefb655Srie * .PLTP32927:
2745aefb655Srie * .xword .PLT0-(.PLT32927+4)
2755aefb655Srie *
2765aefb655Srie */
277ba2be530Sab196087 static void
plt_far_entry(Ofl_desc * ofl,Xword pltndx,Xword * roffset,Sxword * raddend)2785aefb655Srie plt_far_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
2795aefb655Srie {
2805aefb655Srie uint_t blockndx; /* # of far PLT blocks */
2815aefb655Srie uint_t farblkcnt; /* Index to far PLT block */
2825aefb655Srie Xword farpltndx; /* index of Far Plt */
2835aefb655Srie Xword farpltblkndx; /* index of PLT in BLOCK */
2845aefb655Srie uint32_t *pltent; /* ptr to plt instr. sequence */
2855aefb655Srie uint64_t *pltentptr; /* ptr to plt addr ptr */
2865aefb655Srie Sxword pltblockoff; /* offset to Far plt block */
2875aefb655Srie Sxword pltoff; /* offset to PLT instr. sequence */
2885aefb655Srie Sxword pltptroff; /* offset to PLT addr ptr */
2895aefb655Srie uchar_t *pltbuf; /* ptr to PLT's in file */
2905aefb655Srie
2915aefb655Srie
2925aefb655Srie farblkcnt = ((ofl->ofl_pltcnt - 1 +
2935aefb655Srie M_PLT_XNumber - M64_PLT_NEARPLTS) / M64_PLT_FBLKCNTS);
2945aefb655Srie
2955aefb655Srie /*
2965aefb655Srie * Determine the 'Far' PLT index.
2975aefb655Srie */
2985aefb655Srie farpltndx = pltndx - 1 + M_PLT_XNumber - M64_PLT_NEARPLTS;
2995aefb655Srie farpltblkndx = farpltndx % M64_PLT_FBLKCNTS;
3005aefb655Srie
3015aefb655Srie /*
3025aefb655Srie * Determine what FPLT block this plt falls into.
3035aefb655Srie */
3045aefb655Srie blockndx = (uint_t)(farpltndx / M64_PLT_FBLKCNTS);
3055aefb655Srie
3065aefb655Srie /*
3075aefb655Srie * Calculate the starting offset of the Far PLT block
3085aefb655Srie * that this PLT is a member of.
3095aefb655Srie */
3105aefb655Srie pltblockoff = (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) +
3115aefb655Srie (blockndx * M64_PLT_FBLOCKSZ);
3125aefb655Srie
3135aefb655Srie pltoff = pltblockoff +
3145aefb655Srie (farpltblkndx * M64_PLT_FENTSIZE);
3155aefb655Srie
3165aefb655Srie pltptroff = pltblockoff;
3175aefb655Srie
3185aefb655Srie
3195aefb655Srie if (farblkcnt > blockndx) {
3205aefb655Srie /*
3215aefb655Srie * If this is a full block - the 'pltptroffs' start
3225aefb655Srie * after 160 fplts.
3235aefb655Srie */
3245aefb655Srie pltptroff += (M64_PLT_FBLKCNTS * M64_PLT_FENTSIZE) +
3255aefb655Srie (farpltblkndx * M64_PLT_PSIZE);
3265aefb655Srie } else {
3275aefb655Srie Xword lastblkpltndx;
3285aefb655Srie /*
3295aefb655Srie * If this is the last block - the the pltptr's start
3305aefb655Srie * after the last FPLT instruction sequence.
3315aefb655Srie */
3325aefb655Srie lastblkpltndx = (ofl->ofl_pltcnt - 1 + M_PLT_XNumber -
3335aefb655Srie M64_PLT_NEARPLTS) % M64_PLT_FBLKCNTS;
3345aefb655Srie pltptroff += ((lastblkpltndx + 1) * M64_PLT_FENTSIZE) +
3355aefb655Srie (farpltblkndx * M64_PLT_PSIZE);
3365aefb655Srie }
3375aefb655Srie pltbuf = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
3385aefb655Srie
3395aefb655Srie /*
3405aefb655Srie * For far-plts, the Raddend and Roffset fields are defined
3415aefb655Srie * to be:
3425aefb655Srie *
3435aefb655Srie * roffset: address of .PLTP#
3445aefb655Srie * raddend: -(.PLT#+4)
3455aefb655Srie */
3465aefb655Srie *roffset = pltptroff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
3475aefb655Srie *raddend = -(pltoff + 4 + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr));
3485aefb655Srie
3495aefb655Srie /* LINTED */
3505aefb655Srie pltent = (uint32_t *)(pltbuf + pltoff);
3515aefb655Srie /* LINTED */
3525aefb655Srie pltentptr = (uint64_t *)(pltbuf + pltptroff);
3535aefb655Srie (void) memcpy(pltent, farplt_instrs, sizeof (farplt_instrs));
3545aefb655Srie
3555aefb655Srie /*
3565aefb655Srie * update
3575aefb655Srie * ldx [%o7 + 0], %g1
3585aefb655Srie * to
3595aefb655Srie * ldx [%o7 + .PLTP# - (.PLT# + 4)], %g1
3605aefb655Srie */
3615aefb655Srie /* LINTED */
3625aefb655Srie pltent[3] |= (uint32_t)(pltptroff - (pltoff + 4));
3635aefb655Srie
3645aefb655Srie /*
3655aefb655Srie * Store:
3665aefb655Srie * .PLTP#
3675aefb655Srie * .xword .PLT0 - .PLT# + 4
3685aefb655Srie */
3695aefb655Srie *pltentptr = -(pltoff + 4);
3705aefb655Srie }
3715aefb655Srie
3725aefb655Srie /*
3735aefb655Srie * Build a single V9 P.L.T. entry - code is:
3745aefb655Srie *
3755aefb655Srie * For Target Addresses +/- 4GB of the entry
3765aefb655Srie * -----------------------------------------
3775aefb655Srie * sethi (. - .PLT0), %g1
3785aefb655Srie * ba,a %xcc, .PLT1
3795aefb655Srie * nop
3805aefb655Srie * nop
3815aefb655Srie * nop
3825aefb655Srie * nop
3835aefb655Srie * nop
3845aefb655Srie * nop
3855aefb655Srie *
3865aefb655Srie * For Target Addresses +/- 2GB of the entry
3875aefb655Srie * -----------------------------------------
3885aefb655Srie *
3895aefb655Srie * .PLT0 is the address of the first entry in the P.L.T.
3905aefb655Srie * This one is filled in by the run-time link editor. We just
3915aefb655Srie * have to leave space for it.
3925aefb655Srie */
3935aefb655Srie static void
plt_entry(Ofl_desc * ofl,Xword pltndx,Xword * roffset,Sxword * raddend)3945aefb655Srie plt_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
3955aefb655Srie {
3965aefb655Srie uchar_t *pltent; /* PLT entry being created. */
3975aefb655Srie Sxword pltoff; /* Offset of this entry from PLT top */
398ba2be530Sab196087 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
3995aefb655Srie
4005aefb655Srie /*
4015aefb655Srie * The second part of the V9 ABI (sec. 5.2.4)
4025aefb655Srie * applies to plt entries greater than 0x8000 (32,768).
4035aefb655Srie * This is handled in 'plt_far_entry()'
4045aefb655Srie */
4055aefb655Srie if ((pltndx - 1 + M_PLT_XNumber) >= M64_PLT_NEARPLTS) {
4065aefb655Srie plt_far_entry(ofl, pltndx, roffset, raddend);
4075aefb655Srie return;
4085aefb655Srie }
4095aefb655Srie
4105aefb655Srie pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE;
411de777a60Sab196087 pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf + pltoff;
4125aefb655Srie
4135aefb655Srie *roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
4145aefb655Srie *raddend = 0;
4155aefb655Srie
4165aefb655Srie /*
4175aefb655Srie * PLT[0]: sethi %hi(. - .L0), %g1
4185aefb655Srie */
4195aefb655Srie /* LINTED */
4205aefb655Srie *(Word *)pltent = M_SETHIG1 | pltoff;
421ba2be530Sab196087 if (bswap)
422ba2be530Sab196087 /* LINTED */
423ba2be530Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4245aefb655Srie
4255aefb655Srie /*
4265aefb655Srie * PLT[1]: ba,a %xcc, .PLT1 (.PLT1 accessed as a
4275aefb655Srie * PC-relative index of longwords).
4285aefb655Srie */
4295aefb655Srie pltent += M_PLT_INSSIZE;
4305aefb655Srie pltoff += M_PLT_INSSIZE;
4315aefb655Srie pltoff = -pltoff;
4325aefb655Srie /* LINTED */
4335aefb655Srie *(Word *)pltent = M_BA_A_XCC |
4345aefb655Srie (((pltoff + M_PLT_ENTSIZE) >> 2) & S_MASK(19));
435ba2be530Sab196087 if (bswap)
436ba2be530Sab196087 /* LINTED */
437ba2be530Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4385aefb655Srie
4395aefb655Srie /*
4405aefb655Srie * PLT[2]: sethi 0, %g0 (NOP for delay slot of eventual CTI).
4415aefb655Srie */
4425aefb655Srie pltent += M_PLT_INSSIZE;
4435aefb655Srie /* LINTED */
4445aefb655Srie *(Word *)pltent = M_NOP;
445ba2be530Sab196087 if (bswap)
446ba2be530Sab196087 /* LINTED */
447ba2be530Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4485aefb655Srie
4495aefb655Srie /*
4505aefb655Srie * PLT[3]: sethi 0, %g0 (NOP for PLT padding).
4515aefb655Srie */
4525aefb655Srie pltent += M_PLT_INSSIZE;
4535aefb655Srie /* LINTED */
4545aefb655Srie *(Word *)pltent = M_NOP;
455ba2be530Sab196087 if (bswap)
456ba2be530Sab196087 /* LINTED */
457ba2be530Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4585aefb655Srie
4595aefb655Srie /*
4605aefb655Srie * PLT[4]: sethi 0, %g0 (NOP for PLT padding).
4615aefb655Srie */
4625aefb655Srie pltent += M_PLT_INSSIZE;
4635aefb655Srie /* LINTED */
4645aefb655Srie *(Word *)pltent = M_NOP;
465ba2be530Sab196087 if (bswap)
466ba2be530Sab196087 /* LINTED */
467ba2be530Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4685aefb655Srie
4695aefb655Srie /*
4705aefb655Srie * PLT[5]: sethi 0, %g0 (NOP for PLT padding).
4715aefb655Srie */
4725aefb655Srie pltent += M_PLT_INSSIZE;
4735aefb655Srie /* LINTED */
4745aefb655Srie *(Word *)pltent = M_NOP;
475ba2be530Sab196087 if (bswap)
476ba2be530Sab196087 /* LINTED */
477ba2be530Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4785aefb655Srie
4795aefb655Srie /*
4805aefb655Srie * PLT[6]: sethi 0, %g0 (NOP for PLT padding).
4815aefb655Srie */
4825aefb655Srie pltent += M_PLT_INSSIZE;
4835aefb655Srie /* LINTED */
4845aefb655Srie *(Word *)pltent = M_NOP;
485ba2be530Sab196087 if (bswap)
486ba2be530Sab196087 /* LINTED */
487ba2be530Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4885aefb655Srie
4895aefb655Srie /*
4905aefb655Srie * PLT[7]: sethi 0, %g0 (NOP for PLT padding).
4915aefb655Srie */
4925aefb655Srie pltent += M_PLT_INSSIZE;
4935aefb655Srie /* LINTED */
4945aefb655Srie *(Word *)pltent = M_NOP;
495ba2be530Sab196087 if (bswap)
496ba2be530Sab196087 /* LINTED */
497ba2be530Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
4985aefb655Srie }
4995aefb655Srie
5005aefb655Srie
5015aefb655Srie #else /* Elf 32 */
5025aefb655Srie
503ba2be530Sab196087 static Xword
ld_calc_plt_addr(Sym_desc * sdp,Ofl_desc * ofl)5045aefb655Srie ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
5055aefb655Srie {
5065aefb655Srie Xword value, pltndx;
5075aefb655Srie
5085aefb655Srie pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1;
5095aefb655Srie value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
5105aefb655Srie (pltndx * M_PLT_ENTSIZE);
5115aefb655Srie return (value);
5125aefb655Srie }
5135aefb655Srie
5145aefb655Srie
5155aefb655Srie /*
5165aefb655Srie * Build a single P.L.T. entry - code is:
5175aefb655Srie *
5185aefb655Srie * sethi (. - .L0), %g1
5195aefb655Srie * ba,a .L0
5205aefb655Srie * sethi 0, %g0 (nop)
5215aefb655Srie *
5225aefb655Srie * .L0 is the address of the first entry in the P.L.T.
5235aefb655Srie * This one is filled in by the run-time link editor. We just
5245aefb655Srie * have to leave space for it.
5255aefb655Srie */
5265aefb655Srie static void
plt_entry(Ofl_desc * ofl,Xword pltndx,Xword * roffset,Sxword * raddend)5275aefb655Srie plt_entry(Ofl_desc * ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
5285aefb655Srie {
5295aefb655Srie Byte *pltent; /* PLT entry being created. */
5305aefb655Srie Sxword pltoff; /* Offset of this entry from PLT top */
531ba2be530Sab196087 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
5325aefb655Srie
5335aefb655Srie pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE;
5345aefb655Srie pltent = (Byte *)ofl->ofl_osplt->os_outdata->d_buf + pltoff;
5355aefb655Srie
5365aefb655Srie *roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
5375aefb655Srie *raddend = 0;
5385aefb655Srie
5395aefb655Srie /*
5405aefb655Srie * PLT[0]: sethi %hi(. - .L0), %g1
5415aefb655Srie */
5425aefb655Srie /* LINTED */
5435aefb655Srie *(Word *)pltent = M_SETHIG1 | pltoff;
544ba2be530Sab196087 if (bswap)
545ba2be530Sab196087 /* LINTED */
546ba2be530Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
5475aefb655Srie
5485aefb655Srie /*
5495aefb655Srie * PLT[1]: ba,a .L0 (.L0 accessed as a PC-relative index of longwords)
5505aefb655Srie */
5515aefb655Srie pltent += M_PLT_INSSIZE;
5525aefb655Srie pltoff += M_PLT_INSSIZE;
5535aefb655Srie pltoff = -pltoff;
5545aefb655Srie /* LINTED */
5555aefb655Srie *(Word *)pltent = M_BA_A | ((pltoff >> 2) & S_MASK(22));
556ba2be530Sab196087 if (bswap)
557ba2be530Sab196087 /* LINTED */
558ba2be530Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
5595aefb655Srie
5605aefb655Srie /*
5615aefb655Srie * PLT[2]: sethi 0, %g0 (NOP for delay slot of eventual CTI).
5625aefb655Srie */
5635aefb655Srie pltent += M_PLT_INSSIZE;
5645aefb655Srie /* LINTED */
5655aefb655Srie *(Word *)pltent = M_SETHIG0;
566ba2be530Sab196087 if (bswap)
567ba2be530Sab196087 /* LINTED */
568ba2be530Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
5695aefb655Srie
5705aefb655Srie /*
5715aefb655Srie * PLT[3]: sethi 0, %g0 (NOP for PLT padding).
5725aefb655Srie */
5735aefb655Srie pltent += M_PLT_INSSIZE;
5745aefb655Srie /* LINTED */
5755aefb655Srie *(Word *)pltent = M_SETHIG0;
576ba2be530Sab196087 if (bswap)
577ba2be530Sab196087 /* LINTED */
578ba2be530Sab196087 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
5795aefb655Srie }
5805aefb655Srie
5815aefb655Srie #endif /* _ELF64 */
5825aefb655Srie
583ba2be530Sab196087 static uintptr_t
ld_perform_outreloc(Rel_desc * orsp,Ofl_desc * ofl,Boolean * remain_seen)5841007fd6fSAli Bahrami ld_perform_outreloc(Rel_desc *orsp, Ofl_desc *ofl, Boolean *remain_seen)
5855aefb655Srie {
58657ef7aa9SRod Evans Os_desc *relosp, *osp = NULL;
5875aefb655Srie Xword ndx, roffset, value;
5885aefb655Srie Sxword raddend;
5895aefb655Srie const Rel_entry *rep;
5905aefb655Srie Rela rea;
5915aefb655Srie char *relbits;
59257ef7aa9SRod Evans Sym_desc *sdp, *psym = NULL;
5935aefb655Srie int sectmoved = 0;
5945aefb655Srie Word dtflags1 = ofl->ofl_dtflags_1;
5951d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags;
5965aefb655Srie
5975aefb655Srie raddend = orsp->rel_raddend;
5985aefb655Srie sdp = orsp->rel_sym;
5995aefb655Srie
6005aefb655Srie /*
6015aefb655Srie * Special case, a regsiter symbol associated with symbol
6025aefb655Srie * index 0 is initialized (i.e. relocated) to a constant
6035aefb655Srie * in the r_addend field rather than to a symbol value.
6045aefb655Srie */
6055aefb655Srie if ((orsp->rel_rtype == M_R_REGISTER) && !sdp) {
6065aefb655Srie relosp = ofl->ofl_osrel;
6075aefb655Srie relbits = (char *)relosp->os_outdata->d_buf;
6085aefb655Srie
6095aefb655Srie rea.r_info = ELF_R_INFO(0,
610bf994817SAli Bahrami ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp),
611bf994817SAli Bahrami orsp->rel_rtype));
6125aefb655Srie rea.r_offset = orsp->rel_roffset;
6135aefb655Srie rea.r_addend = raddend;
6145aefb655Srie DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea,
615bf994817SAli Bahrami relosp->os_name, ld_reloc_sym_name(orsp)));
6165aefb655Srie
6175aefb655Srie assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
6185aefb655Srie (void) memcpy((relbits + relosp->os_szoutrels),
6195aefb655Srie (char *)&rea, sizeof (Rela));
6205aefb655Srie relosp->os_szoutrels += (Xword)sizeof (Rela);
6215aefb655Srie
6225aefb655Srie return (1);
6235aefb655Srie }
6245aefb655Srie
6255aefb655Srie /*
6265aefb655Srie * If the section this relocation is against has been discarded
6275aefb655Srie * (-zignore), then also discard (skip) the relocation itself.
6285aefb655Srie */
6295aefb655Srie if (orsp->rel_isdesc && ((orsp->rel_flags &
6305aefb655Srie (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
6315aefb655Srie (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
6325aefb655Srie DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
6335aefb655Srie return (1);
6345aefb655Srie }
6355aefb655Srie
6365aefb655Srie /*
6375aefb655Srie * If this is a relocation against a move table, or expanded move
6385aefb655Srie * table, adjust the relocation entries.
6395aefb655Srie */
640bf994817SAli Bahrami if (RELAUX_GET_MOVE(orsp))
6415aefb655Srie ld_adj_movereloc(ofl, orsp);
6425aefb655Srie
6435aefb655Srie /*
6445aefb655Srie * If this is a relocation against a section then we need to adjust the
6455aefb655Srie * raddend field to compensate for the new position of the input section
6465aefb655Srie * within the new output section.
6475aefb655Srie */
6485aefb655Srie if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
64957ef7aa9SRod Evans if (ofl->ofl_parsyms &&
6505aefb655Srie (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
6515aefb655Srie (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
6525aefb655Srie /*
6535aefb655Srie * If the symbol is moved, adjust the value
6545aefb655Srie */
6555aefb655Srie DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
6565aefb655Srie sectmoved = 1;
6575aefb655Srie if (ofl->ofl_flags & FLG_OF_RELOBJ)
6585aefb655Srie raddend = psym->sd_sym->st_value;
6595aefb655Srie else
6605aefb655Srie raddend = psym->sd_sym->st_value -
6615aefb655Srie psym->sd_isc->is_osdesc->os_shdr->sh_addr;
6625aefb655Srie /* LINTED */
6635aefb655Srie raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
6645aefb655Srie if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
6655aefb655Srie raddend +=
6665aefb655Srie psym->sd_isc->is_osdesc->os_shdr->sh_addr;
6675aefb655Srie } else {
6685aefb655Srie /* LINTED */
6695aefb655Srie raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
6705aefb655Srie if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
6715aefb655Srie raddend +=
6725aefb655Srie sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
6735aefb655Srie }
6745aefb655Srie }
6755aefb655Srie
6765aefb655Srie value = sdp->sd_sym->st_value;
6775aefb655Srie
6785aefb655Srie if (orsp->rel_flags & FLG_REL_GOT) {
6795aefb655Srie osp = ofl->ofl_osgot;
6805aefb655Srie roffset = ld_calc_got_offset(orsp, ofl);
6815aefb655Srie
6825aefb655Srie } else if (orsp->rel_flags & FLG_REL_PLT) {
6835aefb655Srie osp = ofl->ofl_osplt;
6845aefb655Srie plt_entry(ofl, sdp->sd_aux->sa_PLTndx, &roffset, &raddend);
6855aefb655Srie } else if (orsp->rel_flags & FLG_REL_BSS) {
6865aefb655Srie /*
6875aefb655Srie * This must be a R_SPARC_COPY. For these set the roffset to
6885aefb655Srie * point to the new symbols location.
6895aefb655Srie */
6905aefb655Srie osp = ofl->ofl_isbss->is_osdesc;
6915aefb655Srie roffset = (Xword)value;
6925aefb655Srie
6935aefb655Srie /*
6945aefb655Srie * The raddend doesn't mean anything in an R_SPARC_COPY
6955aefb655Srie * relocation. Null it out because it can confuse people.
6965aefb655Srie */
6975aefb655Srie raddend = 0;
6985aefb655Srie } else if (orsp->rel_flags & FLG_REL_REG) {
6995aefb655Srie /*
7005aefb655Srie * The offsets of relocations against register symbols
7015aefb655Srie * identifiy the register directly - so the offset
7025aefb655Srie * does not need to be adjusted.
7035aefb655Srie */
7045aefb655Srie roffset = orsp->rel_roffset;
7055aefb655Srie } else {
706bf994817SAli Bahrami osp = RELAUX_GET_OSDESC(orsp);
7075aefb655Srie
7085aefb655Srie /*
7095aefb655Srie * Calculate virtual offset of reference point; equals offset
7105aefb655Srie * into section + vaddr of section for loadable sections, or
7115aefb655Srie * offset plus section displacement for nonloadable sections.
7125aefb655Srie */
7135aefb655Srie roffset = orsp->rel_roffset +
7145aefb655Srie (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
7155aefb655Srie if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
7165aefb655Srie roffset += orsp->rel_isdesc->is_osdesc->
7175aefb655Srie os_shdr->sh_addr;
7185aefb655Srie }
7195aefb655Srie
7205aefb655Srie if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
7215aefb655Srie relosp = ofl->ofl_osrel;
7225aefb655Srie
7235aefb655Srie /*
7245aefb655Srie * Verify that the output relocations offset meets the
7255aefb655Srie * alignment requirements of the relocation being processed.
7265aefb655Srie */
7275aefb655Srie rep = &reloc_table[orsp->rel_rtype];
7285aefb655Srie if (((flags & FLG_OF_RELOBJ) || !(dtflags1 & DF_1_NORELOC)) &&
7295aefb655Srie !(rep->re_flags & FLG_RE_UNALIGN)) {
7305aefb655Srie if (((rep->re_fsize == 2) && (roffset & 0x1)) ||
7315aefb655Srie ((rep->re_fsize == 4) && (roffset & 0x3)) ||
7325aefb655Srie ((rep->re_fsize == 8) && (roffset & 0x7))) {
733de777a60Sab196087 Conv_inv_buf_t inv_buf;
734de777a60Sab196087
7351007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NONALIGN),
736de777a60Sab196087 conv_reloc_SPARC_type(orsp->rel_rtype, 0, &inv_buf),
7375aefb655Srie orsp->rel_isdesc->is_file->ifl_name,
738bf994817SAli Bahrami ld_reloc_sym_name(orsp), EC_XWORD(roffset));
7395aefb655Srie return (S_ERROR);
7405aefb655Srie }
7415aefb655Srie }
7425aefb655Srie
7435aefb655Srie /*
7445aefb655Srie * Assign the symbols index for the output relocation. If the
7455aefb655Srie * relocation refers to a SECTION symbol then it's index is based upon
7465aefb655Srie * the output sections symbols index. Otherwise the index can be
7475aefb655Srie * derived from the symbols index itself.
7485aefb655Srie */
7495aefb655Srie if (orsp->rel_rtype == R_SPARC_RELATIVE)
7505aefb655Srie ndx = STN_UNDEF;
7515aefb655Srie else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
7525aefb655Srie (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
7535aefb655Srie if (sectmoved == 0) {
7545aefb655Srie /*
7555aefb655Srie * Check for a null input section. This can
7565aefb655Srie * occur if this relocation references a symbol
7575aefb655Srie * generated by sym_add_sym().
7585aefb655Srie */
75957ef7aa9SRod Evans if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
76057ef7aa9SRod Evans ndx = sdp->sd_isc->is_osdesc->os_identndx;
7615aefb655Srie else
7625aefb655Srie ndx = sdp->sd_shndx;
7635aefb655Srie } else
76435450702SAli Bahrami ndx = ofl->ofl_parexpnndx;
7655aefb655Srie } else
7665aefb655Srie ndx = sdp->sd_symndx;
7675aefb655Srie
7685aefb655Srie /*
7695aefb655Srie * Add the symbols 'value' to the addend field.
7705aefb655Srie */
7715aefb655Srie if (orsp->rel_flags & FLG_REL_ADVAL)
7725aefb655Srie raddend += value;
7735aefb655Srie
7745aefb655Srie /*
7757010c12aSrie * The addend field for R_SPARC_TLS_DTPMOD32 and R_SPARC_TLS_DTPMOD64
7767010c12aSrie * mean nothing. The addend is propagated in the corresponding
7777010c12aSrie * R_SPARC_TLS_DTPOFF* relocations.
7785aefb655Srie */
7797010c12aSrie if (orsp->rel_rtype == M_R_DTPMOD)
7805aefb655Srie raddend = 0;
7815aefb655Srie
7820bc0887eSRichard Lowe /*
7830bc0887eSRichard Lowe * Note that the other case which writes out the relocation, above, is
7840bc0887eSRichard Lowe * M_R_REGISTER specific and so does not need this check.
7850bc0887eSRichard Lowe */
7860bc0887eSRichard Lowe if ((orsp->rel_rtype != M_R_NONE) &&
7870bc0887eSRichard Lowe (orsp->rel_rtype != M_R_REGISTER) &&
7880bc0887eSRichard Lowe (orsp->rel_rtype != M_R_RELATIVE)) {
7890bc0887eSRichard Lowe if (ndx == 0) {
7900bc0887eSRichard Lowe Conv_inv_buf_t inv_buf;
7910bc0887eSRichard Lowe Is_desc *isp = orsp->rel_isdesc;
7920bc0887eSRichard Lowe
7930bc0887eSRichard Lowe ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NOSYMBOL),
7940bc0887eSRichard Lowe conv_reloc_type(ofl->ofl_nehdr->e_machine,
7950bc0887eSRichard Lowe orsp->rel_rtype, 0, &inv_buf),
7960bc0887eSRichard Lowe isp->is_file->ifl_name, EC_WORD(isp->is_scnndx),
7970bc0887eSRichard Lowe isp->is_name, EC_XWORD(roffset));
7980bc0887eSRichard Lowe return (S_ERROR);
7990bc0887eSRichard Lowe }
8000bc0887eSRichard Lowe }
8015aefb655Srie
802bf994817SAli Bahrami rea.r_info = ELF_R_INFO(ndx,
803bf994817SAli Bahrami ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp), orsp->rel_rtype));
8045aefb655Srie rea.r_offset = roffset;
8055aefb655Srie rea.r_addend = raddend;
8065aefb655Srie DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
807bf994817SAli Bahrami ld_reloc_sym_name(orsp)));
8085aefb655Srie
8095aefb655Srie /*
8105aefb655Srie * Assert we haven't walked off the end of our relocation table.
8115aefb655Srie */
8125aefb655Srie assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
8135aefb655Srie
8140bc0887eSRichard Lowe relbits = (char *)relosp->os_outdata->d_buf;
8150bc0887eSRichard Lowe
8165aefb655Srie (void) memcpy((relbits + relosp->os_szoutrels),
8175aefb655Srie (char *)&rea, sizeof (Rela));
8185aefb655Srie relosp->os_szoutrels += (Xword)sizeof (Rela);
8195aefb655Srie
8205aefb655Srie /*
8215aefb655Srie * Determine if this relocation is against a non-writable, allocatable
8225aefb655Srie * section. If so we may need to provide a text relocation diagnostic.
8235aefb655Srie */
8241007fd6fSAli Bahrami ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
8255aefb655Srie return (1);
8265aefb655Srie }
8275aefb655Srie
8285aefb655Srie
8295aefb655Srie /*
8305aefb655Srie * Sparc Instructions for TLS processing
8315aefb655Srie */
8325aefb655Srie #if defined(_ELF64)
8335aefb655Srie #define TLS_GD_IE_LD 0xd0580000 /* ldx [%g0 + %g0], %o0 */
8345aefb655Srie #else
8355aefb655Srie #define TLS_GD_IE_LD 0xd0000000 /* ld [%g0 + %g0], %o0 */
8365aefb655Srie #endif
8375aefb655Srie #define TLS_GD_IE_ADD 0x9001c008 /* add %g7, %o0, %o0 */
8385aefb655Srie
8395aefb655Srie #define TLS_GD_LE_XOR 0x80182000 /* xor %g0, 0, %g0 */
8405aefb655Srie #define TLS_IE_LE_OR 0x80100000 /* or %g0, %o0, %o1 */
8415aefb655Srie /* synthetic: mov %g0, %g0 */
8425aefb655Srie
8435aefb655Srie #define TLS_LD_LE_CLRO0 0x90100000 /* clr %o0 */
8445aefb655Srie
8455aefb655Srie #define FM3_REG_MSK_RD (0x1f << 25) /* Formate (3) rd register mask */
8465aefb655Srie /* bits 25->29 */
8475aefb655Srie #define FM3_REG_MSK_RS1 (0x1f << 14) /* Formate (3) rs1 register mask */
8485aefb655Srie /* bits 14->18 */
8495aefb655Srie #define FM3_REG_MSK_RS2 0x1f /* Formate (3) rs2 register mask */
8505aefb655Srie /* bits 0->4 */
8515aefb655Srie
8525aefb655Srie #define REG_G7 7 /* %g7 register */
8535aefb655Srie
8545aefb655Srie static Fixupret
tls_fixups(Ofl_desc * ofl,Rel_desc * arsp)8555aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
8565aefb655Srie {
8575aefb655Srie Sym_desc *sdp = arsp->rel_sym;
8585aefb655Srie Word rtype = arsp->rel_rtype;
859ba2be530Sab196087 Word *offset, w;
860ba2be530Sab196087 int bswap = OFL_SWAP_RELOC_DATA(ofl, arsp);
8615aefb655Srie
862ba2be530Sab196087
863ba2be530Sab196087 offset = (Word *)((uintptr_t)arsp->rel_roffset +
8645aefb655Srie (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
865bf994817SAli Bahrami (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
8665aefb655Srie
8675aefb655Srie if (sdp->sd_ref == REF_DYN_NEED) {
8685aefb655Srie /*
8695aefb655Srie * IE reference model
8705aefb655Srie */
8715aefb655Srie switch (rtype) {
8725aefb655Srie case R_SPARC_TLS_GD_HI22:
8735aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
874bf994817SAli Bahrami R_SPARC_TLS_IE_HI22, arsp,
875bf994817SAli Bahrami ld_reloc_sym_name));
8765aefb655Srie arsp->rel_rtype = R_SPARC_TLS_IE_HI22;
8775aefb655Srie return (FIX_RELOC);
8785aefb655Srie
8795aefb655Srie case R_SPARC_TLS_GD_LO10:
8805aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
881bf994817SAli Bahrami R_SPARC_TLS_IE_LO10, arsp,
882bf994817SAli Bahrami ld_reloc_sym_name));
8835aefb655Srie arsp->rel_rtype = R_SPARC_TLS_IE_LO10;
8845aefb655Srie return (FIX_RELOC);
8855aefb655Srie
8865aefb655Srie case R_SPARC_TLS_GD_ADD:
8875aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
888bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
889ba2be530Sab196087 w = bswap ? ld_bswap_Word(*offset) : *offset;
890ba2be530Sab196087 w = (TLS_GD_IE_LD |
891ba2be530Sab196087 (w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RS2)));
892ba2be530Sab196087 *offset = bswap ? ld_bswap_Word(w) : w;
8935aefb655Srie return (FIX_DONE);
8945aefb655Srie
8955aefb655Srie case R_SPARC_TLS_GD_CALL:
8965aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
897bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
8985aefb655Srie *offset = TLS_GD_IE_ADD;
899ba2be530Sab196087 if (bswap)
900ba2be530Sab196087 *offset = ld_bswap_Word(*offset);
9015aefb655Srie return (FIX_DONE);
9025aefb655Srie }
9035aefb655Srie return (FIX_RELOC);
9045aefb655Srie }
9055aefb655Srie
9065aefb655Srie /*
9075aefb655Srie * LE reference model
9085aefb655Srie */
9095aefb655Srie switch (rtype) {
9105aefb655Srie case R_SPARC_TLS_IE_HI22:
9115aefb655Srie case R_SPARC_TLS_GD_HI22:
9125aefb655Srie case R_SPARC_TLS_LDO_HIX22:
913051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
914bf994817SAli Bahrami R_SPARC_TLS_LE_HIX22, arsp, ld_reloc_sym_name));
9155aefb655Srie arsp->rel_rtype = R_SPARC_TLS_LE_HIX22;
9165aefb655Srie return (FIX_RELOC);
9175aefb655Srie
9185aefb655Srie case R_SPARC_TLS_LDO_LOX10:
919051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
920bf994817SAli Bahrami R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name));
9215aefb655Srie arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
9225aefb655Srie return (FIX_RELOC);
9235aefb655Srie
9245aefb655Srie case R_SPARC_TLS_IE_LO10:
9255aefb655Srie case R_SPARC_TLS_GD_LO10:
9265aefb655Srie /*
9275aefb655Srie * Current instruction is:
9285aefb655Srie *
9295aefb655Srie * or r1, %lo(x), r2
9305aefb655Srie * or
9315aefb655Srie * add r1, %lo(x), r2
9325aefb655Srie *
9335aefb655Srie * Need to udpate this to:
9345aefb655Srie *
9355aefb655Srie * xor r1, %lox(x), r2
9365aefb655Srie */
937051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
938bf994817SAli Bahrami R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name));
939ba2be530Sab196087 w = bswap ? ld_bswap_Word(*offset) : *offset;
940ba2be530Sab196087 w = TLS_GD_LE_XOR |
941ba2be530Sab196087 (w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RD));
942ba2be530Sab196087 *offset = bswap ? ld_bswap_Word(w) : w;
9435aefb655Srie arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
9445aefb655Srie return (FIX_RELOC);
9455aefb655Srie
9465aefb655Srie case R_SPARC_TLS_IE_LD:
9475aefb655Srie case R_SPARC_TLS_IE_LDX:
9485aefb655Srie /*
9495aefb655Srie * Current instruction:
9505aefb655Srie * ld{x} [r1 + r2], r3
9515aefb655Srie *
9525aefb655Srie * Need to update this to:
9535aefb655Srie *
9545aefb655Srie * mov r2, r3 (or %g0, r2, r3)
9555aefb655Srie */
956051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
957bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
958ba2be530Sab196087 w = bswap ? ld_bswap_Word(*offset) : *offset;
959ba2be530Sab196087 w = (w & (FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | TLS_IE_LE_OR;
960ba2be530Sab196087 *offset = bswap ? ld_bswap_Word(w) : w;
9615aefb655Srie return (FIX_DONE);
9625aefb655Srie
9635aefb655Srie case R_SPARC_TLS_LDO_ADD:
9645aefb655Srie case R_SPARC_TLS_GD_ADD:
9655aefb655Srie /*
9665aefb655Srie * Current instruction is:
9675aefb655Srie *
9685aefb655Srie * add gptr_reg, r2, r3
9695aefb655Srie *
9705aefb655Srie * Need to updated this to:
9715aefb655Srie *
9725aefb655Srie * add %g7, r2, r3
9735aefb655Srie */
974051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
975bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
976ba2be530Sab196087 w = bswap ? ld_bswap_Word(*offset) : *offset;
977ba2be530Sab196087 w = w & (~FM3_REG_MSK_RS1);
978ba2be530Sab196087 w = w | (REG_G7 << 14);
979ba2be530Sab196087 *offset = bswap ? ld_bswap_Word(w) : w;
9805aefb655Srie return (FIX_DONE);
9815aefb655Srie
9825aefb655Srie case R_SPARC_TLS_LDM_CALL:
983051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
984bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
9855aefb655Srie *offset = TLS_LD_LE_CLRO0;
986ba2be530Sab196087 if (bswap)
987ba2be530Sab196087 *offset = ld_bswap_Word(*offset);
9885aefb655Srie return (FIX_DONE);
9895aefb655Srie
9905aefb655Srie case R_SPARC_TLS_LDM_HI22:
9915aefb655Srie case R_SPARC_TLS_LDM_LO10:
9925aefb655Srie case R_SPARC_TLS_LDM_ADD:
9935aefb655Srie case R_SPARC_TLS_IE_ADD:
9945aefb655Srie case R_SPARC_TLS_GD_CALL:
995051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
996bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
9975aefb655Srie *offset = M_NOP;
998ba2be530Sab196087 if (bswap)
999ba2be530Sab196087 *offset = ld_bswap_Word(*offset);
10005aefb655Srie return (FIX_DONE);
10015aefb655Srie }
10025aefb655Srie return (FIX_RELOC);
10035aefb655Srie }
10045aefb655Srie
10055aefb655Srie #define GOTOP_ADDINST 0x80000000 /* add %g0, %g0, %g0 */
10065aefb655Srie
10075aefb655Srie static Fixupret
gotop_fixups(Ofl_desc * ofl,Rel_desc * arsp)10085aefb655Srie gotop_fixups(Ofl_desc *ofl, Rel_desc *arsp)
10095aefb655Srie {
10105aefb655Srie Word rtype = arsp->rel_rtype;
1011ba2be530Sab196087 Word *offset, w;
10125aefb655Srie const char *ifl_name;
1013de777a60Sab196087 Conv_inv_buf_t inv_buf;
1014ba2be530Sab196087 int bswap;
10155aefb655Srie
10165aefb655Srie switch (rtype) {
10175aefb655Srie case R_SPARC_GOTDATA_OP_HIX22:
1018051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
1019bf994817SAli Bahrami R_SPARC_GOTDATA_HIX22, arsp, ld_reloc_sym_name));
10205aefb655Srie arsp->rel_rtype = R_SPARC_GOTDATA_HIX22;
10215aefb655Srie return (FIX_RELOC);
10225aefb655Srie
10235aefb655Srie case R_SPARC_GOTDATA_OP_LOX10:
1024051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
1025bf994817SAli Bahrami R_SPARC_GOTDATA_LOX10, arsp, ld_reloc_sym_name));
10265aefb655Srie arsp->rel_rtype = R_SPARC_GOTDATA_LOX10;
10275aefb655Srie return (FIX_RELOC);
10285aefb655Srie
10295aefb655Srie case R_SPARC_GOTDATA_OP:
10305aefb655Srie /*
10315aefb655Srie * Current instruction:
10325aefb655Srie * ld{x} [r1 + r2], r3
10335aefb655Srie *
10345aefb655Srie * Need to update this to:
10355aefb655Srie *
10365aefb655Srie * add r1, r2, r3
10375aefb655Srie */
1038051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
1039bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
1040ba2be530Sab196087 offset = (Word *)(uintptr_t)(arsp->rel_roffset +
10415aefb655Srie _elf_getxoff(arsp->rel_isdesc->is_indata) +
1042bf994817SAli Bahrami (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
1043ba2be530Sab196087 bswap = OFL_SWAP_RELOC_DATA(ofl, arsp);
1044ba2be530Sab196087 w = bswap ? ld_bswap_Word(*offset) : *offset;
1045ba2be530Sab196087 w = (w & (FM3_REG_MSK_RS1 |
10465aefb655Srie FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | GOTOP_ADDINST;
1047ba2be530Sab196087 *offset = bswap ? ld_bswap_Word(w) : w;
10485aefb655Srie return (FIX_DONE);
10495aefb655Srie }
10505aefb655Srie /*
10515aefb655Srie * We should not get here
10525aefb655Srie */
10535aefb655Srie if (arsp->rel_isdesc->is_file)
10545aefb655Srie ifl_name = arsp->rel_isdesc->is_file->ifl_name;
10555aefb655Srie else
10565aefb655Srie ifl_name = MSG_INTL(MSG_STR_NULL);
10575aefb655Srie
10581007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADGOTFIX),
1059de777a60Sab196087 conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
1060bf994817SAli Bahrami ifl_name, ld_reloc_sym_name(arsp));
10615aefb655Srie
10625aefb655Srie assert(0);
10635aefb655Srie return (FIX_ERROR);
10645aefb655Srie }
10655aefb655Srie
1066ba2be530Sab196087 static uintptr_t
ld_do_activerelocs(Ofl_desc * ofl)10675aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
10685aefb655Srie {
10695aefb655Srie Rel_desc *arsp;
1070bf994817SAli Bahrami Rel_cachebuf *rcbp;
107157ef7aa9SRod Evans Aliste idx;
10725aefb655Srie uintptr_t return_code = 1;
10731d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags;
10745aefb655Srie
1075bf994817SAli Bahrami if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
10765aefb655Srie DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
10777010c12aSrie
10785aefb655Srie /*
10795aefb655Srie * Process active relocations.
10805aefb655Srie */
1081bf994817SAli Bahrami REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
10825aefb655Srie uchar_t *addr;
10835aefb655Srie Xword value;
10845aefb655Srie Sym_desc *sdp;
10855aefb655Srie const char *ifl_name;
10865aefb655Srie Xword refaddr;
1087bf994817SAli Bahrami Os_desc *osp;
10885aefb655Srie
10895aefb655Srie /*
1090bf994817SAli Bahrami * If the section this relocation is against has been discarded
1091bf994817SAli Bahrami * (-zignore), then discard (skip) the relocation itself.
10925aefb655Srie */
10935aefb655Srie if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
1094bf994817SAli Bahrami ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
10955aefb655Srie FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
1096bf994817SAli Bahrami DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
10975aefb655Srie continue;
10985aefb655Srie }
10995aefb655Srie
11005aefb655Srie /*
11015aefb655Srie * Perform any required TLS fixups.
11025aefb655Srie */
11035aefb655Srie if (arsp->rel_flags & FLG_REL_TLSFIX) {
11045aefb655Srie Fixupret ret;
11055aefb655Srie
11065aefb655Srie if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
11075aefb655Srie return (S_ERROR);
11085aefb655Srie if (ret == FIX_DONE)
11095aefb655Srie continue;
11105aefb655Srie }
11115aefb655Srie
11125aefb655Srie /*
11135aefb655Srie * Perform any required GOTOP fixups.
11145aefb655Srie */
11155aefb655Srie if (arsp->rel_flags & FLG_REL_GOTFIX) {
11165aefb655Srie Fixupret ret;
11175aefb655Srie
1118bf994817SAli Bahrami if ((ret = gotop_fixups(ofl, arsp)) == FIX_ERROR)
11195aefb655Srie return (S_ERROR);
11205aefb655Srie if (ret == FIX_DONE)
11215aefb655Srie continue;
11225aefb655Srie }
11235aefb655Srie
11245aefb655Srie /*
11255aefb655Srie * If this is a relocation against the move table, or
11265aefb655Srie * expanded move table, adjust the relocation entries.
11275aefb655Srie */
1128bf994817SAli Bahrami if (RELAUX_GET_MOVE(arsp))
11295aefb655Srie ld_adj_movereloc(ofl, arsp);
11305aefb655Srie
11315aefb655Srie sdp = arsp->rel_sym;
11325aefb655Srie refaddr = arsp->rel_roffset +
11335aefb655Srie (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
11345aefb655Srie
11355aefb655Srie if ((arsp->rel_flags & FLG_REL_CLVAL) ||
11365aefb655Srie (arsp->rel_flags & FLG_REL_GOTCL))
11375aefb655Srie value = 0;
1138bf994817SAli Bahrami else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
11395aefb655Srie Sym_desc *sym;
11405aefb655Srie
11415aefb655Srie /*
11425aefb655Srie * The value for a symbol pointing to a SECTION
11435aefb655Srie * is based off of that sections position.
11445aefb655Srie */
11455aefb655Srie if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
1146bf994817SAli Bahrami (sym = ld_am_I_partial(arsp, arsp->rel_raddend))) {
11475aefb655Srie /*
1148bf994817SAli Bahrami * The symbol was moved, so adjust the value
1149bf994817SAli Bahrami * relative to the new section.
11505aefb655Srie */
1151bf994817SAli Bahrami value = _elf_getxoff(sym->sd_isc->is_indata);
1152bf994817SAli Bahrami if (sym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
11535aefb655Srie value += sym->sd_isc->
11545aefb655Srie is_osdesc->os_shdr->sh_addr;
1155b26cc8daSAli Bahrami
1156b26cc8daSAli Bahrami /*
1157bf994817SAli Bahrami * The original raddend covers the displacement
1158bf994817SAli Bahrami * from the section start to the desired
1159bf994817SAli Bahrami * address. The value computed above gets us
1160bf994817SAli Bahrami * from the section start to the start of the
1161bf994817SAli Bahrami * symbol range. Adjust the old raddend to
1162bf994817SAli Bahrami * remove the offset from section start to
1163bf994817SAli Bahrami * symbol start, leaving the displacement
1164bf994817SAli Bahrami * within the range of the symbol.
1165b26cc8daSAli Bahrami */
1166bf994817SAli Bahrami arsp->rel_raddend -= sym->sd_osym->st_value;
11675aefb655Srie } else {
1168bf994817SAli Bahrami value = _elf_getxoff(sdp->sd_isc->is_indata);
1169bf994817SAli Bahrami if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
11705aefb655Srie value += sdp->sd_isc->
11715aefb655Srie is_osdesc->os_shdr->sh_addr;
11725aefb655Srie }
11735aefb655Srie
11745aefb655Srie if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
11755aefb655Srie value -= ofl->ofl_tlsphdr->p_vaddr;
11762926dd2eSrie
11772926dd2eSrie } else if (IS_SIZE(arsp->rel_rtype)) {
11782926dd2eSrie /*
11792926dd2eSrie * Size relocations require the symbols size.
11802926dd2eSrie */
11812926dd2eSrie value = sdp->sd_sym->st_size;
118208278a5eSRod Evans
118308278a5eSRod Evans } else if ((sdp->sd_flags & FLG_SY_CAP) &&
118408278a5eSRod Evans sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
11855aefb655Srie /*
1186bf994817SAli Bahrami * If relocation is against a capabilities symbol, we
1187bf994817SAli Bahrami * need to jump to an associated PLT, so that at runtime
1188bf994817SAli Bahrami * ld.so.1 is involved to determine the best binding
1189bf994817SAli Bahrami * choice. Otherwise, the value is the symbols value.
11905aefb655Srie */
119108278a5eSRod Evans value = ld_calc_plt_addr(sdp, ofl);
119208278a5eSRod Evans
119308278a5eSRod Evans } else
11945aefb655Srie value = sdp->sd_sym->st_value;
11955aefb655Srie
11965aefb655Srie /*
11975aefb655Srie * Relocation against the GLOBAL_OFFSET_TABLE.
11985aefb655Srie */
1199bf994817SAli Bahrami if ((arsp->rel_flags & FLG_REL_GOT) &&
1200bf994817SAli Bahrami !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
1201bf994817SAli Bahrami return (S_ERROR);
1202bf994817SAli Bahrami osp = RELAUX_GET_OSDESC(arsp);
12035aefb655Srie
12045aefb655Srie /*
1205bf994817SAli Bahrami * If loadable and not producing a relocatable object add the
1206bf994817SAli Bahrami * sections virtual address to the reference address.
12075aefb655Srie */
12085aefb655Srie if ((arsp->rel_flags & FLG_REL_LOAD) &&
12095aefb655Srie ((flags & FLG_OF_RELOBJ) == 0))
1210bf994817SAli Bahrami refaddr +=
1211bf994817SAli Bahrami arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr;
12125aefb655Srie
12135aefb655Srie /*
1214bf994817SAli Bahrami * If this entry has a PLT assigned to it, its value is actually
1215bf994817SAli Bahrami * the address of the PLT (and not the address of the function).
12165aefb655Srie */
12175aefb655Srie if (IS_PLT(arsp->rel_rtype)) {
12185aefb655Srie if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
12195aefb655Srie value = ld_calc_plt_addr(sdp, ofl);
12205aefb655Srie }
12215aefb655Srie
12225aefb655Srie /*
12235aefb655Srie * Add relocations addend to value. Add extra
12245aefb655Srie * relocation addend if needed.
12255aefb655Srie */
1226bf2f215aSAli Bahrami value += arsp->rel_raddend;
12275aefb655Srie if (IS_EXTOFFSET(arsp->rel_rtype))
1228bf994817SAli Bahrami value += RELAUX_GET_TYPEDATA(arsp);
12295aefb655Srie
12305aefb655Srie /*
1231bf994817SAli Bahrami * Determine whether the value needs further adjustment. Filter
1232bf994817SAli Bahrami * through the attributes of the relocation to determine what
1233bf994817SAli Bahrami * adjustment is required. Note, many of the following cases
1234bf994817SAli Bahrami * are only applicable when a .got is present. As a .got is
1235bf994817SAli Bahrami * not generated when a relocatable object is being built,
1236bf994817SAli Bahrami * any adjustments that require a .got need to be skipped.
12375aefb655Srie */
12385aefb655Srie if ((arsp->rel_flags & FLG_REL_GOT) &&
12395aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) {
12405aefb655Srie Xword R1addr;
12415aefb655Srie uintptr_t R2addr;
12425aefb655Srie Sword gotndx;
12435aefb655Srie Gotndx *gnp;
12445aefb655Srie Gotref gref;
12455aefb655Srie
12465aefb655Srie /*
12475aefb655Srie * Clear the GOT table entry, on SPARC we clear
12485aefb655Srie * the entry and the 'value' if needed is stored
12495aefb655Srie * in an output relocations addend.
12505aefb655Srie *
12515aefb655Srie * Calculate offset into GOT at which to apply
12525aefb655Srie * the relocation.
12535aefb655Srie */
12545aefb655Srie if (arsp->rel_flags & FLG_REL_DTLS)
12555aefb655Srie gref = GOT_REF_TLSGD;
12565aefb655Srie else if (arsp->rel_flags & FLG_REL_MTLS)
12575aefb655Srie gref = GOT_REF_TLSLD;
12585aefb655Srie else if (arsp->rel_flags & FLG_REL_STLS)
12595aefb655Srie gref = GOT_REF_TLSIE;
12605aefb655Srie else
12615aefb655Srie gref = GOT_REF_GENERIC;
12625aefb655Srie
1263bf994817SAli Bahrami gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
12645aefb655Srie assert(gnp);
12655aefb655Srie
12665aefb655Srie if (arsp->rel_rtype == M_R_DTPOFF)
12675aefb655Srie gotndx = gnp->gn_gotndx + 1;
12685aefb655Srie else
12695aefb655Srie gotndx = gnp->gn_gotndx;
12705aefb655Srie
12715aefb655Srie /* LINTED */
1272bf994817SAli Bahrami R1addr = (Xword)((-neggotoffset * M_GOT_ENTSIZE) +
1273bf994817SAli Bahrami (gotndx * M_GOT_ENTSIZE));
12745aefb655Srie
12755aefb655Srie /*
12765aefb655Srie * Add the GOTs data's offset.
12775aefb655Srie */
1278bf994817SAli Bahrami R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
12795aefb655Srie
12805aefb655Srie DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
1281635216b6SRod Evans ELF_DBG_LD_ACT, M_MACH, SHT_RELA,
1282bf994817SAli Bahrami arsp, R1addr, value, ld_reloc_sym_name));
12835aefb655Srie
12845aefb655Srie /*
12855aefb655Srie * And do it.
12865aefb655Srie */
1287f3324781Sab196087 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
1288bf994817SAli Bahrami *(Xword *)R2addr = ld_bswap_Xword(value);
1289f3324781Sab196087 else
12905aefb655Srie *(Xword *)R2addr = value;
12915aefb655Srie continue;
12925aefb655Srie
12935aefb655Srie } else if (IS_GOT_BASED(arsp->rel_rtype) &&
12945aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) {
12955aefb655Srie value -= (ofl->ofl_osgot->os_shdr->sh_addr +
12965aefb655Srie (-neggotoffset * M_GOT_ENTSIZE));
12975aefb655Srie
12985aefb655Srie } else if (IS_PC_RELATIVE(arsp->rel_rtype)) {
12995aefb655Srie value -= refaddr;
13005aefb655Srie
13015aefb655Srie } else if (IS_TLS_INS(arsp->rel_rtype) &&
13025aefb655Srie IS_GOT_RELATIVE(arsp->rel_rtype) &&
13035aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) {
13045aefb655Srie Gotndx *gnp;
13055aefb655Srie Gotref gref;
13065aefb655Srie
13075aefb655Srie if (arsp->rel_flags & FLG_REL_STLS)
13085aefb655Srie gref = GOT_REF_TLSIE;
13095aefb655Srie else if (arsp->rel_flags & FLG_REL_DTLS)
13105aefb655Srie gref = GOT_REF_TLSGD;
13115aefb655Srie else if (arsp->rel_flags & FLG_REL_MTLS)
13125aefb655Srie gref = GOT_REF_TLSLD;
13135aefb655Srie
1314bf994817SAli Bahrami gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
13155aefb655Srie assert(gnp);
13165aefb655Srie
13175aefb655Srie value = gnp->gn_gotndx * M_GOT_ENTSIZE;
13185aefb655Srie
13195aefb655Srie } else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
13205aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) {
13215aefb655Srie Gotndx *gnp;
13225aefb655Srie
132357ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
13245aefb655Srie GOT_REF_GENERIC, ofl, arsp);
13255aefb655Srie assert(gnp);
13265aefb655Srie
13275aefb655Srie value = gnp->gn_gotndx * M_GOT_ENTSIZE;
13285aefb655Srie
13295aefb655Srie } else if ((arsp->rel_flags & FLG_REL_STLS) &&
13305aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) {
13315aefb655Srie Xword tlsstatsize;
13325aefb655Srie
13335aefb655Srie /*
1334bf994817SAli Bahrami * This is the LE TLS reference model. Static offset is
1335bf994817SAli Bahrami * hard-coded, and negated so that it can be added to
1336bf994817SAli Bahrami * the thread pointer (%g7)
13375aefb655Srie */
1338bf994817SAli Bahrami tlsstatsize =
1339bf994817SAli Bahrami S_ROUND(ofl->ofl_tlsphdr->p_memsz, M_TLSSTATALIGN);
13405aefb655Srie value = -(tlsstatsize - value);
13415aefb655Srie }
13425aefb655Srie
13435aefb655Srie if (arsp->rel_isdesc->is_file)
13445aefb655Srie ifl_name = arsp->rel_isdesc->is_file->ifl_name;
13455aefb655Srie else
13465aefb655Srie ifl_name = MSG_INTL(MSG_STR_NULL);
13475aefb655Srie
13485aefb655Srie /*
1349bf994817SAli Bahrami * Make sure we have data to relocate. Compiler and assembler
1350bf994817SAli Bahrami * developers have been known to generate relocations against
1351bf994817SAli Bahrami * invalid sections (normally .bss), so for their benefit give
1352bf994817SAli Bahrami * them sufficient information to help analyze the problem.
1353bf994817SAli Bahrami * End users should never see this.
13545aefb655Srie */
13555aefb655Srie if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1356de777a60Sab196087 Conv_inv_buf_t inv_buf;
1357de777a60Sab196087
13581007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
1359bf994817SAli Bahrami conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
1360bf994817SAli Bahrami ifl_name, ld_reloc_sym_name(arsp),
13614a8d0ea7SAli Bahrami EC_WORD(arsp->rel_isdesc->is_scnndx),
13625aefb655Srie arsp->rel_isdesc->is_name);
13635aefb655Srie return (S_ERROR);
13645aefb655Srie }
13655aefb655Srie
13665aefb655Srie /*
13675aefb655Srie * Get the address of the data item we need to modify.
13685aefb655Srie */
13695aefb655Srie addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1370bf994817SAli Bahrami (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
13715aefb655Srie
1372635216b6SRod Evans DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
1373bf994817SAli Bahrami M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value,
1374bf994817SAli Bahrami ld_reloc_sym_name));
1375bf994817SAli Bahrami addr += (uintptr_t)osp->os_outdata->d_buf;
13765aefb655Srie
13775aefb655Srie if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
13785aefb655Srie ofl->ofl_size) || (arsp->rel_roffset >
1379bf994817SAli Bahrami osp->os_shdr->sh_size)) {
1380de777a60Sab196087 Conv_inv_buf_t inv_buf;
13815aefb655Srie int class;
13825aefb655Srie
1383bf994817SAli Bahrami if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1384bf994817SAli Bahrami ofl->ofl_size)
13855aefb655Srie class = ERR_FATAL;
13865aefb655Srie else
13875aefb655Srie class = ERR_WARNING;
13885aefb655Srie
13891007fd6fSAli Bahrami ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
1390bf994817SAli Bahrami conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
1391bf994817SAli Bahrami ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1392bf994817SAli Bahrami arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
13935aefb655Srie EC_ADDR((uintptr_t)addr -
13945aefb655Srie (uintptr_t)ofl->ofl_nehdr));
13955aefb655Srie
13965aefb655Srie if (class == ERR_FATAL) {
13975aefb655Srie return_code = S_ERROR;
13985aefb655Srie continue;
13995aefb655Srie }
14005aefb655Srie }
14015aefb655Srie
14025aefb655Srie /*
1403bf994817SAli Bahrami * If '-z noreloc' is specified - skip the do_reloc stage.
14045aefb655Srie */
1405f3324781Sab196087 if (OFL_DO_RELOC(ofl)) {
1406bf994817SAli Bahrami if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1407bf994817SAli Bahrami ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
14081007fd6fSAli Bahrami ofl->ofl_lml) == 0) {
14091007fd6fSAli Bahrami ofl->ofl_flags |= FLG_OF_FATAL;
14105aefb655Srie return_code = S_ERROR;
14115aefb655Srie }
14125aefb655Srie }
14131007fd6fSAli Bahrami }
14145aefb655Srie return (return_code);
14155aefb655Srie }
14165aefb655Srie
1417ba2be530Sab196087 static uintptr_t
ld_add_outrel(Word flags,Rel_desc * rsp,Ofl_desc * ofl)14185aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
14195aefb655Srie {
14205aefb655Srie Rel_desc *orsp;
14215aefb655Srie Sym_desc *sdp = rsp->rel_sym;
1422de777a60Sab196087 Conv_inv_buf_t inv_buf;
14235aefb655Srie
14245aefb655Srie /*
14255aefb655Srie * Static executables *do not* want any relocations against them.
14265aefb655Srie * Since our engine still creates relocations against a WEAK UNDEFINED
14275aefb655Srie * symbol in a static executable, it's best to disable them here
14285aefb655Srie * instead of through out the relocation code.
14295aefb655Srie */
1430635216b6SRod Evans if (OFL_IS_STATIC_EXEC(ofl))
14315aefb655Srie return (1);
14325aefb655Srie
14335aefb655Srie /*
14340bc0887eSRichard Lowe * If the symbol will be reduced, we can't leave outstanding
14350bc0887eSRichard Lowe * relocations against it, as nothing will ever be able to satisfy them
14360bc0887eSRichard Lowe * (and the symbol won't be in .dynsym
14370bc0887eSRichard Lowe */
14380bc0887eSRichard Lowe if ((sdp != NULL) &&
14390bc0887eSRichard Lowe (sdp->sd_sym->st_shndx == SHN_UNDEF) &&
14400bc0887eSRichard Lowe (rsp->rel_rtype != M_R_NONE) &&
14410bc0887eSRichard Lowe (rsp->rel_rtype != M_R_REGISTER) &&
14420bc0887eSRichard Lowe (rsp->rel_rtype != M_R_RELATIVE)) {
14430bc0887eSRichard Lowe if (ld_sym_reducable(ofl, sdp))
14440bc0887eSRichard Lowe return (1);
14450bc0887eSRichard Lowe }
14460bc0887eSRichard Lowe
14470bc0887eSRichard Lowe /*
14485aefb655Srie * Certain relocations do not make sense in a 64bit shared object,
14495aefb655Srie * if building a shared object do a sanity check on the output
14505aefb655Srie * relocations being created.
14515aefb655Srie */
14525aefb655Srie if (ofl->ofl_flags & FLG_OF_SHAROBJ) {
14535aefb655Srie Word rtype = rsp->rel_rtype;
14545aefb655Srie /*
14555aefb655Srie * Because the R_SPARC_HIPLT22 & R_SPARC_LOPLT10 relocations
14565aefb655Srie * are not relative they make no sense to create in a shared
14575aefb655Srie * object - so emit the proper error message if that occurs.
14585aefb655Srie */
1459de777a60Sab196087 if ((rtype == R_SPARC_HIPLT22) || (rtype == R_SPARC_LOPLT10)) {
14601007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_UNRELREL),
1461de777a60Sab196087 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
14625aefb655Srie rsp->rel_isdesc->is_file->ifl_name,
1463bf994817SAli Bahrami ld_reloc_sym_name(rsp));
14645aefb655Srie return (S_ERROR);
14655aefb655Srie }
14665aefb655Srie #if defined(_ELF64)
14675aefb655Srie /*
14685aefb655Srie * Each of the following relocations requires that the
14695aefb655Srie * object being built be loaded in either the upper 32 or
14705aefb655Srie * 44 bit range of memory. Since shared libraries traditionally
14715aefb655Srie * are loaded in the lower range of memory - this isn't going
14725aefb655Srie * to work.
14735aefb655Srie */
14745aefb655Srie if ((rtype == R_SPARC_H44) || (rtype == R_SPARC_M44) ||
14755aefb655Srie (rtype == R_SPARC_L44)) {
14761007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SHOBJABS44),
1477de777a60Sab196087 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
14785aefb655Srie rsp->rel_isdesc->is_file->ifl_name,
1479bf994817SAli Bahrami ld_reloc_sym_name(rsp));
14805aefb655Srie return (S_ERROR);
14815aefb655Srie }
14825aefb655Srie #endif
14835aefb655Srie }
14845aefb655Srie
14855aefb655Srie /*
14865aefb655Srie * If we are adding a output relocation against a section
14875aefb655Srie * symbol (non-RELATIVE) then mark that section. These sections
14885aefb655Srie * will be added to the .dynsym symbol table.
14895aefb655Srie */
14905aefb655Srie if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
14915aefb655Srie ((flags & FLG_REL_SCNNDX) ||
14925aefb655Srie (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
14935aefb655Srie
14945aefb655Srie /*
14955aefb655Srie * If this is a COMMON symbol - no output section
14965aefb655Srie * exists yet - (it's created as part of sym_validate()).
14975aefb655Srie * So - we mark here that when it's created it should
14985aefb655Srie * be tagged with the FLG_OS_OUTREL flag.
14995aefb655Srie */
15005aefb655Srie if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
15010bc07c75Srie (sdp->sd_sym->st_shndx == SHN_COMMON)) {
15025aefb655Srie if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
15035aefb655Srie ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
15045aefb655Srie else
15055aefb655Srie ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
15065aefb655Srie } else {
150708278a5eSRod Evans Os_desc *osp;
150808278a5eSRod Evans Is_desc *isp = sdp->sd_isc;
15095aefb655Srie
151008278a5eSRod Evans if (isp && ((osp = isp->is_osdesc) != NULL) &&
151108278a5eSRod Evans ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
15125aefb655Srie ofl->ofl_dynshdrcnt++;
15135aefb655Srie osp->os_flags |= FLG_OS_OUTREL;
15145aefb655Srie }
15155aefb655Srie }
15165aefb655Srie }
15175aefb655Srie
1518bf994817SAli Bahrami /* Enter it into the output relocation cache */
1519bf994817SAli Bahrami if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1520bf994817SAli Bahrami return (S_ERROR);
15215aefb655Srie
15225aefb655Srie if (flags & FLG_REL_GOT)
15235aefb655Srie ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
15245aefb655Srie else if (flags & FLG_REL_PLT)
15255aefb655Srie ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
15265aefb655Srie else if (flags & FLG_REL_BSS)
15275aefb655Srie ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
15285aefb655Srie else if (flags & FLG_REL_NOINFO)
15295aefb655Srie ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
15305aefb655Srie else
1531bf994817SAli Bahrami RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela);
15325aefb655Srie
15335aefb655Srie if (orsp->rel_rtype == M_R_RELATIVE)
15345aefb655Srie ofl->ofl_relocrelcnt++;
15355aefb655Srie
15365aefb655Srie #if defined(_ELF64)
15375aefb655Srie /*
15385aefb655Srie * When building a 64-bit object any R_SPARC_WDISP30 relocation is given
15395aefb655Srie * a plt padding entry, unless we're building a relocatable object
15405aefb655Srie * (ld -r) or -b is in effect.
15415aefb655Srie */
15425aefb655Srie if ((orsp->rel_rtype == R_SPARC_WDISP30) &&
15435aefb655Srie ((ofl->ofl_flags & (FLG_OF_BFLAG | FLG_OF_RELOBJ)) == 0) &&
15445aefb655Srie ((orsp->rel_sym->sd_flags & FLG_SY_PLTPAD) == 0)) {
15455aefb655Srie ofl->ofl_pltpad++;
15465aefb655Srie orsp->rel_sym->sd_flags |= FLG_SY_PLTPAD;
15475aefb655Srie }
15485aefb655Srie #endif
15495aefb655Srie /*
15505aefb655Srie * We don't perform sorting on PLT relocations because
15515aefb655Srie * they have already been assigned a PLT index and if we
15525aefb655Srie * were to sort them we would have to re-assign the plt indexes.
15535aefb655Srie */
15545aefb655Srie if (!(flags & FLG_REL_PLT))
15555aefb655Srie ofl->ofl_reloccnt++;
15565aefb655Srie
15575aefb655Srie /*
15585aefb655Srie * Insure a GLOBAL_OFFSET_TABLE is generated if required.
15595aefb655Srie */
15605aefb655Srie if (IS_GOT_REQUIRED(orsp->rel_rtype))
15615aefb655Srie ofl->ofl_flags |= FLG_OF_BLDGOT;
15625aefb655Srie
15635aefb655Srie /*
15645aefb655Srie * Identify and possibly warn of a displacement relocation.
15655aefb655Srie */
15665aefb655Srie if (orsp->rel_flags & FLG_REL_DISP) {
15675aefb655Srie ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
15685aefb655Srie
15695aefb655Srie if (ofl->ofl_flags & FLG_OF_VERBOSE)
15705aefb655Srie ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
15715aefb655Srie }
15725aefb655Srie DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
15735aefb655Srie M_MACH, orsp));
15745aefb655Srie return (1);
15755aefb655Srie }
15765aefb655Srie
15775aefb655Srie /*
15785aefb655Srie * Process relocation against a register symbol. Note, of -z muldefs is in
15795aefb655Srie * effect there may have been multiple register definitions, which would have
15805aefb655Srie * been processed as non-fatal, with the first definition winning. But, we
15815aefb655Srie * will also process multiple relocations for these multiple definitions. In
15825aefb655Srie * this case we must only preserve the relocation for the definition that was
15835aefb655Srie * kept. The sad part is that register relocations don't typically specify
15845aefb655Srie * the register symbol with which they are associated, so we might have to
15855aefb655Srie * search the input files global symbols to determine if this relocation is
15865aefb655Srie * appropriate.
15875aefb655Srie */
1588ba2be530Sab196087 static uintptr_t
ld_reloc_register(Rel_desc * rsp,Is_desc * isp,Ofl_desc * ofl)15895aefb655Srie ld_reloc_register(Rel_desc *rsp, Is_desc *isp, Ofl_desc *ofl)
15905aefb655Srie {
15915aefb655Srie if (ofl->ofl_flags & FLG_OF_MULDEFS) {
15925aefb655Srie Ifl_desc *ifl = isp->is_file;
15935aefb655Srie Sym_desc *sdp = rsp->rel_sym;
15945aefb655Srie
15955aefb655Srie if (sdp == 0) {
15965aefb655Srie Xword offset = rsp->rel_roffset;
15975aefb655Srie Word ndx;
15985aefb655Srie
15995aefb655Srie for (ndx = ifl->ifl_locscnt;
16005aefb655Srie ndx < ifl->ifl_symscnt; ndx++) {
16015aefb655Srie if (((sdp = ifl->ifl_oldndx[ndx]) != 0) &&
16025aefb655Srie (sdp->sd_flags & FLG_SY_REGSYM) &&
16035aefb655Srie (sdp->sd_sym->st_value == offset))
16045aefb655Srie break;
16055aefb655Srie }
16065aefb655Srie }
16075aefb655Srie if (sdp && (sdp->sd_file != ifl))
16085aefb655Srie return (1);
16095aefb655Srie }
16105aefb655Srie return (ld_add_outrel((rsp->rel_flags | FLG_REL_REG), rsp, ofl));
16115aefb655Srie }
16125aefb655Srie
16135aefb655Srie /*
16145aefb655Srie * process relocation for a LOCAL symbol
16155aefb655Srie */
1616ba2be530Sab196087 static uintptr_t
ld_reloc_local(Rel_desc * rsp,Ofl_desc * ofl)16175aefb655Srie ld_reloc_local(Rel_desc *rsp, Ofl_desc *ofl)
16185aefb655Srie {
16191d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags;
16205aefb655Srie Sym_desc *sdp = rsp->rel_sym;
16210bc07c75Srie Word shndx = sdp->sd_sym->st_shndx;
16225aefb655Srie
16235aefb655Srie /*
16245aefb655Srie * if ((shared object) and (not pc relative relocation) and
16255aefb655Srie * (not against ABS symbol))
16265aefb655Srie * then
16275aefb655Srie * if (rtype != R_SPARC_32)
16285aefb655Srie * then
16295aefb655Srie * build relocation against section
16305aefb655Srie * else
16315aefb655Srie * build R_SPARC_RELATIVE
16325aefb655Srie * fi
16335aefb655Srie * fi
16345aefb655Srie */
16355aefb655Srie if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
16362926dd2eSrie !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
16375aefb655Srie !(IS_GOT_BASED(rsp->rel_rtype)) &&
16385aefb655Srie !(rsp->rel_isdesc != NULL &&
16395aefb655Srie (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
16405aefb655Srie (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
16415aefb655Srie (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
16425aefb655Srie Word ortype = rsp->rel_rtype;
16435aefb655Srie
16445aefb655Srie if ((rsp->rel_rtype != R_SPARC_32) &&
16455aefb655Srie (rsp->rel_rtype != R_SPARC_PLT32) &&
16465aefb655Srie (rsp->rel_rtype != R_SPARC_64))
16475aefb655Srie return (ld_add_outrel((FLG_REL_SCNNDX | FLG_REL_ADVAL),
16485aefb655Srie rsp, ofl));
16495aefb655Srie
16505aefb655Srie rsp->rel_rtype = R_SPARC_RELATIVE;
16515aefb655Srie if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
16525aefb655Srie return (S_ERROR);
16535aefb655Srie rsp->rel_rtype = ortype;
16545aefb655Srie return (1);
16555aefb655Srie }
16565aefb655Srie
16575aefb655Srie /*
16585aefb655Srie * If the relocation is against a 'non-allocatable' section
16595aefb655Srie * and we can not resolve it now - then give a warning
16605aefb655Srie * message.
16615aefb655Srie *
16625aefb655Srie * We can not resolve the symbol if either:
16635aefb655Srie * a) it's undefined
16645aefb655Srie * b) it's defined in a shared library and a
16655aefb655Srie * COPY relocation hasn't moved it to the executable
16665aefb655Srie *
16675aefb655Srie * Note: because we process all of the relocations against the
16685aefb655Srie * text segment before any others - we know whether
16695aefb655Srie * or not a copy relocation will be generated before
16705aefb655Srie * we get here (see reloc_init()->reloc_segments()).
16715aefb655Srie */
16725aefb655Srie if (!(rsp->rel_flags & FLG_REL_LOAD) &&
16735aefb655Srie ((shndx == SHN_UNDEF) ||
16745aefb655Srie ((sdp->sd_ref == REF_DYN_NEED) &&
16755aefb655Srie ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1676de777a60Sab196087 Conv_inv_buf_t inv_buf;
1677bf994817SAli Bahrami Os_desc *osp = RELAUX_GET_OSDESC(rsp);
1678de777a60Sab196087
16795aefb655Srie /*
16805aefb655Srie * If the relocation is against a SHT_SUNW_ANNOTATE
16815aefb655Srie * section - then silently ignore that the relocation
16825aefb655Srie * can not be resolved.
16835aefb655Srie */
1684bf994817SAli Bahrami if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
16855aefb655Srie return (0);
16861007fd6fSAli Bahrami ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1687de777a60Sab196087 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
16885aefb655Srie rsp->rel_isdesc->is_file->ifl_name,
1689bf994817SAli Bahrami ld_reloc_sym_name(rsp), osp->os_name);
16905aefb655Srie return (1);
16915aefb655Srie }
16925aefb655Srie
16935aefb655Srie /*
16945aefb655Srie * Perform relocation.
16955aefb655Srie */
1696*fb8f92baSToomas Soome return (ld_add_actrel(0, rsp, ofl));
16975aefb655Srie }
16985aefb655Srie
1699051d39bbSrie /*
1700051d39bbSrie * Establish a relocation transition. Note, at this point of input relocation
1701051d39bbSrie * processing, we have no idea of the relocation value that will be used in
1702051d39bbSrie * the eventual relocation calculation. This value is only known after the
1703051d39bbSrie * initial image has been constructed. Therefore, there is a small chance
1704051d39bbSrie * that a value can exceed the capabilities of the transitioned relocation.
1705051d39bbSrie * One example might be the offset from the GOT to a symbol.
1706051d39bbSrie *
1707051d39bbSrie * The only instance of this failure discovered so far has been via the use of
1708051d39bbSrie * ABS symbols to represent an external memory location. This situation is
1709051d39bbSrie * rare, since ABS symbols aren't typically generated by the compilers.
1710051d39bbSrie * Therefore, our solution is to excluded ABS symbols from the transition
1711051d39bbSrie * relocation possibilities. As an additional safeguard, if an inappropriate
1712051d39bbSrie * value is passed to the final relocation engine, a verification ("V")
1713051d39bbSrie * relocation should trigger a fatal error condition.
1714051d39bbSrie */
1715ba2be530Sab196087 static uintptr_t
ld_reloc_GOTOP(Boolean local,Rel_desc * rsp,Ofl_desc * ofl)17165aefb655Srie ld_reloc_GOTOP(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
17175aefb655Srie {
17185aefb655Srie Word rtype = rsp->rel_rtype;
17195aefb655Srie
1720051d39bbSrie if (!local || (rsp->rel_sym->sd_sym->st_shndx == SHN_ABS)) {
17215aefb655Srie /*
17225aefb655Srie * When binding to a external symbol, no fixups are required
17235aefb655Srie * and the GOTDATA_OP relocation can be ignored.
17245aefb655Srie */
17255aefb655Srie if (rtype == R_SPARC_GOTDATA_OP)
17265aefb655Srie return (1);
17275aefb655Srie return (ld_reloc_GOT_relative(local, rsp, ofl));
17285aefb655Srie }
17295aefb655Srie
17305aefb655Srie /*
17315aefb655Srie * When binding to a local symbol the relocations can be transitioned:
17325aefb655Srie *
17335aefb655Srie * R_*_GOTDATA_OP_HIX22 -> R_*_GOTDATA_HIX22
17345aefb655Srie * R_*_GOTDATA_OP_LOX10 -> R_*_GOTDATA_LOX10
17355aefb655Srie * R_*_GOTDATA_OP -> instruction fixup
17365aefb655Srie */
17375aefb655Srie return (ld_add_actrel(FLG_REL_GOTFIX, rsp, ofl));
17385aefb655Srie }
17395aefb655Srie
1740ba2be530Sab196087 static uintptr_t
ld_reloc_TLS(Boolean local,Rel_desc * rsp,Ofl_desc * ofl)17415aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
17425aefb655Srie {
17435aefb655Srie Word rtype = rsp->rel_rtype;
17445aefb655Srie Sym_desc *sdp = rsp->rel_sym;
17451d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags;
17465aefb655Srie Gotndx *gnp;
17475aefb655Srie
17485aefb655Srie /*
1749d326b23bSrie * If we're building an executable - use either the IE or LE access
1750d326b23bSrie * model. If we're building a shared object process any IE model.
17515aefb655Srie */
1752d326b23bSrie if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
17535aefb655Srie /*
1754d326b23bSrie * Set the DF_STATIC_TLS flag.
17555aefb655Srie */
17565aefb655Srie ofl->ofl_dtflags |= DF_STATIC_TLS;
17575aefb655Srie
1758d326b23bSrie if (!local || ((flags & FLG_OF_EXEC) == 0)) {
17595aefb655Srie /*
1760d326b23bSrie * When processing static TLS - these relocations
17615aefb655Srie * can be ignored.
17625aefb655Srie */
17635aefb655Srie if ((rtype == R_SPARC_TLS_IE_LD) ||
17645aefb655Srie (rtype == R_SPARC_TLS_IE_LDX) ||
17655aefb655Srie (rtype == R_SPARC_TLS_IE_ADD))
17665aefb655Srie return (1);
17675aefb655Srie
17685aefb655Srie /*
1769d326b23bSrie * Assign a GOT entry for IE static TLS references.
17705aefb655Srie */
17715aefb655Srie if (((rtype == R_SPARC_TLS_GD_HI22) ||
17725aefb655Srie (rtype == R_SPARC_TLS_GD_LO10) ||
17735aefb655Srie (rtype == R_SPARC_TLS_IE_HI22) ||
17745aefb655Srie (rtype == R_SPARC_TLS_IE_LO10)) &&
177557ef7aa9SRod Evans ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
177657ef7aa9SRod Evans GOT_REF_TLSIE, ofl, rsp)) == NULL)) {
1777d326b23bSrie
1778d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1779d326b23bSrie gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1780*fb8f92baSToomas Soome rtype, M_R_TPOFF, 0) == S_ERROR)
17815aefb655Srie return (S_ERROR);
17825aefb655Srie }
17835aefb655Srie
1784d326b23bSrie /*
1785d326b23bSrie * IE access model.
1786d326b23bSrie */
17875aefb655Srie if (IS_TLS_IE(rtype))
17885aefb655Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
17895aefb655Srie
17905aefb655Srie /*
1791d326b23bSrie * Fixups are required for other executable models.
17925aefb655Srie */
17935aefb655Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
17945aefb655Srie rsp, ofl));
17955aefb655Srie }
1796d326b23bSrie
17975aefb655Srie /*
1798d326b23bSrie * LE access model.
17995aefb655Srie */
18005aefb655Srie if (IS_TLS_LE(rtype))
18015aefb655Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
18025aefb655Srie
18035aefb655Srie /*
1804d326b23bSrie * When processing static TLS - these relocations can be
1805d326b23bSrie * ignored.
18065aefb655Srie */
18075aefb655Srie if (rtype == R_SPARC_TLS_IE_ADD)
18085aefb655Srie return (1);
18095aefb655Srie
18105aefb655Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
18115aefb655Srie rsp, ofl));
18125aefb655Srie }
18135aefb655Srie
18145aefb655Srie /*
1815d326b23bSrie * Building a shared object.
1816d326b23bSrie *
1817d326b23bSrie * For dynamic TLS references, ADD relocations are ignored.
18185aefb655Srie */
18195aefb655Srie if ((rtype == R_SPARC_TLS_GD_ADD) || (rtype == R_SPARC_TLS_LDM_ADD) ||
18205aefb655Srie (rtype == R_SPARC_TLS_LDO_ADD))
18215aefb655Srie return (1);
18225aefb655Srie
18235aefb655Srie /*
18245aefb655Srie * Assign a GOT entry for a dynamic TLS reference.
18255aefb655Srie */
18265aefb655Srie if (((rtype == R_SPARC_TLS_LDM_HI22) ||
18275aefb655Srie (rtype == R_SPARC_TLS_LDM_LO10)) &&
182857ef7aa9SRod Evans ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSLD,
182957ef7aa9SRod Evans ofl, rsp)) == NULL)) {
18305aefb655Srie
1831d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1832d326b23bSrie FLG_REL_MTLS, rtype, M_R_DTPMOD, 0) == S_ERROR)
18335aefb655Srie return (S_ERROR);
18345aefb655Srie
1835d326b23bSrie } else if (((rtype == R_SPARC_TLS_GD_HI22) ||
1836d326b23bSrie (rtype == R_SPARC_TLS_GD_LO10)) &&
183757ef7aa9SRod Evans ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
183857ef7aa9SRod Evans ofl, rsp)) == NULL)) {
18395aefb655Srie
1840d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1841d326b23bSrie FLG_REL_DTLS, rtype, M_R_DTPMOD, M_R_DTPOFF) == S_ERROR)
18425aefb655Srie return (S_ERROR);
18435aefb655Srie }
1844d326b23bSrie
18455aefb655Srie /*
18465aefb655Srie * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1847d326b23bSrie * cause a call to __tls_get_addr(). Convert this relocation to that
1848d326b23bSrie * symbol now, and prepare for the PLT magic.
18495aefb655Srie */
18505aefb655Srie if ((rtype == R_SPARC_TLS_GD_CALL) || (rtype == R_SPARC_TLS_LDM_CALL)) {
18515aefb655Srie Sym_desc *tlsgetsym;
18525aefb655Srie
18535aefb655Srie if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_U),
1854f5a18a30Srie ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
18555aefb655Srie return (S_ERROR);
1856d326b23bSrie
18575aefb655Srie rsp->rel_sym = tlsgetsym;
18585aefb655Srie rsp->rel_rtype = R_SPARC_WPLT30;
1859d326b23bSrie
18605aefb655Srie if (ld_reloc_plt(rsp, ofl) == S_ERROR)
18615aefb655Srie return (S_ERROR);
1862d326b23bSrie
18635aefb655Srie rsp->rel_sym = sdp;
18645aefb655Srie rsp->rel_rtype = rtype;
18655aefb655Srie return (1);
18665aefb655Srie }
18675aefb655Srie
18685aefb655Srie if (IS_TLS_LD(rtype))
18695aefb655Srie return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
18705aefb655Srie
18715aefb655Srie return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
18725aefb655Srie }
18735aefb655Srie
18745aefb655Srie /*
18755aefb655Srie * ld_allocate_got: if a GOT is to be made, after the section is built this
18765aefb655Srie * function is called to allocate all the GOT slots. The allocation is
18775aefb655Srie * deferred until after all GOTs have been counted and sorted according
18785aefb655Srie * to their size, for only then will we know how to allocate them on
18795aefb655Srie * a processor like SPARC which has different models for addressing the
18805aefb655Srie * GOT. SPARC has two: small and large, small uses a signed 13-bit offset
18815aefb655Srie * into the GOT, whereas large uses an unsigned 32-bit offset.
18825aefb655Srie */
18835aefb655Srie static Sword small_index; /* starting index for small GOT entries */
18846a074c93Sab196087 static Sword mixed_index; /* starting index for mixed GOT entries */
18855aefb655Srie static Sword large_index; /* starting index for large GOT entries */
18865aefb655Srie
1887ba2be530Sab196087 static uintptr_t
ld_assign_got(Ofl_desc * ofl,Sym_desc * sdp)18885aefb655Srie ld_assign_got(Ofl_desc *ofl, Sym_desc *sdp)
18895aefb655Srie {
189057ef7aa9SRod Evans Aliste idx;
18915aefb655Srie Gotndx *gnp;
18925aefb655Srie
189357ef7aa9SRod Evans for (ALIST_TRAVERSE(sdp->sd_GOTndxs, idx, gnp)) {
18945aefb655Srie uint_t gotents;
189557ef7aa9SRod Evans Gotref gref = gnp->gn_gotref;
189657ef7aa9SRod Evans
18975aefb655Srie if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
18985aefb655Srie gotents = 2;
18995aefb655Srie else
19005aefb655Srie gotents = 1;
19015aefb655Srie
19025aefb655Srie switch (gnp->gn_gotndx) {
19035aefb655Srie case M_GOT_SMALL:
19045aefb655Srie gnp->gn_gotndx = small_index;
19055aefb655Srie small_index += gotents;
19065aefb655Srie if (small_index == 0)
19075aefb655Srie small_index = M_GOT_XNumber;
19085aefb655Srie break;
19096a074c93Sab196087 case M_GOT_MIXED:
19106a074c93Sab196087 gnp->gn_gotndx = mixed_index;
19116a074c93Sab196087 mixed_index += gotents;
19126a074c93Sab196087 break;
19135aefb655Srie case M_GOT_LARGE:
19145aefb655Srie gnp->gn_gotndx = large_index;
19155aefb655Srie large_index += gotents;
19165aefb655Srie break;
19175aefb655Srie default:
19181007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_ASSIGNGOT),
19195aefb655Srie EC_XWORD(gnp->gn_gotndx), demangle(sdp->sd_name));
19205aefb655Srie return (S_ERROR);
19215aefb655Srie }
19225aefb655Srie }
19235aefb655Srie return (1);
19245aefb655Srie }
19255aefb655Srie
1926ba2be530Sab196087 static uintptr_t
ld_assign_got_ndx(Alist ** alpp,Gotndx * pgnp,Gotref gref,Ofl_desc * ofl,Rel_desc * rsp,Sym_desc * sdp)192757ef7aa9SRod Evans ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
19285aefb655Srie Rel_desc *rsp, Sym_desc *sdp)
19295aefb655Srie {
19305aefb655Srie Xword raddend;
193157ef7aa9SRod Evans Gotndx gn, *gnp;
193257ef7aa9SRod Evans Aliste idx;
19335aefb655Srie uint_t gotents;
19345aefb655Srie
19356a074c93Sab196087 /* Some TLS requires two relocations with two GOT entries */
19365aefb655Srie if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
19375aefb655Srie gotents = 2;
19385aefb655Srie else
19395aefb655Srie gotents = 1;
19405aefb655Srie
19416a074c93Sab196087 raddend = rsp->rel_raddend;
19426a074c93Sab196087 if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) {
19436a074c93Sab196087
19446a074c93Sab196087 /*
19456a074c93Sab196087 * If an entry for this addend already exists, determine if it
19466a074c93Sab196087 * has mixed mode GOT access (both PIC and pic).
19476a074c93Sab196087 *
19486a074c93Sab196087 * In order to be accessible by both large and small pic,
19496a074c93Sab196087 * a mixed mode GOT must be located in the positive index
19506a074c93Sab196087 * range above _GLOBAL_OFFSET_TABLE_, and in the range
19516a074c93Sab196087 * reachable small pic. This is necessary because the large
19526a074c93Sab196087 * PIC mode cannot use a negative offset. This implies that
19536a074c93Sab196087 * there can be no more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
19546a074c93Sab196087 * such entries.
19556a074c93Sab196087 */
19566a074c93Sab196087 switch (pgnp->gn_gotndx) {
19576a074c93Sab196087 case M_GOT_SMALL:
19586a074c93Sab196087 /*
19596a074c93Sab196087 * This one was previously identified as a small
19606a074c93Sab196087 * GOT. If this access is large, then convert
19616a074c93Sab196087 * it to mixed.
19626a074c93Sab196087 */
19636a074c93Sab196087 if (rsp->rel_rtype != R_SPARC_GOT13) {
19646a074c93Sab196087 pgnp->gn_gotndx = M_GOT_MIXED;
19656a074c93Sab196087 mixgotcnt += gotents;
19666a074c93Sab196087 }
19676a074c93Sab196087 break;
19686a074c93Sab196087
19696a074c93Sab196087 case M_GOT_LARGE:
19706a074c93Sab196087 /*
19716a074c93Sab196087 * This one was previously identified as a large
19726a074c93Sab196087 * GOT. If this access is small, convert it to mixed.
19736a074c93Sab196087 */
19746a074c93Sab196087 if (rsp->rel_rtype == R_SPARC_GOT13) {
19756a074c93Sab196087 smlgotcnt += gotents;
19766a074c93Sab196087 mixgotcnt += gotents;
19776a074c93Sab196087 pgnp->gn_gotndx = M_GOT_MIXED;
19786a074c93Sab196087 sdp->sd_flags |= FLG_SY_SMGOT;
19796a074c93Sab196087 }
19806a074c93Sab196087 break;
19816a074c93Sab196087 }
19826a074c93Sab196087 return (1);
19836a074c93Sab196087 }
19846a074c93Sab196087
198557ef7aa9SRod Evans gn.gn_addend = raddend;
198657ef7aa9SRod Evans gn.gn_gotref = gref;
19875aefb655Srie
19885aefb655Srie if (rsp->rel_rtype == R_SPARC_GOT13) {
198957ef7aa9SRod Evans gn.gn_gotndx = M_GOT_SMALL;
19906a074c93Sab196087 smlgotcnt += gotents;
19915aefb655Srie sdp->sd_flags |= FLG_SY_SMGOT;
199257ef7aa9SRod Evans } else
199357ef7aa9SRod Evans gn.gn_gotndx = M_GOT_LARGE;
199457ef7aa9SRod Evans
199557ef7aa9SRod Evans ofl->ofl_gotcnt += gotents;
19965aefb655Srie
19975aefb655Srie if (gref == GOT_REF_TLSLD) {
199857ef7aa9SRod Evans if (ofl->ofl_tlsldgotndx == NULL) {
199957ef7aa9SRod Evans if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
200057ef7aa9SRod Evans return (S_ERROR);
200157ef7aa9SRod Evans (void) memcpy(gnp, &gn, sizeof (Gotndx));
20025aefb655Srie ofl->ofl_tlsldgotndx = gnp;
200357ef7aa9SRod Evans }
20045aefb655Srie return (1);
20055aefb655Srie }
20065aefb655Srie
200757ef7aa9SRod Evans idx = 0;
200857ef7aa9SRod Evans for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
200957ef7aa9SRod Evans if (gnp->gn_addend > raddend)
201057ef7aa9SRod Evans break;
20115aefb655Srie }
201257ef7aa9SRod Evans
201357ef7aa9SRod Evans /*
201457ef7aa9SRod Evans * GOT indexes are maintained on an Alist, where there is typically
201557ef7aa9SRod Evans * only one index. The usage of this list is to scan the list to find
201657ef7aa9SRod Evans * an index, and then apply that index immediately to a relocation.
201757ef7aa9SRod Evans * Thus there are no external references to these GOT index structures
201857ef7aa9SRod Evans * that can be compromised by the Alist being reallocated.
201957ef7aa9SRod Evans */
202057ef7aa9SRod Evans if (alist_insert(alpp, &gn, sizeof (Gotndx),
202157ef7aa9SRod Evans AL_CNT_SDP_GOT, idx) == NULL)
202257ef7aa9SRod Evans return (S_ERROR);
202357ef7aa9SRod Evans
20245aefb655Srie return (1);
20255aefb655Srie }
20265aefb655Srie
2027ba2be530Sab196087 static void
ld_assign_plt_ndx(Sym_desc * sdp,Ofl_desc * ofl)20285aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
20295aefb655Srie {
20305aefb655Srie sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
20315aefb655Srie }
20325aefb655Srie
20335aefb655Srie
2034ba2be530Sab196087 static uintptr_t
ld_allocate_got(Ofl_desc * ofl)20355aefb655Srie ld_allocate_got(Ofl_desc * ofl)
20365aefb655Srie {
20376a074c93Sab196087 const Sword first_large_ndx = M_GOT_MAXSMALL / 2;
20385aefb655Srie Sym_desc *sdp;
20395aefb655Srie Addr addr;
20405aefb655Srie
20415aefb655Srie /*
20426a074c93Sab196087 * Sanity check -- is this going to fit at all? There are two
20436a074c93Sab196087 * limits to be concerned about:
20446a074c93Sab196087 * 1) There is a limit on the number of small pic GOT indices,
20456a074c93Sab196087 * given by M_GOT_MAXSMALL.
20466a074c93Sab196087 * 2) If there are more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
20476a074c93Sab196087 * small GOT indices, there will be items at negative
20486a074c93Sab196087 * offsets from _GLOBAL_OFFSET_TABLE_. Items that are
20496a074c93Sab196087 * accessed via large (PIC) code cannot reach these
20506a074c93Sab196087 * negative slots, so mixed mode items must be in the
20516a074c93Sab196087 * non-negative range. This implies a limit of
20526a074c93Sab196087 * (M_GOT_MAXSMALL/2 - M_GOT_XNumber) mixed mode indices.
20535aefb655Srie */
20546a074c93Sab196087 if (smlgotcnt > M_GOT_MAXSMALL) {
20551007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SMALLGOT),
20565aefb655Srie EC_WORD(smlgotcnt), M_GOT_MAXSMALL);
20575aefb655Srie return (S_ERROR);
20585aefb655Srie }
20596a074c93Sab196087 if (mixgotcnt > (first_large_ndx - M_GOT_XNumber)) {
20601007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_MIXEDGOT),
20616a074c93Sab196087 EC_WORD(mixgotcnt), first_large_ndx - M_GOT_XNumber);
20626a074c93Sab196087 return (S_ERROR);
20636a074c93Sab196087 }
20645aefb655Srie
20655aefb655Srie /*
20665aefb655Srie * Set starting offset to be either 0, or a negative index into
20675aefb655Srie * the GOT based on the number of small symbols we've got.
20685aefb655Srie */
20696a074c93Sab196087 neggotoffset = ((smlgotcnt >= first_large_ndx) ?
20706a074c93Sab196087 (first_large_ndx - smlgotcnt) : 0);
20715aefb655Srie
20725aefb655Srie /*
20736a074c93Sab196087 * Initialize the got offsets used by assign_got() to
20746a074c93Sab196087 * locate GOT items:
20756a074c93Sab196087 * small - Starting index of items referenced only
20766a074c93Sab196087 * by small offsets (-Kpic).
20776a074c93Sab196087 * mixed - Starting index of items referenced
20786a074c93Sab196087 * by both large (-KPIC) and small (-Kpic).
20796a074c93Sab196087 * large - Indexes referenced only by large (-KPIC)
20806a074c93Sab196087 *
20816a074c93Sab196087 * Small items can have negative indexes (i.e. lie below
20826a074c93Sab196087 * _GLOBAL_OFFSET_TABLE_). Mixed and large items must have
20836a074c93Sab196087 * non-negative offsets.
20845aefb655Srie */
20856a074c93Sab196087 small_index = (neggotoffset == 0) ? M_GOT_XNumber : neggotoffset;
20865aefb655Srie large_index = neggotoffset + smlgotcnt;
20876a074c93Sab196087 mixed_index = large_index - mixgotcnt;
20885aefb655Srie
20895aefb655Srie /*
20905aefb655Srie * Assign bias to GOT symbols.
20915aefb655Srie */
20925aefb655Srie addr = -neggotoffset * M_GOT_ENTSIZE;
2093635216b6SRod Evans if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), SYM_NOHASH,
2094635216b6SRod Evans NULL, ofl)) != NULL)
20955aefb655Srie sdp->sd_sym->st_value = addr;
2096635216b6SRod Evans if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH,
2097635216b6SRod Evans NULL, ofl)) != NULL)
20985aefb655Srie sdp->sd_sym->st_value = addr;
20995aefb655Srie
21005aefb655Srie if (ofl->ofl_tlsldgotndx) {
21015aefb655Srie ofl->ofl_tlsldgotndx->gn_gotndx = large_index;
21025aefb655Srie large_index += 2;
21035aefb655Srie }
21045aefb655Srie return (1);
21055aefb655Srie }
21065aefb655Srie
21075aefb655Srie /*
21085aefb655Srie * Initializes .got[0] with the _DYNAMIC symbol value.
21095aefb655Srie */
2110ba2be530Sab196087 static uintptr_t
ld_fillin_gotplt(Ofl_desc * ofl)21115aefb655Srie ld_fillin_gotplt(Ofl_desc *ofl)
21125aefb655Srie {
21135aefb655Srie if (ofl->ofl_osgot) {
21145aefb655Srie Sym_desc *sdp;
21155aefb655Srie
21165aefb655Srie if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
2117635216b6SRod Evans SYM_NOHASH, NULL, ofl)) != NULL) {
2118d326b23bSrie uchar_t *genptr;
2119d326b23bSrie
2120d326b23bSrie genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
21215aefb655Srie (-neggotoffset * M_GOT_ENTSIZE) +
21225aefb655Srie (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
21235aefb655Srie /* LINTED */
21245aefb655Srie *((Xword *)genptr) = sdp->sd_sym->st_value;
2125ba2be530Sab196087 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
2126ba2be530Sab196087 /* LINTED */
2127ba2be530Sab196087 *((Xword *)genptr) =
2128ba2be530Sab196087 /* LINTED */
2129ba2be530Sab196087 ld_bswap_Xword(*((Xword *)genptr));
21305aefb655Srie }
21315aefb655Srie }
21325aefb655Srie return (1);
21335aefb655Srie }
2134ba2be530Sab196087
2135ba2be530Sab196087
2136ba2be530Sab196087
2137ba2be530Sab196087 /*
2138ba2be530Sab196087 * Template for generating "void (*)(void)" function
2139ba2be530Sab196087 */
2140ba2be530Sab196087 static const uchar_t nullfunc_tmpl[] = {
2141ba2be530Sab196087 /* 0x00 */ 0x81, 0xc3, 0xe0, 0x08, /* retl */
2142ba2be530Sab196087 /* 0x04 */ 0x01, 0x00, 0x00, 0x00 /* nop */
2143ba2be530Sab196087 };
2144ba2be530Sab196087
2145ba2be530Sab196087
2146ba2be530Sab196087
2147ba2be530Sab196087 /*
2148ba2be530Sab196087 * Return the ld_targ definition for this target.
2149ba2be530Sab196087 */
2150ba2be530Sab196087 const Target *
ld_targ_init_sparc(void)2151ba2be530Sab196087 ld_targ_init_sparc(void)
2152ba2be530Sab196087 {
2153ba2be530Sab196087 static const Target _ld_targ = {
2154ba2be530Sab196087 { /* Target_mach */
2155ba2be530Sab196087 M_MACH, /* m_mach */
2156ba2be530Sab196087 M_MACHPLUS, /* m_machplus */
2157ba2be530Sab196087 M_FLAGSPLUS, /* m_flagsplus */
2158ba2be530Sab196087 M_CLASS, /* m_class */
2159ba2be530Sab196087 M_DATA, /* m_data */
2160ba2be530Sab196087
2161ba2be530Sab196087 M_SEGM_ALIGN, /* m_segm_align */
2162ba2be530Sab196087 M_SEGM_ORIGIN, /* m_segm_origin */
2163bb3b4f6cSRod Evans M_SEGM_AORIGIN, /* m_segm_aorigin */
2164ba2be530Sab196087 M_DATASEG_PERM, /* m_dataseg_perm */
216569112eddSAli Bahrami M_STACK_PERM, /* m_stack_perm */
2166ba2be530Sab196087 M_WORD_ALIGN, /* m_word_align */
2167ba2be530Sab196087 /* m_def_interp */
2168ba2be530Sab196087 #if defined(_ELF64)
2169ba2be530Sab196087 MSG_ORIG(MSG_PTH_RTLD_SPARCV9),
2170ba2be530Sab196087 #else
2171ba2be530Sab196087 MSG_ORIG(MSG_PTH_RTLD),
2172ba2be530Sab196087 #endif
2173ba2be530Sab196087
2174ba2be530Sab196087 /* Relocation type codes */
2175ba2be530Sab196087 M_R_ARRAYADDR, /* m_r_arrayaddr */
2176ba2be530Sab196087 M_R_COPY, /* m_r_copy */
2177ba2be530Sab196087 M_R_GLOB_DAT, /* m_r_glob_dat */
2178ba2be530Sab196087 M_R_JMP_SLOT, /* m_r_jmp_slot */
2179ba2be530Sab196087 M_R_NUM, /* m_r_num */
2180ba2be530Sab196087 M_R_NONE, /* m_r_none */
2181ba2be530Sab196087 M_R_RELATIVE, /* m_r_relative */
2182ba2be530Sab196087 M_R_REGISTER, /* m_r_register */
2183ba2be530Sab196087
2184ba2be530Sab196087 /* Relocation related constants */
2185ba2be530Sab196087 M_REL_DT_COUNT, /* m_rel_dt_count */
2186ba2be530Sab196087 M_REL_DT_ENT, /* m_rel_dt_ent */
2187ba2be530Sab196087 M_REL_DT_SIZE, /* m_rel_dt_size */
2188ba2be530Sab196087 M_REL_DT_TYPE, /* m_rel_dt_type */
2189ba2be530Sab196087 M_REL_SHT_TYPE, /* m_rel_sht_type */
2190ba2be530Sab196087
2191ba2be530Sab196087 /* GOT related constants */
2192ba2be530Sab196087 M_GOT_ENTSIZE, /* m_got_entsize */
2193ba2be530Sab196087 M_GOT_XNumber, /* m_got_xnumber */
2194ba2be530Sab196087
2195ba2be530Sab196087 /* PLT related constants */
2196ba2be530Sab196087 M_PLT_ALIGN, /* m_plt_align */
2197ba2be530Sab196087 M_PLT_ENTSIZE, /* m_plt_entsize */
2198ba2be530Sab196087 M_PLT_RESERVSZ, /* m_plt_reservsz */
2199ba2be530Sab196087 M_PLT_SHF_FLAGS, /* m_plt_shf_flags */
2200ba2be530Sab196087
22017e16fca0SAli Bahrami /* Section type of .eh_frame/.eh_frame_hdr sections */
22027e16fca0SAli Bahrami SHT_PROGBITS, /* m_sht_unwind */
22037e16fca0SAli Bahrami
2204ba2be530Sab196087 M_DT_REGISTER, /* m_dt_register */
2205ba2be530Sab196087 },
2206ba2be530Sab196087 { /* Target_machid */
2207ba2be530Sab196087 M_ID_ARRAY, /* id_array */
2208ba2be530Sab196087 M_ID_BSS, /* id_bss */
2209ba2be530Sab196087 M_ID_CAP, /* id_cap */
221008278a5eSRod Evans M_ID_CAPINFO, /* id_capinfo */
221108278a5eSRod Evans M_ID_CAPCHAIN, /* id_capchain */
2212ba2be530Sab196087 M_ID_DATA, /* id_data */
2213ba2be530Sab196087 M_ID_DYNAMIC, /* id_dynamic */
2214ba2be530Sab196087 M_ID_DYNSORT, /* id_dynsort */
2215ba2be530Sab196087 M_ID_DYNSTR, /* id_dynstr */
2216ba2be530Sab196087 M_ID_DYNSYM, /* id_dynsym */
2217ba2be530Sab196087 M_ID_DYNSYM_NDX, /* id_dynsym_ndx */
2218ba2be530Sab196087 M_ID_GOT, /* id_got */
2219ba2be530Sab196087 M_ID_GOTDATA, /* id_gotdata */
2220ba2be530Sab196087 M_ID_HASH, /* id_hash */
2221ba2be530Sab196087 M_ID_INTERP, /* id_interp */
2222ba2be530Sab196087 M_ID_UNKNOWN, /* id_lbss (unused) */
2223ba2be530Sab196087 M_ID_LDYNSYM, /* id_ldynsym */
2224ba2be530Sab196087 M_ID_NOTE, /* id_note */
2225ba2be530Sab196087 M_ID_NULL, /* id_null */
2226ba2be530Sab196087 M_ID_PLT, /* id_plt */
2227ba2be530Sab196087 M_ID_REL, /* id_rel */
2228ba2be530Sab196087 M_ID_STRTAB, /* id_strtab */
2229ba2be530Sab196087 M_ID_SYMINFO, /* id_syminfo */
2230ba2be530Sab196087 M_ID_SYMTAB, /* id_symtab */
2231ba2be530Sab196087 M_ID_SYMTAB_NDX, /* id_symtab_ndx */
2232ba2be530Sab196087 M_ID_TEXT, /* id_text */
2233ba2be530Sab196087 M_ID_TLS, /* id_tls */
2234ba2be530Sab196087 M_ID_TLSBSS, /* id_tlsbss */
2235ba2be530Sab196087 M_ID_UNKNOWN, /* id_unknown */
22367e16fca0SAli Bahrami M_ID_UNWIND, /* id_unwind */
22377e16fca0SAli Bahrami M_ID_UNWINDHDR, /* id_unwindhdr */
2238ba2be530Sab196087 M_ID_USER, /* id_user */
2239ba2be530Sab196087 M_ID_VERSION, /* id_version */
2240ba2be530Sab196087 },
2241ba2be530Sab196087 { /* Target_nullfunc */
2242ba2be530Sab196087 nullfunc_tmpl, /* nf_template */
2243ba2be530Sab196087 sizeof (nullfunc_tmpl), /* nf_size */
2244ba2be530Sab196087 },
22453c573fccSAli Bahrami { /* Target_fillfunc */
22463c573fccSAli Bahrami /*
22473c573fccSAli Bahrami * On sparc, special filling of executable sections
22483c573fccSAli Bahrami * is undesirable, and the default 0 fill supplied
22493c573fccSAli Bahrami * by libelf is preferred:
22503c573fccSAli Bahrami *
22513c573fccSAli Bahrami * - 0 fill is interpreted as UNIMP instructions,
22523c573fccSAli Bahrami * which cause an illegal_instruction_trap. These
22533c573fccSAli Bahrami * serve as a sentinel against poorly written
22543c573fccSAli Bahrami * code. The sparc architecture manual discusses
22553c573fccSAli Bahrami * this as providing a measure of runtime safety.
22563c573fccSAli Bahrami *
22573c573fccSAli Bahrami * - The one place where a hole should conceivably
22583c573fccSAli Bahrami * be filled with NOP instructions is in the
22593c573fccSAli Bahrami * .init/.fini sections. However, the sparc
22603c573fccSAli Bahrami * assembler sizes the sections it generates
22613c573fccSAli Bahrami * to a multiple of the section alignment, and as
22623c573fccSAli Bahrami * such, takes the filling task out of our hands.
22633c573fccSAli Bahrami * Furthermore, the sparc assembler uses 0-fill
22643c573fccSAli Bahrami * for this, forcing the authors of sparc
22653c573fccSAli Bahrami * assembler for .init/.fini sections to be aware
22663c573fccSAli Bahrami * of this case and explicitly supply NOP fill.
22673c573fccSAli Bahrami * Hence, there is no role for the link-editor.
22683c573fccSAli Bahrami */
22693c573fccSAli Bahrami NULL /* ff_execfill */
22703c573fccSAli Bahrami },
2271ba2be530Sab196087 { /* Target_machrel */
2272ba2be530Sab196087 reloc_table,
2273ba2be530Sab196087
2274ba2be530Sab196087 ld_init_rel, /* mr_init_rel */
2275ba2be530Sab196087 ld_mach_eflags, /* mr_mach_eflags */
2276ba2be530Sab196087 ld_mach_make_dynamic, /* mr_mach_make_dynamic */
2277ba2be530Sab196087 ld_mach_update_odynamic, /* mr_mach_update_odynamic */
2278ba2be530Sab196087 ld_calc_plt_addr, /* mr_calc_plt_addr */
2279ba2be530Sab196087 ld_perform_outreloc, /* mr_perform_outreloc */
2280ba2be530Sab196087 ld_do_activerelocs, /* mr_do_activerelocs */
2281ba2be530Sab196087 ld_add_outrel, /* mr_add_outrel */
2282ba2be530Sab196087 ld_reloc_register, /* mr_reloc_register */
2283ba2be530Sab196087 ld_reloc_local, /* mr_reloc_local */
2284ba2be530Sab196087 ld_reloc_GOTOP, /* mr_reloc_GOTOP */
2285ba2be530Sab196087 ld_reloc_TLS, /* mr_reloc_TLS */
2286ba2be530Sab196087 ld_assign_got, /* mr_assign_got */
228757ef7aa9SRod Evans ld_find_got_ndx, /* mr_find_got_ndx */
2288ba2be530Sab196087 ld_calc_got_offset, /* mr_calc_got_offset */
2289ba2be530Sab196087 ld_assign_got_ndx, /* mr_assign_got_ndx */
2290ba2be530Sab196087 ld_assign_plt_ndx, /* mr_assign_plt_ndx */
2291ba2be530Sab196087 ld_allocate_got, /* mr_allocate_got */
2292ba2be530Sab196087 ld_fillin_gotplt, /* mr_fillin_gotplt */
2293ba2be530Sab196087 },
2294ba2be530Sab196087 { /* Target_machsym */
2295ba2be530Sab196087 ld_reg_check_sparc, /* ms_reg_check */
2296ba2be530Sab196087 ld_mach_sym_typecheck_sparc, /* ms_mach_sym_typecheck */
2297ba2be530Sab196087 ld_is_regsym_sparc, /* ms_is_regsym */
2298ba2be530Sab196087 ld_reg_find_sparc, /* ms_reg_find */
2299ba2be530Sab196087 ld_reg_enter_sparc /* ms_reg_enter */
2300ba2be530Sab196087 }
2301ba2be530Sab196087 };
2302ba2be530Sab196087
2303ba2be530Sab196087 return (&_ld_targ);
2304ba2be530Sab196087 }
2305