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