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