xref: /illumos-gate/usr/src/cmd/sgs/libld/common/update.c (revision 08278a5e)
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 	/*
2948 	 * Note. it may be necessary to update the `e_flags' field in the
2949 	 * machine dependent section.
2950 	 */
2951 	ehdr->e_ident[EI_DATA] = ld_targ.t_m.m_data;
2952 	ehdr->e_machine = ofl->ofl_dehdr->e_machine;
2953 	ehdr->e_flags = ofl->ofl_dehdr->e_flags;
2954 	ehdr->e_version = ofl->ofl_dehdr->e_version;
2955 
2956 	if (ehdr->e_machine != ld_targ.t_m.m_mach) {
2957 		if (ehdr->e_machine != ld_targ.t_m.m_machplus)
2958 			return (S_ERROR);
2959 		if ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0)
2960 			return (S_ERROR);
2961 	}
2962 
2963 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
2964 		ehdr->e_type = ET_DYN;
2965 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
2966 		ehdr->e_type = ET_REL;
2967 	else
2968 		ehdr->e_type = ET_EXEC;
2969 
2970 	return (1);
2971 }
2972 
2973 /*
2974  * Perform move table expansion.
2975  */
2976 static void
2977 expand_move(Ofl_desc *ofl, Sym_desc *sdp, Move *mvp)
2978 {
2979 	Os_desc		*osp;
2980 	uchar_t		*taddr, *taddr0;
2981 	Sxword		offset;
2982 	Half		cnt;
2983 	uint_t		stride;
2984 
2985 	osp = ofl->ofl_isparexpn->is_osdesc;
2986 	offset = sdp->sd_sym->st_value - osp->os_shdr->sh_addr;
2987 
2988 	taddr0 = taddr = osp->os_outdata->d_buf;
2989 	taddr += offset;
2990 	taddr = taddr + mvp->m_poffset;
2991 
2992 	for (cnt = 0; cnt < mvp->m_repeat; cnt++) {
2993 		/* LINTED */
2994 		DBG_CALL(Dbg_move_expand(ofl->ofl_lml, mvp,
2995 		    (Addr)(taddr - taddr0)));
2996 		stride = (uint_t)mvp->m_stride + 1;
2997 
2998 		/*
2999 		 * Update the target address based upon the move entry size.
3000 		 * This size was validated in ld_process_move().
3001 		 */
3002 		/* LINTED */
3003 		switch (ELF_M_SIZE(mvp->m_info)) {
3004 		case 1:
3005 			/* LINTED */
3006 			*taddr = (uchar_t)mvp->m_value;
3007 			taddr += stride;
3008 			break;
3009 		case 2:
3010 			/* LINTED */
3011 			*((Half *)taddr) = (Half)mvp->m_value;
3012 			taddr += 2 * stride;
3013 			break;
3014 		case 4:
3015 			/* LINTED */
3016 			*((Word *)taddr) = (Word)mvp->m_value;
3017 			taddr += 4 * stride;
3018 			break;
3019 		case 8:
3020 			/* LINTED */
3021 			*((u_longlong_t *)taddr) = mvp->m_value;
3022 			taddr += 8 * stride;
3023 			break;
3024 		}
3025 	}
3026 }
3027 
3028 /*
3029  * Update Move sections.
3030  */
3031 static void
3032 update_move(Ofl_desc *ofl)
3033 {
3034 	Word		ndx = 0;
3035 	ofl_flag_t	flags = ofl->ofl_flags;
3036 	Move		*omvp;
3037 	Aliste		idx1;
3038 	Sym_desc	*sdp;
3039 
3040 	/*
3041 	 * Determine the index of the symbol table that will be referenced by
3042 	 * the Move section.
3043 	 */
3044 	if (OFL_ALLOW_DYNSYM(ofl))
3045 		/* LINTED */
3046 		ndx = (Word) elf_ndxscn(ofl->ofl_osdynsym->os_scn);
3047 	else if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ))
3048 		/* LINTED */
3049 		ndx = (Word) elf_ndxscn(ofl->ofl_ossymtab->os_scn);
3050 
3051 	/*
3052 	 * Update sh_link of the Move section, and point to the new Move data.
3053 	 */
3054 	if (ofl->ofl_osmove) {
3055 		ofl->ofl_osmove->os_shdr->sh_link = ndx;
3056 		omvp = (Move *)ofl->ofl_osmove->os_outdata->d_buf;
3057 	}
3058 
3059 	/*
3060 	 * Update symbol entry index
3061 	 */
3062 	for (APLIST_TRAVERSE(ofl->ofl_parsyms, idx1, sdp)) {
3063 		Aliste		idx2;
3064 		Mv_desc		*mdp;
3065 
3066 		/*
3067 		 * Expand move table
3068 		 */
3069 		if (sdp->sd_flags & FLG_SY_PAREXPN) {
3070 			const char	*str;
3071 
3072 			if (flags & FLG_OF_STATIC)
3073 				str = MSG_INTL(MSG_PSYM_EXPREASON1);
3074 			else if (ofl->ofl_flags1 & FLG_OF1_NOPARTI)
3075 				str = MSG_INTL(MSG_PSYM_EXPREASON2);
3076 			else
3077 				str = MSG_INTL(MSG_PSYM_EXPREASON3);
3078 
3079 			DBG_CALL(Dbg_move_parexpn(ofl->ofl_lml,
3080 			    sdp->sd_name, str));
3081 
3082 			for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp)) {
3083 				DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0,
3084 				    mdp->md_move, sdp));
3085 				expand_move(ofl, sdp, mdp->md_move);
3086 			}
3087 			continue;
3088 		}
3089 
3090 		/*
3091 		 * Process move table
3092 		 */
3093 		DBG_CALL(Dbg_move_outmove(ofl->ofl_lml, sdp->sd_name));
3094 
3095 		for (ALIST_TRAVERSE(sdp->sd_move, idx2, mdp)) {
3096 			Move	*imvp;
3097 			int	idx = 1;
3098 			Sym	*sym;
3099 
3100 			imvp = mdp->md_move;
3101 			sym = sdp->sd_sym;
3102 
3103 			DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 1, imvp, sdp));
3104 
3105 			*omvp = *imvp;
3106 			if ((flags & FLG_OF_RELOBJ) == 0) {
3107 				if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
3108 					Os_desc	*osp = sdp->sd_isc->is_osdesc;
3109 					Word	ndx = osp->os_identndx;
3110 
3111 					omvp->m_info =
3112 					    /* LINTED */
3113 					    ELF_M_INFO(ndx, imvp->m_info);
3114 
3115 					if (ELF_ST_TYPE(sym->st_info) !=
3116 					    STT_SECTION) {
3117 						omvp->m_poffset =
3118 						    sym->st_value -
3119 						    osp->os_shdr->sh_addr +
3120 						    imvp->m_poffset;
3121 					}
3122 				} else {
3123 					omvp->m_info =
3124 					    /* LINTED */
3125 					    ELF_M_INFO(sdp->sd_symndx,
3126 					    imvp->m_info);
3127 				}
3128 			} else {
3129 				Boolean 	isredloc = FALSE;
3130 
3131 				if ((ELF_ST_BIND(sym->st_info) == STB_LOCAL) &&
3132 				    (ofl->ofl_flags & FLG_OF_REDLSYM))
3133 					isredloc = TRUE;
3134 
3135 				if (isredloc && !(sdp->sd_move)) {
3136 					Os_desc	*osp = sdp->sd_isc->is_osdesc;
3137 					Word	ndx = osp->os_identndx;
3138 
3139 					omvp->m_info =
3140 					    /* LINTED */
3141 					    ELF_M_INFO(ndx, imvp->m_info);
3142 
3143 					omvp->m_poffset += sym->st_value;
3144 				} else {
3145 					if (isredloc)
3146 						DBG_CALL(Dbg_syms_reduce(ofl,
3147 						    DBG_SYM_REDUCE_RETAIN,
3148 						    sdp, idx,
3149 						    ofl->ofl_osmove->os_name));
3150 
3151 					omvp->m_info =
3152 					    /* LINTED */
3153 					    ELF_M_INFO(sdp->sd_symndx,
3154 					    imvp->m_info);
3155 				}
3156 			}
3157 
3158 			DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0, omvp, sdp));
3159 			omvp++;
3160 			idx++;
3161 		}
3162 	}
3163 }
3164 
3165 /*
3166  * Scan through the SHT_GROUP output sections.  Update their sh_link/sh_info
3167  * fields as well as the section contents.
3168  */
3169 static uintptr_t
3170 update_ogroup(Ofl_desc *ofl)
3171 {
3172 	Aliste		idx;
3173 	Os_desc		*osp;
3174 	uintptr_t	error = 0;
3175 
3176 	for (APLIST_TRAVERSE(ofl->ofl_osgroups, idx, osp)) {
3177 		Is_desc		*isp;
3178 		Ifl_desc	*ifl;
3179 		Shdr		*shdr = osp->os_shdr;
3180 		Sym_desc	*sdp;
3181 		Xword		i, grpcnt;
3182 		Word		*gdata;
3183 
3184 		/*
3185 		 * Since input GROUP sections always create unique
3186 		 * output GROUP sections - we know there is only one
3187 		 * item on the list.
3188 		 */
3189 		isp = ld_os_first_isdesc(osp);
3190 
3191 		ifl = isp->is_file;
3192 		sdp = ifl->ifl_oldndx[isp->is_shdr->sh_info];
3193 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
3194 		shdr->sh_info = sdp->sd_symndx;
3195 
3196 		/*
3197 		 * Scan through the group data section and update
3198 		 * all of the links to new values.
3199 		 */
3200 		grpcnt = shdr->sh_size / shdr->sh_entsize;
3201 		gdata = (Word *)osp->os_outdata->d_buf;
3202 
3203 		for (i = 1; i < grpcnt; i++) {
3204 			Os_desc	*_osp;
3205 			Is_desc	*_isp = ifl->ifl_isdesc[gdata[i]];
3206 
3207 			/*
3208 			 * If the referenced section didn't make it to the
3209 			 * output file - just zero out the entry.
3210 			 */
3211 			if ((_osp = _isp->is_osdesc) == NULL)
3212 				gdata[i] = 0;
3213 			else
3214 				gdata[i] = (Word)elf_ndxscn(_osp->os_scn);
3215 		}
3216 	}
3217 	return (error);
3218 }
3219 
3220 static void
3221 update_ostrtab(Os_desc *osp, Str_tbl *stp, uint_t extra)
3222 {
3223 	Elf_Data	*data;
3224 
3225 	if (osp == NULL)
3226 		return;
3227 
3228 	data = osp->os_outdata;
3229 	assert(data->d_size == (st_getstrtab_sz(stp) + extra));
3230 	(void) st_setstrbuf(stp, data->d_buf, data->d_size - extra);
3231 	/* If leaving an extra hole at the end, zero it */
3232 	if (extra > 0)
3233 		(void) memset((char *)data->d_buf + data->d_size - extra,
3234 		    0x0, extra);
3235 }
3236 
3237 /*
3238  * Update capabilities information.
3239  *
3240  * If string table capabilities exist, then the associated string must be
3241  * translated into an offset into the string table.
3242  */
3243 static void
3244 update_oscap(Ofl_desc *ofl)
3245 {
3246 	Os_desc		*strosp, *cosp;
3247 	Cap		*cap;
3248 	Str_tbl		*strtbl;
3249 	Capstr		*capstr;
3250 	size_t		stoff;
3251 	Aliste		idx1;
3252 
3253 	/*
3254 	 * Determine which symbol table or string table is appropriate.
3255 	 */
3256 	if (OFL_IS_STATIC_OBJ(ofl)) {
3257 		strosp = ofl->ofl_osstrtab;
3258 		strtbl = ofl->ofl_strtab;
3259 	} else {
3260 		strosp = ofl->ofl_osdynstr;
3261 		strtbl = ofl->ofl_dynstrtab;
3262 	}
3263 
3264 	/*
3265 	 * If symbol capabilities exist, set the sh_link field of the .SUNW_cap
3266 	 * section to the .SUNW_capinfo section.
3267 	 */
3268 	if (ofl->ofl_oscapinfo) {
3269 		cosp = ofl->ofl_oscap;
3270 		cosp->os_shdr->sh_link =
3271 		    (Word)elf_ndxscn(ofl->ofl_oscapinfo->os_scn);
3272 	}
3273 
3274 	/*
3275 	 * If there are capability strings to process, set the sh_info
3276 	 * field of the .SUNW_cap section to the associated string table, and
3277 	 * proceed to process any CA_SUNW_PLAT entries.
3278 	 */
3279 	if ((ofl->ofl_flags & FLG_OF_CAPSTRS) == 0)
3280 		return;
3281 
3282 	cosp = ofl->ofl_oscap;
3283 	cosp->os_shdr->sh_info = (Word)elf_ndxscn(strosp->os_scn);
3284 
3285 	cap = ofl->ofl_oscap->os_outdata->d_buf;
3286 
3287 	/*
3288 	 * Determine whether an object capability identifier, or object
3289 	 * machine/platform capabilities exists.
3290 	 */
3291 	capstr = &ofl->ofl_ocapset.oc_id;
3292 	if (capstr->cs_str) {
3293 		(void) st_setstring(strtbl, capstr->cs_str, &stoff);
3294 		cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3295 	}
3296 	for (ALIST_TRAVERSE(ofl->ofl_ocapset.oc_plat.cl_val, idx1, capstr)) {
3297 		(void) st_setstring(strtbl, capstr->cs_str, &stoff);
3298 		cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3299 	}
3300 	for (ALIST_TRAVERSE(ofl->ofl_ocapset.oc_mach.cl_val, idx1, capstr)) {
3301 		(void) st_setstring(strtbl, capstr->cs_str, &stoff);
3302 		cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3303 	}
3304 
3305 	/*
3306 	 * Determine any symbol capability identifiers, or machine/platform
3307 	 * capabilities.
3308 	 */
3309 	if (ofl->ofl_capgroups) {
3310 		Cap_group	*cgp;
3311 
3312 		for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
3313 			Objcapset	*ocapset = &cgp->cg_set;
3314 			Aliste		idx2;
3315 
3316 			capstr = &ocapset->oc_id;
3317 			if (capstr->cs_str) {
3318 				(void) st_setstring(strtbl, capstr->cs_str,
3319 				    &stoff);
3320 				cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3321 			}
3322 			for (ALIST_TRAVERSE(ocapset->oc_plat.cl_val, idx2,
3323 			    capstr)) {
3324 				(void) st_setstring(strtbl, capstr->cs_str,
3325 				    &stoff);
3326 				cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3327 			}
3328 			for (ALIST_TRAVERSE(ocapset->oc_mach.cl_val, idx2,
3329 			    capstr)) {
3330 				(void) st_setstring(strtbl, capstr->cs_str,
3331 				    &stoff);
3332 				cap[capstr->cs_ndx].c_un.c_ptr = stoff;
3333 			}
3334 		}
3335 	}
3336 }
3337 
3338 /*
3339  * Update the .SUNW_capinfo, and possibly the .SUNW_capchain sections.
3340  */
3341 static void
3342 update_oscapinfo(Ofl_desc *ofl)
3343 {
3344 	Os_desc		*symosp, *ciosp, *ccosp = NULL;
3345 	Capinfo		*ocapinfo;
3346 	Capchain	*ocapchain;
3347 	Cap_avlnode	*cav;
3348 	Word		chainndx = 0;
3349 
3350 	/*
3351 	 * Determine which symbol table is appropriate.
3352 	 */
3353 	if (OFL_IS_STATIC_OBJ(ofl))
3354 		symosp = ofl->ofl_ossymtab;
3355 	else
3356 		symosp = ofl->ofl_osdynsym;
3357 
3358 	/*
3359 	 * Update the .SUNW_capinfo sh_link to point to the appropriate symbol
3360 	 * table section.  If we're creating a dynamic object, the
3361 	 * .SUNW_capinfo sh_info is updated to point to the .SUNW_capchain
3362 	 * section.
3363 	 */
3364 	ciosp = ofl->ofl_oscapinfo;
3365 	ciosp->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
3366 
3367 	if (OFL_IS_STATIC_OBJ(ofl) == 0) {
3368 		ccosp = ofl->ofl_oscapchain;
3369 		ciosp->os_shdr->sh_info = (Word)elf_ndxscn(ccosp->os_scn);
3370 	}
3371 
3372 	/*
3373 	 * Establish the data for each section.  The first element of each
3374 	 * section defines the section's version number.
3375 	 */
3376 	ocapinfo = ciosp->os_outdata->d_buf;
3377 	ocapinfo[0] = CAPINFO_CURRENT;
3378 	if (ccosp) {
3379 		ocapchain = ccosp->os_outdata->d_buf;
3380 		ocapchain[chainndx++] = CAPCHAIN_CURRENT;
3381 	}
3382 
3383 	/*
3384 	 * Traverse all capabilities families.  Each member has a .SUNW_capinfo
3385 	 * assignment.  The .SUNW_capinfo entry differs for relocatable objects
3386 	 * and dynamic objects.
3387 	 *
3388 	 * Relocatable objects:
3389 	 *			ELF_C_GROUP		ELF_C_SYM
3390 	 *
3391 	 * Family lead:		CAPINFO_SUNW_GLOB	lead symbol index
3392 	 * Family lead alias:	CAPINFO_SUNW_GLOB	lead symbol index
3393 	 * Family member:	.SUNW_cap index		lead symbol index
3394 	 *
3395 	 * Dynamic objects:
3396 	 *			ELF_C_GROUP		ELF_C_SYM
3397 	 *
3398 	 * Family lead:		CAPINFO_SUNW_GLOB	.SUNW_capchain index
3399 	 * Family lead alias:	CAPINFO_SUNW_GLOB	.SUNW_capchain index
3400 	 * Family member:	.SUNW_cap index		lead symbol index
3401 	 *
3402 	 * The ELF_C_GROUP field identifies a capabilities symbol.  Lead
3403 	 * capability symbols, and lead capability aliases are identified by
3404 	 * a CAPINFO_SUNW_GLOB group identifier.  For family members, the
3405 	 * ELF_C_GROUP provides an index to the associate capabilities group
3406 	 * (i.e, an index into the SUNW_cap section that defines a group).
3407 	 *
3408 	 * For relocatable objects, the ELF_C_SYM field identifies the lead
3409 	 * capability symbol.  For the lead symbol itself, the .SUNW_capinfo
3410 	 * index is the same as the ELF_C_SYM value.  For lead alias symbols,
3411 	 * the .SUNW_capinfo index differs from the ELF_C_SYM value.  This
3412 	 * differentiation of CAPINFO_SUNW_GLOB symbols allows ld(1) to
3413 	 * identify, and propagate lead alias symbols.  For example, the lead
3414 	 * capability symbol memcpy() would have the ELF_C_SYM for memcpy(),
3415 	 * and the lead alias _memcpy() would also have the ELF_C_SYM for
3416 	 * memcpy().
3417 	 *
3418 	 * For dynamic objects, both a lead capability symbol, and alias symbol
3419 	 * would have a ELF_C_SYM value that represents the same capability
3420 	 * chain index.  The capability chain allows ld.so.1 to traverse a
3421 	 * family chain for a given lead symbol, and select the most appropriate
3422 	 * family member.  The .SUNW_capchain array contains a series of symbol
3423 	 * indexes for each family member:
3424 	 *
3425 	 *    chaincap[n]  chaincap[n + 1]  chaincap[n + 2]  chaincap[n + x]
3426 	 *	foo() ndx    foo%x() ndx	foo%y() ndx	0
3427 	 *
3428 	 * For family members, the ELF_C_SYM value associates the capability
3429 	 * members with their family lead symbol.  This association, although
3430 	 * unused within a dynamic object, allows ld(1) to identify, and
3431 	 * propagate family members when processing relocatable objects.
3432 	 */
3433 	for (cav = avl_first(ofl->ofl_capfamilies); cav;
3434 	    cav = AVL_NEXT(ofl->ofl_capfamilies, cav)) {
3435 		Cap_sym		*csp;
3436 		Aliste		idx;
3437 		Sym_desc	*asdp, *lsdp = cav->cn_symavlnode.sav_sdp;
3438 
3439 		if (ccosp) {
3440 			/*
3441 			 * For a dynamic object, identify this lead symbol, and
3442 			 * point it to the head of a capability chain.  Set the
3443 			 * head of the capability chain to the same lead symbol.
3444 			 */
3445 			ocapinfo[lsdp->sd_symndx] =
3446 			    ELF_C_INFO(chainndx, CAPINFO_SUNW_GLOB);
3447 			ocapchain[chainndx] = lsdp->sd_symndx;
3448 		} else {
3449 			/*
3450 			 * For a relocatable object, identify this lead symbol,
3451 			 * and set the lead symbol index to itself.
3452 			 */
3453 			ocapinfo[lsdp->sd_symndx] =
3454 			    ELF_C_INFO(lsdp->sd_symndx, CAPINFO_SUNW_GLOB);
3455 		}
3456 
3457 		/*
3458 		 * Gather any lead symbol aliases.
3459 		 */
3460 		for (APLIST_TRAVERSE(cav->cn_aliases, idx, asdp)) {
3461 			if (ccosp) {
3462 				/*
3463 				 * For a dynamic object, identify this lead
3464 				 * alias symbol, and point it to the same
3465 				 * capability chain index as the lead symbol.
3466 				 */
3467 				ocapinfo[asdp->sd_symndx] =
3468 				    ELF_C_INFO(chainndx, CAPINFO_SUNW_GLOB);
3469 			} else {
3470 				/*
3471 				 * For a relocatable object, identify this lead
3472 				 * alias symbol, and set the lead symbol index
3473 				 * to the lead symbol.
3474 				 */
3475 				ocapinfo[asdp->sd_symndx] =
3476 				    ELF_C_INFO(lsdp->sd_symndx,
3477 				    CAPINFO_SUNW_GLOB);
3478 			}
3479 		}
3480 
3481 		chainndx++;
3482 
3483 		/*
3484 		 * Gather the family members.
3485 		 */
3486 		for (APLIST_TRAVERSE(cav->cn_members, idx, csp)) {
3487 			Sym_desc	*msdp = csp->cs_sdp;
3488 
3489 			/*
3490 			 * Identify the members capability group, and the lead
3491 			 * symbol of the family this symbol is a member of.
3492 			 */
3493 			ocapinfo[msdp->sd_symndx] =
3494 			    ELF_C_INFO(lsdp->sd_symndx, csp->cs_group->cg_ndx);
3495 			if (ccosp) {
3496 				/*
3497 				 * For a dynamic object, set the next capability
3498 				 * chain to point to this family member.
3499 				 */
3500 				ocapchain[chainndx++] = msdp->sd_symndx;
3501 			}
3502 		}
3503 
3504 		/*
3505 		 * Any chain of family members is terminated with a 0 element.
3506 		 */
3507 		if (ccosp)
3508 			ocapchain[chainndx++] = 0;
3509 	}
3510 }
3511 
3512 /*
3513  * Translate the shdr->sh_{link, info} from its input section value to that
3514  * of the corresponding shdr->sh_{link, info} output section value.
3515  */
3516 static Word
3517 translate_link(Ofl_desc *ofl, Os_desc *osp, Word link, const char *msg)
3518 {
3519 	Is_desc		*isp;
3520 	Ifl_desc	*ifl;
3521 
3522 	/*
3523 	 * Don't translate the special section numbers.
3524 	 */
3525 	if (link >= SHN_LORESERVE)
3526 		return (link);
3527 
3528 	/*
3529 	 * Does this output section translate back to an input file.  If not
3530 	 * then there is no translation to do.  In this case we will assume that
3531 	 * if sh_link has a value, it's the right value.
3532 	 */
3533 	isp = ld_os_first_isdesc(osp);
3534 	if ((ifl = isp->is_file) == NULL)
3535 		return (link);
3536 
3537 	/*
3538 	 * Sanity check to make sure that the sh_{link, info} value
3539 	 * is within range for the input file.
3540 	 */
3541 	if (link >= ifl->ifl_shnum) {
3542 		eprintf(ofl->ofl_lml, ERR_WARNING, msg, ifl->ifl_name,
3543 		    EC_WORD(isp->is_scnndx), isp->is_name, EC_XWORD(link));
3544 		return (link);
3545 	}
3546 
3547 	/*
3548 	 * Follow the link to the input section.
3549 	 */
3550 	if ((isp = ifl->ifl_isdesc[link]) == NULL)
3551 		return (0);
3552 	if ((osp = isp->is_osdesc) == NULL)
3553 		return (0);
3554 
3555 	/* LINTED */
3556 	return ((Word)elf_ndxscn(osp->os_scn));
3557 }
3558 
3559 /*
3560  * Having created all of the necessary sections, segments, and associated
3561  * headers, fill in the program headers and update any other data in the
3562  * output image.  Some general rules:
3563  *
3564  *  -	If an interpreter is required always generate a PT_PHDR entry as
3565  *	well.  It is this entry that triggers the kernel into passing the
3566  *	interpreter an aux vector instead of just a file descriptor.
3567  *
3568  *  -	When generating an image that will be interpreted (ie. a dynamic
3569  *	executable, a shared object, or a static executable that has been
3570  *	provided with an interpreter - weird, but possible), make the initial
3571  *	loadable segment include both the ehdr and phdr[].  Both of these
3572  *	tables are used by the interpreter therefore it seems more intuitive
3573  *	to explicitly defined them as part of the mapped image rather than
3574  *	relying on page rounding by the interpreter to allow their access.
3575  *
3576  *  -	When generating a static image that does not require an interpreter
3577  *	have the first loadable segment indicate the address of the first
3578  *	.section as the start address (things like /kernel/unix and ufsboot
3579  *	expect this behavior).
3580  */
3581 uintptr_t
3582 ld_update_outfile(Ofl_desc *ofl)
3583 {
3584 	Addr		size, etext, vaddr;
3585 	Sg_desc		*sgp;
3586 	Sg_desc		*dtracesgp = NULL, *capsgp = NULL, *intpsgp = NULL;
3587 	Os_desc		*osp;
3588 	int		phdrndx = 0, segndx = -1, secndx, intppndx, intpsndx;
3589 	int		dtracepndx, dtracesndx, cappndx, capsndx;
3590 	Ehdr		*ehdr = ofl->ofl_nehdr;
3591 	Shdr		*hshdr;
3592 	Phdr		*_phdr = NULL;
3593 	Word		phdrsz = (ehdr->e_phnum * ehdr->e_phentsize), shscnndx;
3594 	ofl_flag_t	flags = ofl->ofl_flags;
3595 	Word		ehdrsz = ehdr->e_ehsize;
3596 	Boolean		nobits;
3597 	Off		offset;
3598 	Aliste		idx1;
3599 
3600 	/*
3601 	 * Initialize the starting address for the first segment.  Executables
3602 	 * have different starting addresses depending upon the target ABI,
3603 	 * where as shared objects have a starting address of 0.  If this is
3604 	 * a 64-bit executable that is being constructed to run in a restricted
3605 	 * address space, use an alternative origin that will provide more free
3606 	 * address space for the the eventual process.
3607 	 */
3608 	if (ofl->ofl_flags & FLG_OF_EXEC) {
3609 #if	defined(_ELF64)
3610 		if (ofl->ofl_ocapset.oc_sf_1.cm_val & SF1_SUNW_ADDR32)
3611 			vaddr = ld_targ.t_m.m_segm_aorigin;
3612 		else
3613 #endif
3614 			vaddr = ld_targ.t_m.m_segm_origin;
3615 	} else
3616 		vaddr = 0;
3617 
3618 	/*
3619 	 * Loop through the segment descriptors and pick out what we need.
3620 	 */
3621 	DBG_CALL(Dbg_seg_title(ofl->ofl_lml));
3622 	for (APLIST_TRAVERSE(ofl->ofl_segs, idx1, sgp)) {
3623 		Phdr		*phdr = &(sgp->sg_phdr);
3624 		Xword 		p_align;
3625 		Aliste		idx2;
3626 		Sym_desc	*sdp;
3627 
3628 		segndx++;
3629 
3630 		/*
3631 		 * If an interpreter is required generate a PT_INTERP and
3632 		 * PT_PHDR program header entry.  The PT_PHDR entry describes
3633 		 * the program header table itself.  This information will be
3634 		 * passed via the aux vector to the interpreter (ld.so.1).
3635 		 * The program header array is actually part of the first
3636 		 * loadable segment (and the PT_PHDR entry is the first entry),
3637 		 * therefore its virtual address isn't known until the first
3638 		 * loadable segment is processed.
3639 		 */
3640 		if (phdr->p_type == PT_PHDR) {
3641 			if (ofl->ofl_osinterp) {
3642 				phdr->p_offset = ehdr->e_phoff;
3643 				phdr->p_filesz = phdr->p_memsz = phdrsz;
3644 
3645 				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3646 				ofl->ofl_phdr[phdrndx++] = *phdr;
3647 			}
3648 			continue;
3649 		}
3650 		if (phdr->p_type == PT_INTERP) {
3651 			if (ofl->ofl_osinterp) {
3652 				intpsgp = sgp;
3653 				intpsndx = segndx;
3654 				intppndx = phdrndx++;
3655 			}
3656 			continue;
3657 		}
3658 
3659 		/*
3660 		 * If we are creating a PT_SUNWDTRACE segment, remember where
3661 		 * the program header is.  The header values are assigned after
3662 		 * update_osym() has completed and the symbol table addresses
3663 		 * have been updated.
3664 		 */
3665 		if (phdr->p_type == PT_SUNWDTRACE) {
3666 			if (ofl->ofl_dtracesym &&
3667 			    ((flags & FLG_OF_RELOBJ) == 0)) {
3668 				dtracesgp = sgp;
3669 				dtracesndx = segndx;
3670 				dtracepndx = phdrndx++;
3671 			}
3672 			continue;
3673 		}
3674 
3675 		/*
3676 		 * If a hardware/software capabilities section is required,
3677 		 * generate the PT_SUNWCAP header.  Note, as this comes before
3678 		 * the first loadable segment, we don't yet know its real
3679 		 * virtual address.  This is updated later.
3680 		 */
3681 		if (phdr->p_type == PT_SUNWCAP) {
3682 			if (ofl->ofl_oscap && (ofl->ofl_flags & FLG_OF_PTCAP) &&
3683 			    ((flags & FLG_OF_RELOBJ) == 0)) {
3684 				capsgp = sgp;
3685 				capsndx = segndx;
3686 				cappndx = phdrndx++;
3687 			}
3688 			continue;
3689 		}
3690 
3691 		/*
3692 		 * As the dynamic program header occurs after the loadable
3693 		 * headers in the segment descriptor table, all the address
3694 		 * information for the .dynamic output section will have been
3695 		 * figured out by now.
3696 		 */
3697 		if (phdr->p_type == PT_DYNAMIC) {
3698 			if (OFL_ALLOW_DYNSYM(ofl)) {
3699 				Shdr	*shdr = ofl->ofl_osdynamic->os_shdr;
3700 
3701 				phdr->p_vaddr = shdr->sh_addr;
3702 				phdr->p_offset = shdr->sh_offset;
3703 				phdr->p_filesz = shdr->sh_size;
3704 				phdr->p_flags = ld_targ.t_m.m_dataseg_perm;
3705 
3706 				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3707 				ofl->ofl_phdr[phdrndx++] = *phdr;
3708 			}
3709 			continue;
3710 		}
3711 
3712 		/*
3713 		 * As the unwind (.eh_frame_hdr) program header occurs after
3714 		 * the loadable headers in the segment descriptor table, all
3715 		 * the address information for the .eh_frame output section
3716 		 * will have been figured out by now.
3717 		 */
3718 		if (phdr->p_type == PT_SUNW_UNWIND) {
3719 			Shdr	    *shdr;
3720 
3721 			if (ofl->ofl_unwindhdr == NULL)
3722 				continue;
3723 
3724 			shdr = ofl->ofl_unwindhdr->os_shdr;
3725 
3726 			phdr->p_flags = PF_R;
3727 			phdr->p_vaddr = shdr->sh_addr;
3728 			phdr->p_memsz = shdr->sh_size;
3729 			phdr->p_filesz = shdr->sh_size;
3730 			phdr->p_offset = shdr->sh_offset;
3731 			phdr->p_align = shdr->sh_addralign;
3732 			phdr->p_paddr = 0;
3733 			ofl->ofl_phdr[phdrndx++] = *phdr;
3734 			continue;
3735 		}
3736 
3737 		/*
3738 		 * The sunwstack program is used to convey non-default
3739 		 * flags for the process stack. Only emit it if it would
3740 		 * change the default.
3741 		 */
3742 		if (phdr->p_type == PT_SUNWSTACK) {
3743 			if ((sgp->sg_flags & FLG_SG_DISABLED) == 0)
3744 				ofl->ofl_phdr[phdrndx++] = *phdr;
3745 			continue;
3746 		}
3747 
3748 		/*
3749 		 * As the TLS program header occurs after the loadable
3750 		 * headers in the segment descriptor table, all the address
3751 		 * information for the .tls output section will have been
3752 		 * figured out by now.
3753 		 */
3754 		if (phdr->p_type == PT_TLS) {
3755 			Os_desc		*tlsosp;
3756 			Shdr		*lastfileshdr = NULL;
3757 			Shdr		*firstshdr = NULL, *lastshdr;
3758 			Aliste		idx;
3759 
3760 			if (ofl->ofl_ostlsseg == NULL)
3761 				continue;
3762 
3763 			/*
3764 			 * Scan the output sections that have contributed TLS.
3765 			 * Remember the first and last so as to determine the
3766 			 * TLS memory size requirement.  Remember the last
3767 			 * progbits section to determine the TLS data
3768 			 * contribution, which determines the TLS program
3769 			 * header filesz.
3770 			 */
3771 			for (APLIST_TRAVERSE(ofl->ofl_ostlsseg, idx, tlsosp)) {
3772 				Shdr	*tlsshdr = tlsosp->os_shdr;
3773 
3774 				if (firstshdr == NULL)
3775 					firstshdr = tlsshdr;
3776 				if (tlsshdr->sh_type != SHT_NOBITS)
3777 					lastfileshdr = tlsshdr;
3778 				lastshdr = tlsshdr;
3779 			}
3780 
3781 			phdr->p_flags = PF_R | PF_W;
3782 			phdr->p_vaddr = firstshdr->sh_addr;
3783 			phdr->p_offset = firstshdr->sh_offset;
3784 			phdr->p_align = firstshdr->sh_addralign;
3785 
3786 			/*
3787 			 * Determine the initialized TLS data size.  This
3788 			 * address range is from the start of the TLS segment
3789 			 * to the end of the last piece of initialized data.
3790 			 */
3791 			if (lastfileshdr)
3792 				phdr->p_filesz = lastfileshdr->sh_offset +
3793 				    lastfileshdr->sh_size - phdr->p_offset;
3794 			else
3795 				phdr->p_filesz = 0;
3796 
3797 			/*
3798 			 * Determine the total TLS memory size.  This includes
3799 			 * all TLS data and TLS uninitialized data.  This
3800 			 * address range is from the start of the TLS segment
3801 			 * to the memory address of the last piece of
3802 			 * uninitialized data.
3803 			 */
3804 			phdr->p_memsz = lastshdr->sh_addr +
3805 			    lastshdr->sh_size - phdr->p_vaddr;
3806 
3807 			DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3808 			ofl->ofl_phdr[phdrndx] = *phdr;
3809 			ofl->ofl_tlsphdr = &ofl->ofl_phdr[phdrndx++];
3810 			continue;
3811 		}
3812 
3813 		/*
3814 		 * If this is an empty segment declaration, it will occur after
3815 		 * all other loadable segments.  As empty segments can be
3816 		 * defined with fixed addresses, make sure that no loadable
3817 		 * segments overlap.  This might occur as the object evolves
3818 		 * and the loadable segments grow, thus encroaching upon an
3819 		 * existing segment reservation.
3820 		 *
3821 		 * Segments are only created for dynamic objects, thus this
3822 		 * checking can be skipped when building a relocatable object.
3823 		 */
3824 		if (!(flags & FLG_OF_RELOBJ) &&
3825 		    (sgp->sg_flags & FLG_SG_EMPTY)) {
3826 			int	i;
3827 			Addr	v_e;
3828 
3829 			vaddr = phdr->p_vaddr;
3830 			phdr->p_memsz = sgp->sg_length;
3831 			DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3832 			ofl->ofl_phdr[phdrndx++] = *phdr;
3833 
3834 			if (phdr->p_type != PT_LOAD)
3835 				continue;
3836 
3837 			v_e = vaddr + phdr->p_memsz;
3838 
3839 			/*
3840 			 * Check overlaps
3841 			 */
3842 			for (i = 0; i < phdrndx - 1; i++) {
3843 				Addr 	p_s = (ofl->ofl_phdr[i]).p_vaddr;
3844 				Addr 	p_e;
3845 
3846 				if ((ofl->ofl_phdr[i]).p_type != PT_LOAD)
3847 					continue;
3848 
3849 				p_e = p_s + (ofl->ofl_phdr[i]).p_memsz;
3850 				if (((p_s <= vaddr) && (p_e > vaddr)) ||
3851 				    ((vaddr <= p_s) && (v_e > p_s)))
3852 					eprintf(ofl->ofl_lml, ERR_WARNING,
3853 					    MSG_INTL(MSG_UPD_SEGOVERLAP),
3854 					    ofl->ofl_name, EC_ADDR(p_e),
3855 					    sgp->sg_name, EC_ADDR(vaddr));
3856 			}
3857 			continue;
3858 		}
3859 
3860 		/*
3861 		 * Having processed any of the special program headers any
3862 		 * remaining headers will be built to express individual
3863 		 * segments.  Segments are only built if they have output
3864 		 * section descriptors associated with them (ie. some form of
3865 		 * input section has been matched to this segment).
3866 		 */
3867 		if (sgp->sg_osdescs == NULL)
3868 			continue;
3869 
3870 		/*
3871 		 * Determine the segments offset and size from the section
3872 		 * information provided from elf_update().
3873 		 * Allow for multiple NOBITS sections.
3874 		 */
3875 		osp = sgp->sg_osdescs->apl_data[0];
3876 		hshdr = osp->os_shdr;
3877 
3878 		phdr->p_filesz = 0;
3879 		phdr->p_memsz = 0;
3880 		phdr->p_offset = offset = hshdr->sh_offset;
3881 
3882 		nobits = ((hshdr->sh_type == SHT_NOBITS) &&
3883 		    ((sgp->sg_flags & FLG_SG_PHREQ) == 0));
3884 
3885 		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
3886 			Shdr	*shdr = osp->os_shdr;
3887 
3888 			p_align = 0;
3889 			if (shdr->sh_addralign > p_align)
3890 				p_align = shdr->sh_addralign;
3891 
3892 			offset = (Off)S_ROUND(offset, shdr->sh_addralign);
3893 			offset += shdr->sh_size;
3894 
3895 			if (shdr->sh_type != SHT_NOBITS) {
3896 				if (nobits) {
3897 					eprintf(ofl->ofl_lml, ERR_FATAL,
3898 					    MSG_INTL(MSG_UPD_NOBITS));
3899 					return (S_ERROR);
3900 				}
3901 				phdr->p_filesz = offset - phdr->p_offset;
3902 			} else if ((sgp->sg_flags & FLG_SG_PHREQ) == 0)
3903 				nobits = TRUE;
3904 		}
3905 		phdr->p_memsz = offset - hshdr->sh_offset;
3906 
3907 		/*
3908 		 * If this is the first loadable segment of a dynamic object,
3909 		 * or an interpreter has been specified (a static object built
3910 		 * with an interpreter will still be given a PT_HDR entry), then
3911 		 * compensate for the elf header and program header array.  Both
3912 		 * of these are actually part of the loadable segment as they
3913 		 * may be inspected by the interpreter.  Adjust the segments
3914 		 * size and offset accordingly.
3915 		 */
3916 		if ((_phdr == NULL) && (phdr->p_type == PT_LOAD) &&
3917 		    ((ofl->ofl_osinterp) || (flags & FLG_OF_DYNAMIC)) &&
3918 		    (!(ofl->ofl_dtflags_1 & DF_1_NOHDR))) {
3919 			size = (Addr)S_ROUND((phdrsz + ehdrsz),
3920 			    hshdr->sh_addralign);
3921 			phdr->p_offset -= size;
3922 			phdr->p_filesz += size;
3923 			phdr->p_memsz += size;
3924 		}
3925 
3926 		/*
3927 		 * If segment size symbols are required (specified via a
3928 		 * mapfile) update their value.
3929 		 */
3930 		for (APLIST_TRAVERSE(sgp->sg_sizesym, idx2, sdp))
3931 			sdp->sd_sym->st_value = phdr->p_memsz;
3932 
3933 		/*
3934 		 * If no file content has been assigned to this segment (it
3935 		 * only contains no-bits sections), then reset the offset for
3936 		 * consistency.
3937 		 */
3938 		if (phdr->p_filesz == 0)
3939 			phdr->p_offset = 0;
3940 
3941 		/*
3942 		 * If a virtual address has been specified for this segment
3943 		 * from a mapfile use it and make sure the previous segment
3944 		 * does not run into this segment.
3945 		 */
3946 		if (phdr->p_type == PT_LOAD) {
3947 			if ((sgp->sg_flags & FLG_SG_P_VADDR)) {
3948 				if (_phdr && (vaddr > phdr->p_vaddr) &&
3949 				    (phdr->p_type == PT_LOAD))
3950 					eprintf(ofl->ofl_lml, ERR_WARNING,
3951 					    MSG_INTL(MSG_UPD_SEGOVERLAP),
3952 					    ofl->ofl_name, EC_ADDR(vaddr),
3953 					    sgp->sg_name,
3954 					    EC_ADDR(phdr->p_vaddr));
3955 				vaddr = phdr->p_vaddr;
3956 				phdr->p_align = 0;
3957 			} else {
3958 				vaddr = phdr->p_vaddr =
3959 				    (Addr)S_ROUND(vaddr, phdr->p_align);
3960 			}
3961 		}
3962 
3963 		/*
3964 		 * Adjust the address offset and p_align if needed.
3965 		 */
3966 		if (((sgp->sg_flags & FLG_SG_P_VADDR) == 0) &&
3967 		    ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0)) {
3968 			if (phdr->p_align != 0)
3969 				vaddr += phdr->p_offset % phdr->p_align;
3970 			else
3971 				vaddr += phdr->p_offset;
3972 			phdr->p_vaddr = vaddr;
3973 		}
3974 
3975 		/*
3976 		 * If an interpreter is required set the virtual address of the
3977 		 * PT_PHDR program header now that we know the virtual address
3978 		 * of the loadable segment that contains it.  Update the
3979 		 * PT_SUNWCAP header similarly.
3980 		 */
3981 		if ((_phdr == NULL) && (phdr->p_type == PT_LOAD)) {
3982 			_phdr = phdr;
3983 
3984 			if ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0) {
3985 				if (ofl->ofl_osinterp)
3986 					ofl->ofl_phdr[0].p_vaddr =
3987 					    vaddr + ehdrsz;
3988 
3989 				/*
3990 				 * Finally, if we're creating a dynamic object
3991 				 * (or a static object in which an interpreter
3992 				 * is specified) update the vaddr to reflect
3993 				 * the address of the first section within this
3994 				 * segment.
3995 				 */
3996 				if ((ofl->ofl_osinterp) ||
3997 				    (flags & FLG_OF_DYNAMIC))
3998 					vaddr += size;
3999 			} else {
4000 				/*
4001 				 * If the DF_1_NOHDR flag was set, and an
4002 				 * interpreter is being generated, the PT_PHDR
4003 				 * will not be part of any loadable segment.
4004 				 */
4005 				if (ofl->ofl_osinterp) {
4006 					ofl->ofl_phdr[0].p_vaddr = 0;
4007 					ofl->ofl_phdr[0].p_memsz = 0;
4008 					ofl->ofl_phdr[0].p_flags = 0;
4009 				}
4010 			}
4011 		}
4012 
4013 		/*
4014 		 * Ensure the ELF entry point defaults to zero.  Typically, this
4015 		 * value is overridden in update_oehdr() to one of the standard
4016 		 * entry points.  Historically, this default was set to the
4017 		 * address of first executable section, but this has since been
4018 		 * found to be more confusing than it is helpful.
4019 		 */
4020 		ehdr->e_entry = 0;
4021 
4022 		DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
4023 
4024 		/*
4025 		 * Traverse the output section descriptors for this segment so
4026 		 * that we can update the section headers addresses.  We've
4027 		 * calculated the virtual address of the initial section within
4028 		 * this segment, so each successive section can be calculated
4029 		 * based on their offsets from each other.
4030 		 */
4031 		secndx = 0;
4032 		hshdr = 0;
4033 		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx2, osp)) {
4034 			Shdr	*shdr = osp->os_shdr;
4035 
4036 			if (shdr->sh_link)
4037 				shdr->sh_link = translate_link(ofl, osp,
4038 				    shdr->sh_link, MSG_INTL(MSG_FIL_INVSHLINK));
4039 
4040 			if (shdr->sh_info && (shdr->sh_flags & SHF_INFO_LINK))
4041 				shdr->sh_info = translate_link(ofl, osp,
4042 				    shdr->sh_info, MSG_INTL(MSG_FIL_INVSHINFO));
4043 
4044 			if (!(flags & FLG_OF_RELOBJ) &&
4045 			    (phdr->p_type == PT_LOAD)) {
4046 				if (hshdr)
4047 					vaddr += (shdr->sh_offset -
4048 					    hshdr->sh_offset);
4049 
4050 				shdr->sh_addr = vaddr;
4051 				hshdr = shdr;
4052 			}
4053 
4054 			DBG_CALL(Dbg_seg_os(ofl, osp, secndx));
4055 			secndx++;
4056 		}
4057 
4058 		/*
4059 		 * Establish the virtual address of the end of the last section
4060 		 * in this segment so that the next segments offset can be
4061 		 * calculated from this.
4062 		 */
4063 		if (hshdr)
4064 			vaddr += hshdr->sh_size;
4065 
4066 		/*
4067 		 * Output sections for this segment complete.  Adjust the
4068 		 * virtual offset for the last sections size, and make sure we
4069 		 * haven't exceeded any maximum segment length specification.
4070 		 */
4071 		if ((sgp->sg_length != 0) && (sgp->sg_length < phdr->p_memsz)) {
4072 			eprintf(ofl->ofl_lml, ERR_FATAL,
4073 			    MSG_INTL(MSG_UPD_LARGSIZE), ofl->ofl_name,
4074 			    sgp->sg_name, EC_XWORD(phdr->p_memsz),
4075 			    EC_XWORD(sgp->sg_length));
4076 			return (S_ERROR);
4077 		}
4078 
4079 		if (phdr->p_type == PT_NOTE) {
4080 			phdr->p_vaddr = 0;
4081 			phdr->p_paddr = 0;
4082 			phdr->p_align = 0;
4083 			phdr->p_memsz = 0;
4084 		}
4085 
4086 		if ((phdr->p_type != PT_NULL) && !(flags & FLG_OF_RELOBJ))
4087 			ofl->ofl_phdr[phdrndx++] = *phdr;
4088 	}
4089 
4090 	/*
4091 	 * Update any new output sections.  When building the initial output
4092 	 * image, a number of sections were created but left uninitialized (eg.
4093 	 * .dynsym, .dynstr, .symtab, .symtab, etc.).  Here we update these
4094 	 * sections with the appropriate data.  Other sections may still be
4095 	 * modified via reloc_process().
4096 	 *
4097 	 * Copy the interpreter name into the .interp section.
4098 	 */
4099 	if (ofl->ofl_interp)
4100 		(void) strcpy((char *)ofl->ofl_osinterp->os_outdata->d_buf,
4101 		    ofl->ofl_interp);
4102 
4103 	/*
4104 	 * Update the .shstrtab, .strtab and .dynstr sections.
4105 	 */
4106 	update_ostrtab(ofl->ofl_osshstrtab, ofl->ofl_shdrsttab, 0);
4107 	update_ostrtab(ofl->ofl_osstrtab, ofl->ofl_strtab, 0);
4108 	update_ostrtab(ofl->ofl_osdynstr, ofl->ofl_dynstrtab, DYNSTR_EXTRA_PAD);
4109 
4110 	/*
4111 	 * Build any output symbol tables, the symbols information is copied
4112 	 * and updated into the new output image.
4113 	 */
4114 	if ((etext = update_osym(ofl)) == (Addr)S_ERROR)
4115 		return (S_ERROR);
4116 
4117 	/*
4118 	 * If we have an PT_INTERP phdr, update it now from the associated
4119 	 * section information.
4120 	 */
4121 	if (intpsgp) {
4122 		Phdr	*phdr = &(intpsgp->sg_phdr);
4123 		Shdr	*shdr = ofl->ofl_osinterp->os_shdr;
4124 
4125 		phdr->p_vaddr = shdr->sh_addr;
4126 		phdr->p_offset = shdr->sh_offset;
4127 		phdr->p_memsz = phdr->p_filesz = shdr->sh_size;
4128 		phdr->p_flags = PF_R;
4129 
4130 		DBG_CALL(Dbg_seg_entry(ofl, intpsndx, intpsgp));
4131 		ofl->ofl_phdr[intppndx] = *phdr;
4132 	}
4133 
4134 	/*
4135 	 * If we have a PT_SUNWDTRACE phdr, update it now with the address of
4136 	 * the symbol.  It's only now been updated via update_sym().
4137 	 */
4138 	if (dtracesgp) {
4139 		Phdr		*aphdr, *phdr = &(dtracesgp->sg_phdr);
4140 		Sym_desc	*sdp = ofl->ofl_dtracesym;
4141 
4142 		phdr->p_vaddr = sdp->sd_sym->st_value;
4143 		phdr->p_memsz = sdp->sd_sym->st_size;
4144 
4145 		/*
4146 		 * Take permissions from the segment that the symbol is
4147 		 * associated with.
4148 		 */
4149 		aphdr = &sdp->sd_isc->is_osdesc->os_sgdesc->sg_phdr;
4150 		assert(aphdr);
4151 		phdr->p_flags = aphdr->p_flags;
4152 
4153 		DBG_CALL(Dbg_seg_entry(ofl, dtracesndx, dtracesgp));
4154 		ofl->ofl_phdr[dtracepndx] = *phdr;
4155 	}
4156 
4157 	/*
4158 	 * If we have a PT_SUNWCAP phdr, update it now from the associated
4159 	 * section information.
4160 	 */
4161 	if (capsgp) {
4162 		Phdr	*phdr = &(capsgp->sg_phdr);
4163 		Shdr	*shdr = ofl->ofl_oscap->os_shdr;
4164 
4165 		phdr->p_vaddr = shdr->sh_addr;
4166 		phdr->p_offset = shdr->sh_offset;
4167 		phdr->p_memsz = phdr->p_filesz = shdr->sh_size;
4168 		phdr->p_flags = PF_R;
4169 
4170 		DBG_CALL(Dbg_seg_entry(ofl, capsndx, capsgp));
4171 		ofl->ofl_phdr[cappndx] = *phdr;
4172 	}
4173 
4174 	/*
4175 	 * Update the GROUP sections.
4176 	 */
4177 	if (update_ogroup(ofl) == S_ERROR)
4178 		return (S_ERROR);
4179 
4180 	/*
4181 	 * Update Move Table.
4182 	 */
4183 	if (ofl->ofl_osmove || ofl->ofl_isparexpn)
4184 		update_move(ofl);
4185 
4186 	/*
4187 	 * Build any output headers, version information, dynamic structure and
4188 	 * syminfo structure.
4189 	 */
4190 	if (update_oehdr(ofl) == S_ERROR)
4191 		return (S_ERROR);
4192 	if (!(flags & FLG_OF_NOVERSEC)) {
4193 		if ((flags & FLG_OF_VERDEF) &&
4194 		    (update_overdef(ofl) == S_ERROR))
4195 			return (S_ERROR);
4196 		if ((flags & FLG_OF_VERNEED) &&
4197 		    (update_overneed(ofl) == S_ERROR))
4198 			return (S_ERROR);
4199 		if (flags & (FLG_OF_VERNEED | FLG_OF_VERDEF))
4200 			update_oversym(ofl);
4201 	}
4202 	if (flags & FLG_OF_DYNAMIC) {
4203 		if (update_odynamic(ofl) == S_ERROR)
4204 			return (S_ERROR);
4205 	}
4206 	if (ofl->ofl_ossyminfo) {
4207 		if (update_osyminfo(ofl) == S_ERROR)
4208 			return (S_ERROR);
4209 	}
4210 
4211 	/*
4212 	 * Update capabilities information if required.
4213 	 */
4214 	if (ofl->ofl_oscap)
4215 		update_oscap(ofl);
4216 	if (ofl->ofl_oscapinfo)
4217 		update_oscapinfo(ofl);
4218 
4219 	/*
4220 	 * Sanity test: the first and last data byte of a string table
4221 	 * must be NULL.
4222 	 */
4223 	assert((ofl->ofl_osshstrtab == NULL) ||
4224 	    (*((char *)ofl->ofl_osshstrtab->os_outdata->d_buf) == '\0'));
4225 	assert((ofl->ofl_osshstrtab == NULL) ||
4226 	    (*(((char *)ofl->ofl_osshstrtab->os_outdata->d_buf) +
4227 	    ofl->ofl_osshstrtab->os_outdata->d_size - 1) == '\0'));
4228 
4229 	assert((ofl->ofl_osstrtab == NULL) ||
4230 	    (*((char *)ofl->ofl_osstrtab->os_outdata->d_buf) == '\0'));
4231 	assert((ofl->ofl_osstrtab == NULL) ||
4232 	    (*(((char *)ofl->ofl_osstrtab->os_outdata->d_buf) +
4233 	    ofl->ofl_osstrtab->os_outdata->d_size - 1) == '\0'));
4234 
4235 	assert((ofl->ofl_osdynstr == NULL) ||
4236 	    (*((char *)ofl->ofl_osdynstr->os_outdata->d_buf) == '\0'));
4237 	assert((ofl->ofl_osdynstr == NULL) ||
4238 	    (*(((char *)ofl->ofl_osdynstr->os_outdata->d_buf) +
4239 	    ofl->ofl_osdynstr->os_outdata->d_size - DYNSTR_EXTRA_PAD - 1) ==
4240 	    '\0'));
4241 
4242 	/*
4243 	 * Emit Strtab diagnostics.
4244 	 */
4245 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osshstrtab,
4246 	    ofl->ofl_shdrsttab));
4247 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osstrtab,
4248 	    ofl->ofl_strtab));
4249 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osdynstr,
4250 	    ofl->ofl_dynstrtab));
4251 
4252 	/*
4253 	 * Initialize the section headers string table index within the elf
4254 	 * header.
4255 	 */
4256 	/* LINTED */
4257 	if ((shscnndx = elf_ndxscn(ofl->ofl_osshstrtab->os_scn)) <
4258 	    SHN_LORESERVE) {
4259 		ofl->ofl_nehdr->e_shstrndx =
4260 		    /* LINTED */
4261 		    (Half)shscnndx;
4262 	} else {
4263 		/*
4264 		 * If the STRTAB section index doesn't fit into
4265 		 * e_shstrndx, then we store it in 'shdr[0].st_link'.
4266 		 */
4267 		Elf_Scn	*scn;
4268 		Shdr	*shdr0;
4269 
4270 		if ((scn = elf_getscn(ofl->ofl_elf, 0)) == NULL) {
4271 			eprintf(ofl->ofl_lml, ERR_ELF,
4272 			    MSG_INTL(MSG_ELF_GETSCN), ofl->ofl_name);
4273 			return (S_ERROR);
4274 		}
4275 		if ((shdr0 = elf_getshdr(scn)) == NULL) {
4276 			eprintf(ofl->ofl_lml, ERR_ELF,
4277 			    MSG_INTL(MSG_ELF_GETSHDR), ofl->ofl_name);
4278 			return (S_ERROR);
4279 		}
4280 		ofl->ofl_nehdr->e_shstrndx = SHN_XINDEX;
4281 		shdr0->sh_link = shscnndx;
4282 	}
4283 
4284 	return ((uintptr_t)etext);
4285 }
4286