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