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) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
31 * Map file parsing and input section to output segment mapping.
40 * Each time a section is placed, the function set_addralign()
41 * is called. This function performs:
43 * - if the section is from an external file, check if this is empty or not.
44 * If not, we know the segment this section will belong needs a program
45 * header. (Of course, the program is needed only if this section falls
46 * into a loadable segment.)
47 * - compute the Least Common Multiplier for setting the segment alignment.
50 set_addralign(Ofl_desc
*ofl
, Os_desc
*osp
, Is_desc
*isp
)
52 Shdr
*shdr
= isp
->is_shdr
;
54 /* A discarded section has no influence on the output */
55 if (isp
->is_flags
& FLG_IS_DISCARD
)
59 * If this section has data or will be assigned data
60 * later, mark this segment not-empty.
62 if ((shdr
->sh_size
!= 0) ||
63 ((isp
->is_flags
& FLG_IS_EXTERNAL
) == 0))
64 osp
->os_sgdesc
->sg_flags
|= FLG_SG_PHREQ
;
66 if ((ofl
->ofl_dtflags_1
& DF_1_NOHDR
) &&
67 (osp
->os_sgdesc
->sg_phdr
).p_type
!= PT_LOAD
)
70 osp
->os_sgdesc
->sg_align
=
71 ld_lcm(osp
->os_sgdesc
->sg_align
, shdr
->sh_addralign
);
75 * Return the first input descriptor for a given output descriptor,
76 * or NULL if there are none.
80 ld_os_first_isdesc(Os_desc
*osp
)
84 for (i
= 0; i
< OS_ISD_NUM
; i
++) {
85 APlist
*ap_isdesc
= osp
->os_isdescs
[i
];
87 if (aplist_nitems(ap_isdesc
) > 0)
88 return ((Is_desc
*)ap_isdesc
->apl_data
[0]);
95 * Attach an input section to an output section
98 * ofl - File descriptor
99 * osp - Output section descriptor
100 * isp - Input section descriptor
101 * mapfile_sort - True (1) if segment supports mapfile specified ordering
102 * of otherwise unordered input sections, and False (0) otherwise.
105 * - The input section has been attached to the output section
106 * - If the input section is a candidate for string table merging,
107 * then it is appended to the output section's list of merge
108 * candidates (os_mstridescs).
110 * On success, returns True (1). On failure, False (0).
113 os_attach_isp(Ofl_desc
*ofl
, Os_desc
*osp
, Is_desc
*isp
, int mapfile_sort
)
115 Aliste init_arritems
;
116 int os_isdescs_idx
, do_append
= 1;
118 if ((isp
->is_flags
& FLG_IS_ORDERED
) == 0) {
119 init_arritems
= AL_CNT_OS_ISDESCS
;
120 os_isdescs_idx
= OS_ISD_DEFAULT
;
123 * If section ordering was specified for an unordered section
124 * via the mapfile, then search in the OS_ISD_DEFAULT list
125 * and insert it in the specified position. Ordered sections
126 * are placed in ascending order before unordered sections
127 * (sections with an is_ordndx value of zero).
129 * If no mapfile ordering was specified, we append it in
130 * the usual way below.
132 if (mapfile_sort
&& (isp
->is_ordndx
> 0)) {
133 APlist
*ap_isdesc
= osp
->os_isdescs
[OS_ISD_DEFAULT
];
137 for (APLIST_TRAVERSE(ap_isdesc
, idx2
, isp2
)) {
138 if (isp2
->is_ordndx
&&
139 (isp2
->is_ordndx
<= isp
->is_ordndx
))
143 &osp
->os_isdescs
[OS_ISD_DEFAULT
],
144 isp
, init_arritems
, idx2
) == NULL
)
150 } else { /* Ordered section (via shdr flags) */
153 /* SHF_ORDERED uses sh_info, SHF_LINK_ORDERED uses sh_link */
154 shndx
= (isp
->is_shdr
->sh_flags
& SHF_ORDERED
) ?
155 isp
->is_shdr
->sh_info
: isp
->is_shdr
->sh_link
;
157 if (shndx
== SHN_BEFORE
) {
158 init_arritems
= AL_CNT_OS_ISDESCS_BA
;
159 os_isdescs_idx
= OS_ISD_BEFORE
;
160 } else if (shndx
== SHN_AFTER
) {
161 init_arritems
= AL_CNT_OS_ISDESCS_BA
;
162 os_isdescs_idx
= OS_ISD_AFTER
;
164 init_arritems
= AL_CNT_OS_ISDESCS
;
165 os_isdescs_idx
= OS_ISD_ORDERED
;
170 * If we didn't insert a section into the default list using
171 * mapfile specified ordering above, then append the input
172 * section to the appropriate list.
174 if (do_append
&& aplist_append(&(osp
->os_isdescs
[os_isdescs_idx
]),
175 isp
, init_arritems
) == NULL
)
177 isp
->is_osdesc
= osp
;
180 * A section can be merged if the following are true:
181 * - The SHF_MERGE|SHF_STRINGS flags must be set
182 * - String table compression must not be disabled (-znocompstrtab)
183 * - Mapfile ordering must not have been used.
184 * - The section must not be ordered via section header flags.
185 * - It must not be the generated section being built to
186 * replace the sections on this list.
188 if (((isp
->is_shdr
->sh_flags
& (SHF_MERGE
| SHF_STRINGS
)) !=
189 (SHF_MERGE
| SHF_STRINGS
)) ||
190 ((ofl
->ofl_flags1
& FLG_OF1_NCSTTAB
) != 0) ||
192 ((isp
->is_flags
& (FLG_IS_ORDERED
| FLG_IS_GNSTRMRG
)) != 0))
196 * Skip sections with (sh_entsize > 1) or (sh_addralign > 1).
199 * We are currently only able to merge string tables containing
200 * strings with 1-byte (char) characters. Support for wide
201 * characters will require our string table compression code
202 * to be extended to handle larger character sizes.
205 * Alignments greater than 1 would require our string table
206 * compression code to insert null bytes to move each
207 * string to the required alignment.
209 if ((isp
->is_shdr
->sh_entsize
> 1) ||
210 (isp
->is_shdr
->sh_addralign
> 1)) {
211 DBG_CALL(Dbg_sec_unsup_strmerge(ofl
->ofl_lml
, isp
));
215 if (aplist_append(&osp
->os_mstrisdescs
, isp
,
216 AL_CNT_OS_MSTRISDESCS
) == NULL
)
220 * The SHF_MERGE|SHF_STRINGS flags tell us that the program that
221 * created the section intended it to be mergeable. The
222 * FLG_IS_INSTRMRG flag says that we have done validity testing
223 * and decided that it is safe to act on that hint.
225 isp
->is_flags
|= FLG_IS_INSTRMRG
;
231 * Determine whether this input COMDAT section already exists for the associated
232 * output section. If so, then discard this input section. Otherwise, this
233 * must be the first COMDAT section, thus it is kept for future comparisons.
236 add_comdat(Ofl_desc
*ofl
, Os_desc
*osp
, Is_desc
*isp
)
244 * Sections to which COMDAT groups apply are FLG_IS_COMDAT but are
245 * discarded separately by the group logic so should never be
248 if ((isp
->is_shdr
->sh_flags
& SHF_GROUP
) &&
249 ((gr
= ld_get_group(ofl
, isp
)) != NULL
) &&
250 (gr
->gd_data
[0] & GRP_COMDAT
))
254 * Create a COMDAT avl tree for this output section if required.
256 if ((avlt
= osp
->os_comdats
) == NULL
) {
257 if ((avlt
= libld_calloc(sizeof (avl_tree_t
), 1)) == NULL
)
259 avl_create(avlt
, isdavl_compare
, sizeof (Isd_node
),
260 SGSOFFSETOF(Isd_node
, isd_avl
));
261 osp
->os_comdats
= avlt
;
265 * A standard COMDAT section uses the section name as search key.
267 isd
.isd_name
= isp
->is_name
;
268 isd
.isd_hash
= sgs_str_hash(isd
.isd_name
);
270 if ((isdp
= avl_find(avlt
, &isd
, &where
)) != NULL
) {
271 isp
->is_osdesc
= osp
;
274 * If this section hasn't already been identified as discarded,
275 * generate a suitable diagnostic.
277 if ((isp
->is_flags
& FLG_IS_DISCARD
) == 0) {
278 isp
->is_flags
|= FLG_IS_DISCARD
;
279 isp
->is_comdatkeep
= isdp
->isd_isp
;
280 DBG_CALL(Dbg_sec_discarded(ofl
->ofl_lml
, isp
,
285 * A discarded section does not require assignment to an output
286 * section. However, if relaxed relocations have been enabled
287 * (either from -z relaxreloc, or asserted with .gnu.linkonce
288 * processing), then this section must still be assigned to an
289 * output section so that the sloppy relocation logic will have
290 * the information necessary to do its work.
296 * This is a new COMDAT section - so keep it.
298 if ((isdp
= libld_calloc(sizeof (Isd_node
), 1)) == NULL
)
301 isdp
->isd_name
= isd
.isd_name
;
302 isdp
->isd_hash
= isd
.isd_hash
;
305 avl_insert(avlt
, isdp
, where
);
310 * Determine whether a GNU group COMDAT section name follows the convention
312 * section-name.symbol-name
314 * Each section within the input file is compared to see if the full section
315 * name matches the beginning of the COMDAT section, with a following '.'.
316 * A pointer to the symbol name, starting with the '.' is returned so that the
317 * caller can strip off the required section name.
320 gnu_comdat_sym(Ifl_desc
*ifl
, Is_desc
*gisp
)
324 for (ndx
= 1; ndx
< ifl
->ifl_shnum
; ndx
++) {
328 if (((isp
= ifl
->ifl_isdesc
[ndx
]) == NULL
) ||
329 (isp
== gisp
) || (isp
->is_name
== NULL
))
333 * It's questionable whether this size should be cached in the
334 * Is_desc. However, this seems an infrequent operation and
335 * adding Is_desc members can escalate memory usage for large
336 * link-edits. For now, size the section name dynamically.
338 ssize
= strlen(isp
->is_name
);
339 if ((strncmp(isp
->is_name
, gisp
->is_name
, ssize
) == 0) &&
340 (gisp
->is_name
[ssize
] == '.'))
341 return ((char *)&gisp
->is_name
[ssize
]);
347 * GNU .gnu.linkonce sections follow a naming convention that indicates the
348 * required association with an output section. Determine whether this input
349 * section follows the convention, and if so return the appropriate output
352 * .gnu.linkonce.b.* -> .bss
353 * .gnu.linkonce.d.* -> .data
354 * .gnu.linkonce.l.* -> .ldata
355 * .gnu.linkonce.lb.* -> .lbss
356 * .gnu.linkonce.lr.* -> .lrodata
357 * .gnu.linkonce.r.* -> .rodata
358 * .gnu.linkonce.s.* -> .sdata
359 * .gnu.linkonce.s2.* -> .sdata2
360 * .gnu.linkonce.sb.* -> .sbss
361 * .gnu.linkonce.sb2.* -> .sbss2
362 * .gnu.linkonce.t.* -> .text
363 * .gnu.linkonce.tb.* -> .tbss
364 * .gnu.linkonce.td.* -> .tdata
365 * .gnu.linkonce.wi.* -> .debug_info
367 #define NSTR_CH1(ch) (*(nstr + 1) == (ch))
368 #define NSTR_CH2(ch) (*(nstr + 2) == (ch))
369 #define NSTR_CH3(ch) (*(nstr + 3) == (ch))
372 gnu_linkonce_sec(const char *ostr
)
374 const char *nstr
= &ostr
[MSG_SCN_GNU_LINKONCE_SIZE
];
379 return (MSG_ORIG(MSG_SCN_BSS
));
383 return (MSG_ORIG(MSG_SCN_DATA
));
387 return (MSG_ORIG(MSG_SCN_LDATA
));
388 else if (NSTR_CH1('b') && NSTR_CH2('.'))
389 return (MSG_ORIG(MSG_SCN_LBSS
));
390 else if (NSTR_CH1('r') && NSTR_CH2('.'))
391 return (MSG_ORIG(MSG_SCN_LRODATA
));
395 return (MSG_ORIG(MSG_SCN_RODATA
));
399 return (MSG_ORIG(MSG_SCN_SDATA
));
400 else if (NSTR_CH1('2') && NSTR_CH2('.'))
401 return (MSG_ORIG(MSG_SCN_SDATA2
));
402 else if (NSTR_CH1('b') && NSTR_CH2('.'))
403 return (MSG_ORIG(MSG_SCN_SBSS
));
404 else if (NSTR_CH1('b') && NSTR_CH2('2') && NSTR_CH3('.'))
405 return (MSG_ORIG(MSG_SCN_SBSS2
));
409 return (MSG_ORIG(MSG_SCN_TEXT
));
410 else if (NSTR_CH1('b') && NSTR_CH2('.'))
411 return (MSG_ORIG(MSG_SCN_TBSS
));
412 else if (NSTR_CH1('d') && NSTR_CH2('.'))
413 return (MSG_ORIG(MSG_SCN_TDATA
));
416 if (NSTR_CH1('i') && NSTR_CH2('.'))
417 return (MSG_ORIG(MSG_SCN_DEBUG_INFO
));
424 * No special name match found.
433 * The GNU link-editor maps sections generated by the GNU compiler separately
434 * due to -ffunction-sections, -fdata-sections or for other reasons into the
435 * "normal" section represented.
437 * Sections are named .<main>.<symbol> where <main> is the usual section to
438 * which it should be mapped, and <symbol> is providing the unique name for
439 * the original section. Both parts of the name may contain periods, in cases
440 * where the unique part of the name contains a '.' and/or the section it
441 * contributes to does (such as .data.rel.ro)
443 * .rodata.str* and .rodata.cst* are mapped to .rodata.
445 * As a further complication, the GNU link-editor may or may not merge
446 * .ctors.* and .dtors.* into init_array and fini_array, rather than ctors and
447 * dtors. We do not implement this at this time.
449 * The GNU link editor may also arrange for sections with .local in their name
450 * to be mapped as above, but grouped together. We do not implement this (and
451 * do not merge them at all, to make this clear)
453 * This table is processed in order. Longer mappings must come first.
455 static struct split_sec_mapping
{
459 } split_sec_mapping
[] = {
460 { ".bss.", ".bss", B_FALSE
},
461 { ".ctors.", ".ctors", B_FALSE
},
462 { ".data.rel.local.", ".data.rel.local", B_FALSE
},
463 { ".data.rel.local", ".data.rel.local", B_TRUE
},
464 { ".data.rel.ro.local.", ".data.rel.ro", B_FALSE
},
465 { ".data.rel.ro.", ".data.rel.ro", B_FALSE
},
466 { ".data.rel.ro", ".data.rel.ro", B_TRUE
},
467 { ".data.rel.", ".data.rel", B_FALSE
},
468 { ".data.rel", ".data.rel", B_TRUE
},
469 { ".data.", ".data", B_FALSE
},
470 { ".dtors.", ".dtors", B_FALSE
},
471 { ".fini_array.", ".fini_array", B_FALSE
},
472 { ".init_array.", ".init_array", B_FALSE
},
473 { ".lbss.", ".lbss", B_FALSE
},
474 { ".ldata.", ".ldata", B_FALSE
},
475 { ".lrodata.", ".lrodata", B_FALSE
},
476 /* This intentionally applies to .rodata.cstN and .rodata.strN, too */
477 { ".rodata.", ".rodata", B_FALSE
},
478 { ".sbss2.", ".sbss2", B_FALSE
},
479 { ".sbss.", ".sbss", B_FALSE
},
480 { ".sdata2.", ".sdata2", B_FALSE
},
481 { ".sdata.", ".sdata", B_FALSE
},
482 { ".tbss.", ".tbss", B_FALSE
},
483 { ".tdata.", ".tdata", B_FALSE
},
484 { ".text.", ".text", B_FALSE
},
485 { NULL
, NULL
, B_FALSE
}
489 gnu_split_sec(const char *ostr
)
491 struct split_sec_mapping
*mp
;
493 for (mp
= split_sec_mapping
; mp
->leader
!= NULL
; mp
++) {
495 if (strcmp(ostr
, mp
->leader
) == 0)
496 return (mp
->section
);
497 } else if (strncmp(ostr
, mp
->leader
, strlen(mp
->leader
)) == 0) {
498 return (mp
->section
);
506 * Initialize a path info buffer for use with ld_place_section().
509 * ofl - Output descriptor
510 * ifl - Descriptor for input file, or NULL if there is none.
511 * info - Address of buffer to be initialized.
514 * If this is an input file, and if the entrance criteria list
515 * contains at least one criteria that has a non-empty file string
516 * match list (ec_files), then the block pointed at by info is
517 * initialized, and info is returned.
519 * If there is no input file, and/or no entrance criteria containing
520 * a non-empty ec_files list, then NULL is returned. This is not
521 * an error --- the NULL is simply an optimization, understood by
522 * ld_place_path(), that allows it to skip unnecessary work.
525 ld_place_path_info_init(Ofl_desc
*ofl
, Ifl_desc
*ifl
, Place_path_info
*info
)
528 * Return NULL if there is no input file (internally generated section)
529 * or if the entrance criteria list does not contain any items that will
530 * need to be compared to the path (all the ec_files lists are empty).
532 if ((ifl
== NULL
) || !(ofl
->ofl_flags
& FLG_OF_EC_FILES
))
535 info
->ppi_path
= ifl
->ifl_name
;
536 info
->ppi_path_len
= strlen(info
->ppi_path
);
537 info
->ppi_isar
= (ifl
->ifl_flags
& FLG_IF_EXTRACT
) != 0;
540 * The basename is the final segment of the path, equivalent to
541 * the path itself if there are no '/' delimiters.
543 info
->ppi_bname
= strrchr(info
->ppi_path
, '/');
544 if (info
->ppi_bname
== NULL
)
545 info
->ppi_bname
= info
->ppi_path
;
547 info
->ppi_bname
++; /* Skip leading '/' */
548 info
->ppi_bname_len
=
549 info
->ppi_path_len
- (info
->ppi_bname
- info
->ppi_path
);
552 * For an archive, the object name is the member name, which is
553 * enclosed in () at the end of the name string. Otherwise, it is
554 * the same as the basename.
556 if (info
->ppi_isar
) {
557 info
->ppi_oname
= strrchr(info
->ppi_bname
, '(');
558 /* There must be an archive member suffix delimited by parens */
559 assert((info
->ppi_bname
[info
->ppi_bname_len
- 1] == ')') &&
560 (info
->ppi_oname
!= NULL
));
561 info
->ppi_oname
++; /* skip leading '(' */
562 info
->ppi_oname_len
= info
->ppi_bname_len
-
563 (info
->ppi_oname
- info
->ppi_bname
+ 1);
565 info
->ppi_oname
= info
->ppi_bname
;
566 info
->ppi_oname_len
= info
->ppi_bname_len
;
573 * Compare an input section path to the file comparison list the given
577 * path_info - A non-NULL Place_path_info block for the file
578 * containing the input section, initialized by
579 * ld_place_path_info_init()
580 * enp - Entrance criteria with a non-empty ec_files list of file
581 * comparisons to be carried out.
584 * Return TRUE if a match is seen, and FALSE otherwise.
587 eval_ec_files(Place_path_info
*path_info
, Ent_desc
*enp
)
594 for (ALIST_TRAVERSE(enp
->ec_files
, idx
, edfp
)) {
595 Word type
= edfp
->edf_flags
& TYP_ECF_MASK
;
598 * Determine the starting character, and # of characters,
599 * from the file path to compare against this entrance criteria
602 if (type
== TYP_ECF_OBJNAME
) {
603 cmp_str
= path_info
->ppi_oname
;
604 cmp_len
= path_info
->ppi_oname_len
;
606 int ar_stat_diff
= path_info
->ppi_isar
!=
607 ((edfp
->edf_flags
& FLG_ECF_ARMEMBER
) != 0);
610 * If the entrance criteria specifies an archive member
611 * and the file does not, then there can be no match.
614 if (ar_stat_diff
&& !path_info
->ppi_isar
)
617 if (type
== TYP_ECF_PATH
) {
618 cmp_str
= path_info
->ppi_path
;
619 cmp_len
= path_info
->ppi_path_len
;
620 } else { /* TYP_ECF_BASENAME */
621 cmp_str
= path_info
->ppi_bname
;
622 cmp_len
= path_info
->ppi_bname_len
;
626 * If the entrance criteria does not specify an archive
627 * member and the file does, then a match just requires
628 * the paths (without the archive member) to match.
629 * Reduce the length to not include the ar member or
630 * the '(' that precedes it.
632 if (ar_stat_diff
&& path_info
->ppi_isar
)
633 cmp_len
= path_info
->ppi_oname
- cmp_str
- 1;
637 * Compare the resulting string to the one from the
640 if ((cmp_len
== edfp
->edf_name_len
) &&
641 (strncmp(edfp
->edf_name
, cmp_str
, cmp_len
) == 0))
649 * Replace the section header for the given input section with a new section
650 * header of the specified type. All values in the replacement header other
651 * than the type retain their previous values.
654 * isp - Input section to replace
655 * sh_type - New section type to apply
658 * Returns the pointer to the new section header on success, and
662 isp_convert_type(Is_desc
*isp
, Word sh_type
)
666 if ((shdr
= libld_malloc(sizeof (Shdr
))) == NULL
)
668 *shdr
= *isp
->is_shdr
;
670 shdr
->sh_type
= sh_type
;
675 * Issue a fatal warning for the given .eh_frame section, which
676 * cannot be merged with the existing .eh_frame output section.
679 eh_frame_muldef(Ofl_desc
*ofl
, Is_desc
*isp
)
684 Aliste idx1
, idx2
, idx3
;
687 * Locate the .eh_frame output section, and use the first section
688 * assigned to it in the error message. The user can then compare
689 * the two sections to determine what attribute prevented the merge.
691 for (APLIST_TRAVERSE(ofl
->ofl_segs
, idx1
, sgp
)) {
692 for (APLIST_TRAVERSE(sgp
->sg_osdescs
, idx2
, osp
)) {
693 if ((osp
->os_flags
& FLG_OS_EHFRAME
) == 0)
696 for (idx3
= 0; idx3
< OS_ISD_NUM
; idx3
++) {
697 APlist
*lst
= osp
->os_isdescs
[idx3
];
699 if (aplist_nitems(lst
) == 0)
702 isp1
= lst
->apl_data
[0];
703 ld_eprintf(ofl
, ERR_FATAL
,
704 MSG_INTL(MSG_UPD_MULEHFRAME
),
705 isp1
->is_file
->ifl_name
,
706 EC_WORD(isp1
->is_scnndx
), isp1
->is_name
,
707 isp
->is_file
->ifl_name
,
708 EC_WORD(isp
->is_scnndx
), isp
->is_name
);
716 * Place a section into the appropriate segment and output section.
719 * ofl - File descriptor
720 * isp - Input section descriptor of section to be placed.
721 * path_info - NULL, or pointer to Place_path_info buffer initialized
722 * by ld_place_path_info_init() for the file associated to isp,
723 * for use in processing entrance criteria with non-empty
724 * file matching string list (ec_files)
725 * ident - Section identifier, used to order sections relative to
726 * others within the output segment.
727 * alt_os_name - If non-NULL, the name of the output section to place
728 * isp into. If NULL, input sections go to an output section
729 * with the same name as the input section.
732 ld_place_section(Ofl_desc
*ofl
, Is_desc
*isp
, Place_path_info
*path_info
,
733 int ident
, const char *alt_os_name
)
740 Shdr
*shdr
= isp
->is_shdr
;
741 Xword shflagmask
, shflags
= shdr
->sh_flags
;
742 Ifl_desc
*ifl
= isp
->is_file
;
745 Boolean is_ehframe
= (isp
->is_flags
& FLG_IS_EHFRAME
) != 0;
748 * Define any sections that must be thought of as referenced. These
749 * sections may not be referenced externally in a manner ld(1) can
750 * discover, but they must be retained (ie. not removed by -zignore).
752 static const Msg RefSecs
[] = {
753 MSG_SCN_INIT
, /* MSG_ORIG(MSG_SCN_INIT) */
754 MSG_SCN_FINI
, /* MSG_ORIG(MSG_SCN_FINI) */
755 MSG_SCN_EX_RANGES
, /* MSG_ORIG(MSG_SCN_EX_RANGES) */
756 MSG_SCN_EX_SHARED
, /* MSG_ORIG(MSG_SCN_EX_SHARED) */
757 MSG_SCN_CTORS
, /* MSG_ORIG(MSG_SCN_CTORS) */
758 MSG_SCN_DTORS
, /* MSG_ORIG(MSG_SCN_DTORS) */
759 MSG_SCN_EHFRAME
, /* MSG_ORIG(MSG_SCN_EHFRAME) */
760 MSG_SCN_EHFRAME_HDR
, /* MSG_ORIG(MSG_SCN_EHFRAME_HDR) */
761 MSG_SCN_JCR
, /* MSG_ORIG(MSG_SCN_JCR) */
762 MSG_SCN_INITARRAY
, /* MSG_ORIG(MSG_SCN_INITARRAY) */
763 MSG_SCN_FINIARRAY
, /* MSG_ORIG(MSG_SCN_FINIARRAY) */
764 MSG_SCN_PREINITARRAY
, /* MSG_ORIG(MSG_SCN_PREINITARRAY) */
768 DBG_CALL(Dbg_sec_in(ofl
->ofl_lml
, isp
));
771 * If this section identifies group members, or this section indicates
772 * that it is a member of a group, determine whether the section is
775 if ((shflags
& SHF_GROUP
) || (shdr
->sh_type
== SHT_GROUP
)) {
778 if ((gdesc
= ld_get_group(ofl
, isp
)) != NULL
) {
779 DBG_CALL(Dbg_sec_group(ofl
->ofl_lml
, isp
, gdesc
));
782 * If this group has been replaced by another group,
783 * then this section needs to be discarded.
785 if (gdesc
->gd_oisc
) {
786 isp
->is_flags
|= FLG_IS_DISCARD
;
789 * Since we're discarding the section, we
790 * can skip assigning it to an output section.
791 * The exception is that if the user
792 * specifies -z relaxreloc, then
793 * we need to assign the output section so
794 * that the sloppy relocation logic will have
795 * the information necessary to do its work.
797 if (!(ofl
->ofl_flags1
& FLG_OF1_RLXREL
))
803 * SHT_GROUP sections can only be included into relocatable
806 if (shdr
->sh_type
== SHT_GROUP
) {
807 if ((ofl
->ofl_flags
& FLG_OF_RELOBJ
) == 0) {
808 isp
->is_flags
|= FLG_IS_DISCARD
;
815 * Always assign SHF_TLS sections to the DATA segment (and then the
816 * PT_TLS embedded inside of there).
818 if (shflags
& SHF_TLS
)
819 shflags
|= SHF_WRITE
;
822 * Traverse the entrance criteria list searching for a segment that
823 * matches the input section we have. If an entrance criterion is set
824 * then there must be an exact match. If we complete the loop without
825 * finding a segment, then sgp will be NULL.
828 for (APLIST_TRAVERSE(ofl
->ofl_ents
, idx1
, enp
)) {
829 int mask
= is_ehframe
? SHF_WRITE
: 0;
831 /* Disabled segments are not available for assignment */
832 if (enp
->ec_segment
->sg_flags
& FLG_SG_DISABLED
)
836 * If an entrance criteria doesn't have any of its fields
837 * set, it will match any section it is tested against.
838 * We set the FLG_EC_CATCHALL flag on these, primarily because
839 * it helps readers of our debug output to understand what
840 * the criteria means --- otherwise the user would just see
841 * that every field is 0, but might not understand the
842 * significance of that.
844 * Given that we set this flag, we can use it here as an
845 * optimization to short circuit all of the tests in this
846 * loop. Note however, that if we did not do this, the end
847 * result would be the same --- the empty criteria will sail
848 * past the following tests and reach the end of the loop.
850 if (enp
->ec_flags
& FLG_EC_CATCHALL
) {
851 sgp
= enp
->ec_segment
;
855 if (enp
->ec_type
&& (enp
->ec_type
!= shdr
->sh_type
))
857 if (enp
->ec_attrmask
&&
859 (enp
->ec_attrmask
& enp
->ec_attrbits
) !=
860 (enp
->ec_attrmask
& (shflags
& ~mask
)))
862 if (enp
->ec_is_name
&&
863 (strcmp(enp
->ec_is_name
, isp
->is_name
) != 0))
866 if ((alist_nitems(enp
->ec_files
) > 0) &&
867 ((path_info
== NULL
) || !eval_ec_files(path_info
, enp
)))
870 /* All entrance criteria tests passed */
871 sgp
= enp
->ec_segment
;
876 * The final entrance criteria record is a FLG_EC_CATCHALL that points
877 * at the final predefined segment "extra", and this final segment is
878 * tagged FLG_SG_NODISABLE. Therefore, the above loop must always find
884 * Transfer the input section sorting key from the entrance criteria
885 * to the input section. A non-zero value means that the section
886 * will be sorted on this key amoung the other sections that have a
887 * non-zero key. These sorted sections are collectively placed at the
888 * head of the output section.
890 * If the sort key is 0, the section is placed after the sorted
891 * sections in the order they are encountered.
893 isp
->is_ordndx
= enp
->ec_ordndx
;
895 /* Remember that this entrance criteria has placed a section */
896 enp
->ec_flags
|= FLG_EC_USED
;
899 * If our caller has supplied an alternative name for the output
900 * section, then we defer to their request. Otherwise, the default
901 * is to use the same name as that of the input section being placed.
903 * The COMDAT, SHT_GROUP and GNU name translations that follow have
904 * the potential to alter this initial name.
906 oname
= (char *)((alt_os_name
== NULL
) ? isp
->is_name
: alt_os_name
);
909 * Solaris section names may follow the convention:
911 * section-name%symbol-name
913 * This convention has been used to order the layout of sections within
914 * segments for objects built with the compilers -xF option. However,
915 * the final object should not contain individual section headers for
916 * all such input sections, instead the symbol name is stripped from the
917 * name to establish the final output section name.
919 * This convention has also been followed for COMDAT and sections
920 * identified though SHT_GROUP data.
922 * Strip out the % from the section name for:
923 * - Non-relocatable objects
924 * - Relocatable objects if input section sorting is
925 * in force for the segment in question.
927 if (((ofl
->ofl_flags
& FLG_OF_RELOBJ
) == 0) ||
928 (sgp
->sg_flags
& FLG_SG_IS_ORDER
)) {
929 if ((sname
= strchr(isp
->is_name
, '%')) != NULL
) {
930 size_t size
= sname
- isp
->is_name
;
932 if ((oname
= libld_malloc(size
+ 1)) == NULL
)
933 return ((Os_desc
*)S_ERROR
);
934 (void) strncpy(oname
, isp
->is_name
, size
);
936 DBG_CALL(Dbg_sec_redirected(ofl
->ofl_lml
, isp
, oname
));
941 * When building relocatable objects, we must not redirect COMDAT
942 * section names into their outputs, such that our output object may
943 * be successfully used as an input object also requiring COMDAT
948 * GNU section names may follow the convention:
952 * The .gnu.linkonce is a section naming convention that indicates a
953 * COMDAT requirement. Determine whether this section follows the GNU
954 * pattern, and if so, determine whether this section should be
955 * discarded or retained. The comparison of is_name[1] with 'g'
956 * is an optimization to skip using strncmp() too much. This is safe,
957 * because we know the name is not NULL, and therefore must have
958 * at least one character plus a NULL termination.
960 if ((isp
->is_name
== oname
) && (isp
->is_name
[1] == 'g') &&
961 (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE
), isp
->is_name
,
962 MSG_SCN_GNU_LINKONCE_SIZE
) == 0)) {
963 if ((ofl
->ofl_flags
& FLG_OF_RELOBJ
) == 0) {
964 if ((oname
= (char *)gnu_linkonce_sec(isp
->is_name
)) !=
966 DBG_CALL(Dbg_sec_redirected(ofl
->ofl_lml
, isp
,
972 * Explicitly identify this section type as COMDAT. Also,
973 * enable relaxed relocation processing, as this is typically
974 * a requirement with .gnu.linkonce sections.
976 isp
->is_flags
|= FLG_IS_COMDAT
;
977 if ((ofl
->ofl_flags1
& FLG_OF1_NRLXREL
) == 0)
978 ofl
->ofl_flags1
|= FLG_OF1_RLXREL
;
979 DBG_CALL(Dbg_sec_gnu_comdat(ofl
->ofl_lml
, isp
, TRUE
,
980 (ofl
->ofl_flags1
& FLG_OF1_RLXREL
) != 0));
984 * GNU section names may also follow the convention:
986 * section-name.symbol-name
988 * This convention is used when defining SHT_GROUP sections of type
989 * COMDAT. Thus, any group processing will have discovered any group
990 * sections, and this identification can be triggered by a pattern
991 * match section names.
993 if ((isp
->is_name
== oname
) && (isp
->is_flags
& FLG_IS_COMDAT
) &&
994 ((sname
= gnu_comdat_sym(ifl
, isp
)) != NULL
)) {
995 size_t size
= sname
- isp
->is_name
;
997 if ((ofl
->ofl_flags
& FLG_OF_RELOBJ
) == 0) {
998 if ((oname
= libld_malloc(size
+ 1)) == NULL
)
999 return ((Os_desc
*)S_ERROR
);
1000 (void) strncpy(oname
, isp
->is_name
, size
);
1002 DBG_CALL(Dbg_sec_redirected(ofl
->ofl_lml
, isp
, oname
));
1006 * Enable relaxed relocation processing, as this is
1007 * typically a requirement with GNU COMDAT sections.
1009 if ((ofl
->ofl_flags1
& FLG_OF1_NRLXREL
) == 0) {
1010 ofl
->ofl_flags1
|= FLG_OF1_RLXREL
;
1011 DBG_CALL(Dbg_sec_gnu_comdat(ofl
->ofl_lml
, isp
,
1017 * GNU section names named section-name.symbol-name which are not
1018 * members of COMDAT groups are merged according to the behaviour of
1019 * the GNU link-editor.
1021 * See the description of gnu_split_sec().
1023 if (((ofl
->ofl_flags
& FLG_OF_RELOBJ
) == 0) &&
1024 (isp
->is_name
== oname
) &&
1025 ((oname
= (char *)gnu_split_sec(oname
)) != isp
->is_name
)) {
1026 DBG_CALL(Dbg_sec_redirected(ofl
->ofl_lml
, isp
, oname
));
1030 * Assign a hash value now that the output section name has been
1033 onamehash
= sgs_str_hash(oname
);
1036 * Determine if output section ordering is turned on. If so, return
1037 * the appropriate ordering index for the section. This information
1038 * is derived from the Sg_desc->sg_os_order list that was built
1039 * up from the Mapfile.
1041 * A value of 0 for os_ndx means that the section is not sorted
1042 * (i.e. is not found in the sg_os_order). The items in sg_os_order
1043 * are in the desired sort order, so adding 1 to their alist index
1044 * gives a suitable index for sorting.
1047 if (alist_nitems(sgp
->sg_os_order
) > 0) {
1050 for (ALIST_TRAVERSE(sgp
->sg_os_order
, idx1
, scop
)) {
1051 if (strcmp(scop
->sco_secname
, oname
) == 0) {
1052 scop
->sco_flags
|= FLG_SGO_USED
;
1060 * Mask of section header flags to ignore when matching sections. We
1061 * are more strict with relocatable objects, ignoring only the order
1062 * flags, and keeping sections apart if they differ otherwise. This
1063 * follows the policy that sections in a relative object should only
1064 * be merged if their flags are the same, and avoids destroying
1065 * information prematurely. For final products however, we ignore all
1066 * flags that do not prevent a merge.
1069 (ofl
->ofl_flags
& FLG_OF_RELOBJ
) ? ALL_SHF_ORDER
: ALL_SHF_IGNORE
;
1071 if (is_ehframe
&& ld_targ
.t_m
.m_mach
== EM_386
)
1072 shflagmask
|= SHF_WRITE
;
1075 * Traverse the input section list for the output section we have been
1076 * assigned. If we find a matching section simply add this new section.
1079 for (APLIST_TRAVERSE(sgp
->sg_osdescs
, idx1
, osp
)) {
1080 Shdr
*os_shdr
= osp
->os_shdr
;
1083 * An input section matches an output section if:
1084 * - The ident values match
1086 * - Not a GROUP section
1087 * - Not a DTrace dof section
1088 * - Section types match
1089 * - Matching section flags, after screening out the
1092 * Section types are considered to match if any one of
1093 * the following are true:
1094 * - The type codes are the same
1095 * - Both are .eh_frame sections (regardless of type code)
1096 * - The input section is COMDAT, and the output section
1099 if ((ident
== osp
->os_identndx
) &&
1100 (ident
!= ld_targ
.t_id
.id_rel
) &&
1101 (onamehash
== osp
->os_namehash
) &&
1102 (shdr
->sh_type
!= SHT_GROUP
) &&
1103 (shdr
->sh_type
!= SHT_SUNW_dof
) &&
1104 ((shdr
->sh_type
== os_shdr
->sh_type
) ||
1105 (is_ehframe
&& (osp
->os_flags
& FLG_OS_EHFRAME
)) ||
1106 ((shdr
->sh_type
== SHT_SUNW_COMDAT
) &&
1107 (os_shdr
->sh_type
== SHT_PROGBITS
))) &&
1108 ((shflags
& ~shflagmask
) ==
1109 (os_shdr
->sh_flags
& ~shflagmask
)) &&
1110 (strcmp(oname
, osp
->os_name
) == 0)) {
1114 * Process any COMDAT section, keeping the first and
1115 * discarding all others.
1117 if ((isp
->is_flags
& FLG_IS_COMDAT
) &&
1118 ((err
= add_comdat(ofl
, osp
, isp
)) != 1))
1119 return ((Os_desc
*)err
);
1124 set_addralign(ofl
, osp
, isp
);
1127 * If this section is a non-empty TLS section indicate
1128 * that a PT_TLS program header is required.
1130 if ((shflags
& SHF_TLS
) && shdr
->sh_size
&&
1131 ((ofl
->ofl_flags
& FLG_OF_RELOBJ
) == 0))
1132 ofl
->ofl_flags
|= FLG_OF_TLSPHDR
;
1135 * Insert the input section descriptor on the proper
1136 * output section descriptor list.
1138 * If this segment requires input section ordering,
1139 * honor any mapfile specified ordering for otherwise
1140 * unordered sections by setting the mapfile_sort
1141 * argument of os_attach_isp() to True.
1144 if (os_attach_isp(ofl
, osp
, isp
,
1145 (sgp
->sg_flags
& FLG_SG_IS_ORDER
) != 0) == 0)
1146 return ((Os_desc
*)S_ERROR
);
1149 * If this input section and file is associated to an
1150 * artificially referenced output section, make sure
1151 * they are marked as referenced also. This ensures
1152 * that this input section and file isn't eliminated
1153 * when -zignore is in effect.
1155 * See -zignore comments when creating a new output
1159 (ifl
->ifl_flags
& FLG_IF_IGNORE
)) || DBG_ENABLED
) &&
1160 (osp
->os_flags
& FLG_OS_SECTREF
)) {
1161 isp
->is_flags
|= FLG_IS_SECTREF
;
1163 ifl
->ifl_flags
|= FLG_IF_FILEREF
;
1166 DBG_CALL(Dbg_sec_added(ofl
->ofl_lml
, osp
, sgp
));
1171 * Do we need to worry about section ordering?
1174 if (osp
->os_ordndx
) {
1175 if (os_ndx
< osp
->os_ordndx
)
1176 /* insert section here. */
1183 /* insert section here. */
1186 } else if (osp
->os_ordndx
) {
1192 * If the new sections identifier is less than that of the
1193 * present input section we need to insert the new section
1196 if (ident
< osp
->os_identndx
)
1203 * We are adding a new output section. Update the section header
1204 * count and associated string size.
1206 * If the input section triggering this output section has been marked
1207 * for discard, and if no other non-discarded input section comes along
1208 * to join it, then we will over count. We cannot know if this will
1209 * happen or not until all input is seen. Set FLG_OF_AJDOSCNT to
1210 * trigger a final count readjustment.
1212 if (isp
->is_flags
& FLG_IS_DISCARD
)
1213 ofl
->ofl_flags
|= FLG_OF_ADJOSCNT
;
1215 if (st_insert(ofl
->ofl_shdrsttab
, oname
) == -1)
1216 return ((Os_desc
*)S_ERROR
);
1219 * Create a new output section descriptor.
1221 if ((osp
= libld_calloc(sizeof (Os_desc
), 1)) == NULL
)
1222 return ((Os_desc
*)S_ERROR
);
1223 if ((osp
->os_shdr
= libld_calloc(sizeof (Shdr
), 1)) == NULL
)
1224 return ((Os_desc
*)S_ERROR
);
1227 * Convert COMDAT section to PROGBITS as this the first section of the
1228 * output section. Save any COMDAT section for later processing, as
1229 * additional COMDAT sections that match this section need discarding.
1231 if ((shdr
->sh_type
== SHT_SUNW_COMDAT
) &&
1232 ((shdr
= isp_convert_type(isp
, SHT_PROGBITS
)) == NULL
))
1233 return ((Os_desc
*)S_ERROR
);
1234 if ((isp
->is_flags
& FLG_IS_COMDAT
) &&
1235 (add_comdat(ofl
, osp
, isp
) == S_ERROR
))
1236 return ((Os_desc
*)S_ERROR
);
1240 * Executable or sharable objects can have at most a single
1241 * .eh_frame section. Detect attempts to create more than
1242 * one. This occurs if the input sections have incompatible
1245 if ((ofl
->ofl_flags
& FLG_OF_EHFRAME
) &&
1246 !(ofl
->ofl_flags
& FLG_OF_RELOBJ
)) {
1247 eh_frame_muldef(ofl
, isp
);
1248 return ((Os_desc
*)S_ERROR
);
1250 ofl
->ofl_flags
|= FLG_OF_EHFRAME
;
1253 * For .eh_frame sections, we always set the type to be the
1254 * type specified by the ABI. This allows .eh_frame sections
1255 * of type SHT_PROGBITS to be correctly merged with .eh_frame
1256 * sections of the ABI-defined type (e.g. SHT_AMD64_UNWIND),
1257 * with the output being of the ABI-defined type.
1259 osp
->os_shdr
->sh_type
= ld_targ
.t_m
.m_sht_unwind
;
1261 osp
->os_shdr
->sh_type
= shdr
->sh_type
;
1264 osp
->os_shdr
->sh_flags
= shdr
->sh_flags
;
1265 osp
->os_shdr
->sh_entsize
= shdr
->sh_entsize
;
1266 osp
->os_name
= oname
;
1267 osp
->os_namehash
= onamehash
;
1268 osp
->os_ordndx
= os_ndx
;
1269 osp
->os_sgdesc
= sgp
;
1271 osp
->os_flags
|= FLG_OS_EHFRAME
;
1273 if (ifl
&& (shdr
->sh_type
== SHT_PROGBITS
)) {
1275 * Try to preserve the intended meaning of sh_link/sh_info.
1276 * See the translate_link() in update.c.
1278 osp
->os_shdr
->sh_link
= shdr
->sh_link
;
1279 if (shdr
->sh_flags
& SHF_INFO_LINK
)
1280 osp
->os_shdr
->sh_info
= shdr
->sh_info
;
1284 * When -zignore is in effect, user supplied sections and files that are
1285 * not referenced from other sections, are eliminated from the object
1286 * being produced. Some sections, although unreferenced, are special,
1287 * and must not be eliminated. Determine if this new output section is
1288 * one of those special sections, and if so mark it artificially as
1289 * referenced. Any input section and file associated to this output
1290 * section is also be marked as referenced, and thus won't be eliminated
1291 * from the final output.
1293 if (ifl
&& ((ofl
->ofl_flags1
& FLG_OF1_IGNPRC
) || DBG_ENABLED
)) {
1296 for (refsec
= RefSecs
; *refsec
; refsec
++) {
1297 if (strcmp(osp
->os_name
, MSG_ORIG(*refsec
)) == 0) {
1298 osp
->os_flags
|= FLG_OS_SECTREF
;
1300 if ((ifl
->ifl_flags
& FLG_IF_IGNORE
) ||
1302 isp
->is_flags
|= FLG_IS_SECTREF
;
1303 ifl
->ifl_flags
|= FLG_IF_FILEREF
;
1311 * Sections of type SHT_GROUP are added to the ofl->ofl_osgroups list,
1312 * so that they can be updated as a group later.
1314 if ((shdr
->sh_type
== SHT_GROUP
) &&
1315 ((isp
->is_flags
& FLG_IS_DISCARD
) == 0) &&
1316 (aplist_append(&ofl
->ofl_osgroups
, osp
,
1317 AL_CNT_OFL_OSGROUPS
) == NULL
))
1318 return ((Os_desc
*)S_ERROR
);
1321 * If this section is a non-empty TLS section indicate that a PT_TLS
1322 * program header is required.
1324 if ((shflags
& SHF_TLS
) && shdr
->sh_size
&&
1325 ((ofl
->ofl_flags
& FLG_OF_RELOBJ
) == 0))
1326 ofl
->ofl_flags
|= FLG_OF_TLSPHDR
;
1329 * If a non-allocatable section is going to be put into a loadable
1330 * segment then turn on the allocate bit for this section and warn the
1331 * user that we have done so. This could only happen through the use
1334 if ((sgp
->sg_phdr
.p_type
== PT_LOAD
) &&
1335 ((osp
->os_shdr
->sh_flags
& SHF_ALLOC
) == 0)) {
1336 ld_eprintf(ofl
, ERR_WARNING
, MSG_INTL(MSG_SCN_NONALLOC
),
1337 ofl
->ofl_name
, osp
->os_name
, sgp
->sg_name
);
1338 osp
->os_shdr
->sh_flags
|= SHF_ALLOC
;
1342 * Retain this sections identifier for future comparisons when placing
1343 * a section (after all sections have been processed this variable will
1344 * be used to hold the sections symbol index as we don't need to retain
1345 * the identifier any more).
1347 osp
->os_identndx
= ident
;
1352 set_addralign(ofl
, osp
, isp
);
1354 if (os_attach_isp(ofl
, osp
, isp
, 0) == 0)
1355 return ((Os_desc
*)S_ERROR
);
1357 DBG_CALL(Dbg_sec_created(ofl
->ofl_lml
, osp
, sgp
));
1360 * Insert the new section at the offset given by iidx. If no position
1361 * for it was identified above, this will be index 0, causing the new
1362 * section to be prepended to the beginning of the section list.
1363 * Otherwise, it is the index following the section that was identified.
1365 if (aplist_insert(&sgp
->sg_osdescs
, osp
, AL_CNT_SG_OSDESC
,
1367 return ((Os_desc
*)S_ERROR
);