xref: /illumos-gate/usr/src/cmd/sgs/libld/common/sections.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	Copyright (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  *
27  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * Module sections. Initialize special sections
34  */
35 #include	<string.h>
36 #include	<strings.h>
37 #include	<stdio.h>
38 #include	<unistd.h>
39 #include	<link.h>
40 #include	"debug.h"
41 #include	"msg.h"
42 #include	"_libld.h"
43 
44 
45 /*
46  * If -zignore is in effect, scan all input sections to see if there are any
47  * which haven't been referenced (and hence can be discarded).  If sections are
48  * to be discarded, rescan the output relocations and the symbol table and
49  * remove the relocations and symbol entries that are no longer required.
50  *
51  * Note:  It's possible that a section which is being discarded has contributed
52  *	  to the GOT table or the PLT table.  However, we can't at this point
53  *	  eliminate the corresponding entries.  This is because there could well
54  *	  be other sections referencing those same entries, but we don't have
55  *	  the infrastructure to determine this.  So, keep the PLT and GOT
56  *	  entries in the table in case someone wants them.
57  * Note:  The section to be affected needs to be allocatable.
58  *	  So even if -zignore is in effect, if the section is not allocatable,
59  *	  we do not eliminate it.
60  */
61 uintptr_t
62 ignore_section_processing(Ofl_desc *ofl)
63 {
64 	Listnode	*lnp;
65 	Ifl_desc	*ifl;
66 	Rel_cache	*rcp;
67 
68 	for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, ifl)) {
69 		uint_t		num, discard;
70 
71 		/*
72 		 * Diagnose (-D unused) a completely unreferenced file.
73 		 */
74 		if ((ifl->ifl_flags & FLG_IF_FILEREF) == 0)
75 			DBG_CALL(Dbg_unused_file(ifl->ifl_name, 0));
76 		if ((ifl->ifl_flags & FLG_IF_IGNORE) == 0)
77 			continue;
78 
79 		/*
80 		 * Before scanning the whole symbol table to determine
81 		 * if we have symbols to discard - let's do a (relatively)
82 		 * quick scan of the sections and determine if we've discarded
83 		 * any sections.
84 		 */
85 		for (discard = 0, num = 1; num < ifl->ifl_shnum; num++) {
86 			Is_desc	*isp;
87 			Os_desc *osp;
88 
89 			if (((isp = ifl->ifl_isdesc[num]) != 0) &&
90 			    ((isp->is_flags & FLG_IS_SECTREF) == 0) &&
91 			    ((osp = isp->is_osdesc) != 0) &&
92 			    (osp->os_sgdesc->sg_phdr.p_type == PT_LOAD)) {
93 				discard++;
94 				break;
95 			}
96 		}
97 		/*
98 		 * No sections are to be 'ignored'
99 		 */
100 		if ((!discard) || ((ifl->ifl_flags & FLG_IF_FILEREF) == 0))
101 			continue;
102 
103 		/*
104 		 * We know that we have discarded sections - now we
105 		 * scan the symtable for the file in question to determine
106 		 * if we need to discard associated with the 'ignored'
107 		 * sections.
108 		 */
109 		for (num = 1; num < ifl->ifl_symscnt; num++) {
110 			Sym_desc	*sdp;
111 			Sym		*symp;
112 			Os_desc		*osp;
113 			/* LINTED - only used for assert() */
114 			int		err;
115 
116 			sdp = ifl->ifl_oldndx[num];
117 			symp = sdp->sd_sym;
118 
119 			/*
120 			 * Skip any undefined, reserved section symbols, already
121 			 * discarded or eliminated symbols.  Also skip any
122 			 * symbols that don't originate from a section, or
123 			 * aren't defined from the file being examined.
124 			 */
125 			if ((symp->st_shndx == SHN_UNDEF) ||
126 			    (symp->st_shndx >= SHN_LORESERVE) ||
127 			    (ELF_ST_TYPE(symp->st_info) == STT_SECTION) ||
128 			    (sdp->sd_flags & FLG_SY_ISDISC) ||
129 			    (sdp->sd_flags1 & FLG_SY1_ELIM) ||
130 			    (sdp->sd_isc == 0) || (sdp->sd_file != ifl))
131 				continue;
132 
133 			/*
134 			 * If any references were made against the section
135 			 * the symbol is being defined in - skip it.
136 			 */
137 			if ((sdp->sd_isc->is_flags & FLG_IS_SECTREF) ||
138 			    (((ifl->ifl_flags & FLG_IF_FILEREF) &&
139 			    ((osp = sdp->sd_isc->is_osdesc) != 0) &&
140 			    (osp->os_sgdesc->sg_phdr.p_type != PT_LOAD))))
141 				continue;
142 
143 			if (ELF_ST_BIND(symp->st_info) == STB_LOCAL) {
144 				if (!(ofl->ofl_flags1 & FLG_OF1_REDLSYM)) {
145 					ofl->ofl_locscnt--;
146 
147 					err = st_delstring(ofl->ofl_strtab,
148 					    sdp->sd_name);
149 					assert(err != -1);
150 				}
151 				sdp->sd_flags |= FLG_SY_ISDISC;
152 				DBG_CALL(Dbg_syms_discarded(sdp, sdp->sd_isc));
153 				continue;
154 			}
155 
156 			if (sdp->sd_flags1 & FLG_SY1_LOCL) {
157 				ofl->ofl_scopecnt--;
158 				ofl->ofl_elimcnt++;
159 
160 				err = st_delstring(ofl->ofl_strtab,
161 				    sdp->sd_name);
162 				assert(err != -1);
163 
164 				sdp->sd_flags1 |= FLG_SY1_ELIM;
165 				DBG_CALL(Dbg_syms_discarded(sdp, sdp->sd_isc));
166 				continue;
167 			}
168 		}
169 	}
170 
171 	/*
172 	 * Scan all output relocations - searching for relocations
173 	 * against discarded/ignored sections.  If we find one - subtract it
174 	 * from out total outrel count.
175 	 */
176 	for (LIST_TRAVERSE(&ofl->ofl_outrels, lnp, rcp)) {
177 		Rel_desc	*orsp;
178 		Os_desc		*relosp;
179 
180 		/* LINTED */
181 		for (orsp = (Rel_desc *)(rcp + 1);
182 		    orsp < rcp->rc_free; orsp++) {
183 			Is_desc	*	_isdesc = orsp->rel_isdesc;
184 			uint_t		flags, entsize;
185 			Shdr *		shdr;
186 			Ifl_desc *	ifl;
187 
188 			if ((_isdesc == 0) ||
189 			    ((_isdesc->is_flags & (FLG_IS_SECTREF))) ||
190 			    ((ifl = _isdesc->is_file) == 0) ||
191 			    ((ifl->ifl_flags & FLG_IF_IGNORE) == 0) ||
192 			    ((shdr = _isdesc->is_shdr) == 0) ||
193 			    ((shdr->sh_flags & SHF_ALLOC) == 0))
194 				continue;
195 
196 			flags = orsp->rel_flags;
197 
198 			if (flags & (FLG_REL_GOT | FLG_REL_BSS |
199 			    FLG_REL_NOINFO | FLG_REL_PLT))
200 				continue;
201 
202 			relosp = orsp->rel_osdesc;
203 
204 			if (orsp->rel_flags & FLG_REL_RELA)
205 				entsize = sizeof (Rela);
206 			else
207 				entsize = sizeof (Rel);
208 
209 
210 			assert(relosp->os_szoutrels > 0);
211 			relosp->os_szoutrels -= entsize;
212 
213 			if (!(flags & FLG_REL_PLT))
214 				ofl->ofl_reloccntsub++;
215 
216 			if (orsp->rel_rtype == M_R_RELATIVE)
217 				ofl->ofl_relocrelcnt--;
218 		}
219 	}
220 
221 	return (1);
222 }
223 
224 
225 /*
226  * Build a .bss section for allocation of tentative definitions.  Any `static'
227  * .bss definitions would have been associated to their own .bss sections and
228  * thus collected from the input files.  `global' .bss definitions are tagged
229  * as COMMON and do not cause any associated .bss section elements to be
230  * generated.  Here we add up all these COMMON symbols and generate the .bss
231  * section required to represent them.
232  */
233 uintptr_t
234 make_bss(Ofl_desc *ofl, Xword size, Xword align, uint_t tls)
235 {
236 	Shdr		*shdr;
237 	Elf_Data	*data;
238 	Is_desc		*isec;
239 	Os_desc		*osp;
240 	uint_t		ident;
241 	Xword		rsize = (Xword)ofl->ofl_relocbsssz;
242 
243 	/*
244 	 * Allocate and initialize the Elf_Data structure.
245 	 */
246 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
247 		return (S_ERROR);
248 	data->d_type = ELF_T_BYTE;
249 	data->d_size = (size_t)size;
250 	data->d_align = (size_t)align;
251 	data->d_version = ofl->ofl_libver;
252 
253 	/*
254 	 * Allocate and initialize the Shdr structure.
255 	 */
256 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
257 		return (S_ERROR);
258 	shdr->sh_type = SHT_NOBITS;
259 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
260 	if (tls)
261 		shdr->sh_flags |= SHF_TLS;
262 	shdr->sh_size = size;
263 	shdr->sh_addralign = align;
264 
265 	/*
266 	 * Allocate and initialize the Is_desc structure.
267 	 */
268 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
269 		return (S_ERROR);
270 
271 	isec->is_shdr = shdr;
272 	isec->is_indata = data;
273 
274 	if (tls) {
275 		isec->is_name = MSG_ORIG(MSG_SCN_TBSS);
276 		ident = M_ID_TLSBSS;
277 		ofl->ofl_istlsbss = isec;
278 	} else {
279 		isec->is_name = MSG_ORIG(MSG_SCN_BSS);
280 		ident = M_ID_BSS;
281 		ofl->ofl_isbss = isec;
282 	}
283 	/*
284 	 * Retain this .bss input section as this will be where global
285 	 * symbol references are added.
286 	 */
287 	if ((osp = place_section(ofl, isec, ident, 0)) == (Os_desc *)S_ERROR)
288 		return (S_ERROR);
289 
290 	/*
291 	 * If relocations exist against .*bss section, a
292 	 * section symbol must be created for the section in
293 	 * the .dynsym symbol table.
294 	 */
295 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
296 		Word	flagtotest;
297 		if (tls)
298 			flagtotest = FLG_OF1_TLSOREL;
299 		else
300 			flagtotest = FLG_OF1_BSSOREL;
301 
302 		if (ofl->ofl_flags1 & flagtotest) {
303 			ofl->ofl_dynshdrcnt++;
304 			osp->os_flags |= FLG_OS_OUTREL;
305 		}
306 	}
307 
308 	osp->os_szoutrels = rsize;
309 
310 	return (1);
311 }
312 
313 
314 /*
315  * Build a SHT_{INIT|FINI|PREINIT}ARRAY section (specified via
316  * ld -z *array=name
317  */
318 uintptr_t
319 make_array(Ofl_desc *ofl, Word shtype, const char *sectname, List *list)
320 {
321 	uint_t		entcount;
322 	Listnode	*lnp;
323 	Elf_Data	*data;
324 	Is_desc		*isec;
325 	Shdr		*shdr;
326 	Sym_desc	*sdp;
327 	Rel_desc	reld;
328 	Rela		reloc;
329 	Os_desc		*osp;
330 
331 	if (list->head == NULL)
332 		return (1);
333 
334 	entcount = 0;
335 	for (LIST_TRAVERSE(list, lnp, sdp))
336 		entcount++;
337 
338 	/*
339 	 * Allocate and initialize the Elf_Data structure.
340 	 */
341 	if (((data = libld_calloc(sizeof (Elf_Data), 1)) == 0) ||
342 	    ((data->d_buf = libld_calloc(sizeof (Addr), entcount)) == 0))
343 		return (S_ERROR);
344 
345 	data->d_type = ELF_T_ADDR;
346 	data->d_size = sizeof (Addr) * entcount;
347 	data->d_align = sizeof (Addr);
348 	data->d_version = ofl->ofl_libver;
349 
350 	/*
351 	 * Allocate and initialize the Shdr structure.
352 	 */
353 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
354 		return (S_ERROR);
355 	shdr->sh_type = shtype;
356 	shdr->sh_size = (Xword)data->d_size;
357 	shdr->sh_entsize = sizeof (Addr);
358 	shdr->sh_addralign = (Xword)data->d_align;
359 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
360 
361 	/*
362 	 * Allocate and initialize the Is_desc structure.
363 	 */
364 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
365 		return (S_ERROR);
366 	isec->is_name = sectname;
367 	isec->is_shdr = shdr;
368 	isec->is_indata = data;
369 
370 	if (place_section(ofl, isec, M_ID_ARRAY, 0) == (Os_desc *)S_ERROR)
371 		return (S_ERROR);
372 
373 	osp = isec->is_osdesc;
374 
375 	if ((ofl->ofl_osinitarray == 0) && (shtype == SHT_INIT_ARRAY))
376 		ofl->ofl_osinitarray = osp;
377 	if ((ofl->ofl_ospreinitarray == 0) && (shtype == SHT_PREINIT_ARRAY))
378 		ofl->ofl_ospreinitarray = osp;
379 	else if ((ofl->ofl_osfiniarray == 0) && (shtype == SHT_FINI_ARRAY))
380 		ofl->ofl_osfiniarray = osp;
381 
382 	/*
383 	 * Create relocations against this section to initialize it to the
384 	 * function addresses.
385 	 */
386 	reld.rel_osdesc = osp;
387 	reld.rel_isdesc = isec;
388 	reld.rel_move = 0;
389 	reld.rel_flags = FLG_REL_LOAD;
390 
391 	/*
392 	 * Fabricate the relocation information (as if a relocation record had
393 	 * been input - see init_rel()).
394 	 */
395 	reld.rel_rtype = M_R_ARRAYADDR;
396 	reld.rel_roffset = 0;
397 	reld.rel_raddend = 0;
398 	reld.rel_typedata = 0;
399 
400 	/*
401 	 * Create a minimal relocation record to satisfy process_sym_reloc()
402 	 * debugging requirements.
403 	 */
404 	reloc.r_offset = 0;
405 	reloc.r_info = ELF_R_INFO(0, M_R_ARRAYADDR);
406 	reloc.r_addend = 0;
407 
408 	DBG_CALL(Dbg_reloc_generate(osp, M_REL_SHT_TYPE));
409 	for (LIST_TRAVERSE(list, lnp, sdp)) {
410 		reld.rel_sname = sdp->sd_name;
411 		reld.rel_sym = sdp;
412 
413 		if (process_sym_reloc(ofl, &reld, (Rel *)&reloc, isec,
414 		    MSG_INTL(MSG_STR_COMMAND)) == S_ERROR)
415 			return (S_ERROR);
416 
417 		reld.rel_roffset += (Xword)sizeof (Addr);
418 		reloc.r_offset = reld.rel_roffset;
419 	}
420 
421 	return (1);
422 }
423 
424 /*
425  * Build a comment section (-Qy option).
426  */
427 uintptr_t
428 make_comment(Ofl_desc *ofl)
429 {
430 	Shdr		*shdr;
431 	Elf_Data	*data;
432 	Is_desc		*isec;
433 
434 	/*
435 	 * Allocate and initialize the Elf_Data structure.
436 	 */
437 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
438 		return (S_ERROR);
439 	data->d_type = ELF_T_BYTE;
440 	data->d_buf = (void *)ofl->ofl_sgsid;
441 	data->d_size = strlen(ofl->ofl_sgsid) + 1;
442 	data->d_align = 1;
443 	data->d_version = ofl->ofl_libver;
444 
445 	/*
446 	 * Allocate and initialize the Shdr structure.
447 	 */
448 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
449 		return (S_ERROR);
450 	shdr->sh_type = SHT_PROGBITS;
451 	shdr->sh_size = (Xword)data->d_size;
452 	shdr->sh_addralign = 1;
453 
454 	/*
455 	 * Allocate and initialize the Is_desc structure.
456 	 */
457 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
458 		return (S_ERROR);
459 	isec->is_name = MSG_ORIG(MSG_SCN_COMMENT);
460 	isec->is_shdr = shdr;
461 	isec->is_indata = data;
462 
463 	return ((uintptr_t)place_section(ofl, isec, M_ID_NOTE, 0));
464 }
465 
466 /*
467  * Make the dynamic section.  Calculate the size of any strings referenced
468  * within this structure, they will be added to the global string table
469  * (.dynstr).  This routine should be called before make_dynstr().
470  */
471 uintptr_t
472 make_dynamic(Ofl_desc *ofl)
473 {
474 	Shdr		*shdr;
475 	Os_desc		*osp;
476 	Elf_Data	*data;
477 	Is_desc		*isec;
478 	size_t		cnt = 0;
479 	Listnode	*lnp;
480 	Ifl_desc	*ifl;
481 	Sym_desc	*sdp;
482 	size_t		size;
483 	Word		flags = ofl->ofl_flags;
484 	int		unused = 0;
485 
486 	/*
487 	 * Allocate and initialize the Shdr structure.
488 	 */
489 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
490 		return (S_ERROR);
491 	shdr->sh_type = SHT_DYNAMIC;
492 	shdr->sh_flags = SHF_WRITE;
493 	if (!(flags & FLG_OF_RELOBJ))
494 		shdr->sh_flags |= SHF_ALLOC;
495 	shdr->sh_addralign = M_WORD_ALIGN;
496 	shdr->sh_entsize = (Xword)elf_fsize(ELF_T_DYN, 1, ofl->ofl_libver);
497 	if (shdr->sh_entsize == 0) {
498 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_FSIZE), ofl->ofl_name);
499 		return (S_ERROR);
500 	}
501 
502 
503 	/*
504 	 * Allocate and initialize the Elf_Data structure.
505 	 */
506 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
507 		return (S_ERROR);
508 	data->d_type = ELF_T_DYN;
509 	data->d_size = 0;
510 	data->d_align = M_WORD_ALIGN;
511 	data->d_version = ofl->ofl_libver;
512 
513 	/*
514 	 * Allocate and initialize the Is_desc structure.
515 	 */
516 	if ((isec = libld_calloc(1, sizeof (Is_desc))) ==
517 	    (Is_desc *)0)
518 		return (S_ERROR);
519 	isec->is_name = MSG_ORIG(MSG_SCN_DYNAMIC);
520 	isec->is_shdr = shdr;
521 	isec->is_indata = data;
522 
523 	osp = ofl->ofl_osdynamic = place_section(ofl, isec, M_ID_DYNAMIC, 0);
524 
525 	/*
526 	 * Reserve entries for any needed dependencies.
527 	 */
528 	for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, ifl)) {
529 		Sdf_desc *	sdf;
530 
531 		if (!(ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR)))
532 			continue;
533 
534 		/*
535 		 * If this dependency didn't satisfy any symbol references
536 		 * generate a debugging diagnostic (ld(1) -Dunused can be used
537 		 * to display these).  If this is a standard needed dependency,
538 		 * and -z ignore is in effect, drop the dependency.  Explicitly
539 		 * defined dependencies (i.e., -N dep) don't get dropped, and
540 		 * are flagged as being required to simplify update_odynamic()
541 		 * processing.
542 		 */
543 		if (!(ifl->ifl_flags & FLG_IF_DEPREQD)) {
544 			if (unused++ == 0)
545 				DBG_CALL(Dbg_util_nl());
546 			DBG_CALL(Dbg_unused_file(ifl->ifl_soname, 0));
547 
548 			if (ifl->ifl_flags & FLG_IF_NEEDSTR)
549 				ifl->ifl_flags |= FLG_IF_DEPREQD;
550 			else if (ifl->ifl_flags & FLG_IF_IGNORE)
551 				continue;
552 		}
553 
554 		/*
555 		 * If this object has an accompanying shared object definition
556 		 * determine if an alternative shared object name has been
557 		 * specified.
558 		 */
559 		if (((sdf = ifl->ifl_sdfdesc) != 0) &&
560 		    (sdf->sdf_flags & FLG_SDF_SONAME))
561 			ifl->ifl_soname = sdf->sdf_soname;
562 
563 		/*
564 		 * If this object is a lazyload reserve a DT_POSFLAG1 entry.
565 		 */
566 		if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_GRPPRM))
567 			cnt++;
568 
569 		if (st_insert(ofl->ofl_dynstrtab, ifl->ifl_soname) == -1)
570 			return (S_ERROR);
571 		cnt++;
572 
573 		/*
574 		 * If the needed entry contains the $ORIGIN token make sure
575 		 * the associated DT_1_FLAGS entry is created.
576 		 */
577 		if (strstr(ifl->ifl_soname, MSG_ORIG(MSG_STR_ORIGIN))) {
578 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
579 			ofl->ofl_dtflags |= DF_ORIGIN;
580 		}
581 	}
582 
583 	if (unused)
584 		DBG_CALL(Dbg_util_nl());
585 
586 	/*
587 	 * Reserve entries for any per-symbol auxiliary/filter strings.
588 	 */
589 	if (ofl->ofl_dtsfltrs) {
590 		/* LINTED */
591 		Dfltr_desc *	dftp;
592 		Aliste		off;
593 
594 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp))
595 			cnt++;
596 	}
597 
598 	/*
599 	 * Reserve entries for any _init() and _fini() section addresses.
600 	 */
601 	if (((sdp = sym_find(MSG_ORIG(MSG_SYM_INIT_U),
602 	    SYM_NOHASH, 0, ofl)) != NULL) && sdp->sd_ref == REF_REL_NEED) {
603 		sdp->sd_flags |= FLG_SY_UPREQD;
604 		cnt++;
605 	}
606 	if (((sdp = sym_find(MSG_ORIG(MSG_SYM_FINI_U),
607 	    SYM_NOHASH, 0, ofl)) != NULL) && sdp->sd_ref == REF_REL_NEED) {
608 		sdp->sd_flags |= FLG_SY_UPREQD;
609 		cnt++;
610 	}
611 
612 	/*
613 	 * Reserve entries for any soname, filter name (shared libs only),
614 	 * run-path pointers, cache names and audit requirements..
615 	 */
616 	if (ofl->ofl_soname) {
617 		cnt++;
618 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_soname) == -1)
619 			return (S_ERROR);
620 	}
621 	if (ofl->ofl_filtees) {
622 		cnt++;
623 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_filtees) == -1)
624 			return (S_ERROR);
625 
626 		/*
627 		 * If the filtees entry contains the $ORIGIN token make sure
628 		 * the associated DT_1_FLAGS entry is created.
629 		 */
630 		if (strstr(ofl->ofl_filtees, MSG_ORIG(MSG_STR_ORIGIN))) {
631 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
632 			ofl->ofl_dtflags |= DF_ORIGIN;
633 		}
634 	}
635 	if (ofl->ofl_rpath) {
636 		cnt += 2;	/* DT_RPATH & DT_RUNPATH */
637 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_rpath) == -1)
638 			return (S_ERROR);
639 
640 		/*
641 		 * If the rpath entry contains the $ORIGIN token make sure
642 		 * the associated DT_1_FLAGS entry is created.
643 		 */
644 		if (strstr(ofl->ofl_rpath, MSG_ORIG(MSG_STR_ORIGIN))) {
645 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
646 			ofl->ofl_dtflags |= DF_ORIGIN;
647 		}
648 	}
649 	if (ofl->ofl_config) {
650 		cnt++;
651 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_config) == -1)
652 			return (S_ERROR);
653 
654 		/*
655 		 * If the config entry contains the $ORIGIN token make sure
656 		 * the associated DT_1_FLAGS entry is created.
657 		 */
658 		if (strstr(ofl->ofl_config, MSG_ORIG(MSG_STR_ORIGIN))) {
659 			ofl->ofl_dtflags_1 |= DF_1_ORIGIN;
660 			ofl->ofl_dtflags |= DF_ORIGIN;
661 		}
662 	}
663 	if (ofl->ofl_depaudit) {
664 		cnt++;
665 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_depaudit) == -1)
666 			return (S_ERROR);
667 	}
668 	if (ofl->ofl_audit) {
669 		cnt++;
670 		if (st_insert(ofl->ofl_dynstrtab, ofl->ofl_audit) == -1)
671 			return (S_ERROR);
672 	}
673 
674 
675 	/*
676 	 * The following DT_* entries do not apply to relocatable objects
677 	 */
678 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
679 		/*
680 		 * Reserve entries for the HASH, STRTAB, STRSZ, SYMTAB, SYMENT,
681 		 * and CHECKSUM.
682 		 */
683 		cnt += 6;
684 
685 		if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
686 		    FLG_OF_VERDEF)
687 			cnt += 2;		/* DT_VERDEF & DT_VERDEFNUM */
688 
689 		if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
690 		    FLG_OF_VERNEED)
691 			cnt += 2;		/* DT_VERNEED & DT_VERNEEDNUM */
692 
693 		if ((ofl->ofl_flags1 & FLG_OF1_RELCNT) &&
694 		    ofl->ofl_relocrelcnt)	/* RELACOUNT */
695 			cnt++;
696 
697 		if (flags & FLG_OF_TEXTREL)	/* TEXTREL */
698 			cnt++;
699 
700 		if (ofl->ofl_osfiniarray)	/* FINI_ARRAY & FINI_ARRAYSZ */
701 			cnt += 2;
702 
703 		if (ofl->ofl_osinitarray)	/* INIT_ARRAY & INIT_ARRAYSZ */
704 			cnt += 2;
705 
706 		if (ofl->ofl_ospreinitarray)	/* PREINIT_ARRAY & */
707 			cnt += 2;		/*	PREINIT_ARRAYSZ */
708 
709 		/*
710 		 * If we have plt's reserve a PLT, PLTSZ, PLTREL and JMPREL.
711 		 */
712 		if (ofl->ofl_pltcnt)
713 			cnt += 3;
714 
715 		/*
716 		 * If pltpadding is needed (Sparcv9)
717 		 */
718 		if (ofl->ofl_pltpad)
719 			cnt += 2;		/* DT_PLTPAD & DT_PLTPADSZ */
720 
721 		/*
722 		 * If we have any relocations reserve a REL, RELSZ and
723 		 * RELENT entry.
724 		 */
725 		if (ofl->ofl_relocsz)
726 			cnt += 3;
727 
728 		/*
729 		 * If a syminfo section is required create SYMINFO, SYMINSZ,
730 		 * and SYMINENT entries.
731 		 */
732 		if (ofl->ofl_flags & FLG_OF_SYMINFO)
733 			cnt += 3;
734 
735 		/*
736 		 * If there are any partially initialized sections allocate
737 		 * MOVEENT, MOVESZ and MOVETAB.
738 		 */
739 		if (ofl->ofl_osmove)
740 			cnt += 3;
741 
742 		/*
743 		 * Allocate one DT_REGISTER entry for ever register symbol.
744 		 */
745 		cnt += ofl->ofl_regsymcnt;
746 
747 		/*
748 		 * Reserve a entry for each '-zrtldinfo=...' specified
749 		 * on the command line.
750 		 */
751 		for (LIST_TRAVERSE(&ofl->ofl_rtldinfo, lnp, sdp))
752 			cnt++;
753 
754 		/*
755 		 * These two entries should only be placed in a segment
756 		 * which is writable.  If it's a read-only segment
757 		 * (due to mapfile magic, e.g. libdl.so.1) then don't allocate
758 		 * these entries.
759 		 */
760 		if ((osp->os_sgdesc) &&
761 		    (osp->os_sgdesc->sg_phdr.p_flags & PF_W)) {
762 			cnt++;			/* FEATURE_1 */
763 
764 			if (ofl->ofl_osinterp)
765 				cnt++;		/* DEBUG */
766 		}
767 
768 		/*
769 		 * Any hardware/software capabilities?
770 		 */
771 		if (ofl->ofl_oscap)
772 			cnt++;			/* SUNW_CAP */
773 	}
774 
775 	if (flags & FLG_OF_SYMBOLIC)
776 		cnt++;				/* SYMBOLIC */
777 
778 	/*
779 	 * Account for Architecture dependent .dynamic entries, and defaults
780 	 */
781 	mach_make_dynamic(ofl, &cnt);
782 
783 	cnt += 3;				/* DT_FLAGS, DT_FLAGS_1, */
784 						/*   and DT_NULL */
785 
786 	/*
787 	 * Determine the size of the section from the number of entries.
788 	 */
789 	size = cnt * (size_t)shdr->sh_entsize;
790 
791 	shdr->sh_size = (Xword)size;
792 	data->d_size = size;
793 
794 	return ((uintptr_t)ofl->ofl_osdynamic);
795 }
796 
797 /*
798  * Build the GOT section and its associated relocation entries.
799  */
800 uintptr_t
801 make_got(Ofl_desc *ofl)
802 {
803 	Shdr		*shdr;
804 	Elf_Data	*data;
805 	Is_desc		*isec;
806 	size_t		size = (size_t)ofl->ofl_gotcnt * M_GOT_ENTSIZE;
807 	size_t		rsize = (size_t)ofl->ofl_relocgotsz;
808 
809 	/*
810 	 * Allocate and initialize the Elf_Data structure.
811 	 */
812 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
813 		return (S_ERROR);
814 	data->d_type = ELF_T_BYTE;
815 	data->d_size = size;
816 	data->d_align = M_WORD_ALIGN;
817 	data->d_version = ofl->ofl_libver;
818 
819 	/*
820 	 * Allocate and initialize the Shdr structure.
821 	 */
822 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
823 		return (S_ERROR);
824 	shdr->sh_type = SHT_PROGBITS;
825 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
826 	shdr->sh_size = (Xword)size;
827 	shdr->sh_addralign = M_WORD_ALIGN;
828 	shdr->sh_entsize = M_GOT_ENTSIZE;
829 
830 	/*
831 	 * Allocate and initialize the Is_desc structure.
832 	 */
833 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
834 		return (S_ERROR);
835 	isec->is_name = MSG_ORIG(MSG_SCN_GOT);
836 	isec->is_shdr = shdr;
837 	isec->is_indata = data;
838 
839 	if ((ofl->ofl_osgot = place_section(ofl, isec, M_ID_GOT, 0)) ==
840 	    (Os_desc *)S_ERROR)
841 		return (S_ERROR);
842 
843 	ofl->ofl_osgot->os_szoutrels = (Xword)rsize;
844 
845 	return (1);
846 }
847 
848 /*
849  * Build an interp section.
850  */
851 uintptr_t
852 make_interp(Ofl_desc *ofl)
853 {
854 	Shdr		*shdr;
855 	Elf_Data	*data;
856 	Is_desc		*isec;
857 	const char	*iname = ofl->ofl_interp;
858 	size_t		size;
859 
860 	/*
861 	 * We always build an .interp section for dynamic executables.  However
862 	 * if the user has specifically specified an interpretor we'll build
863 	 * this section for any output (presumably the user knows what they are
864 	 * doing. refer ABI section 5-4, and ld.1 man page use of -I).
865 	 */
866 	if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC |
867 	    FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname)
868 		return (1);
869 
870 	/*
871 	 * In the case of a dynamic executable supply a default interpretor
872 	 * if a specific interpreter has not been specified.
873 	 */
874 	if (!iname) {
875 		if (ofl->ofl_e_machine == EM_SPARCV9)
876 			iname = ofl->ofl_interp =
877 				MSG_ORIG(MSG_PTH_RTLD_SPARCV9);
878 		else if (ofl->ofl_e_machine == EM_AMD64)
879 			iname = ofl->ofl_interp =
880 				MSG_ORIG(MSG_PTH_RTLD_AMD64);
881 		else
882 			iname = ofl->ofl_interp = MSG_ORIG(MSG_PTH_RTLD);
883 	}
884 
885 	size = strlen(iname) + 1;
886 
887 	/*
888 	 * Allocate and initialize the Elf_Data structure.
889 	 */
890 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
891 		return (S_ERROR);
892 	data->d_type = ELF_T_BYTE;
893 	data->d_size = size;
894 	data->d_version = ofl->ofl_libver;
895 
896 	/*
897 	 * Allocate and initialize the Shdr structure.
898 	 */
899 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
900 		return (S_ERROR);
901 	shdr->sh_type = SHT_PROGBITS;
902 	shdr->sh_flags = SHF_ALLOC;
903 	shdr->sh_size = (Xword)size;
904 
905 	/*
906 	 * Allocate and initialize the Is_desc structure.
907 	 */
908 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
909 		return (S_ERROR);
910 	isec->is_name = MSG_ORIG(MSG_SCN_INTERP);
911 	isec->is_shdr = shdr;
912 	isec->is_indata = data;
913 
914 	ofl->ofl_osinterp = place_section(ofl, isec, M_ID_INTERP, 0);
915 	return ((uintptr_t)ofl->ofl_osinterp);
916 }
917 
918 /*
919  * Build a hardware/software capabilities section.
920  */
921 uintptr_t
922 make_cap(Ofl_desc *ofl)
923 {
924 	Shdr		*shdr;
925 	Elf_Data	*data;
926 	Is_desc		*isec;
927 	Os_desc		*osec;
928 	Cap		*cap;
929 	size_t		size = 0;
930 
931 	/*
932 	 * Determine how many entries are required.
933 	 */
934 	if (ofl->ofl_hwcap_1)
935 		size++;
936 	if (ofl->ofl_sfcap_1)
937 		size++;
938 	if (size == 0)
939 		return (1);
940 	size++;				/* Add CA_SUNW_NULL */
941 
942 	/*
943 	 * Allocate and initialize the Elf_Data structure.
944 	 */
945 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
946 		return (S_ERROR);
947 	data->d_type = ELF_T_CAP;
948 	data->d_version = ofl->ofl_libver;
949 	data->d_align = M_WORD_ALIGN;
950 
951 	/*
952 	 * Allocate and initialize the Shdr structure.
953 	 */
954 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
955 		return (S_ERROR);
956 	shdr->sh_type = SHT_SUNW_cap;
957 	shdr->sh_flags = SHF_ALLOC;
958 	shdr->sh_addralign = M_WORD_ALIGN;
959 	shdr->sh_entsize = (Xword)elf_fsize(ELF_T_CAP, 1, ofl->ofl_libver);
960 	if (shdr->sh_entsize == 0) {
961 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_FSIZE), ofl->ofl_name);
962 		return (S_ERROR);
963 	}
964 
965 	/*
966 	 * Allocate and initialize the Is_desc structure.
967 	 */
968 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
969 		return (S_ERROR);
970 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWCAP);
971 	isec->is_shdr = shdr;
972 	isec->is_indata = data;
973 
974 	/*
975 	 * Determine the size of the section, and create the data.
976 	 */
977 	size = size * (size_t)shdr->sh_entsize;
978 	shdr->sh_size = (Xword)size;
979 	data->d_size = size;
980 	if ((data->d_buf = libld_malloc(size)) == 0)
981 		return (S_ERROR);
982 
983 	cap = (Cap *)data->d_buf;
984 	if (ofl->ofl_hwcap_1) {
985 		cap->c_tag = CA_SUNW_HW_1;
986 		cap->c_un.c_val = ofl->ofl_hwcap_1;
987 		cap++;
988 	}
989 	if (ofl->ofl_sfcap_1) {
990 		cap->c_tag = CA_SUNW_SF_1;
991 		cap->c_un.c_val = ofl->ofl_sfcap_1;
992 		cap++;
993 	}
994 	cap->c_tag = CA_SUNW_NULL;
995 	cap->c_un.c_val = 0;
996 
997 	/*
998 	 * If we're not creating a relocatable object, save the output section
999 	 * to trigger the creation of an associated  a program header.
1000 	 */
1001 	osec = place_section(ofl, isec, M_ID_CAP, 0);
1002 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
1003 		ofl->ofl_oscap = osec;
1004 
1005 	return ((uintptr_t)osec);
1006 }
1007 
1008 /*
1009  * Build the PLT section and its associated relocation entries.
1010  */
1011 uintptr_t
1012 make_plt(Ofl_desc *ofl)
1013 {
1014 	Shdr		*shdr;
1015 	Elf_Data	*data;
1016 	Is_desc		*isec;
1017 	size_t		size = (size_t)M_PLT_RESERVSZ +
1018 				(((size_t)ofl->ofl_pltcnt +
1019 				(size_t)ofl->ofl_pltpad) * M_PLT_ENTSIZE);
1020 	size_t		rsize = (size_t)ofl->ofl_relocpltsz;
1021 
1022 #if defined(sparc)
1023 	/*
1024 	 * Account for the NOP at the end of the plt.
1025 	 */
1026 	size += sizeof (Word);
1027 #endif
1028 
1029 	/*
1030 	 * Allocate and initialize the Elf_Data structure.
1031 	 */
1032 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1033 		return (S_ERROR);
1034 	data->d_type = ELF_T_BYTE;
1035 	data->d_size = size;
1036 	data->d_align = M_PLT_ALIGN;
1037 	data->d_version = ofl->ofl_libver;
1038 
1039 	/*
1040 	 * Allocate and initialize the Shdr structure.
1041 	 */
1042 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1043 		return (S_ERROR);
1044 	shdr->sh_type = SHT_PROGBITS;
1045 	shdr->sh_flags = M_PLT_SHF_FLAGS;
1046 	shdr->sh_size = (Xword)size;
1047 	shdr->sh_addralign = M_PLT_ALIGN;
1048 	shdr->sh_entsize = M_PLT_ENTSIZE;
1049 
1050 	/*
1051 	 * Allocate and initialize the Is_desc structure.
1052 	 */
1053 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == (Is_desc *)0)
1054 		return (S_ERROR);
1055 	isec->is_name = MSG_ORIG(MSG_SCN_PLT);
1056 	isec->is_shdr = shdr;
1057 	isec->is_indata = data;
1058 
1059 	if ((ofl->ofl_osplt = place_section(ofl, isec, M_ID_PLT, 0)) ==
1060 	    (Os_desc *)S_ERROR)
1061 		return (S_ERROR);
1062 
1063 	ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1064 
1065 	return (1);
1066 }
1067 
1068 /*
1069  * Make the hash table.  Only built for dynamic executables and shared
1070  * libraries, and provides hashed lookup into the global symbol table
1071  * (.dynsym) for the run-time linker to resolve symbol lookups.
1072  */
1073 uintptr_t
1074 make_hash(Ofl_desc *ofl)
1075 {
1076 	Shdr		*shdr;
1077 	Elf_Data	*data;
1078 	Is_desc		*isec;
1079 	size_t		size;
1080 	Word		nsyms = ofl->ofl_globcnt;
1081 	size_t		cnt;
1082 
1083 	/*
1084 	 * Allocate and initialize the Elf_Data structure.
1085 	 */
1086 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1087 		return (S_ERROR);
1088 	data->d_type = ELF_T_WORD;
1089 	data->d_align = M_WORD_ALIGN;
1090 	data->d_version = ofl->ofl_libver;
1091 
1092 	/*
1093 	 * Allocate and initialize the Shdr structure.
1094 	 */
1095 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1096 		return (S_ERROR);
1097 	shdr->sh_type = SHT_HASH;
1098 	shdr->sh_flags = SHF_ALLOC;
1099 	shdr->sh_addralign = M_WORD_ALIGN;
1100 	shdr->sh_entsize = (Xword)elf_fsize(ELF_T_WORD, 1, ofl->ofl_libver);
1101 	if (shdr->sh_entsize == 0) {
1102 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_FSIZE), ofl->ofl_name);
1103 		return (S_ERROR);
1104 	}
1105 
1106 	/*
1107 	 * Allocate and initialize the Is_desc structure.
1108 	 */
1109 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1110 		return (S_ERROR);
1111 	isec->is_name = MSG_ORIG(MSG_SCN_HASH);
1112 	isec->is_shdr = shdr;
1113 	isec->is_indata = data;
1114 
1115 	/*
1116 	 * Place the section first since it will affect the local symbol
1117 	 * count.
1118 	 */
1119 	if ((ofl->ofl_oshash = place_section(ofl, isec, M_ID_HASH, 0)) ==
1120 	    (Os_desc *)S_ERROR)
1121 		return (S_ERROR);
1122 
1123 	/*
1124 	 * Calculate the number of output hash buckets.
1125 	 */
1126 	ofl->ofl_hashbkts = findprime(nsyms);
1127 
1128 	/*
1129 	 * The size of the hash table is determined by
1130 	 *
1131 	 *	i.	the initial nbucket and nchain entries (2)
1132 	 *	ii.	the number of buckets (calculated above)
1133 	 *	iii.	the number of chains (this is based on the number of
1134 	 *		symbols in the .dynsym array + NULL symbol).
1135 	 */
1136 	cnt = 2 + ofl->ofl_hashbkts + (ofl->ofl_dynshdrcnt +
1137 		ofl->ofl_globcnt + ofl->ofl_lregsymcnt + 1);
1138 	size = cnt * shdr->sh_entsize;
1139 
1140 	/*
1141 	 * Finalize the section header and data buffer initialization.
1142 	 */
1143 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
1144 		return (S_ERROR);
1145 	data->d_size = size;
1146 	shdr->sh_size = (Xword)size;
1147 
1148 	return (1);
1149 }
1150 
1151 /*
1152  * Generate the standard symbol table.  Contains all locals and globals,
1153  * and resides in a non-allocatable section (ie. it can be stripped).
1154  */
1155 uintptr_t
1156 make_symtab(Ofl_desc *ofl)
1157 {
1158 	Shdr		*shdr;
1159 	Elf_Data	*data;
1160 	Is_desc		*isec;
1161 	Is_desc		*xisec = 0;
1162 	size_t		size;
1163 	Word		symcnt;
1164 
1165 	/*
1166 	 * Allocate and initialize the Elf_Data structure.
1167 	 */
1168 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1169 		return (S_ERROR);
1170 	data->d_type = ELF_T_SYM;
1171 	data->d_align = M_WORD_ALIGN;
1172 	data->d_version = ofl->ofl_libver;
1173 
1174 	/*
1175 	 * Allocate and initialize the Shdr structure.
1176 	 */
1177 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1178 		return (S_ERROR);
1179 	shdr->sh_type = SHT_SYMTAB;
1180 	shdr->sh_addralign = M_WORD_ALIGN;
1181 	shdr->sh_entsize = (Xword)elf_fsize(ELF_T_SYM, 1, ofl->ofl_libver);
1182 	if (shdr->sh_entsize == 0) {
1183 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_FSIZE), ofl->ofl_name);
1184 		return (S_ERROR);
1185 	}
1186 
1187 	/*
1188 	 * Allocate and initialize the Is_desc structure.
1189 	 */
1190 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1191 		return (S_ERROR);
1192 	isec->is_name = MSG_ORIG(MSG_SCN_SYMTAB);
1193 	isec->is_shdr = shdr;
1194 	isec->is_indata = data;
1195 
1196 	/*
1197 	 * Place the section first since it will affect the local symbol
1198 	 * count.
1199 	 */
1200 	if ((ofl->ofl_ossymtab = place_section(ofl, isec, M_ID_SYMTAB, 0)) ==
1201 	    (Os_desc *)S_ERROR)
1202 		return (S_ERROR);
1203 
1204 	/*
1205 	 * At this point we've created all but the 'shstrtab' section.
1206 	 * Determine if we have to use 'Extended Sections'.  If so - then
1207 	 * also create a SHT_SYMTAB_SHNDX section.
1208 	 */
1209 	if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
1210 		Shdr		*xshdr;
1211 		Elf_Data	*xdata;
1212 
1213 		if ((xdata = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1214 			return (S_ERROR);
1215 		xdata->d_type = ELF_T_WORD;
1216 		xdata->d_align = M_WORD_ALIGN;
1217 		xdata->d_version = ofl->ofl_libver;
1218 		if ((xshdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1219 			return (S_ERROR);
1220 		xshdr->sh_type = SHT_SYMTAB_SHNDX;
1221 		xshdr->sh_addralign = M_WORD_ALIGN;
1222 		xshdr->sh_entsize = sizeof (Word);
1223 		if ((xisec = libld_calloc(1, sizeof (Is_desc))) == 0)
1224 			return (S_ERROR);
1225 		xisec->is_name = MSG_ORIG(MSG_SCN_SYMTAB_SHNDX);
1226 		xisec->is_shdr = xshdr;
1227 		xisec->is_indata = xdata;
1228 		if ((ofl->ofl_ossymshndx = place_section(ofl, xisec,
1229 		    M_ID_SYMTAB_NDX, 0)) == (Os_desc *)S_ERROR)
1230 			return (S_ERROR);
1231 	}
1232 	/*
1233 	 * Calculated number of symbols, which need to be augmented by
1234 	 * the null first entry, the FILE symbol, and the .shstrtab entry.
1235 	 */
1236 	symcnt = (size_t)(3 + ofl->ofl_shdrcnt + ofl->ofl_scopecnt +
1237 		ofl->ofl_locscnt + ofl->ofl_globcnt);
1238 	size = symcnt * shdr->sh_entsize;
1239 
1240 	/*
1241 	 * Finalize the section header and data buffer initialization.
1242 	 */
1243 	data->d_size = size;
1244 	shdr->sh_size = (Xword)size;
1245 
1246 	/*
1247 	 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
1248 	 */
1249 	if (xisec) {
1250 		size_t	xsize = symcnt * sizeof (Word);
1251 
1252 		xisec->is_indata->d_size = xsize;
1253 		xisec->is_shdr->sh_size = (Xword)xsize;
1254 	}
1255 
1256 	return (1);
1257 }
1258 
1259 
1260 /*
1261  * Build a dynamic symbol table.  Contains only globals symbols and resides
1262  * in the text segment of a dynamic executable or shared library.
1263  */
1264 uintptr_t
1265 make_dynsym(Ofl_desc *ofl)
1266 {
1267 	Shdr		*shdr;
1268 	Elf_Data	*data;
1269 	Is_desc		*isec;
1270 	size_t		size;
1271 	Xword		cnt;
1272 
1273 	/*
1274 	 * Allocate and initialize the Elf_Data structure.
1275 	 */
1276 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1277 		return (S_ERROR);
1278 	data->d_type = ELF_T_SYM;
1279 	data->d_align = M_WORD_ALIGN;
1280 	data->d_version = ofl->ofl_libver;
1281 
1282 	/*
1283 	 * Allocate and initialize the Shdr structure.
1284 	 */
1285 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1286 		return (S_ERROR);
1287 	shdr->sh_type = SHT_DYNSYM;
1288 	shdr->sh_flags = SHF_ALLOC;
1289 	shdr->sh_addralign = M_WORD_ALIGN;
1290 	shdr->sh_entsize = (Xword)elf_fsize(ELF_T_SYM, 1, ofl->ofl_libver);
1291 	if (shdr->sh_entsize == 0) {
1292 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_FSIZE), ofl->ofl_name);
1293 		return (S_ERROR);
1294 	}
1295 
1296 	/*
1297 	 * Allocate and initialize the Is_desc structure.
1298 	 */
1299 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1300 		return (S_ERROR);
1301 	isec->is_name = MSG_ORIG(MSG_SCN_DYNSYM);
1302 	isec->is_shdr = shdr;
1303 	isec->is_indata = data;
1304 
1305 	/*
1306 	 * Place the section first since it will affect the local symbol
1307 	 * count.
1308 	 */
1309 	if ((ofl->ofl_osdynsym = place_section(ofl, isec, M_ID_DYNSYM, 0)) ==
1310 	    (Os_desc *)S_ERROR)
1311 		return (S_ERROR);
1312 
1313 	/*
1314 	 * One extra section header entry for the 'null' entry.
1315 	 */
1316 	cnt = 1 + ofl->ofl_dynshdrcnt + ofl->ofl_globcnt + ofl->ofl_lregsymcnt;
1317 	size = (size_t)cnt * shdr->sh_entsize;
1318 
1319 	/*
1320 	 * Finalize the section header and data buffer initialization.
1321 	 */
1322 	data->d_size = size;
1323 	shdr->sh_size = (Xword)size;
1324 
1325 	return (1);
1326 }
1327 
1328 /*
1329  * Build a SHT_SYMTAB_SHNDX for the .dynsym
1330  */
1331 uintptr_t
1332 make_dynsym_shndx(Ofl_desc *ofl)
1333 {
1334 	Is_desc		*isec;
1335 	Is_desc		*dynsymisp;
1336 	Shdr		*shdr, *dynshdr;
1337 	Elf_Data	*data;
1338 
1339 	/*
1340 	 * Allocate the Elf_Data structure.
1341 	 */
1342 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1343 		return (S_ERROR);
1344 	data->d_type = ELF_T_WORD;
1345 	data->d_align = M_WORD_ALIGN;
1346 	data->d_version = ofl->ofl_libver;
1347 
1348 	/*
1349 	 * Allocate the Shdr structure.
1350 	 */
1351 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1352 		return (S_ERROR);
1353 	shdr->sh_type = SHT_SYMTAB_SHNDX;
1354 	shdr->sh_addralign = M_WORD_ALIGN;
1355 	shdr->sh_entsize = sizeof (Word);
1356 
1357 	/*
1358 	 * Allocate the Is_desc structure.
1359 	 */
1360 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1361 		return (S_ERROR);
1362 	isec->is_name = MSG_ORIG(MSG_SCN_DYNSYM_SHNDX);
1363 	isec->is_shdr = shdr;
1364 	isec->is_indata = data;
1365 
1366 	if ((ofl->ofl_osdynshndx = place_section(ofl, isec,
1367 	    M_ID_DYNSYM_NDX, 0)) == (Os_desc *)S_ERROR)
1368 		return (S_ERROR);
1369 
1370 	assert(ofl->ofl_osdynsym);
1371 	dynsymisp = (Is_desc *)ofl->ofl_osdynsym->os_isdescs.head->data;
1372 	dynshdr = dynsymisp->is_shdr;
1373 	shdr->sh_size = (Xword)((dynshdr->sh_size / dynshdr->sh_entsize) *
1374 		sizeof (Word));
1375 	data->d_size = shdr->sh_size;
1376 
1377 	return (1);
1378 }
1379 
1380 
1381 /*
1382  * Build a string table for the section headers.
1383  */
1384 uintptr_t
1385 make_shstrtab(Ofl_desc *ofl)
1386 {
1387 	Shdr		*shdr;
1388 	Elf_Data	*data;
1389 	Is_desc		*isec;
1390 	size_t		size;
1391 
1392 	/*
1393 	 * Allocate the Elf_Data structure.
1394 	 */
1395 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1396 		return (S_ERROR);
1397 	data->d_type = ELF_T_BYTE;
1398 	data->d_align = 1;
1399 	data->d_version = ofl->ofl_libver;
1400 
1401 	/*
1402 	 * Allocate the Shdr structure.
1403 	 */
1404 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1405 		return (S_ERROR);
1406 	shdr->sh_type = SHT_STRTAB;
1407 	shdr->sh_flags |= SHF_STRINGS;
1408 	shdr->sh_addralign = 1;
1409 
1410 	/*
1411 	 * Allocate the Is_desc structure.
1412 	 */
1413 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1414 		return (S_ERROR);
1415 	isec->is_name = MSG_ORIG(MSG_SCN_SHSTRTAB);
1416 	isec->is_shdr = shdr;
1417 	isec->is_indata = data;
1418 
1419 	/*
1420 	 * Place the section first, as it may effect the number of section
1421 	 * headers to account for.
1422 	 */
1423 	if ((ofl->ofl_osshstrtab = place_section(ofl, isec, M_ID_NOTE, 0)) ==
1424 	    (Os_desc *)S_ERROR)
1425 		return (S_ERROR);
1426 
1427 	size = st_getstrtab_sz(ofl->ofl_shdrsttab);
1428 	assert(size > 0);
1429 
1430 	assert(size > 0);
1431 
1432 	data->d_size = size;
1433 	shdr->sh_size = (Xword)size;
1434 
1435 	return (1);
1436 }
1437 
1438 /*
1439  * Build a string section for the standard symbol table.
1440  */
1441 uintptr_t
1442 make_strtab(Ofl_desc *ofl)
1443 {
1444 	Shdr		*shdr;
1445 	Elf_Data	*data;
1446 	Is_desc		*isec;
1447 	size_t		size;
1448 
1449 	/*
1450 	 * This string table consists of all the global and local symbols.
1451 	 * Account for null bytes at end of the file name and the beginning
1452 	 * of section.
1453 	 */
1454 	if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
1455 		return (S_ERROR);
1456 
1457 	size = st_getstrtab_sz(ofl->ofl_strtab);
1458 	assert(size > 0);
1459 
1460 	/*
1461 	 * Allocate the Elf_Data structure.
1462 	 */
1463 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1464 		return (S_ERROR);
1465 	data->d_size = size;
1466 	data->d_type = ELF_T_BYTE;
1467 	data->d_align = 1;
1468 	data->d_version = ofl->ofl_libver;
1469 
1470 	/*
1471 	 * Allocate the Shdr structure.
1472 	 */
1473 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1474 		return (S_ERROR);
1475 	shdr->sh_size = (Xword)size;
1476 	shdr->sh_addralign = 1;
1477 	shdr->sh_type = SHT_STRTAB;
1478 	shdr->sh_flags |= SHF_STRINGS;
1479 
1480 	/*
1481 	 * Allocate and initialize the Is_desc structure.
1482 	 */
1483 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1484 		return (S_ERROR);
1485 	isec->is_name = MSG_ORIG(MSG_SCN_STRTAB);
1486 	isec->is_shdr = shdr;
1487 	isec->is_indata = data;
1488 
1489 	ofl->ofl_osstrtab = place_section(ofl, isec, M_ID_STRTAB, 0);
1490 	return ((uintptr_t)ofl->ofl_osstrtab);
1491 }
1492 
1493 /*
1494  * Build a string table for the dynamic symbol table.
1495  */
1496 uintptr_t
1497 make_dynstr(Ofl_desc *ofl)
1498 {
1499 	Shdr		*shdr;
1500 	Elf_Data	*data;
1501 	Is_desc		*isec;
1502 	size_t		size;
1503 
1504 	/*
1505 	 * Account for any local, named register symbols.  These locals are
1506 	 * required for reference from DT_REGISTER .dynamic entries.
1507 	 */
1508 	if (ofl->ofl_regsyms) {
1509 		int	ndx;
1510 
1511 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1512 			Sym_desc *	sdp;
1513 
1514 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1515 				continue;
1516 
1517 			if (((sdp->sd_flags1 & FLG_SY1_LOCL) == 0) &&
1518 			    (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
1519 				continue;
1520 
1521 			if (sdp->sd_sym->st_name == 0)
1522 				continue;
1523 
1524 			if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
1525 				return (S_ERROR);
1526 		}
1527 	}
1528 
1529 	/*
1530 	 * Reserve entries for any per-symbol auxiliary/filter strings.
1531 	 */
1532 	if (ofl->ofl_dtsfltrs) {
1533 		Dfltr_desc *	dftp;
1534 		Aliste		off;
1535 
1536 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp))
1537 			if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
1538 				return (S_ERROR);
1539 	}
1540 
1541 	size = st_getstrtab_sz(ofl->ofl_dynstrtab);
1542 	assert(size > 0);
1543 
1544 	/*
1545 	 * Allocate the Elf_Data structure.
1546 	 */
1547 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1548 		return (S_ERROR);
1549 	data->d_type = ELF_T_BYTE;
1550 	data->d_size = size;
1551 	data->d_align = 1;
1552 	data->d_version = ofl->ofl_libver;
1553 
1554 	/*
1555 	 * Allocate the Shdr structure.
1556 	 */
1557 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1558 		return (S_ERROR);
1559 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
1560 		shdr->sh_flags = SHF_ALLOC;
1561 
1562 	shdr->sh_type = SHT_STRTAB;
1563 	shdr->sh_flags |= SHF_STRINGS;
1564 	shdr->sh_size = (Xword)size;
1565 	shdr->sh_addralign = 1;
1566 
1567 	/*
1568 	 * Allocate and initialize the Is_desc structure.
1569 	 */
1570 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1571 		return (S_ERROR);
1572 	isec->is_name = MSG_ORIG(MSG_SCN_DYNSTR);
1573 	isec->is_shdr = shdr;
1574 	isec->is_indata = data;
1575 
1576 	ofl->ofl_osdynstr = place_section(ofl, isec, M_ID_DYNSTR, 0);
1577 	return ((uintptr_t)ofl->ofl_osdynstr);
1578 }
1579 
1580 /*
1581  * Generate an output relocation section which will contain the relocation
1582  * information to be applied to the `osp' section.
1583  *
1584  * If (osp == NULL) then we are creating the coalesced relocation section
1585  * for an executable and/or a shared object.
1586  */
1587 uintptr_t
1588 make_reloc(Ofl_desc *ofl, Os_desc *osp)
1589 {
1590 	Shdr		*shdr;
1591 	Elf_Data	*data;
1592 	Is_desc		*isec;
1593 	size_t		size;
1594 	Xword		sh_flags;
1595 	char 		*sectname;
1596 	Os_desc		*rosp;
1597 	Word		relsize;
1598 	const char	*rel_prefix;
1599 
1600 	/* LINTED */
1601 	if (M_REL_SHT_TYPE == SHT_REL) {
1602 		/* REL */
1603 		relsize = sizeof (Rel);
1604 		rel_prefix = MSG_ORIG(MSG_SCN_REL);
1605 	} else {
1606 		/* RELA */
1607 		relsize = sizeof (Rela);
1608 		rel_prefix = MSG_ORIG(MSG_SCN_RELA);
1609 	}
1610 
1611 	if (osp) {
1612 		size = osp->os_szoutrels;
1613 		sh_flags = osp->os_shdr->sh_flags;
1614 		if ((sectname = libld_malloc(strlen(rel_prefix) +
1615 		    strlen(osp->os_name) + 1)) == 0)
1616 			return (S_ERROR);
1617 		(void) strcpy(sectname, rel_prefix);
1618 		(void) strcat(sectname, osp->os_name);
1619 	} else if (ofl->ofl_flags1 & FLG_OF1_RELCNT) {
1620 		size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
1621 		sh_flags = SHF_ALLOC;
1622 		sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
1623 	} else {
1624 		size = ofl->ofl_relocrelsz;
1625 		sh_flags = SHF_ALLOC;
1626 		sectname = (char *)rel_prefix;
1627 	}
1628 
1629 	/*
1630 	 * Keep track of total size of 'output relocations' (to be stored
1631 	 * in .dynamic)
1632 	 */
1633 	/* LINTED */
1634 	ofl->ofl_relocsz += (Xword)size;
1635 
1636 	/*
1637 	 * Allocate and initialize the Elf_Data structure.
1638 	 */
1639 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1640 		return (S_ERROR);
1641 	data->d_type = M_REL_ELF_TYPE;
1642 	data->d_size = size;
1643 	data->d_align = M_WORD_ALIGN;
1644 	data->d_version = ofl->ofl_libver;
1645 
1646 	/*
1647 	 * Allocate and initialize the Shdr structure.
1648 	 */
1649 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1650 		return (S_ERROR);
1651 	shdr->sh_type = M_REL_SHT_TYPE;
1652 	shdr->sh_size = (Xword)size;
1653 	shdr->sh_addralign = M_WORD_ALIGN;
1654 	shdr->sh_entsize = relsize;
1655 
1656 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) &&
1657 	    !(ofl->ofl_flags & FLG_OF_RELOBJ) &&
1658 	    (sh_flags & SHF_ALLOC))
1659 		shdr->sh_flags = SHF_ALLOC;
1660 
1661 	if (osp) {
1662 		/*
1663 		 * The sh_info field of the SHT_REL* sections points to the
1664 		 * section the relocations are to be applied to.
1665 		 */
1666 		shdr->sh_flags |= SHF_INFO_LINK;
1667 	}
1668 
1669 
1670 	/*
1671 	 * Allocate and initialize the Is_desc structure.
1672 	 */
1673 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1674 		return (S_ERROR);
1675 	isec->is_shdr = shdr;
1676 	isec->is_indata = data;
1677 	isec->is_name = sectname;
1678 
1679 
1680 	/*
1681 	 * Associate this relocation section to the section its going to
1682 	 * relocate.
1683 	 */
1684 	if ((rosp = place_section(ofl, isec, M_ID_REL, 0)) ==
1685 	    (Os_desc *)S_ERROR)
1686 		return (S_ERROR);
1687 
1688 	if (osp) {
1689 		Listnode *	lnp;
1690 		Is_desc *	risp;
1691 		/*
1692 		 * We associate the input relocation sections - with
1693 		 * the newly created output relocation section.
1694 		 *
1695 		 * This is used primarily so that we can update
1696 		 * SHT_GROUP[sect_no] entries to point to the
1697 		 * created output relocation sections.
1698 		 */
1699 		for (LIST_TRAVERSE(&(osp->os_relisdescs), lnp, risp)) {
1700 			risp->is_osdesc = rosp;
1701 			/*
1702 			 * If the input relocation section had
1703 			 * the SHF_GROUP flag set - propogate it to
1704 			 * the output relocation section.
1705 			 */
1706 			if (risp->is_shdr->sh_flags & SHF_GROUP) {
1707 				rosp->os_shdr->sh_flags |= SHF_GROUP;
1708 				break;
1709 			}
1710 		}
1711 		osp->os_relosdesc = rosp;
1712 	} else
1713 		ofl->ofl_osrel = rosp;
1714 
1715 	/*
1716 	 * If this is the first relocation section we've encountered save it
1717 	 * so that the .dynamic entry can be initialized accordingly.
1718 	 */
1719 	if (ofl->ofl_osrelhead == (Os_desc *)0)
1720 		ofl->ofl_osrelhead = rosp;
1721 
1722 	return (1);
1723 }
1724 
1725 /*
1726  * Generate version needed section.
1727  */
1728 uintptr_t
1729 make_verneed(Ofl_desc *ofl)
1730 {
1731 	Shdr		*shdr;
1732 	Elf_Data	*data;
1733 	Is_desc		*isec;
1734 	size_t		size = ofl->ofl_verneedsz;
1735 
1736 	/*
1737 	 * Allocate and initialize the Elf_Data structure.
1738 	 */
1739 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1740 		return (S_ERROR);
1741 	data->d_type = ELF_T_BYTE;
1742 	data->d_size = size;
1743 	data->d_align = M_WORD_ALIGN;
1744 	data->d_version = ofl->ofl_libver;
1745 
1746 	/*
1747 	 * Allocate and initialize the Shdr structure.
1748 	 */
1749 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1750 		return (S_ERROR);
1751 	shdr->sh_type = (Word)SHT_SUNW_verneed;
1752 	shdr->sh_flags = SHF_ALLOC;
1753 	shdr->sh_size = (Xword)size;
1754 	shdr->sh_addralign = M_WORD_ALIGN;
1755 
1756 	/*
1757 	 * Allocate and initialize the Is_desc structure.
1758 	 */
1759 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1760 		return (S_ERROR);
1761 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWVERSION);
1762 	isec->is_shdr = shdr;
1763 	isec->is_indata = data;
1764 
1765 	ofl->ofl_osverneed = place_section(ofl, isec, M_ID_VERSION, 0);
1766 	return ((uintptr_t)ofl->ofl_osverneed);
1767 }
1768 
1769 /*
1770  * Generate a version definition section.
1771  *
1772  *  o	the SHT_SUNW_verdef section defines the versions that exist within this
1773  *	image.
1774  */
1775 uintptr_t
1776 make_verdef(Ofl_desc *ofl)
1777 {
1778 	Shdr		*shdr;
1779 	Elf_Data	*data;
1780 	Is_desc		*isec;
1781 	Ver_desc	*vdp;
1782 	size_t		size;
1783 
1784 	/*
1785 	 * Reserve a string table entry for the base version dependency (other
1786 	 * dependencies have symbol representations, which will already be
1787 	 * accounted for during symbol processing).
1788 	 */
1789 	vdp = (Ver_desc *)ofl->ofl_verdesc.head->data;
1790 	size = strlen(vdp->vd_name) + 1;
1791 
1792 	if (ofl->ofl_flags & FLG_OF_DYNAMIC) {
1793 		if (st_insert(ofl->ofl_dynstrtab, vdp->vd_name) == -1)
1794 			return (S_ERROR);
1795 	} else {
1796 		if (st_insert(ofl->ofl_strtab, vdp->vd_name) == -1)
1797 			return (S_ERROR);
1798 	}
1799 
1800 	/*
1801 	 * During version processing we calculated the total number of entries.
1802 	 * Allocate and initialize the Elf_Data structure.
1803 	 */
1804 	size = ofl->ofl_verdefsz;
1805 
1806 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1807 		return (S_ERROR);
1808 	data->d_type = ELF_T_BYTE;
1809 	data->d_size = size;
1810 	data->d_align = M_WORD_ALIGN;
1811 	data->d_version = ofl->ofl_libver;
1812 
1813 	/*
1814 	 * Allocate and initialize the Shdr structure.
1815 	 */
1816 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1817 		return (S_ERROR);
1818 	shdr->sh_type = (Word)SHT_SUNW_verdef;
1819 	shdr->sh_flags = SHF_ALLOC;
1820 	shdr->sh_size = (Xword)size;
1821 	shdr->sh_addralign = M_WORD_ALIGN;
1822 
1823 	/*
1824 	 * Allocate and initialize the Is_desc structure.
1825 	 */
1826 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1827 		return (S_ERROR);
1828 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWVERSION);
1829 	isec->is_shdr = shdr;
1830 	isec->is_indata = data;
1831 
1832 	ofl->ofl_osverdef = place_section(ofl, isec, M_ID_VERSION, 0);
1833 	return ((uintptr_t)ofl->ofl_osverdef);
1834 }
1835 
1836 /*
1837  * Common function used to build both the SHT_SUNW_versym
1838  * section and the SHT_SUNW_syminfo section.  Each of these sections
1839  * provides additional symbol information.
1840  */
1841 Os_desc *
1842 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word entsize,
1843     Word stype, int ident)
1844 {
1845 	Shdr		*shdr;
1846 	Elf_Data	*data;
1847 	Is_desc		*isec;
1848 
1849 	/*
1850 	 * Allocate and initialize the Elf_Data structures for the symbol index
1851 	 * array.
1852 	 */
1853 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1854 		return ((Os_desc *)S_ERROR);
1855 	data->d_type = ELF_T_BYTE;
1856 	data->d_align = M_WORD_ALIGN;
1857 	data->d_version = ofl->ofl_libver;
1858 
1859 	/*
1860 	 * Allocate and initialize the Shdr structure.
1861 	 */
1862 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1863 		return ((Os_desc *)S_ERROR);
1864 	shdr->sh_type = (Word)stype;
1865 	shdr->sh_flags = SHF_ALLOC;
1866 	shdr->sh_addralign = M_WORD_ALIGN;
1867 	shdr->sh_entsize = entsize;
1868 
1869 	if (stype == SHT_SUNW_syminfo) {
1870 		/*
1871 		 * The sh_info field of the SHT_*_syminfo section points
1872 		 * to the header index of the associated .dynamic section.
1873 		 */
1874 		shdr->sh_flags |= SHF_INFO_LINK;
1875 	}
1876 
1877 	/*
1878 	 * Allocate and initialize the Is_desc structure.
1879 	 */
1880 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1881 		return ((Os_desc *)S_ERROR);
1882 	isec->is_name = sectname;
1883 	isec->is_shdr = shdr;
1884 	isec->is_indata = data;
1885 
1886 	return (place_section(ofl, isec, ident, 0));
1887 }
1888 
1889 /*
1890  * Build a .sunwbss section for allocation of tentative definitions.
1891  */
1892 uintptr_t
1893 make_sunwbss(Ofl_desc *ofl, size_t size, Xword align)
1894 {
1895 	Shdr		*shdr;
1896 	Elf_Data	*data;
1897 	Is_desc		*isec;
1898 
1899 	/*
1900 	 * Allocate and initialize the Elf_Data structure.
1901 	 */
1902 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1903 		return (S_ERROR);
1904 	data->d_type = ELF_T_BYTE;
1905 	data->d_size = size;
1906 	data->d_align = align;
1907 	data->d_version = ofl->ofl_libver;
1908 
1909 	/*
1910 	 * Allocate and initialize the Shdr structure.
1911 	 */
1912 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1913 		return (S_ERROR);
1914 	shdr->sh_type = SHT_NOBITS;
1915 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
1916 	shdr->sh_size = (Xword)size;
1917 	shdr->sh_addralign = align;
1918 
1919 	/*
1920 	 * Allocate and initialize the Is_desc structure.
1921 	 */
1922 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1923 		return (S_ERROR);
1924 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWBSS);
1925 	isec->is_shdr = shdr;
1926 	isec->is_indata = data;
1927 
1928 	/*
1929 	 * Retain this .sunwbss input section as this will be where global
1930 	 * symbol references are added.
1931 	 */
1932 	ofl->ofl_issunwbss = isec;
1933 	if (place_section(ofl, isec, 0, 0) == (Os_desc *)S_ERROR)
1934 		return (S_ERROR);
1935 
1936 	return (1);
1937 }
1938 
1939 /*
1940  * This routine is called when -z nopartial is in effect.
1941  */
1942 uintptr_t
1943 make_sunwdata(Ofl_desc *ofl, size_t size, Xword align)
1944 {
1945 	Shdr		*shdr;
1946 	Elf_Data	*data;
1947 	Is_desc		*isec;
1948 	Os_desc		*osp;
1949 
1950 	/*
1951 	 * Allocate and initialize the Elf_Data structure.
1952 	 */
1953 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1954 		return (S_ERROR);
1955 	data->d_type = ELF_T_BYTE;
1956 	data->d_size = size;
1957 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
1958 		return (S_ERROR);
1959 	data->d_align = (size_t)M_WORD_ALIGN;
1960 	data->d_version = ofl->ofl_libver;
1961 
1962 	/*
1963 	 * Allocate and initialize the Shdr structure.
1964 	 */
1965 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1966 		return (S_ERROR);
1967 	shdr->sh_type = SHT_PROGBITS;
1968 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
1969 	shdr->sh_size = (Xword)size;
1970 	if (align == 0)
1971 		shdr->sh_addralign = M_WORD_ALIGN;
1972 	else
1973 		shdr->sh_addralign = align;
1974 
1975 	/*
1976 	 * Allocate and initialize the Is_desc structure.
1977 	 */
1978 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1979 		return (S_ERROR);
1980 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWDATA1);
1981 	isec->is_shdr = shdr;
1982 	isec->is_indata = data;
1983 
1984 	/*
1985 	 * Retain this .sunwdata1 input section as this will
1986 	 * be where global
1987 	 * symbol references are added.
1988 	 */
1989 	ofl->ofl_issunwdata1 = isec;
1990 	if ((osp = place_section(ofl, isec, M_ID_DATA, 0)) ==
1991 	    (Os_desc *)S_ERROR)
1992 		return (S_ERROR);
1993 
1994 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
1995 		ofl->ofl_dynshdrcnt++;
1996 		osp->os_flags |= FLG_OS_OUTREL;
1997 	}
1998 	return (1);
1999 }
2000 
2001 /*
2002  * Make .sunwmove section
2003  */
2004 uintptr_t
2005 make_sunwmove(Ofl_desc *ofl, int mv_nums)
2006 {
2007 	Shdr		*shdr;
2008 	Elf_Data	*data;
2009 	Is_desc		*isec;
2010 	size_t		size;
2011 	Listnode	*lnp1;
2012 	Psym_info	*psym;
2013 	int 		cnt = 1;
2014 
2015 	/*
2016 	 * Generate the move input sections and output sections
2017 	 */
2018 	size = mv_nums * sizeof (Move);
2019 
2020 	/*
2021 	 * Allocate and initialize the Elf_Data structure.
2022 	 */
2023 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
2024 		return (S_ERROR);
2025 	data->d_type = ELF_T_BYTE;
2026 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2027 		return (S_ERROR);
2028 	data->d_size = size;
2029 	data->d_align = sizeof (Lword);
2030 	data->d_version = ofl->ofl_libver;
2031 
2032 	/*
2033 	 * Allocate and initialize the Shdr structure.
2034 	 */
2035 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
2036 		return (S_ERROR);
2037 	shdr->sh_link = 0;
2038 	shdr->sh_info = 0;
2039 	shdr->sh_type = SHT_SUNW_move;
2040 	shdr->sh_size = (Xword)size;
2041 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
2042 	shdr->sh_addralign = sizeof (Lword);
2043 	shdr->sh_entsize = sizeof (Move);
2044 
2045 	/*
2046 	 * Allocate and initialize the Is_desc structure.
2047 	 */
2048 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
2049 		return (S_ERROR);
2050 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWMOVE);
2051 	isec->is_shdr = shdr;
2052 	isec->is_indata = data;
2053 	isec->is_file = 0;
2054 
2055 	/*
2056 	 * Copy move entries
2057 	 */
2058 	for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) {
2059 		Listnode *	lnp2;
2060 		Mv_itm *	mvitm;
2061 
2062 		if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN)
2063 			continue;
2064 		for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvitm)) {
2065 			if ((mvitm->mv_flag & FLG_MV_OUTSECT) == 0)
2066 				continue;
2067 			mvitm->mv_oidx = cnt;
2068 			cnt++;
2069 		}
2070 	}
2071 	if ((ofl->ofl_osmove = place_section(ofl, isec, 0, 0)) ==
2072 	    (Os_desc *)S_ERROR)
2073 		return (S_ERROR);
2074 
2075 	return (1);
2076 }
2077 
2078 
2079 /*
2080  * The following sections are built after all input file processing and symbol
2081  * validation has been carried out.  The order is important (because the
2082  * addition of a section adds a new symbol there is a chicken and egg problem
2083  * of maintaining the appropriate counts).  By maintaining a known order the
2084  * individual routines can compensate for later, known, additions.
2085  */
2086 uintptr_t
2087 make_sections(Ofl_desc *ofl)
2088 {
2089 	Word		flags = ofl->ofl_flags;
2090 	Listnode	*lnp1;
2091 	Sg_desc		*sgp;
2092 
2093 	/*
2094 	 * Generate any special sections.
2095 	 */
2096 	if (flags & FLG_OF_ADDVERS)
2097 		if (make_comment(ofl) == S_ERROR)
2098 			return (S_ERROR);
2099 
2100 	if (make_interp(ofl) == S_ERROR)
2101 		return (S_ERROR);
2102 
2103 	if (make_cap(ofl) == S_ERROR)
2104 		return (S_ERROR);
2105 
2106 	if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
2107 	    &ofl->ofl_initarray) == S_ERROR)
2108 		return (S_ERROR);
2109 
2110 	if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
2111 	    &ofl->ofl_finiarray) == S_ERROR)
2112 		return (S_ERROR);
2113 
2114 	if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
2115 	    &ofl->ofl_preiarray) == S_ERROR)
2116 		return (S_ERROR);
2117 
2118 	/*
2119 	 * Make the .plt section.  This occurs after any other relocation
2120 	 * sections are generated (see reloc_init()) to ensure that the
2121 	 * associated relocation section is after all the other relocation
2122 	 * sections.
2123 	 */
2124 	if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
2125 		if (make_plt(ofl) == S_ERROR)
2126 			return (S_ERROR);
2127 
2128 	if (ofl->ofl_flags1 & FLG_OF1_IGNPRC) {
2129 		if (ignore_section_processing(ofl) == S_ERROR)
2130 			return (S_ERROR);
2131 	}
2132 
2133 	/*
2134 	 * Add any necessary versioning information.
2135 	 */
2136 	if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED) {
2137 		if (make_verneed(ofl) == S_ERROR)
2138 			return (S_ERROR);
2139 	}
2140 	if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == FLG_OF_VERDEF) {
2141 		if (make_verdef(ofl) == S_ERROR)
2142 			return (S_ERROR);
2143 		if ((ofl->ofl_osversym = make_sym_sec(ofl,
2144 		    MSG_ORIG(MSG_SCN_SUNWVERSYM), sizeof (Versym),
2145 		    SHT_SUNW_versym, M_ID_VERSION)) == (Os_desc*)S_ERROR)
2146 			return (S_ERROR);
2147 	}
2148 
2149 	/*
2150 	 * Create a syminfo section is necessary.
2151 	 */
2152 	if (ofl->ofl_flags & FLG_OF_SYMINFO) {
2153 		if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
2154 		    MSG_ORIG(MSG_SCN_SUNWSYMINFO), sizeof (Syminfo),
2155 		    SHT_SUNW_syminfo, M_ID_SYMINFO)) == (Os_desc *)S_ERROR)
2156 			return (S_ERROR);
2157 	}
2158 
2159 	if (ofl->ofl_flags1 & FLG_OF1_RELCNT) {
2160 		/*
2161 		 * If -zcombreloc is enabled then all relocations (except for
2162 		 * the PLT's) are coalesced into a single relocation section.
2163 		 */
2164 		if (ofl->ofl_reloccnt) {
2165 			if (make_reloc(ofl, NULL) == S_ERROR)
2166 				return (S_ERROR);
2167 		}
2168 	} else {
2169 		size_t	reloc_size = 0;
2170 
2171 		/*
2172 		 * Create the required output relocation sections.
2173 		 */
2174 		for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2175 			Os_desc		*osp;
2176 			Listnode	*lnp2;
2177 
2178 			for (LIST_TRAVERSE(&(sgp->sg_osdescs), lnp2, osp)) {
2179 				if (osp->os_szoutrels &&
2180 				    (osp != ofl->ofl_osplt)) {
2181 					if (make_reloc(ofl, osp) == S_ERROR)
2182 						return (S_ERROR);
2183 					reloc_size += reloc_size;
2184 				}
2185 			}
2186 		}
2187 
2188 		/*
2189 		 * If we're not building a combined relocation section, then
2190 		 * build a .rel[a] section as required.
2191 		 */
2192 		if (ofl->ofl_relocrelsz) {
2193 			if (make_reloc(ofl, NULL) == S_ERROR)
2194 				return (S_ERROR);
2195 		}
2196 	}
2197 
2198 	/*
2199 	 * The PLT relocations are always in their own section, and we try to
2200 	 * keep them at the end of the PLT table.  We do this to keep the hot
2201 	 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
2202 	 */
2203 	if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
2204 		if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
2205 			return (S_ERROR);
2206 	}
2207 
2208 	/*
2209 	 * Finally build the symbol and section header sections.
2210 	 */
2211 	if (flags & FLG_OF_DYNAMIC) {
2212 		if (make_dynamic(ofl) == S_ERROR)
2213 			return (S_ERROR);
2214 		if (make_dynstr(ofl) == S_ERROR)
2215 			return (S_ERROR);
2216 		/*
2217 		 * There is no use for .hash and .dynsym sections in a
2218 		 * relocatable object.
2219 		 */
2220 		if (!(flags & FLG_OF_RELOBJ)) {
2221 			if (make_hash(ofl) == S_ERROR)
2222 				return (S_ERROR);
2223 			if (make_dynsym(ofl) == S_ERROR)
2224 				return (S_ERROR);
2225 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
2226 			if (make_amd64_unwindhdr(ofl) == S_ERROR)
2227 				return (S_ERROR);
2228 #endif
2229 		}
2230 	}
2231 
2232 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
2233 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
2234 		/*
2235 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2236 		 * for the dynsym.  If so - do it now.
2237 		 */
2238 		if (ofl->ofl_osdynsym &&
2239 		    ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
2240 			if (make_dynsym_shndx(ofl) == S_ERROR)
2241 				return (S_ERROR);
2242 		}
2243 
2244 		if (make_strtab(ofl) == S_ERROR)
2245 			return (S_ERROR);
2246 		if (make_symtab(ofl) == S_ERROR)
2247 			return (S_ERROR);
2248 	} else {
2249 		/*
2250 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2251 		 * for the dynsym.  If so - do it now.
2252 		 */
2253 		if (ofl->ofl_osdynsym &&
2254 		    ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
2255 			if (make_dynsym_shndx(ofl) == S_ERROR)
2256 				return (S_ERROR);
2257 		}
2258 	}
2259 
2260 	if (make_shstrtab(ofl) == S_ERROR)
2261 		return (S_ERROR);
2262 
2263 	/*
2264 	 * Now that we've created all of our sections adjust the size
2265 	 * of SHT_SUNW_versym & SHT_SUNW_syminfo which are dependent on
2266 	 * the symbol table sizes.
2267 	 */
2268 	if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
2269 		Shdr *		shdr;
2270 		Is_desc *	isec;
2271 		Elf_Data *	data;
2272 		size_t		size;
2273 		ulong_t		cnt;
2274 
2275 		if ((flags & FLG_OF_RELOBJ) || (flags & FLG_OF_STATIC))
2276 			isec = (Is_desc *)ofl->ofl_ossymtab->
2277 				os_isdescs.head->data;
2278 		else
2279 			isec = (Is_desc *)ofl->ofl_osdynsym->
2280 				os_isdescs.head->data;
2281 		cnt = isec->is_shdr->sh_size / isec->is_shdr->sh_entsize;
2282 
2283 		if (ofl->ofl_osversym) {
2284 			isec = (Is_desc *)ofl->ofl_osversym->os_isdescs.
2285 				head->data;
2286 			data = isec->is_indata;
2287 			shdr = ofl->ofl_osversym->os_shdr;
2288 			size = cnt * shdr->sh_entsize;
2289 			shdr->sh_size = (Xword)size;
2290 			data->d_size = size;
2291 		}
2292 		if (ofl->ofl_ossyminfo) {
2293 			isec = (Is_desc *)ofl->ofl_ossyminfo->os_isdescs.
2294 				head->data;
2295 			data = isec->is_indata;
2296 			shdr = ofl->ofl_ossyminfo->os_shdr;
2297 			size = cnt * shdr->sh_entsize;
2298 			shdr->sh_size = (Xword)size;
2299 			data->d_size = size;
2300 		}
2301 	}
2302 
2303 	return (1);
2304 }
2305