xref: /titanic_41/usr/src/cmd/sgs/libld/common/sections.c (revision 22eb7cb54d8a6bcf6fe2674cb4b1f0cf2d85cfb6)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * Module sections. Initialize special sections
33  */
34 
35 #define	ELF_TARGET_AMD64
36 
37 #include	<string.h>
38 #include	<strings.h>
39 #include	<stdio.h>
40 #include	<link.h>
41 #include	<debug.h>
42 #include	"msg.h"
43 #include	"_libld.h"
44 
45 inline static void
46 remove_local(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
47 {
48 	Sym	*sym = sdp->sd_sym;
49 	uchar_t	type = ELF_ST_TYPE(sym->st_info);
50 	/* LINTED - only used for assert() */
51 	int	err;
52 
53 	if ((ofl->ofl_flags & FLG_OF_REDLSYM) == 0) {
54 		ofl->ofl_locscnt--;
55 
56 		err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
57 		assert(err != -1);
58 
59 		if (allow_ldynsym && ldynsym_symtype[type]) {
60 			ofl->ofl_dynlocscnt--;
61 
62 			err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
63 			assert(err != -1);
64 			/* Remove from sort section? */
65 			DYNSORT_COUNT(sdp, sym, type, --);
66 		}
67 	}
68 	sdp->sd_flags |= FLG_SY_ISDISC;
69 }
70 
71 inline static void
72 remove_scoped(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
73 {
74 	Sym	*sym = sdp->sd_sym;
75 	uchar_t	type = ELF_ST_TYPE(sym->st_info);
76 	/* LINTED - only used for assert() */
77 	int	err;
78 
79 	ofl->ofl_scopecnt--;
80 	ofl->ofl_elimcnt++;
81 
82 	err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
83 	assert(err != -1);
84 
85 	if (allow_ldynsym && ldynsym_symtype[type]) {
86 		ofl->ofl_dynscopecnt--;
87 
88 		err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
89 		assert(err != -1);
90 		/* Remove from sort section? */
91 		DYNSORT_COUNT(sdp, sym, type, --);
92 	}
93 	sdp->sd_flags1 |= FLG_SY1_ELIM;
94 }
95 
96 inline static void
97 ignore_sym(Ofl_desc *ofl, Ifl_desc *ifl, Sym_desc *sdp, int allow_ldynsym)
98 {
99 	Os_desc	*osp;
100 	Is_desc	*isp = sdp->sd_isc;
101 	uchar_t	bind = ELF_ST_BIND(sdp->sd_sym->st_info);
102 
103 	if (bind == STB_LOCAL) {
104 		uchar_t	type = ELF_ST_TYPE(sdp->sd_sym->st_info);
105 
106 		/*
107 		 * Skip section symbols, these were never collected in the
108 		 * first place.
109 		 */
110 		if (type == STT_SECTION)
111 			return;
112 
113 		/*
114 		 * Determine if the whole file is being removed.  Remove any
115 		 * file symbol, and any symbol that is not associated with a
116 		 * section, provided the symbol has not been identified as
117 		 * (update) required.
118 		 */
119 		if (((ifl->ifl_flags & FLG_IF_FILEREF) == 0) &&
120 		    ((type == STT_FILE) || ((isp == NULL) &&
121 		    ((sdp->sd_flags & FLG_SY_UPREQD) == 0)))) {
122 			DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
123 			if (ifl->ifl_flags & FLG_IF_IGNORE)
124 				remove_local(ofl, sdp, allow_ldynsym);
125 			return;
126 		}
127 
128 	} else {
129 		/*
130 		 * Global symbols can only be eliminated when the interfaces of
131 		 * an object have been defined via versioning/scoping.
132 		 */
133 		if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0)
134 			return;
135 
136 		/*
137 		 * Remove any unreferenced symbols that are not associated with
138 		 * a section.
139 		 */
140 		if ((isp == NULL) && ((sdp->sd_flags & FLG_SY_UPREQD) == 0)) {
141 			DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
142 			if (ifl->ifl_flags & FLG_IF_IGNORE)
143 				remove_scoped(ofl, sdp, allow_ldynsym);
144 			return;
145 		}
146 	}
147 
148 	/*
149 	 * Do not discard any symbols that are associated with non-allocable
150 	 * segments.
151 	 */
152 	if (isp && ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
153 	    ((osp = isp->is_osdesc) != 0) &&
154 	    (osp->os_sgdesc->sg_phdr.p_type == PT_LOAD)) {
155 		DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
156 		if (ifl->ifl_flags & FLG_IF_IGNORE) {
157 			if (bind == STB_LOCAL)
158 				remove_local(ofl, sdp, allow_ldynsym);
159 			else
160 				remove_scoped(ofl, sdp, allow_ldynsym);
161 		}
162 	}
163 }
164 
165 /*
166  * If -zignore has been in effect, scan all input files to determine if the
167  * file, or sections from the file, have been referenced.  If not, the file or
168  * some of the files sections can be discarded.
169  *
170  * which haven't been referenced (and hence can be discarded).  If sections are
171  * to be discarded, rescan the output relocations and the symbol table and
172  * remove the relocations and symbol entries that are no longer required.
173  *
174  * Note:  It's possible that a section which is being discarded has contributed
175  *	  to the GOT table or the PLT table.  However, we can't at this point
176  *	  eliminate the corresponding entries.  This is because there could well
177  *	  be other sections referencing those same entries, but we don't have
178  *	  the infrastructure to determine this.  So, keep the PLT and GOT
179  *	  entries in the table in case someone wants them.
180  * Note:  The section to be affected needs to be allocatable.
181  *	  So even if -zignore is in effect, if the section is not allocatable,
182  *	  we do not eliminate it.
183  */
184 static uintptr_t
185 ignore_section_processing(Ofl_desc *ofl)
186 {
187 	Listnode	*lnp;
188 	Ifl_desc	*ifl;
189 	Rel_cache	*rcp;
190 	int		allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
191 
192 	for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, ifl)) {
193 		uint_t	num, discard;
194 
195 		/*
196 		 * Diagnose (-D unused) a completely unreferenced file.
197 		 */
198 		if ((ifl->ifl_flags & FLG_IF_FILEREF) == 0)
199 			DBG_CALL(Dbg_unused_file(ofl->ofl_lml,
200 			    ifl->ifl_name, 0, 0));
201 		if (((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) ||
202 		    ((ifl->ifl_flags & FLG_IF_IGNORE) == 0))
203 			continue;
204 
205 		/*
206 		 * Before scanning the whole symbol table to determine if
207 		 * symbols should be discard - quickly (relatively) scan the
208 		 * sections to determine if any are to be discarded.
209 		 */
210 		discard = 0;
211 		if (ifl->ifl_flags & FLG_IF_FILEREF) {
212 			for (num = 1; num < ifl->ifl_shnum; num++) {
213 				Is_desc	*isp = ifl->ifl_isdesc[num];
214 				Os_desc *osp;
215 				Sg_desc	*sgp;
216 
217 				if (((isp = ifl->ifl_isdesc[num]) != 0) &&
218 				    ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
219 				    ((osp = isp->is_osdesc) != 0) &&
220 				    ((sgp = osp->os_sgdesc) != 0) &&
221 				    (sgp->sg_phdr.p_type == PT_LOAD)) {
222 					discard++;
223 					break;
224 				}
225 			}
226 		}
227 
228 		/*
229 		 * No sections are to be 'ignored'
230 		 */
231 		if ((discard == 0) && (ifl->ifl_flags & FLG_IF_FILEREF))
232 			continue;
233 
234 		/*
235 		 * We know that we have discarded sections.  Scan the symbol
236 		 * table for this file to determine if symbols need to be
237 		 * discarded that are associated with the 'ignored' sections.
238 		 */
239 		for (num = 1; num < ifl->ifl_symscnt; num++) {
240 			Sym_desc	*sdp;
241 
242 			/*
243 			 * If the symbol definition has been resolved to another
244 			 * file, or the symbol has already been discarded or
245 			 * eliminated, skip it.
246 			 */
247 			sdp = ifl->ifl_oldndx[num];
248 			if ((sdp->sd_file != ifl) ||
249 			    (sdp->sd_flags & FLG_SY_ISDISC) ||
250 			    (sdp->sd_flags1 & FLG_SY1_ELIM))
251 				continue;
252 
253 			/*
254 			 * Complete the investigation of the symbol.
255 			 */
256 			ignore_sym(ofl, ifl, sdp, allow_ldynsym);
257 		}
258 	}
259 
260 	/*
261 	 * If we were only here to solicit debugging diagnostics, we're done.
262 	 */
263 	if ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0)
264 		return (1);
265 
266 	/*
267 	 * Scan all output relocations searching for those against discarded or
268 	 * ignored sections.  If one is found, decrement the total outrel count.
269 	 */
270 	for (LIST_TRAVERSE(&ofl->ofl_outrels, lnp, rcp)) {
271 		Rel_desc	*rsp;
272 		Os_desc		*osp;
273 
274 		/* LINTED */
275 		for (rsp = (Rel_desc *)(rcp + 1); rsp < rcp->rc_free; rsp++) {
276 			Is_desc		*isc = rsp->rel_isdesc;
277 			uint_t		flags, entsize;
278 			Shdr		*shdr;
279 			Ifl_desc	*ifl;
280 
281 			if ((isc == 0) ||
282 			    ((isc->is_flags & (FLG_IS_SECTREF))) ||
283 			    ((ifl = isc->is_file) == 0) ||
284 			    ((ifl->ifl_flags & FLG_IF_IGNORE) == 0) ||
285 			    ((shdr = isc->is_shdr) == 0) ||
286 			    ((shdr->sh_flags & SHF_ALLOC) == 0))
287 				continue;
288 
289 			flags = rsp->rel_flags;
290 
291 			if (flags & (FLG_REL_GOT | FLG_REL_BSS |
292 			    FLG_REL_NOINFO | FLG_REL_PLT))
293 				continue;
294 
295 			osp = rsp->rel_osdesc;
296 
297 			if (rsp->rel_flags & FLG_REL_RELA)
298 				entsize = sizeof (Rela);
299 			else
300 				entsize = sizeof (Rel);
301 
302 			assert(osp->os_szoutrels > 0);
303 			osp->os_szoutrels -= entsize;
304 
305 			if (!(flags & FLG_REL_PLT))
306 				ofl->ofl_reloccntsub++;
307 
308 			if (rsp->rel_rtype == ld_targ.t_m.m_r_relative)
309 				ofl->ofl_relocrelcnt--;
310 		}
311 	}
312 	return (1);
313 }
314 
315 /*
316  * Allocate Elf_Data, Shdr, and Is_desc structures for a new
317  * section.
318  *
319  * entry:
320  *	ofl - Output file descriptor
321  *	shtype - SHT_ type code for section.
322  *	shname - String giving the name for the new section.
323  *	entcnt - # of items contained in the data part of the new section.
324  *		This value is multiplied against the known element size
325  *		for the section type to determine the size of the data
326  *		area for the section. It is only meaningful in cases where
327  *		the section type has a non-zero element size. In other cases,
328  *		the caller must set the size fields in the *ret_data and
329  *		*ret_shdr structs manually.
330  *	ret_isec, ret_shdr, ret_data - Address of pointers to
331  *		receive address of newly allocated structs.
332  *
333  * exit:
334  *	On error, returns S_ERROR. On success, returns (1), and the
335  *	ret_ pointers have been updated to point at the new structures,
336  *	which have been filled in. To finish the task, the caller must
337  *	update any fields within the supplied descriptors that differ
338  *	from its needs, and then call ld_place_section().
339  */
340 static uintptr_t
341 new_section(Ofl_desc *ofl, Word shtype, const char *shname, Xword entcnt,
342 	Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
343 {
344 	typedef struct sec_info {
345 		Word d_type;
346 		Word align;	/* Used in both data and section header */
347 		Word sh_flags;
348 		Word sh_entsize;
349 	} SEC_INFO_T;
350 
351 	const SEC_INFO_T	*sec_info;
352 
353 	Shdr		*shdr;
354 	Elf_Data	*data;
355 	Is_desc		*isec;
356 	size_t		size;
357 
358 	/*
359 	 * For each type of section, we have a distinct set of
360 	 * SEC_INFO_T values. This macro defines a static structure
361 	 * containing those values and generates code to set the sec_info
362 	 * pointer to refer to it. The pointer in sec_info remains valid
363 	 * outside of the declaration scope because the info_s struct is static.
364 	 *
365 	 * We can't determine the value of M_WORD_ALIGN at compile time, so
366 	 * a different variant is used for those cases.
367 	 */
368 #define	SET_SEC_INFO(d_type, d_align, sh_flags, sh_entsize) \
369 	{ \
370 		static const SEC_INFO_T info_s = { d_type, d_align, sh_flags, \
371 		    sh_entsize}; \
372 		sec_info = &info_s; \
373 	}
374 #define	SET_SEC_INFO_WORD_ALIGN(d_type, sh_flags, sh_entsize) \
375 	{ \
376 		static SEC_INFO_T info_s = { d_type, 0, sh_flags, \
377 		    sh_entsize}; \
378 		info_s.align = ld_targ.t_m.m_word_align; \
379 		sec_info = &info_s; \
380 	}
381 
382 	switch (shtype) {
383 	case SHT_PROGBITS:
384 		/*
385 		 * SHT_PROGBITS sections contain are used for many
386 		 * different sections. Alignments and flags differ.
387 		 * Some have a standard entsize, and others don't.
388 		 * We set some defaults here, but there is no expectation
389 		 * that they are correct or complete for any specific
390 		 * purpose. The caller must provide the correct values.
391 		 */
392 		SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0)
393 		break;
394 
395 	case SHT_SYMTAB:
396 		SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, 0, sizeof (Sym))
397 		break;
398 
399 	case SHT_DYNSYM:
400 	case SHT_SUNW_LDYNSYM:
401 		SET_SEC_INFO_WORD_ALIGN(ELF_T_SYM, SHF_ALLOC, sizeof (Sym))
402 		break;
403 
404 	case SHT_STRTAB:
405 		/*
406 		 * A string table may or may not be allocable, depending
407 		 * on context, so we leave that flag unset and leave it to
408 		 * the caller to add it if necessary.
409 		 *
410 		 * String tables do not have a standard entsize, so
411 		 * we set it to 0.
412 		 */
413 		SET_SEC_INFO(ELF_T_BYTE, 1, SHF_STRINGS, 0)
414 		break;
415 
416 	case SHT_RELA:
417 		/*
418 		 * Relocations with an addend (Everything except 32-bit X86).
419 		 * The caller is expected to set all section header flags.
420 		 */
421 		SET_SEC_INFO_WORD_ALIGN(ELF_T_RELA, 0, sizeof (Rela))
422 		break;
423 
424 	case SHT_REL:
425 		/*
426 		 * Relocations without an addend (32-bit X86 only).
427 		 * The caller is expected to set all section header flags.
428 		 */
429 		SET_SEC_INFO_WORD_ALIGN(ELF_T_REL, 0, sizeof (Rel))
430 		break;
431 
432 	case SHT_HASH:
433 	case SHT_SUNW_symsort:
434 	case SHT_SUNW_tlssort:
435 		SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, SHF_ALLOC, sizeof (Word))
436 		break;
437 
438 	case SHT_DYNAMIC:
439 		/*
440 		 * A dynamic section may or may not be allocable, depending
441 		 * on context, so we leave that flag unset and leave it to
442 		 * the caller to add it if necessary.
443 		 */
444 		SET_SEC_INFO_WORD_ALIGN(ELF_T_DYN, SHF_WRITE, sizeof (Dyn))
445 		break;
446 
447 	case SHT_NOBITS:
448 		/*
449 		 * SHT_NOBITS is used for BSS-type sections. The size and
450 		 * alignment depend on the specific use and must be adjusted
451 		 * by the caller.
452 		 */
453 		SET_SEC_INFO(ELF_T_BYTE, 0, SHF_ALLOC | SHF_WRITE, 0)
454 		break;
455 
456 	case SHT_INIT_ARRAY:
457 	case SHT_FINI_ARRAY:
458 	case SHT_PREINIT_ARRAY:
459 		SET_SEC_INFO(ELF_T_ADDR, sizeof (Addr), SHF_ALLOC | SHF_WRITE,
460 		    sizeof (Addr))
461 		break;
462 
463 	case SHT_SYMTAB_SHNDX:
464 		/*
465 		 * Note that these sections are created to be associated
466 		 * with both symtab and dynsym symbol tables. However, they
467 		 * are non-allocable in all cases, because the runtime
468 		 * linker has no need for this information. It is purely
469 		 * informational, used by elfdump(1), debuggers, etc.
470 		 */
471 		SET_SEC_INFO_WORD_ALIGN(ELF_T_WORD, 0, sizeof (Word));
472 		break;
473 
474 	case SHT_SUNW_cap:
475 		SET_SEC_INFO_WORD_ALIGN(ELF_T_CAP, SHF_ALLOC, sizeof (Cap));
476 		break;
477 
478 	case SHT_SUNW_move:
479 		/*
480 		 * The sh_info field of the SHT_*_syminfo section points
481 		 * to the header index of the associated .dynamic section,
482 		 * so we also set SHF_INFO_LINK.
483 		 */
484 		SET_SEC_INFO(ELF_T_BYTE, sizeof (Lword),
485 		    SHF_ALLOC | SHF_WRITE, sizeof (Move));
486 		break;
487 
488 	case SHT_SUNW_syminfo:
489 		/*
490 		 * The sh_info field of the SHT_*_syminfo section points
491 		 * to the header index of the associated .dynamic section,
492 		 * so we also set SHF_INFO_LINK.
493 		 */
494 		SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE,
495 		    SHF_ALLOC | SHF_INFO_LINK, sizeof (Syminfo));
496 		break;
497 
498 	case SHT_SUNW_verneed:
499 	case SHT_SUNW_verdef:
500 		/*
501 		 * The info for verneed and versym happen to be the same.
502 		 * The entries in these sections are not of uniform size,
503 		 * so we set the entsize to 0.
504 		 */
505 		SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC, 0);
506 		break;
507 
508 	case SHT_SUNW_versym:
509 		SET_SEC_INFO_WORD_ALIGN(ELF_T_BYTE, SHF_ALLOC,
510 		    sizeof (Versym));
511 		break;
512 
513 	default:
514 		/* Should not happen: fcn called with unknown section type */
515 		assert(0);
516 		return (S_ERROR);
517 	}
518 #undef	SET_SEC_INFO
519 #undef	SET_SEC_INFO_WORD_ALIGN
520 
521 	size = entcnt * sec_info->sh_entsize;
522 
523 	/*
524 	 * Allocate and initialize the Elf_Data structure.
525 	 */
526 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
527 		return (S_ERROR);
528 	data->d_type = sec_info->d_type;
529 	data->d_size = size;
530 	data->d_align = sec_info->align;
531 	data->d_version = ofl->ofl_dehdr->e_version;
532 
533 	/*
534 	 * Allocate and initialize the Shdr structure.
535 	 */
536 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
537 		return (S_ERROR);
538 	shdr->sh_type = shtype;
539 	shdr->sh_size = size;
540 	shdr->sh_flags = sec_info->sh_flags;
541 	shdr->sh_addralign = sec_info->align;
542 	shdr->sh_entsize = sec_info->sh_entsize;
543 
544 	/*
545 	 * Allocate and initialize the Is_desc structure.
546 	 */
547 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
548 		return (S_ERROR);
549 	isec->is_name = shname;
550 	isec->is_shdr = shdr;
551 	isec->is_indata = data;
552 
553 
554 	*ret_isec = isec;
555 	*ret_shdr = shdr;
556 	*ret_data = data;
557 	return (1);
558 }
559 
560 /*
561  * Use an existing input section as a template to create a new
562  * input section with the same values as the original, other than
563  * the size of the data area which is supplied by the caller.
564  *
565  * entry:
566  *	ofl - Output file descriptor
567  *	ifl - Input file section to use as a template
568  *	size - Size of data area for new section
569  *	ret_isec, ret_shdr, ret_data - Address of pointers to
570  *		receive address of newly allocated structs.
571  *
572  * exit:
573  *	On error, returns S_ERROR. On success, returns (1), and the
574  *	ret_ pointers have been updated to point at the new structures,
575  *	which have been filled in. To finish the task, the caller must
576  *	update any fields within the supplied descriptors that differ
577  *	from its needs, and then call ld_place_section().
578  */
579 static uintptr_t
580 new_section_from_template(Ofl_desc *ofl, Is_desc *tmpl_isp, size_t size,
581 	Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
582 {
583 	Shdr		*shdr;
584 	Elf_Data	*data;
585 	Is_desc		*isec;
586 
587 	/*
588 	 * Allocate and initialize the Elf_Data structure.
589 	 */
590 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
591 		return (S_ERROR);
592 	data->d_type = tmpl_isp->is_indata->d_type;
593 	data->d_size = size;
594 	data->d_align = tmpl_isp->is_shdr->sh_addralign;
595 	data->d_version = ofl->ofl_dehdr->e_version;
596 
597 	/*
598 	 * Allocate and initialize the Shdr structure.
599 	 */
600 	if ((shdr = libld_malloc(sizeof (Shdr))) == 0)
601 		return (S_ERROR);
602 	*shdr = *tmpl_isp->is_shdr;
603 	shdr->sh_addr = 0;
604 	shdr->sh_offset = 0;
605 	shdr->sh_size = size;
606 
607 	/*
608 	 * Allocate and initialize the Is_desc structure.
609 	 */
610 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
611 		return (S_ERROR);
612 	isec->is_name = tmpl_isp->is_name;
613 	isec->is_shdr = shdr;
614 	isec->is_indata = data;
615 
616 
617 	*ret_isec = isec;
618 	*ret_shdr = shdr;
619 	*ret_data = data;
620 	return (1);
621 }
622 
623 /*
624  * Build a .bss section for allocation of tentative definitions.  Any `static'
625  * .bss definitions would have been associated to their own .bss sections and
626  * thus collected from the input files.  `global' .bss definitions are tagged
627  * as COMMON and do not cause any associated .bss section elements to be
628  * generated.  Here we add up all these COMMON symbols and generate the .bss
629  * section required to represent them.
630  */
631 uintptr_t
632 ld_make_bss(Ofl_desc *ofl, Xword size, Xword align, Bss_Type which)
633 {
634 	Shdr		*shdr;
635 	Elf_Data	*data;
636 	Is_desc		*isec;
637 	Os_desc		*osp;
638 	uint_t		ident;
639 	Xword		rsize = (Xword)ofl->ofl_relocbsssz;
640 
641 	/*
642 	 * Allocate header structs. We will set the name ourselves below,
643 	 * and there is no entcnt for a BSS. So, the shname and entcnt
644 	 * arguments are 0.
645 	 */
646 	if (new_section(ofl, SHT_NOBITS, NULL, 0,
647 	    &isec, &shdr, &data) == S_ERROR)
648 		return (S_ERROR);
649 
650 	data->d_size = (size_t)size;
651 	data->d_align = (size_t)align;
652 
653 	shdr->sh_size = size;
654 	shdr->sh_addralign = align;
655 
656 	if (which == MAKE_TLS) {
657 		isec->is_name = MSG_ORIG(MSG_SCN_TBSS);
658 		ident = ld_targ.t_id.id_tlsbss;
659 		ofl->ofl_istlsbss = isec;
660 		shdr->sh_flags |= SHF_TLS;
661 
662 	} else if (which == MAKE_BSS) {
663 		isec->is_name = MSG_ORIG(MSG_SCN_BSS);
664 		ofl->ofl_isbss = isec;
665 		ident = ld_targ.t_id.id_bss;
666 
667 #if	defined(_ELF64)
668 	} else if ((ld_targ.t_m.m_mach == EM_AMD64) && (which == MAKE_LBSS)) {
669 		isec->is_name = MSG_ORIG(MSG_SCN_LBSS);
670 		ofl->ofl_islbss = isec;
671 		ident = ld_targ.t_id.id_lbss;
672 		shdr->sh_flags |= SHF_AMD64_LARGE;
673 #endif
674 	}
675 
676 	/*
677 	 * Retain this .bss input section as this will be where global
678 	 * symbol references are added.
679 	 */
680 	if ((osp = ld_place_section(ofl, isec, ident, 0)) == (Os_desc *)S_ERROR)
681 		return (S_ERROR);
682 
683 	/*
684 	 * If relocations exist against .*bss section, a
685 	 * section symbol must be created for the section in
686 	 * the .dynsym symbol table.
687 	 */
688 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
689 		ofl_flag_t	flagtotest;
690 		if (which == MAKE_TLS)
691 			flagtotest = FLG_OF1_TLSOREL;
692 		else
693 			flagtotest = FLG_OF1_BSSOREL;
694 
695 		if (ofl->ofl_flags1 & flagtotest) {
696 			ofl->ofl_dynshdrcnt++;
697 			osp->os_flags |= FLG_OS_OUTREL;
698 		}
699 	}
700 
701 	osp->os_szoutrels = rsize;
702 
703 	return (1);
704 }
705 
706 
707 /*
708  * Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via
709  * ld -z *array=name
710  */
711 static uintptr_t
712 make_array(Ofl_desc *ofl, Word shtype, const char *sectname, List *list)
713 {
714 	uint_t		entcount;
715 	Listnode	*lnp;
716 	Elf_Data	*data;
717 	Is_desc		*isec;
718 	Shdr		*shdr;
719 	Sym_desc	*sdp;
720 	Rel_desc	reld;
721 	Rela		reloc;
722 	Os_desc		*osp;
723 	uintptr_t	ret = 1;
724 
725 	if (list->head == NULL)
726 		return (1);
727 
728 	entcount = 0;
729 	for (LIST_TRAVERSE(list, lnp, sdp))
730 		entcount++;
731 
732 	if (new_section(ofl, shtype, sectname, entcount, &isec, &shdr, &data) ==
733 	    S_ERROR)
734 		return (S_ERROR);
735 
736 	if ((data->d_buf = libld_calloc(sizeof (Addr), entcount)) == 0)
737 		return (S_ERROR);
738 
739 	if (ld_place_section(ofl, isec, ld_targ.t_id.id_array, 0) ==
740 	    (Os_desc *)S_ERROR)
741 		return (S_ERROR);
742 
743 	osp = isec->is_osdesc;
744 
745 	if ((ofl->ofl_osinitarray == 0) && (shtype == SHT_INIT_ARRAY))
746 		ofl->ofl_osinitarray = osp;
747 	if ((ofl->ofl_ospreinitarray == 0) && (shtype == SHT_PREINIT_ARRAY))
748 		ofl->ofl_ospreinitarray = osp;
749 	else if ((ofl->ofl_osfiniarray == 0) && (shtype == SHT_FINI_ARRAY))
750 		ofl->ofl_osfiniarray = osp;
751 
752 	/*
753 	 * Create relocations against this section to initialize it to the
754 	 * function addresses.
755 	 */
756 	reld.rel_osdesc = osp;
757 	reld.rel_isdesc = isec;
758 	reld.rel_move = 0;
759 	reld.rel_flags = FLG_REL_LOAD;
760 
761 	/*
762 	 * Fabricate the relocation information (as if a relocation record had
763 	 * been input - see init_rel()).
764 	 */
765 	reld.rel_rtype = ld_targ.t_m.m_r_arrayaddr;
766 	reld.rel_roffset = 0;
767 	reld.rel_raddend = 0;
768 	reld.rel_typedata = 0;
769 
770 	/*
771 	 * Create a minimal relocation record to satisfy process_sym_reloc()
772 	 * debugging requirements.
773 	 */
774 	reloc.r_offset = 0;
775 	reloc.r_info = ELF_R_INFO(0, ld_targ.t_m.m_r_arrayaddr);
776 	reloc.r_addend = 0;
777 
778 	DBG_CALL(Dbg_reloc_generate(ofl->ofl_lml, osp,
779 	    ld_targ.t_m.m_rel_sht_type));
780 	for (LIST_TRAVERSE(list, lnp, sdp)) {
781 		reld.rel_sname = sdp->sd_name;
782 		reld.rel_sym = sdp;
783 
784 		if (ld_process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec,
785 		    MSG_INTL(MSG_STR_COMMAND)) == S_ERROR) {
786 			ret = S_ERROR;
787 			continue;
788 		}
789 
790 		reld.rel_roffset += (Xword)sizeof (Addr);
791 		reloc.r_offset = reld.rel_roffset;
792 	}
793 
794 	return (ret);
795 }
796 
797 /*
798  * Build a comment section (-Qy option).
799  */
800 static uintptr_t
801 make_comment(Ofl_desc *ofl)
802 {
803 	Shdr		*shdr;
804 	Elf_Data	*data;
805 	Is_desc		*isec;
806 
807 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_COMMENT), 0,
808 	    &isec, &shdr, &data) == S_ERROR)
809 		return (S_ERROR);
810 
811 	data->d_buf = (void *)ofl->ofl_sgsid;
812 	data->d_size = strlen(ofl->ofl_sgsid) + 1;
813 	data->d_align = 1;
814 
815 	shdr->sh_size = (Xword)data->d_size;
816 	shdr->sh_flags = 0;
817 	shdr->sh_addralign = 1;
818 
819 	return ((uintptr_t)ld_place_section(ofl, isec,
820 	    ld_targ.t_id.id_note, 0));
821 }
822 
823 /*
824  * Make the dynamic section.  Calculate the size of any strings referenced
825  * within this structure, they will be added to the global string table
826  * (.dynstr).  This routine should be called before make_dynstr().
827  *
828  * This routine must be maintained in parallel with update_odynamic()
829  * in update.c
830  */
831 static uintptr_t
832 make_dynamic(Ofl_desc *ofl)
833 {
834 	Shdr		*shdr;
835 	Os_desc		*osp;
836 	Elf_Data	*data;
837 	Is_desc		*isec;
838 	size_t		cnt = 0;
839 	Listnode	*lnp;
840 	Ifl_desc	*ifl;
841 	Sym_desc	*sdp;
842 	size_t		size;
843 	ofl_flag_t	flags = ofl->ofl_flags;
844 	int		not_relobj = !(flags & FLG_OF_RELOBJ);
845 	int		unused = 0;
846 
847 	/*
848 	 * Only a limited subset of DT_ entries apply to relocatable
849 	 * objects. See the comment at the head of update_odynamic() in
850 	 * update.c for details.
851 	 */
852 
853 	if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0,
854 	    &isec, &shdr, &data) == S_ERROR)
855 		return (S_ERROR);
856 
857 	/* new_section() does not set SHF_ALLOC. Add it if needed */
858 	if (not_relobj)
859 		shdr->sh_flags |= SHF_ALLOC;
860 
861 	osp = ofl->ofl_osdynamic =
862 	    ld_place_section(ofl, isec, ld_targ.t_id.id_dynamic, 0);
863 
864 	/*
865 	 * Reserve entries for any needed dependencies.
866 	 */
867 	for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, ifl)) {
868 		Sdf_desc *	sdf;
869 
870 		if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR)))
871 			continue;
872 
873 		/*
874 		 * If this dependency didn't satisfy any symbol references,
875 		 * generate a debugging diagnostic (ld(1) -Dunused can be used
876 		 * to display these).  If this is a standard needed dependency,
877 		 * and -z ignore is in effect, drop the dependency.  Explicitly
878 		 * defined dependencies (i.e., -N dep) don't get dropped, and
879 		 * are flagged as being required to simplify update_odynamic()
880 		 * processing.
881 		 */
882 		if ((ifl->ifl_flags & FLG_IF_NEEDSTR) ||
883 		    ((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) {
884 			if (unused++ == 0)
885 				DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
886 			DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname,
887 			    (ifl->ifl_flags & FLG_IF_NEEDSTR), 0));
888 
889 			if (ifl->ifl_flags & FLG_IF_NEEDSTR)
890 				ifl->ifl_flags |= FLG_IF_DEPREQD;
891 			else if (ifl->ifl_flags & FLG_IF_IGNORE)
892 				continue;
893 		}
894 
895 		/*
896 		 * If this object has an accompanying shared object definition
897 		 * determine if an alternative shared object name has been
898 		 * specified.
899 		 */
900 		if (((sdf = ifl->ifl_sdfdesc) != 0) &&
901 		    (sdf->sdf_flags & FLG_SDF_SONAME))
902 			ifl->ifl_soname = sdf->sdf_soname;
903 
904 		/*
905 		 * If this object is a lazyload reserve a DT_POSFLAG_1 entry.
906 		 */
907 		if ((ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_GRPPRM)) &&
908 		    not_relobj)
909 			cnt++;
910 
911 		if (st_insert(ofl->ofl_dynstrtab, ifl->ifl_soname) == -1)
912 			return (S_ERROR);
913 		cnt++;
914 
915 		/*
916 		 * If the needed entry contains the $ORIGIN token make sure
917 		 * the associated DT_1_FLAGS entry is created.
918 		 */
919 		if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) {
920 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
921 			ofl->ofl_dtflags |= DF_ORIGIN;
922 		}
923 	}
924 
925 	if (unused)
926 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
927 
928 	if (not_relobj) {
929 		/*
930 		 * Reserve entries for any per-symbol auxiliary/filter strings.
931 		 */
932 		cnt += alist_nitems(ofl->ofl_dtsfltrs);
933 
934 		/*
935 		 * Reserve entries for _init() and _fini() section addresses.
936 		 */
937 		if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
938 		    SYM_NOHASH, 0, ofl)) != NULL) &&
939 		    (sdp->sd_ref == REF_REL_NEED) &&
940 		    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
941 			sdp->sd_flags |= FLG_SY_UPREQD;
942 			cnt++;
943 		}
944 		if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
945 		    SYM_NOHASH, 0, ofl)) != NULL) &&
946 		    (sdp->sd_ref == REF_REL_NEED) &&
947 		    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
948 			sdp->sd_flags |= FLG_SY_UPREQD;
949 			cnt++;
950 		}
951 
952 		/*
953 		 * Reserve entries for any soname, filter name (shared libs
954 		 * only), run-path pointers, cache names and audit requirements.
955 		 */
956 		if (ofl->ofl_soname) {
957 			cnt++;
958 			if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_soname) ==
959 			    -1)
960 				return (S_ERROR);
961 		}
962 		if (ofl->ofl_filtees) {
963 			cnt++;
964 			if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_filtees) ==
965 			    -1)
966 				return (S_ERROR);
967 
968 			/*
969 			 * If the filtees entry contains the $ORIGIN token
970 			 * make sure the associated DT_1_FLAGS entry is created.
971 			 */
972 			if (strstr(ofl->ofl_filtees,
973 			    MSG_ORIG(MSG_STR_ORIGIN))) {
974 				ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
975 				ofl->ofl_dtflags |= DF_ORIGIN;
976 			}
977 		}
978 	}
979 
980 	if (ofl->ofl_rpath) {
981 		cnt += 2;	/* DT_RPATH & DT_RUNPATH */
982 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_rpath) == -1)
983 			return (S_ERROR);
984 
985 		/*
986 		 * If the rpath entry contains the $ORIGIN token make sure
987 		 * the associated DT_1_FLAGS entry is created.
988 		 */
989 		if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) {
990 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
991 			ofl->ofl_dtflags |= DF_ORIGIN;
992 		}
993 	}
994 
995 	if (not_relobj) {
996 		if (ofl->ofl_config) {
997 			cnt++;
998 			if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_config) ==
999 			    -1)
1000 				return (S_ERROR);
1001 
1002 			/*
1003 			 * If the config entry contains the $ORIGIN token
1004 			 * make sure the associated DT_1_FLAGS entry is created.
1005 			 */
1006 			if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) {
1007 				ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1008 				ofl->ofl_dtflags |= DF_ORIGIN;
1009 			}
1010 		}
1011 		if (ofl->ofl_depaudit) {
1012 			cnt++;
1013 			if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_depaudit) ==
1014 			    -1)
1015 				return (S_ERROR);
1016 		}
1017 		if (ofl->ofl_audit) {
1018 			cnt++;
1019 			if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_audit) == -1)
1020 				return (S_ERROR);
1021 		}
1022 
1023 		/*
1024 		 * Reserve entries for the HASH, STRTAB, STRSZ, SYMTAB, SYMENT,
1025 		 * and CHECKSUM.
1026 		 */
1027 		cnt += 6;
1028 
1029 		/*
1030 		 * If we are including local functions at the head of
1031 		 * the dynsym, then also reserve entries for DT_SUNW_SYMTAB
1032 		 * and DT_SUNW_SYMSZ.
1033 		 */
1034 		if (OFL_ALLOW_LDYNSYM(ofl))
1035 			cnt += 2;
1036 
1037 		if ((ofl->ofl_dynsymsortcnt > 0) ||
1038 		    (ofl->ofl_dyntlssortcnt > 0))
1039 			cnt++;		/* DT_SUNW_SORTENT */
1040 
1041 		if (ofl->ofl_dynsymsortcnt > 0)
1042 			cnt += 2;	/* DT_SUNW_[SYMSORT|SYMSORTSZ] */
1043 
1044 		if (ofl->ofl_dyntlssortcnt > 0)
1045 			cnt += 2;	/* DT_SUNW_[TLSSORT|TLSSORTSZ] */
1046 
1047 		if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
1048 		    FLG_OF_VERDEF)
1049 			cnt += 2;		/* DT_VERDEF & DT_VERDEFNUM */
1050 
1051 		if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
1052 		    FLG_OF_VERNEED)
1053 			cnt += 2;		/* DT_VERNEED & DT_VERNEEDNUM */
1054 
1055 		if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt)
1056 			cnt++;			/* RELACOUNT */
1057 
1058 		if (flags & FLG_OF_TEXTREL)	/* TEXTREL */
1059 			cnt++;
1060 
1061 		if (ofl->ofl_osfiniarray)	/* FINI_ARRAY & FINI_ARRAYSZ */
1062 			cnt += 2;
1063 
1064 		if (ofl->ofl_osinitarray)	/* INIT_ARRAY & INIT_ARRAYSZ */
1065 			cnt += 2;
1066 
1067 		if (ofl->ofl_ospreinitarray)	/* PREINIT_ARRAY & */
1068 			cnt += 2;		/*	PREINIT_ARRAYSZ */
1069 
1070 		/*
1071 		 * If we have plt's reserve a PLT, PLTSZ, PLTREL and JMPREL.
1072 		 */
1073 		if (ofl->ofl_pltcnt)
1074 			cnt += 3;
1075 
1076 		/*
1077 		 * If pltpadding is needed (Sparcv9)
1078 		 */
1079 		if (ofl->ofl_pltpad)
1080 			cnt += 2;		/* DT_PLTPAD & DT_PLTPADSZ */
1081 
1082 		/*
1083 		 * If we have any relocations reserve a REL, RELSZ and
1084 		 * RELENT entry.
1085 		 */
1086 		if (ofl->ofl_relocsz)
1087 			cnt += 3;
1088 
1089 		/*
1090 		 * If a syminfo section is required create SYMINFO, SYMINSZ,
1091 		 * and SYMINENT entries.
1092 		 */
1093 		if (flags & FLG_OF_SYMINFO)
1094 			cnt += 3;
1095 
1096 		/*
1097 		 * If there are any partially initialized sections allocate
1098 		 * MOVEENT, MOVESZ and MOVETAB.
1099 		 */
1100 		if (ofl->ofl_osmove)
1101 			cnt += 3;
1102 
1103 		/*
1104 		 * Allocate one DT_REGISTER entry for every register symbol.
1105 		 */
1106 		cnt += ofl->ofl_regsymcnt;
1107 
1108 		/*
1109 		 * Reserve a entry for each '-zrtldinfo=...' specified
1110 		 * on the command line.
1111 		 */
1112 		for (LIST_TRAVERSE(&ofl->ofl_rtldinfo, lnp, sdp))
1113 			cnt++;
1114 
1115 		/*
1116 		 * These two entries should only be placed in a segment
1117 		 * which is writable.  If it's a read-only segment
1118 		 * (due to mapfile magic, e.g. libdl.so.1) then don't allocate
1119 		 * these entries.
1120 		 */
1121 		if ((osp->os_sgdesc) &&
1122 		    (osp->os_sgdesc->sg_phdr.p_flags & PF_W)) {
1123 			cnt++;			/* FEATURE_1 */
1124 
1125 			if (ofl->ofl_osinterp)
1126 				cnt++;		/* DEBUG */
1127 		}
1128 
1129 		/*
1130 		 * Any hardware/software capabilities?
1131 		 */
1132 		if (ofl->ofl_oscap)
1133 			cnt++;			/* SUNW_CAP */
1134 
1135 		if (flags & FLG_OF_SYMBOLIC)
1136 			cnt++;			/* SYMBOLIC */
1137 	}
1138 
1139 	/*
1140 	 * Account for Architecture dependent .dynamic entries, and defaults.
1141 	 */
1142 	(*ld_targ.t_mr.mr_mach_make_dynamic)(ofl, &cnt);
1143 
1144 	/*
1145 	 * DT_FLAGS, DT_FLAGS_1, DT_SUNW_STRPAD, and DT_NULL. Also,
1146 	 * allow room for the unused extra DT_NULLs. These are included
1147 	 * to allow an ELF editor room to add items later.
1148 	 */
1149 	cnt += 4 + DYNAMIC_EXTRA_ELTS;
1150 
1151 	/*
1152 	 * DT_SUNW_LDMACH. Used to hold the ELF machine code of the
1153 	 * linker that produced the output object. This information
1154 	 * allows us to determine whether a given object was linked
1155 	 * natively, or by a linker running on a different type of
1156 	 * system. This information can be valuable if one suspects
1157 	 * that a problem might be due to alignment or byte order issues.
1158 	 */
1159 	cnt++;
1160 
1161 	/*
1162 	 * Determine the size of the section from the number of entries.
1163 	 */
1164 	size = cnt * (size_t)shdr->sh_entsize;
1165 
1166 	shdr->sh_size = (Xword)size;
1167 	data->d_size = size;
1168 
1169 	return ((uintptr_t)ofl->ofl_osdynamic);
1170 }
1171 
1172 /*
1173  * Build the GOT section and its associated relocation entries.
1174  */
1175 uintptr_t
1176 ld_make_got(Ofl_desc *ofl)
1177 {
1178 	Elf_Data	*data;
1179 	Shdr	*shdr;
1180 	Is_desc	*isec;
1181 	size_t	size = (size_t)ofl->ofl_gotcnt * ld_targ.t_m.m_got_entsize;
1182 	size_t	rsize = (size_t)ofl->ofl_relocgotsz;
1183 
1184 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0,
1185 	    &isec, &shdr, &data) == S_ERROR)
1186 		return (S_ERROR);
1187 
1188 	data->d_size = size;
1189 
1190 	shdr->sh_flags |= SHF_WRITE;
1191 	shdr->sh_size = (Xword)size;
1192 	shdr->sh_entsize = ld_targ.t_m.m_got_entsize;
1193 
1194 	ofl->ofl_osgot = ld_place_section(ofl, isec, ld_targ.t_id.id_got, 0);
1195 	if (ofl->ofl_osgot == (Os_desc *)S_ERROR)
1196 		return (S_ERROR);
1197 
1198 	ofl->ofl_osgot->os_szoutrels = (Xword)rsize;
1199 
1200 	return (1);
1201 }
1202 
1203 /*
1204  * Build an interpreter section.
1205  */
1206 static uintptr_t
1207 make_interp(Ofl_desc *ofl)
1208 {
1209 	Shdr		*shdr;
1210 	Elf_Data	*data;
1211 	Is_desc		*isec;
1212 	const char	*iname = ofl->ofl_interp;
1213 	size_t		size;
1214 
1215 	/*
1216 	 * If -z nointerp is in effect, don't create an interpreter section.
1217 	 */
1218 	if (ofl->ofl_flags1 & FLG_OF1_NOINTRP)
1219 		return (1);
1220 
1221 	/*
1222 	 * We always build an .interp section for dynamic executables.  However
1223 	 * if the user has specifically specified an interpreter we'll build
1224 	 * this section for any output (presumably the user knows what they are
1225 	 * doing. refer ABI section 5-4, and ld.1 man page use of -I).
1226 	 */
1227 	if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC |
1228 	    FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname)
1229 		return (1);
1230 
1231 	/*
1232 	 * In the case of a dynamic executable supply a default interpreter
1233 	 * if a specific interpreter has not been specified.
1234 	 */
1235 	if (iname == NULL)
1236 		iname = ofl->ofl_interp = ld_targ.t_m.m_def_interp;
1237 
1238 	size = strlen(iname) + 1;
1239 
1240 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0,
1241 	    &isec, &shdr, &data) == S_ERROR)
1242 		return (S_ERROR);
1243 
1244 	data->d_size = size;
1245 	shdr->sh_size = (Xword)size;
1246 	data->d_align = shdr->sh_addralign = 1;
1247 
1248 	ofl->ofl_osinterp =
1249 	    ld_place_section(ofl, isec, ld_targ.t_id.id_interp, 0);
1250 	return ((uintptr_t)ofl->ofl_osinterp);
1251 }
1252 
1253 /*
1254  * Build a hardware/software capabilities section.
1255  */
1256 static uintptr_t
1257 make_cap(Ofl_desc *ofl)
1258 {
1259 	Shdr		*shdr;
1260 	Elf_Data	*data;
1261 	Is_desc		*isec;
1262 	Os_desc		*osec;
1263 	Cap		*cap;
1264 	size_t		size = 0;
1265 
1266 	/*
1267 	 * Determine how many entries are required.
1268 	 */
1269 	if (ofl->ofl_hwcap_1)
1270 		size++;
1271 	if (ofl->ofl_sfcap_1)
1272 		size++;
1273 	if (size == 0)
1274 		return (1);
1275 	size++;				/* Add CA_SUNW_NULL */
1276 
1277 	if (new_section(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP), size,
1278 	    &isec, &shdr, &data) == S_ERROR)
1279 		return (S_ERROR);
1280 
1281 	if ((data->d_buf = libld_malloc(shdr->sh_size)) == 0)
1282 		return (S_ERROR);
1283 
1284 	cap = (Cap *)data->d_buf;
1285 	if (ofl->ofl_hwcap_1) {
1286 		cap->c_tag = CA_SUNW_HW_1;
1287 		cap->c_un.c_val = ofl->ofl_hwcap_1;
1288 		cap++;
1289 	}
1290 	if (ofl->ofl_sfcap_1) {
1291 		cap->c_tag = CA_SUNW_SF_1;
1292 		cap->c_un.c_val = ofl->ofl_sfcap_1;
1293 		cap++;
1294 	}
1295 	cap->c_tag = CA_SUNW_NULL;
1296 	cap->c_un.c_val = 0;
1297 
1298 	/*
1299 	 * If we're not creating a relocatable object, save the output section
1300 	 * to trigger the creation of an associated program header.
1301 	 */
1302 	osec = ld_place_section(ofl, isec, ld_targ.t_id.id_cap, 0);
1303 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
1304 		ofl->ofl_oscap = osec;
1305 
1306 	return ((uintptr_t)osec);
1307 }
1308 
1309 /*
1310  * Build the PLT section and its associated relocation entries.
1311  */
1312 static uintptr_t
1313 make_plt(Ofl_desc *ofl)
1314 {
1315 	Shdr		*shdr;
1316 	Elf_Data	*data;
1317 	Is_desc		*isec;
1318 	size_t		size = ld_targ.t_m.m_plt_reservsz +
1319 	    (((size_t)ofl->ofl_pltcnt + (size_t)ofl->ofl_pltpad) *
1320 	    ld_targ.t_m.m_plt_entsize);
1321 	size_t		rsize = (size_t)ofl->ofl_relocpltsz;
1322 
1323 	/*
1324 	 * On sparc, account for the NOP at the end of the plt.
1325 	 */
1326 	if (ld_targ.t_m.m_mach == LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
1327 		size += sizeof (Word);
1328 
1329 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0,
1330 	    &isec, &shdr, &data) == S_ERROR)
1331 		return (S_ERROR);
1332 
1333 	data->d_size = size;
1334 	data->d_align = ld_targ.t_m.m_plt_align;
1335 
1336 	shdr->sh_flags = ld_targ.t_m.m_plt_shf_flags;
1337 	shdr->sh_size = (Xword)size;
1338 	shdr->sh_addralign = ld_targ.t_m.m_plt_align;
1339 	shdr->sh_entsize = ld_targ.t_m.m_plt_entsize;
1340 
1341 	ofl->ofl_osplt = ld_place_section(ofl, isec, ld_targ.t_id.id_plt, 0);
1342 	if (ofl->ofl_osplt == (Os_desc *)S_ERROR)
1343 		return (S_ERROR);
1344 
1345 	ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1346 
1347 	return (1);
1348 }
1349 
1350 /*
1351  * Make the hash table.  Only built for dynamic executables and shared
1352  * libraries, and provides hashed lookup into the global symbol table
1353  * (.dynsym) for the run-time linker to resolve symbol lookups.
1354  */
1355 static uintptr_t
1356 make_hash(Ofl_desc *ofl)
1357 {
1358 	Shdr		*shdr;
1359 	Elf_Data	*data;
1360 	Is_desc		*isec;
1361 	size_t		size;
1362 	Word		nsyms = ofl->ofl_globcnt;
1363 	size_t		cnt;
1364 
1365 	/*
1366 	 * Allocate section header structures. We set entcnt to 0
1367 	 * because it's going to change after we place this section.
1368 	 */
1369 	if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0,
1370 	    &isec, &shdr, &data) == S_ERROR)
1371 		return (S_ERROR);
1372 
1373 	/*
1374 	 * Place the section first since it will affect the local symbol
1375 	 * count.
1376 	 */
1377 	ofl->ofl_oshash = ld_place_section(ofl, isec, ld_targ.t_id.id_hash, 0);
1378 	if (ofl->ofl_oshash == (Os_desc *)S_ERROR)
1379 		return (S_ERROR);
1380 
1381 	/*
1382 	 * Calculate the number of output hash buckets.
1383 	 */
1384 	ofl->ofl_hashbkts = findprime(nsyms);
1385 
1386 	/*
1387 	 * The size of the hash table is determined by
1388 	 *
1389 	 *	i.	the initial nbucket and nchain entries (2)
1390 	 *	ii.	the number of buckets (calculated above)
1391 	 *	iii.	the number of chains (this is based on the number of
1392 	 *		symbols in the .dynsym array + NULL symbol).
1393 	 */
1394 	cnt = 2 + ofl->ofl_hashbkts + (ofl->ofl_dynshdrcnt +
1395 	    ofl->ofl_globcnt + ofl->ofl_lregsymcnt + 1);
1396 	size = cnt * shdr->sh_entsize;
1397 
1398 	/*
1399 	 * Finalize the section header and data buffer initialization.
1400 	 */
1401 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
1402 		return (S_ERROR);
1403 	data->d_size = size;
1404 	shdr->sh_size = (Xword)size;
1405 
1406 	return (1);
1407 }
1408 
1409 /*
1410  * Generate the standard symbol table.  Contains all locals and globals,
1411  * and resides in a non-allocatable section (ie. it can be stripped).
1412  */
1413 static uintptr_t
1414 make_symtab(Ofl_desc *ofl)
1415 {
1416 	Shdr		*shdr;
1417 	Elf_Data	*data;
1418 	Is_desc		*isec;
1419 	Is_desc		*xisec = 0;
1420 	size_t		size;
1421 	Word		symcnt;
1422 
1423 	/*
1424 	 * Create the section headers. Note that we supply an ent_cnt
1425 	 * of 0. We won't know the count until the section has been placed.
1426 	 */
1427 	if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0,
1428 	    &isec, &shdr, &data) == S_ERROR)
1429 		return (S_ERROR);
1430 
1431 	/*
1432 	 * Place the section first since it will affect the local symbol
1433 	 * count.
1434 	 */
1435 	ofl->ofl_ossymtab =
1436 	    ld_place_section(ofl, isec, ld_targ.t_id.id_symtab, 0);
1437 	if (ofl->ofl_ossymtab == (Os_desc *)S_ERROR)
1438 		return (S_ERROR);
1439 
1440 	/*
1441 	 * At this point we've created all but the 'shstrtab' section.
1442 	 * Determine if we have to use 'Extended Sections'.  If so - then
1443 	 * also create a SHT_SYMTAB_SHNDX section.
1444 	 */
1445 	if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
1446 		Shdr		*xshdr;
1447 		Elf_Data	*xdata;
1448 
1449 		if (new_section(ofl, SHT_SYMTAB_SHNDX,
1450 		    MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec,
1451 		    &xshdr, &xdata) == S_ERROR)
1452 			return (S_ERROR);
1453 
1454 		if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec,
1455 		    ld_targ.t_id.id_symtab_ndx, 0)) == (Os_desc *)S_ERROR)
1456 			return (S_ERROR);
1457 	}
1458 	/*
1459 	 * Calculated number of symbols, which need to be augmented by
1460 	 * the null first entry, the FILE symbol, and the .shstrtab entry.
1461 	 */
1462 	symcnt = (size_t)(3 + ofl->ofl_shdrcnt + ofl->ofl_scopecnt +
1463 	    ofl->ofl_locscnt + ofl->ofl_globcnt);
1464 	size = symcnt * shdr->sh_entsize;
1465 
1466 	/*
1467 	 * Finalize the section header and data buffer initialization.
1468 	 */
1469 	data->d_size = size;
1470 	shdr->sh_size = (Xword)size;
1471 
1472 	/*
1473 	 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
1474 	 */
1475 	if (xisec) {
1476 		size_t	xsize = symcnt * sizeof (Word);
1477 
1478 		xisec->is_indata->d_size = xsize;
1479 		xisec->is_shdr->sh_size = (Xword)xsize;
1480 	}
1481 
1482 	return (1);
1483 }
1484 
1485 
1486 /*
1487  * Build a dynamic symbol table. These tables reside in the text
1488  * segment of a dynamic executable or shared library.
1489  *
1490  *	.SUNW_ldynsym contains local function symbols
1491  *	.dynsym contains only globals symbols
1492  *
1493  * The two tables are created adjacent to each other, with .SUNW_ldynsym
1494  * coming first.
1495  */
1496 static uintptr_t
1497 make_dynsym(Ofl_desc *ofl)
1498 {
1499 	Shdr		*shdr, *lshdr;
1500 	Elf_Data	*data, *ldata;
1501 	Is_desc		*isec, *lisec;
1502 	size_t		size;
1503 	Xword		cnt;
1504 	int		allow_ldynsym;
1505 
1506 	/*
1507 	 * Unless explicitly disabled, always produce a .SUNW_ldynsym section
1508 	 * when it is allowed by the file type, even if the resulting
1509 	 * table only ends up with a single STT_FILE in it. There are
1510 	 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB
1511 	 * entry in the .dynamic section, which is something we would
1512 	 * like to encourage, and (2) Without it, we cannot generate
1513 	 * the associated .SUNW_dyn[sym|tls]sort sections, which are of
1514 	 * value to DTrace.
1515 	 *
1516 	 * In practice, it is extremely rare for an object not to have
1517 	 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be
1518 	 * doing it anyway.
1519 	 */
1520 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1521 
1522 	/*
1523 	 * Create the section headers. Note that we supply an ent_cnt
1524 	 * of 0. We won't know the count until the section has been placed.
1525 	 */
1526 	if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM,
1527 	    MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR)
1528 		return (S_ERROR);
1529 
1530 	if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0,
1531 	    &isec, &shdr, &data) == S_ERROR)
1532 		return (S_ERROR);
1533 
1534 	/*
1535 	 * Place the section(s) first since it will affect the local symbol
1536 	 * count.
1537 	 */
1538 	if (allow_ldynsym &&
1539 	    ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec,
1540 	    ld_targ.t_id.id_ldynsym, 0)) == (Os_desc *)S_ERROR))
1541 		return (S_ERROR);
1542 	ofl->ofl_osdynsym =
1543 	    ld_place_section(ofl, isec, ld_targ.t_id.id_dynsym, 0);
1544 	if (ofl->ofl_osdynsym == (Os_desc *)S_ERROR)
1545 		return (S_ERROR);
1546 
1547 	/*
1548 	 * One extra section header entry for the 'null' entry.
1549 	 */
1550 	cnt = 1 + ofl->ofl_dynshdrcnt + ofl->ofl_globcnt + ofl->ofl_lregsymcnt;
1551 	size = (size_t)cnt * shdr->sh_entsize;
1552 
1553 	/*
1554 	 * Finalize the section header and data buffer initialization.
1555 	 */
1556 	data->d_size = size;
1557 	shdr->sh_size = (Xword)size;
1558 
1559 	/*
1560 	 * An ldynsym contains local function symbols. It is not
1561 	 * used for linking, but if present, serves to allow better
1562 	 * stack traces to be generated in contexts where the symtab
1563 	 * is not available. (dladdr(), or stripped executable/library files).
1564 	 */
1565 	if (allow_ldynsym) {
1566 		cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt;
1567 		size = (size_t)cnt * shdr->sh_entsize;
1568 
1569 		ldata->d_size = size;
1570 		lshdr->sh_size = (Xword)size;
1571 	}
1572 
1573 	return (1);
1574 }
1575 
1576 /*
1577  * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are
1578  * index sections for the .SUNW_ldynsym/.dynsym pair that present data
1579  * and function symbols sorted by address.
1580  */
1581 static uintptr_t
1582 make_dynsort(Ofl_desc *ofl)
1583 {
1584 	Shdr		*shdr;
1585 	Elf_Data	*data;
1586 	Is_desc		*isec;
1587 
1588 
1589 	/* Only do it if the .SUNW_ldynsym section is present */
1590 	if (!OFL_ALLOW_LDYNSYM(ofl))
1591 		return (1);
1592 
1593 	/* .SUNW_dynsymsort */
1594 	if (ofl->ofl_dynsymsortcnt > 0) {
1595 		if (new_section(ofl, SHT_SUNW_symsort,
1596 		    MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt,
1597 		    &isec, &shdr, &data) == S_ERROR)
1598 		return (S_ERROR);
1599 
1600 		if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec,
1601 		    ld_targ.t_id.id_dynsort, 0)) == (Os_desc *)S_ERROR)
1602 			return (S_ERROR);
1603 	}
1604 
1605 	/* .SUNW_dyntlssort */
1606 	if (ofl->ofl_dyntlssortcnt > 0) {
1607 		if (new_section(ofl, SHT_SUNW_tlssort,
1608 		    MSG_ORIG(MSG_SCN_DYNTLSSORT),
1609 		    ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR)
1610 		return (S_ERROR);
1611 
1612 		if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec,
1613 		    ld_targ.t_id.id_dynsort, 0)) == (Os_desc *)S_ERROR)
1614 			return (S_ERROR);
1615 	}
1616 
1617 	return (1);
1618 }
1619 
1620 /*
1621  * Helper routine for make_dynsym_shndx. Builds a
1622  * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing
1623  * which one it is.
1624  */
1625 static uintptr_t
1626 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab,
1627     Os_desc **ret_os)
1628 {
1629 	Is_desc		*isec;
1630 	Is_desc		*dynsymisp;
1631 	Shdr		*shdr, *dynshdr;
1632 	Elf_Data	*data;
1633 
1634 	dynsymisp = (Is_desc *)symtab->os_isdescs.head->data;
1635 	dynshdr = dynsymisp->is_shdr;
1636 
1637 	if (new_section(ofl, SHT_SYMTAB_SHNDX, shname,
1638 	    (dynshdr->sh_size / dynshdr->sh_entsize),
1639 	    &isec, &shdr, &data) == S_ERROR)
1640 		return (S_ERROR);
1641 
1642 	if ((*ret_os = ld_place_section(ofl, isec,
1643 	    ld_targ.t_id.id_dynsym_ndx, 0)) == (Os_desc *)S_ERROR)
1644 		return (S_ERROR);
1645 
1646 	assert(*ret_os);
1647 
1648 	return (1);
1649 }
1650 
1651 /*
1652  * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym
1653  */
1654 static uintptr_t
1655 make_dynsym_shndx(Ofl_desc *ofl)
1656 {
1657 	/*
1658 	 * If there is a .SUNW_ldynsym, generate a section for its extended
1659 	 * index section as well.
1660 	 */
1661 	if (OFL_ALLOW_LDYNSYM(ofl)) {
1662 		if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX),
1663 		    ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR)
1664 			return (S_ERROR);
1665 	}
1666 
1667 	/* The Generate a section for the dynsym */
1668 	if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX),
1669 	    ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR)
1670 		return (S_ERROR);
1671 
1672 	return (1);
1673 }
1674 
1675 
1676 /*
1677  * Build a string table for the section headers.
1678  */
1679 static uintptr_t
1680 make_shstrtab(Ofl_desc *ofl)
1681 {
1682 	Shdr		*shdr;
1683 	Elf_Data	*data;
1684 	Is_desc		*isec;
1685 	size_t		size;
1686 
1687 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB),
1688 	    0, &isec, &shdr, &data) == S_ERROR)
1689 		return (S_ERROR);
1690 
1691 	/*
1692 	 * Place the section first, as it may effect the number of section
1693 	 * headers to account for.
1694 	 */
1695 	ofl->ofl_osshstrtab =
1696 	    ld_place_section(ofl, isec, ld_targ.t_id.id_note, 0);
1697 	if (ofl->ofl_osshstrtab == (Os_desc *)S_ERROR)
1698 		return (S_ERROR);
1699 
1700 	size = st_getstrtab_sz(ofl->ofl_shdrsttab);
1701 	assert(size > 0);
1702 
1703 	data->d_size = size;
1704 	shdr->sh_size = (Xword)size;
1705 
1706 	return (1);
1707 }
1708 
1709 /*
1710  * Build a string section for the standard symbol table.
1711  */
1712 static uintptr_t
1713 make_strtab(Ofl_desc *ofl)
1714 {
1715 	Shdr		*shdr;
1716 	Elf_Data	*data;
1717 	Is_desc		*isec;
1718 	size_t		size;
1719 
1720 	/*
1721 	 * This string table consists of all the global and local symbols.
1722 	 * Account for null bytes at end of the file name and the beginning
1723 	 * of section.
1724 	 */
1725 	if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
1726 		return (S_ERROR);
1727 
1728 	size = st_getstrtab_sz(ofl->ofl_strtab);
1729 	assert(size > 0);
1730 
1731 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB),
1732 	    0, &isec, &shdr, &data) == S_ERROR)
1733 		return (S_ERROR);
1734 
1735 	/* Set the size of the data area */
1736 	data->d_size = size;
1737 	shdr->sh_size = (Xword)size;
1738 
1739 	ofl->ofl_osstrtab =
1740 	    ld_place_section(ofl, isec, ld_targ.t_id.id_strtab, 0);
1741 	return ((uintptr_t)ofl->ofl_osstrtab);
1742 }
1743 
1744 /*
1745  * Build a string table for the dynamic symbol table.
1746  */
1747 static uintptr_t
1748 make_dynstr(Ofl_desc *ofl)
1749 {
1750 	Shdr		*shdr;
1751 	Elf_Data	*data;
1752 	Is_desc		*isec;
1753 	size_t		size;
1754 
1755 	/*
1756 	 * If producing a .SUNW_ldynsym, account for the initial STT_FILE
1757 	 * symbol that precedes the scope reduced global symbols.
1758 	 */
1759 	if (OFL_ALLOW_LDYNSYM(ofl)) {
1760 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1)
1761 			return (S_ERROR);
1762 		ofl->ofl_dynscopecnt++;
1763 	}
1764 
1765 
1766 	/*
1767 	 * Account for any local, named register symbols.  These locals are
1768 	 * required for reference from DT_REGISTER .dynamic entries.
1769 	 */
1770 	if (ofl->ofl_regsyms) {
1771 		int	ndx;
1772 
1773 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1774 			Sym_desc *	sdp;
1775 
1776 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1777 				continue;
1778 
1779 			if (((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) &&
1780 			    (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
1781 				continue;
1782 
1783 			if (sdp->sd_sym->st_name == 0)
1784 				continue;
1785 
1786 			if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
1787 				return (S_ERROR);
1788 		}
1789 	}
1790 
1791 	/*
1792 	 * Reserve entries for any per-symbol auxiliary/filter strings.
1793 	 */
1794 	if (ofl->ofl_dtsfltrs != NULL) {
1795 		Dfltr_desc	*dftp;
1796 		Aliste		idx;
1797 
1798 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp))
1799 			if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
1800 				return (S_ERROR);
1801 	}
1802 
1803 	size = st_getstrtab_sz(ofl->ofl_dynstrtab);
1804 	assert(size > 0);
1805 
1806 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR),
1807 	    0, &isec, &shdr, &data) == S_ERROR)
1808 		return (S_ERROR);
1809 
1810 	/* Make it allocable if necessary */
1811 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
1812 		shdr->sh_flags |= SHF_ALLOC;
1813 
1814 	/* Set the size of the data area */
1815 	data->d_size = size + DYNSTR_EXTRA_PAD;
1816 
1817 	shdr->sh_size = (Xword)size;
1818 
1819 	ofl->ofl_osdynstr =
1820 	    ld_place_section(ofl, isec, ld_targ.t_id.id_dynstr, 0);
1821 	return ((uintptr_t)ofl->ofl_osdynstr);
1822 }
1823 
1824 /*
1825  * Generate an output relocation section which will contain the relocation
1826  * information to be applied to the `osp' section.
1827  *
1828  * If (osp == NULL) then we are creating the coalesced relocation section
1829  * for an executable and/or a shared object.
1830  */
1831 static uintptr_t
1832 make_reloc(Ofl_desc *ofl, Os_desc *osp)
1833 {
1834 	Shdr		*shdr;
1835 	Elf_Data	*data;
1836 	Is_desc		*isec;
1837 	size_t		size;
1838 	Xword		sh_flags;
1839 	char 		*sectname;
1840 	Os_desc		*rosp;
1841 	Word		relsize;
1842 	const char	*rel_prefix;
1843 
1844 	/* LINTED */
1845 	if (ld_targ.t_m.m_rel_sht_type == SHT_REL) {
1846 		/* REL */
1847 		relsize = sizeof (Rel);
1848 		rel_prefix = MSG_ORIG(MSG_SCN_REL);
1849 	} else {
1850 		/* RELA */
1851 		relsize = sizeof (Rela);
1852 		rel_prefix = MSG_ORIG(MSG_SCN_RELA);
1853 	}
1854 
1855 	if (osp) {
1856 		size = osp->os_szoutrels;
1857 		sh_flags = osp->os_shdr->sh_flags;
1858 		if ((sectname = libld_malloc(strlen(rel_prefix) +
1859 		    strlen(osp->os_name) + 1)) == 0)
1860 			return (S_ERROR);
1861 		(void) strcpy(sectname, rel_prefix);
1862 		(void) strcat(sectname, osp->os_name);
1863 	} else if (ofl->ofl_flags & FLG_OF_COMREL) {
1864 		size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
1865 		sh_flags = SHF_ALLOC;
1866 		sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
1867 	} else {
1868 		size = ofl->ofl_relocrelsz;
1869 		sh_flags = SHF_ALLOC;
1870 		sectname = (char *)rel_prefix;
1871 	}
1872 
1873 	/*
1874 	 * Keep track of total size of 'output relocations' (to be stored
1875 	 * in .dynamic)
1876 	 */
1877 	/* LINTED */
1878 	ofl->ofl_relocsz += (Xword)size;
1879 
1880 	if (new_section(ofl, ld_targ.t_m.m_rel_sht_type, sectname, 0, &isec,
1881 	    &shdr, &data) == S_ERROR)
1882 		return (S_ERROR);
1883 
1884 	data->d_size = size;
1885 
1886 	shdr->sh_size = (Xword)size;
1887 	if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC))
1888 		shdr->sh_flags = SHF_ALLOC;
1889 
1890 	if (osp) {
1891 		/*
1892 		 * The sh_info field of the SHT_REL* sections points to the
1893 		 * section the relocations are to be applied to.
1894 		 */
1895 		shdr->sh_flags |= SHF_INFO_LINK;
1896 	}
1897 
1898 	/*
1899 	 * Associate this relocation section to the section its going to
1900 	 * relocate.
1901 	 */
1902 	rosp = ld_place_section(ofl, isec, ld_targ.t_id.id_rel, 0);
1903 	if (rosp == (Os_desc *)S_ERROR)
1904 		return (S_ERROR);
1905 
1906 	if (osp) {
1907 		Listnode	*lnp;
1908 		Is_desc		*risp;
1909 
1910 		/*
1911 		 * We associate the input relocation sections - with
1912 		 * the newly created output relocation section.
1913 		 *
1914 		 * This is used primarily so that we can update
1915 		 * SHT_GROUP[sect_no] entries to point to the
1916 		 * created output relocation sections.
1917 		 */
1918 		for (LIST_TRAVERSE(&(osp->os_relisdescs), lnp, risp)) {
1919 			risp->is_osdesc = rosp;
1920 
1921 			/*
1922 			 * If the input relocation section had the SHF_GROUP
1923 			 * flag set - propagate it to the output relocation
1924 			 * section.
1925 			 */
1926 			if (risp->is_shdr->sh_flags & SHF_GROUP) {
1927 				rosp->os_shdr->sh_flags |= SHF_GROUP;
1928 				break;
1929 			}
1930 		}
1931 		osp->os_relosdesc = rosp;
1932 	} else
1933 		ofl->ofl_osrel = rosp;
1934 
1935 	/*
1936 	 * If this is the first relocation section we've encountered save it
1937 	 * so that the .dynamic entry can be initialized accordingly.
1938 	 */
1939 	if (ofl->ofl_osrelhead == (Os_desc *)0)
1940 		ofl->ofl_osrelhead = rosp;
1941 
1942 	return (1);
1943 }
1944 
1945 /*
1946  * Generate version needed section.
1947  */
1948 static uintptr_t
1949 make_verneed(Ofl_desc *ofl)
1950 {
1951 	Shdr		*shdr;
1952 	Elf_Data	*data;
1953 	Is_desc		*isec;
1954 
1955 	/*
1956 	 * verneed sections do not have a constant element size, so the
1957 	 * value of ent_cnt specified here (0) is meaningless.
1958 	 */
1959 	if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION),
1960 	    0, &isec, &shdr, &data) == S_ERROR)
1961 		return (S_ERROR);
1962 
1963 	/* During version processing we calculated the total size. */
1964 	data->d_size = ofl->ofl_verneedsz;
1965 	shdr->sh_size = (Xword)ofl->ofl_verneedsz;
1966 
1967 	ofl->ofl_osverneed =
1968 	    ld_place_section(ofl, isec, ld_targ.t_id.id_version, 0);
1969 	return ((uintptr_t)ofl->ofl_osverneed);
1970 }
1971 
1972 /*
1973  * Generate a version definition section.
1974  *
1975  *  o	the SHT_SUNW_verdef section defines the versions that exist within this
1976  *	image.
1977  */
1978 static uintptr_t
1979 make_verdef(Ofl_desc *ofl)
1980 {
1981 	Shdr		*shdr;
1982 	Elf_Data	*data;
1983 	Is_desc		*isec;
1984 	Ver_desc	*vdp;
1985 
1986 	/*
1987 	 * Reserve a string table entry for the base version dependency (other
1988 	 * dependencies have symbol representations, which will already be
1989 	 * accounted for during symbol processing).
1990 	 */
1991 	vdp = (Ver_desc *)ofl->ofl_verdesc.head->data;
1992 
1993 	if (ofl->ofl_flags & FLG_OF_DYNAMIC) {
1994 		if (st_insert(ofl->ofl_dynstrtab, vdp->vd_name) == -1)
1995 			return (S_ERROR);
1996 	} else {
1997 		if (st_insert(ofl->ofl_strtab, vdp->vd_name) == -1)
1998 			return (S_ERROR);
1999 	}
2000 
2001 	/*
2002 	 * verdef sections do not have a constant element size, so the
2003 	 * value of ent_cnt specified here (0) is meaningless.
2004 	 */
2005 	if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION),
2006 	    0, &isec, &shdr, &data) == S_ERROR)
2007 		return (S_ERROR);
2008 
2009 	/* During version processing we calculated the total size. */
2010 	data->d_size = ofl->ofl_verdefsz;
2011 	shdr->sh_size = (Xword)ofl->ofl_verdefsz;
2012 
2013 	ofl->ofl_osverdef =
2014 	    ld_place_section(ofl, isec, ld_targ.t_id.id_version, 0);
2015 	return ((uintptr_t)ofl->ofl_osverdef);
2016 }
2017 
2018 /*
2019  * Common function used to build both the SHT_SUNW_versym
2020  * section and the SHT_SUNW_syminfo section.  Each of these sections
2021  * provides additional symbol information.
2022  */
2023 static Os_desc *
2024 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident)
2025 {
2026 	Shdr		*shdr;
2027 	Elf_Data	*data;
2028 	Is_desc		*isec;
2029 
2030 	/*
2031 	 * We don't know the size of this section yet, so set it to 0.
2032 	 * It gets filled in after the dynsym is sized.
2033 	 */
2034 	if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) ==
2035 	    S_ERROR)
2036 		return ((Os_desc *)S_ERROR);
2037 
2038 	return (ld_place_section(ofl, isec, ident, 0));
2039 }
2040 
2041 /*
2042  * Build a .sunwbss section for allocation of tentative definitions.
2043  */
2044 uintptr_t
2045 ld_make_sunwbss(Ofl_desc *ofl, size_t size, Xword align)
2046 {
2047 	Shdr		*shdr;
2048 	Elf_Data	*data;
2049 	Is_desc		*isec;
2050 
2051 	/*
2052 	 * Allocate header structs. We will set the name ourselves below,
2053 	 * and there is no entcnt for a BSS. So, the shname and entcnt
2054 	 * arguments are 0.
2055 	 */
2056 	if (new_section(ofl, SHT_NOBITS, MSG_ORIG(MSG_SCN_SUNWBSS), 0,
2057 	    &isec, &shdr, &data) == S_ERROR)
2058 		return (S_ERROR);
2059 
2060 	data->d_size = size;
2061 	data->d_align = align;
2062 
2063 	shdr->sh_size = (Xword)size;
2064 	shdr->sh_addralign = align;
2065 
2066 	/*
2067 	 * Retain this .sunwbss input section as this will be where global
2068 	 * symbol references are added.
2069 	 */
2070 	ofl->ofl_issunwbss = isec;
2071 	if (ld_place_section(ofl, isec, 0, 0) == (Os_desc *)S_ERROR)
2072 		return (S_ERROR);
2073 
2074 	return (1);
2075 }
2076 
2077 /*
2078  * This routine is called when -z nopartial is in effect.
2079  */
2080 uintptr_t
2081 ld_make_sunwdata(Ofl_desc *ofl, size_t size, Xword align)
2082 {
2083 	Shdr		*shdr;
2084 	Elf_Data	*data;
2085 	Is_desc		*isec;
2086 	Os_desc		*osp;
2087 
2088 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_SUNWDATA1), 0,
2089 	    &isec, &shdr, &data) == S_ERROR)
2090 		return (S_ERROR);
2091 
2092 	shdr->sh_flags |= SHF_WRITE;
2093 	data->d_size = size;
2094 	shdr->sh_size = (Xword)size;
2095 	if (align != 0) {
2096 		data->d_align = align;
2097 		shdr->sh_addralign = align;
2098 	}
2099 
2100 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2101 		return (S_ERROR);
2102 
2103 	/*
2104 	 * Retain this .sunwdata1 input section as this will
2105 	 * be where global
2106 	 * symbol references are added.
2107 	 */
2108 	ofl->ofl_issunwdata1 = isec;
2109 	osp = ld_place_section(ofl, isec, ld_targ.t_id.id_data, 0);
2110 	if (osp == (Os_desc *)S_ERROR)
2111 		return (S_ERROR);
2112 
2113 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
2114 		ofl->ofl_dynshdrcnt++;
2115 		osp->os_flags |= FLG_OS_OUTREL;
2116 	}
2117 	return (1);
2118 }
2119 
2120 /*
2121  * Make .sunwmove section
2122  */
2123 uintptr_t
2124 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2125 {
2126 	Shdr		*shdr;
2127 	Elf_Data	*data;
2128 	Is_desc		*isec;
2129 	Listnode	*lnp1;
2130 	Psym_info	*psym;
2131 	int 		cnt = 1;
2132 
2133 
2134 	if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE),
2135 	    mv_nums, &isec, &shdr, &data) == S_ERROR)
2136 		return (S_ERROR);
2137 
2138 	if ((data->d_buf = libld_calloc(data->d_size, 1)) == 0)
2139 		return (S_ERROR);
2140 
2141 	/*
2142 	 * Copy move entries
2143 	 */
2144 	for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) {
2145 		Listnode *	lnp2;
2146 		Mv_itm *	mvitm;
2147 
2148 		if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN)
2149 			continue;
2150 		for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvitm)) {
2151 			if ((mvitm->mv_flag & FLG_MV_OUTSECT) == 0)
2152 				continue;
2153 			mvitm->mv_oidx = cnt;
2154 			cnt++;
2155 		}
2156 	}
2157 	if ((ofl->ofl_osmove = ld_place_section(ofl, isec, 0, 0)) ==
2158 	    (Os_desc *)S_ERROR)
2159 		return (S_ERROR);
2160 
2161 	return (1);
2162 }
2163 
2164 
2165 /*
2166  * Given a relocation descriptor that references a string table
2167  * input section, locate the string referenced and return a pointer
2168  * to it.
2169  */
2170 static const char *
2171 strmerge_get_reloc_str(Ofl_desc *ofl, Rel_desc *rsp)
2172 {
2173 	Sym_desc *sdp = rsp->rel_sym;
2174 	Xword	 str_off;
2175 
2176 	/*
2177 	 * In the case of an STT_SECTION symbol, the addend of the
2178 	 * relocation gives the offset into the string section. For
2179 	 * other symbol types, the symbol value is the offset.
2180 	 */
2181 
2182 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
2183 		str_off = sdp->sd_sym->st_value;
2184 	} else if ((rsp->rel_flags & FLG_REL_RELA) == FLG_REL_RELA) {
2185 		/*
2186 		 * For SHT_RELA, the addend value is found in the
2187 		 * rel_raddend field of the relocation.
2188 		 */
2189 		str_off = rsp->rel_raddend;
2190 	} else {	/* REL and STT_SECTION */
2191 		/*
2192 		 * For SHT_REL, the "addend" is not part of the relocation
2193 		 * record. Instead, it is found at the relocation target
2194 		 * address.
2195 		 */
2196 		uchar_t *addr = (uchar_t *)((uintptr_t)rsp->rel_roffset +
2197 		    (uintptr_t)rsp->rel_isdesc->is_indata->d_buf);
2198 
2199 		if (ld_reloc_targval_get(ofl, rsp, addr, &str_off) == 0)
2200 			return (0);
2201 	}
2202 
2203 	return (str_off + (char *)sdp->sd_isc->is_indata->d_buf);
2204 }
2205 
2206 /*
2207  * First pass over the relocation records for string table merging.
2208  * Build lists of relocations and symbols that will need modification,
2209  * and insert the strings they reference into the mstrtab string table.
2210  *
2211  * entry:
2212  *	ofl, osp - As passed to ld_make_strmerge().
2213  *	mstrtab - String table to receive input strings. This table
2214  *		must be in its first (initialization) pass and not
2215  *		yet cooked (st_getstrtab_sz() not yet called).
2216  *	rel_aplist - APlist to receive pointer to any relocation
2217  *		descriptors with STT_SECTION symbols that reference
2218  *		one of the input sections being merged.
2219  *	sym_aplist - APlist to receive pointer to any symbols that reference
2220  *		one of the input sections being merged.
2221  *	reloc_list - List of relocation descriptors to examine.
2222  *		Either ofl->&ofl->ofl_actrels (active relocations)
2223  *		or &ofl->ofl_outrels (output relocations).
2224  *
2225  * exit:
2226  *	On success, rel_aplist and sym_aplist are updated, and
2227  *	any strings in the mergable input sections referenced by
2228  *	a relocation has been entered into mstrtab. True (1) is returned.
2229  *
2230  *	On failure, False (0) is returned.
2231  */
2232 static int
2233 strmerge_pass1(Ofl_desc *ofl, Os_desc *osp, Str_tbl *mstrtab,
2234     APlist **rel_aplist, APlist **sym_aplist, List *reloc_list)
2235 {
2236 	Listnode	*lnp;
2237 	Rel_cache	*rcp;
2238 	Sym_desc	*sdp;
2239 	Sym_desc	*last_sdp = NULL;
2240 	Rel_desc	*rsp;
2241 	const char	*name;
2242 
2243 	for (LIST_TRAVERSE(reloc_list, lnp, rcp)) {
2244 		/* LINTED */
2245 		for (rsp = (Rel_desc *)(rcp + 1); rsp < rcp->rc_free; rsp++) {
2246 			sdp = rsp->rel_sym;
2247 			if ((sdp->sd_isc == NULL) ||
2248 			    ((sdp->sd_isc->is_flags &
2249 			    (FLG_IS_DISCARD | FLG_IS_INSTRMRG)) !=
2250 			    FLG_IS_INSTRMRG) ||
2251 			    (sdp->sd_isc->is_osdesc != osp))
2252 				continue;
2253 
2254 			/*
2255 			 * Remember symbol for use in the third pass.
2256 			 * There is no reason to save a given symbol more
2257 			 * than once, so we take advantage of the fact that
2258 			 * relocations to a given symbol tend to cluster
2259 			 * in the list. If this is the same symbol we saved
2260 			 * last time, don't bother.
2261 			 */
2262 			if (last_sdp != sdp) {
2263 				if (aplist_append(sym_aplist, sdp,
2264 				    AL_CNT_STRMRGSYM) == 0)
2265 					return (0);
2266 				last_sdp = sdp;
2267 			}
2268 
2269 			/* Enter the string into our new string table */
2270 			name = strmerge_get_reloc_str(ofl, rsp);
2271 			if (st_insert(mstrtab, name) == -1)
2272 				return (0);
2273 
2274 			/*
2275 			 * If this is an STT_SECTION symbol, then the
2276 			 * second pass will need to modify this relocation,
2277 			 * so hang on to it.
2278 			 */
2279 			if ((ELF_ST_TYPE(sdp->sd_sym->st_info) ==
2280 			    STT_SECTION) &&
2281 			    (aplist_append(rel_aplist, rsp,
2282 			    AL_CNT_STRMRGREL) == 0))
2283 				return (0);
2284 		}
2285 	}
2286 
2287 	return (1);
2288 }
2289 
2290 /*
2291  * If the output section has any SHF_MERGE|SHF_STRINGS input sections,
2292  * replace them with a single merged/compressed input section.
2293  *
2294  * entry:
2295  *	ofl - Output file descriptor
2296  *	osp - Output section descriptor
2297  *	rel_aplist, sym_aplist, - Address of 2 APlists, to be used
2298  *		for internal processing. On the initial call to
2299  *		ld_make_strmerge, these list pointers must be NULL.
2300  *		The caller is encouraged to pass the same lists back for
2301  *		successive calls to this function without freeing
2302  *		them in between calls. This causes a single pair of
2303  *		memory allocations to be reused multiple times.
2304  *
2305  * exit:
2306  *	If section merging is possible, it is done. If no errors are
2307  *	encountered, True (1) is returned. On error, S_ERROR.
2308  *
2309  *	The contents of rel_aplist and sym_aplist on exit are
2310  *	undefined. The caller can free them, or pass them back to a subsequent
2311  *	call to this routine, but should not examine their contents.
2312  */
2313 static uintptr_t
2314 ld_make_strmerge(Ofl_desc *ofl, Os_desc *osp, APlist **rel_aplist,
2315     APlist **sym_aplist)
2316 {
2317 	Str_tbl		*mstrtab;	/* string table for string merge secs */
2318 	Is_desc		*mstrsec;	/* Generated string merge section */
2319 	Is_desc		*isp;
2320 	Shdr		*mstr_shdr;
2321 	Elf_Data	*mstr_data;
2322 	Sym_desc	*sdp;
2323 	Rel_desc	*rsp;
2324 	Aliste		idx;
2325 	size_t		data_size;
2326 	int		st_setstring_status;
2327 	size_t		stoff;
2328 
2329 	/* If string table compression is disabled, there's nothing to do */
2330 	if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0)
2331 		return (1);
2332 
2333 	/*
2334 	 * Pass over the mergeable input sections, and if they haven't
2335 	 * all been discarded, create a string table.
2336 	 */
2337 	mstrtab = NULL;
2338 	for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
2339 		if (isp->is_flags & FLG_IS_DISCARD)
2340 			continue;
2341 
2342 		/*
2343 		 * We have at least one non-discarded section.
2344 		 * Create a string table descriptor.
2345 		 */
2346 		if ((mstrtab = st_new(FLG_STNEW_COMPRESS)) == NULL)
2347 			return (S_ERROR);
2348 		break;
2349 	}
2350 
2351 	/* If no string table was created, we have no mergeable sections */
2352 	if (mstrtab == NULL)
2353 		return (1);
2354 
2355 	/*
2356 	 * This routine has to make 3 passes:
2357 	 *
2358 	 *	1) Examine all relocations, insert strings from relocations
2359 	 *		to the mergable input sections into the string table.
2360 	 *	2) Modify the relocation values to be correct for the
2361 	 *		new merged section.
2362 	 *	3) Modify the symbols used by the relocations to reference
2363 	 *		the new section.
2364 	 *
2365 	 * These passes cannot be combined:
2366 	 *	- The string table code works in two passes, and all
2367 	 *		strings have to be loaded in pass one before the
2368 	 *		offset of any strings can be determined.
2369 	 *	- Multiple relocations reference a single symbol, so the
2370 	 *		symbol cannot be modified until all relocations are
2371 	 *		fixed.
2372 	 *
2373 	 * The number of relocations related to section merging is usually
2374 	 * a mere fraction of the overall active and output relocation lists,
2375 	 * and the number of symbols is usually a fraction of the number
2376 	 * of related relocations. We therefore build APlists for the
2377 	 * relocations and symbols in the first pass, and then use those
2378 	 * lists to accelerate the operation of pass 2 and 3.
2379 	 *
2380 	 * Reinitialize the lists to a completely empty state.
2381 	 */
2382 	aplist_reset(*rel_aplist);
2383 	aplist_reset(*sym_aplist);
2384 
2385 	/*
2386 	 * Pass 1:
2387 	 *
2388 	 * Every relocation related to this output section (and the input
2389 	 * sections that make it up) is found in either the active, or the
2390 	 * output relocation list, depending on whether the relocation is to
2391 	 * be processed by this invocation of the linker, or inserted into the
2392 	 * output object.
2393 	 *
2394 	 * Build lists of relocations and symbols that will need modification,
2395 	 * and insert the strings they reference into the mstrtab string table.
2396 	 */
2397 	if (strmerge_pass1(ofl, osp, mstrtab, rel_aplist, sym_aplist,
2398 	    &ofl->ofl_actrels) == 0)
2399 		goto return_s_error;
2400 	if (strmerge_pass1(ofl, osp, mstrtab, rel_aplist, sym_aplist,
2401 	    &ofl->ofl_outrels) == 0)
2402 		goto return_s_error;
2403 
2404 	/*
2405 	 * Get the size of the new input section. Requesting the
2406 	 * string table size "cooks" the table, and finalizes its contents.
2407 	 */
2408 	data_size = st_getstrtab_sz(mstrtab);
2409 
2410 	/* Create a new input section to hold the merged strings */
2411 	if (new_section_from_template(ofl, isp, data_size,
2412 	    &mstrsec, &mstr_shdr, &mstr_data) == S_ERROR)
2413 		goto return_s_error;
2414 	mstrsec->is_flags |= FLG_IS_GNSTRMRG;
2415 
2416 	/*
2417 	 * Allocate a data buffer for the new input section.
2418 	 * Then, associate the buffer with the string table descriptor.
2419 	 */
2420 	if ((mstr_data->d_buf = libld_malloc(data_size)) == 0)
2421 		goto return_s_error;
2422 	if (st_setstrbuf(mstrtab, mstr_data->d_buf, data_size) == -1)
2423 		goto return_s_error;
2424 
2425 	/* Add the new section to the output image */
2426 	if (ld_place_section(ofl, mstrsec, osp->os_scnsymndx, 0) ==
2427 	    (Os_desc *)S_ERROR)
2428 		goto return_s_error;
2429 
2430 	/*
2431 	 * Pass 2:
2432 	 *
2433 	 * Revisit the relocation descriptors with STT_SECTION symbols
2434 	 * that were saved by the first pass. Update each relocation
2435 	 * record so that the offset it contains is for the new section
2436 	 * instead of the original.
2437 	 */
2438 	for (APLIST_TRAVERSE(*rel_aplist, idx, rsp)) {
2439 		const char	*name;
2440 
2441 		/* Put the string into the merged string table */
2442 		name = strmerge_get_reloc_str(ofl, rsp);
2443 		st_setstring_status = st_setstring(mstrtab, name, &stoff);
2444 		if (st_setstring_status == -1) {
2445 			/*
2446 			 * A failure to insert at this point means that
2447 			 * something is corrupt. This isn't a resource issue.
2448 			 */
2449 			assert(st_setstring_status != -1);
2450 			goto return_s_error;
2451 		}
2452 
2453 		/*
2454 		 * Alter the relocation to access the string at the
2455 		 * new offset in our new string table.
2456 		 *
2457 		 * For SHT_RELA platforms, it suffices to simply
2458 		 * update the rel_raddend field of the relocation.
2459 		 *
2460 		 * For SHT_REL platforms, the new "addend" value
2461 		 * needs to be written at the address being relocated.
2462 		 * However, we can't alter the input sections which
2463 		 * are mapped readonly, and the output image has not
2464 		 * been created yet. So, we defer this operation,
2465 		 * using the rel_raddend field of the relocation
2466 		 * which is normally 0 on a REL platform, to pass the
2467 		 * new "addend" value to ld_perform_outreloc() or
2468 		 * ld_do_activerelocs(). The FLG_REL_NADDEND flag
2469 		 * tells them that this is the case.
2470 		 */
2471 		if ((rsp->rel_flags & FLG_REL_RELA) == 0)   /* REL */
2472 			rsp->rel_flags |= FLG_REL_NADDEND;
2473 		rsp->rel_raddend = (Sxword)stoff;
2474 
2475 		/*
2476 		 * Change the descriptor name to reflect the fact that it
2477 		 * points at our merged section. This shows up in debug
2478 		 * output and helps show how the relocation has changed
2479 		 * from its original input section to our merged one.
2480 		 */
2481 		rsp->rel_sname = ld_section_reld_name(rsp->rel_sym, mstrsec);
2482 		if (rsp->rel_sname == NULL)
2483 			goto return_s_error;
2484 	}
2485 
2486 	/*
2487 	 * Pass 3:
2488 	 *
2489 	 * Modify the symbols referenced by the relocation descriptors
2490 	 * so that they reference the new input section containing the
2491 	 * merged strings instead of the original input sections.
2492 	 */
2493 	for (APLIST_TRAVERSE(*sym_aplist, idx, sdp)) {
2494 		/*
2495 		 * If we've already processed this symbol, don't do it
2496 		 * twice. strmerge_pass1() uses a heuristic (relocations to
2497 		 * the same symbol clump together) to avoid inserting a
2498 		 * given symbol more than once, but repeat symbols in
2499 		 * the list can occur.
2500 		 */
2501 		if ((sdp->sd_isc->is_flags & FLG_IS_INSTRMRG) == 0)
2502 			continue;
2503 
2504 		if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
2505 			/*
2506 			 * This is not an STT_SECTION symbol, so its
2507 			 * value is the offset of the string within the
2508 			 * input section. Update the address to reflect
2509 			 * the address in our new merged section.
2510 			 */
2511 			const char *name = sdp->sd_sym->st_value +
2512 			    (char *)sdp->sd_isc->is_indata->d_buf;
2513 
2514 			st_setstring_status =
2515 			    st_setstring(mstrtab, name, &stoff);
2516 			if (st_setstring_status == -1) {
2517 				/*
2518 				 * A failure to insert at this point means
2519 				 * something is corrupt. This isn't a
2520 				 * resource issue.
2521 				 */
2522 				assert(st_setstring_status != -1);
2523 				goto return_s_error;
2524 			}
2525 
2526 			if (ld_sym_copy(sdp) == S_ERROR)
2527 				goto return_s_error;
2528 			sdp->sd_sym->st_value = (Word)stoff;
2529 		}
2530 
2531 		/* Redirect the symbol to our new merged section */
2532 		sdp->sd_isc = mstrsec;
2533 	}
2534 
2535 	/*
2536 	 * There are no references left to the original input string sections.
2537 	 * Mark them as discarded so they don't go into the output image.
2538 	 * At the same time, add up the sizes of the replaced sections.
2539 	 */
2540 	data_size = 0;
2541 	for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
2542 		if (isp->is_flags & (FLG_IS_DISCARD | FLG_IS_GNSTRMRG))
2543 			continue;
2544 
2545 		data_size += isp->is_indata->d_size;
2546 
2547 		isp->is_flags |= FLG_IS_DISCARD;
2548 		DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp, mstrsec));
2549 	}
2550 
2551 	/* Report how much space we saved in the output section */
2552 	Dbg_sec_genstr_compress(ofl->ofl_lml, osp->os_name, data_size,
2553 	    mstr_data->d_size);
2554 
2555 	st_destroy(mstrtab);
2556 	return (1);
2557 
2558 return_s_error:
2559 	st_destroy(mstrtab);
2560 	return (S_ERROR);
2561 }
2562 
2563 
2564 /*
2565  * The following sections are built after all input file processing and symbol
2566  * validation has been carried out.  The order is important (because the
2567  * addition of a section adds a new symbol there is a chicken and egg problem
2568  * of maintaining the appropriate counts).  By maintaining a known order the
2569  * individual routines can compensate for later, known, additions.
2570  */
2571 uintptr_t
2572 ld_make_sections(Ofl_desc *ofl)
2573 {
2574 	ofl_flag_t	flags = ofl->ofl_flags;
2575 	Listnode	*lnp1;
2576 	Sg_desc		*sgp;
2577 
2578 	/*
2579 	 * Generate any special sections.
2580 	 */
2581 	if (flags & FLG_OF_ADDVERS)
2582 		if (make_comment(ofl) == S_ERROR)
2583 			return (S_ERROR);
2584 
2585 	if (make_interp(ofl) == S_ERROR)
2586 		return (S_ERROR);
2587 
2588 	if (make_cap(ofl) == S_ERROR)
2589 		return (S_ERROR);
2590 
2591 	if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
2592 	    &ofl->ofl_initarray) == S_ERROR)
2593 		return (S_ERROR);
2594 
2595 	if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
2596 	    &ofl->ofl_finiarray) == S_ERROR)
2597 		return (S_ERROR);
2598 
2599 	if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
2600 	    &ofl->ofl_preiarray) == S_ERROR)
2601 		return (S_ERROR);
2602 
2603 	/*
2604 	 * Make the .plt section.  This occurs after any other relocation
2605 	 * sections are generated (see reloc_init()) to ensure that the
2606 	 * associated relocation section is after all the other relocation
2607 	 * sections.
2608 	 */
2609 	if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
2610 		if (make_plt(ofl) == S_ERROR)
2611 			return (S_ERROR);
2612 
2613 	/*
2614 	 * Determine whether any sections or files are not referenced.  Under
2615 	 * -Dunused a diagnostic for any unused components is generated, under
2616 	 * -zignore the component is removed from the final output.
2617 	 */
2618 	if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
2619 		if (ignore_section_processing(ofl) == S_ERROR)
2620 			return (S_ERROR);
2621 	}
2622 
2623 	/*
2624 	 * Do any of the output sections contain input sections that
2625 	 * are candidates for string table merging? For each such case,
2626 	 * we create a replacement section, insert it, and discard the
2627 	 * originals.
2628 	 *
2629 	 * rel_aplist and sym_aplist are used by ld_make_strmerge()
2630 	 * for its internal processing. We are responsible for the
2631 	 * initialization and cleanup, and ld_make_strmerge() handles the rest.
2632 	 * This allows us to reuse a single pair of memory buffers allocatated
2633 	 * for this processing for all the output sections.
2634 	 */
2635 	if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) == 0) {
2636 		int error_seen = 0;
2637 		APlist *rel_aplist = NULL;
2638 		APlist *sym_aplist = NULL;
2639 
2640 		for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2641 			Os_desc	*osp;
2642 			Aliste	idx;
2643 
2644 			for (APLIST_TRAVERSE(sgp->sg_osdescs, idx, osp))
2645 				if ((osp->os_mstrisdescs != NULL) &&
2646 				    (ld_make_strmerge(ofl, osp,
2647 				    &rel_aplist, &sym_aplist) ==
2648 				    S_ERROR)) {
2649 					error_seen = 1;
2650 					break;
2651 				}
2652 		}
2653 		if (rel_aplist != NULL)
2654 			free(rel_aplist);
2655 		if (sym_aplist != NULL)
2656 			free(sym_aplist);
2657 		if (error_seen != 0)
2658 			return (S_ERROR);
2659 	}
2660 
2661 	/*
2662 	 * Add any necessary versioning information.
2663 	 */
2664 	if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED) {
2665 		if (make_verneed(ofl) == S_ERROR)
2666 			return (S_ERROR);
2667 	}
2668 	if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == FLG_OF_VERDEF) {
2669 		if (make_verdef(ofl) == S_ERROR)
2670 			return (S_ERROR);
2671 		if ((ofl->ofl_osversym = make_sym_sec(ofl,
2672 		    MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym,
2673 		    ld_targ.t_id.id_version)) == (Os_desc*)S_ERROR)
2674 			return (S_ERROR);
2675 	}
2676 
2677 	/*
2678 	 * Create a syminfo section if necessary.
2679 	 */
2680 	if (flags & FLG_OF_SYMINFO) {
2681 		if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
2682 		    MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo,
2683 		    ld_targ.t_id.id_syminfo)) == (Os_desc *)S_ERROR)
2684 			return (S_ERROR);
2685 	}
2686 
2687 	if (flags & FLG_OF_COMREL) {
2688 		/*
2689 		 * If -zcombreloc is enabled then all relocations (except for
2690 		 * the PLT's) are coalesced into a single relocation section.
2691 		 */
2692 		if (ofl->ofl_reloccnt) {
2693 			if (make_reloc(ofl, NULL) == S_ERROR)
2694 				return (S_ERROR);
2695 		}
2696 	} else {
2697 		/*
2698 		 * Create the required output relocation sections.  Note, new
2699 		 * sections may be added to the section list that is being
2700 		 * traversed.  These insertions can move the elements of the
2701 		 * Alist such that a section descriptor is re-read.  Recursion
2702 		 * is prevented by maintaining a previous section pointer and
2703 		 * insuring that this pointer isn't re-examined.
2704 		 */
2705 		for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2706 			Os_desc	*osp, *posp = 0;
2707 			Aliste	idx;
2708 
2709 			for (APLIST_TRAVERSE(sgp->sg_osdescs, idx, osp)) {
2710 				if ((osp != posp) && osp->os_szoutrels &&
2711 				    (osp != ofl->ofl_osplt)) {
2712 					if (make_reloc(ofl, osp) == S_ERROR)
2713 						return (S_ERROR);
2714 				}
2715 				posp = osp;
2716 			}
2717 		}
2718 
2719 		/*
2720 		 * If we're not building a combined relocation section, then
2721 		 * build a .rel[a] section as required.
2722 		 */
2723 		if (ofl->ofl_relocrelsz) {
2724 			if (make_reloc(ofl, NULL) == S_ERROR)
2725 				return (S_ERROR);
2726 		}
2727 	}
2728 
2729 	/*
2730 	 * The PLT relocations are always in their own section, and we try to
2731 	 * keep them at the end of the PLT table.  We do this to keep the hot
2732 	 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
2733 	 */
2734 	if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
2735 		if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
2736 			return (S_ERROR);
2737 	}
2738 
2739 	/*
2740 	 * Finally build the symbol and section header sections.
2741 	 */
2742 	if (flags & FLG_OF_DYNAMIC) {
2743 		if (make_dynamic(ofl) == S_ERROR)
2744 			return (S_ERROR);
2745 		if (make_dynstr(ofl) == S_ERROR)
2746 			return (S_ERROR);
2747 		/*
2748 		 * There is no use for .hash and .dynsym sections in a
2749 		 * relocatable object.
2750 		 */
2751 		if (!(flags & FLG_OF_RELOBJ)) {
2752 			if (make_hash(ofl) == S_ERROR)
2753 				return (S_ERROR);
2754 			if (make_dynsym(ofl) == S_ERROR)
2755 				return (S_ERROR);
2756 #if	defined(_ELF64)
2757 			if ((ld_targ.t_uw.uw_make_unwindhdr != NULL) &&
2758 			    ((*ld_targ.t_uw.uw_make_unwindhdr)(ofl) == S_ERROR))
2759 				return (S_ERROR);
2760 #endif
2761 			if (make_dynsort(ofl) == S_ERROR)
2762 				return (S_ERROR);
2763 		}
2764 	}
2765 
2766 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
2767 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
2768 		/*
2769 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2770 		 * for the dynsym.  If so - do it now.
2771 		 */
2772 		if (ofl->ofl_osdynsym &&
2773 		    ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
2774 			if (make_dynsym_shndx(ofl) == S_ERROR)
2775 				return (S_ERROR);
2776 		}
2777 
2778 		if (make_strtab(ofl) == S_ERROR)
2779 			return (S_ERROR);
2780 		if (make_symtab(ofl) == S_ERROR)
2781 			return (S_ERROR);
2782 	} else {
2783 		/*
2784 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2785 		 * for the dynsym.  If so - do it now.
2786 		 */
2787 		if (ofl->ofl_osdynsym &&
2788 		    ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
2789 			if (make_dynsym_shndx(ofl) == S_ERROR)
2790 				return (S_ERROR);
2791 		}
2792 	}
2793 
2794 	if (make_shstrtab(ofl) == S_ERROR)
2795 		return (S_ERROR);
2796 
2797 	/*
2798 	 * Now that we've created all of our sections adjust the size
2799 	 * of SHT_SUNW_versym & SHT_SUNW_syminfo which are dependent on
2800 	 * the symbol table sizes.
2801 	 */
2802 	if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
2803 		Shdr		*shdr;
2804 		Is_desc		*isec;
2805 		Elf_Data	*data;
2806 		size_t		size;
2807 		ulong_t		cnt;
2808 		Os_desc		*osp;
2809 
2810 		if (flags & (FLG_OF_RELOBJ | FLG_OF_STATIC)) {
2811 			osp = ofl->ofl_ossymtab;
2812 		} else {
2813 			osp = ofl->ofl_osdynsym;
2814 		}
2815 		isec = (Is_desc *)osp->os_isdescs.head->data;
2816 		cnt = (isec->is_shdr->sh_size / isec->is_shdr->sh_entsize);
2817 
2818 		if (ofl->ofl_osversym) {
2819 			osp = ofl->ofl_osversym;
2820 			isec = (Is_desc *)osp->os_isdescs.head->data;
2821 			data = isec->is_indata;
2822 			shdr = osp->os_shdr;
2823 			size = cnt * shdr->sh_entsize;
2824 			shdr->sh_size = (Xword)size;
2825 			data->d_size = size;
2826 		}
2827 		if (ofl->ofl_ossyminfo) {
2828 			osp = ofl->ofl_ossyminfo;
2829 			isec = (Is_desc *)osp->os_isdescs.head->data;
2830 			data = isec->is_indata;
2831 			shdr = osp->os_shdr;
2832 			size = cnt * shdr->sh_entsize;
2833 			shdr->sh_size = (Xword)size;
2834 			data->d_size = size;
2835 		}
2836 	}
2837 
2838 	return (1);
2839 }
2840 
2841 /*
2842  * Build an additional data section - used to back OBJT symbol definitions
2843  * added with a mapfile.
2844  */
2845 Is_desc *
2846 ld_make_data(Ofl_desc *ofl, size_t size)
2847 {
2848 	Shdr		*shdr;
2849 	Elf_Data	*data;
2850 	Is_desc		*isec;
2851 
2852 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2853 	    &isec, &shdr, &data) == S_ERROR)
2854 		return ((Is_desc *)S_ERROR);
2855 
2856 	data->d_size = size;
2857 	shdr->sh_size = (Xword)size;
2858 	shdr->sh_flags |= SHF_WRITE;
2859 
2860 	if (ld_place_section(ofl, isec, ld_targ.t_id.id_data, 0) ==
2861 	    (Os_desc *)S_ERROR)
2862 		return ((Is_desc *)S_ERROR);
2863 
2864 	return (isec);
2865 }
2866 
2867 
2868 /*
2869  * Build an additional text section - used to back FUNC symbol definitions
2870  * added with a mapfile.
2871  */
2872 Is_desc *
2873 ld_make_text(Ofl_desc *ofl, size_t size)
2874 {
2875 	Shdr		*shdr;
2876 	Elf_Data	*data;
2877 	Is_desc		*isec;
2878 
2879 	/*
2880 	 * Insure the size is sufficient to contain the minimum return
2881 	 * instruction.
2882 	 */
2883 	if (size < ld_targ.t_nf.nf_size)
2884 		size = ld_targ.t_nf.nf_size;
2885 
2886 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0,
2887 	    &isec, &shdr, &data) == S_ERROR)
2888 		return ((Is_desc *)S_ERROR);
2889 
2890 	data->d_size = size;
2891 	shdr->sh_size = (Xword)size;
2892 	shdr->sh_flags |= SHF_EXECINSTR;
2893 
2894 	/*
2895 	 * Fill the buffer with the appropriate return instruction.
2896 	 * Note that there is no need to swap bytes on a non-native,
2897 	 * link, as the data being copied is given in bytes.
2898 	 */
2899 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2900 		return ((Is_desc *)S_ERROR);
2901 	(void) memcpy(data->d_buf, ld_targ.t_nf.nf_template,
2902 	    ld_targ.t_nf.nf_size);
2903 
2904 	if (ld_place_section(ofl, isec, ld_targ.t_id.id_text, 0) ==
2905 	    (Os_desc *)S_ERROR)
2906 		return ((Is_desc *)S_ERROR);
2907 
2908 	return (isec);
2909 }
2910