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