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