xref: /illumos-gate/usr/src/cmd/sgs/libld/common/sections.c (revision 5df5713f81d69c1a0797f99b13e95e220da00ef9)
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;
327 
328 			for (APLIST_TRAVERSE(osp->os_isdescs, 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, 0)) == (Os_desc *)S_ERROR)
749 		return (S_ERROR);
750 
751 	/*
752 	 * If relocations exist against a .*bss section, a section symbol must
753 	 * be created for the section in the .dynsym symbol table.
754 	 */
755 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
756 		ofl_flag_t	flagtotest;
757 
758 		if (ident == ld_targ.t_id.id_tlsbss)
759 			flagtotest = FLG_OF1_TLSOREL;
760 		else
761 			flagtotest = FLG_OF1_BSSOREL;
762 
763 		if (ofl->ofl_flags1 & flagtotest) {
764 			ofl->ofl_dynshdrcnt++;
765 			osp->os_flags |= FLG_OS_OUTREL;
766 		}
767 	}
768 
769 	osp->os_szoutrels = rsize;
770 	return (1);
771 }
772 
773 /*
774  * Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via
775  * ld -z *array=name).
776  */
777 static uintptr_t
778 make_array(Ofl_desc *ofl, Word shtype, const char *sectname, APlist *alp)
779 {
780 	uint_t		entcount;
781 	Aliste		idx;
782 	Elf_Data	*data;
783 	Is_desc		*isec;
784 	Shdr		*shdr;
785 	Sym_desc	*sdp;
786 	Rel_desc	reld;
787 	Rela		reloc;
788 	Os_desc		*osp;
789 	uintptr_t	ret = 1;
790 
791 	if (alp == NULL)
792 		return (1);
793 
794 	entcount = 0;
795 	for (APLIST_TRAVERSE(alp, idx, sdp))
796 		entcount++;
797 
798 	if (new_section(ofl, shtype, sectname, entcount, &isec, &shdr, &data) ==
799 	    S_ERROR)
800 		return (S_ERROR);
801 
802 	if ((data->d_buf = libld_calloc(sizeof (Addr), entcount)) == NULL)
803 		return (S_ERROR);
804 
805 	if (ld_place_section(ofl, isec, ld_targ.t_id.id_array, 0) ==
806 	    (Os_desc *)S_ERROR)
807 		return (S_ERROR);
808 
809 	osp = isec->is_osdesc;
810 
811 	if ((ofl->ofl_osinitarray == NULL) && (shtype == SHT_INIT_ARRAY))
812 		ofl->ofl_osinitarray = osp;
813 	if ((ofl->ofl_ospreinitarray == NULL) && (shtype == SHT_PREINIT_ARRAY))
814 		ofl->ofl_ospreinitarray = osp;
815 	else if ((ofl->ofl_osfiniarray == NULL) && (shtype == SHT_FINI_ARRAY))
816 		ofl->ofl_osfiniarray = osp;
817 
818 	/*
819 	 * Create relocations against this section to initialize it to the
820 	 * function addresses.
821 	 */
822 	reld.rel_osdesc = osp;
823 	reld.rel_isdesc = isec;
824 	reld.rel_move = 0;
825 	reld.rel_flags = FLG_REL_LOAD;
826 
827 	/*
828 	 * Fabricate the relocation information (as if a relocation record had
829 	 * been input - see init_rel()).
830 	 */
831 	reld.rel_rtype = ld_targ.t_m.m_r_arrayaddr;
832 	reld.rel_roffset = 0;
833 	reld.rel_raddend = 0;
834 	reld.rel_typedata = 0;
835 
836 	/*
837 	 * Create a minimal relocation record to satisfy process_sym_reloc()
838 	 * debugging requirements.
839 	 */
840 	reloc.r_offset = 0;
841 	reloc.r_info = ELF_R_INFO(0, ld_targ.t_m.m_r_arrayaddr);
842 	reloc.r_addend = 0;
843 
844 	DBG_CALL(Dbg_reloc_generate(ofl->ofl_lml, osp,
845 	    ld_targ.t_m.m_rel_sht_type));
846 	for (APLIST_TRAVERSE(alp, idx, sdp)) {
847 		reld.rel_sname = sdp->sd_name;
848 		reld.rel_sym = sdp;
849 
850 		if (ld_process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec,
851 		    MSG_INTL(MSG_STR_COMMAND), 0) == S_ERROR) {
852 			ret = S_ERROR;
853 			continue;
854 		}
855 
856 		reld.rel_roffset += (Xword)sizeof (Addr);
857 		reloc.r_offset = reld.rel_roffset;
858 	}
859 
860 	return (ret);
861 }
862 
863 /*
864  * Build a comment section (-Qy option).
865  */
866 static uintptr_t
867 make_comment(Ofl_desc *ofl)
868 {
869 	Shdr		*shdr;
870 	Elf_Data	*data;
871 	Is_desc		*isec;
872 
873 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_COMMENT), 0,
874 	    &isec, &shdr, &data) == S_ERROR)
875 		return (S_ERROR);
876 
877 	data->d_buf = (void *)ofl->ofl_sgsid;
878 	data->d_size = strlen(ofl->ofl_sgsid) + 1;
879 	data->d_align = 1;
880 
881 	shdr->sh_size = (Xword)data->d_size;
882 	shdr->sh_flags = 0;
883 	shdr->sh_addralign = 1;
884 
885 	return ((uintptr_t)ld_place_section(ofl, isec,
886 	    ld_targ.t_id.id_note, 0));
887 }
888 
889 /*
890  * Make the dynamic section.  Calculate the size of any strings referenced
891  * within this structure, they will be added to the global string table
892  * (.dynstr).  This routine should be called before make_dynstr().
893  *
894  * This routine must be maintained in parallel with update_odynamic()
895  * in update.c
896  */
897 static uintptr_t
898 make_dynamic(Ofl_desc *ofl)
899 {
900 	Shdr		*shdr;
901 	Os_desc		*osp;
902 	Elf_Data	*data;
903 	Is_desc		*isec;
904 	size_t		cnt = 0;
905 	Aliste		idx;
906 	Ifl_desc	*ifl;
907 	Sym_desc	*sdp;
908 	size_t		size;
909 	ofl_flag_t	flags = ofl->ofl_flags;
910 	int		not_relobj = !(flags & FLG_OF_RELOBJ);
911 	int		unused = 0;
912 
913 	/*
914 	 * Only a limited subset of DT_ entries apply to relocatable
915 	 * objects. See the comment at the head of update_odynamic() in
916 	 * update.c for details.
917 	 */
918 	if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0,
919 	    &isec, &shdr, &data) == S_ERROR)
920 		return (S_ERROR);
921 
922 	/* new_section() does not set SHF_ALLOC. Add it if needed */
923 	if (not_relobj)
924 		shdr->sh_flags |= SHF_ALLOC;
925 
926 	osp = ofl->ofl_osdynamic =
927 	    ld_place_section(ofl, isec, ld_targ.t_id.id_dynamic, 0);
928 
929 	/*
930 	 * Reserve entries for any needed dependencies.
931 	 */
932 	for (APLIST_TRAVERSE(ofl->ofl_sos, idx, ifl)) {
933 		Sdf_desc	*sdf;
934 
935 		if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR)))
936 			continue;
937 
938 		/*
939 		 * If this dependency didn't satisfy any symbol references,
940 		 * generate a debugging diagnostic (ld(1) -Dunused can be used
941 		 * to display these).  If this is a standard needed dependency,
942 		 * and -z ignore is in effect, drop the dependency.  Explicitly
943 		 * defined dependencies (i.e., -N dep) don't get dropped, and
944 		 * are flagged as being required to simplify update_odynamic()
945 		 * processing.
946 		 */
947 		if ((ifl->ifl_flags & FLG_IF_NEEDSTR) ||
948 		    ((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) {
949 			if (unused++ == 0)
950 				DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
951 			DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname,
952 			    (ifl->ifl_flags & FLG_IF_NEEDSTR), 0));
953 
954 			if (ifl->ifl_flags & FLG_IF_NEEDSTR)
955 				ifl->ifl_flags |= FLG_IF_DEPREQD;
956 			else if (ifl->ifl_flags & FLG_IF_IGNORE)
957 				continue;
958 		}
959 
960 		/*
961 		 * If this object has an accompanying shared object definition
962 		 * determine if an alternative shared object name has been
963 		 * specified.
964 		 */
965 		if (((sdf = ifl->ifl_sdfdesc) != NULL) &&
966 		    (sdf->sdf_flags & FLG_SDF_SONAME))
967 			ifl->ifl_soname = sdf->sdf_soname;
968 
969 		/*
970 		 * If this object is a lazyload reserve a DT_POSFLAG_1 entry.
971 		 */
972 		if ((ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_GRPPRM)) &&
973 		    not_relobj)
974 			cnt++;
975 
976 		if (st_insert(ofl->ofl_dynstrtab, ifl->ifl_soname) == -1)
977 			return (S_ERROR);
978 		cnt++;
979 
980 		/*
981 		 * If the needed entry contains the $ORIGIN token make sure
982 		 * the associated DT_1_FLAGS entry is created.
983 		 */
984 		if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) {
985 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
986 			ofl->ofl_dtflags |= DF_ORIGIN;
987 		}
988 	}
989 
990 	if (unused)
991 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
992 
993 	if (not_relobj) {
994 		/*
995 		 * Reserve entries for any per-symbol auxiliary/filter strings.
996 		 */
997 		cnt += alist_nitems(ofl->ofl_dtsfltrs);
998 
999 		/*
1000 		 * Reserve entries for _init() and _fini() section addresses.
1001 		 */
1002 		if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
1003 		    SYM_NOHASH, 0, ofl)) != NULL) &&
1004 		    (sdp->sd_ref == REF_REL_NEED) &&
1005 		    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1006 			sdp->sd_flags |= FLG_SY_UPREQD;
1007 			cnt++;
1008 		}
1009 		if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
1010 		    SYM_NOHASH, 0, ofl)) != NULL) &&
1011 		    (sdp->sd_ref == REF_REL_NEED) &&
1012 		    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1013 			sdp->sd_flags |= FLG_SY_UPREQD;
1014 			cnt++;
1015 		}
1016 
1017 		/*
1018 		 * Reserve entries for any soname, filter name (shared libs
1019 		 * only), run-path pointers, cache names and audit requirements.
1020 		 */
1021 		if (ofl->ofl_soname) {
1022 			cnt++;
1023 			if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_soname) ==
1024 			    -1)
1025 				return (S_ERROR);
1026 		}
1027 		if (ofl->ofl_filtees) {
1028 			cnt++;
1029 			if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_filtees) ==
1030 			    -1)
1031 				return (S_ERROR);
1032 
1033 			/*
1034 			 * If the filtees entry contains the $ORIGIN token
1035 			 * make sure the associated DT_1_FLAGS entry is created.
1036 			 */
1037 			if (strstr(ofl->ofl_filtees,
1038 			    MSG_ORIG(MSG_STR_ORIGIN))) {
1039 				ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1040 				ofl->ofl_dtflags |= DF_ORIGIN;
1041 			}
1042 		}
1043 	}
1044 
1045 	if (ofl->ofl_rpath) {
1046 		cnt += 2;	/* DT_RPATH & DT_RUNPATH */
1047 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_rpath) == -1)
1048 			return (S_ERROR);
1049 
1050 		/*
1051 		 * If the rpath entry contains the $ORIGIN token make sure
1052 		 * the associated DT_1_FLAGS entry is created.
1053 		 */
1054 		if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) {
1055 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1056 			ofl->ofl_dtflags |= DF_ORIGIN;
1057 		}
1058 	}
1059 
1060 	if (not_relobj) {
1061 		Aliste	idx;
1062 
1063 		if (ofl->ofl_config) {
1064 			cnt++;
1065 			if (st_insert(ofl->ofl_dynstrtab,
1066 			    ofl->ofl_config) == -1)
1067 				return (S_ERROR);
1068 
1069 			/*
1070 			 * If the config entry contains the $ORIGIN token
1071 			 * make sure the associated DT_1_FLAGS entry is created.
1072 			 */
1073 			if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) {
1074 				ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1075 				ofl->ofl_dtflags |= DF_ORIGIN;
1076 			}
1077 		}
1078 		if (ofl->ofl_depaudit) {
1079 			cnt++;
1080 			if (st_insert(ofl->ofl_dynstrtab,
1081 			    ofl->ofl_depaudit) == -1)
1082 				return (S_ERROR);
1083 		}
1084 		if (ofl->ofl_audit) {
1085 			cnt++;
1086 			if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_audit) == -1)
1087 				return (S_ERROR);
1088 		}
1089 
1090 		/*
1091 		 * Reserve entries for the HASH, STRTAB, STRSZ, SYMTAB, SYMENT,
1092 		 * and CHECKSUM.
1093 		 */
1094 		cnt += 6;
1095 
1096 		/*
1097 		 * If we are including local functions at the head of
1098 		 * the dynsym, then also reserve entries for DT_SUNW_SYMTAB
1099 		 * and DT_SUNW_SYMSZ.
1100 		 */
1101 		if (OFL_ALLOW_LDYNSYM(ofl))
1102 			cnt += 2;
1103 
1104 		if ((ofl->ofl_dynsymsortcnt > 0) ||
1105 		    (ofl->ofl_dyntlssortcnt > 0))
1106 			cnt++;		/* DT_SUNW_SORTENT */
1107 
1108 		if (ofl->ofl_dynsymsortcnt > 0)
1109 			cnt += 2;	/* DT_SUNW_[SYMSORT|SYMSORTSZ] */
1110 
1111 		if (ofl->ofl_dyntlssortcnt > 0)
1112 			cnt += 2;	/* DT_SUNW_[TLSSORT|TLSSORTSZ] */
1113 
1114 		if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
1115 		    FLG_OF_VERDEF)
1116 			cnt += 2;		/* DT_VERDEF & DT_VERDEFNUM */
1117 
1118 		if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
1119 		    FLG_OF_VERNEED)
1120 			cnt += 2;		/* DT_VERNEED & DT_VERNEEDNUM */
1121 
1122 		if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt)
1123 			cnt++;			/* RELACOUNT */
1124 
1125 		if (flags & FLG_OF_TEXTREL)	/* TEXTREL */
1126 			cnt++;
1127 
1128 		if (ofl->ofl_osfiniarray)	/* FINI_ARRAY & FINI_ARRAYSZ */
1129 			cnt += 2;
1130 
1131 		if (ofl->ofl_osinitarray)	/* INIT_ARRAY & INIT_ARRAYSZ */
1132 			cnt += 2;
1133 
1134 		if (ofl->ofl_ospreinitarray)	/* PREINIT_ARRAY & */
1135 			cnt += 2;		/*	PREINIT_ARRAYSZ */
1136 
1137 		/*
1138 		 * If we have plt's reserve a PLT, PLTSZ, PLTREL and JMPREL.
1139 		 */
1140 		if (ofl->ofl_pltcnt)
1141 			cnt += 3;
1142 
1143 		/*
1144 		 * If pltpadding is needed (Sparcv9)
1145 		 */
1146 		if (ofl->ofl_pltpad)
1147 			cnt += 2;		/* DT_PLTPAD & DT_PLTPADSZ */
1148 
1149 		/*
1150 		 * If we have any relocations reserve a REL, RELSZ and
1151 		 * RELENT entry.
1152 		 */
1153 		if (ofl->ofl_relocsz)
1154 			cnt += 3;
1155 
1156 		/*
1157 		 * If a syminfo section is required create SYMINFO, SYMINSZ,
1158 		 * and SYMINENT entries.
1159 		 */
1160 		if (flags & FLG_OF_SYMINFO)
1161 			cnt += 3;
1162 
1163 		/*
1164 		 * If there are any partially initialized sections allocate
1165 		 * MOVEENT, MOVESZ and MOVETAB.
1166 		 */
1167 		if (ofl->ofl_osmove)
1168 			cnt += 3;
1169 
1170 		/*
1171 		 * Allocate one DT_REGISTER entry for every register symbol.
1172 		 */
1173 		cnt += ofl->ofl_regsymcnt;
1174 
1175 		/*
1176 		 * Reserve a entry for each '-zrtldinfo=...' specified
1177 		 * on the command line.
1178 		 */
1179 		for (APLIST_TRAVERSE(ofl->ofl_rtldinfo, idx, sdp))
1180 			cnt++;
1181 
1182 		/*
1183 		 * These two entries should only be placed in a segment
1184 		 * which is writable.  If it's a read-only segment
1185 		 * (due to mapfile magic, e.g. libdl.so.1) then don't allocate
1186 		 * these entries.
1187 		 */
1188 		if ((osp->os_sgdesc) &&
1189 		    (osp->os_sgdesc->sg_phdr.p_flags & PF_W)) {
1190 			cnt++;			/* FEATURE_1 */
1191 
1192 			if (ofl->ofl_osinterp)
1193 				cnt++;		/* DEBUG */
1194 		}
1195 
1196 		/*
1197 		 * Any hardware/software capabilities?
1198 		 */
1199 		if (ofl->ofl_oscap)
1200 			cnt++;			/* SUNW_CAP */
1201 
1202 		if (flags & FLG_OF_SYMBOLIC)
1203 			cnt++;			/* SYMBOLIC */
1204 	}
1205 
1206 	/*
1207 	 * Account for Architecture dependent .dynamic entries, and defaults.
1208 	 */
1209 	(*ld_targ.t_mr.mr_mach_make_dynamic)(ofl, &cnt);
1210 
1211 	/*
1212 	 * DT_FLAGS, DT_FLAGS_1, DT_SUNW_STRPAD, and DT_NULL. Also,
1213 	 * allow room for the unused extra DT_NULLs. These are included
1214 	 * to allow an ELF editor room to add items later.
1215 	 */
1216 	cnt += 4 + DYNAMIC_EXTRA_ELTS;
1217 
1218 	/*
1219 	 * DT_SUNW_LDMACH. Used to hold the ELF machine code of the
1220 	 * linker that produced the output object. This information
1221 	 * allows us to determine whether a given object was linked
1222 	 * natively, or by a linker running on a different type of
1223 	 * system. This information can be valuable if one suspects
1224 	 * that a problem might be due to alignment or byte order issues.
1225 	 */
1226 	cnt++;
1227 
1228 	/*
1229 	 * Determine the size of the section from the number of entries.
1230 	 */
1231 	size = cnt * (size_t)shdr->sh_entsize;
1232 
1233 	shdr->sh_size = (Xword)size;
1234 	data->d_size = size;
1235 
1236 	/*
1237 	 * There are several tags that are specific to the Solaris osabi
1238 	 * range which we unconditionally put into any dynamic section
1239 	 * we create (e.g. DT_SUNW_STRPAD or DT_SUNW_LDMACH). As such,
1240 	 * any Solaris object with a dynamic section should be tagged as
1241 	 * ELFOSABI_SOLARIS.
1242 	 */
1243 	ofl->ofl_flags |= FLG_OF_OSABI;
1244 
1245 	return ((uintptr_t)ofl->ofl_osdynamic);
1246 }
1247 
1248 /*
1249  * Build the GOT section and its associated relocation entries.
1250  */
1251 uintptr_t
1252 ld_make_got(Ofl_desc *ofl)
1253 {
1254 	Elf_Data	*data;
1255 	Shdr	*shdr;
1256 	Is_desc	*isec;
1257 	size_t	size = (size_t)ofl->ofl_gotcnt * ld_targ.t_m.m_got_entsize;
1258 	size_t	rsize = (size_t)ofl->ofl_relocgotsz;
1259 
1260 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0,
1261 	    &isec, &shdr, &data) == S_ERROR)
1262 		return (S_ERROR);
1263 
1264 	data->d_size = size;
1265 
1266 	shdr->sh_flags |= SHF_WRITE;
1267 	shdr->sh_size = (Xword)size;
1268 	shdr->sh_entsize = ld_targ.t_m.m_got_entsize;
1269 
1270 	ofl->ofl_osgot = ld_place_section(ofl, isec, ld_targ.t_id.id_got, 0);
1271 	if (ofl->ofl_osgot == (Os_desc *)S_ERROR)
1272 		return (S_ERROR);
1273 
1274 	ofl->ofl_osgot->os_szoutrels = (Xword)rsize;
1275 
1276 	return (1);
1277 }
1278 
1279 /*
1280  * Build an interpreter section.
1281  */
1282 static uintptr_t
1283 make_interp(Ofl_desc *ofl)
1284 {
1285 	Shdr		*shdr;
1286 	Elf_Data	*data;
1287 	Is_desc		*isec;
1288 	const char	*iname = ofl->ofl_interp;
1289 	size_t		size;
1290 
1291 	/*
1292 	 * If -z nointerp is in effect, don't create an interpreter section.
1293 	 */
1294 	if (ofl->ofl_flags1 & FLG_OF1_NOINTRP)
1295 		return (1);
1296 
1297 	/*
1298 	 * We always build an .interp section for dynamic executables.  However
1299 	 * if the user has specifically specified an interpreter we'll build
1300 	 * this section for any output (presumably the user knows what they are
1301 	 * doing. refer ABI section 5-4, and ld.1 man page use of -I).
1302 	 */
1303 	if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC |
1304 	    FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname)
1305 		return (1);
1306 
1307 	/*
1308 	 * In the case of a dynamic executable supply a default interpreter
1309 	 * if a specific interpreter has not been specified.
1310 	 */
1311 	if (iname == NULL)
1312 		iname = ofl->ofl_interp = ld_targ.t_m.m_def_interp;
1313 
1314 	size = strlen(iname) + 1;
1315 
1316 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0,
1317 	    &isec, &shdr, &data) == S_ERROR)
1318 		return (S_ERROR);
1319 
1320 	data->d_size = size;
1321 	shdr->sh_size = (Xword)size;
1322 	data->d_align = shdr->sh_addralign = 1;
1323 
1324 	ofl->ofl_osinterp =
1325 	    ld_place_section(ofl, isec, ld_targ.t_id.id_interp, 0);
1326 	return ((uintptr_t)ofl->ofl_osinterp);
1327 }
1328 
1329 /*
1330  * Build a hardware/software capabilities section.
1331  */
1332 static uintptr_t
1333 make_cap(Ofl_desc *ofl)
1334 {
1335 	Shdr		*shdr;
1336 	Elf_Data	*data;
1337 	Is_desc		*isec;
1338 	Os_desc		*osec;
1339 	Cap		*cap;
1340 	size_t		size = 0;
1341 
1342 	/*
1343 	 * Determine how many entries are required.
1344 	 */
1345 	if (ofl->ofl_hwcap_1)
1346 		size++;
1347 	if (ofl->ofl_sfcap_1)
1348 		size++;
1349 	if (size == 0)
1350 		return (1);
1351 	size++;				/* Add CA_SUNW_NULL */
1352 
1353 	if (new_section(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP), size,
1354 	    &isec, &shdr, &data) == S_ERROR)
1355 		return (S_ERROR);
1356 
1357 	if ((data->d_buf = libld_malloc(shdr->sh_size)) == NULL)
1358 		return (S_ERROR);
1359 
1360 	cap = (Cap *)data->d_buf;
1361 	if (ofl->ofl_hwcap_1) {
1362 		cap->c_tag = CA_SUNW_HW_1;
1363 		cap->c_un.c_val = ofl->ofl_hwcap_1;
1364 		cap++;
1365 	}
1366 	if (ofl->ofl_sfcap_1) {
1367 		cap->c_tag = CA_SUNW_SF_1;
1368 		cap->c_un.c_val = ofl->ofl_sfcap_1;
1369 		cap++;
1370 	}
1371 	cap->c_tag = CA_SUNW_NULL;
1372 	cap->c_un.c_val = 0;
1373 
1374 	/*
1375 	 * If we're not creating a relocatable object, save the output section
1376 	 * to trigger the creation of an associated program header.
1377 	 */
1378 	osec = ld_place_section(ofl, isec, ld_targ.t_id.id_cap, 0);
1379 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
1380 		ofl->ofl_oscap = osec;
1381 
1382 	return ((uintptr_t)osec);
1383 }
1384 
1385 /*
1386  * Build the PLT section and its associated relocation entries.
1387  */
1388 static uintptr_t
1389 make_plt(Ofl_desc *ofl)
1390 {
1391 	Shdr		*shdr;
1392 	Elf_Data	*data;
1393 	Is_desc		*isec;
1394 	size_t		size = ld_targ.t_m.m_plt_reservsz +
1395 	    (((size_t)ofl->ofl_pltcnt + (size_t)ofl->ofl_pltpad) *
1396 	    ld_targ.t_m.m_plt_entsize);
1397 	size_t		rsize = (size_t)ofl->ofl_relocpltsz;
1398 
1399 	/*
1400 	 * On sparc, account for the NOP at the end of the plt.
1401 	 */
1402 	if (ld_targ.t_m.m_mach == LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
1403 		size += sizeof (Word);
1404 
1405 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0,
1406 	    &isec, &shdr, &data) == S_ERROR)
1407 		return (S_ERROR);
1408 
1409 	data->d_size = size;
1410 	data->d_align = ld_targ.t_m.m_plt_align;
1411 
1412 	shdr->sh_flags = ld_targ.t_m.m_plt_shf_flags;
1413 	shdr->sh_size = (Xword)size;
1414 	shdr->sh_addralign = ld_targ.t_m.m_plt_align;
1415 	shdr->sh_entsize = ld_targ.t_m.m_plt_entsize;
1416 
1417 	ofl->ofl_osplt = ld_place_section(ofl, isec, ld_targ.t_id.id_plt, 0);
1418 	if (ofl->ofl_osplt == (Os_desc *)S_ERROR)
1419 		return (S_ERROR);
1420 
1421 	ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1422 
1423 	return (1);
1424 }
1425 
1426 /*
1427  * Make the hash table.  Only built for dynamic executables and shared
1428  * libraries, and provides hashed lookup into the global symbol table
1429  * (.dynsym) for the run-time linker to resolve symbol lookups.
1430  */
1431 static uintptr_t
1432 make_hash(Ofl_desc *ofl)
1433 {
1434 	Shdr		*shdr;
1435 	Elf_Data	*data;
1436 	Is_desc		*isec;
1437 	size_t		size;
1438 	Word		nsyms = ofl->ofl_globcnt;
1439 	size_t		cnt;
1440 
1441 	/*
1442 	 * Allocate section header structures. We set entcnt to 0
1443 	 * because it's going to change after we place this section.
1444 	 */
1445 	if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0,
1446 	    &isec, &shdr, &data) == S_ERROR)
1447 		return (S_ERROR);
1448 
1449 	/*
1450 	 * Place the section first since it will affect the local symbol
1451 	 * count.
1452 	 */
1453 	ofl->ofl_oshash = ld_place_section(ofl, isec, ld_targ.t_id.id_hash, 0);
1454 	if (ofl->ofl_oshash == (Os_desc *)S_ERROR)
1455 		return (S_ERROR);
1456 
1457 	/*
1458 	 * Calculate the number of output hash buckets.
1459 	 */
1460 	ofl->ofl_hashbkts = findprime(nsyms);
1461 
1462 	/*
1463 	 * The size of the hash table is determined by
1464 	 *
1465 	 *	i.	the initial nbucket and nchain entries (2)
1466 	 *	ii.	the number of buckets (calculated above)
1467 	 *	iii.	the number of chains (this is based on the number of
1468 	 *		symbols in the .dynsym array + NULL symbol).
1469 	 */
1470 	cnt = 2 + ofl->ofl_hashbkts + (ofl->ofl_dynshdrcnt +
1471 	    ofl->ofl_globcnt + ofl->ofl_lregsymcnt + 1);
1472 	size = cnt * shdr->sh_entsize;
1473 
1474 	/*
1475 	 * Finalize the section header and data buffer initialization.
1476 	 */
1477 	if ((data->d_buf = libld_calloc(size, 1)) == NULL)
1478 		return (S_ERROR);
1479 	data->d_size = size;
1480 	shdr->sh_size = (Xword)size;
1481 
1482 	return (1);
1483 }
1484 
1485 /*
1486  * Generate the standard symbol table.  Contains all locals and globals,
1487  * and resides in a non-allocatable section (ie. it can be stripped).
1488  */
1489 static uintptr_t
1490 make_symtab(Ofl_desc *ofl)
1491 {
1492 	Shdr		*shdr;
1493 	Elf_Data	*data;
1494 	Is_desc		*isec;
1495 	Is_desc		*xisec = 0;
1496 	size_t		size;
1497 	Word		symcnt;
1498 
1499 	/*
1500 	 * Create the section headers. Note that we supply an ent_cnt
1501 	 * of 0. We won't know the count until the section has been placed.
1502 	 */
1503 	if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0,
1504 	    &isec, &shdr, &data) == S_ERROR)
1505 		return (S_ERROR);
1506 
1507 	/*
1508 	 * Place the section first since it will affect the local symbol
1509 	 * count.
1510 	 */
1511 	ofl->ofl_ossymtab =
1512 	    ld_place_section(ofl, isec, ld_targ.t_id.id_symtab, 0);
1513 	if (ofl->ofl_ossymtab == (Os_desc *)S_ERROR)
1514 		return (S_ERROR);
1515 
1516 	/*
1517 	 * At this point we've created all but the 'shstrtab' section.
1518 	 * Determine if we have to use 'Extended Sections'.  If so - then
1519 	 * also create a SHT_SYMTAB_SHNDX section.
1520 	 */
1521 	if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
1522 		Shdr		*xshdr;
1523 		Elf_Data	*xdata;
1524 
1525 		if (new_section(ofl, SHT_SYMTAB_SHNDX,
1526 		    MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec,
1527 		    &xshdr, &xdata) == S_ERROR)
1528 			return (S_ERROR);
1529 
1530 		if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec,
1531 		    ld_targ.t_id.id_symtab_ndx, 0)) == (Os_desc *)S_ERROR)
1532 			return (S_ERROR);
1533 	}
1534 
1535 	/*
1536 	 * Calculated number of symbols, which need to be augmented by
1537 	 * the null first entry, the FILE symbol, and the .shstrtab entry.
1538 	 */
1539 	symcnt = (size_t)(3 + ofl->ofl_shdrcnt + ofl->ofl_scopecnt +
1540 	    ofl->ofl_locscnt + ofl->ofl_globcnt);
1541 	size = symcnt * shdr->sh_entsize;
1542 
1543 	/*
1544 	 * Finalize the section header and data buffer initialization.
1545 	 */
1546 	data->d_size = size;
1547 	shdr->sh_size = (Xword)size;
1548 
1549 	/*
1550 	 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
1551 	 */
1552 	if (xisec) {
1553 		size_t	xsize = symcnt * sizeof (Word);
1554 
1555 		xisec->is_indata->d_size = xsize;
1556 		xisec->is_shdr->sh_size = (Xword)xsize;
1557 	}
1558 
1559 	return (1);
1560 }
1561 
1562 
1563 /*
1564  * Build a dynamic symbol table. These tables reside in the text
1565  * segment of a dynamic executable or shared library.
1566  *
1567  *	.SUNW_ldynsym contains local function symbols
1568  *	.dynsym contains only globals symbols
1569  *
1570  * The two tables are created adjacent to each other, with .SUNW_ldynsym
1571  * coming first.
1572  */
1573 static uintptr_t
1574 make_dynsym(Ofl_desc *ofl)
1575 {
1576 	Shdr		*shdr, *lshdr;
1577 	Elf_Data	*data, *ldata;
1578 	Is_desc		*isec, *lisec;
1579 	size_t		size;
1580 	Xword		cnt;
1581 	int		allow_ldynsym;
1582 
1583 	/*
1584 	 * Unless explicitly disabled, always produce a .SUNW_ldynsym section
1585 	 * when it is allowed by the file type, even if the resulting
1586 	 * table only ends up with a single STT_FILE in it. There are
1587 	 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB
1588 	 * entry in the .dynamic section, which is something we would
1589 	 * like to encourage, and (2) Without it, we cannot generate
1590 	 * the associated .SUNW_dyn[sym|tls]sort sections, which are of
1591 	 * value to DTrace.
1592 	 *
1593 	 * In practice, it is extremely rare for an object not to have
1594 	 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be
1595 	 * doing it anyway.
1596 	 */
1597 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1598 
1599 	/*
1600 	 * Create the section headers. Note that we supply an ent_cnt
1601 	 * of 0. We won't know the count until the section has been placed.
1602 	 */
1603 	if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM,
1604 	    MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR)
1605 		return (S_ERROR);
1606 
1607 	if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0,
1608 	    &isec, &shdr, &data) == S_ERROR)
1609 		return (S_ERROR);
1610 
1611 	/*
1612 	 * Place the section(s) first since it will affect the local symbol
1613 	 * count.
1614 	 */
1615 	if (allow_ldynsym &&
1616 	    ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec,
1617 	    ld_targ.t_id.id_ldynsym, 0)) == (Os_desc *)S_ERROR))
1618 		return (S_ERROR);
1619 	ofl->ofl_osdynsym =
1620 	    ld_place_section(ofl, isec, ld_targ.t_id.id_dynsym, 0);
1621 	if (ofl->ofl_osdynsym == (Os_desc *)S_ERROR)
1622 		return (S_ERROR);
1623 
1624 	/*
1625 	 * One extra section header entry for the 'null' entry.
1626 	 */
1627 	cnt = 1 + ofl->ofl_dynshdrcnt + ofl->ofl_globcnt + ofl->ofl_lregsymcnt;
1628 	size = (size_t)cnt * shdr->sh_entsize;
1629 
1630 	/*
1631 	 * Finalize the section header and data buffer initialization.
1632 	 */
1633 	data->d_size = size;
1634 	shdr->sh_size = (Xword)size;
1635 
1636 	/*
1637 	 * An ldynsym contains local function symbols. It is not
1638 	 * used for linking, but if present, serves to allow better
1639 	 * stack traces to be generated in contexts where the symtab
1640 	 * is not available. (dladdr(), or stripped executable/library files).
1641 	 */
1642 	if (allow_ldynsym) {
1643 		cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt;
1644 		size = (size_t)cnt * shdr->sh_entsize;
1645 
1646 		ldata->d_size = size;
1647 		lshdr->sh_size = (Xword)size;
1648 	}
1649 
1650 	return (1);
1651 }
1652 
1653 /*
1654  * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are
1655  * index sections for the .SUNW_ldynsym/.dynsym pair that present data
1656  * and function symbols sorted by address.
1657  */
1658 static uintptr_t
1659 make_dynsort(Ofl_desc *ofl)
1660 {
1661 	Shdr		*shdr;
1662 	Elf_Data	*data;
1663 	Is_desc		*isec;
1664 
1665 	/* Only do it if the .SUNW_ldynsym section is present */
1666 	if (!OFL_ALLOW_LDYNSYM(ofl))
1667 		return (1);
1668 
1669 	/* .SUNW_dynsymsort */
1670 	if (ofl->ofl_dynsymsortcnt > 0) {
1671 		if (new_section(ofl, SHT_SUNW_symsort,
1672 		    MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt,
1673 		    &isec, &shdr, &data) == S_ERROR)
1674 		return (S_ERROR);
1675 
1676 		if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec,
1677 		    ld_targ.t_id.id_dynsort, 0)) == (Os_desc *)S_ERROR)
1678 			return (S_ERROR);
1679 	}
1680 
1681 	/* .SUNW_dyntlssort */
1682 	if (ofl->ofl_dyntlssortcnt > 0) {
1683 		if (new_section(ofl, SHT_SUNW_tlssort,
1684 		    MSG_ORIG(MSG_SCN_DYNTLSSORT),
1685 		    ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR)
1686 		return (S_ERROR);
1687 
1688 		if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec,
1689 		    ld_targ.t_id.id_dynsort, 0)) == (Os_desc *)S_ERROR)
1690 			return (S_ERROR);
1691 	}
1692 
1693 	return (1);
1694 }
1695 
1696 /*
1697  * Helper routine for make_dynsym_shndx. Builds a
1698  * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing
1699  * which one it is.
1700  */
1701 static uintptr_t
1702 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab,
1703     Os_desc **ret_os)
1704 {
1705 	Is_desc		*isec;
1706 	Is_desc		*dynsymisp;
1707 	Shdr		*shdr, *dynshdr;
1708 	Elf_Data	*data;
1709 
1710 	dynsymisp = (Is_desc *)symtab->os_isdescs->apl_data[0];
1711 	dynshdr = dynsymisp->is_shdr;
1712 
1713 	if (new_section(ofl, SHT_SYMTAB_SHNDX, shname,
1714 	    (dynshdr->sh_size / dynshdr->sh_entsize),
1715 	    &isec, &shdr, &data) == S_ERROR)
1716 		return (S_ERROR);
1717 
1718 	if ((*ret_os = ld_place_section(ofl, isec,
1719 	    ld_targ.t_id.id_dynsym_ndx, 0)) == (Os_desc *)S_ERROR)
1720 		return (S_ERROR);
1721 
1722 	assert(*ret_os);
1723 
1724 	return (1);
1725 }
1726 
1727 /*
1728  * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym
1729  */
1730 static uintptr_t
1731 make_dynsym_shndx(Ofl_desc *ofl)
1732 {
1733 	/*
1734 	 * If there is a .SUNW_ldynsym, generate a section for its extended
1735 	 * index section as well.
1736 	 */
1737 	if (OFL_ALLOW_LDYNSYM(ofl)) {
1738 		if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX),
1739 		    ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR)
1740 			return (S_ERROR);
1741 	}
1742 
1743 	/* The Generate a section for the dynsym */
1744 	if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX),
1745 	    ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR)
1746 		return (S_ERROR);
1747 
1748 	return (1);
1749 }
1750 
1751 
1752 /*
1753  * Build a string table for the section headers.
1754  */
1755 static uintptr_t
1756 make_shstrtab(Ofl_desc *ofl)
1757 {
1758 	Shdr		*shdr;
1759 	Elf_Data	*data;
1760 	Is_desc		*isec;
1761 	size_t		size;
1762 
1763 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB),
1764 	    0, &isec, &shdr, &data) == S_ERROR)
1765 		return (S_ERROR);
1766 
1767 	/*
1768 	 * Place the section first, as it may effect the number of section
1769 	 * headers to account for.
1770 	 */
1771 	ofl->ofl_osshstrtab =
1772 	    ld_place_section(ofl, isec, ld_targ.t_id.id_note, 0);
1773 	if (ofl->ofl_osshstrtab == (Os_desc *)S_ERROR)
1774 		return (S_ERROR);
1775 
1776 	size = st_getstrtab_sz(ofl->ofl_shdrsttab);
1777 	assert(size > 0);
1778 
1779 	data->d_size = size;
1780 	shdr->sh_size = (Xword)size;
1781 
1782 	return (1);
1783 }
1784 
1785 /*
1786  * Build a string section for the standard symbol table.
1787  */
1788 static uintptr_t
1789 make_strtab(Ofl_desc *ofl)
1790 {
1791 	Shdr		*shdr;
1792 	Elf_Data	*data;
1793 	Is_desc		*isec;
1794 	size_t		size;
1795 
1796 	/*
1797 	 * This string table consists of all the global and local symbols.
1798 	 * Account for null bytes at end of the file name and the beginning
1799 	 * of section.
1800 	 */
1801 	if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
1802 		return (S_ERROR);
1803 
1804 	size = st_getstrtab_sz(ofl->ofl_strtab);
1805 	assert(size > 0);
1806 
1807 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB),
1808 	    0, &isec, &shdr, &data) == S_ERROR)
1809 		return (S_ERROR);
1810 
1811 	/* Set the size of the data area */
1812 	data->d_size = size;
1813 	shdr->sh_size = (Xword)size;
1814 
1815 	ofl->ofl_osstrtab =
1816 	    ld_place_section(ofl, isec, ld_targ.t_id.id_strtab, 0);
1817 	return ((uintptr_t)ofl->ofl_osstrtab);
1818 }
1819 
1820 /*
1821  * Build a string table for the dynamic symbol table.
1822  */
1823 static uintptr_t
1824 make_dynstr(Ofl_desc *ofl)
1825 {
1826 	Shdr		*shdr;
1827 	Elf_Data	*data;
1828 	Is_desc		*isec;
1829 	size_t		size;
1830 
1831 	/*
1832 	 * If producing a .SUNW_ldynsym, account for the initial STT_FILE
1833 	 * symbol that precedes the scope reduced global symbols.
1834 	 */
1835 	if (OFL_ALLOW_LDYNSYM(ofl)) {
1836 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1)
1837 			return (S_ERROR);
1838 		ofl->ofl_dynscopecnt++;
1839 	}
1840 
1841 
1842 	/*
1843 	 * Account for any local, named register symbols.  These locals are
1844 	 * required for reference from DT_REGISTER .dynamic entries.
1845 	 */
1846 	if (ofl->ofl_regsyms) {
1847 		int	ndx;
1848 
1849 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1850 			Sym_desc	*sdp;
1851 
1852 			if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
1853 				continue;
1854 
1855 			if (((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) &&
1856 			    (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
1857 				continue;
1858 
1859 			if (sdp->sd_sym->st_name == NULL)
1860 				continue;
1861 
1862 			if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
1863 				return (S_ERROR);
1864 		}
1865 	}
1866 
1867 	/*
1868 	 * Reserve entries for any per-symbol auxiliary/filter strings.
1869 	 */
1870 	if (ofl->ofl_dtsfltrs != NULL) {
1871 		Dfltr_desc	*dftp;
1872 		Aliste		idx;
1873 
1874 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp))
1875 			if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
1876 				return (S_ERROR);
1877 	}
1878 
1879 	size = st_getstrtab_sz(ofl->ofl_dynstrtab);
1880 	assert(size > 0);
1881 
1882 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR),
1883 	    0, &isec, &shdr, &data) == S_ERROR)
1884 		return (S_ERROR);
1885 
1886 	/* Make it allocable if necessary */
1887 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
1888 		shdr->sh_flags |= SHF_ALLOC;
1889 
1890 	/* Set the size of the data area */
1891 	data->d_size = size + DYNSTR_EXTRA_PAD;
1892 
1893 	shdr->sh_size = (Xword)size;
1894 
1895 	ofl->ofl_osdynstr =
1896 	    ld_place_section(ofl, isec, ld_targ.t_id.id_dynstr, 0);
1897 	return ((uintptr_t)ofl->ofl_osdynstr);
1898 }
1899 
1900 /*
1901  * Generate an output relocation section which will contain the relocation
1902  * information to be applied to the `osp' section.
1903  *
1904  * If (osp == NULL) then we are creating the coalesced relocation section
1905  * for an executable and/or a shared object.
1906  */
1907 static uintptr_t
1908 make_reloc(Ofl_desc *ofl, Os_desc *osp)
1909 {
1910 	Shdr		*shdr;
1911 	Elf_Data	*data;
1912 	Is_desc		*isec;
1913 	size_t		size;
1914 	Xword		sh_flags;
1915 	char 		*sectname;
1916 	Os_desc		*rosp;
1917 	Word		relsize;
1918 	const char	*rel_prefix;
1919 
1920 	/* LINTED */
1921 	if (ld_targ.t_m.m_rel_sht_type == SHT_REL) {
1922 		/* REL */
1923 		relsize = sizeof (Rel);
1924 		rel_prefix = MSG_ORIG(MSG_SCN_REL);
1925 	} else {
1926 		/* RELA */
1927 		relsize = sizeof (Rela);
1928 		rel_prefix = MSG_ORIG(MSG_SCN_RELA);
1929 	}
1930 
1931 	if (osp) {
1932 		size = osp->os_szoutrels;
1933 		sh_flags = osp->os_shdr->sh_flags;
1934 		if ((sectname = libld_malloc(strlen(rel_prefix) +
1935 		    strlen(osp->os_name) + 1)) == 0)
1936 			return (S_ERROR);
1937 		(void) strcpy(sectname, rel_prefix);
1938 		(void) strcat(sectname, osp->os_name);
1939 	} else if (ofl->ofl_flags & FLG_OF_COMREL) {
1940 		size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
1941 		sh_flags = SHF_ALLOC;
1942 		sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
1943 	} else {
1944 		size = ofl->ofl_relocrelsz;
1945 		sh_flags = SHF_ALLOC;
1946 		sectname = (char *)rel_prefix;
1947 	}
1948 
1949 	/*
1950 	 * Keep track of total size of 'output relocations' (to be stored
1951 	 * in .dynamic)
1952 	 */
1953 	/* LINTED */
1954 	ofl->ofl_relocsz += (Xword)size;
1955 
1956 	if (new_section(ofl, ld_targ.t_m.m_rel_sht_type, sectname, 0, &isec,
1957 	    &shdr, &data) == S_ERROR)
1958 		return (S_ERROR);
1959 
1960 	data->d_size = size;
1961 
1962 	shdr->sh_size = (Xword)size;
1963 	if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC))
1964 		shdr->sh_flags = SHF_ALLOC;
1965 
1966 	if (osp) {
1967 		/*
1968 		 * The sh_info field of the SHT_REL* sections points to the
1969 		 * section the relocations are to be applied to.
1970 		 */
1971 		shdr->sh_flags |= SHF_INFO_LINK;
1972 	}
1973 
1974 	rosp = ld_place_section(ofl, isec, ld_targ.t_id.id_rel, 0);
1975 	if (rosp == (Os_desc *)S_ERROR)
1976 		return (S_ERROR);
1977 
1978 	/*
1979 	 * Associate this relocation section to the section its going to
1980 	 * relocate.
1981 	 */
1982 	if (osp) {
1983 		Aliste	idx;
1984 		Is_desc	*risp;
1985 
1986 		/*
1987 		 * This is used primarily so that we can update
1988 		 * SHT_GROUP[sect_no] entries to point to the
1989 		 * created output relocation sections.
1990 		 */
1991 		for (APLIST_TRAVERSE(osp->os_relisdescs, idx, risp)) {
1992 			risp->is_osdesc = rosp;
1993 
1994 			/*
1995 			 * If the input relocation section had the SHF_GROUP
1996 			 * flag set - propagate it to the output relocation
1997 			 * section.
1998 			 */
1999 			if (risp->is_shdr->sh_flags & SHF_GROUP) {
2000 				rosp->os_shdr->sh_flags |= SHF_GROUP;
2001 				break;
2002 			}
2003 		}
2004 		osp->os_relosdesc = rosp;
2005 	} else
2006 		ofl->ofl_osrel = rosp;
2007 
2008 	/*
2009 	 * If this is the first relocation section we've encountered save it
2010 	 * so that the .dynamic entry can be initialized accordingly.
2011 	 */
2012 	if (ofl->ofl_osrelhead == (Os_desc *)0)
2013 		ofl->ofl_osrelhead = rosp;
2014 
2015 	return (1);
2016 }
2017 
2018 /*
2019  * Generate version needed section.
2020  */
2021 static uintptr_t
2022 make_verneed(Ofl_desc *ofl)
2023 {
2024 	Shdr		*shdr;
2025 	Elf_Data	*data;
2026 	Is_desc		*isec;
2027 
2028 	/*
2029 	 * verneed sections do not have a constant element size, so the
2030 	 * value of ent_cnt specified here (0) is meaningless.
2031 	 */
2032 	if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION),
2033 	    0, &isec, &shdr, &data) == S_ERROR)
2034 		return (S_ERROR);
2035 
2036 	/* During version processing we calculated the total size. */
2037 	data->d_size = ofl->ofl_verneedsz;
2038 	shdr->sh_size = (Xword)ofl->ofl_verneedsz;
2039 
2040 	ofl->ofl_osverneed =
2041 	    ld_place_section(ofl, isec, ld_targ.t_id.id_version, 0);
2042 	return ((uintptr_t)ofl->ofl_osverneed);
2043 }
2044 
2045 /*
2046  * Generate a version definition section.
2047  *
2048  *  o	the SHT_SUNW_verdef section defines the versions that exist within this
2049  *	image.
2050  */
2051 static uintptr_t
2052 make_verdef(Ofl_desc *ofl)
2053 {
2054 	Shdr		*shdr;
2055 	Elf_Data	*data;
2056 	Is_desc		*isec;
2057 	Ver_desc	*vdp;
2058 
2059 	/*
2060 	 * Reserve a string table entry for the base version dependency (other
2061 	 * dependencies have symbol representations, which will already be
2062 	 * accounted for during symbol processing).
2063 	 */
2064 	vdp = (Ver_desc *)ofl->ofl_verdesc->apl_data[0];
2065 
2066 	if (ofl->ofl_flags & FLG_OF_DYNAMIC) {
2067 		if (st_insert(ofl->ofl_dynstrtab, vdp->vd_name) == -1)
2068 			return (S_ERROR);
2069 	} else {
2070 		if (st_insert(ofl->ofl_strtab, vdp->vd_name) == -1)
2071 			return (S_ERROR);
2072 	}
2073 
2074 	/*
2075 	 * verdef sections do not have a constant element size, so the
2076 	 * value of ent_cnt specified here (0) is meaningless.
2077 	 */
2078 	if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION),
2079 	    0, &isec, &shdr, &data) == S_ERROR)
2080 		return (S_ERROR);
2081 
2082 	/* During version processing we calculated the total size. */
2083 	data->d_size = ofl->ofl_verdefsz;
2084 	shdr->sh_size = (Xword)ofl->ofl_verdefsz;
2085 
2086 	ofl->ofl_osverdef =
2087 	    ld_place_section(ofl, isec, ld_targ.t_id.id_version, 0);
2088 	return ((uintptr_t)ofl->ofl_osverdef);
2089 }
2090 
2091 /*
2092  * Common function used to build both the SHT_SUNW_versym
2093  * section and the SHT_SUNW_syminfo section.  Each of these sections
2094  * provides additional symbol information.
2095  */
2096 static Os_desc *
2097 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident)
2098 {
2099 	Shdr		*shdr;
2100 	Elf_Data	*data;
2101 	Is_desc		*isec;
2102 
2103 	/*
2104 	 * We don't know the size of this section yet, so set it to 0.
2105 	 * It gets filled in after the dynsym is sized.
2106 	 */
2107 	if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) ==
2108 	    S_ERROR)
2109 		return ((Os_desc *)S_ERROR);
2110 
2111 	return (ld_place_section(ofl, isec, ident, 0));
2112 }
2113 
2114 /*
2115  * This routine is called when -z nopartial is in effect.
2116  */
2117 uintptr_t
2118 ld_make_parexpn_data(Ofl_desc *ofl, size_t size, Xword align)
2119 {
2120 	Shdr		*shdr;
2121 	Elf_Data	*data;
2122 	Is_desc		*isec;
2123 	Os_desc		*osp;
2124 
2125 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2126 	    &isec, &shdr, &data) == S_ERROR)
2127 		return (S_ERROR);
2128 
2129 	shdr->sh_flags |= SHF_WRITE;
2130 	data->d_size = size;
2131 	shdr->sh_size = (Xword)size;
2132 	if (align != 0) {
2133 		data->d_align = align;
2134 		shdr->sh_addralign = align;
2135 	}
2136 
2137 	if ((data->d_buf = libld_calloc(size, 1)) == NULL)
2138 		return (S_ERROR);
2139 
2140 	/*
2141 	 * Retain handle to this .data input section. Variables using move
2142 	 * sections (partial initialization) will be redirected here when
2143 	 * such global references are added and '-z nopartial' is in effect.
2144 	 */
2145 	ofl->ofl_isparexpn = isec;
2146 	osp = ld_place_section(ofl, isec, ld_targ.t_id.id_data, 0);
2147 	if (osp == (Os_desc *)S_ERROR)
2148 		return (S_ERROR);
2149 
2150 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
2151 		ofl->ofl_dynshdrcnt++;
2152 		osp->os_flags |= FLG_OS_OUTREL;
2153 	}
2154 	return (1);
2155 }
2156 
2157 /*
2158  * Make .sunwmove section
2159  */
2160 uintptr_t
2161 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2162 {
2163 	Shdr		*shdr;
2164 	Elf_Data	*data;
2165 	Is_desc		*isec;
2166 	Aliste		idx;
2167 	Sym_desc	*sdp;
2168 	int 		cnt = 1;
2169 
2170 
2171 	if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE),
2172 	    mv_nums, &isec, &shdr, &data) == S_ERROR)
2173 		return (S_ERROR);
2174 
2175 	if ((data->d_buf = libld_calloc(data->d_size, 1)) == NULL)
2176 		return (S_ERROR);
2177 
2178 	/*
2179 	 * Copy move entries
2180 	 */
2181 	for (APLIST_TRAVERSE(ofl->ofl_parsyms, idx, sdp)) {
2182 		Aliste		idx2;
2183 		Mv_desc		*mdp;
2184 
2185 		if (sdp->sd_flags & FLG_SY_PAREXPN)
2186 			continue;
2187 
2188 		for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp))
2189 			mdp->md_oidx = cnt++;
2190 	}
2191 
2192 	if ((ofl->ofl_osmove = ld_place_section(ofl, isec, 0, 0)) ==
2193 	    (Os_desc *)S_ERROR)
2194 		return (S_ERROR);
2195 
2196 	return (1);
2197 }
2198 
2199 /*
2200  * Given a relocation descriptor that references a string table
2201  * input section, locate the string referenced and return a pointer
2202  * to it.
2203  */
2204 static const char *
2205 strmerge_get_reloc_str(Ofl_desc *ofl, Rel_desc *rsp)
2206 {
2207 	Sym_desc *sdp = rsp->rel_sym;
2208 	Xword	 str_off;
2209 
2210 	/*
2211 	 * In the case of an STT_SECTION symbol, the addend of the
2212 	 * relocation gives the offset into the string section. For
2213 	 * other symbol types, the symbol value is the offset.
2214 	 */
2215 
2216 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
2217 		str_off = sdp->sd_sym->st_value;
2218 	} else if ((rsp->rel_flags & FLG_REL_RELA) == FLG_REL_RELA) {
2219 		/*
2220 		 * For SHT_RELA, the addend value is found in the
2221 		 * rel_raddend field of the relocation.
2222 		 */
2223 		str_off = rsp->rel_raddend;
2224 	} else {	/* REL and STT_SECTION */
2225 		/*
2226 		 * For SHT_REL, the "addend" is not part of the relocation
2227 		 * record. Instead, it is found at the relocation target
2228 		 * address.
2229 		 */
2230 		uchar_t *addr = (uchar_t *)((uintptr_t)rsp->rel_roffset +
2231 		    (uintptr_t)rsp->rel_isdesc->is_indata->d_buf);
2232 
2233 		if (ld_reloc_targval_get(ofl, rsp, addr, &str_off) == 0)
2234 			return (0);
2235 	}
2236 
2237 	return (str_off + (char *)sdp->sd_isc->is_indata->d_buf);
2238 }
2239 
2240 /*
2241  * First pass over the relocation records for string table merging.
2242  * Build lists of relocations and symbols that will need modification,
2243  * and insert the strings they reference into the mstrtab string table.
2244  *
2245  * entry:
2246  *	ofl, osp - As passed to ld_make_strmerge().
2247  *	mstrtab - String table to receive input strings. This table
2248  *		must be in its first (initialization) pass and not
2249  *		yet cooked (st_getstrtab_sz() not yet called).
2250  *	rel_alpp - APlist to receive pointer to any relocation
2251  *		descriptors with STT_SECTION symbols that reference
2252  *		one of the input sections being merged.
2253  *	sym_alpp - APlist to receive pointer to any symbols that reference
2254  *		one of the input sections being merged.
2255  *	reloc_list - List of relocation descriptors to examine.
2256  *		Either ofl->&ofl->ofl_actrels (active relocations)
2257  *		or &ofl->ofl_outrels (output relocations).
2258  *
2259  * exit:
2260  *	On success, rel_alpp and sym_alpp are updated, and
2261  *	any strings in the mergable input sections referenced by
2262  *	a relocation has been entered into mstrtab. True (1) is returned.
2263  *
2264  *	On failure, False (0) is returned.
2265  */
2266 static int
2267 strmerge_pass1(Ofl_desc *ofl, Os_desc *osp, Str_tbl *mstrtab,
2268     APlist **rel_alpp, APlist **sym_alpp, APlist *reloc_alp)
2269 {
2270 	Aliste		idx;
2271 	Rel_cache	*rcp;
2272 	Sym_desc	*sdp;
2273 	Sym_desc	*last_sdp = NULL;
2274 	Rel_desc	*rsp;
2275 	const char	*name;
2276 
2277 	for (APLIST_TRAVERSE(reloc_alp, idx, rcp)) {
2278 		/* LINTED */
2279 		for (rsp = (Rel_desc *)(rcp + 1); rsp < rcp->rc_free; rsp++) {
2280 			sdp = rsp->rel_sym;
2281 			if ((sdp->sd_isc == NULL) ||
2282 			    ((sdp->sd_isc->is_flags &
2283 			    (FLG_IS_DISCARD | FLG_IS_INSTRMRG)) !=
2284 			    FLG_IS_INSTRMRG) ||
2285 			    (sdp->sd_isc->is_osdesc != osp))
2286 				continue;
2287 
2288 			/*
2289 			 * Remember symbol for use in the third pass.
2290 			 * There is no reason to save a given symbol more
2291 			 * than once, so we take advantage of the fact that
2292 			 * relocations to a given symbol tend to cluster
2293 			 * in the list. If this is the same symbol we saved
2294 			 * last time, don't bother.
2295 			 */
2296 			if (last_sdp != sdp) {
2297 				if (aplist_append(sym_alpp, sdp,
2298 				    AL_CNT_STRMRGSYM) == NULL)
2299 					return (0);
2300 				last_sdp = sdp;
2301 			}
2302 
2303 			/* Enter the string into our new string table */
2304 			name = strmerge_get_reloc_str(ofl, rsp);
2305 			if (st_insert(mstrtab, name) == -1)
2306 				return (0);
2307 
2308 			/*
2309 			 * If this is an STT_SECTION symbol, then the
2310 			 * second pass will need to modify this relocation,
2311 			 * so hang on to it.
2312 			 */
2313 			if ((ELF_ST_TYPE(sdp->sd_sym->st_info) ==
2314 			    STT_SECTION) &&
2315 			    (aplist_append(rel_alpp, rsp,
2316 			    AL_CNT_STRMRGREL) == NULL))
2317 				return (0);
2318 		}
2319 	}
2320 
2321 	return (1);
2322 }
2323 
2324 /*
2325  * If the output section has any SHF_MERGE|SHF_STRINGS input sections,
2326  * replace them with a single merged/compressed input section.
2327  *
2328  * entry:
2329  *	ofl - Output file descriptor
2330  *	osp - Output section descriptor
2331  *	rel_alpp, sym_alpp, - Address of 2 APlists, to be used
2332  *		for internal processing. On the initial call to
2333  *		ld_make_strmerge, these list pointers must be NULL.
2334  *		The caller is encouraged to pass the same lists back for
2335  *		successive calls to this function without freeing
2336  *		them in between calls. This causes a single pair of
2337  *		memory allocations to be reused multiple times.
2338  *
2339  * exit:
2340  *	If section merging is possible, it is done. If no errors are
2341  *	encountered, True (1) is returned. On error, S_ERROR.
2342  *
2343  *	The contents of rel_alpp and sym_alpp on exit are
2344  *	undefined. The caller can free them, or pass them back to a subsequent
2345  *	call to this routine, but should not examine their contents.
2346  */
2347 static uintptr_t
2348 ld_make_strmerge(Ofl_desc *ofl, Os_desc *osp, APlist **rel_alpp,
2349     APlist **sym_alpp)
2350 {
2351 	Str_tbl		*mstrtab;	/* string table for string merge secs */
2352 	Is_desc		*mstrsec;	/* Generated string merge section */
2353 	Is_desc		*isp;
2354 	Shdr		*mstr_shdr;
2355 	Elf_Data	*mstr_data;
2356 	Sym_desc	*sdp;
2357 	Rel_desc	*rsp;
2358 	Aliste		idx;
2359 	size_t		data_size;
2360 	int		st_setstring_status;
2361 	size_t		stoff;
2362 
2363 	/* If string table compression is disabled, there's nothing to do */
2364 	if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0)
2365 		return (1);
2366 
2367 	/*
2368 	 * Pass over the mergeable input sections, and if they haven't
2369 	 * all been discarded, create a string table.
2370 	 */
2371 	mstrtab = NULL;
2372 	for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
2373 		if (isp->is_flags & FLG_IS_DISCARD)
2374 			continue;
2375 
2376 		/*
2377 		 * We have at least one non-discarded section.
2378 		 * Create a string table descriptor.
2379 		 */
2380 		if ((mstrtab = st_new(FLG_STNEW_COMPRESS)) == NULL)
2381 			return (S_ERROR);
2382 		break;
2383 	}
2384 
2385 	/* If no string table was created, we have no mergeable sections */
2386 	if (mstrtab == NULL)
2387 		return (1);
2388 
2389 	/*
2390 	 * This routine has to make 3 passes:
2391 	 *
2392 	 *	1) Examine all relocations, insert strings from relocations
2393 	 *		to the mergable input sections into the string table.
2394 	 *	2) Modify the relocation values to be correct for the
2395 	 *		new merged section.
2396 	 *	3) Modify the symbols used by the relocations to reference
2397 	 *		the new section.
2398 	 *
2399 	 * These passes cannot be combined:
2400 	 *	- The string table code works in two passes, and all
2401 	 *		strings have to be loaded in pass one before the
2402 	 *		offset of any strings can be determined.
2403 	 *	- Multiple relocations reference a single symbol, so the
2404 	 *		symbol cannot be modified until all relocations are
2405 	 *		fixed.
2406 	 *
2407 	 * The number of relocations related to section merging is usually
2408 	 * a mere fraction of the overall active and output relocation lists,
2409 	 * and the number of symbols is usually a fraction of the number
2410 	 * of related relocations. We therefore build APlists for the
2411 	 * relocations and symbols in the first pass, and then use those
2412 	 * lists to accelerate the operation of pass 2 and 3.
2413 	 *
2414 	 * Reinitialize the lists to a completely empty state.
2415 	 */
2416 	aplist_reset(*rel_alpp);
2417 	aplist_reset(*sym_alpp);
2418 
2419 	/*
2420 	 * Pass 1:
2421 	 *
2422 	 * Every relocation related to this output section (and the input
2423 	 * sections that make it up) is found in either the active, or the
2424 	 * output relocation list, depending on whether the relocation is to
2425 	 * be processed by this invocation of the linker, or inserted into the
2426 	 * output object.
2427 	 *
2428 	 * Build lists of relocations and symbols that will need modification,
2429 	 * and insert the strings they reference into the mstrtab string table.
2430 	 */
2431 	if (strmerge_pass1(ofl, osp, mstrtab, rel_alpp, sym_alpp,
2432 	    ofl->ofl_actrels) == 0)
2433 		goto return_s_error;
2434 	if (strmerge_pass1(ofl, osp, mstrtab, rel_alpp, sym_alpp,
2435 	    ofl->ofl_outrels) == 0)
2436 		goto return_s_error;
2437 
2438 	/*
2439 	 * Get the size of the new input section. Requesting the
2440 	 * string table size "cooks" the table, and finalizes its contents.
2441 	 */
2442 	data_size = st_getstrtab_sz(mstrtab);
2443 
2444 	/* Create a new input section to hold the merged strings */
2445 	if (new_section_from_template(ofl, isp, data_size,
2446 	    &mstrsec, &mstr_shdr, &mstr_data) == S_ERROR)
2447 		goto return_s_error;
2448 	mstrsec->is_flags |= FLG_IS_GNSTRMRG;
2449 
2450 	/*
2451 	 * Allocate a data buffer for the new input section.
2452 	 * Then, associate the buffer with the string table descriptor.
2453 	 */
2454 	if ((mstr_data->d_buf = libld_malloc(data_size)) == NULL)
2455 		goto return_s_error;
2456 	if (st_setstrbuf(mstrtab, mstr_data->d_buf, data_size) == -1)
2457 		goto return_s_error;
2458 
2459 	/* Add the new section to the output image */
2460 	if (ld_place_section(ofl, mstrsec, osp->os_identndx, 0) ==
2461 	    (Os_desc *)S_ERROR)
2462 		goto return_s_error;
2463 
2464 	/*
2465 	 * Pass 2:
2466 	 *
2467 	 * Revisit the relocation descriptors with STT_SECTION symbols
2468 	 * that were saved by the first pass. Update each relocation
2469 	 * record so that the offset it contains is for the new section
2470 	 * instead of the original.
2471 	 */
2472 	for (APLIST_TRAVERSE(*rel_alpp, idx, rsp)) {
2473 		const char	*name;
2474 
2475 		/* Put the string into the merged string table */
2476 		name = strmerge_get_reloc_str(ofl, rsp);
2477 		st_setstring_status = st_setstring(mstrtab, name, &stoff);
2478 		if (st_setstring_status == -1) {
2479 			/*
2480 			 * A failure to insert at this point means that
2481 			 * something is corrupt. This isn't a resource issue.
2482 			 */
2483 			assert(st_setstring_status != -1);
2484 			goto return_s_error;
2485 		}
2486 
2487 		/*
2488 		 * Alter the relocation to access the string at the
2489 		 * new offset in our new string table.
2490 		 *
2491 		 * For SHT_RELA platforms, it suffices to simply
2492 		 * update the rel_raddend field of the relocation.
2493 		 *
2494 		 * For SHT_REL platforms, the new "addend" value
2495 		 * needs to be written at the address being relocated.
2496 		 * However, we can't alter the input sections which
2497 		 * are mapped readonly, and the output image has not
2498 		 * been created yet. So, we defer this operation,
2499 		 * using the rel_raddend field of the relocation
2500 		 * which is normally 0 on a REL platform, to pass the
2501 		 * new "addend" value to ld_perform_outreloc() or
2502 		 * ld_do_activerelocs(). The FLG_REL_NADDEND flag
2503 		 * tells them that this is the case.
2504 		 */
2505 		if ((rsp->rel_flags & FLG_REL_RELA) == 0)   /* REL */
2506 			rsp->rel_flags |= FLG_REL_NADDEND;
2507 		rsp->rel_raddend = (Sxword)stoff;
2508 
2509 		/*
2510 		 * Change the descriptor name to reflect the fact that it
2511 		 * points at our merged section. This shows up in debug
2512 		 * output and helps show how the relocation has changed
2513 		 * from its original input section to our merged one.
2514 		 */
2515 		rsp->rel_sname = ld_section_reld_name(rsp->rel_sym, mstrsec);
2516 		if (rsp->rel_sname == NULL)
2517 			goto return_s_error;
2518 	}
2519 
2520 	/*
2521 	 * Pass 3:
2522 	 *
2523 	 * Modify the symbols referenced by the relocation descriptors
2524 	 * so that they reference the new input section containing the
2525 	 * merged strings instead of the original input sections.
2526 	 */
2527 	for (APLIST_TRAVERSE(*sym_alpp, idx, sdp)) {
2528 		/*
2529 		 * If we've already processed this symbol, don't do it
2530 		 * twice. strmerge_pass1() uses a heuristic (relocations to
2531 		 * the same symbol clump together) to avoid inserting a
2532 		 * given symbol more than once, but repeat symbols in
2533 		 * the list can occur.
2534 		 */
2535 		if ((sdp->sd_isc->is_flags & FLG_IS_INSTRMRG) == 0)
2536 			continue;
2537 
2538 		if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
2539 			/*
2540 			 * This is not an STT_SECTION symbol, so its
2541 			 * value is the offset of the string within the
2542 			 * input section. Update the address to reflect
2543 			 * the address in our new merged section.
2544 			 */
2545 			const char *name = sdp->sd_sym->st_value +
2546 			    (char *)sdp->sd_isc->is_indata->d_buf;
2547 
2548 			st_setstring_status =
2549 			    st_setstring(mstrtab, name, &stoff);
2550 			if (st_setstring_status == -1) {
2551 				/*
2552 				 * A failure to insert at this point means
2553 				 * something is corrupt. This isn't a
2554 				 * resource issue.
2555 				 */
2556 				assert(st_setstring_status != -1);
2557 				goto return_s_error;
2558 			}
2559 
2560 			if (ld_sym_copy(sdp) == S_ERROR)
2561 				goto return_s_error;
2562 			sdp->sd_sym->st_value = (Word)stoff;
2563 		}
2564 
2565 		/* Redirect the symbol to our new merged section */
2566 		sdp->sd_isc = mstrsec;
2567 	}
2568 
2569 	/*
2570 	 * There are no references left to the original input string sections.
2571 	 * Mark them as discarded so they don't go into the output image.
2572 	 * At the same time, add up the sizes of the replaced sections.
2573 	 */
2574 	data_size = 0;
2575 	for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
2576 		if (isp->is_flags & (FLG_IS_DISCARD | FLG_IS_GNSTRMRG))
2577 			continue;
2578 
2579 		data_size += isp->is_indata->d_size;
2580 
2581 		isp->is_flags |= FLG_IS_DISCARD;
2582 		DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp, mstrsec));
2583 	}
2584 
2585 	/* Report how much space we saved in the output section */
2586 	Dbg_sec_genstr_compress(ofl->ofl_lml, osp->os_name, data_size,
2587 	    mstr_data->d_size);
2588 
2589 	st_destroy(mstrtab);
2590 	return (1);
2591 
2592 return_s_error:
2593 	st_destroy(mstrtab);
2594 	return (S_ERROR);
2595 }
2596 
2597 
2598 /*
2599  * The following sections are built after all input file processing and symbol
2600  * validation has been carried out.  The order is important (because the
2601  * addition of a section adds a new symbol there is a chicken and egg problem
2602  * of maintaining the appropriate counts).  By maintaining a known order the
2603  * individual routines can compensate for later, known, additions.
2604  */
2605 uintptr_t
2606 ld_make_sections(Ofl_desc *ofl)
2607 {
2608 	ofl_flag_t	flags = ofl->ofl_flags;
2609 	Sg_desc		*sgp;
2610 
2611 	/*
2612 	 * Generate any special sections.
2613 	 */
2614 	if (flags & FLG_OF_ADDVERS)
2615 		if (make_comment(ofl) == S_ERROR)
2616 			return (S_ERROR);
2617 
2618 	if (make_interp(ofl) == S_ERROR)
2619 		return (S_ERROR);
2620 
2621 	if (make_cap(ofl) == S_ERROR)
2622 		return (S_ERROR);
2623 
2624 	if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
2625 	    ofl->ofl_initarray) == S_ERROR)
2626 		return (S_ERROR);
2627 
2628 	if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
2629 	    ofl->ofl_finiarray) == S_ERROR)
2630 		return (S_ERROR);
2631 
2632 	if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
2633 	    ofl->ofl_preiarray) == S_ERROR)
2634 		return (S_ERROR);
2635 
2636 	/*
2637 	 * Make the .plt section.  This occurs after any other relocation
2638 	 * sections are generated (see reloc_init()) to ensure that the
2639 	 * associated relocation section is after all the other relocation
2640 	 * sections.
2641 	 */
2642 	if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
2643 		if (make_plt(ofl) == S_ERROR)
2644 			return (S_ERROR);
2645 
2646 	/*
2647 	 * Determine whether any sections or files are not referenced.  Under
2648 	 * -Dunused a diagnostic for any unused components is generated, under
2649 	 * -zignore the component is removed from the final output.
2650 	 */
2651 	if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
2652 		if (ignore_section_processing(ofl) == S_ERROR)
2653 			return (S_ERROR);
2654 	}
2655 
2656 	/*
2657 	 * Do any of the output sections contain input sections that
2658 	 * are candidates for string table merging? For each such case,
2659 	 * we create a replacement section, insert it, and discard the
2660 	 * originals.
2661 	 *
2662 	 * rel_alpp and sym_alpp are used by ld_make_strmerge()
2663 	 * for its internal processing. We are responsible for the
2664 	 * initialization and cleanup, and ld_make_strmerge() handles the rest.
2665 	 * This allows us to reuse a single pair of memory buffers, allocated
2666 	 * for this processing, for all the output sections.
2667 	 */
2668 	if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) == 0) {
2669 		int	error_seen = 0;
2670 		APlist	*rel_alpp = NULL;
2671 		APlist	*sym_alpp = NULL;
2672 		Aliste	idx1;
2673 
2674 		for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
2675 			Os_desc	*osp;
2676 			Aliste	idx2;
2677 
2678 			for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp))
2679 				if ((osp->os_mstrisdescs != NULL) &&
2680 				    (ld_make_strmerge(ofl, osp,
2681 				    &rel_alpp, &sym_alpp) ==
2682 				    S_ERROR)) {
2683 					error_seen = 1;
2684 					break;
2685 				}
2686 		}
2687 		if (rel_alpp != NULL)
2688 			free(rel_alpp);
2689 		if (sym_alpp != NULL)
2690 			free(sym_alpp);
2691 		if (error_seen != 0)
2692 			return (S_ERROR);
2693 	}
2694 
2695 	/*
2696 	 * Add any necessary versioning information.
2697 	 */
2698 	if (!(flags & FLG_OF_NOVERSEC)) {
2699 		if ((flags & FLG_OF_VERNEED) &&
2700 		    (make_verneed(ofl) == S_ERROR))
2701 			return (S_ERROR);
2702 		if ((flags & FLG_OF_VERDEF) &&
2703 		    (make_verdef(ofl) == S_ERROR))
2704 			return (S_ERROR);
2705 		if ((flags & (FLG_OF_VERNEED | FLG_OF_VERDEF)) &&
2706 		    ((ofl->ofl_osversym = make_sym_sec(ofl,
2707 		    MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym,
2708 		    ld_targ.t_id.id_version)) == (Os_desc*)S_ERROR))
2709 			return (S_ERROR);
2710 	}
2711 
2712 	/*
2713 	 * Create a syminfo section if necessary.
2714 	 */
2715 	if (flags & FLG_OF_SYMINFO) {
2716 		if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
2717 		    MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo,
2718 		    ld_targ.t_id.id_syminfo)) == (Os_desc *)S_ERROR)
2719 			return (S_ERROR);
2720 	}
2721 
2722 	if (flags & FLG_OF_COMREL) {
2723 		/*
2724 		 * If -zcombreloc is enabled then all relocations (except for
2725 		 * the PLT's) are coalesced into a single relocation section.
2726 		 */
2727 		if (ofl->ofl_reloccnt) {
2728 			if (make_reloc(ofl, NULL) == S_ERROR)
2729 				return (S_ERROR);
2730 		}
2731 	} else {
2732 		Aliste	idx1;
2733 
2734 		/*
2735 		 * Create the required output relocation sections.  Note, new
2736 		 * sections may be added to the section list that is being
2737 		 * traversed.  These insertions can move the elements of the
2738 		 * Alist such that a section descriptor is re-read.  Recursion
2739 		 * is prevented by maintaining a previous section pointer and
2740 		 * insuring that this pointer isn't re-examined.
2741 		 */
2742 		for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
2743 			Os_desc	*osp, *posp = 0;
2744 			Aliste	idx2;
2745 
2746 			for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
2747 				if ((osp != posp) && osp->os_szoutrels &&
2748 				    (osp != ofl->ofl_osplt)) {
2749 					if (make_reloc(ofl, osp) == S_ERROR)
2750 						return (S_ERROR);
2751 				}
2752 				posp = osp;
2753 			}
2754 		}
2755 
2756 		/*
2757 		 * If we're not building a combined relocation section, then
2758 		 * build a .rel[a] section as required.
2759 		 */
2760 		if (ofl->ofl_relocrelsz) {
2761 			if (make_reloc(ofl, NULL) == S_ERROR)
2762 				return (S_ERROR);
2763 		}
2764 	}
2765 
2766 	/*
2767 	 * The PLT relocations are always in their own section, and we try to
2768 	 * keep them at the end of the PLT table.  We do this to keep the hot
2769 	 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
2770 	 */
2771 	if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
2772 		if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
2773 			return (S_ERROR);
2774 	}
2775 
2776 	/*
2777 	 * Finally build the symbol and section header sections.
2778 	 */
2779 	if (flags & FLG_OF_DYNAMIC) {
2780 		if (make_dynamic(ofl) == S_ERROR)
2781 			return (S_ERROR);
2782 		if (make_dynstr(ofl) == S_ERROR)
2783 			return (S_ERROR);
2784 		/*
2785 		 * There is no use for .hash and .dynsym sections in a
2786 		 * relocatable object.
2787 		 */
2788 		if (!(flags & FLG_OF_RELOBJ)) {
2789 			if (make_hash(ofl) == S_ERROR)
2790 				return (S_ERROR);
2791 			if (make_dynsym(ofl) == S_ERROR)
2792 				return (S_ERROR);
2793 			if (ld_unwind_make_hdr(ofl) == S_ERROR)
2794 				return (S_ERROR);
2795 			if (make_dynsort(ofl) == S_ERROR)
2796 				return (S_ERROR);
2797 		}
2798 	}
2799 
2800 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
2801 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
2802 		/*
2803 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2804 		 * for the dynsym.  If so - do it now.
2805 		 */
2806 		if (ofl->ofl_osdynsym &&
2807 		    ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
2808 			if (make_dynsym_shndx(ofl) == S_ERROR)
2809 				return (S_ERROR);
2810 		}
2811 
2812 		if (make_strtab(ofl) == S_ERROR)
2813 			return (S_ERROR);
2814 		if (make_symtab(ofl) == S_ERROR)
2815 			return (S_ERROR);
2816 	} else {
2817 		/*
2818 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2819 		 * for the dynsym.  If so - do it now.
2820 		 */
2821 		if (ofl->ofl_osdynsym &&
2822 		    ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
2823 			if (make_dynsym_shndx(ofl) == S_ERROR)
2824 				return (S_ERROR);
2825 		}
2826 	}
2827 
2828 	if (make_shstrtab(ofl) == S_ERROR)
2829 		return (S_ERROR);
2830 
2831 	/*
2832 	 * Now that we've created all of our sections adjust the size
2833 	 * of SHT_SUNW_versym & SHT_SUNW_syminfo which are dependent on
2834 	 * the symbol table sizes.
2835 	 */
2836 	if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
2837 		Shdr		*shdr;
2838 		Is_desc		*isec;
2839 		Elf_Data	*data;
2840 		size_t		size;
2841 		ulong_t		cnt;
2842 		Os_desc		*osp;
2843 
2844 		if (flags & (FLG_OF_RELOBJ | FLG_OF_STATIC)) {
2845 			osp = ofl->ofl_ossymtab;
2846 		} else {
2847 			osp = ofl->ofl_osdynsym;
2848 		}
2849 		isec = (Is_desc *)osp->os_isdescs->apl_data[0];
2850 		cnt = (isec->is_shdr->sh_size / isec->is_shdr->sh_entsize);
2851 
2852 		if (ofl->ofl_osversym) {
2853 			osp = ofl->ofl_osversym;
2854 			isec = (Is_desc *)osp->os_isdescs->apl_data[0];
2855 			data = isec->is_indata;
2856 			shdr = osp->os_shdr;
2857 			size = cnt * shdr->sh_entsize;
2858 			shdr->sh_size = (Xword)size;
2859 			data->d_size = size;
2860 		}
2861 		if (ofl->ofl_ossyminfo) {
2862 			osp = ofl->ofl_ossyminfo;
2863 			isec = (Is_desc *)osp->os_isdescs->apl_data[0];
2864 			data = isec->is_indata;
2865 			shdr = osp->os_shdr;
2866 			size = cnt * shdr->sh_entsize;
2867 			shdr->sh_size = (Xword)size;
2868 			data->d_size = size;
2869 		}
2870 	}
2871 
2872 	return (1);
2873 }
2874 
2875 /*
2876  * Build an additional data section - used to back OBJT symbol definitions
2877  * added with a mapfile.
2878  */
2879 Is_desc *
2880 ld_make_data(Ofl_desc *ofl, size_t size)
2881 {
2882 	Shdr		*shdr;
2883 	Elf_Data	*data;
2884 	Is_desc		*isec;
2885 
2886 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2887 	    &isec, &shdr, &data) == S_ERROR)
2888 		return ((Is_desc *)S_ERROR);
2889 
2890 	data->d_size = size;
2891 	shdr->sh_size = (Xword)size;
2892 	shdr->sh_flags |= SHF_WRITE;
2893 
2894 	if (aplist_append(&ofl->ofl_mapdata, isec, AL_CNT_OFL_MAPSECS) == NULL)
2895 		return ((Is_desc *)S_ERROR);
2896 
2897 	return (isec);
2898 }
2899 
2900 /*
2901  * Build an additional text section - used to back FUNC symbol definitions
2902  * added with a mapfile.
2903  */
2904 Is_desc *
2905 ld_make_text(Ofl_desc *ofl, size_t size)
2906 {
2907 	Shdr		*shdr;
2908 	Elf_Data	*data;
2909 	Is_desc		*isec;
2910 
2911 	/*
2912 	 * Insure the size is sufficient to contain the minimum return
2913 	 * instruction.
2914 	 */
2915 	if (size < ld_targ.t_nf.nf_size)
2916 		size = ld_targ.t_nf.nf_size;
2917 
2918 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0,
2919 	    &isec, &shdr, &data) == S_ERROR)
2920 		return ((Is_desc *)S_ERROR);
2921 
2922 	data->d_size = size;
2923 	shdr->sh_size = (Xword)size;
2924 	shdr->sh_flags |= SHF_EXECINSTR;
2925 
2926 	/*
2927 	 * Fill the buffer with the appropriate return instruction.
2928 	 * Note that there is no need to swap bytes on a non-native,
2929 	 * link, as the data being copied is given in bytes.
2930 	 */
2931 	if ((data->d_buf = libld_calloc(size, 1)) == NULL)
2932 		return ((Is_desc *)S_ERROR);
2933 	(void) memcpy(data->d_buf, ld_targ.t_nf.nf_template,
2934 	    ld_targ.t_nf.nf_size);
2935 
2936 	if (aplist_append(&ofl->ofl_maptext, isec, AL_CNT_OFL_MAPSECS) == NULL)
2937 		return ((Is_desc *)S_ERROR);
2938 
2939 	return (isec);
2940 }
2941