xref: /titanic_50/usr/src/cmd/sgs/libld/common/place.c (revision 753a6d457b330b1b29b2d3eefcd0831116ce950d)
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_name && (strcmp(enp->ec_name, isp->is_name) != 0))
441 			continue;
442 		if (enp->ec_files) {
443 			Aliste	idx2;
444 			char	*file;
445 			int	found = 0;
446 
447 			if (isp->is_file == NULL)
448 				continue;
449 
450 			for (APLIST_TRAVERSE(enp->ec_files, idx2, file)) {
451 				const char	*name = isp->is_file->ifl_name;
452 
453 				if (file[0] == '*') {
454 					const char	*basename;
455 
456 					basename = strrchr(name, '/');
457 					if (basename == NULL)
458 						basename = name;
459 					else if (basename[1] != '\0')
460 						basename++;
461 
462 					if (strcmp(&file[1], basename) == 0) {
463 						found++;
464 						break;
465 					}
466 				} else {
467 					if (strcmp(file, name) == 0) {
468 						found++;
469 						break;
470 					}
471 				}
472 			}
473 			if (!found)
474 				continue;
475 		}
476 		sgp = enp->ec_segment;
477 		break;
478 	}
479 
480 	if (sgp == NULL) {
481 		enp = alist_item(ofl->ofl_ents,
482 		    alist_nitems(ofl->ofl_ents) - 1);
483 		sgp = enp->ec_segment;
484 	}
485 
486 	/*
487 	 * By default, the output section for an input section has the same
488 	 * section name as in the input sections name.  COMDAT, SHT_GROUP and
489 	 * GNU name translations that follow, may indicate that a different
490 	 * output section name be the target for this input section.
491 	 */
492 	oname = (char *)isp->is_name;
493 
494 	/*
495 	 * Solaris section names may follow the convention:
496 	 *
497 	 *	section-name%symbol-name
498 	 *
499 	 * This convention has been used to order the layout of sections within
500 	 * segments for objects built with the compilers -xF option.  However,
501 	 * the final object should not contain individual section headers for
502 	 * all such input sections, instead the symbol name is stripped from the
503 	 * name to establish the final output section name.
504 	 *
505 	 * This convention has also been followed for COMDAT and sections
506 	 * identified though SHT_GROUP data.
507 	 *
508 	 * Strip out the % from the section name in all cases except:
509 	 *
510 	 *    i.	when '-r' is used without '-M', and
511 	 *    ii.	when '-r' is used with '-M' but without the ?O flag.
512 	 */
513 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) ||
514 	    (sgp->sg_flags & FLG_SG_ORDER)) {
515 		if ((sname = strchr(isp->is_name, '%')) != NULL) {
516 			size_t	size = sname - isp->is_name;
517 
518 			if ((oname = libld_malloc(size + 1)) == NULL)
519 				return ((Os_desc *)S_ERROR);
520 			(void) strncpy(oname, isp->is_name, size);
521 			oname[size] = '\0';
522 			DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp->is_name,
523 			    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->is_name,
548 			    oname));
549 		}
550 
551 		/*
552 		 * Explicitly identify this section type as COMDAT.  Also,
553 		 * enable lazy relocation processing, as this is typically a
554 		 * requirement with .gnu.linkonce sections.
555 		 */
556 		isp->is_flags |= FLG_IS_COMDAT;
557 		if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0)
558 			ofl->ofl_flags1 |= FLG_OF1_RLXREL;
559 		Dbg_sec_gnu_comdat(ofl->ofl_lml, isp->is_name, 1,
560 		    (ofl->ofl_flags1 & FLG_OF1_RLXREL));
561 	}
562 
563 	/*
564 	 * GNU section names may also follow the convention:
565 	 *
566 	 *	section-name.symbol-name
567 	 *
568 	 * This convention is used when defining SHT_GROUP sections of type
569 	 * COMDAT.  Thus, any group processing will have discovered any group
570 	 * sections, and this identification can be triggered by a pattern
571 	 * match section names.
572 	 */
573 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
574 	    (isp->is_name == oname) && (isp->is_flags & FLG_IS_COMDAT) &&
575 	    ((sname = gnu_comdat_sym(ifl, isp)) != NULL)) {
576 		size_t	size = sname - isp->is_name;
577 
578 		if ((oname = libld_malloc(size + 1)) == NULL)
579 			return ((Os_desc *)S_ERROR);
580 		(void) strncpy(oname, isp->is_name, size);
581 		oname[size] = '\0';
582 		DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp->is_name,
583 		    oname));
584 
585 		/*
586 		 * Enable lazy relocation processing, as this is typically a
587 		 * requirement with GNU COMDAT sections.
588 		 */
589 		if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0) {
590 			ofl->ofl_flags1 |= FLG_OF1_RLXREL;
591 			Dbg_sec_gnu_comdat(ofl->ofl_lml, isp->is_name, 0,
592 			    (ofl->ofl_flags1 & FLG_OF1_RLXREL));
593 		}
594 	}
595 
596 	/*
597 	 * Assign a hash value now that the output section name has been
598 	 * finalized.
599 	 */
600 	onamehash = sgs_str_hash(oname);
601 
602 	if (sgp->sg_flags & FLG_SG_ORDER)
603 		enp->ec_flags |= FLG_EC_USED;
604 
605 	/*
606 	 * If the link is not 0, then the input section is appended to the
607 	 * defined output section.  The append occurs at the input section
608 	 * pointed to by the link.
609 	 */
610 	if (link) {
611 		uintptr_t	err;
612 
613 		osp = isp->is_file->ifl_isdesc[link]->is_osdesc;
614 
615 		/*
616 		 * Process any COMDAT section, keeping the first and
617 		 * discarding all others.
618 		 */
619 		if ((isp->is_flags & FLG_IS_COMDAT) &&
620 		    ((err = add_comdat(ofl, osp, isp)) != 1))
621 			return ((Os_desc *)err);
622 
623 		/*
624 		 * Set alignment
625 		 */
626 		set_addralign(ofl, osp, isp);
627 
628 		if (ld_append_isp(ofl, osp, isp, 0) == 0)
629 			return ((Os_desc *)S_ERROR);
630 
631 		isp->is_osdesc = osp;
632 		sgp = osp->os_sgdesc;
633 
634 		DBG_CALL(Dbg_sec_added(ofl->ofl_lml, osp, sgp));
635 		return (osp);
636 	}
637 
638 	/*
639 	 * Determine if section ordering is turned on.  If so, return the
640 	 * appropriate os_txtndx.  This information is derived from the
641 	 * Sg_desc->sg_segorder list that was built up from the Mapfile.
642 	 */
643 	os_ndx = 0;
644 	if (sgp->sg_secorder) {
645 		Sec_order	*scop;
646 
647 		for (APLIST_TRAVERSE(sgp->sg_secorder, idx1, scop)) {
648 			if (strcmp(scop->sco_secname, oname) == 0) {
649 				scop->sco_flags |= FLG_SGO_USED;
650 				os_ndx = scop->sco_index;
651 				break;
652 			}
653 		}
654 	}
655 
656 	/*
657 	 * Mask of section header flags to ignore when
658 	 * matching sections. We are more strict with
659 	 * relocatable objects, ignoring only the order
660 	 * flags, and keeping sections apart if they differ
661 	 * otherwise. This follows the policy that sections
662 	 * in a relative object should only be merged if their
663 	 * flags are the same, and avoids destroying information
664 	 * prematurely. For final products however, we ignore all
665 	 * flags that do not prevent a merge.
666 	 */
667 	shflagmask =
668 	    (ofl->ofl_flags & FLG_OF_RELOBJ) ? ALL_SHF_ORDER : ALL_SHF_IGNORE;
669 
670 	/*
671 	 * Traverse the input section list for the output section we have been
672 	 * assigned. If we find a matching section simply add this new section.
673 	 */
674 	iidx = 0;
675 	for (APLIST_TRAVERSE(sgp->sg_osdescs, idx1, osp)) {
676 		Shdr	*_shdr = osp->os_shdr;
677 
678 		if ((ident == osp->os_identndx) &&
679 		    (ident != ld_targ.t_id.id_rel) &&
680 		    (onamehash == osp->os_namehash) &&
681 		    (shdr->sh_type != SHT_GROUP) &&
682 		    (shdr->sh_type != SHT_SUNW_dof) &&
683 		    ((shdr->sh_type == _shdr->sh_type) ||
684 		    ((shdr->sh_type == SHT_SUNW_COMDAT) &&
685 		    (_shdr->sh_type == SHT_PROGBITS))) &&
686 		    ((shflags & ~shflagmask) ==
687 		    (_shdr->sh_flags & ~shflagmask)) &&
688 		    (strcmp(oname, osp->os_name) == 0)) {
689 			uintptr_t	err;
690 			int		inserted;
691 
692 			/*
693 			 * Process any COMDAT section, keeping the first and
694 			 * discarding all others.
695 			 */
696 			if ((isp->is_flags & FLG_IS_COMDAT) &&
697 			    ((err = add_comdat(ofl, osp, isp)) != 1))
698 				return ((Os_desc *)err);
699 
700 			/*
701 			 * Set alignment
702 			 */
703 			set_addralign(ofl, osp, isp);
704 
705 			/*
706 			 * If this section is a non-empty TLS section indicate
707 			 * that a PT_TLS program header is required.
708 			 */
709 			if ((shflags & SHF_TLS) && shdr->sh_size &&
710 			    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0))
711 				ofl->ofl_flags |= FLG_OF_TLSPHDR;
712 
713 			/*
714 			 * Determine whether this segment requires input section
715 			 * ordering.  Sections that require ordering were
716 			 * defined via a mapfile, and have an ordering index
717 			 * assigned to them (is_ordndx).  Ordered sections are
718 			 * placed in ascending order before unordered sections
719 			 * (sections with a is_ordndx value of zero).
720 			 */
721 			inserted = 0;
722 			if ((sgp->sg_flags & FLG_SG_ORDER) && isp->is_ordndx) {
723 				Aliste	idx2;
724 				Is_desc	*isp2;
725 
726 				for (APLIST_TRAVERSE(osp->os_isdescs,
727 				    idx2, isp2)) {
728 					if (isp2->is_ordndx &&
729 					    (isp2->is_ordndx <= isp->is_ordndx))
730 						continue;
731 
732 					if (aplist_insert(&(osp->os_isdescs),
733 					    isp, AL_CNT_OS_ISDESCS,
734 					    idx2) == NULL)
735 						return ((Os_desc *)S_ERROR);
736 					inserted ++;
737 					break;
738 				}
739 			}
740 			if (inserted == 0) {
741 				if (aplist_append(&(osp->os_isdescs),
742 				    isp, AL_CNT_OS_ISDESCS) == NULL)
743 					return ((Os_desc *)S_ERROR);
744 			}
745 			if (ld_append_isp(ofl, osp, isp, 1) == 0)
746 				return ((Os_desc *)S_ERROR);
747 
748 			isp->is_osdesc = osp;
749 
750 			/*
751 			 * If this input section and file is associated to an
752 			 * artificially referenced output section, make sure
753 			 * they are marked as referenced also. This insures this
754 			 * input section and file isn't eliminated when -zignore
755 			 * is in effect.
756 			 * See -zignore comments when creating a new output
757 			 * section below.
758 			 */
759 			if (((ifl &&
760 			    (ifl->ifl_flags & FLG_IF_IGNORE)) || DBG_ENABLED) &&
761 			    (osp->os_flags & FLG_OS_SECTREF)) {
762 				isp->is_flags |= FLG_IS_SECTREF;
763 				if (ifl)
764 					ifl->ifl_flags |= FLG_IF_FILEREF;
765 			}
766 
767 			DBG_CALL(Dbg_sec_added(ofl->ofl_lml, osp, sgp));
768 			return (osp);
769 		}
770 
771 		/*
772 		 * Do we need to worry about section ordering?
773 		 */
774 		if (os_ndx) {
775 			if (osp->os_ordndx) {
776 				if (os_ndx < osp->os_ordndx)
777 					/* insert section here. */
778 					break;
779 				else {
780 					iidx = idx1 + 1;
781 					continue;
782 				}
783 			} else {
784 				/* insert section here. */
785 				break;
786 			}
787 		} else if (osp->os_ordndx) {
788 			iidx = idx1 + 1;
789 			continue;
790 		}
791 
792 		/*
793 		 * If the new sections identifier is less than that of the
794 		 * present input section we need to insert the new section
795 		 * at this point.
796 		 */
797 		if (ident < osp->os_identndx)
798 			break;
799 
800 		iidx = idx1 + 1;
801 	}
802 
803 	/*
804 	 * We are adding a new output section.  Update the section header
805 	 * count and associated string size.
806 	 */
807 	ofl->ofl_shdrcnt++;
808 	if (st_insert(ofl->ofl_shdrsttab, oname) == -1)
809 		return ((Os_desc *)S_ERROR);
810 
811 	/*
812 	 * Create a new output section descriptor.
813 	 */
814 	if ((osp = libld_calloc(sizeof (Os_desc), 1)) == NULL)
815 		return ((Os_desc *)S_ERROR);
816 	if ((osp->os_shdr = libld_calloc(sizeof (Shdr), 1)) == NULL)
817 		return ((Os_desc *)S_ERROR);
818 
819 	/*
820 	 * Convert COMDAT section to PROGBITS as this the first section of the
821 	 * output section.  Save any COMDAT section for later processing, as
822 	 * additional COMDAT sections that match this section need discarding.
823 	 */
824 	if (shdr->sh_type == SHT_SUNW_COMDAT) {
825 		Shdr	*tshdr;
826 
827 		if ((tshdr = libld_malloc(sizeof (Shdr))) == NULL)
828 			return ((Os_desc *)S_ERROR);
829 		*tshdr = *shdr;
830 		isp->is_shdr = shdr = tshdr;
831 		shdr->sh_type = SHT_PROGBITS;
832 	}
833 	if ((isp->is_flags & FLG_IS_COMDAT) &&
834 	    (add_comdat(ofl, osp, isp) == S_ERROR))
835 		return ((Os_desc *)S_ERROR);
836 
837 	osp->os_shdr->sh_type = shdr->sh_type;
838 	osp->os_shdr->sh_flags = shdr->sh_flags;
839 	osp->os_shdr->sh_entsize = shdr->sh_entsize;
840 	osp->os_name = oname;
841 	osp->os_namehash = onamehash;
842 	osp->os_ordndx = os_ndx;
843 	osp->os_sgdesc = sgp;
844 
845 	if (ifl && (shdr->sh_type == SHT_PROGBITS)) {
846 		/*
847 		 * Try to preserve the intended meaning of sh_link/sh_info.
848 		 * See the translate_link() in update.c.
849 		 */
850 		osp->os_shdr->sh_link = shdr->sh_link;
851 		if (shdr->sh_flags & SHF_INFO_LINK)
852 			osp->os_shdr->sh_info = shdr->sh_info;
853 	}
854 
855 	/*
856 	 * When -zignore is in effect, user supplied sections and files that are
857 	 * not referenced from other sections, are eliminated from the object
858 	 * being produced.  Some sections, although unreferenced, are special,
859 	 * and must not be eliminated.  Determine if this new output section is
860 	 * one of those special sections, and if so mark it artificially as
861 	 * referenced.  Any input section and file associated to this output
862 	 * section is also be marked as referenced, and thus won't be eliminated
863 	 * from the final output.
864 	 */
865 	if (ifl && ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) || DBG_ENABLED)) {
866 		const Msg	*refsec;
867 
868 		for (refsec = RefSecs; *refsec; refsec++) {
869 			if (strcmp(osp->os_name, MSG_ORIG(*refsec)) == 0) {
870 				osp->os_flags |= FLG_OS_SECTREF;
871 
872 				if ((ifl->ifl_flags & FLG_IF_IGNORE) ||
873 				    DBG_ENABLED) {
874 					isp->is_flags |= FLG_IS_SECTREF;
875 					ifl->ifl_flags |= FLG_IF_FILEREF;
876 				}
877 				break;
878 			}
879 		}
880 	}
881 
882 	/*
883 	 * Sections of type SHT_GROUP are added to the ofl->ofl_osgroups list,
884 	 * so that they can be updated as a group later.
885 	 */
886 	if ((shdr->sh_type == SHT_GROUP) &&
887 	    (aplist_append(&ofl->ofl_osgroups, osp,
888 	    AL_CNT_OFL_OSGROUPS) == NULL))
889 		return ((Os_desc *)S_ERROR);
890 
891 	/*
892 	 * If this section is a non-empty TLS section indicate that a PT_TLS
893 	 * program header is required.
894 	 */
895 	if ((shflags & SHF_TLS) && shdr->sh_size &&
896 	    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0))
897 		ofl->ofl_flags |= FLG_OF_TLSPHDR;
898 
899 	/*
900 	 * If a non-allocatable section is going to be put into a loadable
901 	 * segment then turn on the allocate bit for this section and warn the
902 	 * user that we have done so.  This could only happen through the use
903 	 * of a mapfile.
904 	 */
905 	if ((sgp->sg_phdr.p_type == PT_LOAD) &&
906 	    ((osp->os_shdr->sh_flags & SHF_ALLOC) == 0)) {
907 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_SCN_NONALLOC),
908 		    ofl->ofl_name, osp->os_name);
909 		osp->os_shdr->sh_flags |= SHF_ALLOC;
910 	}
911 
912 	/*
913 	 * Retain this sections identifier for future comparisons when placing
914 	 * a section (after all sections have been processed this variable will
915 	 * be used to hold the sections symbol index as we don't need to retain
916 	 * the identifier any more).
917 	 */
918 	osp->os_identndx = ident;
919 
920 	/*
921 	 * Set alignment.
922 	 */
923 	set_addralign(ofl, osp, isp);
924 
925 	if (ld_append_isp(ofl, osp, isp, 0) == 0)
926 		return ((Os_desc *)S_ERROR);
927 
928 	DBG_CALL(Dbg_sec_created(ofl->ofl_lml, osp, sgp));
929 	isp->is_osdesc = osp;
930 
931 	/*
932 	 * Insert the new section at the offset given by iidx.  If no position
933 	 * for it was identified above, this will be index 0, causing the new
934 	 * section to be prepended to the beginning of the section list.
935 	 * Otherwise, it is the index following the section that was identified.
936 	 */
937 	if (aplist_insert(&sgp->sg_osdescs, osp, AL_CNT_SG_OSDESC,
938 	    iidx) == NULL)
939 		return ((Os_desc *)S_ERROR);
940 	return (osp);
941 }
942