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