xref: /illumos-gate/usr/src/cmd/sgs/libld/common/update.c (revision 9ef500b0)
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 (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27  */
28 
29 /*
30  * Update the new output file image, perform virtual address, offset and
31  * displacement calculations on the program headers and sections headers,
32  * and generate any new output section information.
33  */
34 
35 #define	ELF_TARGET_AMD64
36 
37 #include	<stdio.h>
38 #include	<string.h>
39 #include	<unistd.h>
40 #include	<debug.h>
41 #include	"msg.h"
42 #include	"_libld.h"
43 
44 /*
45  * Comparison routine used by qsort() for sorting of the global symbol list
46  * based off of the hashbuckets the symbol will eventually be deposited in.
47  */
48 static int
sym_hash_compare(Sym_s_list * s1,Sym_s_list * s2)49 sym_hash_compare(Sym_s_list * s1, Sym_s_list * s2)
50 {
51 	return (s1->sl_hval - s2->sl_hval);
52 }
53 
54 /*
55  * Comparison routine used by qsort() for sorting of dyn[sym|tls]sort section
56  * indices based on the address of the symbols they reference. The
57  * use of the global dynsort_compare_syms variable is needed because
58  * we need to examine the symbols the indices reference. It is safe, because
59  * the linker is single threaded.
60  */
61 static Sym *dynsort_compare_syms;
62 
63 static int
dynsort_compare(const void * idx1,const void * idx2)64 dynsort_compare(const void *idx1, const void *idx2)
65 {
66 	Sym *s1 = dynsort_compare_syms + *((const Word *) idx1);
67 	Sym *s2 = dynsort_compare_syms + *((const Word *) idx2);
68 
69 	/*
70 	 * Note: the logical computation for this is
71 	 *	(st_value1 - st_value2)
72 	 * However, that is only correct if the address type is smaller
73 	 * than a pointer. Writing it this way makes it immune to the
74 	 * class (32 or 64-bit) of the linker.
75 	 */
76 	return ((s1->st_value < s2->st_value) ? -1 :
77 	    (s1->st_value > s2->st_value));
78 }
79 
80 /*
81  * Scan the sorted symbols, and issue warnings if there are any duplicate
82  * values in the list. We only do this if -zverbose is set, or we are
83  * running with LD_DEBUG defined
84  *
85  * entry:
86  *	ofl - Output file descriptor
87  *	ldynsym - Pointer to start of .SUNW_ldynsym section that the
88  *		sort section indexes reference.
89  *	symsort - Pointer to start of .SUNW_dynsymsort or .SUNW_dyntlssort
90  *		section.
91  *	n - # of indices in symsort array
92  *	secname - Name of the symsort section.
93  *
94  * exit:
95  *	If the symsort section contains indexes to more than one
96  *	symbol with the same address value, a warning is issued.
97  */
98 static void
dynsort_dupwarn(Ofl_desc * ofl,Sym * ldynsym,const char * str,Word * symsort,Word n,const char * secname)99 dynsort_dupwarn(Ofl_desc *ofl, Sym *ldynsym, const char *str,
100     Word *symsort, Word n, const char *secname)
101 {
102 	int zverbose = (ofl->ofl_flags & FLG_OF_VERBOSE) != 0;
103 	Word ndx, cmp_ndx;
104 	Addr addr, cmp_addr;
105 
106 	/* Nothing to do if -zverbose or LD_DEBUG are not active */
107 	if (!(zverbose || DBG_ENABLED))
108 		return;
109 
110 	cmp_ndx = 0;
111 	cmp_addr = ldynsym[symsort[cmp_ndx]].st_value;
112 	for (ndx = 1; ndx < n; ndx++) {
113 		addr = ldynsym[symsort[ndx]].st_value;
114 		if (cmp_addr == addr) {
115 			if (zverbose)
116 				ld_eprintf(ofl, ERR_WARNING,
117 				    MSG_INTL(MSG_SYM_DUPSORTADDR), secname,
118 				    str + ldynsym[symsort[cmp_ndx]].st_name,
119 				    str + ldynsym[symsort[ndx]].st_name,
120 				    EC_ADDR(addr));
121 			DBG_CALL(Dbg_syms_dup_sort_addr(ofl->ofl_lml, secname,
122 			    str + ldynsym[symsort[cmp_ndx]].st_name,
123 			    str + ldynsym[symsort[ndx]].st_name,
124 			    EC_ADDR(addr)));
125 		} else {	/* Not a dup. Move reference up */
126 			cmp_ndx = ndx;
127 			cmp_addr = addr;
128 		}
129 	}
130 }
131 
132 static inline Boolean
ass_enabled(Ass_desc * ma,uint_t ass)133 ass_enabled(Ass_desc *ma, uint_t ass)
134 {
135 	return ((ma->ass_enabled & ass) != 0);
136 }
137 
138 /*
139  * Build and update any output symbol tables.  Here we work on all the symbol
140  * tables at once to reduce the duplication of symbol and string manipulation.
141  * Symbols and their associated strings are copied from the read-only input
142  * file images to the output image and their values and index's updated in the
143  * output image.
144  */
145 static Addr
update_osym(Ofl_desc * ofl)146 update_osym(Ofl_desc *ofl)
147 {
148 	/*
149 	 * There are several places in this function where we wish
150 	 * to insert a symbol index to the combined .SUNW_ldynsym/.dynsym
151 	 * symbol table into one of the two sort sections (.SUNW_dynsymsort
152 	 * or .SUNW_dyntlssort), if that symbol has the right attributes.
153 	 * This macro is used to generate the necessary code from a single
154 	 * specification.
155 	 *
156 	 * entry:
157 	 *	_sdp, _sym, _type - As per DYNSORT_COUNT. See _libld.h
158 	 *	_sym_ndx - Index that _sym will have in the combined
159 	 *		.SUNW_ldynsym/.dynsym symbol table.
160 	 */
161 #define	ADD_TO_DYNSORT(_sdp, _sym, _type, _sym_ndx) \
162 	{ \
163 		Word *_dynsort_arr, *_dynsort_ndx; \
164 		\
165 		if (dynsymsort_symtype[_type]) { \
166 			_dynsort_arr = dynsymsort; \
167 			_dynsort_ndx = &dynsymsort_ndx; \
168 		} else if (_type == STT_TLS) { \
169 			_dynsort_arr = dyntlssort; \
170 			_dynsort_ndx = &dyntlssort_ndx; \
171 		} else { \
172 			_dynsort_arr = NULL; \
173 		} \
174 		if ((_dynsort_arr != NULL) && DYNSORT_TEST_ATTR(_sdp, _sym)) \
175 		    _dynsort_arr[(*_dynsort_ndx)++] = _sym_ndx; \
176 	}
177 
178 	Sym_desc	*sdp;
179 	Sym_avlnode	*sav;
180 	Sg_desc		*sgp, *tsgp = NULL, *dsgp = NULL, *esgp = NULL;
181 	Os_desc		*osp, *iosp = NULL, *fosp = NULL;
182 	Is_desc		*isc;
183 	Ifl_desc	*ifl;
184 	Word		bssndx, etext_ndx, edata_ndx = 0, end_ndx, start_ndx;
185 	Word		end_abs = 0, etext_abs = 0, edata_abs;
186 	Word		tlsbssndx = 0, parexpnndx;
187 #if	defined(_ELF64)
188 	Word		lbssndx = 0;
189 	Addr		lbssaddr = 0;
190 #endif
191 	Addr		bssaddr, etext = 0, edata = 0, end = 0, start = 0;
192 	Addr		tlsbssaddr = 0;
193 	Addr		parexpnbase, parexpnaddr;
194 	int		start_set = 0;
195 	Sym		_sym = {0}, *sym, *symtab = NULL;
196 	Sym		*dynsym = NULL, *ldynsym = NULL;
197 	Word		symtab_ndx = 0;		/* index into .symtab */
198 	Word		symtab_gbl_bndx;	/* .symtab ndx 1st global */
199 	Word		ldynsym_ndx = 0;	/* index into .SUNW_ldynsym */
200 	Word		dynsym_ndx = 0;		/* index into .dynsym */
201 	Word		scopesym_ndx = 0;	/* index into scoped symbols */
202 	Word		scopesym_bndx = 0;	/* .symtab ndx 1st scoped sym */
203 	Word		ldynscopesym_ndx = 0;	/* index to ldynsym scoped */
204 						/*	symbols */
205 	Word		*dynsymsort = NULL;	/* SUNW_dynsymsort index */
206 						/*	vector */
207 	Word		*dyntlssort = NULL;	/* SUNW_dyntlssort index */
208 						/*	vector */
209 	Word		dynsymsort_ndx;		/* index dynsymsort array */
210 	Word		dyntlssort_ndx;		/* index dyntlssort array */
211 	Word		*symndx;		/* symbol index (for */
212 						/*	relocation use) */
213 	Word		*symshndx = NULL;	/* .symtab_shndx table */
214 	Word		*dynshndx = NULL;	/* .dynsym_shndx table */
215 	Word		*ldynshndx = NULL;	/* .SUNW_ldynsym_shndx table */
216 	Word		ldynsym_cnt = 0;	/* number of items in */
217 						/*	.SUNW_ldynsym */
218 	Str_tbl		*shstrtab;
219 	Str_tbl		*strtab;
220 	Str_tbl		*dynstr;
221 	Word		*hashtab;	/* hash table pointer */
222 	Word		*hashbkt;	/* hash table bucket pointer */
223 	Word		*hashchain;	/* hash table chain pointer */
224 	Wk_desc		*wkp;
225 	Alist		*weak = NULL;
226 	ofl_flag_t	flags = ofl->ofl_flags;
227 	Versym		*versym;
228 	Gottable	*gottable;	/* used for display got debugging */
229 					/*	information */
230 	Syminfo		*syminfo;
231 	Sym_s_list	*sorted_syms;	/* table to hold sorted symbols */
232 	Word		ssndx;		/* global index into sorted_syms */
233 	Word		scndx;		/* scoped index into sorted_syms */
234 	size_t		stoff;		/* string offset */
235 	Aliste		idx1;
236 
237 	/*
238 	 * Initialize pointers to the symbol table entries and the symbol
239 	 * table strings.  Skip the first symbol entry and the first string
240 	 * table byte.  Note that if we are not generating any output symbol
241 	 * tables we must still generate and update internal copies so
242 	 * that the relocation phase has the correct information.
243 	 */
244 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
245 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
246 		symtab = (Sym *)ofl->ofl_ossymtab->os_outdata->d_buf;
247 		symtab[symtab_ndx++] = _sym;
248 		if (ofl->ofl_ossymshndx)
249 			symshndx =
250 			    (Word *)ofl->ofl_ossymshndx->os_outdata->d_buf;
251 	}
252 	if (OFL_ALLOW_DYNSYM(ofl)) {
253 		dynsym = (Sym *)ofl->ofl_osdynsym->os_outdata->d_buf;
254 		dynsym[dynsym_ndx++] = _sym;
255 		/*
256 		 * If we are also constructing a .SUNW_ldynsym section
257 		 * to contain local function symbols, then set it up too.
258 		 */
259 		if (ofl->ofl_osldynsym) {
260 			ldynsym = (Sym *)ofl->ofl_osldynsym->os_outdata->d_buf;
261 			ldynsym[ldynsym_ndx++] = _sym;
262 			ldynsym_cnt = 1 + ofl->ofl_dynlocscnt +
263 			    ofl->ofl_dynscopecnt;
264 
265 			/*
266 			 * If there is a SUNW_ldynsym, then there may also
267 			 * be a .SUNW_dynsymsort and/or .SUNW_dyntlssort
268 			 * sections, used to collect indices of function
269 			 * and data symbols sorted by address order.
270 			 */
271 			if (ofl->ofl_osdynsymsort) {	/* .SUNW_dynsymsort */
272 				dynsymsort = (Word *)
273 				    ofl->ofl_osdynsymsort->os_outdata->d_buf;
274 				dynsymsort_ndx = 0;
275 			}
276 			if (ofl->ofl_osdyntlssort) {	/* .SUNW_dyntlssort */
277 				dyntlssort = (Word *)
278 				    ofl->ofl_osdyntlssort->os_outdata->d_buf;
279 				dyntlssort_ndx = 0;
280 			}
281 		}
282 
283 		/*
284 		 * Initialize the hash table.
285 		 */
286 		hashtab = (Word *)(ofl->ofl_oshash->os_outdata->d_buf);
287 		hashbkt = &hashtab[2];
288 		hashchain = &hashtab[2 + ofl->ofl_hashbkts];
289 		hashtab[0] = ofl->ofl_hashbkts;
290 		hashtab[1] = DYNSYM_ALL_CNT(ofl);
291 		if (ofl->ofl_osdynshndx)
292 			dynshndx =
293 			    (Word *)ofl->ofl_osdynshndx->os_outdata->d_buf;
294 		if (ofl->ofl_osldynshndx)
295 			ldynshndx =
296 			    (Word *)ofl->ofl_osldynshndx->os_outdata->d_buf;
297 	}
298 
299 	/*
300 	 * symndx is the symbol index to be used for relocation processing.  It
301 	 * points to the relevant symtab's (.dynsym or .symtab) symbol ndx.
302 	 */
303 	if (dynsym)
304 		symndx = &dynsym_ndx;
305 	else
306 		symndx = &symtab_ndx;
307 
308 	/*
309 	 * If we have version definitions initialize the version symbol index
310 	 * table.  There is one entry for each symbol which contains the symbols
311 	 * version index.
312 	 */
313 	if (!(flags & FLG_OF_NOVERSEC) &&
314 	    (flags & (FLG_OF_VERNEED | FLG_OF_VERDEF))) {
315 		versym = (Versym *)ofl->ofl_osversym->os_outdata->d_buf;
316 		versym[0] = 0;
317 	} else
318 		versym = NULL;
319 
320 	/*
321 	 * If syminfo section exists be prepared to fill it in.
322 	 */
323 	if (ofl->ofl_ossyminfo) {
324 		syminfo = ofl->ofl_ossyminfo->os_outdata->d_buf;
325 		syminfo[0].si_flags = SYMINFO_CURRENT;
326 	} else
327 		syminfo = NULL;
328 
329 	/*
330 	 * Setup our string tables.
331 	 */
332 	shstrtab = ofl->ofl_shdrsttab;
333 	strtab = ofl->ofl_strtab;
334 	dynstr = ofl->ofl_dynstrtab;
335 
336 	DBG_CALL(Dbg_syms_sec_title(ofl->ofl_lml));
337 
338 	/*
339 	 * Put output file name to the first .symtab and .SUNW_ldynsym symbol.
340 	 */
341 	if (symtab) {
342 		(void) st_setstring(strtab, ofl->ofl_name, &stoff);
343 		sym = &symtab[symtab_ndx++];
344 		/* LINTED */
345 		sym->st_name = stoff;
346 		sym->st_value = 0;
347 		sym->st_size = 0;
348 		sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE);
349 		sym->st_other = 0;
350 		sym->st_shndx = SHN_ABS;
351 
352 		if (versym && !dynsym)
353 			versym[1] = 0;
354 	}
355 	if (ldynsym) {
356 		(void) st_setstring(dynstr, ofl->ofl_name, &stoff);
357 		sym = &ldynsym[ldynsym_ndx];
358 		/* LINTED */
359 		sym->st_name = stoff;
360 		sym->st_value = 0;
361 		sym->st_size = 0;
362 		sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE);
363 		sym->st_other = 0;
364 		sym->st_shndx = SHN_ABS;
365 
366 		/* Scoped symbols get filled in global loop below */
367 		ldynscopesym_ndx = ldynsym_ndx + 1;
368 		ldynsym_ndx += ofl->ofl_dynscopecnt;
369 	}
370 
371 	/*
372 	 * If we are to display GOT summary information, then allocate
373 	 * the buffer to 'cache' the GOT symbols into now.
374 	 */
375 	if (DBG_ENABLED) {
376 		if ((ofl->ofl_gottable = gottable =
377 		    libld_calloc(ofl->ofl_gotcnt, sizeof (Gottable))) == NULL)
378 			return ((Addr)S_ERROR);
379 	}
380 
381 	/*
382 	 * Traverse the program headers.  Determine the last executable segment
383 	 * and the last data segment so that we can update etext and edata. If
384 	 * we have empty segments (reservations) record them for setting _end.
385 	 */
386 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
387 		Phdr	*phd = &(sgp->sg_phdr);
388 		Os_desc	*osp;
389 		Aliste	idx2;
390 
391 		if (phd->p_type == PT_LOAD) {
392 			if (aplist_nitems(sgp->sg_osdescs) != 0) {
393 				Word	_flags = phd->p_flags & (PF_W | PF_R);
394 
395 				if (_flags == PF_R)
396 					tsgp = sgp;
397 				else if (_flags == (PF_W | PF_R))
398 					dsgp = sgp;
399 			} else if (sgp->sg_flags & FLG_SG_EMPTY)
400 				esgp = sgp;
401 		}
402 
403 		/*
404 		 * Generate a section symbol for each output section.
405 		 */
406 		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
407 			Word	sectndx;
408 
409 			sym = &_sym;
410 			sym->st_value = osp->os_shdr->sh_addr;
411 			sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_SECTION);
412 			/* LINTED */
413 			sectndx = elf_ndxscn(osp->os_scn);
414 
415 			if (symtab) {
416 				if (sectndx >= SHN_LORESERVE) {
417 					symshndx[symtab_ndx] = sectndx;
418 					sym->st_shndx = SHN_XINDEX;
419 				} else {
420 					/* LINTED */
421 					sym->st_shndx = (Half)sectndx;
422 				}
423 				symtab[symtab_ndx++] = *sym;
424 			}
425 
426 			if (dynsym && (osp->os_flags & FLG_OS_OUTREL))
427 				dynsym[dynsym_ndx++] = *sym;
428 
429 			if ((dynsym == NULL) ||
430 			    (osp->os_flags & FLG_OS_OUTREL)) {
431 				if (versym)
432 					versym[*symndx - 1] = 0;
433 				osp->os_identndx = *symndx - 1;
434 				DBG_CALL(Dbg_syms_sec_entry(ofl->ofl_lml,
435 				    osp->os_identndx, sgp, osp));
436 			}
437 
438 			/*
439 			 * Generate the .shstrtab for this section.
440 			 */
441 			(void) st_setstring(shstrtab, osp->os_name, &stoff);
442 			osp->os_shdr->sh_name = (Word)stoff;
443 
444 			/*
445 			 * Find the section index for our special symbols.
446 			 */
447 			if (sgp == tsgp) {
448 				/* LINTED */
449 				etext_ndx = elf_ndxscn(osp->os_scn);
450 			} else if (dsgp == sgp) {
451 				if (osp->os_shdr->sh_type != SHT_NOBITS) {
452 					/* LINTED */
453 					edata_ndx = elf_ndxscn(osp->os_scn);
454 				}
455 			}
456 
457 			if (start_set == 0) {
458 				start = sgp->sg_phdr.p_vaddr;
459 				/* LINTED */
460 				start_ndx = elf_ndxscn(osp->os_scn);
461 				start_set++;
462 			}
463 
464 			/*
465 			 * While we're here, determine whether a .init or .fini
466 			 * section exist.
467 			 */
468 			if ((iosp == NULL) && (strcmp(osp->os_name,
469 			    MSG_ORIG(MSG_SCN_INIT)) == 0))
470 				iosp = osp;
471 			if ((fosp == NULL) && (strcmp(osp->os_name,
472 			    MSG_ORIG(MSG_SCN_FINI)) == 0))
473 				fosp = osp;
474 		}
475 	}
476 
477 	/*
478 	 * Add local register symbols to the .dynsym.  These are required as
479 	 * DT_REGISTER .dynamic entries must have a symbol to reference.
480 	 */
481 	if (ofl->ofl_regsyms && dynsym) {
482 		int	ndx;
483 
484 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
485 			Sym_desc	*rsdp;
486 
487 			if ((rsdp = ofl->ofl_regsyms[ndx]) == NULL)
488 				continue;
489 
490 			if (!SYM_IS_HIDDEN(rsdp) &&
491 			    (ELF_ST_BIND(rsdp->sd_sym->st_info) != STB_LOCAL))
492 				continue;
493 
494 			dynsym[dynsym_ndx] = *(rsdp->sd_sym);
495 			rsdp->sd_symndx = *symndx;
496 
497 			if (dynsym[dynsym_ndx].st_name) {
498 				(void) st_setstring(dynstr, rsdp->sd_name,
499 				    &stoff);
500 				dynsym[dynsym_ndx].st_name = stoff;
501 			}
502 			dynsym_ndx++;
503 		}
504 	}
505 
506 	/*
507 	 * Having traversed all the output segments, warn the user if the
508 	 * traditional text or data segments don't exist.  Otherwise from these
509 	 * segments establish the values for `etext', `edata', `end', `END',
510 	 * and `START'.
511 	 */
512 	if (!(flags & FLG_OF_RELOBJ)) {
513 		Sg_desc	*sgp;
514 
515 		if (tsgp)
516 			etext = tsgp->sg_phdr.p_vaddr + tsgp->sg_phdr.p_filesz;
517 		else {
518 			etext = (Addr)0;
519 			etext_ndx = SHN_ABS;
520 			etext_abs = 1;
521 			if (flags & FLG_OF_VERBOSE)
522 				ld_eprintf(ofl, ERR_WARNING,
523 				    MSG_INTL(MSG_UPD_NOREADSEG));
524 		}
525 		if (dsgp) {
526 			edata = dsgp->sg_phdr.p_vaddr + dsgp->sg_phdr.p_filesz;
527 		} else {
528 			edata = (Addr)0;
529 			edata_ndx = SHN_ABS;
530 			edata_abs = 1;
531 			if (flags & FLG_OF_VERBOSE)
532 				ld_eprintf(ofl, ERR_WARNING,
533 				    MSG_INTL(MSG_UPD_NORDWRSEG));
534 		}
535 
536 		if (dsgp == NULL) {
537 			if (tsgp)
538 				sgp = tsgp;
539 			else
540 				sgp = 0;
541 		} else if (tsgp == NULL)
542 			sgp = dsgp;
543 		else if (dsgp->sg_phdr.p_vaddr > tsgp->sg_phdr.p_vaddr)
544 			sgp = dsgp;
545 		else if (dsgp->sg_phdr.p_vaddr < tsgp->sg_phdr.p_vaddr)
546 			sgp = tsgp;
547 		else {
548 			/*
549 			 * One of the segments must be of zero size.
550 			 */
551 			if (tsgp->sg_phdr.p_memsz)
552 				sgp = tsgp;
553 			else
554 				sgp = dsgp;
555 		}
556 
557 		if (esgp && (esgp->sg_phdr.p_vaddr > sgp->sg_phdr.p_vaddr))
558 			sgp = esgp;
559 
560 		if (sgp) {
561 			end = sgp->sg_phdr.p_vaddr + sgp->sg_phdr.p_memsz;
562 
563 			/*
564 			 * If the last loadable segment is a read-only segment,
565 			 * then the application which uses the symbol _end to
566 			 * find the beginning of writable heap area may cause
567 			 * segmentation violation. We adjust the value of the
568 			 * _end to skip to the next page boundary.
569 			 *
570 			 * 6401812 System interface which returs beginning
571 			 *	   heap would be nice.
572 			 * When the above RFE is implemented, the changes below
573 			 * could be changed in a better way.
574 			 */
575 			if ((sgp->sg_phdr.p_flags & PF_W) == 0)
576 				end = (Addr)S_ROUND(end, sysconf(_SC_PAGESIZE));
577 
578 			/*
579 			 * If we're dealing with a memory reservation there are
580 			 * no sections to establish an index for _end, so assign
581 			 * it as an absolute.
582 			 */
583 			if (aplist_nitems(sgp->sg_osdescs) != 0) {
584 				/*
585 				 * Determine the last section for this segment.
586 				 */
587 				Os_desc	*osp = sgp->sg_osdescs->apl_data
588 				    [sgp->sg_osdescs->apl_nitems - 1];
589 
590 				/* LINTED */
591 				end_ndx = elf_ndxscn(osp->os_scn);
592 			} else {
593 				end_ndx = SHN_ABS;
594 				end_abs = 1;
595 			}
596 		} else {
597 			end = (Addr) 0;
598 			end_ndx = SHN_ABS;
599 			end_abs = 1;
600 			ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_UPD_NOSEG));
601 		}
602 	}
603 
604 	/*
605 	 * Initialize the scoped symbol table entry point.  This is for all
606 	 * the global symbols that have been scoped to locals and will be
607 	 * filled in during global symbol processing so that we don't have
608 	 * to traverse the globals symbol hash array more than once.
609 	 */
610 	if (symtab) {
611 		scopesym_bndx = symtab_ndx;
612 		scopesym_ndx = scopesym_bndx;
613 		symtab_ndx += ofl->ofl_scopecnt;
614 	}
615 
616 	/*
617 	 * If expanding partially expanded symbols under '-z nopartial',
618 	 * prepare to do that.
619 	 */
620 	if (ofl->ofl_isparexpn) {
621 		osp = ofl->ofl_isparexpn->is_osdesc;
622 		parexpnbase = parexpnaddr = (Addr)(osp->os_shdr->sh_addr +
623 		    ofl->ofl_isparexpn->is_indata->d_off);
624 		/* LINTED */
625 		parexpnndx = elf_ndxscn(osp->os_scn);
626 		ofl->ofl_parexpnndx = osp->os_identndx;
627 	}
628 
629 	/*
630 	 * If we are generating a .symtab collect all the local symbols,
631 	 * assigning a new virtual address or displacement (value).
632 	 */
633 	for (APLIST_TRAVERSE(ofl->ofl_objs, idx1, ifl)) {
634 		Xword		lndx, local = ifl->ifl_locscnt;
635 		Cap_desc	*cdp = ifl->ifl_caps;
636 
637 		for (lndx = 1; lndx < local; lndx++) {
638 			Gotndx		*gnp;
639 			uchar_t		type;
640 			Word		*_symshndx;
641 			int		enter_in_symtab, enter_in_ldynsym;
642 			int		update_done;
643 
644 			sdp = ifl->ifl_oldndx[lndx];
645 			sym = sdp->sd_sym;
646 
647 			/*
648 			 * Assign a got offset if necessary.
649 			 */
650 			if ((ld_targ.t_mr.mr_assign_got != NULL) &&
651 			    (*ld_targ.t_mr.mr_assign_got)(ofl, sdp) == S_ERROR)
652 				return ((Addr)S_ERROR);
653 
654 			if (DBG_ENABLED) {
655 				Aliste	idx2;
656 
657 				for (ALIST_TRAVERSE(sdp->sd_GOTndxs,
658 				    idx2, gnp)) {
659 					gottable->gt_sym = sdp;
660 					gottable->gt_gndx.gn_gotndx =
661 					    gnp->gn_gotndx;
662 					gottable->gt_gndx.gn_addend =
663 					    gnp->gn_addend;
664 					gottable++;
665 				}
666 			}
667 
668 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION)
669 				continue;
670 
671 			/*
672 			 * Ignore any symbols that have been marked as invalid
673 			 * during input processing.  Providing these aren't used
674 			 * for relocation they'll just be dropped from the
675 			 * output image.
676 			 */
677 			if (sdp->sd_flags & FLG_SY_INVALID)
678 				continue;
679 
680 			/*
681 			 * If the section that this symbol was associated
682 			 * with has been discarded - then we discard
683 			 * the local symbol along with it.
684 			 */
685 			if (sdp->sd_flags & FLG_SY_ISDISC)
686 				continue;
687 
688 			/*
689 			 * If this symbol is from a different file
690 			 * than the input descriptor we are processing,
691 			 * treat it as if it has FLG_SY_ISDISC set.
692 			 * This happens when sloppy_comdat_reloc()
693 			 * replaces a symbol to a discarded comdat section
694 			 * with an equivalent symbol from a different
695 			 * file. We only want to enter such a symbol
696 			 * once --- as part of the file that actually
697 			 * supplies it.
698 			 */
699 			if (ifl != sdp->sd_file)
700 				continue;
701 
702 			/*
703 			 * Generate an output symbol to represent this input
704 			 * symbol.  Even if the symbol table is to be stripped
705 			 * we still need to update any local symbols that are
706 			 * used during relocation.
707 			 */
708 			enter_in_symtab = symtab &&
709 			    (!(ofl->ofl_flags & FLG_OF_REDLSYM) ||
710 			    sdp->sd_move);
711 			enter_in_ldynsym = ldynsym &&
712 			    ((sym->st_name != 0) || (type == STT_FILE)) &&
713 			    ldynsym_symtype[type] &&
714 			    !(ofl->ofl_flags & FLG_OF_REDLSYM);
715 
716 			_symshndx = NULL;
717 
718 			if (enter_in_symtab) {
719 				if (!dynsym)
720 					sdp->sd_symndx = *symndx;
721 				symtab[symtab_ndx] = *sym;
722 
723 				/*
724 				 * Provided this isn't an unnamed register
725 				 * symbol, update its name.
726 				 */
727 				if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
728 				    symtab[symtab_ndx].st_name) {
729 					(void) st_setstring(strtab,
730 					    sdp->sd_name, &stoff);
731 					symtab[symtab_ndx].st_name = stoff;
732 				}
733 				sdp->sd_flags &= ~FLG_SY_CLEAN;
734 				if (symshndx)
735 					_symshndx = &symshndx[symtab_ndx];
736 				sdp->sd_sym = sym = &symtab[symtab_ndx++];
737 
738 				if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
739 				    (sym->st_shndx == SHN_ABS) &&
740 				    !enter_in_ldynsym)
741 					continue;
742 			} else if (enter_in_ldynsym) {
743 				/*
744 				 * Not using symtab, but we do have ldynsym
745 				 * available.
746 				 */
747 				ldynsym[ldynsym_ndx] = *sym;
748 				(void) st_setstring(dynstr, sdp->sd_name,
749 				    &stoff);
750 				ldynsym[ldynsym_ndx].st_name = stoff;
751 
752 				sdp->sd_flags &= ~FLG_SY_CLEAN;
753 				if (ldynshndx)
754 					_symshndx = &ldynshndx[ldynsym_ndx];
755 				sdp->sd_sym = sym = &ldynsym[ldynsym_ndx];
756 				/* Add it to sort section if it qualifies */
757 				ADD_TO_DYNSORT(sdp, sym, type, ldynsym_ndx);
758 				ldynsym_ndx++;
759 			} else {	/* Not using symtab or ldynsym */
760 				/*
761 				 * If this symbol requires modifying to provide
762 				 * for a relocation or move table update, make
763 				 * a copy of it.
764 				 */
765 				if (!(sdp->sd_flags & FLG_SY_UPREQD) &&
766 				    !(sdp->sd_move))
767 					continue;
768 				if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
769 				    (sym->st_shndx == SHN_ABS))
770 					continue;
771 
772 				if (ld_sym_copy(sdp) == S_ERROR)
773 					return ((Addr)S_ERROR);
774 				sym = sdp->sd_sym;
775 			}
776 
777 			/*
778 			 * Update the symbols contents if necessary.
779 			 */
780 			update_done = 0;
781 			if (type == STT_FILE) {
782 				sdp->sd_shndx = sym->st_shndx = SHN_ABS;
783 				sdp->sd_flags |= FLG_SY_SPECSEC;
784 				update_done = 1;
785 			}
786 
787 			/*
788 			 * If we are expanding the locally bound partially
789 			 * initialized symbols, then update the address here.
790 			 */
791 			if (ofl->ofl_isparexpn &&
792 			    (sdp->sd_flags & FLG_SY_PAREXPN) && !update_done) {
793 				sym->st_shndx = parexpnndx;
794 				sdp->sd_isc = ofl->ofl_isparexpn;
795 				sym->st_value = parexpnaddr;
796 				parexpnaddr += sym->st_size;
797 				if ((flags & FLG_OF_RELOBJ) == 0)
798 					sym->st_value -= parexpnbase;
799 			}
800 
801 			/*
802 			 * If this isn't an UNDEF symbol (ie. an input section
803 			 * is associated), update the symbols value and index.
804 			 */
805 			if (((isc = sdp->sd_isc) != NULL) && !update_done) {
806 				Word	sectndx;
807 
808 				osp = isc->is_osdesc;
809 				/* LINTED */
810 				sym->st_value +=
811 				    (Off)_elf_getxoff(isc->is_indata);
812 				if ((flags & FLG_OF_RELOBJ) == 0) {
813 					sym->st_value += osp->os_shdr->sh_addr;
814 					/*
815 					 * TLS symbols are relative to
816 					 * the TLS segment.
817 					 */
818 					if ((type == STT_TLS) &&
819 					    (ofl->ofl_tlsphdr)) {
820 						sym->st_value -=
821 						    ofl->ofl_tlsphdr->p_vaddr;
822 					}
823 				}
824 				/* LINTED */
825 				if ((sdp->sd_shndx = sectndx =
826 				    elf_ndxscn(osp->os_scn)) >= SHN_LORESERVE) {
827 					if (_symshndx) {
828 						*_symshndx = sectndx;
829 					}
830 					sym->st_shndx = SHN_XINDEX;
831 				} else {
832 					/* LINTED */
833 					sym->st_shndx = sectndx;
834 				}
835 			}
836 
837 			/*
838 			 * If entering the symbol in both the symtab and the
839 			 * ldynsym, then the one in symtab needs to be
840 			 * copied to ldynsym. If it is only in the ldynsym,
841 			 * then the code above already set it up and we have
842 			 * nothing more to do here.
843 			 */
844 			if (enter_in_symtab && enter_in_ldynsym) {
845 				ldynsym[ldynsym_ndx] = *sym;
846 				(void) st_setstring(dynstr, sdp->sd_name,
847 				    &stoff);
848 				ldynsym[ldynsym_ndx].st_name = stoff;
849 
850 				if (_symshndx && ldynshndx)
851 					ldynshndx[ldynsym_ndx] = *_symshndx;
852 
853 				/* Add it to sort section if it qualifies */
854 				ADD_TO_DYNSORT(sdp, sym, type, ldynsym_ndx);
855 
856 				ldynsym_ndx++;
857 			}
858 		}
859 
860 		/*
861 		 * If this input file has undergone object to symbol
862 		 * capabilities conversion, supply any new capabilities symbols.
863 		 * These symbols are copies of the original global symbols, and
864 		 * follow the existing local symbols that are supplied from this
865 		 * input file (which are identified with a preceding STT_FILE).
866 		 */
867 		if (symtab && cdp && cdp->ca_syms) {
868 			Aliste		idx2;
869 			Cap_sym		*csp;
870 
871 			for (APLIST_TRAVERSE(cdp->ca_syms, idx2, csp)) {
872 				Is_desc	*isp;
873 
874 				sdp = csp->cs_sdp;
875 				sym = sdp->sd_sym;
876 
877 				if ((isp = sdp->sd_isc) != NULL) {
878 					Os_desc	*osp = isp->is_osdesc;
879 
880 					/*
881 					 * Update the symbols value.
882 					 */
883 					/* LINTED */
884 					sym->st_value +=
885 					    (Off)_elf_getxoff(isp->is_indata);
886 					if ((flags & FLG_OF_RELOBJ) == 0)
887 						sym->st_value +=
888 						    osp->os_shdr->sh_addr;
889 
890 					/*
891 					 * Update the symbols section index.
892 					 */
893 					sdp->sd_shndx = sym->st_shndx =
894 					    elf_ndxscn(osp->os_scn);
895 				}
896 
897 				symtab[symtab_ndx] = *sym;
898 				(void) st_setstring(strtab, sdp->sd_name,
899 				    &stoff);
900 				symtab[symtab_ndx].st_name = stoff;
901 				sdp->sd_symndx = symtab_ndx++;
902 			}
903 		}
904 	}
905 
906 	symtab_gbl_bndx = symtab_ndx;	/* .symtab index of 1st global entry */
907 
908 	/*
909 	 * Two special symbols are `_init' and `_fini'.  If these are supplied
910 	 * by crti.o then they are used to represent the total concatenation of
911 	 * the `.init' and `.fini' sections.
912 	 *
913 	 * Determine whether any .init or .fini sections exist.  If these
914 	 * sections exist and a dynamic object is being built, but no `_init'
915 	 * or `_fini' symbols are found, then the user is probably building
916 	 * this object directly from ld(1) rather than using a compiler driver
917 	 * that provides the symbols via crt's.
918 	 *
919 	 * If the .init or .fini section exist, and their associated symbols,
920 	 * determine the size of the sections and updated the symbols value
921 	 * accordingly.
922 	 */
923 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U), SYM_NOHASH, 0,
924 	    ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) && sdp->sd_isc &&
925 	    (sdp->sd_isc->is_osdesc == iosp)) {
926 		if (ld_sym_copy(sdp) == S_ERROR)
927 			return ((Addr)S_ERROR);
928 		sdp->sd_sym->st_size = sdp->sd_isc->is_osdesc->os_shdr->sh_size;
929 
930 	} else if (iosp && !(flags & FLG_OF_RELOBJ)) {
931 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_NOCRT),
932 		    MSG_ORIG(MSG_SYM_INIT_U), MSG_ORIG(MSG_SCN_INIT));
933 	}
934 
935 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U), SYM_NOHASH, 0,
936 	    ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) && sdp->sd_isc &&
937 	    (sdp->sd_isc->is_osdesc == fosp)) {
938 		if (ld_sym_copy(sdp) == S_ERROR)
939 			return ((Addr)S_ERROR);
940 		sdp->sd_sym->st_size = sdp->sd_isc->is_osdesc->os_shdr->sh_size;
941 
942 	} else if (fosp && !(flags & FLG_OF_RELOBJ)) {
943 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_SYM_NOCRT),
944 		    MSG_ORIG(MSG_SYM_FINI_U), MSG_ORIG(MSG_SCN_FINI));
945 	}
946 
947 	/*
948 	 * Assign .bss information for use with updating COMMON symbols.
949 	 */
950 	if (ofl->ofl_isbss) {
951 		isc = ofl->ofl_isbss;
952 		osp = isc->is_osdesc;
953 
954 		bssaddr = osp->os_shdr->sh_addr +
955 		    (Off)_elf_getxoff(isc->is_indata);
956 		/* LINTED */
957 		bssndx = elf_ndxscn(osp->os_scn);
958 	}
959 
960 #if	defined(_ELF64)
961 	/*
962 	 * For amd64 target, assign .lbss information for use
963 	 * with updating LCOMMON symbols.
964 	 */
965 	if ((ld_targ.t_m.m_mach == EM_AMD64) && ofl->ofl_islbss) {
966 		osp = ofl->ofl_islbss->is_osdesc;
967 
968 		lbssaddr = osp->os_shdr->sh_addr +
969 		    (Off)_elf_getxoff(ofl->ofl_islbss->is_indata);
970 		/* LINTED */
971 		lbssndx = elf_ndxscn(osp->os_scn);
972 	}
973 #endif
974 	/*
975 	 * Assign .tlsbss information for use with updating COMMON symbols.
976 	 */
977 	if (ofl->ofl_istlsbss) {
978 		osp = ofl->ofl_istlsbss->is_osdesc;
979 		tlsbssaddr = osp->os_shdr->sh_addr +
980 		    (Off)_elf_getxoff(ofl->ofl_istlsbss->is_indata);
981 		/* LINTED */
982 		tlsbssndx = elf_ndxscn(osp->os_scn);
983 	}
984 
985 	if ((sorted_syms = libld_calloc(ofl->ofl_globcnt +
986 	    ofl->ofl_elimcnt + ofl->ofl_scopecnt,
987 	    sizeof (*sorted_syms))) == NULL)
988 		return ((Addr)S_ERROR);
989 
990 	scndx = 0;
991 	ssndx = ofl->ofl_scopecnt + ofl->ofl_elimcnt;
992 
993 	DBG_CALL(Dbg_syms_up_title(ofl->ofl_lml));
994 
995 	/*
996 	 * Traverse the internal symbol table updating global symbol information
997 	 * and allocating common.
998 	 */
999 	for (sav = avl_first(&ofl->ofl_symavl); sav;
1000 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
1001 		Sym	*symptr;
1002 		int	local;
1003 		int	restore;
1004 
1005 		sdp = sav->sav_sdp;
1006 
1007 		/*
1008 		 * Ignore any symbols that have been marked as invalid during
1009 		 * input processing.  Providing these aren't used for
1010 		 * relocation, they will be dropped from the output image.
1011 		 */
1012 		if (sdp->sd_flags & FLG_SY_INVALID) {
1013 			DBG_CALL(Dbg_syms_old(ofl, sdp));
1014 			DBG_CALL(Dbg_syms_ignore(ofl, sdp));
1015 			continue;
1016 		}
1017 
1018 		/*
1019 		 * Only needed symbols are copied to the output symbol table.
1020 		 */
1021 		if (sdp->sd_ref == REF_DYN_SEEN)
1022 			continue;
1023 
1024 		if (ld_sym_reducable(ofl, sdp))
1025 			local = 1;
1026 		else
1027 			local = 0;
1028 
1029 		if (local || (ofl->ofl_hashbkts == 0)) {
1030 			sorted_syms[scndx++].sl_sdp = sdp;
1031 		} else {
1032 			sorted_syms[ssndx].sl_hval = sdp->sd_aux->sa_hash %
1033 			    ofl->ofl_hashbkts;
1034 			sorted_syms[ssndx].sl_sdp = sdp;
1035 			ssndx++;
1036 		}
1037 
1038 		/*
1039 		 * Note - expand the COMMON symbols here because an address
1040 		 * must be assigned to them in the same order that space was
1041 		 * calculated in sym_validate().  If this ordering isn't
1042 		 * followed differing alignment requirements can throw us all
1043 		 * out of whack.
1044 		 *
1045 		 * The expanded .bss global symbol is handled here as well.
1046 		 *
1047 		 * The actual adding entries into the symbol table still occurs
1048 		 * below in hashbucket order.
1049 		 */
1050 		symptr = sdp->sd_sym;
1051 		restore = 0;
1052 		if ((sdp->sd_flags & FLG_SY_PAREXPN) ||
1053 		    ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1054 		    (sdp->sd_shndx = symptr->st_shndx) == SHN_COMMON)) {
1055 
1056 			/*
1057 			 * An expanded symbol goes to a special .data section
1058 			 * prepared for that purpose (ofl->ofl_isparexpn).
1059 			 * Assign COMMON allocations to .bss.
1060 			 * Otherwise leave it as is.
1061 			 */
1062 			if (sdp->sd_flags & FLG_SY_PAREXPN) {
1063 				restore = 1;
1064 				sdp->sd_shndx = parexpnndx;
1065 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1066 				symptr->st_value = (Xword) S_ROUND(
1067 				    parexpnaddr, symptr->st_value);
1068 				parexpnaddr = symptr->st_value +
1069 				    symptr->st_size;
1070 				sdp->sd_isc = ofl->ofl_isparexpn;
1071 				sdp->sd_flags |= FLG_SY_COMMEXP;
1072 
1073 			} else if (ELF_ST_TYPE(symptr->st_info) != STT_TLS &&
1074 			    (local || !(flags & FLG_OF_RELOBJ))) {
1075 				restore = 1;
1076 				sdp->sd_shndx = bssndx;
1077 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1078 				symptr->st_value = (Xword)S_ROUND(bssaddr,
1079 				    symptr->st_value);
1080 				bssaddr = symptr->st_value + symptr->st_size;
1081 				sdp->sd_isc = ofl->ofl_isbss;
1082 				sdp->sd_flags |= FLG_SY_COMMEXP;
1083 
1084 			} else if (ELF_ST_TYPE(symptr->st_info) == STT_TLS &&
1085 			    (local || !(flags & FLG_OF_RELOBJ))) {
1086 				restore = 1;
1087 				sdp->sd_shndx = tlsbssndx;
1088 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1089 				symptr->st_value = (Xword)S_ROUND(tlsbssaddr,
1090 				    symptr->st_value);
1091 				tlsbssaddr = symptr->st_value + symptr->st_size;
1092 				sdp->sd_isc = ofl->ofl_istlsbss;
1093 				sdp->sd_flags |= FLG_SY_COMMEXP;
1094 				/*
1095 				 * TLS symbols are relative to the TLS segment.
1096 				 */
1097 				symptr->st_value -= ofl->ofl_tlsphdr->p_vaddr;
1098 			}
1099 #if	defined(_ELF64)
1100 		} else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1101 		    (sdp->sd_flags & FLG_SY_SPECSEC) &&
1102 		    ((sdp->sd_shndx = symptr->st_shndx) ==
1103 		    SHN_X86_64_LCOMMON) &&
1104 		    ((local || !(flags & FLG_OF_RELOBJ)))) {
1105 			restore = 1;
1106 			sdp->sd_shndx = lbssndx;
1107 			sdp->sd_flags &= ~FLG_SY_SPECSEC;
1108 			symptr->st_value = (Xword)S_ROUND(lbssaddr,
1109 			    symptr->st_value);
1110 			lbssaddr = symptr->st_value + symptr->st_size;
1111 			sdp->sd_isc = ofl->ofl_islbss;
1112 			sdp->sd_flags |= FLG_SY_COMMEXP;
1113 #endif
1114 		}
1115 
1116 		if (restore != 0) {
1117 			uchar_t		type, bind;
1118 
1119 			/*
1120 			 * Make sure this COMMON symbol is returned to the same
1121 			 * binding as was defined in the original relocatable
1122 			 * object reference.
1123 			 */
1124 			type = ELF_ST_TYPE(symptr->st_info);
1125 			if (sdp->sd_flags & FLG_SY_GLOBREF)
1126 				bind = STB_GLOBAL;
1127 			else
1128 				bind = STB_WEAK;
1129 
1130 			symptr->st_info = ELF_ST_INFO(bind, type);
1131 		}
1132 
1133 #define	IS_DATA_SYMBOL(x)	((ELF_ST_TYPE(x->st_info) == STT_OBJECT) || \
1134 		    (ELF_ST_TYPE(x->st_info) == STT_COMMON) ||		    \
1135 		    (ELF_ST_TYPE(x->st_info) == STT_TLS))
1136 
1137 /*
1138  * Filter symbols, special symbols, and those that will be reduced aren't
1139  * worth guidance
1140  */
1141 #define	IS_BORING_SYMBOL(x)	(x->sd_flags & (FLG_SY_REDUCED|FLG_SY_STDFLTR| \
1142 		    FLG_SY_SPECSEC|FLG_SY_HIDDEN|FLG_SY_ELIM|FLG_SY_IGNORE))
1143 
1144 /* Local symbols and unresolved weaks aren't useful to guide on */
1145 #define	IS_BORING_SCOPE(x)	((ELF_ST_BIND(x->st_info) == STB_LOCAL) || \
1146 		    ((ELF_ST_BIND(x->st_info) == STB_WEAK) &&		   \
1147 		    (x->st_shndx == SHN_UNDEF)))
1148 
1149 /* Symbol has the assertions recommended for global data */
1150 #define	HAS_NEEDED_ASSERTS(x)	((x->sd_ass != NULL) &&		\
1151 		    (ass_enabled(x->sd_ass, SYM_ASSERT_SIZE) ||	\
1152 		    ass_enabled(x->sd_ass, SYM_ASSERT_ALIAS)))
1153 
1154 		/*
1155 		 * If we're building a shared object and a mapfile is
1156 		 * specified, issue guidance if any symbol mentioned in the
1157 		 * mapfile is a global data symbol with no asserted size.
1158 		 *
1159 		 * This is somewhat heuristic to maximize the chance of
1160 		 * -zguidance users seeing our good advice without us being
1161 		 * annoying (eg. we don't guide when things like
1162 		 * mapfile.noex* are the only mapfiles)
1163 		 */
1164 		if (OFL_GUIDANCE(ofl, FLG_OFG_NO_ASSERTS) &&
1165 		    (aplist_nitems(ofl->ofl_maps) > 0) && /* mapfile used */
1166 		    (ofl->ofl_flags & FLG_OF_SHAROBJ) && /* building .so */
1167 		    (ofl->ofl_flags & FLG_OF_VERDEF) && /* versions/reduce */
1168 		    (sdp->sd_ref == REF_REL_NEED) && /* symbol in .o */
1169 		    IS_DATA_SYMBOL(sdp->sd_sym) &&
1170 		    !IS_BORING_SCOPE(sdp->sd_sym) &&
1171 		    !IS_BORING_SYMBOL(sdp) &&
1172 		    !HAS_NEEDED_ASSERTS(sdp)) {
1173 			ld_eprintf(ofl, ERR_GUIDANCE,
1174 			    MSG_INTL(MSG_GUIDE_ASSERT_SIZE),
1175 			    sdp->sd_name, (Lword)sdp->sd_sym->st_size);
1176 		}
1177 	}
1178 
1179 	/*
1180 	 * If this is a dynamic object then add any local capabilities symbols.
1181 	 */
1182 	if (dynsym && ofl->ofl_capfamilies) {
1183 		Cap_avlnode	*cav;
1184 
1185 		for (cav = avl_first(ofl->ofl_capfamilies); cav;
1186 		    cav = AVL_NEXT(ofl->ofl_capfamilies, cav)) {
1187 			Cap_sym		*csp;
1188 			Aliste		idx;
1189 
1190 			for (APLIST_TRAVERSE(cav->cn_members, idx, csp)) {
1191 				sdp = csp->cs_sdp;
1192 
1193 				DBG_CALL(Dbg_syms_created(ofl->ofl_lml,
1194 				    sdp->sd_name));
1195 				DBG_CALL(Dbg_syms_entered(ofl, sdp->sd_sym,
1196 				    sdp));
1197 
1198 				dynsym[dynsym_ndx] = *sdp->sd_sym;
1199 
1200 				(void) st_setstring(dynstr, sdp->sd_name,
1201 				    &stoff);
1202 				dynsym[dynsym_ndx].st_name = stoff;
1203 
1204 				sdp->sd_sym = &dynsym[dynsym_ndx];
1205 				sdp->sd_symndx = dynsym_ndx;
1206 
1207 				/*
1208 				 * Indicate that this is a capabilities symbol.
1209 				 * Note, that this identification only provides
1210 				 * information regarding the symbol that is
1211 				 * visible from elfdump(1) -y.  The association
1212 				 * of a symbol to its capabilities is derived
1213 				 * from a .SUNW_capinfo entry.
1214 				 */
1215 				if (syminfo) {
1216 					syminfo[dynsym_ndx].si_flags |=
1217 					    SYMINFO_FLG_CAP;
1218 				}
1219 
1220 				dynsym_ndx++;
1221 			}
1222 		}
1223 	}
1224 
1225 	if (ofl->ofl_hashbkts) {
1226 		qsort(sorted_syms + ofl->ofl_scopecnt + ofl->ofl_elimcnt,
1227 		    ofl->ofl_globcnt, sizeof (Sym_s_list),
1228 		    (int (*)(const void *, const void *))sym_hash_compare);
1229 	}
1230 
1231 	for (ssndx = 0; ssndx < (ofl->ofl_elimcnt + ofl->ofl_scopecnt +
1232 	    ofl->ofl_globcnt); ssndx++) {
1233 		const char	*name;
1234 		Sym		*sym;
1235 		Sym_aux		*sap;
1236 		Half		spec;
1237 		int		local = 0, dynlocal = 0, enter_in_symtab;
1238 		Gotndx		*gnp;
1239 		Word		sectndx;
1240 
1241 		sdp = sorted_syms[ssndx].sl_sdp;
1242 		sectndx = 0;
1243 
1244 		if (symtab)
1245 			enter_in_symtab = 1;
1246 		else
1247 			enter_in_symtab = 0;
1248 
1249 		/*
1250 		 * Assign a got offset if necessary.
1251 		 */
1252 		if ((ld_targ.t_mr.mr_assign_got != NULL) &&
1253 		    (*ld_targ.t_mr.mr_assign_got)(ofl, sdp) == S_ERROR)
1254 			return ((Addr)S_ERROR);
1255 
1256 		if (DBG_ENABLED) {
1257 			Aliste	idx2;
1258 
1259 			for (ALIST_TRAVERSE(sdp->sd_GOTndxs, idx2, gnp)) {
1260 				gottable->gt_sym = sdp;
1261 				gottable->gt_gndx.gn_gotndx = gnp->gn_gotndx;
1262 				gottable->gt_gndx.gn_addend = gnp->gn_addend;
1263 				gottable++;
1264 			}
1265 
1266 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTGOTndx) {
1267 				gottable->gt_sym = sdp;
1268 				gottable->gt_gndx.gn_gotndx =
1269 				    sdp->sd_aux->sa_PLTGOTndx;
1270 				gottable++;
1271 			}
1272 		}
1273 
1274 		/*
1275 		 * If this symbol has been marked as being reduced to local
1276 		 * scope then it will have to be placed in the scoped portion
1277 		 * of the .symtab.  Retain the appropriate index for use in
1278 		 * version symbol indexing and relocation.
1279 		 */
1280 		if (ld_sym_reducable(ofl, sdp)) {
1281 			local = 1;
1282 			if (!(sdp->sd_flags & FLG_SY_ELIM) && !dynsym)
1283 				sdp->sd_symndx = scopesym_ndx;
1284 			else
1285 				sdp->sd_symndx = 0;
1286 
1287 			if (sdp->sd_flags & FLG_SY_ELIM) {
1288 				enter_in_symtab = 0;
1289 			} else if (ldynsym && sdp->sd_sym->st_name &&
1290 			    ldynsym_symtype[
1291 			    ELF_ST_TYPE(sdp->sd_sym->st_info)]) {
1292 				dynlocal = 1;
1293 			}
1294 		} else {
1295 			sdp->sd_symndx = *symndx;
1296 		}
1297 
1298 		/*
1299 		 * Copy basic symbol and string information.
1300 		 */
1301 		name = sdp->sd_name;
1302 		sap = sdp->sd_aux;
1303 
1304 		/*
1305 		 * If we require to record version symbol indexes, update the
1306 		 * associated version symbol information for all defined
1307 		 * symbols.  If a version definition is required any zero value
1308 		 * symbol indexes would have been flagged as undefined symbol
1309 		 * errors, however if we're just scoping these need to fall into
1310 		 * the base of global symbols.
1311 		 */
1312 		if (sdp->sd_symndx && versym) {
1313 			Half	vndx = 0;
1314 
1315 			if (sdp->sd_flags & FLG_SY_MVTOCOMM) {
1316 				vndx = VER_NDX_GLOBAL;
1317 			} else if (sdp->sd_ref == REF_REL_NEED) {
1318 				vndx = sap->sa_overndx;
1319 
1320 				if ((vndx == 0) &&
1321 				    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1322 					if (SYM_IS_HIDDEN(sdp))
1323 						vndx = VER_NDX_LOCAL;
1324 					else
1325 						vndx = VER_NDX_GLOBAL;
1326 				}
1327 			} else if ((sdp->sd_ref == REF_DYN_NEED) &&
1328 			    (sap->sa_dverndx > 0) &&
1329 			    (sap->sa_dverndx <= sdp->sd_file->ifl_vercnt) &&
1330 			    (sdp->sd_file->ifl_verndx != NULL)) {
1331 				/* Use index of verneed record */
1332 				vndx = sdp->sd_file->ifl_verndx
1333 				    [sap->sa_dverndx].vi_overndx;
1334 			}
1335 			versym[sdp->sd_symndx] = vndx;
1336 		}
1337 
1338 		/*
1339 		 * If we are creating the .syminfo section then set per symbol
1340 		 * flags here.
1341 		 */
1342 		if (sdp->sd_symndx && syminfo &&
1343 		    !(sdp->sd_flags & FLG_SY_NOTAVAIL)) {
1344 			int	ndx = sdp->sd_symndx;
1345 			APlist	**alpp = &(ofl->ofl_symdtent);
1346 
1347 			if (sdp->sd_flags & FLG_SY_MVTOCOMM)
1348 				/*
1349 				 * Identify a copy relocation symbol.
1350 				 */
1351 				syminfo[ndx].si_flags |= SYMINFO_FLG_COPY;
1352 
1353 			if (sdp->sd_ref == REF_DYN_NEED) {
1354 				/*
1355 				 * A reference is bound to a needed dependency.
1356 				 * Save the syminfo entry, so that when the
1357 				 * .dynamic section has been updated, a
1358 				 * DT_NEEDED entry can be associated
1359 				 * (see update_osyminfo()).
1360 				 */
1361 				if (aplist_append(alpp, sdp,
1362 				    AL_CNT_OFL_SYMINFOSYMS) == NULL)
1363 					return (0);
1364 
1365 				/*
1366 				 * Flag that the symbol has a direct association
1367 				 * with the external reference (this is an old
1368 				 * tagging, that has no real effect by itself).
1369 				 */
1370 				syminfo[ndx].si_flags |= SYMINFO_FLG_DIRECT;
1371 
1372 				/*
1373 				 * Flag any lazy or deferred reference.
1374 				 */
1375 				if (sdp->sd_flags & FLG_SY_LAZYLD)
1376 					syminfo[ndx].si_flags |=
1377 					    SYMINFO_FLG_LAZYLOAD;
1378 				if (sdp->sd_flags & FLG_SY_DEFERRED)
1379 					syminfo[ndx].si_flags |=
1380 					    SYMINFO_FLG_DEFERRED;
1381 
1382 				/*
1383 				 * Enable direct symbol bindings if:
1384 				 *
1385 				 *  -	Symbol was identified with the DIRECT
1386 				 *	keyword in a mapfile.
1387 				 *
1388 				 *  -	Symbol reference has been bound to a
1389 				 *	dependency which was specified as
1390 				 *	requiring direct bindings with -zdirect.
1391 				 *
1392 				 *  -	All symbol references are required to
1393 				 *	use direct bindings via -Bdirect.
1394 				 */
1395 				if (sdp->sd_flags & FLG_SY_DIR)
1396 					syminfo[ndx].si_flags |=
1397 					    SYMINFO_FLG_DIRECTBIND;
1398 
1399 			} else if ((sdp->sd_flags & FLG_SY_EXTERN) &&
1400 			    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
1401 				/*
1402 				 * If this symbol has been explicitly defined
1403 				 * as external, and remains unresolved, mark
1404 				 * it as external.
1405 				 */
1406 				syminfo[ndx].si_boundto = SYMINFO_BT_EXTERN;
1407 
1408 			} else if ((sdp->sd_flags & FLG_SY_PARENT) &&
1409 			    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
1410 				/*
1411 				 * If this symbol has been explicitly defined
1412 				 * to be a reference to a parent object,
1413 				 * indicate whether a direct binding should be
1414 				 * established.
1415 				 */
1416 				syminfo[ndx].si_flags |= SYMINFO_FLG_DIRECT;
1417 				syminfo[ndx].si_boundto = SYMINFO_BT_PARENT;
1418 				if (sdp->sd_flags & FLG_SY_DIR)
1419 					syminfo[ndx].si_flags |=
1420 					    SYMINFO_FLG_DIRECTBIND;
1421 
1422 			} else if (sdp->sd_flags & FLG_SY_STDFLTR) {
1423 				/*
1424 				 * A filter definition.  Although this symbol
1425 				 * can only be a stub, it might be necessary to
1426 				 * prevent external direct bindings.
1427 				 */
1428 				syminfo[ndx].si_flags |= SYMINFO_FLG_FILTER;
1429 				if (sdp->sd_flags & FLG_SY_NDIR)
1430 					syminfo[ndx].si_flags |=
1431 					    SYMINFO_FLG_NOEXTDIRECT;
1432 
1433 			} else if (sdp->sd_flags & FLG_SY_AUXFLTR) {
1434 				/*
1435 				 * An auxiliary filter definition.  By nature,
1436 				 * this definition is direct, in that should the
1437 				 * filtee lookup fail, we'll fall back to this
1438 				 * object.  It may still be necessary to
1439 				 * prevent external direct bindings.
1440 				 */
1441 				syminfo[ndx].si_flags |= SYMINFO_FLG_AUXILIARY;
1442 				if (sdp->sd_flags & FLG_SY_NDIR)
1443 					syminfo[ndx].si_flags |=
1444 					    SYMINFO_FLG_NOEXTDIRECT;
1445 
1446 			} else if ((sdp->sd_ref == REF_REL_NEED) &&
1447 			    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1448 				/*
1449 				 * This definition exists within the object
1450 				 * being created.  Provide a default boundto
1451 				 * definition, which may be overridden later.
1452 				 */
1453 				syminfo[ndx].si_boundto = SYMINFO_BT_NONE;
1454 
1455 				/*
1456 				 * Indicate whether it is necessary to prevent
1457 				 * external direct bindings.
1458 				 */
1459 				if (sdp->sd_flags & FLG_SY_NDIR) {
1460 					syminfo[ndx].si_flags |=
1461 					    SYMINFO_FLG_NOEXTDIRECT;
1462 				}
1463 
1464 				/*
1465 				 * Indicate that this symbol is acting as an
1466 				 * individual interposer.
1467 				 */
1468 				if (sdp->sd_flags & FLG_SY_INTPOSE) {
1469 					syminfo[ndx].si_flags |=
1470 					    SYMINFO_FLG_INTERPOSE;
1471 				}
1472 
1473 				/*
1474 				 * Indicate that this symbol is deferred, and
1475 				 * hence should not be bound to during BIND_NOW
1476 				 * relocations.
1477 				 */
1478 				if (sdp->sd_flags & FLG_SY_DEFERRED) {
1479 					syminfo[ndx].si_flags |=
1480 					    SYMINFO_FLG_DEFERRED;
1481 				}
1482 
1483 				/*
1484 				 * If external bindings are allowed, indicate
1485 				 * the binding, and a direct binding if
1486 				 * necessary.
1487 				 */
1488 				if ((sdp->sd_flags & FLG_SY_NDIR) == 0) {
1489 					syminfo[ndx].si_flags |=
1490 					    SYMINFO_FLG_DIRECT;
1491 
1492 					if (sdp->sd_flags & FLG_SY_DIR)
1493 						syminfo[ndx].si_flags |=
1494 						    SYMINFO_FLG_DIRECTBIND;
1495 
1496 					/*
1497 					 * Provide a default boundto definition,
1498 					 * which may be overridden later.
1499 					 */
1500 					syminfo[ndx].si_boundto =
1501 					    SYMINFO_BT_SELF;
1502 				}
1503 
1504 				/*
1505 				 * Indicate that this is a capabilities symbol.
1506 				 * Note, that this identification only provides
1507 				 * information regarding the symbol that is
1508 				 * visible from elfdump(1) -y.  The association
1509 				 * of a symbol to its capabilities is derived
1510 				 * from a .SUNW_capinfo entry.
1511 				 */
1512 				if ((sdp->sd_flags & FLG_SY_CAP) &&
1513 				    ofl->ofl_oscapinfo) {
1514 					syminfo[ndx].si_flags |=
1515 					    SYMINFO_FLG_CAP;
1516 				}
1517 			}
1518 		}
1519 
1520 		/*
1521 		 * Note that the `sym' value is reset to be one of the new
1522 		 * symbol table entries.  This symbol will be updated further
1523 		 * depending on the type of the symbol.  Process the .symtab
1524 		 * first, followed by the .dynsym, thus the `sym' value will
1525 		 * remain as the .dynsym value when the .dynsym is present.
1526 		 * This ensures that any versioning symbols st_name value will
1527 		 * be appropriate for the string table used by version
1528 		 * entries.
1529 		 */
1530 		if (enter_in_symtab) {
1531 			Word	_symndx;
1532 
1533 			if (local)
1534 				_symndx = scopesym_ndx;
1535 			else
1536 				_symndx = symtab_ndx;
1537 
1538 			symtab[_symndx] = *sdp->sd_sym;
1539 			sdp->sd_sym = sym = &symtab[_symndx];
1540 			(void) st_setstring(strtab, name, &stoff);
1541 			sym->st_name = stoff;
1542 		}
1543 		if (dynlocal) {
1544 			ldynsym[ldynscopesym_ndx] = *sdp->sd_sym;
1545 			sdp->sd_sym = sym = &ldynsym[ldynscopesym_ndx];
1546 			(void) st_setstring(dynstr, name, &stoff);
1547 			ldynsym[ldynscopesym_ndx].st_name = stoff;
1548 			/* Add it to sort section if it qualifies */
1549 			ADD_TO_DYNSORT(sdp, sym, ELF_ST_TYPE(sym->st_info),
1550 			    ldynscopesym_ndx);
1551 		}
1552 
1553 		if (dynsym && !local) {
1554 			dynsym[dynsym_ndx] = *sdp->sd_sym;
1555 
1556 			/*
1557 			 * Provided this isn't an unnamed register symbol,
1558 			 * update the symbols name and hash value.
1559 			 */
1560 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1561 			    dynsym[dynsym_ndx].st_name) {
1562 				(void) st_setstring(dynstr, name, &stoff);
1563 				dynsym[dynsym_ndx].st_name = stoff;
1564 
1565 				if (stoff) {
1566 					Word	hashval, _hashndx;
1567 
1568 					hashval =
1569 					    sap->sa_hash % ofl->ofl_hashbkts;
1570 
1571 					/* LINTED */
1572 					if (_hashndx = hashbkt[hashval]) {
1573 						while (hashchain[_hashndx]) {
1574 							_hashndx =
1575 							    hashchain[_hashndx];
1576 						}
1577 						hashchain[_hashndx] =
1578 						    sdp->sd_symndx;
1579 					} else {
1580 						hashbkt[hashval] =
1581 						    sdp->sd_symndx;
1582 					}
1583 				}
1584 			}
1585 			sdp->sd_sym = sym = &dynsym[dynsym_ndx];
1586 
1587 			/*
1588 			 * Add it to sort section if it qualifies.
1589 			 * The indexes in that section are relative to the
1590 			 * the adjacent SUNW_ldynsym/dymsym pair, so we
1591 			 * add the number of items in SUNW_ldynsym to the
1592 			 * dynsym index.
1593 			 */
1594 			ADD_TO_DYNSORT(sdp, sym, ELF_ST_TYPE(sym->st_info),
1595 			    ldynsym_cnt + dynsym_ndx);
1596 		}
1597 
1598 		if (!enter_in_symtab && (!dynsym || (local && !dynlocal))) {
1599 			if (!(sdp->sd_flags & FLG_SY_UPREQD))
1600 				continue;
1601 			sym = sdp->sd_sym;
1602 		} else
1603 			sdp->sd_flags &= ~FLG_SY_CLEAN;
1604 
1605 		/*
1606 		 * If we have a weak data symbol for which we need the real
1607 		 * symbol also, save this processing until later.
1608 		 *
1609 		 * The exception to this is if the weak/strong have PLT's
1610 		 * assigned to them.  In that case we don't do the post-weak
1611 		 * processing because the PLT's must be maintained so that we
1612 		 * can do 'interpositioning' on both of the symbols.
1613 		 */
1614 		if ((sap->sa_linkndx) &&
1615 		    (ELF_ST_BIND(sym->st_info) == STB_WEAK) &&
1616 		    (!sap->sa_PLTndx)) {
1617 			Sym_desc	*_sdp;
1618 
1619 			_sdp = sdp->sd_file->ifl_oldndx[sap->sa_linkndx];
1620 
1621 			if (_sdp->sd_ref != REF_DYN_SEEN) {
1622 				Wk_desc	wk;
1623 
1624 				if (enter_in_symtab) {
1625 					if (local) {
1626 						wk.wk_symtab =
1627 						    &symtab[scopesym_ndx];
1628 						scopesym_ndx++;
1629 					} else {
1630 						wk.wk_symtab =
1631 						    &symtab[symtab_ndx];
1632 						symtab_ndx++;
1633 					}
1634 				} else {
1635 					wk.wk_symtab = NULL;
1636 				}
1637 				if (dynsym) {
1638 					if (!local) {
1639 						wk.wk_dynsym =
1640 						    &dynsym[dynsym_ndx];
1641 						dynsym_ndx++;
1642 					} else if (dynlocal) {
1643 						wk.wk_dynsym =
1644 						    &ldynsym[ldynscopesym_ndx];
1645 						ldynscopesym_ndx++;
1646 					}
1647 				} else {
1648 					wk.wk_dynsym = NULL;
1649 				}
1650 				wk.wk_weak = sdp;
1651 				wk.wk_alias = _sdp;
1652 
1653 				if (alist_append(&weak, &wk,
1654 				    sizeof (Wk_desc), AL_CNT_WEAK) == NULL)
1655 					return ((Addr)S_ERROR);
1656 
1657 				continue;
1658 			}
1659 		}
1660 
1661 		DBG_CALL(Dbg_syms_old(ofl, sdp));
1662 
1663 		spec = 0;
1664 		/*
1665 		 * assign new symbol value.
1666 		 */
1667 		sectndx = sdp->sd_shndx;
1668 		if (sectndx == SHN_UNDEF) {
1669 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) &&
1670 			    (sym->st_value != 0)) {
1671 				ld_eprintf(ofl, ERR_WARNING,
1672 				    MSG_INTL(MSG_SYM_NOTNULL),
1673 				    demangle(name), sdp->sd_file->ifl_name);
1674 			}
1675 
1676 			/*
1677 			 * Undefined weak global, if we are generating a static
1678 			 * executable, output as an absolute zero.  Otherwise
1679 			 * leave it as is, ld.so.1 will skip symbols of this
1680 			 * type (this technique allows applications and
1681 			 * libraries to test for the existence of a symbol as an
1682 			 * indication of the presence or absence of certain
1683 			 * functionality).
1684 			 */
1685 			if (OFL_IS_STATIC_EXEC(ofl) &&
1686 			    (ELF_ST_BIND(sym->st_info) == STB_WEAK)) {
1687 				sdp->sd_flags |= FLG_SY_SPECSEC;
1688 				sdp->sd_shndx = sectndx = SHN_ABS;
1689 			}
1690 		} else if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1691 		    (sectndx == SHN_COMMON)) {
1692 			/* COMMONs have already been processed */
1693 			/* EMPTY */
1694 			;
1695 		} else {
1696 			if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1697 			    (sectndx == SHN_ABS))
1698 				spec = sdp->sd_aux->sa_symspec;
1699 
1700 			/* LINTED */
1701 			if (sdp->sd_flags & FLG_SY_COMMEXP) {
1702 				/*
1703 				 * This is (or was) a COMMON symbol which was
1704 				 * processed above - no processing
1705 				 * required here.
1706 				 */
1707 				;
1708 			} else if (sdp->sd_ref == REF_DYN_NEED) {
1709 				uchar_t	type, bind;
1710 
1711 				sectndx = SHN_UNDEF;
1712 				sym->st_value = 0;
1713 				sym->st_size = 0;
1714 
1715 				/*
1716 				 * Make sure this undefined symbol is returned
1717 				 * to the same binding as was defined in the
1718 				 * original relocatable object reference.
1719 				 */
1720 				type = ELF_ST_TYPE(sym-> st_info);
1721 				if (sdp->sd_flags & FLG_SY_GLOBREF)
1722 					bind = STB_GLOBAL;
1723 				else
1724 					bind = STB_WEAK;
1725 
1726 				sym->st_info = ELF_ST_INFO(bind, type);
1727 
1728 			} else if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
1729 			    (sdp->sd_ref == REF_REL_NEED)) {
1730 				osp = sdp->sd_isc->is_osdesc;
1731 				/* LINTED */
1732 				sectndx = elf_ndxscn(osp->os_scn);
1733 
1734 				/*
1735 				 * In an executable, the new symbol value is the
1736 				 * old value (offset into defining section) plus
1737 				 * virtual address of defining section.  In a
1738 				 * relocatable, the new value is the old value
1739 				 * plus the displacement of the section within
1740 				 * the file.
1741 				 */
1742 				/* LINTED */
1743 				sym->st_value +=
1744 				    (Off)_elf_getxoff(sdp->sd_isc->is_indata);
1745 
1746 				if (!(flags & FLG_OF_RELOBJ)) {
1747 					sym->st_value += osp->os_shdr->sh_addr;
1748 					/*
1749 					 * TLS symbols are relative to
1750 					 * the TLS segment.
1751 					 */
1752 					if ((ELF_ST_TYPE(sym->st_info) ==
1753 					    STT_TLS) && (ofl->ofl_tlsphdr))
1754 						sym->st_value -=
1755 						    ofl->ofl_tlsphdr->p_vaddr;
1756 				}
1757 			}
1758 		}
1759 
1760 		if (spec) {
1761 			switch (spec) {
1762 			case SDAUX_ID_ETEXT:
1763 				sym->st_value = etext;
1764 				sectndx = etext_ndx;
1765 				if (etext_abs)
1766 					sdp->sd_flags |= FLG_SY_SPECSEC;
1767 				else
1768 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1769 				break;
1770 			case SDAUX_ID_EDATA:
1771 				sym->st_value = edata;
1772 				sectndx = edata_ndx;
1773 				if (edata_abs)
1774 					sdp->sd_flags |= FLG_SY_SPECSEC;
1775 				else
1776 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1777 				break;
1778 			case SDAUX_ID_END:
1779 				sym->st_value = end;
1780 				sectndx = end_ndx;
1781 				if (end_abs)
1782 					sdp->sd_flags |= FLG_SY_SPECSEC;
1783 				else
1784 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1785 				break;
1786 			case SDAUX_ID_START:
1787 				sym->st_value = start;
1788 				sectndx = start_ndx;
1789 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1790 				break;
1791 			case SDAUX_ID_DYN:
1792 				if (flags & FLG_OF_DYNAMIC) {
1793 					sym->st_value = ofl->
1794 					    ofl_osdynamic->os_shdr->sh_addr;
1795 					/* LINTED */
1796 					sectndx = elf_ndxscn(
1797 					    ofl->ofl_osdynamic->os_scn);
1798 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1799 				}
1800 				break;
1801 			case SDAUX_ID_PLT:
1802 				if (ofl->ofl_osplt) {
1803 					sym->st_value = ofl->
1804 					    ofl_osplt->os_shdr->sh_addr;
1805 					/* LINTED */
1806 					sectndx = elf_ndxscn(
1807 					    ofl->ofl_osplt->os_scn);
1808 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1809 				}
1810 				break;
1811 			case SDAUX_ID_GOT:
1812 				/*
1813 				 * Symbol bias for negative growing tables is
1814 				 * stored in symbol's value during
1815 				 * allocate_got().
1816 				 */
1817 				sym->st_value += ofl->
1818 				    ofl_osgot->os_shdr->sh_addr;
1819 				/* LINTED */
1820 				sectndx = elf_ndxscn(ofl->
1821 				    ofl_osgot->os_scn);
1822 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1823 				break;
1824 			case SDAUX_ID_SECBOUND_START:
1825 				sym->st_value = sap->sa_boundsec->
1826 				    os_shdr->sh_addr;
1827 				sectndx = elf_ndxscn(sap->sa_boundsec->os_scn);
1828 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1829 				break;
1830 			case SDAUX_ID_SECBOUND_STOP:
1831 				sym->st_value = sap->sa_boundsec->
1832 				    os_shdr->sh_addr +
1833 				    sap->sa_boundsec->os_shdr->sh_size;
1834 				sectndx = elf_ndxscn(sap->sa_boundsec->os_scn);
1835 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1836 				break;
1837 			default:
1838 				/* NOTHING */
1839 				;
1840 			}
1841 		}
1842 
1843 		/*
1844 		 * If a plt index has been assigned to an undefined function,
1845 		 * update the symbols value to the appropriate .plt address.
1846 		 */
1847 		if ((flags & FLG_OF_DYNAMIC) && (flags & FLG_OF_EXEC) &&
1848 		    (sdp->sd_file) &&
1849 		    (sdp->sd_file->ifl_ehdr->e_type == ET_DYN) &&
1850 		    (ELF_ST_TYPE(sym->st_info) == STT_FUNC) &&
1851 		    !(flags & FLG_OF_BFLAG)) {
1852 			if (sap->sa_PLTndx)
1853 				sym->st_value =
1854 				    (*ld_targ.t_mr.mr_calc_plt_addr)(sdp, ofl);
1855 		}
1856 
1857 		/*
1858 		 * Finish updating the symbols.
1859 		 */
1860 
1861 		/*
1862 		 * Sym Update: if scoped local - set local binding
1863 		 */
1864 		if (local)
1865 			sym->st_info = ELF_ST_INFO(STB_LOCAL,
1866 			    ELF_ST_TYPE(sym->st_info));
1867 
1868 		/*
1869 		 * Sym Updated: If both the .symtab and .dynsym
1870 		 * are present then we've actually updated the information in
1871 		 * the .dynsym, therefore copy this same information to the
1872 		 * .symtab entry.
1873 		 */
1874 		sdp->sd_shndx = sectndx;
1875 		if (enter_in_symtab && dynsym && (!local || dynlocal)) {
1876 			Word _symndx = dynlocal ? scopesym_ndx : symtab_ndx;
1877 
1878 			symtab[_symndx].st_value = sym->st_value;
1879 			symtab[_symndx].st_size = sym->st_size;
1880 			symtab[_symndx].st_info = sym->st_info;
1881 			symtab[_symndx].st_other = sym->st_other;
1882 		}
1883 
1884 		if (enter_in_symtab) {
1885 			Word	_symndx;
1886 
1887 			if (local)
1888 				_symndx = scopesym_ndx++;
1889 			else
1890 				_symndx = symtab_ndx++;
1891 			if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
1892 			    (sectndx >= SHN_LORESERVE)) {
1893 				assert(symshndx != NULL);
1894 				symshndx[_symndx] = sectndx;
1895 				symtab[_symndx].st_shndx = SHN_XINDEX;
1896 			} else {
1897 				/* LINTED */
1898 				symtab[_symndx].st_shndx = (Half)sectndx;
1899 			}
1900 		}
1901 
1902 		if (dynsym && (!local || dynlocal)) {
1903 			/*
1904 			 * dynsym and ldynsym are distinct tables, so
1905 			 * we use indirection to access the right one
1906 			 * and the related extended section index array.
1907 			 */
1908 			Word	_symndx;
1909 			Sym	*_dynsym;
1910 			Word	*_dynshndx;
1911 
1912 			if (!local) {
1913 				_symndx = dynsym_ndx++;
1914 				_dynsym = dynsym;
1915 				_dynshndx = dynshndx;
1916 			} else {
1917 				_symndx = ldynscopesym_ndx++;
1918 				_dynsym = ldynsym;
1919 				_dynshndx = ldynshndx;
1920 			}
1921 			if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
1922 			    (sectndx >= SHN_LORESERVE)) {
1923 				assert(_dynshndx != NULL);
1924 				_dynshndx[_symndx] = sectndx;
1925 				_dynsym[_symndx].st_shndx = SHN_XINDEX;
1926 			} else {
1927 				/* LINTED */
1928 				_dynsym[_symndx].st_shndx = (Half)sectndx;
1929 			}
1930 		}
1931 
1932 		DBG_CALL(Dbg_syms_new(ofl, sym, sdp));
1933 	}
1934 
1935 	/*
1936 	 * Now that all the symbols have been processed update any weak symbols
1937 	 * information (ie. copy all information except `st_name').  As both
1938 	 * symbols will be represented in the output, return the weak symbol to
1939 	 * its correct type.
1940 	 */
1941 	for (ALIST_TRAVERSE(weak, idx1, wkp)) {
1942 		Sym_desc	*sdp, *_sdp;
1943 		Sym		*sym, *_sym, *__sym;
1944 		uchar_t		bind;
1945 
1946 		sdp = wkp->wk_weak;
1947 		_sdp = wkp->wk_alias;
1948 		_sym = __sym = _sdp->sd_sym;
1949 
1950 		sdp->sd_flags |= FLG_SY_WEAKDEF;
1951 
1952 		/*
1953 		 * If the symbol definition has been scoped then assign it to
1954 		 * be local, otherwise if it's from a shared object then we need
1955 		 * to maintain the binding of the original reference.
1956 		 */
1957 		if (SYM_IS_HIDDEN(sdp)) {
1958 			if (ld_sym_reducable(ofl, sdp))
1959 				bind = STB_LOCAL;
1960 			else
1961 				bind = STB_WEAK;
1962 		} else if ((sdp->sd_ref == REF_DYN_NEED) &&
1963 		    (sdp->sd_flags & FLG_SY_GLOBREF))
1964 			bind = STB_GLOBAL;
1965 		else
1966 			bind = STB_WEAK;
1967 
1968 		DBG_CALL(Dbg_syms_old(ofl, sdp));
1969 		if ((sym = wkp->wk_symtab) != NULL) {
1970 			sym->st_value = _sym->st_value;
1971 			sym->st_size = _sym->st_size;
1972 			sym->st_other = _sym->st_other;
1973 			sym->st_shndx = _sym->st_shndx;
1974 			sym->st_info = ELF_ST_INFO(bind,
1975 			    ELF_ST_TYPE(sym->st_info));
1976 			__sym = sym;
1977 		}
1978 		if ((sym = wkp->wk_dynsym) != NULL) {
1979 			sym->st_value = _sym->st_value;
1980 			sym->st_size = _sym->st_size;
1981 			sym->st_other = _sym->st_other;
1982 			sym->st_shndx = _sym->st_shndx;
1983 			sym->st_info = ELF_ST_INFO(bind,
1984 			    ELF_ST_TYPE(sym->st_info));
1985 			__sym = sym;
1986 		}
1987 		DBG_CALL(Dbg_syms_new(ofl, __sym, sdp));
1988 	}
1989 
1990 	/*
1991 	 * Now display GOT debugging information if required.
1992 	 */
1993 	DBG_CALL(Dbg_got_display(ofl, 0, 0,
1994 	    ld_targ.t_m.m_got_xnumber, ld_targ.t_m.m_got_entsize));
1995 
1996 	/*
1997 	 * Update the section headers information. sh_info is
1998 	 * supposed to contain the offset at which the first
1999 	 * global symbol resides in the symbol table, while
2000 	 * sh_link contains the section index of the associated
2001 	 * string table.
2002 	 */
2003 	if (symtab) {
2004 		Shdr	*shdr = ofl->ofl_ossymtab->os_shdr;
2005 
2006 		shdr->sh_info = symtab_gbl_bndx;
2007 		/* LINTED */
2008 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osstrtab->os_scn);
2009 		if (symshndx)
2010 			ofl->ofl_ossymshndx->os_shdr->sh_link =
2011 			    (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
2012 
2013 		/*
2014 		 * Ensure that the expected number of symbols
2015 		 * were entered into the right spots:
2016 		 *	- Scoped symbols in the right range
2017 		 *	- Globals start at the right spot
2018 		 *		(correct number of locals entered)
2019 		 *	- The table is exactly filled
2020 		 *		(correct number of globals entered)
2021 		 */
2022 		assert((scopesym_bndx + ofl->ofl_scopecnt) == scopesym_ndx);
2023 		assert(shdr->sh_info == SYMTAB_LOC_CNT(ofl));
2024 		assert((shdr->sh_info + ofl->ofl_globcnt) == symtab_ndx);
2025 	}
2026 	if (dynsym) {
2027 		Shdr	*shdr = ofl->ofl_osdynsym->os_shdr;
2028 
2029 		shdr->sh_info = DYNSYM_LOC_CNT(ofl);
2030 		/* LINTED */
2031 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
2032 
2033 		ofl->ofl_oshash->os_shdr->sh_link =
2034 		    /* LINTED */
2035 		    (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
2036 		if (dynshndx) {
2037 			shdr = ofl->ofl_osdynshndx->os_shdr;
2038 			shdr->sh_link =
2039 			    (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
2040 		}
2041 	}
2042 	if (ldynsym) {
2043 		Shdr	*shdr = ofl->ofl_osldynsym->os_shdr;
2044 
2045 		/* ldynsym has no globals, so give index one past the end */
2046 		shdr->sh_info = ldynsym_ndx;
2047 
2048 		/*
2049 		 * The ldynsym and dynsym must be adjacent. The
2050 		 * idea is that rtld should be able to start with
2051 		 * the ldynsym and march straight through the end
2052 		 * of dynsym, seeing them as a single symbol table,
2053 		 * despite the fact that they are in distinct sections.
2054 		 * Ensure that this happened correctly.
2055 		 *
2056 		 * Note that I use ldynsym_ndx here instead of the
2057 		 * computation I used to set the section size
2058 		 * (found in ldynsym_cnt). The two will agree, unless
2059 		 * we somehow miscounted symbols or failed to insert them
2060 		 * all. Using ldynsym_ndx here catches that error in
2061 		 * addition to checking for adjacency.
2062 		 */
2063 		assert(dynsym == (ldynsym + ldynsym_ndx));
2064 
2065 
2066 		/* LINTED */
2067 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
2068 
2069 		if (ldynshndx) {
2070 			shdr = ofl->ofl_osldynshndx->os_shdr;
2071 			shdr->sh_link =
2072 			    (Word)elf_ndxscn(ofl->ofl_osldynsym->os_scn);
2073 		}
2074 
2075 		/*
2076 		 * The presence of .SUNW_ldynsym means that there may be
2077 		 * associated sort sections, one for regular symbols
2078 		 * and the other for TLS. Each sort section needs the
2079 		 * following done:
2080 		 *	- Section header link references .SUNW_ldynsym
2081 		 *	- Should have received the expected # of items
2082 		 *	- Sorted by increasing address
2083 		 */
2084 		if (ofl->ofl_osdynsymsort) {	/* .SUNW_dynsymsort */
2085 			ofl->ofl_osdynsymsort->os_shdr->sh_link =
2086 			    (Word)elf_ndxscn(ofl->ofl_osldynsym->os_scn);
2087 			assert(ofl->ofl_dynsymsortcnt == dynsymsort_ndx);
2088 
2089 			if (dynsymsort_ndx > 1) {
2090 				dynsort_compare_syms = ldynsym;
2091 				qsort(dynsymsort, dynsymsort_ndx,
2092 				    sizeof (*dynsymsort), dynsort_compare);
2093 				dynsort_dupwarn(ofl, ldynsym,
2094 				    st_getstrbuf(dynstr),
2095 				    dynsymsort, dynsymsort_ndx,
2096 				    MSG_ORIG(MSG_SCN_DYNSYMSORT));
2097 			}
2098 		}
2099 		if (ofl->ofl_osdyntlssort) {	/* .SUNW_dyntlssort */
2100 			ofl->ofl_osdyntlssort->os_shdr->sh_link =
2101 			    (Word)elf_ndxscn(ofl->ofl_osldynsym->os_scn);
2102 			assert(ofl->ofl_dyntlssortcnt == dyntlssort_ndx);
2103 
2104 			if (dyntlssort_ndx > 1) {
2105 				dynsort_compare_syms = ldynsym;
2106 				qsort(dyntlssort, dyntlssort_ndx,
2107 				    sizeof (*dyntlssort), dynsort_compare);
2108 				dynsort_dupwarn(ofl, ldynsym,
2109 				    st_getstrbuf(dynstr),
2110 				    dyntlssort, dyntlssort_ndx,
2111 				    MSG_ORIG(MSG_SCN_DYNTLSSORT));
2112 			}
2113 		}
2114 	}
2115 
2116 	/*
2117 	 * Used by ld.so.1 only.
2118 	 */
2119 	return (etext);
2120 
2121 #undef ADD_TO_DYNSORT
2122 }
2123 
2124 /*
2125  * Build the dynamic section.
2126  *
2127  * This routine must be maintained in parallel with make_dynamic()
2128  * in sections.c
2129  */
2130 static int
update_odynamic(Ofl_desc * ofl)2131 update_odynamic(Ofl_desc *ofl)
2132 {
2133 	Aliste		idx;
2134 	Ifl_desc	*ifl;
2135 	Sym_desc	*sdp;
2136 	Shdr		*shdr;
2137 	Dyn		*_dyn = (Dyn *)ofl->ofl_osdynamic->os_outdata->d_buf;
2138 	Dyn		*dyn;
2139 	Os_desc		*symosp, *strosp;
2140 	Str_tbl		*strtbl;
2141 	size_t		stoff;
2142 	ofl_flag_t	flags = ofl->ofl_flags;
2143 	int		not_relobj = !(flags & FLG_OF_RELOBJ);
2144 	Word		cnt;
2145 
2146 	/*
2147 	 * Relocatable objects can be built with -r and -dy to trigger the
2148 	 * creation of a .dynamic section.  This model is used to create kernel
2149 	 * device drivers.  The .dynamic section provides a subset of userland
2150 	 * .dynamic entries, typically entries such as DT_NEEDED and DT_RUNPATH.
2151 	 *
2152 	 * Within a dynamic object, any .dynamic string references are to the
2153 	 * .dynstr table.  Within a relocatable object, these strings can reside
2154 	 * within the .strtab.
2155 	 */
2156 	if (OFL_IS_STATIC_OBJ(ofl)) {
2157 		symosp = ofl->ofl_ossymtab;
2158 		strosp = ofl->ofl_osstrtab;
2159 		strtbl = ofl->ofl_strtab;
2160 	} else {
2161 		symosp = ofl->ofl_osdynsym;
2162 		strosp = ofl->ofl_osdynstr;
2163 		strtbl = ofl->ofl_dynstrtab;
2164 	}
2165 
2166 	/* LINTED */
2167 	ofl->ofl_osdynamic->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
2168 
2169 	dyn = _dyn;
2170 
2171 	for (APLIST_TRAVERSE(ofl->ofl_sos, idx, ifl)) {
2172 		if ((ifl->ifl_flags &
2173 		    (FLG_IF_IGNORE | FLG_IF_DEPREQD)) == FLG_IF_IGNORE)
2174 			continue;
2175 
2176 		/*
2177 		 * Create and set up the DT_POSFLAG_1 entry here if required.
2178 		 */
2179 		if ((ifl->ifl_flags & MSK_IF_POSFLAG1) &&
2180 		    (ifl->ifl_flags & FLG_IF_NEEDED) && not_relobj) {
2181 			dyn->d_tag = DT_POSFLAG_1;
2182 			if (ifl->ifl_flags & FLG_IF_LAZYLD)
2183 				dyn->d_un.d_val = DF_P1_LAZYLOAD;
2184 			if (ifl->ifl_flags & FLG_IF_GRPPRM)
2185 				dyn->d_un.d_val |= DF_P1_GROUPPERM;
2186 			if (ifl->ifl_flags & FLG_IF_DEFERRED)
2187 				dyn->d_un.d_val |= DF_P1_DEFERRED;
2188 			dyn++;
2189 		}
2190 
2191 		if (ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR))
2192 			dyn->d_tag = DT_NEEDED;
2193 		else
2194 			continue;
2195 
2196 		(void) st_setstring(strtbl, ifl->ifl_soname, &stoff);
2197 		dyn->d_un.d_val = stoff;
2198 		/* LINTED */
2199 		ifl->ifl_neededndx = (Half)(((uintptr_t)dyn - (uintptr_t)_dyn) /
2200 		    sizeof (Dyn));
2201 		dyn++;
2202 	}
2203 
2204 	if (not_relobj) {
2205 		if (ofl->ofl_dtsfltrs != NULL) {
2206 			Dfltr_desc	*dftp;
2207 
2208 			for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp)) {
2209 				if (dftp->dft_flag == FLG_SY_AUXFLTR)
2210 					dyn->d_tag = DT_SUNW_AUXILIARY;
2211 				else
2212 					dyn->d_tag = DT_SUNW_FILTER;
2213 
2214 				(void) st_setstring(strtbl, dftp->dft_str,
2215 				    &stoff);
2216 				dyn->d_un.d_val = stoff;
2217 				dftp->dft_ndx = (Half)(((uintptr_t)dyn -
2218 				    (uintptr_t)_dyn) / sizeof (Dyn));
2219 				dyn++;
2220 			}
2221 		}
2222 		if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
2223 		    SYM_NOHASH, 0, ofl)) != NULL) &&
2224 		    (sdp->sd_ref == REF_REL_NEED) &&
2225 		    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
2226 			dyn->d_tag = DT_INIT;
2227 			dyn->d_un.d_ptr = sdp->sd_sym->st_value;
2228 			dyn++;
2229 		}
2230 		if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
2231 		    SYM_NOHASH, 0, ofl)) != NULL) &&
2232 		    (sdp->sd_ref == REF_REL_NEED) &&
2233 		    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
2234 			dyn->d_tag = DT_FINI;
2235 			dyn->d_un.d_ptr = sdp->sd_sym->st_value;
2236 			dyn++;
2237 		}
2238 		if (ofl->ofl_soname) {
2239 			dyn->d_tag = DT_SONAME;
2240 			(void) st_setstring(strtbl, ofl->ofl_soname, &stoff);
2241 			dyn->d_un.d_val = stoff;
2242 			dyn++;
2243 		}
2244 		if (ofl->ofl_filtees) {
2245 			if (flags & FLG_OF_AUX) {
2246 				dyn->d_tag = DT_AUXILIARY;
2247 			} else {
2248 				dyn->d_tag = DT_FILTER;
2249 			}
2250 			(void) st_setstring(strtbl, ofl->ofl_filtees, &stoff);
2251 			dyn->d_un.d_val = stoff;
2252 			dyn++;
2253 		}
2254 	}
2255 
2256 	if (ofl->ofl_rpath) {
2257 		(void) st_setstring(strtbl, ofl->ofl_rpath, &stoff);
2258 		dyn->d_tag = DT_RUNPATH;
2259 		dyn->d_un.d_val = stoff;
2260 		dyn++;
2261 		dyn->d_tag = DT_RPATH;
2262 		dyn->d_un.d_val = stoff;
2263 		dyn++;
2264 	}
2265 
2266 	if (not_relobj) {
2267 		Aliste	idx;
2268 		Sg_desc	*sgp;
2269 
2270 		if (ofl->ofl_config) {
2271 			dyn->d_tag = DT_CONFIG;
2272 			(void) st_setstring(strtbl, ofl->ofl_config, &stoff);
2273 			dyn->d_un.d_val = stoff;
2274 			dyn++;
2275 		}
2276 		if (ofl->ofl_depaudit) {
2277 			dyn->d_tag = DT_DEPAUDIT;
2278 			(void) st_setstring(strtbl, ofl->ofl_depaudit, &stoff);
2279 			dyn->d_un.d_val = stoff;
2280 			dyn++;
2281 		}
2282 		if (ofl->ofl_audit) {
2283 			dyn->d_tag = DT_AUDIT;
2284 			(void) st_setstring(strtbl, ofl->ofl_audit, &stoff);
2285 			dyn->d_un.d_val = stoff;
2286 			dyn++;
2287 		}
2288 
2289 		dyn->d_tag = DT_HASH;
2290 		dyn->d_un.d_ptr = ofl->ofl_oshash->os_shdr->sh_addr;
2291 		dyn++;
2292 
2293 		shdr = strosp->os_shdr;
2294 		dyn->d_tag = DT_STRTAB;
2295 		dyn->d_un.d_ptr = shdr->sh_addr;
2296 		dyn++;
2297 
2298 		dyn->d_tag = DT_STRSZ;
2299 		dyn->d_un.d_ptr = shdr->sh_size;
2300 		dyn++;
2301 
2302 		/*
2303 		 * Note, the shdr is set and used in the ofl->ofl_osldynsym case
2304 		 * that follows.
2305 		 */
2306 		shdr = symosp->os_shdr;
2307 		dyn->d_tag = DT_SYMTAB;
2308 		dyn->d_un.d_ptr = shdr->sh_addr;
2309 		dyn++;
2310 
2311 		dyn->d_tag = DT_SYMENT;
2312 		dyn->d_un.d_ptr = shdr->sh_entsize;
2313 		dyn++;
2314 
2315 		if (ofl->ofl_osldynsym) {
2316 			Shdr	*lshdr = ofl->ofl_osldynsym->os_shdr;
2317 
2318 			/*
2319 			 * We have arranged for the .SUNW_ldynsym data to be
2320 			 * immediately in front of the .dynsym data.
2321 			 * This means that you could start at the top
2322 			 * of .SUNW_ldynsym and see the data for both tables
2323 			 * without a break. This is the view we want to
2324 			 * provide for DT_SUNW_SYMTAB, which is why we
2325 			 * add the lengths together.
2326 			 */
2327 			dyn->d_tag = DT_SUNW_SYMTAB;
2328 			dyn->d_un.d_ptr = lshdr->sh_addr;
2329 			dyn++;
2330 
2331 			dyn->d_tag = DT_SUNW_SYMSZ;
2332 			dyn->d_un.d_val = lshdr->sh_size + shdr->sh_size;
2333 			dyn++;
2334 		}
2335 
2336 		if (ofl->ofl_osdynsymsort || ofl->ofl_osdyntlssort) {
2337 			dyn->d_tag = DT_SUNW_SORTENT;
2338 			dyn->d_un.d_val = sizeof (Word);
2339 			dyn++;
2340 		}
2341 
2342 		if (ofl->ofl_osdynsymsort) {
2343 			shdr = ofl->ofl_osdynsymsort->os_shdr;
2344 
2345 			dyn->d_tag = DT_SUNW_SYMSORT;
2346 			dyn->d_un.d_ptr = shdr->sh_addr;
2347 			dyn++;
2348 
2349 			dyn->d_tag = DT_SUNW_SYMSORTSZ;
2350 			dyn->d_un.d_val = shdr->sh_size;
2351 			dyn++;
2352 		}
2353 
2354 		if (ofl->ofl_osdyntlssort) {
2355 			shdr = ofl->ofl_osdyntlssort->os_shdr;
2356 
2357 			dyn->d_tag = DT_SUNW_TLSSORT;
2358 			dyn->d_un.d_ptr = shdr->sh_addr;
2359 			dyn++;
2360 
2361 			dyn->d_tag = DT_SUNW_TLSSORTSZ;
2362 			dyn->d_un.d_val = shdr->sh_size;
2363 			dyn++;
2364 		}
2365 
2366 		/*
2367 		 * Reserve the DT_CHECKSUM entry.  Its value will be filled in
2368 		 * after the complete image is built.
2369 		 */
2370 		dyn->d_tag = DT_CHECKSUM;
2371 		ofl->ofl_checksum = &dyn->d_un.d_val;
2372 		dyn++;
2373 
2374 		/*
2375 		 * Versioning sections: DT_VERDEF and DT_VERNEED.
2376 		 *
2377 		 * The Solaris ld does not produce DT_VERSYM, but the GNU ld
2378 		 * does, in order to support their style of versioning, which
2379 		 * differs from ours:
2380 		 *
2381 		 *	- The top bit of the 16-bit Versym index is
2382 		 *		not part of the version, but is interpreted
2383 		 *		as a "hidden bit".
2384 		 *
2385 		 *	- External (SHN_UNDEF) symbols can have non-zero
2386 		 *		Versym values, which specify versions in
2387 		 *		referenced objects, via the Verneed section.
2388 		 *
2389 		 *	- The vna_other field of the Vernaux structures
2390 		 *		found in the Verneed section are not zero as
2391 		 *		with Solaris, but instead contain the version
2392 		 *		index to be used by Versym indices to reference
2393 		 *		the given external version.
2394 		 *
2395 		 * The Solaris ld, rtld, and elfdump programs all interpret the
2396 		 * presence of DT_VERSYM as meaning that GNU versioning rules
2397 		 * apply to the given file. If DT_VERSYM is not present,
2398 		 * then Solaris versioning rules apply. If we should ever need
2399 		 * to change our ld so that it does issue DT_VERSYM, then
2400 		 * this rule for detecting GNU versioning will no longer work.
2401 		 * In that case, we will have to invent a way to explicitly
2402 		 * specify the style of versioning in use, perhaps via a
2403 		 * new dynamic entry named something like DT_SUNW_VERSIONSTYLE,
2404 		 * where the d_un.d_val value specifies which style is to be
2405 		 * used.
2406 		 */
2407 		if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
2408 		    FLG_OF_VERDEF) {
2409 			shdr = ofl->ofl_osverdef->os_shdr;
2410 
2411 			dyn->d_tag = DT_VERDEF;
2412 			dyn->d_un.d_ptr = shdr->sh_addr;
2413 			dyn++;
2414 			dyn->d_tag = DT_VERDEFNUM;
2415 			dyn->d_un.d_ptr = shdr->sh_info;
2416 			dyn++;
2417 		}
2418 		if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
2419 		    FLG_OF_VERNEED) {
2420 			shdr = ofl->ofl_osverneed->os_shdr;
2421 
2422 			dyn->d_tag = DT_VERNEED;
2423 			dyn->d_un.d_ptr = shdr->sh_addr;
2424 			dyn++;
2425 			dyn->d_tag = DT_VERNEEDNUM;
2426 			dyn->d_un.d_ptr = shdr->sh_info;
2427 			dyn++;
2428 		}
2429 
2430 		if ((flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt) {
2431 			dyn->d_tag = ld_targ.t_m.m_rel_dt_count;
2432 			dyn->d_un.d_val = ofl->ofl_relocrelcnt;
2433 			dyn++;
2434 		}
2435 		if (flags & FLG_OF_TEXTREL) {
2436 			/*
2437 			 * Only the presence of this entry is used in this
2438 			 * implementation, not the value stored.
2439 			 */
2440 			dyn->d_tag = DT_TEXTREL;
2441 			dyn->d_un.d_val = 0;
2442 			dyn++;
2443 		}
2444 
2445 		if (ofl->ofl_osfiniarray) {
2446 			shdr = ofl->ofl_osfiniarray->os_shdr;
2447 
2448 			dyn->d_tag = DT_FINI_ARRAY;
2449 			dyn->d_un.d_ptr = shdr->sh_addr;
2450 			dyn++;
2451 
2452 			dyn->d_tag = DT_FINI_ARRAYSZ;
2453 			dyn->d_un.d_val = shdr->sh_size;
2454 			dyn++;
2455 		}
2456 
2457 		if (ofl->ofl_osinitarray) {
2458 			shdr = ofl->ofl_osinitarray->os_shdr;
2459 
2460 			dyn->d_tag = DT_INIT_ARRAY;
2461 			dyn->d_un.d_ptr = shdr->sh_addr;
2462 			dyn++;
2463 
2464 			dyn->d_tag = DT_INIT_ARRAYSZ;
2465 			dyn->d_un.d_val = shdr->sh_size;
2466 			dyn++;
2467 		}
2468 
2469 		if (ofl->ofl_ospreinitarray) {
2470 			shdr = ofl->ofl_ospreinitarray->os_shdr;
2471 
2472 			dyn->d_tag = DT_PREINIT_ARRAY;
2473 			dyn->d_un.d_ptr = shdr->sh_addr;
2474 			dyn++;
2475 
2476 			dyn->d_tag = DT_PREINIT_ARRAYSZ;
2477 			dyn->d_un.d_val = shdr->sh_size;
2478 			dyn++;
2479 		}
2480 
2481 		if (ofl->ofl_pltcnt) {
2482 			shdr = ofl->ofl_osplt->os_relosdesc->os_shdr;
2483 
2484 			dyn->d_tag = DT_PLTRELSZ;
2485 			dyn->d_un.d_ptr = shdr->sh_size;
2486 			dyn++;
2487 			dyn->d_tag = DT_PLTREL;
2488 			dyn->d_un.d_ptr = ld_targ.t_m.m_rel_dt_type;
2489 			dyn++;
2490 			dyn->d_tag = DT_JMPREL;
2491 			dyn->d_un.d_ptr = shdr->sh_addr;
2492 			dyn++;
2493 		}
2494 		if (ofl->ofl_pltpad) {
2495 			shdr = ofl->ofl_osplt->os_shdr;
2496 
2497 			dyn->d_tag = DT_PLTPAD;
2498 			if (ofl->ofl_pltcnt) {
2499 				dyn->d_un.d_ptr = shdr->sh_addr +
2500 				    ld_targ.t_m.m_plt_reservsz +
2501 				    ofl->ofl_pltcnt * ld_targ.t_m.m_plt_entsize;
2502 			} else
2503 				dyn->d_un.d_ptr = shdr->sh_addr;
2504 			dyn++;
2505 			dyn->d_tag = DT_PLTPADSZ;
2506 			dyn->d_un.d_val = ofl->ofl_pltpad *
2507 			    ld_targ.t_m.m_plt_entsize;
2508 			dyn++;
2509 		}
2510 		if (ofl->ofl_relocsz) {
2511 			shdr = ofl->ofl_osrelhead->os_shdr;
2512 
2513 			dyn->d_tag = ld_targ.t_m.m_rel_dt_type;
2514 			dyn->d_un.d_ptr = shdr->sh_addr;
2515 			dyn++;
2516 			dyn->d_tag = ld_targ.t_m.m_rel_dt_size;
2517 			dyn->d_un.d_ptr = ofl->ofl_relocsz;
2518 			dyn++;
2519 			dyn->d_tag = ld_targ.t_m.m_rel_dt_ent;
2520 			if (shdr->sh_type == SHT_REL)
2521 				dyn->d_un.d_ptr = sizeof (Rel);
2522 			else
2523 				dyn->d_un.d_ptr = sizeof (Rela);
2524 			dyn++;
2525 		}
2526 		if (ofl->ofl_ossyminfo) {
2527 			shdr = ofl->ofl_ossyminfo->os_shdr;
2528 
2529 			dyn->d_tag = DT_SYMINFO;
2530 			dyn->d_un.d_ptr = shdr->sh_addr;
2531 			dyn++;
2532 			dyn->d_tag = DT_SYMINSZ;
2533 			dyn->d_un.d_val = shdr->sh_size;
2534 			dyn++;
2535 			dyn->d_tag = DT_SYMINENT;
2536 			dyn->d_un.d_val = sizeof (Syminfo);
2537 			dyn++;
2538 		}
2539 		if (ofl->ofl_osmove) {
2540 			shdr = ofl->ofl_osmove->os_shdr;
2541 
2542 			dyn->d_tag = DT_MOVETAB;
2543 			dyn->d_un.d_val = shdr->sh_addr;
2544 			dyn++;
2545 			dyn->d_tag = DT_MOVESZ;
2546 			dyn->d_un.d_val = shdr->sh_size;
2547 			dyn++;
2548 			dyn->d_tag = DT_MOVEENT;
2549 			dyn->d_un.d_val = shdr->sh_entsize;
2550 			dyn++;
2551 		}
2552 		if (ofl->ofl_regsymcnt) {
2553 			int	ndx;
2554 
2555 			for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
2556 				if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
2557 					continue;
2558 
2559 				dyn->d_tag = ld_targ.t_m.m_dt_register;
2560 				dyn->d_un.d_val = sdp->sd_symndx;
2561 				dyn++;
2562 			}
2563 		}
2564 
2565 		for (APLIST_TRAVERSE(ofl->ofl_rtldinfo, idx, sdp)) {
2566 			dyn->d_tag = DT_SUNW_RTLDINF;
2567 			dyn->d_un.d_ptr = sdp->sd_sym->st_value;
2568 			dyn++;
2569 		}
2570 
2571 		if (((sgp = ofl->ofl_osdynamic->os_sgdesc) != NULL) &&
2572 		    (sgp->sg_phdr.p_flags & PF_W) && ofl->ofl_osinterp) {
2573 			dyn->d_tag = DT_DEBUG;
2574 			dyn->d_un.d_ptr = 0;
2575 			dyn++;
2576 		}
2577 
2578 		if (ofl->ofl_oscap) {
2579 			dyn->d_tag = DT_SUNW_CAP;
2580 			dyn->d_un.d_val = ofl->ofl_oscap->os_shdr->sh_addr;
2581 			dyn++;
2582 		}
2583 		if (ofl->ofl_oscapinfo) {
2584 			dyn->d_tag = DT_SUNW_CAPINFO;
2585 			dyn->d_un.d_val = ofl->ofl_oscapinfo->os_shdr->sh_addr;
2586 			dyn++;
2587 		}
2588 		if (ofl->ofl_oscapchain) {
2589 			shdr = ofl->ofl_oscapchain->os_shdr;
2590 
2591 			dyn->d_tag = DT_SUNW_CAPCHAIN;
2592 			dyn->d_un.d_val = shdr->sh_addr;
2593 			dyn++;
2594 			dyn->d_tag = DT_SUNW_CAPCHAINSZ;
2595 			dyn->d_un.d_val = shdr->sh_size;
2596 			dyn++;
2597 			dyn->d_tag = DT_SUNW_CAPCHAINENT;
2598 			dyn->d_un.d_val = shdr->sh_entsize;
2599 			dyn++;
2600 		}
2601 
2602 		if (ofl->ofl_aslr != 0) {
2603 			dyn->d_tag = DT_SUNW_ASLR;
2604 			dyn->d_un.d_val = (ofl->ofl_aslr == 1);
2605 			dyn++;
2606 		}
2607 
2608 		if (flags & FLG_OF_SYMBOLIC) {
2609 			dyn->d_tag = DT_SYMBOLIC;
2610 			dyn->d_un.d_val = 0;
2611 			dyn++;
2612 		}
2613 	}
2614 
2615 	dyn->d_tag = DT_FLAGS;
2616 	dyn->d_un.d_val = ofl->ofl_dtflags;
2617 	dyn++;
2618 
2619 	/*
2620 	 * If -Bdirect was specified, but some NODIRECT symbols were specified
2621 	 * via a mapfile, or -znodirect was used on the command line, then
2622 	 * clear the DF_1_DIRECT flag.  The resultant object will use per-symbol
2623 	 * direct bindings rather than be enabled for global direct bindings.
2624 	 *
2625 	 * If any no-direct bindings exist within this object, set the
2626 	 * DF_1_NODIRECT flag.  ld(1) recognizes this flag when processing
2627 	 * dependencies, and performs extra work to ensure that no direct
2628 	 * bindings are established to the no-direct symbols that exist
2629 	 * within these dependencies.
2630 	 */
2631 	if (ofl->ofl_flags1 & FLG_OF1_NGLBDIR)
2632 		ofl->ofl_dtflags_1 &= ~DF_1_DIRECT;
2633 	if (ofl->ofl_flags1 & FLG_OF1_NDIRECT)
2634 		ofl->ofl_dtflags_1 |= DF_1_NODIRECT;
2635 
2636 	dyn->d_tag = DT_FLAGS_1;
2637 	dyn->d_un.d_val = ofl->ofl_dtflags_1;
2638 	dyn++;
2639 
2640 	dyn->d_tag = DT_SUNW_STRPAD;
2641 	dyn->d_un.d_val = DYNSTR_EXTRA_PAD;
2642 	dyn++;
2643 
2644 	dyn->d_tag = DT_SUNW_LDMACH;
2645 	dyn->d_un.d_val = ld_sunw_ldmach();
2646 	dyn++;
2647 
2648 	if (ofl->ofl_flags & FLG_OF_KMOD) {
2649 		dyn->d_tag = DT_SUNW_KMOD;
2650 		dyn->d_un.d_val = 1;
2651 		dyn++;
2652 	}
2653 
2654 	(*ld_targ.t_mr.mr_mach_update_odynamic)(ofl, &dyn);
2655 
2656 	for (cnt = 1 + DYNAMIC_EXTRA_ELTS; cnt--; dyn++) {
2657 		dyn->d_tag = DT_NULL;
2658 		dyn->d_un.d_val = 0;
2659 	}
2660 
2661 	/*
2662 	 * Ensure that we wrote the right number of entries. If not, we either
2663 	 * miscounted in make_dynamic(), or we did something wrong in this
2664 	 * function.
2665 	 */
2666 	assert((ofl->ofl_osdynamic->os_shdr->sh_size /
2667 	    ofl->ofl_osdynamic->os_shdr->sh_entsize) ==
2668 	    ((uintptr_t)dyn - (uintptr_t)_dyn) / sizeof (*dyn));
2669 
2670 	return (1);
2671 }
2672 
2673 /*
2674  * Build the version definition section
2675  */
2676 static int
update_overdef(Ofl_desc * ofl)2677 update_overdef(Ofl_desc *ofl)
2678 {
2679 	Aliste		idx1;
2680 	Ver_desc	*vdp, *_vdp;
2681 	Verdef		*vdf, *_vdf;
2682 	int		num = 0;
2683 	Os_desc		*strosp;
2684 	Str_tbl		*strtbl;
2685 
2686 	/*
2687 	 * Determine which string table to use.
2688 	 */
2689 	if (OFL_IS_STATIC_OBJ(ofl)) {
2690 		strtbl = ofl->ofl_strtab;
2691 		strosp = ofl->ofl_osstrtab;
2692 	} else {
2693 		strtbl = ofl->ofl_dynstrtab;
2694 		strosp = ofl->ofl_osdynstr;
2695 	}
2696 
2697 	/*
2698 	 * Traverse the version descriptors and update the version structures
2699 	 * to point to the dynstr name in preparation for building the version
2700 	 * section structure.
2701 	 */
2702 	for (APLIST_TRAVERSE(ofl->ofl_verdesc, idx1, vdp)) {
2703 		Sym_desc	*sdp;
2704 
2705 		if (vdp->vd_flags & VER_FLG_BASE) {
2706 			const char	*name = vdp->vd_name;
2707 			size_t		stoff;
2708 
2709 			/*
2710 			 * Create a new string table entry to represent the base
2711 			 * version name (there is no corresponding symbol for
2712 			 * this).
2713 			 */
2714 			(void) st_setstring(strtbl, name, &stoff);
2715 			/* LINTED */
2716 			vdp->vd_name = (const char *)stoff;
2717 		} else {
2718 			sdp = ld_sym_find(vdp->vd_name, vdp->vd_hash, 0, ofl);
2719 			/* LINTED */
2720 			vdp->vd_name = (const char *)
2721 			    (uintptr_t)sdp->sd_sym->st_name;
2722 		}
2723 	}
2724 
2725 	_vdf = vdf = (Verdef *)ofl->ofl_osverdef->os_outdata->d_buf;
2726 
2727 	/*
2728 	 * Traverse the version descriptors and update the version section to
2729 	 * reflect each version and its associated dependencies.
2730 	 */
2731 	for (APLIST_TRAVERSE(ofl->ofl_verdesc, idx1, vdp)) {
2732 		Aliste		idx2;
2733 		Half		cnt = 1;
2734 		Verdaux		*vdap, *_vdap;
2735 
2736 		_vdap = vdap = (Verdaux *)(vdf + 1);
2737 
2738 		vdf->vd_version = VER_DEF_CURRENT;
2739 		vdf->vd_flags	= vdp->vd_flags & MSK_VER_USER;
2740 		vdf->vd_ndx	= vdp->vd_ndx;
2741 		vdf->vd_hash	= vdp->vd_hash;
2742 
2743 		/* LINTED */
2744 		vdap->vda_name = (uintptr_t)vdp->vd_name;
2745 		vdap++;
2746 		/* LINTED */
2747 		_vdap->vda_next = (Word)((uintptr_t)vdap - (uintptr_t)_vdap);
2748 
2749 		/*
2750 		 * Traverse this versions dependency list generating the
2751 		 * appropriate version dependency entries.
2752 		 */
2753 		for (APLIST_TRAVERSE(vdp->vd_deps, idx2, _vdp)) {
2754 			/* LINTED */
2755 			vdap->vda_name = (uintptr_t)_vdp->vd_name;
2756 			_vdap = vdap;
2757 			vdap++, cnt++;
2758 			/* LINTED */
2759 			_vdap->vda_next = (Word)((uintptr_t)vdap -
2760 			    (uintptr_t)_vdap);
2761 		}
2762 		_vdap->vda_next = 0;
2763 
2764 		/*
2765 		 * Record the versions auxiliary array offset and the associated
2766 		 * dependency count.
2767 		 */
2768 		/* LINTED */
2769 		vdf->vd_aux = (Word)((uintptr_t)(vdf + 1) - (uintptr_t)vdf);
2770 		vdf->vd_cnt = cnt;
2771 
2772 		/*
2773 		 * Record the next versions offset and update the version
2774 		 * pointer.  Remember the previous version offset as the very
2775 		 * last structures next pointer should be null.
2776 		 */
2777 		_vdf = vdf;
2778 		vdf = (Verdef *)vdap, num++;
2779 		/* LINTED */
2780 		_vdf->vd_next = (Word)((uintptr_t)vdf - (uintptr_t)_vdf);
2781 	}
2782 	_vdf->vd_next = 0;
2783 
2784 	/*
2785 	 * Record the string table association with the version definition
2786 	 * section, and the symbol table associated with the version symbol
2787 	 * table (the actual contents of the version symbol table are filled
2788 	 * in during symbol update).
2789 	 */
2790 	/* LINTED */
2791 	ofl->ofl_osverdef->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
2792 
2793 	/*
2794 	 * The version definition sections `info' field is used to indicate the
2795 	 * number of entries in this section.
2796 	 */
2797 	ofl->ofl_osverdef->os_shdr->sh_info = num;
2798 
2799 	return (1);
2800 }
2801 
2802 /*
2803  * Finish the version symbol index section
2804  */
2805 static void
update_oversym(Ofl_desc * ofl)2806 update_oversym(Ofl_desc *ofl)
2807 {
2808 	Os_desc		*osp;
2809 
2810 	/*
2811 	 * Record the symbol table associated with the version symbol table.
2812 	 * The contents of the version symbol table are filled in during
2813 	 * symbol update.
2814 	 */
2815 	if (OFL_IS_STATIC_OBJ(ofl))
2816 		osp = ofl->ofl_ossymtab;
2817 	else
2818 		osp = ofl->ofl_osdynsym;
2819 
2820 	/* LINTED */
2821 	ofl->ofl_osversym->os_shdr->sh_link = (Word)elf_ndxscn(osp->os_scn);
2822 }
2823 
2824 /*
2825  * Build the version needed section
2826  */
2827 static int
update_overneed(Ofl_desc * ofl)2828 update_overneed(Ofl_desc *ofl)
2829 {
2830 	Aliste		idx1;
2831 	Ifl_desc	*ifl;
2832 	Verneed		*vnd, *_vnd;
2833 	Os_desc		*strosp;
2834 	Str_tbl		*strtbl;
2835 	Word		num = 0;
2836 
2837 	_vnd = vnd = (Verneed *)ofl->ofl_osverneed->os_outdata->d_buf;
2838 
2839 	/*
2840 	 * Determine which string table is appropriate.
2841 	 */
2842 	if (OFL_IS_STATIC_OBJ(ofl)) {
2843 		strosp = ofl->ofl_osstrtab;
2844 		strtbl = ofl->ofl_strtab;
2845 	} else {
2846 		strosp = ofl->ofl_osdynstr;
2847 		strtbl = ofl->ofl_dynstrtab;
2848 	}
2849 
2850 	/*
2851 	 * Traverse the shared object list looking for dependencies that have
2852 	 * versions defined within them.
2853 	 */
2854 	for (APLIST_TRAVERSE(ofl->ofl_sos, idx1, ifl)) {
2855 		Half		_cnt;
2856 		Word		cnt = 0;
2857 		Vernaux		*_vnap, *vnap;
2858 		size_t		stoff;
2859 
2860 		if (!(ifl->ifl_flags & FLG_IF_VERNEED))
2861 			continue;
2862 
2863 		vnd->vn_version = VER_NEED_CURRENT;
2864 
2865 		(void) st_setstring(strtbl, ifl->ifl_soname, &stoff);
2866 		vnd->vn_file = stoff;
2867 
2868 		_vnap = vnap = (Vernaux *)(vnd + 1);
2869 
2870 		/*
2871 		 * Traverse the version index list recording
2872 		 * each version as a needed dependency.
2873 		 */
2874 		for (_cnt = 0; _cnt <= ifl->ifl_vercnt; _cnt++) {
2875 			Ver_index	*vip = &ifl->ifl_verndx[_cnt];
2876 
2877 			if (vip->vi_flags & FLG_VER_REFER) {
2878 				(void) st_setstring(strtbl, vip->vi_name,
2879 				    &stoff);
2880 				vnap->vna_name = stoff;
2881 
2882 				if (vip->vi_desc) {
2883 					vnap->vna_hash = vip->vi_desc->vd_hash;
2884 					vnap->vna_flags =
2885 					    vip->vi_desc->vd_flags;
2886 				} else {
2887 					vnap->vna_hash = 0;
2888 					vnap->vna_flags = 0;
2889 				}
2890 				vnap->vna_other = vip->vi_overndx;
2891 
2892 				/*
2893 				 * If version A inherits version B, then
2894 				 * B is implicit in A. It suffices for ld.so.1
2895 				 * to verify A at runtime and skip B. The
2896 				 * version normalization process sets the INFO
2897 				 * flag for the versions we want ld.so.1 to
2898 				 * skip.
2899 				 */
2900 				if (vip->vi_flags & VER_FLG_INFO)
2901 					vnap->vna_flags |= VER_FLG_INFO;
2902 
2903 				_vnap = vnap;
2904 				vnap++, cnt++;
2905 				_vnap->vna_next =
2906 				    /* LINTED */
2907 				    (Word)((uintptr_t)vnap - (uintptr_t)_vnap);
2908 			}
2909 		}
2910 
2911 		_vnap->vna_next = 0;
2912 
2913 		/*
2914 		 * Record the versions auxiliary array offset and
2915 		 * the associated dependency count.
2916 		 */
2917 		/* LINTED */
2918 		vnd->vn_aux = (Word)((uintptr_t)(vnd + 1) - (uintptr_t)vnd);
2919 		/* LINTED */
2920 		vnd->vn_cnt = (Half)cnt;
2921 
2922 		/*
2923 		 * Record the next versions offset and update the version
2924 		 * pointer.  Remember the previous version offset as the very
2925 		 * last structures next pointer should be null.
2926 		 */
2927 		_vnd = vnd;
2928 		vnd = (Verneed *)vnap, num++;
2929 		/* LINTED */
2930 		_vnd->vn_next = (Word)((uintptr_t)vnd - (uintptr_t)_vnd);
2931 	}
2932 	_vnd->vn_next = 0;
2933 
2934 	/*
2935 	 * Use sh_link to record the associated string table section, and
2936 	 * sh_info to indicate the number of entries contained in the section.
2937 	 */
2938 	/* LINTED */
2939 	ofl->ofl_osverneed->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
2940 	ofl->ofl_osverneed->os_shdr->sh_info = num;
2941 
2942 	return (1);
2943 }
2944 
2945 /*
2946  * Update syminfo section.
2947  */
2948 static uintptr_t
update_osyminfo(Ofl_desc * ofl)2949 update_osyminfo(Ofl_desc *ofl)
2950 {
2951 	Os_desc		*symosp, *infosp = ofl->ofl_ossyminfo;
2952 	Syminfo		*sip = infosp->os_outdata->d_buf;
2953 	Shdr		*shdr = infosp->os_shdr;
2954 	char		*strtab;
2955 	Aliste		idx;
2956 	Sym_desc	*sdp;
2957 	Sfltr_desc	*sftp;
2958 
2959 	if (ofl->ofl_flags & FLG_OF_RELOBJ) {
2960 		symosp = ofl->ofl_ossymtab;
2961 		strtab = ofl->ofl_osstrtab->os_outdata->d_buf;
2962 	} else {
2963 		symosp = ofl->ofl_osdynsym;
2964 		strtab = ofl->ofl_osdynstr->os_outdata->d_buf;
2965 	}
2966 
2967 	/* LINTED */
2968 	infosp->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
2969 	if (ofl->ofl_osdynamic)
2970 		infosp->os_shdr->sh_info =
2971 		    /* LINTED */
2972 		    (Word)elf_ndxscn(ofl->ofl_osdynamic->os_scn);
2973 
2974 	/*
2975 	 * Update any references with the index into the dynamic table.
2976 	 */
2977 	for (APLIST_TRAVERSE(ofl->ofl_symdtent, idx, sdp))
2978 		sip[sdp->sd_symndx].si_boundto = sdp->sd_file->ifl_neededndx;
2979 
2980 	/*
2981 	 * Update any filtee references with the index into the dynamic table.
2982 	 */
2983 	for (ALIST_TRAVERSE(ofl->ofl_symfltrs, idx, sftp)) {
2984 		Dfltr_desc	*dftp;
2985 
2986 		dftp = alist_item(ofl->ofl_dtsfltrs, sftp->sft_idx);
2987 		sip[sftp->sft_sdp->sd_symndx].si_boundto = dftp->dft_ndx;
2988 	}
2989 
2990 	/*
2991 	 * Display debugging information about section.
2992 	 */
2993 	DBG_CALL(Dbg_syminfo_title(ofl->ofl_lml));
2994 	if (DBG_ENABLED) {
2995 		Word	_cnt, cnt = shdr->sh_size / shdr->sh_entsize;
2996 		Sym	*symtab = symosp->os_outdata->d_buf;
2997 		Dyn	*dyn;
2998 
2999 		if (ofl->ofl_osdynamic)
3000 			dyn = ofl->ofl_osdynamic->os_outdata->d_buf;
3001 		else
3002 			dyn = NULL;
3003 
3004 		for (_cnt = 1; _cnt < cnt; _cnt++) {
3005 			if (sip[_cnt].si_flags || sip[_cnt].si_boundto)
3006 				/* LINTED */
3007 				DBG_CALL(Dbg_syminfo_entry(ofl->ofl_lml, _cnt,
3008 				    &sip[_cnt], &symtab[_cnt], strtab, dyn));
3009 		}
3010 	}
3011 	return (1);
3012 }
3013 
3014 /*
3015  * Build the output elf header.
3016  */
3017 static uintptr_t
update_oehdr(Ofl_desc * ofl)3018 update_oehdr(Ofl_desc * ofl)
3019 {
3020 	Ehdr	*ehdr = ofl->ofl_nehdr;
3021 
3022 	/*
3023 	 * If an entry point symbol has already been established (refer
3024 	 * sym_validate()) simply update the elf header entry point with the
3025 	 * symbols value.  If no entry point is defined it will have been filled
3026 	 * with the start address of the first section within the text segment
3027 	 * (refer update_outfile()).
3028 	 */
3029 	if (ofl->ofl_entry)
3030 		ehdr->e_entry =
3031 		    ((Sym_desc *)(ofl->ofl_entry))->sd_sym->st_value;
3032 
3033 	ehdr->e_ident[EI_DATA] = ld_targ.t_m.m_data;
3034 	ehdr->e_version = ofl->ofl_dehdr->e_version;
3035 
3036 	/*
3037 	 * When generating a relocatable object under -z symbolcap, set the
3038 	 * e_machine to be generic, and remove any e_flags.  Input relocatable
3039 	 * objects may identify alternative e_machine (m.machplus) and e_flags
3040 	 * values.  However, the functions within the created output object
3041 	 * are selected at runtime using the capabilities mechanism, which
3042 	 * supersedes the e-machine and e_flags information.  Therefore,
3043 	 * e_machine and e_flag values are not propagated to the output object,
3044 	 * as these values might prevent the kernel from loading the object
3045 	 * before the runtime linker gets control.
3046 	 */
3047 	if (ofl->ofl_flags & FLG_OF_OTOSCAP) {
3048 		ehdr->e_machine = ld_targ.t_m.m_mach;
3049 		ehdr->e_flags = 0;
3050 	} else {
3051 		/*
3052 		 * Note. it may be necessary to update the e_flags field in the
3053 		 * machine dependent section.
3054 		 */
3055 		ehdr->e_machine = ofl->ofl_dehdr->e_machine;
3056 		ehdr->e_flags = ofl->ofl_dehdr->e_flags;
3057 
3058 		if (ehdr->e_machine != ld_targ.t_m.m_mach) {
3059 			if (ehdr->e_machine != ld_targ.t_m.m_machplus)
3060 				return (S_ERROR);
3061 			if ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0)
3062 				return (S_ERROR);
3063 		}
3064 	}
3065 
3066 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
3067 		ehdr->e_type = ET_DYN;
3068 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
3069 		ehdr->e_type = ET_REL;
3070 	else
3071 		ehdr->e_type = ET_EXEC;
3072 
3073 	return (1);
3074 }
3075 
3076 /*
3077  * Perform move table expansion.
3078  */
3079 static void
expand_move(Ofl_desc * ofl,Sym_desc * sdp,Move * mvp)3080 expand_move(Ofl_desc *ofl, Sym_desc *sdp, Move *mvp)
3081 {
3082 	Os_desc		*osp;
3083 	uchar_t		*taddr, *taddr0;
3084 	Sxword		offset;
3085 	Half		cnt;
3086 	uint_t		stride;
3087 
3088 	osp = ofl->ofl_isparexpn->is_osdesc;
3089 	offset = sdp->sd_sym->st_value - osp->os_shdr->sh_addr;
3090 
3091 	taddr0 = taddr = osp->os_outdata->d_buf;
3092 	taddr += offset;
3093 	taddr = taddr + mvp->m_poffset;
3094 
3095 	for (cnt = 0; cnt < mvp->m_repeat; cnt++) {
3096 		/* LINTED */
3097 		DBG_CALL(Dbg_move_expand(ofl->ofl_lml, mvp,
3098 		    (Addr)(taddr - taddr0)));
3099 		stride = (uint_t)mvp->m_stride + 1;
3100 
3101 		/*
3102 		 * Update the target address based upon the move entry size.
3103 		 * This size was validated in ld_process_move().
3104 		 */
3105 		/* LINTED */
3106 		switch (ELF_M_SIZE(mvp->m_info)) {
3107 		case 1:
3108 			/* LINTED */
3109 			*taddr = (uchar_t)mvp->m_value;
3110 			taddr += stride;
3111 			break;
3112 		case 2:
3113 			/* LINTED */
3114 			*((Half *)taddr) = (Half)mvp->m_value;
3115 			taddr += 2 * stride;
3116 			break;
3117 		case 4:
3118 			/* LINTED */
3119 			*((Word *)taddr) = (Word)mvp->m_value;
3120 			taddr += 4 * stride;
3121 			break;
3122 		case 8:
3123 			/* LINTED */
3124 			*((u_longlong_t *)taddr) = mvp->m_value;
3125 			taddr += 8 * stride;
3126 			break;
3127 		}
3128 	}
3129 }
3130 
3131 /*
3132  * Update Move sections.
3133  */
3134 static void
update_move(Ofl_desc * ofl)3135 update_move(Ofl_desc *ofl)
3136 {
3137 	Word		ndx = 0;
3138 	ofl_flag_t	flags = ofl->ofl_flags;
3139 	Move		*omvp;
3140 	Aliste		idx1;
3141 	Sym_desc	*sdp;
3142 
3143 	/*
3144 	 * Determine the index of the symbol table that will be referenced by
3145 	 * the Move section.
3146 	 */
3147 	if (OFL_ALLOW_DYNSYM(ofl))
3148 		/* LINTED */
3149 		ndx = (Word) elf_ndxscn(ofl->ofl_osdynsym->os_scn);
3150 	else if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ))
3151 		/* LINTED */
3152 		ndx = (Word) elf_ndxscn(ofl->ofl_ossymtab->os_scn);
3153 
3154 	/*
3155 	 * Update sh_link of the Move section, and point to the new Move data.
3156 	 */
3157 	if (ofl->ofl_osmove) {
3158 		ofl->ofl_osmove->os_shdr->sh_link = ndx;
3159 		omvp = (Move *)ofl->ofl_osmove->os_outdata->d_buf;
3160 	}
3161 
3162 	/*
3163 	 * Update symbol entry index
3164 	 */
3165 	for (APLIST_TRAVERSE(ofl->ofl_parsyms, idx1, sdp)) {
3166 		Aliste		idx2;
3167 		Mv_desc		*mdp;
3168 
3169 		/*
3170 		 * Expand move table
3171 		 */
3172 		if (sdp->sd_flags & FLG_SY_PAREXPN) {
3173 			const char	*str;
3174 
3175 			if (flags & FLG_OF_STATIC)
3176 				str = MSG_INTL(MSG_PSYM_EXPREASON1);
3177 			else if (ofl->ofl_flags1 & FLG_OF1_NOPARTI)
3178 				str = MSG_INTL(MSG_PSYM_EXPREASON2);
3179 			else
3180 				str = MSG_INTL(MSG_PSYM_EXPREASON3);
3181 
3182 			DBG_CALL(Dbg_move_parexpn(ofl->ofl_lml,
3183 			    sdp->sd_name, str));
3184 
3185 			for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp)) {
3186 				DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0,
3187 				    mdp->md_move, sdp));
3188 				expand_move(ofl, sdp, mdp->md_move);
3189 			}
3190 			continue;
3191 		}
3192 
3193 		/*
3194 		 * Process move table
3195 		 */
3196 		DBG_CALL(Dbg_move_outmove(ofl->ofl_lml, sdp->sd_name));
3197 
3198 		for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp)) {
3199 			Move	*imvp;
3200 			int	idx = 1;
3201 			Sym	*sym;
3202 
3203 			imvp = mdp->md_move;
3204 			sym = sdp->sd_sym;
3205 
3206 			DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 1, imvp, sdp));
3207 
3208 			*omvp = *imvp;
3209 			if ((flags & FLG_OF_RELOBJ) == 0) {
3210 				if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
3211 					Os_desc	*osp = sdp->sd_isc->is_osdesc;
3212 					Word	ndx = osp->os_identndx;
3213 
3214 					omvp->m_info =
3215 					    /* LINTED */
3216 					    ELF_M_INFO(ndx, imvp->m_info);
3217 
3218 					if (ELF_ST_TYPE(sym->st_info) !=
3219 					    STT_SECTION) {
3220 						omvp->m_poffset =
3221 						    sym->st_value -
3222 						    osp->os_shdr->sh_addr +
3223 						    imvp->m_poffset;
3224 					}
3225 				} else {
3226 					omvp->m_info =
3227 					    /* LINTED */
3228 					    ELF_M_INFO(sdp->sd_symndx,
3229 					    imvp->m_info);
3230 				}
3231 			} else {
3232 				Boolean		isredloc = FALSE;
3233 
3234 				if ((ELF_ST_BIND(sym->st_info) == STB_LOCAL) &&
3235 				    (ofl->ofl_flags & FLG_OF_REDLSYM))
3236 					isredloc = TRUE;
3237 
3238 				if (isredloc && !(sdp->sd_move)) {
3239 					Os_desc	*osp = sdp->sd_isc->is_osdesc;
3240 					Word	ndx = osp->os_identndx;
3241 
3242 					omvp->m_info =
3243 					    /* LINTED */
3244 					    ELF_M_INFO(ndx, imvp->m_info);
3245 
3246 					omvp->m_poffset += sym->st_value;
3247 				} else {
3248 					if (isredloc)
3249 						DBG_CALL(Dbg_syms_reduce(ofl,
3250 						    DBG_SYM_REDUCE_RETAIN,
3251 						    sdp, idx,
3252 						    ofl->ofl_osmove->os_name));
3253 
3254 					omvp->m_info =
3255 					    /* LINTED */
3256 					    ELF_M_INFO(sdp->sd_symndx,
3257 					    imvp->m_info);
3258 				}
3259 			}
3260 
3261 			DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0, omvp, sdp));
3262 			omvp++;
3263 			idx++;
3264 		}
3265 	}
3266 }
3267 
3268 /*
3269  * Scan through the SHT_GROUP output sections.  Update their sh_link/sh_info
3270  * fields as well as the section contents.
3271  */
3272 static uintptr_t
update_ogroup(Ofl_desc * ofl)3273 update_ogroup(Ofl_desc *ofl)
3274 {
3275 	Aliste		idx;
3276 	Os_desc		*osp;
3277 	uintptr_t	error = 0;
3278 
3279 	for (APLIST_TRAVERSE(ofl->ofl_osgroups, idx, osp)) {
3280 		Is_desc		*isp;
3281 		Ifl_desc	*ifl;
3282 		Shdr		*shdr = osp->os_shdr;
3283 		Sym_desc	*sdp;
3284 		Xword		i, grpcnt;
3285 		Word		*gdata;
3286 
3287 		/*
3288 		 * Since input GROUP sections always create unique
3289 		 * output GROUP sections - we know there is only one
3290 		 * item on the list.
3291 		 */
3292 		isp = ld_os_first_isdesc(osp);
3293 
3294 		ifl = isp->is_file;
3295 		sdp = ifl->ifl_oldndx[isp->is_shdr->sh_info];
3296 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
3297 		shdr->sh_info = sdp->sd_symndx;
3298 
3299 		/*
3300 		 * Scan through the group data section and update
3301 		 * all of the links to new values.
3302 		 */
3303 		grpcnt = shdr->sh_size / shdr->sh_entsize;
3304 		gdata = (Word *)osp->os_outdata->d_buf;
3305 
3306 		for (i = 1; i < grpcnt; i++) {
3307 			Is_desc	*_isp = ifl->ifl_isdesc[gdata[i]];
3308 			Os_desc	*_osp = _isp->is_osdesc;
3309 
3310 			/*
3311 			 * If a section in the group is not part of the output
3312 			 * image, something has gone wrong, and corrupted this
3313 			 * group.
3314 			 */
3315 			if (_osp == NULL) {
3316 				ld_eprintf(ofl, ERR_FATAL,
3317 				    MSG_INTL(MSG_GRP_MISSINGOUT),
3318 				    ifl->ifl_name, gdata[i], _isp->is_name,
3319 				    elf_ndxscn(osp->os_scn), osp->os_name);
3320 				error = S_ERROR;
3321 			} else {
3322 				gdata[i] = (Word)elf_ndxscn(_osp->os_scn);
3323 			}
3324 		}
3325 	}
3326 	return (error);
3327 }
3328 
3329 static void
update_ostrtab(Os_desc * osp,Str_tbl * stp,uint_t extra)3330 update_ostrtab(Os_desc *osp, Str_tbl *stp, uint_t extra)
3331 {
3332 	Elf_Data	*data;
3333 
3334 	if (osp == NULL)
3335 		return;
3336 
3337 	data = osp->os_outdata;
3338 	assert(data->d_size == (st_getstrtab_sz(stp) + extra));
3339 	(void) st_setstrbuf(stp, data->d_buf, data->d_size - extra);
3340 	/* If leaving an extra hole at the end, zero it */
3341 	if (extra > 0)
3342 		(void) memset((char *)data->d_buf + data->d_size - extra,
3343 		    0x0, extra);
3344 }
3345 
3346 /*
3347  * Update capabilities information.
3348  *
3349  * If string table capabilities exist, then the associated string must be
3350  * translated into an offset into the string table.
3351  */
3352 static void
update_oscap(Ofl_desc * ofl)3353 update_oscap(Ofl_desc *ofl)
3354 {
3355 	Os_desc		*strosp, *cosp;
3356 	Cap		*cap;
3357 	Str_tbl		*strtbl;
3358 	Capstr		*capstr;
3359 	size_t		stoff;
3360 	Aliste		idx1;
3361 
3362 	/*
3363 	 * Determine which symbol table or string table is appropriate.
3364 	 */
3365 	if (OFL_IS_STATIC_OBJ(ofl)) {
3366 		strosp = ofl->ofl_osstrtab;
3367 		strtbl = ofl->ofl_strtab;
3368 	} else {
3369 		strosp = ofl->ofl_osdynstr;
3370 		strtbl = ofl->ofl_dynstrtab;
3371 	}
3372 
3373 	/*
3374 	 * If symbol capabilities exist, set the sh_link field of the .SUNW_cap
3375 	 * section to the .SUNW_capinfo section.
3376 	 */
3377 	if (ofl->ofl_oscapinfo) {
3378 		cosp = ofl->ofl_oscap;
3379 		cosp->os_shdr->sh_link =
3380 		    (Word)elf_ndxscn(ofl->ofl_oscapinfo->os_scn);
3381 	}
3382 
3383 	/*
3384 	 * If there are capability strings to process, set the sh_info
3385 	 * field of the .SUNW_cap section to the associated string table, and
3386 	 * proceed to process any CA_SUNW_PLAT entries.
3387 	 */
3388 	if ((ofl->ofl_flags & FLG_OF_CAPSTRS) == 0)
3389 		return;
3390 
3391 	cosp = ofl->ofl_oscap;
3392 	cosp->os_shdr->sh_info = (Word)elf_ndxscn(strosp->os_scn);
3393 
3394 	cap = ofl->ofl_oscap->os_outdata->d_buf;
3395 
3396 	/*
3397 	 * Determine whether an object capability identifier, or object
3398 	 * machine/platform capabilities exists.
3399 	 */
3400 	capstr = &ofl->ofl_ocapset.oc_id;
3401 	if (capstr->cs_str) {
3402 		(void) st_setstring(strtbl, capstr->cs_str, &stoff);
3403 		cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3404 	}
3405 	for (ALIST_TRAVERSE(ofl->ofl_ocapset.oc_plat.cl_val, idx1, capstr)) {
3406 		(void) st_setstring(strtbl, capstr->cs_str, &stoff);
3407 		cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3408 	}
3409 	for (ALIST_TRAVERSE(ofl->ofl_ocapset.oc_mach.cl_val, idx1, capstr)) {
3410 		(void) st_setstring(strtbl, capstr->cs_str, &stoff);
3411 		cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3412 	}
3413 
3414 	/*
3415 	 * Determine any symbol capability identifiers, or machine/platform
3416 	 * capabilities.
3417 	 */
3418 	if (ofl->ofl_capgroups) {
3419 		Cap_group	*cgp;
3420 
3421 		for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
3422 			Objcapset	*ocapset = &cgp->cg_set;
3423 			Aliste		idx2;
3424 
3425 			capstr = &ocapset->oc_id;
3426 			if (capstr->cs_str) {
3427 				(void) st_setstring(strtbl, capstr->cs_str,
3428 				    &stoff);
3429 				cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3430 			}
3431 			for (ALIST_TRAVERSE(ocapset->oc_plat.cl_val, idx2,
3432 			    capstr)) {
3433 				(void) st_setstring(strtbl, capstr->cs_str,
3434 				    &stoff);
3435 				cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3436 			}
3437 			for (ALIST_TRAVERSE(ocapset->oc_mach.cl_val, idx2,
3438 			    capstr)) {
3439 				(void) st_setstring(strtbl, capstr->cs_str,
3440 				    &stoff);
3441 				cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3442 			}
3443 		}
3444 	}
3445 }
3446 
3447 /*
3448  * Update the .SUNW_capinfo, and possibly the .SUNW_capchain sections.
3449  */
3450 static void
update_oscapinfo(Ofl_desc * ofl)3451 update_oscapinfo(Ofl_desc *ofl)
3452 {
3453 	Os_desc		*symosp, *ciosp, *ccosp = NULL;
3454 	Capinfo		*ocapinfo;
3455 	Capchain	*ocapchain;
3456 	Cap_avlnode	*cav;
3457 	Word		chainndx = 0;
3458 
3459 	/*
3460 	 * Determine which symbol table is appropriate.
3461 	 */
3462 	if (OFL_IS_STATIC_OBJ(ofl))
3463 		symosp = ofl->ofl_ossymtab;
3464 	else
3465 		symosp = ofl->ofl_osdynsym;
3466 
3467 	/*
3468 	 * Update the .SUNW_capinfo sh_link to point to the appropriate symbol
3469 	 * table section.  If we're creating a dynamic object, the
3470 	 * .SUNW_capinfo sh_info is updated to point to the .SUNW_capchain
3471 	 * section.
3472 	 */
3473 	ciosp = ofl->ofl_oscapinfo;
3474 	ciosp->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
3475 
3476 	if (OFL_IS_STATIC_OBJ(ofl) == 0) {
3477 		ccosp = ofl->ofl_oscapchain;
3478 		ciosp->os_shdr->sh_info = (Word)elf_ndxscn(ccosp->os_scn);
3479 	}
3480 
3481 	/*
3482 	 * Establish the data for each section.  The first element of each
3483 	 * section defines the section's version number.
3484 	 */
3485 	ocapinfo = ciosp->os_outdata->d_buf;
3486 	ocapinfo[0] = CAPINFO_CURRENT;
3487 	if (ccosp) {
3488 		ocapchain = ccosp->os_outdata->d_buf;
3489 		ocapchain[chainndx++] = CAPCHAIN_CURRENT;
3490 	}
3491 
3492 	/*
3493 	 * Traverse all capabilities families.  Each member has a .SUNW_capinfo
3494 	 * assignment.  The .SUNW_capinfo entry differs for relocatable objects
3495 	 * and dynamic objects.
3496 	 *
3497 	 * Relocatable objects:
3498 	 *			ELF_C_GROUP		ELF_C_SYM
3499 	 *
3500 	 * Family lead:		CAPINFO_SUNW_GLOB	lead symbol index
3501 	 * Family lead alias:	CAPINFO_SUNW_GLOB	lead symbol index
3502 	 * Family member:	.SUNW_cap index		lead symbol index
3503 	 *
3504 	 * Dynamic objects:
3505 	 *			ELF_C_GROUP		ELF_C_SYM
3506 	 *
3507 	 * Family lead:		CAPINFO_SUNW_GLOB	.SUNW_capchain index
3508 	 * Family lead alias:	CAPINFO_SUNW_GLOB	.SUNW_capchain index
3509 	 * Family member:	.SUNW_cap index		lead symbol index
3510 	 *
3511 	 * The ELF_C_GROUP field identifies a capabilities symbol.  Lead
3512 	 * capability symbols, and lead capability aliases are identified by
3513 	 * a CAPINFO_SUNW_GLOB group identifier.  For family members, the
3514 	 * ELF_C_GROUP provides an index to the associate capabilities group
3515 	 * (i.e, an index into the SUNW_cap section that defines a group).
3516 	 *
3517 	 * For relocatable objects, the ELF_C_SYM field identifies the lead
3518 	 * capability symbol.  For the lead symbol itself, the .SUNW_capinfo
3519 	 * index is the same as the ELF_C_SYM value.  For lead alias symbols,
3520 	 * the .SUNW_capinfo index differs from the ELF_C_SYM value.  This
3521 	 * differentiation of CAPINFO_SUNW_GLOB symbols allows ld(1) to
3522 	 * identify, and propagate lead alias symbols.  For example, the lead
3523 	 * capability symbol memcpy() would have the ELF_C_SYM for memcpy(),
3524 	 * and the lead alias _memcpy() would also have the ELF_C_SYM for
3525 	 * memcpy().
3526 	 *
3527 	 * For dynamic objects, both a lead capability symbol, and alias symbol
3528 	 * would have a ELF_C_SYM value that represents the same capability
3529 	 * chain index.  The capability chain allows ld.so.1 to traverse a
3530 	 * family chain for a given lead symbol, and select the most appropriate
3531 	 * family member.  The .SUNW_capchain array contains a series of symbol
3532 	 * indexes for each family member:
3533 	 *
3534 	 *    chaincap[n]  chaincap[n + 1]  chaincap[n + 2]  chaincap[n + x]
3535 	 *	foo() ndx    foo%x() ndx	foo%y() ndx	0
3536 	 *
3537 	 * For family members, the ELF_C_SYM value associates the capability
3538 	 * members with their family lead symbol.  This association, although
3539 	 * unused within a dynamic object, allows ld(1) to identify, and
3540 	 * propagate family members when processing relocatable objects.
3541 	 */
3542 	for (cav = avl_first(ofl->ofl_capfamilies); cav;
3543 	    cav = AVL_NEXT(ofl->ofl_capfamilies, cav)) {
3544 		Cap_sym		*csp;
3545 		Aliste		idx;
3546 		Sym_desc	*asdp, *lsdp = cav->cn_symavlnode.sav_sdp;
3547 
3548 		if (ccosp) {
3549 			/*
3550 			 * For a dynamic object, identify this lead symbol, and
3551 			 * point it to the head of a capability chain.  Set the
3552 			 * head of the capability chain to the same lead symbol.
3553 			 */
3554 			ocapinfo[lsdp->sd_symndx] =
3555 			    ELF_C_INFO(chainndx, CAPINFO_SUNW_GLOB);
3556 			ocapchain[chainndx] = lsdp->sd_symndx;
3557 		} else {
3558 			/*
3559 			 * For a relocatable object, identify this lead symbol,
3560 			 * and set the lead symbol index to itself.
3561 			 */
3562 			ocapinfo[lsdp->sd_symndx] =
3563 			    ELF_C_INFO(lsdp->sd_symndx, CAPINFO_SUNW_GLOB);
3564 		}
3565 
3566 		/*
3567 		 * Gather any lead symbol aliases.
3568 		 */
3569 		for (APLIST_TRAVERSE(cav->cn_aliases, idx, asdp)) {
3570 			if (ccosp) {
3571 				/*
3572 				 * For a dynamic object, identify this lead
3573 				 * alias symbol, and point it to the same
3574 				 * capability chain index as the lead symbol.
3575 				 */
3576 				ocapinfo[asdp->sd_symndx] =
3577 				    ELF_C_INFO(chainndx, CAPINFO_SUNW_GLOB);
3578 			} else {
3579 				/*
3580 				 * For a relocatable object, identify this lead
3581 				 * alias symbol, and set the lead symbol index
3582 				 * to the lead symbol.
3583 				 */
3584 				ocapinfo[asdp->sd_symndx] =
3585 				    ELF_C_INFO(lsdp->sd_symndx,
3586 				    CAPINFO_SUNW_GLOB);
3587 			}
3588 		}
3589 
3590 		chainndx++;
3591 
3592 		/*
3593 		 * Gather the family members.
3594 		 */
3595 		for (APLIST_TRAVERSE(cav->cn_members, idx, csp)) {
3596 			Sym_desc	*msdp = csp->cs_sdp;
3597 
3598 			/*
3599 			 * Identify the members capability group, and the lead
3600 			 * symbol of the family this symbol is a member of.
3601 			 */
3602 			ocapinfo[msdp->sd_symndx] =
3603 			    ELF_C_INFO(lsdp->sd_symndx, csp->cs_group->cg_ndx);
3604 			if (ccosp) {
3605 				/*
3606 				 * For a dynamic object, set the next capability
3607 				 * chain to point to this family member.
3608 				 */
3609 				ocapchain[chainndx++] = msdp->sd_symndx;
3610 			}
3611 		}
3612 
3613 		/*
3614 		 * Any chain of family members is terminated with a 0 element.
3615 		 */
3616 		if (ccosp)
3617 			ocapchain[chainndx++] = 0;
3618 	}
3619 }
3620 
3621 /*
3622  * Translate the shdr->sh_{link, info} from its input section value to that
3623  * of the corresponding shdr->sh_{link, info} output section value.
3624  */
3625 static Word
translate_link(Ofl_desc * ofl,Os_desc * osp,Word link,const char * msg)3626 translate_link(Ofl_desc *ofl, Os_desc *osp, Word link, const char *msg)
3627 {
3628 	Is_desc		*isp;
3629 	Ifl_desc	*ifl;
3630 
3631 	/*
3632 	 * Don't translate the special section numbers.
3633 	 */
3634 	if (link >= SHN_LORESERVE)
3635 		return (link);
3636 
3637 	/*
3638 	 * Does this output section translate back to an input file.  If not
3639 	 * then there is no translation to do.  In this case we will assume that
3640 	 * if sh_link has a value, it's the right value.
3641 	 */
3642 	isp = ld_os_first_isdesc(osp);
3643 	if ((ifl = isp->is_file) == NULL)
3644 		return (link);
3645 
3646 	/*
3647 	 * Sanity check to make sure that the sh_{link, info} value
3648 	 * is within range for the input file.
3649 	 */
3650 	if (link >= ifl->ifl_shnum) {
3651 		ld_eprintf(ofl, ERR_WARNING, msg, ifl->ifl_name,
3652 		    EC_WORD(isp->is_scnndx), isp->is_name, EC_XWORD(link));
3653 		return (link);
3654 	}
3655 
3656 	/*
3657 	 * Follow the link to the input section.
3658 	 */
3659 	if ((isp = ifl->ifl_isdesc[link]) == NULL)
3660 		return (0);
3661 	if ((osp = isp->is_osdesc) == NULL)
3662 		return (0);
3663 
3664 	/* LINTED */
3665 	return ((Word)elf_ndxscn(osp->os_scn));
3666 }
3667 
3668 typedef struct {
3669 	avl_node_t	aav_avl;
3670 	Ass_desc	*aav_ass;
3671 } Aav_node;
3672 
3673 static int
aav_compare(const void * first,const void * second)3674 aav_compare(const void *first, const void *second)
3675 {
3676 	Sym *fs = ((Aav_node *)first)->aav_ass->ass_sdp->sd_sym;
3677 	Sym *ss = ((Aav_node *)second)->aav_ass->ass_sdp->sd_sym;
3678 
3679 	if (fs->st_value < ss->st_value)
3680 		return (-1);
3681 	if (fs->st_value > ss->st_value)
3682 		return (1);
3683 
3684 	if (fs->st_size < ss->st_size)
3685 		return (-1);
3686 	if (fs->st_size > ss->st_size)
3687 		return (1);
3688 
3689 	return (0);
3690 }
3691 
3692 
3693 /*
3694  * Check any assertions from mapfiles, provide guidance if there is global
3695  * data in a shared object that does not assert its size.
3696  */
3697 static uintptr_t
check_mapfile_assertions(Ofl_desc * ofl)3698 check_mapfile_assertions(Ofl_desc *ofl)
3699 {
3700 	Ass_desc	*ma;
3701 	uintptr_t	ret = 0;
3702 	Aliste		idx;
3703 	avl_tree_t	ass_avl;
3704 
3705 	avl_create(&ass_avl, &aav_compare, sizeof (Aav_node),
3706 	    SGSOFFSETOF(Aav_node, aav_avl));
3707 
3708 	for (APLIST_TRAVERSE(ofl->ofl_symasserts, idx, ma)) {
3709 		Sym_desc	*sdp = ma->ass_sdp;
3710 		Conv_inv_buf_t	inv_buf, inv_buf2;
3711 
3712 		/*
3713 		 * Try to insert the assertion into the tree if it's not an
3714 		 * alias assertion.  If it's already present, it means we have
3715 		 * two non-alias assertions and should complain
3716 		 */
3717 		if (!ass_enabled(ma, SYM_ASSERT_ALIAS)) {
3718 			Aav_node *av = libld_calloc(1, sizeof (Aav_node));
3719 			Aav_node *dup;
3720 			avl_index_t where;
3721 
3722 			if (av == NULL)
3723 				return (S_ERROR);
3724 
3725 			av->aav_ass = ma;
3726 
3727 			if ((dup = avl_find(&ass_avl, av, &where)) != NULL) {
3728 				ld_eprintf(ofl, ERR_FATAL,
3729 				    MSG_INTL(MSG_ALIAS_NOTALIAS),
3730 				    ma->ass_file,
3731 				    EC_LINENO(ma->ass_lineno),
3732 				    ma->ass_sdp->sd_name,
3733 				    dup->aav_ass->ass_sdp->sd_name,
3734 				    dup->aav_ass->ass_file,
3735 				    EC_LINENO(dup->aav_ass->ass_lineno));
3736 				ret = S_ERROR;
3737 			} else {
3738 				avl_insert(&ass_avl, av, where);
3739 			}
3740 		}
3741 
3742 		/*
3743 		 * All assertions can assert on binding.  Other
3744 		 * assertion behaviour differs based on whether we're
3745 		 * asserting specifics, or asserting an ALIAS.  In the
3746 		 * latter case, we just compare all the attributes
3747 		 * against another symbol.
3748 		 */
3749 		if (ass_enabled(ma, SYM_ASSERT_BIND) &&
3750 		    (ma->ass_bind != ELF_ST_BIND(sdp->sd_sym->st_info))) {
3751 			Sym *sym = sdp->sd_sym;
3752 
3753 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ASSFAIL_SCOPE),
3754 			    ma->ass_file, EC_LINENO(ma->ass_lineno),
3755 			    demangle(sdp->sd_name),
3756 			    conv_sym_info_bind(ma->ass_bind, CONV_FMT_ALT_CFNP,
3757 			    &inv_buf),
3758 			    conv_sym_info_bind(ELF_ST_BIND(sym->st_info),
3759 			    CONV_FMT_ALT_CFNP, &inv_buf2));
3760 			ret = S_ERROR;
3761 		}
3762 
3763 		if (ass_enabled(ma, SYM_ASSERT_ALIAS)) {
3764 			Sym *rsym, *sym;
3765 			Sym_desc *asdp;
3766 
3767 			if ((asdp = ld_sym_find(ma->ass_alias, SYM_NOHASH,
3768 			    NULL, ofl)) == NULL) {
3769 				ld_eprintf(ofl, ERR_FATAL,
3770 				    MSG_INTL(MSG_ALIAS_BADSYM),
3771 				    ma->ass_file, EC_LINENO(ma->ass_lineno),
3772 				    ma->ass_alias);
3773 				ret = S_ERROR;
3774 			} else {
3775 				/*
3776 				 * We forbid aliases of aliases, preferring
3777 				 * they all point to the concrete symbol.
3778 				 * This is unnecessary, but a strong
3779 				 * preference for maintainability.
3780 				 *
3781 				 * This does prevent circular references
3782 				 * however, and if this check is removed a
3783 				 * real check for such would need to be added.
3784 				 */
3785 				if (((asdp->sd_flags & FLG_SY_MAPASSRT) != 0) &&
3786 				    ass_enabled(asdp->sd_ass,
3787 				    SYM_ASSERT_ALIAS)) {
3788 					ld_eprintf(ofl, ERR_FATAL,
3789 					    MSG_INTL(MSG_ALIAS_TOALIAS),
3790 					    ma->ass_file,
3791 					    EC_LINENO(ma->ass_lineno),
3792 					    ma->ass_alias);
3793 					ret = S_ERROR;
3794 				}
3795 
3796 				rsym = asdp->sd_sym;
3797 				sym = sdp->sd_sym;
3798 
3799 				if ((rsym->st_value != sym->st_value) ||
3800 				    (rsym->st_size != sym->st_size) ||
3801 				    (ELF_ST_TYPE(rsym->st_info) !=
3802 				    ELF_ST_TYPE(sym->st_info))) {
3803 					ld_eprintf(ofl, ERR_FATAL,
3804 					    MSG_INTL(MSG_ASSFAIL_ALIAS),
3805 					    ma->ass_file,
3806 					    EC_LINENO(ma->ass_lineno),
3807 					    demangle(sdp->sd_name),
3808 					    asdp->sd_name);
3809 					ret = S_ERROR;
3810 				}
3811 			}
3812 
3813 			/*
3814 			 * If this is an alias, none of our other checks
3815 			 * matter.
3816 			 */
3817 			continue;
3818 		}
3819 
3820 		/*
3821 		 * We only want to assert on the size of a _function_ if it's
3822 		 * explicitly sized, otherwise skip
3823 		 */
3824 		if (ass_enabled(ma, SYM_ASSERT_SIZE) &&
3825 		    (ma->ass_size != sdp->sd_sym->st_size)) {
3826 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ASSFAIL_SIZE),
3827 			    ma->ass_file, EC_LINENO(ma->ass_lineno),
3828 			    demangle(sdp->sd_name),
3829 			    ma->ass_size, (Lword)sdp->sd_sym->st_size);
3830 			ret = S_ERROR;
3831 		}
3832 
3833 		if (ass_enabled(ma, SYM_ASSERT_BITS) && (sdp->sd_isc != NULL)) {
3834 			if ((ma->ass_bits == TRUE) &&
3835 			    (sdp->sd_isc->is_shdr->sh_type == SHT_NOBITS)) {
3836 				ld_eprintf(ofl, ERR_FATAL,
3837 				    MSG_INTL(MSG_ASSFAIL_BITS),
3838 				    ma->ass_file, EC_LINENO(ma->ass_lineno),
3839 				    demangle(sdp->sd_name));
3840 				ret = S_ERROR;
3841 			}
3842 			if ((ma->ass_bits == FALSE) &&
3843 			    (sdp->sd_isc->is_shdr->sh_type != SHT_NOBITS)) {
3844 				ld_eprintf(ofl, ERR_FATAL,
3845 				    MSG_INTL(MSG_ASSFAIL_NOBITS),
3846 				    ma->ass_file, EC_LINENO(ma->ass_lineno),
3847 				    demangle(sdp->sd_name));
3848 				ret = S_ERROR;
3849 			}
3850 		}
3851 
3852 		if (ass_enabled(ma, SYM_ASSERT_TYPE) &&
3853 		    (ma->ass_type != ELF_ST_TYPE(sdp->sd_sym->st_info))) {
3854 			Ehdr *ehdr = sdp->sd_file->ifl_ehdr;
3855 
3856 			ld_eprintf(ofl, ERR_FATAL,
3857 			    MSG_INTL(MSG_ASSFAIL_TYPE),
3858 			    ma->ass_file, EC_LINENO(ma->ass_lineno),
3859 			    demangle(sdp->sd_name),
3860 			    conv_sym_info_type(ehdr->e_machine,
3861 			    ma->ass_type, CONV_FMT_ALT_CFNP, &inv_buf),
3862 			    conv_sym_info_type(ehdr->e_machine,
3863 			    ELF_ST_TYPE(sdp->sd_sym->st_info),
3864 			    CONV_FMT_ALT_CFNP, &inv_buf2));
3865 			ret = S_ERROR;
3866 		}
3867 	}
3868 
3869 	return (ret);
3870 }
3871 
3872 /*
3873  * Use the values from shdr to initialize phdr.
3874  * Used for single-section segments to initialize their phdr.
3875  */
3876 static void
build_phdr_from_shdr(Phdr * phdr,Shdr * shdr,Word flags)3877 build_phdr_from_shdr(Phdr *phdr, Shdr *shdr, Word flags)
3878 {
3879 	phdr->p_vaddr = shdr->sh_addr;
3880 	phdr->p_offset = shdr->sh_offset;
3881 	phdr->p_filesz = shdr->sh_size;
3882 	phdr->p_memsz = shdr->sh_size;
3883 	phdr->p_align = shdr->sh_addralign;
3884 	phdr->p_flags = flags;
3885 }
3886 
3887 
3888 /*
3889  * Having created all of the necessary sections, segments, and associated
3890  * headers, fill in the program headers and update any other data in the
3891  * output image.  Some general rules:
3892  *
3893  *  -	If an interpreter is required always generate a PT_PHDR entry as
3894  *	well.  It is this entry that triggers the kernel into passing the
3895  *	interpreter an aux vector instead of just a file descriptor.
3896  *
3897  *  -	When generating an image that will be interpreted (ie. a dynamic
3898  *	executable, a shared object, or a static executable that has been
3899  *	provided with an interpreter - weird, but possible), make the initial
3900  *	loadable segment include both the ehdr and phdr[].  Both of these
3901  *	tables are used by the interpreter therefore it seems more intuitive
3902  *	to explicitly defined them as part of the mapped image rather than
3903  *	relying on page rounding by the interpreter to allow their access.
3904  *
3905  *  -	When generating a static image that does not require an interpreter
3906  *	have the first loadable segment indicate the address of the first
3907  *	.section as the start address (things like /kernel/unix and ufsboot
3908  *	expect this behavior).
3909  */
3910 uintptr_t
ld_update_outfile(Ofl_desc * ofl)3911 ld_update_outfile(Ofl_desc *ofl)
3912 {
3913 	Addr		size, etext, vaddr;
3914 	Sg_desc		*sgp;
3915 	Sg_desc		*dtracesgp = NULL, *capsgp = NULL, *intpsgp = NULL;
3916 	Os_desc		*osp;
3917 	int		phdrndx = 0, segndx = -1, secndx, intppndx, intpsndx;
3918 	int		dtracepndx, dtracesndx, cappndx, capsndx;
3919 	Ehdr		*ehdr = ofl->ofl_nehdr;
3920 	Shdr		*hshdr;
3921 	Phdr		*_phdr = NULL;
3922 	Word		phdrsz = (ehdr->e_phnum * ehdr->e_phentsize), shscnndx;
3923 	ofl_flag_t	flags = ofl->ofl_flags;
3924 	Word		ehdrsz = ehdr->e_ehsize;
3925 	Boolean		nobits;
3926 	Off		offset;
3927 	Aliste		idx1;
3928 
3929 	/*
3930 	 * Initialize the starting address for the first segment.  Executables
3931 	 * have different starting addresses depending upon the target ABI,
3932 	 * where as shared objects have a starting address of 0.  If this is
3933 	 * a 64-bit executable that is being constructed to run in a restricted
3934 	 * address space, use an alternative origin that will provide more free
3935 	 * address space for the the eventual process.
3936 	 */
3937 	if (ofl->ofl_flags & FLG_OF_EXEC) {
3938 #if	defined(_ELF64)
3939 		if (ofl->ofl_ocapset.oc_sf_1.cm_val & SF1_SUNW_ADDR32)
3940 			vaddr = ld_targ.t_m.m_segm_aorigin;
3941 		else
3942 #endif
3943 			vaddr = ld_targ.t_m.m_segm_origin;
3944 	} else
3945 		vaddr = 0;
3946 
3947 	/*
3948 	 * Loop through the segment descriptors and pick out what we need.
3949 	 */
3950 	DBG_CALL(Dbg_seg_title(ofl->ofl_lml));
3951 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3952 		Phdr		*phdr = &(sgp->sg_phdr);
3953 		Xword		p_align;
3954 		Aliste		idx2;
3955 		Sym_desc	*sdp;
3956 
3957 		segndx++;
3958 
3959 		/*
3960 		 * If an interpreter is required generate a PT_INTERP and
3961 		 * PT_PHDR program header entry.  The PT_PHDR entry describes
3962 		 * the program header table itself.  This information will be
3963 		 * passed via the aux vector to the interpreter (ld.so.1).
3964 		 * The program header array is actually part of the first
3965 		 * loadable segment (and the PT_PHDR entry is the first entry),
3966 		 * therefore its virtual address isn't known until the first
3967 		 * loadable segment is processed.
3968 		 */
3969 		if (phdr->p_type == PT_PHDR) {
3970 			if (ofl->ofl_osinterp) {
3971 				phdr->p_offset = ehdr->e_phoff;
3972 				phdr->p_filesz = phdr->p_memsz = phdrsz;
3973 
3974 				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3975 				ofl->ofl_phdr[phdrndx++] = *phdr;
3976 			}
3977 			continue;
3978 		}
3979 		if (phdr->p_type == PT_INTERP) {
3980 			if (ofl->ofl_osinterp) {
3981 				intpsgp = sgp;
3982 				intpsndx = segndx;
3983 				intppndx = phdrndx++;
3984 			}
3985 			continue;
3986 		}
3987 
3988 		/*
3989 		 * If we are creating a PT_SUNWDTRACE segment, remember where
3990 		 * the program header is.  The header values are assigned after
3991 		 * update_osym() has completed and the symbol table addresses
3992 		 * have been updated.
3993 		 */
3994 		if (phdr->p_type == PT_SUNWDTRACE) {
3995 			if (ofl->ofl_dtracesym &&
3996 			    ((flags & FLG_OF_RELOBJ) == 0)) {
3997 				dtracesgp = sgp;
3998 				dtracesndx = segndx;
3999 				dtracepndx = phdrndx++;
4000 			}
4001 			continue;
4002 		}
4003 
4004 		/*
4005 		 * If a hardware/software capabilities section is required,
4006 		 * generate the PT_SUNWCAP header.  Note, as this comes before
4007 		 * the first loadable segment, we don't yet know its real
4008 		 * virtual address.  This is updated later.
4009 		 */
4010 		if (phdr->p_type == PT_SUNWCAP) {
4011 			if (ofl->ofl_oscap && (ofl->ofl_flags & FLG_OF_PTCAP) &&
4012 			    ((flags & FLG_OF_RELOBJ) == 0)) {
4013 				capsgp = sgp;
4014 				capsndx = segndx;
4015 				cappndx = phdrndx++;
4016 			}
4017 			continue;
4018 		}
4019 
4020 		/*
4021 		 * As the dynamic program header occurs after the loadable
4022 		 * headers in the segment descriptor table, all the address
4023 		 * information for the .dynamic output section will have been
4024 		 * figured out by now.
4025 		 */
4026 		if (phdr->p_type == PT_DYNAMIC) {
4027 			if (OFL_ALLOW_DYNSYM(ofl)) {
4028 				Shdr	*shdr = ofl->ofl_osdynamic->os_shdr;
4029 
4030 				build_phdr_from_shdr(phdr, shdr,
4031 				    ld_targ.t_m.m_dataseg_perm);
4032 
4033 				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
4034 				ofl->ofl_phdr[phdrndx++] = *phdr;
4035 			}
4036 			continue;
4037 		}
4038 
4039 		/*
4040 		 * As the unwind (.eh_frame_hdr) program header occurs after
4041 		 * the loadable headers in the segment descriptor table, all
4042 		 * the address information for the .eh_frame output section
4043 		 * will have been figured out by now.
4044 		 */
4045 		if (phdr->p_type == PT_SUNW_UNWIND) {
4046 			Shdr	    *shdr;
4047 
4048 			if (ofl->ofl_unwindhdr == NULL)
4049 				continue;
4050 
4051 			shdr = ofl->ofl_unwindhdr->os_shdr;
4052 
4053 			build_phdr_from_shdr(phdr, shdr, PF_R);
4054 			phdr->p_paddr = 0;
4055 			ofl->ofl_phdr[phdrndx++] = *phdr;
4056 			continue;
4057 		}
4058 
4059 		/*
4060 		 * The sunwstack program is used to convey non-default
4061 		 * flags for the process stack. Only emit it if it would
4062 		 * change the default.
4063 		 */
4064 		if (phdr->p_type == PT_SUNWSTACK) {
4065 			if (((flags & FLG_OF_RELOBJ) == 0) &&
4066 			    ((sgp->sg_flags & FLG_SG_DISABLED) == 0))
4067 				ofl->ofl_phdr[phdrndx++] = *phdr;
4068 			continue;
4069 		}
4070 
4071 		/*
4072 		 * As the TLS program header occurs after the loadable
4073 		 * headers in the segment descriptor table, all the address
4074 		 * information for the .tls output section will have been
4075 		 * figured out by now.
4076 		 */
4077 		if (phdr->p_type == PT_TLS) {
4078 			Os_desc		*tlsosp;
4079 			Shdr		*lastfileshdr = NULL;
4080 			Shdr		*firstshdr = NULL, *lastshdr;
4081 			Aliste		idx;
4082 
4083 			if (ofl->ofl_ostlsseg == NULL)
4084 				continue;
4085 
4086 			/*
4087 			 * Scan the output sections that have contributed TLS.
4088 			 * Remember the first and last so as to determine the
4089 			 * TLS memory size requirement.  Remember the last
4090 			 * progbits section to determine the TLS data
4091 			 * contribution, which determines the TLS program
4092 			 * header filesz.
4093 			 */
4094 			for (APLIST_TRAVERSE(ofl->ofl_ostlsseg, idx, tlsosp)) {
4095 				Shdr	*tlsshdr = tlsosp->os_shdr;
4096 
4097 				if (firstshdr == NULL)
4098 					firstshdr = tlsshdr;
4099 				if (tlsshdr->sh_type != SHT_NOBITS)
4100 					lastfileshdr = tlsshdr;
4101 				lastshdr = tlsshdr;
4102 			}
4103 
4104 			build_phdr_from_shdr(phdr, firstshdr, PF_R | PF_W);
4105 
4106 			/*
4107 			 * Determine the initialized TLS data size.  This
4108 			 * address range is from the start of the TLS segment
4109 			 * to the end of the last piece of initialized data.
4110 			 */
4111 			if (lastfileshdr)
4112 				phdr->p_filesz = lastfileshdr->sh_offset +
4113 				    lastfileshdr->sh_size - phdr->p_offset;
4114 			else
4115 				phdr->p_filesz = 0;
4116 
4117 			/*
4118 			 * Determine the total TLS memory size.  This includes
4119 			 * all TLS data and TLS uninitialized data.  This
4120 			 * address range is from the start of the TLS segment
4121 			 * to the memory address of the last piece of
4122 			 * uninitialized data.
4123 			 */
4124 			phdr->p_memsz = lastshdr->sh_addr +
4125 			    lastshdr->sh_size - phdr->p_vaddr;
4126 
4127 			DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
4128 			ofl->ofl_phdr[phdrndx] = *phdr;
4129 			ofl->ofl_tlsphdr = &ofl->ofl_phdr[phdrndx++];
4130 			continue;
4131 		}
4132 
4133 		/*
4134 		 * If this is an empty segment declaration, it will occur after
4135 		 * all other loadable segments.  As empty segments can be
4136 		 * defined with fixed addresses, make sure that no loadable
4137 		 * segments overlap.  This might occur as the object evolves
4138 		 * and the loadable segments grow, thus encroaching upon an
4139 		 * existing segment reservation.
4140 		 *
4141 		 * Segments are only created for dynamic objects, thus this
4142 		 * checking can be skipped when building a relocatable object.
4143 		 */
4144 		if (!(flags & FLG_OF_RELOBJ) &&
4145 		    (sgp->sg_flags & FLG_SG_EMPTY)) {
4146 			int	i;
4147 			Addr	v_e;
4148 
4149 			vaddr = phdr->p_vaddr;
4150 			phdr->p_memsz = sgp->sg_length;
4151 			DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
4152 			ofl->ofl_phdr[phdrndx++] = *phdr;
4153 
4154 			if (phdr->p_type != PT_LOAD)
4155 				continue;
4156 
4157 			v_e = vaddr + phdr->p_memsz;
4158 
4159 			/*
4160 			 * Check overlaps
4161 			 */
4162 			for (i = 0; i < phdrndx - 1; i++) {
4163 				Addr	p_s = (ofl->ofl_phdr[i]).p_vaddr;
4164 				Addr	p_e;
4165 
4166 				if ((ofl->ofl_phdr[i]).p_type != PT_LOAD)
4167 					continue;
4168 
4169 				p_e = p_s + (ofl->ofl_phdr[i]).p_memsz;
4170 				if (((p_s <= vaddr) && (p_e > vaddr)) ||
4171 				    ((vaddr <= p_s) && (v_e > p_s)))
4172 					ld_eprintf(ofl, ERR_WARNING,
4173 					    MSG_INTL(MSG_UPD_SEGOVERLAP),
4174 					    ofl->ofl_name, EC_ADDR(p_e),
4175 					    sgp->sg_name, EC_ADDR(vaddr));
4176 			}
4177 			continue;
4178 		}
4179 
4180 		/*
4181 		 * Having processed any of the special program headers any
4182 		 * remaining headers will be built to express individual
4183 		 * segments.  Segments are only built if they have output
4184 		 * section descriptors associated with them (ie. some form of
4185 		 * input section has been matched to this segment).
4186 		 */
4187 		if (aplist_nitems(sgp->sg_osdescs) == 0)
4188 			continue;
4189 
4190 		/*
4191 		 * Determine the segments offset and size from the section
4192 		 * information provided from elf_update().
4193 		 * Allow for multiple NOBITS sections.
4194 		 */
4195 		osp = sgp->sg_osdescs->apl_data[0];
4196 		hshdr = osp->os_shdr;
4197 
4198 		phdr->p_filesz = 0;
4199 		phdr->p_memsz = 0;
4200 		phdr->p_offset = offset = hshdr->sh_offset;
4201 
4202 		nobits = ((hshdr->sh_type == SHT_NOBITS) &&
4203 		    ((sgp->sg_flags & FLG_SG_PHREQ) == 0));
4204 
4205 		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
4206 			Shdr	*shdr = osp->os_shdr;
4207 
4208 			p_align = 0;
4209 			if (shdr->sh_addralign > p_align)
4210 				p_align = shdr->sh_addralign;
4211 
4212 			offset = (Off)S_ROUND(offset, shdr->sh_addralign);
4213 			offset += shdr->sh_size;
4214 
4215 			if (shdr->sh_type != SHT_NOBITS) {
4216 				if (nobits) {
4217 					ld_eprintf(ofl, ERR_FATAL,
4218 					    MSG_INTL(MSG_UPD_NOBITS));
4219 					return (S_ERROR);
4220 				}
4221 				phdr->p_filesz = offset - phdr->p_offset;
4222 			} else if ((sgp->sg_flags & FLG_SG_PHREQ) == 0)
4223 				nobits = TRUE;
4224 		}
4225 		phdr->p_memsz = offset - hshdr->sh_offset;
4226 
4227 		/*
4228 		 * If this is the first loadable segment of a dynamic object,
4229 		 * or an interpreter has been specified (a static object built
4230 		 * with an interpreter will still be given a PT_HDR entry), then
4231 		 * compensate for the elf header and program header array.  Both
4232 		 * of these are actually part of the loadable segment as they
4233 		 * may be inspected by the interpreter.  Adjust the segments
4234 		 * size and offset accordingly.
4235 		 */
4236 		if ((_phdr == NULL) && (phdr->p_type == PT_LOAD) &&
4237 		    ((ofl->ofl_osinterp) || (flags & FLG_OF_DYNAMIC)) &&
4238 		    (!(ofl->ofl_dtflags_1 & DF_1_NOHDR))) {
4239 			size = (Addr)S_ROUND((phdrsz + ehdrsz),
4240 			    hshdr->sh_addralign);
4241 			phdr->p_offset -= size;
4242 			phdr->p_filesz += size;
4243 			phdr->p_memsz += size;
4244 		}
4245 
4246 		/*
4247 		 * If segment size symbols are required (specified via a
4248 		 * mapfile) update their value.
4249 		 */
4250 		for (APLIST_TRAVERSE(sgp->sg_sizesym, idx2, sdp))
4251 			sdp->sd_sym->st_value = phdr->p_memsz;
4252 
4253 		/*
4254 		 * If no file content has been assigned to this segment (it
4255 		 * only contains no-bits sections), then reset the offset for
4256 		 * consistency.
4257 		 */
4258 		if (phdr->p_filesz == 0)
4259 			phdr->p_offset = 0;
4260 
4261 		/*
4262 		 * If a virtual address has been specified for this segment
4263 		 * from a mapfile use it and make sure the previous segment
4264 		 * does not run into this segment.
4265 		 */
4266 		if (phdr->p_type == PT_LOAD) {
4267 			if ((sgp->sg_flags & FLG_SG_P_VADDR)) {
4268 				if (_phdr && (vaddr > phdr->p_vaddr) &&
4269 				    (phdr->p_type == PT_LOAD))
4270 					ld_eprintf(ofl, ERR_WARNING,
4271 					    MSG_INTL(MSG_UPD_SEGOVERLAP),
4272 					    ofl->ofl_name, EC_ADDR(vaddr),
4273 					    sgp->sg_name,
4274 					    EC_ADDR(phdr->p_vaddr));
4275 				vaddr = phdr->p_vaddr;
4276 				phdr->p_align = 0;
4277 			} else {
4278 				vaddr = phdr->p_vaddr =
4279 				    (Addr)S_ROUND(vaddr, phdr->p_align);
4280 			}
4281 		}
4282 
4283 		/*
4284 		 * Adjust the address offset and p_align if needed.
4285 		 */
4286 		if (((sgp->sg_flags & FLG_SG_P_VADDR) == 0) &&
4287 		    ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0)) {
4288 			if (phdr->p_align != 0)
4289 				vaddr += phdr->p_offset % phdr->p_align;
4290 			else
4291 				vaddr += phdr->p_offset;
4292 			phdr->p_vaddr = vaddr;
4293 		}
4294 
4295 		/*
4296 		 * If an interpreter is required set the virtual address of the
4297 		 * PT_PHDR program header now that we know the virtual address
4298 		 * of the loadable segment that contains it.  Update the
4299 		 * PT_SUNWCAP header similarly.
4300 		 */
4301 		if ((_phdr == NULL) && (phdr->p_type == PT_LOAD)) {
4302 			_phdr = phdr;
4303 
4304 			if ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0) {
4305 				if (ofl->ofl_osinterp)
4306 					ofl->ofl_phdr[0].p_vaddr =
4307 					    vaddr + ehdrsz;
4308 
4309 				/*
4310 				 * Finally, if we're creating a dynamic object
4311 				 * (or a static object in which an interpreter
4312 				 * is specified) update the vaddr to reflect
4313 				 * the address of the first section within this
4314 				 * segment.
4315 				 */
4316 				if ((ofl->ofl_osinterp) ||
4317 				    (flags & FLG_OF_DYNAMIC))
4318 					vaddr += size;
4319 			} else {
4320 				/*
4321 				 * If the DF_1_NOHDR flag was set, and an
4322 				 * interpreter is being generated, the PT_PHDR
4323 				 * will not be part of any loadable segment.
4324 				 */
4325 				if (ofl->ofl_osinterp) {
4326 					ofl->ofl_phdr[0].p_vaddr = 0;
4327 					ofl->ofl_phdr[0].p_memsz = 0;
4328 					ofl->ofl_phdr[0].p_flags = 0;
4329 				}
4330 			}
4331 		}
4332 
4333 		/*
4334 		 * Ensure the ELF entry point defaults to zero.  Typically, this
4335 		 * value is overridden in update_oehdr() to one of the standard
4336 		 * entry points.  Historically, this default was set to the
4337 		 * address of first executable section, but this has since been
4338 		 * found to be more confusing than it is helpful.
4339 		 */
4340 		ehdr->e_entry = 0;
4341 
4342 		DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
4343 
4344 		/*
4345 		 * Traverse the output section descriptors for this segment so
4346 		 * that we can update the section headers addresses.  We've
4347 		 * calculated the virtual address of the initial section within
4348 		 * this segment, so each successive section can be calculated
4349 		 * based on their offsets from each other.
4350 		 */
4351 		secndx = 0;
4352 		hshdr = 0;
4353 		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
4354 			Shdr	*shdr = osp->os_shdr;
4355 
4356 			if (shdr->sh_link)
4357 				shdr->sh_link = translate_link(ofl, osp,
4358 				    shdr->sh_link, MSG_INTL(MSG_FIL_INVSHLINK));
4359 
4360 			if (shdr->sh_info && (shdr->sh_flags & SHF_INFO_LINK))
4361 				shdr->sh_info = translate_link(ofl, osp,
4362 				    shdr->sh_info, MSG_INTL(MSG_FIL_INVSHINFO));
4363 
4364 			if (!(flags & FLG_OF_RELOBJ) &&
4365 			    (phdr->p_type == PT_LOAD)) {
4366 				if (hshdr)
4367 					vaddr += (shdr->sh_offset -
4368 					    hshdr->sh_offset);
4369 
4370 				shdr->sh_addr = vaddr;
4371 				hshdr = shdr;
4372 			}
4373 
4374 			DBG_CALL(Dbg_seg_os(ofl, osp, secndx));
4375 			secndx++;
4376 		}
4377 
4378 		/*
4379 		 * Establish the virtual address of the end of the last section
4380 		 * in this segment so that the next segments offset can be
4381 		 * calculated from this.
4382 		 */
4383 		if (hshdr)
4384 			vaddr += hshdr->sh_size;
4385 
4386 		/*
4387 		 * Output sections for this segment complete.  Adjust the
4388 		 * virtual offset for the last sections size, and make sure we
4389 		 * haven't exceeded any maximum segment length specification.
4390 		 */
4391 		if ((sgp->sg_length != 0) && (sgp->sg_length < phdr->p_memsz)) {
4392 			ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_UPD_LARGSIZE),
4393 			    ofl->ofl_name, sgp->sg_name,
4394 			    EC_XWORD(phdr->p_memsz), EC_XWORD(sgp->sg_length));
4395 			return (S_ERROR);
4396 		}
4397 
4398 		if (phdr->p_type == PT_NOTE) {
4399 			phdr->p_vaddr = 0;
4400 			phdr->p_paddr = 0;
4401 			phdr->p_align = 0;
4402 			phdr->p_memsz = 0;
4403 		}
4404 
4405 		if ((phdr->p_type != PT_NULL) && !(flags & FLG_OF_RELOBJ))
4406 			ofl->ofl_phdr[phdrndx++] = *phdr;
4407 	}
4408 
4409 	/*
4410 	 * Update any new output sections.  When building the initial output
4411 	 * image, a number of sections were created but left uninitialized (eg.
4412 	 * .dynsym, .dynstr, .symtab, .symtab, etc.).  Here we update these
4413 	 * sections with the appropriate data.  Other sections may still be
4414 	 * modified via reloc_process().
4415 	 *
4416 	 * Copy the interpreter name into the .interp section.
4417 	 */
4418 	if (ofl->ofl_interp)
4419 		(void) strcpy((char *)ofl->ofl_osinterp->os_outdata->d_buf,
4420 		    ofl->ofl_interp);
4421 
4422 	/*
4423 	 * Update the .shstrtab, .strtab and .dynstr sections.
4424 	 */
4425 	update_ostrtab(ofl->ofl_osshstrtab, ofl->ofl_shdrsttab, 0);
4426 	update_ostrtab(ofl->ofl_osstrtab, ofl->ofl_strtab, 0);
4427 	update_ostrtab(ofl->ofl_osdynstr, ofl->ofl_dynstrtab, DYNSTR_EXTRA_PAD);
4428 
4429 	/*
4430 	 * Build any output symbol tables, the symbols information is copied
4431 	 * and updated into the new output image.
4432 	 */
4433 	if ((etext = update_osym(ofl)) == (Addr)S_ERROR)
4434 		return (S_ERROR);
4435 
4436 
4437 	/*
4438 	 * Now the symbol tables are complete, process any mapfile
4439 	 * assertions
4440 	 */
4441 	if (check_mapfile_assertions(ofl) == S_ERROR)
4442 		return (S_ERROR);
4443 	/*
4444 	 * If we have an PT_INTERP phdr, update it now from the associated
4445 	 * section information.
4446 	 */
4447 	if (intpsgp) {
4448 		Phdr	*phdr = &(intpsgp->sg_phdr);
4449 		Shdr	*shdr = ofl->ofl_osinterp->os_shdr;
4450 
4451 		build_phdr_from_shdr(phdr, shdr, PF_R);
4452 
4453 		DBG_CALL(Dbg_seg_entry(ofl, intpsndx, intpsgp));
4454 		ofl->ofl_phdr[intppndx] = *phdr;
4455 	}
4456 
4457 	/*
4458 	 * If we have a PT_SUNWDTRACE phdr, update it now with the address of
4459 	 * the symbol.  It's only now been updated via update_sym().
4460 	 *
4461 	 * The program header is used to find this symbol for its private use
4462 	 * during process startup (see fasttrap.h), and it is not itself
4463 	 * loadable.  Phdr fields not used by DTrace are left at 0.
4464 	 */
4465 	if (dtracesgp) {
4466 		Phdr		*aphdr, *phdr = &(dtracesgp->sg_phdr);
4467 		Sym_desc	*sdp = ofl->ofl_dtracesym;
4468 
4469 		phdr->p_vaddr = sdp->sd_sym->st_value;
4470 		phdr->p_memsz = sdp->sd_sym->st_size;
4471 
4472 		/*
4473 		 * Take permissions from the segment that the symbol is
4474 		 * associated with.
4475 		 */
4476 		aphdr = &sdp->sd_isc->is_osdesc->os_sgdesc->sg_phdr;
4477 		assert(aphdr);
4478 		phdr->p_flags = aphdr->p_flags;
4479 
4480 		DBG_CALL(Dbg_seg_entry(ofl, dtracesndx, dtracesgp));
4481 		ofl->ofl_phdr[dtracepndx] = *phdr;
4482 	}
4483 
4484 	/*
4485 	 * If we have a PT_SUNWCAP phdr, update it now from the associated
4486 	 * section information.
4487 	 */
4488 	if (capsgp) {
4489 		Phdr	*phdr = &(capsgp->sg_phdr);
4490 		Shdr	*shdr = ofl->ofl_oscap->os_shdr;
4491 
4492 		build_phdr_from_shdr(phdr, shdr, PF_R);
4493 
4494 		DBG_CALL(Dbg_seg_entry(ofl, capsndx, capsgp));
4495 		ofl->ofl_phdr[cappndx] = *phdr;
4496 	}
4497 
4498 	/*
4499 	 * Update the GROUP sections.
4500 	 */
4501 	if (update_ogroup(ofl) == S_ERROR)
4502 		return (S_ERROR);
4503 
4504 	/*
4505 	 * Update Move Table.
4506 	 */
4507 	if (ofl->ofl_osmove || ofl->ofl_isparexpn)
4508 		update_move(ofl);
4509 
4510 	/*
4511 	 * Build any output headers, version information, dynamic structure and
4512 	 * syminfo structure.
4513 	 */
4514 	if (update_oehdr(ofl) == S_ERROR)
4515 		return (S_ERROR);
4516 	if (!(flags & FLG_OF_NOVERSEC)) {
4517 		if ((flags & FLG_OF_VERDEF) &&
4518 		    (update_overdef(ofl) == S_ERROR))
4519 			return (S_ERROR);
4520 		if ((flags & FLG_OF_VERNEED) &&
4521 		    (update_overneed(ofl) == S_ERROR))
4522 			return (S_ERROR);
4523 		if (flags & (FLG_OF_VERNEED | FLG_OF_VERDEF))
4524 			update_oversym(ofl);
4525 	}
4526 	if (flags & FLG_OF_DYNAMIC) {
4527 		if (update_odynamic(ofl) == S_ERROR)
4528 			return (S_ERROR);
4529 	}
4530 	if (ofl->ofl_ossyminfo) {
4531 		if (update_osyminfo(ofl) == S_ERROR)
4532 			return (S_ERROR);
4533 	}
4534 
4535 	/*
4536 	 * Update capabilities information if required.
4537 	 */
4538 	if (ofl->ofl_oscap)
4539 		update_oscap(ofl);
4540 	if (ofl->ofl_oscapinfo)
4541 		update_oscapinfo(ofl);
4542 
4543 	/*
4544 	 * Sanity test: the first and last data byte of a string table
4545 	 * must be NULL.
4546 	 */
4547 	assert((ofl->ofl_osshstrtab == NULL) ||
4548 	    (*((char *)ofl->ofl_osshstrtab->os_outdata->d_buf) == '\0'));
4549 	assert((ofl->ofl_osshstrtab == NULL) ||
4550 	    (*(((char *)ofl->ofl_osshstrtab->os_outdata->d_buf) +
4551 	    ofl->ofl_osshstrtab->os_outdata->d_size - 1) == '\0'));
4552 
4553 	assert((ofl->ofl_osstrtab == NULL) ||
4554 	    (*((char *)ofl->ofl_osstrtab->os_outdata->d_buf) == '\0'));
4555 	assert((ofl->ofl_osstrtab == NULL) ||
4556 	    (*(((char *)ofl->ofl_osstrtab->os_outdata->d_buf) +
4557 	    ofl->ofl_osstrtab->os_outdata->d_size - 1) == '\0'));
4558 
4559 	assert((ofl->ofl_osdynstr == NULL) ||
4560 	    (*((char *)ofl->ofl_osdynstr->os_outdata->d_buf) == '\0'));
4561 	assert((ofl->ofl_osdynstr == NULL) ||
4562 	    (*(((char *)ofl->ofl_osdynstr->os_outdata->d_buf) +
4563 	    ofl->ofl_osdynstr->os_outdata->d_size - DYNSTR_EXTRA_PAD - 1) ==
4564 	    '\0'));
4565 
4566 	/*
4567 	 * Emit Strtab diagnostics.
4568 	 */
4569 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osshstrtab,
4570 	    ofl->ofl_shdrsttab));
4571 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osstrtab,
4572 	    ofl->ofl_strtab));
4573 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osdynstr,
4574 	    ofl->ofl_dynstrtab));
4575 
4576 	/*
4577 	 * Initialize the section headers string table index within the elf
4578 	 * header.
4579 	 */
4580 	/* LINTED */
4581 	if ((shscnndx = elf_ndxscn(ofl->ofl_osshstrtab->os_scn)) <
4582 	    SHN_LORESERVE) {
4583 		ofl->ofl_nehdr->e_shstrndx =
4584 		    /* LINTED */
4585 		    (Half)shscnndx;
4586 	} else {
4587 		/*
4588 		 * If the STRTAB section index doesn't fit into
4589 		 * e_shstrndx, then we store it in 'shdr[0].st_link'.
4590 		 */
4591 		Elf_Scn	*scn;
4592 		Shdr	*shdr0;
4593 
4594 		if ((scn = elf_getscn(ofl->ofl_elf, 0)) == NULL) {
4595 			ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
4596 			    ofl->ofl_name);
4597 			return (S_ERROR);
4598 		}
4599 		if ((shdr0 = elf_getshdr(scn)) == NULL) {
4600 			ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
4601 			    ofl->ofl_name);
4602 			return (S_ERROR);
4603 		}
4604 		ofl->ofl_nehdr->e_shstrndx = SHN_XINDEX;
4605 		shdr0->sh_link = shscnndx;
4606 	}
4607 
4608 	return ((uintptr_t)etext);
4609 }
4610