xref: /titanic_41/usr/src/cmd/sgs/libld/common/place.c (revision 1f0f5e3e328e41529296f756090856aa7f239b1c)
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 2009 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 /*
31  * Map file parsing and input section to output segment mapping.
32  */
33 #include	<stdio.h>
34 #include	<string.h>
35 #include	<debug.h>
36 #include	"msg.h"
37 #include	"_libld.h"
38 
39 /*
40  * Each time a section is placed, the function set_addralign()
41  * is called.  This function performs:
42  *
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.
48  */
49 static void
50 set_addralign(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp)
51 {
52 	Shdr	*shdr = isp->is_shdr;
53 
54 	/* A discarded section has no influence on the output */
55 	if (isp->is_flags & FLG_IS_DISCARD)
56 		return;
57 
58 	/*
59 	 * If this section has data or will be assigned data
60 	 * later, mark this segment not-empty.
61 	 */
62 	if ((shdr->sh_size != 0) ||
63 	    ((isp->is_flags & FLG_IS_EXTERNAL) == 0))
64 		osp->os_sgdesc->sg_flags |= FLG_SG_PHREQ;
65 
66 	if ((ofl->ofl_dtflags_1 & DF_1_NOHDR) &&
67 	    (osp->os_sgdesc->sg_phdr).p_type != PT_LOAD)
68 		return;
69 
70 	osp->os_sgdesc->sg_addralign =
71 	    ld_lcm(osp->os_sgdesc->sg_addralign, shdr->sh_addralign);
72 }
73 
74 /*
75  * Return the first input descriptor for a given output descriptor,
76  * or NULL if there are none.
77  */
78 
79 Is_desc *
80 ld_os_first_isdesc(Os_desc *osp)
81 {
82 	int i;
83 
84 	for (i = 0; i < OS_ISD_NUM; i++) {
85 		APlist *ap_isdesc = osp->os_isdescs[i];
86 
87 		if (aplist_nitems(ap_isdesc) > 0)
88 			return ((Is_desc *)ap_isdesc->apl_data[0]);
89 	}
90 
91 	return (NULL);
92 }
93 
94 /*
95  * Attach an input section to an output section
96  *
97  * entry:
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.
103  *
104  * exit:
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).
109  *
110  *	On success, returns True (1). On failure, False (0).
111  */
112 static int
113 os_attach_isp(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp, int mapfile_sort)
114 {
115 	Aliste	init_arritems;
116 	int	os_isdescs_idx, do_append = 1;
117 
118 	if ((isp->is_flags & FLG_IS_ORDERED) == 0) {
119 		init_arritems = AL_CNT_OS_ISDESCS;
120 		os_isdescs_idx = OS_ISD_DEFAULT;
121 
122 		/*
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).
128 		 *
129 		 * If no mapfile ordering was specified, we append it in
130 		 * the usual way below.
131 		 */
132 		if (mapfile_sort && (isp->is_ordndx > 0)) {
133 			APlist *ap_isdesc = osp->os_isdescs[OS_ISD_DEFAULT];
134 			Aliste	idx2;
135 			Is_desc	*isp2;
136 
137 			for (APLIST_TRAVERSE(ap_isdesc, idx2, isp2)) {
138 				if (isp2->is_ordndx &&
139 				    (isp2->is_ordndx <= isp->is_ordndx))
140 						continue;
141 
142 				if (aplist_insert(
143 				    &osp->os_isdescs[OS_ISD_DEFAULT],
144 				    isp, init_arritems, idx2) == NULL)
145 					return (0);
146 				do_append = 0;
147 				break;
148 			}
149 		}
150 	} else {		/* Ordered section (via shdr flags) */
151 		Word shndx;
152 
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;
156 
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;
163 		} else {
164 			init_arritems = AL_CNT_OS_ISDESCS;
165 			os_isdescs_idx = OS_ISD_ORDERED;
166 		}
167 	}
168 
169 	/*
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.
173 	 */
174 	if (do_append && aplist_append(&(osp->os_isdescs[os_isdescs_idx]),
175 	    isp, init_arritems) == NULL)
176 		return (0);
177 	isp->is_osdesc = osp;
178 
179 	/*
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.
187 	 */
188 	if (((isp->is_shdr->sh_flags & (SHF_MERGE | SHF_STRINGS)) !=
189 	    (SHF_MERGE | SHF_STRINGS)) ||
190 	    ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0) ||
191 	    !do_append ||
192 	    ((isp->is_flags & (FLG_IS_ORDERED | FLG_IS_GNSTRMRG)) != 0))
193 		return (1);
194 
195 	/*
196 	 * Skip sections with (sh_entsize > 1) or (sh_addralign > 1).
197 	 *
198 	 * sh_entsize:
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.
203 	 *
204 	 * sh_addralign:
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.
208 	 */
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));
212 		return (1);
213 	}
214 
215 	if (aplist_append(&osp->os_mstrisdescs, isp,
216 	    AL_CNT_OS_MSTRISDESCS) == NULL)
217 		return (0);
218 
219 	/*
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.
224 	 */
225 	isp->is_flags |= FLG_IS_INSTRMRG;
226 
227 	return (1);
228 }
229 
230 /*
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.
234  */
235 static uintptr_t
236 add_comdat(Ofl_desc *ofl, Os_desc *osp, Is_desc *isp)
237 {
238 	Isd_node	isd, *isdp;
239 	avl_tree_t	*avlt;
240 	avl_index_t	where;
241 
242 	/*
243 	 * Create a COMDAT avl tree for this output section if required.
244 	 */
245 	if ((avlt = osp->os_comdats) == NULL) {
246 		if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL)
247 			return (S_ERROR);
248 		avl_create(avlt, isdavl_compare, sizeof (Isd_node),
249 		    SGSOFFSETOF(Isd_node, isd_avl));
250 		osp->os_comdats = avlt;
251 	}
252 
253 	/*
254 	 * A standard COMDAT section uses the section name as search key.
255 	 */
256 	isd.isd_name = isp->is_name;
257 	isd.isd_hash = sgs_str_hash(isd.isd_name);
258 
259 	if ((isdp = avl_find(avlt, &isd, &where)) != NULL) {
260 		isp->is_osdesc = osp;
261 
262 		/*
263 		 * If this section hasn't already been identified as discarded,
264 		 * generate a suitable diagnostic.
265 		 */
266 		if ((isp->is_flags & FLG_IS_DISCARD) == 0) {
267 			isp->is_flags |= FLG_IS_DISCARD;
268 			isp->is_comdatkeep = isdp->isd_isp;
269 			DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp,
270 			    isdp->isd_isp));
271 		}
272 
273 		/*
274 		 * A discarded section does not require assignment to an output
275 		 * section.  However, if relaxed relocations have been enabled
276 		 * (either from -z relaxreloc, or asserted with .gnu.linkonce
277 		 * processing), then this section must still be assigned to an
278 		 * output section so that the sloppy relocation logic will have
279 		 * the information necessary to do its work.
280 		 */
281 		return (0);
282 	}
283 
284 	/*
285 	 * This is a new COMDAT section - so keep it.
286 	 */
287 	if ((isdp = libld_calloc(sizeof (Isd_node), 1)) == NULL)
288 		return (S_ERROR);
289 
290 	isdp->isd_name = isd.isd_name;
291 	isdp->isd_hash = isd.isd_hash;
292 	isdp->isd_isp = isp;
293 
294 	avl_insert(avlt, isdp, where);
295 	return (1);
296 }
297 
298 /*
299  * Determine whether a GNU group COMDAT section name follows the convention
300  *
301  *	section-name.symbol-name
302  *
303  * Each section within the input file is compared to see if the full section
304  * name matches the beginning of the COMDAT section, with a following '.'.
305  * A pointer to the symbol name, starting with the '.' is returned so that the
306  * caller can strip off the required section name.
307  */
308 static char *
309 gnu_comdat_sym(Ifl_desc *ifl, Is_desc *gisp)
310 {
311 	size_t	ndx;
312 
313 	for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
314 		Is_desc	*isp;
315 		size_t	ssize;
316 
317 		if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
318 		    (isp == gisp) || (isp->is_name == NULL))
319 			continue;
320 
321 		/*
322 		 * It's questionable whether this size should be cached in the
323 		 * Is_desc.  However, this seems an infrequent operation and
324 		 * adding Is_desc members can escalate memory usage for large
325 		 * link-edits.  For now, size the section name dynamically.
326 		 */
327 		ssize = strlen(isp->is_name);
328 		if ((strncmp(isp->is_name, gisp->is_name, ssize) != 0) &&
329 		    (gisp->is_name[ssize] == '.'))
330 			return ((char *)&gisp->is_name[ssize]);
331 	}
332 	return (NULL);
333 }
334 
335 /*
336  * GNU .gnu.linkonce sections follow a naming convention that indicates the
337  * required association with an output section.  Determine whether this input
338  * section follows the convention, and if so return the appropriate output
339  * section name.
340  *
341  *	.gnu.linkonce.b.*    ->	.bss
342  *	.gnu.linkonce.d.*    ->	.data
343  *	.gnu.linkonce.l.*    ->	.ldata
344  *	.gnu.linkonce.lb.*   ->	.lbss
345  *	.gnu.linkonce.lr.*   ->	.lrodata
346  *	.gnu.linkonce.r.*    ->	.rodata
347  *	.gnu.linkonce.s.*    ->	.sdata
348  *	.gnu.linkonce.s2.*   ->	.sdata2
349  *	.gnu.linkonce.sb.*   ->	.sbss
350  *	.gnu.linkonce.sb2.*  ->	.sbss2
351  *	.gnu.linkonce.t.*    ->	.text
352  *	.gnu.linkonce.tb.*   ->	.tbss
353  *	.gnu.linkonce.td.*   ->	.tdata
354  *	.gnu.linkonce.wi.*   ->	.debug_info
355  */
356 #define	NSTR_CH1(ch) (*(nstr + 1) == (ch))
357 #define	NSTR_CH2(ch) (*(nstr + 2) == (ch))
358 #define	NSTR_CH3(ch) (*(nstr + 3) == (ch))
359 
360 static const char *
361 gnu_linkonce_sec(const char *ostr)
362 {
363 	const char	*nstr = &ostr[MSG_SCN_GNU_LINKONCE_SIZE];
364 
365 	switch (*nstr) {
366 	case 'b':
367 		if (NSTR_CH1('.'))
368 			return (MSG_ORIG(MSG_SCN_BSS));
369 		break;
370 	case 'd':
371 		if (NSTR_CH1('.'))
372 			return (MSG_ORIG(MSG_SCN_DATA));
373 		break;
374 	case 'l':
375 		if (NSTR_CH1('.'))
376 			return (MSG_ORIG(MSG_SCN_LDATA));
377 		else if (NSTR_CH1('b') && NSTR_CH2('.'))
378 			return (MSG_ORIG(MSG_SCN_LBSS));
379 		else if (NSTR_CH1('r') && NSTR_CH2('.'))
380 			return (MSG_ORIG(MSG_SCN_LRODATA));
381 		break;
382 	case 'r':
383 		if (NSTR_CH1('.'))
384 			return (MSG_ORIG(MSG_SCN_RODATA));
385 		break;
386 	case 's':
387 		if (NSTR_CH1('.'))
388 			return (MSG_ORIG(MSG_SCN_SDATA));
389 		else if (NSTR_CH1('2') && NSTR_CH2('.'))
390 			return (MSG_ORIG(MSG_SCN_SDATA2));
391 		else if (NSTR_CH1('b') && NSTR_CH2('.'))
392 			return (MSG_ORIG(MSG_SCN_SBSS));
393 		else if (NSTR_CH1('b') && NSTR_CH2('2') && NSTR_CH3('.'))
394 			return (MSG_ORIG(MSG_SCN_SBSS2));
395 		break;
396 	case 't':
397 		if (NSTR_CH1('.'))
398 			return (MSG_ORIG(MSG_SCN_TEXT));
399 		else if (NSTR_CH1('b') && NSTR_CH2('.'))
400 			return (MSG_ORIG(MSG_SCN_TBSS));
401 		else if (NSTR_CH1('d') && NSTR_CH2('.'))
402 			return (MSG_ORIG(MSG_SCN_TDATA));
403 		break;
404 	case 'w':
405 		if (NSTR_CH1('i') && NSTR_CH2('.'))
406 			return (MSG_ORIG(MSG_SCN_DEBUG_INFO));
407 		break;
408 	default:
409 		break;
410 	}
411 
412 	/*
413 	 * No special name match found.
414 	 */
415 	return (ostr);
416 }
417 #undef	NSTR_CH1
418 #undef	NSTR_CH2
419 #undef	NSTR_CH3
420 
421 /*
422  * Place a section into the appropriate segment and output section.
423  *
424  * entry:
425  *	ofl - File descriptor
426  *	isp - Input section descriptor of section to be placed.
427  *	ident - Section identifier, used to order sections relative to
428  *		others within the output segment.
429  *	alt_os_name - If non-NULL, the name of the output section to place
430  *		isp into. If NULL, input sections go to an output section
431  *		with the same name as the input section.
432  */
433 Os_desc *
434 ld_place_section(Ofl_desc *ofl, Is_desc *isp, int ident,
435     const char *alt_os_name)
436 {
437 	Ent_desc	*enp;
438 	Sg_desc		*sgp;
439 	Os_desc		*osp;
440 	Aliste		idx1, iidx;
441 	int		os_ndx;
442 	Shdr		*shdr = isp->is_shdr;
443 	Xword		shflagmask, shflags = shdr->sh_flags;
444 	Ifl_desc	*ifl = isp->is_file;
445 	char		*oname, *sname;
446 	uint_t		onamehash;
447 
448 	/*
449 	 * Define any sections that must be thought of as referenced.  These
450 	 * sections may not be referenced externaly in a manner ld(1) can
451 	 * discover, but they must be retained (ie. not removed by -zignore).
452 	 */
453 	static const Msg RefSecs[] = {
454 		MSG_SCN_INIT,		/* MSG_ORIG(MSG_SCN_INIT) */
455 		MSG_SCN_FINI,		/* MSG_ORIG(MSG_SCN_FINI) */
456 		MSG_SCN_EX_RANGES,	/* MSG_ORIG(MSG_SCN_EX_RANGES) */
457 		MSG_SCN_EX_SHARED,	/* MSG_ORIG(MSG_SCN_EX_SHARED) */
458 		MSG_SCN_CTORS,		/* MSG_ORIG(MSG_SCN_CTORS) */
459 		MSG_SCN_DTORS,		/* MSG_ORIG(MSG_SCN_DTORS) */
460 		MSG_SCN_EHFRAME,	/* MSG_ORIG(MSG_SCN_EHFRAME) */
461 		MSG_SCN_EHFRAME_HDR,	/* MSG_ORIG(MSG_SCN_EHFRAME_HDR) */
462 		MSG_SCN_JCR,		/* MSG_ORIG(MSG_SCN_JCR) */
463 		0
464 	};
465 
466 	DBG_CALL(Dbg_sec_in(ofl->ofl_lml, isp));
467 
468 	/*
469 	 * If this section identfies group members, or this section indicates
470 	 * that it is a member of a group, determine whether the section is
471 	 * still required.
472 	 */
473 	if ((shflags & SHF_GROUP) || (shdr->sh_type == SHT_GROUP)) {
474 		Group_desc	*gdesc;
475 
476 		if ((gdesc = ld_get_group(ofl, isp)) != NULL) {
477 			DBG_CALL(Dbg_sec_group(ofl->ofl_lml, isp, gdesc));
478 
479 			/*
480 			 * If this group has been replaced by another group,
481 			 * then this section needs to be discarded.
482 			 */
483 			if (gdesc->gd_oisc) {
484 				isp->is_flags |= FLG_IS_DISCARD;
485 
486 				/*
487 				 * Since we're discarding the section, we
488 				 * can skip assigning it to an output section.
489 				 * The exception is that if the user
490 				 * specifies -z relaxreloc, then
491 				 * we need to assign the output section so
492 				 * that the sloppy relocation logic will have
493 				 * the information necessary to do its work.
494 				 */
495 				if (!(ofl->ofl_flags1 & FLG_OF1_RLXREL))
496 					return (NULL);
497 			}
498 		}
499 
500 		/*
501 		 * SHT_GROUP sections can only be included into relocatable
502 		 * objects.
503 		 */
504 		if (shdr->sh_type == SHT_GROUP) {
505 			if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) {
506 				isp->is_flags |= FLG_IS_DISCARD;
507 				return (NULL);
508 			}
509 		}
510 	}
511 
512 	/*
513 	 * Always assign SHF_TLS sections to the DATA segment (and then the
514 	 * PT_TLS embedded inside of there).
515 	 */
516 	if (shflags & SHF_TLS)
517 		shflags |= SHF_WRITE;
518 
519 	/*
520 	 * Traverse the entrance criteria list searching for a segment that
521 	 * matches the input section we have.  If an entrance criterion is set
522 	 * then there must be an exact match.  If we complete the loop without
523 	 * finding a segment, then sgp will be NULL.
524 	 */
525 	sgp = NULL;
526 	for (ALIST_TRAVERSE(ofl->ofl_ents, idx1, enp)) {
527 		if (enp->ec_segment &&
528 		    (enp->ec_segment->sg_flags & FLG_SG_DISABLED))
529 			continue;
530 		if (enp->ec_type && (enp->ec_type != shdr->sh_type))
531 			continue;
532 		if (enp->ec_attrmask &&
533 		    /* LINTED */
534 		    (enp->ec_attrmask & enp->ec_attrbits) !=
535 		    (enp->ec_attrmask & shflags))
536 			continue;
537 		if (((enp->ec_flags & FLG_EC_BUILTIN) == 0) &&
538 		    enp->ec_name && (strcmp(enp->ec_name, isp->is_name) != 0))
539 			continue;
540 		if (enp->ec_files) {
541 			Aliste	idx2;
542 			char	*file;
543 			int	found = 0;
544 
545 			if (isp->is_file == NULL)
546 				continue;
547 
548 			for (APLIST_TRAVERSE(enp->ec_files, idx2, file)) {
549 				const char	*name = isp->is_file->ifl_name;
550 
551 				if (file[0] == '*') {
552 					const char	*basename;
553 
554 					basename = strrchr(name, '/');
555 					if (basename == NULL)
556 						basename = name;
557 					else if (basename[1] != '\0')
558 						basename++;
559 
560 					if (strcmp(&file[1], basename) == 0) {
561 						found++;
562 						break;
563 					}
564 				} else {
565 					if (strcmp(file, name) == 0) {
566 						found++;
567 						break;
568 					}
569 				}
570 			}
571 			if (!found)
572 				continue;
573 		}
574 		sgp = enp->ec_segment;
575 		break;
576 	}
577 
578 	if (sgp == NULL) {
579 		enp = alist_item(ofl->ofl_ents,
580 		    alist_nitems(ofl->ofl_ents) - 1);
581 		sgp = enp->ec_segment;
582 	}
583 
584 	/*
585 	 * If our caller has supplied an alternative name for the output
586 	 * section, then we defer to their request. Otherwise, the default
587 	 * is to use the same name as that of the input section being placed.
588 	 *
589 	 * The COMDAT, SHT_GROUP and GNU name translations that follow have
590 	 * the potential to alter this initial name.
591 	 */
592 	oname = (char *)((alt_os_name == NULL) ? isp->is_name : alt_os_name);
593 
594 	/*
595 	 * Solaris section names may follow the convention:
596 	 *
597 	 *	section-name%symbol-name
598 	 *
599 	 * This convention has been used to order the layout of sections within
600 	 * segments for objects built with the compilers -xF option.  However,
601 	 * the final object should not contain individual section headers for
602 	 * all such input sections, instead the symbol name is stripped from the
603 	 * name to establish the final output section name.
604 	 *
605 	 * This convention has also been followed for COMDAT and sections
606 	 * identified though SHT_GROUP data.
607 	 *
608 	 * Strip out the % from the section name in all cases except:
609 	 *
610 	 *    i.	when '-r' is used without '-M', and
611 	 *    ii.	when '-r' is used with '-M' but without the ?O flag.
612 	 */
613 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) ||
614 	    (sgp->sg_flags & FLG_SG_ORDER)) {
615 		if ((sname = strchr(isp->is_name, '%')) != NULL) {
616 			size_t	size = sname - isp->is_name;
617 
618 			if ((oname = libld_malloc(size + 1)) == NULL)
619 				return ((Os_desc *)S_ERROR);
620 			(void) strncpy(oname, isp->is_name, size);
621 			oname[size] = '\0';
622 			DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname));
623 		}
624 		isp->is_ordndx = enp->ec_ordndx;
625 	}
626 
627 	/*
628 	 * GNU section names may follow the convention:
629 	 *
630 	 *	.gnu.linkonce.*
631 	 *
632 	 * The .gnu.linkonce is a section naming convention that indicates a
633 	 * COMDAT requirement.  Determine whether this section follows the GNU
634 	 * pattern, and if so, determine whether this section should be
635 	 * discarded or retained.  The comparison of is_name[1] with 'g'
636 	 * is an optimization to skip using strncmp() too much. This is safe,
637 	 * because we know the name is not NULL, and therefore must have
638 	 * at least one character plus a NULL termination.
639 	 */
640 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
641 	    (isp->is_name == oname) && (isp->is_name[1] == 'g') &&
642 	    (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE), isp->is_name,
643 	    MSG_SCN_GNU_LINKONCE_SIZE) == 0)) {
644 		if ((oname =
645 		    (char *)gnu_linkonce_sec(isp->is_name)) != isp->is_name) {
646 			DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname));
647 		}
648 
649 		/*
650 		 * Explicitly identify this section type as COMDAT.  Also,
651 		 * enable relaxed relocation processing, as this is typically
652 		 * a requirement with .gnu.linkonce sections.
653 		 */
654 		isp->is_flags |= FLG_IS_COMDAT;
655 		if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0)
656 			ofl->ofl_flags1 |= FLG_OF1_RLXREL;
657 		Dbg_sec_gnu_comdat(ofl->ofl_lml, isp, TRUE,
658 		    (ofl->ofl_flags1 & FLG_OF1_RLXREL) != 0);
659 	}
660 
661 	/*
662 	 * GNU section names may also follow the convention:
663 	 *
664 	 *	section-name.symbol-name
665 	 *
666 	 * This convention is used when defining SHT_GROUP sections of type
667 	 * COMDAT.  Thus, any group processing will have discovered any group
668 	 * sections, and this identification can be triggered by a pattern
669 	 * match section names.
670 	 */
671 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
672 	    (isp->is_name == oname) && (isp->is_flags & FLG_IS_COMDAT) &&
673 	    ((sname = gnu_comdat_sym(ifl, isp)) != NULL)) {
674 		size_t	size = sname - isp->is_name;
675 
676 		if ((oname = libld_malloc(size + 1)) == NULL)
677 			return ((Os_desc *)S_ERROR);
678 		(void) strncpy(oname, isp->is_name, size);
679 		oname[size] = '\0';
680 		DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp, oname));
681 
682 		/*
683 		 * Enable relaxed relocation processing, as this is
684 		 * typically a requirement with GNU COMDAT sections.
685 		 */
686 		if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0) {
687 			ofl->ofl_flags1 |= FLG_OF1_RLXREL;
688 			Dbg_sec_gnu_comdat(ofl->ofl_lml, isp, FALSE, TRUE);
689 		}
690 	}
691 
692 	/*
693 	 * Assign a hash value now that the output section name has been
694 	 * finalized.
695 	 */
696 	onamehash = sgs_str_hash(oname);
697 
698 	if (sgp->sg_flags & FLG_SG_ORDER)
699 		enp->ec_flags |= FLG_EC_USED;
700 
701 	/*
702 	 * Determine if section ordering is turned on. If so, return the
703 	 * appropriate ordering index for the section. This information
704 	 * is derived from the Sg_desc->sg_segorder list that was built
705 	 * up from the Mapfile.
706 	 */
707 	os_ndx = 0;
708 	if (sgp->sg_secorder) {
709 		Sec_order	*scop;
710 
711 		for (APLIST_TRAVERSE(sgp->sg_secorder, idx1, scop)) {
712 			if (strcmp(scop->sco_secname, oname) == 0) {
713 				scop->sco_flags |= FLG_SGO_USED;
714 				os_ndx = scop->sco_index;
715 				break;
716 			}
717 		}
718 	}
719 
720 	/*
721 	 * Mask of section header flags to ignore when matching sections. We
722 	 * are more strict with relocatable objects, ignoring only the order
723 	 * flags, and keeping sections apart if they differ otherwise. This
724 	 * follows the policy that sections in a relative object should only
725 	 * be merged if their flags are the same, and avoids destroying
726 	 * information prematurely. For final products however, we ignore all
727 	 * flags that do not prevent a merge.
728 	 */
729 	shflagmask =
730 	    (ofl->ofl_flags & FLG_OF_RELOBJ) ? ALL_SHF_ORDER : ALL_SHF_IGNORE;
731 
732 	/*
733 	 * Traverse the input section list for the output section we have been
734 	 * assigned. If we find a matching section simply add this new section.
735 	 */
736 	iidx = 0;
737 	for (APLIST_TRAVERSE(sgp->sg_osdescs, idx1, osp)) {
738 		Shdr	*_shdr = osp->os_shdr;
739 
740 		if ((ident == osp->os_identndx) &&
741 		    (ident != ld_targ.t_id.id_rel) &&
742 		    (onamehash == osp->os_namehash) &&
743 		    (shdr->sh_type != SHT_GROUP) &&
744 		    (shdr->sh_type != SHT_SUNW_dof) &&
745 		    ((shdr->sh_type == _shdr->sh_type) ||
746 		    ((shdr->sh_type == SHT_SUNW_COMDAT) &&
747 		    (_shdr->sh_type == SHT_PROGBITS))) &&
748 		    ((shflags & ~shflagmask) ==
749 		    (_shdr->sh_flags & ~shflagmask)) &&
750 		    (strcmp(oname, osp->os_name) == 0)) {
751 			uintptr_t	err;
752 
753 			/*
754 			 * Process any COMDAT section, keeping the first and
755 			 * discarding all others.
756 			 */
757 			if ((isp->is_flags & FLG_IS_COMDAT) &&
758 			    ((err = add_comdat(ofl, osp, isp)) != 1))
759 				return ((Os_desc *)err);
760 
761 			/*
762 			 * Set alignment
763 			 */
764 			set_addralign(ofl, osp, isp);
765 
766 			/*
767 			 * If this section is a non-empty TLS section indicate
768 			 * that a PT_TLS program header is required.
769 			 */
770 			if ((shflags & SHF_TLS) && shdr->sh_size &&
771 			    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0))
772 				ofl->ofl_flags |= FLG_OF_TLSPHDR;
773 
774 			/*
775 			 * Insert the input section descriptor on the proper
776 			 * output section descriptor list.
777 			 *
778 			 * If this segment requires input section ordering,
779 			 * honor any mapfile specified ordering for otherwise
780 			 * unordered sections by setting the mapfile_sort
781 			 * argument of os_attach_isp() to True.
782 			 */
783 
784 			if (os_attach_isp(ofl, osp, isp,
785 			    (sgp->sg_flags & FLG_SG_ORDER) != 0) == 0)
786 				return ((Os_desc *)S_ERROR);
787 
788 			/*
789 			 * If this input section and file is associated to an
790 			 * artificially referenced output section, make sure
791 			 * they are marked as referenced also. This ensures
792 			 * that this input section and file isn't eliminated
793 			 * when -zignore is in effect.
794 			 *
795 			 * See -zignore comments when creating a new output
796 			 * section below.
797 			 */
798 			if (((ifl &&
799 			    (ifl->ifl_flags & FLG_IF_IGNORE)) || DBG_ENABLED) &&
800 			    (osp->os_flags & FLG_OS_SECTREF)) {
801 				isp->is_flags |= FLG_IS_SECTREF;
802 				if (ifl)
803 					ifl->ifl_flags |= FLG_IF_FILEREF;
804 			}
805 
806 			DBG_CALL(Dbg_sec_added(ofl->ofl_lml, osp, sgp));
807 			return (osp);
808 		}
809 
810 		/*
811 		 * Do we need to worry about section ordering?
812 		 */
813 		if (os_ndx) {
814 			if (osp->os_ordndx) {
815 				if (os_ndx < osp->os_ordndx)
816 					/* insert section here. */
817 					break;
818 				else {
819 					iidx = idx1 + 1;
820 					continue;
821 				}
822 			} else {
823 				/* insert section here. */
824 				break;
825 			}
826 		} else if (osp->os_ordndx) {
827 			iidx = idx1 + 1;
828 			continue;
829 		}
830 
831 		/*
832 		 * If the new sections identifier is less than that of the
833 		 * present input section we need to insert the new section
834 		 * at this point.
835 		 */
836 		if (ident < osp->os_identndx)
837 			break;
838 
839 		iidx = idx1 + 1;
840 	}
841 
842 	/*
843 	 * We are adding a new output section.  Update the section header
844 	 * count and associated string size.
845 	 *
846 	 * If the input section triggering this output section has been marked
847 	 * for discard, and if no other non-discarded input section comes along
848 	 * to join it, then we will over count. We cannot know if this will
849 	 * happen or not until all input is seen. Set FLG_OF_AJDOSCNT to
850 	 * trigger a final count readjustment.
851 	 */
852 	if (isp->is_flags & FLG_IS_DISCARD)
853 		ofl->ofl_flags |= FLG_OF_ADJOSCNT;
854 	ofl->ofl_shdrcnt++;
855 	if (st_insert(ofl->ofl_shdrsttab, oname) == -1)
856 		return ((Os_desc *)S_ERROR);
857 
858 	/*
859 	 * Create a new output section descriptor.
860 	 */
861 	if ((osp = libld_calloc(sizeof (Os_desc), 1)) == NULL)
862 		return ((Os_desc *)S_ERROR);
863 	if ((osp->os_shdr = libld_calloc(sizeof (Shdr), 1)) == NULL)
864 		return ((Os_desc *)S_ERROR);
865 
866 	/*
867 	 * Convert COMDAT section to PROGBITS as this the first section of the
868 	 * output section.  Save any COMDAT section for later processing, as
869 	 * additional COMDAT sections that match this section need discarding.
870 	 */
871 	if (shdr->sh_type == SHT_SUNW_COMDAT) {
872 		Shdr	*tshdr;
873 
874 		if ((tshdr = libld_malloc(sizeof (Shdr))) == NULL)
875 			return ((Os_desc *)S_ERROR);
876 		*tshdr = *shdr;
877 		isp->is_shdr = shdr = tshdr;
878 		shdr->sh_type = SHT_PROGBITS;
879 	}
880 	if ((isp->is_flags & FLG_IS_COMDAT) &&
881 	    (add_comdat(ofl, osp, isp) == S_ERROR))
882 		return ((Os_desc *)S_ERROR);
883 
884 	osp->os_shdr->sh_type = shdr->sh_type;
885 	osp->os_shdr->sh_flags = shdr->sh_flags;
886 	osp->os_shdr->sh_entsize = shdr->sh_entsize;
887 	osp->os_name = oname;
888 	osp->os_namehash = onamehash;
889 	osp->os_ordndx = os_ndx;
890 	osp->os_sgdesc = sgp;
891 
892 	if (ifl && (shdr->sh_type == SHT_PROGBITS)) {
893 		/*
894 		 * Try to preserve the intended meaning of sh_link/sh_info.
895 		 * See the translate_link() in update.c.
896 		 */
897 		osp->os_shdr->sh_link = shdr->sh_link;
898 		if (shdr->sh_flags & SHF_INFO_LINK)
899 			osp->os_shdr->sh_info = shdr->sh_info;
900 	}
901 
902 	/*
903 	 * When -zignore is in effect, user supplied sections and files that are
904 	 * not referenced from other sections, are eliminated from the object
905 	 * being produced.  Some sections, although unreferenced, are special,
906 	 * and must not be eliminated.  Determine if this new output section is
907 	 * one of those special sections, and if so mark it artificially as
908 	 * referenced.  Any input section and file associated to this output
909 	 * section is also be marked as referenced, and thus won't be eliminated
910 	 * from the final output.
911 	 */
912 	if (ifl && ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) || DBG_ENABLED)) {
913 		const Msg	*refsec;
914 
915 		for (refsec = RefSecs; *refsec; refsec++) {
916 			if (strcmp(osp->os_name, MSG_ORIG(*refsec)) == 0) {
917 				osp->os_flags |= FLG_OS_SECTREF;
918 
919 				if ((ifl->ifl_flags & FLG_IF_IGNORE) ||
920 				    DBG_ENABLED) {
921 					isp->is_flags |= FLG_IS_SECTREF;
922 					ifl->ifl_flags |= FLG_IF_FILEREF;
923 				}
924 				break;
925 			}
926 		}
927 	}
928 
929 	/*
930 	 * Sections of type SHT_GROUP are added to the ofl->ofl_osgroups list,
931 	 * so that they can be updated as a group later.
932 	 */
933 	if ((shdr->sh_type == SHT_GROUP) &&
934 	    (aplist_append(&ofl->ofl_osgroups, osp,
935 	    AL_CNT_OFL_OSGROUPS) == NULL))
936 		return ((Os_desc *)S_ERROR);
937 
938 	/*
939 	 * If this section is a non-empty TLS section indicate that a PT_TLS
940 	 * program header is required.
941 	 */
942 	if ((shflags & SHF_TLS) && shdr->sh_size &&
943 	    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0))
944 		ofl->ofl_flags |= FLG_OF_TLSPHDR;
945 
946 	/*
947 	 * If a non-allocatable section is going to be put into a loadable
948 	 * segment then turn on the allocate bit for this section and warn the
949 	 * user that we have done so.  This could only happen through the use
950 	 * of a mapfile.
951 	 */
952 	if ((sgp->sg_phdr.p_type == PT_LOAD) &&
953 	    ((osp->os_shdr->sh_flags & SHF_ALLOC) == 0)) {
954 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_SCN_NONALLOC),
955 		    ofl->ofl_name, osp->os_name);
956 		osp->os_shdr->sh_flags |= SHF_ALLOC;
957 	}
958 
959 	/*
960 	 * Retain this sections identifier for future comparisons when placing
961 	 * a section (after all sections have been processed this variable will
962 	 * be used to hold the sections symbol index as we don't need to retain
963 	 * the identifier any more).
964 	 */
965 	osp->os_identndx = ident;
966 
967 	/*
968 	 * Set alignment.
969 	 */
970 	set_addralign(ofl, osp, isp);
971 
972 	if (os_attach_isp(ofl, osp, isp, 0) == 0)
973 		return ((Os_desc *)S_ERROR);
974 
975 	DBG_CALL(Dbg_sec_created(ofl->ofl_lml, osp, sgp));
976 
977 	/*
978 	 * Insert the new section at the offset given by iidx.  If no position
979 	 * for it was identified above, this will be index 0, causing the new
980 	 * section to be prepended to the beginning of the section list.
981 	 * Otherwise, it is the index following the section that was identified.
982 	 */
983 	if (aplist_insert(&sgp->sg_osdescs, osp, AL_CNT_SG_OSDESC,
984 	    iidx) == NULL)
985 		return ((Os_desc *)S_ERROR);
986 	return (osp);
987 }
988