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)584*1007fd6fSAli 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
735*1007fd6fSAli 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
7825aefb655Srie relbits = (char *)relosp->os_outdata->d_buf;
7835aefb655Srie
784bf994817SAli Bahrami rea.r_info = ELF_R_INFO(ndx,
785bf994817SAli Bahrami ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp), orsp->rel_rtype));
7865aefb655Srie rea.r_offset = roffset;
7875aefb655Srie rea.r_addend = raddend;
7885aefb655Srie DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
789bf994817SAli Bahrami ld_reloc_sym_name(orsp)));
7905aefb655Srie
7915aefb655Srie /*
7925aefb655Srie * Assert we haven't walked off the end of our relocation table.
7935aefb655Srie */
7945aefb655Srie assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
7955aefb655Srie
7965aefb655Srie (void) memcpy((relbits + relosp->os_szoutrels),
7975aefb655Srie (char *)&rea, sizeof (Rela));
7985aefb655Srie relosp->os_szoutrels += (Xword)sizeof (Rela);
7995aefb655Srie
8005aefb655Srie /*
8015aefb655Srie * Determine if this relocation is against a non-writable, allocatable
8025aefb655Srie * section. If so we may need to provide a text relocation diagnostic.
8035aefb655Srie */
804*1007fd6fSAli Bahrami ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
8055aefb655Srie return (1);
8065aefb655Srie }
8075aefb655Srie
8085aefb655Srie
8095aefb655Srie /*
8105aefb655Srie * Sparc Instructions for TLS processing
8115aefb655Srie */
8125aefb655Srie #if defined(_ELF64)
8135aefb655Srie #define TLS_GD_IE_LD 0xd0580000 /* ldx [%g0 + %g0], %o0 */
8145aefb655Srie #else
8155aefb655Srie #define TLS_GD_IE_LD 0xd0000000 /* ld [%g0 + %g0], %o0 */
8165aefb655Srie #endif
8175aefb655Srie #define TLS_GD_IE_ADD 0x9001c008 /* add %g7, %o0, %o0 */
8185aefb655Srie
8195aefb655Srie #define TLS_GD_LE_XOR 0x80182000 /* xor %g0, 0, %g0 */
8205aefb655Srie #define TLS_IE_LE_OR 0x80100000 /* or %g0, %o0, %o1 */
8215aefb655Srie /* synthetic: mov %g0, %g0 */
8225aefb655Srie
8235aefb655Srie #define TLS_LD_LE_CLRO0 0x90100000 /* clr %o0 */
8245aefb655Srie
8255aefb655Srie #define FM3_REG_MSK_RD (0x1f << 25) /* Formate (3) rd register mask */
8265aefb655Srie /* bits 25->29 */
8275aefb655Srie #define FM3_REG_MSK_RS1 (0x1f << 14) /* Formate (3) rs1 register mask */
8285aefb655Srie /* bits 14->18 */
8295aefb655Srie #define FM3_REG_MSK_RS2 0x1f /* Formate (3) rs2 register mask */
8305aefb655Srie /* bits 0->4 */
8315aefb655Srie
8325aefb655Srie #define REG_G7 7 /* %g7 register */
8335aefb655Srie
8345aefb655Srie static Fixupret
tls_fixups(Ofl_desc * ofl,Rel_desc * arsp)8355aefb655Srie tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
8365aefb655Srie {
8375aefb655Srie Sym_desc *sdp = arsp->rel_sym;
8385aefb655Srie Word rtype = arsp->rel_rtype;
839ba2be530Sab196087 Word *offset, w;
840ba2be530Sab196087 int bswap = OFL_SWAP_RELOC_DATA(ofl, arsp);
8415aefb655Srie
842ba2be530Sab196087
843ba2be530Sab196087 offset = (Word *)((uintptr_t)arsp->rel_roffset +
8445aefb655Srie (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
845bf994817SAli Bahrami (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
8465aefb655Srie
8475aefb655Srie if (sdp->sd_ref == REF_DYN_NEED) {
8485aefb655Srie /*
8495aefb655Srie * IE reference model
8505aefb655Srie */
8515aefb655Srie switch (rtype) {
8525aefb655Srie case R_SPARC_TLS_GD_HI22:
8535aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
854bf994817SAli Bahrami R_SPARC_TLS_IE_HI22, arsp,
855bf994817SAli Bahrami ld_reloc_sym_name));
8565aefb655Srie arsp->rel_rtype = R_SPARC_TLS_IE_HI22;
8575aefb655Srie return (FIX_RELOC);
8585aefb655Srie
8595aefb655Srie case R_SPARC_TLS_GD_LO10:
8605aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
861bf994817SAli Bahrami R_SPARC_TLS_IE_LO10, arsp,
862bf994817SAli Bahrami ld_reloc_sym_name));
8635aefb655Srie arsp->rel_rtype = R_SPARC_TLS_IE_LO10;
8645aefb655Srie return (FIX_RELOC);
8655aefb655Srie
8665aefb655Srie case R_SPARC_TLS_GD_ADD:
8675aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
868bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
869ba2be530Sab196087 w = bswap ? ld_bswap_Word(*offset) : *offset;
870ba2be530Sab196087 w = (TLS_GD_IE_LD |
871ba2be530Sab196087 (w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RS2)));
872ba2be530Sab196087 *offset = bswap ? ld_bswap_Word(w) : w;
8735aefb655Srie return (FIX_DONE);
8745aefb655Srie
8755aefb655Srie case R_SPARC_TLS_GD_CALL:
8765aefb655Srie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
877bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
8785aefb655Srie *offset = TLS_GD_IE_ADD;
879ba2be530Sab196087 if (bswap)
880ba2be530Sab196087 *offset = ld_bswap_Word(*offset);
8815aefb655Srie return (FIX_DONE);
8825aefb655Srie }
8835aefb655Srie return (FIX_RELOC);
8845aefb655Srie }
8855aefb655Srie
8865aefb655Srie /*
8875aefb655Srie * LE reference model
8885aefb655Srie */
8895aefb655Srie switch (rtype) {
8905aefb655Srie case R_SPARC_TLS_IE_HI22:
8915aefb655Srie case R_SPARC_TLS_GD_HI22:
8925aefb655Srie case R_SPARC_TLS_LDO_HIX22:
893051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
894bf994817SAli Bahrami R_SPARC_TLS_LE_HIX22, arsp, ld_reloc_sym_name));
8955aefb655Srie arsp->rel_rtype = R_SPARC_TLS_LE_HIX22;
8965aefb655Srie return (FIX_RELOC);
8975aefb655Srie
8985aefb655Srie case R_SPARC_TLS_LDO_LOX10:
899051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
900bf994817SAli Bahrami R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name));
9015aefb655Srie arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
9025aefb655Srie return (FIX_RELOC);
9035aefb655Srie
9045aefb655Srie case R_SPARC_TLS_IE_LO10:
9055aefb655Srie case R_SPARC_TLS_GD_LO10:
9065aefb655Srie /*
9075aefb655Srie * Current instruction is:
9085aefb655Srie *
9095aefb655Srie * or r1, %lo(x), r2
9105aefb655Srie * or
9115aefb655Srie * add r1, %lo(x), r2
9125aefb655Srie *
9135aefb655Srie * Need to udpate this to:
9145aefb655Srie *
9155aefb655Srie * xor r1, %lox(x), r2
9165aefb655Srie */
917051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
918bf994817SAli Bahrami R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name));
919ba2be530Sab196087 w = bswap ? ld_bswap_Word(*offset) : *offset;
920ba2be530Sab196087 w = TLS_GD_LE_XOR |
921ba2be530Sab196087 (w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RD));
922ba2be530Sab196087 *offset = bswap ? ld_bswap_Word(w) : w;
9235aefb655Srie arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
9245aefb655Srie return (FIX_RELOC);
9255aefb655Srie
9265aefb655Srie case R_SPARC_TLS_IE_LD:
9275aefb655Srie case R_SPARC_TLS_IE_LDX:
9285aefb655Srie /*
9295aefb655Srie * Current instruction:
9305aefb655Srie * ld{x} [r1 + r2], r3
9315aefb655Srie *
9325aefb655Srie * Need to update this to:
9335aefb655Srie *
9345aefb655Srie * mov r2, r3 (or %g0, r2, r3)
9355aefb655Srie */
936051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
937bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
938ba2be530Sab196087 w = bswap ? ld_bswap_Word(*offset) : *offset;
939ba2be530Sab196087 w = (w & (FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | TLS_IE_LE_OR;
940ba2be530Sab196087 *offset = bswap ? ld_bswap_Word(w) : w;
9415aefb655Srie return (FIX_DONE);
9425aefb655Srie
9435aefb655Srie case R_SPARC_TLS_LDO_ADD:
9445aefb655Srie case R_SPARC_TLS_GD_ADD:
9455aefb655Srie /*
9465aefb655Srie * Current instruction is:
9475aefb655Srie *
9485aefb655Srie * add gptr_reg, r2, r3
9495aefb655Srie *
9505aefb655Srie * Need to updated this to:
9515aefb655Srie *
9525aefb655Srie * add %g7, r2, r3
9535aefb655Srie */
954051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
955bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
956ba2be530Sab196087 w = bswap ? ld_bswap_Word(*offset) : *offset;
957ba2be530Sab196087 w = w & (~FM3_REG_MSK_RS1);
958ba2be530Sab196087 w = w | (REG_G7 << 14);
959ba2be530Sab196087 *offset = bswap ? ld_bswap_Word(w) : w;
9605aefb655Srie return (FIX_DONE);
9615aefb655Srie
9625aefb655Srie case R_SPARC_TLS_LDM_CALL:
963051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
964bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
9655aefb655Srie *offset = TLS_LD_LE_CLRO0;
966ba2be530Sab196087 if (bswap)
967ba2be530Sab196087 *offset = ld_bswap_Word(*offset);
9685aefb655Srie return (FIX_DONE);
9695aefb655Srie
9705aefb655Srie case R_SPARC_TLS_LDM_HI22:
9715aefb655Srie case R_SPARC_TLS_LDM_LO10:
9725aefb655Srie case R_SPARC_TLS_LDM_ADD:
9735aefb655Srie case R_SPARC_TLS_IE_ADD:
9745aefb655Srie case R_SPARC_TLS_GD_CALL:
975051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
976bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
9775aefb655Srie *offset = M_NOP;
978ba2be530Sab196087 if (bswap)
979ba2be530Sab196087 *offset = ld_bswap_Word(*offset);
9805aefb655Srie return (FIX_DONE);
9815aefb655Srie }
9825aefb655Srie return (FIX_RELOC);
9835aefb655Srie }
9845aefb655Srie
9855aefb655Srie #define GOTOP_ADDINST 0x80000000 /* add %g0, %g0, %g0 */
9865aefb655Srie
9875aefb655Srie static Fixupret
gotop_fixups(Ofl_desc * ofl,Rel_desc * arsp)9885aefb655Srie gotop_fixups(Ofl_desc *ofl, Rel_desc *arsp)
9895aefb655Srie {
9905aefb655Srie Word rtype = arsp->rel_rtype;
991ba2be530Sab196087 Word *offset, w;
9925aefb655Srie const char *ifl_name;
993de777a60Sab196087 Conv_inv_buf_t inv_buf;
994ba2be530Sab196087 int bswap;
9955aefb655Srie
9965aefb655Srie switch (rtype) {
9975aefb655Srie case R_SPARC_GOTDATA_OP_HIX22:
998051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
999bf994817SAli Bahrami R_SPARC_GOTDATA_HIX22, arsp, ld_reloc_sym_name));
10005aefb655Srie arsp->rel_rtype = R_SPARC_GOTDATA_HIX22;
10015aefb655Srie return (FIX_RELOC);
10025aefb655Srie
10035aefb655Srie case R_SPARC_GOTDATA_OP_LOX10:
1004051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
1005bf994817SAli Bahrami R_SPARC_GOTDATA_LOX10, arsp, ld_reloc_sym_name));
10065aefb655Srie arsp->rel_rtype = R_SPARC_GOTDATA_LOX10;
10075aefb655Srie return (FIX_RELOC);
10085aefb655Srie
10095aefb655Srie case R_SPARC_GOTDATA_OP:
10105aefb655Srie /*
10115aefb655Srie * Current instruction:
10125aefb655Srie * ld{x} [r1 + r2], r3
10135aefb655Srie *
10145aefb655Srie * Need to update this to:
10155aefb655Srie *
10165aefb655Srie * add r1, r2, r3
10175aefb655Srie */
1018051d39bbSrie DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
1019bf994817SAli Bahrami R_SPARC_NONE, arsp, ld_reloc_sym_name));
1020ba2be530Sab196087 offset = (Word *)(uintptr_t)(arsp->rel_roffset +
10215aefb655Srie _elf_getxoff(arsp->rel_isdesc->is_indata) +
1022bf994817SAli Bahrami (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
1023ba2be530Sab196087 bswap = OFL_SWAP_RELOC_DATA(ofl, arsp);
1024ba2be530Sab196087 w = bswap ? ld_bswap_Word(*offset) : *offset;
1025ba2be530Sab196087 w = (w & (FM3_REG_MSK_RS1 |
10265aefb655Srie FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | GOTOP_ADDINST;
1027ba2be530Sab196087 *offset = bswap ? ld_bswap_Word(w) : w;
10285aefb655Srie return (FIX_DONE);
10295aefb655Srie }
10305aefb655Srie /*
10315aefb655Srie * We should not get here
10325aefb655Srie */
10335aefb655Srie if (arsp->rel_isdesc->is_file)
10345aefb655Srie ifl_name = arsp->rel_isdesc->is_file->ifl_name;
10355aefb655Srie else
10365aefb655Srie ifl_name = MSG_INTL(MSG_STR_NULL);
10375aefb655Srie
1038*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADGOTFIX),
1039de777a60Sab196087 conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
1040bf994817SAli Bahrami ifl_name, ld_reloc_sym_name(arsp));
10415aefb655Srie
10425aefb655Srie assert(0);
10435aefb655Srie return (FIX_ERROR);
10445aefb655Srie }
10455aefb655Srie
1046ba2be530Sab196087 static uintptr_t
ld_do_activerelocs(Ofl_desc * ofl)10475aefb655Srie ld_do_activerelocs(Ofl_desc *ofl)
10485aefb655Srie {
10495aefb655Srie Rel_desc *arsp;
1050bf994817SAli Bahrami Rel_cachebuf *rcbp;
105157ef7aa9SRod Evans Aliste idx;
10525aefb655Srie uintptr_t return_code = 1;
10531d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags;
10545aefb655Srie
1055bf994817SAli Bahrami if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
10565aefb655Srie DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
10577010c12aSrie
10585aefb655Srie /*
10595aefb655Srie * Process active relocations.
10605aefb655Srie */
1061bf994817SAli Bahrami REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
10625aefb655Srie uchar_t *addr;
10635aefb655Srie Xword value;
10645aefb655Srie Sym_desc *sdp;
10655aefb655Srie const char *ifl_name;
10665aefb655Srie Xword refaddr;
1067bf994817SAli Bahrami Os_desc *osp;
10685aefb655Srie
10695aefb655Srie /*
1070bf994817SAli Bahrami * If the section this relocation is against has been discarded
1071bf994817SAli Bahrami * (-zignore), then discard (skip) the relocation itself.
10725aefb655Srie */
10735aefb655Srie if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
1074bf994817SAli Bahrami ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
10755aefb655Srie FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
1076bf994817SAli Bahrami DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
10775aefb655Srie continue;
10785aefb655Srie }
10795aefb655Srie
10805aefb655Srie /*
10815aefb655Srie * Perform any required TLS fixups.
10825aefb655Srie */
10835aefb655Srie if (arsp->rel_flags & FLG_REL_TLSFIX) {
10845aefb655Srie Fixupret ret;
10855aefb655Srie
10865aefb655Srie if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
10875aefb655Srie return (S_ERROR);
10885aefb655Srie if (ret == FIX_DONE)
10895aefb655Srie continue;
10905aefb655Srie }
10915aefb655Srie
10925aefb655Srie /*
10935aefb655Srie * Perform any required GOTOP fixups.
10945aefb655Srie */
10955aefb655Srie if (arsp->rel_flags & FLG_REL_GOTFIX) {
10965aefb655Srie Fixupret ret;
10975aefb655Srie
1098bf994817SAli Bahrami if ((ret = gotop_fixups(ofl, arsp)) == FIX_ERROR)
10995aefb655Srie return (S_ERROR);
11005aefb655Srie if (ret == FIX_DONE)
11015aefb655Srie continue;
11025aefb655Srie }
11035aefb655Srie
11045aefb655Srie /*
11055aefb655Srie * If this is a relocation against the move table, or
11065aefb655Srie * expanded move table, adjust the relocation entries.
11075aefb655Srie */
1108bf994817SAli Bahrami if (RELAUX_GET_MOVE(arsp))
11095aefb655Srie ld_adj_movereloc(ofl, arsp);
11105aefb655Srie
11115aefb655Srie sdp = arsp->rel_sym;
11125aefb655Srie refaddr = arsp->rel_roffset +
11135aefb655Srie (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
11145aefb655Srie
11155aefb655Srie if ((arsp->rel_flags & FLG_REL_CLVAL) ||
11165aefb655Srie (arsp->rel_flags & FLG_REL_GOTCL))
11175aefb655Srie value = 0;
1118bf994817SAli Bahrami else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
11195aefb655Srie Sym_desc *sym;
11205aefb655Srie
11215aefb655Srie /*
11225aefb655Srie * The value for a symbol pointing to a SECTION
11235aefb655Srie * is based off of that sections position.
11245aefb655Srie */
11255aefb655Srie if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
1126bf994817SAli Bahrami (sym = ld_am_I_partial(arsp, arsp->rel_raddend))) {
11275aefb655Srie /*
1128bf994817SAli Bahrami * The symbol was moved, so adjust the value
1129bf994817SAli Bahrami * relative to the new section.
11305aefb655Srie */
1131bf994817SAli Bahrami value = _elf_getxoff(sym->sd_isc->is_indata);
1132bf994817SAli Bahrami if (sym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
11335aefb655Srie value += sym->sd_isc->
11345aefb655Srie is_osdesc->os_shdr->sh_addr;
1135b26cc8daSAli Bahrami
1136b26cc8daSAli Bahrami /*
1137bf994817SAli Bahrami * The original raddend covers the displacement
1138bf994817SAli Bahrami * from the section start to the desired
1139bf994817SAli Bahrami * address. The value computed above gets us
1140bf994817SAli Bahrami * from the section start to the start of the
1141bf994817SAli Bahrami * symbol range. Adjust the old raddend to
1142bf994817SAli Bahrami * remove the offset from section start to
1143bf994817SAli Bahrami * symbol start, leaving the displacement
1144bf994817SAli Bahrami * within the range of the symbol.
1145b26cc8daSAli Bahrami */
1146bf994817SAli Bahrami arsp->rel_raddend -= sym->sd_osym->st_value;
11475aefb655Srie } else {
1148bf994817SAli Bahrami value = _elf_getxoff(sdp->sd_isc->is_indata);
1149bf994817SAli Bahrami if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
11505aefb655Srie value += sdp->sd_isc->
11515aefb655Srie is_osdesc->os_shdr->sh_addr;
11525aefb655Srie }
11535aefb655Srie
11545aefb655Srie if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
11555aefb655Srie value -= ofl->ofl_tlsphdr->p_vaddr;
11562926dd2eSrie
11572926dd2eSrie } else if (IS_SIZE(arsp->rel_rtype)) {
11582926dd2eSrie /*
11592926dd2eSrie * Size relocations require the symbols size.
11602926dd2eSrie */
11612926dd2eSrie value = sdp->sd_sym->st_size;
116208278a5eSRod Evans
116308278a5eSRod Evans } else if ((sdp->sd_flags & FLG_SY_CAP) &&
116408278a5eSRod Evans sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
11655aefb655Srie /*
1166bf994817SAli Bahrami * If relocation is against a capabilities symbol, we
1167bf994817SAli Bahrami * need to jump to an associated PLT, so that at runtime
1168bf994817SAli Bahrami * ld.so.1 is involved to determine the best binding
1169bf994817SAli Bahrami * choice. Otherwise, the value is the symbols value.
11705aefb655Srie */
117108278a5eSRod Evans value = ld_calc_plt_addr(sdp, ofl);
117208278a5eSRod Evans
117308278a5eSRod Evans } else
11745aefb655Srie value = sdp->sd_sym->st_value;
11755aefb655Srie
11765aefb655Srie /*
11775aefb655Srie * Relocation against the GLOBAL_OFFSET_TABLE.
11785aefb655Srie */
1179bf994817SAli Bahrami if ((arsp->rel_flags & FLG_REL_GOT) &&
1180bf994817SAli Bahrami !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
1181bf994817SAli Bahrami return (S_ERROR);
1182bf994817SAli Bahrami osp = RELAUX_GET_OSDESC(arsp);
11835aefb655Srie
11845aefb655Srie /*
1185bf994817SAli Bahrami * If loadable and not producing a relocatable object add the
1186bf994817SAli Bahrami * sections virtual address to the reference address.
11875aefb655Srie */
11885aefb655Srie if ((arsp->rel_flags & FLG_REL_LOAD) &&
11895aefb655Srie ((flags & FLG_OF_RELOBJ) == 0))
1190bf994817SAli Bahrami refaddr +=
1191bf994817SAli Bahrami arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr;
11925aefb655Srie
11935aefb655Srie /*
1194bf994817SAli Bahrami * If this entry has a PLT assigned to it, its value is actually
1195bf994817SAli Bahrami * the address of the PLT (and not the address of the function).
11965aefb655Srie */
11975aefb655Srie if (IS_PLT(arsp->rel_rtype)) {
11985aefb655Srie if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
11995aefb655Srie value = ld_calc_plt_addr(sdp, ofl);
12005aefb655Srie }
12015aefb655Srie
12025aefb655Srie /*
12035aefb655Srie * Add relocations addend to value. Add extra
12045aefb655Srie * relocation addend if needed.
12055aefb655Srie */
1206bf2f215aSAli Bahrami value += arsp->rel_raddend;
12075aefb655Srie if (IS_EXTOFFSET(arsp->rel_rtype))
1208bf994817SAli Bahrami value += RELAUX_GET_TYPEDATA(arsp);
12095aefb655Srie
12105aefb655Srie /*
1211bf994817SAli Bahrami * Determine whether the value needs further adjustment. Filter
1212bf994817SAli Bahrami * through the attributes of the relocation to determine what
1213bf994817SAli Bahrami * adjustment is required. Note, many of the following cases
1214bf994817SAli Bahrami * are only applicable when a .got is present. As a .got is
1215bf994817SAli Bahrami * not generated when a relocatable object is being built,
1216bf994817SAli Bahrami * any adjustments that require a .got need to be skipped.
12175aefb655Srie */
12185aefb655Srie if ((arsp->rel_flags & FLG_REL_GOT) &&
12195aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) {
12205aefb655Srie Xword R1addr;
12215aefb655Srie uintptr_t R2addr;
12225aefb655Srie Sword gotndx;
12235aefb655Srie Gotndx *gnp;
12245aefb655Srie Gotref gref;
12255aefb655Srie
12265aefb655Srie /*
12275aefb655Srie * Clear the GOT table entry, on SPARC we clear
12285aefb655Srie * the entry and the 'value' if needed is stored
12295aefb655Srie * in an output relocations addend.
12305aefb655Srie *
12315aefb655Srie * Calculate offset into GOT at which to apply
12325aefb655Srie * the relocation.
12335aefb655Srie */
12345aefb655Srie if (arsp->rel_flags & FLG_REL_DTLS)
12355aefb655Srie gref = GOT_REF_TLSGD;
12365aefb655Srie else if (arsp->rel_flags & FLG_REL_MTLS)
12375aefb655Srie gref = GOT_REF_TLSLD;
12385aefb655Srie else if (arsp->rel_flags & FLG_REL_STLS)
12395aefb655Srie gref = GOT_REF_TLSIE;
12405aefb655Srie else
12415aefb655Srie gref = GOT_REF_GENERIC;
12425aefb655Srie
1243bf994817SAli Bahrami gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
12445aefb655Srie assert(gnp);
12455aefb655Srie
12465aefb655Srie if (arsp->rel_rtype == M_R_DTPOFF)
12475aefb655Srie gotndx = gnp->gn_gotndx + 1;
12485aefb655Srie else
12495aefb655Srie gotndx = gnp->gn_gotndx;
12505aefb655Srie
12515aefb655Srie /* LINTED */
1252bf994817SAli Bahrami R1addr = (Xword)((-neggotoffset * M_GOT_ENTSIZE) +
1253bf994817SAli Bahrami (gotndx * M_GOT_ENTSIZE));
12545aefb655Srie
12555aefb655Srie /*
12565aefb655Srie * Add the GOTs data's offset.
12575aefb655Srie */
1258bf994817SAli Bahrami R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
12595aefb655Srie
12605aefb655Srie DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
1261635216b6SRod Evans ELF_DBG_LD_ACT, M_MACH, SHT_RELA,
1262bf994817SAli Bahrami arsp, R1addr, value, ld_reloc_sym_name));
12635aefb655Srie
12645aefb655Srie /*
12655aefb655Srie * And do it.
12665aefb655Srie */
1267f3324781Sab196087 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
1268bf994817SAli Bahrami *(Xword *)R2addr = ld_bswap_Xword(value);
1269f3324781Sab196087 else
12705aefb655Srie *(Xword *)R2addr = value;
12715aefb655Srie continue;
12725aefb655Srie
12735aefb655Srie } else if (IS_GOT_BASED(arsp->rel_rtype) &&
12745aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) {
12755aefb655Srie value -= (ofl->ofl_osgot->os_shdr->sh_addr +
12765aefb655Srie (-neggotoffset * M_GOT_ENTSIZE));
12775aefb655Srie
12785aefb655Srie } else if (IS_PC_RELATIVE(arsp->rel_rtype)) {
12795aefb655Srie value -= refaddr;
12805aefb655Srie
12815aefb655Srie } else if (IS_TLS_INS(arsp->rel_rtype) &&
12825aefb655Srie IS_GOT_RELATIVE(arsp->rel_rtype) &&
12835aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) {
12845aefb655Srie Gotndx *gnp;
12855aefb655Srie Gotref gref;
12865aefb655Srie
12875aefb655Srie if (arsp->rel_flags & FLG_REL_STLS)
12885aefb655Srie gref = GOT_REF_TLSIE;
12895aefb655Srie else if (arsp->rel_flags & FLG_REL_DTLS)
12905aefb655Srie gref = GOT_REF_TLSGD;
12915aefb655Srie else if (arsp->rel_flags & FLG_REL_MTLS)
12925aefb655Srie gref = GOT_REF_TLSLD;
12935aefb655Srie
1294bf994817SAli Bahrami gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
12955aefb655Srie assert(gnp);
12965aefb655Srie
12975aefb655Srie value = gnp->gn_gotndx * M_GOT_ENTSIZE;
12985aefb655Srie
12995aefb655Srie } else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
13005aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) {
13015aefb655Srie Gotndx *gnp;
13025aefb655Srie
130357ef7aa9SRod Evans gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
13045aefb655Srie GOT_REF_GENERIC, ofl, arsp);
13055aefb655Srie assert(gnp);
13065aefb655Srie
13075aefb655Srie value = gnp->gn_gotndx * M_GOT_ENTSIZE;
13085aefb655Srie
13095aefb655Srie } else if ((arsp->rel_flags & FLG_REL_STLS) &&
13105aefb655Srie ((flags & FLG_OF_RELOBJ) == 0)) {
13115aefb655Srie Xword tlsstatsize;
13125aefb655Srie
13135aefb655Srie /*
1314bf994817SAli Bahrami * This is the LE TLS reference model. Static offset is
1315bf994817SAli Bahrami * hard-coded, and negated so that it can be added to
1316bf994817SAli Bahrami * the thread pointer (%g7)
13175aefb655Srie */
1318bf994817SAli Bahrami tlsstatsize =
1319bf994817SAli Bahrami S_ROUND(ofl->ofl_tlsphdr->p_memsz, M_TLSSTATALIGN);
13205aefb655Srie value = -(tlsstatsize - value);
13215aefb655Srie }
13225aefb655Srie
13235aefb655Srie if (arsp->rel_isdesc->is_file)
13245aefb655Srie ifl_name = arsp->rel_isdesc->is_file->ifl_name;
13255aefb655Srie else
13265aefb655Srie ifl_name = MSG_INTL(MSG_STR_NULL);
13275aefb655Srie
13285aefb655Srie /*
1329bf994817SAli Bahrami * Make sure we have data to relocate. Compiler and assembler
1330bf994817SAli Bahrami * developers have been known to generate relocations against
1331bf994817SAli Bahrami * invalid sections (normally .bss), so for their benefit give
1332bf994817SAli Bahrami * them sufficient information to help analyze the problem.
1333bf994817SAli Bahrami * End users should never see this.
13345aefb655Srie */
13355aefb655Srie if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1336de777a60Sab196087 Conv_inv_buf_t inv_buf;
1337de777a60Sab196087
1338*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
1339bf994817SAli Bahrami conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
1340bf994817SAli Bahrami ifl_name, ld_reloc_sym_name(arsp),
13414a8d0ea7SAli Bahrami EC_WORD(arsp->rel_isdesc->is_scnndx),
13425aefb655Srie arsp->rel_isdesc->is_name);
13435aefb655Srie return (S_ERROR);
13445aefb655Srie }
13455aefb655Srie
13465aefb655Srie /*
13475aefb655Srie * Get the address of the data item we need to modify.
13485aefb655Srie */
13495aefb655Srie addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1350bf994817SAli Bahrami (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
13515aefb655Srie
1352635216b6SRod Evans DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
1353bf994817SAli Bahrami M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value,
1354bf994817SAli Bahrami ld_reloc_sym_name));
1355bf994817SAli Bahrami addr += (uintptr_t)osp->os_outdata->d_buf;
13565aefb655Srie
13575aefb655Srie if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
13585aefb655Srie ofl->ofl_size) || (arsp->rel_roffset >
1359bf994817SAli Bahrami osp->os_shdr->sh_size)) {
1360de777a60Sab196087 Conv_inv_buf_t inv_buf;
13615aefb655Srie int class;
13625aefb655Srie
1363bf994817SAli Bahrami if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1364bf994817SAli Bahrami ofl->ofl_size)
13655aefb655Srie class = ERR_FATAL;
13665aefb655Srie else
13675aefb655Srie class = ERR_WARNING;
13685aefb655Srie
1369*1007fd6fSAli Bahrami ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
1370bf994817SAli Bahrami conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
1371bf994817SAli Bahrami ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1372bf994817SAli Bahrami arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
13735aefb655Srie EC_ADDR((uintptr_t)addr -
13745aefb655Srie (uintptr_t)ofl->ofl_nehdr));
13755aefb655Srie
13765aefb655Srie if (class == ERR_FATAL) {
13775aefb655Srie return_code = S_ERROR;
13785aefb655Srie continue;
13795aefb655Srie }
13805aefb655Srie }
13815aefb655Srie
13825aefb655Srie /*
1383bf994817SAli Bahrami * If '-z noreloc' is specified - skip the do_reloc stage.
13845aefb655Srie */
1385f3324781Sab196087 if (OFL_DO_RELOC(ofl)) {
1386bf994817SAli Bahrami if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1387bf994817SAli Bahrami ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
1388*1007fd6fSAli Bahrami ofl->ofl_lml) == 0) {
1389*1007fd6fSAli Bahrami ofl->ofl_flags |= FLG_OF_FATAL;
13905aefb655Srie return_code = S_ERROR;
13915aefb655Srie }
13925aefb655Srie }
1393*1007fd6fSAli Bahrami }
13945aefb655Srie return (return_code);
13955aefb655Srie }
13965aefb655Srie
1397ba2be530Sab196087 static uintptr_t
ld_add_outrel(Word flags,Rel_desc * rsp,Ofl_desc * ofl)13985aefb655Srie ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
13995aefb655Srie {
14005aefb655Srie Rel_desc *orsp;
14015aefb655Srie Sym_desc *sdp = rsp->rel_sym;
1402de777a60Sab196087 Conv_inv_buf_t inv_buf;
14035aefb655Srie
14045aefb655Srie /*
14055aefb655Srie * Static executables *do not* want any relocations against them.
14065aefb655Srie * Since our engine still creates relocations against a WEAK UNDEFINED
14075aefb655Srie * symbol in a static executable, it's best to disable them here
14085aefb655Srie * instead of through out the relocation code.
14095aefb655Srie */
1410635216b6SRod Evans if (OFL_IS_STATIC_EXEC(ofl))
14115aefb655Srie return (1);
14125aefb655Srie
14135aefb655Srie /*
14145aefb655Srie * Certain relocations do not make sense in a 64bit shared object,
14155aefb655Srie * if building a shared object do a sanity check on the output
14165aefb655Srie * relocations being created.
14175aefb655Srie */
14185aefb655Srie if (ofl->ofl_flags & FLG_OF_SHAROBJ) {
14195aefb655Srie Word rtype = rsp->rel_rtype;
14205aefb655Srie /*
14215aefb655Srie * Because the R_SPARC_HIPLT22 & R_SPARC_LOPLT10 relocations
14225aefb655Srie * are not relative they make no sense to create in a shared
14235aefb655Srie * object - so emit the proper error message if that occurs.
14245aefb655Srie */
1425de777a60Sab196087 if ((rtype == R_SPARC_HIPLT22) || (rtype == R_SPARC_LOPLT10)) {
1426*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_UNRELREL),
1427de777a60Sab196087 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
14285aefb655Srie rsp->rel_isdesc->is_file->ifl_name,
1429bf994817SAli Bahrami ld_reloc_sym_name(rsp));
14305aefb655Srie return (S_ERROR);
14315aefb655Srie }
14325aefb655Srie #if defined(_ELF64)
14335aefb655Srie /*
14345aefb655Srie * Each of the following relocations requires that the
14355aefb655Srie * object being built be loaded in either the upper 32 or
14365aefb655Srie * 44 bit range of memory. Since shared libraries traditionally
14375aefb655Srie * are loaded in the lower range of memory - this isn't going
14385aefb655Srie * to work.
14395aefb655Srie */
14405aefb655Srie if ((rtype == R_SPARC_H44) || (rtype == R_SPARC_M44) ||
14415aefb655Srie (rtype == R_SPARC_L44)) {
1442*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SHOBJABS44),
1443de777a60Sab196087 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
14445aefb655Srie rsp->rel_isdesc->is_file->ifl_name,
1445bf994817SAli Bahrami ld_reloc_sym_name(rsp));
14465aefb655Srie return (S_ERROR);
14475aefb655Srie }
14485aefb655Srie #endif
14495aefb655Srie }
14505aefb655Srie
14515aefb655Srie /*
14525aefb655Srie * If we are adding a output relocation against a section
14535aefb655Srie * symbol (non-RELATIVE) then mark that section. These sections
14545aefb655Srie * will be added to the .dynsym symbol table.
14555aefb655Srie */
14565aefb655Srie if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
14575aefb655Srie ((flags & FLG_REL_SCNNDX) ||
14585aefb655Srie (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
14595aefb655Srie
14605aefb655Srie /*
14615aefb655Srie * If this is a COMMON symbol - no output section
14625aefb655Srie * exists yet - (it's created as part of sym_validate()).
14635aefb655Srie * So - we mark here that when it's created it should
14645aefb655Srie * be tagged with the FLG_OS_OUTREL flag.
14655aefb655Srie */
14665aefb655Srie if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
14670bc07c75Srie (sdp->sd_sym->st_shndx == SHN_COMMON)) {
14685aefb655Srie if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
14695aefb655Srie ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
14705aefb655Srie else
14715aefb655Srie ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
14725aefb655Srie } else {
147308278a5eSRod Evans Os_desc *osp;
147408278a5eSRod Evans Is_desc *isp = sdp->sd_isc;
14755aefb655Srie
147608278a5eSRod Evans if (isp && ((osp = isp->is_osdesc) != NULL) &&
147708278a5eSRod Evans ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
14785aefb655Srie ofl->ofl_dynshdrcnt++;
14795aefb655Srie osp->os_flags |= FLG_OS_OUTREL;
14805aefb655Srie }
14815aefb655Srie }
14825aefb655Srie }
14835aefb655Srie
1484bf994817SAli Bahrami /* Enter it into the output relocation cache */
1485bf994817SAli Bahrami if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1486bf994817SAli Bahrami return (S_ERROR);
14875aefb655Srie
14885aefb655Srie if (flags & FLG_REL_GOT)
14895aefb655Srie ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
14905aefb655Srie else if (flags & FLG_REL_PLT)
14915aefb655Srie ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
14925aefb655Srie else if (flags & FLG_REL_BSS)
14935aefb655Srie ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
14945aefb655Srie else if (flags & FLG_REL_NOINFO)
14955aefb655Srie ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
14965aefb655Srie else
1497bf994817SAli Bahrami RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela);
14985aefb655Srie
14995aefb655Srie if (orsp->rel_rtype == M_R_RELATIVE)
15005aefb655Srie ofl->ofl_relocrelcnt++;
15015aefb655Srie
15025aefb655Srie #if defined(_ELF64)
15035aefb655Srie /*
15045aefb655Srie * When building a 64-bit object any R_SPARC_WDISP30 relocation is given
15055aefb655Srie * a plt padding entry, unless we're building a relocatable object
15065aefb655Srie * (ld -r) or -b is in effect.
15075aefb655Srie */
15085aefb655Srie if ((orsp->rel_rtype == R_SPARC_WDISP30) &&
15095aefb655Srie ((ofl->ofl_flags & (FLG_OF_BFLAG | FLG_OF_RELOBJ)) == 0) &&
15105aefb655Srie ((orsp->rel_sym->sd_flags & FLG_SY_PLTPAD) == 0)) {
15115aefb655Srie ofl->ofl_pltpad++;
15125aefb655Srie orsp->rel_sym->sd_flags |= FLG_SY_PLTPAD;
15135aefb655Srie }
15145aefb655Srie #endif
15155aefb655Srie /*
15165aefb655Srie * We don't perform sorting on PLT relocations because
15175aefb655Srie * they have already been assigned a PLT index and if we
15185aefb655Srie * were to sort them we would have to re-assign the plt indexes.
15195aefb655Srie */
15205aefb655Srie if (!(flags & FLG_REL_PLT))
15215aefb655Srie ofl->ofl_reloccnt++;
15225aefb655Srie
15235aefb655Srie /*
15245aefb655Srie * Insure a GLOBAL_OFFSET_TABLE is generated if required.
15255aefb655Srie */
15265aefb655Srie if (IS_GOT_REQUIRED(orsp->rel_rtype))
15275aefb655Srie ofl->ofl_flags |= FLG_OF_BLDGOT;
15285aefb655Srie
15295aefb655Srie /*
15305aefb655Srie * Identify and possibly warn of a displacement relocation.
15315aefb655Srie */
15325aefb655Srie if (orsp->rel_flags & FLG_REL_DISP) {
15335aefb655Srie ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
15345aefb655Srie
15355aefb655Srie if (ofl->ofl_flags & FLG_OF_VERBOSE)
15365aefb655Srie ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
15375aefb655Srie }
15385aefb655Srie DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
15395aefb655Srie M_MACH, orsp));
15405aefb655Srie return (1);
15415aefb655Srie }
15425aefb655Srie
15435aefb655Srie /*
15445aefb655Srie * Process relocation against a register symbol. Note, of -z muldefs is in
15455aefb655Srie * effect there may have been multiple register definitions, which would have
15465aefb655Srie * been processed as non-fatal, with the first definition winning. But, we
15475aefb655Srie * will also process multiple relocations for these multiple definitions. In
15485aefb655Srie * this case we must only preserve the relocation for the definition that was
15495aefb655Srie * kept. The sad part is that register relocations don't typically specify
15505aefb655Srie * the register symbol with which they are associated, so we might have to
15515aefb655Srie * search the input files global symbols to determine if this relocation is
15525aefb655Srie * appropriate.
15535aefb655Srie */
1554ba2be530Sab196087 static uintptr_t
ld_reloc_register(Rel_desc * rsp,Is_desc * isp,Ofl_desc * ofl)15555aefb655Srie ld_reloc_register(Rel_desc *rsp, Is_desc *isp, Ofl_desc *ofl)
15565aefb655Srie {
15575aefb655Srie if (ofl->ofl_flags & FLG_OF_MULDEFS) {
15585aefb655Srie Ifl_desc *ifl = isp->is_file;
15595aefb655Srie Sym_desc *sdp = rsp->rel_sym;
15605aefb655Srie
15615aefb655Srie if (sdp == 0) {
15625aefb655Srie Xword offset = rsp->rel_roffset;
15635aefb655Srie Word ndx;
15645aefb655Srie
15655aefb655Srie for (ndx = ifl->ifl_locscnt;
15665aefb655Srie ndx < ifl->ifl_symscnt; ndx++) {
15675aefb655Srie if (((sdp = ifl->ifl_oldndx[ndx]) != 0) &&
15685aefb655Srie (sdp->sd_flags & FLG_SY_REGSYM) &&
15695aefb655Srie (sdp->sd_sym->st_value == offset))
15705aefb655Srie break;
15715aefb655Srie }
15725aefb655Srie }
15735aefb655Srie if (sdp && (sdp->sd_file != ifl))
15745aefb655Srie return (1);
15755aefb655Srie }
15765aefb655Srie return (ld_add_outrel((rsp->rel_flags | FLG_REL_REG), rsp, ofl));
15775aefb655Srie }
15785aefb655Srie
15795aefb655Srie /*
15805aefb655Srie * process relocation for a LOCAL symbol
15815aefb655Srie */
1582ba2be530Sab196087 static uintptr_t
ld_reloc_local(Rel_desc * rsp,Ofl_desc * ofl)15835aefb655Srie ld_reloc_local(Rel_desc *rsp, Ofl_desc *ofl)
15845aefb655Srie {
15851d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags;
15865aefb655Srie Sym_desc *sdp = rsp->rel_sym;
15870bc07c75Srie Word shndx = sdp->sd_sym->st_shndx;
15885aefb655Srie
15895aefb655Srie /*
15905aefb655Srie * if ((shared object) and (not pc relative relocation) and
15915aefb655Srie * (not against ABS symbol))
15925aefb655Srie * then
15935aefb655Srie * if (rtype != R_SPARC_32)
15945aefb655Srie * then
15955aefb655Srie * build relocation against section
15965aefb655Srie * else
15975aefb655Srie * build R_SPARC_RELATIVE
15985aefb655Srie * fi
15995aefb655Srie * fi
16005aefb655Srie */
16015aefb655Srie if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) &&
16022926dd2eSrie !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
16035aefb655Srie !(IS_GOT_BASED(rsp->rel_rtype)) &&
16045aefb655Srie !(rsp->rel_isdesc != NULL &&
16055aefb655Srie (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
16065aefb655Srie (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
16075aefb655Srie (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
16085aefb655Srie Word ortype = rsp->rel_rtype;
16095aefb655Srie
16105aefb655Srie if ((rsp->rel_rtype != R_SPARC_32) &&
16115aefb655Srie (rsp->rel_rtype != R_SPARC_PLT32) &&
16125aefb655Srie (rsp->rel_rtype != R_SPARC_64))
16135aefb655Srie return (ld_add_outrel((FLG_REL_SCNNDX | FLG_REL_ADVAL),
16145aefb655Srie rsp, ofl));
16155aefb655Srie
16165aefb655Srie rsp->rel_rtype = R_SPARC_RELATIVE;
16175aefb655Srie if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
16185aefb655Srie return (S_ERROR);
16195aefb655Srie rsp->rel_rtype = ortype;
16205aefb655Srie return (1);
16215aefb655Srie }
16225aefb655Srie
16235aefb655Srie /*
16245aefb655Srie * If the relocation is against a 'non-allocatable' section
16255aefb655Srie * and we can not resolve it now - then give a warning
16265aefb655Srie * message.
16275aefb655Srie *
16285aefb655Srie * We can not resolve the symbol if either:
16295aefb655Srie * a) it's undefined
16305aefb655Srie * b) it's defined in a shared library and a
16315aefb655Srie * COPY relocation hasn't moved it to the executable
16325aefb655Srie *
16335aefb655Srie * Note: because we process all of the relocations against the
16345aefb655Srie * text segment before any others - we know whether
16355aefb655Srie * or not a copy relocation will be generated before
16365aefb655Srie * we get here (see reloc_init()->reloc_segments()).
16375aefb655Srie */
16385aefb655Srie if (!(rsp->rel_flags & FLG_REL_LOAD) &&
16395aefb655Srie ((shndx == SHN_UNDEF) ||
16405aefb655Srie ((sdp->sd_ref == REF_DYN_NEED) &&
16415aefb655Srie ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1642de777a60Sab196087 Conv_inv_buf_t inv_buf;
1643bf994817SAli Bahrami Os_desc *osp = RELAUX_GET_OSDESC(rsp);
1644de777a60Sab196087
16455aefb655Srie /*
16465aefb655Srie * If the relocation is against a SHT_SUNW_ANNOTATE
16475aefb655Srie * section - then silently ignore that the relocation
16485aefb655Srie * can not be resolved.
16495aefb655Srie */
1650bf994817SAli Bahrami if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
16515aefb655Srie return (0);
1652*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1653de777a60Sab196087 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
16545aefb655Srie rsp->rel_isdesc->is_file->ifl_name,
1655bf994817SAli Bahrami ld_reloc_sym_name(rsp), osp->os_name);
16565aefb655Srie return (1);
16575aefb655Srie }
16585aefb655Srie
16595aefb655Srie /*
16605aefb655Srie * Perform relocation.
16615aefb655Srie */
16625aefb655Srie return (ld_add_actrel(NULL, rsp, ofl));
16635aefb655Srie }
16645aefb655Srie
1665051d39bbSrie /*
1666051d39bbSrie * Establish a relocation transition. Note, at this point of input relocation
1667051d39bbSrie * processing, we have no idea of the relocation value that will be used in
1668051d39bbSrie * the eventual relocation calculation. This value is only known after the
1669051d39bbSrie * initial image has been constructed. Therefore, there is a small chance
1670051d39bbSrie * that a value can exceed the capabilities of the transitioned relocation.
1671051d39bbSrie * One example might be the offset from the GOT to a symbol.
1672051d39bbSrie *
1673051d39bbSrie * The only instance of this failure discovered so far has been via the use of
1674051d39bbSrie * ABS symbols to represent an external memory location. This situation is
1675051d39bbSrie * rare, since ABS symbols aren't typically generated by the compilers.
1676051d39bbSrie * Therefore, our solution is to excluded ABS symbols from the transition
1677051d39bbSrie * relocation possibilities. As an additional safeguard, if an inappropriate
1678051d39bbSrie * value is passed to the final relocation engine, a verification ("V")
1679051d39bbSrie * relocation should trigger a fatal error condition.
1680051d39bbSrie */
1681ba2be530Sab196087 static uintptr_t
ld_reloc_GOTOP(Boolean local,Rel_desc * rsp,Ofl_desc * ofl)16825aefb655Srie ld_reloc_GOTOP(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
16835aefb655Srie {
16845aefb655Srie Word rtype = rsp->rel_rtype;
16855aefb655Srie
1686051d39bbSrie if (!local || (rsp->rel_sym->sd_sym->st_shndx == SHN_ABS)) {
16875aefb655Srie /*
16885aefb655Srie * When binding to a external symbol, no fixups are required
16895aefb655Srie * and the GOTDATA_OP relocation can be ignored.
16905aefb655Srie */
16915aefb655Srie if (rtype == R_SPARC_GOTDATA_OP)
16925aefb655Srie return (1);
16935aefb655Srie return (ld_reloc_GOT_relative(local, rsp, ofl));
16945aefb655Srie }
16955aefb655Srie
16965aefb655Srie /*
16975aefb655Srie * When binding to a local symbol the relocations can be transitioned:
16985aefb655Srie *
16995aefb655Srie * R_*_GOTDATA_OP_HIX22 -> R_*_GOTDATA_HIX22
17005aefb655Srie * R_*_GOTDATA_OP_LOX10 -> R_*_GOTDATA_LOX10
17015aefb655Srie * R_*_GOTDATA_OP -> instruction fixup
17025aefb655Srie */
17035aefb655Srie return (ld_add_actrel(FLG_REL_GOTFIX, rsp, ofl));
17045aefb655Srie }
17055aefb655Srie
1706ba2be530Sab196087 static uintptr_t
ld_reloc_TLS(Boolean local,Rel_desc * rsp,Ofl_desc * ofl)17075aefb655Srie ld_reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
17085aefb655Srie {
17095aefb655Srie Word rtype = rsp->rel_rtype;
17105aefb655Srie Sym_desc *sdp = rsp->rel_sym;
17111d9df23bSab196087 ofl_flag_t flags = ofl->ofl_flags;
17125aefb655Srie Gotndx *gnp;
17135aefb655Srie
17145aefb655Srie /*
1715d326b23bSrie * If we're building an executable - use either the IE or LE access
1716d326b23bSrie * model. If we're building a shared object process any IE model.
17175aefb655Srie */
1718d326b23bSrie if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
17195aefb655Srie /*
1720d326b23bSrie * Set the DF_STATIC_TLS flag.
17215aefb655Srie */
17225aefb655Srie ofl->ofl_dtflags |= DF_STATIC_TLS;
17235aefb655Srie
1724d326b23bSrie if (!local || ((flags & FLG_OF_EXEC) == 0)) {
17255aefb655Srie /*
1726d326b23bSrie * When processing static TLS - these relocations
17275aefb655Srie * can be ignored.
17285aefb655Srie */
17295aefb655Srie if ((rtype == R_SPARC_TLS_IE_LD) ||
17305aefb655Srie (rtype == R_SPARC_TLS_IE_LDX) ||
17315aefb655Srie (rtype == R_SPARC_TLS_IE_ADD))
17325aefb655Srie return (1);
17335aefb655Srie
17345aefb655Srie /*
1735d326b23bSrie * Assign a GOT entry for IE static TLS references.
17365aefb655Srie */
17375aefb655Srie if (((rtype == R_SPARC_TLS_GD_HI22) ||
17385aefb655Srie (rtype == R_SPARC_TLS_GD_LO10) ||
17395aefb655Srie (rtype == R_SPARC_TLS_IE_HI22) ||
17405aefb655Srie (rtype == R_SPARC_TLS_IE_LO10)) &&
174157ef7aa9SRod Evans ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
174257ef7aa9SRod Evans GOT_REF_TLSIE, ofl, rsp)) == NULL)) {
1743d326b23bSrie
1744d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1745d326b23bSrie gnp, GOT_REF_TLSIE, FLG_REL_STLS,
174657ef7aa9SRod Evans rtype, M_R_TPOFF, NULL) == S_ERROR)
17475aefb655Srie return (S_ERROR);
17485aefb655Srie }
17495aefb655Srie
1750d326b23bSrie /*
1751d326b23bSrie * IE access model.
1752d326b23bSrie */
17535aefb655Srie if (IS_TLS_IE(rtype))
17545aefb655Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
17555aefb655Srie
17565aefb655Srie /*
1757d326b23bSrie * Fixups are required for other executable models.
17585aefb655Srie */
17595aefb655Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
17605aefb655Srie rsp, ofl));
17615aefb655Srie }
1762d326b23bSrie
17635aefb655Srie /*
1764d326b23bSrie * LE access model.
17655aefb655Srie */
17665aefb655Srie if (IS_TLS_LE(rtype))
17675aefb655Srie return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
17685aefb655Srie
17695aefb655Srie /*
1770d326b23bSrie * When processing static TLS - these relocations can be
1771d326b23bSrie * ignored.
17725aefb655Srie */
17735aefb655Srie if (rtype == R_SPARC_TLS_IE_ADD)
17745aefb655Srie return (1);
17755aefb655Srie
17765aefb655Srie return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
17775aefb655Srie rsp, ofl));
17785aefb655Srie }
17795aefb655Srie
17805aefb655Srie /*
1781d326b23bSrie * Building a shared object.
1782d326b23bSrie *
1783d326b23bSrie * For dynamic TLS references, ADD relocations are ignored.
17845aefb655Srie */
17855aefb655Srie if ((rtype == R_SPARC_TLS_GD_ADD) || (rtype == R_SPARC_TLS_LDM_ADD) ||
17865aefb655Srie (rtype == R_SPARC_TLS_LDO_ADD))
17875aefb655Srie return (1);
17885aefb655Srie
17895aefb655Srie /*
17905aefb655Srie * Assign a GOT entry for a dynamic TLS reference.
17915aefb655Srie */
17925aefb655Srie if (((rtype == R_SPARC_TLS_LDM_HI22) ||
17935aefb655Srie (rtype == R_SPARC_TLS_LDM_LO10)) &&
179457ef7aa9SRod Evans ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSLD,
179557ef7aa9SRod Evans ofl, rsp)) == NULL)) {
17965aefb655Srie
1797d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1798d326b23bSrie FLG_REL_MTLS, rtype, M_R_DTPMOD, 0) == S_ERROR)
17995aefb655Srie return (S_ERROR);
18005aefb655Srie
1801d326b23bSrie } else if (((rtype == R_SPARC_TLS_GD_HI22) ||
1802d326b23bSrie (rtype == R_SPARC_TLS_GD_LO10)) &&
180357ef7aa9SRod Evans ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
180457ef7aa9SRod Evans ofl, rsp)) == NULL)) {
18055aefb655Srie
1806d326b23bSrie if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1807d326b23bSrie FLG_REL_DTLS, rtype, M_R_DTPMOD, M_R_DTPOFF) == S_ERROR)
18085aefb655Srie return (S_ERROR);
18095aefb655Srie }
1810d326b23bSrie
18115aefb655Srie /*
18125aefb655Srie * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1813d326b23bSrie * cause a call to __tls_get_addr(). Convert this relocation to that
1814d326b23bSrie * symbol now, and prepare for the PLT magic.
18155aefb655Srie */
18165aefb655Srie if ((rtype == R_SPARC_TLS_GD_CALL) || (rtype == R_SPARC_TLS_LDM_CALL)) {
18175aefb655Srie Sym_desc *tlsgetsym;
18185aefb655Srie
18195aefb655Srie if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_U),
1820f5a18a30Srie ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
18215aefb655Srie return (S_ERROR);
1822d326b23bSrie
18235aefb655Srie rsp->rel_sym = tlsgetsym;
18245aefb655Srie rsp->rel_rtype = R_SPARC_WPLT30;
1825d326b23bSrie
18265aefb655Srie if (ld_reloc_plt(rsp, ofl) == S_ERROR)
18275aefb655Srie return (S_ERROR);
1828d326b23bSrie
18295aefb655Srie rsp->rel_sym = sdp;
18305aefb655Srie rsp->rel_rtype = rtype;
18315aefb655Srie return (1);
18325aefb655Srie }
18335aefb655Srie
18345aefb655Srie if (IS_TLS_LD(rtype))
18355aefb655Srie return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
18365aefb655Srie
18375aefb655Srie return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
18385aefb655Srie }
18395aefb655Srie
18405aefb655Srie /*
18415aefb655Srie * ld_allocate_got: if a GOT is to be made, after the section is built this
18425aefb655Srie * function is called to allocate all the GOT slots. The allocation is
18435aefb655Srie * deferred until after all GOTs have been counted and sorted according
18445aefb655Srie * to their size, for only then will we know how to allocate them on
18455aefb655Srie * a processor like SPARC which has different models for addressing the
18465aefb655Srie * GOT. SPARC has two: small and large, small uses a signed 13-bit offset
18475aefb655Srie * into the GOT, whereas large uses an unsigned 32-bit offset.
18485aefb655Srie */
18495aefb655Srie static Sword small_index; /* starting index for small GOT entries */
18506a074c93Sab196087 static Sword mixed_index; /* starting index for mixed GOT entries */
18515aefb655Srie static Sword large_index; /* starting index for large GOT entries */
18525aefb655Srie
1853ba2be530Sab196087 static uintptr_t
ld_assign_got(Ofl_desc * ofl,Sym_desc * sdp)18545aefb655Srie ld_assign_got(Ofl_desc *ofl, Sym_desc *sdp)
18555aefb655Srie {
185657ef7aa9SRod Evans Aliste idx;
18575aefb655Srie Gotndx *gnp;
18585aefb655Srie
185957ef7aa9SRod Evans for (ALIST_TRAVERSE(sdp->sd_GOTndxs, idx, gnp)) {
18605aefb655Srie uint_t gotents;
186157ef7aa9SRod Evans Gotref gref = gnp->gn_gotref;
186257ef7aa9SRod Evans
18635aefb655Srie if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
18645aefb655Srie gotents = 2;
18655aefb655Srie else
18665aefb655Srie gotents = 1;
18675aefb655Srie
18685aefb655Srie switch (gnp->gn_gotndx) {
18695aefb655Srie case M_GOT_SMALL:
18705aefb655Srie gnp->gn_gotndx = small_index;
18715aefb655Srie small_index += gotents;
18725aefb655Srie if (small_index == 0)
18735aefb655Srie small_index = M_GOT_XNumber;
18745aefb655Srie break;
18756a074c93Sab196087 case M_GOT_MIXED:
18766a074c93Sab196087 gnp->gn_gotndx = mixed_index;
18776a074c93Sab196087 mixed_index += gotents;
18786a074c93Sab196087 break;
18795aefb655Srie case M_GOT_LARGE:
18805aefb655Srie gnp->gn_gotndx = large_index;
18815aefb655Srie large_index += gotents;
18825aefb655Srie break;
18835aefb655Srie default:
1884*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_ASSIGNGOT),
18855aefb655Srie EC_XWORD(gnp->gn_gotndx), demangle(sdp->sd_name));
18865aefb655Srie return (S_ERROR);
18875aefb655Srie }
18885aefb655Srie }
18895aefb655Srie return (1);
18905aefb655Srie }
18915aefb655Srie
1892ba2be530Sab196087 static uintptr_t
ld_assign_got_ndx(Alist ** alpp,Gotndx * pgnp,Gotref gref,Ofl_desc * ofl,Rel_desc * rsp,Sym_desc * sdp)189357ef7aa9SRod Evans ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
18945aefb655Srie Rel_desc *rsp, Sym_desc *sdp)
18955aefb655Srie {
18965aefb655Srie Xword raddend;
189757ef7aa9SRod Evans Gotndx gn, *gnp;
189857ef7aa9SRod Evans Aliste idx;
18995aefb655Srie uint_t gotents;
19005aefb655Srie
19016a074c93Sab196087 /* Some TLS requires two relocations with two GOT entries */
19025aefb655Srie if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
19035aefb655Srie gotents = 2;
19045aefb655Srie else
19055aefb655Srie gotents = 1;
19065aefb655Srie
19076a074c93Sab196087 raddend = rsp->rel_raddend;
19086a074c93Sab196087 if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) {
19096a074c93Sab196087
19106a074c93Sab196087 /*
19116a074c93Sab196087 * If an entry for this addend already exists, determine if it
19126a074c93Sab196087 * has mixed mode GOT access (both PIC and pic).
19136a074c93Sab196087 *
19146a074c93Sab196087 * In order to be accessible by both large and small pic,
19156a074c93Sab196087 * a mixed mode GOT must be located in the positive index
19166a074c93Sab196087 * range above _GLOBAL_OFFSET_TABLE_, and in the range
19176a074c93Sab196087 * reachable small pic. This is necessary because the large
19186a074c93Sab196087 * PIC mode cannot use a negative offset. This implies that
19196a074c93Sab196087 * there can be no more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
19206a074c93Sab196087 * such entries.
19216a074c93Sab196087 */
19226a074c93Sab196087 switch (pgnp->gn_gotndx) {
19236a074c93Sab196087 case M_GOT_SMALL:
19246a074c93Sab196087 /*
19256a074c93Sab196087 * This one was previously identified as a small
19266a074c93Sab196087 * GOT. If this access is large, then convert
19276a074c93Sab196087 * it to mixed.
19286a074c93Sab196087 */
19296a074c93Sab196087 if (rsp->rel_rtype != R_SPARC_GOT13) {
19306a074c93Sab196087 pgnp->gn_gotndx = M_GOT_MIXED;
19316a074c93Sab196087 mixgotcnt += gotents;
19326a074c93Sab196087 }
19336a074c93Sab196087 break;
19346a074c93Sab196087
19356a074c93Sab196087 case M_GOT_LARGE:
19366a074c93Sab196087 /*
19376a074c93Sab196087 * This one was previously identified as a large
19386a074c93Sab196087 * GOT. If this access is small, convert it to mixed.
19396a074c93Sab196087 */
19406a074c93Sab196087 if (rsp->rel_rtype == R_SPARC_GOT13) {
19416a074c93Sab196087 smlgotcnt += gotents;
19426a074c93Sab196087 mixgotcnt += gotents;
19436a074c93Sab196087 pgnp->gn_gotndx = M_GOT_MIXED;
19446a074c93Sab196087 sdp->sd_flags |= FLG_SY_SMGOT;
19456a074c93Sab196087 }
19466a074c93Sab196087 break;
19476a074c93Sab196087 }
19486a074c93Sab196087 return (1);
19496a074c93Sab196087 }
19506a074c93Sab196087
195157ef7aa9SRod Evans gn.gn_addend = raddend;
195257ef7aa9SRod Evans gn.gn_gotref = gref;
19535aefb655Srie
19545aefb655Srie if (rsp->rel_rtype == R_SPARC_GOT13) {
195557ef7aa9SRod Evans gn.gn_gotndx = M_GOT_SMALL;
19566a074c93Sab196087 smlgotcnt += gotents;
19575aefb655Srie sdp->sd_flags |= FLG_SY_SMGOT;
195857ef7aa9SRod Evans } else
195957ef7aa9SRod Evans gn.gn_gotndx = M_GOT_LARGE;
196057ef7aa9SRod Evans
196157ef7aa9SRod Evans ofl->ofl_gotcnt += gotents;
19625aefb655Srie
19635aefb655Srie if (gref == GOT_REF_TLSLD) {
196457ef7aa9SRod Evans if (ofl->ofl_tlsldgotndx == NULL) {
196557ef7aa9SRod Evans if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
196657ef7aa9SRod Evans return (S_ERROR);
196757ef7aa9SRod Evans (void) memcpy(gnp, &gn, sizeof (Gotndx));
19685aefb655Srie ofl->ofl_tlsldgotndx = gnp;
196957ef7aa9SRod Evans }
19705aefb655Srie return (1);
19715aefb655Srie }
19725aefb655Srie
197357ef7aa9SRod Evans idx = 0;
197457ef7aa9SRod Evans for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
197557ef7aa9SRod Evans if (gnp->gn_addend > raddend)
197657ef7aa9SRod Evans break;
19775aefb655Srie }
197857ef7aa9SRod Evans
197957ef7aa9SRod Evans /*
198057ef7aa9SRod Evans * GOT indexes are maintained on an Alist, where there is typically
198157ef7aa9SRod Evans * only one index. The usage of this list is to scan the list to find
198257ef7aa9SRod Evans * an index, and then apply that index immediately to a relocation.
198357ef7aa9SRod Evans * Thus there are no external references to these GOT index structures
198457ef7aa9SRod Evans * that can be compromised by the Alist being reallocated.
198557ef7aa9SRod Evans */
198657ef7aa9SRod Evans if (alist_insert(alpp, &gn, sizeof (Gotndx),
198757ef7aa9SRod Evans AL_CNT_SDP_GOT, idx) == NULL)
198857ef7aa9SRod Evans return (S_ERROR);
198957ef7aa9SRod Evans
19905aefb655Srie return (1);
19915aefb655Srie }
19925aefb655Srie
1993ba2be530Sab196087 static void
ld_assign_plt_ndx(Sym_desc * sdp,Ofl_desc * ofl)19945aefb655Srie ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
19955aefb655Srie {
19965aefb655Srie sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
19975aefb655Srie }
19985aefb655Srie
19995aefb655Srie
2000ba2be530Sab196087 static uintptr_t
ld_allocate_got(Ofl_desc * ofl)20015aefb655Srie ld_allocate_got(Ofl_desc * ofl)
20025aefb655Srie {
20036a074c93Sab196087 const Sword first_large_ndx = M_GOT_MAXSMALL / 2;
20045aefb655Srie Sym_desc *sdp;
20055aefb655Srie Addr addr;
20065aefb655Srie
20075aefb655Srie /*
20086a074c93Sab196087 * Sanity check -- is this going to fit at all? There are two
20096a074c93Sab196087 * limits to be concerned about:
20106a074c93Sab196087 * 1) There is a limit on the number of small pic GOT indices,
20116a074c93Sab196087 * given by M_GOT_MAXSMALL.
20126a074c93Sab196087 * 2) If there are more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
20136a074c93Sab196087 * small GOT indices, there will be items at negative
20146a074c93Sab196087 * offsets from _GLOBAL_OFFSET_TABLE_. Items that are
20156a074c93Sab196087 * accessed via large (PIC) code cannot reach these
20166a074c93Sab196087 * negative slots, so mixed mode items must be in the
20176a074c93Sab196087 * non-negative range. This implies a limit of
20186a074c93Sab196087 * (M_GOT_MAXSMALL/2 - M_GOT_XNumber) mixed mode indices.
20195aefb655Srie */
20206a074c93Sab196087 if (smlgotcnt > M_GOT_MAXSMALL) {
2021*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SMALLGOT),
20225aefb655Srie EC_WORD(smlgotcnt), M_GOT_MAXSMALL);
20235aefb655Srie return (S_ERROR);
20245aefb655Srie }
20256a074c93Sab196087 if (mixgotcnt > (first_large_ndx - M_GOT_XNumber)) {
2026*1007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_MIXEDGOT),
20276a074c93Sab196087 EC_WORD(mixgotcnt), first_large_ndx - M_GOT_XNumber);
20286a074c93Sab196087 return (S_ERROR);
20296a074c93Sab196087 }
20305aefb655Srie
20315aefb655Srie /*
20325aefb655Srie * Set starting offset to be either 0, or a negative index into
20335aefb655Srie * the GOT based on the number of small symbols we've got.
20345aefb655Srie */
20356a074c93Sab196087 neggotoffset = ((smlgotcnt >= first_large_ndx) ?
20366a074c93Sab196087 (first_large_ndx - smlgotcnt) : 0);
20375aefb655Srie
20385aefb655Srie /*
20396a074c93Sab196087 * Initialize the got offsets used by assign_got() to
20406a074c93Sab196087 * locate GOT items:
20416a074c93Sab196087 * small - Starting index of items referenced only
20426a074c93Sab196087 * by small offsets (-Kpic).
20436a074c93Sab196087 * mixed - Starting index of items referenced
20446a074c93Sab196087 * by both large (-KPIC) and small (-Kpic).
20456a074c93Sab196087 * large - Indexes referenced only by large (-KPIC)
20466a074c93Sab196087 *
20476a074c93Sab196087 * Small items can have negative indexes (i.e. lie below
20486a074c93Sab196087 * _GLOBAL_OFFSET_TABLE_). Mixed and large items must have
20496a074c93Sab196087 * non-negative offsets.
20505aefb655Srie */
20516a074c93Sab196087 small_index = (neggotoffset == 0) ? M_GOT_XNumber : neggotoffset;
20525aefb655Srie large_index = neggotoffset + smlgotcnt;
20536a074c93Sab196087 mixed_index = large_index - mixgotcnt;
20545aefb655Srie
20555aefb655Srie /*
20565aefb655Srie * Assign bias to GOT symbols.
20575aefb655Srie */
20585aefb655Srie addr = -neggotoffset * M_GOT_ENTSIZE;
2059635216b6SRod Evans if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), SYM_NOHASH,
2060635216b6SRod Evans NULL, ofl)) != NULL)
20615aefb655Srie sdp->sd_sym->st_value = addr;
2062635216b6SRod Evans if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH,
2063635216b6SRod Evans NULL, ofl)) != NULL)
20645aefb655Srie sdp->sd_sym->st_value = addr;
20655aefb655Srie
20665aefb655Srie if (ofl->ofl_tlsldgotndx) {
20675aefb655Srie ofl->ofl_tlsldgotndx->gn_gotndx = large_index;
20685aefb655Srie large_index += 2;
20695aefb655Srie }
20705aefb655Srie return (1);
20715aefb655Srie }
20725aefb655Srie
20735aefb655Srie /*
20745aefb655Srie * Initializes .got[0] with the _DYNAMIC symbol value.
20755aefb655Srie */
2076ba2be530Sab196087 static uintptr_t
ld_fillin_gotplt(Ofl_desc * ofl)20775aefb655Srie ld_fillin_gotplt(Ofl_desc *ofl)
20785aefb655Srie {
20795aefb655Srie if (ofl->ofl_osgot) {
20805aefb655Srie Sym_desc *sdp;
20815aefb655Srie
20825aefb655Srie if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
2083635216b6SRod Evans SYM_NOHASH, NULL, ofl)) != NULL) {
2084d326b23bSrie uchar_t *genptr;
2085d326b23bSrie
2086d326b23bSrie genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
20875aefb655Srie (-neggotoffset * M_GOT_ENTSIZE) +
20885aefb655Srie (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
20895aefb655Srie /* LINTED */
20905aefb655Srie *((Xword *)genptr) = sdp->sd_sym->st_value;
2091ba2be530Sab196087 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
2092ba2be530Sab196087 /* LINTED */
2093ba2be530Sab196087 *((Xword *)genptr) =
2094ba2be530Sab196087 /* LINTED */
2095ba2be530Sab196087 ld_bswap_Xword(*((Xword *)genptr));
20965aefb655Srie }
20975aefb655Srie }
20985aefb655Srie return (1);
20995aefb655Srie }
2100ba2be530Sab196087
2101ba2be530Sab196087
2102ba2be530Sab196087
2103ba2be530Sab196087 /*
2104ba2be530Sab196087 * Template for generating "void (*)(void)" function
2105ba2be530Sab196087 */
2106ba2be530Sab196087 static const uchar_t nullfunc_tmpl[] = {
2107ba2be530Sab196087 /* 0x00 */ 0x81, 0xc3, 0xe0, 0x08, /* retl */
2108ba2be530Sab196087 /* 0x04 */ 0x01, 0x00, 0x00, 0x00 /* nop */
2109ba2be530Sab196087 };
2110ba2be530Sab196087
2111ba2be530Sab196087
2112ba2be530Sab196087
2113ba2be530Sab196087 /*
2114ba2be530Sab196087 * Return the ld_targ definition for this target.
2115ba2be530Sab196087 */
2116ba2be530Sab196087 const Target *
ld_targ_init_sparc(void)2117ba2be530Sab196087 ld_targ_init_sparc(void)
2118ba2be530Sab196087 {
2119ba2be530Sab196087 static const Target _ld_targ = {
2120ba2be530Sab196087 { /* Target_mach */
2121ba2be530Sab196087 M_MACH, /* m_mach */
2122ba2be530Sab196087 M_MACHPLUS, /* m_machplus */
2123ba2be530Sab196087 M_FLAGSPLUS, /* m_flagsplus */
2124ba2be530Sab196087 M_CLASS, /* m_class */
2125ba2be530Sab196087 M_DATA, /* m_data */
2126ba2be530Sab196087
2127ba2be530Sab196087 M_SEGM_ALIGN, /* m_segm_align */
2128ba2be530Sab196087 M_SEGM_ORIGIN, /* m_segm_origin */
2129bb3b4f6cSRod Evans M_SEGM_AORIGIN, /* m_segm_aorigin */
2130ba2be530Sab196087 M_DATASEG_PERM, /* m_dataseg_perm */
213169112eddSAli Bahrami M_STACK_PERM, /* m_stack_perm */
2132ba2be530Sab196087 M_WORD_ALIGN, /* m_word_align */
2133ba2be530Sab196087 /* m_def_interp */
2134ba2be530Sab196087 #if defined(_ELF64)
2135ba2be530Sab196087 MSG_ORIG(MSG_PTH_RTLD_SPARCV9),
2136ba2be530Sab196087 #else
2137ba2be530Sab196087 MSG_ORIG(MSG_PTH_RTLD),
2138ba2be530Sab196087 #endif
2139ba2be530Sab196087
2140ba2be530Sab196087 /* Relocation type codes */
2141ba2be530Sab196087 M_R_ARRAYADDR, /* m_r_arrayaddr */
2142ba2be530Sab196087 M_R_COPY, /* m_r_copy */
2143ba2be530Sab196087 M_R_GLOB_DAT, /* m_r_glob_dat */
2144ba2be530Sab196087 M_R_JMP_SLOT, /* m_r_jmp_slot */
2145ba2be530Sab196087 M_R_NUM, /* m_r_num */
2146ba2be530Sab196087 M_R_NONE, /* m_r_none */
2147ba2be530Sab196087 M_R_RELATIVE, /* m_r_relative */
2148ba2be530Sab196087 M_R_REGISTER, /* m_r_register */
2149ba2be530Sab196087
2150ba2be530Sab196087 /* Relocation related constants */
2151ba2be530Sab196087 M_REL_DT_COUNT, /* m_rel_dt_count */
2152ba2be530Sab196087 M_REL_DT_ENT, /* m_rel_dt_ent */
2153ba2be530Sab196087 M_REL_DT_SIZE, /* m_rel_dt_size */
2154ba2be530Sab196087 M_REL_DT_TYPE, /* m_rel_dt_type */
2155ba2be530Sab196087 M_REL_SHT_TYPE, /* m_rel_sht_type */
2156ba2be530Sab196087
2157ba2be530Sab196087 /* GOT related constants */
2158ba2be530Sab196087 M_GOT_ENTSIZE, /* m_got_entsize */
2159ba2be530Sab196087 M_GOT_XNumber, /* m_got_xnumber */
2160ba2be530Sab196087
2161ba2be530Sab196087 /* PLT related constants */
2162ba2be530Sab196087 M_PLT_ALIGN, /* m_plt_align */
2163ba2be530Sab196087 M_PLT_ENTSIZE, /* m_plt_entsize */
2164ba2be530Sab196087 M_PLT_RESERVSZ, /* m_plt_reservsz */
2165ba2be530Sab196087 M_PLT_SHF_FLAGS, /* m_plt_shf_flags */
2166ba2be530Sab196087
21677e16fca0SAli Bahrami /* Section type of .eh_frame/.eh_frame_hdr sections */
21687e16fca0SAli Bahrami SHT_PROGBITS, /* m_sht_unwind */
21697e16fca0SAli Bahrami
2170ba2be530Sab196087 M_DT_REGISTER, /* m_dt_register */
2171ba2be530Sab196087 },
2172ba2be530Sab196087 { /* Target_machid */
2173ba2be530Sab196087 M_ID_ARRAY, /* id_array */
2174ba2be530Sab196087 M_ID_BSS, /* id_bss */
2175ba2be530Sab196087 M_ID_CAP, /* id_cap */
217608278a5eSRod Evans M_ID_CAPINFO, /* id_capinfo */
217708278a5eSRod Evans M_ID_CAPCHAIN, /* id_capchain */
2178ba2be530Sab196087 M_ID_DATA, /* id_data */
2179ba2be530Sab196087 M_ID_DYNAMIC, /* id_dynamic */
2180ba2be530Sab196087 M_ID_DYNSORT, /* id_dynsort */
2181ba2be530Sab196087 M_ID_DYNSTR, /* id_dynstr */
2182ba2be530Sab196087 M_ID_DYNSYM, /* id_dynsym */
2183ba2be530Sab196087 M_ID_DYNSYM_NDX, /* id_dynsym_ndx */
2184ba2be530Sab196087 M_ID_GOT, /* id_got */
2185ba2be530Sab196087 M_ID_GOTDATA, /* id_gotdata */
2186ba2be530Sab196087 M_ID_HASH, /* id_hash */
2187ba2be530Sab196087 M_ID_INTERP, /* id_interp */
2188ba2be530Sab196087 M_ID_UNKNOWN, /* id_lbss (unused) */
2189ba2be530Sab196087 M_ID_LDYNSYM, /* id_ldynsym */
2190ba2be530Sab196087 M_ID_NOTE, /* id_note */
2191ba2be530Sab196087 M_ID_NULL, /* id_null */
2192ba2be530Sab196087 M_ID_PLT, /* id_plt */
2193ba2be530Sab196087 M_ID_REL, /* id_rel */
2194ba2be530Sab196087 M_ID_STRTAB, /* id_strtab */
2195ba2be530Sab196087 M_ID_SYMINFO, /* id_syminfo */
2196ba2be530Sab196087 M_ID_SYMTAB, /* id_symtab */
2197ba2be530Sab196087 M_ID_SYMTAB_NDX, /* id_symtab_ndx */
2198ba2be530Sab196087 M_ID_TEXT, /* id_text */
2199ba2be530Sab196087 M_ID_TLS, /* id_tls */
2200ba2be530Sab196087 M_ID_TLSBSS, /* id_tlsbss */
2201ba2be530Sab196087 M_ID_UNKNOWN, /* id_unknown */
22027e16fca0SAli Bahrami M_ID_UNWIND, /* id_unwind */
22037e16fca0SAli Bahrami M_ID_UNWINDHDR, /* id_unwindhdr */
2204ba2be530Sab196087 M_ID_USER, /* id_user */
2205ba2be530Sab196087 M_ID_VERSION, /* id_version */
2206ba2be530Sab196087 },
2207ba2be530Sab196087 { /* Target_nullfunc */
2208ba2be530Sab196087 nullfunc_tmpl, /* nf_template */
2209ba2be530Sab196087 sizeof (nullfunc_tmpl), /* nf_size */
2210ba2be530Sab196087 },
22113c573fccSAli Bahrami { /* Target_fillfunc */
22123c573fccSAli Bahrami /*
22133c573fccSAli Bahrami * On sparc, special filling of executable sections
22143c573fccSAli Bahrami * is undesirable, and the default 0 fill supplied
22153c573fccSAli Bahrami * by libelf is preferred:
22163c573fccSAli Bahrami *
22173c573fccSAli Bahrami * - 0 fill is interpreted as UNIMP instructions,
22183c573fccSAli Bahrami * which cause an illegal_instruction_trap. These
22193c573fccSAli Bahrami * serve as a sentinel against poorly written
22203c573fccSAli Bahrami * code. The sparc architecture manual discusses
22213c573fccSAli Bahrami * this as providing a measure of runtime safety.
22223c573fccSAli Bahrami *
22233c573fccSAli Bahrami * - The one place where a hole should conceivably
22243c573fccSAli Bahrami * be filled with NOP instructions is in the
22253c573fccSAli Bahrami * .init/.fini sections. However, the sparc
22263c573fccSAli Bahrami * assembler sizes the sections it generates
22273c573fccSAli Bahrami * to a multiple of the section alignment, and as
22283c573fccSAli Bahrami * such, takes the filling task out of our hands.
22293c573fccSAli Bahrami * Furthermore, the sparc assembler uses 0-fill
22303c573fccSAli Bahrami * for this, forcing the authors of sparc
22313c573fccSAli Bahrami * assembler for .init/.fini sections to be aware
22323c573fccSAli Bahrami * of this case and explicitly supply NOP fill.
22333c573fccSAli Bahrami * Hence, there is no role for the link-editor.
22343c573fccSAli Bahrami */
22353c573fccSAli Bahrami NULL /* ff_execfill */
22363c573fccSAli Bahrami },
2237ba2be530Sab196087 { /* Target_machrel */
2238ba2be530Sab196087 reloc_table,
2239ba2be530Sab196087
2240ba2be530Sab196087 ld_init_rel, /* mr_init_rel */
2241ba2be530Sab196087 ld_mach_eflags, /* mr_mach_eflags */
2242ba2be530Sab196087 ld_mach_make_dynamic, /* mr_mach_make_dynamic */
2243ba2be530Sab196087 ld_mach_update_odynamic, /* mr_mach_update_odynamic */
2244ba2be530Sab196087 ld_calc_plt_addr, /* mr_calc_plt_addr */
2245ba2be530Sab196087 ld_perform_outreloc, /* mr_perform_outreloc */
2246ba2be530Sab196087 ld_do_activerelocs, /* mr_do_activerelocs */
2247ba2be530Sab196087 ld_add_outrel, /* mr_add_outrel */
2248ba2be530Sab196087 ld_reloc_register, /* mr_reloc_register */
2249ba2be530Sab196087 ld_reloc_local, /* mr_reloc_local */
2250ba2be530Sab196087 ld_reloc_GOTOP, /* mr_reloc_GOTOP */
2251ba2be530Sab196087 ld_reloc_TLS, /* mr_reloc_TLS */
2252ba2be530Sab196087 ld_assign_got, /* mr_assign_got */
225357ef7aa9SRod Evans ld_find_got_ndx, /* mr_find_got_ndx */
2254ba2be530Sab196087 ld_calc_got_offset, /* mr_calc_got_offset */
2255ba2be530Sab196087 ld_assign_got_ndx, /* mr_assign_got_ndx */
2256ba2be530Sab196087 ld_assign_plt_ndx, /* mr_assign_plt_ndx */
2257ba2be530Sab196087 ld_allocate_got, /* mr_allocate_got */
2258ba2be530Sab196087 ld_fillin_gotplt, /* mr_fillin_gotplt */
2259ba2be530Sab196087 },
2260ba2be530Sab196087 { /* Target_machsym */
2261ba2be530Sab196087 ld_reg_check_sparc, /* ms_reg_check */
2262ba2be530Sab196087 ld_mach_sym_typecheck_sparc, /* ms_mach_sym_typecheck */
2263ba2be530Sab196087 ld_is_regsym_sparc, /* ms_is_regsym */
2264ba2be530Sab196087 ld_reg_find_sparc, /* ms_reg_find */
2265ba2be530Sab196087 ld_reg_enter_sparc /* ms_reg_enter */
2266ba2be530Sab196087 }
2267ba2be530Sab196087 };
2268ba2be530Sab196087
2269ba2be530Sab196087 return (&_ld_targ);
2270ba2be530Sab196087 }
2271