1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 1988 AT&T
24 * All Rights Reserved
25 *
26 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
27 */
28
29 /*
30 * Map file parsing and input section to output segment mapping.
31 */
32 #include <stdio.h>
33 #include <string.h>
34 #include <debug.h>
35 #include "msg.h"
36 #include "_libld.h"
37
38 /*
39 * Each time a section is placed, the function set_addralign()
40 * is called. This function performs:
41 *
42 * - if the section is from an external file, check if this is empty or not.
43 * If not, we know the segment this section will belong needs a program
44 * header. (Of course, the program is needed only if this section falls
45 * into a loadable segment.)
46 * - compute the Least Common Multiplier for setting the segment alignment.
47 */
48 static void
set_addralign(Ofl_desc * ofl,Os_desc * osp,Is_desc * isp)49 set_addralign(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp)
50 {
51 Shdr *shdr = isp->is_shdr;
52
53 /* A discarded section has no influence on the output */
54 if (isp->is_flags & FLG_IS_DISCARD)
55 return;
56
57 /*
58 * If this section has data or will be assigned data
59 * later, mark this segment not-empty.
60 */
61 if ((shdr->sh_size != 0) ||
62 ((isp->is_flags & FLG_IS_EXTERNAL) == 0))
63 osp->os_sgdesc->sg_flags |= FLG_SG_PHREQ;
64
65 if ((ofl->ofl_dtflags_1 & DF_1_NOHDR) &&
66 (osp->os_sgdesc->sg_phdr).p_type != PT_LOAD)
67 return;
68
69 osp->os_sgdesc->sg_align =
70 ld_lcm(osp->os_sgdesc->sg_align, shdr->sh_addralign);
71 }
72
73 /*
74 * Return the first input descriptor for a given output descriptor,
75 * or NULL if there are none.
76 */
77
78 Is_desc *
ld_os_first_isdesc(Os_desc * osp)79 ld_os_first_isdesc(Os_desc *osp)
80 {
81 int i;
82
83 for (i = 0; i < OS_ISD_NUM; i++) {
84 APlist *ap_isdesc = osp->os_isdescs[i];
85
86 if (aplist_nitems(ap_isdesc) > 0)
87 return ((Is_desc *)ap_isdesc->apl_data[0]);
88 }
89
90 return (NULL);
91 }
92
93 /*
94 * Attach an input section to an output section
95 *
96 * entry:
97 * ofl - File descriptor
98 * osp - Output section descriptor
99 * isp - Input section descriptor
100 * mapfile_sort - True (1) if segment supports mapfile specified ordering
101 * of otherwise unordered input sections, and False (0) otherwise.
102 *
103 * exit:
104 * - The input section has been attached to the output section
105 * - If the input section is a candidate for string table merging,
106 * then it is appended to the output section's list of merge
107 * candidates (os_mstridescs).
108 *
109 * On success, returns True (1). On failure, False (0).
110 */
111 static int
os_attach_isp(Ofl_desc * ofl,Os_desc * osp,Is_desc * isp,int mapfile_sort)112 os_attach_isp(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp, int mapfile_sort)
113 {
114 Aliste init_arritems;
115 int os_isdescs_idx, do_append = 1;
116
117 if ((isp->is_flags & FLG_IS_ORDERED) == 0) {
118 init_arritems = AL_CNT_OS_ISDESCS;
119 os_isdescs_idx = OS_ISD_DEFAULT;
120
121 /*
122 * If section ordering was specified for an unordered section
123 * via the mapfile, then search in the OS_ISD_DEFAULT list
124 * and insert it in the specified position. Ordered sections
125 * are placed in ascending order before unordered sections
126 * (sections with an is_ordndx value of zero).
127 *
128 * If no mapfile ordering was specified, we append it in
129 * the usual way below.
130 */
131 if (mapfile_sort && (isp->is_ordndx > 0)) {
132 APlist *ap_isdesc = osp->os_isdescs[OS_ISD_DEFAULT];
133 Aliste idx2;
134 Is_desc *isp2;
135
136 for (APLIST_TRAVERSE(ap_isdesc, idx2, isp2)) {
137 if (isp2->is_ordndx &&
138 (isp2->is_ordndx <= isp->is_ordndx))
139 continue;
140
141 if (aplist_insert(
142 &osp->os_isdescs[OS_ISD_DEFAULT],
143 isp, init_arritems, idx2) == NULL)
144 return (0);
145 do_append = 0;
146 break;
147 }
148 }
149 } else { /* Ordered section (via shdr flags) */
150 Word shndx;
151
152 /* SHF_ORDERED uses sh_info, SHF_LINK_ORDERED uses sh_link */
153 shndx = (isp->is_shdr->sh_flags & SHF_ORDERED) ?
154 isp->is_shdr->sh_info : isp->is_shdr->sh_link;
155
156 if (shndx == SHN_BEFORE) {
157 init_arritems = AL_CNT_OS_ISDESCS_BA;
158 os_isdescs_idx = OS_ISD_BEFORE;
159 } else if (shndx == SHN_AFTER) {
160 init_arritems = AL_CNT_OS_ISDESCS_BA;
161 os_isdescs_idx = OS_ISD_AFTER;
162 } else {
163 init_arritems = AL_CNT_OS_ISDESCS;
164 os_isdescs_idx = OS_ISD_ORDERED;
165 }
166 }
167
168 /*
169 * If we didn't insert a section into the default list using
170 * mapfile specified ordering above, then append the input
171 * section to the appropriate list.
172 */
173 if (do_append && aplist_append(&(osp->os_isdescs[os_isdescs_idx]),
174 isp, init_arritems) == NULL)
175 return (0);
176 isp->is_osdesc = osp;
177
178 /*
179 * A section can be merged if the following are true:
180 * - The SHF_MERGE|SHF_STRINGS flags must be set
181 * - String table compression must not be disabled (-znocompstrtab)
182 * - Mapfile ordering must not have been used.
183 * - The section must not be ordered via section header flags.
184 * - It must not be the generated section being built to
185 * replace the sections on this list.
186 */
187 if (((isp->is_shdr->sh_flags & (SHF_MERGE | SHF_STRINGS)) !=
188 (SHF_MERGE | SHF_STRINGS)) ||
189 ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0) ||
190 !do_append ||
191 ((isp->is_flags & (FLG_IS_ORDERED | FLG_IS_GNSTRMRG)) != 0))
192 return (1);
193
194 /*
195 * Skip sections with (sh_entsize > 1) or (sh_addralign > 1).
196 *
197 * sh_entsize:
198 * We are currently only able to merge string tables containing
199 * strings with 1-byte (char) characters. Support for wide
200 * characters will require our string table compression code
201 * to be extended to handle larger character sizes.
202 *
203 * sh_addralign:
204 * Alignments greater than 1 would require our string table
205 * compression code to insert null bytes to move each
206 * string to the required alignment.
207 */
208 if ((isp->is_shdr->sh_entsize > 1) ||
209 (isp->is_shdr->sh_addralign > 1)) {
210 DBG_CALL(Dbg_sec_unsup_strmerge(ofl->ofl_lml, isp));
211 return (1);
212 }
213
214 if (aplist_append(&osp->os_mstrisdescs, isp,
215 AL_CNT_OS_MSTRISDESCS) == NULL)
216 return (0);
217
218 /*
219 * The SHF_MERGE|SHF_STRINGS flags tell us that the program that
220 * created the section intended it to be mergeable. The
221 * FLG_IS_INSTRMRG flag says that we have done validity testing
222 * and decided that it is safe to act on that hint.
223 */
224 isp->is_flags |= FLG_IS_INSTRMRG;
225
226 return (1);
227 }
228
229 /*
230 * Determine whether this input COMDAT section already exists for the associated
231 * output section. If so, then discard this input section. Otherwise, this
232 * must be the first COMDAT section, thus it is kept for future comparisons.
233 */
234 static uintptr_t
add_comdat(Ofl_desc * ofl,Os_desc * osp,Is_desc * isp)235 add_comdat(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp)
236 {
237 Isd_node isd, *isdp;
238 avl_tree_t *avlt;
239 avl_index_t where;
240
241 /*
242 * Create a COMDAT avl tree for this output section if required.
243 */
244 if ((avlt = osp->os_comdats) == NULL) {
245 if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL)
246 return (S_ERROR);
247 avl_create(avlt, isdavl_compare, sizeof (Isd_node),
248 SGSOFFSETOF(Isd_node, isd_avl));
249 osp->os_comdats = avlt;
250 }
251
252 /*
253 * A standard COMDAT section uses the section name as search key.
254 */
255 isd.isd_name = isp->is_name;
256 isd.isd_hash = sgs_str_hash(isd.isd_name);
257
258 if ((isdp = avl_find(avlt, &isd, &where)) != NULL) {
259 isp->is_osdesc = osp;
260
261 /*
262 * If this section hasn't already been identified as discarded,
263 * generate a suitable diagnostic.
264 */
265 if ((isp->is_flags & FLG_IS_DISCARD) == 0) {
266 isp->is_flags |= FLG_IS_DISCARD;
267 isp->is_comdatkeep = isdp->isd_isp;
268 DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp,
269 isdp->isd_isp));
270 }
271
272 /*
273 * A discarded section does not require assignment to an output
274 * section. However, if relaxed relocations have been enabled
275 * (either from -z relaxreloc, or asserted with .gnu.linkonce
276 * processing), then this section must still be assigned to an
277 * output section so that the sloppy relocation logic will have
278 * the information necessary to do its work.
279 */
280 return (0);
281 }
282
283 /*
284 * This is a new COMDAT section - so keep it.
285 */
286 if ((isdp = libld_calloc(sizeof (Isd_node), 1)) == NULL)
287 return (S_ERROR);
288
289 isdp->isd_name = isd.isd_name;
290 isdp->isd_hash = isd.isd_hash;
291 isdp->isd_isp = isp;
292
293 avl_insert(avlt, isdp, where);
294 return (1);
295 }
296
297 /*
298 * Determine whether a GNU group COMDAT section name follows the convention
299 *
300 * section-name.symbol-name
301 *
302 * Each section within the input file is compared to see if the full section
303 * name matches the beginning of the COMDAT section, with a following '.'.
304 * A pointer to the symbol name, starting with the '.' is returned so that the
305 * caller can strip off the required section name.
306 */
307 static char *
gnu_comdat_sym(Ifl_desc * ifl,Is_desc * gisp)308 gnu_comdat_sym(Ifl_desc *ifl, Is_desc *gisp)
309 {
310 size_t ndx;
311
312 for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
313 Is_desc *isp;
314 size_t ssize;
315
316 if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
317 (isp == gisp) || (isp->is_name == NULL))
318 continue;
319
320 /*
321 * It's questionable whether this size should be cached in the
322 * Is_desc. However, this seems an infrequent operation and
323 * adding Is_desc members can escalate memory usage for large
324 * link-edits. For now, size the section name dynamically.
325 */
326 ssize = strlen(isp->is_name);
327 if ((strncmp(isp->is_name, gisp->is_name, ssize) != 0) &&
328 (gisp->is_name[ssize] == '.'))
329 return ((char *)&gisp->is_name[ssize]);
330 }
331 return (NULL);
332 }
333
334 /*
335 * GNU .gnu.linkonce sections follow a naming convention that indicates the
336 * required association with an output section. Determine whether this input
337 * section follows the convention, and if so return the appropriate output
338 * section name.
339 *
340 * .gnu.linkonce.b.* -> .bss
341 * .gnu.linkonce.d.* -> .data
342 * .gnu.linkonce.l.* -> .ldata
343 * .gnu.linkonce.lb.* -> .lbss
344 * .gnu.linkonce.lr.* -> .lrodata
345 * .gnu.linkonce.r.* -> .rodata
346 * .gnu.linkonce.s.* -> .sdata
347 * .gnu.linkonce.s2.* -> .sdata2
348 * .gnu.linkonce.sb.* -> .sbss
349 * .gnu.linkonce.sb2.* -> .sbss2
350 * .gnu.linkonce.t.* -> .text
351 * .gnu.linkonce.tb.* -> .tbss
352 * .gnu.linkonce.td.* -> .tdata
353 * .gnu.linkonce.wi.* -> .debug_info
354 */
355 #define NSTR_CH1(ch) (*(nstr + 1) == (ch))
356 #define NSTR_CH2(ch) (*(nstr + 2) == (ch))
357 #define NSTR_CH3(ch) (*(nstr + 3) == (ch))
358
359 static const char *
gnu_linkonce_sec(const char * ostr)360 gnu_linkonce_sec(const char *ostr)
361 {
362 const char *nstr = &ostr[MSG_SCN_GNU_LINKONCE_SIZE];
363
364 switch (*nstr) {
365 case 'b':
366 if (NSTR_CH1('.'))
367 return (MSG_ORIG(MSG_SCN_BSS));
368 break;
369 case 'd':
370 if (NSTR_CH1('.'))
371 return (MSG_ORIG(MSG_SCN_DATA));
372 break;
373 case 'l':
374 if (NSTR_CH1('.'))
375 return (MSG_ORIG(MSG_SCN_LDATA));
376 else if (NSTR_CH1('b') && NSTR_CH2('.'))
377 return (MSG_ORIG(MSG_SCN_LBSS));
378 else if (NSTR_CH1('r') && NSTR_CH2('.'))
379 return (MSG_ORIG(MSG_SCN_LRODATA));
380 break;
381 case 'r':
382 if (NSTR_CH1('.'))
383 return (MSG_ORIG(MSG_SCN_RODATA));
384 break;
385 case 's':
386 if (NSTR_CH1('.'))
387 return (MSG_ORIG(MSG_SCN_SDATA));
388 else if (NSTR_CH1('2') && NSTR_CH2('.'))
389 return (MSG_ORIG(MSG_SCN_SDATA2));
390 else if (NSTR_CH1('b') && NSTR_CH2('.'))
391 return (MSG_ORIG(MSG_SCN_SBSS));
392 else if (NSTR_CH1('b') && NSTR_CH2('2') && NSTR_CH3('.'))
393 return (MSG_ORIG(MSG_SCN_SBSS2));
394 break;
395 case 't':
396 if (NSTR_CH1('.'))
397 return (MSG_ORIG(MSG_SCN_TEXT));
398 else if (NSTR_CH1('b') && NSTR_CH2('.'))
399 return (MSG_ORIG(MSG_SCN_TBSS));
400 else if (NSTR_CH1('d') && NSTR_CH2('.'))
401 return (MSG_ORIG(MSG_SCN_TDATA));
402 break;
403 case 'w':
404 if (NSTR_CH1('i') && NSTR_CH2('.'))
405 return (MSG_ORIG(MSG_SCN_DEBUG_INFO));
406 break;
407 default:
408 break;
409 }
410
411 /*
412 * No special name match found.
413 */
414 return (ostr);
415 }
416 #undef NSTR_CH1
417 #undef NSTR_CH2
418 #undef NSTR_CH3
419
420
421 /*
422 * Initialize a path info buffer for use with ld_place_section().
423 *
424 * entry:
425 * ofl - Output descriptor
426 * ifl - Descriptor for input file, or NULL if there is none.
427 * info - Address of buffer to be initialized.
428 *
429 * exit:
430 * If this is an input file, and if the entrance criteria list
431 * contains at least one criteria that has a non-empty file string
432 * match list (ec_files), then the block pointed at by info is
433 * initialized, and info is returned.
434 *
435 * If there is no input file, and/or no entrance criteria containing
436 * a non-empty ec_files list, then NULL is returned. This is not
437 * an error --- the NULL is simply an optimization, understood by
438 * ld_place_path(), that allows it to skip unnecessary work.
439 */
440 Place_path_info *
ld_place_path_info_init(Ofl_desc * ofl,Ifl_desc * ifl,Place_path_info * info)441 ld_place_path_info_init(Ofl_desc *ofl, Ifl_desc *ifl, Place_path_info *info)
442 {
443 /*
444 * Return NULL if there is no input file (internally generated section)
445 * or if the entrance criteria list does not contain any items that will
446 * need to be compared to the path (all the ec_files lists are empty).
447 */
448 if ((ifl == NULL) || !(ofl->ofl_flags & FLG_OF_EC_FILES))
449 return (NULL);
450
451 info->ppi_path = ifl->ifl_name;
452 info->ppi_path_len = strlen(info->ppi_path);
453 info->ppi_isar = (ifl->ifl_flags & FLG_IF_EXTRACT) != 0;
454
455 /*
456 * The basename is the final segment of the path, equivalent to
457 * the path itself if there are no '/' delimiters.
458 */
459 info->ppi_bname = strrchr(info->ppi_path, '/');
460 if (info->ppi_bname == NULL)
461 info->ppi_bname = info->ppi_path;
462 else
463 info->ppi_bname++; /* Skip leading '/' */
464 info->ppi_bname_len =
465 info->ppi_path_len - (info->ppi_bname - info->ppi_path);
466
467 /*
468 * For an archive, the object name is the member name, which is
469 * enclosed in () at the end of the name string. Otherwise, it is
470 * the same as the basename.
471 */
472 if (info->ppi_isar) {
473 info->ppi_oname = strrchr(info->ppi_bname, '(');
474 /* There must be an archive member suffix delimited by parens */
475 assert((info->ppi_bname[info->ppi_bname_len - 1] == ')') &&
476 (info->ppi_oname != NULL));
477 info->ppi_oname++; /* skip leading '(' */
478 info->ppi_oname_len = info->ppi_bname_len -
479 (info->ppi_oname - info->ppi_bname + 1);
480 } else {
481 info->ppi_oname = info->ppi_bname;
482 info->ppi_oname_len = info->ppi_bname_len;
483 }
484
485 return (info);
486 }
487
488 /*
489 * Compare an input section path to the file comparison list the given
490 * entrance criteria.
491 *
492 * entry:
493 * path_info - A non-NULL Place_path_info block for the file
494 * containing the input section, initialized by
495 * ld_place_path_info_init()
496 * enp - Entrance criteria with a non-empty ec_files list of file
497 * comparisons to be carried out.
498 *
499 * exit:
500 * Return TRUE if a match is seen, and FALSE otherwise.
501 */
502 static Boolean
eval_ec_files(Place_path_info * path_info,Ent_desc * enp)503 eval_ec_files(Place_path_info *path_info, Ent_desc *enp)
504 {
505 Aliste idx;
506 Ent_desc_file *edfp;
507 size_t cmp_len;
508 const char *cmp_str;
509
510 for (ALIST_TRAVERSE(enp->ec_files, idx, edfp)) {
511 Word type = edfp->edf_flags & TYP_ECF_MASK;
512
513 /*
514 * Determine the starting character, and # of characters,
515 * from the file path to compare against this entrance criteria
516 * file string.
517 */
518 if (type == TYP_ECF_OBJNAME) {
519 cmp_str = path_info->ppi_oname;
520 cmp_len = path_info->ppi_oname_len;
521 } else {
522 int ar_stat_diff = path_info->ppi_isar !=
523 ((edfp->edf_flags & FLG_ECF_ARMEMBER) != 0);
524
525 /*
526 * If the entrance criteria specifies an archive member
527 * and the file does not, then there can be no match.
528 */
529
530 if (ar_stat_diff && !path_info->ppi_isar)
531 continue;
532
533 if (type == TYP_ECF_PATH) {
534 cmp_str = path_info->ppi_path;
535 cmp_len = path_info->ppi_path_len;
536 } else { /* TYP_ECF_BASENAME */
537 cmp_str = path_info->ppi_bname;
538 cmp_len = path_info->ppi_bname_len;
539 }
540
541 /*
542 * If the entrance criteria does not specify an archive
543 * member and the file does, then a match just requires
544 * the paths (without the archive member) to match.
545 * Reduce the length to not include the ar member or
546 * the '(' that precedes it.
547 */
548 if (ar_stat_diff && path_info->ppi_isar)
549 cmp_len = path_info->ppi_oname - cmp_str - 1;
550 }
551
552 /*
553 * Compare the resulting string to the one from the
554 * entrance criteria.
555 */
556 if ((cmp_len == edfp->edf_name_len) &&
557 (strncmp(edfp->edf_name, cmp_str, cmp_len) == 0))
558 return (TRUE);
559 }
560
561 return (FALSE);
562 }
563
564 /*
565 * Replace the section header for the given input section with a new section
566 * header of the specified type. All values in the replacement header other
567 * than the type retain their previous values.
568 *
569 * entry:
570 * isp - Input section to replace
571 * sh_type - New section type to apply
572 *
573 * exit:
574 * Returns the pointer to the new section header on success, and
575 * NULL for failure.
576 */
577 static Shdr *
isp_convert_type(Is_desc * isp,Word sh_type)578 isp_convert_type(Is_desc *isp, Word sh_type)
579 {
580 Shdr *shdr;
581
582 if ((shdr = libld_malloc(sizeof (Shdr))) == NULL)
583 return (NULL);
584 *shdr = *isp->is_shdr;
585 isp->is_shdr = shdr;
586 shdr->sh_type = sh_type;
587 return (shdr);
588 }
589
590 /*
591 * Issue a fatal warning for the given .eh_frame section, which
592 * cannot be merged with the existing .eh_frame output section.
593 */
594 static void
eh_frame_muldef(Ofl_desc * ofl,Is_desc * isp)595 eh_frame_muldef(Ofl_desc *ofl, Is_desc *isp)
596 {
597 Sg_desc *sgp;
598 Is_desc *isp1;
599 Os_desc *osp;
600 Aliste idx1, idx2, idx3;
601
602 /*
603 * Locate the .eh_frame output section, and use the first section
604 * assigned to it in the error message. The user can then compare
605 * the two sections to determine what attribute prevented the merge.
606 */
607 for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
608 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
609 if ((osp->os_flags & FLG_OS_EHFRAME) == 0)
610 continue;
611
612 for (idx3 = 0; idx3 < OS_ISD_NUM; idx3++) {
613 APlist *lst = osp->os_isdescs[idx3];
614
615 if (aplist_nitems(lst) == 0)
616 continue;
617
618 isp1 = lst->apl_data[0];
619 ld_eprintf(ofl, ERR_FATAL,
620 MSG_INTL(MSG_UPD_MULEHFRAME),
621 isp1->is_file->ifl_name,
622 EC_WORD(isp1->is_scnndx), isp1->is_name,
623 isp->is_file->ifl_name,
624 EC_WORD(isp->is_scnndx), isp->is_name);
625 return;
626 }
627 }
628 }
629 }
630
631 /*
632 * Place a section into the appropriate segment and output section.
633 *
634 * entry:
635 * ofl - File descriptor
636 * isp - Input section descriptor of section to be placed.
637 * path_info - NULL, or pointer to Place_path_info buffer initialized
638 * by ld_place_path_info_init() for the file associated to isp,
639 * for use in processing entrance criteria with non-empty
640 * file matching string list (ec_files)
641 * ident - Section identifier, used to order sections relative to
642 * others within the output segment.
643 * alt_os_name - If non-NULL, the name of the output section to place
644 * isp into. If NULL, input sections go to an output section
645 * with the same name as the input section.
646 */
647 Os_desc *
ld_place_section(Ofl_desc * ofl,Is_desc * isp,Place_path_info * path_info,int ident,const char * alt_os_name)648 ld_place_section(Ofl_desc *ofl, Is_desc *isp, Place_path_info *path_info,
649 int ident, const char *alt_os_name)
650 {
651 Ent_desc *enp;
652 Sg_desc *sgp;
653 Os_desc *osp;
654 Aliste idx1, iidx;
655 int os_ndx;
656 Shdr *shdr = isp->is_shdr;
657 Xword shflagmask, shflags = shdr->sh_flags;
658 Ifl_desc *ifl = isp->is_file;
659 char *oname, *sname;
660 uint_t onamehash;
661 Boolean is_ehframe = (isp->is_flags & FLG_IS_EHFRAME) != 0;
662
663 /*
664 * Define any sections that must be thought of as referenced. These
665 * sections may not be referenced externally in a manner ld(1) can
666 * discover, but they must be retained (ie. not removed by -zignore).
667 */
668 static const Msg RefSecs[] = {
669 MSG_SCN_INIT, /* MSG_ORIG(MSG_SCN_INIT) */
670 MSG_SCN_FINI, /* MSG_ORIG(MSG_SCN_FINI) */
671 MSG_SCN_EX_RANGES, /* MSG_ORIG(MSG_SCN_EX_RANGES) */
672 MSG_SCN_EX_SHARED, /* MSG_ORIG(MSG_SCN_EX_SHARED) */
673 MSG_SCN_CTORS, /* MSG_ORIG(MSG_SCN_CTORS) */
674 MSG_SCN_DTORS, /* MSG_ORIG(MSG_SCN_DTORS) */
675 MSG_SCN_EHFRAME, /* MSG_ORIG(MSG_SCN_EHFRAME) */
676 MSG_SCN_EHFRAME_HDR, /* MSG_ORIG(MSG_SCN_EHFRAME_HDR) */
677 MSG_SCN_JCR, /* MSG_ORIG(MSG_SCN_JCR) */
678 0
679 };
680
681 DBG_CALL(Dbg_sec_in(ofl->ofl_lml, isp));
682
683 /*
684 * If this section identifies group members, or this section indicates
685 * that it is a member of a group, determine whether the section is
686 * still required.
687 */
688 if ((shflags & SHF_GROUP) || (shdr->sh_type == SHT_GROUP)) {
689 Group_desc *gdesc;
690
691 if ((gdesc = ld_get_group(ofl, isp)) != NULL) {
692 DBG_CALL(Dbg_sec_group(ofl->ofl_lml, isp, gdesc));
693
694 /*
695 * If this group has been replaced by another group,
696 * then this section needs to be discarded.
697 */
698 if (gdesc->gd_oisc) {
699 isp->is_flags |= FLG_IS_DISCARD;
700
701 /*
702 * Since we're discarding the section, we
703 * can skip assigning it to an output section.
704 * The exception is that if the user
705 * specifies -z relaxreloc, then
706 * we need to assign the output section so
707 * that the sloppy relocation logic will have
708 * the information necessary to do its work.
709 */
710 if (!(ofl->ofl_flags1 & FLG_OF1_RLXREL))
711 return (NULL);
712 }
713 }
714
715 /*
716 * SHT_GROUP sections can only be included into relocatable
717 * objects.
718 */
719 if (shdr->sh_type == SHT_GROUP) {
720 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) {
721 isp->is_flags |= FLG_IS_DISCARD;
722 return (NULL);
723 }
724 }
725 }
726
727 /*
728 * Always assign SHF_TLS sections to the DATA segment (and then the
729 * PT_TLS embedded inside of there).
730 */
731 if (shflags & SHF_TLS)
732 shflags |= SHF_WRITE;
733
734 /*
735 * Traverse the entrance criteria list searching for a segment that
736 * matches the input section we have. If an entrance criterion is set
737 * then there must be an exact match. If we complete the loop without
738 * finding a segment, then sgp will be NULL.
739 */
740 sgp = NULL;
741 for (APLIST_TRAVERSE(ofl->ofl_ents, idx1, enp)) {
742
743 /* Disabled segments are not available for assignment */
744 if (enp->ec_segment->sg_flags & FLG_SG_DISABLED)
745 continue;
746
747 /*
748 * If an entrance criteria doesn't have any of its fields
749 * set, it will match any section it is tested against.
750 * We set the FLG_EC_CATCHALL flag on these, primarily because
751 * it helps readers of our debug output to understand what
752 * the criteria means --- otherwise the user would just see
753 * that every field is 0, but might not understand the
754 * significance of that.
755 *
756 * Given that we set this flag, we can use it here as an
757 * optimization to short circuit all of the tests in this
758 * loop. Note however, that if we did not do this, the end
759 * result would be the same --- the empty criteria will sail
760 * past the following tests and reach the end of the loop.
761 */
762 if (enp->ec_flags & FLG_EC_CATCHALL) {
763 sgp = enp->ec_segment;
764 break;
765 }
766
767 if (enp->ec_type && (enp->ec_type != shdr->sh_type))
768 continue;
769 if (enp->ec_attrmask &&
770 /* LINTED */
771 (enp->ec_attrmask & enp->ec_attrbits) !=
772 (enp->ec_attrmask & shflags))
773 continue;
774 if (enp->ec_is_name &&
775 (strcmp(enp->ec_is_name, isp->is_name) != 0))
776 continue;
777
778 if ((alist_nitems(enp->ec_files) > 0) &&
779 ((path_info == NULL) || !eval_ec_files(path_info, enp)))
780 continue;
781
782 /* All entrance criteria tests passed */
783 sgp = enp->ec_segment;
784 break;
785 }
786
787 /*
788 * The final entrance criteria record is a FLG_EC_CATCHALL that points
789 * at the final predefined segment "extra", and this final segment is
790 * tagged FLG_SG_NODISABLE. Therefore, the above loop must always find
791 * a segment.
792 */
793 assert(sgp != NULL);
794
795 /*
796 * Transfer the input section sorting key from the entrance criteria
797 * to the input section. A non-zero value means that the section
798 * will be sorted on this key amoung the other sections that have a
799 * non-zero key. These sorted sections are collectively placed at the
800 * head of the output section.
801 *
802 * If the sort key is 0, the section is placed after the sorted
803 * sections in the order they are encountered.
804 */
805 isp->is_ordndx = enp->ec_ordndx;
806
807 /* Remember that this entrance criteria has placed a section */
808 enp->ec_flags |= FLG_EC_USED;
809
810 /*
811 * If our caller has supplied an alternative name for the output
812 * section, then we defer to their request. Otherwise, the default
813 * is to use the same name as that of the input section being placed.
814 *
815 * The COMDAT, SHT_GROUP and GNU name translations that follow have
816 * the potential to alter this initial name.
817 */
818 oname = (char *)((alt_os_name == NULL) ? isp->is_name : alt_os_name);
819
820 /*
821 * Solaris section names may follow the convention:
822 *
823 * section-name%symbol-name
824 *
825 * This convention has been used to order the layout of sections within
826 * segments for objects built with the compilers -xF option. However,
827 * the final object should not contain individual section headers for
828 * all such input sections, instead the symbol name is stripped from the
829 * name to establish the final output section name.
830 *
831 * This convention has also been followed for COMDAT and sections
832 * identified though SHT_GROUP data.
833 *
834 * Strip out the % from the section name for:
835 * - Non-relocatable objects
836 * - Relocatable objects if input section sorting is
837 * in force for the segment in question.
838 */
839 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) ||
840 (sgp->sg_flags & FLG_SG_IS_ORDER)) {
841 if ((sname = strchr(isp->is_name, '%')) != NULL) {
842 size_t size = sname - isp->is_name;
843
844 if ((oname = libld_malloc(size + 1)) == NULL)
845 return ((Os_desc *)S_ERROR);
846 (void) strncpy(oname, isp->is_name, size);
847 oname[size] = '\0';
848 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname));
849 }
850 }
851
852 /*
853 * GNU section names may follow the convention:
854 *
855 * .gnu.linkonce.*
856 *
857 * The .gnu.linkonce is a section naming convention that indicates a
858 * COMDAT requirement. Determine whether this section follows the GNU
859 * pattern, and if so, determine whether this section should be
860 * discarded or retained. The comparison of is_name[1] with 'g'
861 * is an optimization to skip using strncmp() too much. This is safe,
862 * because we know the name is not NULL, and therefore must have
863 * at least one character plus a NULL termination.
864 */
865 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
866 (isp->is_name == oname) && (isp->is_name[1] == 'g') &&
867 (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE), isp->is_name,
868 MSG_SCN_GNU_LINKONCE_SIZE) == 0)) {
869 if ((oname =
870 (char *)gnu_linkonce_sec(isp->is_name)) != isp->is_name) {
871 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname));
872 }
873
874 /*
875 * Explicitly identify this section type as COMDAT. Also,
876 * enable relaxed relocation processing, as this is typically
877 * a requirement with .gnu.linkonce sections.
878 */
879 isp->is_flags |= FLG_IS_COMDAT;
880 if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0)
881 ofl->ofl_flags1 |= FLG_OF1_RLXREL;
882 DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, isp, TRUE,
883 (ofl->ofl_flags1 & FLG_OF1_RLXREL) != 0));
884 }
885
886 /*
887 * GNU section names may also follow the convention:
888 *
889 * section-name.symbol-name
890 *
891 * This convention is used when defining SHT_GROUP sections of type
892 * COMDAT. Thus, any group processing will have discovered any group
893 * sections, and this identification can be triggered by a pattern
894 * match section names.
895 */
896 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
897 (isp->is_name == oname) && (isp->is_flags & FLG_IS_COMDAT) &&
898 ((sname = gnu_comdat_sym(ifl, isp)) != NULL)) {
899 size_t size = sname - isp->is_name;
900
901 if ((oname = libld_malloc(size + 1)) == NULL)
902 return ((Os_desc *)S_ERROR);
903 (void) strncpy(oname, isp->is_name, size);
904 oname[size] = '\0';
905 DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname));
906
907 /*
908 * Enable relaxed relocation processing, as this is
909 * typically a requirement with GNU COMDAT sections.
910 */
911 if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0) {
912 ofl->ofl_flags1 |= FLG_OF1_RLXREL;
913 DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, isp,
914 FALSE, TRUE));
915 }
916 }
917
918 /*
919 * Assign a hash value now that the output section name has been
920 * finalized.
921 */
922 onamehash = sgs_str_hash(oname);
923
924 /*
925 * Determine if output section ordering is turned on. If so, return
926 * the appropriate ordering index for the section. This information
927 * is derived from the Sg_desc->sg_os_order list that was built
928 * up from the Mapfile.
929 *
930 * A value of 0 for os_ndx means that the section is not sorted
931 * (i.e. is not found in the sg_os_order). The items in sg_os_order
932 * are in the desired sort order, so adding 1 to their alist index
933 * gives a suitable index for sorting.
934 */
935 os_ndx = 0;
936 if (alist_nitems(sgp->sg_os_order) > 0) {
937 Sec_order *scop;
938
939 for (ALIST_TRAVERSE(sgp->sg_os_order, idx1, scop)) {
940 if (strcmp(scop->sco_secname, oname) == 0) {
941 scop->sco_flags |= FLG_SGO_USED;
942 os_ndx = idx1 + 1;
943 break;
944 }
945 }
946 }
947
948 /*
949 * Mask of section header flags to ignore when matching sections. We
950 * are more strict with relocatable objects, ignoring only the order
951 * flags, and keeping sections apart if they differ otherwise. This
952 * follows the policy that sections in a relative object should only
953 * be merged if their flags are the same, and avoids destroying
954 * information prematurely. For final products however, we ignore all
955 * flags that do not prevent a merge.
956 */
957 shflagmask =
958 (ofl->ofl_flags & FLG_OF_RELOBJ) ? ALL_SHF_ORDER : ALL_SHF_IGNORE;
959
960 /*
961 * Traverse the input section list for the output section we have been
962 * assigned. If we find a matching section simply add this new section.
963 */
964 iidx = 0;
965 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx1, osp)) {
966 Shdr *os_shdr = osp->os_shdr;
967
968 /*
969 * An input section matches an output section if:
970 * - The ident values match
971 * - The names match
972 * - Not a GROUP section
973 * - Not a DTrace dof section
974 * - Section types match
975 * - Matching section flags, after screening out the
976 * shflagmask flags.
977 *
978 * Section types are considered to match if any one of
979 * the following are true:
980 * - The type codes are the same
981 * - The input section is COMDAT, and the output section
982 * is SHT_PROGBITS.
983 */
984 if ((ident == osp->os_identndx) &&
985 (ident != ld_targ.t_id.id_rel) &&
986 (onamehash == osp->os_namehash) &&
987 (shdr->sh_type != SHT_GROUP) &&
988 (shdr->sh_type != SHT_SUNW_dof) &&
989 ((shdr->sh_type == os_shdr->sh_type) ||
990 ((shdr->sh_type == SHT_SUNW_COMDAT) &&
991 (os_shdr->sh_type == SHT_PROGBITS))) &&
992 ((shflags & ~shflagmask) ==
993 (os_shdr->sh_flags & ~shflagmask)) &&
994 (strcmp(oname, osp->os_name) == 0)) {
995 uintptr_t err;
996
997 /*
998 * Process any COMDAT section, keeping the first and
999 * discarding all others.
1000 */
1001 if ((isp->is_flags & FLG_IS_COMDAT) &&
1002 ((err = add_comdat(ofl, osp, isp)) != 1))
1003 return ((Os_desc *)err);
1004
1005 /*
1006 * Set alignment
1007 */
1008 set_addralign(ofl, osp, isp);
1009
1010 /*
1011 * If this section is a non-empty TLS section indicate
1012 * that a PT_TLS program header is required.
1013 */
1014 if ((shflags & SHF_TLS) && shdr->sh_size &&
1015 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0))
1016 ofl->ofl_flags |= FLG_OF_TLSPHDR;
1017
1018 /*
1019 * Insert the input section descriptor on the proper
1020 * output section descriptor list.
1021 *
1022 * If this segment requires input section ordering,
1023 * honor any mapfile specified ordering for otherwise
1024 * unordered sections by setting the mapfile_sort
1025 * argument of os_attach_isp() to True.
1026 */
1027
1028 if (os_attach_isp(ofl, osp, isp,
1029 (sgp->sg_flags & FLG_SG_IS_ORDER) != 0) == 0)
1030 return ((Os_desc *)S_ERROR);
1031
1032 /*
1033 * If this input section and file is associated to an
1034 * artificially referenced output section, make sure
1035 * they are marked as referenced also. This ensures
1036 * that this input section and file isn't eliminated
1037 * when -zignore is in effect.
1038 *
1039 * See -zignore comments when creating a new output
1040 * section below.
1041 */
1042 if (((ifl &&
1043 (ifl->ifl_flags & FLG_IF_IGNORE)) || DBG_ENABLED) &&
1044 (osp->os_flags & FLG_OS_SECTREF)) {
1045 isp->is_flags |= FLG_IS_SECTREF;
1046 if (ifl)
1047 ifl->ifl_flags |= FLG_IF_FILEREF;
1048 }
1049
1050 DBG_CALL(Dbg_sec_added(ofl->ofl_lml, osp, sgp));
1051 return (osp);
1052 }
1053
1054 /*
1055 * Do we need to worry about section ordering?
1056 */
1057 if (os_ndx) {
1058 if (osp->os_ordndx) {
1059 if (os_ndx < osp->os_ordndx)
1060 /* insert section here. */
1061 break;
1062 else {
1063 iidx = idx1 + 1;
1064 continue;
1065 }
1066 } else {
1067 /* insert section here. */
1068 break;
1069 }
1070 } else if (osp->os_ordndx) {
1071 iidx = idx1 + 1;
1072 continue;
1073 }
1074
1075 /*
1076 * If the new sections identifier is less than that of the
1077 * present input section we need to insert the new section
1078 * at this point.
1079 */
1080 if (ident < osp->os_identndx)
1081 break;
1082
1083 iidx = idx1 + 1;
1084 }
1085
1086 /*
1087 * We are adding a new output section. Update the section header
1088 * count and associated string size.
1089 *
1090 * If the input section triggering this output section has been marked
1091 * for discard, and if no other non-discarded input section comes along
1092 * to join it, then we will over count. We cannot know if this will
1093 * happen or not until all input is seen. Set FLG_OF_AJDOSCNT to
1094 * trigger a final count readjustment.
1095 */
1096 if (isp->is_flags & FLG_IS_DISCARD)
1097 ofl->ofl_flags |= FLG_OF_ADJOSCNT;
1098 ofl->ofl_shdrcnt++;
1099 if (st_insert(ofl->ofl_shdrsttab, oname) == -1)
1100 return ((Os_desc *)S_ERROR);
1101
1102 /*
1103 * Create a new output section descriptor.
1104 */
1105 if ((osp = libld_calloc(sizeof (Os_desc), 1)) == NULL)
1106 return ((Os_desc *)S_ERROR);
1107 if ((osp->os_shdr = libld_calloc(sizeof (Shdr), 1)) == NULL)
1108 return ((Os_desc *)S_ERROR);
1109
1110 /*
1111 * Convert COMDAT section to PROGBITS as this the first section of the
1112 * output section. Save any COMDAT section for later processing, as
1113 * additional COMDAT sections that match this section need discarding.
1114 */
1115 if ((shdr->sh_type == SHT_SUNW_COMDAT) &&
1116 ((shdr = isp_convert_type(isp, SHT_PROGBITS)) == NULL))
1117 return ((Os_desc *)S_ERROR);
1118 if ((isp->is_flags & FLG_IS_COMDAT) &&
1119 (add_comdat(ofl, osp, isp) == S_ERROR))
1120 return ((Os_desc *)S_ERROR);
1121
1122 if (is_ehframe) {
1123 /*
1124 * Executable or sharable objects can have at most a single
1125 * .eh_frame section. Detect attempts to create more than
1126 * one. This occurs if the input sections have incompatible
1127 * attributes.
1128 */
1129 if ((ofl->ofl_flags & FLG_OF_EHFRAME) &&
1130 !(ofl->ofl_flags & FLG_OF_RELOBJ)) {
1131 eh_frame_muldef(ofl, isp);
1132 return ((Os_desc *)S_ERROR);
1133 }
1134 ofl->ofl_flags |= FLG_OF_EHFRAME;
1135 }
1136
1137 osp->os_shdr->sh_type = shdr->sh_type;
1138 osp->os_shdr->sh_flags = shdr->sh_flags;
1139 osp->os_shdr->sh_entsize = shdr->sh_entsize;
1140 osp->os_name = oname;
1141 osp->os_namehash = onamehash;
1142 osp->os_ordndx = os_ndx;
1143 osp->os_sgdesc = sgp;
1144 if (is_ehframe)
1145 osp->os_flags |= FLG_OS_EHFRAME;
1146
1147 if (ifl && (shdr->sh_type == SHT_PROGBITS)) {
1148 /*
1149 * Try to preserve the intended meaning of sh_link/sh_info.
1150 * See the translate_link() in update.c.
1151 */
1152 osp->os_shdr->sh_link = shdr->sh_link;
1153 if (shdr->sh_flags & SHF_INFO_LINK)
1154 osp->os_shdr->sh_info = shdr->sh_info;
1155 }
1156
1157 /*
1158 * When -zignore is in effect, user supplied sections and files that are
1159 * not referenced from other sections, are eliminated from the object
1160 * being produced. Some sections, although unreferenced, are special,
1161 * and must not be eliminated. Determine if this new output section is
1162 * one of those special sections, and if so mark it artificially as
1163 * referenced. Any input section and file associated to this output
1164 * section is also be marked as referenced, and thus won't be eliminated
1165 * from the final output.
1166 */
1167 if (ifl && ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) || DBG_ENABLED)) {
1168 const Msg *refsec;
1169
1170 for (refsec = RefSecs; *refsec; refsec++) {
1171 if (strcmp(osp->os_name, MSG_ORIG(*refsec)) == 0) {
1172 osp->os_flags |= FLG_OS_SECTREF;
1173
1174 if ((ifl->ifl_flags & FLG_IF_IGNORE) ||
1175 DBG_ENABLED) {
1176 isp->is_flags |= FLG_IS_SECTREF;
1177 ifl->ifl_flags |= FLG_IF_FILEREF;
1178 }
1179 break;
1180 }
1181 }
1182 }
1183
1184 /*
1185 * Sections of type SHT_GROUP are added to the ofl->ofl_osgroups list,
1186 * so that they can be updated as a group later.
1187 */
1188 if ((shdr->sh_type == SHT_GROUP) &&
1189 (aplist_append(&ofl->ofl_osgroups, osp,
1190 AL_CNT_OFL_OSGROUPS) == NULL))
1191 return ((Os_desc *)S_ERROR);
1192
1193 /*
1194 * If this section is a non-empty TLS section indicate that a PT_TLS
1195 * program header is required.
1196 */
1197 if ((shflags & SHF_TLS) && shdr->sh_size &&
1198 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0))
1199 ofl->ofl_flags |= FLG_OF_TLSPHDR;
1200
1201 /*
1202 * If a non-allocatable section is going to be put into a loadable
1203 * segment then turn on the allocate bit for this section and warn the
1204 * user that we have done so. This could only happen through the use
1205 * of a mapfile.
1206 */
1207 if ((sgp->sg_phdr.p_type == PT_LOAD) &&
1208 ((osp->os_shdr->sh_flags & SHF_ALLOC) == 0)) {
1209 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SCN_NONALLOC),
1210 ofl->ofl_name, osp->os_name, sgp->sg_name);
1211 osp->os_shdr->sh_flags |= SHF_ALLOC;
1212 }
1213
1214 /*
1215 * Retain this sections identifier for future comparisons when placing
1216 * a section (after all sections have been processed this variable will
1217 * be used to hold the sections symbol index as we don't need to retain
1218 * the identifier any more).
1219 */
1220 osp->os_identndx = ident;
1221
1222 /*
1223 * Set alignment.
1224 */
1225 set_addralign(ofl, osp, isp);
1226
1227 if (os_attach_isp(ofl, osp, isp, 0) == 0)
1228 return ((Os_desc *)S_ERROR);
1229
1230 DBG_CALL(Dbg_sec_created(ofl->ofl_lml, osp, sgp));
1231
1232 /*
1233 * Insert the new section at the offset given by iidx. If no position
1234 * for it was identified above, this will be index 0, causing the new
1235 * section to be prepended to the beginning of the section list.
1236 * Otherwise, it is the index following the section that was identified.
1237 */
1238 if (aplist_insert(&sgp->sg_osdescs, osp, AL_CNT_SG_OSDESC,
1239 iidx) == NULL)
1240 return ((Os_desc *)S_ERROR);
1241 return (osp);
1242 }
1243