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