xref: /illumos-gate/usr/src/cmd/sgs/libld/common/sections.c (revision 8d4e547db823a866b8f73efc0acdc423e2963caf)
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(__x86) && 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 	uintptr_t	ret = 1;
634 
635 	if (list->head == NULL)
636 		return (1);
637 
638 	entcount = 0;
639 	for (LIST_TRAVERSE(list, lnp, sdp))
640 		entcount++;
641 
642 	if (new_section(ofl, shtype, sectname, entcount, &isec, &shdr, &data) ==
643 	    S_ERROR)
644 		return (S_ERROR);
645 
646 	if ((data->d_buf = libld_calloc(sizeof (Addr), entcount)) == 0)
647 		return (S_ERROR);
648 
649 	if (ld_place_section(ofl, isec, M_ID_ARRAY, 0) == (Os_desc *)S_ERROR)
650 		return (S_ERROR);
651 
652 	osp = isec->is_osdesc;
653 
654 	if ((ofl->ofl_osinitarray == 0) && (shtype == SHT_INIT_ARRAY))
655 		ofl->ofl_osinitarray = osp;
656 	if ((ofl->ofl_ospreinitarray == 0) && (shtype == SHT_PREINIT_ARRAY))
657 		ofl->ofl_ospreinitarray = osp;
658 	else if ((ofl->ofl_osfiniarray == 0) && (shtype == SHT_FINI_ARRAY))
659 		ofl->ofl_osfiniarray = osp;
660 
661 	/*
662 	 * Create relocations against this section to initialize it to the
663 	 * function addresses.
664 	 */
665 	reld.rel_osdesc = osp;
666 	reld.rel_isdesc = isec;
667 	reld.rel_move = 0;
668 	reld.rel_flags = FLG_REL_LOAD;
669 
670 	/*
671 	 * Fabricate the relocation information (as if a relocation record had
672 	 * been input - see init_rel()).
673 	 */
674 	reld.rel_rtype = M_R_ARRAYADDR;
675 	reld.rel_roffset = 0;
676 	reld.rel_raddend = 0;
677 	reld.rel_typedata = 0;
678 
679 	/*
680 	 * Create a minimal relocation record to satisfy process_sym_reloc()
681 	 * debugging requirements.
682 	 */
683 	reloc.r_offset = 0;
684 	reloc.r_info = ELF_R_INFO(0, M_R_ARRAYADDR);
685 	reloc.r_addend = 0;
686 
687 	DBG_CALL(Dbg_reloc_generate(ofl->ofl_lml, osp, M_REL_SHT_TYPE));
688 	for (LIST_TRAVERSE(list, lnp, sdp)) {
689 		reld.rel_sname = sdp->sd_name;
690 		reld.rel_sym = sdp;
691 
692 		if (ld_process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec,
693 		    MSG_INTL(MSG_STR_COMMAND)) == S_ERROR) {
694 			ret = S_ERROR;
695 			continue;
696 		}
697 
698 		reld.rel_roffset += (Xword)sizeof (Addr);
699 		reloc.r_offset = reld.rel_roffset;
700 	}
701 
702 	return (ret);
703 }
704 
705 /*
706  * Build a comment section (-Qy option).
707  */
708 static uintptr_t
709 make_comment(Ofl_desc *ofl)
710 {
711 	Shdr		*shdr;
712 	Elf_Data	*data;
713 	Is_desc		*isec;
714 
715 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_COMMENT), 0,
716 	    &isec, &shdr, &data) == S_ERROR)
717 		return (S_ERROR);
718 
719 	data->d_buf = (void *)ofl->ofl_sgsid;
720 	data->d_size = strlen(ofl->ofl_sgsid) + 1;
721 	data->d_align = 1;
722 
723 	shdr->sh_size = (Xword)data->d_size;
724 	shdr->sh_flags = 0;
725 	shdr->sh_addralign = 1;
726 
727 	return ((uintptr_t)ld_place_section(ofl, isec, M_ID_NOTE, 0));
728 }
729 
730 /*
731  * Make the dynamic section.  Calculate the size of any strings referenced
732  * within this structure, they will be added to the global string table
733  * (.dynstr).  This routine should be called before make_dynstr().
734  */
735 static uintptr_t
736 make_dynamic(Ofl_desc *ofl)
737 {
738 	Shdr		*shdr;
739 	Os_desc		*osp;
740 	Elf_Data	*data;
741 	Is_desc		*isec;
742 	size_t		cnt = 0;
743 	Listnode	*lnp;
744 	Ifl_desc	*ifl;
745 	Sym_desc	*sdp;
746 	size_t		size;
747 	Word		flags = ofl->ofl_flags;
748 	int		unused = 0;
749 
750 	if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0,
751 	    &isec, &shdr, &data) == S_ERROR)
752 		return (S_ERROR);
753 
754 	/* new_section() does not set SHF_ALLOC. Add it if needed */
755 	if (!(flags & FLG_OF_RELOBJ))
756 		shdr->sh_flags |= SHF_ALLOC;
757 
758 	osp = ofl->ofl_osdynamic = ld_place_section(ofl, isec, M_ID_DYNAMIC, 0);
759 
760 	/*
761 	 * Reserve entries for any needed dependencies.
762 	 */
763 	for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, ifl)) {
764 		Sdf_desc *	sdf;
765 
766 		if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR)))
767 			continue;
768 
769 		/*
770 		 * If this dependency didn't satisfy any symbol references,
771 		 * generate a debugging diagnostic (ld(1) -Dunused can be used
772 		 * to display these).  If this is a standard needed dependency,
773 		 * and -z ignore is in effect, drop the dependency.  Explicitly
774 		 * defined dependencies (i.e., -N dep) don't get dropped, and
775 		 * are flagged as being required to simplify update_odynamic()
776 		 * processing.
777 		 */
778 		if ((ifl->ifl_flags & FLG_IF_NEEDSTR) ||
779 		    ((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) {
780 			if (unused++ == 0)
781 				DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
782 			DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname,
783 			    (ifl->ifl_flags & FLG_IF_NEEDSTR), 0));
784 
785 			if (ifl->ifl_flags & FLG_IF_NEEDSTR)
786 				ifl->ifl_flags |= FLG_IF_DEPREQD;
787 			else if (ifl->ifl_flags & FLG_IF_IGNORE)
788 				continue;
789 		}
790 
791 		/*
792 		 * If this object has an accompanying shared object definition
793 		 * determine if an alternative shared object name has been
794 		 * specified.
795 		 */
796 		if (((sdf = ifl->ifl_sdfdesc) != 0) &&
797 		    (sdf->sdf_flags & FLG_SDF_SONAME))
798 			ifl->ifl_soname = sdf->sdf_soname;
799 
800 		/*
801 		 * If this object is a lazyload reserve a DT_POSFLAG1 entry.
802 		 */
803 		if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_GRPPRM))
804 			cnt++;
805 
806 		if (st_insert(ofl->ofl_dynstrtab, ifl->ifl_soname) == -1)
807 			return (S_ERROR);
808 		cnt++;
809 
810 		/*
811 		 * If the needed entry contains the $ORIGIN token make sure
812 		 * the associated DT_1_FLAGS entry is created.
813 		 */
814 		if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) {
815 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
816 			ofl->ofl_dtflags |= DF_ORIGIN;
817 		}
818 	}
819 
820 	if (unused)
821 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
822 
823 	/*
824 	 * Reserve entries for any per-symbol auxiliary/filter strings.
825 	 */
826 	if (ofl->ofl_dtsfltrs) {
827 		/* LINTED */
828 		Dfltr_desc *	dftp;
829 		Aliste		off;
830 
831 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp))
832 			cnt++;
833 	}
834 
835 	/*
836 	 * Reserve entries for any _init() and _fini() section addresses.
837 	 */
838 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
839 	    SYM_NOHASH, 0, ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED)) {
840 		sdp->sd_flags |= FLG_SY_UPREQD;
841 		cnt++;
842 	}
843 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
844 	    SYM_NOHASH, 0, ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED)) {
845 		sdp->sd_flags |= FLG_SY_UPREQD;
846 		cnt++;
847 	}
848 
849 	/*
850 	 * Reserve entries for any soname, filter name (shared libs only),
851 	 * run-path pointers, cache names and audit requirements..
852 	 */
853 	if (ofl->ofl_soname) {
854 		cnt++;
855 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_soname) == -1)
856 			return (S_ERROR);
857 	}
858 	if (ofl->ofl_filtees) {
859 		cnt++;
860 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_filtees) == -1)
861 			return (S_ERROR);
862 
863 		/*
864 		 * If the filtees entry contains the $ORIGIN token make sure
865 		 * the associated DT_1_FLAGS entry is created.
866 		 */
867 		if (strstr(ofl->ofl_filtees, MSG_ORIG(MSG_STR_ORIGIN))) {
868 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
869 			ofl->ofl_dtflags |= DF_ORIGIN;
870 		}
871 	}
872 	if (ofl->ofl_rpath) {
873 		cnt += 2;	/* DT_RPATH & DT_RUNPATH */
874 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_rpath) == -1)
875 			return (S_ERROR);
876 
877 		/*
878 		 * If the rpath entry contains the $ORIGIN token make sure
879 		 * the associated DT_1_FLAGS entry is created.
880 		 */
881 		if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) {
882 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
883 			ofl->ofl_dtflags |= DF_ORIGIN;
884 		}
885 	}
886 	if (ofl->ofl_config) {
887 		cnt++;
888 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_config) == -1)
889 			return (S_ERROR);
890 
891 		/*
892 		 * If the config entry contains the $ORIGIN token make sure
893 		 * the associated DT_1_FLAGS entry is created.
894 		 */
895 		if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) {
896 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
897 			ofl->ofl_dtflags |= DF_ORIGIN;
898 		}
899 	}
900 	if (ofl->ofl_depaudit) {
901 		cnt++;
902 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_depaudit) == -1)
903 			return (S_ERROR);
904 	}
905 	if (ofl->ofl_audit) {
906 		cnt++;
907 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_audit) == -1)
908 			return (S_ERROR);
909 	}
910 
911 
912 	/*
913 	 * The following DT_* entries do not apply to relocatable objects
914 	 */
915 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
916 		/*
917 		 * Reserve entries for the HASH, STRTAB, STRSZ, SYMTAB, SYMENT,
918 		 * and CHECKSUM.
919 		 */
920 		cnt += 6;
921 
922 		/*
923 		 * If we are including local functions at the head of
924 		 * the dynsym, then also reserve entries for DT_SUNW_SYMTAB
925 		 * and DT_SUNW_SYMSZ.
926 		 */
927 		if (OFL_ALLOW_LDYNSYM(ofl))
928 			cnt += 2;
929 
930 		if ((ofl->ofl_dynsymsortcnt > 0) ||
931 		    (ofl->ofl_dyntlssortcnt > 0))
932 			cnt++;		/* DT_SUNW_SORTENT */
933 
934 		if (ofl->ofl_dynsymsortcnt > 0)
935 			cnt += 2;	/* DT_SUNW_[SYMSORT|SYMSORTSZ] */
936 
937 		if (ofl->ofl_dyntlssortcnt > 0)
938 			cnt += 2;	/* DT_SUNW_[TLSSORT|TLSSORTSZ] */
939 
940 		if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
941 		    FLG_OF_VERDEF)
942 			cnt += 2;		/* DT_VERDEF & DT_VERDEFNUM */
943 
944 		if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
945 		    FLG_OF_VERNEED)
946 			cnt += 2;		/* DT_VERNEED & DT_VERNEEDNUM */
947 
948 		if ((ofl->ofl_flags1 & FLG_OF1_RELCNT) &&
949 		    ofl->ofl_relocrelcnt)	/* RELACOUNT */
950 			cnt++;
951 
952 		if (flags & FLG_OF_TEXTREL)	/* TEXTREL */
953 			cnt++;
954 
955 		if (ofl->ofl_osfiniarray)	/* FINI_ARRAY & FINI_ARRAYSZ */
956 			cnt += 2;
957 
958 		if (ofl->ofl_osinitarray)	/* INIT_ARRAY & INIT_ARRAYSZ */
959 			cnt += 2;
960 
961 		if (ofl->ofl_ospreinitarray)	/* PREINIT_ARRAY & */
962 			cnt += 2;		/*	PREINIT_ARRAYSZ */
963 
964 		/*
965 		 * If we have plt's reserve a PLT, PLTSZ, PLTREL and JMPREL.
966 		 */
967 		if (ofl->ofl_pltcnt)
968 			cnt += 3;
969 
970 		/*
971 		 * If pltpadding is needed (Sparcv9)
972 		 */
973 		if (ofl->ofl_pltpad)
974 			cnt += 2;		/* DT_PLTPAD & DT_PLTPADSZ */
975 
976 		/*
977 		 * If we have any relocations reserve a REL, RELSZ and
978 		 * RELENT entry.
979 		 */
980 		if (ofl->ofl_relocsz)
981 			cnt += 3;
982 
983 		/*
984 		 * If a syminfo section is required create SYMINFO, SYMINSZ,
985 		 * and SYMINENT entries.
986 		 */
987 		if (ofl->ofl_flags & FLG_OF_SYMINFO)
988 			cnt += 3;
989 
990 		/*
991 		 * If there are any partially initialized sections allocate
992 		 * MOVEENT, MOVESZ and MOVETAB.
993 		 */
994 		if (ofl->ofl_osmove)
995 			cnt += 3;
996 
997 		/*
998 		 * Allocate one DT_REGISTER entry for every register symbol.
999 		 */
1000 		cnt += ofl->ofl_regsymcnt;
1001 
1002 		/*
1003 		 * Reserve a entry for each '-zrtldinfo=...' specified
1004 		 * on the command line.
1005 		 */
1006 		for (LIST_TRAVERSE(&ofl->ofl_rtldinfo, lnp, sdp))
1007 			cnt++;
1008 
1009 		/*
1010 		 * These two entries should only be placed in a segment
1011 		 * which is writable.  If it's a read-only segment
1012 		 * (due to mapfile magic, e.g. libdl.so.1) then don't allocate
1013 		 * these entries.
1014 		 */
1015 		if ((osp->os_sgdesc) &&
1016 		    (osp->os_sgdesc->sg_phdr.p_flags & PF_W)) {
1017 			cnt++;			/* FEATURE_1 */
1018 
1019 			if (ofl->ofl_osinterp)
1020 				cnt++;		/* DEBUG */
1021 		}
1022 
1023 		/*
1024 		 * Any hardware/software capabilities?
1025 		 */
1026 		if (ofl->ofl_oscap)
1027 			cnt++;			/* SUNW_CAP */
1028 	}
1029 
1030 	if (flags & FLG_OF_SYMBOLIC)
1031 		cnt++;				/* SYMBOLIC */
1032 
1033 	/*
1034 	 * Account for Architecture dependent .dynamic entries, and defaults.
1035 	 */
1036 	ld_mach_make_dynamic(ofl, &cnt);
1037 
1038 	cnt += 3;				/* DT_FLAGS, DT_FLAGS_1, */
1039 						/*   and DT_NULL */
1040 
1041 	/*
1042 	 * Determine the size of the section from the number of entries.
1043 	 */
1044 	size = cnt * (size_t)shdr->sh_entsize;
1045 
1046 	shdr->sh_size = (Xword)size;
1047 	data->d_size = size;
1048 
1049 	return ((uintptr_t)ofl->ofl_osdynamic);
1050 }
1051 
1052 /*
1053  * Build the GOT section and its associated relocation entries.
1054  */
1055 uintptr_t
1056 ld_make_got(Ofl_desc *ofl)
1057 {
1058 	Shdr		*shdr;
1059 	Elf_Data	*data;
1060 	Is_desc		*isec;
1061 	size_t		size = (size_t)ofl->ofl_gotcnt * M_GOT_ENTSIZE;
1062 	size_t		rsize = (size_t)ofl->ofl_relocgotsz;
1063 
1064 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0,
1065 	    &isec, &shdr, &data) == S_ERROR)
1066 		return (S_ERROR);
1067 
1068 	data->d_size = size;
1069 
1070 	shdr->sh_flags |= SHF_WRITE;
1071 	shdr->sh_size = (Xword)size;
1072 	shdr->sh_entsize = M_GOT_ENTSIZE;
1073 
1074 	if ((ofl->ofl_osgot = ld_place_section(ofl, isec, M_ID_GOT, 0)) ==
1075 	    (Os_desc *)S_ERROR)
1076 		return (S_ERROR);
1077 
1078 	ofl->ofl_osgot->os_szoutrels = (Xword)rsize;
1079 
1080 	return (1);
1081 }
1082 
1083 /*
1084  * Build an interpreter section.
1085  */
1086 static uintptr_t
1087 make_interp(Ofl_desc *ofl)
1088 {
1089 	Shdr		*shdr;
1090 	Elf_Data	*data;
1091 	Is_desc		*isec;
1092 	const char	*iname = ofl->ofl_interp;
1093 	size_t		size;
1094 
1095 	/*
1096 	 * If -z nointerp is in effect, don't create an interpreter section.
1097 	 */
1098 	if (ofl->ofl_flags1 & FLG_OF1_NOINTRP)
1099 		return (1);
1100 
1101 	/*
1102 	 * We always build an .interp section for dynamic executables.  However
1103 	 * if the user has specifically specified an interpreter we'll build
1104 	 * this section for any output (presumably the user knows what they are
1105 	 * doing. refer ABI section 5-4, and ld.1 man page use of -I).
1106 	 */
1107 	if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC |
1108 	    FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname)
1109 		return (1);
1110 
1111 	/*
1112 	 * In the case of a dynamic executable supply a default interpreter
1113 	 * if a specific interpreter has not been specified.
1114 	 */
1115 	if (iname == 0) {
1116 		if (ofl->ofl_dehdr->e_machine == EM_SPARCV9)
1117 			iname = ofl->ofl_interp =
1118 			    MSG_ORIG(MSG_PTH_RTLD_SPARCV9);
1119 		else if (ofl->ofl_dehdr->e_machine == EM_AMD64)
1120 			iname = ofl->ofl_interp =
1121 			    MSG_ORIG(MSG_PTH_RTLD_AMD64);
1122 		else
1123 			iname = ofl->ofl_interp = MSG_ORIG(MSG_PTH_RTLD);
1124 	}
1125 
1126 	size = strlen(iname) + 1;
1127 
1128 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0,
1129 	    &isec, &shdr, &data) == S_ERROR)
1130 		return (S_ERROR);
1131 
1132 	data->d_size = size;
1133 	shdr->sh_size = (Xword)size;
1134 	data->d_align = shdr->sh_addralign = 1;
1135 
1136 	ofl->ofl_osinterp = ld_place_section(ofl, isec, M_ID_INTERP, 0);
1137 	return ((uintptr_t)ofl->ofl_osinterp);
1138 }
1139 
1140 /*
1141  * Build a hardware/software capabilities section.
1142  */
1143 static uintptr_t
1144 make_cap(Ofl_desc *ofl)
1145 {
1146 	Shdr		*shdr;
1147 	Elf_Data	*data;
1148 	Is_desc		*isec;
1149 	Os_desc		*osec;
1150 	Cap		*cap;
1151 	size_t		size = 0;
1152 
1153 	/*
1154 	 * Determine how many entries are required.
1155 	 */
1156 	if (ofl->ofl_hwcap_1)
1157 		size++;
1158 	if (ofl->ofl_sfcap_1)
1159 		size++;
1160 	if (size == 0)
1161 		return (1);
1162 	size++;				/* Add CA_SUNW_NULL */
1163 
1164 	if (new_section(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP), size,
1165 	    &isec, &shdr, &data) == S_ERROR)
1166 		return (S_ERROR);
1167 
1168 	if ((data->d_buf = libld_malloc(shdr->sh_size)) == 0)
1169 		return (S_ERROR);
1170 
1171 	cap = (Cap *)data->d_buf;
1172 	if (ofl->ofl_hwcap_1) {
1173 		cap->c_tag = CA_SUNW_HW_1;
1174 		cap->c_un.c_val = ofl->ofl_hwcap_1;
1175 		cap++;
1176 	}
1177 	if (ofl->ofl_sfcap_1) {
1178 		cap->c_tag = CA_SUNW_SF_1;
1179 		cap->c_un.c_val = ofl->ofl_sfcap_1;
1180 		cap++;
1181 	}
1182 	cap->c_tag = CA_SUNW_NULL;
1183 	cap->c_un.c_val = 0;
1184 
1185 	/*
1186 	 * If we're not creating a relocatable object, save the output section
1187 	 * to trigger the creation of an associated program header.
1188 	 */
1189 	osec = ld_place_section(ofl, isec, M_ID_CAP, 0);
1190 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
1191 		ofl->ofl_oscap = osec;
1192 
1193 	return ((uintptr_t)osec);
1194 }
1195 
1196 /*
1197  * Build the PLT section and its associated relocation entries.
1198  */
1199 static uintptr_t
1200 make_plt(Ofl_desc *ofl)
1201 {
1202 	Shdr		*shdr;
1203 	Elf_Data	*data;
1204 	Is_desc		*isec;
1205 	size_t		size = (size_t)M_PLT_RESERVSZ +
1206 				(((size_t)ofl->ofl_pltcnt +
1207 				(size_t)ofl->ofl_pltpad) * M_PLT_ENTSIZE);
1208 	size_t		rsize = (size_t)ofl->ofl_relocpltsz;
1209 
1210 #if	defined(__sparc)
1211 	/*
1212 	 * Account for the NOP at the end of the plt.
1213 	 */
1214 	size += sizeof (Word);
1215 #endif
1216 
1217 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0,
1218 	    &isec, &shdr, &data) == S_ERROR)
1219 		return (S_ERROR);
1220 
1221 	data->d_size = size;
1222 	data->d_align = M_PLT_ALIGN;
1223 
1224 	shdr->sh_flags = M_PLT_SHF_FLAGS;
1225 	shdr->sh_size = (Xword)size;
1226 	shdr->sh_addralign = M_PLT_ALIGN;
1227 	shdr->sh_entsize = M_PLT_ENTSIZE;
1228 
1229 	if ((ofl->ofl_osplt = ld_place_section(ofl, isec, M_ID_PLT, 0)) ==
1230 	    (Os_desc *)S_ERROR)
1231 		return (S_ERROR);
1232 
1233 	ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1234 
1235 	return (1);
1236 }
1237 
1238 /*
1239  * Make the hash table.  Only built for dynamic executables and shared
1240  * libraries, and provides hashed lookup into the global symbol table
1241  * (.dynsym) for the run-time linker to resolve symbol lookups.
1242  */
1243 static uintptr_t
1244 make_hash(Ofl_desc *ofl)
1245 {
1246 	Shdr		*shdr;
1247 	Elf_Data	*data;
1248 	Is_desc		*isec;
1249 	size_t		size;
1250 	Word		nsyms = ofl->ofl_globcnt;
1251 	size_t		cnt;
1252 
1253 	/*
1254 	 * Allocate section header structures. We set entcnt to 0
1255 	 * because it's going to change after we place this section.
1256 	 */
1257 	if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0,
1258 	    &isec, &shdr, &data) == S_ERROR)
1259 		return (S_ERROR);
1260 
1261 	/*
1262 	 * Place the section first since it will affect the local symbol
1263 	 * count.
1264 	 */
1265 	if ((ofl->ofl_oshash = ld_place_section(ofl, isec, M_ID_HASH, 0)) ==
1266 	    (Os_desc *)S_ERROR)
1267 		return (S_ERROR);
1268 
1269 	/*
1270 	 * Calculate the number of output hash buckets.
1271 	 */
1272 	ofl->ofl_hashbkts = findprime(nsyms);
1273 
1274 	/*
1275 	 * The size of the hash table is determined by
1276 	 *
1277 	 *	i.	the initial nbucket and nchain entries (2)
1278 	 *	ii.	the number of buckets (calculated above)
1279 	 *	iii.	the number of chains (this is based on the number of
1280 	 *		symbols in the .dynsym array + NULL symbol).
1281 	 */
1282 	cnt = 2 + ofl->ofl_hashbkts + (ofl->ofl_dynshdrcnt +
1283 		ofl->ofl_globcnt + ofl->ofl_lregsymcnt + 1);
1284 	size = cnt * shdr->sh_entsize;
1285 
1286 	/*
1287 	 * Finalize the section header and data buffer initialization.
1288 	 */
1289 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
1290 		return (S_ERROR);
1291 	data->d_size = size;
1292 	shdr->sh_size = (Xword)size;
1293 
1294 	return (1);
1295 }
1296 
1297 /*
1298  * Generate the standard symbol table.  Contains all locals and globals,
1299  * and resides in a non-allocatable section (ie. it can be stripped).
1300  */
1301 static uintptr_t
1302 make_symtab(Ofl_desc *ofl)
1303 {
1304 	Shdr		*shdr;
1305 	Elf_Data	*data;
1306 	Is_desc		*isec;
1307 	Is_desc		*xisec = 0;
1308 	size_t		size;
1309 	Word		symcnt;
1310 
1311 	/*
1312 	 * Create the section headers. Note that we supply an ent_cnt
1313 	 * of 0. We won't know the count until the section has been placed.
1314 	 */
1315 	if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0,
1316 	    &isec, &shdr, &data) == S_ERROR)
1317 		return (S_ERROR);
1318 
1319 	/*
1320 	 * Place the section first since it will affect the local symbol
1321 	 * count.
1322 	 */
1323 	if ((ofl->ofl_ossymtab = ld_place_section(ofl, isec, M_ID_SYMTAB, 0)) ==
1324 	    (Os_desc *)S_ERROR)
1325 		return (S_ERROR);
1326 
1327 	/*
1328 	 * At this point we've created all but the 'shstrtab' section.
1329 	 * Determine if we have to use 'Extended Sections'.  If so - then
1330 	 * also create a SHT_SYMTAB_SHNDX section.
1331 	 */
1332 	if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
1333 		Shdr		*xshdr;
1334 		Elf_Data	*xdata;
1335 
1336 		if (new_section(ofl, SHT_SYMTAB_SHNDX,
1337 		    MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec,
1338 		    &xshdr, &xdata) == S_ERROR)
1339 			return (S_ERROR);
1340 
1341 		if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec,
1342 		    M_ID_SYMTAB_NDX, 0)) == (Os_desc *)S_ERROR)
1343 			return (S_ERROR);
1344 	}
1345 	/*
1346 	 * Calculated number of symbols, which need to be augmented by
1347 	 * the null first entry, the FILE symbol, and the .shstrtab entry.
1348 	 */
1349 	symcnt = (size_t)(3 + ofl->ofl_shdrcnt + ofl->ofl_scopecnt +
1350 		ofl->ofl_locscnt + ofl->ofl_globcnt);
1351 	size = symcnt * shdr->sh_entsize;
1352 
1353 	/*
1354 	 * Finalize the section header and data buffer initialization.
1355 	 */
1356 	data->d_size = size;
1357 	shdr->sh_size = (Xword)size;
1358 
1359 	/*
1360 	 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
1361 	 */
1362 	if (xisec) {
1363 		size_t	xsize = symcnt * sizeof (Word);
1364 
1365 		xisec->is_indata->d_size = xsize;
1366 		xisec->is_shdr->sh_size = (Xword)xsize;
1367 	}
1368 
1369 	return (1);
1370 }
1371 
1372 
1373 /*
1374  * Build a dynamic symbol table. These tables reside in the text
1375  * segment of a dynamic executable or shared library.
1376  *
1377  *	.SUNW_ldynsym contains local function symbols
1378  *	.dynsym contains only globals symbols
1379  *
1380  * The two tables are created adjacent to each other, with .SUNW_ldynsym
1381  * coming first.
1382  */
1383 static uintptr_t
1384 make_dynsym(Ofl_desc *ofl)
1385 {
1386 	Shdr		*shdr, *lshdr;
1387 	Elf_Data	*data, *ldata;
1388 	Is_desc		*isec, *lisec;
1389 	size_t		size;
1390 	Xword		cnt;
1391 	int		allow_ldynsym;
1392 
1393 	/*
1394 	 * Unless explicitly disabled, always produce a .SUNW_ldynsym section
1395 	 * when it is allowed by the file type, even if the resulting
1396 	 * table only ends up with a single STT_FILE in it. There are
1397 	 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB
1398 	 * entry in the .dynamic section, which is something we would
1399 	 * like to encourage, and (2) Without it, we cannot generate
1400 	 * the associated .SUNW_dyn[sym|tls]sort sections, which are of
1401 	 * value to DTrace.
1402 	 *
1403 	 * In practice, it is extremely rare for an object not to have
1404 	 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be
1405 	 * doing it anyway.
1406 	 */
1407 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1408 
1409 	/*
1410 	 * Create the section headers. Note that we supply an ent_cnt
1411 	 * of 0. We won't know the count until the section has been placed.
1412 	 */
1413 	if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM,
1414 	    MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR)
1415 		return (S_ERROR);
1416 
1417 	if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0,
1418 	    &isec, &shdr, &data) == S_ERROR)
1419 		return (S_ERROR);
1420 
1421 	/*
1422 	 * Place the section(s) first since it will affect the local symbol
1423 	 * count.
1424 	 */
1425 	if (allow_ldynsym &&
1426 	    ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec,
1427 	    M_ID_LDYNSYM, 0)) == (Os_desc *)S_ERROR))
1428 		return (S_ERROR);
1429 	if ((ofl->ofl_osdynsym = ld_place_section(ofl, isec, M_ID_DYNSYM, 0))
1430 	    == (Os_desc *)S_ERROR)
1431 		return (S_ERROR);
1432 
1433 	/*
1434 	 * One extra section header entry for the 'null' entry.
1435 	 */
1436 	cnt = 1 + ofl->ofl_dynshdrcnt + ofl->ofl_globcnt + ofl->ofl_lregsymcnt;
1437 	size = (size_t)cnt * shdr->sh_entsize;
1438 
1439 	/*
1440 	 * Finalize the section header and data buffer initialization.
1441 	 */
1442 	data->d_size = size;
1443 	shdr->sh_size = (Xword)size;
1444 
1445 	/*
1446 	 * An ldynsym contains local function symbols. It is not
1447 	 * used for linking, but if present, serves to allow better
1448 	 * stack traces to be generated in contexts where the symtab
1449 	 * is not available. (dladdr(), or stripped executable/library files).
1450 	 */
1451 	if (allow_ldynsym) {
1452 		cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt;
1453 		size = (size_t)cnt * shdr->sh_entsize;
1454 
1455 		ldata->d_size = size;
1456 		lshdr->sh_size = (Xword)size;
1457 	}
1458 
1459 	return (1);
1460 }
1461 
1462 /*
1463  * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are
1464  * index sections for the .SUNW_ldynsym/.dynsym pair that present data
1465  * and function symbols sorted by address.
1466  */
1467 static uintptr_t
1468 make_dynsort(Ofl_desc *ofl)
1469 {
1470 	Shdr		*shdr;
1471 	Elf_Data	*data;
1472 	Is_desc		*isec;
1473 
1474 
1475 	/* Only do it if the .SUNW_ldynsym section is present */
1476 	if (!OFL_ALLOW_LDYNSYM(ofl))
1477 		return (1);
1478 
1479 	/* .SUNW_dynsymsort */
1480 	if (ofl->ofl_dynsymsortcnt > 0) {
1481 		if (new_section(ofl, SHT_SUNW_symsort,
1482 		    MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt,
1483 		    &isec, &shdr, &data) == S_ERROR)
1484 		return (S_ERROR);
1485 
1486 		if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec,
1487 		    M_ID_DYNSORT, 0)) == (Os_desc *)S_ERROR)
1488 			return (S_ERROR);
1489 	}
1490 
1491 	/* .SUNW_dyntlssort */
1492 	if (ofl->ofl_dyntlssortcnt > 0) {
1493 		if (new_section(ofl, SHT_SUNW_tlssort,
1494 		    MSG_ORIG(MSG_SCN_DYNTLSSORT),
1495 		    ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR)
1496 		return (S_ERROR);
1497 
1498 		if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec,
1499 		    M_ID_DYNSORT, 0)) == (Os_desc *)S_ERROR)
1500 			return (S_ERROR);
1501 	}
1502 
1503 	return (1);
1504 }
1505 
1506 /*
1507  * Helper routine for make_dynsym_shndx. Builds a
1508  * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing
1509  * which one it is.
1510  */
1511 static uintptr_t
1512 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab,
1513     Os_desc **ret_os)
1514 {
1515 	Is_desc		*isec;
1516 	Is_desc		*dynsymisp;
1517 	Shdr		*shdr, *dynshdr;
1518 	Elf_Data	*data;
1519 
1520 	dynsymisp = (Is_desc *)symtab->os_isdescs.head->data;
1521 	dynshdr = dynsymisp->is_shdr;
1522 
1523 	if (new_section(ofl, SHT_SYMTAB_SHNDX, shname,
1524 	    (dynshdr->sh_size / dynshdr->sh_entsize),
1525 	    &isec, &shdr, &data) == S_ERROR)
1526 		return (S_ERROR);
1527 
1528 	if ((*ret_os = ld_place_section(ofl, isec,
1529 	    M_ID_DYNSYM_NDX, 0)) == (Os_desc *)S_ERROR)
1530 		return (S_ERROR);
1531 
1532 	assert(*ret_os);
1533 
1534 	return (1);
1535 }
1536 
1537 /*
1538  * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym
1539  */
1540 static uintptr_t
1541 make_dynsym_shndx(Ofl_desc *ofl)
1542 {
1543 	/*
1544 	 * If there is a .SUNW_ldynsym, generate a section for its extended
1545 	 * index section as well.
1546 	 */
1547 	if (OFL_ALLOW_LDYNSYM(ofl)) {
1548 		if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX),
1549 		    ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR)
1550 			return (S_ERROR);
1551 	}
1552 
1553 	/* The Generate a section for the dynsym */
1554 	if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX),
1555 	    ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR)
1556 		return (S_ERROR);
1557 
1558 	return (1);
1559 }
1560 
1561 
1562 /*
1563  * Build a string table for the section headers.
1564  */
1565 static uintptr_t
1566 make_shstrtab(Ofl_desc *ofl)
1567 {
1568 	Shdr		*shdr;
1569 	Elf_Data	*data;
1570 	Is_desc		*isec;
1571 	size_t		size;
1572 
1573 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB),
1574 	    0, &isec, &shdr, &data) == S_ERROR)
1575 		return (S_ERROR);
1576 
1577 	/*
1578 	 * Place the section first, as it may effect the number of section
1579 	 * headers to account for.
1580 	 */
1581 	if ((ofl->ofl_osshstrtab = ld_place_section(ofl, isec, M_ID_NOTE, 0)) ==
1582 	    (Os_desc *)S_ERROR)
1583 		return (S_ERROR);
1584 
1585 	size = st_getstrtab_sz(ofl->ofl_shdrsttab);
1586 	assert(size > 0);
1587 
1588 	data->d_size = size;
1589 	shdr->sh_size = (Xword)size;
1590 
1591 	return (1);
1592 }
1593 
1594 /*
1595  * Build a string section for the standard symbol table.
1596  */
1597 static uintptr_t
1598 make_strtab(Ofl_desc *ofl)
1599 {
1600 	Shdr		*shdr;
1601 	Elf_Data	*data;
1602 	Is_desc		*isec;
1603 	size_t		size;
1604 
1605 	/*
1606 	 * This string table consists of all the global and local symbols.
1607 	 * Account for null bytes at end of the file name and the beginning
1608 	 * of section.
1609 	 */
1610 	if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
1611 		return (S_ERROR);
1612 
1613 	size = st_getstrtab_sz(ofl->ofl_strtab);
1614 	assert(size > 0);
1615 
1616 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB),
1617 	    0, &isec, &shdr, &data) == S_ERROR)
1618 		return (S_ERROR);
1619 
1620 	/* Set the size of the data area */
1621 	data->d_size = size;
1622 	shdr->sh_size = (Xword)size;
1623 
1624 	ofl->ofl_osstrtab = ld_place_section(ofl, isec, M_ID_STRTAB, 0);
1625 	return ((uintptr_t)ofl->ofl_osstrtab);
1626 }
1627 
1628 /*
1629  * Build a string table for the dynamic symbol table.
1630  */
1631 static uintptr_t
1632 make_dynstr(Ofl_desc *ofl)
1633 {
1634 	Shdr		*shdr;
1635 	Elf_Data	*data;
1636 	Is_desc		*isec;
1637 	size_t		size;
1638 
1639 	/*
1640 	 * If producing a .SUNW_ldynsym, account for the initial STT_FILE
1641 	 * symbol that precedes the scope reduced global symbols.
1642 	 */
1643 	if (OFL_ALLOW_LDYNSYM(ofl)) {
1644 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1)
1645 			return (S_ERROR);
1646 		ofl->ofl_dynscopecnt++;
1647 	}
1648 
1649 
1650 	/*
1651 	 * Account for any local, named register symbols.  These locals are
1652 	 * required for reference from DT_REGISTER .dynamic entries.
1653 	 */
1654 	if (ofl->ofl_regsyms) {
1655 		int	ndx;
1656 
1657 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1658 			Sym_desc *	sdp;
1659 
1660 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1661 				continue;
1662 
1663 			if (((sdp->sd_flags1 & FLG_SY1_LOCL) == 0) &&
1664 			    (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
1665 				continue;
1666 
1667 			if (sdp->sd_sym->st_name == 0)
1668 				continue;
1669 
1670 			if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
1671 				return (S_ERROR);
1672 		}
1673 	}
1674 
1675 	/*
1676 	 * Reserve entries for any per-symbol auxiliary/filter strings.
1677 	 */
1678 	if (ofl->ofl_dtsfltrs) {
1679 		Dfltr_desc *	dftp;
1680 		Aliste		off;
1681 
1682 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp))
1683 			if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
1684 				return (S_ERROR);
1685 	}
1686 
1687 	size = st_getstrtab_sz(ofl->ofl_dynstrtab);
1688 	assert(size > 0);
1689 
1690 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR),
1691 	    0, &isec, &shdr, &data) == S_ERROR)
1692 		return (S_ERROR);
1693 
1694 	/* Make it allocable if necessary */
1695 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
1696 		shdr->sh_flags |= SHF_ALLOC;
1697 
1698 	/* Set the size of the data area */
1699 	data->d_size = size;
1700 	shdr->sh_size = (Xword)size;
1701 
1702 	ofl->ofl_osdynstr = ld_place_section(ofl, isec, M_ID_DYNSTR, 0);
1703 	return ((uintptr_t)ofl->ofl_osdynstr);
1704 }
1705 
1706 /*
1707  * Generate an output relocation section which will contain the relocation
1708  * information to be applied to the `osp' section.
1709  *
1710  * If (osp == NULL) then we are creating the coalesced relocation section
1711  * for an executable and/or a shared object.
1712  */
1713 static uintptr_t
1714 make_reloc(Ofl_desc *ofl, Os_desc *osp)
1715 {
1716 	Shdr		*shdr;
1717 	Elf_Data	*data;
1718 	Is_desc		*isec;
1719 	size_t		size;
1720 	Xword		sh_flags;
1721 	char 		*sectname;
1722 	Os_desc		*rosp;
1723 	Word		relsize;
1724 	const char	*rel_prefix;
1725 
1726 	/* LINTED */
1727 	if (M_REL_SHT_TYPE == SHT_REL) {
1728 		/* REL */
1729 		relsize = sizeof (Rel);
1730 		rel_prefix = MSG_ORIG(MSG_SCN_REL);
1731 	} else {
1732 		/* RELA */
1733 		relsize = sizeof (Rela);
1734 		rel_prefix = MSG_ORIG(MSG_SCN_RELA);
1735 	}
1736 
1737 	if (osp) {
1738 		size = osp->os_szoutrels;
1739 		sh_flags = osp->os_shdr->sh_flags;
1740 		if ((sectname = libld_malloc(strlen(rel_prefix) +
1741 		    strlen(osp->os_name) + 1)) == 0)
1742 			return (S_ERROR);
1743 		(void) strcpy(sectname, rel_prefix);
1744 		(void) strcat(sectname, osp->os_name);
1745 	} else if (ofl->ofl_flags1 & FLG_OF1_RELCNT) {
1746 		size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
1747 		sh_flags = SHF_ALLOC;
1748 		sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
1749 	} else {
1750 		size = ofl->ofl_relocrelsz;
1751 		sh_flags = SHF_ALLOC;
1752 		sectname = (char *)rel_prefix;
1753 	}
1754 
1755 	/*
1756 	 * Keep track of total size of 'output relocations' (to be stored
1757 	 * in .dynamic)
1758 	 */
1759 	/* LINTED */
1760 	ofl->ofl_relocsz += (Xword)size;
1761 
1762 	if (new_section(ofl, M_REL_SHT_TYPE, sectname, 0, &isec, &shdr, &data)
1763 	    == S_ERROR)
1764 		return (S_ERROR);
1765 
1766 	data->d_size = size;
1767 
1768 	shdr->sh_size = (Xword)size;
1769 	if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC))
1770 		shdr->sh_flags = SHF_ALLOC;
1771 
1772 	if (osp) {
1773 		/*
1774 		 * The sh_info field of the SHT_REL* sections points to the
1775 		 * section the relocations are to be applied to.
1776 		 */
1777 		shdr->sh_flags |= SHF_INFO_LINK;
1778 	}
1779 
1780 	/*
1781 	 * Associate this relocation section to the section its going to
1782 	 * relocate.
1783 	 */
1784 	if ((rosp = ld_place_section(ofl, isec, M_ID_REL, 0)) ==
1785 	    (Os_desc *)S_ERROR)
1786 		return (S_ERROR);
1787 
1788 	if (osp) {
1789 		Listnode	*lnp;
1790 		Is_desc		*risp;
1791 
1792 		/*
1793 		 * We associate the input relocation sections - with
1794 		 * the newly created output relocation section.
1795 		 *
1796 		 * This is used primarily so that we can update
1797 		 * SHT_GROUP[sect_no] entries to point to the
1798 		 * created output relocation sections.
1799 		 */
1800 		for (LIST_TRAVERSE(&(osp->os_relisdescs), lnp, risp)) {
1801 			risp->is_osdesc = rosp;
1802 
1803 			/*
1804 			 * If the input relocation section had the SHF_GROUP
1805 			 * flag set - propagate it to the output relocation
1806 			 * section.
1807 			 */
1808 			if (risp->is_shdr->sh_flags & SHF_GROUP) {
1809 				rosp->os_shdr->sh_flags |= SHF_GROUP;
1810 				break;
1811 			}
1812 		}
1813 		osp->os_relosdesc = rosp;
1814 	} else
1815 		ofl->ofl_osrel = rosp;
1816 
1817 	/*
1818 	 * If this is the first relocation section we've encountered save it
1819 	 * so that the .dynamic entry can be initialized accordingly.
1820 	 */
1821 	if (ofl->ofl_osrelhead == (Os_desc *)0)
1822 		ofl->ofl_osrelhead = rosp;
1823 
1824 	return (1);
1825 }
1826 
1827 /*
1828  * Generate version needed section.
1829  */
1830 static uintptr_t
1831 make_verneed(Ofl_desc *ofl)
1832 {
1833 	Shdr		*shdr;
1834 	Elf_Data	*data;
1835 	Is_desc		*isec;
1836 
1837 	/*
1838 	 * verneed sections do not have a constant element size, so the
1839 	 * value of ent_cnt specified here (0) is meaningless.
1840 	 */
1841 	if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION),
1842 			0, &isec, &shdr, &data) == S_ERROR)
1843 		return (S_ERROR);
1844 
1845 	/* During version processing we calculated the total size. */
1846 	data->d_size = ofl->ofl_verneedsz;
1847 	shdr->sh_size = (Xword)ofl->ofl_verneedsz;
1848 
1849 	ofl->ofl_osverneed = ld_place_section(ofl, isec, M_ID_VERSION, 0);
1850 	return ((uintptr_t)ofl->ofl_osverneed);
1851 }
1852 
1853 /*
1854  * Generate a version definition section.
1855  *
1856  *  o	the SHT_SUNW_verdef section defines the versions that exist within this
1857  *	image.
1858  */
1859 static uintptr_t
1860 make_verdef(Ofl_desc *ofl)
1861 {
1862 	Shdr		*shdr;
1863 	Elf_Data	*data;
1864 	Is_desc		*isec;
1865 	Ver_desc	*vdp;
1866 
1867 	/*
1868 	 * Reserve a string table entry for the base version dependency (other
1869 	 * dependencies have symbol representations, which will already be
1870 	 * accounted for during symbol processing).
1871 	 */
1872 	vdp = (Ver_desc *)ofl->ofl_verdesc.head->data;
1873 
1874 	if (ofl->ofl_flags & FLG_OF_DYNAMIC) {
1875 		if (st_insert(ofl->ofl_dynstrtab, vdp->vd_name) == -1)
1876 			return (S_ERROR);
1877 	} else {
1878 		if (st_insert(ofl->ofl_strtab, vdp->vd_name) == -1)
1879 			return (S_ERROR);
1880 	}
1881 
1882 	/*
1883 	 * verdef sections do not have a constant element size, so the
1884 	 * value of ent_cnt specified here (0) is meaningless.
1885 	 */
1886 	if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION),
1887 			0, &isec, &shdr, &data) == S_ERROR)
1888 		return (S_ERROR);
1889 
1890 	/* During version processing we calculated the total size. */
1891 	data->d_size = ofl->ofl_verdefsz;
1892 	shdr->sh_size = (Xword)ofl->ofl_verdefsz;
1893 
1894 	ofl->ofl_osverdef = ld_place_section(ofl, isec, M_ID_VERSION, 0);
1895 	return ((uintptr_t)ofl->ofl_osverdef);
1896 }
1897 
1898 /*
1899  * Common function used to build both the SHT_SUNW_versym
1900  * section and the SHT_SUNW_syminfo section.  Each of these sections
1901  * provides additional symbol information.
1902  */
1903 static Os_desc *
1904 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident)
1905 {
1906 	Shdr		*shdr;
1907 	Elf_Data	*data;
1908 	Is_desc		*isec;
1909 
1910 	/*
1911 	 * We don't know the size of this section yet, so set it to 0.
1912 	 * It gets filled in after the dynsym is sized.
1913 	 */
1914 	if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) ==
1915 	    S_ERROR)
1916 		return ((Os_desc *)S_ERROR);
1917 
1918 	return (ld_place_section(ofl, isec, ident, 0));
1919 }
1920 
1921 /*
1922  * Build a .sunwbss section for allocation of tentative definitions.
1923  */
1924 uintptr_t
1925 ld_make_sunwbss(Ofl_desc *ofl, size_t size, Xword align)
1926 {
1927 	Shdr		*shdr;
1928 	Elf_Data	*data;
1929 	Is_desc		*isec;
1930 
1931 	/*
1932 	 * Allocate header structs. We will set the name ourselves below,
1933 	 * and there is no entcnt for a BSS. So, the shname and entcnt
1934 	 * arguments are 0.
1935 	 */
1936 	if (new_section(ofl, SHT_NOBITS, MSG_ORIG(MSG_SCN_SUNWBSS), 0,
1937 	    &isec, &shdr, &data) == S_ERROR)
1938 		return (S_ERROR);
1939 
1940 	data->d_size = size;
1941 	data->d_align = align;
1942 
1943 	shdr->sh_size = (Xword)size;
1944 	shdr->sh_addralign = align;
1945 
1946 	/*
1947 	 * Retain this .sunwbss input section as this will be where global
1948 	 * symbol references are added.
1949 	 */
1950 	ofl->ofl_issunwbss = isec;
1951 	if (ld_place_section(ofl, isec, 0, 0) == (Os_desc *)S_ERROR)
1952 		return (S_ERROR);
1953 
1954 	return (1);
1955 }
1956 
1957 /*
1958  * This routine is called when -z nopartial is in effect.
1959  */
1960 uintptr_t
1961 ld_make_sunwdata(Ofl_desc *ofl, size_t size, Xword align)
1962 {
1963 	Shdr		*shdr;
1964 	Elf_Data	*data;
1965 	Is_desc		*isec;
1966 	Os_desc		*osp;
1967 
1968 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_SUNWDATA1), 0,
1969 	    &isec, &shdr, &data) == S_ERROR)
1970 		return (S_ERROR);
1971 
1972 	shdr->sh_flags |= SHF_WRITE;
1973 	data->d_size = size;
1974 	shdr->sh_size = (Xword)size;
1975 	if (align != 0) {
1976 		data->d_align = align;
1977 		shdr->sh_addralign = align;
1978 	}
1979 
1980 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
1981 		return (S_ERROR);
1982 
1983 	/*
1984 	 * Retain this .sunwdata1 input section as this will
1985 	 * be where global
1986 	 * symbol references are added.
1987 	 */
1988 	ofl->ofl_issunwdata1 = isec;
1989 	if ((osp = ld_place_section(ofl, isec, M_ID_DATA, 0)) ==
1990 	    (Os_desc *)S_ERROR)
1991 		return (S_ERROR);
1992 
1993 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
1994 		ofl->ofl_dynshdrcnt++;
1995 		osp->os_flags |= FLG_OS_OUTREL;
1996 	}
1997 	return (1);
1998 }
1999 
2000 /*
2001  * Make .sunwmove section
2002  */
2003 uintptr_t
2004 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2005 {
2006 	Shdr		*shdr;
2007 	Elf_Data	*data;
2008 	Is_desc		*isec;
2009 	Listnode	*lnp1;
2010 	Psym_info	*psym;
2011 	int 		cnt = 1;
2012 
2013 
2014 	if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE),
2015 	    mv_nums, &isec, &shdr, &data) == S_ERROR)
2016 		return (S_ERROR);
2017 
2018 	if ((data->d_buf = libld_calloc(data->d_size, 1)) == 0)
2019 		return (S_ERROR);
2020 
2021 	/*
2022 	 * Copy move entries
2023 	 */
2024 	for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) {
2025 		Listnode *	lnp2;
2026 		Mv_itm *	mvitm;
2027 
2028 		if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN)
2029 			continue;
2030 		for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvitm)) {
2031 			if ((mvitm->mv_flag & FLG_MV_OUTSECT) == 0)
2032 				continue;
2033 			mvitm->mv_oidx = cnt;
2034 			cnt++;
2035 		}
2036 	}
2037 	if ((ofl->ofl_osmove = ld_place_section(ofl, isec, 0, 0)) ==
2038 	    (Os_desc *)S_ERROR)
2039 		return (S_ERROR);
2040 
2041 	return (1);
2042 }
2043 
2044 
2045 /*
2046  * The following sections are built after all input file processing and symbol
2047  * validation has been carried out.  The order is important (because the
2048  * addition of a section adds a new symbol there is a chicken and egg problem
2049  * of maintaining the appropriate counts).  By maintaining a known order the
2050  * individual routines can compensate for later, known, additions.
2051  */
2052 uintptr_t
2053 ld_make_sections(Ofl_desc *ofl)
2054 {
2055 	Word		flags = ofl->ofl_flags;
2056 	Listnode	*lnp1;
2057 	Sg_desc		*sgp;
2058 
2059 	/*
2060 	 * Generate any special sections.
2061 	 */
2062 	if (flags & FLG_OF_ADDVERS)
2063 		if (make_comment(ofl) == S_ERROR)
2064 			return (S_ERROR);
2065 
2066 	if (make_interp(ofl) == S_ERROR)
2067 		return (S_ERROR);
2068 
2069 	if (make_cap(ofl) == S_ERROR)
2070 		return (S_ERROR);
2071 
2072 	if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
2073 	    &ofl->ofl_initarray) == S_ERROR)
2074 		return (S_ERROR);
2075 
2076 	if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
2077 	    &ofl->ofl_finiarray) == S_ERROR)
2078 		return (S_ERROR);
2079 
2080 	if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
2081 	    &ofl->ofl_preiarray) == S_ERROR)
2082 		return (S_ERROR);
2083 
2084 	/*
2085 	 * Make the .plt section.  This occurs after any other relocation
2086 	 * sections are generated (see reloc_init()) to ensure that the
2087 	 * associated relocation section is after all the other relocation
2088 	 * sections.
2089 	 */
2090 	if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
2091 		if (make_plt(ofl) == S_ERROR)
2092 			return (S_ERROR);
2093 
2094 	/*
2095 	 * Determine whether any sections or files are not referenced.  Under
2096 	 * -Dunused a diagnostic for any unused components is generated, under
2097 	 * -zignore the component is removed from the final output.
2098 	 */
2099 	if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
2100 		if (ignore_section_processing(ofl) == S_ERROR)
2101 			return (S_ERROR);
2102 	}
2103 
2104 	/*
2105 	 * Add any necessary versioning information.
2106 	 */
2107 	if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED) {
2108 		if (make_verneed(ofl) == S_ERROR)
2109 			return (S_ERROR);
2110 	}
2111 	if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == FLG_OF_VERDEF) {
2112 		if (make_verdef(ofl) == S_ERROR)
2113 			return (S_ERROR);
2114 		if ((ofl->ofl_osversym = make_sym_sec(ofl,
2115 		    MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym,
2116 		    M_ID_VERSION)) == (Os_desc*)S_ERROR)
2117 			return (S_ERROR);
2118 	}
2119 
2120 	/*
2121 	 * Create a syminfo section if necessary.
2122 	 */
2123 	if (ofl->ofl_flags & FLG_OF_SYMINFO) {
2124 		if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
2125 		    MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo,
2126 		    M_ID_SYMINFO)) == (Os_desc *)S_ERROR)
2127 			return (S_ERROR);
2128 	}
2129 
2130 	if (ofl->ofl_flags1 & FLG_OF1_RELCNT) {
2131 		/*
2132 		 * If -zcombreloc is enabled then all relocations (except for
2133 		 * the PLT's) are coalesced into a single relocation section.
2134 		 */
2135 		if (ofl->ofl_reloccnt) {
2136 			if (make_reloc(ofl, NULL) == S_ERROR)
2137 				return (S_ERROR);
2138 		}
2139 	} else {
2140 		/*
2141 		 * Create the required output relocation sections.  Note, new
2142 		 * sections may be added to the section list that is being
2143 		 * traversed.  These insertions can move the elements of the
2144 		 * Alist such that a section descriptor is re-read.  Recursion
2145 		 * is prevented by maintaining a previous section pointer and
2146 		 * insuring that this pointer isn't re-examined.
2147 		 */
2148 		for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2149 			Os_desc	**ospp, *posp = 0;
2150 			Aliste	off;
2151 
2152 			for (ALIST_TRAVERSE(sgp->sg_osdescs, off, ospp)) {
2153 				Os_desc	*osp = *ospp;
2154 
2155 				if ((osp != posp) && osp->os_szoutrels &&
2156 				    (osp != ofl->ofl_osplt)) {
2157 					if (make_reloc(ofl, osp) == S_ERROR)
2158 						return (S_ERROR);
2159 				}
2160 				posp = osp;
2161 			}
2162 		}
2163 
2164 		/*
2165 		 * If we're not building a combined relocation section, then
2166 		 * build a .rel[a] section as required.
2167 		 */
2168 		if (ofl->ofl_relocrelsz) {
2169 			if (make_reloc(ofl, NULL) == S_ERROR)
2170 				return (S_ERROR);
2171 		}
2172 	}
2173 
2174 	/*
2175 	 * The PLT relocations are always in their own section, and we try to
2176 	 * keep them at the end of the PLT table.  We do this to keep the hot
2177 	 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
2178 	 */
2179 	if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
2180 		if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
2181 			return (S_ERROR);
2182 	}
2183 
2184 	/*
2185 	 * Finally build the symbol and section header sections.
2186 	 */
2187 	if (flags & FLG_OF_DYNAMIC) {
2188 		if (make_dynamic(ofl) == S_ERROR)
2189 			return (S_ERROR);
2190 		if (make_dynstr(ofl) == S_ERROR)
2191 			return (S_ERROR);
2192 		/*
2193 		 * There is no use for .hash and .dynsym sections in a
2194 		 * relocatable object.
2195 		 */
2196 		if (!(flags & FLG_OF_RELOBJ)) {
2197 			if (make_hash(ofl) == S_ERROR)
2198 				return (S_ERROR);
2199 			if (make_dynsym(ofl) == S_ERROR)
2200 				return (S_ERROR);
2201 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
2202 			if (make_amd64_unwindhdr(ofl) == S_ERROR)
2203 				return (S_ERROR);
2204 #endif
2205 			if (make_dynsort(ofl) == S_ERROR)
2206 				return (S_ERROR);
2207 		}
2208 	}
2209 
2210 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
2211 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
2212 		/*
2213 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2214 		 * for the dynsym.  If so - do it now.
2215 		 */
2216 		if (ofl->ofl_osdynsym &&
2217 		    ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
2218 			if (make_dynsym_shndx(ofl) == S_ERROR)
2219 				return (S_ERROR);
2220 		}
2221 
2222 		if (make_strtab(ofl) == S_ERROR)
2223 			return (S_ERROR);
2224 		if (make_symtab(ofl) == S_ERROR)
2225 			return (S_ERROR);
2226 	} else {
2227 		/*
2228 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2229 		 * for the dynsym.  If so - do it now.
2230 		 */
2231 		if (ofl->ofl_osdynsym &&
2232 		    ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
2233 			if (make_dynsym_shndx(ofl) == S_ERROR)
2234 				return (S_ERROR);
2235 		}
2236 	}
2237 
2238 	if (make_shstrtab(ofl) == S_ERROR)
2239 		return (S_ERROR);
2240 
2241 	/*
2242 	 * Now that we've created all of our sections adjust the size
2243 	 * of SHT_SUNW_versym & SHT_SUNW_syminfo which are dependent on
2244 	 * the symbol table sizes.
2245 	 */
2246 	if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
2247 		Shdr *		shdr;
2248 		Is_desc *	isec;
2249 		Elf_Data *	data;
2250 		size_t		size;
2251 		ulong_t		cnt;
2252 
2253 		if (flags & (FLG_OF_RELOBJ | FLG_OF_STATIC)) {
2254 			isec = (Is_desc *)ofl->ofl_ossymtab->
2255 				os_isdescs.head->data;
2256 		} else {
2257 			isec = (Is_desc *)ofl->ofl_osdynsym->
2258 				os_isdescs.head->data;
2259 		}
2260 		cnt = (isec->is_shdr->sh_size / isec->is_shdr->sh_entsize);
2261 
2262 		if (ofl->ofl_osversym) {
2263 			isec = (Is_desc *)ofl->ofl_osversym->os_isdescs.
2264 				head->data;
2265 			data = isec->is_indata;
2266 			shdr = ofl->ofl_osversym->os_shdr;
2267 			size = cnt * shdr->sh_entsize;
2268 			shdr->sh_size = (Xword)size;
2269 			data->d_size = size;
2270 		}
2271 		if (ofl->ofl_ossyminfo) {
2272 			isec = (Is_desc *)ofl->ofl_ossyminfo->os_isdescs.
2273 				head->data;
2274 			data = isec->is_indata;
2275 			shdr = ofl->ofl_ossyminfo->os_shdr;
2276 			size = cnt * shdr->sh_entsize;
2277 			shdr->sh_size = (Xword)size;
2278 			data->d_size = size;
2279 		}
2280 	}
2281 
2282 	return (1);
2283 }
2284 
2285 /*
2286  * Build an additional data section - used to back OBJT symbol definitions
2287  * added with a mapfile.
2288  */
2289 Is_desc *
2290 ld_make_data(Ofl_desc *ofl, size_t size)
2291 {
2292 	Shdr		*shdr;
2293 	Elf_Data	*data;
2294 	Is_desc		*isec;
2295 
2296 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2297 	    &isec, &shdr, &data) == S_ERROR)
2298 		return ((Is_desc *)S_ERROR);
2299 
2300 	data->d_size = size;
2301 	shdr->sh_size = (Xword)size;
2302 	shdr->sh_flags |= SHF_WRITE;
2303 
2304 	if (ld_place_section(ofl, isec, M_ID_DATA, 0) == (Os_desc *)S_ERROR)
2305 		return ((Is_desc *)S_ERROR);
2306 
2307 	return (isec);
2308 }
2309 
2310 /*
2311  * Define a set of templates for generating "void (*)(void)" function
2312  * definitions.
2313  */
2314 #if	defined(__i386) || defined(__amd64)
2315 #if	defined(__lint)
2316 static const uchar_t ret_template[] = { 0 };
2317 #else	/* __lint */
2318 #if	defined(_ELF64)
2319 #define	ret_template	ret64_template
2320 #else
2321 #define	ret_template	ret32_template
2322 #endif
2323 
2324 static const uchar_t ret32_template[] = {
2325 /* 0x00 */	0xc3				/* ret */
2326 };
2327 
2328 static const uchar_t ret64_template[] = {
2329 /* 0x00 */	0x55,				/* pushq  %rbp */
2330 /* 0x01 */	0x48, 0x8b, 0xec,		/* movq   %rsp,%rbp */
2331 /* 0x04 */	0x48, 0x8b, 0xe5,		/* movq   %rbp,%rsp */
2332 /* 0x07 */	0x5d,				/* popq   %rbp */
2333 /* 0x08 */	0xc3				/* ret */
2334 };
2335 #endif	/* __lint */
2336 
2337 #elif	defined(__sparc)
2338 static const uchar_t ret_template[] = {
2339 /* 0x00 */	0x81, 0xc3, 0xe0, 0x08,		/* retl */
2340 /* 0x04 */	0x01, 0x00, 0x00, 0x00		/* nop */
2341 };
2342 #else
2343 #error	unsupported architecture!
2344 #endif
2345 
2346 /*
2347  * Build an additional text section - used to back FUNC symbol definitions
2348  * added with a mapfile.
2349  */
2350 Is_desc *
2351 ld_make_text(Ofl_desc *ofl, size_t size)
2352 {
2353 	Shdr		*shdr;
2354 	Elf_Data	*data;
2355 	Is_desc		*isec;
2356 
2357 	/*
2358 	 * Insure the size is sufficient to contain the minimum return
2359 	 * instruction.
2360 	 */
2361 	if (size < sizeof (ret_template))
2362 		size = sizeof (ret_template);
2363 
2364 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0,
2365 	    &isec, &shdr, &data) == S_ERROR)
2366 		return ((Is_desc *)S_ERROR);
2367 
2368 	data->d_size = size;
2369 	shdr->sh_size = (Xword)size;
2370 	shdr->sh_flags |= SHF_EXECINSTR;
2371 
2372 	/* Fill the buffer with the appropriate return instruction. */
2373 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2374 		return ((Is_desc *)S_ERROR);
2375 	(void) memcpy(data->d_buf, ret_template, sizeof (ret_template));
2376 
2377 	if (ld_place_section(ofl, isec, M_ID_TEXT, 0) == (Os_desc *)S_ERROR)
2378 		return ((Is_desc *)S_ERROR);
2379 
2380 	return (isec);
2381 }
2382