dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / sgs / libld / common / relocate.c
blobb9c942a4d923e10194b987f214e657ada9ace35b
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.
30 * set-up for relocations
33 #define ELF_TARGET_AMD64
34 #define ELF_TARGET_SPARC
36 #include <string.h>
37 #include <stdio.h>
38 #include <alloca.h>
39 #include <debug.h>
40 #include "msg.h"
41 #include "_libld.h"
44 * Set up the relocation table flag test macros so that they use the
45 * relocation table for the current target machine.
47 #define IS_PLT(X) RELTAB_IS_PLT(X, ld_targ.t_mr.mr_reloc_table)
48 #define IS_GOT_RELATIVE(X) \
49 RELTAB_IS_GOT_RELATIVE(X, ld_targ.t_mr.mr_reloc_table)
50 #define IS_GOT_PC(X) RELTAB_IS_GOT_PC(X, ld_targ.t_mr.mr_reloc_table)
51 #define IS_GOTPCREL(X) RELTAB_IS_GOTPCREL(X, ld_targ.t_mr.mr_reloc_table)
52 #define IS_GOT_BASED(X) RELTAB_IS_GOT_BASED(X, ld_targ.t_mr.mr_reloc_table)
53 #define IS_GOT_OPINS(X) RELTAB_IS_GOT_OPINS(X, ld_targ.t_mr.mr_reloc_table)
54 #define IS_GOT_REQUIRED(X) \
55 RELTAB_IS_GOT_REQUIRED(X, ld_targ.t_mr.mr_reloc_table)
56 #define IS_PC_RELATIVE(X) RELTAB_IS_PC_RELATIVE(X, ld_targ.t_mr.mr_reloc_table)
57 #define IS_ADD_RELATIVE(X) \
58 RELTAB_IS_ADD_RELATIVE(X, ld_targ.t_mr.mr_reloc_table)
59 #define IS_REGISTER(X) RELTAB_IS_REGISTER(X, ld_targ.t_mr.mr_reloc_table)
60 #define IS_NOTSUP(X) RELTAB_IS_NOTSUP(X, ld_targ.t_mr.mr_reloc_table)
61 #define IS_SEG_RELATIVE(X) \
62 RELTAB_IS_SEG_RELATIVE(X, ld_targ.t_mr.mr_reloc_table)
63 #define IS_EXTOFFSET(X) RELTAB_IS_EXTOFFSET(X, ld_targ.t_mr.mr_reloc_table)
64 #define IS_SEC_RELATIVE(X) \
65 RELTAB_IS_SEC_RELATIVE(X, ld_targ.t_mr.mr_reloc_table)
66 #define IS_TLS_INS(X) RELTAB_IS_TLS_INS(X, ld_targ.t_mr.mr_reloc_table)
67 #define IS_TLS_GD(X) RELTAB_IS_TLS_GD(X, ld_targ.t_mr.mr_reloc_table)
68 #define IS_TLS_LD(X) RELTAB_IS_TLS_LD(X, ld_targ.t_mr.mr_reloc_table)
69 #define IS_TLS_IE(X) RELTAB_IS_TLS_IE(X, ld_targ.t_mr.mr_reloc_table)
70 #define IS_TLS_LE(X) RELTAB_IS_TLS_LE(X, ld_targ.t_mr.mr_reloc_table)
71 #define IS_LOCALBND(X) RELTAB_IS_LOCALBND(X, ld_targ.t_mr.mr_reloc_table)
72 #define IS_SIZE(X) RELTAB_IS_SIZE(X, ld_targ.t_mr.mr_reloc_table)
75 * Structure to hold copy relocation items.
77 typedef struct copy_rel {
78 Sym_desc *c_sdp; /* symbol descriptor to be copied */
79 Addr c_val; /* original symbol value */
80 } Copy_rel;
83 * For each copy relocation symbol, determine if the symbol is:
84 * 1) to be *disp* relocated at runtime
85 * 2) a reference symbol for *disp* relocation
86 * 3) possibly *disp* relocated at ld time.
88 * The first and the second are serious errors.
90 static void
91 is_disp_copied(Ofl_desc *ofl, Copy_rel *crp)
93 Ifl_desc *ifl = crp->c_sdp->sd_file;
94 Sym_desc *sdp = crp->c_sdp;
95 Addr symaddr = crp->c_val;
96 Is_desc *irel;
97 Aliste idx;
98 Conv_inv_buf_t inv_buf;
101 * This symbol may not be *disp* relocated at run time, but could
102 * already have been *disp* relocated when the shared object was
103 * created. Warn the user.
105 if ((ifl->ifl_flags & FLG_IF_DISPDONE) &&
106 (ofl->ofl_flags & FLG_OF_VERBOSE))
107 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_DISPREL2),
108 conv_reloc_type(ifl->ifl_ehdr->e_machine,
109 ld_targ.t_m.m_r_copy, 0, &inv_buf),
110 ifl->ifl_name, demangle(sdp->sd_name));
112 if ((ifl->ifl_flags & FLG_IF_DISPPEND) == 0)
113 return;
116 * Traverse the input relocation sections.
118 for (APLIST_TRAVERSE(ifl->ifl_relsect, idx, irel)) {
119 Sym_desc *rsdp;
120 Is_desc *trel;
121 Rel *rend, *reloc;
122 Xword rsize, entsize;
124 trel = ifl->ifl_isdesc[irel->is_shdr->sh_info];
125 rsize = irel->is_shdr->sh_size;
126 entsize = irel->is_shdr->sh_entsize;
127 reloc = (Rel *)irel->is_indata->d_buf;
130 * Decide entry size
132 if ((entsize == 0) || (entsize > rsize)) {
133 if (irel->is_shdr->sh_type == SHT_RELA)
134 entsize = sizeof (Rela);
135 else
136 entsize = sizeof (Rel);
140 * Traverse the relocation entries.
142 for (rend = (Rel *)((uintptr_t)reloc + (uintptr_t)rsize);
143 reloc < rend;
144 reloc = (Rel *)((uintptr_t)reloc + (uintptr_t)entsize)) {
145 const char *str;
146 Word rstndx;
148 if (IS_PC_RELATIVE(ELF_R_TYPE(reloc->r_info,
149 ld_targ.t_m.m_mach)) == 0)
150 continue;
153 * Determine if symbol is referenced from a relocation.
155 rstndx = (Word) ELF_R_SYM(reloc->r_info);
156 rsdp = ifl->ifl_oldndx[rstndx];
157 if (rsdp == sdp) {
158 if ((str = demangle(rsdp->sd_name)) !=
159 rsdp->sd_name) {
160 char *_str = alloca(strlen(str) + 1);
161 (void) strcpy(_str, str);
162 str = (const char *)_str;
164 ld_eprintf(ofl, ERR_WARNING,
165 MSG_INTL(MSG_REL_DISPREL1),
166 conv_reloc_type(ifl->ifl_ehdr->e_machine,
167 (uint_t)ELF_R_TYPE(reloc->r_info,
168 ld_targ.t_m.m_mach),
169 0, &inv_buf), ifl->ifl_name, str,
170 MSG_INTL(MSG_STR_UNKNOWN),
171 EC_XWORD(reloc->r_offset),
172 demangle(sdp->sd_name));
176 * Determine whether the relocation entry is relocating
177 * this symbol.
179 if ((sdp->sd_isc != trel) ||
180 (reloc->r_offset < symaddr) ||
181 (reloc->r_offset >=
182 (symaddr + sdp->sd_sym->st_size)))
183 continue;
186 * This symbol is truely *disp* relocated, so should
187 * really be fixed by user.
189 if ((str = demangle(sdp->sd_name)) != sdp->sd_name) {
190 char *_str = alloca(strlen(str) + 1);
191 (void) strcpy(_str, str);
192 str = (const char *)_str;
194 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_DISPREL1),
195 conv_reloc_type(ifl->ifl_ehdr->e_machine,
196 (uint_t)ELF_R_TYPE(reloc->r_info,
197 ld_targ.t_m.m_mach), 0, &inv_buf),
198 ifl->ifl_name, demangle(rsdp->sd_name), str,
199 EC_XWORD(reloc->r_offset), str);
205 * The number of symbols provided by some objects can be very large. Use a
206 * binary search to match the associated value to a symbol table entry.
208 static int
209 disp_bsearch(const void *key, const void *array)
211 Addr kvalue, avalue;
212 Ssv_desc *ssvp = (Ssv_desc *)array;
214 kvalue = *((Addr *)key);
215 avalue = ssvp->ssv_value;
217 if (avalue > kvalue)
218 return (-1);
219 if ((avalue < kvalue) &&
220 ((avalue + ssvp->ssv_sdp->sd_sym->st_size) <= kvalue))
221 return (1);
222 return (0);
226 * Given a sorted list of symbols, look for a symbol in which the relocation
227 * offset falls between the [sym.st_value - sym.st_value + sym.st_size]. Since
228 * the symbol list is maintained in sorted order, we can bail once the
229 * relocation offset becomes less than the symbol values. The symbol is
230 * returned for use in error diagnostics.
232 static Sym_desc *
233 disp_scansyms(Ifl_desc * ifl, Rel_desc *rld, Boolean rlocal, int inspect,
234 Ofl_desc *ofl)
236 Sym_desc *tsdp, *rsdp;
237 Sym *rsym, *tsym;
238 Ssv_desc *ssvp;
239 uchar_t rtype, ttype;
240 Addr value;
243 * Sorted symbol values have been uniquified by adding their associated
244 * section offset. Uniquify the relocation offset by adding its
245 * associated section offset, and search for the symbol.
247 value = rld->rel_roffset;
248 if (rld->rel_isdesc->is_shdr)
249 value += rld->rel_isdesc->is_shdr->sh_offset;
251 if ((ssvp = bsearch((void *)&value, (void *)ifl->ifl_sortsyms,
252 ifl->ifl_sortcnt, sizeof (Ssv_desc), &disp_bsearch)) != 0)
253 tsdp = ssvp->ssv_sdp;
254 else
255 tsdp = 0;
257 if (inspect)
258 return (tsdp);
261 * Determine the relocation reference symbol and its type.
263 rsdp = rld->rel_sym;
264 rsym = rsdp->sd_sym;
265 rtype = ELF_ST_TYPE(rsym->st_info);
268 * If there is no target symbol to match the relocation offset, then the
269 * offset is effectively local data. If the relocation symbol is global
270 * data we have a potential for this displacement relocation to be
271 * invalidated should the global symbol be copied.
273 if (tsdp == 0) {
274 if ((rlocal == TRUE) ||
275 ((rtype != STT_OBJECT) && (rtype != STT_SECTION)))
276 return (tsdp);
277 } else {
279 * If both symbols are local, no copy relocations can occur to
280 * either symbol. Note, this test is very similar to the test
281 * used in ld_sym_adjust_vis().
283 if ((rlocal == TRUE) && (SYM_IS_HIDDEN(tsdp) ||
284 (ELF_ST_BIND(tsdp->sd_sym->st_info) != STB_GLOBAL) ||
285 ((ofl->ofl_flags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) &&
286 ((tsdp->sd_flags & MSK_SY_NOAUTO) == 0))))
287 return (tsdp);
290 * Determine the relocation target symbols type.
292 tsym = tsdp->sd_sym;
293 ttype = ELF_ST_TYPE(tsym->st_info);
296 * If the reference symbol is local, and the target isn't a
297 * data element, then no copy relocations can occur to either
298 * symbol. Note, this catches pc-relative relocations against
299 * the _GLOBAL_OFFSET_TABLE_, which is effectively treated as
300 * a local symbol.
302 if ((rlocal == TRUE) && (ttype != STT_OBJECT) &&
303 (ttype != STT_SECTION))
304 return (tsdp);
307 * Finally, one of the symbols must reference a data element.
309 if ((rtype != STT_OBJECT) && (rtype != STT_SECTION) &&
310 (ttype != STT_OBJECT) && (ttype != STT_SECTION))
311 return (tsdp);
315 * We have two global symbols, at least one of which is a data item.
316 * The last case where a displacement relocation can be ignored, is
317 * if the reference symbol is included in the target symbol.
319 value = rsym->st_value;
320 if ((rld->rel_flags & FLG_REL_RELA) == FLG_REL_RELA)
321 value += rld->rel_raddend;
323 if ((rld->rel_roffset >= value) &&
324 (rld->rel_roffset < (value + rsym->st_size)))
325 return (tsdp);
328 * We have a displacement relocation that could be compromised by a
329 * copy relocation of one of the associated data items.
331 rld->rel_flags |= FLG_REL_DISP;
332 return (tsdp);
335 void
336 ld_disp_errmsg(const char *msg, Rel_desc *rsp, Ofl_desc *ofl)
338 Sym_desc *sdp;
339 const char *str;
340 Ifl_desc *ifl = rsp->rel_isdesc->is_file;
341 Conv_inv_buf_t inv_buf;
343 if ((sdp = disp_scansyms(ifl, rsp, 0, 1, ofl)) != 0)
344 str = demangle(sdp->sd_name);
345 else
346 str = MSG_INTL(MSG_STR_UNKNOWN);
348 ld_eprintf(ofl, ERR_WARNING, msg,
349 conv_reloc_type(ifl->ifl_ehdr->e_machine, rsp->rel_rtype,
350 0, &inv_buf), ifl->ifl_name, ld_reloc_sym_name(rsp), str,
351 EC_OFF(rsp->rel_roffset));
355 * qsort(3C) comparison routine used for the disp_sortsyms().
357 static int
358 disp_qsort(const void * s1, const void * s2)
360 Ssv_desc *ssvp1 = ((Ssv_desc *)s1);
361 Ssv_desc *ssvp2 = ((Ssv_desc *)s2);
362 Addr val1 = ssvp1->ssv_value;
363 Addr val2 = ssvp2->ssv_value;
365 if (val1 > val2)
366 return (1);
367 if (val1 < val2)
368 return (-1);
369 return (0);
373 * Determine whether a displacement relocation is between a local and global
374 * symbol pair. One symbol is used to perform the relocation, and the other
375 * is the destination offset of the relocation.
377 static uintptr_t
378 disp_inspect(Ofl_desc *ofl, Rel_desc *rld, Boolean rlocal)
380 Is_desc *isp = rld->rel_isdesc;
381 Ifl_desc *ifl = rld->rel_isdesc->is_file;
384 * If the input files symbols haven't been sorted yet, do so.
386 if (ifl->ifl_sortsyms == 0) {
387 Word ondx, nndx;
389 if ((ifl->ifl_sortsyms = libld_malloc((ifl->ifl_symscnt + 1) *
390 sizeof (Ssv_desc))) == 0)
391 return (S_ERROR);
393 for (ondx = 0, nndx = 0; ondx < ifl->ifl_symscnt; ondx++) {
394 Sym_desc *sdp;
395 Addr value;
398 * As symbol resolution has already occurred, various
399 * symbols from this object may have been satisfied
400 * from other objects. Only select symbols from this
401 * object. For the displacement test, we only really
402 * need to observe data definitions, however, later as
403 * part of providing warning disgnostics, relating the
404 * relocation offset to a symbol is desirable. Thus,
405 * collect all symbols that define a memory area.
407 if (((sdp = ifl->ifl_oldndx[ondx]) == 0) ||
408 (sdp->sd_sym->st_shndx == SHN_UNDEF) ||
409 (sdp->sd_sym->st_shndx >= SHN_LORESERVE) ||
410 (sdp->sd_ref != REF_REL_NEED) ||
411 (sdp->sd_file != ifl) ||
412 (sdp->sd_sym->st_size == 0))
413 continue;
416 * As a further optimization for later checking, mark
417 * this section if this a global data definition.
419 if (sdp->sd_isc && (ondx >= ifl->ifl_locscnt))
420 sdp->sd_isc->is_flags |= FLG_IS_GDATADEF;
423 * Capture the symbol. Within relocatable objects, a
424 * symbols value is its offset within its associated
425 * section. Add the section offset to this value to
426 * uniquify the symbol.
428 value = sdp->sd_sym->st_value;
429 if (sdp->sd_isc && sdp->sd_isc->is_shdr)
430 value += sdp->sd_isc->is_shdr->sh_offset;
432 ifl->ifl_sortsyms[nndx].ssv_value = value;
433 ifl->ifl_sortsyms[nndx].ssv_sdp = sdp;
434 nndx++;
438 * Sort the list based on the symbols value (address).
440 if ((ifl->ifl_sortcnt = nndx) != 0)
441 qsort(ifl->ifl_sortsyms, nndx, sizeof (Ssv_desc),
442 &disp_qsort);
446 * If the reference symbol is local, and the section being relocated
447 * contains no global definitions, neither can be the target of a copy
448 * relocation.
450 if ((rlocal == FALSE) && ((isp->is_flags & FLG_IS_GDATADEF) == 0))
451 return (1);
454 * Otherwise determine whether this relocation symbol and its offset
455 * could be candidates for a copy relocation.
457 if (ifl->ifl_sortcnt)
458 (void) disp_scansyms(ifl, rld, rlocal, 0, ofl);
459 return (1);
463 * Return a Rel_cachebuf with an available Rel_desc entry from the
464 * specified cache, allocating a cache buffer if necessary.
466 * entry:
467 * ofl - Output file descriptor
468 * rcp - Relocation cache to allocate the descriptor from.
469 * One of &ofl->ofl_actrels or &ofl->ofl_outrels.
471 * exit:
472 * Returns the allocated descriptor, or NULL if the allocation fails.
474 static Rel_cachebuf *
475 ld_add_rel_cache(Ofl_desc *ofl, Rel_cache *rcp)
477 Rel_cachebuf *rcbp;
478 size_t nelts, size, alloc_cnt;
481 * If there is space available in the present cache bucket, return the
482 * next free entry.
484 alloc_cnt = aplist_nitems(rcp->rc_list);
485 if (rcp->rc_list &&
486 ((rcbp = rcp->rc_list->apl_data[alloc_cnt - 1]) != NULL) &&
487 (rcbp->rc_free < rcbp->rc_end))
488 return (rcbp);
491 * Allocate a new bucket. As we cannot know the number of relocations
492 * we'll have in the active and output cache until after the link is
493 * complete, the size of the bucket is a heuristic.
495 * In general, if the output object is an executable, or a sharable
496 * object, then the size of the active relocation list will be nearly
497 * the same as the number of input relocations, and the output
498 * relocation list will be very short. If the output object is a
499 * relocatable object, then the reverse is true. Therefore, the initial
500 * allocation for the appropriate list is sized to fit all the input
501 * allocations in a single shot.
503 * All other allocations are done in units of REL_CACHEBUF_ALLOC,
504 * which is chosen to be large enough to cover most common cases,
505 * but small enough that not using it fully is inconsequential.
507 * In an ideal scenario, this results in one allocation on each list.
509 nelts = REL_CACHEBUF_ALLOC;
510 if ((alloc_cnt == 0) && (ofl->ofl_relocincnt > REL_CACHEBUF_ALLOC)) {
511 Boolean is_rel = (ofl->ofl_flags & FLG_OF_RELOBJ) != 0;
513 if (((rcp == &ofl->ofl_actrels) && !is_rel) ||
514 ((rcp == &ofl->ofl_outrels) && is_rel))
515 nelts = ofl->ofl_relocincnt;
519 * Compute the total number of bytes to allocate. The first element
520 * of the array is built into the Rel_cachebuf header, so we subtract
521 * one from nelts.
523 size = sizeof (Rel_cachebuf) + ((nelts - 1) * sizeof (Rel_desc));
525 if (((rcbp = libld_malloc(size)) == NULL) ||
526 (aplist_append(&rcp->rc_list, rcbp, AL_CNT_OFL_RELS) == NULL))
527 return (NULL);
529 rcbp->rc_free = rcbp->rc_arr;
530 rcbp->rc_end = rcbp->rc_arr + nelts;
532 return (rcbp);
536 * Allocate a Rel_aux descriptor and attach it to the given Rel_desc,
537 * allocating an auxiliary cache buffer if necessary.
539 * entry:
540 * ofl - Output file descriptor
541 * rdp - Rel_desc descriptor that requires an auxiliary block
543 * exit:
544 * Returns TRUE on success, and FALSE if the allocation fails.
545 * On success, the caller is responsible for initializing the
546 * auxiliary block properly.
548 static Boolean
549 ld_add_rel_aux(Ofl_desc *ofl, Rel_desc *rdesc)
551 Rel_aux_cachebuf *racp = NULL;
552 size_t size;
555 * If there is space available in the present cache bucket, use it.
556 * Otherwise, allocate a new bucket.
558 if (ofl->ofl_relaux) {
559 racp = ofl->ofl_relaux->apl_data[
560 ofl->ofl_relaux->apl_nitems - 1];
562 if (racp && (racp->rac_free >= racp->rac_end))
563 racp = NULL;
565 if (racp == NULL) {
567 * Compute the total number of bytes to allocate. The first
568 * element of the array is built into the Rel_aux_cachebuf
569 * header, so we subtract one from the number of elements.
571 size = sizeof (Rel_aux_cachebuf) +
572 ((RELAUX_CACHEBUF_ALLOC - 1) * sizeof (Rel_aux));
573 if (((racp = libld_malloc(size)) == NULL) ||
574 (aplist_append(&ofl->ofl_relaux, racp, AL_CNT_OFL_RELS) ==
575 NULL))
576 return (FALSE);
578 racp->rac_free = racp->rac_arr;
579 racp->rac_end = racp->rac_arr + RELAUX_CACHEBUF_ALLOC;
582 /* Take an auxiliary descriptor from the cache and add it to rdesc */
583 rdesc->rel_aux = racp->rac_free++;
585 return (TRUE);
589 * Enter a copy of the given Rel_desc relocation descriptor, and
590 * any associated auxiliary Rel_aux it may reference, into the
591 * specified relocation cache.
593 * entry:
594 * ofl - Output file descriptor
595 * rcp - Relocation descriptor cache to recieve relocation
596 * rdesc - Rel_desc image to be inserted
597 * flags - Flags to add to rdest->rel_flags in the inserted descriptor
599 * exit:
600 * Returns the pointer to the inserted descriptor on success.
601 * Returns NULL if an allocation error occurs.
603 Rel_desc *
604 ld_reloc_enter(Ofl_desc *ofl, Rel_cache *rcp, Rel_desc *rdesc, Word flags)
606 Rel_desc *arsp;
607 Rel_aux *auxp;
608 Rel_cachebuf *rcbp;
612 * If no relocation cache structures are available, allocate a new
613 * one and link it to the buffer list.
615 rcbp = ld_add_rel_cache(ofl, rcp);
616 if (rcbp == NULL)
617 return (NULL);
618 arsp = rcbp->rc_free;
621 * If there is an auxiliary block on the original, allocate
622 * one for the clone. Save the pointer, because the struct copy
623 * below will crush it.
625 if (rdesc->rel_aux != NULL) {
626 if (!ld_add_rel_aux(ofl, arsp))
627 return (NULL);
628 auxp = arsp->rel_aux;
631 /* Copy contents of the original into the clone */
632 *arsp = *rdesc;
635 * If there is an auxiliary block, restore the clone's pointer to
636 * it, and copy the auxiliary contents.
638 if (rdesc->rel_aux != NULL) {
639 arsp->rel_aux = auxp;
640 *auxp = *rdesc->rel_aux;
642 arsp->rel_flags |= flags;
644 rcbp->rc_free++;
645 rcp->rc_cnt++;
647 return (arsp);
651 * Initialize a relocation descriptor auxiliary block to default
652 * values.
654 * entry:
655 * rdesc - Relocation descriptor, with a non-NULL rel_aux field
656 * pointing at the auxiliary block to be initialized.
658 * exit:
659 * Each field in rdesc->rel_aux has been set to its default value
661 static void
662 ld_init_rel_aux(Rel_desc *rdesc)
664 Rel_aux *rap = rdesc->rel_aux;
667 * The default output section is the one the input section
668 * is assigned to, assuming that there is an input section.
669 * Failing that, NULL is the only possibility, and we expect
670 * that the caller will assign an explicit value.
672 rap->ra_osdesc = (rdesc->rel_isdesc == NULL) ? NULL :
673 rdesc->rel_isdesc->is_osdesc;
675 /* The ra_usym defaults to the value in rel_sym */
676 rap->ra_usym = rdesc->rel_sym;
678 /* Remaining fields are zeroed */
679 rap->ra_move = NULL;
680 rap->ra_typedata = 0;
684 * The ld_reloc_set_aux_XXX() functions are used to set the value of an
685 * auxiliary relocation item on a relocation descriptor that exists in
686 * the active or output relocation cache. These descriptors are created
687 * via a call to ld_reloc_enter().
689 * These functions preserve the illusion that every relocation descriptor
690 * has a non-NULL auxiliary block into which values can be set, while
691 * only creating an auxiliary block if one is actually necessary, preventing
692 * the large memory allocations that would otherwise occur. They operate
693 * as follows:
695 * - If an auxiliary block already exists, set the desired value and
696 * and return TRUE.
698 * - If no auxiliary block exists, but the desired value is the default
699 * value for the specified item, then no auxiliary block is needed,
700 * and TRUE is returned.
702 * - If no auxiliary block exists, and the desired value is not the
703 * default for the specified item, allocate an auxiliary block for
704 * the descriptor, initialize its contents to default values for all
705 * items, set the specified value, and return TRUE.
707 * - If an auxiliary block needs to be added, but the allocation fails,
708 * an error is issued, and FALSE is returned.
710 * Note that we only provide an ld_reloc_set_aux_XXX() function for those
711 * auxiliary items that libld actually modifies in Rel_desc descriptors
712 * in the active or output caches. If another one is needed, add it here.
714 * The PROCESS_NULL_REL_AUX macro is used to provide a single implementation
715 * for the logic that determines if an auxiliary block is needed or not,
716 * and handles the details of allocating and initializing it. It accepts
717 * one argument, _isdefault_predicate, which should be a call to the
718 * RELAUX_ISDEFAULT_xxx() macro appropriate for the auxiliary item
721 #define PROCESS_NULL_REL_AUX(_isdefault_predicate) \
722 if (rdesc->rel_aux == NULL) { \
723 /* If requested value is the default, no need for aux block */ \
724 if (_isdefault_predicate) \
725 return (TRUE); \
726 /* Allocate and attach an auxiliary block */ \
727 if (!ld_add_rel_aux(ofl, rdesc)) \
728 return (FALSE); \
729 /* Initialize the auxiliary block with default values */ \
730 ld_init_rel_aux(rdesc); \
733 Boolean
734 ld_reloc_set_aux_osdesc(Ofl_desc *ofl, Rel_desc *rdesc, Os_desc *osp)
736 PROCESS_NULL_REL_AUX(RELAUX_ISDEFAULT_OSDESC(rdesc, osp))
737 rdesc->rel_aux->ra_osdesc = osp;
738 return (TRUE);
741 Boolean
742 ld_reloc_set_aux_usym(Ofl_desc *ofl, Rel_desc *rdesc, Sym_desc *sdp)
744 PROCESS_NULL_REL_AUX(RELAUX_ISDEFAULT_USYM(rdesc, sdp))
745 rdesc->rel_aux->ra_usym = sdp;
746 return (TRUE);
749 #undef PROCESS_NULL_REL_AUX
752 * Return a descriptive name for the symbol associated with the
753 * given relocation descriptor. This will be the actual symbol
754 * name if one exists, or a suitable alternative otherwise.
756 * entry:
757 * rsp - Relocation descriptor
759 const char *
760 ld_reloc_sym_name(Rel_desc *rsp)
762 Sym_desc *sdp = rsp->rel_sym;
764 if (sdp != NULL) {
765 /* If the symbol has a valid name use it */
766 if (sdp->sd_name && *sdp->sd_name)
767 return (demangle(sdp->sd_name));
770 * If the symbol is STT_SECTION, and the corresponding
771 * section symbol has the specially prepared string intended
772 * for this use, use that string. The string is of the form
773 * secname (section)
775 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) &&
776 (sdp->sd_isc != NULL) && (sdp->sd_isc->is_sym_name != NULL))
777 return (demangle(sdp->sd_isc->is_sym_name));
778 } else {
780 * Use an empty name for a register relocation with
781 * no symbol.
783 if (IS_REGISTER(rsp->rel_rtype))
784 return (MSG_ORIG(MSG_STR_EMPTY));
787 /* If all else fails, report it as <unknown> */
788 return (MSG_INTL(MSG_STR_UNKNOWN));
792 * Add an active relocation record.
794 uintptr_t
795 ld_add_actrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl)
797 Rel_desc *arsp;
799 if ((arsp = ld_reloc_enter(ofl, &ofl->ofl_actrels, rsp, flags)) == NULL)
800 return (S_ERROR);
803 * Any GOT relocation reference requires the creation of a .got table.
804 * Most references to a .got require a .got entry, which is accounted
805 * for with the ofl_gotcnt counter. However, some references are
806 * relative to the .got table, but require no .got entry. This test
807 * insures a .got is created regardless of the type of reference.
809 if (IS_GOT_REQUIRED(arsp->rel_rtype))
810 ofl->ofl_flags |= FLG_OF_BLDGOT;
813 * If this is a displacement relocation generate a warning.
815 if (arsp->rel_flags & FLG_REL_DISP) {
816 ofl->ofl_dtflags_1 |= DF_1_DISPRELDNE;
818 if (ofl->ofl_flags & FLG_OF_VERBOSE)
819 ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL3), arsp, ofl);
822 DBG_CALL(Dbg_reloc_ars_entry(ofl->ofl_lml, ELF_DBG_LD,
823 arsp->rel_isdesc->is_shdr->sh_type, ld_targ.t_m.m_mach, arsp));
824 return (1);
828 * In the platform specific machrel.XXX.c files, we sometimes write
829 * a value directly into the got/plt. These function can be used when
830 * the running linker has the opposite byte order of the object being
831 * produced.
833 Word
834 ld_bswap_Word(Word v)
836 return (BSWAP_WORD(v));
840 Xword
841 ld_bswap_Xword(Xword v)
843 return (BSWAP_XWORD(v));
847 uintptr_t
848 ld_reloc_GOT_relative(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
850 Sym_desc *sdp = rsp->rel_sym;
851 ofl_flag_t flags = ofl->ofl_flags;
852 Gotndx *gnp;
855 * If this is the first time we've seen this symbol in a GOT
856 * relocation we need to assign it a GOT token. Once we've got
857 * all of the GOT's assigned we can assign the actual indexes.
859 if ((gnp = (*ld_targ.t_mr.mr_find_got_ndx)(sdp->sd_GOTndxs,
860 GOT_REF_GENERIC, ofl, rsp)) == 0) {
861 Word rtype = rsp->rel_rtype;
863 if ((*ld_targ.t_mr.mr_assign_got_ndx)(&(sdp->sd_GOTndxs), NULL,
864 GOT_REF_GENERIC, ofl, rsp, sdp) == S_ERROR)
865 return (S_ERROR);
868 * Initialize the GOT table entry.
870 * For global symbols, we clear the GOT table entry and create
871 * a GLOB_DAT relocation against the symbol.
873 * For local symbols, we enter the symbol value into a GOT
874 * table entry and create a relative relocation if all of
875 * the following hold:
877 * - Output is a shared object
878 * - Symbol is not ABS
879 * - Relocation is not against one of the special sections
880 * (COMMON, ...)
881 * - This is not one of the generated symbols we have
882 * to update after the output object has been fully
883 * laid out (_START_, _END_, ...)
885 * Local symbols that don't meet the above requirements
886 * are processed as is.
888 if (local == TRUE) {
889 if ((flags & (FLG_OF_SHAROBJ | FLG_OF_PIE)) &&
890 (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) ||
891 ((sdp->sd_sym->st_shndx != SHN_ABS)) ||
892 (sdp->sd_aux && sdp->sd_aux->sa_symspec))) {
893 if (ld_add_actrel((FLG_REL_GOT | FLG_REL_GOTCL),
894 rsp, ofl) == S_ERROR)
895 return (S_ERROR);
897 rsp->rel_rtype = ld_targ.t_m.m_r_relative;
899 if ((*ld_targ.t_mr.mr_add_outrel)
900 ((FLG_REL_GOT | FLG_REL_ADVAL),
901 rsp, ofl) == S_ERROR)
902 return (S_ERROR);
904 rsp->rel_rtype = rtype;
905 } else {
906 if (ld_add_actrel(FLG_REL_GOT, rsp,
907 ofl) == S_ERROR)
908 return (S_ERROR);
910 } else {
911 rsp->rel_rtype = ld_targ.t_m.m_r_glob_dat;
912 if ((*ld_targ.t_mr.mr_add_outrel)(FLG_REL_GOT,
913 rsp, ofl) == S_ERROR)
914 return (S_ERROR);
915 rsp->rel_rtype = rtype;
917 } else {
918 if ((*ld_targ.t_mr.mr_assign_got_ndx)(&(sdp->sd_GOTndxs), gnp,
919 GOT_REF_GENERIC, ofl, rsp, sdp) == S_ERROR)
920 return (S_ERROR);
924 * Perform relocation to GOT table entry.
926 return (ld_add_actrel(0, rsp, ofl));
930 * Perform relocations for PLT's
932 uintptr_t
933 ld_reloc_plt(Rel_desc *rsp, Ofl_desc *ofl)
935 Sym_desc *sdp = rsp->rel_sym;
937 switch (ld_targ.t_m.m_mach) {
938 case EM_AMD64:
940 * AMD64 TLS code sequences do not use a unique TLS
941 * relocation to reference the __tls_get_addr() function call.
943 if ((ofl->ofl_flags & FLG_OF_EXEC) &&
944 (strcmp(sdp->sd_name, MSG_ORIG(MSG_SYM_TLSGETADDR_U)) ==
946 return (ld_add_actrel(FLG_REL_TLSFIX, rsp, ofl));
947 break;
949 case EM_386:
951 * GNUC IA32 TLS code sequences do not use a unique TLS
952 * relocation to reference the ___tls_get_addr() function call.
954 if ((ofl->ofl_flags & FLG_OF_EXEC) &&
955 (strcmp(sdp->sd_name, MSG_ORIG(MSG_SYM_TLSGETADDR_UU)) ==
957 return (ld_add_actrel(FLG_REL_TLSFIX, rsp, ofl));
958 break;
962 * if (not PLT yet assigned)
963 * then
964 * assign PLT index to symbol
965 * build output JMP_SLOT relocation
966 * fi
968 if (sdp->sd_aux->sa_PLTndx == 0) {
969 Word ortype = rsp->rel_rtype;
971 (*ld_targ.t_mr.mr_assign_plt_ndx)(sdp, ofl);
974 * If this symbol is binding to a lazy loadable, or deferred
975 * dependency, then identify the symbol.
977 if (sdp->sd_file) {
978 if (sdp->sd_file->ifl_flags & FLG_IF_LAZYLD)
979 sdp->sd_flags |= FLG_SY_LAZYLD;
980 if (sdp->sd_file->ifl_flags & FLG_IF_DEFERRED)
981 sdp->sd_flags |= FLG_SY_DEFERRED;
984 rsp->rel_rtype = ld_targ.t_m.m_r_jmp_slot;
985 if ((*ld_targ.t_mr.mr_add_outrel)(FLG_REL_PLT, rsp, ofl) ==
986 S_ERROR)
987 return (S_ERROR);
988 rsp->rel_rtype = ortype;
992 * Perform relocation to PLT table entry.
994 if ((ofl->ofl_flags & (FLG_OF_SHAROBJ | FLG_OF_PIE)) &&
995 IS_ADD_RELATIVE(rsp->rel_rtype)) {
996 Word ortype = rsp->rel_rtype;
998 rsp->rel_rtype = ld_targ.t_m.m_r_relative;
999 if ((*ld_targ.t_mr.mr_add_outrel)(FLG_REL_ADVAL, rsp, ofl) ==
1000 S_ERROR)
1001 return (S_ERROR);
1002 rsp->rel_rtype = ortype;
1003 return (1);
1004 } else
1005 return (ld_add_actrel(0, rsp, ofl));
1009 * Round up to the next power of 2. Used to ensure section alignments that can
1010 * be used for copy relocation symbol alignments are sane values.
1012 static Word
1013 nlpo2(Word val)
1015 val--;
1016 val |= (val >> 1);
1017 val |= (val >> 2);
1018 val |= (val >> 4);
1019 val |= (val >> 8);
1020 val |= (val >> 16);
1021 return (++val);
1025 * process GLOBAL undefined and ref_dyn_need symbols.
1027 static uintptr_t
1028 reloc_exec(Rel_desc *rsp, Ofl_desc *ofl)
1030 Sym_desc *_sdp, *sdp = rsp->rel_sym;
1031 Sym_aux *sap = sdp->sd_aux;
1032 Sym *sym = sdp->sd_sym;
1033 Addr stval;
1036 * Reference is to a function so simply create a plt entry for it.
1038 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC)
1039 return (ld_reloc_plt(rsp, ofl));
1042 * Catch absolutes - these may cause a text relocation.
1044 if ((sdp->sd_flags & FLG_SY_SPECSEC) && (sym->st_shndx == SHN_ABS)) {
1045 if ((ofl->ofl_flags1 & FLG_OF1_ABSEXEC) == 0)
1046 return ((*ld_targ.t_mr.mr_add_outrel)(0, rsp, ofl));
1049 * If -zabsexec is set then promote the ABSOLUTE symbol to
1050 * current the current object and perform the relocation now.
1052 sdp->sd_ref = REF_REL_NEED;
1053 return (ld_add_actrel(0, rsp, ofl));
1057 * If the relocation is against a writable section simply compute the
1058 * necessary output relocation. As an optimization, if the symbol has
1059 * already been transformed into a copy relocation then we can perform
1060 * the relocation directly (copy relocations should only be generated
1061 * for references from the text segment and these relocations are
1062 * normally carried out before we get to the data segment relocations).
1064 if ((ELF_ST_TYPE(sym->st_info) == STT_OBJECT) &&
1065 (RELAUX_GET_OSDESC(rsp)->os_shdr->sh_flags & SHF_WRITE)) {
1066 if (sdp->sd_flags & FLG_SY_MVTOCOMM)
1067 return (ld_add_actrel(0, rsp, ofl));
1068 else
1069 return ((*ld_targ.t_mr.mr_add_outrel)(0, rsp, ofl));
1073 * If the reference isn't to an object (normally because a .type
1074 * directive wasn't defined in some assembler source), then apply
1075 * a generic relocation (this has a tendency to result in text
1076 * relocations).
1078 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT) {
1079 Conv_inv_buf_t inv_buf;
1081 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_UNEXPSYM),
1082 conv_sym_info_type(sdp->sd_file->ifl_ehdr->e_machine,
1083 ELF_ST_TYPE(sym->st_info), 0, &inv_buf),
1084 rsp->rel_isdesc->is_file->ifl_name,
1085 ld_reloc_sym_name(rsp), sdp->sd_file->ifl_name);
1086 return ((*ld_targ.t_mr.mr_add_outrel)(0, rsp, ofl));
1090 * Prepare for generating a copy relocation.
1092 * If this symbol is one of an alias pair, we need to ensure both
1093 * symbols become part of the output (the strong symbol will be used to
1094 * maintain the symbols state). And, if we did raise the precedence of
1095 * a symbol we need to check and see if this is a weak symbol. If it is
1096 * we want to use it's strong counter part.
1098 * The results of this logic should be:
1099 * ra_usym: assigned to strong
1100 * rel_sym: assigned to symbol to perform
1101 * copy_reloc against (weak or strong).
1103 if (sap->sa_linkndx) {
1104 _sdp = sdp->sd_file->ifl_oldndx[sap->sa_linkndx];
1106 if (_sdp->sd_ref < sdp->sd_ref) {
1107 _sdp->sd_ref = sdp->sd_ref;
1108 _sdp->sd_flags |= FLG_SY_REFRSD;
1111 * As we're going to replicate a symbol from a shared
1112 * object, retain its correct binding status.
1114 if (ELF_ST_BIND(_sdp->sd_sym->st_info) == STB_GLOBAL)
1115 _sdp->sd_flags |= FLG_SY_GLOBREF;
1117 } else if (_sdp->sd_ref > sdp->sd_ref) {
1118 sdp->sd_ref = _sdp->sd_ref;
1119 sdp->sd_flags |= FLG_SY_REFRSD;
1122 * As we're going to replicate a symbol from a shared
1123 * object, retain its correct binding status.
1125 if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL)
1126 sdp->sd_flags |= FLG_SY_GLOBREF;
1130 * If this is a weak symbol then we want to move the strong
1131 * symbol into local .bss. If there is a copy_reloc to be
1132 * performed, that should still occur against the WEAK symbol.
1134 if (((ELF_ST_BIND(sdp->sd_sym->st_info) == STB_WEAK) ||
1135 (sdp->sd_flags & FLG_SY_WEAKDEF)) &&
1136 !ld_reloc_set_aux_usym(ofl, rsp, _sdp))
1137 return (S_ERROR);
1138 } else
1139 _sdp = 0;
1142 * If the reference is to an object then allocate space for the object
1143 * within the executables .bss. Relocations will now be performed from
1144 * this new location. If the original shared objects data is
1145 * initialized, then generate a copy relocation that will copy the data
1146 * to the executables .bss at runtime.
1148 if (!(RELAUX_GET_USYM(rsp)->sd_flags & FLG_SY_MVTOCOMM)) {
1149 Word rtype = rsp->rel_rtype, w2align;
1150 Copy_rel cr;
1153 * Diagnose the original copy reference, as this symbol
1154 * information will be overridden with the new destination.
1156 DBG_CALL(Dbg_syms_copy_reloc(ofl, sdp, 0));
1159 * Indicate that the symbol(s) against which we're relocating
1160 * have been moved to the executables common. Also, insure that
1161 * the symbol(s) remain marked as global, as the shared object
1162 * from which they are copied must be able to relocate to the
1163 * new common location within the executable.
1165 * Note that even though a new symbol has been generated in the
1166 * output files' .bss, the symbol must remain REF_DYN_NEED and
1167 * not be promoted to REF_REL_NEED. sym_validate() still needs
1168 * to carry out a number of checks against the symbols binding
1169 * that are triggered by the REF_DYN_NEED state.
1171 sdp->sd_flags |=
1172 (FLG_SY_MVTOCOMM | FLG_SY_DEFAULT | FLG_SY_EXPDEF);
1173 sdp->sd_flags &= ~MSK_SY_LOCAL;
1174 sdp->sd_sym->st_other &= ~MSK_SYM_VISIBILITY;
1175 if (_sdp) {
1176 _sdp->sd_flags |= (FLG_SY_MVTOCOMM |
1177 FLG_SY_DEFAULT | FLG_SY_EXPDEF);
1178 _sdp->sd_flags &= ~MSK_SY_LOCAL;
1179 _sdp->sd_sym->st_other &= ~MSK_SYM_VISIBILITY;
1182 * Make sure the symbol has a reference in case of any
1183 * error diagnostics against it (perhaps this belongs
1184 * to a version that isn't allowable for this build).
1185 * The resulting diagnostic (see sym_undef_entry())
1186 * might seem a little bogus, as the symbol hasn't
1187 * really been referenced by this file, but has been
1188 * promoted as a consequence of its alias reference.
1190 if (!(_sdp->sd_aux->sa_rfile))
1191 _sdp->sd_aux->sa_rfile = sdp->sd_aux->sa_rfile;
1195 * Assign the symbol to the bss.
1197 _sdp = RELAUX_GET_USYM(rsp);
1198 stval = _sdp->sd_sym->st_value;
1199 if (ld_sym_copy(_sdp) == S_ERROR)
1200 return (S_ERROR);
1201 _sdp->sd_shndx = _sdp->sd_sym->st_shndx = SHN_COMMON;
1202 _sdp->sd_flags |= FLG_SY_SPECSEC;
1205 * Ensure the symbol has sufficient alignment. The symbol
1206 * definition has no alignment information that can be used,
1207 * hence we use a heuristic. Historically, twice the native
1208 * word alignment was sufficient for any data type, however,
1209 * the developer may have requested larger alignments (pragma
1210 * align). The most conservative approach is to use a power
1211 * of two alignment, determined from the alignment of the
1212 * section containing the symbol definition. Note that this
1213 * can result in some bloat to the .bss as the not every item
1214 * of copied data might need the section alignment.
1216 * COMMON symbols carry their alignment requirements in the
1217 * symbols st_value field. This alignment is applied to the
1218 * symbol when it is eventually transformed into .bss.
1220 w2align = ld_targ.t_m.m_word_align * 2;
1221 if (_sdp->sd_sym->st_size < w2align)
1222 _sdp->sd_sym->st_value = ld_targ.t_m.m_word_align;
1223 else {
1224 Shdr *shdr;
1225 Word isalign;
1227 if (_sdp->sd_isc &&
1228 ((shdr = _sdp->sd_isc->is_shdr) != NULL) &&
1229 ((isalign = shdr->sh_addralign) != 0))
1230 _sdp->sd_sym->st_value = nlpo2(isalign);
1231 else
1232 _sdp->sd_sym->st_value = w2align;
1236 * Whether or not the symbol references initialized data we
1237 * generate a copy relocation - this differs from the past
1238 * where we would not create the COPY_RELOC if we were binding
1239 * against .bss. This is done for *two* reasons.
1241 * - If the symbol in the shared object changes to a
1242 * initialized data - we need the COPY to pick it up.
1243 * - Without the COPY RELOC we can't tell that the symbol
1244 * from the COPY'd object has been moved and all bindings
1245 * to it should bind here.
1247 * Keep this symbol in the copy relocation list to check the
1248 * validity later.
1250 cr.c_sdp = _sdp;
1251 cr.c_val = stval;
1252 if (alist_append(&ofl->ofl_copyrels, &cr, sizeof (Copy_rel),
1253 AL_CNT_OFL_COPYRELS) == NULL)
1254 return (S_ERROR);
1256 rsp->rel_rtype = ld_targ.t_m.m_r_copy;
1257 if ((*ld_targ.t_mr.mr_add_outrel)(FLG_REL_BSS, rsp, ofl) ==
1258 S_ERROR)
1259 return (S_ERROR);
1260 rsp->rel_rtype = rtype;
1263 * If this symbol is a protected symbol, warn the user. A
1264 * potential issue exists as the copy relocated symbol within
1265 * the executable can be visible to others, whereas the shared
1266 * object that defined the original copy data symbol is pre-
1267 * bound to reference it's own definition. Any modification
1268 * of the symbols data could lead to inconsistencies for the
1269 * various users.
1271 if (_sdp->sd_flags & FLG_SY_PROT) {
1272 Conv_inv_buf_t inv_buf;
1274 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_COPY),
1275 conv_reloc_type(_sdp->sd_file->ifl_ehdr->e_machine,
1276 ld_targ.t_m.m_r_copy, 0, &inv_buf),
1277 _sdp->sd_file->ifl_name, _sdp->sd_name);
1279 DBG_CALL(Dbg_syms_copy_reloc(ofl, _sdp,
1280 _sdp->sd_sym->st_value));
1282 return (ld_add_actrel(0, rsp, ofl));
1286 * All relocations should have been handled by the other routines. This
1287 * routine is here as a catch all, if we do enter it we've goofed - but
1288 * we'll try and do the best we can.
1290 static uintptr_t
1291 reloc_generic(Rel_desc *rsp, Ofl_desc *ofl)
1293 Ifl_desc *ifl = rsp->rel_isdesc->is_file;
1294 Conv_inv_buf_t inv_buf;
1296 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_UNEXPREL),
1297 conv_reloc_type(ifl->ifl_ehdr->e_machine, rsp->rel_rtype,
1298 0, &inv_buf), ifl->ifl_name, ld_reloc_sym_name(rsp));
1301 * If building a shared object then put the relocation off
1302 * until runtime.
1304 if (ofl->ofl_flags & FLG_OF_SHAROBJ)
1305 return ((*ld_targ.t_mr.mr_add_outrel)(0, rsp, ofl));
1308 * Otherwise process relocation now.
1310 return (ld_add_actrel(0, rsp, ofl));
1314 * Process relocations when building a relocatable object. Typically, there
1315 * aren't many relocations that can be caught at this point, most are simply
1316 * passed through to the output relocatable object.
1318 static uintptr_t
1319 reloc_relobj(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1321 Word rtype = rsp->rel_rtype;
1322 Sym_desc *sdp = rsp->rel_sym;
1323 Is_desc *isp = rsp->rel_isdesc;
1324 Word oflags = 0;
1327 * Determine if we can do any relocations at this point. We can if:
1329 * this is local_symbol and a non-GOT relocation, and
1330 * the relocation is pc-relative, and
1331 * the relocation is against a symbol in same section
1333 if (local && !IS_GOT_RELATIVE(rtype) &&
1334 !IS_GOT_BASED(rtype) && !IS_GOT_PC(rtype) &&
1335 IS_PC_RELATIVE(rtype) &&
1336 ((sdp->sd_isc) && (sdp->sd_isc->is_osdesc == isp->is_osdesc)))
1337 return (ld_add_actrel(0, rsp, ofl));
1340 * If -zredlocsym is in effect, translate all local symbol relocations
1341 * to be against section symbols, since section symbols are the only
1342 * local symbols which will be added to the .symtab.
1344 if (local && (((ofl->ofl_flags & FLG_OF_REDLSYM) &&
1345 (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL)) ||
1346 ((sdp->sd_flags & FLG_SY_ELIM) &&
1347 (ofl->ofl_flags & FLG_OF_PROCRED)))) {
1349 * But if this is PIC code, don't allow it for now.
1351 if (IS_GOT_RELATIVE(rsp->rel_rtype)) {
1352 Ifl_desc *ifl = rsp->rel_isdesc->is_file;
1353 Conv_inv_buf_t inv_buf;
1355 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_PICREDLOC),
1356 ld_reloc_sym_name(rsp), ifl->ifl_name,
1357 conv_reloc_type(ifl->ifl_ehdr->e_machine,
1358 rsp->rel_rtype, 0, &inv_buf));
1359 return (S_ERROR);
1363 * Indicate that this relocation should be processed the same
1364 * as a section symbol. For RELA, indicate that the addend
1365 * also needs to be applied to this relocation.
1367 if ((rsp->rel_flags & FLG_REL_RELA) == FLG_REL_RELA)
1368 oflags = FLG_REL_SCNNDX | FLG_REL_ADVAL;
1369 else
1370 oflags = FLG_REL_SCNNDX;
1373 if ((rsp->rel_flags & FLG_REL_RELA) == 0) {
1375 * Intel (Rel) relocations do not contain an addend. Any
1376 * addend is contained within the file at the location
1377 * identified by the relocation offset. Therefore, if we're
1378 * processing a section symbol, or a -zredlocsym relocation
1379 * (that basically transforms a local symbol reference into
1380 * a section reference), perform an active relocation to
1381 * propagate any addend.
1383 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) ||
1384 (oflags == FLG_REL_SCNNDX))
1385 if (ld_add_actrel(0, rsp, ofl) == S_ERROR)
1386 return (S_ERROR);
1388 return ((*ld_targ.t_mr.mr_add_outrel)(oflags, rsp, ofl));
1392 * Perform any generic TLS validations before passing control to machine
1393 * specific routines. At this point we know we are dealing with an executable
1394 * or shared object - relocatable objects have already been processed.
1396 static uintptr_t
1397 reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl)
1399 Word rtype = rsp->rel_rtype;
1400 ofl_flag_t flags = ofl->ofl_flags;
1401 Ifl_desc *ifl = rsp->rel_isdesc->is_file;
1402 Half mach = ifl->ifl_ehdr->e_machine;
1403 Sym_desc *sdp = rsp->rel_sym;
1404 unsigned char type;
1405 Conv_inv_buf_t inv_buf1, inv_buf2;
1408 * All TLS relocations are illegal in a static executable.
1410 if (OFL_IS_STATIC_EXEC(ofl)) {
1411 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_TLSSTAT),
1412 conv_reloc_type(mach, rtype, 0, &inv_buf1), ifl->ifl_name,
1413 ld_reloc_sym_name(rsp));
1414 return (S_ERROR);
1418 * Any TLS relocation must be against a STT_TLS symbol, all others
1419 * are illegal.
1421 if ((type = ELF_ST_TYPE(sdp->sd_sym->st_info)) != STT_TLS) {
1422 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_TLSBADSYM),
1423 conv_reloc_type(mach, rtype, 0, &inv_buf1), ifl->ifl_name,
1424 ld_reloc_sym_name(rsp),
1425 conv_sym_info_type(mach, type, 0, &inv_buf2));
1426 return (S_ERROR);
1430 * A dynamic executable can not use the LD or LE reference models to
1431 * reference an external symbol. A shared object can not use the LD
1432 * reference model to reference an external symbol.
1434 if (!local && (IS_TLS_LD(rtype) ||
1435 ((flags & FLG_OF_EXEC) && IS_TLS_LE(rtype)))) {
1436 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_TLSBND),
1437 conv_reloc_type(mach, rtype, 0, &inv_buf1), ifl->ifl_name,
1438 ld_reloc_sym_name(rsp), sdp->sd_file->ifl_name);
1439 return (S_ERROR);
1443 * The TLS LE model is only allowed for dynamic executables. The TLS IE
1444 * model is allowed for shared objects, but this model has restrictions.
1445 * This model can only be used freely in dependencies that are loaded
1446 * immediately as part of process initialization. However, during the
1447 * initial runtime handshake with libc that establishes the thread
1448 * pointer, a small backup TLS reservation is created. This area can
1449 * be used by objects that are loaded after threads are initialized.
1450 * However, this area is limited in size and may have already been
1451 * used. This area is intended for specialized applications, and does
1452 * not provide the degree of flexibility dynamic TLS can offer. Under
1453 * -z verbose indicate this restriction to the user.
1455 if ((flags & FLG_OF_EXEC) == 0) {
1456 if (IS_TLS_LE(rtype)) {
1457 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_TLSLE),
1458 conv_reloc_type(mach, rtype, 0, &inv_buf1),
1459 ifl->ifl_name, ld_reloc_sym_name(rsp));
1460 return (S_ERROR);
1462 } else if ((IS_TLS_IE(rtype)) &&
1463 (flags & FLG_OF_VERBOSE)) {
1464 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_TLSIE),
1465 conv_reloc_type(mach, rtype, 0, &inv_buf1),
1466 ifl->ifl_name, ld_reloc_sym_name(rsp));
1470 return ((*ld_targ.t_mr.mr_reloc_TLS)(local, rsp, ofl));
1473 uintptr_t
1474 ld_process_sym_reloc(Ofl_desc *ofl, Rel_desc *reld, Rel *reloc, Is_desc *isp,
1475 const char *isname, Word isscnndx)
1477 Word rtype = reld->rel_rtype;
1478 ofl_flag_t flags = ofl->ofl_flags;
1479 Sym_desc *sdp = reld->rel_sym;
1480 Sym_aux *sap;
1481 Boolean local;
1482 Conv_inv_buf_t inv_buf;
1484 DBG_CALL(Dbg_reloc_in(ofl->ofl_lml, ELF_DBG_LD, ld_targ.t_m.m_mach,
1485 ld_targ.t_m.m_rel_sht_type, (void *)reloc, isname, isscnndx,
1486 ld_reloc_sym_name(reld)));
1489 * Indicate this symbol is being used for relocation and therefore must
1490 * have its output address updated accordingly (refer to update_osym()).
1492 sdp->sd_flags |= FLG_SY_UPREQD;
1495 * Indicate the section this symbol is defined in has been referenced,
1496 * therefor it *is not* a candidate for elimination.
1498 if (sdp->sd_isc) {
1499 sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1500 sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1503 if (!ld_reloc_set_aux_usym(ofl, reld, sdp))
1504 return (S_ERROR);
1507 * Determine if this symbol is actually an alias to another symbol. If
1508 * so, and the alias is not REF_DYN_SEEN, set ra_usym to point to the
1509 * weak symbols strong counter-part. The one exception is if the
1510 * FLG_SY_MVTOCOMM flag is set on the weak symbol. If this is the case,
1511 * the strong is only here because of its promotion, and the weak symbol
1512 * should still be used for the relocation reference (see reloc_exec()).
1514 sap = sdp->sd_aux;
1515 if (sap && sap->sa_linkndx &&
1516 ((ELF_ST_BIND(sdp->sd_sym->st_info) == STB_WEAK) ||
1517 (sdp->sd_flags & FLG_SY_WEAKDEF)) &&
1518 (!(sdp->sd_flags & FLG_SY_MVTOCOMM))) {
1519 Sym_desc *_sdp;
1521 _sdp = sdp->sd_file->ifl_oldndx[sap->sa_linkndx];
1522 if ((_sdp->sd_ref != REF_DYN_SEEN) &&
1523 !ld_reloc_set_aux_usym(ofl, reld, _sdp))
1524 return (S_ERROR);
1528 * Determine whether this symbol should be bound locally or not.
1529 * Symbols are bound locally if one of the following is true:
1531 * - the symbol is of type STB_LOCAL.
1533 * - the output image is not a relocatable object and the relocation
1534 * is relative to the .got.
1536 * - the section being relocated is of type SHT_SUNW_dof. These
1537 * sections must be bound to the functions in the containing
1538 * object and can not be interposed upon.
1540 * - the symbol has been reduced (scoped to a local or symbolic) and
1541 * reductions are being processed.
1543 * - the -Bsymbolic flag is in use when building a shared object,
1544 * and the symbol hasn't explicitly been defined as nodirect.
1546 * - an executable is being created, and the symbol
1547 * is defined in the executable.
1549 * - the relocation is against a segment which will not be loaded
1550 * into memory. In this case, the relocation must be resolved
1551 * now, as ld.so.1 can not process relocations against unmapped
1552 * segments.
1554 local = FALSE;
1555 if (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL) {
1556 local = TRUE;
1557 } else if (!(reld->rel_flags & FLG_REL_LOAD)) {
1558 local = TRUE;
1559 } else if (sdp->sd_sym->st_shndx != SHN_UNDEF) {
1560 if (reld->rel_isdesc &&
1561 reld->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof) {
1562 local = TRUE;
1563 } else if (!(flags & FLG_OF_RELOBJ) &&
1564 (IS_LOCALBND(rtype) || IS_SEG_RELATIVE(rtype))) {
1565 local = TRUE;
1566 } else if ((sdp->sd_ref == REF_REL_NEED) &&
1567 ((sdp->sd_flags & FLG_SY_CAP) == 0)) {
1569 * Global symbols may have been individually reduced in
1570 * scope. If the whole object is to be self contained,
1571 * such as when generating an executable or a symbolic
1572 * shared object, make sure all relocation symbol
1573 * references (sections too) are treated locally. Note,
1574 * explicit no-direct symbols should not be bound to
1575 * locally.
1577 if ((sdp->sd_flags &
1578 (FLG_SY_HIDDEN | FLG_SY_PROTECT)))
1579 local = TRUE;
1580 else if ((flags & (FLG_OF_EXEC | FLG_OF_PIE)) ||
1581 ((flags & FLG_OF_SYMBOLIC) &&
1582 ((sdp->sd_flags & FLG_SY_NDIR) == 0))) {
1583 local = TRUE;
1589 * If this is a PC_RELATIVE relocation, the relocation could be
1590 * compromised if the relocated address is later used as a copy
1591 * relocated symbol (PSARC 1999/636, bugid 4187211). Scan the input
1592 * files symbol table to cross reference this relocation offset.
1594 if ((ofl->ofl_flags & (FLG_OF_SHAROBJ | FLG_OF_PIE)) &&
1595 IS_PC_RELATIVE(rtype) &&
1596 (IS_GOT_PC(rtype) == 0) &&
1597 (IS_PLT(rtype) == 0)) {
1598 if (disp_inspect(ofl, reld, local) == S_ERROR)
1599 return (S_ERROR);
1603 * GOT based relocations must bind to the object being built - since
1604 * they are relevant to the current GOT. If not building a relocatable
1605 * object - give a appropriate error message.
1607 if (!local && !(flags & FLG_OF_RELOBJ) &&
1608 IS_GOT_BASED(rtype)) {
1609 Ifl_desc *ifl = reld->rel_isdesc->is_file;
1611 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADGOTBASED),
1612 conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype,
1613 0, &inv_buf), ifl->ifl_name, demangle(sdp->sd_name));
1614 return (S_ERROR);
1618 * TLS symbols can only have TLS relocations.
1620 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_TLS) &&
1621 (IS_TLS_INS(rtype) == 0)) {
1623 * The above test is relaxed if the target section is
1624 * non-allocable.
1626 if (RELAUX_GET_OSDESC(reld)->os_shdr->sh_flags & SHF_ALLOC) {
1627 Ifl_desc *ifl = reld->rel_isdesc->is_file;
1629 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_BADTLS),
1630 conv_reloc_type(ifl->ifl_ehdr->e_machine,
1631 rtype, 0, &inv_buf), ifl->ifl_name,
1632 demangle(sdp->sd_name));
1633 return (S_ERROR);
1638 * Select the relocation to perform.
1640 if (IS_REGISTER(rtype)) {
1641 if (ld_targ.t_mr.mr_reloc_register == NULL) {
1642 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NOREG));
1643 return (S_ERROR);
1645 return ((*ld_targ.t_mr.mr_reloc_register)(reld, isp, ofl));
1648 if (flags & FLG_OF_RELOBJ)
1649 return (reloc_relobj(local, reld, ofl));
1651 if (IS_TLS_INS(rtype))
1652 return (reloc_TLS(local, reld, ofl));
1654 if (IS_GOT_OPINS(rtype)) {
1655 if (ld_targ.t_mr.mr_reloc_GOTOP == NULL) {
1656 assert(0);
1657 return (S_ERROR);
1659 return ((*ld_targ.t_mr.mr_reloc_GOTOP)(local, reld, ofl));
1662 if (IS_GOT_RELATIVE(rtype))
1663 return (ld_reloc_GOT_relative(local, reld, ofl));
1665 if (local)
1666 return ((*ld_targ.t_mr.mr_reloc_local)(reld, ofl));
1668 if ((IS_PLT(rtype) || ((sdp->sd_flags & FLG_SY_CAP) &&
1669 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_FUNC))) &&
1670 ((flags & FLG_OF_BFLAG) == 0))
1671 return (ld_reloc_plt(reld, ofl));
1673 if ((sdp->sd_ref == REF_REL_NEED) ||
1674 (flags & FLG_OF_BFLAG) ||
1675 (flags & (FLG_OF_SHAROBJ | FLG_OF_PIE)) ||
1676 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_NOTYPE))
1677 return ((*ld_targ.t_mr.mr_add_outrel)(0, reld, ofl));
1679 if (sdp->sd_ref == REF_DYN_NEED)
1680 return (reloc_exec(reld, ofl));
1683 * IS_NOT_REL(rtype)
1685 return (reloc_generic(reld, ofl));
1689 * Given a relocation that references a local symbol from a discarded COMDAT
1690 * section, replace the symbol with the corresponding symbol from the section
1691 * that was kept.
1693 * entry:
1694 * reld - Relocation
1695 * sdp - Symbol to be replaced. Must be a local symbol (STB_LOCAL).
1696 * reject - Address of variable to receive rejection code
1697 * if no replacement symbol is found.
1699 * exit:
1700 * Returns address of replacement symbol descriptor if one was
1701 * found, and NULL otherwise. The result is also cached in
1702 * ofl->ofl_sr_cache as an optimization to speed following calls
1703 * for the same value of sdp.
1705 * On success (non-NULL result), *reject is set to RLXREL_REJ_NONE.
1706 * On failure (NULL result), *reject is filled in with a code
1707 * describing the underlying reason.
1709 * note:
1710 * The word "COMDAT" is used to refer to actual COMDAT sections, COMDAT
1711 * groups tied together with an SHF_GROUP section, and .gnu.linkonce
1712 * sections which provide a simplified COMDAT requirement. COMDAT
1713 * sections are identified with the FLG_IS_COMDAT section flag.
1715 * In principle, this sort of sloppy relocation remapping is
1716 * a questionable practice. All self-referential sections should
1717 * be in a common SHF_GROUP so that they are all kept or removed
1718 * together. The problem is that there is no way to ensure that the
1719 * two sections are similar enough that the replacement section will
1720 * really supply the correct information. However, we see a couple of
1721 * situations where it is useful to do this: (1) Older Sun C compilers
1722 * generated DWARF sections that would refer to one of the COMDAT
1723 * sections, and (2) gcc, when its GNU linkonce COMDAT feature is enabled.
1724 * It turns out that the GNU ld does these sloppy remappings.
1726 * The GNU ld takes an approach that hard wires special section
1727 * names and treats them specially. We avoid that practice and
1728 * try to get the necessary work done relying only on the ELF
1729 * attributes of the sections and symbols involved. This means
1730 * that our heuristic is somewhat different than theirs, but the
1731 * end result is close enough to solve the same problem.
1733 * gcc is in the process of converting to SHF_GROUP. This will
1734 * eventually phase out the need for sloppy relocations, and
1735 * then this logic won't be needed. In the meantime, relaxed relocation
1736 * processing allows us to interoperate.
1738 static Sym_desc *
1739 sloppy_comdat_reloc(Ofl_desc *ofl, Rel_desc *reld, Sym_desc *sdp,
1740 Rlxrel_rej *reject)
1742 Is_desc *rep_isp;
1743 Sym *sym, *rep_sym;
1744 Is_desc *isp;
1745 Ifl_desc *ifl;
1746 Conv_inv_buf_t inv_buf;
1747 Word scnndx, symscnt;
1748 Sym_desc **oldndx, *rep_sdp;
1749 const char *is_name;
1753 * Sloppy relocations are never applied to .eh_frame or
1754 * .gcc_except_table sections. The entries in these sections
1755 * for discarded sections are better left uninitialized.
1757 * We match these sections by name, because on most platforms they
1758 * are SHT_PROGBITS, and cannot be identified otherwise. On amd64
1759 * architectures, .eh_frame is SHT_AMD64_UNWIND, but that is ambiguous
1760 * (.eh_frame_hdr is also SHT_AMD64_UNWIND), so we still match it by
1761 * name.
1763 is_name = reld->rel_isdesc->is_name;
1764 if (((is_name[1] == 'e') &&
1765 (strcmp(is_name, MSG_ORIG(MSG_SCN_EHFRAME)) == 0)) ||
1766 ((is_name[1] == 'g') &&
1767 (strcmp(is_name, MSG_ORIG(MSG_SCN_GCC_X_TBL)) == 0))) {
1768 *reject = RLXREL_REJ_TARGET;
1769 return (NULL);
1773 * If we looked up the same symbol on the previous call, we can
1774 * return the cached value.
1776 if (sdp == ofl->ofl_sr_cache.sr_osdp) {
1777 *reject = ofl->ofl_sr_cache.sr_rej;
1778 return (ofl->ofl_sr_cache.sr_rsdp);
1781 ofl->ofl_sr_cache.sr_osdp = sdp;
1782 sym = sdp->sd_sym;
1783 isp = sdp->sd_isc;
1784 ifl = sdp->sd_file;
1787 * When a COMDAT section is discarded in favor of another COMDAT
1788 * section, the replacement is recorded in its section descriptor
1789 * (is_comdatkeep). We must validate the replacement before using
1790 * it. The replacement section must:
1791 * - Not have been discarded
1792 * - Have the same size (*)
1793 * - Have the same section type
1794 * - Have the same SHF_GROUP flag setting (either on or off)
1795 * - Must be a COMDAT section of one form or the other.
1797 * (*) One might imagine that the replacement section could be
1798 * larger than the original, rather than the exact size. However,
1799 * we have verified that this is the same policy used by the GNU
1800 * ld. If the sections are not the same size, the chance of them
1801 * being interchangeable drops significantly.
1803 if (((rep_isp = isp->is_comdatkeep) == NULL) ||
1804 ((rep_isp->is_flags & FLG_IS_DISCARD) != 0) ||
1805 ((rep_isp->is_flags & FLG_IS_COMDAT) == 0) ||
1806 (isp->is_indata->d_size != rep_isp->is_indata->d_size) ||
1807 (isp->is_shdr->sh_type != rep_isp->is_shdr->sh_type) ||
1808 ((isp->is_shdr->sh_flags & SHF_GROUP) !=
1809 (rep_isp->is_shdr->sh_flags & SHF_GROUP))) {
1810 *reject = ofl->ofl_sr_cache.sr_rej = RLXREL_REJ_SECTION;
1811 return (ofl->ofl_sr_cache.sr_rsdp = NULL);
1815 * We found the kept COMDAT section. Now, look at all of the
1816 * symbols from the input file that contains it to find the
1817 * symbol that corresponds to the one we started with:
1818 * - Hasn't been discarded
1819 * - Has section index of kept section
1820 * - If one symbol has a name, the other must have
1821 * the same name. The st_name field of a symbol
1822 * is 0 if there is no name, and is a string
1823 * table offset otherwise. The string table
1824 * offsets may well not agree --- it is the
1825 * actual string that matters.
1826 * - Type and binding attributes match (st_info)
1827 * - Values match (st_value)
1828 * - Sizes match (st_size)
1829 * - Visibility matches (st_other)
1831 scnndx = rep_isp->is_scnndx;
1832 oldndx = rep_isp->is_file->ifl_oldndx;
1833 symscnt = rep_isp->is_file->ifl_symscnt;
1834 while (symscnt--) {
1835 rep_sdp = *oldndx++;
1836 if ((rep_sdp == NULL) || (rep_sdp->sd_flags & FLG_SY_ISDISC) ||
1837 ((rep_sym = rep_sdp->sd_sym)->st_shndx != scnndx) ||
1838 ((sym->st_name == 0) != (rep_sym->st_name == 0)) ||
1839 ((sym->st_name != 0) &&
1840 (strcmp(sdp->sd_name, rep_sdp->sd_name) != 0)) ||
1841 (sym->st_info != rep_sym->st_info) ||
1842 (sym->st_value != rep_sym->st_value) ||
1843 (sym->st_size != rep_sym->st_size) ||
1844 (sym->st_other != rep_sym->st_other))
1845 continue;
1848 if (ofl->ofl_flags & FLG_OF_VERBOSE) {
1849 if (sym->st_name != 0) {
1850 ld_eprintf(ofl, ERR_WARNING,
1851 MSG_INTL(MSG_REL_SLOPCDATNAM),
1852 conv_reloc_type(ifl->ifl_ehdr->e_machine,
1853 reld->rel_rtype, 0, &inv_buf),
1854 ifl->ifl_name,
1855 EC_WORD(reld->rel_isdesc->is_scnndx),
1856 reld->rel_isdesc->is_name,
1857 rep_sdp->sd_name,
1858 EC_WORD(isp->is_scnndx), isp->is_name,
1859 rep_sdp->sd_file->ifl_name);
1860 } else {
1861 ld_eprintf(ofl, ERR_WARNING,
1862 MSG_INTL(MSG_REL_SLOPCDATNONAM),
1863 conv_reloc_type(ifl->ifl_ehdr->e_machine,
1864 reld->rel_rtype, 0, &inv_buf),
1865 ifl->ifl_name,
1866 EC_WORD(reld->rel_isdesc->is_scnndx),
1867 reld->rel_isdesc->is_name,
1868 EC_WORD(isp->is_scnndx), isp->is_name,
1869 rep_sdp->sd_file->ifl_name);
1872 DBG_CALL(Dbg_reloc_sloppycomdat(ofl->ofl_lml, rep_sdp));
1873 *reject = ofl->ofl_sr_cache.sr_rej = RLXREL_REJ_NONE;
1874 return (ofl->ofl_sr_cache.sr_rsdp = rep_sdp);
1877 /* If didn't return above, we didn't find it */
1878 *reject = ofl->ofl_sr_cache.sr_rej = RLXREL_REJ_SYMBOL;
1879 return (ofl->ofl_sr_cache.sr_rsdp = NULL);
1883 * Generate relocation descriptor and dispatch
1885 static uintptr_t
1886 process_reld(Ofl_desc *ofl, Is_desc *isp, Rel_desc *reld, Word rsndx,
1887 Rel *reloc)
1889 Ifl_desc *ifl = isp->is_file;
1890 Word rtype = reld->rel_rtype;
1891 Sym_desc *sdp;
1892 Conv_inv_buf_t inv_buf;
1895 * Make sure the relocation is in the valid range.
1897 if (rtype >= ld_targ.t_m.m_r_num) {
1898 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_INVALRELT),
1899 ifl->ifl_name, EC_WORD(isp->is_scnndx), isp->is_name,
1900 rtype);
1901 return (S_ERROR);
1904 ofl->ofl_entrelscnt++;
1907 * Special case: a register symbol associated with symbol index 0 is
1908 * initialized (i.e., relocated) to a constant from the r_addend field
1909 * rather than from a symbol value.
1911 if (IS_REGISTER(rtype) && (rsndx == 0)) {
1912 reld->rel_sym = NULL;
1913 DBG_CALL(Dbg_reloc_in(ofl->ofl_lml, ELF_DBG_LD,
1914 ld_targ.t_m.m_mach, isp->is_shdr->sh_type,
1915 (void *)reloc, isp->is_name, isp->is_scnndx,
1916 ld_reloc_sym_name(reld)));
1917 if (ld_targ.t_mr.mr_reloc_register == NULL) {
1918 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NOREG));
1919 return (S_ERROR);
1921 return ((*ld_targ.t_mr.mr_reloc_register)(reld, isp, ofl));
1925 * If this is a STT_SECTION symbol, make sure the associated
1926 * section has a descriptive non-NULL is_sym_name field that can
1927 * be accessed by ld_reloc_sym_name() to satisfy debugging output
1928 * and errors.
1930 * In principle, we could add this string to every input section
1931 * as it is created, but we defer it until we see a relocation
1932 * symbol that might need it. Not every section will have such
1933 * a relocation, so we create fewer of them this way.
1935 sdp = reld->rel_sym = ifl->ifl_oldndx[rsndx];
1936 if ((sdp != NULL) &&
1937 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) &&
1938 (sdp->sd_isc != NULL) && (sdp->sd_isc->is_name != NULL) &&
1939 (sdp->sd_isc->is_sym_name == NULL) &&
1940 (ld_stt_section_sym_name(sdp->sd_isc) == NULL))
1941 return (S_ERROR);
1944 * If for some reason we have a null relocation record issue a
1945 * warning and continue (the compiler folks can get into this
1946 * state some time). Normal users should never see this error.
1948 if (rtype == ld_targ.t_m.m_r_none) {
1949 DBG_CALL(Dbg_reloc_in(ofl->ofl_lml, ELF_DBG_LD,
1950 ld_targ.t_m.m_mach, ld_targ.t_m.m_rel_sht_type,
1951 (void *)reloc, isp->is_name, isp->is_scnndx,
1952 ld_reloc_sym_name(reld)));
1953 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_REL_NULL),
1954 ifl->ifl_name, EC_WORD(isp->is_scnndx), isp->is_name);
1955 return (1);
1958 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
1959 IS_NOTSUP(rtype)) {
1960 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NOTSUP),
1961 conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype,
1962 0, &inv_buf), ifl->ifl_name, EC_WORD(isp->is_scnndx),
1963 isp->is_name);
1964 return (S_ERROR);
1968 * If we are here, we know that the relocation requires reference
1969 * symbol. If no symbol is assigned, this is a fatal error.
1971 if (sdp == NULL) {
1972 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_NOSYMBOL),
1973 conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype,
1974 0, &inv_buf), ifl->ifl_name, EC_WORD(isp->is_scnndx),
1975 isp->is_name, EC_XWORD(reloc->r_offset));
1976 return (S_ERROR);
1979 if (sdp->sd_flags & FLG_SY_IGNORE)
1980 return (1);
1983 * If this symbol is part of a DISCARDED section attempt to find another
1984 * definition.
1986 if (sdp->sd_flags & FLG_SY_ISDISC) {
1987 Sym_desc *nsdp = NULL;
1988 Rlxrel_rej reject;
1990 if (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL) {
1992 * If "-z relaxreloc", and the input section is COMDAT
1993 * that has been assigned to an output section, then
1994 * determine if this is a reference to a discarded
1995 * COMDAT section that can be replaced with a COMDAT
1996 * that has been kept.
1998 if ((ofl->ofl_flags1 & FLG_OF1_RLXREL) &&
1999 sdp->sd_isc->is_osdesc &&
2000 (sdp->sd_isc->is_flags & FLG_IS_COMDAT) &&
2001 ((nsdp = sloppy_comdat_reloc(ofl, reld,
2002 sdp, &reject)) == NULL)) {
2003 Shdr *is_shdr = reld->rel_isdesc->is_shdr;
2006 * A matching symbol was not found. We will
2007 * ignore this relocation. Determine whether
2008 * or not to issue a warning.
2009 * Warnings are always issued under -z verbose,
2010 * but otherwise, we will follow the lead of
2011 * the GNU ld and suppress them for certain
2012 * cases:
2014 * - It is a non-allocable debug section.
2015 * The GNU ld tests for these by name,
2016 * but we are willing to extend it to
2017 * any non-allocable section.
2018 * - The target section is excluded from
2019 * sloppy relocations by policy.
2021 if (((ofl->ofl_flags & FLG_OF_VERBOSE) != 0) ||
2022 ((is_shdr->sh_flags & SHF_ALLOC) &&
2023 (reject != RLXREL_REJ_TARGET)))
2024 ld_eprintf(ofl, ERR_WARNING,
2025 MSG_INTL(MSG_REL_SLOPCDATNOSYM),
2026 conv_reloc_type(
2027 ifl->ifl_ehdr->e_machine,
2028 reld->rel_rtype, 0, &inv_buf),
2029 ifl->ifl_name,
2030 EC_WORD(isp->is_scnndx),
2031 isp->is_name,
2032 ld_reloc_sym_name(reld),
2033 EC_WORD(sdp->sd_isc->is_scnndx),
2034 sdp->sd_isc->is_name);
2035 return (1);
2037 } else if ((sdp != NULL) && sdp->sd_name && *sdp->sd_name)
2038 nsdp = ld_sym_find(sdp->sd_name, SYM_NOHASH, NULL, ofl);
2040 if (nsdp == NULL) {
2041 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_SYMDISC),
2042 conv_reloc_type(ifl->ifl_ehdr->e_machine,
2043 reld->rel_rtype, 0, &inv_buf), ifl->ifl_name,
2044 EC_WORD(isp->is_scnndx), isp->is_name,
2045 ld_reloc_sym_name(reld),
2046 EC_WORD(sdp->sd_isc->is_scnndx),
2047 sdp->sd_isc->is_name);
2048 return (S_ERROR);
2050 ifl->ifl_oldndx[rsndx] = sdp = nsdp;
2051 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) &&
2052 (sdp->sd_isc != NULL) && (sdp->sd_isc->is_name != NULL) &&
2053 (sdp->sd_isc->is_sym_name == NULL) &&
2054 (ld_stt_section_sym_name(sdp->sd_isc) == NULL))
2055 return (S_ERROR);
2059 * If this is a global symbol, determine whether its visibility needs
2060 * adjusting.
2062 if (sdp->sd_aux && ((sdp->sd_flags & FLG_SY_VISIBLE) == 0))
2063 ld_sym_adjust_vis(sdp, ofl);
2066 * Ignore any relocation against a section that will not be in the
2067 * output file (has been stripped).
2069 if ((sdp->sd_isc == 0) &&
2070 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))
2071 return (1);
2074 * If the input section exists, but the section has not been associated
2075 * to an output section, then this is a little suspicious.
2077 if (sdp->sd_isc && (sdp->sd_isc->is_osdesc == 0) &&
2078 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
2079 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_RELINVSEC),
2080 conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype,
2081 0, &inv_buf), ifl->ifl_name, EC_WORD(isp->is_scnndx),
2082 isp->is_name, EC_WORD(sdp->sd_isc->is_scnndx),
2083 sdp->sd_isc->is_name);
2084 return (1);
2088 * If the symbol for this relocation is invalid (which should have
2089 * generated a message during symbol processing), or the relocation
2090 * record's symbol reference is in any other way invalid, then it's
2091 * about time we gave up.
2093 if ((sdp->sd_flags & FLG_SY_INVALID) || (rsndx == 0) ||
2094 (rsndx >= ifl->ifl_symscnt)) {
2095 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_UNKNWSYM),
2096 conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype,
2097 0, &inv_buf), ifl->ifl_name, EC_WORD(isp->is_scnndx),
2098 isp->is_name, ld_reloc_sym_name(reld),
2099 EC_XWORD(reloc->r_offset), EC_WORD(rsndx));
2100 return (S_ERROR);
2104 * Size relocations against section symbols are presently unsupported.
2105 * There is a question as to whether the input section size, or output
2106 * section size would be used. Until an explicit requirement is
2107 * established for either case, we'll punt.
2109 if (IS_SIZE(rtype) &&
2110 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) {
2111 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_UNSUPSIZE),
2112 conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype,
2113 0, &inv_buf), ifl->ifl_name, EC_WORD(isp->is_scnndx),
2114 isp->is_name);
2115 return (S_ERROR);
2118 reld->rel_sym = sdp;
2119 if (reld->rel_aux)
2120 reld->rel_aux->ra_usym = sdp;
2121 return (ld_process_sym_reloc(ofl, reld, reloc, isp, isp->is_name,
2122 isp->is_scnndx));
2125 static uintptr_t
2126 reloc_section(Ofl_desc *ofl, Is_desc *isect, Is_desc *rsect, Os_desc *osect)
2128 Rel *rend; /* end of relocation section data */
2129 Rel *reloc; /* current relocation entry */
2130 Xword rsize; /* size of relocation section data */
2131 Xword entsize; /* size of relocation entry */
2132 Rel_desc reld; /* relocation descriptor */
2133 Rel_aux rel_aux;
2134 Shdr * shdr;
2135 Word flags = 0;
2136 uintptr_t ret = 1;
2138 shdr = rsect->is_shdr;
2139 rsize = shdr->sh_size;
2140 reloc = (Rel *)rsect->is_indata->d_buf;
2143 * Decide entry size.
2145 if (((entsize = shdr->sh_entsize) == 0) || (entsize > rsize)) {
2146 if (shdr->sh_type == SHT_RELA)
2147 entsize = sizeof (Rela);
2148 else
2149 entsize = sizeof (Rel);
2153 * Build up the basic information in for the Rel_desc structure.
2155 reld.rel_isdesc = isect;
2156 reld.rel_aux = &rel_aux;
2157 ld_init_rel_aux(&reld);
2158 rel_aux.ra_osdesc = osect;
2160 if ((ofl->ofl_flags & FLG_OF_RELOBJ) ||
2161 (osect && (osect->os_sgdesc->sg_phdr.p_type == PT_LOAD)))
2162 flags |= FLG_REL_LOAD;
2164 if (shdr->sh_info == 0)
2165 flags |= FLG_REL_NOINFO;
2167 DBG_CALL(Dbg_reloc_proc(ofl->ofl_lml, osect, isect, rsect));
2169 for (rend = (Rel *)((uintptr_t)reloc + (uintptr_t)rsize);
2170 reloc < rend;
2171 reloc = (Rel *)((uintptr_t)reloc + (uintptr_t)entsize)) {
2172 Word rsndx;
2175 * Initialize the relocation record information and process
2176 * the individual relocation. Reinitialize the flags to
2177 * insure we don't carry any state over from the previous
2178 * relocation records processing.
2180 reld.rel_flags = flags;
2181 rsndx = (*ld_targ.t_mr.mr_init_rel)(&reld,
2182 &rel_aux.ra_typedata, (void *)reloc);
2185 * Determine whether or not to pass an auxiliary block
2186 * in with this Rel_desc. It is not needed if both the
2187 * osdesc and typedata fields have default values.
2189 reld.rel_aux =
2190 (RELAUX_ISDEFAULT_OSDESC(&reld, rel_aux.ra_osdesc) &&
2191 RELAUX_ISDEFAULT_TYPEDATA(&reld, rel_aux.ra_typedata)) ?
2192 NULL : &rel_aux;
2194 if (process_reld(ofl, rsect, &reld, rsndx, reloc) == S_ERROR)
2195 ret = S_ERROR;
2197 return (ret);
2200 static uintptr_t
2201 reloc_segments(int wr_flag, Ofl_desc *ofl)
2203 Aliste idx1;
2204 Sg_desc *sgp;
2205 Is_desc *isp;
2207 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
2208 Os_desc *osp;
2209 Aliste idx2;
2211 if ((sgp->sg_phdr.p_flags & PF_W) != wr_flag)
2212 continue;
2214 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
2215 Is_desc *risp;
2216 Aliste idx3;
2218 osp->os_szoutrels = 0;
2219 for (APLIST_TRAVERSE(osp->os_relisdescs, idx3, risp)) {
2220 Word indx;
2223 * Determine the input section that this
2224 * relocation information refers to.
2226 indx = risp->is_shdr->sh_info;
2227 isp = risp->is_file->ifl_isdesc[indx];
2230 * Do not process relocations against sections
2231 * which are being discarded (COMDAT)
2233 if (isp->is_flags & FLG_IS_DISCARD)
2234 continue;
2236 if (reloc_section(ofl, isp, risp, osp) ==
2237 S_ERROR)
2238 return (S_ERROR);
2242 * Check for relocations against non-writable
2243 * allocatable sections.
2245 if (osp->os_szoutrels &&
2246 (sgp->sg_phdr.p_type == PT_LOAD) &&
2247 ((sgp->sg_phdr.p_flags & PF_W) == 0)) {
2248 ofl->ofl_flags |= FLG_OF_TEXTREL;
2249 ofl->ofl_dtflags |= DF_TEXTREL;
2254 return (1);
2258 * Move Section related function
2259 * Get move entry
2261 static Move *
2262 get_move_entry(Is_desc *rsect, Xword roffset)
2264 Ifl_desc *ifile = rsect->is_file;
2265 Shdr *rshdr = rsect->is_shdr;
2266 Is_desc *misp;
2267 Shdr *mshdr;
2268 Xword midx;
2269 Move *mvp;
2272 * Set info for the target move section
2274 misp = ifile->ifl_isdesc[rshdr->sh_info];
2275 mshdr = misp->is_shdr;
2277 if (mshdr->sh_entsize == 0)
2278 return (NULL);
2281 * If this is an invalid entry, return NULL.
2283 midx = roffset / mshdr->sh_entsize;
2284 if ((midx * mshdr->sh_entsize) >= mshdr->sh_size)
2285 return (NULL);
2287 mvp = (Move *)misp->is_indata->d_buf;
2288 mvp += midx;
2289 return (mvp);
2293 * Relocation against Move Table.
2295 static uintptr_t
2296 process_movereloc(Ofl_desc *ofl, Is_desc *rsect)
2298 Ifl_desc *file = rsect->is_file;
2299 Rel *rend, *reloc;
2300 Xword rsize, entsize;
2301 Rel_desc reld;
2302 Rel_aux rel_aux;
2304 rsize = rsect->is_shdr->sh_size;
2305 reloc = (Rel *)rsect->is_indata->d_buf;
2308 * Decide entry size.
2310 entsize = rsect->is_shdr->sh_entsize;
2311 if ((entsize == 0) ||
2312 (entsize > rsect->is_shdr->sh_size)) {
2313 if (rsect->is_shdr->sh_type == SHT_RELA)
2314 entsize = sizeof (Rela);
2315 else
2316 entsize = sizeof (Rel);
2320 * The requirement for move data ensures that we have to supply a
2321 * Rel_aux auxiliary block.
2323 reld.rel_aux = &rel_aux;
2324 ld_init_rel_aux(&reld);
2327 * Go through the relocation entries.
2329 for (rend = (Rel *)((uintptr_t)reloc + (uintptr_t)rsize);
2330 reloc < rend;
2331 reloc = (Rel *)((uintptr_t)reloc + (uintptr_t)entsize)) {
2332 Sym_desc *psdp;
2333 Move *mvp;
2334 Word rsndx;
2337 * Initialize the relocation record information.
2339 reld.rel_flags = FLG_REL_LOAD;
2340 rsndx = (*ld_targ.t_mr.mr_init_rel)(&reld,
2341 &rel_aux.ra_typedata, (void *)reloc);
2343 if (((mvp = get_move_entry(rsect, reloc->r_offset)) == NULL) ||
2344 ((rel_aux.ra_move =
2345 libld_malloc(sizeof (Mv_reloc))) == NULL))
2346 return (S_ERROR);
2348 psdp = file->ifl_oldndx[ELF_M_SYM(mvp->m_info)];
2349 rel_aux.ra_move->mr_move = mvp;
2350 rel_aux.ra_move->mr_sym = psdp;
2352 if (psdp->sd_flags & FLG_SY_PAREXPN) {
2353 int _num, num = mvp->m_repeat;
2355 rel_aux.ra_osdesc = ofl->ofl_isparexpn->is_osdesc;
2356 reld.rel_isdesc = ofl->ofl_isparexpn;
2357 reld.rel_roffset = mvp->m_poffset;
2359 for (_num = 0; _num < num; _num++) {
2360 reld.rel_roffset +=
2361 /* LINTED */
2362 (_num * ELF_M_SIZE(mvp->m_info));
2365 * Generate Reld
2367 if (process_reld(ofl,
2368 rsect, &reld, rsndx, reloc) == S_ERROR)
2369 return (S_ERROR);
2371 } else {
2373 * Generate Reld
2375 reld.rel_flags |= FLG_REL_MOVETAB;
2376 rel_aux.ra_osdesc = ofl->ofl_osmove;
2377 reld.rel_isdesc = ld_os_first_isdesc(ofl->ofl_osmove);
2379 if (process_reld(ofl,
2380 rsect, &reld, rsndx, reloc) == S_ERROR)
2381 return (S_ERROR);
2384 return (1);
2388 * This function is similar to reloc_init().
2390 * This function is called when the SHT_SUNW_move table is expanded and there
2391 * are relocations against the SHT_SUNW_move section.
2393 static uintptr_t
2394 reloc_movesections(Ofl_desc *ofl)
2396 Aliste idx;
2397 Is_desc *risp;
2398 uintptr_t ret = 1;
2401 * Generate/Expand relocation entries
2403 for (APLIST_TRAVERSE(ofl->ofl_ismoverel, idx, risp)) {
2404 if (process_movereloc(ofl, risp) == S_ERROR)
2405 ret = S_ERROR;
2408 return (ret);
2412 * Count the number of output relocation entries, global offset table entries,
2413 * and procedure linkage table entries. This function searches the segment and
2414 * outsect lists and passes each input reloc section to process_reloc().
2415 * It allocates space for any output relocations needed. And builds up
2416 * the relocation structures for later processing.
2418 uintptr_t
2419 ld_reloc_init(Ofl_desc *ofl)
2421 Aliste idx;
2422 Is_desc *isp;
2423 Sym_desc *sdp;
2425 DBG_CALL(Dbg_basic_collect(ofl->ofl_lml));
2428 * At this point we have finished processing all input symbols. Make
2429 * sure we add any absolute (internal) symbols before continuing with
2430 * any relocation processing.
2432 if (ld_sym_spec(ofl) == S_ERROR)
2433 return (S_ERROR);
2435 ofl->ofl_gotcnt = ld_targ.t_m.m_got_xnumber;
2438 * Process all of the relocations against NON-writable segments
2439 * followed by relocations against the writable segments.
2441 * This separation is so that when the writable segments are processed
2442 * we know whether or not a COPYRELOC will be produced for any symbols.
2443 * If relocations aren't processed in this order, a COPYRELOC and a
2444 * regular relocation can be produced against the same symbol. The
2445 * regular relocation would be redundant.
2447 if (reloc_segments(0, ofl) == S_ERROR)
2448 return (S_ERROR);
2450 if (reloc_segments(PF_W, ofl) == S_ERROR)
2451 return (S_ERROR);
2454 * Process any extra relocations. These are relocation sections that
2455 * have a NULL sh_info.
2457 for (APLIST_TRAVERSE(ofl->ofl_extrarels, idx, isp)) {
2458 if (reloc_section(ofl, NULL, isp, NULL) == S_ERROR)
2459 return (S_ERROR);
2463 * If there were relocation against move table,
2464 * process the relocation sections.
2466 if (reloc_movesections(ofl) == S_ERROR)
2467 return (S_ERROR);
2470 * Now all the relocations are pre-processed,
2471 * check the validity of copy relocations.
2473 if (ofl->ofl_copyrels) {
2474 Copy_rel *crp;
2476 for (ALIST_TRAVERSE(ofl->ofl_copyrels, idx, crp)) {
2478 * If there were no displacement relocation
2479 * in this file, don't worry about it.
2481 if (crp->c_sdp->sd_file->ifl_flags &
2482 (FLG_IF_DISPPEND | FLG_IF_DISPDONE))
2483 is_disp_copied(ofl, crp);
2488 * GOT sections are created for dynamic executables and shared objects
2489 * if the FLG_OF_BLDGOT is set, or explicit reference has been made to
2490 * a GOT symbol.
2492 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
2493 ((ofl->ofl_flags & FLG_OF_BLDGOT) ||
2494 ((((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL),
2495 SYM_NOHASH, NULL, ofl)) != NULL) ||
2496 ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U),
2497 SYM_NOHASH, NULL, ofl)) != NULL)) &&
2498 (sdp->sd_ref != REF_DYN_SEEN)))) {
2499 if (ld_make_got(ofl) == S_ERROR)
2500 return (S_ERROR);
2502 /* Allocate the GOT if required by target */
2503 if ((ld_targ.t_mr.mr_allocate_got != NULL) &&
2504 ((*ld_targ.t_mr.mr_allocate_got)(ofl) == S_ERROR))
2505 return (S_ERROR);
2508 return (1);
2512 * Simple comparison routine to be used by qsort() for
2513 * the sorting of the output relocation list.
2515 * The reloc_compare() routine results in a relocation
2516 * table which is located on:
2518 * file referenced (NEEDED NDX)
2519 * referenced symbol
2520 * relocation offset
2522 * This provides the most efficient traversal of the relocation
2523 * table at run-time.
2525 static int
2526 reloc_compare(Reloc_list *i, Reloc_list *j)
2530 * first - sort on neededndx
2532 if (i->rl_key1 > j->rl_key1)
2533 return (1);
2534 if (i->rl_key1 < j->rl_key1)
2535 return (-1);
2538 * Then sort on symbol
2540 if ((uintptr_t)i->rl_key2 > (uintptr_t)j->rl_key2)
2541 return (1);
2542 if ((uintptr_t)i->rl_key2 < (uintptr_t)j->rl_key2)
2543 return (-1);
2546 * i->key2 == j->key2
2548 * At this point we fall back to key2 (offsets) to
2549 * sort the output relocations. Ideally this will
2550 * make for the most efficient processing of these
2551 * relocations at run-time.
2553 if (i->rl_key3 > j->rl_key3)
2554 return (1);
2555 if (i->rl_key3 < j->rl_key3)
2556 return (-1);
2557 return (0);
2560 static uintptr_t
2561 do_sorted_outrelocs(Ofl_desc *ofl)
2563 Rel_desc *orsp;
2564 Rel_cachebuf *rcbp;
2565 Aliste idx;
2566 Reloc_list *sorted_list;
2567 Word index = 0;
2568 int debug = 0;
2569 uintptr_t error = 1;
2570 Boolean remain_seen = FALSE;
2572 if ((sorted_list = libld_malloc((size_t)(sizeof (Reloc_list) *
2573 ofl->ofl_reloccnt))) == NULL)
2574 return (S_ERROR);
2577 * All but the PLT output relocations are sorted in the output file
2578 * based upon their sym_desc. By doing this multiple relocations
2579 * against the same symbol are grouped together, thus when the object
2580 * is later relocated by ld.so.1 it will take advantage of the symbol
2581 * cache that ld.so.1 has. This can significantly reduce the runtime
2582 * relocation cost of a dynamic object.
2584 * PLT relocations are not sorted because the order of the PLT
2585 * relocations is used by ld.so.1 to determine what symbol a PLT
2586 * relocation is against.
2588 REL_CACHE_TRAVERSE(&ofl->ofl_outrels, idx, rcbp, orsp) {
2589 if (debug == 0) {
2590 DBG_CALL(Dbg_reloc_dooutrel(ofl->ofl_lml,
2591 ld_targ.t_m.m_rel_sht_type));
2592 debug = 1;
2596 * If it's a PLT relocation we output it now in the
2597 * order that it was originally processed.
2599 if (orsp->rel_flags & FLG_REL_PLT) {
2600 if ((*ld_targ.t_mr.mr_perform_outreloc)
2601 (orsp, ofl, &remain_seen) == S_ERROR)
2602 error = S_ERROR;
2603 continue;
2606 if ((orsp->rel_rtype == ld_targ.t_m.m_r_relative) ||
2607 (orsp->rel_rtype == ld_targ.t_m.m_r_register)) {
2608 sorted_list[index].rl_key1 = 0;
2609 sorted_list[index].rl_key2 =
2610 /* LINTED */
2611 (Sym_desc *)(uintptr_t)orsp->rel_rtype;
2612 } else {
2613 sorted_list[index].rl_key1 =
2614 orsp->rel_sym->sd_file->ifl_neededndx;
2615 sorted_list[index].rl_key2 = orsp->rel_sym;
2618 if (orsp->rel_flags & FLG_REL_GOT) {
2619 sorted_list[index].rl_key3 =
2620 (*ld_targ.t_mr.mr_calc_got_offset)(orsp, ofl);
2621 } else {
2622 if (orsp->rel_rtype == ld_targ.t_m.m_r_register) {
2623 sorted_list[index].rl_key3 = 0;
2624 } else {
2625 sorted_list[index].rl_key3 = orsp->rel_roffset +
2626 (Xword)_elf_getxoff(orsp->
2627 rel_isdesc->is_indata) +
2628 orsp->rel_isdesc->is_osdesc->
2629 os_shdr->sh_addr;
2633 sorted_list[index++].rl_rsp = orsp;
2636 qsort(sorted_list, (size_t)ofl->ofl_reloccnt, sizeof (Reloc_list),
2637 (int (*)(const void *, const void *))reloc_compare);
2640 * All output relocations have now been sorted, go through
2641 * and process each relocation.
2643 for (index = 0; index < ofl->ofl_reloccnt; index++) {
2644 if ((*ld_targ.t_mr.mr_perform_outreloc)
2645 (sorted_list[index].rl_rsp, ofl, &remain_seen) == S_ERROR)
2646 error = S_ERROR;
2649 /* Guidance: Use -z text when building shared objects */
2650 if (remain_seen && OFL_GUIDANCE(ofl, FLG_OFG_NO_TEXT))
2651 ld_eprintf(ofl, ERR_GUIDANCE, MSG_INTL(MSG_GUIDE_TEXT));
2653 return (error);
2657 * Process relocations. Finds every input relocation section for each output
2658 * section and invokes reloc_section() to relocate that section.
2660 uintptr_t
2661 ld_reloc_process(Ofl_desc *ofl)
2663 Sg_desc *sgp;
2664 Os_desc *osp;
2665 Word ndx = 0;
2666 ofl_flag_t flags = ofl->ofl_flags;
2667 Shdr *shdr;
2669 DBG_CALL(Dbg_basic_relocate(ofl->ofl_lml));
2672 * Determine the index of the symbol table that will be referenced by
2673 * the relocation entries.
2675 if (OFL_ALLOW_DYNSYM(ofl))
2676 /* LINTED */
2677 ndx = (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
2678 else if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ))
2679 /* LINTED */
2680 ndx = (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
2683 * Re-initialize counters. These are used to provide relocation
2684 * offsets within the output buffers.
2686 ofl->ofl_relocpltsz = 0;
2687 ofl->ofl_relocgotsz = 0;
2688 ofl->ofl_relocbsssz = 0;
2691 * Now that the output file is created and symbol update has occurred,
2692 * process the relocations collected in process_reloc().
2694 if (do_sorted_outrelocs(ofl) == S_ERROR)
2695 return (S_ERROR);
2697 if ((*ld_targ.t_mr.mr_do_activerelocs)(ofl) == S_ERROR)
2698 return (S_ERROR);
2700 if ((flags & FLG_OF_COMREL) == 0) {
2701 Aliste idx1;
2704 * Process the relocation sections. For each relocation
2705 * section generated for the output image update its shdr
2706 * information to reflect the symbol table it needs (sh_link)
2707 * and the section to which the relocation must be applied
2708 * (sh_info).
2710 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
2711 Os_desc *osp;
2712 Aliste idx2;
2714 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
2715 if (osp->os_relosdesc == 0)
2716 continue;
2718 shdr = osp->os_relosdesc->os_shdr;
2719 shdr->sh_link = ndx;
2720 /* LINTED */
2721 shdr->sh_info = (Word)elf_ndxscn(osp->os_scn);
2726 * Since the .rel[a] section is not tied to any specific
2727 * section, we'd not have found it above.
2729 if ((osp = ofl->ofl_osrel) != NULL) {
2730 shdr = osp->os_shdr;
2731 shdr->sh_link = ndx;
2732 shdr->sh_info = 0;
2734 } else {
2736 * We only have two relocation sections here, (PLT's,
2737 * coalesced) so just hit them directly instead of stepping
2738 * over the output sections.
2740 if ((osp = ofl->ofl_osrelhead) != NULL) {
2741 shdr = osp->os_shdr;
2742 shdr->sh_link = ndx;
2743 shdr->sh_info = 0;
2745 if (((osp = ofl->ofl_osplt) != NULL) && osp->os_relosdesc) {
2746 shdr = osp->os_relosdesc->os_shdr;
2747 shdr->sh_link = ndx;
2748 /* LINTED */
2749 shdr->sh_info = (Word)elf_ndxscn(osp->os_scn);
2754 * If the -z text option was given, and we have output relocations
2755 * against a non-writable, allocatable section, issue a diagnostic and
2756 * return (the actual entries that caused this error would have been
2757 * output during the relocating section phase).
2759 if ((flags & (FLG_OF_PURETXT | FLG_OF_TEXTREL)) ==
2760 (FLG_OF_PURETXT | FLG_OF_TEXTREL)) {
2761 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_REMAIN_3));
2762 return (S_ERROR);
2766 * Finally, initialize the first got entry with the address of the
2767 * .dynamic section (_DYNAMIC).
2769 if (flags & FLG_OF_DYNAMIC) {
2770 if ((*ld_targ.t_mr.mr_fillin_gotplt)(ofl) == S_ERROR)
2771 return (S_ERROR);
2775 * Now that any GOT information has been written, display the debugging
2776 * information if required.
2778 if ((osp = ofl->ofl_osgot) != NULL)
2779 DBG_CALL(Dbg_got_display(ofl, osp->os_shdr->sh_addr, 1,
2780 ld_targ.t_m.m_got_xnumber, ld_targ.t_m.m_got_entsize));
2782 return (1);
2786 * If the -z text option was given, and we have output relocations against a
2787 * non-writable, allocatable section, issue a diagnostic. Print offending
2788 * symbols in tabular form similar to the way undefined symbols are presented.
2789 * Called from reloc_count(). The actual fatal error condition is triggered on
2790 * in reloc_process() above.
2792 * Note. For historic reasons -ztext is not a default option (however all OS
2793 * shared object builds use this option). It can be argued that this option
2794 * should also be default when generating an a.out (see 1163979). However, if
2795 * an a.out contains text relocations it is either because the user is creating
2796 * something pretty weird (they've used the -b or -znodefs options), or because
2797 * the library against which they're building wasn't constructed correctly (ie.
2798 * a function has a NOTYPE type, in which case the a.out won't generate an
2799 * associated plt). In the latter case the builder of the a.out can't do
2800 * anything to fix the error - thus we've chosen not to give the user an error,
2801 * or warning, for this case.
2803 void
2804 ld_reloc_remain_entry(Rel_desc *orsp, Os_desc *osp, Ofl_desc *ofl,
2805 Boolean *remain_seen)
2809 * -ztextoff
2811 if (ofl->ofl_flags1 & FLG_OF1_TEXTOFF)
2812 return;
2815 * Only give relocation errors against loadable read-only segments.
2817 if ((orsp->rel_rtype == ld_targ.t_m.m_r_register) || (!osp) ||
2818 (osp->os_sgdesc->sg_phdr.p_type != PT_LOAD) ||
2819 (osp->os_sgdesc->sg_phdr.p_flags & PF_W))
2820 return;
2823 * If we are in -ztextwarn mode, it's a silent error if a relocation is
2824 * due to a 'WEAK REFERENCE'. This is because if the symbol is not
2825 * provided at run-time we will not perform a text-relocation.
2827 if (((ofl->ofl_flags & FLG_OF_PURETXT) == 0) &&
2828 (ELF_ST_BIND(orsp->rel_sym->sd_sym->st_info) == STB_WEAK) &&
2829 (orsp->rel_sym->sd_sym->st_shndx == SHN_UNDEF))
2830 return;
2832 if (*remain_seen == FALSE) {
2834 * If building with '-ztext' then emit a fatal error. If
2835 * building a executable then only emit a 'warning'.
2837 const char *str1 = (ofl->ofl_flags & FLG_OF_PURETXT) ?
2838 MSG_INTL(MSG_REL_RMN_ITM_11) : MSG_INTL(MSG_REL_RMN_ITM_13);
2840 ld_eprintf(ofl, ERR_NONE, MSG_INTL(MSG_REL_REMAIN_FMT_1), str1,
2841 MSG_INTL(MSG_REL_RMN_ITM_31), MSG_INTL(MSG_REL_RMN_ITM_12),
2842 MSG_INTL(MSG_REL_RMN_ITM_2), MSG_INTL(MSG_REL_RMN_ITM_32));
2844 *remain_seen = TRUE;
2847 ld_eprintf(ofl, ERR_NONE, MSG_INTL(MSG_REL_REMAIN_2),
2848 ld_reloc_sym_name(orsp), EC_OFF(orsp->rel_roffset),
2849 orsp->rel_isdesc->is_file->ifl_name);
2853 * Generic encapsulation for generating a TLS got index.
2855 uintptr_t
2856 ld_assign_got_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl, Sym_desc *sdp,
2857 Gotndx *gnp, Gotref gref, Word rflag, Word ortype, Word rtype1, Word rtype2)
2859 Word rflags;
2861 if ((*ld_targ.t_mr.mr_assign_got_ndx)(&(sdp->sd_GOTndxs), gnp,
2862 gref, ofl, rsp, sdp) == S_ERROR)
2863 return (S_ERROR);
2865 rflags = FLG_REL_GOT | rflag;
2866 if (local)
2867 rflags |= FLG_REL_SCNNDX;
2868 rsp->rel_rtype = rtype1;
2870 if ((*ld_targ.t_mr.mr_add_outrel)(rflags, rsp, ofl) == S_ERROR)
2871 return (S_ERROR);
2873 if (local && (gref == GOT_REF_TLSIE)) {
2875 * If this is a local LE TLS symbol, then the symbol won't be
2876 * available at runtime. The value of the local symbol will
2877 * be placed in the associated got entry, and the got
2878 * relocation is reassigned to a section symbol.
2880 if (ld_add_actrel(rflags, rsp, ofl) == S_ERROR)
2881 return (S_ERROR);
2884 if (rtype2) {
2885 rflags = FLG_REL_GOT | rflag;
2886 rsp->rel_rtype = rtype2;
2888 if (local) {
2889 if (ld_add_actrel(rflags, rsp, ofl) == S_ERROR)
2890 return (S_ERROR);
2891 } else {
2892 if ((*ld_targ.t_mr.mr_add_outrel)(rflags, rsp, ofl) ==
2893 S_ERROR)
2894 return (S_ERROR);
2898 rsp->rel_rtype = ortype;
2900 return (1);
2904 * Move Section related function
2906 static void
2907 newroffset_for_move(Sym_desc *sdp, Move *mvp, Xword offset1, Xword *offset2)
2909 Mv_desc *mdp;
2910 Aliste idx;
2913 * Search for matching move entry.
2915 for (ALIST_TRAVERSE(sdp->sd_move, idx, mdp)) {
2916 if (mdp->md_move == mvp) {
2918 * Update r_offset
2920 *offset2 = (Xword)((mdp->md_oidx - 1) * sizeof (Move) +
2921 offset1 % sizeof (Move));
2922 return;
2927 void
2928 ld_adj_movereloc(Ofl_desc *ofl, Rel_desc *arsp)
2930 Move *move = arsp->rel_aux->ra_move->mr_move;
2931 Sym_desc *psdp = arsp->rel_aux->ra_move->mr_sym;
2932 Xword newoffset;
2934 if (arsp->rel_flags & FLG_REL_MOVETAB) {
2936 * We are relocating the move table itself.
2938 newroffset_for_move(psdp, move, arsp->rel_roffset,
2939 &newoffset);
2940 DBG_CALL(Dbg_move_adjmovereloc(ofl->ofl_lml, arsp->rel_roffset,
2941 newoffset, psdp->sd_name));
2942 arsp->rel_roffset = newoffset;
2943 } else {
2945 * We are expanding the partial symbol. So we are generating
2946 * the relocation entry relocating the expanded partial symbol.
2948 arsp->rel_roffset += psdp->sd_sym->st_value -
2949 ofl->ofl_isparexpn->is_osdesc->os_shdr->sh_addr;
2950 DBG_CALL(Dbg_move_adjexpandreloc(ofl->ofl_lml,
2951 arsp->rel_roffset, psdp->sd_name));
2956 * Partially Initialized Symbol Handling routines
2957 * For RELA architecture, the second argument is reld->rel_raddend. For REL
2958 * architecure, the second argument is the value stored at the relocation
2959 * target address.
2961 Sym_desc *
2962 ld_am_I_partial(Rel_desc *reld, Xword val)
2964 Ifl_desc *ifile = reld->rel_sym->sd_isc->is_file;
2965 int nlocs = ifile->ifl_locscnt, i;
2967 for (i = 1; i < nlocs; i++) {
2968 Sym *osym;
2969 Sym_desc *symd = ifile->ifl_oldndx[i];
2971 if ((osym = symd->sd_osym) == 0)
2972 continue;
2973 if ((symd->sd_flags & FLG_SY_PAREXPN) == 0)
2974 continue;
2975 if ((osym->st_value <= val) &&
2976 (osym->st_value + osym->st_size > val))
2977 return (symd);
2979 return (NULL);
2983 * Return True (1) if the code processing the given relocation
2984 * needs to perform byte swapping when accessing the section data.
2987 ld_swap_reloc_data(Ofl_desc *ofl, Rel_desc *rsp)
2990 * In a cross-link situation where the linker host and target
2991 * have opposite byte orders, it can be necessary to swap bytes
2992 * when doing relocation processing. This is indicated by the
2993 * presence of the FLG_OF1_ENCDIFF flag bit. However, swapping
2994 * is only needed for the section types that libelf doesn't
2995 * automatically xlate.
2997 if ((ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0) {
2998 switch (RELAUX_GET_OSDESC(rsp)->os_shdr->sh_type) {
2999 case SHT_PROGBITS:
3000 return (1);
3002 case SHT_SPARC_GOTDATA:
3003 if (ld_targ.t_m.m_mach ==
3004 LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
3005 return (1);
3006 break;
3008 case SHT_AMD64_UNWIND:
3009 if (ld_targ.t_m.m_mach == EM_AMD64)
3010 return (1);
3011 break;
3016 * If FLG_OF1_ENCDIFF isn't set, or the section isn't
3017 * progbits (or similar), then no swapping is needed.
3019 return (0);
3025 * Obtain the current value at the given relocation target.
3027 * entry:
3028 * ofl - Output file descriptor
3029 * rsp - Relocation record
3030 * data - Pointer to relocation target
3031 * value - Address of variable to recieve value
3033 * exit:
3034 * The value of the data at the relocation target has
3035 * been stored in value.
3038 ld_reloc_targval_get(Ofl_desc *ofl, Rel_desc *rsp, uchar_t *data, Xword *value)
3040 const Rel_entry *rep;
3042 rep = &ld_targ.t_mr.mr_reloc_table[rsp->rel_rtype];
3044 switch (rep->re_fsize) {
3045 case 1:
3046 /* LINTED */
3047 *value = (Xword) *((uchar_t *)data);
3048 break;
3049 case 2:
3051 Half v;
3052 uchar_t *v_bytes = (uchar_t *)&v;
3054 if (OFL_SWAP_RELOC_DATA(ofl, rsp)) {
3055 UL_ASSIGN_BSWAP_HALF(v_bytes, data);
3056 } else {
3057 UL_ASSIGN_HALF(v_bytes, data);
3059 *value = (Xword) v;
3061 break;
3062 case 4:
3064 Word v;
3065 uchar_t *v_bytes = (uchar_t *)&v;
3067 if (OFL_SWAP_RELOC_DATA(ofl, rsp)) {
3068 UL_ASSIGN_BSWAP_WORD(v_bytes, data);
3069 } else {
3070 UL_ASSIGN_WORD(v_bytes, data);
3072 *value = (Xword) v;
3074 break;
3075 default:
3077 Conv_inv_buf_t inv_buf;
3078 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_UNSUPSZ),
3079 conv_reloc_type(ld_targ.t_m.m_mach, rsp->rel_rtype,
3080 0, &inv_buf), rsp->rel_isdesc->is_file->ifl_name,
3081 ld_reloc_sym_name(rsp), (int)rep->re_fsize);
3083 return (0);
3085 return (1);
3090 * Set the value at the given relocation target.
3092 * entry:
3093 * ofl - Output file descriptor
3094 * rsp - Relocation record
3095 * data - Pointer to relocation target
3096 * value - Address of variable to recieve value
3098 * exit:
3099 * The value of the data at the relocation target has
3100 * been stored in value.
3103 ld_reloc_targval_set(Ofl_desc *ofl, Rel_desc *rsp, uchar_t *data, Xword value)
3105 const Rel_entry *rep;
3107 rep = &ld_targ.t_mr.mr_reloc_table[rsp->rel_rtype];
3109 switch (rep->re_fsize) {
3110 case 1:
3111 /* LINTED */
3112 *((uchar_t *)data) = (uchar_t)value;
3113 break;
3114 case 2:
3116 Half v = (Half)value;
3117 uchar_t *v_bytes = (uchar_t *)&v;
3119 if (OFL_SWAP_RELOC_DATA(ofl, rsp)) {
3120 UL_ASSIGN_BSWAP_HALF(data, v_bytes);
3121 } else {
3122 UL_ASSIGN_HALF(data, v_bytes);
3125 break;
3126 case 4:
3128 Word v = (Word)value;
3129 uchar_t *v_bytes = (uchar_t *)&v;
3131 if (OFL_SWAP_RELOC_DATA(ofl, rsp)) {
3132 UL_ASSIGN_BSWAP_WORD(data, v_bytes);
3133 } else {
3134 UL_ASSIGN_WORD(data, v_bytes);
3137 break;
3138 default:
3140 Conv_inv_buf_t inv_buf;
3141 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_REL_UNSUPSZ),
3142 conv_reloc_type(ld_targ.t_m.m_mach, rsp->rel_rtype,
3143 0, &inv_buf), rsp->rel_isdesc->is_file->ifl_name,
3144 ld_reloc_sym_name(rsp), (int)rep->re_fsize);
3146 return (0);
3148 return (1);
3153 * Because of the combinations of 32-bit lib providing 64-bit support, and
3154 * visa-versa, the use of krtld's dorelocs can result in differing message
3155 * requirements that make msg.c/msg.h creation and chkmsg "interesting".
3156 * Thus the actual message files contain a couple of entries to satisfy
3157 * each architectures build. Here we add dummy calls to quieten chkmsg.
3159 * chkmsg: MSG_INTL(MSG_REL_NOFIT)
3160 * chkmsg: MSG_INTL(MSG_REL_NONALIGN)