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