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