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