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