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 collected, but no symbols are left
1587 	 * referencing these capabilities, promote the capability groups back
1588 	 * to an object capability definition.
1589 	 */
1590 	if ((ofl->ofl_flags & FLG_OF_OTOSCAP) && ofl->ofl_capsymcnt &&
1591 	    (ofl->ofl_capfamilies == NULL)) {
1592 		ld_cap_move_symtoobj(ofl);
1593 		ofl->ofl_capsymcnt = 0;
1594 		ofl->ofl_capgroups = NULL;
1595 		ofl->ofl_flags &= ~FLG_OF_OTOSCAP;
1596 	}
1597 
1598 	/*
1599 	 * Remove any excluded capabilities.
1600 	 */
1601 	capstr_value(ofl->ofl_lml, CA_SUNW_PLAT, &ocapset->oc_plat, &title);
1602 	capstr_value(ofl->ofl_lml, CA_SUNW_MACH, &ocapset->oc_mach, &title);
1603 	capmask_value(ofl->ofl_lml, CA_SUNW_HW_2, &ocapset->oc_hw_2, &title);
1604 	capmask_value(ofl->ofl_lml, CA_SUNW_HW_1, &ocapset->oc_hw_1, &title);
1605 	capmask_value(ofl->ofl_lml, CA_SUNW_SF_1, &ocapset->oc_sf_1, &title);
1606 
1607 	/*
1608 	 * Determine how many entries are required for any object capabilities.
1609 	 */
1610 	size += alist_nitems(ocapset->oc_plat.cl_val);
1611 	size += alist_nitems(ocapset->oc_mach.cl_val);
1612 	if (ocapset->oc_hw_2.cm_val)
1613 		size++;
1614 	if (ocapset->oc_hw_1.cm_val)
1615 		size++;
1616 	if (ocapset->oc_sf_1.cm_val)
1617 		size++;
1618 
1619 	/*
1620 	 * Only identify a capabilities group if the group has content.  If a
1621 	 * capabilities identifier exists, and no other capabilities have been
1622 	 * supplied, remove the identifier.  This scenario could exist if a
1623 	 * user mistakenly defined a lone identifier, or if an identified group
1624 	 * was overridden so as to clear the existing capabilities and the
1625 	 * identifier was not also cleared.
1626 	 */
1627 	if (ocapset->oc_id.cs_str) {
1628 		if (size)
1629 			size++;
1630 		else
1631 			ocapset->oc_id.cs_str = NULL;
1632 	}
1633 	if (size)
1634 		size++;			/* Add CA_SUNW_NULL */
1635 
1636 	/*
1637 	 * Determine how many entries are required for any symbol capabilities.
1638 	 */
1639 	if (ofl->ofl_capsymcnt) {
1640 		/*
1641 		 * If there are no object capabilities, a CA_SUNW_NULL entry
1642 		 * is required before any symbol capabilities.
1643 		 */
1644 		if (size == 0)
1645 			size++;
1646 		size += ofl->ofl_capsymcnt;
1647 	}
1648 
1649 	if (size == 0)
1650 		return (NULL);
1651 
1652 	if (new_section(ofl, shtype, shname, size, &isec,
1653 	    &shdr, &data) == S_ERROR)
1654 		return (S_ERROR);
1655 
1656 	if ((data->d_buf = libld_malloc(shdr->sh_size)) == NULL)
1657 		return (S_ERROR);
1658 
1659 	cap = (Cap *)data->d_buf;
1660 
1661 	/*
1662 	 * Fill in any object capabilities.  If there is an identifier, then the
1663 	 * identifier comes first.  The remaining items follow in precedence
1664 	 * order, although the order isn't important for runtime verification.
1665 	 */
1666 	if (ocapset->oc_id.cs_str) {
1667 		ofl->ofl_flags |= FLG_OF_CAPSTRS;
1668 		if (st_insert(strtbl, ocapset->oc_id.cs_str) == -1)
1669 			return (S_ERROR);
1670 		ocapset->oc_id.cs_ndx = capndx;
1671 		CAP_UPDATE(cap, capndx, CA_SUNW_ID, 0);
1672 	}
1673 	if (ocapset->oc_plat.cl_val) {
1674 		ofl->ofl_flags |= (FLG_OF_PTCAP | FLG_OF_CAPSTRS);
1675 
1676 		/*
1677 		 * Insert any platform name strings in the appropriate string
1678 		 * table.  The capability value can't be filled in yet, as the
1679 		 * final offset of the strings isn't known until later.
1680 		 */
1681 		for (ALIST_TRAVERSE(ocapset->oc_plat.cl_val, idx1, capstr)) {
1682 			if (st_insert(strtbl, capstr->cs_str) == -1)
1683 				return (S_ERROR);
1684 			capstr->cs_ndx = capndx;
1685 			CAP_UPDATE(cap, capndx, CA_SUNW_PLAT, 0);
1686 		}
1687 	}
1688 	if (ocapset->oc_mach.cl_val) {
1689 		ofl->ofl_flags |= (FLG_OF_PTCAP | FLG_OF_CAPSTRS);
1690 
1691 		/*
1692 		 * Insert the machine name strings in the appropriate string
1693 		 * table.  The capability value can't be filled in yet, as the
1694 		 * final offset of the strings isn't known until later.
1695 		 */
1696 		for (ALIST_TRAVERSE(ocapset->oc_mach.cl_val, idx1, capstr)) {
1697 			if (st_insert(strtbl, capstr->cs_str) == -1)
1698 				return (S_ERROR);
1699 			capstr->cs_ndx = capndx;
1700 			CAP_UPDATE(cap, capndx, CA_SUNW_MACH, 0);
1701 		}
1702 	}
1703 	if (ocapset->oc_hw_2.cm_val) {
1704 		ofl->ofl_flags |= FLG_OF_PTCAP;
1705 		CAP_UPDATE(cap, capndx, CA_SUNW_HW_2, ocapset->oc_hw_2.cm_val);
1706 	}
1707 	if (ocapset->oc_hw_1.cm_val) {
1708 		ofl->ofl_flags |= FLG_OF_PTCAP;
1709 		CAP_UPDATE(cap, capndx, CA_SUNW_HW_1, ocapset->oc_hw_1.cm_val);
1710 	}
1711 	if (ocapset->oc_sf_1.cm_val) {
1712 		ofl->ofl_flags |= FLG_OF_PTCAP;
1713 		CAP_UPDATE(cap, capndx, CA_SUNW_SF_1, ocapset->oc_sf_1.cm_val);
1714 	}
1715 	CAP_UPDATE(cap, capndx, CA_SUNW_NULL, 0);
1716 
1717 	/*
1718 	 * Fill in any symbol capabilities.
1719 	 */
1720 	if (ofl->ofl_capgroups) {
1721 		Cap_group	*cgp;
1722 
1723 		for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
1724 			Objcapset	*scapset = &cgp->cg_set;
1725 			Aliste		idx2;
1726 			Is_desc		*isp;
1727 
1728 			cgp->cg_ndx = capndx;
1729 
1730 			if (scapset->oc_id.cs_str) {
1731 				ofl->ofl_flags |= FLG_OF_CAPSTRS;
1732 				/*
1733 				 * Insert the identifier string in the
1734 				 * appropriate string table.  The capability
1735 				 * value can't be filled in yet, as the final
1736 				 * offset of the string isn't known until later.
1737 				 */
1738 				if (st_insert(strtbl,
1739 				    scapset->oc_id.cs_str) == -1)
1740 					return (S_ERROR);
1741 				scapset->oc_id.cs_ndx = capndx;
1742 				CAP_UPDATE(cap, capndx, CA_SUNW_ID, 0);
1743 			}
1744 
1745 			if (scapset->oc_plat.cl_val) {
1746 				ofl->ofl_flags |= FLG_OF_CAPSTRS;
1747 
1748 				/*
1749 				 * Insert the platform name string in the
1750 				 * appropriate string table.  The capability
1751 				 * value can't be filled in yet, as the final
1752 				 * offset of the string isn't known until later.
1753 				 */
1754 				for (ALIST_TRAVERSE(scapset->oc_plat.cl_val,
1755 				    idx2, capstr)) {
1756 					if (st_insert(strtbl,
1757 					    capstr->cs_str) == -1)
1758 						return (S_ERROR);
1759 					capstr->cs_ndx = capndx;
1760 					CAP_UPDATE(cap, capndx,
1761 					    CA_SUNW_PLAT, 0);
1762 				}
1763 			}
1764 			if (scapset->oc_mach.cl_val) {
1765 				ofl->ofl_flags |= FLG_OF_CAPSTRS;
1766 
1767 				/*
1768 				 * Insert the machine name string in the
1769 				 * appropriate string table.  The capability
1770 				 * value can't be filled in yet, as the final
1771 				 * offset of the string isn't known until later.
1772 				 */
1773 				for (ALIST_TRAVERSE(scapset->oc_mach.cl_val,
1774 				    idx2, capstr)) {
1775 					if (st_insert(strtbl,
1776 					    capstr->cs_str) == -1)
1777 						return (S_ERROR);
1778 					capstr->cs_ndx = capndx;
1779 					CAP_UPDATE(cap, capndx,
1780 					    CA_SUNW_MACH, 0);
1781 				}
1782 			}
1783 			if (scapset->oc_hw_2.cm_val) {
1784 				CAP_UPDATE(cap, capndx, CA_SUNW_HW_2,
1785 				    scapset->oc_hw_2.cm_val);
1786 			}
1787 			if (scapset->oc_hw_1.cm_val) {
1788 				CAP_UPDATE(cap, capndx, CA_SUNW_HW_1,
1789 				    scapset->oc_hw_1.cm_val);
1790 			}
1791 			if (scapset->oc_sf_1.cm_val) {
1792 				CAP_UPDATE(cap, capndx, CA_SUNW_SF_1,
1793 				    scapset->oc_sf_1.cm_val);
1794 			}
1795 			CAP_UPDATE(cap, capndx, CA_SUNW_NULL, 0);
1796 
1797 			/*
1798 			 * If any object capabilities are available, determine
1799 			 * whether these symbol capabilities are less
1800 			 * restrictive, and hence redundant.
1801 			 */
1802 			if (((ofl->ofl_flags & FLG_OF_PTCAP) == 0) ||
1803 			    (is_cap_redundant(ocapset, scapset) == 0))
1804 				continue;
1805 
1806 			/*
1807 			 * Indicate any files that provide redundant symbol
1808 			 * capabilities.
1809 			 */
1810 			for (APLIST_TRAVERSE(cgp->cg_secs, idx2, isp)) {
1811 				eprintf(ofl->ofl_lml, ERR_WARNING,
1812 				    MSG_INTL(MSG_CAP_REDUNDANT),
1813 				    isp->is_file->ifl_name,
1814 				    EC_WORD(isp->is_scnndx), isp->is_name);
1815 			}
1816 		}
1817 	}
1818 
1819 	/*
1820 	 * If capabilities strings are required, the sh_info field of the
1821 	 * section header will be set to the associated string table.
1822 	 */
1823 	if (ofl->ofl_flags & FLG_OF_CAPSTRS)
1824 		shdr->sh_flags |= SHF_INFO_LINK;
1825 
1826 	/*
1827 	 * Place these capabilities in the output file.
1828 	 */
1829 	if ((ofl->ofl_oscap = ld_place_section(ofl, isec,
1830 	    NULL, ident, NULL)) == (Os_desc *)S_ERROR)
1831 		return (S_ERROR);
1832 
1833 	/*
1834 	 * If symbol capabilities are required, then a .SUNW_capinfo section is
1835 	 * also created.  This table will eventually be sized to match the
1836 	 * associated symbol table.
1837 	 */
1838 	if (ofl->ofl_capfamilies) {
1839 		if ((ofl->ofl_oscapinfo = make_sym_sec(ofl,
1840 		    MSG_ORIG(MSG_SCN_SUNWCAPINFO), SHT_SUNW_capinfo,
1841 		    ld_targ.t_id.id_capinfo)) == (Os_desc *)S_ERROR)
1842 			return (S_ERROR);
1843 
1844 		/*
1845 		 * If we're generating a dynamic object, capabilities family
1846 		 * members are maintained in a .SUNW_capchain section.
1847 		 */
1848 		if (ofl->ofl_capchaincnt &&
1849 		    ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
1850 			if (new_section(ofl, SHT_SUNW_capchain,
1851 			    MSG_ORIG(MSG_SCN_SUNWCAPCHAIN),
1852 			    ofl->ofl_capchaincnt, &isec, &shdr,
1853 			    &data) == S_ERROR)
1854 				return (S_ERROR);
1855 
1856 			ofl->ofl_oscapchain = ld_place_section(ofl, isec,
1857 			    NULL, ld_targ.t_id.id_capchain, NULL);
1858 			if (ofl->ofl_oscapchain == (Os_desc *)S_ERROR)
1859 				return (S_ERROR);
1860 
1861 		}
1862 	}
1863 	return (1);
1864 }
1865 #undef	CAP_UPDATE
1866 
1867 /*
1868  * Build the PLT section and its associated relocation entries.
1869  */
1870 static uintptr_t
1871 make_plt(Ofl_desc *ofl)
1872 {
1873 	Shdr		*shdr;
1874 	Elf_Data	*data;
1875 	Is_desc		*isec;
1876 	size_t		size = ld_targ.t_m.m_plt_reservsz +
1877 	    (((size_t)ofl->ofl_pltcnt + (size_t)ofl->ofl_pltpad) *
1878 	    ld_targ.t_m.m_plt_entsize);
1879 	size_t		rsize = (size_t)ofl->ofl_relocpltsz;
1880 
1881 	/*
1882 	 * On sparc, account for the NOP at the end of the plt.
1883 	 */
1884 	if (ld_targ.t_m.m_mach == LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
1885 		size += sizeof (Word);
1886 
1887 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0,
1888 	    &isec, &shdr, &data) == S_ERROR)
1889 		return (S_ERROR);
1890 
1891 	data->d_size = size;
1892 	data->d_align = ld_targ.t_m.m_plt_align;
1893 
1894 	shdr->sh_flags = ld_targ.t_m.m_plt_shf_flags;
1895 	shdr->sh_size = (Xword)size;
1896 	shdr->sh_addralign = ld_targ.t_m.m_plt_align;
1897 	shdr->sh_entsize = ld_targ.t_m.m_plt_entsize;
1898 
1899 	ofl->ofl_osplt = ld_place_section(ofl, isec, NULL,
1900 	    ld_targ.t_id.id_plt, NULL);
1901 	if (ofl->ofl_osplt == (Os_desc *)S_ERROR)
1902 		return (S_ERROR);
1903 
1904 	ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1905 
1906 	return (1);
1907 }
1908 
1909 /*
1910  * Make the hash table.  Only built for dynamic executables and shared
1911  * libraries, and provides hashed lookup into the global symbol table
1912  * (.dynsym) for the run-time linker to resolve symbol lookups.
1913  */
1914 static uintptr_t
1915 make_hash(Ofl_desc *ofl)
1916 {
1917 	Shdr		*shdr;
1918 	Elf_Data	*data;
1919 	Is_desc		*isec;
1920 	size_t		size;
1921 	Word		nsyms = ofl->ofl_globcnt;
1922 	size_t		cnt;
1923 
1924 	/*
1925 	 * Allocate section header structures. We set entcnt to 0
1926 	 * because it's going to change after we place this section.
1927 	 */
1928 	if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0,
1929 	    &isec, &shdr, &data) == S_ERROR)
1930 		return (S_ERROR);
1931 
1932 	/*
1933 	 * Place the section first since it will affect the local symbol
1934 	 * count.
1935 	 */
1936 	ofl->ofl_oshash =
1937 	    ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_hash, NULL);
1938 	if (ofl->ofl_oshash == (Os_desc *)S_ERROR)
1939 		return (S_ERROR);
1940 
1941 	/*
1942 	 * Calculate the number of output hash buckets.
1943 	 */
1944 	ofl->ofl_hashbkts = findprime(nsyms);
1945 
1946 	/*
1947 	 * The size of the hash table is determined by
1948 	 *
1949 	 *	i.	the initial nbucket and nchain entries (2)
1950 	 *	ii.	the number of buckets (calculated above)
1951 	 *	iii.	the number of chains (this is based on the number of
1952 	 *		symbols in the .dynsym array).
1953 	 */
1954 	cnt = 2 + ofl->ofl_hashbkts + DYNSYM_ALL_CNT(ofl);
1955 	size = cnt * shdr->sh_entsize;
1956 
1957 	/*
1958 	 * Finalize the section header and data buffer initialization.
1959 	 */
1960 	if ((data->d_buf = libld_calloc(size, 1)) == NULL)
1961 		return (S_ERROR);
1962 	data->d_size = size;
1963 	shdr->sh_size = (Xword)size;
1964 
1965 	return (1);
1966 }
1967 
1968 /*
1969  * Generate the standard symbol table.  Contains all locals and globals,
1970  * and resides in a non-allocatable section (ie. it can be stripped).
1971  */
1972 static uintptr_t
1973 make_symtab(Ofl_desc *ofl)
1974 {
1975 	Shdr		*shdr;
1976 	Elf_Data	*data;
1977 	Is_desc		*isec;
1978 	Is_desc		*xisec = 0;
1979 	size_t		size;
1980 	Word		symcnt;
1981 
1982 	/*
1983 	 * Create the section headers. Note that we supply an ent_cnt
1984 	 * of 0. We won't know the count until the section has been placed.
1985 	 */
1986 	if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0,
1987 	    &isec, &shdr, &data) == S_ERROR)
1988 		return (S_ERROR);
1989 
1990 	/*
1991 	 * Place the section first since it will affect the local symbol
1992 	 * count.
1993 	 */
1994 	if ((ofl->ofl_ossymtab = ld_place_section(ofl, isec, NULL,
1995 	    ld_targ.t_id.id_symtab, NULL)) == (Os_desc *)S_ERROR)
1996 		return (S_ERROR);
1997 
1998 	/*
1999 	 * At this point we've created all but the 'shstrtab' section.
2000 	 * Determine if we have to use 'Extended Sections'.  If so - then
2001 	 * also create a SHT_SYMTAB_SHNDX section.
2002 	 */
2003 	if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
2004 		Shdr		*xshdr;
2005 		Elf_Data	*xdata;
2006 
2007 		if (new_section(ofl, SHT_SYMTAB_SHNDX,
2008 		    MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec,
2009 		    &xshdr, &xdata) == S_ERROR)
2010 			return (S_ERROR);
2011 
2012 		if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec, NULL,
2013 		    ld_targ.t_id.id_symtab_ndx, NULL)) == (Os_desc *)S_ERROR)
2014 			return (S_ERROR);
2015 	}
2016 
2017 	/*
2018 	 * Calculated number of symbols, which need to be augmented by
2019 	 * the (yet to be created) .shstrtab entry.
2020 	 */
2021 	symcnt = (size_t)(1 + SYMTAB_ALL_CNT(ofl));
2022 	size = symcnt * shdr->sh_entsize;
2023 
2024 	/*
2025 	 * Finalize the section header and data buffer initialization.
2026 	 */
2027 	data->d_size = size;
2028 	shdr->sh_size = (Xword)size;
2029 
2030 	/*
2031 	 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
2032 	 */
2033 	if (xisec) {
2034 		size_t	xsize = symcnt * sizeof (Word);
2035 
2036 		xisec->is_indata->d_size = xsize;
2037 		xisec->is_shdr->sh_size = (Xword)xsize;
2038 	}
2039 
2040 	return (1);
2041 }
2042 
2043 /*
2044  * Build a dynamic symbol table. These tables reside in the text
2045  * segment of a dynamic executable or shared library.
2046  *
2047  *	.SUNW_ldynsym contains local function symbols
2048  *	.dynsym contains only globals symbols
2049  *
2050  * The two tables are created adjacent to each other, with .SUNW_ldynsym
2051  * coming first.
2052  */
2053 static uintptr_t
2054 make_dynsym(Ofl_desc *ofl)
2055 {
2056 	Shdr		*shdr, *lshdr;
2057 	Elf_Data	*data, *ldata;
2058 	Is_desc		*isec, *lisec;
2059 	size_t		size;
2060 	Xword		cnt;
2061 	int		allow_ldynsym;
2062 
2063 	/*
2064 	 * Unless explicitly disabled, always produce a .SUNW_ldynsym section
2065 	 * when it is allowed by the file type, even if the resulting
2066 	 * table only ends up with a single STT_FILE in it. There are
2067 	 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB
2068 	 * entry in the .dynamic section, which is something we would
2069 	 * like to encourage, and (2) Without it, we cannot generate
2070 	 * the associated .SUNW_dyn[sym|tls]sort sections, which are of
2071 	 * value to DTrace.
2072 	 *
2073 	 * In practice, it is extremely rare for an object not to have
2074 	 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be
2075 	 * doing it anyway.
2076 	 */
2077 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
2078 
2079 	/*
2080 	 * Create the section headers. Note that we supply an ent_cnt
2081 	 * of 0. We won't know the count until the section has been placed.
2082 	 */
2083 	if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM,
2084 	    MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR)
2085 		return (S_ERROR);
2086 
2087 	if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0,
2088 	    &isec, &shdr, &data) == S_ERROR)
2089 		return (S_ERROR);
2090 
2091 	/*
2092 	 * Place the section(s) first since it will affect the local symbol
2093 	 * count.
2094 	 */
2095 	if (allow_ldynsym &&
2096 	    ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec, NULL,
2097 	    ld_targ.t_id.id_ldynsym, NULL)) == (Os_desc *)S_ERROR))
2098 		return (S_ERROR);
2099 	ofl->ofl_osdynsym =
2100 	    ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynsym, NULL);
2101 	if (ofl->ofl_osdynsym == (Os_desc *)S_ERROR)
2102 		return (S_ERROR);
2103 
2104 	cnt = DYNSYM_ALL_CNT(ofl);
2105 	size = (size_t)cnt * shdr->sh_entsize;
2106 
2107 	/*
2108 	 * Finalize the section header and data buffer initialization.
2109 	 */
2110 	data->d_size = size;
2111 	shdr->sh_size = (Xword)size;
2112 
2113 	/*
2114 	 * An ldynsym contains local function symbols. It is not
2115 	 * used for linking, but if present, serves to allow better
2116 	 * stack traces to be generated in contexts where the symtab
2117 	 * is not available. (dladdr(), or stripped executable/library files).
2118 	 */
2119 	if (allow_ldynsym) {
2120 		cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt;
2121 		size = (size_t)cnt * shdr->sh_entsize;
2122 
2123 		ldata->d_size = size;
2124 		lshdr->sh_size = (Xword)size;
2125 	}
2126 
2127 	return (1);
2128 }
2129 
2130 /*
2131  * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are
2132  * index sections for the .SUNW_ldynsym/.dynsym pair that present data
2133  * and function symbols sorted by address.
2134  */
2135 static uintptr_t
2136 make_dynsort(Ofl_desc *ofl)
2137 {
2138 	Shdr		*shdr;
2139 	Elf_Data	*data;
2140 	Is_desc		*isec;
2141 
2142 	/* Only do it if the .SUNW_ldynsym section is present */
2143 	if (!OFL_ALLOW_LDYNSYM(ofl))
2144 		return (1);
2145 
2146 	/* .SUNW_dynsymsort */
2147 	if (ofl->ofl_dynsymsortcnt > 0) {
2148 		if (new_section(ofl, SHT_SUNW_symsort,
2149 		    MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt,
2150 		    &isec, &shdr, &data) == S_ERROR)
2151 		return (S_ERROR);
2152 
2153 		if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec, NULL,
2154 		    ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR)
2155 			return (S_ERROR);
2156 	}
2157 
2158 	/* .SUNW_dyntlssort */
2159 	if (ofl->ofl_dyntlssortcnt > 0) {
2160 		if (new_section(ofl, SHT_SUNW_tlssort,
2161 		    MSG_ORIG(MSG_SCN_DYNTLSSORT),
2162 		    ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR)
2163 		return (S_ERROR);
2164 
2165 		if ((ofl->ofl_osdyntlssort = 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 	return (1);
2171 }
2172 
2173 /*
2174  * Helper routine for make_dynsym_shndx. Builds a
2175  * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing
2176  * which one it is.
2177  */
2178 static uintptr_t
2179 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab,
2180     Os_desc **ret_os)
2181 {
2182 	Is_desc		*isec;
2183 	Is_desc		*dynsymisp;
2184 	Shdr		*shdr, *dynshdr;
2185 	Elf_Data	*data;
2186 
2187 	dynsymisp = ld_os_first_isdesc(symtab);
2188 	dynshdr = dynsymisp->is_shdr;
2189 
2190 	if (new_section(ofl, SHT_SYMTAB_SHNDX, shname,
2191 	    (dynshdr->sh_size / dynshdr->sh_entsize),
2192 	    &isec, &shdr, &data) == S_ERROR)
2193 		return (S_ERROR);
2194 
2195 	if ((*ret_os = ld_place_section(ofl, isec, NULL,
2196 	    ld_targ.t_id.id_dynsym_ndx, NULL)) == (Os_desc *)S_ERROR)
2197 		return (S_ERROR);
2198 
2199 	assert(*ret_os);
2200 
2201 	return (1);
2202 }
2203 
2204 /*
2205  * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym
2206  */
2207 static uintptr_t
2208 make_dynsym_shndx(Ofl_desc *ofl)
2209 {
2210 	/*
2211 	 * If there is a .SUNW_ldynsym, generate a section for its extended
2212 	 * index section as well.
2213 	 */
2214 	if (OFL_ALLOW_LDYNSYM(ofl)) {
2215 		if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX),
2216 		    ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR)
2217 			return (S_ERROR);
2218 	}
2219 
2220 	/* The Generate a section for the dynsym */
2221 	if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX),
2222 	    ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR)
2223 		return (S_ERROR);
2224 
2225 	return (1);
2226 }
2227 
2228 
2229 /*
2230  * Build a string table for the section headers.
2231  */
2232 static uintptr_t
2233 make_shstrtab(Ofl_desc *ofl)
2234 {
2235 	Shdr		*shdr;
2236 	Elf_Data	*data;
2237 	Is_desc		*isec;
2238 	size_t		size;
2239 
2240 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB),
2241 	    0, &isec, &shdr, &data) == S_ERROR)
2242 		return (S_ERROR);
2243 
2244 	/*
2245 	 * Place the section first, as it may effect the number of section
2246 	 * headers to account for.
2247 	 */
2248 	ofl->ofl_osshstrtab =
2249 	    ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_note, NULL);
2250 	if (ofl->ofl_osshstrtab == (Os_desc *)S_ERROR)
2251 		return (S_ERROR);
2252 
2253 	size = st_getstrtab_sz(ofl->ofl_shdrsttab);
2254 	assert(size > 0);
2255 
2256 	data->d_size = size;
2257 	shdr->sh_size = (Xword)size;
2258 
2259 	return (1);
2260 }
2261 
2262 /*
2263  * Build a string section for the standard symbol table.
2264  */
2265 static uintptr_t
2266 make_strtab(Ofl_desc *ofl)
2267 {
2268 	Shdr		*shdr;
2269 	Elf_Data	*data;
2270 	Is_desc		*isec;
2271 	size_t		size;
2272 
2273 	/*
2274 	 * This string table consists of all the global and local symbols.
2275 	 * Account for null bytes at end of the file name and the beginning
2276 	 * of section.
2277 	 */
2278 	if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
2279 		return (S_ERROR);
2280 
2281 	size = st_getstrtab_sz(ofl->ofl_strtab);
2282 	assert(size > 0);
2283 
2284 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB),
2285 	    0, &isec, &shdr, &data) == S_ERROR)
2286 		return (S_ERROR);
2287 
2288 	/* Set the size of the data area */
2289 	data->d_size = size;
2290 	shdr->sh_size = (Xword)size;
2291 
2292 	ofl->ofl_osstrtab =
2293 	    ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_strtab, NULL);
2294 	return ((uintptr_t)ofl->ofl_osstrtab);
2295 }
2296 
2297 /*
2298  * Build a string table for the dynamic symbol table.
2299  */
2300 static uintptr_t
2301 make_dynstr(Ofl_desc *ofl)
2302 {
2303 	Shdr		*shdr;
2304 	Elf_Data	*data;
2305 	Is_desc		*isec;
2306 	size_t		size;
2307 
2308 	/*
2309 	 * If producing a .SUNW_ldynsym, account for the initial STT_FILE
2310 	 * symbol that precedes the scope reduced global symbols.
2311 	 */
2312 	if (OFL_ALLOW_LDYNSYM(ofl)) {
2313 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1)
2314 			return (S_ERROR);
2315 		ofl->ofl_dynscopecnt++;
2316 	}
2317 
2318 	/*
2319 	 * Account for any local, named register symbols.  These locals are
2320 	 * required for reference from DT_REGISTER .dynamic entries.
2321 	 */
2322 	if (ofl->ofl_regsyms) {
2323 		int	ndx;
2324 
2325 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
2326 			Sym_desc	*sdp;
2327 
2328 			if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
2329 				continue;
2330 
2331 			if (!SYM_IS_HIDDEN(sdp) &&
2332 			    (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
2333 				continue;
2334 
2335 			if (sdp->sd_sym->st_name == NULL)
2336 				continue;
2337 
2338 			if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
2339 				return (S_ERROR);
2340 		}
2341 	}
2342 
2343 	/*
2344 	 * Reserve entries for any per-symbol auxiliary/filter strings.
2345 	 */
2346 	if (ofl->ofl_dtsfltrs != NULL) {
2347 		Dfltr_desc	*dftp;
2348 		Aliste		idx;
2349 
2350 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp))
2351 			if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
2352 				return (S_ERROR);
2353 	}
2354 
2355 	size = st_getstrtab_sz(ofl->ofl_dynstrtab);
2356 	assert(size > 0);
2357 
2358 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR),
2359 	    0, &isec, &shdr, &data) == S_ERROR)
2360 		return (S_ERROR);
2361 
2362 	/* Make it allocable if necessary */
2363 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
2364 		shdr->sh_flags |= SHF_ALLOC;
2365 
2366 	/* Set the size of the data area */
2367 	data->d_size = size + DYNSTR_EXTRA_PAD;
2368 
2369 	shdr->sh_size = (Xword)size;
2370 
2371 	ofl->ofl_osdynstr =
2372 	    ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_dynstr, NULL);
2373 	return ((uintptr_t)ofl->ofl_osdynstr);
2374 }
2375 
2376 /*
2377  * Generate an output relocation section which will contain the relocation
2378  * information to be applied to the `osp' section.
2379  *
2380  * If (osp == NULL) then we are creating the coalesced relocation section
2381  * for an executable and/or a shared object.
2382  */
2383 static uintptr_t
2384 make_reloc(Ofl_desc *ofl, Os_desc *osp)
2385 {
2386 	Shdr		*shdr;
2387 	Elf_Data	*data;
2388 	Is_desc		*isec;
2389 	size_t		size;
2390 	Xword		sh_flags;
2391 	char 		*sectname;
2392 	Os_desc		*rosp;
2393 	Word		relsize;
2394 	const char	*rel_prefix;
2395 
2396 	/* LINTED */
2397 	if (ld_targ.t_m.m_rel_sht_type == SHT_REL) {
2398 		/* REL */
2399 		relsize = sizeof (Rel);
2400 		rel_prefix = MSG_ORIG(MSG_SCN_REL);
2401 	} else {
2402 		/* RELA */
2403 		relsize = sizeof (Rela);
2404 		rel_prefix = MSG_ORIG(MSG_SCN_RELA);
2405 	}
2406 
2407 	if (osp) {
2408 		size = osp->os_szoutrels;
2409 		sh_flags = osp->os_shdr->sh_flags;
2410 		if ((sectname = libld_malloc(strlen(rel_prefix) +
2411 		    strlen(osp->os_name) + 1)) == 0)
2412 			return (S_ERROR);
2413 		(void) strcpy(sectname, rel_prefix);
2414 		(void) strcat(sectname, osp->os_name);
2415 	} else if (ofl->ofl_flags & FLG_OF_COMREL) {
2416 		size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
2417 		sh_flags = SHF_ALLOC;
2418 		sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
2419 	} else {
2420 		size = ofl->ofl_relocrelsz;
2421 		sh_flags = SHF_ALLOC;
2422 		sectname = (char *)rel_prefix;
2423 	}
2424 
2425 	/*
2426 	 * Keep track of total size of 'output relocations' (to be stored
2427 	 * in .dynamic)
2428 	 */
2429 	/* LINTED */
2430 	ofl->ofl_relocsz += (Xword)size;
2431 
2432 	if (new_section(ofl, ld_targ.t_m.m_rel_sht_type, sectname, 0, &isec,
2433 	    &shdr, &data) == S_ERROR)
2434 		return (S_ERROR);
2435 
2436 	data->d_size = size;
2437 
2438 	shdr->sh_size = (Xword)size;
2439 	if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC))
2440 		shdr->sh_flags = SHF_ALLOC;
2441 
2442 	if (osp) {
2443 		/*
2444 		 * The sh_info field of the SHT_REL* sections points to the
2445 		 * section the relocations are to be applied to.
2446 		 */
2447 		shdr->sh_flags |= SHF_INFO_LINK;
2448 	}
2449 
2450 	rosp = ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_rel, NULL);
2451 	if (rosp == (Os_desc *)S_ERROR)
2452 		return (S_ERROR);
2453 
2454 	/*
2455 	 * Associate this relocation section to the section its going to
2456 	 * relocate.
2457 	 */
2458 	if (osp) {
2459 		Aliste	idx;
2460 		Is_desc	*risp;
2461 
2462 		/*
2463 		 * This is used primarily so that we can update
2464 		 * SHT_GROUP[sect_no] entries to point to the
2465 		 * created output relocation sections.
2466 		 */
2467 		for (APLIST_TRAVERSE(osp->os_relisdescs, idx, risp)) {
2468 			risp->is_osdesc = rosp;
2469 
2470 			/*
2471 			 * If the input relocation section had the SHF_GROUP
2472 			 * flag set - propagate it to the output relocation
2473 			 * section.
2474 			 */
2475 			if (risp->is_shdr->sh_flags & SHF_GROUP) {
2476 				rosp->os_shdr->sh_flags |= SHF_GROUP;
2477 				break;
2478 			}
2479 		}
2480 		osp->os_relosdesc = rosp;
2481 	} else
2482 		ofl->ofl_osrel = rosp;
2483 
2484 	/*
2485 	 * If this is the first relocation section we've encountered save it
2486 	 * so that the .dynamic entry can be initialized accordingly.
2487 	 */
2488 	if (ofl->ofl_osrelhead == (Os_desc *)0)
2489 		ofl->ofl_osrelhead = rosp;
2490 
2491 	return (1);
2492 }
2493 
2494 /*
2495  * Generate version needed section.
2496  */
2497 static uintptr_t
2498 make_verneed(Ofl_desc *ofl)
2499 {
2500 	Shdr		*shdr;
2501 	Elf_Data	*data;
2502 	Is_desc		*isec;
2503 
2504 	/*
2505 	 * verneed sections do not have a constant element size, so the
2506 	 * value of ent_cnt specified here (0) is meaningless.
2507 	 */
2508 	if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION),
2509 	    0, &isec, &shdr, &data) == S_ERROR)
2510 		return (S_ERROR);
2511 
2512 	/* During version processing we calculated the total size. */
2513 	data->d_size = ofl->ofl_verneedsz;
2514 	shdr->sh_size = (Xword)ofl->ofl_verneedsz;
2515 
2516 	ofl->ofl_osverneed =
2517 	    ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_version, NULL);
2518 	return ((uintptr_t)ofl->ofl_osverneed);
2519 }
2520 
2521 /*
2522  * Generate a version definition section.
2523  *
2524  *  o	the SHT_SUNW_verdef section defines the versions that exist within this
2525  *	image.
2526  */
2527 static uintptr_t
2528 make_verdef(Ofl_desc *ofl)
2529 {
2530 	Shdr		*shdr;
2531 	Elf_Data	*data;
2532 	Is_desc		*isec;
2533 	Ver_desc	*vdp;
2534 	Str_tbl		*strtab;
2535 
2536 	/*
2537 	 * Reserve a string table entry for the base version dependency (other
2538 	 * dependencies have symbol representations, which will already be
2539 	 * accounted for during symbol processing).
2540 	 */
2541 	vdp = (Ver_desc *)ofl->ofl_verdesc->apl_data[0];
2542 
2543 	if (OFL_IS_STATIC_OBJ(ofl))
2544 		strtab = ofl->ofl_strtab;
2545 	else
2546 		strtab = ofl->ofl_dynstrtab;
2547 
2548 	if (st_insert(strtab, vdp->vd_name) == -1)
2549 		return (S_ERROR);
2550 
2551 	/*
2552 	 * verdef sections do not have a constant element size, so the
2553 	 * value of ent_cnt specified here (0) is meaningless.
2554 	 */
2555 	if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION),
2556 	    0, &isec, &shdr, &data) == S_ERROR)
2557 		return (S_ERROR);
2558 
2559 	/* During version processing we calculated the total size. */
2560 	data->d_size = ofl->ofl_verdefsz;
2561 	shdr->sh_size = (Xword)ofl->ofl_verdefsz;
2562 
2563 	ofl->ofl_osverdef =
2564 	    ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_version, NULL);
2565 	return ((uintptr_t)ofl->ofl_osverdef);
2566 }
2567 
2568 /*
2569  * This routine is called when -z nopartial is in effect.
2570  */
2571 uintptr_t
2572 ld_make_parexpn_data(Ofl_desc *ofl, size_t size, Xword align)
2573 {
2574 	Shdr		*shdr;
2575 	Elf_Data	*data;
2576 	Is_desc		*isec;
2577 	Os_desc		*osp;
2578 
2579 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2580 	    &isec, &shdr, &data) == S_ERROR)
2581 		return (S_ERROR);
2582 
2583 	shdr->sh_flags |= SHF_WRITE;
2584 	data->d_size = size;
2585 	shdr->sh_size = (Xword)size;
2586 	if (align != 0) {
2587 		data->d_align = align;
2588 		shdr->sh_addralign = align;
2589 	}
2590 
2591 	if ((data->d_buf = libld_calloc(size, 1)) == NULL)
2592 		return (S_ERROR);
2593 
2594 	/*
2595 	 * Retain handle to this .data input section. Variables using move
2596 	 * sections (partial initialization) will be redirected here when
2597 	 * such global references are added and '-z nopartial' is in effect.
2598 	 */
2599 	ofl->ofl_isparexpn = isec;
2600 	osp = ld_place_section(ofl, isec, NULL, ld_targ.t_id.id_data, NULL);
2601 	if (osp == (Os_desc *)S_ERROR)
2602 		return (S_ERROR);
2603 
2604 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
2605 		ofl->ofl_dynshdrcnt++;
2606 		osp->os_flags |= FLG_OS_OUTREL;
2607 	}
2608 	return (1);
2609 }
2610 
2611 /*
2612  * Make .sunwmove section
2613  */
2614 uintptr_t
2615 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2616 {
2617 	Shdr		*shdr;
2618 	Elf_Data	*data;
2619 	Is_desc		*isec;
2620 	Aliste		idx;
2621 	Sym_desc	*sdp;
2622 	int 		cnt = 1;
2623 
2624 
2625 	if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE),
2626 	    mv_nums, &isec, &shdr, &data) == S_ERROR)
2627 		return (S_ERROR);
2628 
2629 	if ((data->d_buf = libld_calloc(data->d_size, 1)) == NULL)
2630 		return (S_ERROR);
2631 
2632 	/*
2633 	 * Copy move entries
2634 	 */
2635 	for (APLIST_TRAVERSE(ofl->ofl_parsyms, idx, sdp)) {
2636 		Aliste		idx2;
2637 		Mv_desc		*mdp;
2638 
2639 		if (sdp->sd_flags & FLG_SY_PAREXPN)
2640 			continue;
2641 
2642 		for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp))
2643 			mdp->md_oidx = cnt++;
2644 	}
2645 
2646 	if ((ofl->ofl_osmove = ld_place_section(ofl, isec, NULL, 0, NULL)) ==
2647 	    (Os_desc *)S_ERROR)
2648 		return (S_ERROR);
2649 
2650 	return (1);
2651 }
2652 
2653 /*
2654  * Given a relocation descriptor that references a string table
2655  * input section, locate the string referenced and return a pointer
2656  * to it.
2657  */
2658 static const char *
2659 strmerge_get_reloc_str(Ofl_desc *ofl, Rel_desc *rsp)
2660 {
2661 	Sym_desc *sdp = rsp->rel_sym;
2662 	Xword	 str_off;
2663 
2664 	/*
2665 	 * In the case of an STT_SECTION symbol, the addend of the
2666 	 * relocation gives the offset into the string section. For
2667 	 * other symbol types, the symbol value is the offset.
2668 	 */
2669 
2670 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
2671 		str_off = sdp->sd_sym->st_value;
2672 	} else if ((rsp->rel_flags & FLG_REL_RELA) == FLG_REL_RELA) {
2673 		/*
2674 		 * For SHT_RELA, the addend value is found in the
2675 		 * rel_raddend field of the relocation.
2676 		 */
2677 		str_off = rsp->rel_raddend;
2678 	} else {	/* REL and STT_SECTION */
2679 		/*
2680 		 * For SHT_REL, the "addend" is not part of the relocation
2681 		 * record. Instead, it is found at the relocation target
2682 		 * address.
2683 		 */
2684 		uchar_t *addr = (uchar_t *)((uintptr_t)rsp->rel_roffset +
2685 		    (uintptr_t)rsp->rel_isdesc->is_indata->d_buf);
2686 
2687 		if (ld_reloc_targval_get(ofl, rsp, addr, &str_off) == 0)
2688 			return (0);
2689 	}
2690 
2691 	return (str_off + (char *)sdp->sd_isc->is_indata->d_buf);
2692 }
2693 
2694 /*
2695  * First pass over the relocation records for string table merging.
2696  * Build lists of relocations and symbols that will need modification,
2697  * and insert the strings they reference into the mstrtab string table.
2698  *
2699  * entry:
2700  *	ofl, osp - As passed to ld_make_strmerge().
2701  *	mstrtab - String table to receive input strings. This table
2702  *		must be in its first (initialization) pass and not
2703  *		yet cooked (st_getstrtab_sz() not yet called).
2704  *	rel_alpp - APlist to receive pointer to any relocation
2705  *		descriptors with STT_SECTION symbols that reference
2706  *		one of the input sections being merged.
2707  *	sym_alpp - APlist to receive pointer to any symbols that reference
2708  *		one of the input sections being merged.
2709  *	rcp - Pointer to cache of relocation descriptors to examine.
2710  *		Either &ofl->ofl_actrels (active relocations)
2711  *		or &ofl->ofl_outrels (output relocations).
2712  *
2713  * exit:
2714  *	On success, rel_alpp and sym_alpp are updated, and
2715  *	any strings in the mergeable input sections referenced by
2716  *	a relocation has been entered into mstrtab. True (1) is returned.
2717  *
2718  *	On failure, False (0) is returned.
2719  */
2720 static int
2721 strmerge_pass1(Ofl_desc *ofl, Os_desc *osp, Str_tbl *mstrtab,
2722     APlist **rel_alpp, APlist **sym_alpp, Rel_cache *rcp)
2723 {
2724 	Aliste		idx;
2725 	Rel_cachebuf	*rcbp;
2726 	Sym_desc	*sdp;
2727 	Sym_desc	*last_sdp = NULL;
2728 	Rel_desc	*rsp;
2729 	const char	*name;
2730 
2731 	REL_CACHE_TRAVERSE(rcp, idx, rcbp, rsp) {
2732 		sdp = rsp->rel_sym;
2733 		if ((sdp->sd_isc == NULL) || ((sdp->sd_isc->is_flags &
2734 		    (FLG_IS_DISCARD | FLG_IS_INSTRMRG)) != FLG_IS_INSTRMRG) ||
2735 		    (sdp->sd_isc->is_osdesc != osp))
2736 			continue;
2737 
2738 		/*
2739 		 * Remember symbol for use in the third pass. There is no
2740 		 * reason to save a given symbol more than once, so we take
2741 		 * advantage of the fact that relocations to a given symbol
2742 		 * tend to cluster in the list. If this is the same symbol
2743 		 * we saved last time, don't bother.
2744 		 */
2745 		if (last_sdp != sdp) {
2746 			if (aplist_append(sym_alpp, sdp, AL_CNT_STRMRGSYM) ==
2747 			    NULL)
2748 				return (0);
2749 			last_sdp = sdp;
2750 		}
2751 
2752 		/* Enter the string into our new string table */
2753 		name = strmerge_get_reloc_str(ofl, rsp);
2754 		if (st_insert(mstrtab, name) == -1)
2755 			return (0);
2756 
2757 		/*
2758 		 * If this is an STT_SECTION symbol, then the second pass
2759 		 * will need to modify this relocation, so hang on to it.
2760 		 */
2761 		if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) &&
2762 		    (aplist_append(rel_alpp, rsp, AL_CNT_STRMRGREL) == NULL))
2763 			return (0);
2764 	}
2765 
2766 	return (1);
2767 }
2768 
2769 /*
2770  * If the output section has any SHF_MERGE|SHF_STRINGS input sections,
2771  * replace them with a single merged/compressed input section.
2772  *
2773  * entry:
2774  *	ofl - Output file descriptor
2775  *	osp - Output section descriptor
2776  *	rel_alpp, sym_alpp, - Address of 2 APlists, to be used
2777  *		for internal processing. On the initial call to
2778  *		ld_make_strmerge, these list pointers must be NULL.
2779  *		The caller is encouraged to pass the same lists back for
2780  *		successive calls to this function without freeing
2781  *		them in between calls. This causes a single pair of
2782  *		memory allocations to be reused multiple times.
2783  *
2784  * exit:
2785  *	If section merging is possible, it is done. If no errors are
2786  *	encountered, True (1) is returned. On error, S_ERROR.
2787  *
2788  *	The contents of rel_alpp and sym_alpp on exit are
2789  *	undefined. The caller can free them, or pass them back to a subsequent
2790  *	call to this routine, but should not examine their contents.
2791  */
2792 static uintptr_t
2793 ld_make_strmerge(Ofl_desc *ofl, Os_desc *osp, APlist **rel_alpp,
2794     APlist **sym_alpp)
2795 {
2796 	Str_tbl		*mstrtab;	/* string table for string merge secs */
2797 	Is_desc		*mstrsec;	/* Generated string merge section */
2798 	Is_desc		*isp;
2799 	Shdr		*mstr_shdr;
2800 	Elf_Data	*mstr_data;
2801 	Sym_desc	*sdp;
2802 	Rel_desc	*rsp;
2803 	Aliste		idx;
2804 	size_t		data_size;
2805 	int		st_setstring_status;
2806 	size_t		stoff;
2807 
2808 	/* If string table compression is disabled, there's nothing to do */
2809 	if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0)
2810 		return (1);
2811 
2812 	/*
2813 	 * Pass over the mergeable input sections, and if they haven't
2814 	 * all been discarded, create a string table.
2815 	 */
2816 	mstrtab = NULL;
2817 	for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
2818 		if (isp->is_flags & FLG_IS_DISCARD)
2819 			continue;
2820 
2821 		/*
2822 		 * We have at least one non-discarded section.
2823 		 * Create a string table descriptor.
2824 		 */
2825 		if ((mstrtab = st_new(FLG_STNEW_COMPRESS)) == NULL)
2826 			return (S_ERROR);
2827 		break;
2828 	}
2829 
2830 	/* If no string table was created, we have no mergeable sections */
2831 	if (mstrtab == NULL)
2832 		return (1);
2833 
2834 	/*
2835 	 * This routine has to make 3 passes:
2836 	 *
2837 	 *	1) Examine all relocations, insert strings from relocations
2838 	 *		to the mergeable input sections into the string table.
2839 	 *	2) Modify the relocation values to be correct for the
2840 	 *		new merged section.
2841 	 *	3) Modify the symbols used by the relocations to reference
2842 	 *		the new section.
2843 	 *
2844 	 * These passes cannot be combined:
2845 	 *	- The string table code works in two passes, and all
2846 	 *		strings have to be loaded in pass one before the
2847 	 *		offset of any strings can be determined.
2848 	 *	- Multiple relocations reference a single symbol, so the
2849 	 *		symbol cannot be modified until all relocations are
2850 	 *		fixed.
2851 	 *
2852 	 * The number of relocations related to section merging is usually
2853 	 * a mere fraction of the overall active and output relocation lists,
2854 	 * and the number of symbols is usually a fraction of the number
2855 	 * of related relocations. We therefore build APlists for the
2856 	 * relocations and symbols in the first pass, and then use those
2857 	 * lists to accelerate the operation of pass 2 and 3.
2858 	 *
2859 	 * Reinitialize the lists to a completely empty state.
2860 	 */
2861 	aplist_reset(*rel_alpp);
2862 	aplist_reset(*sym_alpp);
2863 
2864 	/*
2865 	 * Pass 1:
2866 	 *
2867 	 * Every relocation related to this output section (and the input
2868 	 * sections that make it up) is found in either the active, or the
2869 	 * output relocation list, depending on whether the relocation is to
2870 	 * be processed by this invocation of the linker, or inserted into the
2871 	 * output object.
2872 	 *
2873 	 * Build lists of relocations and symbols that will need modification,
2874 	 * and insert the strings they reference into the mstrtab string table.
2875 	 */
2876 	if (strmerge_pass1(ofl, osp, mstrtab, rel_alpp, sym_alpp,
2877 	    &ofl->ofl_actrels) == 0)
2878 		goto return_s_error;
2879 	if (strmerge_pass1(ofl, osp, mstrtab, rel_alpp, sym_alpp,
2880 	    &ofl->ofl_outrels) == 0)
2881 		goto return_s_error;
2882 
2883 	/*
2884 	 * Get the size of the new input section. Requesting the
2885 	 * string table size "cooks" the table, and finalizes its contents.
2886 	 */
2887 	data_size = st_getstrtab_sz(mstrtab);
2888 
2889 	/* Create a new input section to hold the merged strings */
2890 	if (new_section_from_template(ofl, isp, data_size,
2891 	    &mstrsec, &mstr_shdr, &mstr_data) == S_ERROR)
2892 		goto return_s_error;
2893 	mstrsec->is_flags |= FLG_IS_GNSTRMRG;
2894 
2895 	/*
2896 	 * Allocate a data buffer for the new input section.
2897 	 * Then, associate the buffer with the string table descriptor.
2898 	 */
2899 	if ((mstr_data->d_buf = libld_malloc(data_size)) == NULL)
2900 		goto return_s_error;
2901 	if (st_setstrbuf(mstrtab, mstr_data->d_buf, data_size) == -1)
2902 		goto return_s_error;
2903 
2904 	/* Add the new section to the output image */
2905 	if (ld_place_section(ofl, mstrsec, NULL, osp->os_identndx, NULL) ==
2906 	    (Os_desc *)S_ERROR)
2907 		goto return_s_error;
2908 
2909 	/*
2910 	 * Pass 2:
2911 	 *
2912 	 * Revisit the relocation descriptors with STT_SECTION symbols
2913 	 * that were saved by the first pass. Update each relocation
2914 	 * record so that the offset it contains is for the new section
2915 	 * instead of the original.
2916 	 */
2917 	for (APLIST_TRAVERSE(*rel_alpp, idx, rsp)) {
2918 		const char	*name;
2919 
2920 		/* Put the string into the merged string table */
2921 		name = strmerge_get_reloc_str(ofl, rsp);
2922 		st_setstring_status = st_setstring(mstrtab, name, &stoff);
2923 		if (st_setstring_status == -1) {
2924 			/*
2925 			 * A failure to insert at this point means that
2926 			 * something is corrupt. This isn't a resource issue.
2927 			 */
2928 			assert(st_setstring_status != -1);
2929 			goto return_s_error;
2930 		}
2931 
2932 		/*
2933 		 * Alter the relocation to access the string at the
2934 		 * new offset in our new string table.
2935 		 *
2936 		 * For SHT_RELA platforms, it suffices to simply
2937 		 * update the rel_raddend field of the relocation.
2938 		 *
2939 		 * For SHT_REL platforms, the new "addend" value
2940 		 * needs to be written at the address being relocated.
2941 		 * However, we can't alter the input sections which
2942 		 * are mapped readonly, and the output image has not
2943 		 * been created yet. So, we defer this operation,
2944 		 * using the rel_raddend field of the relocation
2945 		 * which is normally 0 on a REL platform, to pass the
2946 		 * new "addend" value to ld_perform_outreloc() or
2947 		 * ld_do_activerelocs(). The FLG_REL_NADDEND flag
2948 		 * tells them that this is the case.
2949 		 */
2950 		if ((rsp->rel_flags & FLG_REL_RELA) == 0)   /* REL */
2951 			rsp->rel_flags |= FLG_REL_NADDEND;
2952 		rsp->rel_raddend = (Sxword)stoff;
2953 
2954 		/*
2955 		 * Generate a symbol name string for STT_SECTION symbols
2956 		 * that might reference our merged section. This shows up
2957 		 * in debug output and helps show how the relocation has
2958 		 * changed from its original input section to our merged one.
2959 		 */
2960 		if (ld_stt_section_sym_name(mstrsec) == NULL)
2961 			goto return_s_error;
2962 	}
2963 
2964 	/*
2965 	 * Pass 3:
2966 	 *
2967 	 * Modify the symbols referenced by the relocation descriptors
2968 	 * so that they reference the new input section containing the
2969 	 * merged strings instead of the original input sections.
2970 	 */
2971 	for (APLIST_TRAVERSE(*sym_alpp, idx, sdp)) {
2972 		/*
2973 		 * If we've already processed this symbol, don't do it
2974 		 * twice. strmerge_pass1() uses a heuristic (relocations to
2975 		 * the same symbol clump together) to avoid inserting a
2976 		 * given symbol more than once, but repeat symbols in
2977 		 * the list can occur.
2978 		 */
2979 		if ((sdp->sd_isc->is_flags & FLG_IS_INSTRMRG) == 0)
2980 			continue;
2981 
2982 		if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
2983 			/*
2984 			 * This is not an STT_SECTION symbol, so its
2985 			 * value is the offset of the string within the
2986 			 * input section. Update the address to reflect
2987 			 * the address in our new merged section.
2988 			 */
2989 			const char *name = sdp->sd_sym->st_value +
2990 			    (char *)sdp->sd_isc->is_indata->d_buf;
2991 
2992 			st_setstring_status =
2993 			    st_setstring(mstrtab, name, &stoff);
2994 			if (st_setstring_status == -1) {
2995 				/*
2996 				 * A failure to insert at this point means
2997 				 * something is corrupt. This isn't a
2998 				 * resource issue.
2999 				 */
3000 				assert(st_setstring_status != -1);
3001 				goto return_s_error;
3002 			}
3003 
3004 			if (ld_sym_copy(sdp) == S_ERROR)
3005 				goto return_s_error;
3006 			sdp->sd_sym->st_value = (Word)stoff;
3007 		}
3008 
3009 		/* Redirect the symbol to our new merged section */
3010 		sdp->sd_isc = mstrsec;
3011 	}
3012 
3013 	/*
3014 	 * There are no references left to the original input string sections.
3015 	 * Mark them as discarded so they don't go into the output image.
3016 	 * At the same time, add up the sizes of the replaced sections.
3017 	 */
3018 	data_size = 0;
3019 	for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
3020 		if (isp->is_flags & (FLG_IS_DISCARD | FLG_IS_GNSTRMRG))
3021 			continue;
3022 
3023 		data_size += isp->is_indata->d_size;
3024 
3025 		isp->is_flags |= FLG_IS_DISCARD;
3026 		DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp, mstrsec));
3027 	}
3028 
3029 	/* Report how much space we saved in the output section */
3030 	DBG_CALL(Dbg_sec_genstr_compress(ofl->ofl_lml, osp->os_name, data_size,
3031 	    mstr_data->d_size));
3032 
3033 	st_destroy(mstrtab);
3034 	return (1);
3035 
3036 return_s_error:
3037 	st_destroy(mstrtab);
3038 	return (S_ERROR);
3039 }
3040 
3041 /*
3042  * Update a data buffers size.  A number of sections have to be created, and
3043  * the sections header contributes to the size of the eventual section.  Thus,
3044  * a section may be created, and once all associated sections have been created,
3045  * we return to establish the required section size.
3046  */
3047 inline static void
3048 update_data_size(Os_desc *osp, ulong_t cnt)
3049 {
3050 	Is_desc		*isec = ld_os_first_isdesc(osp);
3051 	Elf_Data	*data = isec->is_indata;
3052 	Shdr		*shdr = osp->os_shdr;
3053 	size_t		size = cnt * shdr->sh_entsize;
3054 
3055 	shdr->sh_size = (Xword)size;
3056 	data->d_size = size;
3057 }
3058 
3059 /*
3060  * The following sections are built after all input file processing and symbol
3061  * validation has been carried out.  The order is important (because the
3062  * addition of a section adds a new symbol there is a chicken and egg problem
3063  * of maintaining the appropriate counts).  By maintaining a known order the
3064  * individual routines can compensate for later, known, additions.
3065  */
3066 uintptr_t
3067 ld_make_sections(Ofl_desc *ofl)
3068 {
3069 	ofl_flag_t	flags = ofl->ofl_flags;
3070 	Sg_desc		*sgp;
3071 
3072 	/*
3073 	 * Generate any special sections.
3074 	 */
3075 	if (flags & FLG_OF_ADDVERS)
3076 		if (make_comment(ofl) == S_ERROR)
3077 			return (S_ERROR);
3078 
3079 	if (make_interp(ofl) == S_ERROR)
3080 		return (S_ERROR);
3081 
3082 	/*
3083 	 * Create a capabilities section if required.
3084 	 */
3085 	if (make_cap(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP),
3086 	    ld_targ.t_id.id_cap) == S_ERROR)
3087 		return (S_ERROR);
3088 
3089 	/*
3090 	 * Create any init/fini array sections.
3091 	 */
3092 	if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
3093 	    ofl->ofl_initarray) == S_ERROR)
3094 		return (S_ERROR);
3095 
3096 	if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
3097 	    ofl->ofl_finiarray) == S_ERROR)
3098 		return (S_ERROR);
3099 
3100 	if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
3101 	    ofl->ofl_preiarray) == S_ERROR)
3102 		return (S_ERROR);
3103 
3104 	/*
3105 	 * Make the .plt section.  This occurs after any other relocation
3106 	 * sections are generated (see reloc_init()) to ensure that the
3107 	 * associated relocation section is after all the other relocation
3108 	 * sections.
3109 	 */
3110 	if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
3111 		if (make_plt(ofl) == S_ERROR)
3112 			return (S_ERROR);
3113 
3114 	/*
3115 	 * Determine whether any sections or files are not referenced.  Under
3116 	 * -Dunused a diagnostic for any unused components is generated, under
3117 	 * -zignore the component is removed from the final output.
3118 	 */
3119 	if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
3120 		if (ignore_section_processing(ofl) == S_ERROR)
3121 			return (S_ERROR);
3122 	}
3123 
3124 	/*
3125 	 * If we have detected a situation in which previously placed
3126 	 * output sections may have been discarded, perform the necessary
3127 	 * readjustment.
3128 	 */
3129 	if (ofl->ofl_flags & FLG_OF_ADJOSCNT)
3130 		adjust_os_count(ofl);
3131 
3132 	/*
3133 	 * Do any of the output sections contain input sections that
3134 	 * are candidates for string table merging? For each such case,
3135 	 * we create a replacement section, insert it, and discard the
3136 	 * originals.
3137 	 *
3138 	 * rel_alpp and sym_alpp are used by ld_make_strmerge()
3139 	 * for its internal processing. We are responsible for the
3140 	 * initialization and cleanup, and ld_make_strmerge() handles the rest.
3141 	 * This allows us to reuse a single pair of memory buffers, allocated
3142 	 * for this processing, for all the output sections.
3143 	 */
3144 	if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) == 0) {
3145 		int	error_seen = 0;
3146 		APlist	*rel_alpp = NULL;
3147 		APlist	*sym_alpp = NULL;
3148 		Aliste	idx1;
3149 
3150 		for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3151 			Os_desc	*osp;
3152 			Aliste	idx2;
3153 
3154 			for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp))
3155 				if ((osp->os_mstrisdescs != NULL) &&
3156 				    (ld_make_strmerge(ofl, osp,
3157 				    &rel_alpp, &sym_alpp) ==
3158 				    S_ERROR)) {
3159 					error_seen = 1;
3160 					break;
3161 				}
3162 		}
3163 		if (rel_alpp != NULL)
3164 			libld_free(rel_alpp);
3165 		if (sym_alpp != NULL)
3166 			libld_free(sym_alpp);
3167 		if (error_seen != 0)
3168 			return (S_ERROR);
3169 	}
3170 
3171 	/*
3172 	 * Add any necessary versioning information.
3173 	 */
3174 	if (!(flags & FLG_OF_NOVERSEC)) {
3175 		if ((flags & FLG_OF_VERNEED) &&
3176 		    (make_verneed(ofl) == S_ERROR))
3177 			return (S_ERROR);
3178 		if ((flags & FLG_OF_VERDEF) &&
3179 		    (make_verdef(ofl) == S_ERROR))
3180 			return (S_ERROR);
3181 		if ((flags & (FLG_OF_VERNEED | FLG_OF_VERDEF)) &&
3182 		    ((ofl->ofl_osversym = make_sym_sec(ofl,
3183 		    MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym,
3184 		    ld_targ.t_id.id_version)) == (Os_desc*)S_ERROR))
3185 			return (S_ERROR);
3186 	}
3187 
3188 	/*
3189 	 * Create a syminfo section if necessary.
3190 	 */
3191 	if (flags & FLG_OF_SYMINFO) {
3192 		if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
3193 		    MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo,
3194 		    ld_targ.t_id.id_syminfo)) == (Os_desc *)S_ERROR)
3195 			return (S_ERROR);
3196 	}
3197 
3198 	if (flags & FLG_OF_COMREL) {
3199 		/*
3200 		 * If -zcombreloc is enabled then all relocations (except for
3201 		 * the PLT's) are coalesced into a single relocation section.
3202 		 */
3203 		if (ofl->ofl_reloccnt) {
3204 			if (make_reloc(ofl, NULL) == S_ERROR)
3205 				return (S_ERROR);
3206 		}
3207 	} else {
3208 		Aliste	idx1;
3209 
3210 		/*
3211 		 * Create the required output relocation sections.  Note, new
3212 		 * sections may be added to the section list that is being
3213 		 * traversed.  These insertions can move the elements of the
3214 		 * Alist such that a section descriptor is re-read.  Recursion
3215 		 * is prevented by maintaining a previous section pointer and
3216 		 * insuring that this pointer isn't re-examined.
3217 		 */
3218 		for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3219 			Os_desc	*osp, *posp = 0;
3220 			Aliste	idx2;
3221 
3222 			for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
3223 				if ((osp != posp) && osp->os_szoutrels &&
3224 				    (osp != ofl->ofl_osplt)) {
3225 					if (make_reloc(ofl, osp) == S_ERROR)
3226 						return (S_ERROR);
3227 				}
3228 				posp = osp;
3229 			}
3230 		}
3231 
3232 		/*
3233 		 * If we're not building a combined relocation section, then
3234 		 * build a .rel[a] section as required.
3235 		 */
3236 		if (ofl->ofl_relocrelsz) {
3237 			if (make_reloc(ofl, NULL) == S_ERROR)
3238 				return (S_ERROR);
3239 		}
3240 	}
3241 
3242 	/*
3243 	 * The PLT relocations are always in their own section, and we try to
3244 	 * keep them at the end of the PLT table.  We do this to keep the hot
3245 	 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
3246 	 */
3247 	if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
3248 		if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
3249 			return (S_ERROR);
3250 	}
3251 
3252 	/*
3253 	 * Finally build the symbol and section header sections.
3254 	 */
3255 	if (flags & FLG_OF_DYNAMIC) {
3256 		if (make_dynamic(ofl) == S_ERROR)
3257 			return (S_ERROR);
3258 
3259 		/*
3260 		 * A number of sections aren't necessary within a relocatable
3261 		 * object, even if -dy has been used.
3262 		 */
3263 		if (!(flags & FLG_OF_RELOBJ)) {
3264 			if (make_hash(ofl) == S_ERROR)
3265 				return (S_ERROR);
3266 			if (make_dynstr(ofl) == S_ERROR)
3267 				return (S_ERROR);
3268 			if (make_dynsym(ofl) == S_ERROR)
3269 				return (S_ERROR);
3270 			if (ld_unwind_make_hdr(ofl) == S_ERROR)
3271 				return (S_ERROR);
3272 			if (make_dynsort(ofl) == S_ERROR)
3273 				return (S_ERROR);
3274 		}
3275 	}
3276 
3277 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
3278 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
3279 		/*
3280 		 * Do we need to make a SHT_SYMTAB_SHNDX section
3281 		 * for the dynsym.  If so - do it now.
3282 		 */
3283 		if (ofl->ofl_osdynsym &&
3284 		    ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
3285 			if (make_dynsym_shndx(ofl) == S_ERROR)
3286 				return (S_ERROR);
3287 		}
3288 
3289 		if (make_strtab(ofl) == S_ERROR)
3290 			return (S_ERROR);
3291 		if (make_symtab(ofl) == S_ERROR)
3292 			return (S_ERROR);
3293 	} else {
3294 		/*
3295 		 * Do we need to make a SHT_SYMTAB_SHNDX section
3296 		 * for the dynsym.  If so - do it now.
3297 		 */
3298 		if (ofl->ofl_osdynsym &&
3299 		    ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
3300 			if (make_dynsym_shndx(ofl) == S_ERROR)
3301 				return (S_ERROR);
3302 		}
3303 	}
3304 
3305 	if (make_shstrtab(ofl) == S_ERROR)
3306 		return (S_ERROR);
3307 
3308 	/*
3309 	 * Now that we've created all output sections, adjust the size of the
3310 	 * SHT_SUNW_versym and SHT_SUNW_syminfo section, which are dependent on
3311 	 * the associated symbol table sizes.
3312 	 */
3313 	if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
3314 		ulong_t		cnt;
3315 		Is_desc		*isp;
3316 		Os_desc		*osp;
3317 
3318 		if (OFL_IS_STATIC_OBJ(ofl))
3319 			osp = ofl->ofl_ossymtab;
3320 		else
3321 			osp = ofl->ofl_osdynsym;
3322 
3323 		isp = ld_os_first_isdesc(osp);
3324 		cnt = (isp->is_shdr->sh_size / isp->is_shdr->sh_entsize);
3325 
3326 		if (ofl->ofl_osversym)
3327 			update_data_size(ofl->ofl_osversym, cnt);
3328 
3329 		if (ofl->ofl_ossyminfo)
3330 			update_data_size(ofl->ofl_ossyminfo, cnt);
3331 	}
3332 
3333 	/*
3334 	 * Now that we've created all output sections, adjust the size of the
3335 	 * SHT_SUNW_capinfo, which is dependent on the associated symbol table
3336 	 * size.
3337 	 */
3338 	if (ofl->ofl_oscapinfo) {
3339 		ulong_t	cnt;
3340 
3341 		/*
3342 		 * Symbol capabilities symbols are placed directly after the
3343 		 * STT_FILE symbol, section symbols, and any register symbols.
3344 		 * Effectively these are the first of any series of demoted
3345 		 * (scoped) symbols.
3346 		 */
3347 		if (OFL_IS_STATIC_OBJ(ofl))
3348 			cnt = SYMTAB_ALL_CNT(ofl);
3349 		else
3350 			cnt = DYNSYM_ALL_CNT(ofl);
3351 
3352 		update_data_size(ofl->ofl_oscapinfo, cnt);
3353 	}
3354 	return (1);
3355 }
3356 
3357 /*
3358  * Build an additional data section - used to back OBJT symbol definitions
3359  * added with a mapfile.
3360  */
3361 Is_desc *
3362 ld_make_data(Ofl_desc *ofl, size_t size)
3363 {
3364 	Shdr		*shdr;
3365 	Elf_Data	*data;
3366 	Is_desc		*isec;
3367 
3368 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
3369 	    &isec, &shdr, &data) == S_ERROR)
3370 		return ((Is_desc *)S_ERROR);
3371 
3372 	data->d_size = size;
3373 	shdr->sh_size = (Xword)size;
3374 	shdr->sh_flags |= SHF_WRITE;
3375 
3376 	if (aplist_append(&ofl->ofl_mapdata, isec, AL_CNT_OFL_MAPSECS) == NULL)
3377 		return ((Is_desc *)S_ERROR);
3378 
3379 	return (isec);
3380 }
3381 
3382 /*
3383  * Build an additional text section - used to back FUNC symbol definitions
3384  * added with a mapfile.
3385  */
3386 Is_desc *
3387 ld_make_text(Ofl_desc *ofl, size_t size)
3388 {
3389 	Shdr		*shdr;
3390 	Elf_Data	*data;
3391 	Is_desc		*isec;
3392 
3393 	/*
3394 	 * Insure the size is sufficient to contain the minimum return
3395 	 * instruction.
3396 	 */
3397 	if (size < ld_targ.t_nf.nf_size)
3398 		size = ld_targ.t_nf.nf_size;
3399 
3400 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0,
3401 	    &isec, &shdr, &data) == S_ERROR)
3402 		return ((Is_desc *)S_ERROR);
3403 
3404 	data->d_size = size;
3405 	shdr->sh_size = (Xword)size;
3406 	shdr->sh_flags |= SHF_EXECINSTR;
3407 
3408 	/*
3409 	 * Fill the buffer with the appropriate return instruction.
3410 	 * Note that there is no need to swap bytes on a non-native,
3411 	 * link, as the data being copied is given in bytes.
3412 	 */
3413 	if ((data->d_buf = libld_calloc(size, 1)) == NULL)
3414 		return ((Is_desc *)S_ERROR);
3415 	(void) memcpy(data->d_buf, ld_targ.t_nf.nf_template,
3416 	    ld_targ.t_nf.nf_size);
3417 
3418 	/*
3419 	 * If size was larger than required, and the target supplies
3420 	 * a fill function, use it to fill the balance. If there is no
3421 	 * fill function, we accept the 0-fill supplied by libld_calloc().
3422 	 */
3423 	if ((ld_targ.t_ff.ff_execfill != NULL) && (size > ld_targ.t_nf.nf_size))
3424 		ld_targ.t_ff.ff_execfill(data->d_buf, ld_targ.t_nf.nf_size,
3425 		    size - ld_targ.t_nf.nf_size);
3426 
3427 	if (aplist_append(&ofl->ofl_maptext, isec, AL_CNT_OFL_MAPSECS) == NULL)
3428 		return ((Is_desc *)S_ERROR);
3429 
3430 	return (isec);
3431 }
3432