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