xref: /titanic_41/usr/src/cmd/sgs/libld/common/sections.c (revision fd06a699040f011e80ab8ac5213bb0a47858e69b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  *	Copyright (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * Module sections. Initialize special sections
33  */
34 #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_flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt)
964 			cnt++;			/* RELACOUNT */
965 
966 		if (flags & FLG_OF_TEXTREL)	/* TEXTREL */
967 			cnt++;
968 
969 		if (ofl->ofl_osfiniarray)	/* FINI_ARRAY & FINI_ARRAYSZ */
970 			cnt += 2;
971 
972 		if (ofl->ofl_osinitarray)	/* INIT_ARRAY & INIT_ARRAYSZ */
973 			cnt += 2;
974 
975 		if (ofl->ofl_ospreinitarray)	/* PREINIT_ARRAY & */
976 			cnt += 2;		/*	PREINIT_ARRAYSZ */
977 
978 		/*
979 		 * If we have plt's reserve a PLT, PLTSZ, PLTREL and JMPREL.
980 		 */
981 		if (ofl->ofl_pltcnt)
982 			cnt += 3;
983 
984 		/*
985 		 * If pltpadding is needed (Sparcv9)
986 		 */
987 		if (ofl->ofl_pltpad)
988 			cnt += 2;		/* DT_PLTPAD & DT_PLTPADSZ */
989 
990 		/*
991 		 * If we have any relocations reserve a REL, RELSZ and
992 		 * RELENT entry.
993 		 */
994 		if (ofl->ofl_relocsz)
995 			cnt += 3;
996 
997 		/*
998 		 * If a syminfo section is required create SYMINFO, SYMINSZ,
999 		 * and SYMINENT entries.
1000 		 */
1001 		if (ofl->ofl_flags & FLG_OF_SYMINFO)
1002 			cnt += 3;
1003 
1004 		/*
1005 		 * If there are any partially initialized sections allocate
1006 		 * MOVEENT, MOVESZ and MOVETAB.
1007 		 */
1008 		if (ofl->ofl_osmove)
1009 			cnt += 3;
1010 
1011 		/*
1012 		 * Allocate one DT_REGISTER entry for every register symbol.
1013 		 */
1014 		cnt += ofl->ofl_regsymcnt;
1015 
1016 		/*
1017 		 * Reserve a entry for each '-zrtldinfo=...' specified
1018 		 * on the command line.
1019 		 */
1020 		for (LIST_TRAVERSE(&ofl->ofl_rtldinfo, lnp, sdp))
1021 			cnt++;
1022 
1023 		/*
1024 		 * These two entries should only be placed in a segment
1025 		 * which is writable.  If it's a read-only segment
1026 		 * (due to mapfile magic, e.g. libdl.so.1) then don't allocate
1027 		 * these entries.
1028 		 */
1029 		if ((osp->os_sgdesc) &&
1030 		    (osp->os_sgdesc->sg_phdr.p_flags & PF_W)) {
1031 			cnt++;			/* FEATURE_1 */
1032 
1033 			if (ofl->ofl_osinterp)
1034 				cnt++;		/* DEBUG */
1035 		}
1036 
1037 		/*
1038 		 * Any hardware/software capabilities?
1039 		 */
1040 		if (ofl->ofl_oscap)
1041 			cnt++;			/* SUNW_CAP */
1042 	}
1043 
1044 	if (flags & FLG_OF_SYMBOLIC)
1045 		cnt++;				/* SYMBOLIC */
1046 
1047 	/*
1048 	 * Account for Architecture dependent .dynamic entries, and defaults.
1049 	 */
1050 	ld_mach_make_dynamic(ofl, &cnt);
1051 
1052 	/*
1053 	 * DT_FLAGS, DT_FLAGS_1, DT_SUNW_STRPAD, and DT_NULL. Also,
1054 	 * allow room for the unused extra DT_NULLs. These are included
1055 	 * to allow an ELF editor room to add items later.
1056 	 */
1057 	cnt += 4 + DYNAMIC_EXTRA_ELTS;
1058 
1059 	/*
1060 	 * Determine the size of the section from the number of entries.
1061 	 */
1062 	size = cnt * (size_t)shdr->sh_entsize;
1063 
1064 	shdr->sh_size = (Xword)size;
1065 	data->d_size = size;
1066 
1067 	return ((uintptr_t)ofl->ofl_osdynamic);
1068 }
1069 
1070 /*
1071  * Build the GOT section and its associated relocation entries.
1072  */
1073 uintptr_t
1074 ld_make_got(Ofl_desc *ofl)
1075 {
1076 	Shdr		*shdr;
1077 	Elf_Data	*data;
1078 	Is_desc		*isec;
1079 	size_t		size = (size_t)ofl->ofl_gotcnt * M_GOT_ENTSIZE;
1080 	size_t		rsize = (size_t)ofl->ofl_relocgotsz;
1081 
1082 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0,
1083 	    &isec, &shdr, &data) == S_ERROR)
1084 		return (S_ERROR);
1085 
1086 	data->d_size = size;
1087 
1088 	shdr->sh_flags |= SHF_WRITE;
1089 	shdr->sh_size = (Xword)size;
1090 	shdr->sh_entsize = M_GOT_ENTSIZE;
1091 
1092 	if ((ofl->ofl_osgot = ld_place_section(ofl, isec, M_ID_GOT, 0)) ==
1093 	    (Os_desc *)S_ERROR)
1094 		return (S_ERROR);
1095 
1096 	ofl->ofl_osgot->os_szoutrels = (Xword)rsize;
1097 
1098 	return (1);
1099 }
1100 
1101 /*
1102  * Build an interpreter section.
1103  */
1104 static uintptr_t
1105 make_interp(Ofl_desc *ofl)
1106 {
1107 	Shdr		*shdr;
1108 	Elf_Data	*data;
1109 	Is_desc		*isec;
1110 	const char	*iname = ofl->ofl_interp;
1111 	size_t		size;
1112 
1113 	/*
1114 	 * If -z nointerp is in effect, don't create an interpreter section.
1115 	 */
1116 	if (ofl->ofl_flags1 & FLG_OF1_NOINTRP)
1117 		return (1);
1118 
1119 	/*
1120 	 * We always build an .interp section for dynamic executables.  However
1121 	 * if the user has specifically specified an interpreter we'll build
1122 	 * this section for any output (presumably the user knows what they are
1123 	 * doing. refer ABI section 5-4, and ld.1 man page use of -I).
1124 	 */
1125 	if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC |
1126 	    FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname)
1127 		return (1);
1128 
1129 	/*
1130 	 * In the case of a dynamic executable supply a default interpreter
1131 	 * if a specific interpreter has not been specified.
1132 	 */
1133 	if (iname == 0) {
1134 		if (ofl->ofl_dehdr->e_machine == EM_SPARCV9)
1135 			iname = ofl->ofl_interp =
1136 			    MSG_ORIG(MSG_PTH_RTLD_SPARCV9);
1137 		else if (ofl->ofl_dehdr->e_machine == EM_AMD64)
1138 			iname = ofl->ofl_interp =
1139 			    MSG_ORIG(MSG_PTH_RTLD_AMD64);
1140 		else
1141 			iname = ofl->ofl_interp = MSG_ORIG(MSG_PTH_RTLD);
1142 	}
1143 
1144 	size = strlen(iname) + 1;
1145 
1146 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0,
1147 	    &isec, &shdr, &data) == S_ERROR)
1148 		return (S_ERROR);
1149 
1150 	data->d_size = size;
1151 	shdr->sh_size = (Xword)size;
1152 	data->d_align = shdr->sh_addralign = 1;
1153 
1154 	ofl->ofl_osinterp = ld_place_section(ofl, isec, M_ID_INTERP, 0);
1155 	return ((uintptr_t)ofl->ofl_osinterp);
1156 }
1157 
1158 /*
1159  * Build a hardware/software capabilities section.
1160  */
1161 static uintptr_t
1162 make_cap(Ofl_desc *ofl)
1163 {
1164 	Shdr		*shdr;
1165 	Elf_Data	*data;
1166 	Is_desc		*isec;
1167 	Os_desc		*osec;
1168 	Cap		*cap;
1169 	size_t		size = 0;
1170 
1171 	/*
1172 	 * Determine how many entries are required.
1173 	 */
1174 	if (ofl->ofl_hwcap_1)
1175 		size++;
1176 	if (ofl->ofl_sfcap_1)
1177 		size++;
1178 	if (size == 0)
1179 		return (1);
1180 	size++;				/* Add CA_SUNW_NULL */
1181 
1182 	if (new_section(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP), size,
1183 	    &isec, &shdr, &data) == S_ERROR)
1184 		return (S_ERROR);
1185 
1186 	if ((data->d_buf = libld_malloc(shdr->sh_size)) == 0)
1187 		return (S_ERROR);
1188 
1189 	cap = (Cap *)data->d_buf;
1190 	if (ofl->ofl_hwcap_1) {
1191 		cap->c_tag = CA_SUNW_HW_1;
1192 		cap->c_un.c_val = ofl->ofl_hwcap_1;
1193 		cap++;
1194 	}
1195 	if (ofl->ofl_sfcap_1) {
1196 		cap->c_tag = CA_SUNW_SF_1;
1197 		cap->c_un.c_val = ofl->ofl_sfcap_1;
1198 		cap++;
1199 	}
1200 	cap->c_tag = CA_SUNW_NULL;
1201 	cap->c_un.c_val = 0;
1202 
1203 	/*
1204 	 * If we're not creating a relocatable object, save the output section
1205 	 * to trigger the creation of an associated program header.
1206 	 */
1207 	osec = ld_place_section(ofl, isec, M_ID_CAP, 0);
1208 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
1209 		ofl->ofl_oscap = osec;
1210 
1211 	return ((uintptr_t)osec);
1212 }
1213 
1214 /*
1215  * Build the PLT section and its associated relocation entries.
1216  */
1217 static uintptr_t
1218 make_plt(Ofl_desc *ofl)
1219 {
1220 	Shdr		*shdr;
1221 	Elf_Data	*data;
1222 	Is_desc		*isec;
1223 	size_t		size = (size_t)M_PLT_RESERVSZ +
1224 	    (((size_t)ofl->ofl_pltcnt + (size_t)ofl->ofl_pltpad) *
1225 	    M_PLT_ENTSIZE);
1226 	size_t		rsize = (size_t)ofl->ofl_relocpltsz;
1227 
1228 #if	defined(__sparc)
1229 	/*
1230 	 * Account for the NOP at the end of the plt.
1231 	 */
1232 	size += sizeof (Word);
1233 #endif
1234 
1235 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0,
1236 	    &isec, &shdr, &data) == S_ERROR)
1237 		return (S_ERROR);
1238 
1239 	data->d_size = size;
1240 	data->d_align = M_PLT_ALIGN;
1241 
1242 	shdr->sh_flags = M_PLT_SHF_FLAGS;
1243 	shdr->sh_size = (Xword)size;
1244 	shdr->sh_addralign = M_PLT_ALIGN;
1245 	shdr->sh_entsize = M_PLT_ENTSIZE;
1246 
1247 	if ((ofl->ofl_osplt = ld_place_section(ofl, isec, M_ID_PLT, 0)) ==
1248 	    (Os_desc *)S_ERROR)
1249 		return (S_ERROR);
1250 
1251 	ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1252 
1253 	return (1);
1254 }
1255 
1256 /*
1257  * Make the hash table.  Only built for dynamic executables and shared
1258  * libraries, and provides hashed lookup into the global symbol table
1259  * (.dynsym) for the run-time linker to resolve symbol lookups.
1260  */
1261 static uintptr_t
1262 make_hash(Ofl_desc *ofl)
1263 {
1264 	Shdr		*shdr;
1265 	Elf_Data	*data;
1266 	Is_desc		*isec;
1267 	size_t		size;
1268 	Word		nsyms = ofl->ofl_globcnt;
1269 	size_t		cnt;
1270 
1271 	/*
1272 	 * Allocate section header structures. We set entcnt to 0
1273 	 * because it's going to change after we place this section.
1274 	 */
1275 	if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0,
1276 	    &isec, &shdr, &data) == S_ERROR)
1277 		return (S_ERROR);
1278 
1279 	/*
1280 	 * Place the section first since it will affect the local symbol
1281 	 * count.
1282 	 */
1283 	if ((ofl->ofl_oshash = ld_place_section(ofl, isec, M_ID_HASH, 0)) ==
1284 	    (Os_desc *)S_ERROR)
1285 		return (S_ERROR);
1286 
1287 	/*
1288 	 * Calculate the number of output hash buckets.
1289 	 */
1290 	ofl->ofl_hashbkts = findprime(nsyms);
1291 
1292 	/*
1293 	 * The size of the hash table is determined by
1294 	 *
1295 	 *	i.	the initial nbucket and nchain entries (2)
1296 	 *	ii.	the number of buckets (calculated above)
1297 	 *	iii.	the number of chains (this is based on the number of
1298 	 *		symbols in the .dynsym array + NULL symbol).
1299 	 */
1300 	cnt = 2 + ofl->ofl_hashbkts + (ofl->ofl_dynshdrcnt +
1301 	    ofl->ofl_globcnt + ofl->ofl_lregsymcnt + 1);
1302 	size = cnt * shdr->sh_entsize;
1303 
1304 	/*
1305 	 * Finalize the section header and data buffer initialization.
1306 	 */
1307 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
1308 		return (S_ERROR);
1309 	data->d_size = size;
1310 	shdr->sh_size = (Xword)size;
1311 
1312 	return (1);
1313 }
1314 
1315 /*
1316  * Generate the standard symbol table.  Contains all locals and globals,
1317  * and resides in a non-allocatable section (ie. it can be stripped).
1318  */
1319 static uintptr_t
1320 make_symtab(Ofl_desc *ofl)
1321 {
1322 	Shdr		*shdr;
1323 	Elf_Data	*data;
1324 	Is_desc		*isec;
1325 	Is_desc		*xisec = 0;
1326 	size_t		size;
1327 	Word		symcnt;
1328 
1329 	/*
1330 	 * Create the section headers. Note that we supply an ent_cnt
1331 	 * of 0. We won't know the count until the section has been placed.
1332 	 */
1333 	if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0,
1334 	    &isec, &shdr, &data) == S_ERROR)
1335 		return (S_ERROR);
1336 
1337 	/*
1338 	 * Place the section first since it will affect the local symbol
1339 	 * count.
1340 	 */
1341 	if ((ofl->ofl_ossymtab = ld_place_section(ofl, isec, M_ID_SYMTAB, 0)) ==
1342 	    (Os_desc *)S_ERROR)
1343 		return (S_ERROR);
1344 
1345 	/*
1346 	 * At this point we've created all but the 'shstrtab' section.
1347 	 * Determine if we have to use 'Extended Sections'.  If so - then
1348 	 * also create a SHT_SYMTAB_SHNDX section.
1349 	 */
1350 	if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
1351 		Shdr		*xshdr;
1352 		Elf_Data	*xdata;
1353 
1354 		if (new_section(ofl, SHT_SYMTAB_SHNDX,
1355 		    MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec,
1356 		    &xshdr, &xdata) == S_ERROR)
1357 			return (S_ERROR);
1358 
1359 		if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec,
1360 		    M_ID_SYMTAB_NDX, 0)) == (Os_desc *)S_ERROR)
1361 			return (S_ERROR);
1362 	}
1363 	/*
1364 	 * Calculated number of symbols, which need to be augmented by
1365 	 * the null first entry, the FILE symbol, and the .shstrtab entry.
1366 	 */
1367 	symcnt = (size_t)(3 + ofl->ofl_shdrcnt + ofl->ofl_scopecnt +
1368 	    ofl->ofl_locscnt + ofl->ofl_globcnt);
1369 	size = symcnt * shdr->sh_entsize;
1370 
1371 	/*
1372 	 * Finalize the section header and data buffer initialization.
1373 	 */
1374 	data->d_size = size;
1375 	shdr->sh_size = (Xword)size;
1376 
1377 	/*
1378 	 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
1379 	 */
1380 	if (xisec) {
1381 		size_t	xsize = symcnt * sizeof (Word);
1382 
1383 		xisec->is_indata->d_size = xsize;
1384 		xisec->is_shdr->sh_size = (Xword)xsize;
1385 	}
1386 
1387 	return (1);
1388 }
1389 
1390 
1391 /*
1392  * Build a dynamic symbol table. These tables reside in the text
1393  * segment of a dynamic executable or shared library.
1394  *
1395  *	.SUNW_ldynsym contains local function symbols
1396  *	.dynsym contains only globals symbols
1397  *
1398  * The two tables are created adjacent to each other, with .SUNW_ldynsym
1399  * coming first.
1400  */
1401 static uintptr_t
1402 make_dynsym(Ofl_desc *ofl)
1403 {
1404 	Shdr		*shdr, *lshdr;
1405 	Elf_Data	*data, *ldata;
1406 	Is_desc		*isec, *lisec;
1407 	size_t		size;
1408 	Xword		cnt;
1409 	int		allow_ldynsym;
1410 
1411 	/*
1412 	 * Unless explicitly disabled, always produce a .SUNW_ldynsym section
1413 	 * when it is allowed by the file type, even if the resulting
1414 	 * table only ends up with a single STT_FILE in it. There are
1415 	 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB
1416 	 * entry in the .dynamic section, which is something we would
1417 	 * like to encourage, and (2) Without it, we cannot generate
1418 	 * the associated .SUNW_dyn[sym|tls]sort sections, which are of
1419 	 * value to DTrace.
1420 	 *
1421 	 * In practice, it is extremely rare for an object not to have
1422 	 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be
1423 	 * doing it anyway.
1424 	 */
1425 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1426 
1427 	/*
1428 	 * Create the section headers. Note that we supply an ent_cnt
1429 	 * of 0. We won't know the count until the section has been placed.
1430 	 */
1431 	if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM,
1432 	    MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR)
1433 		return (S_ERROR);
1434 
1435 	if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0,
1436 	    &isec, &shdr, &data) == S_ERROR)
1437 		return (S_ERROR);
1438 
1439 	/*
1440 	 * Place the section(s) first since it will affect the local symbol
1441 	 * count.
1442 	 */
1443 	if (allow_ldynsym &&
1444 	    ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec,
1445 	    M_ID_LDYNSYM, 0)) == (Os_desc *)S_ERROR))
1446 		return (S_ERROR);
1447 	if ((ofl->ofl_osdynsym = ld_place_section(ofl, isec, M_ID_DYNSYM, 0))
1448 	    == (Os_desc *)S_ERROR)
1449 		return (S_ERROR);
1450 
1451 	/*
1452 	 * One extra section header entry for the 'null' entry.
1453 	 */
1454 	cnt = 1 + ofl->ofl_dynshdrcnt + ofl->ofl_globcnt + ofl->ofl_lregsymcnt;
1455 	size = (size_t)cnt * shdr->sh_entsize;
1456 
1457 	/*
1458 	 * Finalize the section header and data buffer initialization.
1459 	 */
1460 	data->d_size = size;
1461 	shdr->sh_size = (Xword)size;
1462 
1463 	/*
1464 	 * An ldynsym contains local function symbols. It is not
1465 	 * used for linking, but if present, serves to allow better
1466 	 * stack traces to be generated in contexts where the symtab
1467 	 * is not available. (dladdr(), or stripped executable/library files).
1468 	 */
1469 	if (allow_ldynsym) {
1470 		cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt;
1471 		size = (size_t)cnt * shdr->sh_entsize;
1472 
1473 		ldata->d_size = size;
1474 		lshdr->sh_size = (Xword)size;
1475 	}
1476 
1477 	return (1);
1478 }
1479 
1480 /*
1481  * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are
1482  * index sections for the .SUNW_ldynsym/.dynsym pair that present data
1483  * and function symbols sorted by address.
1484  */
1485 static uintptr_t
1486 make_dynsort(Ofl_desc *ofl)
1487 {
1488 	Shdr		*shdr;
1489 	Elf_Data	*data;
1490 	Is_desc		*isec;
1491 
1492 
1493 	/* Only do it if the .SUNW_ldynsym section is present */
1494 	if (!OFL_ALLOW_LDYNSYM(ofl))
1495 		return (1);
1496 
1497 	/* .SUNW_dynsymsort */
1498 	if (ofl->ofl_dynsymsortcnt > 0) {
1499 		if (new_section(ofl, SHT_SUNW_symsort,
1500 		    MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt,
1501 		    &isec, &shdr, &data) == S_ERROR)
1502 		return (S_ERROR);
1503 
1504 		if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec,
1505 		    M_ID_DYNSORT, 0)) == (Os_desc *)S_ERROR)
1506 			return (S_ERROR);
1507 	}
1508 
1509 	/* .SUNW_dyntlssort */
1510 	if (ofl->ofl_dyntlssortcnt > 0) {
1511 		if (new_section(ofl, SHT_SUNW_tlssort,
1512 		    MSG_ORIG(MSG_SCN_DYNTLSSORT),
1513 		    ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR)
1514 		return (S_ERROR);
1515 
1516 		if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec,
1517 		    M_ID_DYNSORT, 0)) == (Os_desc *)S_ERROR)
1518 			return (S_ERROR);
1519 	}
1520 
1521 	return (1);
1522 }
1523 
1524 /*
1525  * Helper routine for make_dynsym_shndx. Builds a
1526  * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing
1527  * which one it is.
1528  */
1529 static uintptr_t
1530 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab,
1531     Os_desc **ret_os)
1532 {
1533 	Is_desc		*isec;
1534 	Is_desc		*dynsymisp;
1535 	Shdr		*shdr, *dynshdr;
1536 	Elf_Data	*data;
1537 
1538 	dynsymisp = (Is_desc *)symtab->os_isdescs.head->data;
1539 	dynshdr = dynsymisp->is_shdr;
1540 
1541 	if (new_section(ofl, SHT_SYMTAB_SHNDX, shname,
1542 	    (dynshdr->sh_size / dynshdr->sh_entsize),
1543 	    &isec, &shdr, &data) == S_ERROR)
1544 		return (S_ERROR);
1545 
1546 	if ((*ret_os = ld_place_section(ofl, isec,
1547 	    M_ID_DYNSYM_NDX, 0)) == (Os_desc *)S_ERROR)
1548 		return (S_ERROR);
1549 
1550 	assert(*ret_os);
1551 
1552 	return (1);
1553 }
1554 
1555 /*
1556  * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym
1557  */
1558 static uintptr_t
1559 make_dynsym_shndx(Ofl_desc *ofl)
1560 {
1561 	/*
1562 	 * If there is a .SUNW_ldynsym, generate a section for its extended
1563 	 * index section as well.
1564 	 */
1565 	if (OFL_ALLOW_LDYNSYM(ofl)) {
1566 		if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX),
1567 		    ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR)
1568 			return (S_ERROR);
1569 	}
1570 
1571 	/* The Generate a section for the dynsym */
1572 	if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX),
1573 	    ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR)
1574 		return (S_ERROR);
1575 
1576 	return (1);
1577 }
1578 
1579 
1580 /*
1581  * Build a string table for the section headers.
1582  */
1583 static uintptr_t
1584 make_shstrtab(Ofl_desc *ofl)
1585 {
1586 	Shdr		*shdr;
1587 	Elf_Data	*data;
1588 	Is_desc		*isec;
1589 	size_t		size;
1590 
1591 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB),
1592 	    0, &isec, &shdr, &data) == S_ERROR)
1593 		return (S_ERROR);
1594 
1595 	/*
1596 	 * Place the section first, as it may effect the number of section
1597 	 * headers to account for.
1598 	 */
1599 	if ((ofl->ofl_osshstrtab = ld_place_section(ofl, isec, M_ID_NOTE, 0)) ==
1600 	    (Os_desc *)S_ERROR)
1601 		return (S_ERROR);
1602 
1603 	size = st_getstrtab_sz(ofl->ofl_shdrsttab);
1604 	assert(size > 0);
1605 
1606 	data->d_size = size;
1607 	shdr->sh_size = (Xword)size;
1608 
1609 	return (1);
1610 }
1611 
1612 /*
1613  * Build a string section for the standard symbol table.
1614  */
1615 static uintptr_t
1616 make_strtab(Ofl_desc *ofl)
1617 {
1618 	Shdr		*shdr;
1619 	Elf_Data	*data;
1620 	Is_desc		*isec;
1621 	size_t		size;
1622 
1623 	/*
1624 	 * This string table consists of all the global and local symbols.
1625 	 * Account for null bytes at end of the file name and the beginning
1626 	 * of section.
1627 	 */
1628 	if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
1629 		return (S_ERROR);
1630 
1631 	size = st_getstrtab_sz(ofl->ofl_strtab);
1632 	assert(size > 0);
1633 
1634 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB),
1635 	    0, &isec, &shdr, &data) == S_ERROR)
1636 		return (S_ERROR);
1637 
1638 	/* Set the size of the data area */
1639 	data->d_size = size;
1640 	shdr->sh_size = (Xword)size;
1641 
1642 	ofl->ofl_osstrtab = ld_place_section(ofl, isec, M_ID_STRTAB, 0);
1643 	return ((uintptr_t)ofl->ofl_osstrtab);
1644 }
1645 
1646 /*
1647  * Build a string table for the dynamic symbol table.
1648  */
1649 static uintptr_t
1650 make_dynstr(Ofl_desc *ofl)
1651 {
1652 	Shdr		*shdr;
1653 	Elf_Data	*data;
1654 	Is_desc		*isec;
1655 	size_t		size;
1656 
1657 	/*
1658 	 * If producing a .SUNW_ldynsym, account for the initial STT_FILE
1659 	 * symbol that precedes the scope reduced global symbols.
1660 	 */
1661 	if (OFL_ALLOW_LDYNSYM(ofl)) {
1662 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1)
1663 			return (S_ERROR);
1664 		ofl->ofl_dynscopecnt++;
1665 	}
1666 
1667 
1668 	/*
1669 	 * Account for any local, named register symbols.  These locals are
1670 	 * required for reference from DT_REGISTER .dynamic entries.
1671 	 */
1672 	if (ofl->ofl_regsyms) {
1673 		int	ndx;
1674 
1675 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1676 			Sym_desc *	sdp;
1677 
1678 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1679 				continue;
1680 
1681 			if (((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) &&
1682 			    (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
1683 				continue;
1684 
1685 			if (sdp->sd_sym->st_name == 0)
1686 				continue;
1687 
1688 			if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
1689 				return (S_ERROR);
1690 		}
1691 	}
1692 
1693 	/*
1694 	 * Reserve entries for any per-symbol auxiliary/filter strings.
1695 	 */
1696 	if (ofl->ofl_dtsfltrs) {
1697 		Dfltr_desc *	dftp;
1698 		Aliste		off;
1699 
1700 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp))
1701 			if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
1702 				return (S_ERROR);
1703 	}
1704 
1705 	size = st_getstrtab_sz(ofl->ofl_dynstrtab);
1706 	assert(size > 0);
1707 
1708 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR),
1709 	    0, &isec, &shdr, &data) == S_ERROR)
1710 		return (S_ERROR);
1711 
1712 	/* Make it allocable if necessary */
1713 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
1714 		shdr->sh_flags |= SHF_ALLOC;
1715 
1716 	/* Set the size of the data area */
1717 	data->d_size = size + DYNSTR_EXTRA_PAD;
1718 
1719 	shdr->sh_size = (Xword)size;
1720 
1721 	ofl->ofl_osdynstr = ld_place_section(ofl, isec, M_ID_DYNSTR, 0);
1722 	return ((uintptr_t)ofl->ofl_osdynstr);
1723 }
1724 
1725 /*
1726  * Generate an output relocation section which will contain the relocation
1727  * information to be applied to the `osp' section.
1728  *
1729  * If (osp == NULL) then we are creating the coalesced relocation section
1730  * for an executable and/or a shared object.
1731  */
1732 static uintptr_t
1733 make_reloc(Ofl_desc *ofl, Os_desc *osp)
1734 {
1735 	Shdr		*shdr;
1736 	Elf_Data	*data;
1737 	Is_desc		*isec;
1738 	size_t		size;
1739 	Xword		sh_flags;
1740 	char 		*sectname;
1741 	Os_desc		*rosp;
1742 	Word		relsize;
1743 	const char	*rel_prefix;
1744 
1745 	/* LINTED */
1746 	if (M_REL_SHT_TYPE == SHT_REL) {
1747 		/* REL */
1748 		relsize = sizeof (Rel);
1749 		rel_prefix = MSG_ORIG(MSG_SCN_REL);
1750 	} else {
1751 		/* RELA */
1752 		relsize = sizeof (Rela);
1753 		rel_prefix = MSG_ORIG(MSG_SCN_RELA);
1754 	}
1755 
1756 	if (osp) {
1757 		size = osp->os_szoutrels;
1758 		sh_flags = osp->os_shdr->sh_flags;
1759 		if ((sectname = libld_malloc(strlen(rel_prefix) +
1760 		    strlen(osp->os_name) + 1)) == 0)
1761 			return (S_ERROR);
1762 		(void) strcpy(sectname, rel_prefix);
1763 		(void) strcat(sectname, osp->os_name);
1764 	} else if (ofl->ofl_flags & FLG_OF_COMREL) {
1765 		size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
1766 		sh_flags = SHF_ALLOC;
1767 		sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
1768 	} else {
1769 		size = ofl->ofl_relocrelsz;
1770 		sh_flags = SHF_ALLOC;
1771 		sectname = (char *)rel_prefix;
1772 	}
1773 
1774 	/*
1775 	 * Keep track of total size of 'output relocations' (to be stored
1776 	 * in .dynamic)
1777 	 */
1778 	/* LINTED */
1779 	ofl->ofl_relocsz += (Xword)size;
1780 
1781 	if (new_section(ofl, M_REL_SHT_TYPE, sectname, 0, &isec, &shdr, &data)
1782 	    == S_ERROR)
1783 		return (S_ERROR);
1784 
1785 	data->d_size = size;
1786 
1787 	shdr->sh_size = (Xword)size;
1788 	if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC))
1789 		shdr->sh_flags = SHF_ALLOC;
1790 
1791 	if (osp) {
1792 		/*
1793 		 * The sh_info field of the SHT_REL* sections points to the
1794 		 * section the relocations are to be applied to.
1795 		 */
1796 		shdr->sh_flags |= SHF_INFO_LINK;
1797 	}
1798 
1799 	/*
1800 	 * Associate this relocation section to the section its going to
1801 	 * relocate.
1802 	 */
1803 	if ((rosp = ld_place_section(ofl, isec, M_ID_REL, 0)) ==
1804 	    (Os_desc *)S_ERROR)
1805 		return (S_ERROR);
1806 
1807 	if (osp) {
1808 		Listnode	*lnp;
1809 		Is_desc		*risp;
1810 
1811 		/*
1812 		 * We associate the input relocation sections - with
1813 		 * the newly created output relocation section.
1814 		 *
1815 		 * This is used primarily so that we can update
1816 		 * SHT_GROUP[sect_no] entries to point to the
1817 		 * created output relocation sections.
1818 		 */
1819 		for (LIST_TRAVERSE(&(osp->os_relisdescs), lnp, risp)) {
1820 			risp->is_osdesc = rosp;
1821 
1822 			/*
1823 			 * If the input relocation section had the SHF_GROUP
1824 			 * flag set - propagate it to the output relocation
1825 			 * section.
1826 			 */
1827 			if (risp->is_shdr->sh_flags & SHF_GROUP) {
1828 				rosp->os_shdr->sh_flags |= SHF_GROUP;
1829 				break;
1830 			}
1831 		}
1832 		osp->os_relosdesc = rosp;
1833 	} else
1834 		ofl->ofl_osrel = rosp;
1835 
1836 	/*
1837 	 * If this is the first relocation section we've encountered save it
1838 	 * so that the .dynamic entry can be initialized accordingly.
1839 	 */
1840 	if (ofl->ofl_osrelhead == (Os_desc *)0)
1841 		ofl->ofl_osrelhead = rosp;
1842 
1843 	return (1);
1844 }
1845 
1846 /*
1847  * Generate version needed section.
1848  */
1849 static uintptr_t
1850 make_verneed(Ofl_desc *ofl)
1851 {
1852 	Shdr		*shdr;
1853 	Elf_Data	*data;
1854 	Is_desc		*isec;
1855 
1856 	/*
1857 	 * verneed sections do not have a constant element size, so the
1858 	 * value of ent_cnt specified here (0) is meaningless.
1859 	 */
1860 	if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION),
1861 	    0, &isec, &shdr, &data) == S_ERROR)
1862 		return (S_ERROR);
1863 
1864 	/* During version processing we calculated the total size. */
1865 	data->d_size = ofl->ofl_verneedsz;
1866 	shdr->sh_size = (Xword)ofl->ofl_verneedsz;
1867 
1868 	ofl->ofl_osverneed = ld_place_section(ofl, isec, M_ID_VERSION, 0);
1869 	return ((uintptr_t)ofl->ofl_osverneed);
1870 }
1871 
1872 /*
1873  * Generate a version definition section.
1874  *
1875  *  o	the SHT_SUNW_verdef section defines the versions that exist within this
1876  *	image.
1877  */
1878 static uintptr_t
1879 make_verdef(Ofl_desc *ofl)
1880 {
1881 	Shdr		*shdr;
1882 	Elf_Data	*data;
1883 	Is_desc		*isec;
1884 	Ver_desc	*vdp;
1885 
1886 	/*
1887 	 * Reserve a string table entry for the base version dependency (other
1888 	 * dependencies have symbol representations, which will already be
1889 	 * accounted for during symbol processing).
1890 	 */
1891 	vdp = (Ver_desc *)ofl->ofl_verdesc.head->data;
1892 
1893 	if (ofl->ofl_flags & FLG_OF_DYNAMIC) {
1894 		if (st_insert(ofl->ofl_dynstrtab, vdp->vd_name) == -1)
1895 			return (S_ERROR);
1896 	} else {
1897 		if (st_insert(ofl->ofl_strtab, vdp->vd_name) == -1)
1898 			return (S_ERROR);
1899 	}
1900 
1901 	/*
1902 	 * verdef sections do not have a constant element size, so the
1903 	 * value of ent_cnt specified here (0) is meaningless.
1904 	 */
1905 	if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION),
1906 	    0, &isec, &shdr, &data) == S_ERROR)
1907 		return (S_ERROR);
1908 
1909 	/* During version processing we calculated the total size. */
1910 	data->d_size = ofl->ofl_verdefsz;
1911 	shdr->sh_size = (Xword)ofl->ofl_verdefsz;
1912 
1913 	ofl->ofl_osverdef = ld_place_section(ofl, isec, M_ID_VERSION, 0);
1914 	return ((uintptr_t)ofl->ofl_osverdef);
1915 }
1916 
1917 /*
1918  * Common function used to build both the SHT_SUNW_versym
1919  * section and the SHT_SUNW_syminfo section.  Each of these sections
1920  * provides additional symbol information.
1921  */
1922 static Os_desc *
1923 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident)
1924 {
1925 	Shdr		*shdr;
1926 	Elf_Data	*data;
1927 	Is_desc		*isec;
1928 
1929 	/*
1930 	 * We don't know the size of this section yet, so set it to 0.
1931 	 * It gets filled in after the dynsym is sized.
1932 	 */
1933 	if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) ==
1934 	    S_ERROR)
1935 		return ((Os_desc *)S_ERROR);
1936 
1937 	return (ld_place_section(ofl, isec, ident, 0));
1938 }
1939 
1940 /*
1941  * Build a .sunwbss section for allocation of tentative definitions.
1942  */
1943 uintptr_t
1944 ld_make_sunwbss(Ofl_desc *ofl, size_t size, Xword align)
1945 {
1946 	Shdr		*shdr;
1947 	Elf_Data	*data;
1948 	Is_desc		*isec;
1949 
1950 	/*
1951 	 * Allocate header structs. We will set the name ourselves below,
1952 	 * and there is no entcnt for a BSS. So, the shname and entcnt
1953 	 * arguments are 0.
1954 	 */
1955 	if (new_section(ofl, SHT_NOBITS, MSG_ORIG(MSG_SCN_SUNWBSS), 0,
1956 	    &isec, &shdr, &data) == S_ERROR)
1957 		return (S_ERROR);
1958 
1959 	data->d_size = size;
1960 	data->d_align = align;
1961 
1962 	shdr->sh_size = (Xword)size;
1963 	shdr->sh_addralign = align;
1964 
1965 	/*
1966 	 * Retain this .sunwbss input section as this will be where global
1967 	 * symbol references are added.
1968 	 */
1969 	ofl->ofl_issunwbss = isec;
1970 	if (ld_place_section(ofl, isec, 0, 0) == (Os_desc *)S_ERROR)
1971 		return (S_ERROR);
1972 
1973 	return (1);
1974 }
1975 
1976 /*
1977  * This routine is called when -z nopartial is in effect.
1978  */
1979 uintptr_t
1980 ld_make_sunwdata(Ofl_desc *ofl, size_t size, Xword align)
1981 {
1982 	Shdr		*shdr;
1983 	Elf_Data	*data;
1984 	Is_desc		*isec;
1985 	Os_desc		*osp;
1986 
1987 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_SUNWDATA1), 0,
1988 	    &isec, &shdr, &data) == S_ERROR)
1989 		return (S_ERROR);
1990 
1991 	shdr->sh_flags |= SHF_WRITE;
1992 	data->d_size = size;
1993 	shdr->sh_size = (Xword)size;
1994 	if (align != 0) {
1995 		data->d_align = align;
1996 		shdr->sh_addralign = align;
1997 	}
1998 
1999 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2000 		return (S_ERROR);
2001 
2002 	/*
2003 	 * Retain this .sunwdata1 input section as this will
2004 	 * be where global
2005 	 * symbol references are added.
2006 	 */
2007 	ofl->ofl_issunwdata1 = isec;
2008 	if ((osp = ld_place_section(ofl, isec, M_ID_DATA, 0)) ==
2009 	    (Os_desc *)S_ERROR)
2010 		return (S_ERROR);
2011 
2012 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
2013 		ofl->ofl_dynshdrcnt++;
2014 		osp->os_flags |= FLG_OS_OUTREL;
2015 	}
2016 	return (1);
2017 }
2018 
2019 /*
2020  * Make .sunwmove section
2021  */
2022 uintptr_t
2023 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2024 {
2025 	Shdr		*shdr;
2026 	Elf_Data	*data;
2027 	Is_desc		*isec;
2028 	Listnode	*lnp1;
2029 	Psym_info	*psym;
2030 	int 		cnt = 1;
2031 
2032 
2033 	if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE),
2034 	    mv_nums, &isec, &shdr, &data) == S_ERROR)
2035 		return (S_ERROR);
2036 
2037 	if ((data->d_buf = libld_calloc(data->d_size, 1)) == 0)
2038 		return (S_ERROR);
2039 
2040 	/*
2041 	 * Copy move entries
2042 	 */
2043 	for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) {
2044 		Listnode *	lnp2;
2045 		Mv_itm *	mvitm;
2046 
2047 		if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN)
2048 			continue;
2049 		for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvitm)) {
2050 			if ((mvitm->mv_flag & FLG_MV_OUTSECT) == 0)
2051 				continue;
2052 			mvitm->mv_oidx = cnt;
2053 			cnt++;
2054 		}
2055 	}
2056 	if ((ofl->ofl_osmove = ld_place_section(ofl, isec, 0, 0)) ==
2057 	    (Os_desc *)S_ERROR)
2058 		return (S_ERROR);
2059 
2060 	return (1);
2061 }
2062 
2063 
2064 /*
2065  * The following sections are built after all input file processing and symbol
2066  * validation has been carried out.  The order is important (because the
2067  * addition of a section adds a new symbol there is a chicken and egg problem
2068  * of maintaining the appropriate counts).  By maintaining a known order the
2069  * individual routines can compensate for later, known, additions.
2070  */
2071 uintptr_t
2072 ld_make_sections(Ofl_desc *ofl)
2073 {
2074 	Word		flags = ofl->ofl_flags;
2075 	Listnode	*lnp1;
2076 	Sg_desc		*sgp;
2077 
2078 	/*
2079 	 * Generate any special sections.
2080 	 */
2081 	if (flags & FLG_OF_ADDVERS)
2082 		if (make_comment(ofl) == S_ERROR)
2083 			return (S_ERROR);
2084 
2085 	if (make_interp(ofl) == S_ERROR)
2086 		return (S_ERROR);
2087 
2088 	if (make_cap(ofl) == S_ERROR)
2089 		return (S_ERROR);
2090 
2091 	if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
2092 	    &ofl->ofl_initarray) == S_ERROR)
2093 		return (S_ERROR);
2094 
2095 	if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
2096 	    &ofl->ofl_finiarray) == S_ERROR)
2097 		return (S_ERROR);
2098 
2099 	if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
2100 	    &ofl->ofl_preiarray) == S_ERROR)
2101 		return (S_ERROR);
2102 
2103 	/*
2104 	 * Make the .plt section.  This occurs after any other relocation
2105 	 * sections are generated (see reloc_init()) to ensure that the
2106 	 * associated relocation section is after all the other relocation
2107 	 * sections.
2108 	 */
2109 	if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
2110 		if (make_plt(ofl) == S_ERROR)
2111 			return (S_ERROR);
2112 
2113 	/*
2114 	 * Determine whether any sections or files are not referenced.  Under
2115 	 * -Dunused a diagnostic for any unused components is generated, under
2116 	 * -zignore the component is removed from the final output.
2117 	 */
2118 	if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
2119 		if (ignore_section_processing(ofl) == S_ERROR)
2120 			return (S_ERROR);
2121 	}
2122 
2123 	/*
2124 	 * Add any necessary versioning information.
2125 	 */
2126 	if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED) {
2127 		if (make_verneed(ofl) == S_ERROR)
2128 			return (S_ERROR);
2129 	}
2130 	if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == FLG_OF_VERDEF) {
2131 		if (make_verdef(ofl) == S_ERROR)
2132 			return (S_ERROR);
2133 		if ((ofl->ofl_osversym = make_sym_sec(ofl,
2134 		    MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym,
2135 		    M_ID_VERSION)) == (Os_desc*)S_ERROR)
2136 			return (S_ERROR);
2137 	}
2138 
2139 	/*
2140 	 * Create a syminfo section if necessary.
2141 	 */
2142 	if (ofl->ofl_flags & FLG_OF_SYMINFO) {
2143 		if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
2144 		    MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo,
2145 		    M_ID_SYMINFO)) == (Os_desc *)S_ERROR)
2146 			return (S_ERROR);
2147 	}
2148 
2149 	if (ofl->ofl_flags & FLG_OF_COMREL) {
2150 		/*
2151 		 * If -zcombreloc is enabled then all relocations (except for
2152 		 * the PLT's) are coalesced into a single relocation section.
2153 		 */
2154 		if (ofl->ofl_reloccnt) {
2155 			if (make_reloc(ofl, NULL) == S_ERROR)
2156 				return (S_ERROR);
2157 		}
2158 	} else {
2159 		/*
2160 		 * Create the required output relocation sections.  Note, new
2161 		 * sections may be added to the section list that is being
2162 		 * traversed.  These insertions can move the elements of the
2163 		 * Alist such that a section descriptor is re-read.  Recursion
2164 		 * is prevented by maintaining a previous section pointer and
2165 		 * insuring that this pointer isn't re-examined.
2166 		 */
2167 		for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2168 			Os_desc	**ospp, *posp = 0;
2169 			Aliste	off;
2170 
2171 			for (ALIST_TRAVERSE(sgp->sg_osdescs, off, ospp)) {
2172 				Os_desc	*osp = *ospp;
2173 
2174 				if ((osp != posp) && osp->os_szoutrels &&
2175 				    (osp != ofl->ofl_osplt)) {
2176 					if (make_reloc(ofl, osp) == S_ERROR)
2177 						return (S_ERROR);
2178 				}
2179 				posp = osp;
2180 			}
2181 		}
2182 
2183 		/*
2184 		 * If we're not building a combined relocation section, then
2185 		 * build a .rel[a] section as required.
2186 		 */
2187 		if (ofl->ofl_relocrelsz) {
2188 			if (make_reloc(ofl, NULL) == S_ERROR)
2189 				return (S_ERROR);
2190 		}
2191 	}
2192 
2193 	/*
2194 	 * The PLT relocations are always in their own section, and we try to
2195 	 * keep them at the end of the PLT table.  We do this to keep the hot
2196 	 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
2197 	 */
2198 	if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
2199 		if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
2200 			return (S_ERROR);
2201 	}
2202 
2203 	/*
2204 	 * Finally build the symbol and section header sections.
2205 	 */
2206 	if (flags & FLG_OF_DYNAMIC) {
2207 		if (make_dynamic(ofl) == S_ERROR)
2208 			return (S_ERROR);
2209 		if (make_dynstr(ofl) == S_ERROR)
2210 			return (S_ERROR);
2211 		/*
2212 		 * There is no use for .hash and .dynsym sections in a
2213 		 * relocatable object.
2214 		 */
2215 		if (!(flags & FLG_OF_RELOBJ)) {
2216 			if (make_hash(ofl) == S_ERROR)
2217 				return (S_ERROR);
2218 			if (make_dynsym(ofl) == S_ERROR)
2219 				return (S_ERROR);
2220 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
2221 			if (make_amd64_unwindhdr(ofl) == S_ERROR)
2222 				return (S_ERROR);
2223 #endif
2224 			if (make_dynsort(ofl) == S_ERROR)
2225 				return (S_ERROR);
2226 		}
2227 	}
2228 
2229 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
2230 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
2231 		/*
2232 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2233 		 * for the dynsym.  If so - do it now.
2234 		 */
2235 		if (ofl->ofl_osdynsym &&
2236 		    ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
2237 			if (make_dynsym_shndx(ofl) == S_ERROR)
2238 				return (S_ERROR);
2239 		}
2240 
2241 		if (make_strtab(ofl) == S_ERROR)
2242 			return (S_ERROR);
2243 		if (make_symtab(ofl) == S_ERROR)
2244 			return (S_ERROR);
2245 	} else {
2246 		/*
2247 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2248 		 * for the dynsym.  If so - do it now.
2249 		 */
2250 		if (ofl->ofl_osdynsym &&
2251 		    ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
2252 			if (make_dynsym_shndx(ofl) == S_ERROR)
2253 				return (S_ERROR);
2254 		}
2255 	}
2256 
2257 	if (make_shstrtab(ofl) == S_ERROR)
2258 		return (S_ERROR);
2259 
2260 	/*
2261 	 * Now that we've created all of our sections adjust the size
2262 	 * of SHT_SUNW_versym & SHT_SUNW_syminfo which are dependent on
2263 	 * the symbol table sizes.
2264 	 */
2265 	if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
2266 		Shdr		*shdr;
2267 		Is_desc		*isec;
2268 		Elf_Data	*data;
2269 		size_t		size;
2270 		ulong_t		cnt;
2271 		Os_desc		*osp;
2272 
2273 		if (flags & (FLG_OF_RELOBJ | FLG_OF_STATIC)) {
2274 			osp = ofl->ofl_ossymtab;
2275 		} else {
2276 			osp = ofl->ofl_osdynsym;
2277 		}
2278 		isec = (Is_desc *)osp->os_isdescs.head->data;
2279 		cnt = (isec->is_shdr->sh_size / isec->is_shdr->sh_entsize);
2280 
2281 		if (ofl->ofl_osversym) {
2282 			osp = ofl->ofl_osversym;
2283 			isec = (Is_desc *)osp->os_isdescs.head->data;
2284 			data = isec->is_indata;
2285 			shdr = osp->os_shdr;
2286 			size = cnt * shdr->sh_entsize;
2287 			shdr->sh_size = (Xword)size;
2288 			data->d_size = size;
2289 		}
2290 		if (ofl->ofl_ossyminfo) {
2291 			osp = ofl->ofl_ossyminfo;
2292 			isec = (Is_desc *)osp->os_isdescs.head->data;
2293 			data = isec->is_indata;
2294 			shdr = osp->os_shdr;
2295 			size = cnt * shdr->sh_entsize;
2296 			shdr->sh_size = (Xword)size;
2297 			data->d_size = size;
2298 		}
2299 	}
2300 
2301 	return (1);
2302 }
2303 
2304 /*
2305  * Build an additional data section - used to back OBJT symbol definitions
2306  * added with a mapfile.
2307  */
2308 Is_desc *
2309 ld_make_data(Ofl_desc *ofl, size_t size)
2310 {
2311 	Shdr		*shdr;
2312 	Elf_Data	*data;
2313 	Is_desc		*isec;
2314 
2315 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2316 	    &isec, &shdr, &data) == S_ERROR)
2317 		return ((Is_desc *)S_ERROR);
2318 
2319 	data->d_size = size;
2320 	shdr->sh_size = (Xword)size;
2321 	shdr->sh_flags |= SHF_WRITE;
2322 
2323 	if (ld_place_section(ofl, isec, M_ID_DATA, 0) == (Os_desc *)S_ERROR)
2324 		return ((Is_desc *)S_ERROR);
2325 
2326 	return (isec);
2327 }
2328 
2329 /*
2330  * Define a set of templates for generating "void (*)(void)" function
2331  * definitions.
2332  */
2333 #if	defined(__i386) || defined(__amd64)
2334 #if	defined(__lint)
2335 static const uchar_t ret_template[] = { 0 };
2336 #else	/* __lint */
2337 #if	defined(_ELF64)
2338 #define	ret_template	ret64_template
2339 #else
2340 #define	ret_template	ret32_template
2341 #endif
2342 
2343 static const uchar_t ret32_template[] = {
2344 /* 0x00 */	0xc3				/* ret */
2345 };
2346 
2347 static const uchar_t ret64_template[] = {
2348 /* 0x00 */	0x55,				/* pushq  %rbp */
2349 /* 0x01 */	0x48, 0x8b, 0xec,		/* movq   %rsp,%rbp */
2350 /* 0x04 */	0x48, 0x8b, 0xe5,		/* movq   %rbp,%rsp */
2351 /* 0x07 */	0x5d,				/* popq   %rbp */
2352 /* 0x08 */	0xc3				/* ret */
2353 };
2354 #endif	/* __lint */
2355 
2356 #elif	defined(__sparc)
2357 static const uchar_t ret_template[] = {
2358 /* 0x00 */	0x81, 0xc3, 0xe0, 0x08,		/* retl */
2359 /* 0x04 */	0x01, 0x00, 0x00, 0x00		/* nop */
2360 };
2361 #else
2362 #error	unsupported architecture!
2363 #endif
2364 
2365 /*
2366  * Build an additional text section - used to back FUNC symbol definitions
2367  * added with a mapfile.
2368  */
2369 Is_desc *
2370 ld_make_text(Ofl_desc *ofl, size_t size)
2371 {
2372 	Shdr		*shdr;
2373 	Elf_Data	*data;
2374 	Is_desc		*isec;
2375 
2376 	/*
2377 	 * Insure the size is sufficient to contain the minimum return
2378 	 * instruction.
2379 	 */
2380 	if (size < sizeof (ret_template))
2381 		size = sizeof (ret_template);
2382 
2383 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0,
2384 	    &isec, &shdr, &data) == S_ERROR)
2385 		return ((Is_desc *)S_ERROR);
2386 
2387 	data->d_size = size;
2388 	shdr->sh_size = (Xword)size;
2389 	shdr->sh_flags |= SHF_EXECINSTR;
2390 
2391 	/* Fill the buffer with the appropriate return instruction. */
2392 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2393 		return ((Is_desc *)S_ERROR);
2394 	(void) memcpy(data->d_buf, ret_template, sizeof (ret_template));
2395 
2396 	if (ld_place_section(ofl, isec, M_ID_TEXT, 0) == (Os_desc *)S_ERROR)
2397 		return ((Is_desc *)S_ERROR);
2398 
2399 	return (isec);
2400 }
2401