xref: /titanic_50/usr/src/cmd/sgs/libld/common/sections.c (revision 4088bb40326b75ef60834a6c2a92e29e25474b68)
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 interpreter 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 	 * If -z nointerp is in effect, don't create an interpreter section.
912 	 */
913 	if (ofl->ofl_flags1 & FLG_OF1_NOINTRP)
914 		return (1);
915 
916 	/*
917 	 * We always build an .interp section for dynamic executables.  However
918 	 * if the user has specifically specified an interpreter we'll build
919 	 * this section for any output (presumably the user knows what they are
920 	 * doing. refer ABI section 5-4, and ld.1 man page use of -I).
921 	 */
922 	if (((ofl->ofl_flags & (FLG_OF_DYNAMIC | FLG_OF_EXEC |
923 	    FLG_OF_RELOBJ)) != (FLG_OF_DYNAMIC | FLG_OF_EXEC)) && !iname)
924 		return (1);
925 
926 	/*
927 	 * In the case of a dynamic executable supply a default interpreter
928 	 * if a specific interpreter has not been specified.
929 	 */
930 	if (iname == 0) {
931 		if (ofl->ofl_dehdr->e_machine == EM_SPARCV9)
932 			iname = ofl->ofl_interp =
933 			    MSG_ORIG(MSG_PTH_RTLD_SPARCV9);
934 		else if (ofl->ofl_dehdr->e_machine == EM_AMD64)
935 			iname = ofl->ofl_interp =
936 			    MSG_ORIG(MSG_PTH_RTLD_AMD64);
937 		else
938 			iname = ofl->ofl_interp = MSG_ORIG(MSG_PTH_RTLD);
939 	}
940 
941 	size = strlen(iname) + 1;
942 
943 	/*
944 	 * Allocate and initialize the Elf_Data structure.
945 	 */
946 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
947 		return (S_ERROR);
948 	data->d_type = ELF_T_BYTE;
949 	data->d_size = size;
950 	data->d_version = ofl->ofl_dehdr->e_version;
951 
952 	/*
953 	 * Allocate and initialize the Shdr structure.
954 	 */
955 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
956 		return (S_ERROR);
957 	shdr->sh_type = SHT_PROGBITS;
958 	shdr->sh_flags = SHF_ALLOC;
959 	shdr->sh_size = (Xword)size;
960 
961 	/*
962 	 * Allocate and initialize the Is_desc structure.
963 	 */
964 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
965 		return (S_ERROR);
966 	isec->is_name = MSG_ORIG(MSG_SCN_INTERP);
967 	isec->is_shdr = shdr;
968 	isec->is_indata = data;
969 
970 	ofl->ofl_osinterp = ld_place_section(ofl, isec, M_ID_INTERP, 0);
971 	return ((uintptr_t)ofl->ofl_osinterp);
972 }
973 
974 /*
975  * Build a hardware/software capabilities section.
976  */
977 static uintptr_t
978 make_cap(Ofl_desc *ofl)
979 {
980 	Shdr		*shdr;
981 	Elf_Data	*data;
982 	Is_desc		*isec;
983 	Os_desc		*osec;
984 	Cap		*cap;
985 	size_t		size = 0;
986 
987 	/*
988 	 * Determine how many entries are required.
989 	 */
990 	if (ofl->ofl_hwcap_1)
991 		size++;
992 	if (ofl->ofl_sfcap_1)
993 		size++;
994 	if (size == 0)
995 		return (1);
996 	size++;				/* Add CA_SUNW_NULL */
997 
998 	/*
999 	 * Allocate and initialize the Elf_Data structure.
1000 	 */
1001 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1002 		return (S_ERROR);
1003 	data->d_type = ELF_T_CAP;
1004 	data->d_version = ofl->ofl_dehdr->e_version;
1005 	data->d_align = M_WORD_ALIGN;
1006 
1007 	/*
1008 	 * Allocate and initialize the Shdr structure.
1009 	 */
1010 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1011 		return (S_ERROR);
1012 	shdr->sh_type = SHT_SUNW_cap;
1013 	shdr->sh_flags = SHF_ALLOC;
1014 	shdr->sh_addralign = M_WORD_ALIGN;
1015 	if ((shdr->sh_entsize = (Xword)elf_fsize(ELF_T_CAP, 1,
1016 	    ofl->ofl_dehdr->e_version)) == 0) {
1017 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_FSIZE),
1018 		    ofl->ofl_name);
1019 		return (S_ERROR);
1020 	}
1021 
1022 	/*
1023 	 * Allocate and initialize the Is_desc structure.
1024 	 */
1025 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1026 		return (S_ERROR);
1027 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWCAP);
1028 	isec->is_shdr = shdr;
1029 	isec->is_indata = data;
1030 
1031 	/*
1032 	 * Determine the size of the section, and create the data.
1033 	 */
1034 	size = size * (size_t)shdr->sh_entsize;
1035 	shdr->sh_size = (Xword)size;
1036 	data->d_size = size;
1037 	if ((data->d_buf = libld_malloc(size)) == 0)
1038 		return (S_ERROR);
1039 
1040 	cap = (Cap *)data->d_buf;
1041 	if (ofl->ofl_hwcap_1) {
1042 		cap->c_tag = CA_SUNW_HW_1;
1043 		cap->c_un.c_val = ofl->ofl_hwcap_1;
1044 		cap++;
1045 	}
1046 	if (ofl->ofl_sfcap_1) {
1047 		cap->c_tag = CA_SUNW_SF_1;
1048 		cap->c_un.c_val = ofl->ofl_sfcap_1;
1049 		cap++;
1050 	}
1051 	cap->c_tag = CA_SUNW_NULL;
1052 	cap->c_un.c_val = 0;
1053 
1054 	/*
1055 	 * If we're not creating a relocatable object, save the output section
1056 	 * to trigger the creation of an associated  a program header.
1057 	 */
1058 	osec = ld_place_section(ofl, isec, M_ID_CAP, 0);
1059 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)
1060 		ofl->ofl_oscap = osec;
1061 
1062 	return ((uintptr_t)osec);
1063 }
1064 
1065 /*
1066  * Build the PLT section and its associated relocation entries.
1067  */
1068 static uintptr_t
1069 make_plt(Ofl_desc *ofl)
1070 {
1071 	Shdr		*shdr;
1072 	Elf_Data	*data;
1073 	Is_desc		*isec;
1074 	size_t		size = (size_t)M_PLT_RESERVSZ +
1075 				(((size_t)ofl->ofl_pltcnt +
1076 				(size_t)ofl->ofl_pltpad) * M_PLT_ENTSIZE);
1077 	size_t		rsize = (size_t)ofl->ofl_relocpltsz;
1078 
1079 #if	defined(sparc)
1080 	/*
1081 	 * Account for the NOP at the end of the plt.
1082 	 */
1083 	size += sizeof (Word);
1084 #endif
1085 
1086 	/*
1087 	 * Allocate and initialize the Elf_Data structure.
1088 	 */
1089 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1090 		return (S_ERROR);
1091 	data->d_type = ELF_T_BYTE;
1092 	data->d_size = size;
1093 	data->d_align = M_PLT_ALIGN;
1094 	data->d_version = ofl->ofl_dehdr->e_version;
1095 
1096 	/*
1097 	 * Allocate and initialize the Shdr structure.
1098 	 */
1099 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1100 		return (S_ERROR);
1101 	shdr->sh_type = SHT_PROGBITS;
1102 	shdr->sh_flags = M_PLT_SHF_FLAGS;
1103 	shdr->sh_size = (Xword)size;
1104 	shdr->sh_addralign = M_PLT_ALIGN;
1105 	shdr->sh_entsize = M_PLT_ENTSIZE;
1106 
1107 	/*
1108 	 * Allocate and initialize the Is_desc structure.
1109 	 */
1110 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == (Is_desc *)0)
1111 		return (S_ERROR);
1112 	isec->is_name = MSG_ORIG(MSG_SCN_PLT);
1113 	isec->is_shdr = shdr;
1114 	isec->is_indata = data;
1115 
1116 	if ((ofl->ofl_osplt = ld_place_section(ofl, isec, M_ID_PLT, 0)) ==
1117 	    (Os_desc *)S_ERROR)
1118 		return (S_ERROR);
1119 
1120 	ofl->ofl_osplt->os_szoutrels = (Xword)rsize;
1121 
1122 	return (1);
1123 }
1124 
1125 /*
1126  * Make the hash table.  Only built for dynamic executables and shared
1127  * libraries, and provides hashed lookup into the global symbol table
1128  * (.dynsym) for the run-time linker to resolve symbol lookups.
1129  */
1130 static uintptr_t
1131 make_hash(Ofl_desc *ofl)
1132 {
1133 	Shdr		*shdr;
1134 	Elf_Data	*data;
1135 	Is_desc		*isec;
1136 	size_t		size;
1137 	Word		nsyms = ofl->ofl_globcnt;
1138 	size_t		cnt;
1139 
1140 	/*
1141 	 * Allocate and initialize the Elf_Data structure.
1142 	 */
1143 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1144 		return (S_ERROR);
1145 	data->d_type = ELF_T_WORD;
1146 	data->d_align = M_WORD_ALIGN;
1147 	data->d_version = ofl->ofl_dehdr->e_version;
1148 
1149 	/*
1150 	 * Allocate and initialize the Shdr structure.
1151 	 */
1152 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1153 		return (S_ERROR);
1154 	shdr->sh_type = SHT_HASH;
1155 	shdr->sh_flags = SHF_ALLOC;
1156 	shdr->sh_addralign = M_WORD_ALIGN;
1157 	if ((shdr->sh_entsize = (Xword)elf_fsize(ELF_T_WORD, 1,
1158 	    ofl->ofl_dehdr->e_version)) == 0) {
1159 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_FSIZE),
1160 		    ofl->ofl_name);
1161 		return (S_ERROR);
1162 	}
1163 
1164 	/*
1165 	 * Allocate and initialize the Is_desc structure.
1166 	 */
1167 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1168 		return (S_ERROR);
1169 	isec->is_name = MSG_ORIG(MSG_SCN_HASH);
1170 	isec->is_shdr = shdr;
1171 	isec->is_indata = data;
1172 
1173 	/*
1174 	 * Place the section first since it will affect the local symbol
1175 	 * count.
1176 	 */
1177 	if ((ofl->ofl_oshash = ld_place_section(ofl, isec, M_ID_HASH, 0)) ==
1178 	    (Os_desc *)S_ERROR)
1179 		return (S_ERROR);
1180 
1181 	/*
1182 	 * Calculate the number of output hash buckets.
1183 	 */
1184 	ofl->ofl_hashbkts = findprime(nsyms);
1185 
1186 	/*
1187 	 * The size of the hash table is determined by
1188 	 *
1189 	 *	i.	the initial nbucket and nchain entries (2)
1190 	 *	ii.	the number of buckets (calculated above)
1191 	 *	iii.	the number of chains (this is based on the number of
1192 	 *		symbols in the .dynsym array + NULL symbol).
1193 	 */
1194 	cnt = 2 + ofl->ofl_hashbkts + (ofl->ofl_dynshdrcnt +
1195 		ofl->ofl_globcnt + ofl->ofl_lregsymcnt + 1);
1196 	size = cnt * shdr->sh_entsize;
1197 
1198 	/*
1199 	 * Finalize the section header and data buffer initialization.
1200 	 */
1201 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
1202 		return (S_ERROR);
1203 	data->d_size = size;
1204 	shdr->sh_size = (Xword)size;
1205 
1206 	return (1);
1207 }
1208 
1209 /*
1210  * Generate the standard symbol table.  Contains all locals and globals,
1211  * and resides in a non-allocatable section (ie. it can be stripped).
1212  */
1213 static uintptr_t
1214 make_symtab(Ofl_desc *ofl)
1215 {
1216 	Shdr		*shdr;
1217 	Elf_Data	*data;
1218 	Is_desc		*isec;
1219 	Is_desc		*xisec = 0;
1220 	size_t		size;
1221 	Word		symcnt;
1222 
1223 	/*
1224 	 * Allocate and initialize the Elf_Data structure.
1225 	 */
1226 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1227 		return (S_ERROR);
1228 	data->d_type = ELF_T_SYM;
1229 	data->d_align = M_WORD_ALIGN;
1230 	data->d_version = ofl->ofl_dehdr->e_version;
1231 
1232 	/*
1233 	 * Allocate and initialize the Shdr structure.
1234 	 */
1235 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1236 		return (S_ERROR);
1237 	shdr->sh_type = SHT_SYMTAB;
1238 	shdr->sh_addralign = M_WORD_ALIGN;
1239 	if ((shdr->sh_entsize = (Xword)elf_fsize(ELF_T_SYM, 1,
1240 	    ofl->ofl_dehdr->e_version)) == 0) {
1241 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_FSIZE),
1242 		    ofl->ofl_name);
1243 		return (S_ERROR);
1244 	}
1245 
1246 	/*
1247 	 * Allocate and initialize the Is_desc structure.
1248 	 */
1249 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1250 		return (S_ERROR);
1251 	isec->is_name = MSG_ORIG(MSG_SCN_SYMTAB);
1252 	isec->is_shdr = shdr;
1253 	isec->is_indata = data;
1254 
1255 	/*
1256 	 * Place the section first since it will affect the local symbol
1257 	 * count.
1258 	 */
1259 	if ((ofl->ofl_ossymtab = ld_place_section(ofl, isec, M_ID_SYMTAB, 0)) ==
1260 	    (Os_desc *)S_ERROR)
1261 		return (S_ERROR);
1262 
1263 	/*
1264 	 * At this point we've created all but the 'shstrtab' section.
1265 	 * Determine if we have to use 'Extended Sections'.  If so - then
1266 	 * also create a SHT_SYMTAB_SHNDX section.
1267 	 */
1268 	if ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE) {
1269 		Shdr		*xshdr;
1270 		Elf_Data	*xdata;
1271 
1272 		if ((xdata = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1273 			return (S_ERROR);
1274 		xdata->d_type = ELF_T_WORD;
1275 		xdata->d_align = M_WORD_ALIGN;
1276 		xdata->d_version = ofl->ofl_dehdr->e_version;
1277 		if ((xshdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1278 			return (S_ERROR);
1279 		xshdr->sh_type = SHT_SYMTAB_SHNDX;
1280 		xshdr->sh_addralign = M_WORD_ALIGN;
1281 		xshdr->sh_entsize = sizeof (Word);
1282 		if ((xisec = libld_calloc(1, sizeof (Is_desc))) == 0)
1283 			return (S_ERROR);
1284 		xisec->is_name = MSG_ORIG(MSG_SCN_SYMTAB_SHNDX);
1285 		xisec->is_shdr = xshdr;
1286 		xisec->is_indata = xdata;
1287 		if ((ofl->ofl_ossymshndx = ld_place_section(ofl, xisec,
1288 		    M_ID_SYMTAB_NDX, 0)) == (Os_desc *)S_ERROR)
1289 			return (S_ERROR);
1290 	}
1291 	/*
1292 	 * Calculated number of symbols, which need to be augmented by
1293 	 * the null first entry, the FILE symbol, and the .shstrtab entry.
1294 	 */
1295 	symcnt = (size_t)(3 + ofl->ofl_shdrcnt + ofl->ofl_scopecnt +
1296 		ofl->ofl_locscnt + ofl->ofl_globcnt);
1297 	size = symcnt * shdr->sh_entsize;
1298 
1299 	/*
1300 	 * Finalize the section header and data buffer initialization.
1301 	 */
1302 	data->d_size = size;
1303 	shdr->sh_size = (Xword)size;
1304 
1305 	/*
1306 	 * If we created a SHT_SYMTAB_SHNDX - then set it's sizes too.
1307 	 */
1308 	if (xisec) {
1309 		size_t	xsize = symcnt * sizeof (Word);
1310 
1311 		xisec->is_indata->d_size = xsize;
1312 		xisec->is_shdr->sh_size = (Xword)xsize;
1313 	}
1314 
1315 	return (1);
1316 }
1317 
1318 
1319 /*
1320  * Build a dynamic symbol table.  Contains only globals symbols and resides
1321  * in the text segment of a dynamic executable or shared library.
1322  */
1323 static uintptr_t
1324 make_dynsym(Ofl_desc *ofl)
1325 {
1326 	Shdr		*shdr;
1327 	Elf_Data	*data;
1328 	Is_desc		*isec;
1329 	size_t		size;
1330 	Xword		cnt;
1331 
1332 	/*
1333 	 * Allocate and initialize the Elf_Data structure.
1334 	 */
1335 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1336 		return (S_ERROR);
1337 	data->d_type = ELF_T_SYM;
1338 	data->d_align = M_WORD_ALIGN;
1339 	data->d_version = ofl->ofl_dehdr->e_version;
1340 
1341 	/*
1342 	 * Allocate and initialize the Shdr structure.
1343 	 */
1344 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1345 		return (S_ERROR);
1346 	shdr->sh_type = SHT_DYNSYM;
1347 	shdr->sh_flags = SHF_ALLOC;
1348 	shdr->sh_addralign = M_WORD_ALIGN;
1349 	if ((shdr->sh_entsize = (Xword)elf_fsize(ELF_T_SYM, 1,
1350 	    ofl->ofl_dehdr->e_version)) == 0) {
1351 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_FSIZE),
1352 		    ofl->ofl_name);
1353 		return (S_ERROR);
1354 	}
1355 
1356 	/*
1357 	 * Allocate and initialize the Is_desc structure.
1358 	 */
1359 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1360 		return (S_ERROR);
1361 	isec->is_name = MSG_ORIG(MSG_SCN_DYNSYM);
1362 	isec->is_shdr = shdr;
1363 	isec->is_indata = data;
1364 
1365 	/*
1366 	 * Place the section first since it will affect the local symbol
1367 	 * count.
1368 	 */
1369 	if ((ofl->ofl_osdynsym = ld_place_section(ofl, isec, M_ID_DYNSYM, 0)) ==
1370 	    (Os_desc *)S_ERROR)
1371 		return (S_ERROR);
1372 
1373 	/*
1374 	 * One extra section header entry for the 'null' entry.
1375 	 */
1376 	cnt = 1 + ofl->ofl_dynshdrcnt + ofl->ofl_globcnt + ofl->ofl_lregsymcnt;
1377 	size = (size_t)cnt * shdr->sh_entsize;
1378 
1379 	/*
1380 	 * Finalize the section header and data buffer initialization.
1381 	 */
1382 	data->d_size = size;
1383 	shdr->sh_size = (Xword)size;
1384 
1385 	return (1);
1386 }
1387 
1388 /*
1389  * Build a SHT_SYMTAB_SHNDX for the .dynsym
1390  */
1391 static uintptr_t
1392 make_dynsym_shndx(Ofl_desc *ofl)
1393 {
1394 	Is_desc		*isec;
1395 	Is_desc		*dynsymisp;
1396 	Shdr		*shdr, *dynshdr;
1397 	Elf_Data	*data;
1398 
1399 	/*
1400 	 * Allocate the Elf_Data structure.
1401 	 */
1402 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1403 		return (S_ERROR);
1404 	data->d_type = ELF_T_WORD;
1405 	data->d_align = M_WORD_ALIGN;
1406 	data->d_version = ofl->ofl_dehdr->e_version;
1407 
1408 	/*
1409 	 * Allocate the Shdr structure.
1410 	 */
1411 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1412 		return (S_ERROR);
1413 	shdr->sh_type = SHT_SYMTAB_SHNDX;
1414 	shdr->sh_addralign = M_WORD_ALIGN;
1415 	shdr->sh_entsize = sizeof (Word);
1416 
1417 	/*
1418 	 * Allocate the Is_desc structure.
1419 	 */
1420 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1421 		return (S_ERROR);
1422 	isec->is_name = MSG_ORIG(MSG_SCN_DYNSYM_SHNDX);
1423 	isec->is_shdr = shdr;
1424 	isec->is_indata = data;
1425 
1426 	if ((ofl->ofl_osdynshndx = ld_place_section(ofl, isec,
1427 	    M_ID_DYNSYM_NDX, 0)) == (Os_desc *)S_ERROR)
1428 		return (S_ERROR);
1429 
1430 	assert(ofl->ofl_osdynsym);
1431 	dynsymisp = (Is_desc *)ofl->ofl_osdynsym->os_isdescs.head->data;
1432 	dynshdr = dynsymisp->is_shdr;
1433 	shdr->sh_size = (Xword)((dynshdr->sh_size / dynshdr->sh_entsize) *
1434 		sizeof (Word));
1435 	data->d_size = shdr->sh_size;
1436 
1437 	return (1);
1438 }
1439 
1440 
1441 /*
1442  * Build a string table for the section headers.
1443  */
1444 static uintptr_t
1445 make_shstrtab(Ofl_desc *ofl)
1446 {
1447 	Shdr		*shdr;
1448 	Elf_Data	*data;
1449 	Is_desc		*isec;
1450 	size_t		size;
1451 
1452 	/*
1453 	 * Allocate the Elf_Data structure.
1454 	 */
1455 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1456 		return (S_ERROR);
1457 	data->d_type = ELF_T_BYTE;
1458 	data->d_align = 1;
1459 	data->d_version = ofl->ofl_dehdr->e_version;
1460 
1461 	/*
1462 	 * Allocate the Shdr structure.
1463 	 */
1464 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1465 		return (S_ERROR);
1466 	shdr->sh_type = SHT_STRTAB;
1467 	shdr->sh_flags |= SHF_STRINGS;
1468 	shdr->sh_addralign = 1;
1469 
1470 	/*
1471 	 * Allocate the Is_desc structure.
1472 	 */
1473 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1474 		return (S_ERROR);
1475 	isec->is_name = MSG_ORIG(MSG_SCN_SHSTRTAB);
1476 	isec->is_shdr = shdr;
1477 	isec->is_indata = data;
1478 
1479 	/*
1480 	 * Place the section first, as it may effect the number of section
1481 	 * headers to account for.
1482 	 */
1483 	if ((ofl->ofl_osshstrtab = ld_place_section(ofl, isec, M_ID_NOTE, 0)) ==
1484 	    (Os_desc *)S_ERROR)
1485 		return (S_ERROR);
1486 
1487 	size = st_getstrtab_sz(ofl->ofl_shdrsttab);
1488 	assert(size > 0);
1489 
1490 	data->d_size = size;
1491 	shdr->sh_size = (Xword)size;
1492 
1493 	return (1);
1494 }
1495 
1496 /*
1497  * Build a string section for the standard symbol table.
1498  */
1499 static uintptr_t
1500 make_strtab(Ofl_desc *ofl)
1501 {
1502 	Shdr		*shdr;
1503 	Elf_Data	*data;
1504 	Is_desc		*isec;
1505 	size_t		size;
1506 
1507 	/*
1508 	 * This string table consists of all the global and local symbols.
1509 	 * Account for null bytes at end of the file name and the beginning
1510 	 * of section.
1511 	 */
1512 	if (st_insert(ofl->ofl_strtab, ofl->ofl_name) == -1)
1513 		return (S_ERROR);
1514 
1515 	size = st_getstrtab_sz(ofl->ofl_strtab);
1516 	assert(size > 0);
1517 
1518 	/*
1519 	 * Allocate the Elf_Data structure.
1520 	 */
1521 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1522 		return (S_ERROR);
1523 	data->d_size = size;
1524 	data->d_type = ELF_T_BYTE;
1525 	data->d_align = 1;
1526 	data->d_version = ofl->ofl_dehdr->e_version;
1527 
1528 	/*
1529 	 * Allocate the Shdr structure.
1530 	 */
1531 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1532 		return (S_ERROR);
1533 	shdr->sh_size = (Xword)size;
1534 	shdr->sh_addralign = 1;
1535 	shdr->sh_type = SHT_STRTAB;
1536 	shdr->sh_flags |= SHF_STRINGS;
1537 
1538 	/*
1539 	 * Allocate and initialize the Is_desc structure.
1540 	 */
1541 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1542 		return (S_ERROR);
1543 	isec->is_name = MSG_ORIG(MSG_SCN_STRTAB);
1544 	isec->is_shdr = shdr;
1545 	isec->is_indata = data;
1546 
1547 	ofl->ofl_osstrtab = ld_place_section(ofl, isec, M_ID_STRTAB, 0);
1548 	return ((uintptr_t)ofl->ofl_osstrtab);
1549 }
1550 
1551 /*
1552  * Build a string table for the dynamic symbol table.
1553  */
1554 static uintptr_t
1555 make_dynstr(Ofl_desc *ofl)
1556 {
1557 	Shdr		*shdr;
1558 	Elf_Data	*data;
1559 	Is_desc		*isec;
1560 	size_t		size;
1561 
1562 	/*
1563 	 * Account for any local, named register symbols.  These locals are
1564 	 * required for reference from DT_REGISTER .dynamic entries.
1565 	 */
1566 	if (ofl->ofl_regsyms) {
1567 		int	ndx;
1568 
1569 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1570 			Sym_desc *	sdp;
1571 
1572 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1573 				continue;
1574 
1575 			if (((sdp->sd_flags1 & FLG_SY1_LOCL) == 0) &&
1576 			    (ELF_ST_BIND(sdp->sd_sym->st_info) != STB_LOCAL))
1577 				continue;
1578 
1579 			if (sdp->sd_sym->st_name == 0)
1580 				continue;
1581 
1582 			if (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1)
1583 				return (S_ERROR);
1584 		}
1585 	}
1586 
1587 	/*
1588 	 * Reserve entries for any per-symbol auxiliary/filter strings.
1589 	 */
1590 	if (ofl->ofl_dtsfltrs) {
1591 		Dfltr_desc *	dftp;
1592 		Aliste		off;
1593 
1594 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp))
1595 			if (st_insert(ofl->ofl_dynstrtab, dftp->dft_str) == -1)
1596 				return (S_ERROR);
1597 	}
1598 
1599 	size = st_getstrtab_sz(ofl->ofl_dynstrtab);
1600 	assert(size > 0);
1601 
1602 	/*
1603 	 * Allocate the Elf_Data structure.
1604 	 */
1605 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1606 		return (S_ERROR);
1607 	data->d_type = ELF_T_BYTE;
1608 	data->d_size = size;
1609 	data->d_align = 1;
1610 	data->d_version = ofl->ofl_dehdr->e_version;
1611 
1612 	/*
1613 	 * Allocate the Shdr structure.
1614 	 */
1615 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1616 		return (S_ERROR);
1617 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
1618 		shdr->sh_flags = SHF_ALLOC;
1619 
1620 	shdr->sh_type = SHT_STRTAB;
1621 	shdr->sh_flags |= SHF_STRINGS;
1622 	shdr->sh_size = (Xword)size;
1623 	shdr->sh_addralign = 1;
1624 
1625 	/*
1626 	 * Allocate and initialize the Is_desc structure.
1627 	 */
1628 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1629 		return (S_ERROR);
1630 	isec->is_name = MSG_ORIG(MSG_SCN_DYNSTR);
1631 	isec->is_shdr = shdr;
1632 	isec->is_indata = data;
1633 
1634 	ofl->ofl_osdynstr = ld_place_section(ofl, isec, M_ID_DYNSTR, 0);
1635 	return ((uintptr_t)ofl->ofl_osdynstr);
1636 }
1637 
1638 /*
1639  * Generate an output relocation section which will contain the relocation
1640  * information to be applied to the `osp' section.
1641  *
1642  * If (osp == NULL) then we are creating the coalesced relocation section
1643  * for an executable and/or a shared object.
1644  */
1645 static uintptr_t
1646 make_reloc(Ofl_desc *ofl, Os_desc *osp)
1647 {
1648 	Shdr		*shdr;
1649 	Elf_Data	*data;
1650 	Is_desc		*isec;
1651 	size_t		size;
1652 	Xword		sh_flags;
1653 	char 		*sectname;
1654 	Os_desc		*rosp;
1655 	Word		relsize;
1656 	const char	*rel_prefix;
1657 
1658 	/* LINTED */
1659 	if (M_REL_SHT_TYPE == SHT_REL) {
1660 		/* REL */
1661 		relsize = sizeof (Rel);
1662 		rel_prefix = MSG_ORIG(MSG_SCN_REL);
1663 	} else {
1664 		/* RELA */
1665 		relsize = sizeof (Rela);
1666 		rel_prefix = MSG_ORIG(MSG_SCN_RELA);
1667 	}
1668 
1669 	if (osp) {
1670 		size = osp->os_szoutrels;
1671 		sh_flags = osp->os_shdr->sh_flags;
1672 		if ((sectname = libld_malloc(strlen(rel_prefix) +
1673 		    strlen(osp->os_name) + 1)) == 0)
1674 			return (S_ERROR);
1675 		(void) strcpy(sectname, rel_prefix);
1676 		(void) strcat(sectname, osp->os_name);
1677 	} else if (ofl->ofl_flags1 & FLG_OF1_RELCNT) {
1678 		size = (ofl->ofl_reloccnt - ofl->ofl_reloccntsub) * relsize;
1679 		sh_flags = SHF_ALLOC;
1680 		sectname = (char *)MSG_ORIG(MSG_SCN_SUNWRELOC);
1681 	} else {
1682 		size = ofl->ofl_relocrelsz;
1683 		sh_flags = SHF_ALLOC;
1684 		sectname = (char *)rel_prefix;
1685 	}
1686 
1687 	/*
1688 	 * Keep track of total size of 'output relocations' (to be stored
1689 	 * in .dynamic)
1690 	 */
1691 	/* LINTED */
1692 	ofl->ofl_relocsz += (Xword)size;
1693 
1694 	/*
1695 	 * Allocate and initialize the Elf_Data structure.
1696 	 */
1697 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1698 		return (S_ERROR);
1699 	data->d_type = M_REL_ELF_TYPE;
1700 	data->d_size = size;
1701 	data->d_align = M_WORD_ALIGN;
1702 	data->d_version = ofl->ofl_dehdr->e_version;
1703 
1704 	/*
1705 	 * Allocate and initialize the Shdr structure.
1706 	 */
1707 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1708 		return (S_ERROR);
1709 	shdr->sh_type = M_REL_SHT_TYPE;
1710 	shdr->sh_size = (Xword)size;
1711 	shdr->sh_addralign = M_WORD_ALIGN;
1712 	shdr->sh_entsize = relsize;
1713 
1714 	if ((ofl->ofl_flags & FLG_OF_DYNAMIC) &&
1715 	    !(ofl->ofl_flags & FLG_OF_RELOBJ) &&
1716 	    (sh_flags & SHF_ALLOC))
1717 		shdr->sh_flags = SHF_ALLOC;
1718 
1719 	if (osp) {
1720 		/*
1721 		 * The sh_info field of the SHT_REL* sections points to the
1722 		 * section the relocations are to be applied to.
1723 		 */
1724 		shdr->sh_flags |= SHF_INFO_LINK;
1725 	}
1726 
1727 
1728 	/*
1729 	 * Allocate and initialize the Is_desc structure.
1730 	 */
1731 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1732 		return (S_ERROR);
1733 	isec->is_shdr = shdr;
1734 	isec->is_indata = data;
1735 	isec->is_name = sectname;
1736 
1737 
1738 	/*
1739 	 * Associate this relocation section to the section its going to
1740 	 * relocate.
1741 	 */
1742 	if ((rosp = ld_place_section(ofl, isec, M_ID_REL, 0)) ==
1743 	    (Os_desc *)S_ERROR)
1744 		return (S_ERROR);
1745 
1746 	if (osp) {
1747 		Listnode	*lnp;
1748 		Is_desc		*risp;
1749 
1750 		/*
1751 		 * We associate the input relocation sections - with
1752 		 * the newly created output relocation section.
1753 		 *
1754 		 * This is used primarily so that we can update
1755 		 * SHT_GROUP[sect_no] entries to point to the
1756 		 * created output relocation sections.
1757 		 */
1758 		for (LIST_TRAVERSE(&(osp->os_relisdescs), lnp, risp)) {
1759 			risp->is_osdesc = rosp;
1760 
1761 			/*
1762 			 * If the input relocation section had the SHF_GROUP
1763 			 * flag set - propagate it to the output relocation
1764 			 * section.
1765 			 */
1766 			if (risp->is_shdr->sh_flags & SHF_GROUP) {
1767 				rosp->os_shdr->sh_flags |= SHF_GROUP;
1768 				break;
1769 			}
1770 		}
1771 		osp->os_relosdesc = rosp;
1772 	} else
1773 		ofl->ofl_osrel = rosp;
1774 
1775 	/*
1776 	 * If this is the first relocation section we've encountered save it
1777 	 * so that the .dynamic entry can be initialized accordingly.
1778 	 */
1779 	if (ofl->ofl_osrelhead == (Os_desc *)0)
1780 		ofl->ofl_osrelhead = rosp;
1781 
1782 	return (1);
1783 }
1784 
1785 /*
1786  * Generate version needed section.
1787  */
1788 static uintptr_t
1789 make_verneed(Ofl_desc *ofl)
1790 {
1791 	Shdr		*shdr;
1792 	Elf_Data	*data;
1793 	Is_desc		*isec;
1794 	size_t		size = ofl->ofl_verneedsz;
1795 
1796 	/*
1797 	 * Allocate and initialize the Elf_Data structure.
1798 	 */
1799 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1800 		return (S_ERROR);
1801 	data->d_type = ELF_T_BYTE;
1802 	data->d_size = size;
1803 	data->d_align = M_WORD_ALIGN;
1804 	data->d_version = ofl->ofl_dehdr->e_version;
1805 
1806 	/*
1807 	 * Allocate and initialize the Shdr structure.
1808 	 */
1809 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1810 		return (S_ERROR);
1811 	shdr->sh_type = (Word)SHT_SUNW_verneed;
1812 	shdr->sh_flags = SHF_ALLOC;
1813 	shdr->sh_size = (Xword)size;
1814 	shdr->sh_addralign = M_WORD_ALIGN;
1815 
1816 	/*
1817 	 * Allocate and initialize the Is_desc structure.
1818 	 */
1819 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1820 		return (S_ERROR);
1821 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWVERSION);
1822 	isec->is_shdr = shdr;
1823 	isec->is_indata = data;
1824 
1825 	ofl->ofl_osverneed = ld_place_section(ofl, isec, M_ID_VERSION, 0);
1826 	return ((uintptr_t)ofl->ofl_osverneed);
1827 }
1828 
1829 /*
1830  * Generate a version definition section.
1831  *
1832  *  o	the SHT_SUNW_verdef section defines the versions that exist within this
1833  *	image.
1834  */
1835 static uintptr_t
1836 make_verdef(Ofl_desc *ofl)
1837 {
1838 	Shdr		*shdr;
1839 	Elf_Data	*data;
1840 	Is_desc		*isec;
1841 	Ver_desc	*vdp;
1842 	size_t		size;
1843 
1844 	/*
1845 	 * Reserve a string table entry for the base version dependency (other
1846 	 * dependencies have symbol representations, which will already be
1847 	 * accounted for during symbol processing).
1848 	 */
1849 	vdp = (Ver_desc *)ofl->ofl_verdesc.head->data;
1850 	size = strlen(vdp->vd_name) + 1;
1851 
1852 	if (ofl->ofl_flags & FLG_OF_DYNAMIC) {
1853 		if (st_insert(ofl->ofl_dynstrtab, vdp->vd_name) == -1)
1854 			return (S_ERROR);
1855 	} else {
1856 		if (st_insert(ofl->ofl_strtab, vdp->vd_name) == -1)
1857 			return (S_ERROR);
1858 	}
1859 
1860 	/*
1861 	 * During version processing we calculated the total number of entries.
1862 	 * Allocate and initialize the Elf_Data structure.
1863 	 */
1864 	size = ofl->ofl_verdefsz;
1865 
1866 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1867 		return (S_ERROR);
1868 	data->d_type = ELF_T_BYTE;
1869 	data->d_size = size;
1870 	data->d_align = M_WORD_ALIGN;
1871 	data->d_version = ofl->ofl_dehdr->e_version;
1872 
1873 	/*
1874 	 * Allocate and initialize the Shdr structure.
1875 	 */
1876 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1877 		return (S_ERROR);
1878 	shdr->sh_type = (Word)SHT_SUNW_verdef;
1879 	shdr->sh_flags = SHF_ALLOC;
1880 	shdr->sh_size = (Xword)size;
1881 	shdr->sh_addralign = M_WORD_ALIGN;
1882 
1883 	/*
1884 	 * Allocate and initialize the Is_desc structure.
1885 	 */
1886 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1887 		return (S_ERROR);
1888 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWVERSION);
1889 	isec->is_shdr = shdr;
1890 	isec->is_indata = data;
1891 
1892 	ofl->ofl_osverdef = ld_place_section(ofl, isec, M_ID_VERSION, 0);
1893 	return ((uintptr_t)ofl->ofl_osverdef);
1894 }
1895 
1896 /*
1897  * Common function used to build both the SHT_SUNW_versym
1898  * section and the SHT_SUNW_syminfo section.  Each of these sections
1899  * provides additional symbol information.
1900  */
1901 static Os_desc *
1902 make_sym_sec(Ofl_desc *ofl, const char *sectname, Word entsize,
1903     Word stype, int ident)
1904 {
1905 	Shdr		*shdr;
1906 	Elf_Data	*data;
1907 	Is_desc		*isec;
1908 
1909 	/*
1910 	 * Allocate and initialize the Elf_Data structures for the symbol index
1911 	 * array.
1912 	 */
1913 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1914 		return ((Os_desc *)S_ERROR);
1915 	data->d_type = ELF_T_BYTE;
1916 	data->d_align = M_WORD_ALIGN;
1917 	data->d_version = ofl->ofl_dehdr->e_version;
1918 
1919 	/*
1920 	 * Allocate and initialize the Shdr structure.
1921 	 */
1922 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1923 		return ((Os_desc *)S_ERROR);
1924 	shdr->sh_type = (Word)stype;
1925 	shdr->sh_flags = SHF_ALLOC;
1926 	shdr->sh_addralign = M_WORD_ALIGN;
1927 	shdr->sh_entsize = entsize;
1928 
1929 	if (stype == SHT_SUNW_syminfo) {
1930 		/*
1931 		 * The sh_info field of the SHT_*_syminfo section points
1932 		 * to the header index of the associated .dynamic section.
1933 		 */
1934 		shdr->sh_flags |= SHF_INFO_LINK;
1935 	}
1936 
1937 	/*
1938 	 * Allocate and initialize the Is_desc structure.
1939 	 */
1940 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1941 		return ((Os_desc *)S_ERROR);
1942 	isec->is_name = sectname;
1943 	isec->is_shdr = shdr;
1944 	isec->is_indata = data;
1945 
1946 	return (ld_place_section(ofl, isec, ident, 0));
1947 }
1948 
1949 /*
1950  * Build a .sunwbss section for allocation of tentative definitions.
1951  */
1952 uintptr_t
1953 ld_make_sunwbss(Ofl_desc *ofl, size_t size, Xword align)
1954 {
1955 	Shdr		*shdr;
1956 	Elf_Data	*data;
1957 	Is_desc		*isec;
1958 
1959 	/*
1960 	 * Allocate and initialize the Elf_Data structure.
1961 	 */
1962 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
1963 		return (S_ERROR);
1964 	data->d_type = ELF_T_BYTE;
1965 	data->d_size = size;
1966 	data->d_align = align;
1967 	data->d_version = ofl->ofl_dehdr->e_version;
1968 
1969 	/*
1970 	 * Allocate and initialize the Shdr structure.
1971 	 */
1972 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
1973 		return (S_ERROR);
1974 	shdr->sh_type = SHT_NOBITS;
1975 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
1976 	shdr->sh_size = (Xword)size;
1977 	shdr->sh_addralign = align;
1978 
1979 	/*
1980 	 * Allocate and initialize the Is_desc structure.
1981 	 */
1982 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
1983 		return (S_ERROR);
1984 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWBSS);
1985 	isec->is_shdr = shdr;
1986 	isec->is_indata = data;
1987 
1988 	/*
1989 	 * Retain this .sunwbss input section as this will be where global
1990 	 * symbol references are added.
1991 	 */
1992 	ofl->ofl_issunwbss = isec;
1993 	if (ld_place_section(ofl, isec, 0, 0) == (Os_desc *)S_ERROR)
1994 		return (S_ERROR);
1995 
1996 	return (1);
1997 }
1998 
1999 /*
2000  * This routine is called when -z nopartial is in effect.
2001  */
2002 uintptr_t
2003 ld_make_sunwdata(Ofl_desc *ofl, size_t size, Xword align)
2004 {
2005 	Shdr		*shdr;
2006 	Elf_Data	*data;
2007 	Is_desc		*isec;
2008 	Os_desc		*osp;
2009 
2010 	/*
2011 	 * Allocate and initialize the Elf_Data structure.
2012 	 */
2013 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
2014 		return (S_ERROR);
2015 	data->d_type = ELF_T_BYTE;
2016 	data->d_size = size;
2017 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2018 		return (S_ERROR);
2019 	data->d_align = (size_t)M_WORD_ALIGN;
2020 	data->d_version = ofl->ofl_dehdr->e_version;
2021 
2022 	/*
2023 	 * Allocate and initialize the Shdr structure.
2024 	 */
2025 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
2026 		return (S_ERROR);
2027 	shdr->sh_type = SHT_PROGBITS;
2028 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
2029 	shdr->sh_size = (Xword)size;
2030 	if (align == 0)
2031 		shdr->sh_addralign = M_WORD_ALIGN;
2032 	else
2033 		shdr->sh_addralign = align;
2034 
2035 	/*
2036 	 * Allocate and initialize the Is_desc structure.
2037 	 */
2038 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
2039 		return (S_ERROR);
2040 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWDATA1);
2041 	isec->is_shdr = shdr;
2042 	isec->is_indata = data;
2043 
2044 	/*
2045 	 * Retain this .sunwdata1 input section as this will
2046 	 * be where global
2047 	 * symbol references are added.
2048 	 */
2049 	ofl->ofl_issunwdata1 = isec;
2050 	if ((osp = ld_place_section(ofl, isec, M_ID_DATA, 0)) ==
2051 	    (Os_desc *)S_ERROR)
2052 		return (S_ERROR);
2053 
2054 	if (!(osp->os_flags & FLG_OS_OUTREL)) {
2055 		ofl->ofl_dynshdrcnt++;
2056 		osp->os_flags |= FLG_OS_OUTREL;
2057 	}
2058 	return (1);
2059 }
2060 
2061 /*
2062  * Make .sunwmove section
2063  */
2064 uintptr_t
2065 ld_make_sunwmove(Ofl_desc *ofl, int mv_nums)
2066 {
2067 	Shdr		*shdr;
2068 	Elf_Data	*data;
2069 	Is_desc		*isec;
2070 	size_t		size;
2071 	Listnode	*lnp1;
2072 	Psym_info	*psym;
2073 	int 		cnt = 1;
2074 
2075 	/*
2076 	 * Generate the move input sections and output sections
2077 	 */
2078 	size = mv_nums * sizeof (Move);
2079 
2080 	/*
2081 	 * Allocate and initialize the Elf_Data structure.
2082 	 */
2083 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
2084 		return (S_ERROR);
2085 	data->d_type = ELF_T_BYTE;
2086 	if ((data->d_buf = libld_calloc(size, 1)) == 0)
2087 		return (S_ERROR);
2088 	data->d_size = size;
2089 	data->d_align = sizeof (Lword);
2090 	data->d_version = ofl->ofl_dehdr->e_version;
2091 
2092 	/*
2093 	 * Allocate and initialize the Shdr structure.
2094 	 */
2095 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
2096 		return (S_ERROR);
2097 	shdr->sh_link = 0;
2098 	shdr->sh_info = 0;
2099 	shdr->sh_type = SHT_SUNW_move;
2100 	shdr->sh_size = (Xword)size;
2101 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
2102 	shdr->sh_addralign = sizeof (Lword);
2103 	shdr->sh_entsize = sizeof (Move);
2104 
2105 	/*
2106 	 * Allocate and initialize the Is_desc structure.
2107 	 */
2108 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == 0)
2109 		return (S_ERROR);
2110 	isec->is_name = MSG_ORIG(MSG_SCN_SUNWMOVE);
2111 	isec->is_shdr = shdr;
2112 	isec->is_indata = data;
2113 	isec->is_file = 0;
2114 
2115 	/*
2116 	 * Copy move entries
2117 	 */
2118 	for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) {
2119 		Listnode *	lnp2;
2120 		Mv_itm *	mvitm;
2121 
2122 		if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN)
2123 			continue;
2124 		for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvitm)) {
2125 			if ((mvitm->mv_flag & FLG_MV_OUTSECT) == 0)
2126 				continue;
2127 			mvitm->mv_oidx = cnt;
2128 			cnt++;
2129 		}
2130 	}
2131 	if ((ofl->ofl_osmove = ld_place_section(ofl, isec, 0, 0)) ==
2132 	    (Os_desc *)S_ERROR)
2133 		return (S_ERROR);
2134 
2135 	return (1);
2136 }
2137 
2138 
2139 /*
2140  * The following sections are built after all input file processing and symbol
2141  * validation has been carried out.  The order is important (because the
2142  * addition of a section adds a new symbol there is a chicken and egg problem
2143  * of maintaining the appropriate counts).  By maintaining a known order the
2144  * individual routines can compensate for later, known, additions.
2145  */
2146 uintptr_t
2147 ld_make_sections(Ofl_desc *ofl)
2148 {
2149 	Word		flags = ofl->ofl_flags;
2150 	Listnode	*lnp1;
2151 	Sg_desc		*sgp;
2152 
2153 	/*
2154 	 * Generate any special sections.
2155 	 */
2156 	if (flags & FLG_OF_ADDVERS)
2157 		if (make_comment(ofl) == S_ERROR)
2158 			return (S_ERROR);
2159 
2160 	if (make_interp(ofl) == S_ERROR)
2161 		return (S_ERROR);
2162 
2163 	if (make_cap(ofl) == S_ERROR)
2164 		return (S_ERROR);
2165 
2166 	if (make_array(ofl, SHT_INIT_ARRAY, MSG_ORIG(MSG_SCN_INITARRAY),
2167 	    &ofl->ofl_initarray) == S_ERROR)
2168 		return (S_ERROR);
2169 
2170 	if (make_array(ofl, SHT_FINI_ARRAY, MSG_ORIG(MSG_SCN_FINIARRAY),
2171 	    &ofl->ofl_finiarray) == S_ERROR)
2172 		return (S_ERROR);
2173 
2174 	if (make_array(ofl, SHT_PREINIT_ARRAY, MSG_ORIG(MSG_SCN_PREINITARRAY),
2175 	    &ofl->ofl_preiarray) == S_ERROR)
2176 		return (S_ERROR);
2177 
2178 	/*
2179 	 * Make the .plt section.  This occurs after any other relocation
2180 	 * sections are generated (see reloc_init()) to ensure that the
2181 	 * associated relocation section is after all the other relocation
2182 	 * sections.
2183 	 */
2184 	if ((ofl->ofl_pltcnt) || (ofl->ofl_pltpad))
2185 		if (make_plt(ofl) == S_ERROR)
2186 			return (S_ERROR);
2187 
2188 	/*
2189 	 * Determine whether any sections or files are not referenced.  Under
2190 	 * -Dunused a diagnostic for any unused components is generated, under
2191 	 * -zignore the component is removed from the final output.
2192 	 */
2193 	if (DBG_ENABLED || (ofl->ofl_flags1 & FLG_OF1_IGNPRC)) {
2194 		if (ignore_section_processing(ofl) == S_ERROR)
2195 			return (S_ERROR);
2196 	}
2197 
2198 	/*
2199 	 * Add any necessary versioning information.
2200 	 */
2201 	if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED) {
2202 		if (make_verneed(ofl) == S_ERROR)
2203 			return (S_ERROR);
2204 	}
2205 	if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == FLG_OF_VERDEF) {
2206 		if (make_verdef(ofl) == S_ERROR)
2207 			return (S_ERROR);
2208 		if ((ofl->ofl_osversym = make_sym_sec(ofl,
2209 		    MSG_ORIG(MSG_SCN_SUNWVERSYM), sizeof (Versym),
2210 		    SHT_SUNW_versym, M_ID_VERSION)) == (Os_desc*)S_ERROR)
2211 			return (S_ERROR);
2212 	}
2213 
2214 	/*
2215 	 * Create a syminfo section is necessary.
2216 	 */
2217 	if (ofl->ofl_flags & FLG_OF_SYMINFO) {
2218 		if ((ofl->ofl_ossyminfo = make_sym_sec(ofl,
2219 		    MSG_ORIG(MSG_SCN_SUNWSYMINFO), sizeof (Syminfo),
2220 		    SHT_SUNW_syminfo, M_ID_SYMINFO)) == (Os_desc *)S_ERROR)
2221 			return (S_ERROR);
2222 	}
2223 
2224 	if (ofl->ofl_flags1 & FLG_OF1_RELCNT) {
2225 		/*
2226 		 * If -zcombreloc is enabled then all relocations (except for
2227 		 * the PLT's) are coalesced into a single relocation section.
2228 		 */
2229 		if (ofl->ofl_reloccnt) {
2230 			if (make_reloc(ofl, NULL) == S_ERROR)
2231 				return (S_ERROR);
2232 		}
2233 	} else {
2234 		/*
2235 		 * Create the required output relocation sections.  Note, new
2236 		 * sections may be added to the section list that is being
2237 		 * traversed.  These insertions can move the elements of the
2238 		 * Alist such that a section descriptor is re-read.  Recursion
2239 		 * is prevented by maintaining a previous section pointer and
2240 		 * insuring that this pointer isn't re-examined.
2241 		 */
2242 		for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2243 			Os_desc	**ospp, *posp = 0;
2244 			Aliste	off;
2245 
2246 			for (ALIST_TRAVERSE(sgp->sg_osdescs, off, ospp)) {
2247 				Os_desc	*osp = *ospp;
2248 
2249 				if ((osp != posp) && osp->os_szoutrels &&
2250 				    (osp != ofl->ofl_osplt)) {
2251 					if (make_reloc(ofl, osp) == S_ERROR)
2252 						return (S_ERROR);
2253 				}
2254 				posp = osp;
2255 			}
2256 		}
2257 
2258 		/*
2259 		 * If we're not building a combined relocation section, then
2260 		 * build a .rel[a] section as required.
2261 		 */
2262 		if (ofl->ofl_relocrelsz) {
2263 			if (make_reloc(ofl, NULL) == S_ERROR)
2264 				return (S_ERROR);
2265 		}
2266 	}
2267 
2268 	/*
2269 	 * The PLT relocations are always in their own section, and we try to
2270 	 * keep them at the end of the PLT table.  We do this to keep the hot
2271 	 * "data" PLT's at the head of the table nearer the .dynsym & .hash.
2272 	 */
2273 	if (ofl->ofl_osplt && ofl->ofl_relocpltsz) {
2274 		if (make_reloc(ofl, ofl->ofl_osplt) == S_ERROR)
2275 			return (S_ERROR);
2276 	}
2277 
2278 	/*
2279 	 * Finally build the symbol and section header sections.
2280 	 */
2281 	if (flags & FLG_OF_DYNAMIC) {
2282 		if (make_dynamic(ofl) == S_ERROR)
2283 			return (S_ERROR);
2284 		if (make_dynstr(ofl) == S_ERROR)
2285 			return (S_ERROR);
2286 		/*
2287 		 * There is no use for .hash and .dynsym sections in a
2288 		 * relocatable object.
2289 		 */
2290 		if (!(flags & FLG_OF_RELOBJ)) {
2291 			if (make_hash(ofl) == S_ERROR)
2292 				return (S_ERROR);
2293 			if (make_dynsym(ofl) == S_ERROR)
2294 				return (S_ERROR);
2295 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
2296 			if (make_amd64_unwindhdr(ofl) == S_ERROR)
2297 				return (S_ERROR);
2298 #endif
2299 		}
2300 	}
2301 
2302 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
2303 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
2304 		/*
2305 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2306 		 * for the dynsym.  If so - do it now.
2307 		 */
2308 		if (ofl->ofl_osdynsym &&
2309 		    ((ofl->ofl_shdrcnt + 3) >= SHN_LORESERVE)) {
2310 			if (make_dynsym_shndx(ofl) == S_ERROR)
2311 				return (S_ERROR);
2312 		}
2313 
2314 		if (make_strtab(ofl) == S_ERROR)
2315 			return (S_ERROR);
2316 		if (make_symtab(ofl) == S_ERROR)
2317 			return (S_ERROR);
2318 	} else {
2319 		/*
2320 		 * Do we need to make a SHT_SYMTAB_SHNDX section
2321 		 * for the dynsym.  If so - do it now.
2322 		 */
2323 		if (ofl->ofl_osdynsym &&
2324 		    ((ofl->ofl_shdrcnt + 1) >= SHN_LORESERVE)) {
2325 			if (make_dynsym_shndx(ofl) == S_ERROR)
2326 				return (S_ERROR);
2327 		}
2328 	}
2329 
2330 	if (make_shstrtab(ofl) == S_ERROR)
2331 		return (S_ERROR);
2332 
2333 	/*
2334 	 * Now that we've created all of our sections adjust the size
2335 	 * of SHT_SUNW_versym & SHT_SUNW_syminfo which are dependent on
2336 	 * the symbol table sizes.
2337 	 */
2338 	if (ofl->ofl_osversym || ofl->ofl_ossyminfo) {
2339 		Shdr *		shdr;
2340 		Is_desc *	isec;
2341 		Elf_Data *	data;
2342 		size_t		size;
2343 		ulong_t		cnt;
2344 
2345 		if ((flags & FLG_OF_RELOBJ) || (flags & FLG_OF_STATIC))
2346 			isec = (Is_desc *)ofl->ofl_ossymtab->
2347 				os_isdescs.head->data;
2348 		else
2349 			isec = (Is_desc *)ofl->ofl_osdynsym->
2350 				os_isdescs.head->data;
2351 		cnt = isec->is_shdr->sh_size / isec->is_shdr->sh_entsize;
2352 
2353 		if (ofl->ofl_osversym) {
2354 			isec = (Is_desc *)ofl->ofl_osversym->os_isdescs.
2355 				head->data;
2356 			data = isec->is_indata;
2357 			shdr = ofl->ofl_osversym->os_shdr;
2358 			size = cnt * shdr->sh_entsize;
2359 			shdr->sh_size = (Xword)size;
2360 			data->d_size = size;
2361 		}
2362 		if (ofl->ofl_ossyminfo) {
2363 			isec = (Is_desc *)ofl->ofl_ossyminfo->os_isdescs.
2364 				head->data;
2365 			data = isec->is_indata;
2366 			shdr = ofl->ofl_ossyminfo->os_shdr;
2367 			size = cnt * shdr->sh_entsize;
2368 			shdr->sh_size = (Xword)size;
2369 			data->d_size = size;
2370 		}
2371 	}
2372 
2373 	return (1);
2374 }
2375 
2376 /*
2377  * Build an additional data section - used to back OBJT symbol definitions
2378  * added with a mapfile.
2379  */
2380 Is_desc *
2381 ld_make_data(Ofl_desc *ofl, size_t size)
2382 {
2383 	Shdr		*shdr;
2384 	Elf_Data	*data;
2385 	Is_desc		*isec;
2386 
2387 	/*
2388 	 * Allocate and initialize the Elf_Data structure.
2389 	 */
2390 	if ((data = libld_calloc(sizeof (Elf_Data), 1)) == 0)
2391 		return ((Is_desc *)S_ERROR);
2392 	data->d_type = ELF_T_BYTE;
2393 	data->d_size = size;
2394 	data->d_align = M_WORD_ALIGN;
2395 	data->d_version = ofl->ofl_dehdr->e_version;
2396 
2397 	/*
2398 	 * Allocate and initialize the Shdr structure.
2399 	 */
2400 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
2401 		return ((Is_desc *)S_ERROR);
2402 	shdr->sh_type = SHT_PROGBITS;
2403 	shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
2404 	shdr->sh_size = (Xword)size;
2405 	shdr->sh_addralign = M_WORD_ALIGN;
2406 
2407 	/*
2408 	 * Allocate and initialize the Is_desc structure.
2409 	 */
2410 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == (Is_desc *)0)
2411 		return ((Is_desc *)S_ERROR);
2412 	isec->is_name = MSG_ORIG(MSG_SCN_DATA);
2413 	isec->is_shdr = shdr;
2414 	isec->is_indata = data;
2415 
2416 	if (ld_place_section(ofl, isec, M_ID_DATA, 0) == (Os_desc *)S_ERROR)
2417 		return ((Is_desc *)S_ERROR);
2418 
2419 	return (isec);
2420 }
2421 
2422 /*
2423  * Define a set of templates for generating "void (*)(void)" function
2424  * definitions.
2425  */
2426 #if	defined(i386) || defined(__amd64)
2427 #if	defined(__lint)
2428 static const uchar_t ret_template[] = { 0 };
2429 #else	/* __lint */
2430 #if	defined(_ELF64)
2431 #define	ret_template	ret64_template
2432 #else
2433 #define	ret_template	ret32_template
2434 #endif
2435 
2436 static const uchar_t ret32_template[] = {
2437 /* 0x00 */	0xc3				/* ret */
2438 };
2439 
2440 static const uchar_t ret64_template[] = {
2441 /* 0x00 */	0x55,				/* pushq  %rbp */
2442 /* 0x01 */	0x48, 0x8b, 0xec,		/* movq   %rsp,%rbp */
2443 /* 0x04 */	0x48, 0x8b, 0xe5,		/* movq   %rbp,%rsp */
2444 /* 0x07 */	0x5d,				/* popq   %rbp */
2445 /* 0x08 */	0xc3				/* ret */
2446 };
2447 #endif	/* __lint */
2448 
2449 #elif	defined(sparc) || defined(__sparcv9)
2450 static const uchar_t ret_template[] = {
2451 /* 0x00 */	0x81, 0xc3, 0xe0, 0x08,		/* retl */
2452 /* 0x04 */	0x01, 0x00, 0x00, 0x00		/* nop */
2453 };
2454 #else
2455 #error	unsupported architecture!
2456 #endif
2457 
2458 /*
2459  * Build an additional text section - used to back FUNC symbol definitions
2460  * added with a mapfile.
2461  */
2462 Is_desc *
2463 ld_make_text(Ofl_desc *ofl, size_t size)
2464 {
2465 	Shdr		*shdr;
2466 	Elf_Data	*data;
2467 	Is_desc		*isec;
2468 
2469 	/*
2470 	 * Insure the size is sufficient to contain the minimum return
2471 	 * instruction.
2472 	 */
2473 	if (size < sizeof (ret_template))
2474 		size = sizeof (ret_template);
2475 
2476 	/*
2477 	 * Allocate and initialize the Elf_Data structure.  Fill the buffer
2478 	 * with the appropriate return instruction.
2479 	 */
2480 	if (((data = libld_calloc(sizeof (Elf_Data), 1)) == 0) ||
2481 	    ((data->d_buf = libld_calloc(size, 1)) == 0))
2482 		return ((Is_desc *)S_ERROR);
2483 	data->d_type = ELF_T_BYTE;
2484 	data->d_size = size;
2485 	data->d_align = M_WORD_ALIGN;
2486 	data->d_version = ofl->ofl_dehdr->e_version;
2487 
2488 	(void) memcpy(data->d_buf, ret_template, sizeof (ret_template));
2489 
2490 	/*
2491 	 * Allocate and initialize the Shdr structure.
2492 	 */
2493 	if ((shdr = libld_calloc(sizeof (Shdr), 1)) == 0)
2494 		return ((Is_desc *)S_ERROR);
2495 	shdr->sh_type = SHT_PROGBITS;
2496 	shdr->sh_flags = SHF_ALLOC | SHF_EXECINSTR;
2497 	shdr->sh_size = (Xword)size;
2498 	shdr->sh_addralign = M_WORD_ALIGN;
2499 
2500 	/*
2501 	 * Allocate and initialize the Is_desc structure.
2502 	 */
2503 	if ((isec = libld_calloc(1, sizeof (Is_desc))) == (Is_desc *)0)
2504 		return ((Is_desc *)S_ERROR);
2505 	isec->is_name = MSG_ORIG(MSG_SCN_TEXT);
2506 	isec->is_shdr = shdr;
2507 	isec->is_indata = data;
2508 
2509 	if (ld_place_section(ofl, isec, M_ID_TEXT, 0) == (Os_desc *)S_ERROR)
2510 		return ((Is_desc *)S_ERROR);
2511 
2512 	return (isec);
2513 }
2514