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