16677 ipadm show-addr inconsistently reports if a specified addrobj is missing
[illumos-gate.git] / usr / src / cmd / sgs / libld / common / sections.c
blob5ff110e346686c318efbc44c3d83661b88b66ded
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.
27 * Copyright 2022 Oxide Computer Company
31 * Module sections. Initialize special sections
34 #define ELF_TARGET_AMD64
36 #include <sys/debug.h>
38 #include <string.h>
39 #include <strings.h>
40 #include <stdio.h>
41 #include <link.h>
42 #include <debug.h>
43 #include "msg.h"
44 #include "_libld.h"
46 inline static void
47 remove_local(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
49 Sym *sym = sdp->sd_sym;
50 uchar_t type = ELF_ST_TYPE(sym->st_info);
51 /* LINTED - only used for assert() */
52 int err;
54 if ((ofl->ofl_flags & FLG_OF_REDLSYM) == 0) {
55 ofl->ofl_locscnt--;
57 err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
58 assert(err != -1);
60 if (allow_ldynsym && ldynsym_symtype[type]) {
61 ofl->ofl_dynlocscnt--;
63 err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
64 assert(err != -1);
65 /* Remove from sort section? */
66 DYNSORT_COUNT(sdp, sym, type, --);
69 sdp->sd_flags |= FLG_SY_ISDISC;
72 inline static void
73 remove_scoped(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
75 Sym *sym = sdp->sd_sym;
76 uchar_t type = ELF_ST_TYPE(sym->st_info);
77 /* LINTED - only used for assert() */
78 int err;
80 ofl->ofl_scopecnt--;
81 ofl->ofl_elimcnt++;
83 err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
84 assert(err != -1);
86 if (allow_ldynsym && ldynsym_symtype[type]) {
87 ofl->ofl_dynscopecnt--;
89 err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
90 assert(err != -1);
91 /* Remove from sort section? */
92 DYNSORT_COUNT(sdp, sym, type, --);
94 sdp->sd_flags |= FLG_SY_ELIM;
97 inline static void
98 ignore_sym(Ofl_desc *ofl, Ifl_desc *ifl, Sym_desc *sdp, int allow_ldynsym)
100 Os_desc *osp;
101 Is_desc *isp = sdp->sd_isc;
102 uchar_t bind = ELF_ST_BIND(sdp->sd_sym->st_info);
104 if (bind == STB_LOCAL) {
105 uchar_t type = ELF_ST_TYPE(sdp->sd_sym->st_info);
108 * Skip section symbols, these were never collected in the
109 * first place.
111 if (type == STT_SECTION)
112 return;
115 * Determine if the whole file is being removed. Remove any
116 * file symbol, and any symbol that is not associated with a
117 * section, provided the symbol has not been identified as
118 * (update) required.
120 if (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) &&
121 ((type == STT_FILE) || ((isp == NULL) &&
122 ((sdp->sd_flags & FLG_SY_UPREQD) == 0)))) {
123 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
124 if (ifl->ifl_flags & FLG_IF_IGNORE)
125 remove_local(ofl, sdp, allow_ldynsym);
126 return;
129 } else {
131 * Global symbols can only be eliminated when the interfaces of
132 * an object have been defined via versioning/scoping.
134 if (!SYM_IS_HIDDEN(sdp))
135 return;
138 * Remove any unreferenced symbols that are not associated with
139 * a section.
141 if ((isp == NULL) && ((sdp->sd_flags & FLG_SY_UPREQD) == 0)) {
142 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
143 if (ifl->ifl_flags & FLG_IF_IGNORE)
144 remove_scoped(ofl, sdp, allow_ldynsym);
145 return;
150 * Do not discard any symbols that are associated with non-allocable
151 * segments.
153 if (isp && ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
154 ((osp = isp->is_osdesc) != 0) &&
155 (osp->os_sgdesc->sg_phdr.p_type == PT_LOAD)) {
156 DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
157 if (ifl->ifl_flags & FLG_IF_IGNORE) {
158 if (bind == STB_LOCAL)
159 remove_local(ofl, sdp, allow_ldynsym);
160 else
161 remove_scoped(ofl, sdp, allow_ldynsym);
166 static Boolean
167 isdesc_discarded(Is_desc *isp)
169 Ifl_desc *ifl = isp->is_file;
170 Os_desc *osp = isp->is_osdesc;
171 Word ptype = osp->os_sgdesc->sg_phdr.p_type;
173 if (isp->is_flags & FLG_IS_DISCARD)
174 return (TRUE);
177 * If the file is discarded, it will take
178 * the section with it.
180 if (ifl &&
181 (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) ||
182 ((ptype == PT_LOAD) &&
183 ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
184 (isp->is_shdr->sh_size > 0))) &&
185 (ifl->ifl_flags & FLG_IF_IGNORE))
186 return (TRUE);
188 return (FALSE);
192 * There are situations where we may count output sections (ofl_shdrcnt)
193 * that are subsequently eliminated from the output object. Whether or
194 * not this happens cannot be known until all input has been seen and
195 * section elimination code has run. However, the situations where this
196 * outcome is possible are known, and are flagged by setting FLG_OF_ADJOSCNT.
198 * If FLG_OF_ADJOSCNT is set, this routine makes a pass over the output
199 * sections. If an unused output section is encountered, we decrement
200 * ofl->ofl_shdrcnt and remove the section name from the .shstrtab string
201 * table (ofl->ofl_shdrsttab).
203 * This code must be kept in sync with the similar code
204 * found in outfile.c:ld_create_outfile().
206 static void
207 adjust_os_count(Ofl_desc *ofl)
209 Sg_desc *sgp;
210 Is_desc *isp;
211 Os_desc *osp;
212 Aliste idx1;
214 if ((ofl->ofl_flags & FLG_OF_ADJOSCNT) == 0)
215 return;
218 * For each output section, look at the input sections to find at least
219 * one input section that has not been eliminated. If none are found,
220 * the -z ignore processing above has eliminated that output section.
222 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
223 Aliste idx2;
225 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
226 Aliste idx3;
227 int keep = 0, os_isdescs_idx;
229 OS_ISDESCS_TRAVERSE(os_isdescs_idx, osp, idx3, isp) {
231 * We have found a kept input section,
232 * so the output section will be created.
234 if (!isdesc_discarded(isp)) {
235 keep = 1;
236 break;
240 * If no section of this name was kept, decrement
241 * the count and remove the name from .shstrtab.
243 if (keep == 0) {
244 /* LINTED - only used for assert() */
245 int err;
247 ofl->ofl_shdrcnt--;
248 err = st_delstring(ofl->ofl_shdrsttab,
249 osp->os_name);
250 assert(err != -1);
257 * If -zignore has been in effect, scan all input files to determine if the
258 * file, or sections from the file, have been referenced. If not, the file or
259 * some of the files sections can be discarded. If sections are to be
260 * discarded, rescan the output relocations and the symbol table and remove
261 * the relocations and symbol entries that are no longer required.
263 * Note: It's possible that a section which is being discarded has contributed
264 * to the GOT table or the PLT table. However, we can't at this point
265 * eliminate the corresponding entries. This is because there could well
266 * be other sections referencing those same entries, but we don't have
267 * the infrastructure to determine this. So, keep the PLT and GOT
268 * entries in the table in case someone wants them.
269 * Note: The section to be affected needs to be allocatable.
270 * So even if -zignore is in effect, if the section is not allocatable,
271 * we do not eliminate it.
273 static uintptr_t
274 ignore_section_processing(Ofl_desc *ofl)
276 Sg_desc *sgp;
277 Is_desc *isp;
278 Os_desc *osp;
279 Ifl_desc *ifl;
280 Rel_cachebuf *rcbp;
281 Rel_desc *rsp;
282 int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
283 Aliste idx1;
285 for (APLIST_TRAVERSE(ofl->ofl_objs, idx1, ifl)) {
286 uint_t num, discard;
289 * Diagnose (-D unused) a completely unreferenced file.
291 if ((ifl->ifl_flags & FLG_IF_FILEREF) == 0)
292 DBG_CALL(Dbg_unused_file(ofl->ofl_lml,
293 ifl->ifl_name, 0, 0));
294 if (((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) ||
295 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0))
296 continue;
299 * Before scanning the whole symbol table to determine if
300 * symbols should be discard - quickly (relatively) scan the
301 * sections to determine if any are to be discarded.
303 discard = 0;
304 if (ifl->ifl_flags & FLG_IF_FILEREF) {
305 for (num = 1; num < ifl->ifl_shnum; num++) {
306 if (((isp = ifl->ifl_isdesc[num]) != NULL) &&
307 ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
308 ((osp = isp->is_osdesc) != NULL) &&
309 ((sgp = osp->os_sgdesc) != NULL) &&
310 (sgp->sg_phdr.p_type == PT_LOAD)) {
311 discard++;
312 break;
318 * No sections are to be 'ignored'
320 if ((discard == 0) && (ifl->ifl_flags & FLG_IF_FILEREF))
321 continue;
324 * We know that we have discarded sections. Scan the symbol
325 * table for this file to determine if symbols need to be
326 * discarded that are associated with the 'ignored' sections.
328 for (num = 1; num < ifl->ifl_symscnt; num++) {
329 Sym_desc *sdp;
332 * If the symbol definition has been resolved to another
333 * file, or the symbol has already been discarded or
334 * eliminated, skip it.
336 sdp = ifl->ifl_oldndx[num];
337 if ((sdp->sd_file != ifl) ||
338 (sdp->sd_flags &
339 (FLG_SY_ISDISC | FLG_SY_INVALID | FLG_SY_ELIM)))
340 continue;
343 * Complete the investigation of the symbol.
345 ignore_sym(ofl, ifl, sdp, allow_ldynsym);
350 * If we were only here to solicit debugging diagnostics, we're done.
352 if ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0)
353 return (1);
356 * Scan all output relocations searching for those against discarded or
357 * ignored sections. If one is found, decrement the total outrel count.
359 REL_CACHE_TRAVERSE(&ofl->ofl_outrels, idx1, rcbp, rsp) {
360 Is_desc *isc = rsp->rel_isdesc;
361 uint_t flags, entsize;
362 Shdr *shdr;
364 if ((isc == NULL) || ((isc->is_flags & (FLG_IS_SECTREF))) ||
365 ((ifl = isc->is_file) == NULL) ||
366 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0) ||
367 ((shdr = isc->is_shdr) == NULL) ||
368 ((shdr->sh_flags & SHF_ALLOC) == 0))
369 continue;
371 flags = rsp->rel_flags;
373 if (flags & (FLG_REL_GOT | FLG_REL_BSS |
374 FLG_REL_NOINFO | FLG_REL_PLT))
375 continue;
377 osp = RELAUX_GET_OSDESC(rsp);
379 if (rsp->rel_flags & FLG_REL_RELA)
380 entsize = sizeof (Rela);
381 else
382 entsize = sizeof (Rel);
384 assert(osp->os_szoutrels > 0);
385 osp->os_szoutrels -= entsize;
387 if (!(flags & FLG_REL_PLT))
388 ofl->ofl_reloccntsub++;
390 if (rsp->rel_rtype == ld_targ.t_m.m_r_relative)
391 ofl->ofl_relocrelcnt--;
395 * As a result of our work here, the number of output sections may
396 * have decreased. Trigger a call to adjust_os_count().
398 ofl->ofl_flags |= FLG_OF_ADJOSCNT;
400 return (1);
404 * Allocate Elf_Data, Shdr, and Is_desc structures for a new
405 * section.
407 * entry:
408 * ofl - Output file descriptor
409 * shtype - SHT_ type code for section.
410 * shname - String giving the name for the new section.
411 * entcnt - # of items contained in the data part of the new section.
412 * This value is multiplied against the known element size
413 * for the section type to determine the size of the data
414 * area for the section. It is only meaningful in cases where
415 * the section type has a non-zero element size. In other cases,
416 * the caller must set the size fields in the *ret_data and
417 * *ret_shdr structs manually.
418 * ret_isec, ret_shdr, ret_data - Address of pointers to
419 * receive address of newly allocated structs.
421 * exit:
422 * On error, returns S_ERROR. On success, returns (1), and the
423 * ret_ pointers have been updated to point at the new structures,
424 * which have been filled in. To finish the task, the caller must
425 * update any fields within the supplied descriptors that differ
426 * from its needs, and then call ld_place_section().
428 static uintptr_t
429 new_section(Ofl_desc *ofl, Word shtype, const char *shname, Xword entcnt,
430 Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
432 typedef struct sec_info {
433 Word d_type;
434 Word align; /* Used in both data and section header */
435 Word sh_flags;
436 Word sh_entsize;
437 } SEC_INFO_T;
439 const SEC_INFO_T *sec_info;
441 Shdr *shdr;
442 Elf_Data *data;
443 Is_desc *isec;
444 size_t size;
447 * For each type of section, we have a distinct set of
448 * SEC_INFO_T values. This macro defines a static structure
449 * containing those values and generates code to set the sec_info
450 * pointer to refer to it. The pointer in sec_info remains valid
451 * outside of the declaration scope because the info_s struct is static.
453 * We can't determine the value of M_WORD_ALIGN at compile time, so
454 * a different variant is used for those cases.
456 #define SET_SEC_INFO(d_type, d_align, sh_flags, sh_entsize) \
458 static const SEC_INFO_T info_s = { d_type, d_align, sh_flags, \
459 sh_entsize}; \
460 sec_info = &info_s; \
462 #define SET_SEC_INFO_WORD_ALIGN(d_type, sh_flags, sh_entsize) \
464 static SEC_INFO_T info_s = { d_type, 0, sh_flags, \
465 sh_entsize}; \
466 info_s.align = ld_targ.t_m.m_word_align; \
467 sec_info = &info_s; \
470 switch (shtype) {
471 case SHT_PROGBITS:
473 * SHT_PROGBITS sections contain are used for many
474 * different sections. Alignments and flags differ.
475 * Some have a standard entsize, and others don't.
476 * We set some defaults here, but there is no expectation
477 * that they are correct or complete for any specific
478 * purpose. The caller must provide the correct values.
480 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0)
481 break;
483 case SHT_SYMTAB:
484 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, 0, sizeof (Sym))
485 break;
487 case SHT_DYNSYM:
488 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym))
489 break;
491 case SHT_SUNW_LDYNSYM:
492 ofl->ofl_flags |= FLG_OF_OSABI;
493 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym))
494 break;
496 case SHT_STRTAB:
498 * A string table may or may not be allocable, depending
499 * on context, so we leave that flag unset and leave it to
500 * the caller to add it if necessary.
502 * String tables do not have a standard entsize, so
503 * we set it to 0.
505 SET_SEC_INFO(ELF_T_BYTE, 1, SHF_STRINGS, 0)
506 break;
508 case SHT_RELA:
510 * Relocations with an addend (Everything except 32-bit X86).
511 * The caller is expected to set all section header flags.
513 SET_SEC_INFO_WORD_ALIGN(ELF_T_RELA, 0, sizeof (Rela))
514 break;
516 case SHT_REL:
518 * Relocations without an addend (32-bit X86 only).
519 * The caller is expected to set all section header flags.
521 SET_SEC_INFO_WORD_ALIGN(ELF_T_REL, 0, sizeof (Rel))
522 break;
524 case SHT_HASH:
525 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word))
526 break;
528 case SHT_SUNW_symsort:
529 case SHT_SUNW_tlssort:
530 ofl->ofl_flags |= FLG_OF_OSABI;
531 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word))
532 break;
534 case SHT_DYNAMIC:
536 * A dynamic section may or may not be allocable, and may or
537 * may not be writable, depending on context, so we leave the
538 * flags unset and leave it to the caller to add them if
539 * necessary.
541 SET_SEC_INFO_WORD_ALIGN(ELF_T_DYN, 0, sizeof (Dyn))
542 break;
544 case SHT_NOBITS:
546 * SHT_NOBITS is used for BSS-type sections. The size and
547 * alignment depend on the specific use and must be adjusted
548 * by the caller.
550 SET_SEC_INFO(ELF_T_BYTE, 0, SHF_ALLOC | SHF_WRITE, 0)
551 break;
553 case SHT_INIT_ARRAY:
554 case SHT_FINI_ARRAY:
555 case SHT_PREINIT_ARRAY:
556 SET_SEC_INFO(ELF_T_ADDR, sizeof (Addr), SHF_ALLOC | SHF_WRITE,
557 sizeof (Addr))
558 break;
560 case SHT_SYMTAB_SHNDX:
562 * Note that these sections are created to be associated
563 * with both symtab and dynsym symbol tables. However, they
564 * are non-allocable in all cases, because the runtime
565 * linker has no need for this information. It is purely
566 * informational, used by elfdump(1), debuggers, etc.
568 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, 0, sizeof (Word));
569 break;
571 case SHT_SUNW_cap:
572 ofl->ofl_flags |= FLG_OF_OSABI;
573 SET_SEC_INFO_WORD_ALIGN(ELF_T_CAP, SHF_ALLOC, sizeof (Cap));
574 break;
576 case SHT_SUNW_capchain:
577 ofl->ofl_flags |= FLG_OF_OSABI;
578 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC,
579 sizeof (Capchain));
580 break;
582 case SHT_SUNW_capinfo:
583 ofl->ofl_flags |= FLG_OF_OSABI;
584 #if _ELF64
585 SET_SEC_INFO(ELF_T_XWORD, sizeof (Xword), SHF_ALLOC,
586 sizeof (Capinfo));
587 #else
588 SET_SEC_INFO(ELF_T_WORD, sizeof (Word), SHF_ALLOC,
589 sizeof (Capinfo));
590 #endif
591 break;
593 case SHT_SUNW_move:
594 ofl->ofl_flags |= FLG_OF_OSABI;
595 SET_SEC_INFO(ELF_T_BYTE, sizeof (Lword),
596 SHF_ALLOC | SHF_WRITE, sizeof (Move));
597 break;
599 case SHT_SUNW_syminfo:
600 ofl->ofl_flags |= FLG_OF_OSABI;
602 * The sh_info field of the SHT_*_syminfo section points
603 * to the header index of the associated .dynamic section,
604 * so we also set SHF_INFO_LINK.
606 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE,
607 SHF_ALLOC | SHF_INFO_LINK, sizeof (Syminfo));
608 break;
610 case SHT_SUNW_verneed:
611 case SHT_SUNW_verdef:
612 ofl->ofl_flags |= FLG_OF_OSABI;
614 * The info for verneed and versym happen to be the same.
615 * The entries in these sections are not of uniform size,
616 * so we set the entsize to 0.
618 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0);
619 break;
621 case SHT_SUNW_versym:
622 ofl->ofl_flags |= FLG_OF_OSABI;
623 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC,
624 sizeof (Versym));
625 break;
627 default:
628 /* Should not happen: fcn called with unknown section type */
629 assert(0);
630 return (S_ERROR);
632 #undef SET_SEC_INFO
633 #undef SET_SEC_INFO_WORD_ALIGN
635 size = entcnt * sec_info->sh_entsize;
638 * Allocate and initialize the Elf_Data structure.
640 if ((data = libld_calloc(1, sizeof (Elf_Data))) == NULL)
641 return (S_ERROR);
642 data->d_type = sec_info->d_type;
643 data->d_size = size;
644 data->d_align = sec_info->align;
645 data->d_version = ofl->ofl_dehdr->e_version;
648 * Allocate and initialize the Shdr structure.
650 if ((shdr = libld_calloc(1, sizeof (Shdr))) == NULL)
651 return (S_ERROR);
652 shdr->sh_type = shtype;
653 shdr->sh_size = size;
654 shdr->sh_flags = sec_info->sh_flags;
655 shdr->sh_addralign = sec_info->align;
656 shdr->sh_entsize = sec_info->sh_entsize;
659 * Allocate and initialize the Is_desc structure.
661 if ((isec = libld_calloc(1, sizeof (Is_desc))) == NULL)
662 return (S_ERROR);
663 isec->is_name = shname;
664 isec->is_shdr = shdr;
665 isec->is_indata = data;
668 *ret_isec = isec;
669 *ret_shdr = shdr;
670 *ret_data = data;
671 return (1);
675 * Use an existing input section as a template to create a new
676 * input section with the same values as the original, other than
677 * the size of the data area which is supplied by the caller.
679 * entry:
680 * ofl - Output file descriptor
681 * ifl - Input file section to use as a template
682 * size - Size of data area for new section
683 * ret_isec, ret_shdr, ret_data - Address of pointers to
684 * receive address of newly allocated structs.
686 * exit:
687 * On error, returns S_ERROR. On success, returns (1), and the
688 * ret_ pointers have been updated to point at the new structures,
689 * which have been filled in. To finish the task, the caller must
690 * update any fields within the supplied descriptors that differ
691 * from its needs, and then call ld_place_section().
693 static uintptr_t
694 new_section_from_template(Ofl_desc *ofl, Is_desc *tmpl_isp, size_t size,
695 Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
697 Shdr *shdr;
698 Elf_Data *data;
699 Is_desc *isec;
702 * Allocate and initialize the Elf_Data structure.
704 if ((data = libld_calloc(1, sizeof (Elf_Data))) == NULL)
705 return (S_ERROR);
706 data->d_type = tmpl_isp->is_indata->d_type;
707 data->d_size = size;
708 data->d_align = tmpl_isp->is_shdr->sh_addralign;
709 data->d_version = ofl->ofl_dehdr->e_version;
712 * Allocate and initialize the Shdr structure.
714 if ((shdr = libld_malloc(sizeof (Shdr))) == NULL)
715 return (S_ERROR);
716 *shdr = *tmpl_isp->is_shdr;
717 shdr->sh_addr = 0;
718 shdr->sh_offset = 0;
719 shdr->sh_size = size;
722 * Allocate and initialize the Is_desc structure.
724 if ((isec = libld_calloc(1, sizeof (Is_desc))) == NULL)
725 return (S_ERROR);
726 isec->is_name = tmpl_isp->is_name;
727 isec->is_shdr = shdr;
728 isec->is_indata = data;
731 *ret_isec = isec;
732 *ret_shdr = shdr;
733 *ret_data = data;
734 return (1);
738 * Build a .bss section for allocation of tentative definitions. Any `static'
739 * .bss definitions would have been associated to their own .bss sections and
740 * thus collected from the input files. `global' .bss definitions are tagged
741 * as COMMON and do not cause any associated .bss section elements to be
742 * generated. Here we add up all these COMMON symbols and generate the .bss
743 * section required to represent them.
745 uintptr_t
746 ld_make_bss(Ofl_desc *ofl, Xword size, Xword align, uint_t ident)
748 Shdr *shdr;
749 Elf_Data *data;
750 Is_desc *isec;
751 Os_desc *osp;
752 Xword rsize = (Xword)ofl->ofl_relocbsssz;
755 * Allocate header structs. We will set the name ourselves below,
756 * and there is no entcnt for a BSS. So, the shname and entcnt
757 * arguments are 0.
759 if (new_section(ofl, SHT_NOBITS, NULL, 0,
760 &isec, &shdr, &data) == S_ERROR)
761 return (S_ERROR);
763 data->d_size = (size_t)size;
764 data->d_align = (size_t)align;
766 shdr->sh_size = size;
767 shdr->sh_addralign = align;
769 if (ident == ld_targ.t_id.id_tlsbss) {
770 isec->is_name = MSG_ORIG(MSG_SCN_TBSS);
771 ofl->ofl_istlsbss = isec;
772 shdr->sh_flags |= SHF_TLS;
774 } else if (ident == ld_targ.t_id.id_bss) {
775 isec->is_name = MSG_ORIG(MSG_SCN_BSS);
776 ofl->ofl_isbss = isec;
778 #if defined(_ELF64)
779 } else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
780 (ident == ld_targ.t_id.id_lbss)) {
781 isec->is_name = MSG_ORIG(MSG_SCN_LBSS);
782 ofl->ofl_islbss = isec;
783 shdr->sh_flags |= SHF_AMD64_LARGE;
784 #endif
788 * Retain this .*bss input section as this will be where global symbol
789 * references are added.
791 if ((osp = ld_place_section(ofl, isec, NULL, ident, NULL)) ==
792 (Os_desc *)S_ERROR)
793 return (S_ERROR);
796 * If relocations exist against a .*bss section, a section symbol must
797 * be created for the section in the .dynsym symbol table.
799 if (!(osp->os_flags & FLG_OS_OUTREL)) {
800 ofl_flag_t flagtotest;
802 if (ident == ld_targ.t_id.id_tlsbss)
803 flagtotest = FLG_OF1_TLSOREL;
804 else
805 flagtotest = FLG_OF1_BSSOREL;
807 if (ofl->ofl_flags1 & flagtotest) {
808 ofl->ofl_dynshdrcnt++;
809 osp->os_flags |= FLG_OS_OUTREL;
813 osp->os_szoutrels = rsize;
814 return (1);
818 * Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via
819 * ld -z *array=name).
821 static uintptr_t
822 make_array(Ofl_desc *ofl, Word shtype, const char *sectname, APlist *alp)
824 uint_t entcount;
825 Aliste idx;
826 Elf_Data *data;
827 Is_desc *isec;
828 Shdr *shdr;
829 Sym_desc *sdp;
830 Rel_desc reld;
831 Rela reloc;
832 Os_desc *osp;
833 uintptr_t ret = 1;
835 if (alp == NULL)
836 return (1);
838 entcount = 0;
839 for (APLIST_TRAVERSE(alp, idx, sdp))
840 entcount++;
842 if (new_section(ofl, shtype, sectname, entcount, &isec, &shdr, &data) ==
843 S_ERROR)
844 return (S_ERROR);
846 if ((data->d_buf = libld_calloc(entcount, sizeof (Addr))) == NULL)
847 return (S_ERROR);
849 if (ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_array, NULL) ==
850 (Os_desc *)S_ERROR)
851 return (S_ERROR);
853 osp = isec->is_osdesc;
855 if ((ofl->ofl_osinitarray == NULL) && (shtype == SHT_INIT_ARRAY))
856 ofl->ofl_osinitarray = osp;
857 if ((ofl->ofl_ospreinitarray == NULL) && (shtype == SHT_PREINIT_ARRAY))
858 ofl->ofl_ospreinitarray = osp;
859 else if ((ofl->ofl_osfiniarray == NULL) && (shtype == SHT_FINI_ARRAY))
860 ofl->ofl_osfiniarray = osp;
863 * Create relocations against this section to initialize it to the
864 * function addresses.
866 reld.rel_isdesc = isec;
867 reld.rel_aux = NULL;
868 reld.rel_flags = FLG_REL_LOAD;
871 * Fabricate the relocation information (as if a relocation record had
872 * been input - see init_rel()).
874 reld.rel_rtype = ld_targ.t_m.m_r_arrayaddr;
875 reld.rel_roffset = 0;
876 reld.rel_raddend = 0;
879 * Create a minimal relocation record to satisfy process_sym_reloc()
880 * debugging requirements.
882 reloc.r_offset = 0;
883 reloc.r_info = ELF_R_INFO(0, ld_targ.t_m.m_r_arrayaddr);
884 reloc.r_addend = 0;
886 DBG_CALL(Dbg_reloc_generate(ofl->ofl_lml, osp,
887 ld_targ.t_m.m_rel_sht_type));
888 for (APLIST_TRAVERSE(alp, idx, sdp)) {
889 reld.rel_sym = sdp;
891 if (ld_process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec,
892 MSG_INTL(MSG_STR_COMMAND), 0) == S_ERROR) {
893 ret = S_ERROR;
894 continue;
897 reld.rel_roffset += (Xword)sizeof (Addr);
898 reloc.r_offset = reld.rel_roffset;
901 return (ret);
905 * Build a comment section (-Qy option).
907 static uintptr_t
908 make_comment(Ofl_desc *ofl)
910 Shdr *shdr;
911 Elf_Data *data;
912 Is_desc *isec;
914 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_COMMENT), 0,
915 &isec, &shdr, &data) == S_ERROR)
916 return (S_ERROR);
918 data->d_buf = (void *)ofl->ofl_sgsid;
919 data->d_size = strlen(ofl->ofl_sgsid) + 1;
920 data->d_align = 1;
922 shdr->sh_size = (Xword)data->d_size;
923 shdr->sh_flags = 0;
924 shdr->sh_addralign = 1;
926 return ((uintptr_t)ld_place_section(ofl, isec, NULL,
927 ld_targ.t_id.id_note, NULL));
931 * Make the dynamic section. Calculate the size of any strings referenced
932 * within this structure, they will be added to the global string table
933 * (.dynstr). This routine should be called before make_dynstr().
935 * This routine must be maintained in parallel with update_odynamic()
936 * in update.c
938 static uintptr_t
939 make_dynamic(Ofl_desc *ofl)
941 Shdr *shdr;
942 Os_desc *osp;
943 Elf_Data *data;
944 Is_desc *isec;
945 size_t cnt = 0;
946 Aliste idx;
947 Ifl_desc *ifl;
948 Sym_desc *sdp;
949 size_t size;
950 Str_tbl *strtbl;
951 ofl_flag_t flags = ofl->ofl_flags;
952 int not_relobj = !(flags & FLG_OF_RELOBJ);
953 int unused = 0;
956 * Select the required string table.
958 if (OFL_IS_STATIC_OBJ(ofl))
959 strtbl = ofl->ofl_strtab;
960 else
961 strtbl = ofl->ofl_dynstrtab;
964 * Only a limited subset of DT_ entries apply to relocatable
965 * objects. See the comment at the head of update_odynamic() in
966 * update.c for details.
968 if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0,
969 &isec, &shdr, &data) == S_ERROR)
970 return (S_ERROR);
973 * new_section() does not set SHF_ALLOC. If we're building anything
974 * besides a relocatable object, then the .dynamic section should
975 * reside in allocatable memory.
977 if (not_relobj)
978 shdr->sh_flags |= SHF_ALLOC;
981 * new_section() does not set SHF_WRITE. If we're building an object
982 * that specifies an interpretor, then a DT_DEBUG entry is created,
983 * which is initialized to the applications link-map list at runtime.
985 if (ofl->ofl_osinterp)
986 shdr->sh_flags |= SHF_WRITE;
988 osp = ofl->ofl_osdynamic =
989 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynamic, NULL);
992 * Reserve entries for any needed dependencies.
994 for (APLIST_TRAVERSE(ofl->ofl_sos, idx, ifl)) {
995 if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR)))
996 continue;
999 * If this dependency didn't satisfy any symbol references,
1000 * generate a debugging diagnostic (ld(1) -Dunused can be used
1001 * to display these). If this is a standard needed dependency,
1002 * and -z ignore is in effect, drop the dependency. Explicitly
1003 * defined dependencies (i.e., -N dep) don't get dropped, and
1004 * are flagged as being required to simplify update_odynamic()
1005 * processing.
1007 if ((ifl->ifl_flags & FLG_IF_NEEDSTR) ||
1008 ((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) {
1009 if (unused++ == 0)
1010 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
1011 DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname,
1012 (ifl->ifl_flags & FLG_IF_NEEDSTR), 0));
1015 * Guidance: Remove unused dependency.
1017 * If -z ignore is in effect, this warning is not
1018 * needed because we will quietly remove the unused
1019 * dependency.
1021 if (OFL_GUIDANCE(ofl, FLG_OFG_NO_UNUSED) &&
1022 ((ifl->ifl_flags & FLG_IF_IGNORE) == 0))
1023 ld_eprintf(ofl, ERR_GUIDANCE,
1024 MSG_INTL(MSG_GUIDE_UNUSED),
1025 ifl->ifl_soname);
1027 if (ifl->ifl_flags & FLG_IF_NEEDSTR)
1028 ifl->ifl_flags |= FLG_IF_DEPREQD;
1029 else if (ifl->ifl_flags & FLG_IF_IGNORE)
1030 continue;
1034 * If this object requires a DT_POSFLAG_1 entry, reserve it.
1036 if ((ifl->ifl_flags & MSK_IF_POSFLAG1) && not_relobj)
1037 cnt++;
1039 if (st_insert(strtbl, ifl->ifl_soname) == -1)
1040 return (S_ERROR);
1041 cnt++;
1044 * If the needed entry contains the $ORIGIN token make sure
1045 * the associated DT_1_FLAGS entry is created.
1047 if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) {
1048 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1049 ofl->ofl_dtflags |= DF_ORIGIN;
1053 if (unused)
1054 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
1056 if (not_relobj) {
1058 * Reserve entries for any per-symbol auxiliary/filter strings.
1060 cnt += alist_nitems(ofl->ofl_dtsfltrs);
1063 * Reserve entries for _init() and _fini() section addresses.
1065 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
1066 SYM_NOHASH, NULL, ofl)) != NULL) &&
1067 (sdp->sd_ref == REF_REL_NEED) &&
1068 (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1069 sdp->sd_flags |= FLG_SY_UPREQD;
1070 cnt++;
1072 if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
1073 SYM_NOHASH, NULL, ofl)) != NULL) &&
1074 (sdp->sd_ref == REF_REL_NEED) &&
1075 (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1076 sdp->sd_flags |= FLG_SY_UPREQD;
1077 cnt++;
1081 * Reserve entries for any soname, filter name (shared libs
1082 * only), run-path pointers, cache names and audit requirements.
1084 if (ofl->ofl_soname) {
1085 cnt++;
1086 if (st_insert(strtbl, ofl->ofl_soname) == -1)
1087 return (S_ERROR);
1089 if (ofl->ofl_filtees) {
1090 cnt++;
1091 if (st_insert(strtbl, ofl->ofl_filtees) == -1)
1092 return (S_ERROR);
1095 * If the filtees entry contains the $ORIGIN token
1096 * make sure the associated DT_1_FLAGS entry is created.
1098 if (strstr(ofl->ofl_filtees,
1099 MSG_ORIG(MSG_STR_ORIGIN))) {
1100 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1101 ofl->ofl_dtflags |= DF_ORIGIN;
1106 if (ofl->ofl_rpath) {
1107 cnt += 2; /* DT_RPATH & DT_RUNPATH */
1108 if (st_insert(strtbl, ofl->ofl_rpath) == -1)
1109 return (S_ERROR);
1112 * If the rpath entry contains the $ORIGIN token make sure
1113 * the associated DT_1_FLAGS entry is created.
1115 if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) {
1116 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1117 ofl->ofl_dtflags |= DF_ORIGIN;
1121 if (not_relobj) {
1122 Aliste idx;
1123 Sg_desc *sgp;
1125 if (ofl->ofl_config) {
1126 cnt++;
1127 if (st_insert(strtbl, ofl->ofl_config) == -1)
1128 return (S_ERROR);
1131 * If the config entry contains the $ORIGIN token
1132 * make sure the associated DT_1_FLAGS entry is created.
1134 if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) {
1135 ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1136 ofl->ofl_dtflags |= DF_ORIGIN;
1139 if (ofl->ofl_depaudit) {
1140 cnt++;
1141 if (st_insert(strtbl, ofl->ofl_depaudit) == -1)
1142 return (S_ERROR);
1144 if (ofl->ofl_audit) {
1145 cnt++;
1146 if (st_insert(strtbl, ofl->ofl_audit) == -1)
1147 return (S_ERROR);
1151 * Reserve entries for the DT_HASH, DT_STRTAB, DT_STRSZ,
1152 * DT_SYMTAB, DT_SYMENT, and DT_CHECKSUM.
1154 cnt += 6;
1157 * If we are including local functions at the head of
1158 * the dynsym, then also reserve entries for DT_SUNW_SYMTAB
1159 * and DT_SUNW_SYMSZ.
1161 if (OFL_ALLOW_LDYNSYM(ofl))
1162 cnt += 2;
1164 if ((ofl->ofl_dynsymsortcnt > 0) ||
1165 (ofl->ofl_dyntlssortcnt > 0))
1166 cnt++; /* DT_SUNW_SORTENT */
1168 if (ofl->ofl_dynsymsortcnt > 0)
1169 cnt += 2; /* DT_SUNW_[SYMSORT|SYMSORTSZ] */
1171 if (ofl->ofl_dyntlssortcnt > 0)
1172 cnt += 2; /* DT_SUNW_[TLSSORT|TLSSORTSZ] */
1174 if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
1175 FLG_OF_VERDEF)
1176 cnt += 2; /* DT_VERDEF & DT_VERDEFNUM */
1178 if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
1179 FLG_OF_VERNEED)
1180 cnt += 2; /* DT_VERNEED & DT_VERNEEDNUM */
1182 if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt)
1183 cnt++; /* DT_RELACOUNT */
1185 if (flags & FLG_OF_TEXTREL) /* DT_TEXTREL */
1186 cnt++;
1188 if (ofl->ofl_osfiniarray) /* DT_FINI_ARRAY */
1189 cnt += 2; /* DT_FINI_ARRAYSZ */
1191 if (ofl->ofl_osinitarray) /* DT_INIT_ARRAY */
1192 cnt += 2; /* DT_INIT_ARRAYSZ */
1194 if (ofl->ofl_ospreinitarray) /* DT_PREINIT_ARRAY & */
1195 cnt += 2; /* DT_PREINIT_ARRAYSZ */
1198 * If we have plt's reserve a DT_PLTRELSZ, DT_PLTREL and
1199 * DT_JMPREL.
1201 if (ofl->ofl_pltcnt)
1202 cnt += 3;
1205 * If plt padding is needed (Sparcv9).
1207 if (ofl->ofl_pltpad)
1208 cnt += 2; /* DT_PLTPAD & DT_PLTPADSZ */
1211 * If we have any relocations reserve a DT_REL, DT_RELSZ and
1212 * DT_RELENT entry.
1214 if (ofl->ofl_relocsz)
1215 cnt += 3;
1218 * If a syminfo section is required create DT_SYMINFO,
1219 * DT_SYMINSZ, and DT_SYMINENT entries.
1221 if (flags & FLG_OF_SYMINFO)
1222 cnt += 3;
1225 * If there are any partially initialized sections allocate
1226 * DT_MOVETAB, DT_MOVESZ and DT_MOVEENT.
1228 if (ofl->ofl_osmove)
1229 cnt += 3;
1232 * Allocate one DT_REGISTER entry for every register symbol.
1234 cnt += ofl->ofl_regsymcnt;
1237 * Reserve a entry for each '-zrtldinfo=...' specified
1238 * on the command line.
1240 for (APLIST_TRAVERSE(ofl->ofl_rtldinfo, idx, sdp))
1241 cnt++;
1244 * The following entry should only be placed in a segment that
1245 * is writable.
1247 if (((sgp = osp->os_sgdesc) != NULL) &&
1248 (sgp->sg_phdr.p_flags & PF_W) && ofl->ofl_osinterp)
1249 cnt++; /* DT_DEBUG */
1252 * Capabilities require a .dynamic entry for the .SUNW_cap
1253 * section.
1255 if (ofl->ofl_oscap)
1256 cnt++; /* DT_SUNW_CAP */
1259 * Symbol capabilities require a .dynamic entry for the
1260 * .SUNW_capinfo section.
1262 if (ofl->ofl_oscapinfo)
1263 cnt++; /* DT_SUNW_CAPINFO */
1266 * Capabilities chain information requires a .SUNW_capchain
1267 * entry (DT_SUNW_CAPCHAIN), entry size (DT_SUNW_CAPCHAINENT),
1268 * and total size (DT_SUNW_CAPCHAINSZ).
1270 if (ofl->ofl_oscapchain)
1271 cnt += 3;
1273 if (flags & FLG_OF_SYMBOLIC)
1274 cnt++; /* DT_SYMBOLIC */
1276 if (ofl->ofl_aslr != 0) /* DT_SUNW_ASLR */
1277 cnt++;
1280 /* DT_SUNW_KMOD */
1281 if (ofl->ofl_flags & FLG_OF_KMOD)
1282 cnt++;
1285 * Account for Architecture dependent .dynamic entries, and defaults.
1287 (*ld_targ.t_mr.mr_mach_make_dynamic)(ofl, &cnt);
1290 * DT_FLAGS, DT_FLAGS_1, DT_SUNW_STRPAD, and DT_NULL. Also,
1291 * allow room for the unused extra DT_NULLs. These are included
1292 * to allow an ELF editor room to add items later.
1294 cnt += 4 + DYNAMIC_EXTRA_ELTS;
1297 * DT_SUNW_LDMACH. Used to hold the ELF machine code of the
1298 * linker that produced the output object. This information
1299 * allows us to determine whether a given object was linked
1300 * natively, or by a linker running on a different type of
1301 * system. This information can be valuable if one suspects
1302 * that a problem might be due to alignment or byte order issues.
1304 cnt++;
1307 * Determine the size of the section from the number of entries.
1309 size = cnt * (size_t)shdr->sh_entsize;
1311 shdr->sh_size = (Xword)size;
1312 data->d_size = size;
1315 * There are several tags that are specific to the Solaris osabi
1316 * range which we unconditionally put into any dynamic section
1317 * we create (e.g. DT_SUNW_STRPAD or DT_SUNW_LDMACH). As such,
1318 * any Solaris object with a dynamic section should be tagged as
1319 * ELFOSABI_SOLARIS.
1321 ofl->ofl_flags |= FLG_OF_OSABI;
1323 return ((uintptr_t)ofl->ofl_osdynamic);
1327 * Build the GOT section and its associated relocation entries.
1329 uintptr_t
1330 ld_make_got(Ofl_desc *ofl)
1332 Elf_Data *data;
1333 Shdr *shdr;
1334 Is_desc *isec;
1335 size_t size = (size_t)ofl->ofl_gotcnt * ld_targ.t_m.m_got_entsize;
1336 size_t rsize = (size_t)ofl->ofl_relocgotsz;
1338 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0,
1339 &isec, &shdr, &data) == S_ERROR)
1340 return (S_ERROR);
1342 data->d_size = size;
1344 shdr->sh_flags |= SHF_WRITE;
1345 shdr->sh_size = (Xword)size;
1346 shdr->sh_entsize = ld_targ.t_m.m_got_entsize;
1348 ofl->ofl_osgot = ld_place_section(ofl, isec, NULL,
1349 ld_targ.t_id.id_got, NULL);
1350 if (ofl->ofl_osgot == (Os_desc *)S_ERROR)
1351 return (S_ERROR);
1353 ofl->ofl_osgot->os_szoutrels = (Xword)rsize;
1355 return (1);
1359 * Build an interpreter section.
1361 static uintptr_t
1362 make_interp(Ofl_desc *ofl)
1364 Shdr *shdr;
1365 Elf_Data *data;
1366 Is_desc *isec;
1367 const char *iname = ofl->ofl_interp;
1368 size_t size;
1371 * If -z nointerp is in effect, don't create an interpreter section.
1373 if (ofl->ofl_flags1 & FLG_OF1_NOINTRP)
1374 return (1);
1377 * An .interp section is always created for a dynamic executable.
1378 * A user can define the interpreter to use. This definition overrides
1379 * the default that would be recorded in an executable, and triggers
1380 * the creation of an .interp section in any other object. Presumably
1381 * the user knows what they are doing. Refer to the generic ELF ABI
1382 * section 5-4, and the ld(1) -I option.
1384 if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC |
1385 FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname)
1386 return (1);
1389 * In the case of a dynamic executable, supply a default interpreter
1390 * if the user has not specified their own.
1392 if (iname == NULL)
1393 iname = ofl->ofl_interp = ld_targ.t_m.m_def_interp;
1395 size = strlen(iname) + 1;
1397 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0,
1398 &isec, &shdr, &data) == S_ERROR)
1399 return (S_ERROR);
1401 data->d_size = size;
1402 shdr->sh_size = (Xword)size;
1403 data->d_align = shdr->sh_addralign = 1;
1405 ofl->ofl_osinterp =
1406 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_interp, NULL);
1407 return ((uintptr_t)ofl->ofl_osinterp);
1411 * Common function used to build the SHT_SUNW_versym section, SHT_SUNW_syminfo
1412 * section, and SHT_SUNW_capinfo section. Each of these sections provide
1413 * additional symbol information, and their size parallels the associated
1414 * symbol table.
1416 static Os_desc *
1417 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident)
1419 Shdr *shdr;
1420 Elf_Data *data;
1421 Is_desc *isec;
1424 * We don't know the size of this section yet, so set it to 0. The
1425 * size gets filled in after the associated symbol table is sized.
1427 if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) ==
1428 S_ERROR)
1429 return ((Os_desc *)S_ERROR);
1431 return (ld_place_section(ofl, isec, NULL, ident, NULL));
1435 * Determine whether a symbol capability is redundant because the object
1436 * capabilities are more restrictive.
1438 inline static int
1439 is_cap_redundant(Objcapset *ocapset, Objcapset *scapset)
1441 Alist *oalp, *salp;
1442 elfcap_mask_t omsk, smsk;
1445 * Inspect any platform capabilities. If the object defines platform
1446 * capabilities, then the object will only be loaded for those
1447 * platforms. A symbol capability set that doesn't define the same
1448 * platforms is redundant, and a symbol capability that does not provide
1449 * at least one platform name that matches a platform name in the object
1450 * capabilities will never execute (as the object wouldn't have been
1451 * loaded).
1453 oalp = ocapset->oc_plat.cl_val;
1454 salp = scapset->oc_plat.cl_val;
1455 if (oalp && ((salp == NULL) || cap_names_match(oalp, salp)))
1456 return (1);
1459 * If the symbol capability set defines platforms, and the object
1460 * doesn't, then the symbol set is more restrictive.
1462 if (salp && (oalp == NULL))
1463 return (0);
1466 * Next, inspect any machine name capabilities. If the object defines
1467 * machine name capabilities, then the object will only be loaded for
1468 * those machines. A symbol capability set that doesn't define the same
1469 * machine names is redundant, and a symbol capability that does not
1470 * provide at least one machine name that matches a machine name in the
1471 * object capabilities will never execute (as the object wouldn't have
1472 * been loaded).
1474 oalp = ocapset->oc_plat.cl_val;
1475 salp = scapset->oc_plat.cl_val;
1476 if (oalp && ((salp == NULL) || cap_names_match(oalp, salp)))
1477 return (1);
1480 * If the symbol capability set defines machine names, and the object
1481 * doesn't, then the symbol set is more restrictive.
1483 if (salp && (oalp == NULL))
1484 return (0);
1487 * Next, inspect any hardware capabilities. If the objects hardware
1488 * capabilities are greater than or equal to that of the symbols
1489 * capabilities, then the symbol capability set is redundant. If the
1490 * symbols hardware capabilities are greater that the objects, then the
1491 * symbol set is more restrictive.
1493 * Note that this is a somewhat arbitrary definition, as each capability
1494 * bit is independent of the others, and some of the higher order bits
1495 * could be considered to be less important than lower ones. However,
1496 * this is the only reasonable non-subjective definition.
1498 omsk = ocapset->oc_hw_3.cm_val;
1499 smsk = scapset->oc_hw_3.cm_val;
1500 if ((omsk > smsk) || (omsk && (omsk == smsk)))
1501 return (1);
1502 if (omsk < smsk)
1503 return (0);
1506 omsk = ocapset->oc_hw_2.cm_val;
1507 smsk = scapset->oc_hw_2.cm_val;
1508 if ((omsk > smsk) || (omsk && (omsk == smsk)))
1509 return (1);
1510 if (omsk < smsk)
1511 return (0);
1514 * Finally, inspect the remaining hardware capabilities.
1516 omsk = ocapset->oc_hw_1.cm_val;
1517 smsk = scapset->oc_hw_1.cm_val;
1518 if ((omsk > smsk) || (omsk && (omsk == smsk)))
1519 return (1);
1521 return (0);
1525 * Capabilities values might have been assigned excluded values. These
1526 * excluded values should be removed before calculating any capabilities
1527 * sections size.
1529 static void
1530 capmask_value(Lm_list *lml, Word type, Capmask *capmask, int *title)
1533 * First determine whether any bits should be excluded.
1535 if ((capmask->cm_val & capmask->cm_exc) == 0)
1536 return;
1538 DBG_CALL(Dbg_cap_post_title(lml, title));
1540 DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_CURRENT, type,
1541 capmask->cm_val, ld_targ.t_m.m_mach));
1542 DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_EXCLUDE, type,
1543 capmask->cm_exc, ld_targ.t_m.m_mach));
1545 capmask->cm_val &= ~capmask->cm_exc;
1547 DBG_CALL(Dbg_cap_val_entry(lml, DBG_STATE_RESOLVED, type,
1548 capmask->cm_val, ld_targ.t_m.m_mach));
1551 static void
1552 capstr_value(Lm_list *lml, Word type, Caplist *caplist, int *title)
1554 Aliste idx1, idx2;
1555 char *estr;
1556 Capstr *capstr;
1557 Boolean found = FALSE;
1560 * First determine whether any strings should be excluded.
1562 for (APLIST_TRAVERSE(caplist->cl_exc, idx1, estr)) {
1563 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1564 if (strcmp(estr, capstr->cs_str) == 0) {
1565 found = TRUE;
1566 break;
1571 if (found == FALSE)
1572 return;
1575 * Traverse the current strings, then delete the excluded strings,
1576 * and finally display the resolved strings.
1578 if (DBG_ENABLED) {
1579 Dbg_cap_post_title(lml, title);
1580 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1581 Dbg_cap_ptr_entry(lml, DBG_STATE_CURRENT, type,
1582 capstr->cs_str);
1585 for (APLIST_TRAVERSE(caplist->cl_exc, idx1, estr)) {
1586 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1587 if (strcmp(estr, capstr->cs_str) == 0) {
1588 DBG_CALL(Dbg_cap_ptr_entry(lml,
1589 DBG_STATE_EXCLUDE, type, capstr->cs_str));
1590 alist_delete(caplist->cl_val, &idx2);
1591 break;
1595 if (DBG_ENABLED) {
1596 for (ALIST_TRAVERSE(caplist->cl_val, idx2, capstr)) {
1597 Dbg_cap_ptr_entry(lml, DBG_STATE_RESOLVED, type,
1598 capstr->cs_str);
1604 * Build a capabilities section.
1606 #define CAP_UPDATE(cap, capndx, tag, val) \
1607 cap->c_tag = tag; \
1608 cap->c_un.c_val = val; \
1609 cap++, capndx++;
1611 static uintptr_t
1612 make_cap(Ofl_desc *ofl, Word shtype, const char *shname, int ident)
1614 Shdr *shdr;
1615 Elf_Data *data;
1616 Is_desc *isec;
1617 Cap *cap;
1618 size_t size = 0;
1619 Word capndx = 0;
1620 Str_tbl *strtbl;
1621 Objcapset *ocapset = &ofl->ofl_ocapset;
1622 Aliste idx1;
1623 Capstr *capstr;
1624 int title = 0;
1627 * Determine which string table to use for any CA_SUNW_MACH,
1628 * CA_SUNW_PLAT, or CA_SUNW_ID strings.
1630 if (OFL_IS_STATIC_OBJ(ofl))
1631 strtbl = ofl->ofl_strtab;
1632 else
1633 strtbl = ofl->ofl_dynstrtab;
1636 * If symbol capabilities have been requested, but none have been
1637 * created, warn the user. This scenario can occur if none of the
1638 * input relocatable objects defined any object capabilities.
1640 if ((ofl->ofl_flags & FLG_OF_OTOSCAP) && (ofl->ofl_capsymcnt == 0))
1641 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_CAP_NOSYMSFOUND));
1644 * If symbol capabilities have been collected, but no symbols are left
1645 * referencing these capabilities, promote the capability groups back
1646 * to an object capability definition.
1648 if ((ofl->ofl_flags & FLG_OF_OTOSCAP) && ofl->ofl_capsymcnt &&
1649 (ofl->ofl_capfamilies == NULL)) {
1650 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_CAP_NOSYMSFOUND));
1651 ld_cap_move_symtoobj(ofl);
1652 ofl->ofl_capsymcnt = 0;
1653 ofl->ofl_capgroups = NULL;
1654 ofl->ofl_flags &= ~FLG_OF_OTOSCAP;
1658 * Remove any excluded capabilities.
1660 capstr_value(ofl->ofl_lml, CA_SUNW_PLAT, &ocapset->oc_plat, &title);
1661 capstr_value(ofl->ofl_lml, CA_SUNW_MACH, &ocapset->oc_mach, &title);
1662 capmask_value(ofl->ofl_lml, CA_SUNW_HW_3, &ocapset->oc_hw_3, &title);
1663 capmask_value(ofl->ofl_lml, CA_SUNW_HW_2, &ocapset->oc_hw_2, &title);
1664 capmask_value(ofl->ofl_lml, CA_SUNW_HW_1, &ocapset->oc_hw_1, &title);
1665 capmask_value(ofl->ofl_lml, CA_SUNW_SF_1, &ocapset->oc_sf_1, &title);
1668 * Determine how many entries are required for any object capabilities.
1670 size += alist_nitems(ocapset->oc_plat.cl_val);
1671 size += alist_nitems(ocapset->oc_mach.cl_val);
1672 if (ocapset->oc_hw_3.cm_val)
1673 size++;
1674 if (ocapset->oc_hw_2.cm_val)
1675 size++;
1676 if (ocapset->oc_hw_1.cm_val)
1677 size++;
1678 if (ocapset->oc_sf_1.cm_val)
1679 size++;
1682 * Only identify a capabilities group if the group has content. If a
1683 * capabilities identifier exists, and no other capabilities have been
1684 * supplied, remove the identifier. This scenario could exist if a
1685 * user mistakenly defined a lone identifier, or if an identified group
1686 * was overridden so as to clear the existing capabilities and the
1687 * identifier was not also cleared.
1689 if (ocapset->oc_id.cs_str) {
1690 if (size)
1691 size++;
1692 else
1693 ocapset->oc_id.cs_str = NULL;
1695 if (size)
1696 size++; /* Add CA_SUNW_NULL */
1699 * Determine how many entries are required for any symbol capabilities.
1701 if (ofl->ofl_capsymcnt) {
1703 * If there are no object capabilities, a CA_SUNW_NULL entry
1704 * is required before any symbol capabilities.
1706 if (size == 0)
1707 size++;
1708 size += ofl->ofl_capsymcnt;
1711 if (size == 0)
1712 return (0);
1714 if (new_section(ofl, shtype, shname, size, &isec,
1715 &shdr, &data) == S_ERROR)
1716 return (S_ERROR);
1718 if ((data->d_buf = libld_malloc(shdr->sh_size)) == NULL)
1719 return (S_ERROR);
1721 cap = (Cap *)data->d_buf;
1724 * Fill in any object capabilities. If there is an identifier, then the
1725 * identifier comes first. The remaining items follow in precedence
1726 * order, although the order isn't important for runtime verification.
1728 if (ocapset->oc_id.cs_str) {
1729 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1730 if (st_insert(strtbl, ocapset->oc_id.cs_str) == -1)
1731 return (S_ERROR);
1732 ocapset->oc_id.cs_ndx = capndx;
1733 CAP_UPDATE(cap, capndx, CA_SUNW_ID, 0);
1735 if (ocapset->oc_plat.cl_val) {
1736 ofl->ofl_flags |= (FLG_OF_PTCAP | FLG_OF_CAPSTRS);
1739 * Insert any platform name strings in the appropriate string
1740 * table. The capability value can't be filled in yet, as the
1741 * final offset of the strings isn't known until later.
1743 for (ALIST_TRAVERSE(ocapset->oc_plat.cl_val, idx1, capstr)) {
1744 if (st_insert(strtbl, capstr->cs_str) == -1)
1745 return (S_ERROR);
1746 capstr->cs_ndx = capndx;
1747 CAP_UPDATE(cap, capndx, CA_SUNW_PLAT, 0);
1750 if (ocapset->oc_mach.cl_val) {
1751 ofl->ofl_flags |= (FLG_OF_PTCAP | FLG_OF_CAPSTRS);
1754 * Insert the machine name strings in the appropriate string
1755 * table. The capability value can't be filled in yet, as the
1756 * final offset of the strings isn't known until later.
1758 for (ALIST_TRAVERSE(ocapset->oc_mach.cl_val, idx1, capstr)) {
1759 if (st_insert(strtbl, capstr->cs_str) == -1)
1760 return (S_ERROR);
1761 capstr->cs_ndx = capndx;
1762 CAP_UPDATE(cap, capndx, CA_SUNW_MACH, 0);
1765 if (ocapset->oc_hw_3.cm_val) {
1766 ofl->ofl_flags |= FLG_OF_PTCAP;
1767 CAP_UPDATE(cap, capndx, CA_SUNW_HW_3, ocapset->oc_hw_3.cm_val);
1769 if (ocapset->oc_hw_2.cm_val) {
1770 ofl->ofl_flags |= FLG_OF_PTCAP;
1771 CAP_UPDATE(cap, capndx, CA_SUNW_HW_2, ocapset->oc_hw_2.cm_val);
1773 if (ocapset->oc_hw_1.cm_val) {
1774 ofl->ofl_flags |= FLG_OF_PTCAP;
1775 CAP_UPDATE(cap, capndx, CA_SUNW_HW_1, ocapset->oc_hw_1.cm_val);
1777 if (ocapset->oc_sf_1.cm_val) {
1778 ofl->ofl_flags |= FLG_OF_PTCAP;
1779 CAP_UPDATE(cap, capndx, CA_SUNW_SF_1, ocapset->oc_sf_1.cm_val);
1781 CAP_UPDATE(cap, capndx, CA_SUNW_NULL, 0);
1784 * Fill in any symbol capabilities.
1786 if (ofl->ofl_capgroups) {
1787 Cap_group *cgp;
1789 for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
1790 Objcapset *scapset = &cgp->cg_set;
1791 Aliste idx2;
1792 Is_desc *isp;
1794 cgp->cg_ndx = capndx;
1796 if (scapset->oc_id.cs_str) {
1797 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1799 * Insert the identifier string in the
1800 * appropriate string table. The capability
1801 * value can't be filled in yet, as the final
1802 * offset of the string isn't known until later.
1804 if (st_insert(strtbl,
1805 scapset->oc_id.cs_str) == -1)
1806 return (S_ERROR);
1807 scapset->oc_id.cs_ndx = capndx;
1808 CAP_UPDATE(cap, capndx, CA_SUNW_ID, 0);
1811 if (scapset->oc_plat.cl_val) {
1812 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1815 * Insert the platform name string in the
1816 * appropriate string table. The capability
1817 * value can't be filled in yet, as the final
1818 * offset of the string isn't known until later.
1820 for (ALIST_TRAVERSE(scapset->oc_plat.cl_val,
1821 idx2, capstr)) {
1822 if (st_insert(strtbl,
1823 capstr->cs_str) == -1)
1824 return (S_ERROR);
1825 capstr->cs_ndx = capndx;
1826 CAP_UPDATE(cap, capndx,
1827 CA_SUNW_PLAT, 0);
1830 if (scapset->oc_mach.cl_val) {
1831 ofl->ofl_flags |= FLG_OF_CAPSTRS;
1834 * Insert the machine name string in the
1835 * appropriate string table. The capability
1836 * value can't be filled in yet, as the final
1837 * offset of the string isn't known until later.
1839 for (ALIST_TRAVERSE(scapset->oc_mach.cl_val,
1840 idx2, capstr)) {
1841 if (st_insert(strtbl,
1842 capstr->cs_str) == -1)
1843 return (S_ERROR);
1844 capstr->cs_ndx = capndx;
1845 CAP_UPDATE(cap, capndx,
1846 CA_SUNW_MACH, 0);
1849 if (scapset->oc_hw_3.cm_val) {
1850 CAP_UPDATE(cap, capndx, CA_SUNW_HW_3,
1851 scapset->oc_hw_3.cm_val);
1853 if (scapset->oc_hw_2.cm_val) {
1854 CAP_UPDATE(cap, capndx, CA_SUNW_HW_2,
1855 scapset->oc_hw_2.cm_val);
1857 if (scapset->oc_hw_1.cm_val) {
1858 CAP_UPDATE(cap, capndx, CA_SUNW_HW_1,
1859 scapset->oc_hw_1.cm_val);
1861 if (scapset->oc_sf_1.cm_val) {
1862 CAP_UPDATE(cap, capndx, CA_SUNW_SF_1,
1863 scapset->oc_sf_1.cm_val);
1865 CAP_UPDATE(cap, capndx, CA_SUNW_NULL, 0);
1868 * If any object capabilities are available, determine
1869 * whether these symbol capabilities are less
1870 * restrictive, and hence redundant.
1872 if (((ofl->ofl_flags & FLG_OF_PTCAP) == 0) ||
1873 (is_cap_redundant(ocapset, scapset) == 0))
1874 continue;
1877 * Indicate any files that provide redundant symbol
1878 * capabilities.
1880 for (APLIST_TRAVERSE(cgp->cg_secs, idx2, isp)) {
1881 ld_eprintf(ofl, ERR_WARNING,
1882 MSG_INTL(MSG_CAP_REDUNDANT),
1883 isp->is_file->ifl_name,
1884 EC_WORD(isp->is_scnndx), isp->is_name);
1890 * If capabilities strings are required, the sh_info field of the
1891 * section header will be set to the associated string table.
1893 if (ofl->ofl_flags & FLG_OF_CAPSTRS)
1894 shdr->sh_flags |= SHF_INFO_LINK;
1897 * Place these capabilities in the output file.
1899 if ((ofl->ofl_oscap = ld_place_section(ofl, isec,
1900 NULL, ident, NULL)) == (Os_desc *)S_ERROR)
1901 return (S_ERROR);
1904 * If symbol capabilities are required, then a .SUNW_capinfo section is
1905 * also created. This table will eventually be sized to match the
1906 * associated symbol table.
1908 if (ofl->ofl_capfamilies) {
1909 if ((ofl->ofl_oscapinfo = make_sym_sec(ofl,
1910 MSG_ORIG(MSG_SCN_SUNWCAPINFO), SHT_SUNW_capinfo,
1911 ld_targ.t_id.id_capinfo)) == (Os_desc *)S_ERROR)
1912 return (S_ERROR);
1915 * If we're generating a dynamic object, capabilities family
1916 * members are maintained in a .SUNW_capchain section.
1918 if (ofl->ofl_capchaincnt &&
1919 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
1920 if (new_section(ofl, SHT_SUNW_capchain,
1921 MSG_ORIG(MSG_SCN_SUNWCAPCHAIN),
1922 ofl->ofl_capchaincnt, &isec, &shdr,
1923 &data) == S_ERROR)
1924 return (S_ERROR);
1926 ofl->ofl_oscapchain = ld_place_section(ofl, isec,
1927 NULL, ld_targ.t_id.id_capchain, NULL);
1928 if (ofl->ofl_oscapchain == (Os_desc *)S_ERROR)
1929 return (S_ERROR);
1933 return (1);
1935 #undef CAP_UPDATE
1938 * Build the PLT section and its associated relocation entries.
1940 static uintptr_t
1941 make_plt(Ofl_desc *ofl)
1943 Shdr *shdr;
1944 Elf_Data *data;
1945 Is_desc *isec;
1946 size_t size = ld_targ.t_m.m_plt_reservsz +
1947 (((size_t)ofl->ofl_pltcnt + (size_t)ofl->ofl_pltpad) *
1948 ld_targ.t_m.m_plt_entsize);
1949 size_t rsize = (size_t)ofl->ofl_relocpltsz;
1952 * On sparc, account for the NOP at the end of the plt.
1954 if (ld_targ.t_m.m_mach == LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
1955 size += sizeof (Word);
1957 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0,
1958 &isec, &shdr, &data) == S_ERROR)
1959 return (S_ERROR);
1961 data->d_size = size;
1962 data->d_align = ld_targ.t_m.m_plt_align;
1964 shdr->sh_flags = ld_targ.t_m.m_plt_shf_flags;
1965 shdr->sh_size = (Xword)size;
1966 shdr->sh_addralign = ld_targ.t_m.m_plt_align;
1967 shdr->sh_entsize = ld_targ.t_m.m_plt_entsize;
1969 ofl->ofl_osplt = ld_place_section(ofl, isec, NULL,
1970 ld_targ.t_id.id_plt, NULL);
1971 if (ofl->ofl_osplt == (Os_desc *)S_ERROR)
1972 return (S_ERROR);
1974 ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1976 return (1);
1980 * Make the hash table. Only built for dynamic executables and shared
1981 * libraries, and provides hashed lookup into the global symbol table
1982 * (.dynsym) for the run-time linker to resolve symbol lookups.
1984 static uintptr_t
1985 make_hash(Ofl_desc *ofl)
1987 Shdr *shdr;
1988 Elf_Data *data;
1989 Is_desc *isec;
1990 size_t size;
1991 Word nsyms = ofl->ofl_globcnt;
1992 size_t cnt;
1995 * Allocate section header structures. We set entcnt to 0
1996 * because it's going to change after we place this section.
1998 if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0,
1999 &isec, &shdr, &data) == S_ERROR)
2000 return (S_ERROR);
2003 * Place the section first since it will affect the local symbol
2004 * count.
2006 ofl->ofl_oshash =
2007 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_hash, NULL);
2008 if (ofl->ofl_oshash == (Os_desc *)S_ERROR)
2009 return (S_ERROR);
2012 * Calculate the number of output hash buckets.
2014 ofl->ofl_hashbkts = findprime(nsyms);
2017 * The size of the hash table is determined by
2019 * i. the initial nbucket and nchain entries (2)
2020 * ii. the number of buckets (calculated above)
2021 * iii. the number of chains (this is based on the number of
2022 * symbols in the .dynsym array).
2024 cnt = 2 + ofl->ofl_hashbkts + DYNSYM_ALL_CNT(ofl);
2025 size = cnt * shdr->sh_entsize;
2028 * Finalize the section header and data buffer initialization.
2030 if ((data->d_buf = libld_calloc(size, 1)) == NULL)
2031 return (S_ERROR);
2032 data->d_size = size;
2033 shdr->sh_size = (Xword)size;
2035 return (1);
2039 * Generate the standard symbol table. Contains all locals and globals,
2040 * and resides in a non-allocatable section (ie. it can be stripped).
2042 static uintptr_t
2043 make_symtab(Ofl_desc *ofl)
2045 Shdr *shdr;
2046 Elf_Data *data;
2047 Is_desc *isec;
2048 Is_desc *xisec = 0;
2049 size_t size;
2050 Word symcnt;
2053 * Create the section headers. Note that we supply an ent_cnt
2054 * of 0. We won't know the count until the section has been placed.
2056 if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0,
2057 &isec, &shdr, &data) == S_ERROR)
2058 return (S_ERROR);
2061 * Place the section first since it will affect the local symbol
2062 * count.
2064 if ((ofl->ofl_ossymtab = ld_place_section(ofl, isec, NULL,
2065 ld_targ.t_id.id_symtab, NULL)) == (Os_desc *)S_ERROR)
2066 return (S_ERROR);
2069 * At this point we've created all but the 'shstrtab' section.
2070 * Determine if we have to use 'Extended Sections'. If so - then
2071 * also create a SHT_SYMTAB_SHNDX section.
2073 if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
2074 Shdr *xshdr;
2075 Elf_Data *xdata;
2077 if (new_section(ofl, SHT_SYMTAB_SHNDX,
2078 MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec,
2079 &xshdr, &xdata) == S_ERROR)
2080 return (S_ERROR);
2082 if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec, NULL,
2083 ld_targ.t_id.id_symtab_ndx, NULL)) == (Os_desc *)S_ERROR)
2084 return (S_ERROR);
2088 * Calculated number of symbols, which need to be augmented by
2089 * the (yet to be created) .shstrtab entry.
2091 symcnt = (size_t)(1 + SYMTAB_ALL_CNT(ofl));
2092 size = symcnt * shdr->sh_entsize;
2095 * Finalize the section header and data buffer initialization.
2097 data->d_size = size;
2098 shdr->sh_size = (Xword)size;
2101 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
2103 if (xisec) {
2104 size_t xsize = symcnt * sizeof (Word);
2106 xisec->is_indata->d_size = xsize;
2107 xisec->is_shdr->sh_size = (Xword)xsize;
2110 return (1);
2114 * Build a dynamic symbol table. These tables reside in the text
2115 * segment of a dynamic executable or shared library.
2117 * .SUNW_ldynsym contains local function symbols
2118 * .dynsym contains only globals symbols
2120 * The two tables are created adjacent to each other, with .SUNW_ldynsym
2121 * coming first.
2123 static uintptr_t
2124 make_dynsym(Ofl_desc *ofl)
2126 Shdr *shdr, *lshdr;
2127 Elf_Data *data, *ldata;
2128 Is_desc *isec, *lisec;
2129 size_t size;
2130 Xword cnt;
2131 int allow_ldynsym;
2134 * Unless explicitly disabled, always produce a .SUNW_ldynsym section
2135 * when it is allowed by the file type, even if the resulting
2136 * table only ends up with a single STT_FILE in it. There are
2137 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB
2138 * entry in the .dynamic section, which is something we would
2139 * like to encourage, and (2) Without it, we cannot generate
2140 * the associated .SUNW_dyn[sym|tls]sort sections, which are of
2141 * value to DTrace.
2143 * In practice, it is extremely rare for an object not to have
2144 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be
2145 * doing it anyway.
2147 allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
2150 * Create the section headers. Note that we supply an ent_cnt
2151 * of 0. We won't know the count until the section has been placed.
2153 if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM,
2154 MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR)
2155 return (S_ERROR);
2157 if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0,
2158 &isec, &shdr, &data) == S_ERROR)
2159 return (S_ERROR);
2162 * Place the section(s) first since it will affect the local symbol
2163 * count.
2165 if (allow_ldynsym &&
2166 ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec, NULL,
2167 ld_targ.t_id.id_ldynsym, NULL)) == (Os_desc *)S_ERROR))
2168 return (S_ERROR);
2169 ofl->ofl_osdynsym =
2170 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynsym, NULL);
2171 if (ofl->ofl_osdynsym == (Os_desc *)S_ERROR)
2172 return (S_ERROR);
2174 cnt = DYNSYM_ALL_CNT(ofl);
2175 size = (size_t)cnt * shdr->sh_entsize;
2178 * Finalize the section header and data buffer initialization.
2180 data->d_size = size;
2181 shdr->sh_size = (Xword)size;
2184 * An ldynsym contains local function symbols. It is not
2185 * used for linking, but if present, serves to allow better
2186 * stack traces to be generated in contexts where the symtab
2187 * is not available. (dladdr(), or stripped executable/library files).
2189 if (allow_ldynsym) {
2190 cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt;
2191 size = (size_t)cnt * shdr->sh_entsize;
2193 ldata->d_size = size;
2194 lshdr->sh_size = (Xword)size;
2197 return (1);
2201 * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are
2202 * index sections for the .SUNW_ldynsym/.dynsym pair that present data
2203 * and function symbols sorted by address.
2205 static uintptr_t
2206 make_dynsort(Ofl_desc *ofl)
2208 Shdr *shdr;
2209 Elf_Data *data;
2210 Is_desc *isec;
2212 /* Only do it if the .SUNW_ldynsym section is present */
2213 if (!OFL_ALLOW_LDYNSYM(ofl))
2214 return (1);
2216 /* .SUNW_dynsymsort */
2217 if (ofl->ofl_dynsymsortcnt > 0) {
2218 if (new_section(ofl, SHT_SUNW_symsort,
2219 MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt,
2220 &isec, &shdr, &data) == S_ERROR)
2221 return (S_ERROR);
2223 if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec, NULL,
2224 ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR)
2225 return (S_ERROR);
2228 /* .SUNW_dyntlssort */
2229 if (ofl->ofl_dyntlssortcnt > 0) {
2230 if (new_section(ofl, SHT_SUNW_tlssort,
2231 MSG_ORIG(MSG_SCN_DYNTLSSORT),
2232 ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR)
2233 return (S_ERROR);
2235 if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec, NULL,
2236 ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR)
2237 return (S_ERROR);
2240 return (1);
2244 * Helper routine for make_dynsym_shndx. Builds a
2245 * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing
2246 * which one it is.
2248 static uintptr_t
2249 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab,
2250 Os_desc **ret_os)
2252 Is_desc *isec;
2253 Is_desc *dynsymisp;
2254 Shdr *shdr, *dynshdr;
2255 Elf_Data *data;
2257 dynsymisp = ld_os_first_isdesc(symtab);
2258 dynshdr = dynsymisp->is_shdr;
2260 if (new_section(ofl, SHT_SYMTAB_SHNDX, shname,
2261 (dynshdr->sh_size / dynshdr->sh_entsize),
2262 &isec, &shdr, &data) == S_ERROR)
2263 return (S_ERROR);
2265 if ((*ret_os = ld_place_section(ofl, isec, NULL,
2266 ld_targ.t_id.id_dynsym_ndx, NULL)) == (Os_desc *)S_ERROR)
2267 return (S_ERROR);
2269 assert(*ret_os);
2271 return (1);
2275 * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym
2277 static uintptr_t
2278 make_dynsym_shndx(Ofl_desc *ofl)
2281 * If there is a .SUNW_ldynsym, generate a section for its extended
2282 * index section as well.
2284 if (OFL_ALLOW_LDYNSYM(ofl)) {
2285 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX),
2286 ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR)
2287 return (S_ERROR);
2290 /* The Generate a section for the dynsym */
2291 if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX),
2292 ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR)
2293 return (S_ERROR);
2295 return (1);
2300 * Build a string table for the section headers.
2302 static uintptr_t
2303 make_shstrtab(Ofl_desc *ofl)
2305 Shdr *shdr;
2306 Elf_Data *data;
2307 Is_desc *isec;
2308 size_t size;
2310 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB),
2311 0, &isec, &shdr, &data) == S_ERROR)
2312 return (S_ERROR);
2315 * Place the section first, as it may effect the number of section
2316 * headers to account for.
2318 ofl->ofl_osshstrtab =
2319 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_note, NULL);
2320 if (ofl->ofl_osshstrtab == (Os_desc *)S_ERROR)
2321 return (S_ERROR);
2323 size = st_getstrtab_sz(ofl->ofl_shdrsttab);
2324 assert(size > 0);
2326 data->d_size = size;
2327 shdr->sh_size = (Xword)size;
2329 return (1);
2333 * Build a string section for the standard symbol table.
2335 static uintptr_t
2336 make_strtab(Ofl_desc *ofl)
2338 Shdr *shdr;
2339 Elf_Data *data;
2340 Is_desc *isec;
2341 size_t size;
2344 * This string table consists of all the global and local symbols.
2345 * Account for null bytes at end of the file name and the beginning
2346 * of section.
2348 if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
2349 return (S_ERROR);
2351 size = st_getstrtab_sz(ofl->ofl_strtab);
2352 assert(size > 0);
2354 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB),
2355 0, &isec, &shdr, &data) == S_ERROR)
2356 return (S_ERROR);
2358 /* Set the size of the data area */
2359 data->d_size = size;
2360 shdr->sh_size = (Xword)size;
2362 ofl->ofl_osstrtab =
2363 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_strtab, NULL);
2364 return ((uintptr_t)ofl->ofl_osstrtab);
2368 * Build a string table for the dynamic symbol table.
2370 static uintptr_t
2371 make_dynstr(Ofl_desc *ofl)
2373 Shdr *shdr;
2374 Elf_Data *data;
2375 Is_desc *isec;
2376 size_t size;
2379 * If producing a .SUNW_ldynsym, account for the initial STT_FILE
2380 * symbol that precedes the scope reduced global symbols.
2382 if (OFL_ALLOW_LDYNSYM(ofl)) {
2383 if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1)
2384 return (S_ERROR);
2385 ofl->ofl_dynscopecnt++;
2389 * Account for any local, named register symbols. These locals are
2390 * required for reference from DT_REGISTER .dynamic entries.
2392 if (ofl->ofl_regsyms) {
2393 int ndx;
2395 for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
2396 Sym_desc *sdp;
2398 if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
2399 continue;
2401 if (!SYM_IS_HIDDEN(sdp) &&
2402 (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
2403 continue;
2405 if (sdp->sd_sym->st_name == 0)
2406 continue;
2408 if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
2409 return (S_ERROR);
2414 * Reserve entries for any per-symbol auxiliary/filter strings.
2416 if (ofl->ofl_dtsfltrs != NULL) {
2417 Dfltr_desc *dftp;
2418 Aliste idx;
2420 for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp))
2421 if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
2422 return (S_ERROR);
2425 size = st_getstrtab_sz(ofl->ofl_dynstrtab);
2426 assert(size > 0);
2428 if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR),
2429 0, &isec, &shdr, &data) == S_ERROR)
2430 return (S_ERROR);
2432 /* Make it allocable if necessary */
2433 if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
2434 shdr->sh_flags |= SHF_ALLOC;
2436 /* Set the size of the data area */
2437 data->d_size = size + DYNSTR_EXTRA_PAD;
2439 shdr->sh_size = (Xword)size;
2441 ofl->ofl_osdynstr =
2442 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynstr, NULL);
2443 return ((uintptr_t)ofl->ofl_osdynstr);
2447 * Generate an output relocation section which will contain the relocation
2448 * information to be applied to the `osp' section.
2450 * If (osp == NULL) then we are creating the coalesced relocation section
2451 * for an executable and/or a shared object.
2453 static uintptr_t
2454 make_reloc(Ofl_desc *ofl, Os_desc *osp)
2456 Shdr *shdr;
2457 Elf_Data *data;
2458 Is_desc *isec;
2459 size_t size;
2460 Xword sh_flags;
2461 char *sectname;
2462 Os_desc *rosp;
2463 Word relsize;
2464 const char *rel_prefix;
2466 /* LINTED */
2467 if (ld_targ.t_m.m_rel_sht_type == SHT_REL) {
2468 /* REL */
2469 relsize = sizeof (Rel);
2470 rel_prefix = MSG_ORIG(MSG_SCN_REL);
2471 } else {
2472 /* RELA */
2473 relsize = sizeof (Rela);
2474 rel_prefix = MSG_ORIG(MSG_SCN_RELA);
2477 if (osp) {
2478 size = osp->os_szoutrels;
2479 sh_flags = osp->os_shdr->sh_flags;
2480 if ((sectname = libld_malloc(strlen(rel_prefix) +
2481 strlen(osp->os_name) + 1)) == 0)
2482 return (S_ERROR);
2483 (void) strcpy(sectname, rel_prefix);
2484 (void) strcat(sectname, osp->os_name);
2485 } else if (ofl->ofl_flags & FLG_OF_COMREL) {
2486 size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
2487 sh_flags = SHF_ALLOC;
2488 sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
2489 } else {
2490 size = ofl->ofl_relocrelsz;
2491 sh_flags = SHF_ALLOC;
2492 sectname = (char *)rel_prefix;
2496 * Keep track of total size of 'output relocations' (to be stored
2497 * in .dynamic)
2499 /* LINTED */
2500 ofl->ofl_relocsz += (Xword)size;
2502 if (new_section(ofl, ld_targ.t_m.m_rel_sht_type, sectname, 0, &isec,
2503 &shdr, &data) == S_ERROR)
2504 return (S_ERROR);
2506 data->d_size = size;
2508 shdr->sh_size = (Xword)size;
2509 if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC))
2510 shdr->sh_flags = SHF_ALLOC;
2512 if (osp) {
2514 * The sh_info field of the SHT_REL* sections points to the
2515 * section the relocations are to be applied to.
2517 shdr->sh_flags |= SHF_INFO_LINK;
2520 rosp = ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_rel, NULL);
2521 if (rosp == (Os_desc *)S_ERROR)
2522 return (S_ERROR);
2525 * Associate this relocation section to the section its going to
2526 * relocate.
2528 if (osp) {
2529 Aliste idx;
2530 Is_desc *risp;
2533 * This is used primarily so that we can update
2534 * SHT_GROUP[sect_no] entries to point to the
2535 * created output relocation sections.
2537 for (APLIST_TRAVERSE(osp->os_relisdescs, idx, risp)) {
2538 risp->is_osdesc = rosp;
2541 * If the input relocation section had the SHF_GROUP
2542 * flag set - propagate it to the output relocation
2543 * section.
2545 if (risp->is_shdr->sh_flags & SHF_GROUP) {
2546 rosp->os_shdr->sh_flags |= SHF_GROUP;
2547 break;
2550 osp->os_relosdesc = rosp;
2551 } else
2552 ofl->ofl_osrel = rosp;
2555 * If this is the first relocation section we've encountered save it
2556 * so that the .dynamic entry can be initialized accordingly.
2558 if (ofl->ofl_osrelhead == (Os_desc *)0)
2559 ofl->ofl_osrelhead = rosp;
2561 return (1);
2565 * Generate version needed section.
2567 static uintptr_t
2568 make_verneed(Ofl_desc *ofl)
2570 Shdr *shdr;
2571 Elf_Data *data;
2572 Is_desc *isec;
2575 * verneed sections do not have a constant element size, so the
2576 * value of ent_cnt specified here (0) is meaningless.
2578 if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION),
2579 0, &isec, &shdr, &data) == S_ERROR)
2580 return (S_ERROR);
2582 /* During version processing we calculated the total size. */
2583 data->d_size = ofl->ofl_verneedsz;
2584 shdr->sh_size = (Xword)ofl->ofl_verneedsz;
2586 ofl->ofl_osverneed =
2587 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_version, NULL);
2588 return ((uintptr_t)ofl->ofl_osverneed);
2592 * Generate a version definition section.
2594 * o the SHT_SUNW_verdef section defines the versions that exist within this
2595 * image.
2597 static uintptr_t
2598 make_verdef(Ofl_desc *ofl)
2600 Shdr *shdr;
2601 Elf_Data *data;
2602 Is_desc *isec;
2603 Ver_desc *vdp;
2604 Str_tbl *strtab;
2607 * Reserve a string table entry for the base version dependency (other
2608 * dependencies have symbol representations, which will already be
2609 * accounted for during symbol processing).
2611 vdp = (Ver_desc *)ofl->ofl_verdesc->apl_data[0];
2613 if (OFL_IS_STATIC_OBJ(ofl))
2614 strtab = ofl->ofl_strtab;
2615 else
2616 strtab = ofl->ofl_dynstrtab;
2618 if (st_insert(strtab, vdp->vd_name) == -1)
2619 return (S_ERROR);
2622 * verdef sections do not have a constant element size, so the
2623 * value of ent_cnt specified here (0) is meaningless.
2625 if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION),
2626 0, &isec, &shdr, &data) == S_ERROR)
2627 return (S_ERROR);
2629 /* During version processing we calculated the total size. */
2630 data->d_size = ofl->ofl_verdefsz;
2631 shdr->sh_size = (Xword)ofl->ofl_verdefsz;
2633 ofl->ofl_osverdef =
2634 ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_version, NULL);
2635 return ((uintptr_t)ofl->ofl_osverdef);
2639 * This routine is called when -z nopartial is in effect.
2641 uintptr_t
2642 ld_make_parexpn_data(Ofl_desc *ofl, size_t size, Xword align)
2644 Shdr *shdr;
2645 Elf_Data *data;
2646 Is_desc *isec;
2647 Os_desc *osp;
2649 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2650 &isec, &shdr, &data) == S_ERROR)
2651 return (S_ERROR);
2653 shdr->sh_flags |= SHF_WRITE;
2654 data->d_size = size;
2655 shdr->sh_size = (Xword)size;
2656 if (align != 0) {
2657 data->d_align = align;
2658 shdr->sh_addralign = align;
2661 if ((data->d_buf = libld_calloc(size, 1)) == NULL)
2662 return (S_ERROR);
2665 * Retain handle to this .data input section. Variables using move
2666 * sections (partial initialization) will be redirected here when
2667 * such global references are added and '-z nopartial' is in effect.
2669 ofl->ofl_isparexpn = isec;
2670 osp = ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_data, NULL);
2671 if (osp == (Os_desc *)S_ERROR)
2672 return (S_ERROR);
2674 if (!(osp->os_flags & FLG_OS_OUTREL)) {
2675 ofl->ofl_dynshdrcnt++;
2676 osp->os_flags |= FLG_OS_OUTREL;
2678 return (1);
2682 * Make .sunwmove section
2684 uintptr_t
2685 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2687 Shdr *shdr;
2688 Elf_Data *data;
2689 Is_desc *isec;
2690 Aliste idx;
2691 Sym_desc *sdp;
2692 int cnt = 1;
2695 if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE),
2696 mv_nums, &isec, &shdr, &data) == S_ERROR)
2697 return (S_ERROR);
2699 if ((data->d_buf = libld_calloc(data->d_size, 1)) == NULL)
2700 return (S_ERROR);
2703 * Copy move entries
2705 for (APLIST_TRAVERSE(ofl->ofl_parsyms, idx, sdp)) {
2706 Aliste idx2;
2707 Mv_desc *mdp;
2709 if (sdp->sd_flags & FLG_SY_PAREXPN)
2710 continue;
2712 for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp))
2713 mdp->md_oidx = cnt++;
2716 if ((ofl->ofl_osmove = ld_place_section(ofl, isec, NULL, 0, NULL)) ==
2717 (Os_desc *)S_ERROR)
2718 return (S_ERROR);
2720 return (1);
2724 * Given a relocation descriptor that references a string table
2725 * input section, locate the string referenced and return a pointer
2726 * to it.
2728 static const char *
2729 strmerge_get_reloc_str(Ofl_desc *ofl, Rel_desc *rsp)
2731 Sym_desc *sdp = rsp->rel_sym;
2732 Xword str_off;
2735 * In the case of an STT_SECTION symbol, the addend of the
2736 * relocation gives the offset into the string section. For
2737 * other symbol types, the symbol value is the offset.
2740 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
2741 str_off = sdp->sd_sym->st_value;
2742 } else if ((rsp->rel_flags & FLG_REL_RELA) == FLG_REL_RELA) {
2744 * For SHT_RELA, the addend value is found in the
2745 * rel_raddend field of the relocation.
2747 str_off = rsp->rel_raddend;
2748 } else { /* REL and STT_SECTION */
2750 * For SHT_REL, the "addend" is not part of the relocation
2751 * record. Instead, it is found at the relocation target
2752 * address.
2754 uchar_t *addr = (uchar_t *)((uintptr_t)rsp->rel_roffset +
2755 (uintptr_t)rsp->rel_isdesc->is_indata->d_buf);
2757 if (ld_reloc_targval_get(ofl, rsp, addr, &str_off) == 0)
2758 return (0);
2761 return (str_off + (char *)sdp->sd_isc->is_indata->d_buf);
2765 * First pass over the relocation records for string table merging.
2766 * Build lists of relocations and symbols that will need modification,
2767 * and insert the strings they reference into the output string table.
2769 * entry:
2770 * ofl - Output file descriptor
2772 * exit: On success, the string merging specific members of each output
2773 * section descriptor in ofl are updated based on information from the
2774 * relocation entries, and 0 is returned.
2776 * On error, S_ERROR is returned.
2778 static uintptr_t
2779 ld_gather_strmerge(Ofl_desc *ofl, Rel_cache *cache)
2781 Rel_cachebuf *rbcp;
2782 Rel_desc *rsp;
2783 Sym_desc *last_sdp = NULL;
2784 Aliste idx1;
2787 * Pass 1:
2789 * Build lists of relocations and symbols that will need
2790 * modification, and insert the strings they reference into
2791 * the output string table.
2793 REL_CACHE_TRAVERSE(cache, idx1, rbcp, rsp) {
2794 Sym_desc *sdp = rsp->rel_sym;
2795 Os_desc *osp;
2796 const char *name;
2799 * If there's no input section, or the input section is
2800 * discarded or does not contain mergable strings, we have
2801 * nothing to do.
2803 if ((sdp->sd_isc == NULL) || ((sdp->sd_isc->is_flags &
2804 (FLG_IS_DISCARD | FLG_IS_INSTRMRG)) != FLG_IS_INSTRMRG))
2805 continue;
2807 osp = sdp->sd_isc->is_osdesc;
2810 * Remember symbol for use in the third pass. There is no
2811 * reason to save a given symbol more than once, so we take
2812 * advantage of the fact that relocations to a given symbol
2813 * tend to cluster in the list. If this is the same symbol
2814 * we saved last time, don't bother.
2816 if (last_sdp != sdp) {
2817 if (aplist_append(&osp->os_mstrsyms, sdp,
2818 AL_CNT_STRMRGSYM) == NULL)
2819 return (S_ERROR);
2820 last_sdp = sdp;
2823 if ((osp->os_mstrtab == NULL) &&
2824 (osp->os_mstrtab = st_new(FLG_STNEW_COMPRESS)) == NULL)
2825 return (S_ERROR);
2827 /* Enter the string into our new string table */
2828 name = strmerge_get_reloc_str(ofl, rsp);
2829 if (st_insert(osp->os_mstrtab, name) == -1)
2830 return (S_ERROR);
2833 * If this is an STT_SECTION symbol, then the second pass
2834 * will need to modify this relocation, so hang on to it.
2836 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) &&
2837 (aplist_append(&osp->os_mstrrels, rsp,
2838 AL_CNT_STRMRGREL) == NULL)) {
2839 return (S_ERROR);
2843 return (0);
2847 * If any an output section has more than one SHF_MERGE|SHF_STRINGS input
2848 * section, replace them with a single merged/compressed input section.
2850 * This is done by making a Str_tbl (as we use for managing SHT_STRTAB
2851 * sections) per output section with compression enabled to manage all strings
2852 * in the mergeable input sections. We then discard all inputs which
2853 * contributed to this table and replace them with an input section we create
2854 * taking data from this Str_tbl. References to the now discarded sections
2855 * are then updated to refer to our new merged input section, and the string
2856 * table and other metadata are freed.
2858 * This process is done in 3 passes. For efficiency reasons half of pass 1 is
2859 * done by ld_strmerge_gather() so relocations only need to be processed once.
2860 * Steps 1.5 onward are performed here. The steps are:
2862 * 1) In ld_strmerge_gather() examine all relocations, insert strings
2863 * from relocations to the mergeable input sections into the string
2864 * table.
2865 * 1.5) Gather every string from the mergeable input sections, regardless
2866 * of whether it is referenced from a relocation. All strings
2867 * must be processed, and relocations may point into the middle
2868 * of an actual NUL-terminated string, so we must enter both the
2869 * precise strings referenced by relocations and full strings
2870 * within the section.
2871 * 2) Modify the relocation values to be correct for the
2872 * new merged section.
2873 * 3) Modify the symbols used by the relocations to reference
2874 * the new section.
2876 * These passes cannot be combined:
2877 * - The string table code works in two passes, and all
2878 * strings have to be loaded in pass one before the
2879 * offset of any strings can be determined.
2880 * - Multiple relocations reference a single symbol, so the
2881 * symbol cannot be modified until all relocations are
2882 * fixed.
2884 * entry:
2885 * ofl - Output file descriptor
2886 * osp - Outputs section descriptor
2888 * exit:
2889 * If section merging is possible for this output section, it is done.
2890 * If no errors are encountered, 0 is returned. On error, S_ERROR is
2891 * returned.
2893 * The contents of the string-merging specific members of this output
2894 * section descriptor are undefined after this function returns.
2896 static uintptr_t
2897 ld_strmerge_sec(Ofl_desc *ofl, Os_desc *osp)
2899 Is_desc *isp = NULL;
2900 Sym_desc *sdp;
2901 Rel_desc *rsp;
2902 Is_desc *mstrsec = NULL; /* Generated string merge section */
2903 Shdr *mstr_shdr = NULL;
2904 Elf_Data *mstr_data = NULL;
2905 size_t data_size;
2906 Aliste idx;
2907 uintptr_t ret = 0;
2908 Boolean placed = FALSE;
2911 * Pass 1.5: Add all strings from all mergeable input sections.
2913 * The last section we find also serves as the template for our
2914 * replacement merged section, providing the section attributes, etc.
2916 for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
2917 const char *str, *end;
2919 if (isdesc_discarded(isp))
2920 continue;
2923 * Input sections of 0 size are dubiously valid since they do
2924 * not even contain the NUL string. Ignore them.
2926 if (isp->is_shdr->sh_size == 0)
2927 continue;
2929 if ((osp->os_mstrtab == NULL) &&
2930 (osp->os_mstrtab = st_new(FLG_STNEW_COMPRESS)) == NULL) {
2931 ret = S_ERROR;
2932 goto out;
2935 end = isp->is_indata->d_buf + isp->is_indata->d_size;
2936 for (str = isp->is_indata->d_buf; str < end;
2937 str += strlen(str) + 1) {
2938 if (st_insert(osp->os_mstrtab, str) != 0) {
2939 ret = S_ERROR;
2940 goto out;
2945 IMPLY(osp->os_mstrtab != NULL, isp != NULL);
2946 if (osp->os_mstrtab == NULL) {
2947 ret = 0;
2948 goto out;
2952 * Get the size of the new input section. Requesting the string
2953 * table size "cooks" the table, and finalizes its contents.
2955 data_size = st_getstrtab_sz(osp->os_mstrtab);
2957 /* Create a new input section to hold the merged strings */
2958 if (new_section_from_template(ofl, isp, data_size,
2959 &mstrsec, &mstr_shdr, &mstr_data) == S_ERROR) {
2960 ret = S_ERROR;
2961 goto out;
2963 mstrsec->is_flags |= FLG_IS_GNSTRMRG;
2966 * Allocate a data buffer for the new input section, associate the
2967 * buffer with the string table descriptor, and fill it from the
2968 * string table.
2970 if ((mstr_data->d_buf = libld_malloc(data_size)) == NULL) {
2971 ret = S_ERROR;
2972 goto out;
2974 if ((st_setstrbuf(osp->os_mstrtab, mstr_data->d_buf,
2975 data_size) == -1)) {
2976 ret = S_ERROR;
2977 goto out;
2980 st_setallstrings(osp->os_mstrtab);
2982 /* Add the new section to the output image */
2983 if (ld_place_section(ofl, mstrsec, NULL, osp->os_identndx, NULL) ==
2984 (Os_desc *)S_ERROR) {
2985 ret = S_ERROR;
2986 goto out;
2988 placed = TRUE;
2991 * Pass 2:
2993 * Revisit the relocation descriptors with STT_SECTION symbols
2994 * that were saved by the first pass. Update each relocation
2995 * record so that the offset it contains is for the new section
2996 * instead of the original.
2998 for (APLIST_TRAVERSE(osp->os_mstrrels, idx, rsp)) {
2999 const char *name;
3000 size_t stoff;
3003 * Find the string to the merged table's buffer and get its
3004 * offset.
3006 name = strmerge_get_reloc_str(ofl, rsp);
3007 stoff = st_findstring(osp->os_mstrtab, name);
3008 VERIFY3S(stoff, !=, -1);
3011 * Alter the relocation to access the string at the
3012 * new offset in our new string table.
3014 * For SHT_RELA platforms, it suffices to simply
3015 * update the rel_raddend field of the relocation.
3017 * For SHT_REL platforms, the new "addend" value
3018 * needs to be written at the address being relocated.
3019 * However, we can't alter the input sections which
3020 * are mapped readonly, and the output image has not
3021 * been created yet. So, we defer this operation,
3022 * using the rel_raddend field of the relocation
3023 * which is normally 0 on a REL platform, to pass the
3024 * new "addend" value to ld_perform_outreloc() or
3025 * ld_do_activerelocs(). The FLG_REL_NADDEND flag
3026 * tells them that this is the case.
3028 if ((rsp->rel_flags & FLG_REL_RELA) == 0) {
3029 /* REL */
3030 rsp->rel_flags |= FLG_REL_NADDEND;
3032 rsp->rel_raddend = (Sxword)stoff;
3035 * Generate a symbol name string for STT_SECTION symbols
3036 * that might reference our merged section. This shows up
3037 * in debug output and helps show how the relocation has
3038 * changed from its original input section to our merged
3039 * one.
3041 if (ld_stt_section_sym_name(mstrsec) == NULL) {
3042 ret = S_ERROR;
3043 goto out;
3048 * Pass 3:
3050 * Modify the symbols referenced by the relocation descriptors
3051 * so that they reference the new input section containing the
3052 * merged strings instead of the original input sections.
3054 for (APLIST_TRAVERSE(osp->os_mstrsyms, idx, sdp)) {
3056 * If we've already redirected this symbol to the merged data,
3057 * don't do it again. ld_gather_strmerge() uses a heuristic
3058 * (relocations to the same symbol clump together) to avoid
3059 * inserting a given symbol more than once, but repeat symbols
3060 * in the list can occur.
3062 if ((sdp->sd_isc->is_flags & FLG_IS_INSTRMRG) == 0)
3063 continue;
3065 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
3067 * This is not an STT_SECTION symbol, so its
3068 * value is the offset of the string within the
3069 * input section. Update the address to reflect
3070 * the address in our new merged section.
3072 const char *name;
3073 size_t stoff;
3076 * Find the string in the merged table's buffer and get
3077 * its offset.
3079 name = sdp->sd_sym->st_value +
3080 (char *)sdp->sd_isc->is_indata->d_buf;
3081 stoff = st_findstring(osp->os_mstrtab, name);
3082 VERIFY3S(stoff, !=, -1);
3084 if (ld_sym_copy(sdp) == S_ERROR) {
3085 ret = S_ERROR;
3086 goto out;
3088 sdp->sd_sym->st_value = (Word)stoff;
3091 /* Redirect the symbol to our new merged section */
3092 sdp->sd_isc = mstrsec;
3096 * There are no references left to the original input string sections.
3097 * Mark them as discarded so they don't go into the output image.
3098 * At the same time, add up the sizes of the replaced sections.
3100 data_size = 0;
3101 for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
3102 if (isp->is_flags & (FLG_IS_DISCARD | FLG_IS_GNSTRMRG))
3103 continue;
3105 data_size += isp->is_indata->d_size;
3107 isp->is_flags |= FLG_IS_DISCARD;
3108 DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp, mstrsec));
3111 /* Report how much space we saved in the output section */
3112 DBG_CALL(Dbg_sec_genstr_compress(ofl->ofl_lml, osp->os_name, data_size,
3113 mstr_data->d_size));
3115 out:
3116 if ((ret == S_ERROR) && !placed) {
3117 libld_free(mstrsec);
3118 if (mstr_data != NULL)
3119 libld_free(mstr_data->d_buf);
3120 libld_free(mstr_data);
3121 libld_free(mstr_shdr);
3124 libld_free(osp->os_mstrsyms);
3125 osp->os_mstrsyms = NULL;
3126 libld_free(osp->os_mstrrels);
3127 osp->os_mstrrels = NULL;
3129 if (osp->os_mstrtab != NULL) {
3130 st_destroy(osp->os_mstrtab);
3131 osp->os_mstrtab = NULL;
3134 return (ret);
3138 * If any output section has SHF_MERGE|SHF_STRINGS input sections,
3139 * replace them with a single merged/compressed input section.
3141 * entry:
3142 * ofl - Output file descriptor
3144 * exit:
3145 * If section merging is possible, it is done. If no errors are
3146 * encountered, 0 is returned. On error, S_ERROR is returned.
3148 * The contents of the string-merging specific members of any output
3149 * section descriptor are undefined after this function returns.
3151 static uintptr_t
3152 ld_make_strmerge(Ofl_desc *ofl)
3154 Sg_desc *sgp;
3155 Aliste idx1;
3157 if (ld_gather_strmerge(ofl, &ofl->ofl_actrels) == S_ERROR)
3158 return (S_ERROR);
3159 if (ld_gather_strmerge(ofl, &ofl->ofl_outrels) == S_ERROR)
3160 return (S_ERROR);
3162 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3163 Os_desc *osp;
3164 Aliste idx2;
3166 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
3167 if (ld_strmerge_sec(ofl, osp) == S_ERROR)
3168 return (S_ERROR);
3172 return (0);
3176 * Update a data buffers size. A number of sections have to be created, and
3177 * the sections header contributes to the size of the eventual section. Thus,
3178 * a section may be created, and once all associated sections have been created,
3179 * we return to establish the required section size.
3181 inline static void
3182 update_data_size(Os_desc *osp, ulong_t cnt)
3184 Is_desc *isec = ld_os_first_isdesc(osp);
3185 Elf_Data *data = isec->is_indata;
3186 Shdr *shdr = osp->os_shdr;
3187 size_t size = cnt * shdr->sh_entsize;
3189 shdr->sh_size = (Xword)size;
3190 data->d_size = size;
3194 * The following sections are built after all input file processing and symbol
3195 * validation has been carried out. The order is important (because the
3196 * addition of a section adds a new symbol there is a chicken and egg problem
3197 * of maintaining the appropriate counts). By maintaining a known order the
3198 * individual routines can compensate for later, known, additions.
3200 uintptr_t
3201 ld_make_sections(Ofl_desc *ofl)
3203 ofl_flag_t flags = ofl->ofl_flags;
3204 Sg_desc *sgp;
3207 * Generate any special sections.
3209 if (flags & FLG_OF_ADDVERS)
3210 if (make_comment(ofl) == S_ERROR)
3211 return (S_ERROR);
3213 if (make_interp(ofl) == S_ERROR)
3214 return (S_ERROR);
3217 * Create a capabilities section if required.
3219 if (make_cap(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP),
3220 ld_targ.t_id.id_cap) == S_ERROR)
3221 return (S_ERROR);
3224 * Create any init/fini array sections.
3226 if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
3227 ofl->ofl_initarray) == S_ERROR)
3228 return (S_ERROR);
3230 if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
3231 ofl->ofl_finiarray) == S_ERROR)
3232 return (S_ERROR);
3234 if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
3235 ofl->ofl_preiarray) == S_ERROR)
3236 return (S_ERROR);
3239 * Make the .plt section. This occurs after any other relocation
3240 * sections are generated (see reloc_init()) to ensure that the
3241 * associated relocation section is after all the other relocation
3242 * sections.
3244 if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
3245 if (make_plt(ofl) == S_ERROR)
3246 return (S_ERROR);
3249 * Determine whether any sections or files are not referenced. Under
3250 * -Dunused a diagnostic for any unused components is generated, under
3251 * -zignore the component is removed from the final output.
3253 if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
3254 if (ignore_section_processing(ofl) == S_ERROR)
3255 return (S_ERROR);
3259 * If we have detected a situation in which previously placed
3260 * output sections may have been discarded, perform the necessary
3261 * readjustment.
3263 if (ofl->ofl_flags & FLG_OF_ADJOSCNT)
3264 adjust_os_count(ofl);
3267 * Do any of the output sections contain input sections that
3268 * are candidates for string table merging? For each such case,
3269 * we create a replacement section, insert it, and discard the
3270 * originals.
3272 * rel_alpp and sym_alpp are used by ld_make_strmerge()
3273 * for its internal processing. We are responsible for the
3274 * initialization and cleanup, and ld_make_strmerge() handles the rest.
3275 * This allows us to reuse a single pair of memory buffers, allocated
3276 * for this processing, for all the output sections.
3278 if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) == 0) {
3279 if (ld_make_strmerge(ofl) == S_ERROR)
3280 return (S_ERROR);
3284 * Add any necessary versioning information.
3286 if (!(flags & FLG_OF_NOVERSEC)) {
3287 if ((flags & FLG_OF_VERNEED) &&
3288 (make_verneed(ofl) == S_ERROR))
3289 return (S_ERROR);
3290 if ((flags & FLG_OF_VERDEF) &&
3291 (make_verdef(ofl) == S_ERROR))
3292 return (S_ERROR);
3293 if ((flags & (FLG_OF_VERNEED | FLG_OF_VERDEF)) &&
3294 ((ofl->ofl_osversym = make_sym_sec(ofl,
3295 MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym,
3296 ld_targ.t_id.id_version)) == (Os_desc*)S_ERROR))
3297 return (S_ERROR);
3301 * Create a syminfo section if necessary.
3303 if (flags & FLG_OF_SYMINFO) {
3304 if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
3305 MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo,
3306 ld_targ.t_id.id_syminfo)) == (Os_desc *)S_ERROR)
3307 return (S_ERROR);
3310 if (flags & FLG_OF_COMREL) {
3312 * If -zcombreloc is enabled then all relocations (except for
3313 * the PLT's) are coalesced into a single relocation section.
3315 if (ofl->ofl_reloccnt) {
3316 if (make_reloc(ofl, NULL) == S_ERROR)
3317 return (S_ERROR);
3319 } else {
3320 Aliste idx1;
3323 * Create the required output relocation sections. Note, new
3324 * sections may be added to the section list that is being
3325 * traversed. These insertions can move the elements of the
3326 * Alist such that a section descriptor is re-read. Recursion
3327 * is prevented by maintaining a previous section pointer and
3328 * insuring that this pointer isn't re-examined.
3330 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3331 Os_desc *osp, *posp = 0;
3332 Aliste idx2;
3334 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
3335 if ((osp != posp) && osp->os_szoutrels &&
3336 (osp != ofl->ofl_osplt)) {
3337 if (make_reloc(ofl, osp) == S_ERROR)
3338 return (S_ERROR);
3340 posp = osp;
3345 * If we're not building a combined relocation section, then
3346 * build a .rel[a] section as required.
3348 if (ofl->ofl_relocrelsz) {
3349 if (make_reloc(ofl, NULL) == S_ERROR)
3350 return (S_ERROR);
3355 * The PLT relocations are always in their own section, and we try to
3356 * keep them at the end of the PLT table. We do this to keep the hot
3357 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
3359 if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
3360 if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
3361 return (S_ERROR);
3365 * Finally build the symbol and section header sections.
3367 if (flags & FLG_OF_DYNAMIC) {
3368 if (make_dynamic(ofl) == S_ERROR)
3369 return (S_ERROR);
3372 * A number of sections aren't necessary within a relocatable
3373 * object, even if -dy has been used.
3375 if (!(flags & FLG_OF_RELOBJ)) {
3376 if (make_hash(ofl) == S_ERROR)
3377 return (S_ERROR);
3378 if (make_dynstr(ofl) == S_ERROR)
3379 return (S_ERROR);
3380 if (make_dynsym(ofl) == S_ERROR)
3381 return (S_ERROR);
3382 if (ld_unwind_make_hdr(ofl) == S_ERROR)
3383 return (S_ERROR);
3384 if (make_dynsort(ofl) == S_ERROR)
3385 return (S_ERROR);
3389 if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
3390 ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
3392 * Do we need to make a SHT_SYMTAB_SHNDX section
3393 * for the dynsym. If so - do it now.
3395 if (ofl->ofl_osdynsym &&
3396 ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
3397 if (make_dynsym_shndx(ofl) == S_ERROR)
3398 return (S_ERROR);
3401 if (make_strtab(ofl) == S_ERROR)
3402 return (S_ERROR);
3403 if (make_symtab(ofl) == S_ERROR)
3404 return (S_ERROR);
3405 } else {
3407 * Do we need to make a SHT_SYMTAB_SHNDX section
3408 * for the dynsym. If so - do it now.
3410 if (ofl->ofl_osdynsym &&
3411 ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
3412 if (make_dynsym_shndx(ofl) == S_ERROR)
3413 return (S_ERROR);
3417 if (make_shstrtab(ofl) == S_ERROR)
3418 return (S_ERROR);
3421 * Now that we've created all output sections, adjust the size of the
3422 * SHT_SUNW_versym and SHT_SUNW_syminfo section, which are dependent on
3423 * the associated symbol table sizes.
3425 if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
3426 ulong_t cnt;
3427 Is_desc *isp;
3428 Os_desc *osp;
3430 if (OFL_IS_STATIC_OBJ(ofl))
3431 osp = ofl->ofl_ossymtab;
3432 else
3433 osp = ofl->ofl_osdynsym;
3435 isp = ld_os_first_isdesc(osp);
3436 cnt = (isp->is_shdr->sh_size / isp->is_shdr->sh_entsize);
3438 if (ofl->ofl_osversym)
3439 update_data_size(ofl->ofl_osversym, cnt);
3441 if (ofl->ofl_ossyminfo)
3442 update_data_size(ofl->ofl_ossyminfo, cnt);
3446 * Now that we've created all output sections, adjust the size of the
3447 * SHT_SUNW_capinfo, which is dependent on the associated symbol table
3448 * size.
3450 if (ofl->ofl_oscapinfo) {
3451 ulong_t cnt;
3454 * Symbol capabilities symbols are placed directly after the
3455 * STT_FILE symbol, section symbols, and any register symbols.
3456 * Effectively these are the first of any series of demoted
3457 * (scoped) symbols.
3459 if (OFL_IS_STATIC_OBJ(ofl))
3460 cnt = SYMTAB_ALL_CNT(ofl);
3461 else
3462 cnt = DYNSYM_ALL_CNT(ofl);
3464 update_data_size(ofl->ofl_oscapinfo, cnt);
3466 return (1);
3470 * Build an additional data section - used to back OBJT symbol definitions
3471 * added with a mapfile.
3473 Is_desc *
3474 ld_make_data(Ofl_desc *ofl, size_t size)
3476 Shdr *shdr;
3477 Elf_Data *data;
3478 Is_desc *isec;
3480 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
3481 &isec, &shdr, &data) == S_ERROR)
3482 return ((Is_desc *)S_ERROR);
3484 data->d_size = size;
3485 shdr->sh_size = (Xword)size;
3486 shdr->sh_flags |= SHF_WRITE;
3488 if (aplist_append(&ofl->ofl_mapdata, isec, AL_CNT_OFL_MAPSECS) == NULL)
3489 return ((Is_desc *)S_ERROR);
3491 return (isec);
3495 * Build an additional text section - used to back FUNC symbol definitions
3496 * added with a mapfile.
3498 Is_desc *
3499 ld_make_text(Ofl_desc *ofl, size_t size)
3501 Shdr *shdr;
3502 Elf_Data *data;
3503 Is_desc *isec;
3506 * Insure the size is sufficient to contain the minimum return
3507 * instruction.
3509 if (size < ld_targ.t_nf.nf_size)
3510 size = ld_targ.t_nf.nf_size;
3512 if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0,
3513 &isec, &shdr, &data) == S_ERROR)
3514 return ((Is_desc *)S_ERROR);
3516 data->d_size = size;
3517 shdr->sh_size = (Xword)size;
3518 shdr->sh_flags |= SHF_EXECINSTR;
3521 * Fill the buffer with the appropriate return instruction.
3522 * Note that there is no need to swap bytes on a non-native,
3523 * link, as the data being copied is given in bytes.
3525 if ((data->d_buf = libld_calloc(size, 1)) == NULL)
3526 return ((Is_desc *)S_ERROR);
3527 (void) memcpy(data->d_buf, ld_targ.t_nf.nf_template,
3528 ld_targ.t_nf.nf_size);
3531 * If size was larger than required, and the target supplies
3532 * a fill function, use it to fill the balance. If there is no
3533 * fill function, we accept the 0-fill supplied by libld_calloc().
3535 if ((ld_targ.t_ff.ff_execfill != NULL) && (size > ld_targ.t_nf.nf_size))
3536 ld_targ.t_ff.ff_execfill(data->d_buf, ld_targ.t_nf.nf_size,
3537 size - ld_targ.t_nf.nf_size);
3539 if (aplist_append(&ofl->ofl_maptext, isec, AL_CNT_OFL_MAPSECS) == NULL)
3540 return ((Is_desc *)S_ERROR);
3542 return (isec);
3545 void
3546 ld_comdat_validate(Ofl_desc *ofl, Ifl_desc *ifl)
3548 int i;
3550 for (i = 0; i < ifl->ifl_shnum; i++) {
3551 Is_desc *isp = ifl->ifl_isdesc[i];
3552 int types = 0;
3553 char buf[1024] = "";
3554 Group_desc *gr = NULL;
3556 if ((isp == NULL) || (isp->is_flags & FLG_IS_COMDAT) == 0)
3557 continue;
3559 if (isp->is_shdr->sh_type == SHT_SUNW_COMDAT) {
3560 types++;
3561 (void) strlcpy(buf, MSG_ORIG(MSG_STR_SUNW_COMDAT),
3562 sizeof (buf));
3565 if (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE), isp->is_name,
3566 MSG_SCN_GNU_LINKONCE_SIZE) == 0) {
3567 types++;
3568 if (types > 1)
3569 (void) strlcat(buf, ", ", sizeof (buf));
3570 (void) strlcat(buf, MSG_ORIG(MSG_SCN_GNU_LINKONCE),
3571 sizeof (buf));
3574 if ((isp->is_shdr->sh_flags & SHF_GROUP) &&
3575 ((gr = ld_get_group(ofl, isp)) != NULL) &&
3576 (gr->gd_data[0] & GRP_COMDAT)) {
3577 types++;
3578 if (types > 1)
3579 (void) strlcat(buf, ", ", sizeof (buf));
3580 (void) strlcat(buf, MSG_ORIG(MSG_STR_GROUP),
3581 sizeof (buf));
3584 if (types > 1)
3585 ld_eprintf(ofl, ERR_FATAL,
3586 MSG_INTL(MSG_SCN_MULTICOMDAT), ifl->ifl_name,
3587 EC_WORD(isp->is_scnndx), isp->is_name, buf);