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