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