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