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