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]
23 * Copyright (c) 1988 AT&T
26 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Module sections. Initialize special sections
33 #define ELF_TARGET_AMD64
44 remove_local(Ofl_desc
*ofl
, Sym_desc
*sdp
, int allow_ldynsym
)
46 Sym
*sym
= sdp
->sd_sym
;
47 uchar_t type
= ELF_ST_TYPE(sym
->st_info
);
48 /* LINTED - only used for assert() */
51 if ((ofl
->ofl_flags
& FLG_OF_REDLSYM
) == 0) {
54 err
= st_delstring(ofl
->ofl_strtab
, sdp
->sd_name
);
57 if (allow_ldynsym
&& ldynsym_symtype
[type
]) {
58 ofl
->ofl_dynlocscnt
--;
60 err
= st_delstring(ofl
->ofl_dynstrtab
, sdp
->sd_name
);
62 /* Remove from sort section? */
63 DYNSORT_COUNT(sdp
, sym
, type
, --);
66 sdp
->sd_flags
|= FLG_SY_ISDISC
;
70 remove_scoped(Ofl_desc
*ofl
, Sym_desc
*sdp
, int allow_ldynsym
)
72 Sym
*sym
= sdp
->sd_sym
;
73 uchar_t type
= ELF_ST_TYPE(sym
->st_info
);
74 /* LINTED - only used for assert() */
80 err
= st_delstring(ofl
->ofl_strtab
, sdp
->sd_name
);
83 if (allow_ldynsym
&& ldynsym_symtype
[type
]) {
84 ofl
->ofl_dynscopecnt
--;
86 err
= st_delstring(ofl
->ofl_dynstrtab
, sdp
->sd_name
);
88 /* Remove from sort section? */
89 DYNSORT_COUNT(sdp
, sym
, type
, --);
91 sdp
->sd_flags
|= FLG_SY_ELIM
;
95 ignore_sym(Ofl_desc
*ofl
, Ifl_desc
*ifl
, Sym_desc
*sdp
, int allow_ldynsym
)
98 Is_desc
*isp
= sdp
->sd_isc
;
99 uchar_t bind
= ELF_ST_BIND(sdp
->sd_sym
->st_info
);
101 if (bind
== STB_LOCAL
) {
102 uchar_t type
= ELF_ST_TYPE(sdp
->sd_sym
->st_info
);
105 * Skip section symbols, these were never collected in the
108 if (type
== STT_SECTION
)
112 * Determine if the whole file is being removed. Remove any
113 * file symbol, and any symbol that is not associated with a
114 * section, provided the symbol has not been identified as
117 if (((ifl
->ifl_flags
& FLG_IF_FILEREF
) == 0) &&
118 ((type
== STT_FILE
) || ((isp
== NULL
) &&
119 ((sdp
->sd_flags
& FLG_SY_UPREQD
) == 0)))) {
120 DBG_CALL(Dbg_syms_discarded(ofl
->ofl_lml
, sdp
));
121 if (ifl
->ifl_flags
& FLG_IF_IGNORE
)
122 remove_local(ofl
, sdp
, allow_ldynsym
);
128 * Global symbols can only be eliminated when the interfaces of
129 * an object have been defined via versioning/scoping.
131 if (!SYM_IS_HIDDEN(sdp
))
135 * Remove any unreferenced symbols that are not associated with
138 if ((isp
== NULL
) && ((sdp
->sd_flags
& FLG_SY_UPREQD
) == 0)) {
139 DBG_CALL(Dbg_syms_discarded(ofl
->ofl_lml
, sdp
));
140 if (ifl
->ifl_flags
& FLG_IF_IGNORE
)
141 remove_scoped(ofl
, sdp
, allow_ldynsym
);
147 * Do not discard any symbols that are associated with non-allocable
150 if (isp
&& ((isp
->is_flags
& FLG_IS_SECTREF
) == 0) &&
151 ((osp
= isp
->is_osdesc
) != 0) &&
152 (osp
->os_sgdesc
->sg_phdr
.p_type
== PT_LOAD
)) {
153 DBG_CALL(Dbg_syms_discarded(ofl
->ofl_lml
, sdp
));
154 if (ifl
->ifl_flags
& FLG_IF_IGNORE
) {
155 if (bind
== STB_LOCAL
)
156 remove_local(ofl
, sdp
, allow_ldynsym
);
158 remove_scoped(ofl
, sdp
, allow_ldynsym
);
164 isdesc_discarded(Is_desc
*isp
)
166 Ifl_desc
*ifl
= isp
->is_file
;
167 Os_desc
*osp
= isp
->is_osdesc
;
168 Word ptype
= osp
->os_sgdesc
->sg_phdr
.p_type
;
170 if (isp
->is_flags
& FLG_IS_DISCARD
)
174 * If the file is discarded, it will take
175 * the section with it.
178 (((ifl
->ifl_flags
& FLG_IF_FILEREF
) == 0) ||
179 ((ptype
== PT_LOAD
) &&
180 ((isp
->is_flags
& FLG_IS_SECTREF
) == 0) &&
181 (isp
->is_shdr
->sh_size
> 0))) &&
182 (ifl
->ifl_flags
& FLG_IF_IGNORE
))
189 * There are situations where we may count output sections (ofl_shdrcnt)
190 * that are subsequently eliminated from the output object. Whether or
191 * not this happens cannot be known until all input has been seen and
192 * section elimination code has run. However, the situations where this
193 * outcome is possible are known, and are flagged by setting FLG_OF_ADJOSCNT.
195 * If FLG_OF_ADJOSCNT is set, this routine makes a pass over the output
196 * sections. If an unused output section is encountered, we decrement
197 * ofl->ofl_shdrcnt and remove the section name from the .shstrtab string
198 * table (ofl->ofl_shdrsttab).
200 * This code must be kept in sync with the similar code
201 * found in outfile.c:ld_create_outfile().
204 adjust_os_count(Ofl_desc
*ofl
)
211 if ((ofl
->ofl_flags
& FLG_OF_ADJOSCNT
) == 0)
215 * For each output section, look at the input sections to find at least
216 * one input section that has not been eliminated. If none are found,
217 * the -z ignore processing above has eliminated that output section.
219 for (APLIST_TRAVERSE(ofl
->ofl_segs
, idx1
, sgp
)) {
222 for (APLIST_TRAVERSE(sgp
->sg_osdescs
, idx2
, osp
)) {
224 int keep
= 0, os_isdescs_idx
;
226 OS_ISDESCS_TRAVERSE(os_isdescs_idx
, osp
, idx3
, isp
) {
228 * We have found a kept input section,
229 * so the output section will be created.
231 if (!isdesc_discarded(isp
)) {
237 * If no section of this name was kept, decrement
238 * the count and remove the name from .shstrtab.
241 /* LINTED - only used for assert() */
245 err
= st_delstring(ofl
->ofl_shdrsttab
,
254 * If -zignore has been in effect, scan all input files to determine if the
255 * file, or sections from the file, have been referenced. If not, the file or
256 * some of the files sections can be discarded. If sections are to be
257 * discarded, rescan the output relocations and the symbol table and remove
258 * the relocations and symbol entries that are no longer required.
260 * Note: It's possible that a section which is being discarded has contributed
261 * to the GOT table or the PLT table. However, we can't at this point
262 * eliminate the corresponding entries. This is because there could well
263 * be other sections referencing those same entries, but we don't have
264 * the infrastructure to determine this. So, keep the PLT and GOT
265 * entries in the table in case someone wants them.
266 * Note: The section to be affected needs to be allocatable.
267 * So even if -zignore is in effect, if the section is not allocatable,
268 * we do not eliminate it.
271 ignore_section_processing(Ofl_desc
*ofl
)
279 int allow_ldynsym
= OFL_ALLOW_LDYNSYM(ofl
);
282 for (APLIST_TRAVERSE(ofl
->ofl_objs
, idx1
, ifl
)) {
286 * Diagnose (-D unused) a completely unreferenced file.
288 if ((ifl
->ifl_flags
& FLG_IF_FILEREF
) == 0)
289 DBG_CALL(Dbg_unused_file(ofl
->ofl_lml
,
290 ifl
->ifl_name
, 0, 0));
291 if (((ofl
->ofl_flags1
& FLG_OF1_IGNPRC
) == 0) ||
292 ((ifl
->ifl_flags
& FLG_IF_IGNORE
) == 0))
296 * Before scanning the whole symbol table to determine if
297 * symbols should be discard - quickly (relatively) scan the
298 * sections to determine if any are to be discarded.
301 if (ifl
->ifl_flags
& FLG_IF_FILEREF
) {
302 for (num
= 1; num
< ifl
->ifl_shnum
; num
++) {
303 if (((isp
= ifl
->ifl_isdesc
[num
]) != NULL
) &&
304 ((isp
->is_flags
& FLG_IS_SECTREF
) == 0) &&
305 ((osp
= isp
->is_osdesc
) != NULL
) &&
306 ((sgp
= osp
->os_sgdesc
) != NULL
) &&
307 (sgp
->sg_phdr
.p_type
== PT_LOAD
)) {
315 * No sections are to be 'ignored'
317 if ((discard
== 0) && (ifl
->ifl_flags
& FLG_IF_FILEREF
))
321 * We know that we have discarded sections. Scan the symbol
322 * table for this file to determine if symbols need to be
323 * discarded that are associated with the 'ignored' sections.
325 for (num
= 1; num
< ifl
->ifl_symscnt
; num
++) {
329 * If the symbol definition has been resolved to another
330 * file, or the symbol has already been discarded or
331 * eliminated, skip it.
333 sdp
= ifl
->ifl_oldndx
[num
];
334 if ((sdp
->sd_file
!= ifl
) ||
336 (FLG_SY_ISDISC
| FLG_SY_INVALID
| FLG_SY_ELIM
)))
340 * Complete the investigation of the symbol.
342 ignore_sym(ofl
, ifl
, sdp
, allow_ldynsym
);
347 * If we were only here to solicit debugging diagnostics, we're done.
349 if ((ofl
->ofl_flags1
& FLG_OF1_IGNPRC
) == 0)
353 * Scan all output relocations searching for those against discarded or
354 * ignored sections. If one is found, decrement the total outrel count.
356 REL_CACHE_TRAVERSE(&ofl
->ofl_outrels
, idx1
, rcbp
, rsp
) {
357 Is_desc
*isc
= rsp
->rel_isdesc
;
358 uint_t flags
, entsize
;
361 if ((isc
== NULL
) || ((isc
->is_flags
& (FLG_IS_SECTREF
))) ||
362 ((ifl
= isc
->is_file
) == NULL
) ||
363 ((ifl
->ifl_flags
& FLG_IF_IGNORE
) == 0) ||
364 ((shdr
= isc
->is_shdr
) == NULL
) ||
365 ((shdr
->sh_flags
& SHF_ALLOC
) == 0))
368 flags
= rsp
->rel_flags
;
370 if (flags
& (FLG_REL_GOT
| FLG_REL_BSS
|
371 FLG_REL_NOINFO
| FLG_REL_PLT
))
374 osp
= RELAUX_GET_OSDESC(rsp
);
376 if (rsp
->rel_flags
& FLG_REL_RELA
)
377 entsize
= sizeof (Rela
);
379 entsize
= sizeof (Rel
);
381 assert(osp
->os_szoutrels
> 0);
382 osp
->os_szoutrels
-= entsize
;
384 if (!(flags
& FLG_REL_PLT
))
385 ofl
->ofl_reloccntsub
++;
387 if (rsp
->rel_rtype
== ld_targ
.t_m
.m_r_relative
)
388 ofl
->ofl_relocrelcnt
--;
392 * As a result of our work here, the number of output sections may
393 * have decreased. Trigger a call to adjust_os_count().
395 ofl
->ofl_flags
|= FLG_OF_ADJOSCNT
;
401 * Allocate Elf_Data, Shdr, and Is_desc structures for a new
405 * ofl - Output file descriptor
406 * shtype - SHT_ type code for section.
407 * shname - String giving the name for the new section.
408 * entcnt - # of items contained in the data part of the new section.
409 * This value is multiplied against the known element size
410 * for the section type to determine the size of the data
411 * area for the section. It is only meaningful in cases where
412 * the section type has a non-zero element size. In other cases,
413 * the caller must set the size fields in the *ret_data and
414 * *ret_shdr structs manually.
415 * ret_isec, ret_shdr, ret_data - Address of pointers to
416 * receive address of newly allocated structs.
419 * On error, returns S_ERROR. On success, returns (1), and the
420 * ret_ pointers have been updated to point at the new structures,
421 * which have been filled in. To finish the task, the caller must
422 * update any fields within the supplied descriptors that differ
423 * from its needs, and then call ld_place_section().
426 new_section(Ofl_desc
*ofl
, Word shtype
, const char *shname
, Xword entcnt
,
427 Is_desc
**ret_isec
, Shdr
**ret_shdr
, Elf_Data
**ret_data
)
429 typedef struct sec_info
{
431 Word align
; /* Used in both data and section header */
436 const SEC_INFO_T
*sec_info
;
444 * For each type of section, we have a distinct set of
445 * SEC_INFO_T values. This macro defines a static structure
446 * containing those values and generates code to set the sec_info
447 * pointer to refer to it. The pointer in sec_info remains valid
448 * outside of the declaration scope because the info_s struct is static.
450 * We can't determine the value of M_WORD_ALIGN at compile time, so
451 * a different variant is used for those cases.
453 #define SET_SEC_INFO(d_type, d_align, sh_flags, sh_entsize) \
455 static const SEC_INFO_T info_s = { d_type, d_align, sh_flags, \
457 sec_info = &info_s; \
459 #define SET_SEC_INFO_WORD_ALIGN(d_type, sh_flags, sh_entsize) \
461 static SEC_INFO_T info_s = { d_type, 0, sh_flags, \
463 info_s.align = ld_targ.t_m.m_word_align; \
464 sec_info = &info_s; \
470 * SHT_PROGBITS sections contain are used for many
471 * different sections. Alignments and flags differ.
472 * Some have a standard entsize, and others don't.
473 * We set some defaults here, but there is no expectation
474 * that they are correct or complete for any specific
475 * purpose. The caller must provide the correct values.
477 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE
, SHF_ALLOC
, 0)
481 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM
, 0, sizeof (Sym
))
485 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM
, SHF_ALLOC
, sizeof (Sym
))
488 case SHT_SUNW_LDYNSYM
:
489 ofl
->ofl_flags
|= FLG_OF_OSABI
;
490 SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM
, SHF_ALLOC
, sizeof (Sym
))
495 * A string table may or may not be allocable, depending
496 * on context, so we leave that flag unset and leave it to
497 * the caller to add it if necessary.
499 * String tables do not have a standard entsize, so
502 SET_SEC_INFO(ELF_T_BYTE
, 1, SHF_STRINGS
, 0)
507 * Relocations with an addend (Everything except 32-bit X86).
508 * The caller is expected to set all section header flags.
510 SET_SEC_INFO_WORD_ALIGN(ELF_T_RELA
, 0, sizeof (Rela
))
515 * Relocations without an addend (32-bit X86 only).
516 * The caller is expected to set all section header flags.
518 SET_SEC_INFO_WORD_ALIGN(ELF_T_REL
, 0, sizeof (Rel
))
522 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD
, SHF_ALLOC
, sizeof (Word
))
525 case SHT_SUNW_symsort
:
526 case SHT_SUNW_tlssort
:
527 ofl
->ofl_flags
|= FLG_OF_OSABI
;
528 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD
, SHF_ALLOC
, sizeof (Word
))
533 * A dynamic section may or may not be allocable, and may or
534 * may not be writable, depending on context, so we leave the
535 * flags unset and leave it to the caller to add them if
538 SET_SEC_INFO_WORD_ALIGN(ELF_T_DYN
, 0, sizeof (Dyn
))
543 * SHT_NOBITS is used for BSS-type sections. The size and
544 * alignment depend on the specific use and must be adjusted
547 SET_SEC_INFO(ELF_T_BYTE
, 0, SHF_ALLOC
| SHF_WRITE
, 0)
552 case SHT_PREINIT_ARRAY
:
553 SET_SEC_INFO(ELF_T_ADDR
, sizeof (Addr
), SHF_ALLOC
| SHF_WRITE
,
557 case SHT_SYMTAB_SHNDX
:
559 * Note that these sections are created to be associated
560 * with both symtab and dynsym symbol tables. However, they
561 * are non-allocable in all cases, because the runtime
562 * linker has no need for this information. It is purely
563 * informational, used by elfdump(1), debuggers, etc.
565 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD
, 0, sizeof (Word
));
569 ofl
->ofl_flags
|= FLG_OF_OSABI
;
570 SET_SEC_INFO_WORD_ALIGN(ELF_T_CAP
, SHF_ALLOC
, sizeof (Cap
));
573 case SHT_SUNW_capchain
:
574 ofl
->ofl_flags
|= FLG_OF_OSABI
;
575 SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD
, SHF_ALLOC
,
579 case SHT_SUNW_capinfo
:
580 ofl
->ofl_flags
|= FLG_OF_OSABI
;
582 SET_SEC_INFO(ELF_T_XWORD
, sizeof (Xword
), SHF_ALLOC
,
585 SET_SEC_INFO(ELF_T_WORD
, sizeof (Word
), SHF_ALLOC
,
591 ofl
->ofl_flags
|= FLG_OF_OSABI
;
592 SET_SEC_INFO(ELF_T_BYTE
, sizeof (Lword
),
593 SHF_ALLOC
| SHF_WRITE
, sizeof (Move
));
596 case SHT_SUNW_syminfo
:
597 ofl
->ofl_flags
|= FLG_OF_OSABI
;
599 * The sh_info field of the SHT_*_syminfo section points
600 * to the header index of the associated .dynamic section,
601 * so we also set SHF_INFO_LINK.
603 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE
,
604 SHF_ALLOC
| SHF_INFO_LINK
, sizeof (Syminfo
));
607 case SHT_SUNW_verneed
:
608 case SHT_SUNW_verdef
:
609 ofl
->ofl_flags
|= FLG_OF_OSABI
;
611 * The info for verneed and versym happen to be the same.
612 * The entries in these sections are not of uniform size,
613 * so we set the entsize to 0.
615 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE
, SHF_ALLOC
, 0);
618 case SHT_SUNW_versym
:
619 ofl
->ofl_flags
|= FLG_OF_OSABI
;
620 SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE
, SHF_ALLOC
,
625 /* Should not happen: fcn called with unknown section type */
630 #undef SET_SEC_INFO_WORD_ALIGN
632 size
= entcnt
* sec_info
->sh_entsize
;
635 * Allocate and initialize the Elf_Data structure.
637 if ((data
= libld_calloc(sizeof (Elf_Data
), 1)) == NULL
)
639 data
->d_type
= sec_info
->d_type
;
641 data
->d_align
= sec_info
->align
;
642 data
->d_version
= ofl
->ofl_dehdr
->e_version
;
645 * Allocate and initialize the Shdr structure.
647 if ((shdr
= libld_calloc(sizeof (Shdr
), 1)) == NULL
)
649 shdr
->sh_type
= shtype
;
650 shdr
->sh_size
= size
;
651 shdr
->sh_flags
= sec_info
->sh_flags
;
652 shdr
->sh_addralign
= sec_info
->align
;
653 shdr
->sh_entsize
= sec_info
->sh_entsize
;
656 * Allocate and initialize the Is_desc structure.
658 if ((isec
= libld_calloc(1, sizeof (Is_desc
))) == NULL
)
660 isec
->is_name
= shname
;
661 isec
->is_shdr
= shdr
;
662 isec
->is_indata
= data
;
672 * Use an existing input section as a template to create a new
673 * input section with the same values as the original, other than
674 * the size of the data area which is supplied by the caller.
677 * ofl - Output file descriptor
678 * ifl - Input file section to use as a template
679 * size - Size of data area for new section
680 * ret_isec, ret_shdr, ret_data - Address of pointers to
681 * receive address of newly allocated structs.
684 * On error, returns S_ERROR. On success, returns (1), and the
685 * ret_ pointers have been updated to point at the new structures,
686 * which have been filled in. To finish the task, the caller must
687 * update any fields within the supplied descriptors that differ
688 * from its needs, and then call ld_place_section().
691 new_section_from_template(Ofl_desc
*ofl
, Is_desc
*tmpl_isp
, size_t size
,
692 Is_desc
**ret_isec
, Shdr
**ret_shdr
, Elf_Data
**ret_data
)
699 * Allocate and initialize the Elf_Data structure.
701 if ((data
= libld_calloc(sizeof (Elf_Data
), 1)) == NULL
)
703 data
->d_type
= tmpl_isp
->is_indata
->d_type
;
705 data
->d_align
= tmpl_isp
->is_shdr
->sh_addralign
;
706 data
->d_version
= ofl
->ofl_dehdr
->e_version
;
709 * Allocate and initialize the Shdr structure.
711 if ((shdr
= libld_malloc(sizeof (Shdr
))) == NULL
)
713 *shdr
= *tmpl_isp
->is_shdr
;
716 shdr
->sh_size
= size
;
719 * Allocate and initialize the Is_desc structure.
721 if ((isec
= libld_calloc(1, sizeof (Is_desc
))) == NULL
)
723 isec
->is_name
= tmpl_isp
->is_name
;
724 isec
->is_shdr
= shdr
;
725 isec
->is_indata
= data
;
735 * Build a .bss section for allocation of tentative definitions. Any `static'
736 * .bss definitions would have been associated to their own .bss sections and
737 * thus collected from the input files. `global' .bss definitions are tagged
738 * as COMMON and do not cause any associated .bss section elements to be
739 * generated. Here we add up all these COMMON symbols and generate the .bss
740 * section required to represent them.
743 ld_make_bss(Ofl_desc
*ofl
, Xword size
, Xword align
, uint_t ident
)
749 Xword rsize
= (Xword
)ofl
->ofl_relocbsssz
;
752 * Allocate header structs. We will set the name ourselves below,
753 * and there is no entcnt for a BSS. So, the shname and entcnt
756 if (new_section(ofl
, SHT_NOBITS
, NULL
, 0,
757 &isec
, &shdr
, &data
) == S_ERROR
)
760 data
->d_size
= (size_t)size
;
761 data
->d_align
= (size_t)align
;
763 shdr
->sh_size
= size
;
764 shdr
->sh_addralign
= align
;
766 if (ident
== ld_targ
.t_id
.id_tlsbss
) {
767 isec
->is_name
= MSG_ORIG(MSG_SCN_TBSS
);
768 ofl
->ofl_istlsbss
= isec
;
769 shdr
->sh_flags
|= SHF_TLS
;
771 } else if (ident
== ld_targ
.t_id
.id_bss
) {
772 isec
->is_name
= MSG_ORIG(MSG_SCN_BSS
);
773 ofl
->ofl_isbss
= isec
;
776 } else if ((ld_targ
.t_m
.m_mach
== EM_AMD64
) &&
777 (ident
== ld_targ
.t_id
.id_lbss
)) {
778 isec
->is_name
= MSG_ORIG(MSG_SCN_LBSS
);
779 ofl
->ofl_islbss
= isec
;
780 shdr
->sh_flags
|= SHF_AMD64_LARGE
;
785 * Retain this .*bss input section as this will be where global symbol
786 * references are added.
788 if ((osp
= ld_place_section(ofl
, isec
, NULL
, ident
, NULL
)) ==
793 * If relocations exist against a .*bss section, a section symbol must
794 * be created for the section in the .dynsym symbol table.
796 if (!(osp
->os_flags
& FLG_OS_OUTREL
)) {
797 ofl_flag_t flagtotest
;
799 if (ident
== ld_targ
.t_id
.id_tlsbss
)
800 flagtotest
= FLG_OF1_TLSOREL
;
802 flagtotest
= FLG_OF1_BSSOREL
;
804 if (ofl
->ofl_flags1
& flagtotest
) {
805 ofl
->ofl_dynshdrcnt
++;
806 osp
->os_flags
|= FLG_OS_OUTREL
;
810 osp
->os_szoutrels
= rsize
;
815 * Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via
816 * ld -z *array=name).
819 make_array(Ofl_desc
*ofl
, Word shtype
, const char *sectname
, APlist
*alp
)
836 for (APLIST_TRAVERSE(alp
, idx
, sdp
))
839 if (new_section(ofl
, shtype
, sectname
, entcount
, &isec
, &shdr
, &data
) ==
843 if ((data
->d_buf
= libld_calloc(sizeof (Addr
), entcount
)) == NULL
)
846 if (ld_place_section(ofl
, isec
, NULL
, ld_targ
.t_id
.id_array
, NULL
) ==
850 osp
= isec
->is_osdesc
;
852 if ((ofl
->ofl_osinitarray
== NULL
) && (shtype
== SHT_INIT_ARRAY
))
853 ofl
->ofl_osinitarray
= osp
;
854 if ((ofl
->ofl_ospreinitarray
== NULL
) && (shtype
== SHT_PREINIT_ARRAY
))
855 ofl
->ofl_ospreinitarray
= osp
;
856 else if ((ofl
->ofl_osfiniarray
== NULL
) && (shtype
== SHT_FINI_ARRAY
))
857 ofl
->ofl_osfiniarray
= osp
;
860 * Create relocations against this section to initialize it to the
861 * function addresses.
863 reld
.rel_isdesc
= isec
;
865 reld
.rel_flags
= FLG_REL_LOAD
;
868 * Fabricate the relocation information (as if a relocation record had
869 * been input - see init_rel()).
871 reld
.rel_rtype
= ld_targ
.t_m
.m_r_arrayaddr
;
872 reld
.rel_roffset
= 0;
873 reld
.rel_raddend
= 0;
876 * Create a minimal relocation record to satisfy process_sym_reloc()
877 * debugging requirements.
880 reloc
.r_info
= ELF_R_INFO(0, ld_targ
.t_m
.m_r_arrayaddr
);
883 DBG_CALL(Dbg_reloc_generate(ofl
->ofl_lml
, osp
,
884 ld_targ
.t_m
.m_rel_sht_type
));
885 for (APLIST_TRAVERSE(alp
, idx
, sdp
)) {
888 if (ld_process_sym_reloc(ofl
, &reld
, (Rel
*)&reloc
, isec
,
889 MSG_INTL(MSG_STR_COMMAND
), 0) == S_ERROR
) {
894 reld
.rel_roffset
+= (Xword
)sizeof (Addr
);
895 reloc
.r_offset
= reld
.rel_roffset
;
902 * Build a comment section (-Qy option).
905 make_comment(Ofl_desc
*ofl
)
911 if (new_section(ofl
, SHT_PROGBITS
, MSG_ORIG(MSG_SCN_COMMENT
), 0,
912 &isec
, &shdr
, &data
) == S_ERROR
)
915 data
->d_buf
= (void *)ofl
->ofl_sgsid
;
916 data
->d_size
= strlen(ofl
->ofl_sgsid
) + 1;
919 shdr
->sh_size
= (Xword
)data
->d_size
;
921 shdr
->sh_addralign
= 1;
923 return ((uintptr_t)ld_place_section(ofl
, isec
, NULL
,
924 ld_targ
.t_id
.id_note
, NULL
));
928 * Make the dynamic section. Calculate the size of any strings referenced
929 * within this structure, they will be added to the global string table
930 * (.dynstr). This routine should be called before make_dynstr().
932 * This routine must be maintained in parallel with update_odynamic()
936 make_dynamic(Ofl_desc
*ofl
)
948 ofl_flag_t flags
= ofl
->ofl_flags
;
949 int not_relobj
= !(flags
& FLG_OF_RELOBJ
);
953 * Select the required string table.
955 if (OFL_IS_STATIC_OBJ(ofl
))
956 strtbl
= ofl
->ofl_strtab
;
958 strtbl
= ofl
->ofl_dynstrtab
;
961 * Only a limited subset of DT_ entries apply to relocatable
962 * objects. See the comment at the head of update_odynamic() in
963 * update.c for details.
965 if (new_section(ofl
, SHT_DYNAMIC
, MSG_ORIG(MSG_SCN_DYNAMIC
), 0,
966 &isec
, &shdr
, &data
) == S_ERROR
)
970 * new_section() does not set SHF_ALLOC. If we're building anything
971 * besides a relocatable object, then the .dynamic section should
972 * reside in allocatable memory.
975 shdr
->sh_flags
|= SHF_ALLOC
;
978 * new_section() does not set SHF_WRITE. If we're building an object
979 * that specifies an interpretor, then a DT_DEBUG entry is created,
980 * which is initialized to the applications link-map list at runtime.
982 if (ofl
->ofl_osinterp
)
983 shdr
->sh_flags
|= SHF_WRITE
;
985 osp
= ofl
->ofl_osdynamic
=
986 ld_place_section(ofl
, isec
, NULL
, ld_targ
.t_id
.id_dynamic
, NULL
);
989 * Reserve entries for any needed dependencies.
991 for (APLIST_TRAVERSE(ofl
->ofl_sos
, idx
, ifl
)) {
992 if (!(ifl
->ifl_flags
& (FLG_IF_NEEDED
| FLG_IF_NEEDSTR
)))
996 * If this dependency didn't satisfy any symbol references,
997 * generate a debugging diagnostic (ld(1) -Dunused can be used
998 * to display these). If this is a standard needed dependency,
999 * and -z ignore is in effect, drop the dependency. Explicitly
1000 * defined dependencies (i.e., -N dep) don't get dropped, and
1001 * are flagged as being required to simplify update_odynamic()
1004 if ((ifl
->ifl_flags
& FLG_IF_NEEDSTR
) ||
1005 ((ifl
->ifl_flags
& FLG_IF_DEPREQD
) == 0)) {
1007 DBG_CALL(Dbg_util_nl(ofl
->ofl_lml
, DBG_NL_STD
));
1008 DBG_CALL(Dbg_unused_file(ofl
->ofl_lml
, ifl
->ifl_soname
,
1009 (ifl
->ifl_flags
& FLG_IF_NEEDSTR
), 0));
1012 * Guidance: Remove unused dependency.
1014 * If -z ignore is in effect, this warning is not
1015 * needed because we will quietly remove the unused
1018 if (OFL_GUIDANCE(ofl
, FLG_OFG_NO_UNUSED
) &&
1019 ((ifl
->ifl_flags
& FLG_IF_IGNORE
) == 0))
1020 ld_eprintf(ofl
, ERR_GUIDANCE
,
1021 MSG_INTL(MSG_GUIDE_UNUSED
),
1024 if (ifl
->ifl_flags
& FLG_IF_NEEDSTR
)
1025 ifl
->ifl_flags
|= FLG_IF_DEPREQD
;
1026 else if (ifl
->ifl_flags
& FLG_IF_IGNORE
)
1031 * If this object requires a DT_POSFLAG_1 entry, reserve it.
1033 if ((ifl
->ifl_flags
& MSK_IF_POSFLAG1
) && not_relobj
)
1036 if (st_insert(strtbl
, ifl
->ifl_soname
) == -1)
1041 * If the needed entry contains the $ORIGIN token make sure
1042 * the associated DT_1_FLAGS entry is created.
1044 if (strstr(ifl
->ifl_soname
, MSG_ORIG(MSG_STR_ORIGIN
))) {
1045 ofl
->ofl_dtflags_1
|= DF_1_ORIGIN
;
1046 ofl
->ofl_dtflags
|= DF_ORIGIN
;
1051 DBG_CALL(Dbg_util_nl(ofl
->ofl_lml
, DBG_NL_STD
));
1055 * Reserve entries for any per-symbol auxiliary/filter strings.
1057 cnt
+= alist_nitems(ofl
->ofl_dtsfltrs
);
1060 * Reserve entries for _init() and _fini() section addresses.
1062 if (((sdp
= ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U
),
1063 SYM_NOHASH
, NULL
, ofl
)) != NULL
) &&
1064 (sdp
->sd_ref
== REF_REL_NEED
) &&
1065 (sdp
->sd_sym
->st_shndx
!= SHN_UNDEF
)) {
1066 sdp
->sd_flags
|= FLG_SY_UPREQD
;
1069 if (((sdp
= ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U
),
1070 SYM_NOHASH
, NULL
, ofl
)) != NULL
) &&
1071 (sdp
->sd_ref
== REF_REL_NEED
) &&
1072 (sdp
->sd_sym
->st_shndx
!= SHN_UNDEF
)) {
1073 sdp
->sd_flags
|= FLG_SY_UPREQD
;
1078 * Reserve entries for any soname, filter name (shared libs
1079 * only), run-path pointers, cache names and audit requirements.
1081 if (ofl
->ofl_soname
) {
1083 if (st_insert(strtbl
, ofl
->ofl_soname
) == -1)
1086 if (ofl
->ofl_filtees
) {
1088 if (st_insert(strtbl
, ofl
->ofl_filtees
) == -1)
1092 * If the filtees entry contains the $ORIGIN token
1093 * make sure the associated DT_1_FLAGS entry is created.
1095 if (strstr(ofl
->ofl_filtees
,
1096 MSG_ORIG(MSG_STR_ORIGIN
))) {
1097 ofl
->ofl_dtflags_1
|= DF_1_ORIGIN
;
1098 ofl
->ofl_dtflags
|= DF_ORIGIN
;
1103 if (ofl
->ofl_rpath
) {
1104 cnt
+= 2; /* DT_RPATH & DT_RUNPATH */
1105 if (st_insert(strtbl
, ofl
->ofl_rpath
) == -1)
1109 * If the rpath entry contains the $ORIGIN token make sure
1110 * the associated DT_1_FLAGS entry is created.
1112 if (strstr(ofl
->ofl_rpath
, MSG_ORIG(MSG_STR_ORIGIN
))) {
1113 ofl
->ofl_dtflags_1
|= DF_1_ORIGIN
;
1114 ofl
->ofl_dtflags
|= DF_ORIGIN
;
1122 if (ofl
->ofl_config
) {
1124 if (st_insert(strtbl
, ofl
->ofl_config
) == -1)
1128 * If the config entry contains the $ORIGIN token
1129 * make sure the associated DT_1_FLAGS entry is created.
1131 if (strstr(ofl
->ofl_config
, MSG_ORIG(MSG_STR_ORIGIN
))) {
1132 ofl
->ofl_dtflags_1
|= DF_1_ORIGIN
;
1133 ofl
->ofl_dtflags
|= DF_ORIGIN
;
1136 if (ofl
->ofl_depaudit
) {
1138 if (st_insert(strtbl
, ofl
->ofl_depaudit
) == -1)
1141 if (ofl
->ofl_audit
) {
1143 if (st_insert(strtbl
, ofl
->ofl_audit
) == -1)
1148 * Reserve entries for the DT_HASH, DT_STRTAB, DT_STRSZ,
1149 * DT_SYMTAB, DT_SYMENT, and DT_CHECKSUM.
1154 * If we are including local functions at the head of
1155 * the dynsym, then also reserve entries for DT_SUNW_SYMTAB
1156 * and DT_SUNW_SYMSZ.
1158 if (OFL_ALLOW_LDYNSYM(ofl
))
1161 if ((ofl
->ofl_dynsymsortcnt
> 0) ||
1162 (ofl
->ofl_dyntlssortcnt
> 0))
1163 cnt
++; /* DT_SUNW_SORTENT */
1165 if (ofl
->ofl_dynsymsortcnt
> 0)
1166 cnt
+= 2; /* DT_SUNW_[SYMSORT|SYMSORTSZ] */
1168 if (ofl
->ofl_dyntlssortcnt
> 0)
1169 cnt
+= 2; /* DT_SUNW_[TLSSORT|TLSSORTSZ] */
1171 if ((flags
& (FLG_OF_VERDEF
| FLG_OF_NOVERSEC
)) ==
1173 cnt
+= 2; /* DT_VERDEF & DT_VERDEFNUM */
1175 if ((flags
& (FLG_OF_VERNEED
| FLG_OF_NOVERSEC
)) ==
1177 cnt
+= 2; /* DT_VERNEED & DT_VERNEEDNUM */
1179 if ((flags
& FLG_OF_COMREL
) && ofl
->ofl_relocrelcnt
)
1180 cnt
++; /* DT_RELACOUNT */
1182 if (flags
& FLG_OF_TEXTREL
) /* DT_TEXTREL */
1185 if (ofl
->ofl_osfiniarray
) /* DT_FINI_ARRAY */
1186 cnt
+= 2; /* DT_FINI_ARRAYSZ */
1188 if (ofl
->ofl_osinitarray
) /* DT_INIT_ARRAY */
1189 cnt
+= 2; /* DT_INIT_ARRAYSZ */
1191 if (ofl
->ofl_ospreinitarray
) /* DT_PREINIT_ARRAY & */
1192 cnt
+= 2; /* DT_PREINIT_ARRAYSZ */
1195 * If we have plt's reserve a DT_PLTRELSZ, DT_PLTREL and
1198 if (ofl
->ofl_pltcnt
)
1202 * If plt padding is needed (Sparcv9).
1204 if (ofl
->ofl_pltpad
)
1205 cnt
+= 2; /* DT_PLTPAD & DT_PLTPADSZ */
1208 * If we have any relocations reserve a DT_REL, DT_RELSZ and
1211 if (ofl
->ofl_relocsz
)
1215 * If a syminfo section is required create DT_SYMINFO,
1216 * DT_SYMINSZ, and DT_SYMINENT entries.
1218 if (flags
& FLG_OF_SYMINFO
)
1222 * If there are any partially initialized sections allocate
1223 * DT_MOVETAB, DT_MOVESZ and DT_MOVEENT.
1225 if (ofl
->ofl_osmove
)
1229 * Allocate one DT_REGISTER entry for every register symbol.
1231 cnt
+= ofl
->ofl_regsymcnt
;
1234 * Reserve a entry for each '-zrtldinfo=...' specified
1235 * on the command line.
1237 for (APLIST_TRAVERSE(ofl
->ofl_rtldinfo
, idx
, sdp
))
1241 * The following entry should only be placed in a segment that
1244 if (((sgp
= osp
->os_sgdesc
) != NULL
) &&
1245 (sgp
->sg_phdr
.p_flags
& PF_W
) && ofl
->ofl_osinterp
)
1246 cnt
++; /* DT_DEBUG */
1249 * Capabilities require a .dynamic entry for the .SUNW_cap
1253 cnt
++; /* DT_SUNW_CAP */
1256 * Symbol capabilities require a .dynamic entry for the
1257 * .SUNW_capinfo section.
1259 if (ofl
->ofl_oscapinfo
)
1260 cnt
++; /* DT_SUNW_CAPINFO */
1263 * Capabilities chain information requires a .SUNW_capchain
1264 * entry (DT_SUNW_CAPCHAIN), entry size (DT_SUNW_CAPCHAINENT),
1265 * and total size (DT_SUNW_CAPCHAINSZ).
1267 if (ofl
->ofl_oscapchain
)
1270 if (flags
& FLG_OF_SYMBOLIC
)
1271 cnt
++; /* DT_SYMBOLIC */
1275 * Account for Architecture dependent .dynamic entries, and defaults.
1277 (*ld_targ
.t_mr
.mr_mach_make_dynamic
)(ofl
, &cnt
);
1280 * DT_FLAGS, DT_FLAGS_1, DT_SUNW_STRPAD, and DT_NULL. Also,
1281 * allow room for the unused extra DT_NULLs. These are included
1282 * to allow an ELF editor room to add items later.
1284 cnt
+= 4 + DYNAMIC_EXTRA_ELTS
;
1287 * DT_SUNW_LDMACH. Used to hold the ELF machine code of the
1288 * linker that produced the output object. This information
1289 * allows us to determine whether a given object was linked
1290 * natively, or by a linker running on a different type of
1291 * system. This information can be valuable if one suspects
1292 * that a problem might be due to alignment or byte order issues.
1297 * Determine the size of the section from the number of entries.
1299 size
= cnt
* (size_t)shdr
->sh_entsize
;
1301 shdr
->sh_size
= (Xword
)size
;
1302 data
->d_size
= size
;
1305 * There are several tags that are specific to the Solaris osabi
1306 * range which we unconditionally put into any dynamic section
1307 * we create (e.g. DT_SUNW_STRPAD or DT_SUNW_LDMACH). As such,
1308 * any Solaris object with a dynamic section should be tagged as
1311 ofl
->ofl_flags
|= FLG_OF_OSABI
;
1313 return ((uintptr_t)ofl
->ofl_osdynamic
);
1317 * Build the GOT section and its associated relocation entries.
1320 ld_make_got(Ofl_desc
*ofl
)
1325 size_t size
= (size_t)ofl
->ofl_gotcnt
* ld_targ
.t_m
.m_got_entsize
;
1326 size_t rsize
= (size_t)ofl
->ofl_relocgotsz
;
1328 if (new_section(ofl
, SHT_PROGBITS
, MSG_ORIG(MSG_SCN_GOT
), 0,
1329 &isec
, &shdr
, &data
) == S_ERROR
)
1332 data
->d_size
= size
;
1334 shdr
->sh_flags
|= SHF_WRITE
;
1335 shdr
->sh_size
= (Xword
)size
;
1336 shdr
->sh_entsize
= ld_targ
.t_m
.m_got_entsize
;
1338 ofl
->ofl_osgot
= ld_place_section(ofl
, isec
, NULL
,
1339 ld_targ
.t_id
.id_got
, NULL
);
1340 if (ofl
->ofl_osgot
== (Os_desc
*)S_ERROR
)
1343 ofl
->ofl_osgot
->os_szoutrels
= (Xword
)rsize
;
1349 * Build an interpreter section.
1352 make_interp(Ofl_desc
*ofl
)
1357 const char *iname
= ofl
->ofl_interp
;
1361 * If -z nointerp is in effect, don't create an interpreter section.
1363 if (ofl
->ofl_flags1
& FLG_OF1_NOINTRP
)
1367 * An .interp section is always created for a dynamic executable.
1368 * A user can define the interpreter to use. This definition overrides
1369 * the default that would be recorded in an executable, and triggers
1370 * the creation of an .interp section in any other object. Presumably
1371 * the user knows what they are doing. Refer to the generic ELF ABI
1372 * section 5-4, and the ld(1) -I option.
1374 if (((ofl
->ofl_flags
& (FLG_OF_DYNAMIC
| FLG_OF_EXEC
|
1375 FLG_OF_RELOBJ
)) != (FLG_OF_DYNAMIC
| FLG_OF_EXEC
)) && !iname
)
1379 * In the case of a dynamic executable, supply a default interpreter
1380 * if the user has not specified their own.
1383 iname
= ofl
->ofl_interp
= ld_targ
.t_m
.m_def_interp
;
1385 size
= strlen(iname
) + 1;
1387 if (new_section(ofl
, SHT_PROGBITS
, MSG_ORIG(MSG_SCN_INTERP
), 0,
1388 &isec
, &shdr
, &data
) == S_ERROR
)
1391 data
->d_size
= size
;
1392 shdr
->sh_size
= (Xword
)size
;
1393 data
->d_align
= shdr
->sh_addralign
= 1;
1396 ld_place_section(ofl
, isec
, NULL
, ld_targ
.t_id
.id_interp
, NULL
);
1397 return ((uintptr_t)ofl
->ofl_osinterp
);
1401 * Common function used to build the SHT_SUNW_versym section, SHT_SUNW_syminfo
1402 * section, and SHT_SUNW_capinfo section. Each of these sections provide
1403 * additional symbol information, and their size parallels the associated
1407 make_sym_sec(Ofl_desc
*ofl
, const char *sectname
, Word stype
, int ident
)
1414 * We don't know the size of this section yet, so set it to 0. The
1415 * size gets filled in after the associated symbol table is sized.
1417 if (new_section(ofl
, stype
, sectname
, 0, &isec
, &shdr
, &data
) ==
1419 return ((Os_desc
*)S_ERROR
);
1421 return (ld_place_section(ofl
, isec
, NULL
, ident
, NULL
));
1425 * Determine whether a symbol capability is redundant because the object
1426 * capabilities are more restrictive.
1429 is_cap_redundant(Objcapset
*ocapset
, Objcapset
*scapset
)
1432 elfcap_mask_t omsk
, smsk
;
1435 * Inspect any platform capabilities. If the object defines platform
1436 * capabilities, then the object will only be loaded for those
1437 * platforms. A symbol capability set that doesn't define the same
1438 * platforms is redundant, and a symbol capability that does not provide
1439 * at least one platform name that matches a platform name in the object
1440 * capabilities will never execute (as the object wouldn't have been
1443 oalp
= ocapset
->oc_plat
.cl_val
;
1444 salp
= scapset
->oc_plat
.cl_val
;
1445 if (oalp
&& ((salp
== NULL
) || cap_names_match(oalp
, salp
)))
1449 * If the symbol capability set defines platforms, and the object
1450 * doesn't, then the symbol set is more restrictive.
1452 if (salp
&& (oalp
== NULL
))
1456 * Next, inspect any machine name capabilities. If the object defines
1457 * machine name capabilities, then the object will only be loaded for
1458 * those machines. A symbol capability set that doesn't define the same
1459 * machine names is redundant, and a symbol capability that does not
1460 * provide at least one machine name that matches a machine name in the
1461 * object capabilities will never execute (as the object wouldn't have
1464 oalp
= ocapset
->oc_plat
.cl_val
;
1465 salp
= scapset
->oc_plat
.cl_val
;
1466 if (oalp
&& ((salp
== NULL
) || cap_names_match(oalp
, salp
)))
1470 * If the symbol capability set defines machine names, and the object
1471 * doesn't, then the symbol set is more restrictive.
1473 if (salp
&& (oalp
== NULL
))
1477 * Next, inspect any hardware capabilities. If the objects hardware
1478 * capabilities are greater than or equal to that of the symbols
1479 * capabilities, then the symbol capability set is redundant. If the
1480 * symbols hardware capabilities are greater that the objects, then the
1481 * symbol set is more restrictive.
1483 * Note that this is a somewhat arbitrary definition, as each capability
1484 * bit is independent of the others, and some of the higher order bits
1485 * could be considered to be less important than lower ones. However,
1486 * this is the only reasonable non-subjective definition.
1488 omsk
= ocapset
->oc_hw_2
.cm_val
;
1489 smsk
= scapset
->oc_hw_2
.cm_val
;
1490 if ((omsk
> smsk
) || (omsk
&& (omsk
== smsk
)))
1496 * Finally, inspect the remaining hardware capabilities.
1498 omsk
= ocapset
->oc_hw_1
.cm_val
;
1499 smsk
= scapset
->oc_hw_1
.cm_val
;
1500 if ((omsk
> smsk
) || (omsk
&& (omsk
== smsk
)))
1507 * Capabilities values might have been assigned excluded values. These
1508 * excluded values should be removed before calculating any capabilities
1512 capmask_value(Lm_list
*lml
, Word type
, Capmask
*capmask
, int *title
)
1515 * First determine whether any bits should be excluded.
1517 if ((capmask
->cm_val
& capmask
->cm_exc
) == 0)
1520 DBG_CALL(Dbg_cap_post_title(lml
, title
));
1522 DBG_CALL(Dbg_cap_val_entry(lml
, DBG_STATE_CURRENT
, type
,
1523 capmask
->cm_val
, ld_targ
.t_m
.m_mach
));
1524 DBG_CALL(Dbg_cap_val_entry(lml
, DBG_STATE_EXCLUDE
, type
,
1525 capmask
->cm_exc
, ld_targ
.t_m
.m_mach
));
1527 capmask
->cm_val
&= ~capmask
->cm_exc
;
1529 DBG_CALL(Dbg_cap_val_entry(lml
, DBG_STATE_RESOLVED
, type
,
1530 capmask
->cm_val
, ld_targ
.t_m
.m_mach
));
1534 capstr_value(Lm_list
*lml
, Word type
, Caplist
*caplist
, int *title
)
1539 Boolean found
= FALSE
;
1542 * First determine whether any strings should be excluded.
1544 for (APLIST_TRAVERSE(caplist
->cl_exc
, idx1
, estr
)) {
1545 for (ALIST_TRAVERSE(caplist
->cl_val
, idx2
, capstr
)) {
1546 if (strcmp(estr
, capstr
->cs_str
) == 0) {
1557 * Traverse the current strings, then delete the excluded strings,
1558 * and finally display the resolved strings.
1561 Dbg_cap_post_title(lml
, title
);
1562 for (ALIST_TRAVERSE(caplist
->cl_val
, idx2
, capstr
)) {
1563 Dbg_cap_ptr_entry(lml
, DBG_STATE_CURRENT
, type
,
1567 for (APLIST_TRAVERSE(caplist
->cl_exc
, idx1
, estr
)) {
1568 for (ALIST_TRAVERSE(caplist
->cl_val
, idx2
, capstr
)) {
1569 if (strcmp(estr
, capstr
->cs_str
) == 0) {
1570 DBG_CALL(Dbg_cap_ptr_entry(lml
,
1571 DBG_STATE_EXCLUDE
, type
, capstr
->cs_str
));
1572 alist_delete(caplist
->cl_val
, &idx2
);
1578 for (ALIST_TRAVERSE(caplist
->cl_val
, idx2
, capstr
)) {
1579 Dbg_cap_ptr_entry(lml
, DBG_STATE_RESOLVED
, type
,
1586 * Build a capabilities section.
1588 #define CAP_UPDATE(cap, capndx, tag, val) \
1590 cap->c_un.c_val = val; \
1594 make_cap(Ofl_desc
*ofl
, Word shtype
, const char *shname
, int ident
)
1603 Objcapset
*ocapset
= &ofl
->ofl_ocapset
;
1609 * Determine which string table to use for any CA_SUNW_MACH,
1610 * CA_SUNW_PLAT, or CA_SUNW_ID strings.
1612 if (OFL_IS_STATIC_OBJ(ofl
))
1613 strtbl
= ofl
->ofl_strtab
;
1615 strtbl
= ofl
->ofl_dynstrtab
;
1618 * If symbol capabilities have been requested, but none have been
1619 * created, warn the user. This scenario can occur if none of the
1620 * input relocatable objects defined any object capabilities.
1622 if ((ofl
->ofl_flags
& FLG_OF_OTOSCAP
) && (ofl
->ofl_capsymcnt
== 0))
1623 ld_eprintf(ofl
, ERR_WARNING
, MSG_INTL(MSG_CAP_NOSYMSFOUND
));
1626 * If symbol capabilities have been collected, but no symbols are left
1627 * referencing these capabilities, promote the capability groups back
1628 * to an object capability definition.
1630 if ((ofl
->ofl_flags
& FLG_OF_OTOSCAP
) && ofl
->ofl_capsymcnt
&&
1631 (ofl
->ofl_capfamilies
== NULL
)) {
1632 ld_eprintf(ofl
, ERR_WARNING
, MSG_INTL(MSG_CAP_NOSYMSFOUND
));
1633 ld_cap_move_symtoobj(ofl
);
1634 ofl
->ofl_capsymcnt
= 0;
1635 ofl
->ofl_capgroups
= NULL
;
1636 ofl
->ofl_flags
&= ~FLG_OF_OTOSCAP
;
1640 * Remove any excluded capabilities.
1642 capstr_value(ofl
->ofl_lml
, CA_SUNW_PLAT
, &ocapset
->oc_plat
, &title
);
1643 capstr_value(ofl
->ofl_lml
, CA_SUNW_MACH
, &ocapset
->oc_mach
, &title
);
1644 capmask_value(ofl
->ofl_lml
, CA_SUNW_HW_2
, &ocapset
->oc_hw_2
, &title
);
1645 capmask_value(ofl
->ofl_lml
, CA_SUNW_HW_1
, &ocapset
->oc_hw_1
, &title
);
1646 capmask_value(ofl
->ofl_lml
, CA_SUNW_SF_1
, &ocapset
->oc_sf_1
, &title
);
1649 * Determine how many entries are required for any object capabilities.
1651 size
+= alist_nitems(ocapset
->oc_plat
.cl_val
);
1652 size
+= alist_nitems(ocapset
->oc_mach
.cl_val
);
1653 if (ocapset
->oc_hw_2
.cm_val
)
1655 if (ocapset
->oc_hw_1
.cm_val
)
1657 if (ocapset
->oc_sf_1
.cm_val
)
1661 * Only identify a capabilities group if the group has content. If a
1662 * capabilities identifier exists, and no other capabilities have been
1663 * supplied, remove the identifier. This scenario could exist if a
1664 * user mistakenly defined a lone identifier, or if an identified group
1665 * was overridden so as to clear the existing capabilities and the
1666 * identifier was not also cleared.
1668 if (ocapset
->oc_id
.cs_str
) {
1672 ocapset
->oc_id
.cs_str
= NULL
;
1675 size
++; /* Add CA_SUNW_NULL */
1678 * Determine how many entries are required for any symbol capabilities.
1680 if (ofl
->ofl_capsymcnt
) {
1682 * If there are no object capabilities, a CA_SUNW_NULL entry
1683 * is required before any symbol capabilities.
1687 size
+= ofl
->ofl_capsymcnt
;
1693 if (new_section(ofl
, shtype
, shname
, size
, &isec
,
1694 &shdr
, &data
) == S_ERROR
)
1697 if ((data
->d_buf
= libld_malloc(shdr
->sh_size
)) == NULL
)
1700 cap
= (Cap
*)data
->d_buf
;
1703 * Fill in any object capabilities. If there is an identifier, then the
1704 * identifier comes first. The remaining items follow in precedence
1705 * order, although the order isn't important for runtime verification.
1707 if (ocapset
->oc_id
.cs_str
) {
1708 ofl
->ofl_flags
|= FLG_OF_CAPSTRS
;
1709 if (st_insert(strtbl
, ocapset
->oc_id
.cs_str
) == -1)
1711 ocapset
->oc_id
.cs_ndx
= capndx
;
1712 CAP_UPDATE(cap
, capndx
, CA_SUNW_ID
, 0);
1714 if (ocapset
->oc_plat
.cl_val
) {
1715 ofl
->ofl_flags
|= (FLG_OF_PTCAP
| FLG_OF_CAPSTRS
);
1718 * Insert any platform name strings in the appropriate string
1719 * table. The capability value can't be filled in yet, as the
1720 * final offset of the strings isn't known until later.
1722 for (ALIST_TRAVERSE(ocapset
->oc_plat
.cl_val
, idx1
, capstr
)) {
1723 if (st_insert(strtbl
, capstr
->cs_str
) == -1)
1725 capstr
->cs_ndx
= capndx
;
1726 CAP_UPDATE(cap
, capndx
, CA_SUNW_PLAT
, 0);
1729 if (ocapset
->oc_mach
.cl_val
) {
1730 ofl
->ofl_flags
|= (FLG_OF_PTCAP
| FLG_OF_CAPSTRS
);
1733 * Insert the machine name strings in the appropriate string
1734 * table. The capability value can't be filled in yet, as the
1735 * final offset of the strings isn't known until later.
1737 for (ALIST_TRAVERSE(ocapset
->oc_mach
.cl_val
, idx1
, capstr
)) {
1738 if (st_insert(strtbl
, capstr
->cs_str
) == -1)
1740 capstr
->cs_ndx
= capndx
;
1741 CAP_UPDATE(cap
, capndx
, CA_SUNW_MACH
, 0);
1744 if (ocapset
->oc_hw_2
.cm_val
) {
1745 ofl
->ofl_flags
|= FLG_OF_PTCAP
;
1746 CAP_UPDATE(cap
, capndx
, CA_SUNW_HW_2
, ocapset
->oc_hw_2
.cm_val
);
1748 if (ocapset
->oc_hw_1
.cm_val
) {
1749 ofl
->ofl_flags
|= FLG_OF_PTCAP
;
1750 CAP_UPDATE(cap
, capndx
, CA_SUNW_HW_1
, ocapset
->oc_hw_1
.cm_val
);
1752 if (ocapset
->oc_sf_1
.cm_val
) {
1753 ofl
->ofl_flags
|= FLG_OF_PTCAP
;
1754 CAP_UPDATE(cap
, capndx
, CA_SUNW_SF_1
, ocapset
->oc_sf_1
.cm_val
);
1756 CAP_UPDATE(cap
, capndx
, CA_SUNW_NULL
, 0);
1759 * Fill in any symbol capabilities.
1761 if (ofl
->ofl_capgroups
) {
1764 for (APLIST_TRAVERSE(ofl
->ofl_capgroups
, idx1
, cgp
)) {
1765 Objcapset
*scapset
= &cgp
->cg_set
;
1769 cgp
->cg_ndx
= capndx
;
1771 if (scapset
->oc_id
.cs_str
) {
1772 ofl
->ofl_flags
|= FLG_OF_CAPSTRS
;
1774 * Insert the identifier string in the
1775 * appropriate string table. The capability
1776 * value can't be filled in yet, as the final
1777 * offset of the string isn't known until later.
1779 if (st_insert(strtbl
,
1780 scapset
->oc_id
.cs_str
) == -1)
1782 scapset
->oc_id
.cs_ndx
= capndx
;
1783 CAP_UPDATE(cap
, capndx
, CA_SUNW_ID
, 0);
1786 if (scapset
->oc_plat
.cl_val
) {
1787 ofl
->ofl_flags
|= FLG_OF_CAPSTRS
;
1790 * Insert the platform name string in the
1791 * appropriate string table. The capability
1792 * value can't be filled in yet, as the final
1793 * offset of the string isn't known until later.
1795 for (ALIST_TRAVERSE(scapset
->oc_plat
.cl_val
,
1797 if (st_insert(strtbl
,
1798 capstr
->cs_str
) == -1)
1800 capstr
->cs_ndx
= capndx
;
1801 CAP_UPDATE(cap
, capndx
,
1805 if (scapset
->oc_mach
.cl_val
) {
1806 ofl
->ofl_flags
|= FLG_OF_CAPSTRS
;
1809 * Insert the machine name string in the
1810 * appropriate string table. The capability
1811 * value can't be filled in yet, as the final
1812 * offset of the string isn't known until later.
1814 for (ALIST_TRAVERSE(scapset
->oc_mach
.cl_val
,
1816 if (st_insert(strtbl
,
1817 capstr
->cs_str
) == -1)
1819 capstr
->cs_ndx
= capndx
;
1820 CAP_UPDATE(cap
, capndx
,
1824 if (scapset
->oc_hw_2
.cm_val
) {
1825 CAP_UPDATE(cap
, capndx
, CA_SUNW_HW_2
,
1826 scapset
->oc_hw_2
.cm_val
);
1828 if (scapset
->oc_hw_1
.cm_val
) {
1829 CAP_UPDATE(cap
, capndx
, CA_SUNW_HW_1
,
1830 scapset
->oc_hw_1
.cm_val
);
1832 if (scapset
->oc_sf_1
.cm_val
) {
1833 CAP_UPDATE(cap
, capndx
, CA_SUNW_SF_1
,
1834 scapset
->oc_sf_1
.cm_val
);
1836 CAP_UPDATE(cap
, capndx
, CA_SUNW_NULL
, 0);
1839 * If any object capabilities are available, determine
1840 * whether these symbol capabilities are less
1841 * restrictive, and hence redundant.
1843 if (((ofl
->ofl_flags
& FLG_OF_PTCAP
) == 0) ||
1844 (is_cap_redundant(ocapset
, scapset
) == 0))
1848 * Indicate any files that provide redundant symbol
1851 for (APLIST_TRAVERSE(cgp
->cg_secs
, idx2
, isp
)) {
1852 ld_eprintf(ofl
, ERR_WARNING
,
1853 MSG_INTL(MSG_CAP_REDUNDANT
),
1854 isp
->is_file
->ifl_name
,
1855 EC_WORD(isp
->is_scnndx
), isp
->is_name
);
1861 * If capabilities strings are required, the sh_info field of the
1862 * section header will be set to the associated string table.
1864 if (ofl
->ofl_flags
& FLG_OF_CAPSTRS
)
1865 shdr
->sh_flags
|= SHF_INFO_LINK
;
1868 * Place these capabilities in the output file.
1870 if ((ofl
->ofl_oscap
= ld_place_section(ofl
, isec
,
1871 NULL
, ident
, NULL
)) == (Os_desc
*)S_ERROR
)
1875 * If symbol capabilities are required, then a .SUNW_capinfo section is
1876 * also created. This table will eventually be sized to match the
1877 * associated symbol table.
1879 if (ofl
->ofl_capfamilies
) {
1880 if ((ofl
->ofl_oscapinfo
= make_sym_sec(ofl
,
1881 MSG_ORIG(MSG_SCN_SUNWCAPINFO
), SHT_SUNW_capinfo
,
1882 ld_targ
.t_id
.id_capinfo
)) == (Os_desc
*)S_ERROR
)
1886 * If we're generating a dynamic object, capabilities family
1887 * members are maintained in a .SUNW_capchain section.
1889 if (ofl
->ofl_capchaincnt
&&
1890 ((ofl
->ofl_flags
& FLG_OF_RELOBJ
) == 0)) {
1891 if (new_section(ofl
, SHT_SUNW_capchain
,
1892 MSG_ORIG(MSG_SCN_SUNWCAPCHAIN
),
1893 ofl
->ofl_capchaincnt
, &isec
, &shdr
,
1897 ofl
->ofl_oscapchain
= ld_place_section(ofl
, isec
,
1898 NULL
, ld_targ
.t_id
.id_capchain
, NULL
);
1899 if (ofl
->ofl_oscapchain
== (Os_desc
*)S_ERROR
)
1909 * Build the PLT section and its associated relocation entries.
1912 make_plt(Ofl_desc
*ofl
)
1917 size_t size
= ld_targ
.t_m
.m_plt_reservsz
+
1918 (((size_t)ofl
->ofl_pltcnt
+ (size_t)ofl
->ofl_pltpad
) *
1919 ld_targ
.t_m
.m_plt_entsize
);
1920 size_t rsize
= (size_t)ofl
->ofl_relocpltsz
;
1923 * On sparc, account for the NOP at the end of the plt.
1925 if (ld_targ
.t_m
.m_mach
== LD_TARG_BYCLASS(EM_SPARC
, EM_SPARCV9
))
1926 size
+= sizeof (Word
);
1928 if (new_section(ofl
, SHT_PROGBITS
, MSG_ORIG(MSG_SCN_PLT
), 0,
1929 &isec
, &shdr
, &data
) == S_ERROR
)
1932 data
->d_size
= size
;
1933 data
->d_align
= ld_targ
.t_m
.m_plt_align
;
1935 shdr
->sh_flags
= ld_targ
.t_m
.m_plt_shf_flags
;
1936 shdr
->sh_size
= (Xword
)size
;
1937 shdr
->sh_addralign
= ld_targ
.t_m
.m_plt_align
;
1938 shdr
->sh_entsize
= ld_targ
.t_m
.m_plt_entsize
;
1940 ofl
->ofl_osplt
= ld_place_section(ofl
, isec
, NULL
,
1941 ld_targ
.t_id
.id_plt
, NULL
);
1942 if (ofl
->ofl_osplt
== (Os_desc
*)S_ERROR
)
1945 ofl
->ofl_osplt
->os_szoutrels
= (Xword
)rsize
;
1951 * Make the hash table. Only built for dynamic executables and shared
1952 * libraries, and provides hashed lookup into the global symbol table
1953 * (.dynsym) for the run-time linker to resolve symbol lookups.
1956 make_hash(Ofl_desc
*ofl
)
1962 Word nsyms
= ofl
->ofl_globcnt
;
1966 * Allocate section header structures. We set entcnt to 0
1967 * because it's going to change after we place this section.
1969 if (new_section(ofl
, SHT_HASH
, MSG_ORIG(MSG_SCN_HASH
), 0,
1970 &isec
, &shdr
, &data
) == S_ERROR
)
1974 * Place the section first since it will affect the local symbol
1978 ld_place_section(ofl
, isec
, NULL
, ld_targ
.t_id
.id_hash
, NULL
);
1979 if (ofl
->ofl_oshash
== (Os_desc
*)S_ERROR
)
1983 * Calculate the number of output hash buckets.
1985 ofl
->ofl_hashbkts
= findprime(nsyms
);
1988 * The size of the hash table is determined by
1990 * i. the initial nbucket and nchain entries (2)
1991 * ii. the number of buckets (calculated above)
1992 * iii. the number of chains (this is based on the number of
1993 * symbols in the .dynsym array).
1995 cnt
= 2 + ofl
->ofl_hashbkts
+ DYNSYM_ALL_CNT(ofl
);
1996 size
= cnt
* shdr
->sh_entsize
;
1999 * Finalize the section header and data buffer initialization.
2001 if ((data
->d_buf
= libld_calloc(size
, 1)) == NULL
)
2003 data
->d_size
= size
;
2004 shdr
->sh_size
= (Xword
)size
;
2010 * Generate the standard symbol table. Contains all locals and globals,
2011 * and resides in a non-allocatable section (ie. it can be stripped).
2014 make_symtab(Ofl_desc
*ofl
)
2024 * Create the section headers. Note that we supply an ent_cnt
2025 * of 0. We won't know the count until the section has been placed.
2027 if (new_section(ofl
, SHT_SYMTAB
, MSG_ORIG(MSG_SCN_SYMTAB
), 0,
2028 &isec
, &shdr
, &data
) == S_ERROR
)
2032 * Place the section first since it will affect the local symbol
2035 if ((ofl
->ofl_ossymtab
= ld_place_section(ofl
, isec
, NULL
,
2036 ld_targ
.t_id
.id_symtab
, NULL
)) == (Os_desc
*)S_ERROR
)
2040 * At this point we've created all but the 'shstrtab' section.
2041 * Determine if we have to use 'Extended Sections'. If so - then
2042 * also create a SHT_SYMTAB_SHNDX section.
2044 if ((ofl
->ofl_shdrcnt
+ 1) >= SHN_LORESERVE
) {
2048 if (new_section(ofl
, SHT_SYMTAB_SHNDX
,
2049 MSG_ORIG(MSG_SCN_SYMTAB_SHNDX
), 0, &xisec
,
2050 &xshdr
, &xdata
) == S_ERROR
)
2053 if ((ofl
->ofl_ossymshndx
= ld_place_section(ofl
, xisec
, NULL
,
2054 ld_targ
.t_id
.id_symtab_ndx
, NULL
)) == (Os_desc
*)S_ERROR
)
2059 * Calculated number of symbols, which need to be augmented by
2060 * the (yet to be created) .shstrtab entry.
2062 symcnt
= (size_t)(1 + SYMTAB_ALL_CNT(ofl
));
2063 size
= symcnt
* shdr
->sh_entsize
;
2066 * Finalize the section header and data buffer initialization.
2068 data
->d_size
= size
;
2069 shdr
->sh_size
= (Xword
)size
;
2072 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
2075 size_t xsize
= symcnt
* sizeof (Word
);
2077 xisec
->is_indata
->d_size
= xsize
;
2078 xisec
->is_shdr
->sh_size
= (Xword
)xsize
;
2085 * Build a dynamic symbol table. These tables reside in the text
2086 * segment of a dynamic executable or shared library.
2088 * .SUNW_ldynsym contains local function symbols
2089 * .dynsym contains only globals symbols
2091 * The two tables are created adjacent to each other, with .SUNW_ldynsym
2095 make_dynsym(Ofl_desc
*ofl
)
2098 Elf_Data
*data
, *ldata
;
2099 Is_desc
*isec
, *lisec
;
2105 * Unless explicitly disabled, always produce a .SUNW_ldynsym section
2106 * when it is allowed by the file type, even if the resulting
2107 * table only ends up with a single STT_FILE in it. There are
2108 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB
2109 * entry in the .dynamic section, which is something we would
2110 * like to encourage, and (2) Without it, we cannot generate
2111 * the associated .SUNW_dyn[sym|tls]sort sections, which are of
2114 * In practice, it is extremely rare for an object not to have
2115 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be
2118 allow_ldynsym
= OFL_ALLOW_LDYNSYM(ofl
);
2121 * Create the section headers. Note that we supply an ent_cnt
2122 * of 0. We won't know the count until the section has been placed.
2124 if (allow_ldynsym
&& new_section(ofl
, SHT_SUNW_LDYNSYM
,
2125 MSG_ORIG(MSG_SCN_LDYNSYM
), 0, &lisec
, &lshdr
, &ldata
) == S_ERROR
)
2128 if (new_section(ofl
, SHT_DYNSYM
, MSG_ORIG(MSG_SCN_DYNSYM
), 0,
2129 &isec
, &shdr
, &data
) == S_ERROR
)
2133 * Place the section(s) first since it will affect the local symbol
2136 if (allow_ldynsym
&&
2137 ((ofl
->ofl_osldynsym
= ld_place_section(ofl
, lisec
, NULL
,
2138 ld_targ
.t_id
.id_ldynsym
, NULL
)) == (Os_desc
*)S_ERROR
))
2141 ld_place_section(ofl
, isec
, NULL
, ld_targ
.t_id
.id_dynsym
, NULL
);
2142 if (ofl
->ofl_osdynsym
== (Os_desc
*)S_ERROR
)
2145 cnt
= DYNSYM_ALL_CNT(ofl
);
2146 size
= (size_t)cnt
* shdr
->sh_entsize
;
2149 * Finalize the section header and data buffer initialization.
2151 data
->d_size
= size
;
2152 shdr
->sh_size
= (Xword
)size
;
2155 * An ldynsym contains local function symbols. It is not
2156 * used for linking, but if present, serves to allow better
2157 * stack traces to be generated in contexts where the symtab
2158 * is not available. (dladdr(), or stripped executable/library files).
2160 if (allow_ldynsym
) {
2161 cnt
= 1 + ofl
->ofl_dynlocscnt
+ ofl
->ofl_dynscopecnt
;
2162 size
= (size_t)cnt
* shdr
->sh_entsize
;
2164 ldata
->d_size
= size
;
2165 lshdr
->sh_size
= (Xword
)size
;
2172 * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are
2173 * index sections for the .SUNW_ldynsym/.dynsym pair that present data
2174 * and function symbols sorted by address.
2177 make_dynsort(Ofl_desc
*ofl
)
2183 /* Only do it if the .SUNW_ldynsym section is present */
2184 if (!OFL_ALLOW_LDYNSYM(ofl
))
2187 /* .SUNW_dynsymsort */
2188 if (ofl
->ofl_dynsymsortcnt
> 0) {
2189 if (new_section(ofl
, SHT_SUNW_symsort
,
2190 MSG_ORIG(MSG_SCN_DYNSYMSORT
), ofl
->ofl_dynsymsortcnt
,
2191 &isec
, &shdr
, &data
) == S_ERROR
)
2194 if ((ofl
->ofl_osdynsymsort
= ld_place_section(ofl
, isec
, NULL
,
2195 ld_targ
.t_id
.id_dynsort
, NULL
)) == (Os_desc
*)S_ERROR
)
2199 /* .SUNW_dyntlssort */
2200 if (ofl
->ofl_dyntlssortcnt
> 0) {
2201 if (new_section(ofl
, SHT_SUNW_tlssort
,
2202 MSG_ORIG(MSG_SCN_DYNTLSSORT
),
2203 ofl
->ofl_dyntlssortcnt
, &isec
, &shdr
, &data
) == S_ERROR
)
2206 if ((ofl
->ofl_osdyntlssort
= ld_place_section(ofl
, isec
, NULL
,
2207 ld_targ
.t_id
.id_dynsort
, NULL
)) == (Os_desc
*)S_ERROR
)
2215 * Helper routine for make_dynsym_shndx. Builds a
2216 * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing
2220 make_dyn_shndx(Ofl_desc
*ofl
, const char *shname
, Os_desc
*symtab
,
2225 Shdr
*shdr
, *dynshdr
;
2228 dynsymisp
= ld_os_first_isdesc(symtab
);
2229 dynshdr
= dynsymisp
->is_shdr
;
2231 if (new_section(ofl
, SHT_SYMTAB_SHNDX
, shname
,
2232 (dynshdr
->sh_size
/ dynshdr
->sh_entsize
),
2233 &isec
, &shdr
, &data
) == S_ERROR
)
2236 if ((*ret_os
= ld_place_section(ofl
, isec
, NULL
,
2237 ld_targ
.t_id
.id_dynsym_ndx
, NULL
)) == (Os_desc
*)S_ERROR
)
2246 * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym
2249 make_dynsym_shndx(Ofl_desc
*ofl
)
2252 * If there is a .SUNW_ldynsym, generate a section for its extended
2253 * index section as well.
2255 if (OFL_ALLOW_LDYNSYM(ofl
)) {
2256 if (make_dyn_shndx(ofl
, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX
),
2257 ofl
->ofl_osldynsym
, &ofl
->ofl_osldynshndx
) == S_ERROR
)
2261 /* The Generate a section for the dynsym */
2262 if (make_dyn_shndx(ofl
, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX
),
2263 ofl
->ofl_osdynsym
, &ofl
->ofl_osdynshndx
) == S_ERROR
)
2271 * Build a string table for the section headers.
2274 make_shstrtab(Ofl_desc
*ofl
)
2281 if (new_section(ofl
, SHT_STRTAB
, MSG_ORIG(MSG_SCN_SHSTRTAB
),
2282 0, &isec
, &shdr
, &data
) == S_ERROR
)
2286 * Place the section first, as it may effect the number of section
2287 * headers to account for.
2289 ofl
->ofl_osshstrtab
=
2290 ld_place_section(ofl
, isec
, NULL
, ld_targ
.t_id
.id_note
, NULL
);
2291 if (ofl
->ofl_osshstrtab
== (Os_desc
*)S_ERROR
)
2294 size
= st_getstrtab_sz(ofl
->ofl_shdrsttab
);
2297 data
->d_size
= size
;
2298 shdr
->sh_size
= (Xword
)size
;
2304 * Build a string section for the standard symbol table.
2307 make_strtab(Ofl_desc
*ofl
)
2315 * This string table consists of all the global and local symbols.
2316 * Account for null bytes at end of the file name and the beginning
2319 if (st_insert(ofl
->ofl_strtab
, ofl
->ofl_name
) == -1)
2322 size
= st_getstrtab_sz(ofl
->ofl_strtab
);
2325 if (new_section(ofl
, SHT_STRTAB
, MSG_ORIG(MSG_SCN_STRTAB
),
2326 0, &isec
, &shdr
, &data
) == S_ERROR
)
2329 /* Set the size of the data area */
2330 data
->d_size
= size
;
2331 shdr
->sh_size
= (Xword
)size
;
2334 ld_place_section(ofl
, isec
, NULL
, ld_targ
.t_id
.id_strtab
, NULL
);
2335 return ((uintptr_t)ofl
->ofl_osstrtab
);
2339 * Build a string table for the dynamic symbol table.
2342 make_dynstr(Ofl_desc
*ofl
)
2350 * If producing a .SUNW_ldynsym, account for the initial STT_FILE
2351 * symbol that precedes the scope reduced global symbols.
2353 if (OFL_ALLOW_LDYNSYM(ofl
)) {
2354 if (st_insert(ofl
->ofl_dynstrtab
, ofl
->ofl_name
) == -1)
2356 ofl
->ofl_dynscopecnt
++;
2360 * Account for any local, named register symbols. These locals are
2361 * required for reference from DT_REGISTER .dynamic entries.
2363 if (ofl
->ofl_regsyms
) {
2366 for (ndx
= 0; ndx
< ofl
->ofl_regsymsno
; ndx
++) {
2369 if ((sdp
= ofl
->ofl_regsyms
[ndx
]) == NULL
)
2372 if (!SYM_IS_HIDDEN(sdp
) &&
2373 (ELF_ST_BIND(sdp
->sd_sym
->st_info
) != STB_LOCAL
))
2376 if (sdp
->sd_sym
->st_name
== NULL
)
2379 if (st_insert(ofl
->ofl_dynstrtab
, sdp
->sd_name
) == -1)
2385 * Reserve entries for any per-symbol auxiliary/filter strings.
2387 if (ofl
->ofl_dtsfltrs
!= NULL
) {
2391 for (ALIST_TRAVERSE(ofl
->ofl_dtsfltrs
, idx
, dftp
))
2392 if (st_insert(ofl
->ofl_dynstrtab
, dftp
->dft_str
) == -1)
2396 size
= st_getstrtab_sz(ofl
->ofl_dynstrtab
);
2399 if (new_section(ofl
, SHT_STRTAB
, MSG_ORIG(MSG_SCN_DYNSTR
),
2400 0, &isec
, &shdr
, &data
) == S_ERROR
)
2403 /* Make it allocable if necessary */
2404 if (!(ofl
->ofl_flags
& FLG_OF_RELOBJ
))
2405 shdr
->sh_flags
|= SHF_ALLOC
;
2407 /* Set the size of the data area */
2408 data
->d_size
= size
+ DYNSTR_EXTRA_PAD
;
2410 shdr
->sh_size
= (Xword
)size
;
2413 ld_place_section(ofl
, isec
, NULL
, ld_targ
.t_id
.id_dynstr
, NULL
);
2414 return ((uintptr_t)ofl
->ofl_osdynstr
);
2418 * Generate an output relocation section which will contain the relocation
2419 * information to be applied to the `osp' section.
2421 * If (osp == NULL) then we are creating the coalesced relocation section
2422 * for an executable and/or a shared object.
2425 make_reloc(Ofl_desc
*ofl
, Os_desc
*osp
)
2435 const char *rel_prefix
;
2438 if (ld_targ
.t_m
.m_rel_sht_type
== SHT_REL
) {
2440 relsize
= sizeof (Rel
);
2441 rel_prefix
= MSG_ORIG(MSG_SCN_REL
);
2444 relsize
= sizeof (Rela
);
2445 rel_prefix
= MSG_ORIG(MSG_SCN_RELA
);
2449 size
= osp
->os_szoutrels
;
2450 sh_flags
= osp
->os_shdr
->sh_flags
;
2451 if ((sectname
= libld_malloc(strlen(rel_prefix
) +
2452 strlen(osp
->os_name
) + 1)) == 0)
2454 (void) strcpy(sectname
, rel_prefix
);
2455 (void) strcat(sectname
, osp
->os_name
);
2456 } else if (ofl
->ofl_flags
& FLG_OF_COMREL
) {
2457 size
= (ofl
->ofl_reloccnt
- ofl
->ofl_reloccntsub
) * relsize
;
2458 sh_flags
= SHF_ALLOC
;
2459 sectname
= (char *)MSG_ORIG(MSG_SCN_SUNWRELOC
);
2461 size
= ofl
->ofl_relocrelsz
;
2462 sh_flags
= SHF_ALLOC
;
2463 sectname
= (char *)rel_prefix
;
2467 * Keep track of total size of 'output relocations' (to be stored
2471 ofl
->ofl_relocsz
+= (Xword
)size
;
2473 if (new_section(ofl
, ld_targ
.t_m
.m_rel_sht_type
, sectname
, 0, &isec
,
2474 &shdr
, &data
) == S_ERROR
)
2477 data
->d_size
= size
;
2479 shdr
->sh_size
= (Xword
)size
;
2480 if (OFL_ALLOW_DYNSYM(ofl
) && (sh_flags
& SHF_ALLOC
))
2481 shdr
->sh_flags
= SHF_ALLOC
;
2485 * The sh_info field of the SHT_REL* sections points to the
2486 * section the relocations are to be applied to.
2488 shdr
->sh_flags
|= SHF_INFO_LINK
;
2491 rosp
= ld_place_section(ofl
, isec
, NULL
, ld_targ
.t_id
.id_rel
, NULL
);
2492 if (rosp
== (Os_desc
*)S_ERROR
)
2496 * Associate this relocation section to the section its going to
2504 * This is used primarily so that we can update
2505 * SHT_GROUP[sect_no] entries to point to the
2506 * created output relocation sections.
2508 for (APLIST_TRAVERSE(osp
->os_relisdescs
, idx
, risp
)) {
2509 risp
->is_osdesc
= rosp
;
2512 * If the input relocation section had the SHF_GROUP
2513 * flag set - propagate it to the output relocation
2516 if (risp
->is_shdr
->sh_flags
& SHF_GROUP
) {
2517 rosp
->os_shdr
->sh_flags
|= SHF_GROUP
;
2521 osp
->os_relosdesc
= rosp
;
2523 ofl
->ofl_osrel
= rosp
;
2526 * If this is the first relocation section we've encountered save it
2527 * so that the .dynamic entry can be initialized accordingly.
2529 if (ofl
->ofl_osrelhead
== (Os_desc
*)0)
2530 ofl
->ofl_osrelhead
= rosp
;
2536 * Generate version needed section.
2539 make_verneed(Ofl_desc
*ofl
)
2546 * verneed sections do not have a constant element size, so the
2547 * value of ent_cnt specified here (0) is meaningless.
2549 if (new_section(ofl
, SHT_SUNW_verneed
, MSG_ORIG(MSG_SCN_SUNWVERSION
),
2550 0, &isec
, &shdr
, &data
) == S_ERROR
)
2553 /* During version processing we calculated the total size. */
2554 data
->d_size
= ofl
->ofl_verneedsz
;
2555 shdr
->sh_size
= (Xword
)ofl
->ofl_verneedsz
;
2557 ofl
->ofl_osverneed
=
2558 ld_place_section(ofl
, isec
, NULL
, ld_targ
.t_id
.id_version
, NULL
);
2559 return ((uintptr_t)ofl
->ofl_osverneed
);
2563 * Generate a version definition section.
2565 * o the SHT_SUNW_verdef section defines the versions that exist within this
2569 make_verdef(Ofl_desc
*ofl
)
2578 * Reserve a string table entry for the base version dependency (other
2579 * dependencies have symbol representations, which will already be
2580 * accounted for during symbol processing).
2582 vdp
= (Ver_desc
*)ofl
->ofl_verdesc
->apl_data
[0];
2584 if (OFL_IS_STATIC_OBJ(ofl
))
2585 strtab
= ofl
->ofl_strtab
;
2587 strtab
= ofl
->ofl_dynstrtab
;
2589 if (st_insert(strtab
, vdp
->vd_name
) == -1)
2593 * verdef sections do not have a constant element size, so the
2594 * value of ent_cnt specified here (0) is meaningless.
2596 if (new_section(ofl
, SHT_SUNW_verdef
, MSG_ORIG(MSG_SCN_SUNWVERSION
),
2597 0, &isec
, &shdr
, &data
) == S_ERROR
)
2600 /* During version processing we calculated the total size. */
2601 data
->d_size
= ofl
->ofl_verdefsz
;
2602 shdr
->sh_size
= (Xword
)ofl
->ofl_verdefsz
;
2605 ld_place_section(ofl
, isec
, NULL
, ld_targ
.t_id
.id_version
, NULL
);
2606 return ((uintptr_t)ofl
->ofl_osverdef
);
2610 * This routine is called when -z nopartial is in effect.
2613 ld_make_parexpn_data(Ofl_desc
*ofl
, size_t size
, Xword align
)
2620 if (new_section(ofl
, SHT_PROGBITS
, MSG_ORIG(MSG_SCN_DATA
), 0,
2621 &isec
, &shdr
, &data
) == S_ERROR
)
2624 shdr
->sh_flags
|= SHF_WRITE
;
2625 data
->d_size
= size
;
2626 shdr
->sh_size
= (Xword
)size
;
2628 data
->d_align
= align
;
2629 shdr
->sh_addralign
= align
;
2632 if ((data
->d_buf
= libld_calloc(size
, 1)) == NULL
)
2636 * Retain handle to this .data input section. Variables using move
2637 * sections (partial initialization) will be redirected here when
2638 * such global references are added and '-z nopartial' is in effect.
2640 ofl
->ofl_isparexpn
= isec
;
2641 osp
= ld_place_section(ofl
, isec
, NULL
, ld_targ
.t_id
.id_data
, NULL
);
2642 if (osp
== (Os_desc
*)S_ERROR
)
2645 if (!(osp
->os_flags
& FLG_OS_OUTREL
)) {
2646 ofl
->ofl_dynshdrcnt
++;
2647 osp
->os_flags
|= FLG_OS_OUTREL
;
2653 * Make .sunwmove section
2656 ld_make_sunwmove(Ofl_desc
*ofl
, int mv_nums
)
2666 if (new_section(ofl
, SHT_SUNW_move
, MSG_ORIG(MSG_SCN_SUNWMOVE
),
2667 mv_nums
, &isec
, &shdr
, &data
) == S_ERROR
)
2670 if ((data
->d_buf
= libld_calloc(data
->d_size
, 1)) == NULL
)
2676 for (APLIST_TRAVERSE(ofl
->ofl_parsyms
, idx
, sdp
)) {
2680 if (sdp
->sd_flags
& FLG_SY_PAREXPN
)
2683 for (ALIST_TRAVERSE(sdp
->sd_move
, idx2
, mdp
))
2684 mdp
->md_oidx
= cnt
++;
2687 if ((ofl
->ofl_osmove
= ld_place_section(ofl
, isec
, NULL
, 0, NULL
)) ==
2695 * Given a relocation descriptor that references a string table
2696 * input section, locate the string referenced and return a pointer
2700 strmerge_get_reloc_str(Ofl_desc
*ofl
, Rel_desc
*rsp
)
2702 Sym_desc
*sdp
= rsp
->rel_sym
;
2706 * In the case of an STT_SECTION symbol, the addend of the
2707 * relocation gives the offset into the string section. For
2708 * other symbol types, the symbol value is the offset.
2711 if (ELF_ST_TYPE(sdp
->sd_sym
->st_info
) != STT_SECTION
) {
2712 str_off
= sdp
->sd_sym
->st_value
;
2713 } else if ((rsp
->rel_flags
& FLG_REL_RELA
) == FLG_REL_RELA
) {
2715 * For SHT_RELA, the addend value is found in the
2716 * rel_raddend field of the relocation.
2718 str_off
= rsp
->rel_raddend
;
2719 } else { /* REL and STT_SECTION */
2721 * For SHT_REL, the "addend" is not part of the relocation
2722 * record. Instead, it is found at the relocation target
2725 uchar_t
*addr
= (uchar_t
*)((uintptr_t)rsp
->rel_roffset
+
2726 (uintptr_t)rsp
->rel_isdesc
->is_indata
->d_buf
);
2728 if (ld_reloc_targval_get(ofl
, rsp
, addr
, &str_off
) == 0)
2732 return (str_off
+ (char *)sdp
->sd_isc
->is_indata
->d_buf
);
2736 * First pass over the relocation records for string table merging.
2737 * Build lists of relocations and symbols that will need modification,
2738 * and insert the strings they reference into the mstrtab string table.
2741 * ofl, osp - As passed to ld_make_strmerge().
2742 * mstrtab - String table to receive input strings. This table
2743 * must be in its first (initialization) pass and not
2744 * yet cooked (st_getstrtab_sz() not yet called).
2745 * rel_alpp - APlist to receive pointer to any relocation
2746 * descriptors with STT_SECTION symbols that reference
2747 * one of the input sections being merged.
2748 * sym_alpp - APlist to receive pointer to any symbols that reference
2749 * one of the input sections being merged.
2750 * rcp - Pointer to cache of relocation descriptors to examine.
2751 * Either &ofl->ofl_actrels (active relocations)
2752 * or &ofl->ofl_outrels (output relocations).
2755 * On success, rel_alpp and sym_alpp are updated, and
2756 * any strings in the mergeable input sections referenced by
2757 * a relocation has been entered into mstrtab. True (1) is returned.
2759 * On failure, False (0) is returned.
2762 strmerge_pass1(Ofl_desc
*ofl
, Os_desc
*osp
, Str_tbl
*mstrtab
,
2763 APlist
**rel_alpp
, APlist
**sym_alpp
, Rel_cache
*rcp
)
2768 Sym_desc
*last_sdp
= NULL
;
2772 REL_CACHE_TRAVERSE(rcp
, idx
, rcbp
, rsp
) {
2774 if ((sdp
->sd_isc
== NULL
) || ((sdp
->sd_isc
->is_flags
&
2775 (FLG_IS_DISCARD
| FLG_IS_INSTRMRG
)) != FLG_IS_INSTRMRG
) ||
2776 (sdp
->sd_isc
->is_osdesc
!= osp
))
2780 * Remember symbol for use in the third pass. There is no
2781 * reason to save a given symbol more than once, so we take
2782 * advantage of the fact that relocations to a given symbol
2783 * tend to cluster in the list. If this is the same symbol
2784 * we saved last time, don't bother.
2786 if (last_sdp
!= sdp
) {
2787 if (aplist_append(sym_alpp
, sdp
, AL_CNT_STRMRGSYM
) ==
2793 /* Enter the string into our new string table */
2794 name
= strmerge_get_reloc_str(ofl
, rsp
);
2795 if (st_insert(mstrtab
, name
) == -1)
2799 * If this is an STT_SECTION symbol, then the second pass
2800 * will need to modify this relocation, so hang on to it.
2802 if ((ELF_ST_TYPE(sdp
->sd_sym
->st_info
) == STT_SECTION
) &&
2803 (aplist_append(rel_alpp
, rsp
, AL_CNT_STRMRGREL
) == NULL
))
2811 * If the output section has any SHF_MERGE|SHF_STRINGS input sections,
2812 * replace them with a single merged/compressed input section.
2815 * ofl - Output file descriptor
2816 * osp - Output section descriptor
2817 * rel_alpp, sym_alpp, - Address of 2 APlists, to be used
2818 * for internal processing. On the initial call to
2819 * ld_make_strmerge, these list pointers must be NULL.
2820 * The caller is encouraged to pass the same lists back for
2821 * successive calls to this function without freeing
2822 * them in between calls. This causes a single pair of
2823 * memory allocations to be reused multiple times.
2826 * If section merging is possible, it is done. If no errors are
2827 * encountered, True (1) is returned. On error, S_ERROR.
2829 * The contents of rel_alpp and sym_alpp on exit are
2830 * undefined. The caller can free them, or pass them back to a subsequent
2831 * call to this routine, but should not examine their contents.
2834 ld_make_strmerge(Ofl_desc
*ofl
, Os_desc
*osp
, APlist
**rel_alpp
,
2837 Str_tbl
*mstrtab
; /* string table for string merge secs */
2838 Is_desc
*mstrsec
; /* Generated string merge section */
2841 Elf_Data
*mstr_data
;
2846 int st_setstring_status
;
2849 /* If string table compression is disabled, there's nothing to do */
2850 if ((ofl
->ofl_flags1
& FLG_OF1_NCSTTAB
) != 0)
2854 * Pass over the mergeable input sections, and if they haven't
2855 * all been discarded, create a string table.
2858 for (APLIST_TRAVERSE(osp
->os_mstrisdescs
, idx
, isp
)) {
2859 if (isdesc_discarded(isp
))
2863 * Input sections of 0 size are dubiously valid since they do
2864 * not even contain the NUL string. Ignore them.
2866 if (isp
->is_shdr
->sh_size
== 0)
2870 * We have at least one non-discarded section.
2871 * Create a string table descriptor.
2873 if ((mstrtab
= st_new(FLG_STNEW_COMPRESS
)) == NULL
)
2878 /* If no string table was created, we have no mergeable sections */
2879 if (mstrtab
== NULL
)
2883 * This routine has to make 3 passes:
2885 * 1) Examine all relocations, insert strings from relocations
2886 * to the mergeable input sections into the string table.
2887 * 2) Modify the relocation values to be correct for the
2888 * new merged section.
2889 * 3) Modify the symbols used by the relocations to reference
2892 * These passes cannot be combined:
2893 * - The string table code works in two passes, and all
2894 * strings have to be loaded in pass one before the
2895 * offset of any strings can be determined.
2896 * - Multiple relocations reference a single symbol, so the
2897 * symbol cannot be modified until all relocations are
2900 * The number of relocations related to section merging is usually
2901 * a mere fraction of the overall active and output relocation lists,
2902 * and the number of symbols is usually a fraction of the number
2903 * of related relocations. We therefore build APlists for the
2904 * relocations and symbols in the first pass, and then use those
2905 * lists to accelerate the operation of pass 2 and 3.
2907 * Reinitialize the lists to a completely empty state.
2909 aplist_reset(*rel_alpp
);
2910 aplist_reset(*sym_alpp
);
2915 * Every relocation related to this output section (and the input
2916 * sections that make it up) is found in either the active, or the
2917 * output relocation list, depending on whether the relocation is to
2918 * be processed by this invocation of the linker, or inserted into the
2921 * Build lists of relocations and symbols that will need modification,
2922 * and insert the strings they reference into the mstrtab string table.
2924 if (strmerge_pass1(ofl
, osp
, mstrtab
, rel_alpp
, sym_alpp
,
2925 &ofl
->ofl_actrels
) == 0)
2926 goto return_s_error
;
2927 if (strmerge_pass1(ofl
, osp
, mstrtab
, rel_alpp
, sym_alpp
,
2928 &ofl
->ofl_outrels
) == 0)
2929 goto return_s_error
;
2932 * Get the size of the new input section. Requesting the
2933 * string table size "cooks" the table, and finalizes its contents.
2935 data_size
= st_getstrtab_sz(mstrtab
);
2937 /* Create a new input section to hold the merged strings */
2938 if (new_section_from_template(ofl
, isp
, data_size
,
2939 &mstrsec
, &mstr_shdr
, &mstr_data
) == S_ERROR
)
2940 goto return_s_error
;
2941 mstrsec
->is_flags
|= FLG_IS_GNSTRMRG
;
2944 * Allocate a data buffer for the new input section.
2945 * Then, associate the buffer with the string table descriptor.
2947 if ((mstr_data
->d_buf
= libld_malloc(data_size
)) == NULL
)
2948 goto return_s_error
;
2949 if (st_setstrbuf(mstrtab
, mstr_data
->d_buf
, data_size
) == -1)
2950 goto return_s_error
;
2952 /* Add the new section to the output image */
2953 if (ld_place_section(ofl
, mstrsec
, NULL
, osp
->os_identndx
, NULL
) ==
2955 goto return_s_error
;
2960 * Revisit the relocation descriptors with STT_SECTION symbols
2961 * that were saved by the first pass. Update each relocation
2962 * record so that the offset it contains is for the new section
2963 * instead of the original.
2965 for (APLIST_TRAVERSE(*rel_alpp
, idx
, rsp
)) {
2968 /* Put the string into the merged string table */
2969 name
= strmerge_get_reloc_str(ofl
, rsp
);
2970 st_setstring_status
= st_setstring(mstrtab
, name
, &stoff
);
2971 if (st_setstring_status
== -1) {
2973 * A failure to insert at this point means that
2974 * something is corrupt. This isn't a resource issue.
2976 assert(st_setstring_status
!= -1);
2977 goto return_s_error
;
2981 * Alter the relocation to access the string at the
2982 * new offset in our new string table.
2984 * For SHT_RELA platforms, it suffices to simply
2985 * update the rel_raddend field of the relocation.
2987 * For SHT_REL platforms, the new "addend" value
2988 * needs to be written at the address being relocated.
2989 * However, we can't alter the input sections which
2990 * are mapped readonly, and the output image has not
2991 * been created yet. So, we defer this operation,
2992 * using the rel_raddend field of the relocation
2993 * which is normally 0 on a REL platform, to pass the
2994 * new "addend" value to ld_perform_outreloc() or
2995 * ld_do_activerelocs(). The FLG_REL_NADDEND flag
2996 * tells them that this is the case.
2998 if ((rsp
->rel_flags
& FLG_REL_RELA
) == 0) /* REL */
2999 rsp
->rel_flags
|= FLG_REL_NADDEND
;
3000 rsp
->rel_raddend
= (Sxword
)stoff
;
3003 * Generate a symbol name string for STT_SECTION symbols
3004 * that might reference our merged section. This shows up
3005 * in debug output and helps show how the relocation has
3006 * changed from its original input section to our merged one.
3008 if (ld_stt_section_sym_name(mstrsec
) == NULL
)
3009 goto return_s_error
;
3015 * Modify the symbols referenced by the relocation descriptors
3016 * so that they reference the new input section containing the
3017 * merged strings instead of the original input sections.
3019 for (APLIST_TRAVERSE(*sym_alpp
, idx
, sdp
)) {
3021 * If we've already processed this symbol, don't do it
3022 * twice. strmerge_pass1() uses a heuristic (relocations to
3023 * the same symbol clump together) to avoid inserting a
3024 * given symbol more than once, but repeat symbols in
3025 * the list can occur.
3027 if ((sdp
->sd_isc
->is_flags
& FLG_IS_INSTRMRG
) == 0)
3030 if (ELF_ST_TYPE(sdp
->sd_sym
->st_info
) != STT_SECTION
) {
3032 * This is not an STT_SECTION symbol, so its
3033 * value is the offset of the string within the
3034 * input section. Update the address to reflect
3035 * the address in our new merged section.
3037 const char *name
= sdp
->sd_sym
->st_value
+
3038 (char *)sdp
->sd_isc
->is_indata
->d_buf
;
3040 st_setstring_status
=
3041 st_setstring(mstrtab
, name
, &stoff
);
3042 if (st_setstring_status
== -1) {
3044 * A failure to insert at this point means
3045 * something is corrupt. This isn't a
3048 assert(st_setstring_status
!= -1);
3049 goto return_s_error
;
3052 if (ld_sym_copy(sdp
) == S_ERROR
)
3053 goto return_s_error
;
3054 sdp
->sd_sym
->st_value
= (Word
)stoff
;
3057 /* Redirect the symbol to our new merged section */
3058 sdp
->sd_isc
= mstrsec
;
3062 * There are no references left to the original input string sections.
3063 * Mark them as discarded so they don't go into the output image.
3064 * At the same time, add up the sizes of the replaced sections.
3067 for (APLIST_TRAVERSE(osp
->os_mstrisdescs
, idx
, isp
)) {
3068 if (isp
->is_flags
& (FLG_IS_DISCARD
| FLG_IS_GNSTRMRG
))
3071 data_size
+= isp
->is_indata
->d_size
;
3073 isp
->is_flags
|= FLG_IS_DISCARD
;
3074 DBG_CALL(Dbg_sec_discarded(ofl
->ofl_lml
, isp
, mstrsec
));
3077 /* Report how much space we saved in the output section */
3078 DBG_CALL(Dbg_sec_genstr_compress(ofl
->ofl_lml
, osp
->os_name
, data_size
,
3079 mstr_data
->d_size
));
3081 st_destroy(mstrtab
);
3085 st_destroy(mstrtab
);
3090 * Update a data buffers size. A number of sections have to be created, and
3091 * the sections header contributes to the size of the eventual section. Thus,
3092 * a section may be created, and once all associated sections have been created,
3093 * we return to establish the required section size.
3096 update_data_size(Os_desc
*osp
, ulong_t cnt
)
3098 Is_desc
*isec
= ld_os_first_isdesc(osp
);
3099 Elf_Data
*data
= isec
->is_indata
;
3100 Shdr
*shdr
= osp
->os_shdr
;
3101 size_t size
= cnt
* shdr
->sh_entsize
;
3103 shdr
->sh_size
= (Xword
)size
;
3104 data
->d_size
= size
;
3108 * The following sections are built after all input file processing and symbol
3109 * validation has been carried out. The order is important (because the
3110 * addition of a section adds a new symbol there is a chicken and egg problem
3111 * of maintaining the appropriate counts). By maintaining a known order the
3112 * individual routines can compensate for later, known, additions.
3115 ld_make_sections(Ofl_desc
*ofl
)
3117 ofl_flag_t flags
= ofl
->ofl_flags
;
3121 * Generate any special sections.
3123 if (flags
& FLG_OF_ADDVERS
)
3124 if (make_comment(ofl
) == S_ERROR
)
3127 if (make_interp(ofl
) == S_ERROR
)
3131 * Create a capabilities section if required.
3133 if (make_cap(ofl
, SHT_SUNW_cap
, MSG_ORIG(MSG_SCN_SUNWCAP
),
3134 ld_targ
.t_id
.id_cap
) == S_ERROR
)
3138 * Create any init/fini array sections.
3140 if (make_array(ofl
, SHT_INIT_ARRAY
, MSG_ORIG(MSG_SCN_INITARRAY
),
3141 ofl
->ofl_initarray
) == S_ERROR
)
3144 if (make_array(ofl
, SHT_FINI_ARRAY
, MSG_ORIG(MSG_SCN_FINIARRAY
),
3145 ofl
->ofl_finiarray
) == S_ERROR
)
3148 if (make_array(ofl
, SHT_PREINIT_ARRAY
, MSG_ORIG(MSG_SCN_PREINITARRAY
),
3149 ofl
->ofl_preiarray
) == S_ERROR
)
3153 * Make the .plt section. This occurs after any other relocation
3154 * sections are generated (see reloc_init()) to ensure that the
3155 * associated relocation section is after all the other relocation
3158 if ((ofl
->ofl_pltcnt
) || (ofl
->ofl_pltpad
))
3159 if (make_plt(ofl
) == S_ERROR
)
3163 * Determine whether any sections or files are not referenced. Under
3164 * -Dunused a diagnostic for any unused components is generated, under
3165 * -zignore the component is removed from the final output.
3167 if (DBG_ENABLED
|| (ofl
->ofl_flags1
& FLG_OF1_IGNPRC
)) {
3168 if (ignore_section_processing(ofl
) == S_ERROR
)
3173 * If we have detected a situation in which previously placed
3174 * output sections may have been discarded, perform the necessary
3177 if (ofl
->ofl_flags
& FLG_OF_ADJOSCNT
)
3178 adjust_os_count(ofl
);
3181 * Do any of the output sections contain input sections that
3182 * are candidates for string table merging? For each such case,
3183 * we create a replacement section, insert it, and discard the
3186 * rel_alpp and sym_alpp are used by ld_make_strmerge()
3187 * for its internal processing. We are responsible for the
3188 * initialization and cleanup, and ld_make_strmerge() handles the rest.
3189 * This allows us to reuse a single pair of memory buffers, allocated
3190 * for this processing, for all the output sections.
3192 if ((ofl
->ofl_flags1
& FLG_OF1_NCSTTAB
) == 0) {
3194 APlist
*rel_alpp
= NULL
;
3195 APlist
*sym_alpp
= NULL
;
3198 for (APLIST_TRAVERSE(ofl
->ofl_segs
, idx1
, sgp
)) {
3202 for (APLIST_TRAVERSE(sgp
->sg_osdescs
, idx2
, osp
))
3203 if ((osp
->os_mstrisdescs
!= NULL
) &&
3204 (ld_make_strmerge(ofl
, osp
,
3205 &rel_alpp
, &sym_alpp
) ==
3211 if (rel_alpp
!= NULL
)
3212 libld_free(rel_alpp
);
3213 if (sym_alpp
!= NULL
)
3214 libld_free(sym_alpp
);
3215 if (error_seen
!= 0)
3220 * Add any necessary versioning information.
3222 if (!(flags
& FLG_OF_NOVERSEC
)) {
3223 if ((flags
& FLG_OF_VERNEED
) &&
3224 (make_verneed(ofl
) == S_ERROR
))
3226 if ((flags
& FLG_OF_VERDEF
) &&
3227 (make_verdef(ofl
) == S_ERROR
))
3229 if ((flags
& (FLG_OF_VERNEED
| FLG_OF_VERDEF
)) &&
3230 ((ofl
->ofl_osversym
= make_sym_sec(ofl
,
3231 MSG_ORIG(MSG_SCN_SUNWVERSYM
), SHT_SUNW_versym
,
3232 ld_targ
.t_id
.id_version
)) == (Os_desc
*)S_ERROR
))
3237 * Create a syminfo section if necessary.
3239 if (flags
& FLG_OF_SYMINFO
) {
3240 if ((ofl
->ofl_ossyminfo
= make_sym_sec(ofl
,
3241 MSG_ORIG(MSG_SCN_SUNWSYMINFO
), SHT_SUNW_syminfo
,
3242 ld_targ
.t_id
.id_syminfo
)) == (Os_desc
*)S_ERROR
)
3246 if (flags
& FLG_OF_COMREL
) {
3248 * If -zcombreloc is enabled then all relocations (except for
3249 * the PLT's) are coalesced into a single relocation section.
3251 if (ofl
->ofl_reloccnt
) {
3252 if (make_reloc(ofl
, NULL
) == S_ERROR
)
3259 * Create the required output relocation sections. Note, new
3260 * sections may be added to the section list that is being
3261 * traversed. These insertions can move the elements of the
3262 * Alist such that a section descriptor is re-read. Recursion
3263 * is prevented by maintaining a previous section pointer and
3264 * insuring that this pointer isn't re-examined.
3266 for (APLIST_TRAVERSE(ofl
->ofl_segs
, idx1
, sgp
)) {
3267 Os_desc
*osp
, *posp
= 0;
3270 for (APLIST_TRAVERSE(sgp
->sg_osdescs
, idx2
, osp
)) {
3271 if ((osp
!= posp
) && osp
->os_szoutrels
&&
3272 (osp
!= ofl
->ofl_osplt
)) {
3273 if (make_reloc(ofl
, osp
) == S_ERROR
)
3281 * If we're not building a combined relocation section, then
3282 * build a .rel[a] section as required.
3284 if (ofl
->ofl_relocrelsz
) {
3285 if (make_reloc(ofl
, NULL
) == S_ERROR
)
3291 * The PLT relocations are always in their own section, and we try to
3292 * keep them at the end of the PLT table. We do this to keep the hot
3293 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
3295 if (ofl
->ofl_osplt
&& ofl
->ofl_relocpltsz
) {
3296 if (make_reloc(ofl
, ofl
->ofl_osplt
) == S_ERROR
)
3301 * Finally build the symbol and section header sections.
3303 if (flags
& FLG_OF_DYNAMIC
) {
3304 if (make_dynamic(ofl
) == S_ERROR
)
3308 * A number of sections aren't necessary within a relocatable
3309 * object, even if -dy has been used.
3311 if (!(flags
& FLG_OF_RELOBJ
)) {
3312 if (make_hash(ofl
) == S_ERROR
)
3314 if (make_dynstr(ofl
) == S_ERROR
)
3316 if (make_dynsym(ofl
) == S_ERROR
)
3318 if (ld_unwind_make_hdr(ofl
) == S_ERROR
)
3320 if (make_dynsort(ofl
) == S_ERROR
)
3325 if (!(flags
& FLG_OF_STRIP
) || (flags
& FLG_OF_RELOBJ
) ||
3326 ((flags
& FLG_OF_STATIC
) && ofl
->ofl_osversym
)) {
3328 * Do we need to make a SHT_SYMTAB_SHNDX section
3329 * for the dynsym. If so - do it now.
3331 if (ofl
->ofl_osdynsym
&&
3332 ((ofl
->ofl_shdrcnt
+ 3) >= SHN_LORESERVE
)) {
3333 if (make_dynsym_shndx(ofl
) == S_ERROR
)
3337 if (make_strtab(ofl
) == S_ERROR
)
3339 if (make_symtab(ofl
) == S_ERROR
)
3343 * Do we need to make a SHT_SYMTAB_SHNDX section
3344 * for the dynsym. If so - do it now.
3346 if (ofl
->ofl_osdynsym
&&
3347 ((ofl
->ofl_shdrcnt
+ 1) >= SHN_LORESERVE
)) {
3348 if (make_dynsym_shndx(ofl
) == S_ERROR
)
3353 if (make_shstrtab(ofl
) == S_ERROR
)
3357 * Now that we've created all output sections, adjust the size of the
3358 * SHT_SUNW_versym and SHT_SUNW_syminfo section, which are dependent on
3359 * the associated symbol table sizes.
3361 if (ofl
->ofl_osversym
|| ofl
->ofl_ossyminfo
) {
3366 if (OFL_IS_STATIC_OBJ(ofl
))
3367 osp
= ofl
->ofl_ossymtab
;
3369 osp
= ofl
->ofl_osdynsym
;
3371 isp
= ld_os_first_isdesc(osp
);
3372 cnt
= (isp
->is_shdr
->sh_size
/ isp
->is_shdr
->sh_entsize
);
3374 if (ofl
->ofl_osversym
)
3375 update_data_size(ofl
->ofl_osversym
, cnt
);
3377 if (ofl
->ofl_ossyminfo
)
3378 update_data_size(ofl
->ofl_ossyminfo
, cnt
);
3382 * Now that we've created all output sections, adjust the size of the
3383 * SHT_SUNW_capinfo, which is dependent on the associated symbol table
3386 if (ofl
->ofl_oscapinfo
) {
3390 * Symbol capabilities symbols are placed directly after the
3391 * STT_FILE symbol, section symbols, and any register symbols.
3392 * Effectively these are the first of any series of demoted
3395 if (OFL_IS_STATIC_OBJ(ofl
))
3396 cnt
= SYMTAB_ALL_CNT(ofl
);
3398 cnt
= DYNSYM_ALL_CNT(ofl
);
3400 update_data_size(ofl
->ofl_oscapinfo
, cnt
);
3406 * Build an additional data section - used to back OBJT symbol definitions
3407 * added with a mapfile.
3410 ld_make_data(Ofl_desc
*ofl
, size_t size
)
3416 if (new_section(ofl
, SHT_PROGBITS
, MSG_ORIG(MSG_SCN_DATA
), 0,
3417 &isec
, &shdr
, &data
) == S_ERROR
)
3418 return ((Is_desc
*)S_ERROR
);
3420 data
->d_size
= size
;
3421 shdr
->sh_size
= (Xword
)size
;
3422 shdr
->sh_flags
|= SHF_WRITE
;
3424 if (aplist_append(&ofl
->ofl_mapdata
, isec
, AL_CNT_OFL_MAPSECS
) == NULL
)
3425 return ((Is_desc
*)S_ERROR
);
3431 * Build an additional text section - used to back FUNC symbol definitions
3432 * added with a mapfile.
3435 ld_make_text(Ofl_desc
*ofl
, size_t size
)
3442 * Insure the size is sufficient to contain the minimum return
3445 if (size
< ld_targ
.t_nf
.nf_size
)
3446 size
= ld_targ
.t_nf
.nf_size
;
3448 if (new_section(ofl
, SHT_PROGBITS
, MSG_ORIG(MSG_SCN_TEXT
), 0,
3449 &isec
, &shdr
, &data
) == S_ERROR
)
3450 return ((Is_desc
*)S_ERROR
);
3452 data
->d_size
= size
;
3453 shdr
->sh_size
= (Xword
)size
;
3454 shdr
->sh_flags
|= SHF_EXECINSTR
;
3457 * Fill the buffer with the appropriate return instruction.
3458 * Note that there is no need to swap bytes on a non-native,
3459 * link, as the data being copied is given in bytes.
3461 if ((data
->d_buf
= libld_calloc(size
, 1)) == NULL
)
3462 return ((Is_desc
*)S_ERROR
);
3463 (void) memcpy(data
->d_buf
, ld_targ
.t_nf
.nf_template
,
3464 ld_targ
.t_nf
.nf_size
);
3467 * If size was larger than required, and the target supplies
3468 * a fill function, use it to fill the balance. If there is no
3469 * fill function, we accept the 0-fill supplied by libld_calloc().
3471 if ((ld_targ
.t_ff
.ff_execfill
!= NULL
) && (size
> ld_targ
.t_nf
.nf_size
))
3472 ld_targ
.t_ff
.ff_execfill(data
->d_buf
, ld_targ
.t_nf
.nf_size
,
3473 size
- ld_targ
.t_nf
.nf_size
);
3475 if (aplist_append(&ofl
->ofl_maptext
, isec
, AL_CNT_OFL_MAPSECS
) == NULL
)
3476 return ((Is_desc
*)S_ERROR
);
3482 ld_comdat_validate(Ofl_desc
*ofl
, Ifl_desc
*ifl
)
3486 for (i
= 0; i
< ifl
->ifl_shnum
; i
++) {
3487 Is_desc
*isp
= ifl
->ifl_isdesc
[i
];
3489 char buf
[1024] = "";
3490 Group_desc
*gr
= NULL
;
3492 if ((isp
== NULL
) || (isp
->is_flags
& FLG_IS_COMDAT
) == 0)
3495 if (isp
->is_shdr
->sh_type
== SHT_SUNW_COMDAT
) {
3497 (void) strlcpy(buf
, MSG_ORIG(MSG_STR_SUNW_COMDAT
),
3501 if (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE
), isp
->is_name
,
3502 MSG_SCN_GNU_LINKONCE_SIZE
) == 0) {
3505 (void) strlcat(buf
, ", ", sizeof (buf
));
3506 (void) strlcat(buf
, MSG_ORIG(MSG_SCN_GNU_LINKONCE
),
3510 if ((isp
->is_shdr
->sh_flags
& SHF_GROUP
) &&
3511 ((gr
= ld_get_group(ofl
, isp
)) != NULL
) &&
3512 (gr
->gd_data
[0] & GRP_COMDAT
)) {
3515 (void) strlcat(buf
, ", ", sizeof (buf
));
3516 (void) strlcat(buf
, MSG_ORIG(MSG_STR_GROUP
),
3521 ld_eprintf(ofl
, ERR_FATAL
,
3522 MSG_INTL(MSG_SCN_MULTICOMDAT
), ifl
->ifl_name
,
3523 EC_WORD(isp
->is_scnndx
), isp
->is_name
, buf
);