xref: /titanic_50/usr/src/cmd/sgs/libld/common/place.c (revision be6e67355fa2266ba427189118d91a9e67b9163f)
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 'g' and 'l' are an
518 	 * optimization to skip using strncmp() too much.
519 	 */
520 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
521 	    (isp->is_name == oname) &&
522 	    (isp->is_name[1] == 'g') && (isp->is_name[5] == 'l') &&
523 	    (strncmp(MSG_ORIG(MSG_SCN_GNU_LINKONCE), isp->is_name,
524 	    MSG_SCN_GNU_LINKONCE_SIZE) == 0)) {
525 		if ((oname =
526 		    (char *)gnu_linkonce_sec(isp->is_name)) != isp->is_name) {
527 			DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp->is_name,
528 			    oname));
529 		}
530 
531 		/*
532 		 * Explicitly identify this section type as COMDAT.  Also,
533 		 * enable lazy relocation processing, as this is typically a
534 		 * requirement with .gnu.linkonce sections.
535 		 */
536 		isp->is_flags |= FLG_IS_COMDAT;
537 		if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0)
538 			ofl->ofl_flags1 |= FLG_OF1_RLXREL;
539 		Dbg_sec_gnu_comdat(ofl->ofl_lml, isp->is_name, 1,
540 		    (ofl->ofl_flags1 & FLG_OF1_RLXREL));
541 	}
542 
543 	/*
544 	 * GNU section names may also follow the convention:
545 	 *
546 	 *	section-name.symbol-name
547 	 *
548 	 * This convention is used when defining SHT_GROUP sections of type
549 	 * COMDAT.  Thus, any group processing will have discovered any group
550 	 * sections, and this identification can be triggered by a pattern
551 	 * match section names.
552 	 */
553 	if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
554 	    (isp->is_name == oname) && (isp->is_flags & FLG_IS_COMDAT) &&
555 	    ((sname = gnu_comdat_sym(ifl, isp)) != NULL)) {
556 		size_t	size = sname - isp->is_name;
557 
558 		if ((oname = libld_malloc(size + 1)) == NULL)
559 			return ((Os_desc *)S_ERROR);
560 		(void) strncpy(oname, isp->is_name, size);
561 		oname[size] = '\0';
562 		DBG_CALL(Dbg_sec_redirected(ofl->ofl_lml, isp->is_name,
563 		    oname));
564 
565 		/*
566 		 * Enable lazy relocation processing, as this is typically a
567 		 * requirement with GNU COMDAT sections.
568 		 */
569 		if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0) {
570 			ofl->ofl_flags1 |= FLG_OF1_RLXREL;
571 			Dbg_sec_gnu_comdat(ofl->ofl_lml, isp->is_name, 0,
572 			    (ofl->ofl_flags1 & FLG_OF1_RLXREL));
573 		}
574 	}
575 
576 	/*
577 	 * Assign a hash value now that the output section name has been
578 	 * finalized.
579 	 */
580 	onamehash = sgs_str_hash(oname);
581 
582 	if (sgp->sg_flags & FLG_SG_ORDER)
583 		enp->ec_flags |= FLG_EC_USED;
584 
585 	/*
586 	 * If the link is not 0, then the input section is appended to the
587 	 * defined output section.  The append occurs at the input section
588 	 * pointed to by the link.
589 	 */
590 	if (link) {
591 		uintptr_t	err;
592 
593 		osp = isp->is_file->ifl_isdesc[link]->is_osdesc;
594 
595 		/*
596 		 * Process any COMDAT section, keeping the first and
597 		 * discarding all others.
598 		 */
599 		if ((isp->is_flags & FLG_IS_COMDAT) &&
600 		    ((err = add_comdat(ofl, osp, isp)) != 1))
601 			return ((Os_desc *)err);
602 
603 		/*
604 		 * Set alignment
605 		 */
606 		set_addralign(ofl, osp, isp);
607 
608 		if (ld_append_isp(ofl, osp, isp, 0) == 0)
609 			return ((Os_desc *)S_ERROR);
610 
611 		isp->is_osdesc = osp;
612 		sgp = osp->os_sgdesc;
613 
614 		DBG_CALL(Dbg_sec_added(ofl->ofl_lml, osp, sgp));
615 		return (osp);
616 	}
617 
618 	/*
619 	 * Determine if section ordering is turned on.  If so, return the
620 	 * appropriate os_txtndx.  This information is derived from the
621 	 * Sg_desc->sg_segorder list that was built up from the Mapfile.
622 	 */
623 	os_ndx = 0;
624 	if (sgp->sg_secorder) {
625 		Aliste		idx;
626 		Sec_order	*scop;
627 
628 		for (APLIST_TRAVERSE(sgp->sg_secorder, idx, scop)) {
629 			if (strcmp(scop->sco_secname, oname) == 0) {
630 				scop->sco_flags |= FLG_SGO_USED;
631 				os_ndx = scop->sco_index;
632 				break;
633 			}
634 		}
635 	}
636 
637 	/*
638 	 * Mask of section header flags to ignore when
639 	 * matching sections. We are more strict with
640 	 * relocatable objects, ignoring only the order
641 	 * flags, and keeping sections apart if they differ
642 	 * otherwise. This follows the policy that sections
643 	 * in a relative object should only be merged if their
644 	 * flags are the same, and avoids destroying information
645 	 * prematurely. For final products however, we ignore all
646 	 * flags that do not prevent a merge.
647 	 */
648 	shflagmask = (ofl->ofl_flags & FLG_OF_RELOBJ)
649 	    ? ALL_SHF_ORDER : ALL_SHF_IGNORE;
650 
651 	/*
652 	 * Traverse the input section list for the output section we have been
653 	 * assigned. If we find a matching section simply add this new section.
654 	 */
655 	idx2 = 0;
656 	for (APLIST_TRAVERSE(sgp->sg_osdescs, idx1, osp)) {
657 		Shdr	*_shdr = osp->os_shdr;
658 
659 		if ((ident == osp->os_scnsymndx) &&
660 		    (ident != ld_targ.t_id.id_rel) &&
661 		    (onamehash == osp->os_namehash) &&
662 		    (shdr->sh_type != SHT_GROUP) &&
663 		    (shdr->sh_type != SHT_SUNW_dof) &&
664 		    ((shdr->sh_type == _shdr->sh_type) ||
665 		    ((shdr->sh_type == SHT_SUNW_COMDAT) &&
666 		    (_shdr->sh_type == SHT_PROGBITS))) &&
667 		    ((shflags & ~shflagmask) ==
668 		    (_shdr->sh_flags & ~shflagmask)) &&
669 		    (strcmp(oname, osp->os_name) == 0)) {
670 			uintptr_t	err;
671 
672 			/*
673 			 * Process any COMDAT section, keeping the first and
674 			 * discarding all others.
675 			 */
676 			if ((isp->is_flags & FLG_IS_COMDAT) &&
677 			    ((err = add_comdat(ofl, osp, isp)) != 1))
678 				return ((Os_desc *)err);
679 
680 			/*
681 			 * Set alignment
682 			 */
683 			set_addralign(ofl, osp, isp);
684 
685 			/*
686 			 * If this section is a non-empty TLS section indicate
687 			 * that a PT_TLS program header is required.
688 			 */
689 			if ((shflags & SHF_TLS) && shdr->sh_size &&
690 			    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0))
691 				ofl->ofl_flags |= FLG_OF_TLSPHDR;
692 
693 			/*
694 			 * If is_txtndx is 0 then this section was not
695 			 * seen in mapfile, so put it at the end.
696 			 * If is_txtndx is not 0 and ?O is turned on
697 			 * then check to see where this section should
698 			 * be inserted.
699 			 */
700 			if ((sgp->sg_flags & FLG_SG_ORDER) && isp->is_txtndx) {
701 				Listnode *	tlist;
702 
703 				tlist = list_where(&(osp->os_isdescs),
704 				    isp->is_txtndx);
705 				if (tlist != NULL) {
706 					if (list_insertc(&(osp->os_isdescs),
707 					    isp, tlist) == 0)
708 						return ((Os_desc *)S_ERROR);
709 				} else {
710 					if (list_prependc(&(osp->os_isdescs),
711 					    isp) == 0)
712 						return ((Os_desc *)S_ERROR);
713 				}
714 			} else {
715 				if (list_appendc(&(osp->os_isdescs), isp) == 0)
716 					return ((Os_desc *)S_ERROR);
717 			}
718 			if (ld_append_isp(ofl, osp, isp, 1) == 0)
719 				return ((Os_desc *)S_ERROR);
720 
721 			isp->is_osdesc = osp;
722 
723 			/*
724 			 * If this input section and file is associated to an
725 			 * artificially referenced output section, make sure
726 			 * they are marked as referenced also. This insures this
727 			 * input section and file isn't eliminated when -zignore
728 			 * is in effect.
729 			 * See -zignore comments when creating a new output
730 			 * section below.
731 			 */
732 			if (((ifl &&
733 			    (ifl->ifl_flags & FLG_IF_IGNORE)) || DBG_ENABLED) &&
734 			    (osp->os_flags & FLG_OS_SECTREF)) {
735 				isp->is_flags |= FLG_IS_SECTREF;
736 				if (ifl)
737 					ifl->ifl_flags |= FLG_IF_FILEREF;
738 			}
739 
740 			DBG_CALL(Dbg_sec_added(ofl->ofl_lml, osp, sgp));
741 			return (osp);
742 		}
743 
744 		/*
745 		 * Do we need to worry about section ordering?
746 		 */
747 		if (os_ndx) {
748 			if (osp->os_txtndx) {
749 				if (os_ndx < osp->os_txtndx)
750 					/* insert section here. */
751 					break;
752 				else {
753 					idx2 = idx1 + 1;
754 					continue;
755 				}
756 			} else {
757 				/* insert section here. */
758 				break;
759 			}
760 		} else if (osp->os_txtndx) {
761 			idx2 = idx1 + 1;
762 			continue;
763 		}
764 
765 		/*
766 		 * If the new sections identifier is less than that of the
767 		 * present input section we need to insert the new section
768 		 * at this point.
769 		 */
770 		if (ident < osp->os_scnsymndx)
771 			break;
772 
773 		idx2 = idx1 + 1;
774 	}
775 
776 	/*
777 	 * We are adding a new output section.  Update the section header
778 	 * count and associated string size.
779 	 */
780 	ofl->ofl_shdrcnt++;
781 	if (st_insert(ofl->ofl_shdrsttab, oname) == -1)
782 		return ((Os_desc *)S_ERROR);
783 
784 	/*
785 	 * Create a new output section descriptor.
786 	 */
787 	if ((osp = libld_calloc(sizeof (Os_desc), 1)) == 0)
788 		return ((Os_desc *)S_ERROR);
789 	if ((osp->os_shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
790 		return ((Os_desc *)S_ERROR);
791 
792 	/*
793 	 * Convert COMDAT section to PROGBITS as this the first section of the
794 	 * output section.  Save any COMDAT section for later processing, as
795 	 * additional COMDAT sections that match this section need discarding.
796 	 */
797 	if (shdr->sh_type == SHT_SUNW_COMDAT) {
798 		Shdr	*tshdr;
799 
800 		if ((tshdr = libld_malloc(sizeof (Shdr))) == NULL)
801 			return ((Os_desc *)S_ERROR);
802 		*tshdr = *shdr;
803 		isp->is_shdr = shdr = tshdr;
804 		shdr->sh_type = SHT_PROGBITS;
805 	}
806 	if ((isp->is_flags & FLG_IS_COMDAT) &&
807 	    (aplist_append(&(osp->os_comdats), isp, AL_CNT_OS_COMDATS) == NULL))
808 		return ((Os_desc *)S_ERROR);
809 
810 	osp->os_shdr->sh_type = shdr->sh_type;
811 	osp->os_shdr->sh_flags = shdr->sh_flags;
812 	osp->os_shdr->sh_entsize = shdr->sh_entsize;
813 	osp->os_name = oname;
814 	osp->os_namehash = onamehash;
815 	osp->os_txtndx = os_ndx;
816 	osp->os_sgdesc = sgp;
817 
818 	if (ifl && (shdr->sh_type == SHT_PROGBITS)) {
819 		/*
820 		 * Try to preserve the intended meaning of sh_link/sh_info.
821 		 * See the translate_link() in update.c.
822 		 */
823 		osp->os_shdr->sh_link = shdr->sh_link;
824 		if (shdr->sh_flags & SHF_INFO_LINK)
825 			osp->os_shdr->sh_info = shdr->sh_info;
826 	}
827 
828 	/*
829 	 * When -zignore is in effect, user supplied sections and files that are
830 	 * not referenced from other sections, are eliminated from the object
831 	 * being produced.  Some sections, although unreferenced, are special,
832 	 * and must not be eliminated.  Determine if this new output section is
833 	 * one of those special sections, and if so mark it artificially as
834 	 * referenced.  Any input section and file associated to this output
835 	 * section is also be marked as referenced, and thus won't be eliminated
836 	 * from the final output.
837 	 */
838 	if (ifl && ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) || DBG_ENABLED)) {
839 		const Msg	*refsec;
840 
841 		for (refsec = RefSecs; *refsec; refsec++) {
842 			if (strcmp(osp->os_name, MSG_ORIG(*refsec)) == 0) {
843 				osp->os_flags |= FLG_OS_SECTREF;
844 
845 				if ((ifl->ifl_flags & FLG_IF_IGNORE) ||
846 				    DBG_ENABLED) {
847 					isp->is_flags |= FLG_IS_SECTREF;
848 					ifl->ifl_flags |= FLG_IF_FILEREF;
849 				}
850 				break;
851 			}
852 		}
853 	}
854 
855 	/*
856 	 * Setions of SHT_GROUP are added to the ofl->ofl_osgroups
857 	 * list - so that they can be updated as a group later.
858 	 */
859 	if (shdr->sh_type == SHT_GROUP) {
860 		if (list_appendc(&ofl->ofl_osgroups, osp) == 0)
861 			return ((Os_desc *)S_ERROR);
862 	}
863 
864 	/*
865 	 * If this section is a non-empty TLS section indicate that a PT_TLS
866 	 * program header is required.
867 	 */
868 	if ((shflags & SHF_TLS) && shdr->sh_size &&
869 	    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0))
870 		ofl->ofl_flags |= FLG_OF_TLSPHDR;
871 
872 	/*
873 	 * If a non-allocatable section is going to be put into a loadable
874 	 * segment then turn on the allocate bit for this section and warn the
875 	 * user that we have done so.  This could only happen through the use
876 	 * of a mapfile.
877 	 */
878 	if ((sgp->sg_phdr.p_type == PT_LOAD) &&
879 	    ((osp->os_shdr->sh_flags & SHF_ALLOC) == 0)) {
880 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_SCN_NONALLOC),
881 		    ofl->ofl_name, osp->os_name);
882 		osp->os_shdr->sh_flags |= SHF_ALLOC;
883 	}
884 
885 	/*
886 	 * Retain this sections identifier for future comparisons when placing
887 	 * a section (after all sections have been processed this variable will
888 	 * be used to hold the sections symbol index as we don't need to retain
889 	 * the identifier any more).
890 	 */
891 	osp->os_scnsymndx = ident;
892 
893 	/*
894 	 * Set alignment
895 	 */
896 	set_addralign(ofl, osp, isp);
897 
898 	if (ld_append_isp(ofl, osp, isp, 0) == 0)
899 		return ((Os_desc *)S_ERROR);
900 
901 	DBG_CALL(Dbg_sec_created(ofl->ofl_lml, osp, sgp));
902 	isp->is_osdesc = osp;
903 
904 	/*
905 	 * Insert the new section at the offset given by idx2. If no
906 	 * position for it was identified above, this will be index 0,
907 	 * causing the new section to be prepended to the beginning of
908 	 * the section list. Otherwise, it is the index following the section
909 	 * that was identified.
910 	 */
911 	if (aplist_insert(&sgp->sg_osdescs, osp, AL_CNT_SG_OSDESC,
912 	    idx2) == NULL)
913 		return ((Os_desc *)S_ERROR);
914 	return (osp);
915 }
916