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