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