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