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