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