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