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