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