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