xref: /illumos-gate/usr/src/cmd/sgs/libld/common/sections.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
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 2007 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 #include	<string.h>
35 #include	<strings.h>
36 #include	<stdio.h>
37 #include	<link.h>
38 #include	<debug.h>
39 #include	"msg.h"
40 #include	"_libld.h"
41 
42 static void
43 remove_local(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
44 {
45 	Sym	*sym = sdp->sd_sym;
46 	uchar_t	type = ELF_ST_TYPE(sym->st_info);
47 	/* LINTED - only used for assert() */
48 	int	err;
49 
50 	if ((ofl->ofl_flags1 & FLG_OF1_REDLSYM) == 0) {
51 		ofl->ofl_locscnt--;
52 		err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
53 		assert(err != -1);
54 
55 		if (allow_ldynsym && ldynsym_symtype[type]) {
56 			ofl->ofl_dynlocscnt--;
57 			err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
58 			assert(err != -1);
59 			/* Remove from sort section? */
60 			DYNSORT_COUNT(sdp, sym, type, --);
61 		}
62 	}
63 	sdp->sd_flags |= FLG_SY_ISDISC;
64 }
65 
66 static void
67 remove_scoped(Ofl_desc *ofl, Sym_desc *sdp, int allow_ldynsym)
68 {
69 	Sym	*sym = sdp->sd_sym;
70 	uchar_t	type = ELF_ST_TYPE(sym->st_info);
71 	/* LINTED - only used for assert() */
72 	int	err;
73 
74 	ofl->ofl_scopecnt--;
75 	ofl->ofl_elimcnt++;
76 
77 	err = st_delstring(ofl->ofl_strtab, sdp->sd_name);
78 	assert(err != -1);
79 
80 	if (allow_ldynsym && ldynsym_symtype[type]) {
81 		ofl->ofl_dynscopecnt--;
82 		err = st_delstring(ofl->ofl_dynstrtab, sdp->sd_name);
83 		assert(err != -1);
84 		/* Remove from sort section? */
85 		DYNSORT_COUNT(sdp, sym, type, --);
86 	}
87 	sdp->sd_flags1 |= FLG_SY1_ELIM;
88 }
89 
90 #pragma inline(remove_local)
91 #pragma inline(remove_scoped)
92 
93 /*
94  * If -zignore is in effect, scan all input sections to see if there are any
95  * which haven't been referenced (and hence can be discarded).  If sections are
96  * to be discarded, rescan the output relocations and the symbol table and
97  * remove the relocations and symbol entries that are no longer required.
98  *
99  * Note:  It's possible that a section which is being discarded has contributed
100  *	  to the GOT table or the PLT table.  However, we can't at this point
101  *	  eliminate the corresponding entries.  This is because there could well
102  *	  be other sections referencing those same entries, but we don't have
103  *	  the infrastructure to determine this.  So, keep the PLT and GOT
104  *	  entries in the table in case someone wants them.
105  * Note:  The section to be affected needs to be allocatable.
106  *	  So even if -zignore is in effect, if the section is not allocatable,
107  *	  we do not eliminate it.
108  */
109 static uintptr_t
110 ignore_section_processing(Ofl_desc *ofl)
111 {
112 	Listnode	*lnp;
113 	Ifl_desc	*ifl;
114 	Rel_cache	*rcp;
115 	int		allow_ldynsym;
116 
117 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
118 
119 	for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, ifl)) {
120 		uint_t	num, discard;
121 
122 		/*
123 		 * Diagnose (-D unused) a completely unreferenced file.
124 		 */
125 		if ((ifl->ifl_flags & FLG_IF_FILEREF) == 0)
126 			DBG_CALL(Dbg_unused_file(ofl->ofl_lml,
127 			    ifl->ifl_name, 0, 0));
128 		if (((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0) ||
129 		    ((ifl->ifl_flags & FLG_IF_IGNORE) == 0))
130 			continue;
131 
132 		/*
133 		 * Before scanning the whole symbol table to determine if
134 		 * symbols should be discard - quickly (relatively) scan the
135 		 * sections to determine if any are to be discarded.
136 		 */
137 		discard = 0;
138 		if (ifl->ifl_flags & FLG_IF_FILEREF) {
139 			for (num = 1; num < ifl->ifl_shnum; num++) {
140 				Is_desc	*isp = ifl->ifl_isdesc[num];
141 				Os_desc *osp;
142 				Sg_desc	*sgp;
143 
144 				if (((isp = ifl->ifl_isdesc[num]) != 0) &&
145 				    ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
146 				    ((osp = isp->is_osdesc) != 0) &&
147 				    ((sgp = osp->os_sgdesc) != 0) &&
148 				    (sgp->sg_phdr.p_type == PT_LOAD)) {
149 					discard++;
150 					break;
151 				}
152 			}
153 		}
154 
155 		/*
156 		 * No sections are to be 'ignored'
157 		 */
158 		if ((discard == 0) && (ifl->ifl_flags & FLG_IF_FILEREF))
159 			continue;
160 
161 		/*
162 		 * We know that we have discarded sections.  Scan the symbol
163 		 * table for this file to determine if symbols need to be
164 		 * discarded that are associated with the 'ignored' sections.
165 		 */
166 		for (num = 1; num < ifl->ifl_symscnt; num++) {
167 			Sym_desc	*sdp;
168 			Sym		*symp;
169 			Os_desc		*osp;
170 			/* LINTED - only used for assert() */
171 			int		err;
172 			uchar_t		type;
173 
174 			sdp = ifl->ifl_oldndx[num];
175 			symp = sdp->sd_sym;
176 			type = ELF_ST_TYPE(symp->st_info);
177 
178 			/*
179 			 * If the whole file is being eliminated, remove the
180 			 * local file symbol, and any COMMON symbols (which
181 			 * aren't associated with a section) provided they
182 			 * haven't been referenced by a relocation.
183 			 */
184 			if ((ofl->ofl_flags1 & FLG_OF1_IGNORE) &&
185 			    ((ifl->ifl_flags & FLG_IF_FILEREF) == 0) &&
186 			    ((type == STT_FILE) ||
187 			    ((symp->st_shndx == SHN_COMMON) &&
188 			    ((sdp->sd_flags & FLG_SY_UPREQD) == 0)))) {
189 				remove_local(ofl, sdp, allow_ldynsym);
190 				continue;
191 			}
192 
193 			/*
194 			 * Skip any undefined, reserved section symbols, already
195 			 * discarded or eliminated symbols.  Also skip any
196 			 * symbols that don't originate from a section, or
197 			 * aren't defined from the file being examined.
198 			 */
199 			if ((symp->st_shndx == SHN_UNDEF) ||
200 			    (symp->st_shndx >= SHN_LORESERVE) ||
201 			    (type == STT_SECTION) ||
202 			    (sdp->sd_flags & FLG_SY_ISDISC) ||
203 			    (sdp->sd_flags1 & FLG_SY1_ELIM) ||
204 			    (sdp->sd_isc == 0) || (sdp->sd_file != ifl))
205 				continue;
206 
207 			/*
208 			 * If any references were made against the section
209 			 * the symbol is being defined in - skip it.
210 			 */
211 			if ((sdp->sd_isc->is_flags & FLG_IS_SECTREF) ||
212 			    ((ifl->ifl_flags & FLG_IF_FILEREF) &&
213 			    ((osp = sdp->sd_isc->is_osdesc) != 0) &&
214 			    (osp->os_sgdesc->sg_phdr.p_type != PT_LOAD)))
215 				continue;
216 
217 			/*
218 			 * Finish processing any local symbols.
219 			 */
220 			if (ELF_ST_BIND(symp->st_info) == STB_LOCAL) {
221 				if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
222 					remove_local(ofl, sdp, allow_ldynsym);
223 
224 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml,
225 				    sdp, sdp->sd_isc));
226 				continue;
227 			}
228 
229 			/*
230 			 * Global symbols can only be eliminated when an objects
231 			 * interfaces (versioning/scoping) is defined.
232 			 */
233 			if (sdp->sd_flags1 & FLG_SY1_HIDDEN) {
234 				if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
235 					remove_scoped(ofl, sdp, allow_ldynsym);
236 
237 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml,
238 				    sdp, sdp->sd_isc));
239 				continue;
240 			}
241 		}
242 	}
243 
244 	if ((ofl->ofl_flags1 & FLG_OF1_IGNPRC) == 0)
245 		return (1);
246 
247 	/*
248 	 * Scan all output relocations searching for those against discarded or
249 	 * ignored sections.  If one is found, decrement the total outrel count.
250 	 */
251 	for (LIST_TRAVERSE(&ofl->ofl_outrels, lnp, rcp)) {
252 		Rel_desc	*orsp;
253 		Os_desc		*relosp;
254 
255 		/* LINTED */
256 		for (orsp = (Rel_desc *)(rcp + 1);
257 		    orsp < rcp->rc_free; orsp++) {
258 			Is_desc		*_isdesc = orsp->rel_isdesc;
259 			uint_t		flags, entsize;
260 			Shdr		*shdr;
261 			Ifl_desc	*ifl;
262 
263 			if ((_isdesc == 0) ||
264 			    ((_isdesc->is_flags & (FLG_IS_SECTREF))) ||
265 			    ((ifl = _isdesc->is_file) == 0) ||
266 			    ((ifl->ifl_flags & FLG_IF_IGNORE) == 0) ||
267 			    ((shdr = _isdesc->is_shdr) == 0) ||
268 			    ((shdr->sh_flags & SHF_ALLOC) == 0))
269 				continue;
270 
271 			flags = orsp->rel_flags;
272 
273 			if (flags & (FLG_REL_GOT | FLG_REL_BSS |
274 			    FLG_REL_NOINFO | FLG_REL_PLT))
275 				continue;
276 
277 			relosp = orsp->rel_osdesc;
278 
279 			if (orsp->rel_flags & FLG_REL_RELA)
280 				entsize = sizeof (Rela);
281 			else
282 				entsize = sizeof (Rel);
283 
284 			assert(relosp->os_szoutrels > 0);
285 			relosp->os_szoutrels -= entsize;
286 
287 			if (!(flags & FLG_REL_PLT))
288 				ofl->ofl_reloccntsub++;
289 
290 			if (orsp->rel_rtype == M_R_RELATIVE)
291 				ofl->ofl_relocrelcnt--;
292 		}
293 	}
294 	return (1);
295 }
296 
297 /*
298  * Allocate Elf_Data, Shdr, and Is_desc structures for a new
299  * section.
300  *
301  * entry:
302  *	ofl - Output file descriptor
303  *	shtype - SHT_ type code for section.
304  *	shname - String giving the name for the new section.
305  *	entcnt - # of items contained in the data part of the new section.
306  *		This value is multiplied against the known element size
307  *		for the section type to determine the size of the data
308  *		area for the section. It is only meaningful in cases where
309  *		the section type has a non-zero element size. In other cases,
310  *		the caller must set the size fields in the *ret_data and
311  *		*ret_shdr structs manually.
312  *	ret_isec, ret_shdr, ret_data - Address of pointers to
313  *		receive address of newly allocated structs.
314  *
315  * exit:
316  *	On error, returns S_ERROR. On success, returns (1), and the
317  *	ret_ pointers have been updated to point at the new structures,
318  *	which has been filled in. To finish the task, the caller must
319  *	update any fields within the supplied descriptors that differ
320  *	from its needs, and then call ld_place_section().
321  */
322 static uintptr_t
323 new_section(Ofl_desc *ofl, Word shtype, const char *shname, Xword entcnt,
324 	Is_desc **ret_isec, Shdr **ret_shdr, Elf_Data **ret_data)
325 {
326 	typedef struct sec_info {
327 		Word d_type;
328 		Word align;	/* Used in both data and section header */
329 		Word sh_flags;
330 		Word sh_entsize;
331 	} SEC_INFO_T;
332 
333 	const SEC_INFO_T	*sec_info;
334 
335 	Shdr		*shdr;
336 	Elf_Data	*data;
337 	Is_desc		*isec;
338 	size_t		size;
339 
340 	/*
341 	 * For each type of section, we have a distinct set of
342 	 * SEC_INFO_T values. This macro defines a static structure
343 	 * containing those values and generates code to set the sec_info
344 	 * pointer to refer to it. The pointer in sec_info remains valid
345 	 * outside of the declaration scope because the info_s struct is static.
346 	 */
347 #define	SET_SEC_INFO(d_type, d_align, sh_flags, sh_entsize) \
348 	{ \
349 		static const SEC_INFO_T info_s = { d_type, d_align, sh_flags, \
350 		    sh_entsize}; \
351 		sec_info = &info_s; \
352 	}
353 
354 	switch (shtype) {
355 	case SHT_PROGBITS:
356 		/*
357 		 * SHT_PROGBITS sections contain are used for many
358 		 * different sections. Alignments and flags differ.
359 		 * Some have a standard entsize, and others don't.
360 		 * We set some defaults here, but there is no expectation
361 		 * that they are correct or complete for any specific
362 		 * purpose. The caller must provide the correct values.
363 		 */
364 		SET_SEC_INFO(ELF_T_BYTE, M_WORD_ALIGN, SHF_ALLOC, 0)
365 		break;
366 
367 	case SHT_SYMTAB:
368 		SET_SEC_INFO(ELF_T_SYM, M_WORD_ALIGN, 0, sizeof (Sym))
369 		break;
370 
371 	case SHT_DYNSYM:
372 	case SHT_SUNW_LDYNSYM:
373 		SET_SEC_INFO(ELF_T_SYM, M_WORD_ALIGN, SHF_ALLOC, sizeof (Sym))
374 		break;
375 
376 	case SHT_STRTAB:
377 		/*
378 		 * A string table may or may not be allocable, depending
379 		 * on context, so we leave that flag unset and leave it to
380 		 * the caller to add it if necessary.
381 		 *
382 		 * String tables do not have a standard entsize, so
383 		 * we set it to 0.
384 		 */
385 		SET_SEC_INFO(ELF_T_BYTE, 1, SHF_STRINGS, 0)
386 		break;
387 
388 	case SHT_RELA:
389 		/*
390 		 * Relocations with an addend (Everything except 32-bit X86).
391 		 * The caller is expected to set all section header flags.
392 		 */
393 		SET_SEC_INFO(ELF_T_RELA, M_WORD_ALIGN, 0, sizeof (Rela))
394 		break;
395 
396 	case SHT_REL:
397 		/*
398 		 * Relocations without an addend (32-bit X86 only).
399 		 * The caller is expected to set all section header flags.
400 		 */
401 		SET_SEC_INFO(ELF_T_REL, M_WORD_ALIGN, 0, sizeof (Rel))
402 		break;
403 
404 	case SHT_HASH:
405 	case SHT_SUNW_symsort:
406 	case SHT_SUNW_tlssort:
407 		SET_SEC_INFO(ELF_T_WORD, M_WORD_ALIGN, SHF_ALLOC, sizeof (Word))
408 		break;
409 
410 	case SHT_DYNAMIC:
411 		/*
412 		 * A dynamic section may or may not be allocable, depending
413 		 * on context, so we leave that flag unset and leave it to
414 		 * the caller to add it if necessary.
415 		 */
416 		SET_SEC_INFO(ELF_T_DYN, M_WORD_ALIGN, SHF_WRITE, sizeof (Dyn))
417 		break;
418 
419 	case SHT_NOBITS:
420 		/*
421 		 * SHT_NOBITS is used for BSS-type sections. The size and
422 		 * alignment depend on the specific use and must be adjusted
423 		 * by the caller.
424 		 */
425 		SET_SEC_INFO(ELF_T_BYTE, 0, SHF_ALLOC | SHF_WRITE, 0)
426 		break;
427 
428 	case SHT_INIT_ARRAY:
429 	case SHT_FINI_ARRAY:
430 	case SHT_PREINIT_ARRAY:
431 		SET_SEC_INFO(ELF_T_ADDR, sizeof (Addr), SHF_ALLOC | SHF_WRITE,
432 		    sizeof (Addr))
433 		break;
434 
435 	case SHT_SYMTAB_SHNDX:
436 		/*
437 		 * Note that these sections are created to be associated
438 		 * with both symtab and dynsym symbol tables. However, they
439 		 * are non-allocable in all cases, because the runtime
440 		 * linker has no need for this information. It is purely
441 		 * informational, used by elfdump(1), debuggers, etc.
442 		 */
443 		SET_SEC_INFO(ELF_T_WORD, M_WORD_ALIGN, 0, sizeof (Word));
444 		break;
445 
446 	case SHT_SUNW_cap:
447 		SET_SEC_INFO(ELF_T_CAP, M_WORD_ALIGN, SHF_ALLOC, sizeof (Cap));
448 		break;
449 
450 	case SHT_SUNW_move:
451 		/*
452 		 * The sh_info field of the SHT_*_syminfo section points
453 		 * to the header index of the associated .dynamic section,
454 		 * so we also set SHF_INFO_LINK.
455 		 */
456 		SET_SEC_INFO(ELF_T_BYTE, sizeof (Lword),
457 		    SHF_ALLOC | SHF_WRITE, sizeof (Move));
458 		break;
459 
460 	case SHT_SUNW_syminfo:
461 		/*
462 		 * The sh_info field of the SHT_*_syminfo section points
463 		 * to the header index of the associated .dynamic section,
464 		 * so we also set SHF_INFO_LINK.
465 		 */
466 		SET_SEC_INFO(ELF_T_BYTE, M_WORD_ALIGN,
467 		    SHF_ALLOC | SHF_INFO_LINK, sizeof (Syminfo));
468 		break;
469 
470 	case SHT_SUNW_verneed:
471 	case SHT_SUNW_verdef:
472 		/*
473 		 * The info for verneed and versym happen to be the same.
474 		 * The entries in these sections are not of uniform size,
475 		 * so we set the entsize to 0.
476 		 */
477 		SET_SEC_INFO(ELF_T_BYTE, M_WORD_ALIGN, SHF_ALLOC, 0);
478 		break;
479 
480 	case SHT_SUNW_versym:
481 		SET_SEC_INFO(ELF_T_BYTE, M_WORD_ALIGN, SHF_ALLOC,
482 		    sizeof (Versym));
483 		break;
484 
485 	default:
486 		/* Should not happen: fcn called with unknown section type */
487 		assert(0);
488 		return (S_ERROR);
489 	}
490 #undef	SET_SEC_INFO
491 
492 	size = entcnt * sec_info->sh_entsize;
493 
494 	/*
495 	 * Allocate and initialize the Elf_Data structure.
496 	 */
497 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
498 		return (S_ERROR);
499 	data->d_type = sec_info->d_type;
500 	data->d_size = size;
501 	data->d_align = sec_info->align;
502 	data->d_version = ofl->ofl_dehdr->e_version;
503 
504 	/*
505 	 * Allocate and initialize the Shdr structure.
506 	 */
507 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
508 		return (S_ERROR);
509 	shdr->sh_type = shtype;
510 	shdr->sh_size = size;
511 	shdr->sh_flags = sec_info->sh_flags;
512 	shdr->sh_addralign = sec_info->align;
513 	shdr->sh_entsize = sec_info->sh_entsize;
514 
515 	/*
516 	 * Allocate and initialize the Is_desc structure.
517 	 */
518 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
519 		return (S_ERROR);
520 	isec->is_name = shname;
521 	isec->is_shdr = shdr;
522 	isec->is_indata = data;
523 
524 
525 	*ret_isec = isec;
526 	*ret_shdr = shdr;
527 	*ret_data = data;
528 	return (1);
529 }
530 
531 /*
532  * Build a .bss section for allocation of tentative definitions.  Any `static'
533  * .bss definitions would have been associated to their own .bss sections and
534  * thus collected from the input files.  `global' .bss definitions are tagged
535  * as COMMON and do not cause any associated .bss section elements to be
536  * generated.  Here we add up all these COMMON symbols and generate the .bss
537  * section required to represent them.
538  */
539 uintptr_t
540 ld_make_bss(Ofl_desc *ofl, Xword size, Xword align, Bss_Type which)
541 {
542 	Shdr		*shdr;
543 	Elf_Data	*data;
544 	Is_desc		*isec;
545 	Os_desc		*osp;
546 	uint_t		ident;
547 	Xword		rsize = (Xword)ofl->ofl_relocbsssz;
548 
549 	/*
550 	 * Allocate header structs. We will set the name ourselves below,
551 	 * and there is no entcnt for a BSS. So, the shname and entcnt
552 	 * arguments are 0.
553 	 */
554 	if (new_section(ofl, SHT_NOBITS, NULL, 0,
555 	    &isec, &shdr, &data) == S_ERROR)
556 		return (S_ERROR);
557 
558 	data->d_size = (size_t)size;
559 	data->d_align = (size_t)align;
560 
561 	shdr->sh_size = size;
562 	shdr->sh_addralign = align;
563 
564 	if (which == MAKE_TLS) {
565 		isec->is_name = MSG_ORIG(MSG_SCN_TBSS);
566 		ident = M_ID_TLSBSS;
567 		ofl->ofl_istlsbss = isec;
568 		shdr->sh_flags |= SHF_TLS;
569 
570 	} else if (which == MAKE_BSS) {
571 		isec->is_name = MSG_ORIG(MSG_SCN_BSS);
572 		ofl->ofl_isbss = isec;
573 		ident = M_ID_BSS;
574 
575 #if	defined(__x86) && defined(_ELF64)
576 	} else if (which == MAKE_LBSS) {
577 		isec->is_name = MSG_ORIG(MSG_SCN_LBSS);
578 		ofl->ofl_islbss = isec;
579 		ident = M_ID_LBSS;
580 		shdr->sh_flags |= SHF_AMD64_LARGE;
581 #endif
582 	}
583 
584 	/*
585 	 * Retain this .bss input section as this will be where global
586 	 * symbol references are added.
587 	 */
588 	if ((osp = ld_place_section(ofl, isec, ident, 0)) == (Os_desc *)S_ERROR)
589 		return (S_ERROR);
590 
591 	/*
592 	 * If relocations exist against .*bss section, a
593 	 * section symbol must be created for the section in
594 	 * the .dynsym symbol table.
595 	 */
596 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
597 		Word	flagtotest;
598 		if (which == MAKE_TLS)
599 			flagtotest = FLG_OF1_TLSOREL;
600 		else
601 			flagtotest = FLG_OF1_BSSOREL;
602 
603 		if (ofl->ofl_flags1 & flagtotest) {
604 			ofl->ofl_dynshdrcnt++;
605 			osp->os_flags |= FLG_OS_OUTREL;
606 		}
607 	}
608 
609 	osp->os_szoutrels = rsize;
610 
611 	return (1);
612 }
613 
614 
615 /*
616  * Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via
617  * ld -z *array=name
618  */
619 static uintptr_t
620 make_array(Ofl_desc *ofl, Word shtype, const char *sectname, List *list)
621 {
622 	uint_t		entcount;
623 	Listnode	*lnp;
624 	Elf_Data	*data;
625 	Is_desc		*isec;
626 	Shdr		*shdr;
627 	Sym_desc	*sdp;
628 	Rel_desc	reld;
629 	Rela		reloc;
630 	Os_desc		*osp;
631 	uintptr_t	ret = 1;
632 
633 	if (list->head == NULL)
634 		return (1);
635 
636 	entcount = 0;
637 	for (LIST_TRAVERSE(list, lnp, sdp))
638 		entcount++;
639 
640 	if (new_section(ofl, shtype, sectname, entcount, &isec, &shdr, &data) ==
641 	    S_ERROR)
642 		return (S_ERROR);
643 
644 	if ((data->d_buf = libld_calloc(sizeof (Addr), entcount)) == 0)
645 		return (S_ERROR);
646 
647 	if (ld_place_section(ofl, isec, M_ID_ARRAY, 0) == (Os_desc *)S_ERROR)
648 		return (S_ERROR);
649 
650 	osp = isec->is_osdesc;
651 
652 	if ((ofl->ofl_osinitarray == 0) && (shtype == SHT_INIT_ARRAY))
653 		ofl->ofl_osinitarray = osp;
654 	if ((ofl->ofl_ospreinitarray == 0) && (shtype == SHT_PREINIT_ARRAY))
655 		ofl->ofl_ospreinitarray = osp;
656 	else if ((ofl->ofl_osfiniarray == 0) && (shtype == SHT_FINI_ARRAY))
657 		ofl->ofl_osfiniarray = osp;
658 
659 	/*
660 	 * Create relocations against this section to initialize it to the
661 	 * function addresses.
662 	 */
663 	reld.rel_osdesc = osp;
664 	reld.rel_isdesc = isec;
665 	reld.rel_move = 0;
666 	reld.rel_flags = FLG_REL_LOAD;
667 
668 	/*
669 	 * Fabricate the relocation information (as if a relocation record had
670 	 * been input - see init_rel()).
671 	 */
672 	reld.rel_rtype = M_R_ARRAYADDR;
673 	reld.rel_roffset = 0;
674 	reld.rel_raddend = 0;
675 	reld.rel_typedata = 0;
676 
677 	/*
678 	 * Create a minimal relocation record to satisfy process_sym_reloc()
679 	 * debugging requirements.
680 	 */
681 	reloc.r_offset = 0;
682 	reloc.r_info = ELF_R_INFO(0, M_R_ARRAYADDR);
683 	reloc.r_addend = 0;
684 
685 	DBG_CALL(Dbg_reloc_generate(ofl->ofl_lml, osp, M_REL_SHT_TYPE));
686 	for (LIST_TRAVERSE(list, lnp, sdp)) {
687 		reld.rel_sname = sdp->sd_name;
688 		reld.rel_sym = sdp;
689 
690 		if (ld_process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec,
691 		    MSG_INTL(MSG_STR_COMMAND)) == S_ERROR) {
692 			ret = S_ERROR;
693 			continue;
694 		}
695 
696 		reld.rel_roffset += (Xword)sizeof (Addr);
697 		reloc.r_offset = reld.rel_roffset;
698 	}
699 
700 	return (ret);
701 }
702 
703 /*
704  * Build a comment section (-Qy option).
705  */
706 static uintptr_t
707 make_comment(Ofl_desc *ofl)
708 {
709 	Shdr		*shdr;
710 	Elf_Data	*data;
711 	Is_desc		*isec;
712 
713 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_COMMENT), 0,
714 	    &isec, &shdr, &data) == S_ERROR)
715 		return (S_ERROR);
716 
717 	data->d_buf = (void *)ofl->ofl_sgsid;
718 	data->d_size = strlen(ofl->ofl_sgsid) + 1;
719 	data->d_align = 1;
720 
721 	shdr->sh_size = (Xword)data->d_size;
722 	shdr->sh_flags = 0;
723 	shdr->sh_addralign = 1;
724 
725 	return ((uintptr_t)ld_place_section(ofl, isec, M_ID_NOTE, 0));
726 }
727 
728 /*
729  * Make the dynamic section.  Calculate the size of any strings referenced
730  * within this structure, they will be added to the global string table
731  * (.dynstr).  This routine should be called before make_dynstr().
732  */
733 static uintptr_t
734 make_dynamic(Ofl_desc *ofl)
735 {
736 	Shdr		*shdr;
737 	Os_desc		*osp;
738 	Elf_Data	*data;
739 	Is_desc		*isec;
740 	size_t		cnt = 0;
741 	Listnode	*lnp;
742 	Ifl_desc	*ifl;
743 	Sym_desc	*sdp;
744 	size_t		size;
745 	Word		flags = ofl->ofl_flags;
746 	int		unused = 0;
747 
748 	if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0,
749 	    &isec, &shdr, &data) == S_ERROR)
750 		return (S_ERROR);
751 
752 	/* new_section() does not set SHF_ALLOC. Add it if needed */
753 	if (!(flags & FLG_OF_RELOBJ))
754 		shdr->sh_flags |= SHF_ALLOC;
755 
756 	osp = ofl->ofl_osdynamic = ld_place_section(ofl, isec, M_ID_DYNAMIC, 0);
757 
758 	/*
759 	 * Reserve entries for any needed dependencies.
760 	 */
761 	for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, ifl)) {
762 		Sdf_desc *	sdf;
763 
764 		if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR)))
765 			continue;
766 
767 		/*
768 		 * If this dependency didn't satisfy any symbol references,
769 		 * generate a debugging diagnostic (ld(1) -Dunused can be used
770 		 * to display these).  If this is a standard needed dependency,
771 		 * and -z ignore is in effect, drop the dependency.  Explicitly
772 		 * defined dependencies (i.e., -N dep) don't get dropped, and
773 		 * are flagged as being required to simplify update_odynamic()
774 		 * processing.
775 		 */
776 		if ((ifl->ifl_flags & FLG_IF_NEEDSTR) ||
777 		    ((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) {
778 			if (unused++ == 0)
779 				DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
780 			DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname,
781 			    (ifl->ifl_flags & FLG_IF_NEEDSTR), 0));
782 
783 			if (ifl->ifl_flags & FLG_IF_NEEDSTR)
784 				ifl->ifl_flags |= FLG_IF_DEPREQD;
785 			else if (ifl->ifl_flags & FLG_IF_IGNORE)
786 				continue;
787 		}
788 
789 		/*
790 		 * If this object has an accompanying shared object definition
791 		 * determine if an alternative shared object name has been
792 		 * specified.
793 		 */
794 		if (((sdf = ifl->ifl_sdfdesc) != 0) &&
795 		    (sdf->sdf_flags & FLG_SDF_SONAME))
796 			ifl->ifl_soname = sdf->sdf_soname;
797 
798 		/*
799 		 * If this object is a lazyload reserve a DT_POSFLAG1 entry.
800 		 */
801 		if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_GRPPRM))
802 			cnt++;
803 
804 		if (st_insert(ofl->ofl_dynstrtab, ifl->ifl_soname) == -1)
805 			return (S_ERROR);
806 		cnt++;
807 
808 		/*
809 		 * If the needed entry contains the $ORIGIN token make sure
810 		 * the associated DT_1_FLAGS entry is created.
811 		 */
812 		if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) {
813 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
814 			ofl->ofl_dtflags |= DF_ORIGIN;
815 		}
816 	}
817 
818 	if (unused)
819 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
820 
821 	/*
822 	 * Reserve entries for any per-symbol auxiliary/filter strings.
823 	 */
824 	if (ofl->ofl_dtsfltrs) {
825 		/* LINTED */
826 		Dfltr_desc *	dftp;
827 		Aliste		off;
828 
829 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp))
830 			cnt++;
831 	}
832 
833 	/*
834 	 * Reserve entries for any _init() and _fini() section addresses.
835 	 */
836 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
837 	    SYM_NOHASH, 0, ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) &&
838 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
839 		sdp->sd_flags |= FLG_SY_UPREQD;
840 		cnt++;
841 	}
842 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
843 	    SYM_NOHASH, 0, ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) &&
844 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
845 		sdp->sd_flags |= FLG_SY_UPREQD;
846 		cnt++;
847 	}
848 
849 	/*
850 	 * Reserve entries for any soname, filter name (shared libs only),
851 	 * run-path pointers, cache names and audit requirements..
852 	 */
853 	if (ofl->ofl_soname) {
854 		cnt++;
855 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_soname) == -1)
856 			return (S_ERROR);
857 	}
858 	if (ofl->ofl_filtees) {
859 		cnt++;
860 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_filtees) == -1)
861 			return (S_ERROR);
862 
863 		/*
864 		 * If the filtees entry contains the $ORIGIN token make sure
865 		 * the associated DT_1_FLAGS entry is created.
866 		 */
867 		if (strstr(ofl->ofl_filtees, MSG_ORIG(MSG_STR_ORIGIN))) {
868 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
869 			ofl->ofl_dtflags |= DF_ORIGIN;
870 		}
871 	}
872 	if (ofl->ofl_rpath) {
873 		cnt += 2;	/* DT_RPATH & DT_RUNPATH */
874 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_rpath) == -1)
875 			return (S_ERROR);
876 
877 		/*
878 		 * If the rpath entry contains the $ORIGIN token make sure
879 		 * the associated DT_1_FLAGS entry is created.
880 		 */
881 		if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) {
882 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
883 			ofl->ofl_dtflags |= DF_ORIGIN;
884 		}
885 	}
886 	if (ofl->ofl_config) {
887 		cnt++;
888 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_config) == -1)
889 			return (S_ERROR);
890 
891 		/*
892 		 * If the config entry contains the $ORIGIN token make sure
893 		 * the associated DT_1_FLAGS entry is created.
894 		 */
895 		if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) {
896 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
897 			ofl->ofl_dtflags |= DF_ORIGIN;
898 		}
899 	}
900 	if (ofl->ofl_depaudit) {
901 		cnt++;
902 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_depaudit) == -1)
903 			return (S_ERROR);
904 	}
905 	if (ofl->ofl_audit) {
906 		cnt++;
907 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_audit) == -1)
908 			return (S_ERROR);
909 	}
910 
911 
912 	/*
913 	 * The following DT_* entries do not apply to relocatable objects
914 	 */
915 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
916 		/*
917 		 * Reserve entries for the HASH, STRTAB, STRSZ, SYMTAB, SYMENT,
918 		 * and CHECKSUM.
919 		 */
920 		cnt += 6;
921 
922 		/*
923 		 * If we are including local functions at the head of
924 		 * the dynsym, then also reserve entries for DT_SUNW_SYMTAB
925 		 * and DT_SUNW_SYMSZ.
926 		 */
927 		if (OFL_ALLOW_LDYNSYM(ofl))
928 			cnt += 2;
929 
930 		if ((ofl->ofl_dynsymsortcnt > 0) ||
931 		    (ofl->ofl_dyntlssortcnt > 0))
932 			cnt++;		/* DT_SUNW_SORTENT */
933 
934 		if (ofl->ofl_dynsymsortcnt > 0)
935 			cnt += 2;	/* DT_SUNW_[SYMSORT|SYMSORTSZ] */
936 
937 		if (ofl->ofl_dyntlssortcnt > 0)
938 			cnt += 2;	/* DT_SUNW_[TLSSORT|TLSSORTSZ] */
939 
940 		if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
941 		    FLG_OF_VERDEF)
942 			cnt += 2;		/* DT_VERDEF & DT_VERDEFNUM */
943 
944 		if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
945 		    FLG_OF_VERNEED)
946 			cnt += 2;		/* DT_VERNEED & DT_VERNEEDNUM */
947 
948 		if ((ofl->ofl_flags1 & FLG_OF1_RELCNT) &&
949 		    ofl->ofl_relocrelcnt)	/* RELACOUNT */
950 			cnt++;
951 
952 		if (flags & FLG_OF_TEXTREL)	/* TEXTREL */
953 			cnt++;
954 
955 		if (ofl->ofl_osfiniarray)	/* FINI_ARRAY & FINI_ARRAYSZ */
956 			cnt += 2;
957 
958 		if (ofl->ofl_osinitarray)	/* INIT_ARRAY & INIT_ARRAYSZ */
959 			cnt += 2;
960 
961 		if (ofl->ofl_ospreinitarray)	/* PREINIT_ARRAY & */
962 			cnt += 2;		/*	PREINIT_ARRAYSZ */
963 
964 		/*
965 		 * If we have plt's reserve a PLT, PLTSZ, PLTREL and JMPREL.
966 		 */
967 		if (ofl->ofl_pltcnt)
968 			cnt += 3;
969 
970 		/*
971 		 * If pltpadding is needed (Sparcv9)
972 		 */
973 		if (ofl->ofl_pltpad)
974 			cnt += 2;		/* DT_PLTPAD & DT_PLTPADSZ */
975 
976 		/*
977 		 * If we have any relocations reserve a REL, RELSZ and
978 		 * RELENT entry.
979 		 */
980 		if (ofl->ofl_relocsz)
981 			cnt += 3;
982 
983 		/*
984 		 * If a syminfo section is required create SYMINFO, SYMINSZ,
985 		 * and SYMINENT entries.
986 		 */
987 		if (ofl->ofl_flags & FLG_OF_SYMINFO)
988 			cnt += 3;
989 
990 		/*
991 		 * If there are any partially initialized sections allocate
992 		 * MOVEENT, MOVESZ and MOVETAB.
993 		 */
994 		if (ofl->ofl_osmove)
995 			cnt += 3;
996 
997 		/*
998 		 * Allocate one DT_REGISTER entry for every register symbol.
999 		 */
1000 		cnt += ofl->ofl_regsymcnt;
1001 
1002 		/*
1003 		 * Reserve a entry for each '-zrtldinfo=...' specified
1004 		 * on the command line.
1005 		 */
1006 		for (LIST_TRAVERSE(&ofl->ofl_rtldinfo, lnp, sdp))
1007 			cnt++;
1008 
1009 		/*
1010 		 * These two entries should only be placed in a segment
1011 		 * which is writable.  If it's a read-only segment
1012 		 * (due to mapfile magic, e.g. libdl.so.1) then don't allocate
1013 		 * these entries.
1014 		 */
1015 		if ((osp->os_sgdesc) &&
1016 		    (osp->os_sgdesc->sg_phdr.p_flags & PF_W)) {
1017 			cnt++;			/* FEATURE_1 */
1018 
1019 			if (ofl->ofl_osinterp)
1020 				cnt++;		/* DEBUG */
1021 		}
1022 
1023 		/*
1024 		 * Any hardware/software capabilities?
1025 		 */
1026 		if (ofl->ofl_oscap)
1027 			cnt++;			/* SUNW_CAP */
1028 	}
1029 
1030 	if (flags & FLG_OF_SYMBOLIC)
1031 		cnt++;				/* SYMBOLIC */
1032 
1033 	/*
1034 	 * Account for Architecture dependent .dynamic entries, and defaults.
1035 	 */
1036 	ld_mach_make_dynamic(ofl, &cnt);
1037 
1038 	/*
1039 	 * DT_FLAGS, DT_FLAGS_1, DT_SUNW_STRPAD, and DT_NULL. Also,
1040 	 * allow room for the unused extra DT_NULLs. These are included
1041 	 * to allow an ELF editor room to add items later.
1042 	 */
1043 	cnt += 4 + DYNAMIC_EXTRA_ELTS;
1044 
1045 	/*
1046 	 * Determine the size of the section from the number of entries.
1047 	 */
1048 	size = cnt * (size_t)shdr->sh_entsize;
1049 
1050 	shdr->sh_size = (Xword)size;
1051 	data->d_size = size;
1052 
1053 	return ((uintptr_t)ofl->ofl_osdynamic);
1054 }
1055 
1056 /*
1057  * Build the GOT section and its associated relocation entries.
1058  */
1059 uintptr_t
1060 ld_make_got(Ofl_desc *ofl)
1061 {
1062 	Shdr		*shdr;
1063 	Elf_Data	*data;
1064 	Is_desc		*isec;
1065 	size_t		size = (size_t)ofl->ofl_gotcnt * M_GOT_ENTSIZE;
1066 	size_t		rsize = (size_t)ofl->ofl_relocgotsz;
1067 
1068 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0,
1069 	    &isec, &shdr, &data) == S_ERROR)
1070 		return (S_ERROR);
1071 
1072 	data->d_size = size;
1073 
1074 	shdr->sh_flags |= SHF_WRITE;
1075 	shdr->sh_size = (Xword)size;
1076 	shdr->sh_entsize = M_GOT_ENTSIZE;
1077 
1078 	if ((ofl->ofl_osgot = ld_place_section(ofl, isec, M_ID_GOT, 0)) ==
1079 	    (Os_desc *)S_ERROR)
1080 		return (S_ERROR);
1081 
1082 	ofl->ofl_osgot->os_szoutrels = (Xword)rsize;
1083 
1084 	return (1);
1085 }
1086 
1087 /*
1088  * Build an interpreter section.
1089  */
1090 static uintptr_t
1091 make_interp(Ofl_desc *ofl)
1092 {
1093 	Shdr		*shdr;
1094 	Elf_Data	*data;
1095 	Is_desc		*isec;
1096 	const char	*iname = ofl->ofl_interp;
1097 	size_t		size;
1098 
1099 	/*
1100 	 * If -z nointerp is in effect, don't create an interpreter section.
1101 	 */
1102 	if (ofl->ofl_flags1 & FLG_OF1_NOINTRP)
1103 		return (1);
1104 
1105 	/*
1106 	 * We always build an .interp section for dynamic executables.  However
1107 	 * if the user has specifically specified an interpreter we'll build
1108 	 * this section for any output (presumably the user knows what they are
1109 	 * doing. refer ABI section 5-4, and ld.1 man page use of -I).
1110 	 */
1111 	if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC |
1112 	    FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname)
1113 		return (1);
1114 
1115 	/*
1116 	 * In the case of a dynamic executable supply a default interpreter
1117 	 * if a specific interpreter has not been specified.
1118 	 */
1119 	if (iname == 0) {
1120 		if (ofl->ofl_dehdr->e_machine == EM_SPARCV9)
1121 			iname = ofl->ofl_interp =
1122 			    MSG_ORIG(MSG_PTH_RTLD_SPARCV9);
1123 		else if (ofl->ofl_dehdr->e_machine == EM_AMD64)
1124 			iname = ofl->ofl_interp =
1125 			    MSG_ORIG(MSG_PTH_RTLD_AMD64);
1126 		else
1127 			iname = ofl->ofl_interp = MSG_ORIG(MSG_PTH_RTLD);
1128 	}
1129 
1130 	size = strlen(iname) + 1;
1131 
1132 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0,
1133 	    &isec, &shdr, &data) == S_ERROR)
1134 		return (S_ERROR);
1135 
1136 	data->d_size = size;
1137 	shdr->sh_size = (Xword)size;
1138 	data->d_align = shdr->sh_addralign = 1;
1139 
1140 	ofl->ofl_osinterp = ld_place_section(ofl, isec, M_ID_INTERP, 0);
1141 	return ((uintptr_t)ofl->ofl_osinterp);
1142 }
1143 
1144 /*
1145  * Build a hardware/software capabilities section.
1146  */
1147 static uintptr_t
1148 make_cap(Ofl_desc *ofl)
1149 {
1150 	Shdr		*shdr;
1151 	Elf_Data	*data;
1152 	Is_desc		*isec;
1153 	Os_desc		*osec;
1154 	Cap		*cap;
1155 	size_t		size = 0;
1156 
1157 	/*
1158 	 * Determine how many entries are required.
1159 	 */
1160 	if (ofl->ofl_hwcap_1)
1161 		size++;
1162 	if (ofl->ofl_sfcap_1)
1163 		size++;
1164 	if (size == 0)
1165 		return (1);
1166 	size++;				/* Add CA_SUNW_NULL */
1167 
1168 	if (new_section(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP), size,
1169 	    &isec, &shdr, &data) == S_ERROR)
1170 		return (S_ERROR);
1171 
1172 	if ((data->d_buf = libld_malloc(shdr->sh_size)) == 0)
1173 		return (S_ERROR);
1174 
1175 	cap = (Cap *)data->d_buf;
1176 	if (ofl->ofl_hwcap_1) {
1177 		cap->c_tag = CA_SUNW_HW_1;
1178 		cap->c_un.c_val = ofl->ofl_hwcap_1;
1179 		cap++;
1180 	}
1181 	if (ofl->ofl_sfcap_1) {
1182 		cap->c_tag = CA_SUNW_SF_1;
1183 		cap->c_un.c_val = ofl->ofl_sfcap_1;
1184 		cap++;
1185 	}
1186 	cap->c_tag = CA_SUNW_NULL;
1187 	cap->c_un.c_val = 0;
1188 
1189 	/*
1190 	 * If we're not creating a relocatable object, save the output section
1191 	 * to trigger the creation of an associated program header.
1192 	 */
1193 	osec = ld_place_section(ofl, isec, M_ID_CAP, 0);
1194 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
1195 		ofl->ofl_oscap = osec;
1196 
1197 	return ((uintptr_t)osec);
1198 }
1199 
1200 /*
1201  * Build the PLT section and its associated relocation entries.
1202  */
1203 static uintptr_t
1204 make_plt(Ofl_desc *ofl)
1205 {
1206 	Shdr		*shdr;
1207 	Elf_Data	*data;
1208 	Is_desc		*isec;
1209 	size_t		size = (size_t)M_PLT_RESERVSZ +
1210 	    (((size_t)ofl->ofl_pltcnt + (size_t)ofl->ofl_pltpad) *
1211 	    M_PLT_ENTSIZE);
1212 	size_t		rsize = (size_t)ofl->ofl_relocpltsz;
1213 
1214 #if	defined(__sparc)
1215 	/*
1216 	 * Account for the NOP at the end of the plt.
1217 	 */
1218 	size += sizeof (Word);
1219 #endif
1220 
1221 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0,
1222 	    &isec, &shdr, &data) == S_ERROR)
1223 		return (S_ERROR);
1224 
1225 	data->d_size = size;
1226 	data->d_align = M_PLT_ALIGN;
1227 
1228 	shdr->sh_flags = M_PLT_SHF_FLAGS;
1229 	shdr->sh_size = (Xword)size;
1230 	shdr->sh_addralign = M_PLT_ALIGN;
1231 	shdr->sh_entsize = M_PLT_ENTSIZE;
1232 
1233 	if ((ofl->ofl_osplt = ld_place_section(ofl, isec, M_ID_PLT, 0)) ==
1234 	    (Os_desc *)S_ERROR)
1235 		return (S_ERROR);
1236 
1237 	ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1238 
1239 	return (1);
1240 }
1241 
1242 /*
1243  * Make the hash table.  Only built for dynamic executables and shared
1244  * libraries, and provides hashed lookup into the global symbol table
1245  * (.dynsym) for the run-time linker to resolve symbol lookups.
1246  */
1247 static uintptr_t
1248 make_hash(Ofl_desc *ofl)
1249 {
1250 	Shdr		*shdr;
1251 	Elf_Data	*data;
1252 	Is_desc		*isec;
1253 	size_t		size;
1254 	Word		nsyms = ofl->ofl_globcnt;
1255 	size_t		cnt;
1256 
1257 	/*
1258 	 * Allocate section header structures. We set entcnt to 0
1259 	 * because it's going to change after we place this section.
1260 	 */
1261 	if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0,
1262 	    &isec, &shdr, &data) == S_ERROR)
1263 		return (S_ERROR);
1264 
1265 	/*
1266 	 * Place the section first since it will affect the local symbol
1267 	 * count.
1268 	 */
1269 	if ((ofl->ofl_oshash = ld_place_section(ofl, isec, M_ID_HASH, 0)) ==
1270 	    (Os_desc *)S_ERROR)
1271 		return (S_ERROR);
1272 
1273 	/*
1274 	 * Calculate the number of output hash buckets.
1275 	 */
1276 	ofl->ofl_hashbkts = findprime(nsyms);
1277 
1278 	/*
1279 	 * The size of the hash table is determined by
1280 	 *
1281 	 *	i.	the initial nbucket and nchain entries (2)
1282 	 *	ii.	the number of buckets (calculated above)
1283 	 *	iii.	the number of chains (this is based on the number of
1284 	 *		symbols in the .dynsym array + NULL symbol).
1285 	 */
1286 	cnt = 2 + ofl->ofl_hashbkts + (ofl->ofl_dynshdrcnt +
1287 	    ofl->ofl_globcnt + ofl->ofl_lregsymcnt + 1);
1288 	size = cnt * shdr->sh_entsize;
1289 
1290 	/*
1291 	 * Finalize the section header and data buffer initialization.
1292 	 */
1293 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
1294 		return (S_ERROR);
1295 	data->d_size = size;
1296 	shdr->sh_size = (Xword)size;
1297 
1298 	return (1);
1299 }
1300 
1301 /*
1302  * Generate the standard symbol table.  Contains all locals and globals,
1303  * and resides in a non-allocatable section (ie. it can be stripped).
1304  */
1305 static uintptr_t
1306 make_symtab(Ofl_desc *ofl)
1307 {
1308 	Shdr		*shdr;
1309 	Elf_Data	*data;
1310 	Is_desc		*isec;
1311 	Is_desc		*xisec = 0;
1312 	size_t		size;
1313 	Word		symcnt;
1314 
1315 	/*
1316 	 * Create the section headers. Note that we supply an ent_cnt
1317 	 * of 0. We won't know the count until the section has been placed.
1318 	 */
1319 	if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0,
1320 	    &isec, &shdr, &data) == S_ERROR)
1321 		return (S_ERROR);
1322 
1323 	/*
1324 	 * Place the section first since it will affect the local symbol
1325 	 * count.
1326 	 */
1327 	if ((ofl->ofl_ossymtab = ld_place_section(ofl, isec, M_ID_SYMTAB, 0)) ==
1328 	    (Os_desc *)S_ERROR)
1329 		return (S_ERROR);
1330 
1331 	/*
1332 	 * At this point we've created all but the 'shstrtab' section.
1333 	 * Determine if we have to use 'Extended Sections'.  If so - then
1334 	 * also create a SHT_SYMTAB_SHNDX section.
1335 	 */
1336 	if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
1337 		Shdr		*xshdr;
1338 		Elf_Data	*xdata;
1339 
1340 		if (new_section(ofl, SHT_SYMTAB_SHNDX,
1341 		    MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec,
1342 		    &xshdr, &xdata) == S_ERROR)
1343 			return (S_ERROR);
1344 
1345 		if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec,
1346 		    M_ID_SYMTAB_NDX, 0)) == (Os_desc *)S_ERROR)
1347 			return (S_ERROR);
1348 	}
1349 	/*
1350 	 * Calculated number of symbols, which need to be augmented by
1351 	 * the null first entry, the FILE symbol, and the .shstrtab entry.
1352 	 */
1353 	symcnt = (size_t)(3 + ofl->ofl_shdrcnt + ofl->ofl_scopecnt +
1354 	    ofl->ofl_locscnt + ofl->ofl_globcnt);
1355 	size = symcnt * shdr->sh_entsize;
1356 
1357 	/*
1358 	 * Finalize the section header and data buffer initialization.
1359 	 */
1360 	data->d_size = size;
1361 	shdr->sh_size = (Xword)size;
1362 
1363 	/*
1364 	 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
1365 	 */
1366 	if (xisec) {
1367 		size_t	xsize = symcnt * sizeof (Word);
1368 
1369 		xisec->is_indata->d_size = xsize;
1370 		xisec->is_shdr->sh_size = (Xword)xsize;
1371 	}
1372 
1373 	return (1);
1374 }
1375 
1376 
1377 /*
1378  * Build a dynamic symbol table. These tables reside in the text
1379  * segment of a dynamic executable or shared library.
1380  *
1381  *	.SUNW_ldynsym contains local function symbols
1382  *	.dynsym contains only globals symbols
1383  *
1384  * The two tables are created adjacent to each other, with .SUNW_ldynsym
1385  * coming first.
1386  */
1387 static uintptr_t
1388 make_dynsym(Ofl_desc *ofl)
1389 {
1390 	Shdr		*shdr, *lshdr;
1391 	Elf_Data	*data, *ldata;
1392 	Is_desc		*isec, *lisec;
1393 	size_t		size;
1394 	Xword		cnt;
1395 	int		allow_ldynsym;
1396 
1397 	/*
1398 	 * Unless explicitly disabled, always produce a .SUNW_ldynsym section
1399 	 * when it is allowed by the file type, even if the resulting
1400 	 * table only ends up with a single STT_FILE in it. There are
1401 	 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB
1402 	 * entry in the .dynamic section, which is something we would
1403 	 * like to encourage, and (2) Without it, we cannot generate
1404 	 * the associated .SUNW_dyn[sym|tls]sort sections, which are of
1405 	 * value to DTrace.
1406 	 *
1407 	 * In practice, it is extremely rare for an object not to have
1408 	 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be
1409 	 * doing it anyway.
1410 	 */
1411 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1412 
1413 	/*
1414 	 * Create the section headers. Note that we supply an ent_cnt
1415 	 * of 0. We won't know the count until the section has been placed.
1416 	 */
1417 	if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM,
1418 	    MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR)
1419 		return (S_ERROR);
1420 
1421 	if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0,
1422 	    &isec, &shdr, &data) == S_ERROR)
1423 		return (S_ERROR);
1424 
1425 	/*
1426 	 * Place the section(s) first since it will affect the local symbol
1427 	 * count.
1428 	 */
1429 	if (allow_ldynsym &&
1430 	    ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec,
1431 	    M_ID_LDYNSYM, 0)) == (Os_desc *)S_ERROR))
1432 		return (S_ERROR);
1433 	if ((ofl->ofl_osdynsym = ld_place_section(ofl, isec, M_ID_DYNSYM, 0))
1434 	    == (Os_desc *)S_ERROR)
1435 		return (S_ERROR);
1436 
1437 	/*
1438 	 * One extra section header entry for the 'null' entry.
1439 	 */
1440 	cnt = 1 + ofl->ofl_dynshdrcnt + ofl->ofl_globcnt + ofl->ofl_lregsymcnt;
1441 	size = (size_t)cnt * shdr->sh_entsize;
1442 
1443 	/*
1444 	 * Finalize the section header and data buffer initialization.
1445 	 */
1446 	data->d_size = size;
1447 	shdr->sh_size = (Xword)size;
1448 
1449 	/*
1450 	 * An ldynsym contains local function symbols. It is not
1451 	 * used for linking, but if present, serves to allow better
1452 	 * stack traces to be generated in contexts where the symtab
1453 	 * is not available. (dladdr(), or stripped executable/library files).
1454 	 */
1455 	if (allow_ldynsym) {
1456 		cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt;
1457 		size = (size_t)cnt * shdr->sh_entsize;
1458 
1459 		ldata->d_size = size;
1460 		lshdr->sh_size = (Xword)size;
1461 	}
1462 
1463 	return (1);
1464 }
1465 
1466 /*
1467  * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are
1468  * index sections for the .SUNW_ldynsym/.dynsym pair that present data
1469  * and function symbols sorted by address.
1470  */
1471 static uintptr_t
1472 make_dynsort(Ofl_desc *ofl)
1473 {
1474 	Shdr		*shdr;
1475 	Elf_Data	*data;
1476 	Is_desc		*isec;
1477 
1478 
1479 	/* Only do it if the .SUNW_ldynsym section is present */
1480 	if (!OFL_ALLOW_LDYNSYM(ofl))
1481 		return (1);
1482 
1483 	/* .SUNW_dynsymsort */
1484 	if (ofl->ofl_dynsymsortcnt > 0) {
1485 		if (new_section(ofl, SHT_SUNW_symsort,
1486 		    MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt,
1487 		    &isec, &shdr, &data) == S_ERROR)
1488 		return (S_ERROR);
1489 
1490 		if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec,
1491 		    M_ID_DYNSORT, 0)) == (Os_desc *)S_ERROR)
1492 			return (S_ERROR);
1493 	}
1494 
1495 	/* .SUNW_dyntlssort */
1496 	if (ofl->ofl_dyntlssortcnt > 0) {
1497 		if (new_section(ofl, SHT_SUNW_tlssort,
1498 		    MSG_ORIG(MSG_SCN_DYNTLSSORT),
1499 		    ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR)
1500 		return (S_ERROR);
1501 
1502 		if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec,
1503 		    M_ID_DYNSORT, 0)) == (Os_desc *)S_ERROR)
1504 			return (S_ERROR);
1505 	}
1506 
1507 	return (1);
1508 }
1509 
1510 /*
1511  * Helper routine for make_dynsym_shndx. Builds a
1512  * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing
1513  * which one it is.
1514  */
1515 static uintptr_t
1516 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab,
1517     Os_desc **ret_os)
1518 {
1519 	Is_desc		*isec;
1520 	Is_desc		*dynsymisp;
1521 	Shdr		*shdr, *dynshdr;
1522 	Elf_Data	*data;
1523 
1524 	dynsymisp = (Is_desc *)symtab->os_isdescs.head->data;
1525 	dynshdr = dynsymisp->is_shdr;
1526 
1527 	if (new_section(ofl, SHT_SYMTAB_SHNDX, shname,
1528 	    (dynshdr->sh_size / dynshdr->sh_entsize),
1529 	    &isec, &shdr, &data) == S_ERROR)
1530 		return (S_ERROR);
1531 
1532 	if ((*ret_os = ld_place_section(ofl, isec,
1533 	    M_ID_DYNSYM_NDX, 0)) == (Os_desc *)S_ERROR)
1534 		return (S_ERROR);
1535 
1536 	assert(*ret_os);
1537 
1538 	return (1);
1539 }
1540 
1541 /*
1542  * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym
1543  */
1544 static uintptr_t
1545 make_dynsym_shndx(Ofl_desc *ofl)
1546 {
1547 	/*
1548 	 * If there is a .SUNW_ldynsym, generate a section for its extended
1549 	 * index section as well.
1550 	 */
1551 	if (OFL_ALLOW_LDYNSYM(ofl)) {
1552 		if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX),
1553 		    ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR)
1554 			return (S_ERROR);
1555 	}
1556 
1557 	/* The Generate a section for the dynsym */
1558 	if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX),
1559 	    ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR)
1560 		return (S_ERROR);
1561 
1562 	return (1);
1563 }
1564 
1565 
1566 /*
1567  * Build a string table for the section headers.
1568  */
1569 static uintptr_t
1570 make_shstrtab(Ofl_desc *ofl)
1571 {
1572 	Shdr		*shdr;
1573 	Elf_Data	*data;
1574 	Is_desc		*isec;
1575 	size_t		size;
1576 
1577 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB),
1578 	    0, &isec, &shdr, &data) == S_ERROR)
1579 		return (S_ERROR);
1580 
1581 	/*
1582 	 * Place the section first, as it may effect the number of section
1583 	 * headers to account for.
1584 	 */
1585 	if ((ofl->ofl_osshstrtab = ld_place_section(ofl, isec, M_ID_NOTE, 0)) ==
1586 	    (Os_desc *)S_ERROR)
1587 		return (S_ERROR);
1588 
1589 	size = st_getstrtab_sz(ofl->ofl_shdrsttab);
1590 	assert(size > 0);
1591 
1592 	data->d_size = size;
1593 	shdr->sh_size = (Xword)size;
1594 
1595 	return (1);
1596 }
1597 
1598 /*
1599  * Build a string section for the standard symbol table.
1600  */
1601 static uintptr_t
1602 make_strtab(Ofl_desc *ofl)
1603 {
1604 	Shdr		*shdr;
1605 	Elf_Data	*data;
1606 	Is_desc		*isec;
1607 	size_t		size;
1608 
1609 	/*
1610 	 * This string table consists of all the global and local symbols.
1611 	 * Account for null bytes at end of the file name and the beginning
1612 	 * of section.
1613 	 */
1614 	if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
1615 		return (S_ERROR);
1616 
1617 	size = st_getstrtab_sz(ofl->ofl_strtab);
1618 	assert(size > 0);
1619 
1620 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB),
1621 	    0, &isec, &shdr, &data) == S_ERROR)
1622 		return (S_ERROR);
1623 
1624 	/* Set the size of the data area */
1625 	data->d_size = size;
1626 	shdr->sh_size = (Xword)size;
1627 
1628 	ofl->ofl_osstrtab = ld_place_section(ofl, isec, M_ID_STRTAB, 0);
1629 	return ((uintptr_t)ofl->ofl_osstrtab);
1630 }
1631 
1632 /*
1633  * Build a string table for the dynamic symbol table.
1634  */
1635 static uintptr_t
1636 make_dynstr(Ofl_desc *ofl)
1637 {
1638 	Shdr		*shdr;
1639 	Elf_Data	*data;
1640 	Is_desc		*isec;
1641 	size_t		size;
1642 
1643 	/*
1644 	 * If producing a .SUNW_ldynsym, account for the initial STT_FILE
1645 	 * symbol that precedes the scope reduced global symbols.
1646 	 */
1647 	if (OFL_ALLOW_LDYNSYM(ofl)) {
1648 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1)
1649 			return (S_ERROR);
1650 		ofl->ofl_dynscopecnt++;
1651 	}
1652 
1653 
1654 	/*
1655 	 * Account for any local, named register symbols.  These locals are
1656 	 * required for reference from DT_REGISTER .dynamic entries.
1657 	 */
1658 	if (ofl->ofl_regsyms) {
1659 		int	ndx;
1660 
1661 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1662 			Sym_desc *	sdp;
1663 
1664 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1665 				continue;
1666 
1667 			if (((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) &&
1668 			    (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
1669 				continue;
1670 
1671 			if (sdp->sd_sym->st_name == 0)
1672 				continue;
1673 
1674 			if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
1675 				return (S_ERROR);
1676 		}
1677 	}
1678 
1679 	/*
1680 	 * Reserve entries for any per-symbol auxiliary/filter strings.
1681 	 */
1682 	if (ofl->ofl_dtsfltrs) {
1683 		Dfltr_desc *	dftp;
1684 		Aliste		off;
1685 
1686 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp))
1687 			if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
1688 				return (S_ERROR);
1689 	}
1690 
1691 	size = st_getstrtab_sz(ofl->ofl_dynstrtab);
1692 	assert(size > 0);
1693 
1694 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR),
1695 	    0, &isec, &shdr, &data) == S_ERROR)
1696 		return (S_ERROR);
1697 
1698 	/* Make it allocable if necessary */
1699 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
1700 		shdr->sh_flags |= SHF_ALLOC;
1701 
1702 	/* Set the size of the data area */
1703 	data->d_size = size + DYNSTR_EXTRA_PAD;
1704 
1705 	shdr->sh_size = (Xword)size;
1706 
1707 	ofl->ofl_osdynstr = ld_place_section(ofl, isec, M_ID_DYNSTR, 0);
1708 	return ((uintptr_t)ofl->ofl_osdynstr);
1709 }
1710 
1711 /*
1712  * Generate an output relocation section which will contain the relocation
1713  * information to be applied to the `osp' section.
1714  *
1715  * If (osp == NULL) then we are creating the coalesced relocation section
1716  * for an executable and/or a shared object.
1717  */
1718 static uintptr_t
1719 make_reloc(Ofl_desc *ofl, Os_desc *osp)
1720 {
1721 	Shdr		*shdr;
1722 	Elf_Data	*data;
1723 	Is_desc		*isec;
1724 	size_t		size;
1725 	Xword		sh_flags;
1726 	char 		*sectname;
1727 	Os_desc		*rosp;
1728 	Word		relsize;
1729 	const char	*rel_prefix;
1730 
1731 	/* LINTED */
1732 	if (M_REL_SHT_TYPE == SHT_REL) {
1733 		/* REL */
1734 		relsize = sizeof (Rel);
1735 		rel_prefix = MSG_ORIG(MSG_SCN_REL);
1736 	} else {
1737 		/* RELA */
1738 		relsize = sizeof (Rela);
1739 		rel_prefix = MSG_ORIG(MSG_SCN_RELA);
1740 	}
1741 
1742 	if (osp) {
1743 		size = osp->os_szoutrels;
1744 		sh_flags = osp->os_shdr->sh_flags;
1745 		if ((sectname = libld_malloc(strlen(rel_prefix) +
1746 		    strlen(osp->os_name) + 1)) == 0)
1747 			return (S_ERROR);
1748 		(void) strcpy(sectname, rel_prefix);
1749 		(void) strcat(sectname, osp->os_name);
1750 	} else if (ofl->ofl_flags1 & FLG_OF1_RELCNT) {
1751 		size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
1752 		sh_flags = SHF_ALLOC;
1753 		sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
1754 	} else {
1755 		size = ofl->ofl_relocrelsz;
1756 		sh_flags = SHF_ALLOC;
1757 		sectname = (char *)rel_prefix;
1758 	}
1759 
1760 	/*
1761 	 * Keep track of total size of 'output relocations' (to be stored
1762 	 * in .dynamic)
1763 	 */
1764 	/* LINTED */
1765 	ofl->ofl_relocsz += (Xword)size;
1766 
1767 	if (new_section(ofl, M_REL_SHT_TYPE, sectname, 0, &isec, &shdr, &data)
1768 	    == S_ERROR)
1769 		return (S_ERROR);
1770 
1771 	data->d_size = size;
1772 
1773 	shdr->sh_size = (Xword)size;
1774 	if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC))
1775 		shdr->sh_flags = SHF_ALLOC;
1776 
1777 	if (osp) {
1778 		/*
1779 		 * The sh_info field of the SHT_REL* sections points to the
1780 		 * section the relocations are to be applied to.
1781 		 */
1782 		shdr->sh_flags |= SHF_INFO_LINK;
1783 	}
1784 
1785 	/*
1786 	 * Associate this relocation section to the section its going to
1787 	 * relocate.
1788 	 */
1789 	if ((rosp = ld_place_section(ofl, isec, M_ID_REL, 0)) ==
1790 	    (Os_desc *)S_ERROR)
1791 		return (S_ERROR);
1792 
1793 	if (osp) {
1794 		Listnode	*lnp;
1795 		Is_desc		*risp;
1796 
1797 		/*
1798 		 * We associate the input relocation sections - with
1799 		 * the newly created output relocation section.
1800 		 *
1801 		 * This is used primarily so that we can update
1802 		 * SHT_GROUP[sect_no] entries to point to the
1803 		 * created output relocation sections.
1804 		 */
1805 		for (LIST_TRAVERSE(&(osp->os_relisdescs), lnp, risp)) {
1806 			risp->is_osdesc = rosp;
1807 
1808 			/*
1809 			 * If the input relocation section had the SHF_GROUP
1810 			 * flag set - propagate it to the output relocation
1811 			 * section.
1812 			 */
1813 			if (risp->is_shdr->sh_flags & SHF_GROUP) {
1814 				rosp->os_shdr->sh_flags |= SHF_GROUP;
1815 				break;
1816 			}
1817 		}
1818 		osp->os_relosdesc = rosp;
1819 	} else
1820 		ofl->ofl_osrel = rosp;
1821 
1822 	/*
1823 	 * If this is the first relocation section we've encountered save it
1824 	 * so that the .dynamic entry can be initialized accordingly.
1825 	 */
1826 	if (ofl->ofl_osrelhead == (Os_desc *)0)
1827 		ofl->ofl_osrelhead = rosp;
1828 
1829 	return (1);
1830 }
1831 
1832 /*
1833  * Generate version needed section.
1834  */
1835 static uintptr_t
1836 make_verneed(Ofl_desc *ofl)
1837 {
1838 	Shdr		*shdr;
1839 	Elf_Data	*data;
1840 	Is_desc		*isec;
1841 
1842 	/*
1843 	 * verneed sections do not have a constant element size, so the
1844 	 * value of ent_cnt specified here (0) is meaningless.
1845 	 */
1846 	if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION),
1847 	    0, &isec, &shdr, &data) == S_ERROR)
1848 		return (S_ERROR);
1849 
1850 	/* During version processing we calculated the total size. */
1851 	data->d_size = ofl->ofl_verneedsz;
1852 	shdr->sh_size = (Xword)ofl->ofl_verneedsz;
1853 
1854 	ofl->ofl_osverneed = ld_place_section(ofl, isec, M_ID_VERSION, 0);
1855 	return ((uintptr_t)ofl->ofl_osverneed);
1856 }
1857 
1858 /*
1859  * Generate a version definition section.
1860  *
1861  *  o	the SHT_SUNW_verdef section defines the versions that exist within this
1862  *	image.
1863  */
1864 static uintptr_t
1865 make_verdef(Ofl_desc *ofl)
1866 {
1867 	Shdr		*shdr;
1868 	Elf_Data	*data;
1869 	Is_desc		*isec;
1870 	Ver_desc	*vdp;
1871 
1872 	/*
1873 	 * Reserve a string table entry for the base version dependency (other
1874 	 * dependencies have symbol representations, which will already be
1875 	 * accounted for during symbol processing).
1876 	 */
1877 	vdp = (Ver_desc *)ofl->ofl_verdesc.head->data;
1878 
1879 	if (ofl->ofl_flags & FLG_OF_DYNAMIC) {
1880 		if (st_insert(ofl->ofl_dynstrtab, vdp->vd_name) == -1)
1881 			return (S_ERROR);
1882 	} else {
1883 		if (st_insert(ofl->ofl_strtab, vdp->vd_name) == -1)
1884 			return (S_ERROR);
1885 	}
1886 
1887 	/*
1888 	 * verdef sections do not have a constant element size, so the
1889 	 * value of ent_cnt specified here (0) is meaningless.
1890 	 */
1891 	if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION),
1892 	    0, &isec, &shdr, &data) == S_ERROR)
1893 		return (S_ERROR);
1894 
1895 	/* During version processing we calculated the total size. */
1896 	data->d_size = ofl->ofl_verdefsz;
1897 	shdr->sh_size = (Xword)ofl->ofl_verdefsz;
1898 
1899 	ofl->ofl_osverdef = ld_place_section(ofl, isec, M_ID_VERSION, 0);
1900 	return ((uintptr_t)ofl->ofl_osverdef);
1901 }
1902 
1903 /*
1904  * Common function used to build both the SHT_SUNW_versym
1905  * section and the SHT_SUNW_syminfo section.  Each of these sections
1906  * provides additional symbol information.
1907  */
1908 static Os_desc *
1909 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident)
1910 {
1911 	Shdr		*shdr;
1912 	Elf_Data	*data;
1913 	Is_desc		*isec;
1914 
1915 	/*
1916 	 * We don't know the size of this section yet, so set it to 0.
1917 	 * It gets filled in after the dynsym is sized.
1918 	 */
1919 	if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) ==
1920 	    S_ERROR)
1921 		return ((Os_desc *)S_ERROR);
1922 
1923 	return (ld_place_section(ofl, isec, ident, 0));
1924 }
1925 
1926 /*
1927  * Build a .sunwbss section for allocation of tentative definitions.
1928  */
1929 uintptr_t
1930 ld_make_sunwbss(Ofl_desc *ofl, size_t size, Xword align)
1931 {
1932 	Shdr		*shdr;
1933 	Elf_Data	*data;
1934 	Is_desc		*isec;
1935 
1936 	/*
1937 	 * Allocate header structs. We will set the name ourselves below,
1938 	 * and there is no entcnt for a BSS. So, the shname and entcnt
1939 	 * arguments are 0.
1940 	 */
1941 	if (new_section(ofl, SHT_NOBITS, MSG_ORIG(MSG_SCN_SUNWBSS), 0,
1942 	    &isec, &shdr, &data) == S_ERROR)
1943 		return (S_ERROR);
1944 
1945 	data->d_size = size;
1946 	data->d_align = align;
1947 
1948 	shdr->sh_size = (Xword)size;
1949 	shdr->sh_addralign = align;
1950 
1951 	/*
1952 	 * Retain this .sunwbss input section as this will be where global
1953 	 * symbol references are added.
1954 	 */
1955 	ofl->ofl_issunwbss = isec;
1956 	if (ld_place_section(ofl, isec, 0, 0) == (Os_desc *)S_ERROR)
1957 		return (S_ERROR);
1958 
1959 	return (1);
1960 }
1961 
1962 /*
1963  * This routine is called when -z nopartial is in effect.
1964  */
1965 uintptr_t
1966 ld_make_sunwdata(Ofl_desc *ofl, size_t size, Xword align)
1967 {
1968 	Shdr		*shdr;
1969 	Elf_Data	*data;
1970 	Is_desc		*isec;
1971 	Os_desc		*osp;
1972 
1973 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_SUNWDATA1), 0,
1974 	    &isec, &shdr, &data) == S_ERROR)
1975 		return (S_ERROR);
1976 
1977 	shdr->sh_flags |= SHF_WRITE;
1978 	data->d_size = size;
1979 	shdr->sh_size = (Xword)size;
1980 	if (align != 0) {
1981 		data->d_align = align;
1982 		shdr->sh_addralign = align;
1983 	}
1984 
1985 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
1986 		return (S_ERROR);
1987 
1988 	/*
1989 	 * Retain this .sunwdata1 input section as this will
1990 	 * be where global
1991 	 * symbol references are added.
1992 	 */
1993 	ofl->ofl_issunwdata1 = isec;
1994 	if ((osp = ld_place_section(ofl, isec, M_ID_DATA, 0)) ==
1995 	    (Os_desc *)S_ERROR)
1996 		return (S_ERROR);
1997 
1998 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
1999 		ofl->ofl_dynshdrcnt++;
2000 		osp->os_flags |= FLG_OS_OUTREL;
2001 	}
2002 	return (1);
2003 }
2004 
2005 /*
2006  * Make .sunwmove section
2007  */
2008 uintptr_t
2009 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2010 {
2011 	Shdr		*shdr;
2012 	Elf_Data	*data;
2013 	Is_desc		*isec;
2014 	Listnode	*lnp1;
2015 	Psym_info	*psym;
2016 	int 		cnt = 1;
2017 
2018 
2019 	if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE),
2020 	    mv_nums, &isec, &shdr, &data) == S_ERROR)
2021 		return (S_ERROR);
2022 
2023 	if ((data->d_buf = libld_calloc(data->d_size, 1)) == 0)
2024 		return (S_ERROR);
2025 
2026 	/*
2027 	 * Copy move entries
2028 	 */
2029 	for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) {
2030 		Listnode *	lnp2;
2031 		Mv_itm *	mvitm;
2032 
2033 		if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN)
2034 			continue;
2035 		for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvitm)) {
2036 			if ((mvitm->mv_flag & FLG_MV_OUTSECT) == 0)
2037 				continue;
2038 			mvitm->mv_oidx = cnt;
2039 			cnt++;
2040 		}
2041 	}
2042 	if ((ofl->ofl_osmove = ld_place_section(ofl, isec, 0, 0)) ==
2043 	    (Os_desc *)S_ERROR)
2044 		return (S_ERROR);
2045 
2046 	return (1);
2047 }
2048 
2049 
2050 /*
2051  * The following sections are built after all input file processing and symbol
2052  * validation has been carried out.  The order is important (because the
2053  * addition of a section adds a new symbol there is a chicken and egg problem
2054  * of maintaining the appropriate counts).  By maintaining a known order the
2055  * individual routines can compensate for later, known, additions.
2056  */
2057 uintptr_t
2058 ld_make_sections(Ofl_desc *ofl)
2059 {
2060 	Word		flags = ofl->ofl_flags;
2061 	Listnode	*lnp1;
2062 	Sg_desc		*sgp;
2063 
2064 	/*
2065 	 * Generate any special sections.
2066 	 */
2067 	if (flags & FLG_OF_ADDVERS)
2068 		if (make_comment(ofl) == S_ERROR)
2069 			return (S_ERROR);
2070 
2071 	if (make_interp(ofl) == S_ERROR)
2072 		return (S_ERROR);
2073 
2074 	if (make_cap(ofl) == S_ERROR)
2075 		return (S_ERROR);
2076 
2077 	if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
2078 	    &ofl->ofl_initarray) == S_ERROR)
2079 		return (S_ERROR);
2080 
2081 	if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
2082 	    &ofl->ofl_finiarray) == S_ERROR)
2083 		return (S_ERROR);
2084 
2085 	if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
2086 	    &ofl->ofl_preiarray) == S_ERROR)
2087 		return (S_ERROR);
2088 
2089 	/*
2090 	 * Make the .plt section.  This occurs after any other relocation
2091 	 * sections are generated (see reloc_init()) to ensure that the
2092 	 * associated relocation section is after all the other relocation
2093 	 * sections.
2094 	 */
2095 	if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
2096 		if (make_plt(ofl) == S_ERROR)
2097 			return (S_ERROR);
2098 
2099 	/*
2100 	 * Determine whether any sections or files are not referenced.  Under
2101 	 * -Dunused a diagnostic for any unused components is generated, under
2102 	 * -zignore the component is removed from the final output.
2103 	 */
2104 	if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
2105 		if (ignore_section_processing(ofl) == S_ERROR)
2106 			return (S_ERROR);
2107 	}
2108 
2109 	/*
2110 	 * Add any necessary versioning information.
2111 	 */
2112 	if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED) {
2113 		if (make_verneed(ofl) == S_ERROR)
2114 			return (S_ERROR);
2115 	}
2116 	if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == FLG_OF_VERDEF) {
2117 		if (make_verdef(ofl) == S_ERROR)
2118 			return (S_ERROR);
2119 		if ((ofl->ofl_osversym = make_sym_sec(ofl,
2120 		    MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym,
2121 		    M_ID_VERSION)) == (Os_desc*)S_ERROR)
2122 			return (S_ERROR);
2123 	}
2124 
2125 	/*
2126 	 * Create a syminfo section if necessary.
2127 	 */
2128 	if (ofl->ofl_flags & FLG_OF_SYMINFO) {
2129 		if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
2130 		    MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo,
2131 		    M_ID_SYMINFO)) == (Os_desc *)S_ERROR)
2132 			return (S_ERROR);
2133 	}
2134 
2135 	if (ofl->ofl_flags1 & FLG_OF1_RELCNT) {
2136 		/*
2137 		 * If -zcombreloc is enabled then all relocations (except for
2138 		 * the PLT's) are coalesced into a single relocation section.
2139 		 */
2140 		if (ofl->ofl_reloccnt) {
2141 			if (make_reloc(ofl, NULL) == S_ERROR)
2142 				return (S_ERROR);
2143 		}
2144 	} else {
2145 		/*
2146 		 * Create the required output relocation sections.  Note, new
2147 		 * sections may be added to the section list that is being
2148 		 * traversed.  These insertions can move the elements of the
2149 		 * Alist such that a section descriptor is re-read.  Recursion
2150 		 * is prevented by maintaining a previous section pointer and
2151 		 * insuring that this pointer isn't re-examined.
2152 		 */
2153 		for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2154 			Os_desc	**ospp, *posp = 0;
2155 			Aliste	off;
2156 
2157 			for (ALIST_TRAVERSE(sgp->sg_osdescs, off, ospp)) {
2158 				Os_desc	*osp = *ospp;
2159 
2160 				if ((osp != posp) && osp->os_szoutrels &&
2161 				    (osp != ofl->ofl_osplt)) {
2162 					if (make_reloc(ofl, osp) == S_ERROR)
2163 						return (S_ERROR);
2164 				}
2165 				posp = osp;
2166 			}
2167 		}
2168 
2169 		/*
2170 		 * If we're not building a combined relocation section, then
2171 		 * build a .rel[a] section as required.
2172 		 */
2173 		if (ofl->ofl_relocrelsz) {
2174 			if (make_reloc(ofl, NULL) == S_ERROR)
2175 				return (S_ERROR);
2176 		}
2177 	}
2178 
2179 	/*
2180 	 * The PLT relocations are always in their own section, and we try to
2181 	 * keep them at the end of the PLT table.  We do this to keep the hot
2182 	 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
2183 	 */
2184 	if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
2185 		if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
2186 			return (S_ERROR);
2187 	}
2188 
2189 	/*
2190 	 * Finally build the symbol and section header sections.
2191 	 */
2192 	if (flags & FLG_OF_DYNAMIC) {
2193 		if (make_dynamic(ofl) == S_ERROR)
2194 			return (S_ERROR);
2195 		if (make_dynstr(ofl) == S_ERROR)
2196 			return (S_ERROR);
2197 		/*
2198 		 * There is no use for .hash and .dynsym sections in a
2199 		 * relocatable object.
2200 		 */
2201 		if (!(flags & FLG_OF_RELOBJ)) {
2202 			if (make_hash(ofl) == S_ERROR)
2203 				return (S_ERROR);
2204 			if (make_dynsym(ofl) == S_ERROR)
2205 				return (S_ERROR);
2206 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
2207 			if (make_amd64_unwindhdr(ofl) == S_ERROR)
2208 				return (S_ERROR);
2209 #endif
2210 			if (make_dynsort(ofl) == S_ERROR)
2211 				return (S_ERROR);
2212 		}
2213 	}
2214 
2215 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
2216 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
2217 		/*
2218 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2219 		 * for the dynsym.  If so - do it now.
2220 		 */
2221 		if (ofl->ofl_osdynsym &&
2222 		    ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
2223 			if (make_dynsym_shndx(ofl) == S_ERROR)
2224 				return (S_ERROR);
2225 		}
2226 
2227 		if (make_strtab(ofl) == S_ERROR)
2228 			return (S_ERROR);
2229 		if (make_symtab(ofl) == S_ERROR)
2230 			return (S_ERROR);
2231 	} else {
2232 		/*
2233 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2234 		 * for the dynsym.  If so - do it now.
2235 		 */
2236 		if (ofl->ofl_osdynsym &&
2237 		    ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
2238 			if (make_dynsym_shndx(ofl) == S_ERROR)
2239 				return (S_ERROR);
2240 		}
2241 	}
2242 
2243 	if (make_shstrtab(ofl) == S_ERROR)
2244 		return (S_ERROR);
2245 
2246 	/*
2247 	 * Now that we've created all of our sections adjust the size
2248 	 * of SHT_SUNW_versym & SHT_SUNW_syminfo which are dependent on
2249 	 * the symbol table sizes.
2250 	 */
2251 	if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
2252 		Shdr		*shdr;
2253 		Is_desc		*isec;
2254 		Elf_Data	*data;
2255 		size_t		size;
2256 		ulong_t		cnt;
2257 		Os_desc		*osp;
2258 
2259 		if (flags & (FLG_OF_RELOBJ | FLG_OF_STATIC)) {
2260 			osp = ofl->ofl_ossymtab;
2261 		} else {
2262 			osp = ofl->ofl_osdynsym;
2263 		}
2264 		isec = (Is_desc *)osp->os_isdescs.head->data;
2265 		cnt = (isec->is_shdr->sh_size / isec->is_shdr->sh_entsize);
2266 
2267 		if (ofl->ofl_osversym) {
2268 			osp = ofl->ofl_osversym;
2269 			isec = (Is_desc *)osp->os_isdescs.head->data;
2270 			data = isec->is_indata;
2271 			shdr = osp->os_shdr;
2272 			size = cnt * shdr->sh_entsize;
2273 			shdr->sh_size = (Xword)size;
2274 			data->d_size = size;
2275 		}
2276 		if (ofl->ofl_ossyminfo) {
2277 			osp = ofl->ofl_ossyminfo;
2278 			isec = (Is_desc *)osp->os_isdescs.head->data;
2279 			data = isec->is_indata;
2280 			shdr = osp->os_shdr;
2281 			size = cnt * shdr->sh_entsize;
2282 			shdr->sh_size = (Xword)size;
2283 			data->d_size = size;
2284 		}
2285 	}
2286 
2287 	return (1);
2288 }
2289 
2290 /*
2291  * Build an additional data section - used to back OBJT symbol definitions
2292  * added with a mapfile.
2293  */
2294 Is_desc *
2295 ld_make_data(Ofl_desc *ofl, size_t size)
2296 {
2297 	Shdr		*shdr;
2298 	Elf_Data	*data;
2299 	Is_desc		*isec;
2300 
2301 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2302 	    &isec, &shdr, &data) == S_ERROR)
2303 		return ((Is_desc *)S_ERROR);
2304 
2305 	data->d_size = size;
2306 	shdr->sh_size = (Xword)size;
2307 	shdr->sh_flags |= SHF_WRITE;
2308 
2309 	if (ld_place_section(ofl, isec, M_ID_DATA, 0) == (Os_desc *)S_ERROR)
2310 		return ((Is_desc *)S_ERROR);
2311 
2312 	return (isec);
2313 }
2314 
2315 /*
2316  * Define a set of templates for generating "void (*)(void)" function
2317  * definitions.
2318  */
2319 #if	defined(__i386) || defined(__amd64)
2320 #if	defined(__lint)
2321 static const uchar_t ret_template[] = { 0 };
2322 #else	/* __lint */
2323 #if	defined(_ELF64)
2324 #define	ret_template	ret64_template
2325 #else
2326 #define	ret_template	ret32_template
2327 #endif
2328 
2329 static const uchar_t ret32_template[] = {
2330 /* 0x00 */	0xc3				/* ret */
2331 };
2332 
2333 static const uchar_t ret64_template[] = {
2334 /* 0x00 */	0x55,				/* pushq  %rbp */
2335 /* 0x01 */	0x48, 0x8b, 0xec,		/* movq   %rsp,%rbp */
2336 /* 0x04 */	0x48, 0x8b, 0xe5,		/* movq   %rbp,%rsp */
2337 /* 0x07 */	0x5d,				/* popq   %rbp */
2338 /* 0x08 */	0xc3				/* ret */
2339 };
2340 #endif	/* __lint */
2341 
2342 #elif	defined(__sparc)
2343 static const uchar_t ret_template[] = {
2344 /* 0x00 */	0x81, 0xc3, 0xe0, 0x08,		/* retl */
2345 /* 0x04 */	0x01, 0x00, 0x00, 0x00		/* nop */
2346 };
2347 #else
2348 #error	unsupported architecture!
2349 #endif
2350 
2351 /*
2352  * Build an additional text section - used to back FUNC symbol definitions
2353  * added with a mapfile.
2354  */
2355 Is_desc *
2356 ld_make_text(Ofl_desc *ofl, size_t size)
2357 {
2358 	Shdr		*shdr;
2359 	Elf_Data	*data;
2360 	Is_desc		*isec;
2361 
2362 	/*
2363 	 * Insure the size is sufficient to contain the minimum return
2364 	 * instruction.
2365 	 */
2366 	if (size < sizeof (ret_template))
2367 		size = sizeof (ret_template);
2368 
2369 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0,
2370 	    &isec, &shdr, &data) == S_ERROR)
2371 		return ((Is_desc *)S_ERROR);
2372 
2373 	data->d_size = size;
2374 	shdr->sh_size = (Xword)size;
2375 	shdr->sh_flags |= SHF_EXECINSTR;
2376 
2377 	/* Fill the buffer with the appropriate return instruction. */
2378 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2379 		return ((Is_desc *)S_ERROR);
2380 	(void) memcpy(data->d_buf, ret_template, sizeof (ret_template));
2381 
2382 	if (ld_place_section(ofl, isec, M_ID_TEXT, 0) == (Os_desc *)S_ERROR)
2383 		return ((Is_desc *)S_ERROR);
2384 
2385 	return (isec);
2386 }
2387