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