xref: /titanic_52/usr/src/cmd/sgs/libld/common/update.c (revision 55553f719b521a0bb4deab6efc944cd30c1a56aa)
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 	Word		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 (ofl->ofl_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 (ofl->ofl_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 (ofl->ofl_flags & FLG_OF_RELOBJ) {
780 					sym->st_value = sunwdata1addr;
781 				} else {
782 					sym->st_value = laddr;
783 					laddr += sym->st_size;
784 				}
785 				sunwdata1addr += sym->st_size;
786 			}
787 
788 			/*
789 			 * If this isn't an UNDEF symbol (ie. an input section
790 			 * is associated), update the symbols value and index.
791 			 */
792 			if (((isc = sdp->sd_isc) != 0) && !update_done) {
793 				Word	sectndx;
794 
795 				osp = isc->is_osdesc;
796 				/* LINTED */
797 				sym->st_value +=
798 				    (Off)_elf_getxoff(isc->is_indata);
799 				if (!(flags & FLG_OF_RELOBJ)) {
800 					sym->st_value += osp->os_shdr->sh_addr;
801 					/*
802 					 * TLS symbols are relative to
803 					 * the TLS segment.
804 					 */
805 					if ((type == STT_TLS) &&
806 					    (ofl->ofl_tlsphdr)) {
807 						sym->st_value -=
808 						    ofl->ofl_tlsphdr->p_vaddr;
809 					}
810 				}
811 				/* LINTED */
812 				if ((sdp->sd_shndx = sectndx =
813 				    elf_ndxscn(osp->os_scn)) >= SHN_LORESERVE) {
814 					if (_symshndx) {
815 						*_symshndx = sectndx;
816 					}
817 					sym->st_shndx = SHN_XINDEX;
818 				} else {
819 					/* LINTED */
820 					sym->st_shndx = sectndx;
821 				}
822 			}
823 
824 			/*
825 			 * If entering the symbol in both the symtab and the
826 			 * ldynsym, then the one in symtab needs to be
827 			 * copied to ldynsym. If it is only in the ldynsym,
828 			 * then the code above already set it up and we have
829 			 * nothing more to do here.
830 			 */
831 			if (enter_in_symtab && enter_in_ldynsym) {
832 				ldynsym[ldynsym_ndx] = *sym;
833 				(void) st_setstring(dynstr, sdp->sd_name,
834 				    &stoff);
835 				ldynsym[ldynsym_ndx].st_name = stoff;
836 
837 				if (_symshndx && ldynshndx)
838 					ldynshndx[ldynsym_ndx] = *_symshndx;
839 
840 				/* Add it to sort section if it qualifies */
841 				ADD_TO_DYNSORT(sdp, sym, type, ldynsym_ndx);
842 
843 				ldynsym_ndx++;
844 			}
845 		}
846 	}
847 
848 	/*
849 	 * Two special symbols are `_init' and `_fini'.  If these are supplied
850 	 * by crti.o then they are used to represent the total concatenation of
851 	 * the `.init' and `.fini' sections.
852 	 *
853 	 * First, determine whether any .init or .fini sections exist.  If these
854 	 * sections exist when a dynamic object is being built, but no `_init'
855 	 * or `_fini' symbols are found, then the user is probably building this
856 	 * object directly from ld(1) rather than using a compiler driver that
857 	 * provides the symbols via crt's.
858 	 *
859 	 * If the .init or .fini section exist, and their associated symbols,
860 	 * determine the size of the sections and updated the symbols value
861 	 * accordingly.
862 	 */
863 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U), SYM_NOHASH, 0,
864 	    ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) && sdp->sd_isc &&
865 	    (sdp->sd_isc->is_osdesc == iosp)) {
866 		if (ld_sym_copy(sdp) == S_ERROR)
867 			return ((Addr)S_ERROR);
868 		sdp->sd_sym->st_size = 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 			    (ofl->ofl_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 static int
1958 update_odynamic(Ofl_desc *ofl)
1959 {
1960 	Listnode	*lnp;
1961 	Ifl_desc	*ifl;
1962 	Sym_desc	*sdp;
1963 	Shdr		*shdr;
1964 	Dyn		*_dyn = (Dyn *)ofl->ofl_osdynamic->os_outdata->d_buf;
1965 	Dyn		*dyn;
1966 	Str_tbl		*dynstr;
1967 	size_t		stoff;
1968 	Word		flags = ofl->ofl_flags;
1969 	Word		cnt;
1970 
1971 	dynstr = ofl->ofl_dynstrtab;
1972 	ofl->ofl_osdynamic->os_shdr->sh_link =
1973 	    /* LINTED */
1974 	    (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
1975 
1976 	dyn = _dyn;
1977 
1978 	for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, ifl)) {
1979 		if ((ifl->ifl_flags &
1980 		    (FLG_IF_IGNORE | FLG_IF_DEPREQD)) == FLG_IF_IGNORE)
1981 			continue;
1982 
1983 		/*
1984 		 * Create and set up the DT_POSFLAG_1 entry here if required.
1985 		 */
1986 		if ((ifl->ifl_flags & (FLG_IF_LAZYLD|FLG_IF_GRPPRM)) &&
1987 		    (ifl->ifl_flags & (FLG_IF_NEEDED))) {
1988 			dyn->d_tag = DT_POSFLAG_1;
1989 			if (ifl->ifl_flags & FLG_IF_LAZYLD)
1990 				dyn->d_un.d_val = DF_P1_LAZYLOAD;
1991 			if (ifl->ifl_flags & FLG_IF_GRPPRM)
1992 				dyn->d_un.d_val |= DF_P1_GROUPPERM;
1993 			dyn++;
1994 		}
1995 
1996 		if (ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR))
1997 			dyn->d_tag = DT_NEEDED;
1998 		else
1999 			continue;
2000 
2001 		(void) st_setstring(dynstr, ifl->ifl_soname, &stoff);
2002 		dyn->d_un.d_val = stoff;
2003 		/* LINTED */
2004 		ifl->ifl_neededndx = (Half)(((uintptr_t)dyn - (uintptr_t)_dyn) /
2005 		    sizeof (Dyn));
2006 		dyn++;
2007 	}
2008 
2009 	if (ofl->ofl_dtsfltrs != NULL) {
2010 		Dfltr_desc	*dftp;
2011 		Aliste		idx;
2012 
2013 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, idx, dftp)) {
2014 			if (dftp->dft_flag == FLG_SY_AUXFLTR)
2015 				dyn->d_tag = DT_SUNW_AUXILIARY;
2016 			else
2017 				dyn->d_tag = DT_SUNW_FILTER;
2018 
2019 			(void) st_setstring(dynstr, dftp->dft_str, &stoff);
2020 			dyn->d_un.d_val = stoff;
2021 			dftp->dft_ndx = (Half)(((uintptr_t)dyn -
2022 			    (uintptr_t)_dyn) / sizeof (Dyn));
2023 			dyn++;
2024 		}
2025 	}
2026 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
2027 	    SYM_NOHASH, 0, ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) &&
2028 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
2029 		dyn->d_tag = DT_INIT;
2030 		dyn->d_un.d_ptr = sdp->sd_sym->st_value;
2031 		dyn++;
2032 	}
2033 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
2034 	    SYM_NOHASH, 0, ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED) &&
2035 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
2036 		dyn->d_tag = DT_FINI;
2037 		dyn->d_un.d_ptr = sdp->sd_sym->st_value;
2038 		dyn++;
2039 	}
2040 	if (ofl->ofl_soname) {
2041 		dyn->d_tag = DT_SONAME;
2042 		(void) st_setstring(dynstr, ofl->ofl_soname, &stoff);
2043 		dyn->d_un.d_val = stoff;
2044 		dyn++;
2045 	}
2046 	if (ofl->ofl_filtees) {
2047 		if (flags & FLG_OF_AUX) {
2048 			dyn->d_tag = DT_AUXILIARY;
2049 		} else {
2050 			dyn->d_tag = DT_FILTER;
2051 		}
2052 		(void) st_setstring(dynstr, ofl->ofl_filtees, &stoff);
2053 		dyn->d_un.d_val = stoff;
2054 		dyn++;
2055 	}
2056 	if (ofl->ofl_rpath) {
2057 		(void) st_setstring(dynstr, ofl->ofl_rpath, &stoff);
2058 		dyn->d_tag = DT_RUNPATH;
2059 		dyn->d_un.d_val = stoff;
2060 		dyn++;
2061 		dyn->d_tag = DT_RPATH;
2062 		dyn->d_un.d_val = stoff;
2063 		dyn++;
2064 	}
2065 	if (ofl->ofl_config) {
2066 		dyn->d_tag = DT_CONFIG;
2067 		(void) st_setstring(dynstr, ofl->ofl_config, &stoff);
2068 		dyn->d_un.d_val = stoff;
2069 		dyn++;
2070 	}
2071 	if (ofl->ofl_depaudit) {
2072 		dyn->d_tag = DT_DEPAUDIT;
2073 		(void) st_setstring(dynstr, ofl->ofl_depaudit, &stoff);
2074 		dyn->d_un.d_val = stoff;
2075 		dyn++;
2076 	}
2077 	if (ofl->ofl_audit) {
2078 		dyn->d_tag = DT_AUDIT;
2079 		(void) st_setstring(dynstr, ofl->ofl_audit, &stoff);
2080 		dyn->d_un.d_val = stoff;
2081 		dyn++;
2082 	}
2083 
2084 	/*
2085 	 * The following DT_* entries do not apply to relocatable objects.
2086 	 */
2087 	if (!(flags & FLG_OF_RELOBJ)) {
2088 
2089 		dyn->d_tag = DT_HASH;
2090 		dyn->d_un.d_ptr = ofl->ofl_oshash->os_shdr->sh_addr;
2091 		dyn++;
2092 
2093 		shdr = ofl->ofl_osdynstr->os_shdr;
2094 		dyn->d_tag = DT_STRTAB;
2095 		dyn->d_un.d_ptr = shdr->sh_addr;
2096 		dyn++;
2097 
2098 		dyn->d_tag = DT_STRSZ;
2099 		dyn->d_un.d_ptr = shdr->sh_size;
2100 		dyn++;
2101 
2102 		shdr = ofl->ofl_osdynsym->os_shdr;
2103 		dyn->d_tag = DT_SYMTAB;
2104 		dyn->d_un.d_ptr = shdr->sh_addr;
2105 		dyn++;
2106 
2107 		dyn->d_tag = DT_SYMENT;
2108 		dyn->d_un.d_ptr = shdr->sh_entsize;
2109 		dyn++;
2110 
2111 		if (ofl->ofl_osldynsym) {
2112 			/*
2113 			 * We have arranged for the .SUNW_ldynsym data to be
2114 			 * immediately in front of the .dynsym data.
2115 			 * This means that you could start at the top
2116 			 * of .SUNW_ldynsym and see the data for both tables
2117 			 * without a break. This is the view we want to
2118 			 * provide for DT_SUNW_SYMTAB, which is why we
2119 			 * add the lengths together.
2120 			 */
2121 			Shdr *lshdr = ofl->ofl_osldynsym->os_shdr;
2122 			dyn->d_tag = DT_SUNW_SYMTAB;
2123 			dyn->d_un.d_ptr = lshdr->sh_addr;
2124 			dyn++;
2125 
2126 			dyn->d_tag = DT_SUNW_SYMSZ;
2127 			dyn->d_un.d_val = lshdr->sh_size + shdr->sh_size;
2128 			dyn++;
2129 		}
2130 
2131 		if (ofl->ofl_osdynsymsort || ofl->ofl_osdyntlssort) {
2132 			dyn->d_tag = DT_SUNW_SORTENT;
2133 			dyn->d_un.d_val = sizeof (Word);
2134 			dyn++;
2135 		}
2136 
2137 		if (ofl->ofl_osdynsymsort) {
2138 			dyn->d_tag = DT_SUNW_SYMSORT;
2139 			dyn->d_un.d_ptr =
2140 			    ofl->ofl_osdynsymsort->os_shdr->sh_addr;
2141 			dyn++;
2142 
2143 			dyn->d_tag = DT_SUNW_SYMSORTSZ;
2144 			dyn->d_un.d_val =
2145 			    ofl->ofl_osdynsymsort->os_shdr->sh_size;
2146 			dyn++;
2147 		}
2148 
2149 		if (ofl->ofl_osdyntlssort) {
2150 			dyn->d_tag = DT_SUNW_TLSSORT;
2151 			dyn->d_un.d_ptr =
2152 			    ofl->ofl_osdyntlssort->os_shdr->sh_addr;
2153 			dyn++;
2154 
2155 			dyn->d_tag = DT_SUNW_TLSSORTSZ;
2156 			dyn->d_un.d_val =
2157 			    ofl->ofl_osdyntlssort->os_shdr->sh_size;
2158 			dyn++;
2159 		}
2160 
2161 		/*
2162 		 * Reserve the DT_CHECKSUM entry.  Its value will be filled in
2163 		 * after the complete image is built.
2164 		 */
2165 		dyn->d_tag = DT_CHECKSUM;
2166 		ofl->ofl_checksum = &dyn->d_un.d_val;
2167 		dyn++;
2168 
2169 		/*
2170 		 * Versioning sections: DT_VERDEF and DT_VERNEED.
2171 		 *
2172 		 * The Solaris ld does not produce DT_VERSYM, but the GNU ld
2173 		 * does, in order to support their style of versioning, which
2174 		 * differs from ours:
2175 		 *
2176 		 *	- The top bit of the 16-bit Versym index is
2177 		 *		not part of the version, but is interpreted
2178 		 *		as a "hidden bit".
2179 		 *
2180 		 *	- External (SHN_UNDEF) symbols can have non-zero
2181 		 *		Versym values, which specify versions in
2182 		 *		referenced objects, via the Verneed section.
2183 		 *
2184 		 *	- The vna_other field of the Vernaux structures
2185 		 *		found in the Verneed section are not zero as
2186 		 *		with Solaris, but instead contain the version
2187 		 *		index to be used by Versym indices to reference
2188 		 *		the given external version.
2189 		 *
2190 		 * The Solaris ld, rtld, and elfdump programs all interpret the
2191 		 * presence of DT_VERSYM as meaning that GNU versioning rules
2192 		 * apply to the given file. If DT_VERSYM is not present,
2193 		 * then Solaris versioning rules apply. If we should ever need
2194 		 * to change our ld so that it does issue DT_VERSYM, then
2195 		 * this rule for detecting GNU versioning will no longer work.
2196 		 * In that case, we will have to invent a way to explicitly
2197 		 * specify the style of versioning in use, perhaps via a
2198 		 * new dynamic entry named something like DT_SUNW_VERSIONSTYLE,
2199 		 * where the d_un.d_val value specifies which style is to be
2200 		 * used.
2201 		 */
2202 		if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
2203 		    FLG_OF_VERDEF) {
2204 			shdr = ofl->ofl_osverdef->os_shdr;
2205 			dyn->d_tag = DT_VERDEF;
2206 			dyn->d_un.d_ptr = shdr->sh_addr;
2207 			dyn++;
2208 			dyn->d_tag = DT_VERDEFNUM;
2209 			dyn->d_un.d_ptr = shdr->sh_info;
2210 			dyn++;
2211 		}
2212 		if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
2213 		    FLG_OF_VERNEED) {
2214 			shdr = ofl->ofl_osverneed->os_shdr;
2215 			dyn->d_tag = DT_VERNEED;
2216 			dyn->d_un.d_ptr = shdr->sh_addr;
2217 			dyn++;
2218 			dyn->d_tag = DT_VERNEEDNUM;
2219 			dyn->d_un.d_ptr = shdr->sh_info;
2220 			dyn++;
2221 		}
2222 
2223 		if ((ofl->ofl_flags & FLG_OF_COMREL) && ofl->ofl_relocrelcnt) {
2224 			dyn->d_tag = ld_targ.t_m.m_rel_dt_count;
2225 			dyn->d_un.d_val = ofl->ofl_relocrelcnt;
2226 			dyn++;
2227 		}
2228 		if (flags & FLG_OF_TEXTREL) {
2229 			/*
2230 			 * Only the presence of this entry is used in this
2231 			 * implementation, not the value stored.
2232 			 */
2233 			dyn->d_tag = DT_TEXTREL;
2234 			dyn->d_un.d_val = 0;
2235 			dyn++;
2236 		}
2237 
2238 		if (ofl->ofl_osfiniarray) {
2239 			shdr = ofl->ofl_osfiniarray->os_shdr;
2240 
2241 			dyn->d_tag = DT_FINI_ARRAY;
2242 			dyn->d_un.d_ptr = shdr->sh_addr;
2243 			dyn++;
2244 
2245 			dyn->d_tag = DT_FINI_ARRAYSZ;
2246 			dyn->d_un.d_val = shdr->sh_size;
2247 			dyn++;
2248 		}
2249 
2250 		if (ofl->ofl_osinitarray) {
2251 			shdr = ofl->ofl_osinitarray->os_shdr;
2252 
2253 			dyn->d_tag = DT_INIT_ARRAY;
2254 			dyn->d_un.d_ptr = shdr->sh_addr;
2255 			dyn++;
2256 
2257 			dyn->d_tag = DT_INIT_ARRAYSZ;
2258 			dyn->d_un.d_val = shdr->sh_size;
2259 			dyn++;
2260 		}
2261 
2262 		if (ofl->ofl_ospreinitarray) {
2263 			shdr = ofl->ofl_ospreinitarray->os_shdr;
2264 
2265 			dyn->d_tag = DT_PREINIT_ARRAY;
2266 			dyn->d_un.d_ptr = shdr->sh_addr;
2267 			dyn++;
2268 
2269 			dyn->d_tag = DT_PREINIT_ARRAYSZ;
2270 			dyn->d_un.d_val = shdr->sh_size;
2271 			dyn++;
2272 		}
2273 
2274 		if (ofl->ofl_pltcnt) {
2275 			shdr =  ofl->ofl_osplt->os_relosdesc->os_shdr;
2276 
2277 			dyn->d_tag = DT_PLTRELSZ;
2278 			dyn->d_un.d_ptr = shdr->sh_size;
2279 			dyn++;
2280 			dyn->d_tag = DT_PLTREL;
2281 			dyn->d_un.d_ptr = ld_targ.t_m.m_rel_dt_type;
2282 			dyn++;
2283 			dyn->d_tag = DT_JMPREL;
2284 			dyn->d_un.d_ptr = shdr->sh_addr;
2285 			dyn++;
2286 		}
2287 		if (ofl->ofl_pltpad) {
2288 			shdr =  ofl->ofl_osplt->os_shdr;
2289 
2290 			dyn->d_tag = DT_PLTPAD;
2291 			if (ofl->ofl_pltcnt) {
2292 				dyn->d_un.d_ptr = shdr->sh_addr +
2293 				    ld_targ.t_m.m_plt_reservsz +
2294 				    ofl->ofl_pltcnt * ld_targ.t_m.m_plt_entsize;
2295 			} else
2296 				dyn->d_un.d_ptr = shdr->sh_addr;
2297 			dyn++;
2298 			dyn->d_tag = DT_PLTPADSZ;
2299 			dyn->d_un.d_val = ofl->ofl_pltpad *
2300 			    ld_targ.t_m.m_plt_entsize;
2301 			dyn++;
2302 		}
2303 		if (ofl->ofl_relocsz) {
2304 			dyn->d_tag = ld_targ.t_m.m_rel_dt_type;
2305 			dyn->d_un.d_ptr = ofl->ofl_osrelhead->os_shdr->sh_addr;
2306 			dyn++;
2307 			dyn->d_tag = ld_targ.t_m.m_rel_dt_size;
2308 			dyn->d_un.d_ptr = ofl->ofl_relocsz;
2309 			dyn++;
2310 			dyn->d_tag = ld_targ.t_m.m_rel_dt_ent;
2311 			if (ofl->ofl_osrelhead->os_shdr->sh_type == SHT_REL)
2312 				dyn->d_un.d_ptr = sizeof (Rel);
2313 			else
2314 				dyn->d_un.d_ptr = sizeof (Rela);
2315 			dyn++;
2316 		}
2317 		if (ofl->ofl_ossyminfo) {
2318 			shdr = ofl->ofl_ossyminfo->os_shdr;
2319 			dyn->d_tag = DT_SYMINFO;
2320 			dyn->d_un.d_ptr = shdr->sh_addr;
2321 			dyn++;
2322 			dyn->d_tag = DT_SYMINSZ;
2323 			dyn->d_un.d_val = shdr->sh_size;
2324 			dyn++;
2325 			dyn->d_tag = DT_SYMINENT;
2326 			dyn->d_un.d_val = sizeof (Syminfo);
2327 			dyn++;
2328 		}
2329 		if (ofl->ofl_osmove) {
2330 			Os_desc *	osp;
2331 
2332 			dyn->d_tag = DT_MOVEENT;
2333 			osp = ofl->ofl_osmove;
2334 			dyn->d_un.d_val = osp->os_shdr->sh_entsize;
2335 			dyn++;
2336 			dyn->d_tag = DT_MOVESZ;
2337 			dyn->d_un.d_val = osp->os_shdr->sh_size;
2338 			dyn++;
2339 			dyn->d_tag = DT_MOVETAB;
2340 			dyn->d_un.d_val = osp->os_shdr->sh_addr;
2341 			dyn++;
2342 		}
2343 		if (ofl->ofl_regsymcnt) {
2344 			int	ndx;
2345 
2346 			for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
2347 				if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
2348 					continue;
2349 
2350 				dyn->d_tag = ld_targ.t_m.m_dt_register;
2351 				dyn->d_un.d_val = sdp->sd_symndx;
2352 				dyn++;
2353 			}
2354 		}
2355 
2356 		for (LIST_TRAVERSE(&ofl->ofl_rtldinfo, lnp, sdp)) {
2357 			dyn->d_tag = DT_SUNW_RTLDINF;
2358 			dyn->d_un.d_ptr = sdp->sd_sym->st_value;
2359 			dyn++;
2360 		}
2361 
2362 		if (ofl->ofl_osdynamic->os_sgdesc &&
2363 		    (ofl->ofl_osdynamic->os_sgdesc->sg_phdr.p_flags & PF_W)) {
2364 			if (ofl->ofl_osinterp) {
2365 				dyn->d_tag = DT_DEBUG;
2366 				dyn->d_un.d_ptr = 0;
2367 				dyn++;
2368 			}
2369 
2370 			dyn->d_tag = DT_FEATURE_1;
2371 			if (ofl->ofl_osmove)
2372 				dyn->d_un.d_val = 0;
2373 			else
2374 				dyn->d_un.d_val = DTF_1_PARINIT;
2375 			dyn++;
2376 		}
2377 
2378 		if (ofl->ofl_oscap) {
2379 			dyn->d_tag = DT_SUNW_CAP;
2380 			dyn->d_un.d_val = ofl->ofl_oscap->os_shdr->sh_addr;
2381 			dyn++;
2382 		}
2383 	}
2384 
2385 	if (flags & FLG_OF_SYMBOLIC) {
2386 		dyn->d_tag = DT_SYMBOLIC;
2387 		dyn->d_un.d_val = 0;
2388 		dyn++;
2389 	}
2390 	dyn->d_tag = DT_FLAGS;
2391 	dyn->d_un.d_val = ofl->ofl_dtflags;
2392 	dyn++;
2393 
2394 	/*
2395 	 * If -Bdirect was specified, but some NODIRECT symbols were specified
2396 	 * via a mapfile, or -znodirect was used on the command line, then
2397 	 * clear the DF_1_DIRECT flag.  The resultant object will use per-symbol
2398 	 * direct bindings rather than be enabled for global direct bindings.
2399 	 */
2400 	if (ofl->ofl_flags1 & FLG_OF1_NDIRECT) {
2401 		ofl->ofl_dtflags_1 &= ~DF_1_DIRECT;
2402 		ofl->ofl_dtflags_1 |= DF_1_NODIRECT;
2403 	}
2404 
2405 	dyn->d_tag = DT_FLAGS_1;
2406 	dyn->d_un.d_val = ofl->ofl_dtflags_1;
2407 	dyn++;
2408 
2409 	dyn->d_tag = DT_SUNW_STRPAD;
2410 	dyn->d_un.d_val = DYNSTR_EXTRA_PAD;
2411 	dyn++;
2412 
2413 	dyn->d_tag = DT_SUNW_LDMACH;
2414 	dyn->d_un.d_val = ld_sunw_ldmach();
2415 	dyn++;
2416 
2417 	(*ld_targ.t_mr.mr_mach_update_odynamic)(ofl, &dyn);
2418 
2419 	for (cnt = 1 + DYNAMIC_EXTRA_ELTS; cnt--; dyn++) {
2420 		dyn->d_tag = DT_NULL;
2421 		dyn->d_un.d_val = 0;
2422 	}
2423 
2424 	/*
2425 	 * Ensure that we wrote the right number of entries. If not,
2426 	 * we either miscounted in make_dynamic(), or we did something wrong
2427 	 * in this function.
2428 	 */
2429 	assert((ofl->ofl_osdynamic->os_shdr->sh_size /
2430 	    ofl->ofl_osdynamic->os_shdr->sh_entsize) ==
2431 	    ((uintptr_t)dyn - (uintptr_t)_dyn) / sizeof (*dyn));
2432 
2433 	return (1);
2434 }
2435 
2436 /*
2437  * Build the version definition section
2438  */
2439 static int
2440 update_overdef(Ofl_desc *ofl)
2441 {
2442 	Listnode	*lnp1, *lnp2;
2443 	Ver_desc	*vdp, *_vdp;
2444 	Verdef		*vdf, *_vdf;
2445 	int		num = 0;
2446 	Os_desc		*strosp, *symosp;
2447 
2448 	/*
2449 	 * Traverse the version descriptors and update the version structures
2450 	 * to point to the dynstr name in preparation for building the version
2451 	 * section structure.
2452 	 */
2453 	for (LIST_TRAVERSE(&ofl->ofl_verdesc, lnp1, vdp)) {
2454 		Sym_desc *	sdp;
2455 
2456 		if (vdp->vd_flags & VER_FLG_BASE) {
2457 			const char	*name = vdp->vd_name;
2458 			size_t		stoff;
2459 
2460 			/*
2461 			 * Create a new string table entry to represent the base
2462 			 * version name (there is no corresponding symbol for
2463 			 * this).
2464 			 */
2465 			if (!(ofl->ofl_flags & FLG_OF_DYNAMIC)) {
2466 				(void) st_setstring(ofl->ofl_strtab,
2467 				    name, &stoff);
2468 				/* LINTED */
2469 				vdp->vd_name = (const char *)stoff;
2470 			} else {
2471 				(void) st_setstring(ofl->ofl_dynstrtab,
2472 				    name, &stoff);
2473 				/* LINTED */
2474 				vdp->vd_name = (const char *)stoff;
2475 			}
2476 		} else {
2477 			sdp = ld_sym_find(vdp->vd_name, vdp->vd_hash, 0, ofl);
2478 			/* LINTED */
2479 			vdp->vd_name = (const char *)
2480 			    (uintptr_t)sdp->sd_sym->st_name;
2481 		}
2482 	}
2483 
2484 	_vdf = vdf = (Verdef *)ofl->ofl_osverdef->os_outdata->d_buf;
2485 
2486 	/*
2487 	 * Traverse the version descriptors and update the version section to
2488 	 * reflect each version and its associated dependencies.
2489 	 */
2490 	for (LIST_TRAVERSE(&ofl->ofl_verdesc, lnp1, vdp)) {
2491 		Half		cnt = 1;
2492 		Verdaux *	vdap, * _vdap;
2493 
2494 		_vdap = vdap = (Verdaux *)(vdf + 1);
2495 
2496 		vdf->vd_version = VER_DEF_CURRENT;
2497 		vdf->vd_flags	= vdp->vd_flags & MSK_VER_USER;
2498 		vdf->vd_ndx	= vdp->vd_ndx;
2499 		vdf->vd_hash	= vdp->vd_hash;
2500 
2501 		/* LINTED */
2502 		vdap->vda_name = (uintptr_t)vdp->vd_name;
2503 		vdap++;
2504 		/* LINTED */
2505 		_vdap->vda_next = (Word)((uintptr_t)vdap - (uintptr_t)_vdap);
2506 
2507 		/*
2508 		 * Traverse this versions dependency list generating the
2509 		 * appropriate version dependency entries.
2510 		 */
2511 		for (LIST_TRAVERSE(&vdp->vd_deps, lnp2, _vdp)) {
2512 			/* LINTED */
2513 			vdap->vda_name = (uintptr_t)_vdp->vd_name;
2514 			_vdap = vdap;
2515 			vdap++, cnt++;
2516 			/* LINTED */
2517 			_vdap->vda_next = (Word)((uintptr_t)vdap -
2518 			    (uintptr_t)_vdap);
2519 		}
2520 		_vdap->vda_next = 0;
2521 
2522 		/*
2523 		 * Record the versions auxiliary array offset and the associated
2524 		 * dependency count.
2525 		 */
2526 		/* LINTED */
2527 		vdf->vd_aux = (Word)((uintptr_t)(vdf + 1) - (uintptr_t)vdf);
2528 		vdf->vd_cnt = cnt;
2529 
2530 		/*
2531 		 * Record the next versions offset and update the version
2532 		 * pointer.  Remember the previous version offset as the very
2533 		 * last structures next pointer should be null.
2534 		 */
2535 		_vdf = vdf;
2536 		vdf = (Verdef *)vdap, num++;
2537 		/* LINTED */
2538 		_vdf->vd_next = (Word)((uintptr_t)vdf - (uintptr_t)_vdf);
2539 	}
2540 	_vdf->vd_next = 0;
2541 
2542 	/*
2543 	 * Record the string table association with the version definition
2544 	 * section, and the symbol table associated with the version symbol
2545 	 * table (the actual contents of the version symbol table are filled
2546 	 * in during symbol update).
2547 	 */
2548 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) ||
2549 	    (ofl->ofl_flags & FLG_OF_STATIC)) {
2550 		strosp = ofl->ofl_osstrtab;
2551 		symosp = ofl->ofl_ossymtab;
2552 	} else {
2553 		strosp = ofl->ofl_osdynstr;
2554 		symosp = ofl->ofl_osdynsym;
2555 	}
2556 	/* LINTED */
2557 	ofl->ofl_osverdef->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
2558 	/* LINTED */
2559 	ofl->ofl_osversym->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
2560 
2561 	/*
2562 	 * The version definition sections `info' field is used to indicate the
2563 	 * number of entries in this section.
2564 	 */
2565 	ofl->ofl_osverdef->os_shdr->sh_info = num;
2566 
2567 	return (1);
2568 }
2569 
2570 /*
2571  * Build the version needed section
2572  */
2573 static int
2574 update_overneed(Ofl_desc *ofl)
2575 {
2576 	Listnode	*lnp;
2577 	Ifl_desc	*ifl;
2578 	Verneed		*vnd, *_vnd;
2579 	Str_tbl		*dynstr;
2580 	Word		num = 0, cnt = 0;
2581 
2582 	dynstr = ofl->ofl_dynstrtab;
2583 	_vnd = vnd = (Verneed *)ofl->ofl_osverneed->os_outdata->d_buf;
2584 
2585 	/*
2586 	 * Traverse the shared object list looking for dependencies that have
2587 	 * versions defined within them.
2588 	 */
2589 	for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, ifl)) {
2590 		Half		_cnt;
2591 		Vernaux		*_vnap, *vnap;
2592 		Sdf_desc	*sdf = ifl->ifl_sdfdesc;
2593 		size_t		stoff;
2594 
2595 		if (!(ifl->ifl_flags & FLG_IF_VERNEED))
2596 			continue;
2597 
2598 		vnd->vn_version = VER_NEED_CURRENT;
2599 
2600 		(void) st_setstring(dynstr, ifl->ifl_soname, &stoff);
2601 		vnd->vn_file = stoff;
2602 
2603 		_vnap = vnap = (Vernaux *)(vnd + 1);
2604 
2605 		if (sdf && (sdf->sdf_flags & FLG_SDF_SPECVER)) {
2606 			Sdv_desc	*sdv;
2607 			Listnode	*lnp2;
2608 
2609 			/*
2610 			 * If version needed definitions were specified in
2611 			 * a mapfile ($VERSION=*) then record those
2612 			 * definitions.
2613 			 */
2614 			for (LIST_TRAVERSE(&sdf->sdf_verneed, lnp2, sdv)) {
2615 				(void) st_setstring(dynstr, sdv->sdv_name,
2616 				    &stoff);
2617 				vnap->vna_name = stoff;
2618 				/* LINTED */
2619 				vnap->vna_hash = (Word)elf_hash(sdv->sdv_name);
2620 				vnap->vna_flags = 0;
2621 				vnap->vna_other = 0;
2622 				_vnap = vnap;
2623 				vnap++;
2624 				cnt++;
2625 				/* LINTED */
2626 				_vnap->vna_next = (Word)((uintptr_t)vnap -
2627 				    (uintptr_t)_vnap);
2628 			}
2629 		} else {
2630 
2631 			/*
2632 			 * Traverse the version index list recording
2633 			 * each version as a needed dependency.
2634 			 */
2635 			for (cnt = _cnt = 0; _cnt <= ifl->ifl_vercnt;
2636 			    _cnt++) {
2637 				Ver_index	*vip = &ifl->ifl_verndx[_cnt];
2638 
2639 				if (vip->vi_flags & FLG_VER_REFER) {
2640 					(void) st_setstring(dynstr,
2641 					    vip->vi_name, &stoff);
2642 					vnap->vna_name = stoff;
2643 
2644 					if (vip->vi_desc) {
2645 						vnap->vna_hash =
2646 						    vip->vi_desc->vd_hash;
2647 						vnap->vna_flags =
2648 						    vip->vi_desc->vd_flags;
2649 					} else {
2650 						vnap->vna_hash = 0;
2651 						vnap->vna_flags = 0;
2652 					}
2653 					vnap->vna_other = 0;
2654 
2655 					_vnap = vnap;
2656 					vnap++, cnt++;
2657 					_vnap->vna_next =
2658 					    /* LINTED */
2659 					    (Word)((uintptr_t)vnap -
2660 					    (uintptr_t)_vnap);
2661 				}
2662 			}
2663 		}
2664 		_vnap->vna_next = 0;
2665 
2666 		/*
2667 		 * Record the versions auxiliary array offset and
2668 		 * the associated dependency count.
2669 		 */
2670 		/* LINTED */
2671 		vnd->vn_aux = (Word)((uintptr_t)(vnd + 1) - (uintptr_t)vnd);
2672 		/* LINTED */
2673 		vnd->vn_cnt = (Half)cnt;
2674 
2675 		/*
2676 		 * Record the next versions offset and update the version
2677 		 * pointer.  Remember the previous version offset as the very
2678 		 * last structures next pointer should be null.
2679 		 */
2680 		_vnd = vnd;
2681 		vnd = (Verneed *)vnap, num++;
2682 		/* LINTED */
2683 		_vnd->vn_next = (Word)((uintptr_t)vnd - (uintptr_t)_vnd);
2684 	}
2685 	_vnd->vn_next = 0;
2686 
2687 	/*
2688 	 * Record association on string table section and use the
2689 	 * `info' field to indicate the number of entries in this
2690 	 * section.
2691 	 */
2692 	ofl->ofl_osverneed->os_shdr->sh_link =
2693 	    /* LINTED */
2694 	    (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
2695 	ofl->ofl_osverneed->os_shdr->sh_info = num;
2696 
2697 	return (1);
2698 }
2699 
2700 
2701 /*
2702  * Update syminfo section.
2703  */
2704 static uintptr_t
2705 update_osyminfo(Ofl_desc * ofl)
2706 {
2707 	Os_desc *	symosp, * infosp = ofl->ofl_ossyminfo;
2708 	Syminfo *	sip = infosp->os_outdata->d_buf;
2709 	Shdr *		shdr = infosp->os_shdr;
2710 	char		*strtab;
2711 	Listnode *	lnp;
2712 	Sym_desc *	sdp;
2713 	Aliste		idx;
2714 	Sfltr_desc *	sftp;
2715 
2716 	if (ofl->ofl_flags & FLG_OF_RELOBJ) {
2717 		symosp = ofl->ofl_ossymtab;
2718 		strtab = ofl->ofl_osstrtab->os_outdata->d_buf;
2719 	} else {
2720 		symosp = ofl->ofl_osdynsym;
2721 		strtab = ofl->ofl_osdynstr->os_outdata->d_buf;
2722 	}
2723 
2724 	/* LINTED */
2725 	infosp->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
2726 	if (ofl->ofl_osdynamic)
2727 		infosp->os_shdr->sh_info =
2728 		    /* LINTED */
2729 		    (Word)elf_ndxscn(ofl->ofl_osdynamic->os_scn);
2730 
2731 	/*
2732 	 * Update any references with the index into the dynamic table.
2733 	 */
2734 	for (LIST_TRAVERSE(&ofl->ofl_syminfsyms, lnp, sdp)) {
2735 		Ifl_desc *	ifl;
2736 		if (sdp->sd_aux && sdp->sd_aux->sa_bindto)
2737 			ifl = sdp->sd_aux->sa_bindto;
2738 		else
2739 			ifl = sdp->sd_file;
2740 		sip[sdp->sd_symndx].si_boundto = ifl->ifl_neededndx;
2741 	}
2742 
2743 	/*
2744 	 * Update any filtee references with the index into the dynamic table.
2745 	 */
2746 	for (ALIST_TRAVERSE(ofl->ofl_symfltrs, idx, sftp)) {
2747 		Dfltr_desc	*dftp;
2748 
2749 		dftp = alist_item(ofl->ofl_dtsfltrs, sftp->sft_idx);
2750 		sip[sftp->sft_sdp->sd_symndx].si_boundto = dftp->dft_ndx;
2751 	}
2752 
2753 	/*
2754 	 * Display debugging information about section.
2755 	 */
2756 	DBG_CALL(Dbg_syminfo_title(ofl->ofl_lml));
2757 	if (DBG_ENABLED) {
2758 		Word	_cnt, cnt = shdr->sh_size / shdr->sh_entsize;
2759 		Sym *	symtab = symosp->os_outdata->d_buf;
2760 		Dyn *	dyn;
2761 
2762 		if (ofl->ofl_osdynamic)
2763 			dyn = ofl->ofl_osdynamic->os_outdata->d_buf;
2764 		else
2765 			dyn = 0;
2766 
2767 		for (_cnt = 1; _cnt < cnt; _cnt++) {
2768 			if (sip[_cnt].si_flags || sip[_cnt].si_boundto)
2769 				/* LINTED */
2770 				DBG_CALL(Dbg_syminfo_entry(ofl->ofl_lml, _cnt,
2771 				    &sip[_cnt], &symtab[_cnt], strtab, dyn));
2772 		}
2773 	}
2774 	return (1);
2775 }
2776 
2777 /*
2778  * Build the output elf header.
2779  */
2780 static uintptr_t
2781 update_oehdr(Ofl_desc * ofl)
2782 {
2783 	Ehdr	*ehdr = ofl->ofl_nehdr;
2784 
2785 	/*
2786 	 * If an entry point symbol has already been established (refer
2787 	 * sym_validate()) simply update the elf header entry point with the
2788 	 * symbols value.  If no entry point is defined it will have been filled
2789 	 * with the start address of the first section within the text segment
2790 	 * (refer update_outfile()).
2791 	 */
2792 	if (ofl->ofl_entry)
2793 		ehdr->e_entry =
2794 		    ((Sym_desc *)(ofl->ofl_entry))->sd_sym->st_value;
2795 
2796 	/*
2797 	 * Note. it may be necessary to update the `e_flags' field in the
2798 	 * machine dependent section.
2799 	 */
2800 	ehdr->e_ident[EI_DATA] = ld_targ.t_m.m_data;
2801 	ehdr->e_machine = ofl->ofl_dehdr->e_machine;
2802 	ehdr->e_flags = ofl->ofl_dehdr->e_flags;
2803 	ehdr->e_version = ofl->ofl_dehdr->e_version;
2804 
2805 	if (ehdr->e_machine != ld_targ.t_m.m_mach) {
2806 		if (ehdr->e_machine != ld_targ.t_m.m_machplus)
2807 			return (S_ERROR);
2808 		if ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0)
2809 			return (S_ERROR);
2810 	}
2811 
2812 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
2813 		ehdr->e_type = ET_DYN;
2814 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
2815 		ehdr->e_type = ET_REL;
2816 	else
2817 		ehdr->e_type = ET_EXEC;
2818 
2819 	return (1);
2820 }
2821 
2822 /*
2823  * Perform move table expansion.
2824  */
2825 static uintptr_t
2826 expand_move(Ofl_desc *ofl, Sym_desc *sdp, Move *u1)
2827 {
2828 	Move		*mv;
2829 	Os_desc		*osp;
2830 	unsigned char	*taddr, *taddr0;
2831 	Sxword		offset;
2832 	int		i;
2833 	Addr		base1;
2834 	unsigned int	stride;
2835 
2836 	osp = ofl->ofl_issunwdata1->is_osdesc;
2837 	base1 = (Addr)(osp->os_shdr->sh_addr +
2838 	    ofl->ofl_issunwdata1->is_indata->d_off);
2839 	taddr0 = taddr = osp->os_outdata->d_buf;
2840 	mv = u1;
2841 
2842 	offset = sdp->sd_sym->st_value - base1;
2843 	taddr += offset;
2844 	taddr = taddr + mv->m_poffset;
2845 	for (i = 0; i < mv->m_repeat; i++) {
2846 		/* LINTED */
2847 		DBG_CALL(Dbg_move_expand(ofl->ofl_lml, mv,
2848 		    (Addr)(taddr - taddr0)));
2849 		stride = (unsigned int)mv->m_stride + 1;
2850 		/* LINTED */
2851 		switch (ELF_M_SIZE(mv->m_info)) {
2852 		case 1:
2853 			/* LINTED */
2854 			*taddr = (unsigned char)mv->m_value;
2855 			taddr += stride;
2856 			break;
2857 		case 2:
2858 			/* LINTED */
2859 			*((Half *)taddr) = (Half)mv->m_value;
2860 			taddr += 2*stride;
2861 			break;
2862 		case 4:
2863 			/* LINTED */
2864 			*((Word *)taddr) = (Word)mv->m_value;
2865 			taddr += 4*stride;
2866 			break;
2867 		case 8:
2868 			/* LINTED */
2869 			*((unsigned long long *)taddr) = mv->m_value;
2870 			taddr += 8*stride;
2871 			break;
2872 		default:
2873 			/*
2874 			 * Should never come here since this is already
2875 			 * checked at sunwmove_preprocess().
2876 			 */
2877 			return (S_ERROR);
2878 		}
2879 	}
2880 	return (1);
2881 }
2882 
2883 /*
2884  * Update Move sections.
2885  */
2886 static uintptr_t
2887 update_move(Ofl_desc *ofl)
2888 {
2889 	Word		ndx = 0;
2890 	Is_desc *	isp;
2891 	Word		flags = ofl->ofl_flags;
2892 	Move *		mv1, * mv2;
2893 	Listnode *	lnp1;
2894 	Psym_info *	psym;
2895 
2896 	/*
2897 	 * Determine the index of the symbol table that will be referenced by
2898 	 * the relocation entries.
2899 	 */
2900 	if (OFL_ALLOW_DYNSYM(ofl))
2901 		/* LINTED */
2902 		ndx = (Word) elf_ndxscn(ofl->ofl_osdynsym->os_scn);
2903 	else if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ))
2904 		/* LINTED */
2905 		ndx = (Word) elf_ndxscn(ofl->ofl_ossymtab->os_scn);
2906 
2907 	/*
2908 	 * update sh_link and mv pointer for updating move table.
2909 	 */
2910 	if (ofl->ofl_osmove) {
2911 		ofl->ofl_osmove->os_shdr->sh_link = ndx;
2912 		mv1 = (Move *) ofl->ofl_osmove->os_outdata->d_buf;
2913 	}
2914 
2915 	/*
2916 	 * Update symbol entry index
2917 	 */
2918 	for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) {
2919 		Listnode *	lnp2;
2920 		Mv_itm *	mvp;
2921 		Sym_desc 	*sdp;
2922 
2923 		/*
2924 		 * Expand move table
2925 		 */
2926 		if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN) {
2927 			const char	*s;
2928 
2929 			if (ofl->ofl_flags & FLG_OF_STATIC)
2930 				s = MSG_INTL(MSG_PSYM_EXPREASON1);
2931 			else if (ofl->ofl_flags1 & FLG_OF1_NOPARTI)
2932 				s = MSG_INTL(MSG_PSYM_EXPREASON2);
2933 			else
2934 				s = MSG_INTL(MSG_PSYM_EXPREASON3);
2935 			DBG_CALL(Dbg_move_parexpn(ofl->ofl_lml,
2936 			    psym->psym_symd->sd_name, s));
2937 			for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvp)) {
2938 				if ((mvp->mv_flag & FLG_MV_OUTSECT) == 0)
2939 					continue;
2940 				mv2 = mvp->mv_ientry;
2941 				sdp = psym->psym_symd;
2942 				DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0,
2943 				    mv2, sdp));
2944 				(void) expand_move(ofl, sdp, mv2);
2945 			}
2946 			continue;
2947 		}
2948 
2949 		/*
2950 		 * Process move table
2951 		 */
2952 		DBG_CALL(Dbg_move_outmove(ofl->ofl_lml,
2953 		    psym->psym_symd->sd_name));
2954 		for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvp)) {
2955 			int	idx = 1;
2956 			Sym	*sym;
2957 
2958 			if ((mvp->mv_flag & FLG_MV_OUTSECT) == 0)
2959 				continue;
2960 
2961 			isp = mvp->mv_isp;
2962 			mv2 = mvp->mv_ientry;
2963 			sdp = isp->is_file->ifl_oldndx[ELF_M_SYM(mv2->m_info)];
2964 			sym = sdp->sd_sym;
2965 
2966 			DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0, mv2, sdp));
2967 
2968 			*mv1 = *mv2;
2969 			if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) {
2970 				if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
2971 					Half	symbssndx = ofl->ofl_isbss->
2972 					    is_osdesc->os_scnsymndx;
2973 
2974 					mv1->m_info =
2975 					    /* LINTED */
2976 					    ELF_M_INFO(symbssndx, mv2->m_info);
2977 
2978 					if (ELF_ST_TYPE(sym->st_info) !=
2979 					    STT_SECTION) {
2980 						mv1->m_poffset = sym->st_value -
2981 						    ofl->ofl_isbss->is_osdesc->
2982 						    os_shdr->sh_addr +
2983 						    mv2->m_poffset;
2984 					}
2985 				} else {
2986 					mv1->m_info =
2987 					    /* LINTED */
2988 					    ELF_M_INFO(sdp->sd_symndx,
2989 					    mv2->m_info);
2990 				}
2991 			} else {
2992 				Boolean 	isredloc = FALSE;
2993 
2994 				if ((ELF_ST_BIND(sym->st_info) == STB_LOCAL) &&
2995 				    (ofl->ofl_flags1 & FLG_OF1_REDLSYM))
2996 					isredloc = TRUE;
2997 
2998 				if (isredloc && !(sdp->sd_psyminfo)) {
2999 					Word	symndx = sdp->sd_isc->
3000 					    is_osdesc->os_scnsymndx;
3001 
3002 					mv1->m_info =
3003 					    /* LINTED */
3004 					    ELF_M_INFO(symndx, mv2->m_info);
3005 					mv1->m_poffset += sym->st_value;
3006 				} else {
3007 					if (isredloc)
3008 						DBG_CALL(Dbg_syms_reduce(ofl,
3009 						    DBG_SYM_REDUCE_RETAIN, sdp,
3010 						    idx,
3011 						    ofl->ofl_osmove->os_name));
3012 
3013 					mv1->m_info =
3014 					    /* LINTED */
3015 					    ELF_M_INFO(sdp->sd_symndx,
3016 					    mv2->m_info);
3017 				}
3018 			}
3019 			DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 1, mv1, sdp));
3020 			mv1++;
3021 			idx++;
3022 		}
3023 	}
3024 	return (1);
3025 }
3026 
3027 
3028 /*
3029  * Scan through the SHT_GROUP output sections.  Update their
3030  * sh_link/sh_info fields as well as the section contents.
3031  */
3032 static uintptr_t
3033 update_ogroup(Ofl_desc * ofl)
3034 {
3035 	Listnode	*lnp;
3036 	Os_desc		*osp;
3037 	uintptr_t	error = 0;
3038 
3039 	for (LIST_TRAVERSE(&ofl->ofl_osgroups, lnp, osp)) {
3040 		Is_desc		*isp;
3041 		Ifl_desc	*ifl;
3042 		Shdr		*shdr = osp->os_shdr;
3043 		Sym_desc	*sdp;
3044 		Xword		i, grpcnt;
3045 		Word		*gdata;
3046 
3047 		/*
3048 		 * Since input GROUP sections always create unique
3049 		 * output GROUP sections - we know there is only one
3050 		 * item on the list.
3051 		 */
3052 		isp = (Is_desc *)osp->os_isdescs.head->data;
3053 
3054 		ifl = isp->is_file;
3055 		sdp = ifl->ifl_oldndx[isp->is_shdr->sh_info];
3056 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
3057 		shdr->sh_info = sdp->sd_symndx;
3058 
3059 		/*
3060 		 * Scan through the group data section and update
3061 		 * all of the links to new values.
3062 		 */
3063 		grpcnt = shdr->sh_size / shdr->sh_entsize;
3064 		gdata = (Word *)osp->os_outdata->d_buf;
3065 		for (i = 1; i < grpcnt; i++) {
3066 			Is_desc	*	_isp;
3067 			Os_desc	*	_osp;
3068 
3069 			/*
3070 			 * Perform a sanity check that the section index
3071 			 * stored in the SHT_GROUP section is valid
3072 			 * for the file it came from.
3073 			 */
3074 			if (gdata[i] >= ifl->ifl_shnum) {
3075 				eprintf(ofl->ofl_lml, ERR_FATAL,
3076 				    MSG_INTL(MSG_GRP_INVALNDX), isp->is_name,
3077 				    ifl->ifl_name, i, gdata[i]);
3078 				error = S_ERROR;
3079 				gdata[i] = 0;
3080 				continue;
3081 			}
3082 
3083 			_isp = ifl->ifl_isdesc[gdata[i]];
3084 
3085 			/*
3086 			 * If the referenced section didn't make it to the
3087 			 * output file - just zero out the entry.
3088 			 */
3089 			if ((_osp = _isp->is_osdesc) == 0)
3090 				gdata[i] = 0;
3091 			else
3092 				gdata[i] = (Word)elf_ndxscn(_osp->os_scn);
3093 		}
3094 	}
3095 	return (error);
3096 }
3097 
3098 static void
3099 update_ostrtab(Os_desc *osp, Str_tbl *stp, uint_t extra)
3100 {
3101 	Elf_Data	*data;
3102 
3103 	if (osp == 0)
3104 		return;
3105 
3106 	data = osp->os_outdata;
3107 	assert(data->d_size == (st_getstrtab_sz(stp) + extra));
3108 	(void) st_setstrbuf(stp, data->d_buf, data->d_size - extra);
3109 	/* If leaving an extra hole at the end, zero it */
3110 	if (extra > 0)
3111 		(void) memset((char *)data->d_buf + data->d_size - extra,
3112 		    0x0, extra);
3113 }
3114 
3115 /*
3116  * Translate the shdr->sh_{link, info} from its input section value to that
3117  * of the corresponding shdr->sh_{link, info} output section value.
3118  */
3119 static Word
3120 translate_link(Ofl_desc *ofl, Os_desc *osp, Word link, const char *msg)
3121 {
3122 	Is_desc *	isp;
3123 	Ifl_desc *	ifl;
3124 
3125 	/*
3126 	 * Don't translate the special section numbers.
3127 	 */
3128 	if (link >= SHN_LORESERVE)
3129 		return (link);
3130 
3131 	/*
3132 	 * Does this output section translate back to an input file.  If not
3133 	 * then there is no translation to do.  In this case we will assume that
3134 	 * if sh_link has a value, it's the right value.
3135 	 */
3136 	isp = (Is_desc *)osp->os_isdescs.head->data;
3137 	if ((ifl = isp->is_file) == NULL)
3138 		return (link);
3139 
3140 	/*
3141 	 * Sanity check to make sure that the sh_{link, info} value
3142 	 * is within range for the input file.
3143 	 */
3144 	if (link >= ifl->ifl_shnum) {
3145 		eprintf(ofl->ofl_lml, ERR_WARNING, msg, ifl->ifl_name,
3146 		    isp->is_name, EC_XWORD(link));
3147 		return (link);
3148 	}
3149 
3150 	/*
3151 	 * Follow the link to the input section.
3152 	 */
3153 	if ((isp = ifl->ifl_isdesc[link]) == 0)
3154 		return (0);
3155 	if ((osp = isp->is_osdesc) == 0)
3156 		return (0);
3157 
3158 	/* LINTED */
3159 	return ((Word)elf_ndxscn(osp->os_scn));
3160 }
3161 
3162 /*
3163  * Having created all of the necessary sections, segments, and associated
3164  * headers, fill in the program headers and update any other data in the
3165  * output image.  Some general rules:
3166  *
3167  *  o	If an interpreter is required always generate a PT_PHDR entry as
3168  *	well.  It is this entry that triggers the kernel into passing the
3169  *	interpreter an aux vector instead of just a file descriptor.
3170  *
3171  *  o	When generating an image that will be interpreted (ie. a dynamic
3172  *	executable, a shared object, or a static executable that has been
3173  *	provided with an interpreter - weird, but possible), make the initial
3174  *	loadable segment include both the ehdr and phdr[].  Both of these
3175  *	tables are used by the interpreter therefore it seems more intuitive
3176  *	to explicitly defined them as part of the mapped image rather than
3177  *	relying on page rounding by the interpreter to allow their access.
3178  *
3179  *  o	When generating a static image that does not require an interpreter
3180  *	have the first loadable segment indicate the address of the first
3181  *	.section as the start address (things like /kernel/unix and ufsboot
3182  *	expect this behavior).
3183  */
3184 uintptr_t
3185 ld_update_outfile(Ofl_desc *ofl)
3186 {
3187 	Addr		size, etext, vaddr = ofl->ofl_segorigin;
3188 	Listnode	*lnp1, *lnp2;
3189 	Sg_desc		*sgp, *dtracesgp = 0, *capsgp = 0;
3190 	Os_desc		*osp;
3191 	int		phdrndx = 0, segndx = -1, secndx;
3192 	int		dtracepndx, dtracesndx, cappndx, capsndx;
3193 	Ehdr		*ehdr = ofl->ofl_nehdr;
3194 	Shdr		*hshdr;
3195 	Phdr		*_phdr = 0;
3196 	Word		phdrsz = (ehdr->e_phnum * ehdr->e_phentsize), shscnndx;
3197 	Word		flags = ofl->ofl_flags, ehdrsz = ehdr->e_ehsize;
3198 	Boolean		nobits;
3199 	Off		offset;
3200 	Aliste		idx;
3201 
3202 	/*
3203 	 * Loop through the segment descriptors and pick out what we need.
3204 	 */
3205 	DBG_CALL(Dbg_seg_title(ofl->ofl_lml));
3206 	for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
3207 		Phdr	*phdr = &(sgp->sg_phdr);
3208 		Xword 	p_align;
3209 
3210 		segndx++;
3211 
3212 		/*
3213 		 * If an interpreter is required generate a PT_INTERP and
3214 		 * PT_PHDR program header entry.  The PT_PHDR entry describes
3215 		 * the program header table itself.  This information will be
3216 		 * passed via the aux vector to the interpreter (ld.so.1).
3217 		 * The program header array is actually part of the first
3218 		 * loadable segment (and the PT_PHDR entry is the first entry),
3219 		 * therefore its virtual address isn't known until the first
3220 		 * loadable segment is processed.
3221 		 */
3222 		if (phdr->p_type == PT_PHDR) {
3223 			if (ofl->ofl_osinterp) {
3224 				phdr->p_offset = ehdr->e_phoff;
3225 				phdr->p_filesz = phdr->p_memsz = phdrsz;
3226 
3227 				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3228 				ofl->ofl_phdr[phdrndx++] = *phdr;
3229 			}
3230 			continue;
3231 		}
3232 		if (phdr->p_type == PT_INTERP) {
3233 			if (ofl->ofl_osinterp) {
3234 				Shdr	*shdr = ofl->ofl_osinterp->os_shdr;
3235 
3236 				phdr->p_vaddr = phdr->p_memsz = 0;
3237 				phdr->p_offset = shdr->sh_offset;
3238 				phdr->p_filesz = shdr->sh_size;
3239 
3240 				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3241 				ofl->ofl_phdr[phdrndx++] = *phdr;
3242 			}
3243 			continue;
3244 		}
3245 
3246 		/*
3247 		 * If we are creating a PT_SUNWDTRACE segment, remember where
3248 		 * the program header is.  The header values are assigned after
3249 		 * update_osym() has completed and the symbol table addresses
3250 		 * have been udpated.
3251 		 */
3252 		if (phdr->p_type == PT_SUNWDTRACE) {
3253 			if ((ofl->ofl_dtracesym) &&
3254 			    ((flags & FLG_OF_RELOBJ) == 0)) {
3255 				dtracesgp = sgp;
3256 				dtracesndx = segndx;
3257 				dtracepndx = phdrndx++;
3258 			}
3259 			continue;
3260 		}
3261 
3262 		/*
3263 		 * If a hardware/software capabilities section is required,
3264 		 * generate the PT_SUNWCAP header.  Note, as this comes before
3265 		 * the first loadable segment, we don't yet know its real
3266 		 * virtual address.  This is updated later.
3267 		 */
3268 		if (phdr->p_type == PT_SUNWCAP) {
3269 			if (ofl->ofl_oscap) {
3270 				capsgp = sgp;
3271 				capsndx = segndx;
3272 				cappndx = phdrndx++;
3273 			}
3274 			continue;
3275 		}
3276 
3277 		/*
3278 		 * As the dynamic program header occurs after the loadable
3279 		 * headers in the segment descriptor table, all the address
3280 		 * information for the .dynamic output section will have been
3281 		 * figured out by now.
3282 		 */
3283 		if (phdr->p_type == PT_DYNAMIC) {
3284 			if (OFL_ALLOW_DYNSYM(ofl)) {
3285 				Shdr	*shdr = ofl->ofl_osdynamic->os_shdr;
3286 
3287 				phdr->p_vaddr = shdr->sh_addr;
3288 				phdr->p_offset = shdr->sh_offset;
3289 				phdr->p_filesz = shdr->sh_size;
3290 				phdr->p_flags = ld_targ.t_m.m_dataseg_perm;
3291 
3292 				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3293 				ofl->ofl_phdr[phdrndx++] = *phdr;
3294 			}
3295 			continue;
3296 		}
3297 
3298 		/*
3299 		 * As the AMD unwind program header occurs after the loadable
3300 		 * headers in the segment descriptor table, all the address
3301 		 * information for the .eh_frame output section will have been
3302 		 * figured out by now.
3303 		 */
3304 #if	defined(_ELF64)
3305 		if ((ld_targ.t_m.m_mach == EM_AMD64) &&
3306 		    (phdr->p_type == PT_SUNW_UNWIND)) {
3307 			Shdr	    *shdr;
3308 
3309 			if (ofl->ofl_unwindhdr == 0)
3310 				continue;
3311 
3312 			shdr = ofl->ofl_unwindhdr->os_shdr;
3313 
3314 			phdr->p_flags = PF_R;
3315 			phdr->p_vaddr = shdr->sh_addr;
3316 			phdr->p_memsz = shdr->sh_size;
3317 			phdr->p_filesz = shdr->sh_size;
3318 			phdr->p_offset = shdr->sh_offset;
3319 			phdr->p_align = shdr->sh_addralign;
3320 			phdr->p_paddr = 0;
3321 			ofl->ofl_phdr[phdrndx++] = *phdr;
3322 			continue;
3323 		}
3324 #endif
3325 		/*
3326 		 * As the TLS program header occurs after the loadable
3327 		 * headers in the segment descriptor table, all the address
3328 		 * information for the .tls output section will have been
3329 		 * figured out by now.
3330 		 */
3331 		if (phdr->p_type == PT_TLS) {
3332 			Os_desc	*tlsosp;
3333 			Shdr	*firstshdr = 0, *lastfileshdr = 0, *lastshdr;
3334 
3335 			if (ofl->ofl_ostlsseg.head == NULL)
3336 				continue;
3337 
3338 			/*
3339 			 * Scan through the sections that have contributed TLS.
3340 			 * Remember the first and last so as to determine the
3341 			 * TLS memory size requirement.  Remember the last
3342 			 * non-nobits section to determine the TLS data
3343 			 * contribution, which determines the TLS file size.
3344 			 */
3345 			for (LIST_TRAVERSE(&ofl->ofl_ostlsseg, lnp2, tlsosp)) {
3346 				Shdr	*tlsshdr = tlsosp->os_shdr;
3347 
3348 				if (firstshdr == 0)
3349 					firstshdr = tlsshdr;
3350 				if (tlsshdr->sh_type != SHT_NOBITS)
3351 					lastfileshdr = tlsshdr;
3352 				lastshdr = tlsshdr;
3353 			}
3354 
3355 			phdr->p_flags = PF_R | PF_W;
3356 			phdr->p_vaddr = firstshdr->sh_addr;
3357 			phdr->p_offset = firstshdr->sh_offset;
3358 			phdr->p_align = firstshdr->sh_addralign;
3359 
3360 			if (lastfileshdr)
3361 				phdr->p_filesz = lastfileshdr->sh_offset +
3362 				    lastfileshdr->sh_size - phdr->p_offset;
3363 			else
3364 				phdr->p_filesz = 0;
3365 
3366 			phdr->p_memsz = lastshdr->sh_offset +
3367 			    lastshdr->sh_size - phdr->p_offset;
3368 
3369 			DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3370 			ofl->ofl_phdr[phdrndx] = *phdr;
3371 			ofl->ofl_tlsphdr = &ofl->ofl_phdr[phdrndx++];
3372 			continue;
3373 		}
3374 
3375 		/*
3376 		 * If this is an empty segment declaration, it will occur after
3377 		 * all other loadable segments.  As empty segments can be
3378 		 * defind with fixed addresses, make sure that no loadable
3379 		 * segments overlap.  This might occur as the object evolves
3380 		 * and the loadable segments grow, thus encroaching upon an
3381 		 * existing segment reservation.
3382 		 *
3383 		 * Segments are only created for dynamic objects, thus this
3384 		 * checking can be skipped when building a relocatable object.
3385 		 */
3386 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
3387 		    (sgp->sg_flags & FLG_SG_EMPTY)) {
3388 			int	i;
3389 			Addr	v_e;
3390 
3391 			vaddr = phdr->p_vaddr;
3392 			phdr->p_memsz = sgp->sg_length;
3393 			DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3394 			ofl->ofl_phdr[phdrndx++] = *phdr;
3395 
3396 			if (phdr->p_type != PT_LOAD)
3397 				continue;
3398 
3399 			v_e = vaddr + phdr->p_memsz;
3400 
3401 			/*
3402 			 * Check overlaps
3403 			 */
3404 			for (i = 0; i < phdrndx - 1; i++) {
3405 				Addr 	p_s = (ofl->ofl_phdr[i]).p_vaddr;
3406 				Addr 	p_e;
3407 
3408 				if ((ofl->ofl_phdr[i]).p_type != PT_LOAD)
3409 					continue;
3410 
3411 				p_e = p_s + (ofl->ofl_phdr[i]).p_memsz;
3412 				if (((p_s <= vaddr) && (p_e > vaddr)) ||
3413 				    ((vaddr <= p_s) && (v_e > p_s)))
3414 					eprintf(ofl->ofl_lml, ERR_WARNING,
3415 					    MSG_INTL(MSG_UPD_SEGOVERLAP),
3416 					    ofl->ofl_name, EC_ADDR(p_e),
3417 					    sgp->sg_name, EC_ADDR(vaddr));
3418 			}
3419 			continue;
3420 		}
3421 
3422 		/*
3423 		 * Having processed any of the special program headers any
3424 		 * remaining headers will be built to express individual
3425 		 * segments.  Segments are only built if they have output
3426 		 * section descriptors associated with them (ie. some form of
3427 		 * input section has been matched to this segment).
3428 		 */
3429 		if (sgp->sg_osdescs == NULL)
3430 			continue;
3431 
3432 		/*
3433 		 * Determine the segments offset and size from the section
3434 		 * information provided from elf_update().
3435 		 * Allow for multiple NOBITS sections.
3436 		 */
3437 		osp = sgp->sg_osdescs->apl_data[0];
3438 		hshdr = osp->os_shdr;
3439 
3440 		phdr->p_filesz = 0;
3441 		phdr->p_memsz = 0;
3442 		phdr->p_offset = offset = hshdr->sh_offset;
3443 
3444 		nobits = ((hshdr->sh_type == SHT_NOBITS) &&
3445 		    ((sgp->sg_flags & FLG_SG_PHREQ) == 0));
3446 
3447 		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx, osp)) {
3448 			Shdr	*shdr = osp->os_shdr;
3449 
3450 			p_align = 0;
3451 			if (shdr->sh_addralign > p_align)
3452 				p_align = shdr->sh_addralign;
3453 
3454 			offset = (Off)S_ROUND(offset, shdr->sh_addralign);
3455 			offset += shdr->sh_size;
3456 
3457 			if (shdr->sh_type != SHT_NOBITS) {
3458 				if (nobits) {
3459 					eprintf(ofl->ofl_lml, ERR_FATAL,
3460 					    MSG_INTL(MSG_UPD_NOBITS));
3461 					return (S_ERROR);
3462 				}
3463 				phdr->p_filesz = offset - phdr->p_offset;
3464 			} else if ((sgp->sg_flags & FLG_SG_PHREQ) == 0)
3465 				nobits = TRUE;
3466 		}
3467 		phdr->p_memsz = offset - hshdr->sh_offset;
3468 
3469 		/*
3470 		 * If this is PT_SUNWBSS, set alignment
3471 		 */
3472 		if (phdr->p_type == PT_SUNWBSS)
3473 			phdr->p_align = p_align;
3474 
3475 		/*
3476 		 * If this is the first loadable segment of a dynamic object,
3477 		 * or an interpreter has been specified (a static object built
3478 		 * with an interpreter will still be given a PT_HDR entry), then
3479 		 * compensate for the elf header and program header array.  Both
3480 		 * of these are actually part of the loadable segment as they
3481 		 * may be inspected by the interpreter.  Adjust the segments
3482 		 * size and offset accordingly.
3483 		 */
3484 		if ((_phdr == 0) && (phdr->p_type == PT_LOAD) &&
3485 		    ((ofl->ofl_osinterp) || (flags & FLG_OF_DYNAMIC)) &&
3486 		    (!(ofl->ofl_dtflags_1 & DF_1_NOHDR))) {
3487 			size = (Addr)S_ROUND((phdrsz + ehdrsz),
3488 			    hshdr->sh_addralign);
3489 			phdr->p_offset -= size;
3490 			phdr->p_filesz += size;
3491 			phdr->p_memsz += size;
3492 		}
3493 
3494 		/*
3495 		 * If a segment size symbol is required (specified via a
3496 		 * mapfile) update its value.
3497 		 */
3498 		if (sgp->sg_sizesym != NULL)
3499 			sgp->sg_sizesym->sd_sym->st_value = phdr->p_memsz;
3500 
3501 		/*
3502 		 * If no file content has been assigned to this segment (it
3503 		 * only contains no-bits sections), then reset the offset for
3504 		 * consistency.
3505 		 */
3506 		if (phdr->p_filesz == 0)
3507 			phdr->p_offset = 0;
3508 
3509 		/*
3510 		 * If a virtual address has been specified for this segment
3511 		 * (presumably from a map file) use it and make sure the
3512 		 * previous segment does not run into this segment.
3513 		 */
3514 		if ((phdr->p_type == PT_LOAD) ||
3515 		    (phdr->p_type == PT_SUNWBSS)) {
3516 			if ((sgp->sg_flags & FLG_SG_VADDR)) {
3517 				if (_phdr && (vaddr > phdr->p_vaddr) &&
3518 				    (phdr->p_type == PT_LOAD))
3519 					eprintf(ofl->ofl_lml, ERR_WARNING,
3520 					    MSG_INTL(MSG_UPD_SEGOVERLAP),
3521 					    ofl->ofl_name, EC_ADDR(vaddr),
3522 					    sgp->sg_name,
3523 					    EC_ADDR(phdr->p_vaddr));
3524 				vaddr = phdr->p_vaddr;
3525 				phdr->p_align = 0;
3526 			} else {
3527 				vaddr = phdr->p_vaddr =
3528 				    (Addr)S_ROUND(vaddr, phdr->p_align);
3529 			}
3530 		}
3531 
3532 		/*
3533 		 * Adjust the address offset and p_align if needed.
3534 		 */
3535 		if (((sgp->sg_flags & FLG_SG_VADDR) == 0) &&
3536 		    ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0)) {
3537 			if (phdr->p_align != 0)
3538 				vaddr += phdr->p_offset % phdr->p_align;
3539 			else
3540 				vaddr += phdr->p_offset;
3541 			phdr->p_vaddr = vaddr;
3542 		}
3543 
3544 		/*
3545 		 * If an interpreter is required set the virtual address of the
3546 		 * PT_PHDR program header now that we know the virtual address
3547 		 * of the loadable segment that contains it.  Update the
3548 		 * PT_SUNWCAP header similarly.
3549 		 */
3550 		if ((_phdr == 0) && (phdr->p_type == PT_LOAD)) {
3551 			_phdr = phdr;
3552 
3553 			if ((ofl->ofl_dtflags_1 & DF_1_NOHDR) == 0) {
3554 				if (ofl->ofl_osinterp)
3555 					ofl->ofl_phdr[0].p_vaddr =
3556 					    vaddr + ehdrsz;
3557 
3558 				/*
3559 				 * Finally, if we're creating a dynamic object
3560 				 * (or a static object in which an interpreter
3561 				 * is specified) update the vaddr to reflect
3562 				 * the address of the first section within this
3563 				 * segment.
3564 				 */
3565 				if ((ofl->ofl_osinterp) ||
3566 				    (flags & FLG_OF_DYNAMIC))
3567 					vaddr += size;
3568 			} else {
3569 				/*
3570 				 * If the DF_1_NOHDR flag was set, and an
3571 				 * interpreter is being generated, the PT_PHDR
3572 				 * will not be part of any loadable segment.
3573 				 */
3574 				if (ofl->ofl_osinterp) {
3575 					ofl->ofl_phdr[0].p_vaddr = 0;
3576 					ofl->ofl_phdr[0].p_memsz = 0;
3577 					ofl->ofl_phdr[0].p_flags = 0;
3578 				}
3579 			}
3580 		}
3581 
3582 		/*
3583 		 * Ensure the ELF entry point defaults to zero.  Typically, this
3584 		 * value is overridden in update_oehdr() to one of the standard
3585 		 * entry points.  Historically, this default was set to the
3586 		 * address of first executable section, but this has since been
3587 		 * found to be more confusing than it is helpful.
3588 		 */
3589 		ehdr->e_entry = 0;
3590 
3591 		DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3592 
3593 		/*
3594 		 * Traverse the output section descriptors for this segment so
3595 		 * that we can update the section headers addresses.  We've
3596 		 * calculated the virtual address of the initial section within
3597 		 * this segment, so each successive section can be calculated
3598 		 * based on their offsets from each other.
3599 		 */
3600 		secndx = 0;
3601 		hshdr = 0;
3602 		for (APLIST_TRAVERSE(sgp->sg_osdescs, idx, osp)) {
3603 			Shdr	*shdr = osp->os_shdr;
3604 
3605 			if (shdr->sh_link)
3606 				shdr->sh_link = translate_link(ofl, osp,
3607 				    shdr->sh_link, MSG_INTL(MSG_FIL_INVSHLINK));
3608 
3609 			if (shdr->sh_info && (shdr->sh_flags & SHF_INFO_LINK))
3610 				shdr->sh_info = translate_link(ofl, osp,
3611 				    shdr->sh_info, MSG_INTL(MSG_FIL_INVSHINFO));
3612 
3613 			if (!(flags & FLG_OF_RELOBJ) &&
3614 			    (phdr->p_type == PT_LOAD) ||
3615 			    (phdr->p_type == PT_SUNWBSS)) {
3616 				if (hshdr)
3617 					vaddr += (shdr->sh_offset -
3618 					    hshdr->sh_offset);
3619 
3620 				shdr->sh_addr = vaddr;
3621 				hshdr = shdr;
3622 			}
3623 
3624 			DBG_CALL(Dbg_seg_os(ofl, osp, secndx));
3625 			secndx++;
3626 		}
3627 
3628 		/*
3629 		 * Establish the virtual address of the end of the last section
3630 		 * in this segment so that the next segments offset can be
3631 		 * calculated from this.
3632 		 */
3633 		if (hshdr)
3634 			vaddr += hshdr->sh_size;
3635 
3636 		/*
3637 		 * Output sections for this segment complete.  Adjust the
3638 		 * virtual offset for the last sections size, and make sure we
3639 		 * haven't exceeded any maximum segment length specification.
3640 		 */
3641 		if ((sgp->sg_length != 0) && (sgp->sg_length < phdr->p_memsz)) {
3642 			eprintf(ofl->ofl_lml, ERR_FATAL,
3643 			    MSG_INTL(MSG_UPD_LARGSIZE), ofl->ofl_name,
3644 			    sgp->sg_name, EC_XWORD(phdr->p_memsz),
3645 			    EC_XWORD(sgp->sg_length));
3646 			return (S_ERROR);
3647 		}
3648 
3649 		if (phdr->p_type == PT_NOTE) {
3650 			phdr->p_vaddr = 0;
3651 			phdr->p_paddr = 0;
3652 			phdr->p_align = 0;
3653 			phdr->p_memsz = 0;
3654 		}
3655 
3656 		if ((phdr->p_type != PT_NULL) && !(flags & FLG_OF_RELOBJ))
3657 			ofl->ofl_phdr[phdrndx++] = *phdr;
3658 	}
3659 
3660 	/*
3661 	 * Update any new output sections.  When building the initial output
3662 	 * image, a number of sections were created but left uninitialized (eg.
3663 	 * .dynsym, .dynstr, .symtab, .symtab, etc.).  Here we update these
3664 	 * sections with the appropriate data.  Other sections may still be
3665 	 * modified via reloc_process().
3666 	 *
3667 	 * Copy the interpreter name into the .interp section.
3668 	 */
3669 	if (ofl->ofl_interp)
3670 		(void) strcpy((char *)ofl->ofl_osinterp->os_outdata->d_buf,
3671 		    ofl->ofl_interp);
3672 
3673 	/*
3674 	 * Update the .shstrtab, .strtab and .dynstr sections.
3675 	 */
3676 	update_ostrtab(ofl->ofl_osshstrtab, ofl->ofl_shdrsttab, 0);
3677 	update_ostrtab(ofl->ofl_osstrtab, ofl->ofl_strtab, 0);
3678 	update_ostrtab(ofl->ofl_osdynstr, ofl->ofl_dynstrtab, DYNSTR_EXTRA_PAD);
3679 
3680 	/*
3681 	 * Build any output symbol tables, the symbols information is copied
3682 	 * and updated into the new output image.
3683 	 */
3684 	if ((etext = update_osym(ofl)) == (Addr)S_ERROR)
3685 		return (S_ERROR);
3686 
3687 	/*
3688 	 * If we have a PT_SUNWDTRACE phdr, update it now with the address of
3689 	 * the symbol.  It's only now been updated via update_sym().
3690 	 */
3691 	if (dtracesgp && ofl->ofl_dtracesym) {
3692 		Phdr		*aphdr, *phdr = &(dtracesgp->sg_phdr);
3693 		Sym_desc	*sdp = ofl->ofl_dtracesym;
3694 
3695 		phdr->p_vaddr = sdp->sd_sym->st_value;
3696 		phdr->p_memsz = sdp->sd_sym->st_size;
3697 
3698 		/*
3699 		 * Take permisions of the segment the symbol is associated with.
3700 		 */
3701 		aphdr = &sdp->sd_isc->is_osdesc->os_sgdesc->sg_phdr;
3702 		assert(aphdr);
3703 		phdr->p_flags = aphdr->p_flags;
3704 
3705 		DBG_CALL(Dbg_seg_entry(ofl, dtracesndx, dtracesgp));
3706 		ofl->ofl_phdr[dtracepndx] = *phdr;
3707 	}
3708 
3709 	/*
3710 	 * If we have a PT_SUNWCAP phdr, update it now from the associated
3711 	 * section information.
3712 	 */
3713 	if (capsgp && ofl->ofl_oscap) {
3714 		Phdr	*phdr = &(capsgp->sg_phdr);
3715 		Shdr	*shdr = ofl->ofl_oscap->os_shdr;
3716 
3717 		phdr->p_vaddr = shdr->sh_addr;
3718 		phdr->p_offset = shdr->sh_offset;
3719 		phdr->p_filesz = shdr->sh_size;
3720 		phdr->p_flags = PF_R;
3721 
3722 		DBG_CALL(Dbg_seg_entry(ofl, capsndx, capsgp));
3723 		ofl->ofl_phdr[cappndx] = *phdr;
3724 	}
3725 
3726 	/*
3727 	 * Update the GROUP sections.
3728 	 */
3729 	if (update_ogroup(ofl) == S_ERROR)
3730 		return (S_ERROR);
3731 
3732 	/*
3733 	 * Update Move Table.
3734 	 */
3735 	if (ofl->ofl_osmove || ofl->ofl_issunwdata1) {
3736 		if (update_move(ofl) == S_ERROR)
3737 			return (S_ERROR);
3738 	}
3739 
3740 	/*
3741 	 * Build any output headers, version information, dynamic structure and
3742 	 * syminfo structure.
3743 	 */
3744 	if (update_oehdr(ofl) == S_ERROR)
3745 		return (S_ERROR);
3746 	if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == FLG_OF_VERDEF)
3747 		if (update_overdef(ofl) == S_ERROR)
3748 			return (S_ERROR);
3749 	if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
3750 		if (update_overneed(ofl) == S_ERROR)
3751 			return (S_ERROR);
3752 	if (flags & FLG_OF_DYNAMIC) {
3753 		if (update_odynamic(ofl) == S_ERROR)
3754 			return (S_ERROR);
3755 		if (ofl->ofl_ossyminfo)
3756 			if (update_osyminfo(ofl) == S_ERROR)
3757 				return (S_ERROR);
3758 	}
3759 
3760 	/*
3761 	 * Emit Strtab diagnostics.
3762 	 */
3763 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osshstrtab,
3764 	    ofl->ofl_shdrsttab));
3765 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osstrtab,
3766 	    ofl->ofl_strtab));
3767 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osdynstr,
3768 	    ofl->ofl_dynstrtab));
3769 
3770 	/*
3771 	 * Initialize the section headers string table index within the elf
3772 	 * header.
3773 	 */
3774 	/* LINTED */
3775 	if ((shscnndx = elf_ndxscn(ofl->ofl_osshstrtab->os_scn)) <
3776 	    SHN_LORESERVE) {
3777 		ofl->ofl_nehdr->e_shstrndx =
3778 		    /* LINTED */
3779 		    (Half)shscnndx;
3780 	} else {
3781 		/*
3782 		 * If the STRTAB section index doesn't fit into
3783 		 * e_shstrndx, then we store it in 'shdr[0].st_link'.
3784 		 */
3785 		Elf_Scn	*scn;
3786 		Shdr	*shdr0;
3787 
3788 		if ((scn = elf_getscn(ofl->ofl_elf, 0)) == NULL) {
3789 			eprintf(ofl->ofl_lml, ERR_ELF,
3790 			    MSG_INTL(MSG_ELF_GETSCN), ofl->ofl_name);
3791 			return (S_ERROR);
3792 		}
3793 		if ((shdr0 = elf_getshdr(scn)) == NULL) {
3794 			eprintf(ofl->ofl_lml, ERR_ELF,
3795 			    MSG_INTL(MSG_ELF_GETSHDR), ofl->ofl_name);
3796 			return (S_ERROR);
3797 		}
3798 		ofl->ofl_nehdr->e_shstrndx = SHN_XINDEX;
3799 		shdr0->sh_link = shscnndx;
3800 	}
3801 
3802 	return ((uintptr_t)etext);
3803 }
3804