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