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