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