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_flags |= FLG_SY_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_flags & FLG_SY_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 &
246 			    (FLG_SY_ISDISC | FLG_SY_INVALID | FLG_SY_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 	Str_tbl		*strtbl;
911 	ofl_flag_t	flags = ofl->ofl_flags;
912 	int		not_relobj = !(flags & FLG_OF_RELOBJ);
913 	int		unused = 0;
914 
915 	/*
916 	 * Select the required string table.
917 	 */
918 	if (OFL_IS_STATIC_OBJ(ofl))
919 		strtbl = ofl->ofl_strtab;
920 	else
921 		strtbl = ofl->ofl_dynstrtab;
922 
923 	/*
924 	 * Only a limited subset of DT_ entries apply to relocatable
925 	 * objects. See the comment at the head of update_odynamic() in
926 	 * update.c for details.
927 	 */
928 	if (new_section(ofl, SHT_DYNAMIC, MSG_ORIG(MSG_SCN_DYNAMIC), 0,
929 	    &isec, &shdr, &data) == S_ERROR)
930 		return (S_ERROR);
931 
932 	/* new_section() does not set SHF_ALLOC. Add it if needed */
933 	if (not_relobj)
934 		shdr->sh_flags |= SHF_ALLOC;
935 
936 	osp = ofl->ofl_osdynamic =
937 	    ld_place_section(ofl, isec, ld_targ.t_id.id_dynamic, NULL);
938 
939 	/*
940 	 * Reserve entries for any needed dependencies.
941 	 */
942 	for (APLIST_TRAVERSE(ofl->ofl_sos, idx, ifl)) {
943 		if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR)))
944 			continue;
945 
946 		/*
947 		 * If this dependency didn't satisfy any symbol references,
948 		 * generate a debugging diagnostic (ld(1) -Dunused can be used
949 		 * to display these).  If this is a standard needed dependency,
950 		 * and -z ignore is in effect, drop the dependency.  Explicitly
951 		 * defined dependencies (i.e., -N dep) don't get dropped, and
952 		 * are flagged as being required to simplify update_odynamic()
953 		 * processing.
954 		 */
955 		if ((ifl->ifl_flags & FLG_IF_NEEDSTR) ||
956 		    ((ifl->ifl_flags & FLG_IF_DEPREQD) == 0)) {
957 			if (unused++ == 0)
958 				DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
959 			DBG_CALL(Dbg_unused_file(ofl->ofl_lml, ifl->ifl_soname,
960 			    (ifl->ifl_flags & FLG_IF_NEEDSTR), 0));
961 
962 			if (ifl->ifl_flags & FLG_IF_NEEDSTR)
963 				ifl->ifl_flags |= FLG_IF_DEPREQD;
964 			else if (ifl->ifl_flags & FLG_IF_IGNORE)
965 				continue;
966 		}
967 
968 		/*
969 		 * If this object is a lazyload reserve a DT_POSFLAG_1 entry.
970 		 */
971 		if ((ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_GRPPRM)) &&
972 		    not_relobj)
973 			cnt++;
974 
975 		if (st_insert(strtbl, ifl->ifl_soname) == -1)
976 			return (S_ERROR);
977 		cnt++;
978 
979 		/*
980 		 * If the needed entry contains the $ORIGIN token make sure
981 		 * the associated DT_1_FLAGS entry is created.
982 		 */
983 		if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) {
984 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
985 			ofl->ofl_dtflags |= DF_ORIGIN;
986 		}
987 	}
988 
989 	if (unused)
990 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
991 
992 	if (not_relobj) {
993 		/*
994 		 * Reserve entries for any per-symbol auxiliary/filter strings.
995 		 */
996 		cnt += alist_nitems(ofl->ofl_dtsfltrs);
997 
998 		/*
999 		 * Reserve entries for _init() and _fini() section addresses.
1000 		 */
1001 		if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
1002 		    SYM_NOHASH, NULL, ofl)) != NULL) &&
1003 		    (sdp->sd_ref == REF_REL_NEED) &&
1004 		    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1005 			sdp->sd_flags |= FLG_SY_UPREQD;
1006 			cnt++;
1007 		}
1008 		if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
1009 		    SYM_NOHASH, NULL, ofl)) != NULL) &&
1010 		    (sdp->sd_ref == REF_REL_NEED) &&
1011 		    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1012 			sdp->sd_flags |= FLG_SY_UPREQD;
1013 			cnt++;
1014 		}
1015 
1016 		/*
1017 		 * Reserve entries for any soname, filter name (shared libs
1018 		 * only), run-path pointers, cache names and audit requirements.
1019 		 */
1020 		if (ofl->ofl_soname) {
1021 			cnt++;
1022 			if (st_insert(strtbl, ofl->ofl_soname) == -1)
1023 				return (S_ERROR);
1024 		}
1025 		if (ofl->ofl_filtees) {
1026 			cnt++;
1027 			if (st_insert(strtbl, ofl->ofl_filtees) == -1)
1028 				return (S_ERROR);
1029 
1030 			/*
1031 			 * If the filtees entry contains the $ORIGIN token
1032 			 * make sure the associated DT_1_FLAGS entry is created.
1033 			 */
1034 			if (strstr(ofl->ofl_filtees,
1035 			    MSG_ORIG(MSG_STR_ORIGIN))) {
1036 				ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1037 				ofl->ofl_dtflags |= DF_ORIGIN;
1038 			}
1039 		}
1040 	}
1041 
1042 	if (ofl->ofl_rpath) {
1043 		cnt += 2;	/* DT_RPATH & DT_RUNPATH */
1044 		if (st_insert(strtbl, ofl->ofl_rpath) == -1)
1045 			return (S_ERROR);
1046 
1047 		/*
1048 		 * If the rpath entry contains the $ORIGIN token make sure
1049 		 * the associated DT_1_FLAGS entry is created.
1050 		 */
1051 		if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) {
1052 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1053 			ofl->ofl_dtflags |= DF_ORIGIN;
1054 		}
1055 	}
1056 
1057 	if (not_relobj) {
1058 		Aliste	idx;
1059 
1060 		if (ofl->ofl_config) {
1061 			cnt++;
1062 			if (st_insert(strtbl, ofl->ofl_config) == -1)
1063 				return (S_ERROR);
1064 
1065 			/*
1066 			 * If the config entry contains the $ORIGIN token
1067 			 * make sure the associated DT_1_FLAGS entry is created.
1068 			 */
1069 			if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) {
1070 				ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
1071 				ofl->ofl_dtflags |= DF_ORIGIN;
1072 			}
1073 		}
1074 		if (ofl->ofl_depaudit) {
1075 			cnt++;
1076 			if (st_insert(strtbl, ofl->ofl_depaudit) == -1)
1077 				return (S_ERROR);
1078 		}
1079 		if (ofl->ofl_audit) {
1080 			cnt++;
1081 			if (st_insert(strtbl, ofl->ofl_audit) == -1)
1082 				return (S_ERROR);
1083 		}
1084 
1085 		/*
1086 		 * Reserve entries for the HASH, STRTAB, STRSZ, SYMTAB, SYMENT,
1087 		 * and CHECKSUM.
1088 		 */
1089 		cnt += 6;
1090 
1091 		/*
1092 		 * If we are including local functions at the head of
1093 		 * the dynsym, then also reserve entries for DT_SUNW_SYMTAB
1094 		 * and DT_SUNW_SYMSZ.
1095 		 */
1096 		if (OFL_ALLOW_LDYNSYM(ofl))
1097 			cnt += 2;
1098 
1099 		if ((ofl->ofl_dynsymsortcnt > 0) ||
1100 		    (ofl->ofl_dyntlssortcnt > 0))
1101 			cnt++;		/* DT_SUNW_SORTENT */
1102 
1103 		if (ofl->ofl_dynsymsortcnt > 0)
1104 			cnt += 2;	/* DT_SUNW_[SYMSORT|SYMSORTSZ] */
1105 
1106 		if (ofl->ofl_dyntlssortcnt > 0)
1107 			cnt += 2;	/* DT_SUNW_[TLSSORT|TLSSORTSZ] */
1108 
1109 		if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
1110 		    FLG_OF_VERDEF)
1111 			cnt += 2;		/* DT_VERDEF & DT_VERDEFNUM */
1112 
1113 		if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
1114 		    FLG_OF_VERNEED)
1115 			cnt += 2;		/* DT_VERNEED & DT_VERNEEDNUM */
1116 
1117 		if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt)
1118 			cnt++;			/* RELACOUNT */
1119 
1120 		if (flags & FLG_OF_TEXTREL)	/* TEXTREL */
1121 			cnt++;
1122 
1123 		if (ofl->ofl_osfiniarray)	/* FINI_ARRAY & FINI_ARRAYSZ */
1124 			cnt += 2;
1125 
1126 		if (ofl->ofl_osinitarray)	/* INIT_ARRAY & INIT_ARRAYSZ */
1127 			cnt += 2;
1128 
1129 		if (ofl->ofl_ospreinitarray)	/* PREINIT_ARRAY & */
1130 			cnt += 2;		/*	PREINIT_ARRAYSZ */
1131 
1132 		/*
1133 		 * If we have plt's reserve a PLT, PLTSZ, PLTREL and JMPREL.
1134 		 */
1135 		if (ofl->ofl_pltcnt)
1136 			cnt += 3;
1137 
1138 		/*
1139 		 * If pltpadding is needed (Sparcv9)
1140 		 */
1141 		if (ofl->ofl_pltpad)
1142 			cnt += 2;		/* DT_PLTPAD & DT_PLTPADSZ */
1143 
1144 		/*
1145 		 * If we have any relocations reserve a REL, RELSZ and
1146 		 * RELENT entry.
1147 		 */
1148 		if (ofl->ofl_relocsz)
1149 			cnt += 3;
1150 
1151 		/*
1152 		 * If a syminfo section is required create SYMINFO, SYMINSZ,
1153 		 * and SYMINENT entries.
1154 		 */
1155 		if (flags & FLG_OF_SYMINFO)
1156 			cnt += 3;
1157 
1158 		/*
1159 		 * If there are any partially initialized sections allocate
1160 		 * MOVEENT, MOVESZ and MOVETAB.
1161 		 */
1162 		if (ofl->ofl_osmove)
1163 			cnt += 3;
1164 
1165 		/*
1166 		 * Allocate one DT_REGISTER entry for every register symbol.
1167 		 */
1168 		cnt += ofl->ofl_regsymcnt;
1169 
1170 		/*
1171 		 * Reserve a entry for each '-zrtldinfo=...' specified
1172 		 * on the command line.
1173 		 */
1174 		for (APLIST_TRAVERSE(ofl->ofl_rtldinfo, idx, sdp))
1175 			cnt++;
1176 
1177 		/*
1178 		 * These two entries should only be placed in a segment
1179 		 * which is writable.  If it's a read-only segment
1180 		 * (due to mapfile magic, e.g. libdl.so.1) then don't allocate
1181 		 * these entries.
1182 		 */
1183 		if ((osp->os_sgdesc) &&
1184 		    (osp->os_sgdesc->sg_phdr.p_flags & PF_W)) {
1185 			cnt++;			/* FEATURE_1 */
1186 
1187 			if (ofl->ofl_osinterp)
1188 				cnt++;		/* DEBUG */
1189 		}
1190 
1191 		/*
1192 		 * Any hardware/software capabilities?
1193 		 */
1194 		if (ofl->ofl_oscap)
1195 			cnt++;			/* SUNW_CAP */
1196 
1197 		if (flags & FLG_OF_SYMBOLIC)
1198 			cnt++;			/* SYMBOLIC */
1199 	}
1200 
1201 	/*
1202 	 * Account for Architecture dependent .dynamic entries, and defaults.
1203 	 */
1204 	(*ld_targ.t_mr.mr_mach_make_dynamic)(ofl, &cnt);
1205 
1206 	/*
1207 	 * DT_FLAGS, DT_FLAGS_1, DT_SUNW_STRPAD, and DT_NULL. Also,
1208 	 * allow room for the unused extra DT_NULLs. These are included
1209 	 * to allow an ELF editor room to add items later.
1210 	 */
1211 	cnt += 4 + DYNAMIC_EXTRA_ELTS;
1212 
1213 	/*
1214 	 * DT_SUNW_LDMACH. Used to hold the ELF machine code of the
1215 	 * linker that produced the output object. This information
1216 	 * allows us to determine whether a given object was linked
1217 	 * natively, or by a linker running on a different type of
1218 	 * system. This information can be valuable if one suspects
1219 	 * that a problem might be due to alignment or byte order issues.
1220 	 */
1221 	cnt++;
1222 
1223 	/*
1224 	 * Determine the size of the section from the number of entries.
1225 	 */
1226 	size = cnt * (size_t)shdr->sh_entsize;
1227 
1228 	shdr->sh_size = (Xword)size;
1229 	data->d_size = size;
1230 
1231 	/*
1232 	 * There are several tags that are specific to the Solaris osabi
1233 	 * range which we unconditionally put into any dynamic section
1234 	 * we create (e.g. DT_SUNW_STRPAD or DT_SUNW_LDMACH). As such,
1235 	 * any Solaris object with a dynamic section should be tagged as
1236 	 * ELFOSABI_SOLARIS.
1237 	 */
1238 	ofl->ofl_flags |= FLG_OF_OSABI;
1239 
1240 	return ((uintptr_t)ofl->ofl_osdynamic);
1241 }
1242 
1243 /*
1244  * Build the GOT section and its associated relocation entries.
1245  */
1246 uintptr_t
1247 ld_make_got(Ofl_desc *ofl)
1248 {
1249 	Elf_Data	*data;
1250 	Shdr	*shdr;
1251 	Is_desc	*isec;
1252 	size_t	size = (size_t)ofl->ofl_gotcnt * ld_targ.t_m.m_got_entsize;
1253 	size_t	rsize = (size_t)ofl->ofl_relocgotsz;
1254 
1255 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_GOT), 0,
1256 	    &isec, &shdr, &data) == S_ERROR)
1257 		return (S_ERROR);
1258 
1259 	data->d_size = size;
1260 
1261 	shdr->sh_flags |= SHF_WRITE;
1262 	shdr->sh_size = (Xword)size;
1263 	shdr->sh_entsize = ld_targ.t_m.m_got_entsize;
1264 
1265 	ofl->ofl_osgot = ld_place_section(ofl, isec, ld_targ.t_id.id_got, NULL);
1266 	if (ofl->ofl_osgot == (Os_desc *)S_ERROR)
1267 		return (S_ERROR);
1268 
1269 	ofl->ofl_osgot->os_szoutrels = (Xword)rsize;
1270 
1271 	return (1);
1272 }
1273 
1274 /*
1275  * Build an interpreter section.
1276  */
1277 static uintptr_t
1278 make_interp(Ofl_desc *ofl)
1279 {
1280 	Shdr		*shdr;
1281 	Elf_Data	*data;
1282 	Is_desc		*isec;
1283 	const char	*iname = ofl->ofl_interp;
1284 	size_t		size;
1285 
1286 	/*
1287 	 * If -z nointerp is in effect, don't create an interpreter section.
1288 	 */
1289 	if (ofl->ofl_flags1 & FLG_OF1_NOINTRP)
1290 		return (1);
1291 
1292 	/*
1293 	 * We always build an .interp section for dynamic executables.  However
1294 	 * if the user has specifically specified an interpreter we'll build
1295 	 * this section for any output (presumably the user knows what they are
1296 	 * doing. refer ABI section 5-4, and ld.1 man page use of -I).
1297 	 */
1298 	if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC |
1299 	    FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname)
1300 		return (1);
1301 
1302 	/*
1303 	 * In the case of a dynamic executable supply a default interpreter
1304 	 * if a specific interpreter has not been specified.
1305 	 */
1306 	if (iname == NULL)
1307 		iname = ofl->ofl_interp = ld_targ.t_m.m_def_interp;
1308 
1309 	size = strlen(iname) + 1;
1310 
1311 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_INTERP), 0,
1312 	    &isec, &shdr, &data) == S_ERROR)
1313 		return (S_ERROR);
1314 
1315 	data->d_size = size;
1316 	shdr->sh_size = (Xword)size;
1317 	data->d_align = shdr->sh_addralign = 1;
1318 
1319 	ofl->ofl_osinterp =
1320 	    ld_place_section(ofl, isec, ld_targ.t_id.id_interp, NULL);
1321 	return ((uintptr_t)ofl->ofl_osinterp);
1322 }
1323 
1324 /*
1325  * Build a hardware/software capabilities section.
1326  */
1327 static uintptr_t
1328 make_cap(Ofl_desc *ofl)
1329 {
1330 	Shdr		*shdr;
1331 	Elf_Data	*data;
1332 	Is_desc		*isec;
1333 	Os_desc		*osec;
1334 	Cap		*cap;
1335 	size_t		size = 0;
1336 
1337 	/*
1338 	 * Determine how many entries are required.
1339 	 */
1340 	if (ofl->ofl_hwcap_1)
1341 		size++;
1342 	if (ofl->ofl_sfcap_1)
1343 		size++;
1344 	if (size == 0)
1345 		return (1);
1346 	size++;				/* Add CA_SUNW_NULL */
1347 
1348 	if (new_section(ofl, SHT_SUNW_cap, MSG_ORIG(MSG_SCN_SUNWCAP), size,
1349 	    &isec, &shdr, &data) == S_ERROR)
1350 		return (S_ERROR);
1351 
1352 	if ((data->d_buf = libld_malloc(shdr->sh_size)) == NULL)
1353 		return (S_ERROR);
1354 
1355 	cap = (Cap *)data->d_buf;
1356 	if (ofl->ofl_hwcap_1) {
1357 		cap->c_tag = CA_SUNW_HW_1;
1358 		cap->c_un.c_val = ofl->ofl_hwcap_1;
1359 		cap++;
1360 	}
1361 	if (ofl->ofl_sfcap_1) {
1362 		cap->c_tag = CA_SUNW_SF_1;
1363 		cap->c_un.c_val = ofl->ofl_sfcap_1;
1364 		cap++;
1365 	}
1366 	cap->c_tag = CA_SUNW_NULL;
1367 	cap->c_un.c_val = 0;
1368 
1369 	/*
1370 	 * If we're not creating a relocatable object, save the output section
1371 	 * to trigger the creation of an associated program header.
1372 	 */
1373 	osec = ld_place_section(ofl, isec, ld_targ.t_id.id_cap, NULL);
1374 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
1375 		ofl->ofl_oscap = osec;
1376 
1377 	return ((uintptr_t)osec);
1378 }
1379 
1380 /*
1381  * Build the PLT section and its associated relocation entries.
1382  */
1383 static uintptr_t
1384 make_plt(Ofl_desc *ofl)
1385 {
1386 	Shdr		*shdr;
1387 	Elf_Data	*data;
1388 	Is_desc		*isec;
1389 	size_t		size = ld_targ.t_m.m_plt_reservsz +
1390 	    (((size_t)ofl->ofl_pltcnt + (size_t)ofl->ofl_pltpad) *
1391 	    ld_targ.t_m.m_plt_entsize);
1392 	size_t		rsize = (size_t)ofl->ofl_relocpltsz;
1393 
1394 	/*
1395 	 * On sparc, account for the NOP at the end of the plt.
1396 	 */
1397 	if (ld_targ.t_m.m_mach == LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
1398 		size += sizeof (Word);
1399 
1400 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_PLT), 0,
1401 	    &isec, &shdr, &data) == S_ERROR)
1402 		return (S_ERROR);
1403 
1404 	data->d_size = size;
1405 	data->d_align = ld_targ.t_m.m_plt_align;
1406 
1407 	shdr->sh_flags = ld_targ.t_m.m_plt_shf_flags;
1408 	shdr->sh_size = (Xword)size;
1409 	shdr->sh_addralign = ld_targ.t_m.m_plt_align;
1410 	shdr->sh_entsize = ld_targ.t_m.m_plt_entsize;
1411 
1412 	ofl->ofl_osplt = ld_place_section(ofl, isec, ld_targ.t_id.id_plt, NULL);
1413 	if (ofl->ofl_osplt == (Os_desc *)S_ERROR)
1414 		return (S_ERROR);
1415 
1416 	ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1417 
1418 	return (1);
1419 }
1420 
1421 /*
1422  * Make the hash table.  Only built for dynamic executables and shared
1423  * libraries, and provides hashed lookup into the global symbol table
1424  * (.dynsym) for the run-time linker to resolve symbol lookups.
1425  */
1426 static uintptr_t
1427 make_hash(Ofl_desc *ofl)
1428 {
1429 	Shdr		*shdr;
1430 	Elf_Data	*data;
1431 	Is_desc		*isec;
1432 	size_t		size;
1433 	Word		nsyms = ofl->ofl_globcnt;
1434 	size_t		cnt;
1435 
1436 	/*
1437 	 * Allocate section header structures. We set entcnt to 0
1438 	 * because it's going to change after we place this section.
1439 	 */
1440 	if (new_section(ofl, SHT_HASH, MSG_ORIG(MSG_SCN_HASH), 0,
1441 	    &isec, &shdr, &data) == S_ERROR)
1442 		return (S_ERROR);
1443 
1444 	/*
1445 	 * Place the section first since it will affect the local symbol
1446 	 * count.
1447 	 */
1448 	ofl->ofl_oshash =
1449 	    ld_place_section(ofl, isec, ld_targ.t_id.id_hash, NULL);
1450 	if (ofl->ofl_oshash == (Os_desc *)S_ERROR)
1451 		return (S_ERROR);
1452 
1453 	/*
1454 	 * Calculate the number of output hash buckets.
1455 	 */
1456 	ofl->ofl_hashbkts = findprime(nsyms);
1457 
1458 	/*
1459 	 * The size of the hash table is determined by
1460 	 *
1461 	 *	i.	the initial nbucket and nchain entries (2)
1462 	 *	ii.	the number of buckets (calculated above)
1463 	 *	iii.	the number of chains (this is based on the number of
1464 	 *		symbols in the .dynsym array).
1465 	 */
1466 	cnt = 2 + ofl->ofl_hashbkts + DYNSYM_ALL_CNT(ofl);
1467 	size = cnt * shdr->sh_entsize;
1468 
1469 	/*
1470 	 * Finalize the section header and data buffer initialization.
1471 	 */
1472 	if ((data->d_buf = libld_calloc(size, 1)) == NULL)
1473 		return (S_ERROR);
1474 	data->d_size = size;
1475 	shdr->sh_size = (Xword)size;
1476 
1477 	return (1);
1478 }
1479 
1480 /*
1481  * Generate the standard symbol table.  Contains all locals and globals,
1482  * and resides in a non-allocatable section (ie. it can be stripped).
1483  */
1484 static uintptr_t
1485 make_symtab(Ofl_desc *ofl)
1486 {
1487 	Shdr		*shdr;
1488 	Elf_Data	*data;
1489 	Is_desc		*isec;
1490 	Is_desc		*xisec = 0;
1491 	size_t		size;
1492 	Word		symcnt;
1493 
1494 	/*
1495 	 * Create the section headers. Note that we supply an ent_cnt
1496 	 * of 0. We won't know the count until the section has been placed.
1497 	 */
1498 	if (new_section(ofl, SHT_SYMTAB, MSG_ORIG(MSG_SCN_SYMTAB), 0,
1499 	    &isec, &shdr, &data) == S_ERROR)
1500 		return (S_ERROR);
1501 
1502 	/*
1503 	 * Place the section first since it will affect the local symbol
1504 	 * count.
1505 	 */
1506 	if ((ofl->ofl_ossymtab = ld_place_section(ofl, isec,
1507 	    ld_targ.t_id.id_symtab, NULL)) == (Os_desc *)S_ERROR)
1508 		return (S_ERROR);
1509 
1510 	/*
1511 	 * At this point we've created all but the 'shstrtab' section.
1512 	 * Determine if we have to use 'Extended Sections'.  If so - then
1513 	 * also create a SHT_SYMTAB_SHNDX section.
1514 	 */
1515 	if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
1516 		Shdr		*xshdr;
1517 		Elf_Data	*xdata;
1518 
1519 		if (new_section(ofl, SHT_SYMTAB_SHNDX,
1520 		    MSG_ORIG(MSG_SCN_SYMTAB_SHNDX), 0, &xisec,
1521 		    &xshdr, &xdata) == S_ERROR)
1522 			return (S_ERROR);
1523 
1524 		if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec,
1525 		    ld_targ.t_id.id_symtab_ndx, NULL)) == (Os_desc *)S_ERROR)
1526 			return (S_ERROR);
1527 	}
1528 
1529 	/*
1530 	 * Calculated number of symbols, which need to be augmented by
1531 	 * the (yet to be created) .shstrtab entry.
1532 	 */
1533 	symcnt = (size_t)(1 + SYMTAB_ALL_CNT(ofl));
1534 	size = symcnt * shdr->sh_entsize;
1535 
1536 	/*
1537 	 * Finalize the section header and data buffer initialization.
1538 	 */
1539 	data->d_size = size;
1540 	shdr->sh_size = (Xword)size;
1541 
1542 	/*
1543 	 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
1544 	 */
1545 	if (xisec) {
1546 		size_t	xsize = symcnt * sizeof (Word);
1547 
1548 		xisec->is_indata->d_size = xsize;
1549 		xisec->is_shdr->sh_size = (Xword)xsize;
1550 	}
1551 
1552 	return (1);
1553 }
1554 
1555 
1556 /*
1557  * Build a dynamic symbol table. These tables reside in the text
1558  * segment of a dynamic executable or shared library.
1559  *
1560  *	.SUNW_ldynsym contains local function symbols
1561  *	.dynsym contains only globals symbols
1562  *
1563  * The two tables are created adjacent to each other, with .SUNW_ldynsym
1564  * coming first.
1565  */
1566 static uintptr_t
1567 make_dynsym(Ofl_desc *ofl)
1568 {
1569 	Shdr		*shdr, *lshdr;
1570 	Elf_Data	*data, *ldata;
1571 	Is_desc		*isec, *lisec;
1572 	size_t		size;
1573 	Xword		cnt;
1574 	int		allow_ldynsym;
1575 
1576 	/*
1577 	 * Unless explicitly disabled, always produce a .SUNW_ldynsym section
1578 	 * when it is allowed by the file type, even if the resulting
1579 	 * table only ends up with a single STT_FILE in it. There are
1580 	 * two reasons: (1) It causes the generation of the DT_SUNW_SYMTAB
1581 	 * entry in the .dynamic section, which is something we would
1582 	 * like to encourage, and (2) Without it, we cannot generate
1583 	 * the associated .SUNW_dyn[sym|tls]sort sections, which are of
1584 	 * value to DTrace.
1585 	 *
1586 	 * In practice, it is extremely rare for an object not to have
1587 	 * local symbols for .SUNW_ldynsym, so 99% of the time, we'd be
1588 	 * doing it anyway.
1589 	 */
1590 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1591 
1592 	/*
1593 	 * Create the section headers. Note that we supply an ent_cnt
1594 	 * of 0. We won't know the count until the section has been placed.
1595 	 */
1596 	if (allow_ldynsym && new_section(ofl, SHT_SUNW_LDYNSYM,
1597 	    MSG_ORIG(MSG_SCN_LDYNSYM), 0, &lisec, &lshdr, &ldata) == S_ERROR)
1598 		return (S_ERROR);
1599 
1600 	if (new_section(ofl, SHT_DYNSYM, MSG_ORIG(MSG_SCN_DYNSYM), 0,
1601 	    &isec, &shdr, &data) == S_ERROR)
1602 		return (S_ERROR);
1603 
1604 	/*
1605 	 * Place the section(s) first since it will affect the local symbol
1606 	 * count.
1607 	 */
1608 	if (allow_ldynsym &&
1609 	    ((ofl->ofl_osldynsym = ld_place_section(ofl, lisec,
1610 	    ld_targ.t_id.id_ldynsym, NULL)) == (Os_desc *)S_ERROR))
1611 		return (S_ERROR);
1612 	ofl->ofl_osdynsym =
1613 	    ld_place_section(ofl, isec, ld_targ.t_id.id_dynsym, NULL);
1614 	if (ofl->ofl_osdynsym == (Os_desc *)S_ERROR)
1615 		return (S_ERROR);
1616 
1617 	cnt = DYNSYM_ALL_CNT(ofl);
1618 	size = (size_t)cnt * shdr->sh_entsize;
1619 
1620 	/*
1621 	 * Finalize the section header and data buffer initialization.
1622 	 */
1623 	data->d_size = size;
1624 	shdr->sh_size = (Xword)size;
1625 
1626 	/*
1627 	 * An ldynsym contains local function symbols. It is not
1628 	 * used for linking, but if present, serves to allow better
1629 	 * stack traces to be generated in contexts where the symtab
1630 	 * is not available. (dladdr(), or stripped executable/library files).
1631 	 */
1632 	if (allow_ldynsym) {
1633 		cnt = 1 + ofl->ofl_dynlocscnt + ofl->ofl_dynscopecnt;
1634 		size = (size_t)cnt * shdr->sh_entsize;
1635 
1636 		ldata->d_size = size;
1637 		lshdr->sh_size = (Xword)size;
1638 	}
1639 
1640 	return (1);
1641 }
1642 
1643 /*
1644  * Build .SUNW_dynsymsort and/or .SUNW_dyntlssort sections. These are
1645  * index sections for the .SUNW_ldynsym/.dynsym pair that present data
1646  * and function symbols sorted by address.
1647  */
1648 static uintptr_t
1649 make_dynsort(Ofl_desc *ofl)
1650 {
1651 	Shdr		*shdr;
1652 	Elf_Data	*data;
1653 	Is_desc		*isec;
1654 
1655 	/* Only do it if the .SUNW_ldynsym section is present */
1656 	if (!OFL_ALLOW_LDYNSYM(ofl))
1657 		return (1);
1658 
1659 	/* .SUNW_dynsymsort */
1660 	if (ofl->ofl_dynsymsortcnt > 0) {
1661 		if (new_section(ofl, SHT_SUNW_symsort,
1662 		    MSG_ORIG(MSG_SCN_DYNSYMSORT), ofl->ofl_dynsymsortcnt,
1663 		    &isec, &shdr, &data) == S_ERROR)
1664 		return (S_ERROR);
1665 
1666 		if ((ofl->ofl_osdynsymsort = ld_place_section(ofl, isec,
1667 		    ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR)
1668 			return (S_ERROR);
1669 	}
1670 
1671 	/* .SUNW_dyntlssort */
1672 	if (ofl->ofl_dyntlssortcnt > 0) {
1673 		if (new_section(ofl, SHT_SUNW_tlssort,
1674 		    MSG_ORIG(MSG_SCN_DYNTLSSORT),
1675 		    ofl->ofl_dyntlssortcnt, &isec, &shdr, &data) == S_ERROR)
1676 		return (S_ERROR);
1677 
1678 		if ((ofl->ofl_osdyntlssort = ld_place_section(ofl, isec,
1679 		    ld_targ.t_id.id_dynsort, NULL)) == (Os_desc *)S_ERROR)
1680 			return (S_ERROR);
1681 	}
1682 
1683 	return (1);
1684 }
1685 
1686 /*
1687  * Helper routine for make_dynsym_shndx. Builds a
1688  * a SHT_SYMTAB_SHNDX for .dynsym or .SUNW_ldynsym, without knowing
1689  * which one it is.
1690  */
1691 static uintptr_t
1692 make_dyn_shndx(Ofl_desc *ofl, const char *shname, Os_desc *symtab,
1693     Os_desc **ret_os)
1694 {
1695 	Is_desc		*isec;
1696 	Is_desc		*dynsymisp;
1697 	Shdr		*shdr, *dynshdr;
1698 	Elf_Data	*data;
1699 
1700 	dynsymisp = ld_os_first_isdesc(symtab);
1701 	dynshdr = dynsymisp->is_shdr;
1702 
1703 	if (new_section(ofl, SHT_SYMTAB_SHNDX, shname,
1704 	    (dynshdr->sh_size / dynshdr->sh_entsize),
1705 	    &isec, &shdr, &data) == S_ERROR)
1706 		return (S_ERROR);
1707 
1708 	if ((*ret_os = ld_place_section(ofl, isec,
1709 	    ld_targ.t_id.id_dynsym_ndx, NULL)) == (Os_desc *)S_ERROR)
1710 		return (S_ERROR);
1711 
1712 	assert(*ret_os);
1713 
1714 	return (1);
1715 }
1716 
1717 /*
1718  * Build a SHT_SYMTAB_SHNDX for the .dynsym, and .SUNW_ldynsym
1719  */
1720 static uintptr_t
1721 make_dynsym_shndx(Ofl_desc *ofl)
1722 {
1723 	/*
1724 	 * If there is a .SUNW_ldynsym, generate a section for its extended
1725 	 * index section as well.
1726 	 */
1727 	if (OFL_ALLOW_LDYNSYM(ofl)) {
1728 		if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_LDYNSYM_SHNDX),
1729 		    ofl->ofl_osldynsym, &ofl->ofl_osldynshndx) == S_ERROR)
1730 			return (S_ERROR);
1731 	}
1732 
1733 	/* The Generate a section for the dynsym */
1734 	if (make_dyn_shndx(ofl, MSG_ORIG(MSG_SCN_DYNSYM_SHNDX),
1735 	    ofl->ofl_osdynsym, &ofl->ofl_osdynshndx) == S_ERROR)
1736 		return (S_ERROR);
1737 
1738 	return (1);
1739 }
1740 
1741 
1742 /*
1743  * Build a string table for the section headers.
1744  */
1745 static uintptr_t
1746 make_shstrtab(Ofl_desc *ofl)
1747 {
1748 	Shdr		*shdr;
1749 	Elf_Data	*data;
1750 	Is_desc		*isec;
1751 	size_t		size;
1752 
1753 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_SHSTRTAB),
1754 	    0, &isec, &shdr, &data) == S_ERROR)
1755 		return (S_ERROR);
1756 
1757 	/*
1758 	 * Place the section first, as it may effect the number of section
1759 	 * headers to account for.
1760 	 */
1761 	ofl->ofl_osshstrtab =
1762 	    ld_place_section(ofl, isec, ld_targ.t_id.id_note, NULL);
1763 	if (ofl->ofl_osshstrtab == (Os_desc *)S_ERROR)
1764 		return (S_ERROR);
1765 
1766 	size = st_getstrtab_sz(ofl->ofl_shdrsttab);
1767 	assert(size > 0);
1768 
1769 	data->d_size = size;
1770 	shdr->sh_size = (Xword)size;
1771 
1772 	return (1);
1773 }
1774 
1775 /*
1776  * Build a string section for the standard symbol table.
1777  */
1778 static uintptr_t
1779 make_strtab(Ofl_desc *ofl)
1780 {
1781 	Shdr		*shdr;
1782 	Elf_Data	*data;
1783 	Is_desc		*isec;
1784 	size_t		size;
1785 
1786 	/*
1787 	 * This string table consists of all the global and local symbols.
1788 	 * Account for null bytes at end of the file name and the beginning
1789 	 * of section.
1790 	 */
1791 	if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
1792 		return (S_ERROR);
1793 
1794 	size = st_getstrtab_sz(ofl->ofl_strtab);
1795 	assert(size > 0);
1796 
1797 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_STRTAB),
1798 	    0, &isec, &shdr, &data) == S_ERROR)
1799 		return (S_ERROR);
1800 
1801 	/* Set the size of the data area */
1802 	data->d_size = size;
1803 	shdr->sh_size = (Xword)size;
1804 
1805 	ofl->ofl_osstrtab =
1806 	    ld_place_section(ofl, isec, ld_targ.t_id.id_strtab, NULL);
1807 	return ((uintptr_t)ofl->ofl_osstrtab);
1808 }
1809 
1810 /*
1811  * Build a string table for the dynamic symbol table.
1812  */
1813 static uintptr_t
1814 make_dynstr(Ofl_desc *ofl)
1815 {
1816 	Shdr		*shdr;
1817 	Elf_Data	*data;
1818 	Is_desc		*isec;
1819 	size_t		size;
1820 
1821 	/*
1822 	 * If producing a .SUNW_ldynsym, account for the initial STT_FILE
1823 	 * symbol that precedes the scope reduced global symbols.
1824 	 */
1825 	if (OFL_ALLOW_LDYNSYM(ofl)) {
1826 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_name) == -1)
1827 			return (S_ERROR);
1828 		ofl->ofl_dynscopecnt++;
1829 	}
1830 
1831 
1832 	/*
1833 	 * Account for any local, named register symbols.  These locals are
1834 	 * required for reference from DT_REGISTER .dynamic entries.
1835 	 */
1836 	if (ofl->ofl_regsyms) {
1837 		int	ndx;
1838 
1839 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1840 			Sym_desc	*sdp;
1841 
1842 			if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
1843 				continue;
1844 
1845 			if (((sdp->sd_flags & FLG_SY_HIDDEN) == 0) &&
1846 			    (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
1847 				continue;
1848 
1849 			if (sdp->sd_sym->st_name == NULL)
1850 				continue;
1851 
1852 			if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
1853 				return (S_ERROR);
1854 		}
1855 	}
1856 
1857 	/*
1858 	 * Reserve entries for any per-symbol auxiliary/filter strings.
1859 	 */
1860 	if (ofl->ofl_dtsfltrs != NULL) {
1861 		Dfltr_desc	*dftp;
1862 		Aliste		idx;
1863 
1864 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp))
1865 			if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
1866 				return (S_ERROR);
1867 	}
1868 
1869 	size = st_getstrtab_sz(ofl->ofl_dynstrtab);
1870 	assert(size > 0);
1871 
1872 	if (new_section(ofl, SHT_STRTAB, MSG_ORIG(MSG_SCN_DYNSTR),
1873 	    0, &isec, &shdr, &data) == S_ERROR)
1874 		return (S_ERROR);
1875 
1876 	/* Make it allocable if necessary */
1877 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
1878 		shdr->sh_flags |= SHF_ALLOC;
1879 
1880 	/* Set the size of the data area */
1881 	data->d_size = size + DYNSTR_EXTRA_PAD;
1882 
1883 	shdr->sh_size = (Xword)size;
1884 
1885 	ofl->ofl_osdynstr =
1886 	    ld_place_section(ofl, isec, ld_targ.t_id.id_dynstr, NULL);
1887 	return ((uintptr_t)ofl->ofl_osdynstr);
1888 }
1889 
1890 /*
1891  * Generate an output relocation section which will contain the relocation
1892  * information to be applied to the `osp' section.
1893  *
1894  * If (osp == NULL) then we are creating the coalesced relocation section
1895  * for an executable and/or a shared object.
1896  */
1897 static uintptr_t
1898 make_reloc(Ofl_desc *ofl, Os_desc *osp)
1899 {
1900 	Shdr		*shdr;
1901 	Elf_Data	*data;
1902 	Is_desc		*isec;
1903 	size_t		size;
1904 	Xword		sh_flags;
1905 	char 		*sectname;
1906 	Os_desc		*rosp;
1907 	Word		relsize;
1908 	const char	*rel_prefix;
1909 
1910 	/* LINTED */
1911 	if (ld_targ.t_m.m_rel_sht_type == SHT_REL) {
1912 		/* REL */
1913 		relsize = sizeof (Rel);
1914 		rel_prefix = MSG_ORIG(MSG_SCN_REL);
1915 	} else {
1916 		/* RELA */
1917 		relsize = sizeof (Rela);
1918 		rel_prefix = MSG_ORIG(MSG_SCN_RELA);
1919 	}
1920 
1921 	if (osp) {
1922 		size = osp->os_szoutrels;
1923 		sh_flags = osp->os_shdr->sh_flags;
1924 		if ((sectname = libld_malloc(strlen(rel_prefix) +
1925 		    strlen(osp->os_name) + 1)) == 0)
1926 			return (S_ERROR);
1927 		(void) strcpy(sectname, rel_prefix);
1928 		(void) strcat(sectname, osp->os_name);
1929 	} else if (ofl->ofl_flags & FLG_OF_COMREL) {
1930 		size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
1931 		sh_flags = SHF_ALLOC;
1932 		sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
1933 	} else {
1934 		size = ofl->ofl_relocrelsz;
1935 		sh_flags = SHF_ALLOC;
1936 		sectname = (char *)rel_prefix;
1937 	}
1938 
1939 	/*
1940 	 * Keep track of total size of 'output relocations' (to be stored
1941 	 * in .dynamic)
1942 	 */
1943 	/* LINTED */
1944 	ofl->ofl_relocsz += (Xword)size;
1945 
1946 	if (new_section(ofl, ld_targ.t_m.m_rel_sht_type, sectname, 0, &isec,
1947 	    &shdr, &data) == S_ERROR)
1948 		return (S_ERROR);
1949 
1950 	data->d_size = size;
1951 
1952 	shdr->sh_size = (Xword)size;
1953 	if (OFL_ALLOW_DYNSYM(ofl) && (sh_flags & SHF_ALLOC))
1954 		shdr->sh_flags = SHF_ALLOC;
1955 
1956 	if (osp) {
1957 		/*
1958 		 * The sh_info field of the SHT_REL* sections points to the
1959 		 * section the relocations are to be applied to.
1960 		 */
1961 		shdr->sh_flags |= SHF_INFO_LINK;
1962 	}
1963 
1964 	rosp = ld_place_section(ofl, isec, ld_targ.t_id.id_rel, NULL);
1965 	if (rosp == (Os_desc *)S_ERROR)
1966 		return (S_ERROR);
1967 
1968 	/*
1969 	 * Associate this relocation section to the section its going to
1970 	 * relocate.
1971 	 */
1972 	if (osp) {
1973 		Aliste	idx;
1974 		Is_desc	*risp;
1975 
1976 		/*
1977 		 * This is used primarily so that we can update
1978 		 * SHT_GROUP[sect_no] entries to point to the
1979 		 * created output relocation sections.
1980 		 */
1981 		for (APLIST_TRAVERSE(osp->os_relisdescs, idx, risp)) {
1982 			risp->is_osdesc = rosp;
1983 
1984 			/*
1985 			 * If the input relocation section had the SHF_GROUP
1986 			 * flag set - propagate it to the output relocation
1987 			 * section.
1988 			 */
1989 			if (risp->is_shdr->sh_flags & SHF_GROUP) {
1990 				rosp->os_shdr->sh_flags |= SHF_GROUP;
1991 				break;
1992 			}
1993 		}
1994 		osp->os_relosdesc = rosp;
1995 	} else
1996 		ofl->ofl_osrel = rosp;
1997 
1998 	/*
1999 	 * If this is the first relocation section we've encountered save it
2000 	 * so that the .dynamic entry can be initialized accordingly.
2001 	 */
2002 	if (ofl->ofl_osrelhead == (Os_desc *)0)
2003 		ofl->ofl_osrelhead = rosp;
2004 
2005 	return (1);
2006 }
2007 
2008 /*
2009  * Generate version needed section.
2010  */
2011 static uintptr_t
2012 make_verneed(Ofl_desc *ofl)
2013 {
2014 	Shdr		*shdr;
2015 	Elf_Data	*data;
2016 	Is_desc		*isec;
2017 
2018 	/*
2019 	 * verneed sections do not have a constant element size, so the
2020 	 * value of ent_cnt specified here (0) is meaningless.
2021 	 */
2022 	if (new_section(ofl, SHT_SUNW_verneed, MSG_ORIG(MSG_SCN_SUNWVERSION),
2023 	    0, &isec, &shdr, &data) == S_ERROR)
2024 		return (S_ERROR);
2025 
2026 	/* During version processing we calculated the total size. */
2027 	data->d_size = ofl->ofl_verneedsz;
2028 	shdr->sh_size = (Xword)ofl->ofl_verneedsz;
2029 
2030 	ofl->ofl_osverneed =
2031 	    ld_place_section(ofl, isec, ld_targ.t_id.id_version, NULL);
2032 	return ((uintptr_t)ofl->ofl_osverneed);
2033 }
2034 
2035 /*
2036  * Generate a version definition section.
2037  *
2038  *  o	the SHT_SUNW_verdef section defines the versions that exist within this
2039  *	image.
2040  */
2041 static uintptr_t
2042 make_verdef(Ofl_desc *ofl)
2043 {
2044 	Shdr		*shdr;
2045 	Elf_Data	*data;
2046 	Is_desc		*isec;
2047 	Ver_desc	*vdp;
2048 	Str_tbl		*strtab;
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_IS_STATIC_OBJ(ofl))
2058 		strtab = ofl->ofl_strtab;
2059 	else
2060 		strtab = ofl->ofl_dynstrtab;
2061 
2062 	if (st_insert(strtab, vdp->vd_name) == -1)
2063 		return (S_ERROR);
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  * Update a data buffers size.  A number of sections have to be created, and
2590  * the sections header contributes to the size of the eventual section.  Thus,
2591  * a section may be created, and once all associated sections have been created,
2592  * we return to establish the required section size.
2593  */
2594 inline static void
2595 update_data_size(Os_desc *osp, ulong_t cnt)
2596 {
2597 	Is_desc		*isec = ld_os_first_isdesc(osp);
2598 	Elf_Data	*data = isec->is_indata;
2599 	Shdr		*shdr = osp->os_shdr;
2600 	size_t		size = cnt * shdr->sh_entsize;
2601 
2602 	shdr->sh_size = (Xword)size;
2603 	data->d_size = size;
2604 }
2605 
2606 /*
2607  * The following sections are built after all input file processing and symbol
2608  * validation has been carried out.  The order is important (because the
2609  * addition of a section adds a new symbol there is a chicken and egg problem
2610  * of maintaining the appropriate counts).  By maintaining a known order the
2611  * individual routines can compensate for later, known, additions.
2612  */
2613 uintptr_t
2614 ld_make_sections(Ofl_desc *ofl)
2615 {
2616 	ofl_flag_t	flags = ofl->ofl_flags;
2617 	Sg_desc		*sgp;
2618 
2619 	/*
2620 	 * Generate any special sections.
2621 	 */
2622 	if (flags & FLG_OF_ADDVERS)
2623 		if (make_comment(ofl) == S_ERROR)
2624 			return (S_ERROR);
2625 
2626 	if (make_interp(ofl) == S_ERROR)
2627 		return (S_ERROR);
2628 
2629 	if (make_cap(ofl) == S_ERROR)
2630 		return (S_ERROR);
2631 
2632 	if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
2633 	    ofl->ofl_initarray) == S_ERROR)
2634 		return (S_ERROR);
2635 
2636 	if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
2637 	    ofl->ofl_finiarray) == S_ERROR)
2638 		return (S_ERROR);
2639 
2640 	if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
2641 	    ofl->ofl_preiarray) == S_ERROR)
2642 		return (S_ERROR);
2643 
2644 	/*
2645 	 * Make the .plt section.  This occurs after any other relocation
2646 	 * sections are generated (see reloc_init()) to ensure that the
2647 	 * associated relocation section is after all the other relocation
2648 	 * sections.
2649 	 */
2650 	if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
2651 		if (make_plt(ofl) == S_ERROR)
2652 			return (S_ERROR);
2653 
2654 	/*
2655 	 * Determine whether any sections or files are not referenced.  Under
2656 	 * -Dunused a diagnostic for any unused components is generated, under
2657 	 * -zignore the component is removed from the final output.
2658 	 */
2659 	if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
2660 		if (ignore_section_processing(ofl) == S_ERROR)
2661 			return (S_ERROR);
2662 	}
2663 
2664 	/*
2665 	 * Do any of the output sections contain input sections that
2666 	 * are candidates for string table merging? For each such case,
2667 	 * we create a replacement section, insert it, and discard the
2668 	 * originals.
2669 	 *
2670 	 * rel_alpp and sym_alpp are used by ld_make_strmerge()
2671 	 * for its internal processing. We are responsible for the
2672 	 * initialization and cleanup, and ld_make_strmerge() handles the rest.
2673 	 * This allows us to reuse a single pair of memory buffers, allocated
2674 	 * for this processing, for all the output sections.
2675 	 */
2676 	if ((ofl->ofl_flags1 & FLG_OF1_NCSTTAB) == 0) {
2677 		int	error_seen = 0;
2678 		APlist	*rel_alpp = NULL;
2679 		APlist	*sym_alpp = NULL;
2680 		Aliste	idx1;
2681 
2682 		for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
2683 			Os_desc	*osp;
2684 			Aliste	idx2;
2685 
2686 			for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp))
2687 				if ((osp->os_mstrisdescs != NULL) &&
2688 				    (ld_make_strmerge(ofl, osp,
2689 				    &rel_alpp, &sym_alpp) ==
2690 				    S_ERROR)) {
2691 					error_seen = 1;
2692 					break;
2693 				}
2694 		}
2695 		if (rel_alpp != NULL)
2696 			free(rel_alpp);
2697 		if (sym_alpp != NULL)
2698 			free(sym_alpp);
2699 		if (error_seen != 0)
2700 			return (S_ERROR);
2701 	}
2702 
2703 	/*
2704 	 * Add any necessary versioning information.
2705 	 */
2706 	if (!(flags & FLG_OF_NOVERSEC)) {
2707 		if ((flags & FLG_OF_VERNEED) &&
2708 		    (make_verneed(ofl) == S_ERROR))
2709 			return (S_ERROR);
2710 		if ((flags & FLG_OF_VERDEF) &&
2711 		    (make_verdef(ofl) == S_ERROR))
2712 			return (S_ERROR);
2713 		if ((flags & (FLG_OF_VERNEED | FLG_OF_VERDEF)) &&
2714 		    ((ofl->ofl_osversym = make_sym_sec(ofl,
2715 		    MSG_ORIG(MSG_SCN_SUNWVERSYM), SHT_SUNW_versym,
2716 		    ld_targ.t_id.id_version)) == (Os_desc*)S_ERROR))
2717 			return (S_ERROR);
2718 	}
2719 
2720 	/*
2721 	 * Create a syminfo section if necessary.
2722 	 */
2723 	if (flags & FLG_OF_SYMINFO) {
2724 		if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
2725 		    MSG_ORIG(MSG_SCN_SUNWSYMINFO), SHT_SUNW_syminfo,
2726 		    ld_targ.t_id.id_syminfo)) == (Os_desc *)S_ERROR)
2727 			return (S_ERROR);
2728 	}
2729 
2730 	if (flags & FLG_OF_COMREL) {
2731 		/*
2732 		 * If -zcombreloc is enabled then all relocations (except for
2733 		 * the PLT's) are coalesced into a single relocation section.
2734 		 */
2735 		if (ofl->ofl_reloccnt) {
2736 			if (make_reloc(ofl, NULL) == S_ERROR)
2737 				return (S_ERROR);
2738 		}
2739 	} else {
2740 		Aliste	idx1;
2741 
2742 		/*
2743 		 * Create the required output relocation sections.  Note, new
2744 		 * sections may be added to the section list that is being
2745 		 * traversed.  These insertions can move the elements of the
2746 		 * Alist such that a section descriptor is re-read.  Recursion
2747 		 * is prevented by maintaining a previous section pointer and
2748 		 * insuring that this pointer isn't re-examined.
2749 		 */
2750 		for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
2751 			Os_desc	*osp, *posp = 0;
2752 			Aliste	idx2;
2753 
2754 			for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
2755 				if ((osp != posp) && osp->os_szoutrels &&
2756 				    (osp != ofl->ofl_osplt)) {
2757 					if (make_reloc(ofl, osp) == S_ERROR)
2758 						return (S_ERROR);
2759 				}
2760 				posp = osp;
2761 			}
2762 		}
2763 
2764 		/*
2765 		 * If we're not building a combined relocation section, then
2766 		 * build a .rel[a] section as required.
2767 		 */
2768 		if (ofl->ofl_relocrelsz) {
2769 			if (make_reloc(ofl, NULL) == S_ERROR)
2770 				return (S_ERROR);
2771 		}
2772 	}
2773 
2774 	/*
2775 	 * The PLT relocations are always in their own section, and we try to
2776 	 * keep them at the end of the PLT table.  We do this to keep the hot
2777 	 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
2778 	 */
2779 	if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
2780 		if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
2781 			return (S_ERROR);
2782 	}
2783 
2784 	/*
2785 	 * Finally build the symbol and section header sections.
2786 	 */
2787 	if (flags & FLG_OF_DYNAMIC) {
2788 		if (make_dynamic(ofl) == S_ERROR)
2789 			return (S_ERROR);
2790 
2791 		/*
2792 		 * A number of sections aren't necessary within a relocatable
2793 		 * object, even if -dy has been used.
2794 		 */
2795 		if (!(flags & FLG_OF_RELOBJ)) {
2796 			if (make_hash(ofl) == S_ERROR)
2797 				return (S_ERROR);
2798 			if (make_dynstr(ofl) == S_ERROR)
2799 				return (S_ERROR);
2800 			if (make_dynsym(ofl) == S_ERROR)
2801 				return (S_ERROR);
2802 			if (ld_unwind_make_hdr(ofl) == S_ERROR)
2803 				return (S_ERROR);
2804 			if (make_dynsort(ofl) == S_ERROR)
2805 				return (S_ERROR);
2806 		}
2807 	}
2808 
2809 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
2810 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
2811 		/*
2812 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2813 		 * for the dynsym.  If so - do it now.
2814 		 */
2815 		if (ofl->ofl_osdynsym &&
2816 		    ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
2817 			if (make_dynsym_shndx(ofl) == S_ERROR)
2818 				return (S_ERROR);
2819 		}
2820 
2821 		if (make_strtab(ofl) == S_ERROR)
2822 			return (S_ERROR);
2823 		if (make_symtab(ofl) == S_ERROR)
2824 			return (S_ERROR);
2825 	} else {
2826 		/*
2827 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2828 		 * for the dynsym.  If so - do it now.
2829 		 */
2830 		if (ofl->ofl_osdynsym &&
2831 		    ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
2832 			if (make_dynsym_shndx(ofl) == S_ERROR)
2833 				return (S_ERROR);
2834 		}
2835 	}
2836 
2837 	if (make_shstrtab(ofl) == S_ERROR)
2838 		return (S_ERROR);
2839 
2840 	/*
2841 	 * Now that we've created all output sections, adjust the size of the
2842 	 * SHT_SUNW_versym and SHT_SUNW_syminfo section, which are dependent on
2843 	 * the associated symbol table sizes.
2844 	 */
2845 	if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
2846 		ulong_t		cnt;
2847 		Is_desc		*isp;
2848 		Os_desc		*osp;
2849 
2850 		if (OFL_IS_STATIC_OBJ(ofl))
2851 			osp = ofl->ofl_ossymtab;
2852 		else
2853 			osp = ofl->ofl_osdynsym;
2854 
2855 		isp = ld_os_first_isdesc(osp);
2856 		cnt = (isp->is_shdr->sh_size / isp->is_shdr->sh_entsize);
2857 
2858 		if (ofl->ofl_osversym)
2859 			update_data_size(ofl->ofl_osversym, cnt);
2860 
2861 		if (ofl->ofl_ossyminfo)
2862 			update_data_size(ofl->ofl_ossyminfo, cnt);
2863 	}
2864 
2865 	return (1);
2866 }
2867 
2868 /*
2869  * Build an additional data section - used to back OBJT symbol definitions
2870  * added with a mapfile.
2871  */
2872 Is_desc *
2873 ld_make_data(Ofl_desc *ofl, size_t size)
2874 {
2875 	Shdr		*shdr;
2876 	Elf_Data	*data;
2877 	Is_desc		*isec;
2878 
2879 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_DATA), 0,
2880 	    &isec, &shdr, &data) == S_ERROR)
2881 		return ((Is_desc *)S_ERROR);
2882 
2883 	data->d_size = size;
2884 	shdr->sh_size = (Xword)size;
2885 	shdr->sh_flags |= SHF_WRITE;
2886 
2887 	if (aplist_append(&ofl->ofl_mapdata, isec, AL_CNT_OFL_MAPSECS) == NULL)
2888 		return ((Is_desc *)S_ERROR);
2889 
2890 	return (isec);
2891 }
2892 
2893 /*
2894  * Build an additional text section - used to back FUNC symbol definitions
2895  * added with a mapfile.
2896  */
2897 Is_desc *
2898 ld_make_text(Ofl_desc *ofl, size_t size)
2899 {
2900 	Shdr		*shdr;
2901 	Elf_Data	*data;
2902 	Is_desc		*isec;
2903 
2904 	/*
2905 	 * Insure the size is sufficient to contain the minimum return
2906 	 * instruction.
2907 	 */
2908 	if (size < ld_targ.t_nf.nf_size)
2909 		size = ld_targ.t_nf.nf_size;
2910 
2911 	if (new_section(ofl, SHT_PROGBITS, MSG_ORIG(MSG_SCN_TEXT), 0,
2912 	    &isec, &shdr, &data) == S_ERROR)
2913 		return ((Is_desc *)S_ERROR);
2914 
2915 	data->d_size = size;
2916 	shdr->sh_size = (Xword)size;
2917 	shdr->sh_flags |= SHF_EXECINSTR;
2918 
2919 	/*
2920 	 * Fill the buffer with the appropriate return instruction.
2921 	 * Note that there is no need to swap bytes on a non-native,
2922 	 * link, as the data being copied is given in bytes.
2923 	 */
2924 	if ((data->d_buf = libld_calloc(size, 1)) == NULL)
2925 		return ((Is_desc *)S_ERROR);
2926 	(void) memcpy(data->d_buf, ld_targ.t_nf.nf_template,
2927 	    ld_targ.t_nf.nf_size);
2928 
2929 	if (aplist_append(&ofl->ofl_maptext, isec, AL_CNT_OFL_MAPSECS) == NULL)
2930 		return ((Is_desc *)S_ERROR);
2931 
2932 	return (isec);
2933 }
2934