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