xref: /illumos-gate/usr/src/cmd/sgs/libld/common/sections.c (revision 60425338a8e9a5ded7e559e227eedd42d30c8967)
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 	data->d_size = size;
1485 	shdr->sh_size = (Xword)size;
1486 
1487 	return (1);
1488 }
1489 
1490 /*
1491  * Build a string section for the standard symbol table.
1492  */
1493 static uintptr_t
1494 make_strtab(Ofl_desc *ofl)
1495 {
1496 	Shdr		*shdr;
1497 	Elf_Data	*data;
1498 	Is_desc		*isec;
1499 	size_t		size;
1500 
1501 	/*
1502 	 * This string table consists of all the global and local symbols.
1503 	 * Account for null bytes at end of the file name and the beginning
1504 	 * of section.
1505 	 */
1506 	if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
1507 		return (S_ERROR);
1508 
1509 	size = st_getstrtab_sz(ofl->ofl_strtab);
1510 	assert(size > 0);
1511 
1512 	/*
1513 	 * Allocate the Elf_Data structure.
1514 	 */
1515 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1516 		return (S_ERROR);
1517 	data->d_size = size;
1518 	data->d_type = ELF_T_BYTE;
1519 	data->d_align = 1;
1520 	data->d_version = ofl->ofl_dehdr->e_version;
1521 
1522 	/*
1523 	 * Allocate the Shdr structure.
1524 	 */
1525 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1526 		return (S_ERROR);
1527 	shdr->sh_size = (Xword)size;
1528 	shdr->sh_addralign = 1;
1529 	shdr->sh_type = SHT_STRTAB;
1530 	shdr->sh_flags |= SHF_STRINGS;
1531 
1532 	/*
1533 	 * Allocate and initialize the Is_desc structure.
1534 	 */
1535 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1536 		return (S_ERROR);
1537 	isec->is_name = MSG_ORIG(MSG_SCN_STRTAB);
1538 	isec->is_shdr = shdr;
1539 	isec->is_indata = data;
1540 
1541 	ofl->ofl_osstrtab = ld_place_section(ofl, isec, M_ID_STRTAB, 0);
1542 	return ((uintptr_t)ofl->ofl_osstrtab);
1543 }
1544 
1545 /*
1546  * Build a string table for the dynamic symbol table.
1547  */
1548 static uintptr_t
1549 make_dynstr(Ofl_desc *ofl)
1550 {
1551 	Shdr		*shdr;
1552 	Elf_Data	*data;
1553 	Is_desc		*isec;
1554 	size_t		size;
1555 
1556 	/*
1557 	 * Account for any local, named register symbols.  These locals are
1558 	 * required for reference from DT_REGISTER .dynamic entries.
1559 	 */
1560 	if (ofl->ofl_regsyms) {
1561 		int	ndx;
1562 
1563 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1564 			Sym_desc *	sdp;
1565 
1566 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1567 				continue;
1568 
1569 			if (((sdp->sd_flags1 & FLG_SY1_LOCL) == 0) &&
1570 			    (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
1571 				continue;
1572 
1573 			if (sdp->sd_sym->st_name == 0)
1574 				continue;
1575 
1576 			if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
1577 				return (S_ERROR);
1578 		}
1579 	}
1580 
1581 	/*
1582 	 * Reserve entries for any per-symbol auxiliary/filter strings.
1583 	 */
1584 	if (ofl->ofl_dtsfltrs) {
1585 		Dfltr_desc *	dftp;
1586 		Aliste		off;
1587 
1588 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp))
1589 			if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
1590 				return (S_ERROR);
1591 	}
1592 
1593 	size = st_getstrtab_sz(ofl->ofl_dynstrtab);
1594 	assert(size > 0);
1595 
1596 	/*
1597 	 * Allocate the Elf_Data structure.
1598 	 */
1599 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1600 		return (S_ERROR);
1601 	data->d_type = ELF_T_BYTE;
1602 	data->d_size = size;
1603 	data->d_align = 1;
1604 	data->d_version = ofl->ofl_dehdr->e_version;
1605 
1606 	/*
1607 	 * Allocate the Shdr structure.
1608 	 */
1609 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1610 		return (S_ERROR);
1611 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
1612 		shdr->sh_flags = SHF_ALLOC;
1613 
1614 	shdr->sh_type = SHT_STRTAB;
1615 	shdr->sh_flags |= SHF_STRINGS;
1616 	shdr->sh_size = (Xword)size;
1617 	shdr->sh_addralign = 1;
1618 
1619 	/*
1620 	 * Allocate and initialize the Is_desc structure.
1621 	 */
1622 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1623 		return (S_ERROR);
1624 	isec->is_name = MSG_ORIG(MSG_SCN_DYNSTR);
1625 	isec->is_shdr = shdr;
1626 	isec->is_indata = data;
1627 
1628 	ofl->ofl_osdynstr = ld_place_section(ofl, isec, M_ID_DYNSTR, 0);
1629 	return ((uintptr_t)ofl->ofl_osdynstr);
1630 }
1631 
1632 /*
1633  * Generate an output relocation section which will contain the relocation
1634  * information to be applied to the `osp' section.
1635  *
1636  * If (osp == NULL) then we are creating the coalesced relocation section
1637  * for an executable and/or a shared object.
1638  */
1639 static uintptr_t
1640 make_reloc(Ofl_desc *ofl, Os_desc *osp)
1641 {
1642 	Shdr		*shdr;
1643 	Elf_Data	*data;
1644 	Is_desc		*isec;
1645 	size_t		size;
1646 	Xword		sh_flags;
1647 	char 		*sectname;
1648 	Os_desc		*rosp;
1649 	Word		relsize;
1650 	const char	*rel_prefix;
1651 
1652 	/* LINTED */
1653 	if (M_REL_SHT_TYPE == SHT_REL) {
1654 		/* REL */
1655 		relsize = sizeof (Rel);
1656 		rel_prefix = MSG_ORIG(MSG_SCN_REL);
1657 	} else {
1658 		/* RELA */
1659 		relsize = sizeof (Rela);
1660 		rel_prefix = MSG_ORIG(MSG_SCN_RELA);
1661 	}
1662 
1663 	if (osp) {
1664 		size = osp->os_szoutrels;
1665 		sh_flags = osp->os_shdr->sh_flags;
1666 		if ((sectname = libld_malloc(strlen(rel_prefix) +
1667 		    strlen(osp->os_name) + 1)) == 0)
1668 			return (S_ERROR);
1669 		(void) strcpy(sectname, rel_prefix);
1670 		(void) strcat(sectname, osp->os_name);
1671 	} else if (ofl->ofl_flags1 & FLG_OF1_RELCNT) {
1672 		size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
1673 		sh_flags = SHF_ALLOC;
1674 		sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
1675 	} else {
1676 		size = ofl->ofl_relocrelsz;
1677 		sh_flags = SHF_ALLOC;
1678 		sectname = (char *)rel_prefix;
1679 	}
1680 
1681 	/*
1682 	 * Keep track of total size of 'output relocations' (to be stored
1683 	 * in .dynamic)
1684 	 */
1685 	/* LINTED */
1686 	ofl->ofl_relocsz += (Xword)size;
1687 
1688 	/*
1689 	 * Allocate and initialize the Elf_Data structure.
1690 	 */
1691 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1692 		return (S_ERROR);
1693 	data->d_type = M_REL_ELF_TYPE;
1694 	data->d_size = size;
1695 	data->d_align = M_WORD_ALIGN;
1696 	data->d_version = ofl->ofl_dehdr->e_version;
1697 
1698 	/*
1699 	 * Allocate and initialize the Shdr structure.
1700 	 */
1701 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1702 		return (S_ERROR);
1703 	shdr->sh_type = M_REL_SHT_TYPE;
1704 	shdr->sh_size = (Xword)size;
1705 	shdr->sh_addralign = M_WORD_ALIGN;
1706 	shdr->sh_entsize = relsize;
1707 
1708 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) &&
1709 	    !(ofl->ofl_flags & FLG_OF_RELOBJ) &&
1710 	    (sh_flags & SHF_ALLOC))
1711 		shdr->sh_flags = SHF_ALLOC;
1712 
1713 	if (osp) {
1714 		/*
1715 		 * The sh_info field of the SHT_REL* sections points to the
1716 		 * section the relocations are to be applied to.
1717 		 */
1718 		shdr->sh_flags |= SHF_INFO_LINK;
1719 	}
1720 
1721 
1722 	/*
1723 	 * Allocate and initialize the Is_desc structure.
1724 	 */
1725 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1726 		return (S_ERROR);
1727 	isec->is_shdr = shdr;
1728 	isec->is_indata = data;
1729 	isec->is_name = sectname;
1730 
1731 
1732 	/*
1733 	 * Associate this relocation section to the section its going to
1734 	 * relocate.
1735 	 */
1736 	if ((rosp = ld_place_section(ofl, isec, M_ID_REL, 0)) ==
1737 	    (Os_desc *)S_ERROR)
1738 		return (S_ERROR);
1739 
1740 	if (osp) {
1741 		Listnode	*lnp;
1742 		Is_desc		*risp;
1743 
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 			/*
1756 			 * If the input relocation section had the SHF_GROUP
1757 			 * flag set - propogate it to the output relocation
1758 			 * section.
1759 			 */
1760 			if (risp->is_shdr->sh_flags & SHF_GROUP) {
1761 				rosp->os_shdr->sh_flags |= SHF_GROUP;
1762 				break;
1763 			}
1764 		}
1765 		osp->os_relosdesc = rosp;
1766 	} else
1767 		ofl->ofl_osrel = rosp;
1768 
1769 	/*
1770 	 * If this is the first relocation section we've encountered save it
1771 	 * so that the .dynamic entry can be initialized accordingly.
1772 	 */
1773 	if (ofl->ofl_osrelhead == (Os_desc *)0)
1774 		ofl->ofl_osrelhead = rosp;
1775 
1776 	return (1);
1777 }
1778 
1779 /*
1780  * Generate version needed section.
1781  */
1782 static uintptr_t
1783 make_verneed(Ofl_desc *ofl)
1784 {
1785 	Shdr		*shdr;
1786 	Elf_Data	*data;
1787 	Is_desc		*isec;
1788 	size_t		size = ofl->ofl_verneedsz;
1789 
1790 	/*
1791 	 * Allocate and initialize the Elf_Data structure.
1792 	 */
1793 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1794 		return (S_ERROR);
1795 	data->d_type = ELF_T_BYTE;
1796 	data->d_size = size;
1797 	data->d_align = M_WORD_ALIGN;
1798 	data->d_version = ofl->ofl_dehdr->e_version;
1799 
1800 	/*
1801 	 * Allocate and initialize the Shdr structure.
1802 	 */
1803 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1804 		return (S_ERROR);
1805 	shdr->sh_type = (Word)SHT_SUNW_verneed;
1806 	shdr->sh_flags = SHF_ALLOC;
1807 	shdr->sh_size = (Xword)size;
1808 	shdr->sh_addralign = M_WORD_ALIGN;
1809 
1810 	/*
1811 	 * Allocate and initialize the Is_desc structure.
1812 	 */
1813 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1814 		return (S_ERROR);
1815 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWVERSION);
1816 	isec->is_shdr = shdr;
1817 	isec->is_indata = data;
1818 
1819 	ofl->ofl_osverneed = ld_place_section(ofl, isec, M_ID_VERSION, 0);
1820 	return ((uintptr_t)ofl->ofl_osverneed);
1821 }
1822 
1823 /*
1824  * Generate a version definition section.
1825  *
1826  *  o	the SHT_SUNW_verdef section defines the versions that exist within this
1827  *	image.
1828  */
1829 static uintptr_t
1830 make_verdef(Ofl_desc *ofl)
1831 {
1832 	Shdr		*shdr;
1833 	Elf_Data	*data;
1834 	Is_desc		*isec;
1835 	Ver_desc	*vdp;
1836 	size_t		size;
1837 
1838 	/*
1839 	 * Reserve a string table entry for the base version dependency (other
1840 	 * dependencies have symbol representations, which will already be
1841 	 * accounted for during symbol processing).
1842 	 */
1843 	vdp = (Ver_desc *)ofl->ofl_verdesc.head->data;
1844 	size = strlen(vdp->vd_name) + 1;
1845 
1846 	if (ofl->ofl_flags & FLG_OF_DYNAMIC) {
1847 		if (st_insert(ofl->ofl_dynstrtab, vdp->vd_name) == -1)
1848 			return (S_ERROR);
1849 	} else {
1850 		if (st_insert(ofl->ofl_strtab, vdp->vd_name) == -1)
1851 			return (S_ERROR);
1852 	}
1853 
1854 	/*
1855 	 * During version processing we calculated the total number of entries.
1856 	 * Allocate and initialize the Elf_Data structure.
1857 	 */
1858 	size = ofl->ofl_verdefsz;
1859 
1860 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1861 		return (S_ERROR);
1862 	data->d_type = ELF_T_BYTE;
1863 	data->d_size = size;
1864 	data->d_align = M_WORD_ALIGN;
1865 	data->d_version = ofl->ofl_dehdr->e_version;
1866 
1867 	/*
1868 	 * Allocate and initialize the Shdr structure.
1869 	 */
1870 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1871 		return (S_ERROR);
1872 	shdr->sh_type = (Word)SHT_SUNW_verdef;
1873 	shdr->sh_flags = SHF_ALLOC;
1874 	shdr->sh_size = (Xword)size;
1875 	shdr->sh_addralign = M_WORD_ALIGN;
1876 
1877 	/*
1878 	 * Allocate and initialize the Is_desc structure.
1879 	 */
1880 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1881 		return (S_ERROR);
1882 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWVERSION);
1883 	isec->is_shdr = shdr;
1884 	isec->is_indata = data;
1885 
1886 	ofl->ofl_osverdef = ld_place_section(ofl, isec, M_ID_VERSION, 0);
1887 	return ((uintptr_t)ofl->ofl_osverdef);
1888 }
1889 
1890 /*
1891  * Common function used to build both the SHT_SUNW_versym
1892  * section and the SHT_SUNW_syminfo section.  Each of these sections
1893  * provides additional symbol information.
1894  */
1895 static Os_desc *
1896 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word entsize,
1897     Word stype, int ident)
1898 {
1899 	Shdr		*shdr;
1900 	Elf_Data	*data;
1901 	Is_desc		*isec;
1902 
1903 	/*
1904 	 * Allocate and initialize the Elf_Data structures for the symbol index
1905 	 * array.
1906 	 */
1907 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1908 		return ((Os_desc *)S_ERROR);
1909 	data->d_type = ELF_T_BYTE;
1910 	data->d_align = M_WORD_ALIGN;
1911 	data->d_version = ofl->ofl_dehdr->e_version;
1912 
1913 	/*
1914 	 * Allocate and initialize the Shdr structure.
1915 	 */
1916 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1917 		return ((Os_desc *)S_ERROR);
1918 	shdr->sh_type = (Word)stype;
1919 	shdr->sh_flags = SHF_ALLOC;
1920 	shdr->sh_addralign = M_WORD_ALIGN;
1921 	shdr->sh_entsize = entsize;
1922 
1923 	if (stype == SHT_SUNW_syminfo) {
1924 		/*
1925 		 * The sh_info field of the SHT_*_syminfo section points
1926 		 * to the header index of the associated .dynamic section.
1927 		 */
1928 		shdr->sh_flags |= SHF_INFO_LINK;
1929 	}
1930 
1931 	/*
1932 	 * Allocate and initialize the Is_desc structure.
1933 	 */
1934 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1935 		return ((Os_desc *)S_ERROR);
1936 	isec->is_name = sectname;
1937 	isec->is_shdr = shdr;
1938 	isec->is_indata = data;
1939 
1940 	return (ld_place_section(ofl, isec, ident, 0));
1941 }
1942 
1943 /*
1944  * Build a .sunwbss section for allocation of tentative definitions.
1945  */
1946 uintptr_t
1947 ld_make_sunwbss(Ofl_desc *ofl, size_t size, Xword align)
1948 {
1949 	Shdr		*shdr;
1950 	Elf_Data	*data;
1951 	Is_desc		*isec;
1952 
1953 	/*
1954 	 * Allocate and initialize the Elf_Data structure.
1955 	 */
1956 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1957 		return (S_ERROR);
1958 	data->d_type = ELF_T_BYTE;
1959 	data->d_size = size;
1960 	data->d_align = align;
1961 	data->d_version = ofl->ofl_dehdr->e_version;
1962 
1963 	/*
1964 	 * Allocate and initialize the Shdr structure.
1965 	 */
1966 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1967 		return (S_ERROR);
1968 	shdr->sh_type = SHT_NOBITS;
1969 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
1970 	shdr->sh_size = (Xword)size;
1971 	shdr->sh_addralign = align;
1972 
1973 	/*
1974 	 * Allocate and initialize the Is_desc structure.
1975 	 */
1976 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1977 		return (S_ERROR);
1978 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWBSS);
1979 	isec->is_shdr = shdr;
1980 	isec->is_indata = data;
1981 
1982 	/*
1983 	 * Retain this .sunwbss input section as this will be where global
1984 	 * symbol references are added.
1985 	 */
1986 	ofl->ofl_issunwbss = isec;
1987 	if (ld_place_section(ofl, isec, 0, 0) == (Os_desc *)S_ERROR)
1988 		return (S_ERROR);
1989 
1990 	return (1);
1991 }
1992 
1993 /*
1994  * This routine is called when -z nopartial is in effect.
1995  */
1996 uintptr_t
1997 ld_make_sunwdata(Ofl_desc *ofl, size_t size, Xword align)
1998 {
1999 	Shdr		*shdr;
2000 	Elf_Data	*data;
2001 	Is_desc		*isec;
2002 	Os_desc		*osp;
2003 
2004 	/*
2005 	 * Allocate and initialize the Elf_Data structure.
2006 	 */
2007 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
2008 		return (S_ERROR);
2009 	data->d_type = ELF_T_BYTE;
2010 	data->d_size = size;
2011 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2012 		return (S_ERROR);
2013 	data->d_align = (size_t)M_WORD_ALIGN;
2014 	data->d_version = ofl->ofl_dehdr->e_version;
2015 
2016 	/*
2017 	 * Allocate and initialize the Shdr structure.
2018 	 */
2019 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
2020 		return (S_ERROR);
2021 	shdr->sh_type = SHT_PROGBITS;
2022 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
2023 	shdr->sh_size = (Xword)size;
2024 	if (align == 0)
2025 		shdr->sh_addralign = M_WORD_ALIGN;
2026 	else
2027 		shdr->sh_addralign = align;
2028 
2029 	/*
2030 	 * Allocate and initialize the Is_desc structure.
2031 	 */
2032 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
2033 		return (S_ERROR);
2034 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWDATA1);
2035 	isec->is_shdr = shdr;
2036 	isec->is_indata = data;
2037 
2038 	/*
2039 	 * Retain this .sunwdata1 input section as this will
2040 	 * be where global
2041 	 * symbol references are added.
2042 	 */
2043 	ofl->ofl_issunwdata1 = isec;
2044 	if ((osp = ld_place_section(ofl, isec, M_ID_DATA, 0)) ==
2045 	    (Os_desc *)S_ERROR)
2046 		return (S_ERROR);
2047 
2048 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
2049 		ofl->ofl_dynshdrcnt++;
2050 		osp->os_flags |= FLG_OS_OUTREL;
2051 	}
2052 	return (1);
2053 }
2054 
2055 /*
2056  * Make .sunwmove section
2057  */
2058 uintptr_t
2059 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2060 {
2061 	Shdr		*shdr;
2062 	Elf_Data	*data;
2063 	Is_desc		*isec;
2064 	size_t		size;
2065 	Listnode	*lnp1;
2066 	Psym_info	*psym;
2067 	int 		cnt = 1;
2068 
2069 	/*
2070 	 * Generate the move input sections and output sections
2071 	 */
2072 	size = mv_nums * sizeof (Move);
2073 
2074 	/*
2075 	 * Allocate and initialize the Elf_Data structure.
2076 	 */
2077 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
2078 		return (S_ERROR);
2079 	data->d_type = ELF_T_BYTE;
2080 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2081 		return (S_ERROR);
2082 	data->d_size = size;
2083 	data->d_align = sizeof (Lword);
2084 	data->d_version = ofl->ofl_dehdr->e_version;
2085 
2086 	/*
2087 	 * Allocate and initialize the Shdr structure.
2088 	 */
2089 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
2090 		return (S_ERROR);
2091 	shdr->sh_link = 0;
2092 	shdr->sh_info = 0;
2093 	shdr->sh_type = SHT_SUNW_move;
2094 	shdr->sh_size = (Xword)size;
2095 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
2096 	shdr->sh_addralign = sizeof (Lword);
2097 	shdr->sh_entsize = sizeof (Move);
2098 
2099 	/*
2100 	 * Allocate and initialize the Is_desc structure.
2101 	 */
2102 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
2103 		return (S_ERROR);
2104 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWMOVE);
2105 	isec->is_shdr = shdr;
2106 	isec->is_indata = data;
2107 	isec->is_file = 0;
2108 
2109 	/*
2110 	 * Copy move entries
2111 	 */
2112 	for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) {
2113 		Listnode *	lnp2;
2114 		Mv_itm *	mvitm;
2115 
2116 		if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN)
2117 			continue;
2118 		for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvitm)) {
2119 			if ((mvitm->mv_flag & FLG_MV_OUTSECT) == 0)
2120 				continue;
2121 			mvitm->mv_oidx = cnt;
2122 			cnt++;
2123 		}
2124 	}
2125 	if ((ofl->ofl_osmove = ld_place_section(ofl, isec, 0, 0)) ==
2126 	    (Os_desc *)S_ERROR)
2127 		return (S_ERROR);
2128 
2129 	return (1);
2130 }
2131 
2132 
2133 /*
2134  * The following sections are built after all input file processing and symbol
2135  * validation has been carried out.  The order is important (because the
2136  * addition of a section adds a new symbol there is a chicken and egg problem
2137  * of maintaining the appropriate counts).  By maintaining a known order the
2138  * individual routines can compensate for later, known, additions.
2139  */
2140 uintptr_t
2141 ld_make_sections(Ofl_desc *ofl)
2142 {
2143 	Word		flags = ofl->ofl_flags;
2144 	Listnode	*lnp1;
2145 	Sg_desc		*sgp;
2146 
2147 	/*
2148 	 * Generate any special sections.
2149 	 */
2150 	if (flags & FLG_OF_ADDVERS)
2151 		if (make_comment(ofl) == S_ERROR)
2152 			return (S_ERROR);
2153 
2154 	if (make_interp(ofl) == S_ERROR)
2155 		return (S_ERROR);
2156 
2157 	if (make_cap(ofl) == S_ERROR)
2158 		return (S_ERROR);
2159 
2160 	if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
2161 	    &ofl->ofl_initarray) == S_ERROR)
2162 		return (S_ERROR);
2163 
2164 	if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
2165 	    &ofl->ofl_finiarray) == S_ERROR)
2166 		return (S_ERROR);
2167 
2168 	if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
2169 	    &ofl->ofl_preiarray) == S_ERROR)
2170 		return (S_ERROR);
2171 
2172 	/*
2173 	 * Make the .plt section.  This occurs after any other relocation
2174 	 * sections are generated (see reloc_init()) to ensure that the
2175 	 * associated relocation section is after all the other relocation
2176 	 * sections.
2177 	 */
2178 	if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
2179 		if (make_plt(ofl) == S_ERROR)
2180 			return (S_ERROR);
2181 
2182 	/*
2183 	 * Determine whether any sections or files are not referenced.  Under
2184 	 * -Dunused a diagnostic for any unused components is generated, under
2185 	 * -zignore the component is removed from the final output.
2186 	 */
2187 	if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
2188 		if (ignore_section_processing(ofl) == S_ERROR)
2189 			return (S_ERROR);
2190 	}
2191 
2192 	/*
2193 	 * Add any necessary versioning information.
2194 	 */
2195 	if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED) {
2196 		if (make_verneed(ofl) == S_ERROR)
2197 			return (S_ERROR);
2198 	}
2199 	if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == FLG_OF_VERDEF) {
2200 		if (make_verdef(ofl) == S_ERROR)
2201 			return (S_ERROR);
2202 		if ((ofl->ofl_osversym = make_sym_sec(ofl,
2203 		    MSG_ORIG(MSG_SCN_SUNWVERSYM), sizeof (Versym),
2204 		    SHT_SUNW_versym, M_ID_VERSION)) == (Os_desc*)S_ERROR)
2205 			return (S_ERROR);
2206 	}
2207 
2208 	/*
2209 	 * Create a syminfo section is necessary.
2210 	 */
2211 	if (ofl->ofl_flags & FLG_OF_SYMINFO) {
2212 		if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
2213 		    MSG_ORIG(MSG_SCN_SUNWSYMINFO), sizeof (Syminfo),
2214 		    SHT_SUNW_syminfo, M_ID_SYMINFO)) == (Os_desc *)S_ERROR)
2215 			return (S_ERROR);
2216 	}
2217 
2218 	if (ofl->ofl_flags1 & FLG_OF1_RELCNT) {
2219 		/*
2220 		 * If -zcombreloc is enabled then all relocations (except for
2221 		 * the PLT's) are coalesced into a single relocation section.
2222 		 */
2223 		if (ofl->ofl_reloccnt) {
2224 			if (make_reloc(ofl, NULL) == S_ERROR)
2225 				return (S_ERROR);
2226 		}
2227 	} else {
2228 		/*
2229 		 * Create the required output relocation sections.  Note, new
2230 		 * sections may be added to the section list that is being
2231 		 * traversed.  These insertions can move the elements of the
2232 		 * Alist such that a section descriptor is re-read.  Recursion
2233 		 * is prevented by maintaining a previous section pointer and
2234 		 * insuring that this pointer isn't re-examined.
2235 		 */
2236 		for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2237 			Os_desc	**ospp, *posp = 0;
2238 			Aliste	off;
2239 
2240 			for (ALIST_TRAVERSE(sgp->sg_osdescs, off, ospp)) {
2241 				Os_desc	*osp = *ospp;
2242 
2243 				if ((osp != posp) && osp->os_szoutrels &&
2244 				    (osp != ofl->ofl_osplt)) {
2245 					if (make_reloc(ofl, osp) == S_ERROR)
2246 						return (S_ERROR);
2247 				}
2248 				posp = osp;
2249 			}
2250 		}
2251 
2252 		/*
2253 		 * If we're not building a combined relocation section, then
2254 		 * build a .rel[a] section as required.
2255 		 */
2256 		if (ofl->ofl_relocrelsz) {
2257 			if (make_reloc(ofl, NULL) == S_ERROR)
2258 				return (S_ERROR);
2259 		}
2260 	}
2261 
2262 	/*
2263 	 * The PLT relocations are always in their own section, and we try to
2264 	 * keep them at the end of the PLT table.  We do this to keep the hot
2265 	 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
2266 	 */
2267 	if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
2268 		if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
2269 			return (S_ERROR);
2270 	}
2271 
2272 	/*
2273 	 * Finally build the symbol and section header sections.
2274 	 */
2275 	if (flags & FLG_OF_DYNAMIC) {
2276 		if (make_dynamic(ofl) == S_ERROR)
2277 			return (S_ERROR);
2278 		if (make_dynstr(ofl) == S_ERROR)
2279 			return (S_ERROR);
2280 		/*
2281 		 * There is no use for .hash and .dynsym sections in a
2282 		 * relocatable object.
2283 		 */
2284 		if (!(flags & FLG_OF_RELOBJ)) {
2285 			if (make_hash(ofl) == S_ERROR)
2286 				return (S_ERROR);
2287 			if (make_dynsym(ofl) == S_ERROR)
2288 				return (S_ERROR);
2289 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
2290 			if (make_amd64_unwindhdr(ofl) == S_ERROR)
2291 				return (S_ERROR);
2292 #endif
2293 		}
2294 	}
2295 
2296 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
2297 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
2298 		/*
2299 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2300 		 * for the dynsym.  If so - do it now.
2301 		 */
2302 		if (ofl->ofl_osdynsym &&
2303 		    ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
2304 			if (make_dynsym_shndx(ofl) == S_ERROR)
2305 				return (S_ERROR);
2306 		}
2307 
2308 		if (make_strtab(ofl) == S_ERROR)
2309 			return (S_ERROR);
2310 		if (make_symtab(ofl) == S_ERROR)
2311 			return (S_ERROR);
2312 	} else {
2313 		/*
2314 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2315 		 * for the dynsym.  If so - do it now.
2316 		 */
2317 		if (ofl->ofl_osdynsym &&
2318 		    ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
2319 			if (make_dynsym_shndx(ofl) == S_ERROR)
2320 				return (S_ERROR);
2321 		}
2322 	}
2323 
2324 	if (make_shstrtab(ofl) == S_ERROR)
2325 		return (S_ERROR);
2326 
2327 	/*
2328 	 * Now that we've created all of our sections adjust the size
2329 	 * of SHT_SUNW_versym & SHT_SUNW_syminfo which are dependent on
2330 	 * the symbol table sizes.
2331 	 */
2332 	if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
2333 		Shdr *		shdr;
2334 		Is_desc *	isec;
2335 		Elf_Data *	data;
2336 		size_t		size;
2337 		ulong_t		cnt;
2338 
2339 		if ((flags & FLG_OF_RELOBJ) || (flags & FLG_OF_STATIC))
2340 			isec = (Is_desc *)ofl->ofl_ossymtab->
2341 				os_isdescs.head->data;
2342 		else
2343 			isec = (Is_desc *)ofl->ofl_osdynsym->
2344 				os_isdescs.head->data;
2345 		cnt = isec->is_shdr->sh_size / isec->is_shdr->sh_entsize;
2346 
2347 		if (ofl->ofl_osversym) {
2348 			isec = (Is_desc *)ofl->ofl_osversym->os_isdescs.
2349 				head->data;
2350 			data = isec->is_indata;
2351 			shdr = ofl->ofl_osversym->os_shdr;
2352 			size = cnt * shdr->sh_entsize;
2353 			shdr->sh_size = (Xword)size;
2354 			data->d_size = size;
2355 		}
2356 		if (ofl->ofl_ossyminfo) {
2357 			isec = (Is_desc *)ofl->ofl_ossyminfo->os_isdescs.
2358 				head->data;
2359 			data = isec->is_indata;
2360 			shdr = ofl->ofl_ossyminfo->os_shdr;
2361 			size = cnt * shdr->sh_entsize;
2362 			shdr->sh_size = (Xword)size;
2363 			data->d_size = size;
2364 		}
2365 	}
2366 
2367 	return (1);
2368 }
2369 
2370 /*
2371  * Build an additional data section - used to back OBJT symbol definitions
2372  * added with a mapfile.
2373  */
2374 Is_desc *
2375 ld_make_data(Ofl_desc *ofl, size_t size)
2376 {
2377 	Shdr		*shdr;
2378 	Elf_Data	*data;
2379 	Is_desc		*isec;
2380 
2381 	/*
2382 	 * Allocate and initialize the Elf_Data structure.
2383 	 */
2384 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
2385 		return ((Is_desc *)S_ERROR);
2386 	data->d_type = ELF_T_BYTE;
2387 	data->d_size = size;
2388 	data->d_align = M_WORD_ALIGN;
2389 	data->d_version = ofl->ofl_dehdr->e_version;
2390 
2391 	/*
2392 	 * Allocate and initialize the Shdr structure.
2393 	 */
2394 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
2395 		return ((Is_desc *)S_ERROR);
2396 	shdr->sh_type = SHT_PROGBITS;
2397 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
2398 	shdr->sh_size = (Xword)size;
2399 	shdr->sh_addralign = M_WORD_ALIGN;
2400 
2401 	/*
2402 	 * Allocate and initialize the Is_desc structure.
2403 	 */
2404 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == (Is_desc *)0)
2405 		return ((Is_desc *)S_ERROR);
2406 	isec->is_name = MSG_ORIG(MSG_SCN_DATA);
2407 	isec->is_shdr = shdr;
2408 	isec->is_indata = data;
2409 
2410 	if (ld_place_section(ofl, isec, M_ID_DATA, 0) == (Os_desc *)S_ERROR)
2411 		return ((Is_desc *)S_ERROR);
2412 
2413 	return (isec);
2414 }
2415 
2416 static const uchar_t ret_template[] = {
2417 #if	defined(i386)
2418 /* 0x00 */	0xc3				/* ret */
2419 #elif	defined(__amd64)
2420 /* 0x00 */	0x55,				/* pushq  %rbp */
2421 /* 0x01 */	0x48, 0x8b, 0xec,		/* movq   %rsp,%rbp */
2422 /* 0x04 */	0x48, 0x8b, 0xe5,		/* movq   %rbp,%rsp */
2423 /* 0x07 */	0x5d,				/* popq   %rbp */
2424 /* 0x08 */	0xc3				/* ret */
2425 #elif	defined(sparc) || defined(__sparcv9)
2426 /* 0x00 */	0x81, 0xc3, 0xe0, 0x08,		/* retl */
2427 /* 0x04 */	0x01, 0x00, 0x00, 0x00		/* nop */
2428 #else
2429 #error	unsupported architecture!
2430 #endif
2431 };
2432 
2433 /*
2434  * Build an additional text section - used to back FUNC symbol definitions
2435  * added with a mapfile.
2436  */
2437 Is_desc *
2438 ld_make_text(Ofl_desc *ofl, size_t size)
2439 {
2440 	Shdr		*shdr;
2441 	Elf_Data	*data;
2442 	Is_desc		*isec;
2443 
2444 	/*
2445 	 * Insure the size is sufficient to contain the minimum return
2446 	 * instruction.
2447 	 */
2448 	if (size < sizeof (ret_template))
2449 		size = sizeof (ret_template);
2450 
2451 	/*
2452 	 * Allocate and initialize the Elf_Data structure.  Fill the buffer
2453 	 * with the appropriate return instruction.
2454 	 */
2455 	if (((data = libld_calloc(sizeof (Elf_Data), 1)) == 0) ||
2456 	    ((data->d_buf = libld_calloc(size, 1)) == 0))
2457 		return ((Is_desc *)S_ERROR);
2458 	data->d_type = ELF_T_BYTE;
2459 	data->d_size = size;
2460 	data->d_align = M_WORD_ALIGN;
2461 	data->d_version = ofl->ofl_dehdr->e_version;
2462 
2463 	(void) memcpy(data->d_buf, ret_template, sizeof (ret_template));
2464 
2465 	/*
2466 	 * Allocate and initialize the Shdr structure.
2467 	 */
2468 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
2469 		return ((Is_desc *)S_ERROR);
2470 	shdr->sh_type = SHT_PROGBITS;
2471 	shdr->sh_flags = SHF_ALLOC | SHF_EXECINSTR;
2472 	shdr->sh_size = (Xword)size;
2473 	shdr->sh_addralign = M_WORD_ALIGN;
2474 
2475 	/*
2476 	 * Allocate and initialize the Is_desc structure.
2477 	 */
2478 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == (Is_desc *)0)
2479 		return ((Is_desc *)S_ERROR);
2480 	isec->is_name = MSG_ORIG(MSG_SCN_TEXT);
2481 	isec->is_shdr = shdr;
2482 	isec->is_indata = data;
2483 
2484 	if (ld_place_section(ofl, isec, M_ID_TEXT, 0) == (Os_desc *)S_ERROR)
2485 		return ((Is_desc *)S_ERROR);
2486 
2487 	return (isec);
2488 }
2489