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