dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / sgs / libld / common / machrel.sparc.c
blob44df9ee2804e9f79b950feaad0984a57e64bd522
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 1988 AT&T
24 * All Rights Reserved
26 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
29 /* Get the sparc version of the relocation engine */
30 #define DO_RELOC_LIBLD_SPARC
32 #include <string.h>
33 #include <stdio.h>
34 #include <sys/elf_SPARC.h>
35 #include <debug.h>
36 #include <krtld/reloc.h>
37 #include <sparc/machdep_sparc.h>
38 #include "msg.h"
39 #include "_libld.h"
40 #include "machsym.sparc.h"
43 * Local Variable Definitions
45 static Sword neggotoffset = 0; /* off. of GOT table from GOT symbol */
46 static Sword smlgotcnt = M_GOT_XNumber; /* no. of small GOT symbols */
47 static Sword mixgotcnt = 0; /* # syms with both large/small GOT */
50 * Search the GOT index list for a GOT entry with a matching reference and the
51 * proper addend.
53 static Gotndx *
54 ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc)
56 Aliste idx;
57 Gotndx *gnp;
59 assert(rdesc != 0);
61 if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx)
62 return (ofl->ofl_tlsldgotndx);
64 for (ALIST_TRAVERSE(alp, idx, gnp)) {
65 if ((rdesc->rel_raddend == gnp->gn_addend) &&
66 (gref == gnp->gn_gotref))
67 return (gnp);
69 return (NULL);
72 static Xword
73 ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl)
75 Os_desc *osp = ofl->ofl_osgot;
76 Sym_desc *sdp = rdesc->rel_sym;
77 Xword gotndx;
78 Gotref gref;
79 Gotndx *gnp;
81 if (rdesc->rel_flags & FLG_REL_DTLS)
82 gref = GOT_REF_TLSGD;
83 else if (rdesc->rel_flags & FLG_REL_MTLS)
84 gref = GOT_REF_TLSLD;
85 else if (rdesc->rel_flags & FLG_REL_STLS)
86 gref = GOT_REF_TLSIE;
87 else
88 gref = GOT_REF_GENERIC;
90 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc);
91 assert(gnp);
93 gotndx = (Xword)gnp->gn_gotndx;
95 if ((rdesc->rel_flags & FLG_REL_DTLS) &&
96 (rdesc->rel_rtype == M_R_DTPOFF))
97 gotndx++;
99 return ((Xword)((osp->os_shdr->sh_addr) + (gotndx * M_GOT_ENTSIZE) +
100 (-neggotoffset * M_GOT_ENTSIZE)));
103 static Word
104 ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc)
106 Rela *rela = (Rela *)reloc;
108 /* LINTED */
109 reld->rel_rtype = (Word)ELF_R_TYPE(rela->r_info, M_MACH);
110 reld->rel_roffset = rela->r_offset;
111 reld->rel_raddend = rela->r_addend;
112 *typedata = (Word)ELF_R_TYPE_DATA(rela->r_info);
114 reld->rel_flags |= FLG_REL_RELA;
116 return ((Word)ELF_R_SYM(rela->r_info));
119 static void
120 ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl)
122 Word eflags = ofl->ofl_dehdr->e_flags;
123 Word memopt1, memopt2;
124 static int firstpass;
127 * If a *PLUS relocatable is included, the output object is type *PLUS.
129 if ((ehdr->e_machine == EM_SPARC32PLUS) &&
130 (ehdr->e_flags & EF_SPARC_32PLUS))
131 ofl->ofl_dehdr->e_machine = EM_SPARC32PLUS;
134 * On the first pass, we don't yet have a memory model to compare
135 * against, therefore the initial file becomes our baseline. Subsequent
136 * passes will do the comparison described below.
138 if (firstpass == 0) {
139 ofl->ofl_dehdr->e_flags |= ehdr->e_flags;
140 firstpass++;
141 return;
145 * Determine which memory model to mark the binary with. The options
146 * are (most restrictive to least):
148 * EF_SPARCV9_TSO 0x0 Total Store Order
149 * EF_SPARCV9_PSO 0x1 Partial Store Order
150 * EF_SPARCV9_RMO 0x2 Relaxed Memory Order
152 * Mark the binary with the most restrictive option encountered from a
153 * relocatable object included in the link.
155 eflags |= (ehdr->e_flags & ~EF_SPARCV9_MM);
156 memopt1 = eflags & EF_SPARCV9_MM;
157 memopt2 = ehdr->e_flags & EF_SPARCV9_MM;
158 eflags &= ~EF_SPARCV9_MM;
160 if ((memopt1 == EF_SPARCV9_TSO) || (memopt2 == EF_SPARCV9_TSO))
161 /* EMPTY */
163 else if ((memopt1 == EF_SPARCV9_PSO) || (memopt2 == EF_SPARCV9_PSO))
164 eflags |= EF_SPARCV9_PSO;
165 else
166 eflags |= EF_SPARCV9_RMO;
168 ofl->ofl_dehdr->e_flags = eflags;
171 static void
172 ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt)
174 if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
176 * Create this entry if we are going to create a PLT table.
178 if (ofl->ofl_pltcnt)
179 (*cnt)++; /* DT_PLTGOT */
183 static void
184 ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn)
186 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) {
187 (*dyn)->d_tag = DT_PLTGOT;
188 if (ofl->ofl_osplt)
189 (*dyn)->d_un.d_ptr = ofl->ofl_osplt->os_shdr->sh_addr;
190 else
191 (*dyn)->d_un.d_ptr = 0;
192 (*dyn)++;
196 #if defined(_ELF64)
198 static Xword
199 ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
201 Xword value, pltndx, farpltndx;
203 pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1;
205 if ((pltndx) < M64_PLT_NEARPLTS) {
206 value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
207 (pltndx * M_PLT_ENTSIZE);
208 return (value);
211 farpltndx = pltndx - M64_PLT_NEARPLTS;
214 * pltoffset of a far plt is calculated by:
216 * <size of near plt table> +
217 * <size of preceding far plt blocks> +
218 * <blockndx * sizeof (far plt entsize)>
220 value =
221 /* size of near plt table */
222 (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) +
223 /* size of preceding far plt blocks */
224 ((farpltndx / M64_PLT_FBLKCNTS) *
225 ((M64_PLT_FENTSIZE + sizeof (Addr)) *
226 M64_PLT_FBLKCNTS)) +
227 /* pltblockendx * fentsize */
228 ((farpltndx % M64_PLT_FBLKCNTS) * M64_PLT_FENTSIZE);
230 value += (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
231 return (value);
235 * Instructions required for Far PLT's
237 static uchar_t farplt_instrs[24] = {
238 0x8a, 0x10, 0x00, 0x0f, /* mov %o7, %g5 */
239 0x40, 0x00, 0x00, 0x02, /* call . + 0x8 */
240 0x01, 0x00, 0x00, 0x00, /* nop */
241 0xc2, 0x5b, 0xe0, 0x00, /* ldx [%o7 + 0], %g1 */
242 0x83, 0xc3, 0xc0, 0x01, /* jmpl %o7 + %g1, %g1 */
243 0x9e, 0x10, 0x00, 0x05 /* mov %g5, %o7 */
247 * Far PLT'S:
249 * Far PLT's are established in blocks of '160' at a time. These
250 * PLT's consist of 6 instructions (24 bytes) and 1 pointer (8 bytes).
251 * The instructions are collected together in blocks of 160 entries
252 * followed by 160 pointers. The last group of entries and pointers
253 * may contain less then 160 items. No padding is required.
255 * .PLT32768:
256 * mov %o7, %g5
257 * call . + 8
258 * nop
259 * ldx [%o7 + .PLTP32768 - (.PLT32768 + 4)], %g1
260 * jmpl %o7 + %g1, %g1
261 * mov %g5, %o7
262 * ................................
263 * .PLT32927:
264 * mov %o7, %g5
265 * call . + 8
266 * nop
267 * ldx [%o7 + .PLTP32927 - (.PLT32927 + 4)], %g1
268 * jmpl %o7 + %g1, %g1
269 * mov %g5, %o7
270 * .PLTP32768:
271 * .xword .PLT0-(.PLT32768+4)
272 * ................................
273 * .PLTP32927:
274 * .xword .PLT0-(.PLT32927+4)
277 static void
278 plt_far_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
280 uint_t blockndx; /* # of far PLT blocks */
281 uint_t farblkcnt; /* Index to far PLT block */
282 Xword farpltndx; /* index of Far Plt */
283 Xword farpltblkndx; /* index of PLT in BLOCK */
284 uint32_t *pltent; /* ptr to plt instr. sequence */
285 uint64_t *pltentptr; /* ptr to plt addr ptr */
286 Sxword pltblockoff; /* offset to Far plt block */
287 Sxword pltoff; /* offset to PLT instr. sequence */
288 Sxword pltptroff; /* offset to PLT addr ptr */
289 uchar_t *pltbuf; /* ptr to PLT's in file */
292 farblkcnt = ((ofl->ofl_pltcnt - 1 +
293 M_PLT_XNumber - M64_PLT_NEARPLTS) / M64_PLT_FBLKCNTS);
296 * Determine the 'Far' PLT index.
298 farpltndx = pltndx - 1 + M_PLT_XNumber - M64_PLT_NEARPLTS;
299 farpltblkndx = farpltndx % M64_PLT_FBLKCNTS;
302 * Determine what FPLT block this plt falls into.
304 blockndx = (uint_t)(farpltndx / M64_PLT_FBLKCNTS);
307 * Calculate the starting offset of the Far PLT block
308 * that this PLT is a member of.
310 pltblockoff = (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) +
311 (blockndx * M64_PLT_FBLOCKSZ);
313 pltoff = pltblockoff +
314 (farpltblkndx * M64_PLT_FENTSIZE);
316 pltptroff = pltblockoff;
319 if (farblkcnt > blockndx) {
321 * If this is a full block - the 'pltptroffs' start
322 * after 160 fplts.
324 pltptroff += (M64_PLT_FBLKCNTS * M64_PLT_FENTSIZE) +
325 (farpltblkndx * M64_PLT_PSIZE);
326 } else {
327 Xword lastblkpltndx;
329 * If this is the last block - the the pltptr's start
330 * after the last FPLT instruction sequence.
332 lastblkpltndx = (ofl->ofl_pltcnt - 1 + M_PLT_XNumber -
333 M64_PLT_NEARPLTS) % M64_PLT_FBLKCNTS;
334 pltptroff += ((lastblkpltndx + 1) * M64_PLT_FENTSIZE) +
335 (farpltblkndx * M64_PLT_PSIZE);
337 pltbuf = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf;
340 * For far-plts, the Raddend and Roffset fields are defined
341 * to be:
343 * roffset: address of .PLTP#
344 * raddend: -(.PLT#+4)
346 *roffset = pltptroff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
347 *raddend = -(pltoff + 4 + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr));
349 /* LINTED */
350 pltent = (uint32_t *)(pltbuf + pltoff);
351 /* LINTED */
352 pltentptr = (uint64_t *)(pltbuf + pltptroff);
353 (void) memcpy(pltent, farplt_instrs, sizeof (farplt_instrs));
356 * update
357 * ldx [%o7 + 0], %g1
358 * to
359 * ldx [%o7 + .PLTP# - (.PLT# + 4)], %g1
361 /* LINTED */
362 pltent[3] |= (uint32_t)(pltptroff - (pltoff + 4));
365 * Store:
366 * .PLTP#
367 * .xword .PLT0 - .PLT# + 4
369 *pltentptr = -(pltoff + 4);
373 * Build a single V9 P.L.T. entry - code is:
375 * For Target Addresses +/- 4GB of the entry
376 * -----------------------------------------
377 * sethi (. - .PLT0), %g1
378 * ba,a %xcc, .PLT1
379 * nop
380 * nop
381 * nop
382 * nop
383 * nop
384 * nop
386 * For Target Addresses +/- 2GB of the entry
387 * -----------------------------------------
389 * .PLT0 is the address of the first entry in the P.L.T.
390 * This one is filled in by the run-time link editor. We just
391 * have to leave space for it.
393 static void
394 plt_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
396 uchar_t *pltent; /* PLT entry being created. */
397 Sxword pltoff; /* Offset of this entry from PLT top */
398 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
401 * The second part of the V9 ABI (sec. 5.2.4)
402 * applies to plt entries greater than 0x8000 (32,768).
403 * This is handled in 'plt_far_entry()'
405 if ((pltndx - 1 + M_PLT_XNumber) >= M64_PLT_NEARPLTS) {
406 plt_far_entry(ofl, pltndx, roffset, raddend);
407 return;
410 pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE;
411 pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf + pltoff;
413 *roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
414 *raddend = 0;
417 * PLT[0]: sethi %hi(. - .L0), %g1
419 /* LINTED */
420 *(Word *)pltent = M_SETHIG1 | pltoff;
421 if (bswap)
422 /* LINTED */
423 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
426 * PLT[1]: ba,a %xcc, .PLT1 (.PLT1 accessed as a
427 * PC-relative index of longwords).
429 pltent += M_PLT_INSSIZE;
430 pltoff += M_PLT_INSSIZE;
431 pltoff = -pltoff;
432 /* LINTED */
433 *(Word *)pltent = M_BA_A_XCC |
434 (((pltoff + M_PLT_ENTSIZE) >> 2) & S_MASK(19));
435 if (bswap)
436 /* LINTED */
437 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
440 * PLT[2]: sethi 0, %g0 (NOP for delay slot of eventual CTI).
442 pltent += M_PLT_INSSIZE;
443 /* LINTED */
444 *(Word *)pltent = M_NOP;
445 if (bswap)
446 /* LINTED */
447 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
450 * PLT[3]: sethi 0, %g0 (NOP for PLT padding).
452 pltent += M_PLT_INSSIZE;
453 /* LINTED */
454 *(Word *)pltent = M_NOP;
455 if (bswap)
456 /* LINTED */
457 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
460 * PLT[4]: sethi 0, %g0 (NOP for PLT padding).
462 pltent += M_PLT_INSSIZE;
463 /* LINTED */
464 *(Word *)pltent = M_NOP;
465 if (bswap)
466 /* LINTED */
467 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
470 * PLT[5]: sethi 0, %g0 (NOP for PLT padding).
472 pltent += M_PLT_INSSIZE;
473 /* LINTED */
474 *(Word *)pltent = M_NOP;
475 if (bswap)
476 /* LINTED */
477 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
480 * PLT[6]: sethi 0, %g0 (NOP for PLT padding).
482 pltent += M_PLT_INSSIZE;
483 /* LINTED */
484 *(Word *)pltent = M_NOP;
485 if (bswap)
486 /* LINTED */
487 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
490 * PLT[7]: sethi 0, %g0 (NOP for PLT padding).
492 pltent += M_PLT_INSSIZE;
493 /* LINTED */
494 *(Word *)pltent = M_NOP;
495 if (bswap)
496 /* LINTED */
497 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
501 #else /* Elf 32 */
503 static Xword
504 ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl)
506 Xword value, pltndx;
508 pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1;
509 value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) +
510 (pltndx * M_PLT_ENTSIZE);
511 return (value);
516 * Build a single P.L.T. entry - code is:
518 * sethi (. - .L0), %g1
519 * ba,a .L0
520 * sethi 0, %g0 (nop)
522 * .L0 is the address of the first entry in the P.L.T.
523 * This one is filled in by the run-time link editor. We just
524 * have to leave space for it.
526 static void
527 plt_entry(Ofl_desc * ofl, Xword pltndx, Xword *roffset, Sxword *raddend)
529 Byte *pltent; /* PLT entry being created. */
530 Sxword pltoff; /* Offset of this entry from PLT top */
531 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0;
533 pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE;
534 pltent = (Byte *)ofl->ofl_osplt->os_outdata->d_buf + pltoff;
536 *roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr);
537 *raddend = 0;
540 * PLT[0]: sethi %hi(. - .L0), %g1
542 /* LINTED */
543 *(Word *)pltent = M_SETHIG1 | pltoff;
544 if (bswap)
545 /* LINTED */
546 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
549 * PLT[1]: ba,a .L0 (.L0 accessed as a PC-relative index of longwords)
551 pltent += M_PLT_INSSIZE;
552 pltoff += M_PLT_INSSIZE;
553 pltoff = -pltoff;
554 /* LINTED */
555 *(Word *)pltent = M_BA_A | ((pltoff >> 2) & S_MASK(22));
556 if (bswap)
557 /* LINTED */
558 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
561 * PLT[2]: sethi 0, %g0 (NOP for delay slot of eventual CTI).
563 pltent += M_PLT_INSSIZE;
564 /* LINTED */
565 *(Word *)pltent = M_SETHIG0;
566 if (bswap)
567 /* LINTED */
568 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
571 * PLT[3]: sethi 0, %g0 (NOP for PLT padding).
573 pltent += M_PLT_INSSIZE;
574 /* LINTED */
575 *(Word *)pltent = M_SETHIG0;
576 if (bswap)
577 /* LINTED */
578 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent);
581 #endif /* _ELF64 */
583 static uintptr_t
584 ld_perform_outreloc(Rel_desc *orsp, Ofl_desc *ofl, Boolean *remain_seen)
586 Os_desc *relosp, *osp = NULL;
587 Xword ndx, roffset, value;
588 Sxword raddend;
589 const Rel_entry *rep;
590 Rela rea;
591 char *relbits;
592 Sym_desc *sdp, *psym = NULL;
593 int sectmoved = 0;
594 Word dtflags1 = ofl->ofl_dtflags_1;
595 ofl_flag_t flags = ofl->ofl_flags;
597 raddend = orsp->rel_raddend;
598 sdp = orsp->rel_sym;
601 * Special case, a regsiter symbol associated with symbol
602 * index 0 is initialized (i.e. relocated) to a constant
603 * in the r_addend field rather than to a symbol value.
605 if ((orsp->rel_rtype == M_R_REGISTER) && !sdp) {
606 relosp = ofl->ofl_osrel;
607 relbits = (char *)relosp->os_outdata->d_buf;
609 rea.r_info = ELF_R_INFO(0,
610 ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp),
611 orsp->rel_rtype));
612 rea.r_offset = orsp->rel_roffset;
613 rea.r_addend = raddend;
614 DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea,
615 relosp->os_name, ld_reloc_sym_name(orsp)));
617 assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
618 (void) memcpy((relbits + relosp->os_szoutrels),
619 (char *)&rea, sizeof (Rela));
620 relosp->os_szoutrels += (Xword)sizeof (Rela);
622 return (1);
626 * If the section this relocation is against has been discarded
627 * (-zignore), then also discard (skip) the relocation itself.
629 if (orsp->rel_isdesc && ((orsp->rel_flags &
630 (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) &&
631 (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) {
632 DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp));
633 return (1);
637 * If this is a relocation against a move table, or expanded move
638 * table, adjust the relocation entries.
640 if (RELAUX_GET_MOVE(orsp))
641 ld_adj_movereloc(ofl, orsp);
644 * If this is a relocation against a section then we need to adjust the
645 * raddend field to compensate for the new position of the input section
646 * within the new output section.
648 if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
649 if (ofl->ofl_parsyms &&
650 (sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
651 (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) {
653 * If the symbol is moved, adjust the value
655 DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym));
656 sectmoved = 1;
657 if (ofl->ofl_flags & FLG_OF_RELOBJ)
658 raddend = psym->sd_sym->st_value;
659 else
660 raddend = psym->sd_sym->st_value -
661 psym->sd_isc->is_osdesc->os_shdr->sh_addr;
662 /* LINTED */
663 raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata);
664 if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
665 raddend +=
666 psym->sd_isc->is_osdesc->os_shdr->sh_addr;
667 } else {
668 /* LINTED */
669 raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata);
670 if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
671 raddend +=
672 sdp->sd_isc->is_osdesc->os_shdr->sh_addr;
676 value = sdp->sd_sym->st_value;
678 if (orsp->rel_flags & FLG_REL_GOT) {
679 osp = ofl->ofl_osgot;
680 roffset = ld_calc_got_offset(orsp, ofl);
682 } else if (orsp->rel_flags & FLG_REL_PLT) {
683 osp = ofl->ofl_osplt;
684 plt_entry(ofl, sdp->sd_aux->sa_PLTndx, &roffset, &raddend);
685 } else if (orsp->rel_flags & FLG_REL_BSS) {
687 * This must be a R_SPARC_COPY. For these set the roffset to
688 * point to the new symbols location.
690 osp = ofl->ofl_isbss->is_osdesc;
691 roffset = (Xword)value;
694 * The raddend doesn't mean anything in an R_SPARC_COPY
695 * relocation. Null it out because it can confuse people.
697 raddend = 0;
698 } else if (orsp->rel_flags & FLG_REL_REG) {
700 * The offsets of relocations against register symbols
701 * identifiy the register directly - so the offset
702 * does not need to be adjusted.
704 roffset = orsp->rel_roffset;
705 } else {
706 osp = RELAUX_GET_OSDESC(orsp);
709 * Calculate virtual offset of reference point; equals offset
710 * into section + vaddr of section for loadable sections, or
711 * offset plus section displacement for nonloadable sections.
713 roffset = orsp->rel_roffset +
714 (Off)_elf_getxoff(orsp->rel_isdesc->is_indata);
715 if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
716 roffset += orsp->rel_isdesc->is_osdesc->
717 os_shdr->sh_addr;
720 if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0))
721 relosp = ofl->ofl_osrel;
724 * Verify that the output relocations offset meets the
725 * alignment requirements of the relocation being processed.
727 rep = &reloc_table[orsp->rel_rtype];
728 if (((flags & FLG_OF_RELOBJ) || !(dtflags1 & DF_1_NORELOC)) &&
729 !(rep->re_flags & FLG_RE_UNALIGN)) {
730 if (((rep->re_fsize == 2) && (roffset & 0x1)) ||
731 ((rep->re_fsize == 4) && (roffset & 0x3)) ||
732 ((rep->re_fsize == 8) && (roffset & 0x7))) {
733 Conv_inv_buf_t inv_buf;
735 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NONALIGN),
736 conv_reloc_SPARC_type(orsp->rel_rtype, 0, &inv_buf),
737 orsp->rel_isdesc->is_file->ifl_name,
738 ld_reloc_sym_name(orsp), EC_XWORD(roffset));
739 return (S_ERROR);
744 * Assign the symbols index for the output relocation. If the
745 * relocation refers to a SECTION symbol then it's index is based upon
746 * the output sections symbols index. Otherwise the index can be
747 * derived from the symbols index itself.
749 if (orsp->rel_rtype == R_SPARC_RELATIVE)
750 ndx = STN_UNDEF;
751 else if ((orsp->rel_flags & FLG_REL_SCNNDX) ||
752 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
753 if (sectmoved == 0) {
755 * Check for a null input section. This can
756 * occur if this relocation references a symbol
757 * generated by sym_add_sym().
759 if (sdp->sd_isc && sdp->sd_isc->is_osdesc)
760 ndx = sdp->sd_isc->is_osdesc->os_identndx;
761 else
762 ndx = sdp->sd_shndx;
763 } else
764 ndx = ofl->ofl_parexpnndx;
765 } else
766 ndx = sdp->sd_symndx;
769 * Add the symbols 'value' to the addend field.
771 if (orsp->rel_flags & FLG_REL_ADVAL)
772 raddend += value;
775 * The addend field for R_SPARC_TLS_DTPMOD32 and R_SPARC_TLS_DTPMOD64
776 * mean nothing. The addend is propagated in the corresponding
777 * R_SPARC_TLS_DTPOFF* relocations.
779 if (orsp->rel_rtype == M_R_DTPMOD)
780 raddend = 0;
782 relbits = (char *)relosp->os_outdata->d_buf;
784 rea.r_info = ELF_R_INFO(ndx,
785 ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp), orsp->rel_rtype));
786 rea.r_offset = roffset;
787 rea.r_addend = raddend;
788 DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name,
789 ld_reloc_sym_name(orsp)));
792 * Assert we haven't walked off the end of our relocation table.
794 assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size);
796 (void) memcpy((relbits + relosp->os_szoutrels),
797 (char *)&rea, sizeof (Rela));
798 relosp->os_szoutrels += (Xword)sizeof (Rela);
801 * Determine if this relocation is against a non-writable, allocatable
802 * section. If so we may need to provide a text relocation diagnostic.
804 ld_reloc_remain_entry(orsp, osp, ofl, remain_seen);
805 return (1);
810 * Sparc Instructions for TLS processing
812 #if defined(_ELF64)
813 #define TLS_GD_IE_LD 0xd0580000 /* ldx [%g0 + %g0], %o0 */
814 #else
815 #define TLS_GD_IE_LD 0xd0000000 /* ld [%g0 + %g0], %o0 */
816 #endif
817 #define TLS_GD_IE_ADD 0x9001c008 /* add %g7, %o0, %o0 */
819 #define TLS_GD_LE_XOR 0x80182000 /* xor %g0, 0, %g0 */
820 #define TLS_IE_LE_OR 0x80100000 /* or %g0, %o0, %o1 */
821 /* synthetic: mov %g0, %g0 */
823 #define TLS_LD_LE_CLRO0 0x90100000 /* clr %o0 */
825 #define FM3_REG_MSK_RD (0x1f << 25) /* Formate (3) rd register mask */
826 /* bits 25->29 */
827 #define FM3_REG_MSK_RS1 (0x1f << 14) /* Formate (3) rs1 register mask */
828 /* bits 14->18 */
829 #define FM3_REG_MSK_RS2 0x1f /* Formate (3) rs2 register mask */
830 /* bits 0->4 */
832 #define REG_G7 7 /* %g7 register */
834 static Fixupret
835 tls_fixups(Ofl_desc *ofl, Rel_desc *arsp)
837 Sym_desc *sdp = arsp->rel_sym;
838 Word rtype = arsp->rel_rtype;
839 Word *offset, w;
840 int bswap = OFL_SWAP_RELOC_DATA(ofl, arsp);
843 offset = (Word *)((uintptr_t)arsp->rel_roffset +
844 (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) +
845 (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
847 if (sdp->sd_ref == REF_DYN_NEED) {
849 * IE reference model
851 switch (rtype) {
852 case R_SPARC_TLS_GD_HI22:
853 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
854 R_SPARC_TLS_IE_HI22, arsp,
855 ld_reloc_sym_name));
856 arsp->rel_rtype = R_SPARC_TLS_IE_HI22;
857 return (FIX_RELOC);
859 case R_SPARC_TLS_GD_LO10:
860 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
861 R_SPARC_TLS_IE_LO10, arsp,
862 ld_reloc_sym_name));
863 arsp->rel_rtype = R_SPARC_TLS_IE_LO10;
864 return (FIX_RELOC);
866 case R_SPARC_TLS_GD_ADD:
867 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
868 R_SPARC_NONE, arsp, ld_reloc_sym_name));
869 w = bswap ? ld_bswap_Word(*offset) : *offset;
870 w = (TLS_GD_IE_LD |
871 (w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RS2)));
872 *offset = bswap ? ld_bswap_Word(w) : w;
873 return (FIX_DONE);
875 case R_SPARC_TLS_GD_CALL:
876 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
877 R_SPARC_NONE, arsp, ld_reloc_sym_name));
878 *offset = TLS_GD_IE_ADD;
879 if (bswap)
880 *offset = ld_bswap_Word(*offset);
881 return (FIX_DONE);
883 return (FIX_RELOC);
887 * LE reference model
889 switch (rtype) {
890 case R_SPARC_TLS_IE_HI22:
891 case R_SPARC_TLS_GD_HI22:
892 case R_SPARC_TLS_LDO_HIX22:
893 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
894 R_SPARC_TLS_LE_HIX22, arsp, ld_reloc_sym_name));
895 arsp->rel_rtype = R_SPARC_TLS_LE_HIX22;
896 return (FIX_RELOC);
898 case R_SPARC_TLS_LDO_LOX10:
899 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
900 R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name));
901 arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
902 return (FIX_RELOC);
904 case R_SPARC_TLS_IE_LO10:
905 case R_SPARC_TLS_GD_LO10:
907 * Current instruction is:
909 * or r1, %lo(x), r2
910 * or
911 * add r1, %lo(x), r2
913 * Need to udpate this to:
915 * xor r1, %lox(x), r2
917 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
918 R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name));
919 w = bswap ? ld_bswap_Word(*offset) : *offset;
920 w = TLS_GD_LE_XOR |
921 (w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RD));
922 *offset = bswap ? ld_bswap_Word(w) : w;
923 arsp->rel_rtype = R_SPARC_TLS_LE_LOX10;
924 return (FIX_RELOC);
926 case R_SPARC_TLS_IE_LD:
927 case R_SPARC_TLS_IE_LDX:
929 * Current instruction:
930 * ld{x} [r1 + r2], r3
932 * Need to update this to:
934 * mov r2, r3 (or %g0, r2, r3)
936 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
937 R_SPARC_NONE, arsp, ld_reloc_sym_name));
938 w = bswap ? ld_bswap_Word(*offset) : *offset;
939 w = (w & (FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | TLS_IE_LE_OR;
940 *offset = bswap ? ld_bswap_Word(w) : w;
941 return (FIX_DONE);
943 case R_SPARC_TLS_LDO_ADD:
944 case R_SPARC_TLS_GD_ADD:
946 * Current instruction is:
948 * add gptr_reg, r2, r3
950 * Need to updated this to:
952 * add %g7, r2, r3
954 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
955 R_SPARC_NONE, arsp, ld_reloc_sym_name));
956 w = bswap ? ld_bswap_Word(*offset) : *offset;
957 w = w & (~FM3_REG_MSK_RS1);
958 w = w | (REG_G7 << 14);
959 *offset = bswap ? ld_bswap_Word(w) : w;
960 return (FIX_DONE);
962 case R_SPARC_TLS_LDM_CALL:
963 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
964 R_SPARC_NONE, arsp, ld_reloc_sym_name));
965 *offset = TLS_LD_LE_CLRO0;
966 if (bswap)
967 *offset = ld_bswap_Word(*offset);
968 return (FIX_DONE);
970 case R_SPARC_TLS_LDM_HI22:
971 case R_SPARC_TLS_LDM_LO10:
972 case R_SPARC_TLS_LDM_ADD:
973 case R_SPARC_TLS_IE_ADD:
974 case R_SPARC_TLS_GD_CALL:
975 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
976 R_SPARC_NONE, arsp, ld_reloc_sym_name));
977 *offset = M_NOP;
978 if (bswap)
979 *offset = ld_bswap_Word(*offset);
980 return (FIX_DONE);
982 return (FIX_RELOC);
985 #define GOTOP_ADDINST 0x80000000 /* add %g0, %g0, %g0 */
987 static Fixupret
988 gotop_fixups(Ofl_desc *ofl, Rel_desc *arsp)
990 Word rtype = arsp->rel_rtype;
991 Word *offset, w;
992 const char *ifl_name;
993 Conv_inv_buf_t inv_buf;
994 int bswap;
996 switch (rtype) {
997 case R_SPARC_GOTDATA_OP_HIX22:
998 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
999 R_SPARC_GOTDATA_HIX22, arsp, ld_reloc_sym_name));
1000 arsp->rel_rtype = R_SPARC_GOTDATA_HIX22;
1001 return (FIX_RELOC);
1003 case R_SPARC_GOTDATA_OP_LOX10:
1004 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
1005 R_SPARC_GOTDATA_LOX10, arsp, ld_reloc_sym_name));
1006 arsp->rel_rtype = R_SPARC_GOTDATA_LOX10;
1007 return (FIX_RELOC);
1009 case R_SPARC_GOTDATA_OP:
1011 * Current instruction:
1012 * ld{x} [r1 + r2], r3
1014 * Need to update this to:
1016 * add r1, r2, r3
1018 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH,
1019 R_SPARC_NONE, arsp, ld_reloc_sym_name));
1020 offset = (Word *)(uintptr_t)(arsp->rel_roffset +
1021 _elf_getxoff(arsp->rel_isdesc->is_indata) +
1022 (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf);
1023 bswap = OFL_SWAP_RELOC_DATA(ofl, arsp);
1024 w = bswap ? ld_bswap_Word(*offset) : *offset;
1025 w = (w & (FM3_REG_MSK_RS1 |
1026 FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | GOTOP_ADDINST;
1027 *offset = bswap ? ld_bswap_Word(w) : w;
1028 return (FIX_DONE);
1031 * We should not get here
1033 if (arsp->rel_isdesc->is_file)
1034 ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1035 else
1036 ifl_name = MSG_INTL(MSG_STR_NULL);
1038 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADGOTFIX),
1039 conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
1040 ifl_name, ld_reloc_sym_name(arsp));
1042 assert(0);
1043 return (FIX_ERROR);
1046 static uintptr_t
1047 ld_do_activerelocs(Ofl_desc *ofl)
1049 Rel_desc *arsp;
1050 Rel_cachebuf *rcbp;
1051 Aliste idx;
1052 uintptr_t return_code = 1;
1053 ofl_flag_t flags = ofl->ofl_flags;
1055 if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0)
1056 DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml));
1059 * Process active relocations.
1061 REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) {
1062 uchar_t *addr;
1063 Xword value;
1064 Sym_desc *sdp;
1065 const char *ifl_name;
1066 Xword refaddr;
1067 Os_desc *osp;
1070 * If the section this relocation is against has been discarded
1071 * (-zignore), then discard (skip) the relocation itself.
1073 if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) &&
1074 ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS |
1075 FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) {
1076 DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp));
1077 continue;
1081 * Perform any required TLS fixups.
1083 if (arsp->rel_flags & FLG_REL_TLSFIX) {
1084 Fixupret ret;
1086 if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR)
1087 return (S_ERROR);
1088 if (ret == FIX_DONE)
1089 continue;
1093 * Perform any required GOTOP fixups.
1095 if (arsp->rel_flags & FLG_REL_GOTFIX) {
1096 Fixupret ret;
1098 if ((ret = gotop_fixups(ofl, arsp)) == FIX_ERROR)
1099 return (S_ERROR);
1100 if (ret == FIX_DONE)
1101 continue;
1105 * If this is a relocation against the move table, or
1106 * expanded move table, adjust the relocation entries.
1108 if (RELAUX_GET_MOVE(arsp))
1109 ld_adj_movereloc(ofl, arsp);
1111 sdp = arsp->rel_sym;
1112 refaddr = arsp->rel_roffset +
1113 (Off)_elf_getxoff(arsp->rel_isdesc->is_indata);
1115 if ((arsp->rel_flags & FLG_REL_CLVAL) ||
1116 (arsp->rel_flags & FLG_REL_GOTCL))
1117 value = 0;
1118 else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) {
1119 Sym_desc *sym;
1122 * The value for a symbol pointing to a SECTION
1123 * is based off of that sections position.
1125 if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) &&
1126 (sym = ld_am_I_partial(arsp, arsp->rel_raddend))) {
1128 * The symbol was moved, so adjust the value
1129 * relative to the new section.
1131 value = _elf_getxoff(sym->sd_isc->is_indata);
1132 if (sym->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
1133 value += sym->sd_isc->
1134 is_osdesc->os_shdr->sh_addr;
1137 * The original raddend covers the displacement
1138 * from the section start to the desired
1139 * address. The value computed above gets us
1140 * from the section start to the start of the
1141 * symbol range. Adjust the old raddend to
1142 * remove the offset from section start to
1143 * symbol start, leaving the displacement
1144 * within the range of the symbol.
1146 arsp->rel_raddend -= sym->sd_osym->st_value;
1147 } else {
1148 value = _elf_getxoff(sdp->sd_isc->is_indata);
1149 if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC)
1150 value += sdp->sd_isc->
1151 is_osdesc->os_shdr->sh_addr;
1154 if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS)
1155 value -= ofl->ofl_tlsphdr->p_vaddr;
1157 } else if (IS_SIZE(arsp->rel_rtype)) {
1159 * Size relocations require the symbols size.
1161 value = sdp->sd_sym->st_size;
1163 } else if ((sdp->sd_flags & FLG_SY_CAP) &&
1164 sdp->sd_aux && sdp->sd_aux->sa_PLTndx) {
1166 * If relocation is against a capabilities symbol, we
1167 * need to jump to an associated PLT, so that at runtime
1168 * ld.so.1 is involved to determine the best binding
1169 * choice. Otherwise, the value is the symbols value.
1171 value = ld_calc_plt_addr(sdp, ofl);
1173 } else
1174 value = sdp->sd_sym->st_value;
1177 * Relocation against the GLOBAL_OFFSET_TABLE.
1179 if ((arsp->rel_flags & FLG_REL_GOT) &&
1180 !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot))
1181 return (S_ERROR);
1182 osp = RELAUX_GET_OSDESC(arsp);
1185 * If loadable and not producing a relocatable object add the
1186 * sections virtual address to the reference address.
1188 if ((arsp->rel_flags & FLG_REL_LOAD) &&
1189 ((flags & FLG_OF_RELOBJ) == 0))
1190 refaddr +=
1191 arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr;
1194 * If this entry has a PLT assigned to it, its value is actually
1195 * the address of the PLT (and not the address of the function).
1197 if (IS_PLT(arsp->rel_rtype)) {
1198 if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx)
1199 value = ld_calc_plt_addr(sdp, ofl);
1203 * Add relocations addend to value. Add extra
1204 * relocation addend if needed.
1206 value += arsp->rel_raddend;
1207 if (IS_EXTOFFSET(arsp->rel_rtype))
1208 value += RELAUX_GET_TYPEDATA(arsp);
1211 * Determine whether the value needs further adjustment. Filter
1212 * through the attributes of the relocation to determine what
1213 * adjustment is required. Note, many of the following cases
1214 * are only applicable when a .got is present. As a .got is
1215 * not generated when a relocatable object is being built,
1216 * any adjustments that require a .got need to be skipped.
1218 if ((arsp->rel_flags & FLG_REL_GOT) &&
1219 ((flags & FLG_OF_RELOBJ) == 0)) {
1220 Xword R1addr;
1221 uintptr_t R2addr;
1222 Sword gotndx;
1223 Gotndx *gnp;
1224 Gotref gref;
1227 * Clear the GOT table entry, on SPARC we clear
1228 * the entry and the 'value' if needed is stored
1229 * in an output relocations addend.
1231 * Calculate offset into GOT at which to apply
1232 * the relocation.
1234 if (arsp->rel_flags & FLG_REL_DTLS)
1235 gref = GOT_REF_TLSGD;
1236 else if (arsp->rel_flags & FLG_REL_MTLS)
1237 gref = GOT_REF_TLSLD;
1238 else if (arsp->rel_flags & FLG_REL_STLS)
1239 gref = GOT_REF_TLSIE;
1240 else
1241 gref = GOT_REF_GENERIC;
1243 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
1244 assert(gnp);
1246 if (arsp->rel_rtype == M_R_DTPOFF)
1247 gotndx = gnp->gn_gotndx + 1;
1248 else
1249 gotndx = gnp->gn_gotndx;
1251 /* LINTED */
1252 R1addr = (Xword)((-neggotoffset * M_GOT_ENTSIZE) +
1253 (gotndx * M_GOT_ENTSIZE));
1256 * Add the GOTs data's offset.
1258 R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf;
1260 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml,
1261 ELF_DBG_LD_ACT, M_MACH, SHT_RELA,
1262 arsp, R1addr, value, ld_reloc_sym_name));
1265 * And do it.
1267 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
1268 *(Xword *)R2addr = ld_bswap_Xword(value);
1269 else
1270 *(Xword *)R2addr = value;
1271 continue;
1273 } else if (IS_GOT_BASED(arsp->rel_rtype) &&
1274 ((flags & FLG_OF_RELOBJ) == 0)) {
1275 value -= (ofl->ofl_osgot->os_shdr->sh_addr +
1276 (-neggotoffset * M_GOT_ENTSIZE));
1278 } else if (IS_PC_RELATIVE(arsp->rel_rtype)) {
1279 value -= refaddr;
1281 } else if (IS_TLS_INS(arsp->rel_rtype) &&
1282 IS_GOT_RELATIVE(arsp->rel_rtype) &&
1283 ((flags & FLG_OF_RELOBJ) == 0)) {
1284 Gotndx *gnp;
1285 Gotref gref;
1287 if (arsp->rel_flags & FLG_REL_STLS)
1288 gref = GOT_REF_TLSIE;
1289 else if (arsp->rel_flags & FLG_REL_DTLS)
1290 gref = GOT_REF_TLSGD;
1291 else if (arsp->rel_flags & FLG_REL_MTLS)
1292 gref = GOT_REF_TLSLD;
1294 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp);
1295 assert(gnp);
1297 value = gnp->gn_gotndx * M_GOT_ENTSIZE;
1299 } else if (IS_GOT_RELATIVE(arsp->rel_rtype) &&
1300 ((flags & FLG_OF_RELOBJ) == 0)) {
1301 Gotndx *gnp;
1303 gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1304 GOT_REF_GENERIC, ofl, arsp);
1305 assert(gnp);
1307 value = gnp->gn_gotndx * M_GOT_ENTSIZE;
1309 } else if ((arsp->rel_flags & FLG_REL_STLS) &&
1310 ((flags & FLG_OF_RELOBJ) == 0)) {
1311 Xword tlsstatsize;
1314 * This is the LE TLS reference model. Static offset is
1315 * hard-coded, and negated so that it can be added to
1316 * the thread pointer (%g7)
1318 tlsstatsize =
1319 S_ROUND(ofl->ofl_tlsphdr->p_memsz, M_TLSSTATALIGN);
1320 value = -(tlsstatsize - value);
1323 if (arsp->rel_isdesc->is_file)
1324 ifl_name = arsp->rel_isdesc->is_file->ifl_name;
1325 else
1326 ifl_name = MSG_INTL(MSG_STR_NULL);
1329 * Make sure we have data to relocate. Compiler and assembler
1330 * developers have been known to generate relocations against
1331 * invalid sections (normally .bss), so for their benefit give
1332 * them sufficient information to help analyze the problem.
1333 * End users should never see this.
1335 if (arsp->rel_isdesc->is_indata->d_buf == 0) {
1336 Conv_inv_buf_t inv_buf;
1338 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_EMPTYSEC),
1339 conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
1340 ifl_name, ld_reloc_sym_name(arsp),
1341 EC_WORD(arsp->rel_isdesc->is_scnndx),
1342 arsp->rel_isdesc->is_name);
1343 return (S_ERROR);
1347 * Get the address of the data item we need to modify.
1349 addr = (uchar_t *)((uintptr_t)arsp->rel_roffset +
1350 (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata));
1352 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT,
1353 M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value,
1354 ld_reloc_sym_name));
1355 addr += (uintptr_t)osp->os_outdata->d_buf;
1357 if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1358 ofl->ofl_size) || (arsp->rel_roffset >
1359 osp->os_shdr->sh_size)) {
1360 Conv_inv_buf_t inv_buf;
1361 int class;
1363 if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) >
1364 ofl->ofl_size)
1365 class = ERR_FATAL;
1366 else
1367 class = ERR_WARNING;
1369 ld_eprintf(ofl, class, MSG_INTL(MSG_REL_INVALOFFSET),
1370 conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf),
1371 ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx),
1372 arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp),
1373 EC_ADDR((uintptr_t)addr -
1374 (uintptr_t)ofl->ofl_nehdr));
1376 if (class == ERR_FATAL) {
1377 return_code = S_ERROR;
1378 continue;
1383 * If '-z noreloc' is specified - skip the do_reloc stage.
1385 if (OFL_DO_RELOC(ofl)) {
1386 if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name,
1387 ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp),
1388 ofl->ofl_lml) == 0) {
1389 ofl->ofl_flags |= FLG_OF_FATAL;
1390 return_code = S_ERROR;
1394 return (return_code);
1397 static uintptr_t
1398 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
1400 Rel_desc *orsp;
1401 Sym_desc *sdp = rsp->rel_sym;
1402 Conv_inv_buf_t inv_buf;
1405 * Static executables *do not* want any relocations against them.
1406 * Since our engine still creates relocations against a WEAK UNDEFINED
1407 * symbol in a static executable, it's best to disable them here
1408 * instead of through out the relocation code.
1410 if (OFL_IS_STATIC_EXEC(ofl))
1411 return (1);
1414 * Certain relocations do not make sense in a 64bit shared object,
1415 * if building a shared object do a sanity check on the output
1416 * relocations being created.
1418 if (ofl->ofl_flags & (FLG_OF_SHAROBJ | FLG_OF_PIE)) {
1419 Word rtype = rsp->rel_rtype;
1421 * Because the R_SPARC_HIPLT22 & R_SPARC_LOPLT10 relocations
1422 * are not relative they make no sense to create in a shared
1423 * object - so emit the proper error message if that occurs.
1425 if ((rtype == R_SPARC_HIPLT22) || (rtype == R_SPARC_LOPLT10)) {
1426 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_UNRELREL),
1427 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1428 rsp->rel_isdesc->is_file->ifl_name,
1429 ld_reloc_sym_name(rsp));
1430 return (S_ERROR);
1432 #if defined(_ELF64)
1434 * Each of the following relocations requires that the
1435 * object being built be loaded in either the upper 32 or
1436 * 44 bit range of memory. Since shared libraries traditionally
1437 * are loaded in the lower range of memory - this isn't going
1438 * to work.
1440 if ((rtype == R_SPARC_H44) || (rtype == R_SPARC_M44) ||
1441 (rtype == R_SPARC_L44)) {
1442 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SHOBJABS44),
1443 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1444 rsp->rel_isdesc->is_file->ifl_name,
1445 ld_reloc_sym_name(rsp));
1446 return (S_ERROR);
1448 #endif
1452 * If we are adding a output relocation against a section
1453 * symbol (non-RELATIVE) then mark that section. These sections
1454 * will be added to the .dynsym symbol table.
1456 if (sdp && (rsp->rel_rtype != M_R_RELATIVE) &&
1457 ((flags & FLG_REL_SCNNDX) ||
1458 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) {
1461 * If this is a COMMON symbol - no output section
1462 * exists yet - (it's created as part of sym_validate()).
1463 * So - we mark here that when it's created it should
1464 * be tagged with the FLG_OS_OUTREL flag.
1466 if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1467 (sdp->sd_sym->st_shndx == SHN_COMMON)) {
1468 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS)
1469 ofl->ofl_flags1 |= FLG_OF1_BSSOREL;
1470 else
1471 ofl->ofl_flags1 |= FLG_OF1_TLSOREL;
1472 } else {
1473 Os_desc *osp;
1474 Is_desc *isp = sdp->sd_isc;
1476 if (isp && ((osp = isp->is_osdesc) != NULL) &&
1477 ((osp->os_flags & FLG_OS_OUTREL) == 0)) {
1478 ofl->ofl_dynshdrcnt++;
1479 osp->os_flags |= FLG_OS_OUTREL;
1484 /* Enter it into the output relocation cache */
1485 if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL)
1486 return (S_ERROR);
1488 if (flags & FLG_REL_GOT)
1489 ofl->ofl_relocgotsz += (Xword)sizeof (Rela);
1490 else if (flags & FLG_REL_PLT)
1491 ofl->ofl_relocpltsz += (Xword)sizeof (Rela);
1492 else if (flags & FLG_REL_BSS)
1493 ofl->ofl_relocbsssz += (Xword)sizeof (Rela);
1494 else if (flags & FLG_REL_NOINFO)
1495 ofl->ofl_relocrelsz += (Xword)sizeof (Rela);
1496 else
1497 RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela);
1499 if (orsp->rel_rtype == M_R_RELATIVE)
1500 ofl->ofl_relocrelcnt++;
1502 #if defined(_ELF64)
1504 * When building a 64-bit object any R_SPARC_WDISP30 relocation is given
1505 * a plt padding entry, unless we're building a relocatable object
1506 * (ld -r) or -b is in effect.
1508 if ((orsp->rel_rtype == R_SPARC_WDISP30) &&
1509 ((ofl->ofl_flags & (FLG_OF_BFLAG | FLG_OF_RELOBJ)) == 0) &&
1510 ((orsp->rel_sym->sd_flags & FLG_SY_PLTPAD) == 0)) {
1511 ofl->ofl_pltpad++;
1512 orsp->rel_sym->sd_flags |= FLG_SY_PLTPAD;
1514 #endif
1516 * We don't perform sorting on PLT relocations because
1517 * they have already been assigned a PLT index and if we
1518 * were to sort them we would have to re-assign the plt indexes.
1520 if (!(flags & FLG_REL_PLT))
1521 ofl->ofl_reloccnt++;
1524 * Insure a GLOBAL_OFFSET_TABLE is generated if required.
1526 if (IS_GOT_REQUIRED(orsp->rel_rtype))
1527 ofl->ofl_flags |= FLG_OF_BLDGOT;
1530 * Identify and possibly warn of a displacement relocation.
1532 if (orsp->rel_flags & FLG_REL_DISP) {
1533 ofl->ofl_dtflags_1 |= DF_1_DISPRELPND;
1535 if (ofl->ofl_flags & FLG_OF_VERBOSE)
1536 ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl);
1538 DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA,
1539 M_MACH, orsp));
1540 return (1);
1544 * Process relocation against a register symbol. Note, of -z muldefs is in
1545 * effect there may have been multiple register definitions, which would have
1546 * been processed as non-fatal, with the first definition winning. But, we
1547 * will also process multiple relocations for these multiple definitions. In
1548 * this case we must only preserve the relocation for the definition that was
1549 * kept. The sad part is that register relocations don't typically specify
1550 * the register symbol with which they are associated, so we might have to
1551 * search the input files global symbols to determine if this relocation is
1552 * appropriate.
1554 static uintptr_t
1555 ld_reloc_register(Rel_desc *rsp, Is_desc *isp, Ofl_desc *ofl)
1557 if (ofl->ofl_flags & FLG_OF_MULDEFS) {
1558 Ifl_desc *ifl = isp->is_file;
1559 Sym_desc *sdp = rsp->rel_sym;
1561 if (sdp == 0) {
1562 Xword offset = rsp->rel_roffset;
1563 Word ndx;
1565 for (ndx = ifl->ifl_locscnt;
1566 ndx < ifl->ifl_symscnt; ndx++) {
1567 if (((sdp = ifl->ifl_oldndx[ndx]) != 0) &&
1568 (sdp->sd_flags & FLG_SY_REGSYM) &&
1569 (sdp->sd_sym->st_value == offset))
1570 break;
1573 if (sdp && (sdp->sd_file != ifl))
1574 return (1);
1576 return (ld_add_outrel((rsp->rel_flags | FLG_REL_REG), rsp, ofl));
1580 * process relocation for a LOCAL symbol
1582 static uintptr_t
1583 ld_reloc_local(Rel_desc *rsp, Ofl_desc *ofl)
1585 ofl_flag_t flags = ofl->ofl_flags;
1586 Sym_desc *sdp = rsp->rel_sym;
1587 Word shndx = sdp->sd_sym->st_shndx;
1590 * if ((shared object) and (not pc relative relocation) and
1591 * (not against ABS symbol))
1592 * then
1593 * if (rtype != R_SPARC_32)
1594 * then
1595 * build relocation against section
1596 * else
1597 * build R_SPARC_RELATIVE
1598 * fi
1599 * fi
1601 if ((flags & (FLG_OF_SHAROBJ | FLG_OF_PIE)) &&
1602 (rsp->rel_flags & FLG_REL_LOAD) &&
1603 !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) &&
1604 !(IS_GOT_BASED(rsp->rel_rtype)) &&
1605 !(rsp->rel_isdesc != NULL &&
1606 (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) &&
1607 (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
1608 (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
1609 Word ortype = rsp->rel_rtype;
1611 if ((rsp->rel_rtype != R_SPARC_32) &&
1612 (rsp->rel_rtype != R_SPARC_PLT32) &&
1613 (rsp->rel_rtype != R_SPARC_64))
1614 return (ld_add_outrel((FLG_REL_SCNNDX | FLG_REL_ADVAL),
1615 rsp, ofl));
1617 rsp->rel_rtype = R_SPARC_RELATIVE;
1618 if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR)
1619 return (S_ERROR);
1620 rsp->rel_rtype = ortype;
1621 return (1);
1625 * If the relocation is against a 'non-allocatable' section
1626 * and we can not resolve it now - then give a warning
1627 * message.
1629 * We can not resolve the symbol if either:
1630 * a) it's undefined
1631 * b) it's defined in a shared library and a
1632 * COPY relocation hasn't moved it to the executable
1634 * Note: because we process all of the relocations against the
1635 * text segment before any others - we know whether
1636 * or not a copy relocation will be generated before
1637 * we get here (see reloc_init()->reloc_segments()).
1639 if (!(rsp->rel_flags & FLG_REL_LOAD) &&
1640 ((shndx == SHN_UNDEF) ||
1641 ((sdp->sd_ref == REF_DYN_NEED) &&
1642 ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) {
1643 Conv_inv_buf_t inv_buf;
1644 Os_desc *osp = RELAUX_GET_OSDESC(rsp);
1647 * If the relocation is against a SHT_SUNW_ANNOTATE
1648 * section - then silently ignore that the relocation
1649 * can not be resolved.
1651 if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE))
1652 return (0);
1653 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM),
1654 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf),
1655 rsp->rel_isdesc->is_file->ifl_name,
1656 ld_reloc_sym_name(rsp), osp->os_name);
1657 return (1);
1661 * Perform relocation.
1663 return (ld_add_actrel(0, rsp, ofl));
1667 * Establish a relocation transition. Note, at this point of input relocation
1668 * processing, we have no idea of the relocation value that will be used in
1669 * the eventual relocation calculation. This value is only known after the
1670 * initial image has been constructed. Therefore, there is a small chance
1671 * that a value can exceed the capabilities of the transitioned relocation.
1672 * One example might be the offset from the GOT to a symbol.
1674 * The only instance of this failure discovered so far has been via the use of
1675 * ABS symbols to represent an external memory location. This situation is
1676 * rare, since ABS symbols aren't typically generated by the compilers.
1677 * Therefore, our solution is to excluded ABS symbols from the transition
1678 * relocation possibilities. As an additional safeguard, if an inappropriate
1679 * value is passed to the final relocation engine, a verification ("V")
1680 * relocation should trigger a fatal error condition.
1682 static uintptr_t
1683 ld_reloc_GOTOP(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1685 Word rtype = rsp->rel_rtype;
1687 if (!local || (rsp->rel_sym->sd_sym->st_shndx == SHN_ABS)) {
1689 * When binding to a external symbol, no fixups are required
1690 * and the GOTDATA_OP relocation can be ignored.
1692 if (rtype == R_SPARC_GOTDATA_OP)
1693 return (1);
1694 return (ld_reloc_GOT_relative(local, rsp, ofl));
1698 * When binding to a local symbol the relocations can be transitioned:
1700 * R_*_GOTDATA_OP_HIX22 -> R_*_GOTDATA_HIX22
1701 * R_*_GOTDATA_OP_LOX10 -> R_*_GOTDATA_LOX10
1702 * R_*_GOTDATA_OP -> instruction fixup
1704 return (ld_add_actrel(FLG_REL_GOTFIX, rsp, ofl));
1707 static uintptr_t
1708 ld_reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1710 Word rtype = rsp->rel_rtype;
1711 Sym_desc *sdp = rsp->rel_sym;
1712 ofl_flag_t flags = ofl->ofl_flags;
1713 Gotndx *gnp;
1716 * If we're building an executable - use either the IE or LE access
1717 * model. If we're building a shared object process any IE model.
1719 if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) {
1721 * Set the DF_STATIC_TLS flag.
1723 ofl->ofl_dtflags |= DF_STATIC_TLS;
1725 if (!local || ((flags & FLG_OF_EXEC) == 0)) {
1727 * When processing static TLS - these relocations
1728 * can be ignored.
1730 if ((rtype == R_SPARC_TLS_IE_LD) ||
1731 (rtype == R_SPARC_TLS_IE_LDX) ||
1732 (rtype == R_SPARC_TLS_IE_ADD))
1733 return (1);
1736 * Assign a GOT entry for IE static TLS references.
1738 if (((rtype == R_SPARC_TLS_GD_HI22) ||
1739 (rtype == R_SPARC_TLS_GD_LO10) ||
1740 (rtype == R_SPARC_TLS_IE_HI22) ||
1741 (rtype == R_SPARC_TLS_IE_LO10)) &&
1742 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs,
1743 GOT_REF_TLSIE, ofl, rsp)) == NULL)) {
1745 if (ld_assign_got_TLS(local, rsp, ofl, sdp,
1746 gnp, GOT_REF_TLSIE, FLG_REL_STLS,
1747 rtype, M_R_TPOFF, 0) == S_ERROR)
1748 return (S_ERROR);
1752 * IE access model.
1754 if (IS_TLS_IE(rtype))
1755 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1758 * Fixups are required for other executable models.
1760 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1761 rsp, ofl));
1765 * LE access model.
1767 if (IS_TLS_LE(rtype))
1768 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl));
1771 * When processing static TLS - these relocations can be
1772 * ignored.
1774 if (rtype == R_SPARC_TLS_IE_ADD)
1775 return (1);
1777 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS),
1778 rsp, ofl));
1782 * Building a shared object.
1784 * For dynamic TLS references, ADD relocations are ignored.
1786 if ((rtype == R_SPARC_TLS_GD_ADD) || (rtype == R_SPARC_TLS_LDM_ADD) ||
1787 (rtype == R_SPARC_TLS_LDO_ADD))
1788 return (1);
1791 * Assign a GOT entry for a dynamic TLS reference.
1793 if (((rtype == R_SPARC_TLS_LDM_HI22) ||
1794 (rtype == R_SPARC_TLS_LDM_LO10)) &&
1795 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSLD,
1796 ofl, rsp)) == NULL)) {
1798 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD,
1799 FLG_REL_MTLS, rtype, M_R_DTPMOD, 0) == S_ERROR)
1800 return (S_ERROR);
1802 } else if (((rtype == R_SPARC_TLS_GD_HI22) ||
1803 (rtype == R_SPARC_TLS_GD_LO10)) &&
1804 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD,
1805 ofl, rsp)) == NULL)) {
1807 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD,
1808 FLG_REL_DTLS, rtype, M_R_DTPMOD, M_R_DTPOFF) == S_ERROR)
1809 return (S_ERROR);
1813 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually
1814 * cause a call to __tls_get_addr(). Convert this relocation to that
1815 * symbol now, and prepare for the PLT magic.
1817 if ((rtype == R_SPARC_TLS_GD_CALL) || (rtype == R_SPARC_TLS_LDM_CALL)) {
1818 Sym_desc *tlsgetsym;
1820 if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_U),
1821 ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR)
1822 return (S_ERROR);
1824 rsp->rel_sym = tlsgetsym;
1825 rsp->rel_rtype = R_SPARC_WPLT30;
1827 if (ld_reloc_plt(rsp, ofl) == S_ERROR)
1828 return (S_ERROR);
1830 rsp->rel_sym = sdp;
1831 rsp->rel_rtype = rtype;
1832 return (1);
1835 if (IS_TLS_LD(rtype))
1836 return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl));
1838 return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl));
1842 * ld_allocate_got: if a GOT is to be made, after the section is built this
1843 * function is called to allocate all the GOT slots. The allocation is
1844 * deferred until after all GOTs have been counted and sorted according
1845 * to their size, for only then will we know how to allocate them on
1846 * a processor like SPARC which has different models for addressing the
1847 * GOT. SPARC has two: small and large, small uses a signed 13-bit offset
1848 * into the GOT, whereas large uses an unsigned 32-bit offset.
1850 static Sword small_index; /* starting index for small GOT entries */
1851 static Sword mixed_index; /* starting index for mixed GOT entries */
1852 static Sword large_index; /* starting index for large GOT entries */
1854 static uintptr_t
1855 ld_assign_got(Ofl_desc *ofl, Sym_desc *sdp)
1857 Aliste idx;
1858 Gotndx *gnp;
1860 for (ALIST_TRAVERSE(sdp->sd_GOTndxs, idx, gnp)) {
1861 uint_t gotents;
1862 Gotref gref = gnp->gn_gotref;
1864 if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1865 gotents = 2;
1866 else
1867 gotents = 1;
1869 switch (gnp->gn_gotndx) {
1870 case M_GOT_SMALL:
1871 gnp->gn_gotndx = small_index;
1872 small_index += gotents;
1873 if (small_index == 0)
1874 small_index = M_GOT_XNumber;
1875 break;
1876 case M_GOT_MIXED:
1877 gnp->gn_gotndx = mixed_index;
1878 mixed_index += gotents;
1879 break;
1880 case M_GOT_LARGE:
1881 gnp->gn_gotndx = large_index;
1882 large_index += gotents;
1883 break;
1884 default:
1885 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_ASSIGNGOT),
1886 EC_XWORD(gnp->gn_gotndx), demangle(sdp->sd_name));
1887 return (S_ERROR);
1890 return (1);
1893 static uintptr_t
1894 ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl,
1895 Rel_desc *rsp, Sym_desc *sdp)
1897 Xword raddend;
1898 Gotndx gn, *gnp;
1899 Aliste idx;
1900 uint_t gotents;
1902 /* Some TLS requires two relocations with two GOT entries */
1903 if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD))
1904 gotents = 2;
1905 else
1906 gotents = 1;
1908 raddend = rsp->rel_raddend;
1909 if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) {
1912 * If an entry for this addend already exists, determine if it
1913 * has mixed mode GOT access (both PIC and pic).
1915 * In order to be accessible by both large and small pic,
1916 * a mixed mode GOT must be located in the positive index
1917 * range above _GLOBAL_OFFSET_TABLE_, and in the range
1918 * reachable small pic. This is necessary because the large
1919 * PIC mode cannot use a negative offset. This implies that
1920 * there can be no more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
1921 * such entries.
1923 switch (pgnp->gn_gotndx) {
1924 case M_GOT_SMALL:
1926 * This one was previously identified as a small
1927 * GOT. If this access is large, then convert
1928 * it to mixed.
1930 if (rsp->rel_rtype != R_SPARC_GOT13) {
1931 pgnp->gn_gotndx = M_GOT_MIXED;
1932 mixgotcnt += gotents;
1934 break;
1936 case M_GOT_LARGE:
1938 * This one was previously identified as a large
1939 * GOT. If this access is small, convert it to mixed.
1941 if (rsp->rel_rtype == R_SPARC_GOT13) {
1942 smlgotcnt += gotents;
1943 mixgotcnt += gotents;
1944 pgnp->gn_gotndx = M_GOT_MIXED;
1945 sdp->sd_flags |= FLG_SY_SMGOT;
1947 break;
1949 return (1);
1952 gn.gn_addend = raddend;
1953 gn.gn_gotref = gref;
1955 if (rsp->rel_rtype == R_SPARC_GOT13) {
1956 gn.gn_gotndx = M_GOT_SMALL;
1957 smlgotcnt += gotents;
1958 sdp->sd_flags |= FLG_SY_SMGOT;
1959 } else
1960 gn.gn_gotndx = M_GOT_LARGE;
1962 ofl->ofl_gotcnt += gotents;
1964 if (gref == GOT_REF_TLSLD) {
1965 if (ofl->ofl_tlsldgotndx == NULL) {
1966 if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL)
1967 return (S_ERROR);
1968 (void) memcpy(gnp, &gn, sizeof (Gotndx));
1969 ofl->ofl_tlsldgotndx = gnp;
1971 return (1);
1974 idx = 0;
1975 for (ALIST_TRAVERSE(*alpp, idx, gnp)) {
1976 if (gnp->gn_addend > raddend)
1977 break;
1981 * GOT indexes are maintained on an Alist, where there is typically
1982 * only one index. The usage of this list is to scan the list to find
1983 * an index, and then apply that index immediately to a relocation.
1984 * Thus there are no external references to these GOT index structures
1985 * that can be compromised by the Alist being reallocated.
1987 if (alist_insert(alpp, &gn, sizeof (Gotndx),
1988 AL_CNT_SDP_GOT, idx) == NULL)
1989 return (S_ERROR);
1991 return (1);
1994 static void
1995 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl)
1997 sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++;
2001 static uintptr_t
2002 ld_allocate_got(Ofl_desc * ofl)
2004 const Sword first_large_ndx = M_GOT_MAXSMALL / 2;
2005 Sym_desc *sdp;
2006 Addr addr;
2009 * Sanity check -- is this going to fit at all? There are two
2010 * limits to be concerned about:
2011 * 1) There is a limit on the number of small pic GOT indices,
2012 * given by M_GOT_MAXSMALL.
2013 * 2) If there are more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber)
2014 * small GOT indices, there will be items at negative
2015 * offsets from _GLOBAL_OFFSET_TABLE_. Items that are
2016 * accessed via large (PIC) code cannot reach these
2017 * negative slots, so mixed mode items must be in the
2018 * non-negative range. This implies a limit of
2019 * (M_GOT_MAXSMALL/2 - M_GOT_XNumber) mixed mode indices.
2021 if (smlgotcnt > M_GOT_MAXSMALL) {
2022 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SMALLGOT),
2023 EC_WORD(smlgotcnt), M_GOT_MAXSMALL);
2024 return (S_ERROR);
2026 if (mixgotcnt > (first_large_ndx - M_GOT_XNumber)) {
2027 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_MIXEDGOT),
2028 EC_WORD(mixgotcnt), first_large_ndx - M_GOT_XNumber);
2029 return (S_ERROR);
2033 * Set starting offset to be either 0, or a negative index into
2034 * the GOT based on the number of small symbols we've got.
2036 neggotoffset = ((smlgotcnt >= first_large_ndx) ?
2037 (first_large_ndx - smlgotcnt) : 0);
2040 * Initialize the got offsets used by assign_got() to
2041 * locate GOT items:
2042 * small - Starting index of items referenced only
2043 * by small offsets (-Kpic).
2044 * mixed - Starting index of items referenced
2045 * by both large (-KPIC) and small (-Kpic).
2046 * large - Indexes referenced only by large (-KPIC)
2048 * Small items can have negative indexes (i.e. lie below
2049 * _GLOBAL_OFFSET_TABLE_). Mixed and large items must have
2050 * non-negative offsets.
2052 small_index = (neggotoffset == 0) ? M_GOT_XNumber : neggotoffset;
2053 large_index = neggotoffset + smlgotcnt;
2054 mixed_index = large_index - mixgotcnt;
2057 * Assign bias to GOT symbols.
2059 addr = -neggotoffset * M_GOT_ENTSIZE;
2060 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), SYM_NOHASH,
2061 NULL, ofl)) != NULL)
2062 sdp->sd_sym->st_value = addr;
2063 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH,
2064 NULL, ofl)) != NULL)
2065 sdp->sd_sym->st_value = addr;
2067 if (ofl->ofl_tlsldgotndx) {
2068 ofl->ofl_tlsldgotndx->gn_gotndx = large_index;
2069 large_index += 2;
2071 return (1);
2075 * Initializes .got[0] with the _DYNAMIC symbol value.
2077 static uintptr_t
2078 ld_fillin_gotplt(Ofl_desc *ofl)
2080 if (ofl->ofl_osgot) {
2081 Sym_desc *sdp;
2083 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U),
2084 SYM_NOHASH, NULL, ofl)) != NULL) {
2085 uchar_t *genptr;
2087 genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf +
2088 (-neggotoffset * M_GOT_ENTSIZE) +
2089 (M_GOT_XDYNAMIC * M_GOT_ENTSIZE));
2090 /* LINTED */
2091 *((Xword *)genptr) = sdp->sd_sym->st_value;
2092 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF)
2093 /* LINTED */
2094 *((Xword *)genptr) =
2095 /* LINTED */
2096 ld_bswap_Xword(*((Xword *)genptr));
2099 return (1);
2105 * Template for generating "void (*)(void)" function
2107 static const uchar_t nullfunc_tmpl[] = {
2108 /* 0x00 */ 0x81, 0xc3, 0xe0, 0x08, /* retl */
2109 /* 0x04 */ 0x01, 0x00, 0x00, 0x00 /* nop */
2115 * Return the ld_targ definition for this target.
2117 const Target *
2118 ld_targ_init_sparc(void)
2120 static const Target _ld_targ = {
2121 { /* Target_mach */
2122 M_MACH, /* m_mach */
2123 M_MACHPLUS, /* m_machplus */
2124 M_FLAGSPLUS, /* m_flagsplus */
2125 M_CLASS, /* m_class */
2126 M_DATA, /* m_data */
2128 M_SEGM_ALIGN, /* m_segm_align */
2129 M_SEGM_ORIGIN, /* m_segm_origin */
2130 M_SEGM_AORIGIN, /* m_segm_aorigin */
2131 M_DATASEG_PERM, /* m_dataseg_perm */
2132 M_STACK_PERM, /* m_stack_perm */
2133 M_WORD_ALIGN, /* m_word_align */
2134 /* m_def_interp */
2135 #if defined(_ELF64)
2136 MSG_ORIG(MSG_PTH_RTLD_SPARCV9),
2137 #else
2138 MSG_ORIG(MSG_PTH_RTLD),
2139 #endif
2141 /* Relocation type codes */
2142 M_R_ARRAYADDR, /* m_r_arrayaddr */
2143 M_R_COPY, /* m_r_copy */
2144 M_R_GLOB_DAT, /* m_r_glob_dat */
2145 M_R_JMP_SLOT, /* m_r_jmp_slot */
2146 M_R_NUM, /* m_r_num */
2147 M_R_NONE, /* m_r_none */
2148 M_R_RELATIVE, /* m_r_relative */
2149 M_R_REGISTER, /* m_r_register */
2151 /* Relocation related constants */
2152 M_REL_DT_COUNT, /* m_rel_dt_count */
2153 M_REL_DT_ENT, /* m_rel_dt_ent */
2154 M_REL_DT_SIZE, /* m_rel_dt_size */
2155 M_REL_DT_TYPE, /* m_rel_dt_type */
2156 M_REL_SHT_TYPE, /* m_rel_sht_type */
2158 /* GOT related constants */
2159 M_GOT_ENTSIZE, /* m_got_entsize */
2160 M_GOT_XNumber, /* m_got_xnumber */
2162 /* PLT related constants */
2163 M_PLT_ALIGN, /* m_plt_align */
2164 M_PLT_ENTSIZE, /* m_plt_entsize */
2165 M_PLT_RESERVSZ, /* m_plt_reservsz */
2166 M_PLT_SHF_FLAGS, /* m_plt_shf_flags */
2168 /* Section type of .eh_frame/.eh_frame_hdr sections */
2169 SHT_PROGBITS, /* m_sht_unwind */
2171 M_DT_REGISTER, /* m_dt_register */
2173 { /* Target_machid */
2174 M_ID_ARRAY, /* id_array */
2175 M_ID_BSS, /* id_bss */
2176 M_ID_CAP, /* id_cap */
2177 M_ID_CAPINFO, /* id_capinfo */
2178 M_ID_CAPCHAIN, /* id_capchain */
2179 M_ID_DATA, /* id_data */
2180 M_ID_DYNAMIC, /* id_dynamic */
2181 M_ID_DYNSORT, /* id_dynsort */
2182 M_ID_DYNSTR, /* id_dynstr */
2183 M_ID_DYNSYM, /* id_dynsym */
2184 M_ID_DYNSYM_NDX, /* id_dynsym_ndx */
2185 M_ID_GOT, /* id_got */
2186 M_ID_GOTDATA, /* id_gotdata */
2187 M_ID_HASH, /* id_hash */
2188 M_ID_INTERP, /* id_interp */
2189 M_ID_UNKNOWN, /* id_lbss (unused) */
2190 M_ID_LDYNSYM, /* id_ldynsym */
2191 M_ID_NOTE, /* id_note */
2192 M_ID_NULL, /* id_null */
2193 M_ID_PLT, /* id_plt */
2194 M_ID_REL, /* id_rel */
2195 M_ID_STRTAB, /* id_strtab */
2196 M_ID_SYMINFO, /* id_syminfo */
2197 M_ID_SYMTAB, /* id_symtab */
2198 M_ID_SYMTAB_NDX, /* id_symtab_ndx */
2199 M_ID_TEXT, /* id_text */
2200 M_ID_TLS, /* id_tls */
2201 M_ID_TLSBSS, /* id_tlsbss */
2202 M_ID_UNKNOWN, /* id_unknown */
2203 M_ID_UNWIND, /* id_unwind */
2204 M_ID_UNWINDHDR, /* id_unwindhdr */
2205 M_ID_USER, /* id_user */
2206 M_ID_VERSION, /* id_version */
2208 { /* Target_nullfunc */
2209 nullfunc_tmpl, /* nf_template */
2210 sizeof (nullfunc_tmpl), /* nf_size */
2212 { /* Target_fillfunc */
2214 * On sparc, special filling of executable sections
2215 * is undesirable, and the default 0 fill supplied
2216 * by libelf is preferred:
2218 * - 0 fill is interpreted as UNIMP instructions,
2219 * which cause an illegal_instruction_trap. These
2220 * serve as a sentinel against poorly written
2221 * code. The sparc architecture manual discusses
2222 * this as providing a measure of runtime safety.
2224 * - The one place where a hole should conceivably
2225 * be filled with NOP instructions is in the
2226 * .init/.fini sections. However, the sparc
2227 * assembler sizes the sections it generates
2228 * to a multiple of the section alignment, and as
2229 * such, takes the filling task out of our hands.
2230 * Furthermore, the sparc assembler uses 0-fill
2231 * for this, forcing the authors of sparc
2232 * assembler for .init/.fini sections to be aware
2233 * of this case and explicitly supply NOP fill.
2234 * Hence, there is no role for the link-editor.
2236 NULL /* ff_execfill */
2238 { /* Target_machrel */
2239 reloc_table,
2241 ld_init_rel, /* mr_init_rel */
2242 ld_mach_eflags, /* mr_mach_eflags */
2243 ld_mach_make_dynamic, /* mr_mach_make_dynamic */
2244 ld_mach_update_odynamic, /* mr_mach_update_odynamic */
2245 ld_calc_plt_addr, /* mr_calc_plt_addr */
2246 ld_perform_outreloc, /* mr_perform_outreloc */
2247 ld_do_activerelocs, /* mr_do_activerelocs */
2248 ld_add_outrel, /* mr_add_outrel */
2249 ld_reloc_register, /* mr_reloc_register */
2250 ld_reloc_local, /* mr_reloc_local */
2251 ld_reloc_GOTOP, /* mr_reloc_GOTOP */
2252 ld_reloc_TLS, /* mr_reloc_TLS */
2253 ld_assign_got, /* mr_assign_got */
2254 ld_find_got_ndx, /* mr_find_got_ndx */
2255 ld_calc_got_offset, /* mr_calc_got_offset */
2256 ld_assign_got_ndx, /* mr_assign_got_ndx */
2257 ld_assign_plt_ndx, /* mr_assign_plt_ndx */
2258 ld_allocate_got, /* mr_allocate_got */
2259 ld_fillin_gotplt, /* mr_fillin_gotplt */
2261 { /* Target_machsym */
2262 ld_reg_check_sparc, /* ms_reg_check */
2263 ld_mach_sym_typecheck_sparc, /* ms_mach_sym_typecheck */
2264 ld_is_regsym_sparc, /* ms_is_regsym */
2265 ld_reg_find_sparc, /* ms_reg_find */
2266 ld_reg_enter_sparc /* ms_reg_enter */
2270 return (&_ld_targ);