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