xref: /titanic_51/usr/src/cmd/sgs/libld/common/sections.c (revision 53a7b6b6763f5865522a76e5e887390a8f4777d7)
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 
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 	/*
1909 	 * Associate this relocation section to the section its going to
1910 	 * relocate.
1911 	 */
1912 	rosp = ld_place_section(ofl, isec, ld_targ.t_id.id_rel, 0);
1913 	if (rosp == (Os_desc *)S_ERROR)
1914 		return (S_ERROR);
1915 
1916 	if (osp) {
1917 		Listnode	*lnp;
1918 		Is_desc		*risp;
1919 
1920 		/*
1921 		 * We associate the input relocation sections - with
1922 		 * the newly created output relocation section.
1923 		 *
1924 		 * This is used primarily so that we can update
1925 		 * SHT_GROUP[sect_no] entries to point to the
1926 		 * created output relocation sections.
1927 		 */
1928 		for (LIST_TRAVERSE(&(osp->os_relisdescs), lnp, risp)) {
1929 			risp->is_osdesc = rosp;
1930 
1931 			/*
1932 			 * If the input relocation section had the SHF_GROUP
1933 			 * flag set - propagate it to the output relocation
1934 			 * section.
1935 			 */
1936 			if (risp->is_shdr->sh_flags & SHF_GROUP) {
1937 				rosp->os_shdr->sh_flags |= SHF_GROUP;
1938 				break;
1939 			}
1940 		}
1941 		osp->os_relosdesc = rosp;
1942 	} else
1943 		ofl->ofl_osrel = rosp;
1944 
1945 	/*
1946 	 * If this is the first relocation section we've encountered save it
1947 	 * so that the .dynamic entry can be initialized accordingly.
1948 	 */
1949 	if (ofl->ofl_osrelhead == (Os_desc *)0)
1950 		ofl->ofl_osrelhead = rosp;
1951 
1952 	return (1);
1953 }
1954 
1955 /*
1956  * Generate version needed section.
1957  */
1958 static uintptr_t
1959 make_verneed(Ofl_desc *ofl)
1960 {
1961 	Shdr		*shdr;
1962 	Elf_Data	*data;
1963 	Is_desc		*isec;
1964 
1965 	/*
1966 	 * verneed sections do not have a constant element size, so the
1967 	 * value of ent_cnt specified here (0) is meaningless.
1968 	 */
1969 	if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION),
1970 	    0, &isec, &shdr, &data) == S_ERROR)
1971 		return (S_ERROR);
1972 
1973 	/* During version processing we calculated the total size. */
1974 	data->d_size = ofl->ofl_verneedsz;
1975 	shdr->sh_size = (Xword)ofl->ofl_verneedsz;
1976 
1977 	ofl->ofl_osverneed =
1978 	    ld_place_section(ofl, isec, ld_targ.t_id.id_version, 0);
1979 	return ((uintptr_t)ofl->ofl_osverneed);
1980 }
1981 
1982 /*
1983  * Generate a version definition section.
1984  *
1985  *  o	the SHT_SUNW_verdef section defines the versions that exist within this
1986  *	image.
1987  */
1988 static uintptr_t
1989 make_verdef(Ofl_desc *ofl)
1990 {
1991 	Shdr		*shdr;
1992 	Elf_Data	*data;
1993 	Is_desc		*isec;
1994 	Ver_desc	*vdp;
1995 
1996 	/*
1997 	 * Reserve a string table entry for the base version dependency (other
1998 	 * dependencies have symbol representations, which will already be
1999 	 * accounted for during symbol processing).
2000 	 */
2001 	vdp = (Ver_desc *)ofl->ofl_verdesc.head->data;
2002 
2003 	if (ofl->ofl_flags & FLG_OF_DYNAMIC) {
2004 		if (st_insert(ofl->ofl_dynstrtab, vdp->vd_name) == -1)
2005 			return (S_ERROR);
2006 	} else {
2007 		if (st_insert(ofl->ofl_strtab, vdp->vd_name) == -1)
2008 			return (S_ERROR);
2009 	}
2010 
2011 	/*
2012 	 * verdef sections do not have a constant element size, so the
2013 	 * value of ent_cnt specified here (0) is meaningless.
2014 	 */
2015 	if (new_section(ofl, SHT_SUNW_verdef, MSG_ORIG(MSG_SCN_SUNWVERSION),
2016 	    0, &isec, &shdr, &data) == S_ERROR)
2017 		return (S_ERROR);
2018 
2019 	/* During version processing we calculated the total size. */
2020 	data->d_size = ofl->ofl_verdefsz;
2021 	shdr->sh_size = (Xword)ofl->ofl_verdefsz;
2022 
2023 	ofl->ofl_osverdef =
2024 	    ld_place_section(ofl, isec, ld_targ.t_id.id_version, 0);
2025 	return ((uintptr_t)ofl->ofl_osverdef);
2026 }
2027 
2028 /*
2029  * Common function used to build both the SHT_SUNW_versym
2030  * section and the SHT_SUNW_syminfo section.  Each of these sections
2031  * provides additional symbol information.
2032  */
2033 static Os_desc *
2034 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word stype, int ident)
2035 {
2036 	Shdr		*shdr;
2037 	Elf_Data	*data;
2038 	Is_desc		*isec;
2039 
2040 	/*
2041 	 * We don't know the size of this section yet, so set it to 0.
2042 	 * It gets filled in after the dynsym is sized.
2043 	 */
2044 	if (new_section(ofl, stype, sectname, 0, &isec, &shdr, &data) ==
2045 	    S_ERROR)
2046 		return ((Os_desc *)S_ERROR);
2047 
2048 	return (ld_place_section(ofl, isec, ident, 0));
2049 }
2050 
2051 /*
2052  * This routine is called when -z nopartial is in effect.
2053  */
2054 uintptr_t
2055 ld_make_parexpn_data(Ofl_desc *ofl, size_t size, Xword align)
2056 {
2057 	Shdr		*shdr;
2058 	Elf_Data	*data;
2059 	Is_desc		*isec;
2060 	Os_desc		*osp;
2061 
2062 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2063 	    &isec, &shdr, &data) == S_ERROR)
2064 		return (S_ERROR);
2065 
2066 	shdr->sh_flags |= SHF_WRITE;
2067 	data->d_size = size;
2068 	shdr->sh_size = (Xword)size;
2069 	if (align != 0) {
2070 		data->d_align = align;
2071 		shdr->sh_addralign = align;
2072 	}
2073 
2074 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2075 		return (S_ERROR);
2076 
2077 	/*
2078 	 * Retain handle to this .data input section. Variables using move
2079 	 * sections (partial initialization) will be redirected here when
2080 	 * such global references are added and '-z nopartial' is in effect.
2081 	 */
2082 	ofl->ofl_isparexpn = isec;
2083 	osp = ld_place_section(ofl, isec, ld_targ.t_id.id_data, 0);
2084 	if (osp == (Os_desc *)S_ERROR)
2085 		return (S_ERROR);
2086 
2087 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
2088 		ofl->ofl_dynshdrcnt++;
2089 		osp->os_flags |= FLG_OS_OUTREL;
2090 	}
2091 	return (1);
2092 }
2093 
2094 /*
2095  * Make .sunwmove section
2096  */
2097 uintptr_t
2098 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2099 {
2100 	Shdr		*shdr;
2101 	Elf_Data	*data;
2102 	Is_desc		*isec;
2103 	Listnode	*lnp1;
2104 	Psym_info	*psym;
2105 	int 		cnt = 1;
2106 
2107 
2108 	if (new_section(ofl, SHT_SUNW_move, MSG_ORIG(MSG_SCN_SUNWMOVE),
2109 	    mv_nums, &isec, &shdr, &data) == S_ERROR)
2110 		return (S_ERROR);
2111 
2112 	if ((data->d_buf = libld_calloc(data->d_size, 1)) == 0)
2113 		return (S_ERROR);
2114 
2115 	/*
2116 	 * Copy move entries
2117 	 */
2118 	for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) {
2119 		Listnode	*lnp2;
2120 		Mv_itm		*mvitm;
2121 
2122 		if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN)
2123 			continue;
2124 		for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvitm)) {
2125 			if ((mvitm->mv_flag & FLG_MV_OUTSECT) == 0)
2126 				continue;
2127 			mvitm->mv_oidx = cnt;
2128 			cnt++;
2129 		}
2130 	}
2131 	if ((ofl->ofl_osmove = ld_place_section(ofl, isec, 0, 0)) ==
2132 	    (Os_desc *)S_ERROR)
2133 		return (S_ERROR);
2134 
2135 	return (1);
2136 }
2137 
2138 
2139 /*
2140  * Given a relocation descriptor that references a string table
2141  * input section, locate the string referenced and return a pointer
2142  * to it.
2143  */
2144 static const char *
2145 strmerge_get_reloc_str(Ofl_desc *ofl, Rel_desc *rsp)
2146 {
2147 	Sym_desc *sdp = rsp->rel_sym;
2148 	Xword	 str_off;
2149 
2150 	/*
2151 	 * In the case of an STT_SECTION symbol, the addend of the
2152 	 * relocation gives the offset into the string section. For
2153 	 * other symbol types, the symbol value is the offset.
2154 	 */
2155 
2156 	if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
2157 		str_off = sdp->sd_sym->st_value;
2158 	} else if ((rsp->rel_flags & FLG_REL_RELA) == FLG_REL_RELA) {
2159 		/*
2160 		 * For SHT_RELA, the addend value is found in the
2161 		 * rel_raddend field of the relocation.
2162 		 */
2163 		str_off = rsp->rel_raddend;
2164 	} else {	/* REL and STT_SECTION */
2165 		/*
2166 		 * For SHT_REL, the "addend" is not part of the relocation
2167 		 * record. Instead, it is found at the relocation target
2168 		 * address.
2169 		 */
2170 		uchar_t *addr = (uchar_t *)((uintptr_t)rsp->rel_roffset +
2171 		    (uintptr_t)rsp->rel_isdesc->is_indata->d_buf);
2172 
2173 		if (ld_reloc_targval_get(ofl, rsp, addr, &str_off) == 0)
2174 			return (0);
2175 	}
2176 
2177 	return (str_off + (char *)sdp->sd_isc->is_indata->d_buf);
2178 }
2179 
2180 /*
2181  * First pass over the relocation records for string table merging.
2182  * Build lists of relocations and symbols that will need modification,
2183  * and insert the strings they reference into the mstrtab string table.
2184  *
2185  * entry:
2186  *	ofl, osp - As passed to ld_make_strmerge().
2187  *	mstrtab - String table to receive input strings. This table
2188  *		must be in its first (initialization) pass and not
2189  *		yet cooked (st_getstrtab_sz() not yet called).
2190  *	rel_aplist - APlist to receive pointer to any relocation
2191  *		descriptors with STT_SECTION symbols that reference
2192  *		one of the input sections being merged.
2193  *	sym_aplist - APlist to receive pointer to any symbols that reference
2194  *		one of the input sections being merged.
2195  *	reloc_list - List of relocation descriptors to examine.
2196  *		Either ofl->&ofl->ofl_actrels (active relocations)
2197  *		or &ofl->ofl_outrels (output relocations).
2198  *
2199  * exit:
2200  *	On success, rel_aplist and sym_aplist are updated, and
2201  *	any strings in the mergable input sections referenced by
2202  *	a relocation has been entered into mstrtab. True (1) is returned.
2203  *
2204  *	On failure, False (0) is returned.
2205  */
2206 static int
2207 strmerge_pass1(Ofl_desc *ofl, Os_desc *osp, Str_tbl *mstrtab,
2208     APlist **rel_aplist, APlist **sym_aplist, List *reloc_list)
2209 {
2210 	Listnode	*lnp;
2211 	Rel_cache	*rcp;
2212 	Sym_desc	*sdp;
2213 	Sym_desc	*last_sdp = NULL;
2214 	Rel_desc	*rsp;
2215 	const char	*name;
2216 
2217 	for (LIST_TRAVERSE(reloc_list, lnp, rcp)) {
2218 		/* LINTED */
2219 		for (rsp = (Rel_desc *)(rcp + 1); rsp < rcp->rc_free; rsp++) {
2220 			sdp = rsp->rel_sym;
2221 			if ((sdp->sd_isc == NULL) ||
2222 			    ((sdp->sd_isc->is_flags &
2223 			    (FLG_IS_DISCARD | FLG_IS_INSTRMRG)) !=
2224 			    FLG_IS_INSTRMRG) ||
2225 			    (sdp->sd_isc->is_osdesc != osp))
2226 				continue;
2227 
2228 			/*
2229 			 * Remember symbol for use in the third pass.
2230 			 * There is no reason to save a given symbol more
2231 			 * than once, so we take advantage of the fact that
2232 			 * relocations to a given symbol tend to cluster
2233 			 * in the list. If this is the same symbol we saved
2234 			 * last time, don't bother.
2235 			 */
2236 			if (last_sdp != sdp) {
2237 				if (aplist_append(sym_aplist, sdp,
2238 				    AL_CNT_STRMRGSYM) == 0)
2239 					return (0);
2240 				last_sdp = sdp;
2241 			}
2242 
2243 			/* Enter the string into our new string table */
2244 			name = strmerge_get_reloc_str(ofl, rsp);
2245 			if (st_insert(mstrtab, name) == -1)
2246 				return (0);
2247 
2248 			/*
2249 			 * If this is an STT_SECTION symbol, then the
2250 			 * second pass will need to modify this relocation,
2251 			 * so hang on to it.
2252 			 */
2253 			if ((ELF_ST_TYPE(sdp->sd_sym->st_info) ==
2254 			    STT_SECTION) &&
2255 			    (aplist_append(rel_aplist, rsp,
2256 			    AL_CNT_STRMRGREL) == 0))
2257 				return (0);
2258 		}
2259 	}
2260 
2261 	return (1);
2262 }
2263 
2264 /*
2265  * If the output section has any SHF_MERGE|SHF_STRINGS input sections,
2266  * replace them with a single merged/compressed input section.
2267  *
2268  * entry:
2269  *	ofl - Output file descriptor
2270  *	osp - Output section descriptor
2271  *	rel_aplist, sym_aplist, - Address of 2 APlists, to be used
2272  *		for internal processing. On the initial call to
2273  *		ld_make_strmerge, these list pointers must be NULL.
2274  *		The caller is encouraged to pass the same lists back for
2275  *		successive calls to this function without freeing
2276  *		them in between calls. This causes a single pair of
2277  *		memory allocations to be reused multiple times.
2278  *
2279  * exit:
2280  *	If section merging is possible, it is done. If no errors are
2281  *	encountered, True (1) is returned. On error, S_ERROR.
2282  *
2283  *	The contents of rel_aplist and sym_aplist on exit are
2284  *	undefined. The caller can free them, or pass them back to a subsequent
2285  *	call to this routine, but should not examine their contents.
2286  */
2287 static uintptr_t
2288 ld_make_strmerge(Ofl_desc *ofl, Os_desc *osp, APlist **rel_aplist,
2289     APlist **sym_aplist)
2290 {
2291 	Str_tbl		*mstrtab;	/* string table for string merge secs */
2292 	Is_desc		*mstrsec;	/* Generated string merge section */
2293 	Is_desc		*isp;
2294 	Shdr		*mstr_shdr;
2295 	Elf_Data	*mstr_data;
2296 	Sym_desc	*sdp;
2297 	Rel_desc	*rsp;
2298 	Aliste		idx;
2299 	size_t		data_size;
2300 	int		st_setstring_status;
2301 	size_t		stoff;
2302 
2303 	/* If string table compression is disabled, there's nothing to do */
2304 	if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) != 0)
2305 		return (1);
2306 
2307 	/*
2308 	 * Pass over the mergeable input sections, and if they haven't
2309 	 * all been discarded, create a string table.
2310 	 */
2311 	mstrtab = NULL;
2312 	for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
2313 		if (isp->is_flags & FLG_IS_DISCARD)
2314 			continue;
2315 
2316 		/*
2317 		 * We have at least one non-discarded section.
2318 		 * Create a string table descriptor.
2319 		 */
2320 		if ((mstrtab = st_new(FLG_STNEW_COMPRESS)) == NULL)
2321 			return (S_ERROR);
2322 		break;
2323 	}
2324 
2325 	/* If no string table was created, we have no mergeable sections */
2326 	if (mstrtab == NULL)
2327 		return (1);
2328 
2329 	/*
2330 	 * This routine has to make 3 passes:
2331 	 *
2332 	 *	1) Examine all relocations, insert strings from relocations
2333 	 *		to the mergable input sections into the string table.
2334 	 *	2) Modify the relocation values to be correct for the
2335 	 *		new merged section.
2336 	 *	3) Modify the symbols used by the relocations to reference
2337 	 *		the new section.
2338 	 *
2339 	 * These passes cannot be combined:
2340 	 *	- The string table code works in two passes, and all
2341 	 *		strings have to be loaded in pass one before the
2342 	 *		offset of any strings can be determined.
2343 	 *	- Multiple relocations reference a single symbol, so the
2344 	 *		symbol cannot be modified until all relocations are
2345 	 *		fixed.
2346 	 *
2347 	 * The number of relocations related to section merging is usually
2348 	 * a mere fraction of the overall active and output relocation lists,
2349 	 * and the number of symbols is usually a fraction of the number
2350 	 * of related relocations. We therefore build APlists for the
2351 	 * relocations and symbols in the first pass, and then use those
2352 	 * lists to accelerate the operation of pass 2 and 3.
2353 	 *
2354 	 * Reinitialize the lists to a completely empty state.
2355 	 */
2356 	aplist_reset(*rel_aplist);
2357 	aplist_reset(*sym_aplist);
2358 
2359 	/*
2360 	 * Pass 1:
2361 	 *
2362 	 * Every relocation related to this output section (and the input
2363 	 * sections that make it up) is found in either the active, or the
2364 	 * output relocation list, depending on whether the relocation is to
2365 	 * be processed by this invocation of the linker, or inserted into the
2366 	 * output object.
2367 	 *
2368 	 * Build lists of relocations and symbols that will need modification,
2369 	 * and insert the strings they reference into the mstrtab string table.
2370 	 */
2371 	if (strmerge_pass1(ofl, osp, mstrtab, rel_aplist, sym_aplist,
2372 	    &ofl->ofl_actrels) == 0)
2373 		goto return_s_error;
2374 	if (strmerge_pass1(ofl, osp, mstrtab, rel_aplist, sym_aplist,
2375 	    &ofl->ofl_outrels) == 0)
2376 		goto return_s_error;
2377 
2378 	/*
2379 	 * Get the size of the new input section. Requesting the
2380 	 * string table size "cooks" the table, and finalizes its contents.
2381 	 */
2382 	data_size = st_getstrtab_sz(mstrtab);
2383 
2384 	/* Create a new input section to hold the merged strings */
2385 	if (new_section_from_template(ofl, isp, data_size,
2386 	    &mstrsec, &mstr_shdr, &mstr_data) == S_ERROR)
2387 		goto return_s_error;
2388 	mstrsec->is_flags |= FLG_IS_GNSTRMRG;
2389 
2390 	/*
2391 	 * Allocate a data buffer for the new input section.
2392 	 * Then, associate the buffer with the string table descriptor.
2393 	 */
2394 	if ((mstr_data->d_buf = libld_malloc(data_size)) == 0)
2395 		goto return_s_error;
2396 	if (st_setstrbuf(mstrtab, mstr_data->d_buf, data_size) == -1)
2397 		goto return_s_error;
2398 
2399 	/* Add the new section to the output image */
2400 	if (ld_place_section(ofl, mstrsec, osp->os_scnsymndx, 0) ==
2401 	    (Os_desc *)S_ERROR)
2402 		goto return_s_error;
2403 
2404 	/*
2405 	 * Pass 2:
2406 	 *
2407 	 * Revisit the relocation descriptors with STT_SECTION symbols
2408 	 * that were saved by the first pass. Update each relocation
2409 	 * record so that the offset it contains is for the new section
2410 	 * instead of the original.
2411 	 */
2412 	for (APLIST_TRAVERSE(*rel_aplist, idx, rsp)) {
2413 		const char	*name;
2414 
2415 		/* Put the string into the merged string table */
2416 		name = strmerge_get_reloc_str(ofl, rsp);
2417 		st_setstring_status = st_setstring(mstrtab, name, &stoff);
2418 		if (st_setstring_status == -1) {
2419 			/*
2420 			 * A failure to insert at this point means that
2421 			 * something is corrupt. This isn't a resource issue.
2422 			 */
2423 			assert(st_setstring_status != -1);
2424 			goto return_s_error;
2425 		}
2426 
2427 		/*
2428 		 * Alter the relocation to access the string at the
2429 		 * new offset in our new string table.
2430 		 *
2431 		 * For SHT_RELA platforms, it suffices to simply
2432 		 * update the rel_raddend field of the relocation.
2433 		 *
2434 		 * For SHT_REL platforms, the new "addend" value
2435 		 * needs to be written at the address being relocated.
2436 		 * However, we can't alter the input sections which
2437 		 * are mapped readonly, and the output image has not
2438 		 * been created yet. So, we defer this operation,
2439 		 * using the rel_raddend field of the relocation
2440 		 * which is normally 0 on a REL platform, to pass the
2441 		 * new "addend" value to ld_perform_outreloc() or
2442 		 * ld_do_activerelocs(). The FLG_REL_NADDEND flag
2443 		 * tells them that this is the case.
2444 		 */
2445 		if ((rsp->rel_flags & FLG_REL_RELA) == 0)   /* REL */
2446 			rsp->rel_flags |= FLG_REL_NADDEND;
2447 		rsp->rel_raddend = (Sxword)stoff;
2448 
2449 		/*
2450 		 * Change the descriptor name to reflect the fact that it
2451 		 * points at our merged section. This shows up in debug
2452 		 * output and helps show how the relocation has changed
2453 		 * from its original input section to our merged one.
2454 		 */
2455 		rsp->rel_sname = ld_section_reld_name(rsp->rel_sym, mstrsec);
2456 		if (rsp->rel_sname == NULL)
2457 			goto return_s_error;
2458 	}
2459 
2460 	/*
2461 	 * Pass 3:
2462 	 *
2463 	 * Modify the symbols referenced by the relocation descriptors
2464 	 * so that they reference the new input section containing the
2465 	 * merged strings instead of the original input sections.
2466 	 */
2467 	for (APLIST_TRAVERSE(*sym_aplist, idx, sdp)) {
2468 		/*
2469 		 * If we've already processed this symbol, don't do it
2470 		 * twice. strmerge_pass1() uses a heuristic (relocations to
2471 		 * the same symbol clump together) to avoid inserting a
2472 		 * given symbol more than once, but repeat symbols in
2473 		 * the list can occur.
2474 		 */
2475 		if ((sdp->sd_isc->is_flags & FLG_IS_INSTRMRG) == 0)
2476 			continue;
2477 
2478 		if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_SECTION) {
2479 			/*
2480 			 * This is not an STT_SECTION symbol, so its
2481 			 * value is the offset of the string within the
2482 			 * input section. Update the address to reflect
2483 			 * the address in our new merged section.
2484 			 */
2485 			const char *name = sdp->sd_sym->st_value +
2486 			    (char *)sdp->sd_isc->is_indata->d_buf;
2487 
2488 			st_setstring_status =
2489 			    st_setstring(mstrtab, name, &stoff);
2490 			if (st_setstring_status == -1) {
2491 				/*
2492 				 * A failure to insert at this point means
2493 				 * something is corrupt. This isn't a
2494 				 * resource issue.
2495 				 */
2496 				assert(st_setstring_status != -1);
2497 				goto return_s_error;
2498 			}
2499 
2500 			if (ld_sym_copy(sdp) == S_ERROR)
2501 				goto return_s_error;
2502 			sdp->sd_sym->st_value = (Word)stoff;
2503 		}
2504 
2505 		/* Redirect the symbol to our new merged section */
2506 		sdp->sd_isc = mstrsec;
2507 	}
2508 
2509 	/*
2510 	 * There are no references left to the original input string sections.
2511 	 * Mark them as discarded so they don't go into the output image.
2512 	 * At the same time, add up the sizes of the replaced sections.
2513 	 */
2514 	data_size = 0;
2515 	for (APLIST_TRAVERSE(osp->os_mstrisdescs, idx, isp)) {
2516 		if (isp->is_flags & (FLG_IS_DISCARD | FLG_IS_GNSTRMRG))
2517 			continue;
2518 
2519 		data_size += isp->is_indata->d_size;
2520 
2521 		isp->is_flags |= FLG_IS_DISCARD;
2522 		DBG_CALL(Dbg_sec_discarded(ofl->ofl_lml, isp, mstrsec));
2523 	}
2524 
2525 	/* Report how much space we saved in the output section */
2526 	Dbg_sec_genstr_compress(ofl->ofl_lml, osp->os_name, data_size,
2527 	    mstr_data->d_size);
2528 
2529 	st_destroy(mstrtab);
2530 	return (1);
2531 
2532 return_s_error:
2533 	st_destroy(mstrtab);
2534 	return (S_ERROR);
2535 }
2536 
2537 
2538 /*
2539  * The following sections are built after all input file processing and symbol
2540  * validation has been carried out.  The order is important (because the
2541  * addition of a section adds a new symbol there is a chicken and egg problem
2542  * of maintaining the appropriate counts).  By maintaining a known order the
2543  * individual routines can compensate for later, known, additions.
2544  */
2545 uintptr_t
2546 ld_make_sections(Ofl_desc *ofl)
2547 {
2548 	ofl_flag_t	flags = ofl->ofl_flags;
2549 	Listnode	*lnp1;
2550 	Sg_desc		*sgp;
2551 
2552 	/*
2553 	 * Generate any special sections.
2554 	 */
2555 	if (flags & FLG_OF_ADDVERS)
2556 		if (make_comment(ofl) == S_ERROR)
2557 			return (S_ERROR);
2558 
2559 	if (make_interp(ofl) == S_ERROR)
2560 		return (S_ERROR);
2561 
2562 	if (make_cap(ofl) == S_ERROR)
2563 		return (S_ERROR);
2564 
2565 	if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
2566 	    &ofl->ofl_initarray) == S_ERROR)
2567 		return (S_ERROR);
2568 
2569 	if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
2570 	    &ofl->ofl_finiarray) == S_ERROR)
2571 		return (S_ERROR);
2572 
2573 	if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
2574 	    &ofl->ofl_preiarray) == S_ERROR)
2575 		return (S_ERROR);
2576 
2577 	/*
2578 	 * Make the .plt section.  This occurs after any other relocation
2579 	 * sections are generated (see reloc_init()) to ensure that the
2580 	 * associated relocation section is after all the other relocation
2581 	 * sections.
2582 	 */
2583 	if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
2584 		if (make_plt(ofl) == S_ERROR)
2585 			return (S_ERROR);
2586 
2587 	/*
2588 	 * Determine whether any sections or files are not referenced.  Under
2589 	 * -Dunused a diagnostic for any unused components is generated, under
2590 	 * -zignore the component is removed from the final output.
2591 	 */
2592 	if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
2593 		if (ignore_section_processing(ofl) == S_ERROR)
2594 			return (S_ERROR);
2595 	}
2596 
2597 	/*
2598 	 * Do any of the output sections contain input sections that
2599 	 * are candidates for string table merging? For each such case,
2600 	 * we create a replacement section, insert it, and discard the
2601 	 * originals.
2602 	 *
2603 	 * rel_aplist and sym_aplist are used by ld_make_strmerge()
2604 	 * for its internal processing. We are responsible for the
2605 	 * initialization and cleanup, and ld_make_strmerge() handles the rest.
2606 	 * This allows us to reuse a single pair of memory buffers allocatated
2607 	 * for this processing for all the output sections.
2608 	 */
2609 	if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) == 0) {
2610 		int error_seen = 0;
2611 		APlist *rel_aplist = NULL;
2612 		APlist *sym_aplist = NULL;
2613 
2614 		for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2615 			Os_desc	*osp;
2616 			Aliste	idx;
2617 
2618 			for (APLIST_TRAVERSE(sgp->sg_osdescs, idx, osp))
2619 				if ((osp->os_mstrisdescs != NULL) &&
2620 				    (ld_make_strmerge(ofl, osp,
2621 				    &rel_aplist, &sym_aplist) ==
2622 				    S_ERROR)) {
2623 					error_seen = 1;
2624 					break;
2625 				}
2626 		}
2627 		if (rel_aplist != NULL)
2628 			free(rel_aplist);
2629 		if (sym_aplist != NULL)
2630 			free(sym_aplist);
2631 		if (error_seen != 0)
2632 			return (S_ERROR);
2633 	}
2634 
2635 	/*
2636 	 * Add any necessary versioning information.
2637 	 */
2638 	if (!(flags & FLG_OF_NOVERSEC)) {
2639 		if ((flags & FLG_OF_VERNEED) &&
2640 		    (make_verneed(ofl) == S_ERROR))
2641 			return (S_ERROR);
2642 		if ((flags & FLG_OF_VERDEF) &&
2643 		    (make_verdef(ofl) == S_ERROR))
2644 			return (S_ERROR);
2645 		if ((flags & (FLG_OF_VERNEED | FLG_OF_VERDEF)) &&
2646 		    ((ofl->ofl_osversym = make_sym_sec(ofl,
2647 		    MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym,
2648 		    ld_targ.t_id.id_version)) == (Os_desc*)S_ERROR))
2649 			return (S_ERROR);
2650 	}
2651 
2652 	/*
2653 	 * Create a syminfo section if necessary.
2654 	 */
2655 	if (flags & FLG_OF_SYMINFO) {
2656 		if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
2657 		    MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo,
2658 		    ld_targ.t_id.id_syminfo)) == (Os_desc *)S_ERROR)
2659 			return (S_ERROR);
2660 	}
2661 
2662 	if (flags & FLG_OF_COMREL) {
2663 		/*
2664 		 * If -zcombreloc is enabled then all relocations (except for
2665 		 * the PLT's) are coalesced into a single relocation section.
2666 		 */
2667 		if (ofl->ofl_reloccnt) {
2668 			if (make_reloc(ofl, NULL) == S_ERROR)
2669 				return (S_ERROR);
2670 		}
2671 	} else {
2672 		/*
2673 		 * Create the required output relocation sections.  Note, new
2674 		 * sections may be added to the section list that is being
2675 		 * traversed.  These insertions can move the elements of the
2676 		 * Alist such that a section descriptor is re-read.  Recursion
2677 		 * is prevented by maintaining a previous section pointer and
2678 		 * insuring that this pointer isn't re-examined.
2679 		 */
2680 		for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2681 			Os_desc	*osp, *posp = 0;
2682 			Aliste	idx;
2683 
2684 			for (APLIST_TRAVERSE(sgp->sg_osdescs, idx, osp)) {
2685 				if ((osp != posp) && osp->os_szoutrels &&
2686 				    (osp != ofl->ofl_osplt)) {
2687 					if (make_reloc(ofl, osp) == S_ERROR)
2688 						return (S_ERROR);
2689 				}
2690 				posp = osp;
2691 			}
2692 		}
2693 
2694 		/*
2695 		 * If we're not building a combined relocation section, then
2696 		 * build a .rel[a] section as required.
2697 		 */
2698 		if (ofl->ofl_relocrelsz) {
2699 			if (make_reloc(ofl, NULL) == S_ERROR)
2700 				return (S_ERROR);
2701 		}
2702 	}
2703 
2704 	/*
2705 	 * The PLT relocations are always in their own section, and we try to
2706 	 * keep them at the end of the PLT table.  We do this to keep the hot
2707 	 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
2708 	 */
2709 	if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
2710 		if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
2711 			return (S_ERROR);
2712 	}
2713 
2714 	/*
2715 	 * Finally build the symbol and section header sections.
2716 	 */
2717 	if (flags & FLG_OF_DYNAMIC) {
2718 		if (make_dynamic(ofl) == S_ERROR)
2719 			return (S_ERROR);
2720 		if (make_dynstr(ofl) == S_ERROR)
2721 			return (S_ERROR);
2722 		/*
2723 		 * There is no use for .hash and .dynsym sections in a
2724 		 * relocatable object.
2725 		 */
2726 		if (!(flags & FLG_OF_RELOBJ)) {
2727 			if (make_hash(ofl) == S_ERROR)
2728 				return (S_ERROR);
2729 			if (make_dynsym(ofl) == S_ERROR)
2730 				return (S_ERROR);
2731 #if	defined(_ELF64)
2732 			if ((ld_targ.t_uw.uw_make_unwindhdr != NULL) &&
2733 			    ((*ld_targ.t_uw.uw_make_unwindhdr)(ofl) == S_ERROR))
2734 				return (S_ERROR);
2735 #endif
2736 			if (make_dynsort(ofl) == S_ERROR)
2737 				return (S_ERROR);
2738 		}
2739 	}
2740 
2741 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
2742 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
2743 		/*
2744 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2745 		 * for the dynsym.  If so - do it now.
2746 		 */
2747 		if (ofl->ofl_osdynsym &&
2748 		    ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
2749 			if (make_dynsym_shndx(ofl) == S_ERROR)
2750 				return (S_ERROR);
2751 		}
2752 
2753 		if (make_strtab(ofl) == S_ERROR)
2754 			return (S_ERROR);
2755 		if (make_symtab(ofl) == S_ERROR)
2756 			return (S_ERROR);
2757 	} else {
2758 		/*
2759 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2760 		 * for the dynsym.  If so - do it now.
2761 		 */
2762 		if (ofl->ofl_osdynsym &&
2763 		    ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
2764 			if (make_dynsym_shndx(ofl) == S_ERROR)
2765 				return (S_ERROR);
2766 		}
2767 	}
2768 
2769 	if (make_shstrtab(ofl) == S_ERROR)
2770 		return (S_ERROR);
2771 
2772 	/*
2773 	 * Now that we've created all of our sections adjust the size
2774 	 * of SHT_SUNW_versym & SHT_SUNW_syminfo which are dependent on
2775 	 * the symbol table sizes.
2776 	 */
2777 	if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
2778 		Shdr		*shdr;
2779 		Is_desc		*isec;
2780 		Elf_Data	*data;
2781 		size_t		size;
2782 		ulong_t		cnt;
2783 		Os_desc		*osp;
2784 
2785 		if (flags & (FLG_OF_RELOBJ | FLG_OF_STATIC)) {
2786 			osp = ofl->ofl_ossymtab;
2787 		} else {
2788 			osp = ofl->ofl_osdynsym;
2789 		}
2790 		isec = (Is_desc *)osp->os_isdescs.head->data;
2791 		cnt = (isec->is_shdr->sh_size / isec->is_shdr->sh_entsize);
2792 
2793 		if (ofl->ofl_osversym) {
2794 			osp = ofl->ofl_osversym;
2795 			isec = (Is_desc *)osp->os_isdescs.head->data;
2796 			data = isec->is_indata;
2797 			shdr = osp->os_shdr;
2798 			size = cnt * shdr->sh_entsize;
2799 			shdr->sh_size = (Xword)size;
2800 			data->d_size = size;
2801 		}
2802 		if (ofl->ofl_ossyminfo) {
2803 			osp = ofl->ofl_ossyminfo;
2804 			isec = (Is_desc *)osp->os_isdescs.head->data;
2805 			data = isec->is_indata;
2806 			shdr = osp->os_shdr;
2807 			size = cnt * shdr->sh_entsize;
2808 			shdr->sh_size = (Xword)size;
2809 			data->d_size = size;
2810 		}
2811 	}
2812 
2813 	return (1);
2814 }
2815 
2816 /*
2817  * Build an additional data section - used to back OBJT symbol definitions
2818  * added with a mapfile.
2819  */
2820 Is_desc *
2821 ld_make_data(Ofl_desc *ofl, size_t size)
2822 {
2823 	Shdr		*shdr;
2824 	Elf_Data	*data;
2825 	Is_desc		*isec;
2826 
2827 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2828 	    &isec, &shdr, &data) == S_ERROR)
2829 		return ((Is_desc *)S_ERROR);
2830 
2831 	data->d_size = size;
2832 	shdr->sh_size = (Xword)size;
2833 	shdr->sh_flags |= SHF_WRITE;
2834 
2835 	if (ld_place_section(ofl, isec, ld_targ.t_id.id_data, 0) ==
2836 	    (Os_desc *)S_ERROR)
2837 		return ((Is_desc *)S_ERROR);
2838 
2839 	return (isec);
2840 }
2841 
2842 
2843 /*
2844  * Build an additional text section - used to back FUNC symbol definitions
2845  * added with a mapfile.
2846  */
2847 Is_desc *
2848 ld_make_text(Ofl_desc *ofl, size_t size)
2849 {
2850 	Shdr		*shdr;
2851 	Elf_Data	*data;
2852 	Is_desc		*isec;
2853 
2854 	/*
2855 	 * Insure the size is sufficient to contain the minimum return
2856 	 * instruction.
2857 	 */
2858 	if (size < ld_targ.t_nf.nf_size)
2859 		size = ld_targ.t_nf.nf_size;
2860 
2861 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0,
2862 	    &isec, &shdr, &data) == S_ERROR)
2863 		return ((Is_desc *)S_ERROR);
2864 
2865 	data->d_size = size;
2866 	shdr->sh_size = (Xword)size;
2867 	shdr->sh_flags |= SHF_EXECINSTR;
2868 
2869 	/*
2870 	 * Fill the buffer with the appropriate return instruction.
2871 	 * Note that there is no need to swap bytes on a non-native,
2872 	 * link, as the data being copied is given in bytes.
2873 	 */
2874 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2875 		return ((Is_desc *)S_ERROR);
2876 	(void) memcpy(data->d_buf, ld_targ.t_nf.nf_template,
2877 	    ld_targ.t_nf.nf_size);
2878 
2879 	if (ld_place_section(ofl, isec, ld_targ.t_id.id_text, 0) ==
2880 	    (Os_desc *)S_ERROR)
2881 		return ((Is_desc *)S_ERROR);
2882 
2883 	return (isec);
2884 }
2885