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