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