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