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