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