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