xref: /titanic_44/usr/src/cmd/sgs/libld/common/update.c (revision 788f581b902c6a79b94b4ec692657b8583545a92)
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 2005 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 the TLS segment.
784 				 */
785 				symptr->st_value -= ofl->ofl_tlsphdr->p_vaddr;
786 			}
787 
788 			if (restore != 0) {
789 				unsigned char type, bind;
790 				/*
791 				 * Make sure this COMMON
792 				 * symbol is returned to the
793 				 * same binding as was defined
794 				 * in the original relocatable
795 				 * object reference.
796 				 */
797 				type = ELF_ST_TYPE(symptr->st_info);
798 				if (sdp->sd_flags & FLG_SY_GLOBREF)
799 					bind = STB_GLOBAL;
800 				else
801 					bind = STB_WEAK;
802 
803 				symptr->st_info = ELF_ST_INFO(bind, type);
804 			}
805 		}
806 	}
807 
808 	if (ofl->ofl_hashbkts) {
809 		qsort(sorted_syms + ofl->ofl_scopecnt + ofl->ofl_elimcnt,
810 		    ofl->ofl_globcnt, sizeof (Sym_s_list),
811 		    (int (*)(const void *, const void *))sym_hash_compare);
812 	}
813 
814 	for (ssndx = 0; ssndx < (ofl->ofl_elimcnt + ofl->ofl_scopecnt +
815 	    ofl->ofl_globcnt); ssndx++) {
816 		const char	*name;
817 		Sym		*sym;
818 		Sym_aux		*sap;
819 		Half		spec;
820 		int		local = 0, enter_in_symtab;
821 		Listnode	*lnp2;
822 		Gotndx		*gnp;
823 		Word		sectndx;
824 
825 		sdp = sorted_syms[ssndx].sl_sdp;
826 		sectndx = 0;
827 
828 		if (symtab)
829 			enter_in_symtab = 1;
830 		else
831 			enter_in_symtab = 0;
832 
833 		/*
834 		 * Assign a got offset if necessary.
835 		 */
836 #if defined(sparc) || defined(__sparcv9)
837 		if (assign_got(sdp) == S_ERROR)
838 			return ((Addr)S_ERROR);
839 #elif defined(i386) || defined(__amd64)
840 /* nothing to do */
841 #else
842 #error Unknown architecture!
843 #endif
844 		if (dbg_mask) {
845 			for (LIST_TRAVERSE(&sdp->sd_GOTndxs, lnp2, gnp)) {
846 				_gottable->gt_sym = sdp;
847 				_gottable->gt_gndx.gn_gotndx = gnp->gn_gotndx;
848 				_gottable->gt_gndx.gn_addend = gnp->gn_addend;
849 				_gottable++;
850 			}
851 
852 			if (sdp->sd_aux && sdp->sd_aux->sa_PLTGOTndx) {
853 				_gottable->gt_sym = sdp;
854 				_gottable->gt_gndx.gn_gotndx =
855 				    sdp->sd_aux->sa_PLTGOTndx;
856 				_gottable++;
857 			}
858 		}
859 
860 
861 		/*
862 		 * If this symbol has been marked as being reduced to local
863 		 * scope then it will have to be placed in the scoped portion
864 		 * of the .symtab.  Retain the appropriate index for use in
865 		 * version symbol indexing and relocation.
866 		 */
867 		if ((sdp->sd_flags1 & FLG_SY1_LOCL) &&
868 		    (flags & FLG_OF_PROCRED)) {
869 			local = 1;
870 			if (!(sdp->sd_flags1 & FLG_SY1_ELIM) && !dynsym)
871 				sdp->sd_symndx = scopesym_ndx;
872 			else
873 				sdp->sd_symndx = 0;
874 
875 			if (sdp->sd_flags1 & FLG_SY1_ELIM)
876 				enter_in_symtab = 0;
877 		} else
878 			sdp->sd_symndx = *symndx;
879 
880 		/*
881 		 * Copy basic symbol and string information.
882 		 */
883 		name = sdp->sd_name;
884 		sap = sdp->sd_aux;
885 
886 		/*
887 		 * If we require to record version symbol indexes, update the
888 		 * associated version symbol information for all defined
889 		 * symbols.  If a version definition is required any zero value
890 		 * symbol indexes would have been flagged as undefined symbol
891 		 * errors, however if we're just scoping these need to fall into
892 		 * the base of global symbols.
893 		 */
894 		if (sdp->sd_symndx && versym) {
895 			Half	vndx = 0;
896 
897 			if (sdp->sd_flags & FLG_SY_MVTOCOMM)
898 				vndx = VER_NDX_GLOBAL;
899 			else if (sdp->sd_ref == REF_REL_NEED) {
900 				Half	symflags1 = sdp->sd_flags1;
901 
902 				vndx = sap->sa_overndx;
903 				if ((vndx == 0) &&
904 				    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
905 					if (symflags1 & FLG_SY1_ELIM)
906 						vndx = VER_NDX_ELIMINATE;
907 					else if (symflags1 & FLG_SY1_LOCL)
908 						vndx = VER_NDX_LOCAL;
909 					else
910 						vndx = VER_NDX_GLOBAL;
911 				}
912 			}
913 			versym[sdp->sd_symndx] = vndx;
914 		}
915 
916 		/*
917 		 * If we are creating the .syminfo section then set per symbol
918 		 * flags here.
919 		 */
920 		if (sdp->sd_symndx && syminfo &&
921 		    !(sdp->sd_flags & FLG_SY_NOTAVAIL)) {
922 			int	ndx = sdp->sd_symndx;
923 			List	*sip = &(ofl->ofl_syminfsyms);
924 
925 			if (sdp->sd_flags & FLG_SY_MVTOCOMM)
926 				/*
927 				 * Identify a copy relocation symbol.
928 				 */
929 				syminfo[ndx].si_flags |= SYMINFO_FLG_COPY;
930 
931 			if (sdp->sd_ref == REF_DYN_NEED) {
932 				/*
933 				 * A reference is bound to a needed dependency.
934 				 * Save this symbol descriptor, as its boundto
935 				 * element will need updating after the .dynamic
936 				 * section has been created.  Flag whether this
937 				 * reference is lazy loadable, and if a direct
938 				 * binding is to be established.
939 				 */
940 				if (list_appendc(sip, sdp) == 0)
941 					return (0);
942 
943 				syminfo[ndx].si_flags |= SYMINFO_FLG_DIRECT;
944 				if (sdp->sd_flags & FLG_SY_LAZYLD)
945 					syminfo[ndx].si_flags |=
946 					    SYMINFO_FLG_LAZYLOAD;
947 
948 				/*
949 				 * Enable direct symbol bindings if:
950 				 *
951 				 *  .	Symbol was identified with the DIRECT
952 				 *	keyword in a mapfile.
953 				 *
954 				 *  .	Symbol reference has been bound to a
955 				 * 	dependency which was specified as
956 				 *	requiring direct bindings with -zdirect.
957 				 *
958 				 *  .	All symbol references are required to
959 				 *	use direct bindings via -Bdirect.
960 				 */
961 				if (sdp->sd_flags1 & FLG_SY1_DIR)
962 					syminfo[ndx].si_flags |=
963 					    SYMINFO_FLG_DIRECTBIND;
964 
965 			} else if ((sdp->sd_flags & FLG_SY_EXTERN) &&
966 			    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
967 				/*
968 				 * If this symbol has been explicitly defined
969 				 * as external, and remains unresolved, mark
970 				 * it as external.
971 				 */
972 				syminfo[ndx].si_boundto = SYMINFO_BT_EXTERN;
973 
974 			} else if (sdp->sd_flags & FLG_SY_PARENT) {
975 				/*
976 				 * A reference to a parent object.  Indicate
977 				 * whether a direct binding should be
978 				 * established.
979 				 */
980 				syminfo[ndx].si_flags |= SYMINFO_FLG_DIRECT;
981 				syminfo[ndx].si_boundto = SYMINFO_BT_PARENT;
982 				if (sdp->sd_flags1 & FLG_SY1_DIR)
983 					syminfo[ndx].si_flags |=
984 					    SYMINFO_FLG_DIRECTBIND;
985 
986 			} else if (sdp->sd_flags & FLG_SY_STDFLTR) {
987 				/*
988 				 * A filter definition.  Although this symbol
989 				 * can only be a stub, it might be necessary to
990 				 * prevent external direct bindings.
991 				 */
992 				syminfo[ndx].si_flags |= SYMINFO_FLG_FILTER;
993 				if (sdp->sd_flags1 & FLG_SY1_NDIR)
994 					syminfo[ndx].si_flags |=
995 					    SYMINFO_FLG_NOEXTDIRECT;
996 
997 			} else if (sdp->sd_flags & FLG_SY_AUXFLTR) {
998 				/*
999 				 * An auxiliary filter definition.  By nature,
1000 				 * this definition is direct, in that should the
1001 				 * filtee lookup fail, we'll fall back to this
1002 				 * object.  It may still be necesssary to
1003 				 * prevent external direct bindings.
1004 				 */
1005 				syminfo[ndx].si_flags |= SYMINFO_FLG_AUXILIARY;
1006 				if (sdp->sd_flags1 & FLG_SY1_NDIR)
1007 					syminfo[ndx].si_flags |=
1008 					    SYMINFO_FLG_NOEXTDIRECT;
1009 
1010 			} else if ((sdp->sd_ref == REF_REL_NEED) &&
1011 			    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
1012 				/*
1013 				 * This definition exists within the object
1014 				 * being created.  Flag whether it is necessary
1015 				 * to prevent external direct bindings.
1016 				 */
1017 				if (sdp->sd_flags1 & FLG_SY1_NDIR) {
1018 					syminfo[ndx].si_boundto =
1019 					    SYMINFO_BT_NONE;
1020 					syminfo[ndx].si_flags |=
1021 					    SYMINFO_FLG_NOEXTDIRECT;
1022 				}
1023 
1024 				/*
1025 				 * If external bindings are allowed, or this is
1026 				 * a translator symbol, indicate the binding,
1027 				 * and a direct binding if necessary.
1028 				 */
1029 				if (((sdp->sd_flags1 & FLG_SY1_NDIR) == 0) ||
1030 				    ((dtflags_1 & DF_1_TRANS) && sdp->sd_aux &&
1031 				    sdp->sd_aux->sa_bindto)) {
1032 
1033 					syminfo[ndx].si_flags |=
1034 					    SYMINFO_FLG_DIRECT;
1035 
1036 					if (sdp->sd_flags1 & FLG_SY1_DIR)
1037 						syminfo[ndx].si_flags |=
1038 						    SYMINFO_FLG_DIRECTBIND;
1039 
1040 					/*
1041 					 * If this is a translator, the symbols
1042 					 * boundto element will indicate the
1043 					 * dependency to which it should resolve
1044 					 * rather than itself.  Save this info
1045 					 * for updating after the .dynamic
1046 					 * section has been created.
1047 					 */
1048 					if ((dtflags_1 & DF_1_TRANS) &&
1049 					    sdp->sd_aux &&
1050 					    sdp->sd_aux->sa_bindto) {
1051 						if (list_appendc(sip, sdp) == 0)
1052 							return (0);
1053 					} else {
1054 						syminfo[ndx].si_boundto =
1055 						    SYMINFO_BT_SELF;
1056 					}
1057 				}
1058 			}
1059 		}
1060 
1061 		/*
1062 		 * Note that the `sym' value is reset to be one of the new
1063 		 * symbol table entries.  This symbol will be updated further
1064 		 * depending on the type of the symbol.  Process the .symtab
1065 		 * first, followed by the .dynsym, thus the `sym' value will
1066 		 * remain as the .dynsym value when the .dynsym is present.
1067 		 * This insures that any versioning symbols st_name value will
1068 		 * be appropriate for the string table used to by version
1069 		 * entries.
1070 		 */
1071 		if (enter_in_symtab) {
1072 			Word	_symndx;
1073 
1074 			if (local)
1075 				_symndx = scopesym_ndx;
1076 			else
1077 				_symndx = symtab_ndx;
1078 			symtab[_symndx] = *sdp->sd_sym;
1079 			sdp->sd_sym = sym = &symtab[_symndx];
1080 			(void) st_setstring(strtab, name, &stoff);
1081 			sym->st_name = stoff;
1082 		}
1083 
1084 		if (dynsym && !local) {
1085 			dynsym[dynsym_ndx] = *sdp->sd_sym;
1086 
1087 			/*
1088 			 * Provided this isn't an unnamed register symbol,
1089 			 * update its name and hash value.
1090 			 */
1091 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1092 			    dynsym[dynsym_ndx].st_name) {
1093 				(void) st_setstring(dynstr, name, &stoff);
1094 				dynsym[dynsym_ndx].st_name = stoff;
1095 				if (stoff) {
1096 					Word	_hashndx;
1097 					hashval =
1098 					    sap->sa_hash % ofl->ofl_hashbkts;
1099 					/* LINTED */
1100 					if (_hashndx = hashbkt[hashval]) {
1101 						while (hashchain[_hashndx])
1102 							_hashndx =
1103 							    hashchain[_hashndx];
1104 						hashchain[_hashndx] =
1105 						    sdp->sd_symndx;
1106 					} else
1107 						hashbkt[hashval] =
1108 						    sdp->sd_symndx;
1109 				}
1110 			}
1111 			sdp->sd_sym = sym = &dynsym[dynsym_ndx];
1112 		}
1113 		if (!enter_in_symtab && (!dynsym || local)) {
1114 			if (!(sdp->sd_flags & FLG_SY_UPREQD))
1115 				continue;
1116 			sym = sdp->sd_sym;
1117 		} else
1118 			sdp->sd_flags &= ~FLG_SY_CLEAN;
1119 
1120 
1121 		/*
1122 		 * If we have a weak data symbol for which we need the real
1123 		 * symbol also, save this processing until later.
1124 		 *
1125 		 * The exception to this is if the weak/strong have PLT's
1126 		 * assigned to them.  In that case we don't do the post-weak
1127 		 * processing because the PLT's must be maintained so that we
1128 		 * can do 'interpositioning' on both of the symbols.
1129 		 */
1130 		if ((sap->sa_linkndx) &&
1131 		    (ELF_ST_BIND(sym->st_info) == STB_WEAK) &&
1132 		    (!sap->sa_PLTndx)) {
1133 			Sym_desc *	_sdp =
1134 			    sdp->sd_file->ifl_oldndx[sap->sa_linkndx];
1135 
1136 			if (_sdp->sd_ref != REF_DYN_SEEN) {
1137 				if ((wkp =
1138 				    libld_calloc(sizeof (Wk_desc), 1)) == 0)
1139 					return ((Addr)S_ERROR);
1140 
1141 				if (enter_in_symtab)
1142 					if (local)
1143 						wkp->wk_symtab =
1144 						    &symtab[scopesym_ndx];
1145 					else
1146 						wkp->wk_symtab =
1147 						    &symtab[symtab_ndx];
1148 				if (dynsym && !local)
1149 					wkp->wk_dynsym = &dynsym[dynsym_ndx];
1150 				wkp->wk_weak = sdp;
1151 				wkp->wk_alias = _sdp;
1152 
1153 				if (!(list_appendc(&weak, wkp)))
1154 					return ((Addr)S_ERROR);
1155 
1156 				if (enter_in_symtab)
1157 					if (local)
1158 						scopesym_ndx++;
1159 					else
1160 						symtab_ndx++;
1161 				if (dynsym && !local)
1162 					dynsym_ndx++;
1163 				continue;
1164 			}
1165 		}
1166 
1167 		DBG_CALL(Dbg_syms_old(ofl->ofl_ehdr, sdp));
1168 
1169 		spec = NULL;
1170 		/*
1171 		 * assign new symbol value.
1172 		 */
1173 		sectndx = sdp->sd_shndx;
1174 		if (sectndx == SHN_UNDEF) {
1175 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) &&
1176 			    (sym->st_value != 0)) {
1177 				eprintf(ERR_WARNING, MSG_INTL(MSG_SYM_NOTNULL),
1178 				    demangle(name), sdp->sd_file->ifl_name);
1179 			}
1180 
1181 			/*
1182 			 * Undefined weak global, if we are generating a static
1183 			 * executable, output as an absolute zero.  Otherwise
1184 			 * leave it as is, ld.so.1 will skip symbols of this
1185 			 * type (this technique allows applications and
1186 			 * libraries to test for the existence of a symbol as an
1187 			 * indication of the presence or absence of certain
1188 			 * functionality).
1189 			 */
1190 			if (((flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
1191 			    (FLG_OF_STATIC | FLG_OF_EXEC)) &&
1192 			    (ELF_ST_BIND(sym->st_info) == STB_WEAK)) {
1193 				sdp->sd_flags |= FLG_SY_SPECSEC;
1194 				sdp->sd_shndx = sectndx = SHN_ABS;
1195 			}
1196 		} else if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1197 		    (sectndx == SHN_COMMON)) {
1198 			/* COMMONs have already been processed */
1199 			/* EMPTY */
1200 			;
1201 		} else {
1202 			if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1203 			    (sectndx == SHN_ABS))
1204 				spec = sdp->sd_aux->sa_symspec;
1205 
1206 			/* LINTED */
1207 			if (sdp->sd_flags & FLG_SY_COMMEXP) {
1208 				/*
1209 				 * This is (or was) a COMMON symbol which was
1210 				 * processed above - no processing
1211 				 * required here.
1212 				 */
1213 				;
1214 			} else if (sdp->sd_ref == REF_DYN_NEED) {
1215 				unsigned char	type, bind;
1216 
1217 				sectndx = SHN_UNDEF;
1218 				sym->st_value = 0;
1219 				sym->st_size = 0;
1220 
1221 				/*
1222 				 * Make sure this undefined symbol is returned
1223 				 * to the same binding as was defined in the
1224 				 * original relocatable object reference.
1225 				 */
1226 				type = ELF_ST_TYPE(sym-> st_info);
1227 				if (sdp->sd_flags & FLG_SY_GLOBREF)
1228 					bind = STB_GLOBAL;
1229 				else
1230 					bind = STB_WEAK;
1231 
1232 				sym->st_info = ELF_ST_INFO(bind, type);
1233 
1234 			} else if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
1235 			    (sdp->sd_ref == REF_REL_NEED)) {
1236 				osp = sdp->sd_isc->is_osdesc;
1237 				/* LINTED */
1238 				sectndx = elf_ndxscn(osp->os_scn);
1239 
1240 				/*
1241 				 * In an executable, the new symbol value is the
1242 				 * old value (offset into defining section) plus
1243 				 * virtual address of defining section.  In a
1244 				 * relocatable, the new value is the old value
1245 				 * plus the displacement of the section within
1246 				 * the file.
1247 				 */
1248 				/* LINTED */
1249 				sym->st_value +=
1250 				    (Off)_elf_getxoff(sdp->sd_isc->is_indata);
1251 
1252 				if (!(flags & FLG_OF_RELOBJ)) {
1253 					sym->st_value += osp->os_shdr->sh_addr;
1254 					/*
1255 					 * TLS symbols are relative to
1256 					 * the TLS segment.
1257 					 */
1258 					if ((ELF_ST_TYPE(sym->st_info) ==
1259 					    STT_TLS) && (ofl->ofl_tlsphdr))
1260 						sym->st_value -=
1261 						    ofl->ofl_tlsphdr->p_vaddr;
1262 				}
1263 			}
1264 		}
1265 
1266 		if (spec) {
1267 			switch (spec) {
1268 			case SDAUX_ID_ETEXT:
1269 				sym->st_value = etext;
1270 				sectndx = etext_ndx;
1271 				if (etext_abs)
1272 					sdp->sd_flags |= FLG_SY_SPECSEC;
1273 				else
1274 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1275 				break;
1276 			case SDAUX_ID_EDATA:
1277 				sym->st_value = edata;
1278 				sectndx = edata_ndx;
1279 				if (edata_abs)
1280 					sdp->sd_flags |= FLG_SY_SPECSEC;
1281 				else
1282 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1283 				break;
1284 			case SDAUX_ID_END:
1285 				sym->st_value = end;
1286 				sectndx = end_ndx;
1287 				if (end_abs)
1288 					sdp->sd_flags |= FLG_SY_SPECSEC;
1289 				else
1290 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1291 				break;
1292 			case SDAUX_ID_START:
1293 				sym->st_value = start;
1294 				sectndx = start_ndx;
1295 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1296 				break;
1297 			case SDAUX_ID_DYN:
1298 				if (flags & FLG_OF_DYNAMIC) {
1299 					sym->st_value = ofl->
1300 					    ofl_osdynamic->os_shdr->sh_addr;
1301 					/* LINTED */
1302 					sectndx = elf_ndxscn(
1303 					    ofl->ofl_osdynamic->os_scn);
1304 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1305 				}
1306 				break;
1307 			case SDAUX_ID_PLT:
1308 				if (ofl->ofl_osplt) {
1309 					sym->st_value = ofl->
1310 					    ofl_osplt->os_shdr->sh_addr;
1311 					/* LINTED */
1312 					sectndx = elf_ndxscn(
1313 					    ofl->ofl_osplt->os_scn);
1314 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1315 				}
1316 				break;
1317 			case SDAUX_ID_GOT:
1318 				/*
1319 				 * Symbol bias for negative growing tables is
1320 				 * stored in symbol's value during
1321 				 * allocate_got().
1322 				 */
1323 				sym->st_value += ofl->
1324 				    ofl_osgot->os_shdr->sh_addr;
1325 				/* LINTED */
1326 				sectndx = elf_ndxscn(ofl->
1327 				    ofl_osgot->os_scn);
1328 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1329 				break;
1330 			default:
1331 				/* NOTHING */
1332 				;
1333 			}
1334 		}
1335 
1336 		/*
1337 		 * If a plt index has been assigned to an undefined function,
1338 		 * update the symbols value to the appropriate .plt address.
1339 		 */
1340 		if ((flags & FLG_OF_DYNAMIC) && (flags & FLG_OF_EXEC) &&
1341 		    (sdp->sd_file) &&
1342 		    (sdp->sd_file->ifl_ehdr->e_type == ET_DYN) &&
1343 		    (ELF_ST_TYPE(sym->st_info) == STT_FUNC) &&
1344 		    !(flags & FLG_OF_BFLAG)) {
1345 			if (sap->sa_PLTndx)
1346 				sym->st_value = calc_plt_addr(sdp, ofl);
1347 		}
1348 
1349 		/*
1350 		 * Finish updating the symbols.
1351 		 */
1352 
1353 		/*
1354 		 * Sym Update: if scoped local - set local binding
1355 		 */
1356 		if (local)
1357 			sym->st_info = ELF_ST_INFO(STB_LOCAL,
1358 			    ELF_ST_TYPE(sym->st_info));
1359 
1360 		/*
1361 		 * Sym Updated: If both the .symtab and .dynsym
1362 		 * are present then we've actually updated the information in
1363 		 * the .dynsym, therefore copy this same information to the
1364 		 * .symtab entry.
1365 		 */
1366 		sdp->sd_shndx = sectndx;
1367 		if (enter_in_symtab && dynsym && !local) {
1368 			symtab[symtab_ndx].st_value = sym->st_value;
1369 			symtab[symtab_ndx].st_size = sym->st_size;
1370 			symtab[symtab_ndx].st_info = sym->st_info;
1371 			symtab[symtab_ndx].st_other = sym->st_other;
1372 		}
1373 
1374 
1375 		if (enter_in_symtab) {
1376 			Word	_symndx;
1377 
1378 			if (local)
1379 				_symndx = scopesym_ndx++;
1380 			else
1381 				_symndx = symtab_ndx++;
1382 			if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
1383 			    (sectndx >= SHN_LORESERVE)) {
1384 				assert(symshndx != 0);
1385 				symshndx[_symndx] = sectndx;
1386 				symtab[_symndx].st_shndx = SHN_XINDEX;
1387 			} else {
1388 				/* LINTED */
1389 				symtab[_symndx].st_shndx = (Half)sectndx;
1390 			}
1391 		}
1392 
1393 		if (dynsym && !local) {
1394 			if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
1395 			    (sectndx >= SHN_LORESERVE)) {
1396 				assert(dynshndx != 0);
1397 				dynshndx[dynsym_ndx] = sectndx;
1398 				dynsym[dynsym_ndx].st_shndx = SHN_XINDEX;
1399 			} else {
1400 				/* LINTED */
1401 				dynsym[dynsym_ndx].st_shndx = (Half)sectndx;
1402 			}
1403 			dynsym_ndx++;
1404 		}
1405 
1406 		DBG_CALL(Dbg_syms_new(ofl->ofl_ehdr, sym, sdp));
1407 	}
1408 
1409 	/*
1410 	 * Now that all the symbols have been processed update any weak symbols
1411 	 * information (ie. copy all information except `st_name').  As both
1412 	 * symbols will be represented in the output, return the weak symbol to
1413 	 * its correct type.
1414 	 */
1415 	for (LIST_TRAVERSE(&weak, lnp1, wkp)) {
1416 		Sym_desc *	sdp, * _sdp;
1417 		Sym *		sym, * _sym, * __sym;
1418 		unsigned char	bind;
1419 
1420 		sdp = wkp->wk_weak;
1421 		_sdp = wkp->wk_alias;
1422 		_sym = _sdp->sd_sym;
1423 
1424 		sdp->sd_flags |= FLG_SY_WEAKDEF;
1425 
1426 		/*
1427 		 * If the symbol definition has been scoped then assign it to
1428 		 * be local, otherwise if it's from a shared object then we need
1429 		 * to maintain the binding of the original reference.
1430 		 */
1431 		if (sdp->sd_flags1 & FLG_SY1_LOCL) {
1432 			if (flags & FLG_OF_PROCRED)
1433 				bind = STB_LOCAL;
1434 			else
1435 				bind = STB_WEAK;
1436 		} else if ((sdp->sd_ref == REF_DYN_NEED) &&
1437 		    (sdp->sd_flags & FLG_SY_GLOBREF))
1438 			bind = STB_GLOBAL;
1439 		else
1440 			bind = STB_WEAK;
1441 
1442 		DBG_CALL(Dbg_syms_old(ofl->ofl_ehdr, sdp));
1443 		if ((sym = wkp->wk_symtab) != 0) {
1444 			sym = wkp->wk_symtab;
1445 			sym->st_value = _sym->st_value;
1446 			sym->st_size = _sym->st_size;
1447 			sym->st_other = _sym->st_other;
1448 			sym->st_shndx = _sym->st_shndx;
1449 			sym->st_info = ELF_ST_INFO(bind,
1450 			    ELF_ST_TYPE(sym->st_info));
1451 			__sym = sym;
1452 		}
1453 		if ((sym = wkp->wk_dynsym) != 0) {
1454 			sym = wkp->wk_dynsym;
1455 			sym->st_value = _sym->st_value;
1456 			sym->st_size = _sym->st_size;
1457 			sym->st_other = _sym->st_other;
1458 			sym->st_shndx = _sym->st_shndx;
1459 			sym->st_info = ELF_ST_INFO(bind,
1460 			    ELF_ST_TYPE(sym->st_info));
1461 			__sym = sym;
1462 		}
1463 		DBG_CALL(Dbg_syms_new(ofl->ofl_ehdr, __sym, sdp));
1464 	}
1465 
1466 	/*
1467 	 * Now display GOT debugging information if required
1468 	 */
1469 	DBG_CALL(Dbg_got_display(gottable, ofl));
1470 
1471 	/*
1472 	 * Update the section headers information.
1473 	 */
1474 	if (symtab) {
1475 		Shdr *	shdr = ofl->ofl_ossymtab->os_shdr;
1476 
1477 		shdr->sh_info = ofl->ofl_shdrcnt + ofl->ofl_locscnt +
1478 			ofl->ofl_scopecnt + 2;
1479 		/* LINTED */
1480 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osstrtab->os_scn);
1481 		if (symshndx) {
1482 			shdr = ofl->ofl_ossymshndx->os_shdr;
1483 			shdr->sh_link =
1484 				(Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
1485 		}
1486 	}
1487 	if (dynsym) {
1488 		Shdr *	shdr = ofl->ofl_osdynsym->os_shdr;
1489 
1490 		shdr->sh_info = ofl->ofl_dynshdrcnt + ofl->ofl_lregsymcnt + 1;
1491 		/* LINTED */
1492 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
1493 
1494 		ofl->ofl_oshash->os_shdr->sh_link =
1495 		    /* LINTED */
1496 		    (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
1497 		if (dynshndx) {
1498 			shdr = ofl->ofl_osdynshndx->os_shdr;
1499 			shdr->sh_link =
1500 				(Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
1501 		}
1502 	}
1503 
1504 	/*
1505 	 * Used by ld.so.1 only.
1506 	 */
1507 	return (etext);
1508 }
1509 
1510 /*
1511  * Build the dynamic section.
1512  */
1513 int
1514 update_odynamic(Ofl_desc *ofl)
1515 {
1516 	Listnode	*lnp;
1517 	Ifl_desc	*ifl;
1518 	Sym_desc	*sdp;
1519 	Shdr		*shdr;
1520 	Dyn		*_dyn = (Dyn *)ofl->ofl_osdynamic->os_outdata->d_buf;
1521 	Dyn		*dyn;
1522 	Str_tbl		*dynstr;
1523 	uint_t		stoff;
1524 	Word		flags = ofl->ofl_flags;
1525 
1526 	dynstr = ofl->ofl_dynstrtab;
1527 	ofl->ofl_osdynamic->os_shdr->sh_link =
1528 	    /* LINTED */
1529 	    (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
1530 
1531 	dyn = _dyn;
1532 
1533 	for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, ifl)) {
1534 		if ((ifl->ifl_flags &
1535 		    (FLG_IF_IGNORE | FLG_IF_DEPREQD)) == FLG_IF_IGNORE)
1536 			continue;
1537 
1538 		/*
1539 		 * Create and set up the DT_POSFLAG_1 entry here if required.
1540 		 */
1541 		if ((ifl->ifl_flags & (FLG_IF_LAZYLD|FLG_IF_GRPPRM)) &&
1542 		    (ifl->ifl_flags & (FLG_IF_NEEDED))) {
1543 			dyn->d_tag = DT_POSFLAG_1;
1544 			if (ifl->ifl_flags & FLG_IF_LAZYLD)
1545 				dyn->d_un.d_val = DF_P1_LAZYLOAD;
1546 			if (ifl->ifl_flags & FLG_IF_GRPPRM)
1547 				dyn->d_un.d_val |= DF_P1_GROUPPERM;
1548 			dyn++;
1549 		}
1550 
1551 		if (ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR))
1552 			dyn->d_tag = DT_NEEDED;
1553 		else
1554 			continue;
1555 
1556 		(void) st_setstring(dynstr, ifl->ifl_soname, &stoff);
1557 		dyn->d_un.d_val = stoff;
1558 		/* LINTED */
1559 		ifl->ifl_neededndx = (Half)(((uintptr_t)dyn - (uintptr_t)_dyn) /
1560 		    sizeof (Dyn));
1561 		dyn++;
1562 	}
1563 
1564 	if (ofl->ofl_dtsfltrs) {
1565 		Dfltr_desc *	dftp;
1566 		Aliste		off;
1567 
1568 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp)) {
1569 			if (dftp->dft_flag == FLG_SY_AUXFLTR)
1570 				dyn->d_tag = DT_SUNW_AUXILIARY;
1571 			else
1572 				dyn->d_tag = DT_SUNW_FILTER;
1573 
1574 			(void) st_setstring(dynstr, dftp->dft_str, &stoff);
1575 			dyn->d_un.d_val = stoff;
1576 			dftp->dft_ndx = (Half)(((uintptr_t)dyn -
1577 			    (uintptr_t)_dyn) / sizeof (Dyn));
1578 			dyn++;
1579 		}
1580 	}
1581 	if (((sdp = sym_find(MSG_ORIG(MSG_SYM_INIT_U),
1582 	    SYM_NOHASH, 0, ofl)) != NULL) &&
1583 		sdp->sd_ref == REF_REL_NEED) {
1584 		dyn->d_tag = DT_INIT;
1585 		dyn->d_un.d_ptr = sdp->sd_sym->st_value;
1586 		dyn++;
1587 	}
1588 	if (((sdp = sym_find(MSG_ORIG(MSG_SYM_FINI_U),
1589 	    SYM_NOHASH, 0, ofl)) != NULL) &&
1590 		sdp->sd_ref == REF_REL_NEED) {
1591 		dyn->d_tag = DT_FINI;
1592 		dyn->d_un.d_ptr = sdp->sd_sym->st_value;
1593 		dyn++;
1594 	}
1595 	if (ofl->ofl_soname) {
1596 		dyn->d_tag = DT_SONAME;
1597 		(void) st_setstring(dynstr, ofl->ofl_soname, &stoff);
1598 		dyn->d_un.d_val = stoff;
1599 		dyn++;
1600 	}
1601 	if (ofl->ofl_filtees) {
1602 		if (flags & FLG_OF_AUX) {
1603 			dyn->d_tag = DT_AUXILIARY;
1604 		} else {
1605 			dyn->d_tag = DT_FILTER;
1606 		}
1607 		(void) st_setstring(dynstr, ofl->ofl_filtees, &stoff);
1608 		dyn->d_un.d_val = stoff;
1609 		dyn++;
1610 	}
1611 	if (ofl->ofl_rpath) {
1612 		(void) st_setstring(dynstr, ofl->ofl_rpath, &stoff);
1613 		dyn->d_tag = DT_RUNPATH;
1614 		dyn->d_un.d_val = stoff;
1615 		dyn++;
1616 		dyn->d_tag = DT_RPATH;
1617 		dyn->d_un.d_val = stoff;
1618 		dyn++;
1619 	}
1620 	if (ofl->ofl_config) {
1621 		dyn->d_tag = DT_CONFIG;
1622 		(void) st_setstring(dynstr, ofl->ofl_config, &stoff);
1623 		dyn->d_un.d_val = stoff;
1624 		dyn++;
1625 	}
1626 	if (ofl->ofl_depaudit) {
1627 		dyn->d_tag = DT_DEPAUDIT;
1628 		(void) st_setstring(dynstr, ofl->ofl_depaudit, &stoff);
1629 		dyn->d_un.d_val = stoff;
1630 		dyn++;
1631 	}
1632 	if (ofl->ofl_audit) {
1633 		dyn->d_tag = DT_AUDIT;
1634 		(void) st_setstring(dynstr, ofl->ofl_audit, &stoff);
1635 		dyn->d_un.d_val = stoff;
1636 		dyn++;
1637 	}
1638 
1639 	/*
1640 	 * The following DT_* entries do not apply to relocatable objects.
1641 	 */
1642 	if (!(flags & FLG_OF_RELOBJ)) {
1643 
1644 		dyn->d_tag = DT_HASH;
1645 		dyn->d_un.d_ptr = ofl->ofl_oshash->os_shdr->sh_addr;
1646 		dyn++;
1647 
1648 		shdr = ofl->ofl_osdynstr->os_shdr;
1649 		dyn->d_tag = DT_STRTAB;
1650 		dyn->d_un.d_ptr = shdr->sh_addr;
1651 		dyn++;
1652 
1653 		dyn->d_tag = DT_STRSZ;
1654 		dyn->d_un.d_ptr = shdr->sh_size;
1655 		dyn++;
1656 
1657 		shdr = ofl->ofl_osdynsym->os_shdr;
1658 		dyn->d_tag = DT_SYMTAB;
1659 		dyn->d_un.d_ptr = shdr->sh_addr;
1660 		dyn++;
1661 
1662 		dyn->d_tag = DT_SYMENT;
1663 		dyn->d_un.d_ptr = shdr->sh_entsize;
1664 		dyn++;
1665 
1666 		/*
1667 		 * Reserve the DT_CHECKSUM entry.  Its value will be filled in
1668 		 * after the complete image is built.
1669 		 */
1670 		dyn->d_tag = DT_CHECKSUM;
1671 		ofl->ofl_checksum = &dyn->d_un.d_val;
1672 		dyn++;
1673 
1674 		if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
1675 		    FLG_OF_VERDEF) {
1676 			shdr = ofl->ofl_osverdef->os_shdr;
1677 			dyn->d_tag = DT_VERDEF;
1678 			dyn->d_un.d_ptr = shdr->sh_addr;
1679 			dyn++;
1680 			dyn->d_tag = DT_VERDEFNUM;
1681 			dyn->d_un.d_ptr = shdr->sh_info;
1682 			dyn++;
1683 		}
1684 		if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
1685 		    FLG_OF_VERNEED) {
1686 			shdr = ofl->ofl_osverneed->os_shdr;
1687 			dyn->d_tag = DT_VERNEED;
1688 			dyn->d_un.d_ptr = shdr->sh_addr;
1689 			dyn++;
1690 			dyn->d_tag = DT_VERNEEDNUM;
1691 			dyn->d_un.d_ptr = shdr->sh_info;
1692 			dyn++;
1693 		}
1694 		if ((ofl->ofl_flags1 & FLG_OF1_RELCNT) &&
1695 		    ofl->ofl_relocrelcnt) {
1696 			dyn->d_tag = M_REL_DT_COUNT;
1697 			dyn->d_un.d_val = ofl->ofl_relocrelcnt;
1698 			dyn++;
1699 		}
1700 		if (flags & FLG_OF_TEXTREL) {
1701 			/*
1702 			 * Only the presence of this entry is used in this
1703 			 * implementation, not the value stored.
1704 			 */
1705 			dyn->d_tag = DT_TEXTREL;
1706 			dyn->d_un.d_val = 0;
1707 			dyn++;
1708 		}
1709 
1710 		if (ofl->ofl_osfiniarray) {
1711 			shdr = ofl->ofl_osfiniarray->os_shdr;
1712 
1713 			dyn->d_tag = DT_FINI_ARRAY;
1714 			dyn->d_un.d_ptr = shdr->sh_addr;
1715 			dyn++;
1716 
1717 			dyn->d_tag = DT_FINI_ARRAYSZ;
1718 			dyn->d_un.d_val = shdr->sh_size;
1719 			dyn++;
1720 		}
1721 
1722 		if (ofl->ofl_osinitarray) {
1723 			shdr = ofl->ofl_osinitarray->os_shdr;
1724 
1725 			dyn->d_tag = DT_INIT_ARRAY;
1726 			dyn->d_un.d_ptr = shdr->sh_addr;
1727 			dyn++;
1728 
1729 			dyn->d_tag = DT_INIT_ARRAYSZ;
1730 			dyn->d_un.d_val = shdr->sh_size;
1731 			dyn++;
1732 		}
1733 
1734 		if (ofl->ofl_ospreinitarray) {
1735 			shdr = ofl->ofl_ospreinitarray->os_shdr;
1736 
1737 			dyn->d_tag = DT_PREINIT_ARRAY;
1738 			dyn->d_un.d_ptr = shdr->sh_addr;
1739 			dyn++;
1740 
1741 			dyn->d_tag = DT_PREINIT_ARRAYSZ;
1742 			dyn->d_un.d_val = shdr->sh_size;
1743 			dyn++;
1744 		}
1745 
1746 		if (ofl->ofl_pltcnt) {
1747 			shdr =  ofl->ofl_osplt->os_relosdesc->os_shdr;
1748 
1749 			dyn->d_tag = DT_PLTRELSZ;
1750 			dyn->d_un.d_ptr = shdr->sh_size;
1751 			dyn++;
1752 			dyn->d_tag = DT_PLTREL;
1753 			dyn->d_un.d_ptr = M_REL_DT_TYPE;
1754 			dyn++;
1755 			dyn->d_tag = DT_JMPREL;
1756 			dyn->d_un.d_ptr = shdr->sh_addr;
1757 			dyn++;
1758 		}
1759 		if (ofl->ofl_pltpad) {
1760 			shdr =  ofl->ofl_osplt->os_shdr;
1761 
1762 			dyn->d_tag = DT_PLTPAD;
1763 			if (ofl->ofl_pltcnt)
1764 				dyn->d_un.d_ptr = shdr->sh_addr +
1765 					M_PLT_RESERVSZ +
1766 					ofl->ofl_pltcnt * M_PLT_ENTSIZE;
1767 			else
1768 				dyn->d_un.d_ptr = shdr->sh_addr;
1769 			dyn++;
1770 			dyn->d_tag = DT_PLTPADSZ;
1771 			dyn->d_un.d_val = ofl->ofl_pltpad *
1772 				M_PLT_ENTSIZE;
1773 			dyn++;
1774 		}
1775 		if (ofl->ofl_relocsz) {
1776 			dyn->d_tag = M_REL_DT_TYPE;
1777 			dyn->d_un.d_ptr = ofl->ofl_osrelhead->os_shdr->sh_addr;
1778 			dyn++;
1779 			dyn->d_tag = M_REL_DT_SIZE;
1780 			dyn->d_un.d_ptr = ofl->ofl_relocsz;
1781 			dyn++;
1782 			dyn->d_tag = M_REL_DT_ENT;
1783 			if (ofl->ofl_osrelhead->os_shdr->sh_type == SHT_REL)
1784 				dyn->d_un.d_ptr = sizeof (Rel);
1785 			else
1786 				dyn->d_un.d_ptr = sizeof (Rela);
1787 			dyn++;
1788 		}
1789 		if (ofl->ofl_ossyminfo) {
1790 			shdr = ofl->ofl_ossyminfo->os_shdr;
1791 			dyn->d_tag = DT_SYMINFO;
1792 			dyn->d_un.d_ptr = shdr->sh_addr;
1793 			dyn++;
1794 			dyn->d_tag = DT_SYMINSZ;
1795 			dyn->d_un.d_val = shdr->sh_size;
1796 			dyn++;
1797 			dyn->d_tag = DT_SYMINENT;
1798 			dyn->d_un.d_val = sizeof (Syminfo);
1799 			dyn++;
1800 		}
1801 		if (ofl->ofl_osmove) {
1802 			Os_desc *	osp;
1803 
1804 			dyn->d_tag = DT_MOVEENT;
1805 			osp = ofl->ofl_osmove;
1806 			dyn->d_un.d_val = osp->os_shdr->sh_entsize;
1807 			dyn++;
1808 			dyn->d_tag = DT_MOVESZ;
1809 			dyn->d_un.d_val = osp->os_shdr->sh_size;
1810 			dyn++;
1811 			dyn->d_tag = DT_MOVETAB;
1812 			dyn->d_un.d_val = osp->os_shdr->sh_addr;
1813 			dyn++;
1814 		}
1815 		if (ofl->ofl_regsymcnt) {
1816 			int	ndx;
1817 
1818 			for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1819 				if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1820 					continue;
1821 
1822 				dyn->d_tag = M_DT_REGISTER;
1823 				dyn->d_un.d_val = sdp->sd_symndx;
1824 				dyn++;
1825 			}
1826 		}
1827 
1828 		for (LIST_TRAVERSE(&ofl->ofl_rtldinfo, lnp, sdp)) {
1829 			dyn->d_tag = DT_SUNW_RTLDINF;
1830 			dyn->d_un.d_ptr = sdp->sd_sym->st_value;
1831 			dyn++;
1832 		}
1833 
1834 		if (ofl->ofl_osdynamic->os_sgdesc &&
1835 		    (ofl->ofl_osdynamic->os_sgdesc->sg_phdr.p_flags & PF_W)) {
1836 			if (ofl->ofl_osinterp) {
1837 				dyn->d_tag = DT_DEBUG;
1838 				dyn->d_un.d_ptr = 0;
1839 				dyn++;
1840 			}
1841 
1842 			dyn->d_tag = DT_FEATURE_1;
1843 			if (ofl->ofl_osmove)
1844 				dyn->d_un.d_val = 0;
1845 			else
1846 				dyn->d_un.d_val = DTF_1_PARINIT;
1847 			dyn++;
1848 		}
1849 
1850 		if (ofl->ofl_oscap) {
1851 			dyn->d_tag = DT_SUNW_CAP;
1852 			dyn->d_un.d_val = ofl->ofl_oscap->os_shdr->sh_addr;
1853 			dyn++;
1854 		}
1855 	}
1856 
1857 	if (flags & FLG_OF_SYMBOLIC) {
1858 		dyn->d_tag = DT_SYMBOLIC;
1859 		dyn->d_un.d_val = 0;
1860 		dyn++;
1861 	}
1862 	dyn->d_tag = DT_FLAGS;
1863 	dyn->d_un.d_val = ofl->ofl_dtflags;
1864 	dyn++;
1865 
1866 	/*
1867 	 * If -Bdirect was specified, but some NODIRECT symbols were specified
1868 	 * via a mapfile, or -znodirect was used on the command line, then
1869 	 * clear the DF_1_DIRECT flag.  The resultant object will use per-symbol
1870 	 * direct bindings rather than be enabled for global direct bindings.
1871 	 */
1872 	if (ofl->ofl_flags1 & FLG_OF1_NDIRECT)
1873 		ofl->ofl_dtflags_1 &= ~DF_1_DIRECT;
1874 
1875 	dyn->d_tag = DT_FLAGS_1;
1876 	dyn->d_un.d_val = ofl->ofl_dtflags_1;
1877 	dyn++;
1878 
1879 	mach_update_odynamic(ofl, &dyn);
1880 
1881 	dyn->d_tag = DT_NULL;
1882 	dyn->d_un.d_val = 0;
1883 
1884 	return (1);
1885 }
1886 
1887 /*
1888  * Build the version definition section
1889  */
1890 int
1891 update_overdef(Ofl_desc *ofl)
1892 {
1893 	Listnode	*lnp1, *lnp2;
1894 	Ver_desc	*vdp, *_vdp;
1895 	Verdef		*vdf, *_vdf;
1896 	int		num = 0;
1897 	Os_desc		*strosp, *symosp;
1898 
1899 	/*
1900 	 * Traverse the version descriptors and update the version structures
1901 	 * to point to the dynstr name in preparation for building the version
1902 	 * section structure.
1903 	 */
1904 	for (LIST_TRAVERSE(&ofl->ofl_verdesc, lnp1, vdp)) {
1905 		Sym_desc *	sdp;
1906 
1907 		if (vdp->vd_flags & VER_FLG_BASE) {
1908 			const char	*name = vdp->vd_name;
1909 			uint_t		stoff;
1910 
1911 			/*
1912 			 * Create a new string table entry to represent the base
1913 			 * version name (there is no corresponding symbol for
1914 			 * this).
1915 			 */
1916 			if (!(ofl->ofl_flags & FLG_OF_DYNAMIC)) {
1917 				(void) st_setstring(ofl->ofl_strtab,
1918 					name, &stoff);
1919 				/* LINTED */
1920 				vdp->vd_name = (const char *)(uintptr_t)stoff;
1921 			} else {
1922 				(void) st_setstring(ofl->ofl_dynstrtab,
1923 					name, &stoff);
1924 				/* LINTED */
1925 				vdp->vd_name = (const char *)(uintptr_t)stoff;
1926 			}
1927 		} else {
1928 			sdp = sym_find(vdp->vd_name, vdp->vd_hash, 0, ofl);
1929 			/* LINTED */
1930 			vdp->vd_name = (const char *)
1931 				(uintptr_t)sdp->sd_sym->st_name;
1932 		}
1933 	}
1934 
1935 	_vdf = vdf = (Verdef *)ofl->ofl_osverdef->os_outdata->d_buf;
1936 
1937 	/*
1938 	 * Traverse the version descriptors and update the version section to
1939 	 * reflect each version and its associated dependencies.
1940 	 */
1941 	for (LIST_TRAVERSE(&ofl->ofl_verdesc, lnp1, vdp)) {
1942 		Half		cnt = 1;
1943 		Verdaux *	vdap, * _vdap;
1944 
1945 		_vdap = vdap = (Verdaux *)(vdf + 1);
1946 
1947 		vdf->vd_version = VER_DEF_CURRENT;
1948 		vdf->vd_flags	= vdp->vd_flags & MSK_VER_USER;
1949 		vdf->vd_ndx	= vdp->vd_ndx;
1950 		vdf->vd_hash	= vdp->vd_hash;
1951 
1952 		/* LINTED */
1953 		vdap->vda_name = (uintptr_t)vdp->vd_name;
1954 		vdap++;
1955 		/* LINTED */
1956 		_vdap->vda_next = (Word)((uintptr_t)vdap - (uintptr_t)_vdap);
1957 
1958 		/*
1959 		 * Traverse this versions dependency list generating the
1960 		 * appropriate version dependency entries.
1961 		 */
1962 		for (LIST_TRAVERSE(&vdp->vd_deps, lnp2, _vdp)) {
1963 			/* LINTED */
1964 			vdap->vda_name = (uintptr_t)_vdp->vd_name;
1965 			_vdap = vdap;
1966 			vdap++, cnt++;
1967 			/* LINTED */
1968 			_vdap->vda_next = (Word)((uintptr_t)vdap -
1969 			    (uintptr_t)_vdap);
1970 		}
1971 		_vdap->vda_next = 0;
1972 
1973 		/*
1974 		 * Record the versions auxiliary array offset and the associated
1975 		 * dependency count.
1976 		 */
1977 		/* LINTED */
1978 		vdf->vd_aux = (Word)((uintptr_t)(vdf + 1) - (uintptr_t)vdf);
1979 		vdf->vd_cnt = cnt;
1980 
1981 		/*
1982 		 * Record the next versions offset and update the version
1983 		 * pointer.  Remember the previous version offset as the very
1984 		 * last structures next pointer should be null.
1985 		 */
1986 		_vdf = vdf;
1987 		vdf = (Verdef *)vdap, num++;
1988 		/* LINTED */
1989 		_vdf->vd_next = (Word)((uintptr_t)vdf - (uintptr_t)_vdf);
1990 	}
1991 	_vdf->vd_next = 0;
1992 
1993 	/*
1994 	 * Record the string table association with the version definition
1995 	 * section, and the symbol table associated with the version symbol
1996 	 * table (the actual contents of the version symbol table are filled
1997 	 * in during symbol update).
1998 	 */
1999 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) ||
2000 	    (ofl->ofl_flags & FLG_OF_STATIC)) {
2001 		strosp = ofl->ofl_osstrtab;
2002 		symosp = ofl->ofl_ossymtab;
2003 	} else {
2004 		strosp = ofl->ofl_osdynstr;
2005 		symosp = ofl->ofl_osdynsym;
2006 	}
2007 	/* LINTED */
2008 	ofl->ofl_osverdef->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
2009 	/* LINTED */
2010 	ofl->ofl_osversym->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
2011 
2012 	/*
2013 	 * The version definition sections `info' field is used to indicate the
2014 	 * number of entries in this section.
2015 	 */
2016 	ofl->ofl_osverdef->os_shdr->sh_info = num;
2017 
2018 	return (1);
2019 }
2020 
2021 /*
2022  * Build the version needed section
2023  */
2024 int
2025 update_overneed(Ofl_desc *ofl)
2026 {
2027 	Listnode	*lnp;
2028 	Ifl_desc	*ifl;
2029 	Verneed		*vnd, *_vnd;
2030 	Str_tbl		*dynstr;
2031 	Word		num = 0, cnt = 0;
2032 
2033 	dynstr = ofl->ofl_dynstrtab;
2034 	_vnd = vnd = (Verneed *)ofl->ofl_osverneed->os_outdata->d_buf;
2035 
2036 	/*
2037 	 * Traverse the shared object list looking for dependencies that have
2038 	 * versions defined within them.
2039 	 */
2040 	for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, ifl)) {
2041 		Half		_cnt;
2042 		Vernaux		*_vnap, *vnap;
2043 		Sdf_desc	*sdf = ifl->ifl_sdfdesc;
2044 		uint_t		stoff;
2045 
2046 		if (!(ifl->ifl_flags & FLG_IF_VERNEED))
2047 			continue;
2048 
2049 		vnd->vn_version = VER_NEED_CURRENT;
2050 
2051 		(void) st_setstring(dynstr, ifl->ifl_soname, &stoff);
2052 		vnd->vn_file = stoff;
2053 
2054 		_vnap = vnap = (Vernaux *)(vnd + 1);
2055 
2056 		if (sdf && (sdf->sdf_flags & FLG_SDF_SPECVER)) {
2057 			Sdv_desc *	sdv;
2058 			Listnode *	lnp2;
2059 
2060 			/*
2061 			 * If version needed definitions were specified in
2062 			 * a mapfile ($VERSION=*) then record those
2063 			 * definitions.
2064 			 */
2065 			for (LIST_TRAVERSE(&sdf->sdf_verneed, lnp2, sdv)) {
2066 				(void) st_setstring(dynstr,
2067 					sdv->sdv_name, &stoff);
2068 				vnap->vna_name = stoff;
2069 				/* LINTED */
2070 				vnap->vna_hash = (Word)elf_hash(sdv->sdv_name);
2071 				vnap->vna_flags = 0;
2072 				vnap->vna_other = 0;
2073 				_vnap = vnap;
2074 				vnap++;
2075 				cnt++;
2076 				/* LINTED */
2077 				_vnap->vna_next = (Word)((uintptr_t)vnap -
2078 				    (uintptr_t)_vnap);
2079 			}
2080 		} else {
2081 
2082 			/*
2083 			 * Traverse the version index list recording
2084 			 * each version as a needed dependency.
2085 			 */
2086 			for (cnt = _cnt = 0; _cnt <= ifl->ifl_vercnt;
2087 			    _cnt++) {
2088 				Ver_index *	vip = &ifl->ifl_verndx[_cnt];
2089 
2090 				if (vip->vi_flags & FLG_VER_REFER) {
2091 					(void) st_setstring(dynstr,
2092 						vip->vi_name, &stoff);
2093 					vnap->vna_name = stoff;
2094 					if (vip->vi_desc) {
2095 					    vnap->vna_hash =
2096 						vip->vi_desc->vd_hash;
2097 					    vnap->vna_flags =
2098 						vip->vi_desc->vd_flags;
2099 					} else {
2100 					    vnap->vna_hash = 0;
2101 					    vnap->vna_flags = 0;
2102 					}
2103 					vnap->vna_other = 0;
2104 
2105 					_vnap = vnap;
2106 					vnap++, cnt++;
2107 					_vnap->vna_next =
2108 						/* LINTED */
2109 						(Word)((uintptr_t)vnap -
2110 						    (uintptr_t)_vnap);
2111 				}
2112 			}
2113 		}
2114 		_vnap->vna_next = 0;
2115 
2116 		/*
2117 		 * Record the versions auxiliary array offset and
2118 		 * the associated dependency count.
2119 		 */
2120 		/* LINTED */
2121 		vnd->vn_aux = (Word)((uintptr_t)(vnd + 1) - (uintptr_t)vnd);
2122 		/* LINTED */
2123 		vnd->vn_cnt = (Half)cnt;
2124 
2125 		/*
2126 		 * Record the next versions offset and update the version
2127 		 * pointer.  Remember the previous version offset as the very
2128 		 * last structures next pointer should be null.
2129 		 */
2130 		_vnd = vnd;
2131 		vnd = (Verneed *)vnap, num++;
2132 		/* LINTED */
2133 		_vnd->vn_next = (Word)((uintptr_t)vnd - (uintptr_t)_vnd);
2134 	}
2135 	_vnd->vn_next = 0;
2136 
2137 	/*
2138 	 * Record association on string table section and use the
2139 	 * `info' field to indicate the number of entries in this
2140 	 * section.
2141 	 */
2142 	ofl->ofl_osverneed->os_shdr->sh_link =
2143 	    /* LINTED */
2144 	    (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
2145 	ofl->ofl_osverneed->os_shdr->sh_info = num;
2146 
2147 	return (1);
2148 }
2149 
2150 
2151 /*
2152  * Update syminfo section.
2153  */
2154 uintptr_t
2155 update_osyminfo(Ofl_desc * ofl)
2156 {
2157 	Os_desc *	symosp, * infosp = ofl->ofl_ossyminfo;
2158 	Syminfo *	sip = infosp->os_outdata->d_buf;
2159 	Shdr *		shdr = infosp->os_shdr;
2160 	char		*strtab;
2161 	Listnode *	lnp;
2162 	Sym_desc *	sdp;
2163 	Aliste		off;
2164 	Sfltr_desc *	sftp;
2165 
2166 	if (ofl->ofl_flags & FLG_OF_RELOBJ) {
2167 		symosp = ofl->ofl_ossymtab;
2168 		strtab = ofl->ofl_osstrtab->os_outdata->d_buf;
2169 	} else {
2170 		symosp = ofl->ofl_osdynsym;
2171 		strtab = ofl->ofl_osdynstr->os_outdata->d_buf;
2172 	}
2173 
2174 	/* LINTED */
2175 	infosp->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
2176 	if (ofl->ofl_osdynamic)
2177 		infosp->os_shdr->sh_info =
2178 		    /* LINTED */
2179 		    (Word)elf_ndxscn(ofl->ofl_osdynamic->os_scn);
2180 
2181 	/*
2182 	 * Update any references with the index into the dynamic table.
2183 	 */
2184 	for (LIST_TRAVERSE(&ofl->ofl_syminfsyms, lnp, sdp)) {
2185 		Ifl_desc *	ifl;
2186 		if (sdp->sd_aux && sdp->sd_aux->sa_bindto)
2187 			ifl = sdp->sd_aux->sa_bindto;
2188 		else
2189 			ifl = sdp->sd_file;
2190 		sip[sdp->sd_symndx].si_boundto = ifl->ifl_neededndx;
2191 	}
2192 
2193 	/*
2194 	 * Update any filtee references with the index into the dynamic table.
2195 	 */
2196 	for (ALIST_TRAVERSE(ofl->ofl_symfltrs, off, sftp)) {
2197 		Dfltr_desc *	dftp;
2198 
2199 		/* LINTED */
2200 		dftp = (Dfltr_desc *)((char *)ofl->ofl_dtsfltrs +
2201 		    sftp->sft_off);
2202 		sip[sftp->sft_sdp->sd_symndx].si_boundto = dftp->dft_ndx;
2203 	}
2204 
2205 	/*
2206 	 * Display debugging information about section.
2207 	 */
2208 	DBG_CALL(Dbg_syminfo_title());
2209 	if (dbg_mask) {
2210 		size_t	_cnt, cnt = shdr->sh_size / shdr->sh_entsize;
2211 		Sym *	symtab = symosp->os_outdata->d_buf;
2212 		Dyn *	dyn;
2213 
2214 		if (ofl->ofl_osdynamic)
2215 			dyn = ofl->ofl_osdynamic->os_outdata->d_buf;
2216 		else
2217 			dyn = 0;
2218 
2219 		for (_cnt = 1; _cnt < cnt; _cnt++) {
2220 			if (sip[_cnt].si_flags || sip[_cnt].si_boundto)
2221 				/* LINTED */
2222 				DBG_CALL(Dbg_syminfo_entry((int)_cnt,
2223 				    &sip[_cnt], &symtab[_cnt], strtab, dyn));
2224 		}
2225 	}
2226 	return (1);
2227 }
2228 
2229 /*
2230  * Build the output elf header.
2231  */
2232 uintptr_t
2233 update_oehdr(Ofl_desc * ofl)
2234 {
2235 	Ehdr *		ehdr = ofl->ofl_ehdr;
2236 
2237 	/*
2238 	 * If an entry point symbol has already been established (refer
2239 	 * sym_validate()) simply update the elf header entry point with the
2240 	 * symbols value.  If no entry point is defined it will have been filled
2241 	 * with the start address of the first section within the text segment
2242 	 * (refer update_outfile()).
2243 	 */
2244 	if (ofl->ofl_entry)
2245 		ehdr->e_entry =
2246 			((Sym_desc *)(ofl->ofl_entry))->sd_sym->st_value;
2247 
2248 	/*
2249 	 * Note. it may be necessary to update the `e_flags' field in the
2250 	 * machine dependent section.
2251 	 */
2252 	ehdr->e_ident[EI_DATA] = M_DATA;
2253 	if (ofl->ofl_e_machine != M_MACH) {
2254 		if (ofl->ofl_e_machine != M_MACHPLUS)
2255 			return (S_ERROR);
2256 		if ((ofl->ofl_e_flags & M_FLAGSPLUS) == 0)
2257 			return (S_ERROR);
2258 	}
2259 	ehdr->e_machine = ofl->ofl_e_machine;
2260 	ehdr->e_flags = ofl->ofl_e_flags;
2261 	ehdr->e_version = ofl->ofl_libver;
2262 
2263 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
2264 		ehdr->e_type = ET_DYN;
2265 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
2266 		ehdr->e_type = ET_REL;
2267 	else
2268 		ehdr->e_type = ET_EXEC;
2269 
2270 	return (1);
2271 }
2272 
2273 /*
2274  * Perform move table expansion.
2275  */
2276 static uintptr_t
2277 expand_move(Ofl_desc *ofl, Sym_desc *sdp, Move *u1)
2278 {
2279 	Move		*mv;
2280 	Os_desc		*osp;
2281 	unsigned char	*taddr, *taddr0;
2282 	Sxword		offset;
2283 	int		i;
2284 	Addr		base1;
2285 	unsigned int	stride;
2286 
2287 	osp = ofl->ofl_issunwdata1->is_osdesc;
2288 	base1 = (Addr)(osp->os_shdr->sh_addr +
2289 		ofl->ofl_issunwdata1->is_indata->d_off);
2290 	taddr0 = taddr = osp->os_outdata->d_buf;
2291 	mv = u1;
2292 
2293 	offset = sdp->sd_sym->st_value - base1;
2294 	taddr += offset;
2295 	taddr = taddr + mv->m_poffset;
2296 	for (i = 0; i < mv->m_repeat; i++) {
2297 		/* LINTED */
2298 		DBG_CALL(Dbg_move_expanding(mv, (Addr)(taddr - taddr0)));
2299 		stride = (unsigned int)mv->m_stride + 1;
2300 		/* LINTED */
2301 		switch (ELF_M_SIZE(mv->m_info)) {
2302 		case 1:
2303 			/* LINTED */
2304 			*taddr = (unsigned char)mv->m_value;
2305 			taddr += stride;
2306 			break;
2307 		case 2:
2308 			/* LINTED */
2309 			*((Half *)taddr) = (Half)mv->m_value;
2310 			taddr += 2*stride;
2311 			break;
2312 		case 4:
2313 			/* LINTED */
2314 			*((Word *)taddr) = (Word)mv->m_value;
2315 			taddr += 4*stride;
2316 			break;
2317 		case 8:
2318 			/* LINTED */
2319 			*((unsigned long long *)taddr) =
2320 				mv->m_value;
2321 			taddr += 8*stride;
2322 			break;
2323 		default:
2324 			/*
2325 			 * Should never come here since this is already
2326 			 * checked at sunwmove_preprocess().
2327 			 */
2328 			return (S_ERROR);
2329 		}
2330 	}
2331 	return (1);
2332 }
2333 
2334 /*
2335  * Update Move sections.
2336  */
2337 uintptr_t
2338 update_move(Ofl_desc *ofl)
2339 {
2340 	Word		ndx = 0;
2341 	Is_desc *	isp;
2342 	Word		flags = ofl->ofl_flags;
2343 	Move *		mv1, * mv2;
2344 	Listnode *	lnp1;
2345 	Psym_info *	psym;
2346 
2347 	/*
2348 	 * Determine the index of the symbol table that will be referenced by
2349 	 * the relocation entries.
2350 	 */
2351 	if ((flags & (FLG_OF_DYNAMIC|FLG_OF_RELOBJ)) == FLG_OF_DYNAMIC)
2352 		/* LINTED */
2353 		ndx = (Word) elf_ndxscn(ofl->ofl_osdynsym->os_scn);
2354 	else if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ))
2355 		/* LINTED */
2356 		ndx = (Word) elf_ndxscn(ofl->ofl_ossymtab->os_scn);
2357 
2358 	/*
2359 	 * update sh_link and mv pointer for updating move table.
2360 	 */
2361 	if (ofl->ofl_osmove) {
2362 		ofl->ofl_osmove->os_shdr->sh_link = ndx;
2363 		mv1 = (Move *) ofl->ofl_osmove->os_outdata->d_buf;
2364 	}
2365 
2366 	/*
2367 	 * Update symbol entry index
2368 	 */
2369 	for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) {
2370 		Listnode *	lnp2;
2371 		Mv_itm *	mvp;
2372 		Sym_desc 	*sdp;
2373 
2374 		/*
2375 		 * Expand move table
2376 		 */
2377 		if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN) {
2378 			const char	*s;
2379 
2380 			if (ofl->ofl_flags & FLG_OF_STATIC)
2381 				s = MSG_INTL(MSG_PSYM_EXPREASON1);
2382 			else if (ofl->ofl_flags1 & FLG_OF1_NOPARTI)
2383 				s = MSG_INTL(MSG_PSYM_EXPREASON2);
2384 			else
2385 				s = MSG_INTL(MSG_PSYM_EXPREASON3);
2386 			DBG_CALL(Dbg_move_parexpn(psym->psym_symd->sd_name, s));
2387 			for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvp)) {
2388 				if ((mvp->mv_flag & FLG_MV_OUTSECT) == 0)
2389 					continue;
2390 				mv2 = mvp->mv_ientry;
2391 				sdp = psym->psym_symd;
2392 				DBG_CALL(Dbg_move_mventry(0, mv2, sdp));
2393 				(void) expand_move(ofl, sdp, mv2);
2394 			}
2395 			continue;
2396 		}
2397 
2398 		/*
2399 		 * Process move table
2400 		 */
2401 		DBG_CALL(Dbg_move_outmove((const unsigned char *)
2402 			psym->psym_symd->sd_name));
2403 		for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvp)) {
2404 			int	idx = 1;
2405 			if ((mvp->mv_flag & FLG_MV_OUTSECT) == 0)
2406 				continue;
2407 			isp = mvp->mv_isp;
2408 			mv2 = mvp->mv_ientry;
2409 			sdp = isp->is_file->ifl_oldndx[
2410 				ELF_M_SYM(mv2->m_info)];
2411 
2412 			DBG_CALL(Dbg_move_mventry(0, mv2, sdp));
2413 			*mv1 = *mv2;
2414 			if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) {
2415 				if (ELF_ST_BIND(sdp->sd_sym->st_info) ==
2416 				    STB_LOCAL) {
2417 				    Half	symbssndx =
2418 					ofl->ofl_isbss->is_osdesc->os_scnsymndx;
2419 				    mv1->m_info =
2420 					/* LINTED */
2421 					ELF_M_INFO(symbssndx, mv2->m_info);
2422 				    if (ELF_ST_TYPE(sdp->sd_sym->st_info) !=
2423 				    STT_SECTION) {
2424 					mv1->m_poffset = sdp->sd_sym->st_value -
2425 					ofl->ofl_isbss->
2426 						is_osdesc->os_shdr->sh_addr +
2427 					mv2->m_poffset;
2428 				    }
2429 				} else {
2430 				    mv1->m_info =
2431 					/* LINTED */
2432 					ELF_M_INFO(sdp->sd_symndx, mv2->m_info);
2433 				}
2434 			} else {
2435 				Boolean 	isredloc = FALSE;
2436 
2437 				if ((ELF_ST_BIND(sdp->sd_sym->st_info) ==
2438 				    STB_LOCAL) &&
2439 				    (ofl->ofl_flags1 & FLG_OF1_REDLSYM))
2440 					isredloc = TRUE;
2441 
2442 				if (isredloc && !(sdp->sd_psyminfo)) {
2443 					Word symndx =
2444 					sdp->sd_isc->is_osdesc->os_scnsymndx;
2445 					mv1->m_info =
2446 					/* LINTED */
2447 					ELF_M_INFO(symndx, mv2->m_info);
2448 					mv1->m_poffset += sdp->sd_sym->st_value;
2449 				} else {
2450 					if (isredloc)
2451 					    DBG_CALL(Dbg_syms_reduce(
2452 						DBG_SYM_REDUCE_RETAIN,
2453 						ofl->ofl_ehdr, sdp,
2454 						idx, ofl->ofl_osmove->os_name));
2455 
2456 					mv1->m_info =
2457 					/* LINTED */
2458 					ELF_M_INFO(sdp->sd_symndx, mv2->m_info);
2459 				}
2460 			}
2461 			DBG_CALL(Dbg_move_mventry(1, mv1, sdp));
2462 			mv1++;
2463 			idx++;
2464 		}
2465 	}
2466 	return (1);
2467 }
2468 
2469 
2470 /*
2471  * Scan through the SHT_GROUP output sections.  Update their
2472  * sh_link/sh_info fields as well as the section contents.
2473  */
2474 uintptr_t
2475 update_ogroup(Ofl_desc * ofl)
2476 {
2477 	Listnode	*lnp;
2478 	Os_desc		*osp;
2479 	uintptr_t	error = 0;
2480 
2481 	for (LIST_TRAVERSE(&ofl->ofl_osgroups, lnp, osp)) {
2482 		Is_desc		*isp;
2483 		Ifl_desc	*ifl;
2484 		Shdr		*shdr = osp->os_shdr;
2485 		Sym_desc	*sdp;
2486 		Xword		i, grpcnt;
2487 		Word		*gdata;
2488 
2489 		/*
2490 		 * Since input GROUP sections always create unique
2491 		 * output GROUP sections - we know there is only one
2492 		 * item on the list.
2493 		 */
2494 		isp = (Is_desc *)osp->os_isdescs.head->data;
2495 
2496 		ifl = isp->is_file;
2497 		sdp = ifl->ifl_oldndx[isp->is_shdr->sh_info];
2498 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
2499 		shdr->sh_info = sdp->sd_symndx;
2500 
2501 		/*
2502 		 * Scan through the group data section and update
2503 		 * all of the links to new values.
2504 		 */
2505 		grpcnt = shdr->sh_size / shdr->sh_entsize;
2506 		gdata = (Word *)osp->os_outdata->d_buf;
2507 		for (i = 1; i < grpcnt; i++) {
2508 			Is_desc	*	_isp;
2509 			Os_desc	*	_osp;
2510 
2511 			/*
2512 			 * Perform a sanity check that the section index
2513 			 * stored in the SHT_GROUP section is valid
2514 			 * for the file it came from.
2515 			 */
2516 			if (gdata[i] >= ifl->ifl_shnum) {
2517 				eprintf(ERR_FATAL, MSG_INTL(MSG_GRP_INVALNDX),
2518 					isp->is_name, ifl->ifl_name, i,
2519 					gdata[i]);
2520 				error = S_ERROR;
2521 				gdata[i] = 0;
2522 				continue;
2523 			}
2524 
2525 			_isp = ifl->ifl_isdesc[gdata[i]];
2526 
2527 			/*
2528 			 * If the referenced section didn't make it to the
2529 			 * output file - just zero out the entry.
2530 			 */
2531 			if ((_osp = _isp->is_osdesc) == 0)
2532 				gdata[i] = 0;
2533 			else
2534 				gdata[i] = (Word)elf_ndxscn(_osp->os_scn);
2535 		}
2536 	}
2537 	return (error);
2538 }
2539 
2540 
2541 void
2542 update_ostrtab(Os_desc *osp, Str_tbl *stp)
2543 {
2544 	Elf_Data	*data;
2545 	if (osp == 0)
2546 		return;
2547 
2548 	data = osp->os_outdata;
2549 	assert(data->d_size == st_getstrtab_sz(stp));
2550 	(void) st_setstrbuf(stp, data->d_buf, (uint_t)data->d_size);
2551 }
2552 
2553 /*
2554  * Translate the shdr->sh_{link, info} from its input section value to that
2555  * of the corresponding shdr->sh_{link, info} output section value.
2556  */
2557 Word
2558 translate_link(Os_desc * osp, Word link, const char *msg)
2559 {
2560 	Is_desc *	isp;
2561 	Ifl_desc *	ifl;
2562 
2563 	/*
2564 	 * Don't translate the special section numbers.
2565 	 */
2566 	if (link >= SHN_LORESERVE)
2567 		return (link);
2568 
2569 	/*
2570 	 * Does this output section translate back to an input file.  If not
2571 	 * then there is no translation to do.  In this case we will assume that
2572 	 * if sh_link has a value, it's the right value.
2573 	 */
2574 	isp = (Is_desc *)osp->os_isdescs.head->data;
2575 	if ((ifl = isp->is_file) == NULL)
2576 		return (link);
2577 
2578 	/*
2579 	 * Sanity check to make sure that the sh_{link, info} value
2580 	 * is within range for the input file.
2581 	 */
2582 	if (link >= ifl->ifl_shnum) {
2583 		eprintf(ERR_WARNING, msg, ifl->ifl_name,
2584 		    isp->is_name, EC_XWORD(link));
2585 		return (link);
2586 	}
2587 
2588 	/*
2589 	 * Follow the link to the input section.
2590 	 */
2591 	if ((isp = ifl->ifl_isdesc[link]) == 0)
2592 		return (0);
2593 	if ((osp = isp->is_osdesc) == 0)
2594 		return (0);
2595 
2596 	/* LINTED */
2597 	return ((Word)elf_ndxscn(osp->os_scn));
2598 }
2599 
2600 /*
2601  * Having created all of the necessary sections, segments, and associated
2602  * headers, fill in the program headers and update any other data in the
2603  * output image.  Some general rules:
2604  *
2605  *  o	If an interpretor is required always generate a PT_PHDR entry as
2606  *	well.  It is this entry that triggers the kernel into passing the
2607  *	interpretor an aux vector instead of just a file descriptor.
2608  *
2609  *  o	When generating an image that will be interpreted (ie. a dynamic
2610  *	executable, a shared object, or a static executable that has been
2611  *	provided with an interpretor - weird, but possible), make the initial
2612  *	loadable segment include both the ehdr and phdr[].  Both of these
2613  *	tables are used by the interpretor therefore it seems more intuitive
2614  *	to explicitly defined them as part of the mapped image rather than
2615  *	relying on page rounding by the interpretor to allow their access.
2616  *
2617  *  o	When generating a static image that does not require an interpretor
2618  *	have the first loadable segment indicate the address of the first
2619  *	.section as the start address (things like /kernel/unix and ufsboot
2620  *	expect this behavior).
2621  */
2622 uintptr_t
2623 update_outfile(Ofl_desc *ofl)
2624 {
2625 	Addr		size, etext, vaddr = ofl->ofl_segorigin;
2626 	Listnode	*lnp1, *lnp2;
2627 	Sg_desc		*sgp;
2628 	Os_desc		*osp;
2629 	int		phdrndx = 0, capndx = 0, segndx = -1, secndx;
2630 	Ehdr		*ehdr = ofl->ofl_ehdr;
2631 	List		osecs;
2632 	Shdr		*hshdr;
2633 	Phdr		*_phdr = 0, *dtracephdr = 0;
2634 	Word		phdrsz = ehdr->e_phnum *ehdr->e_phentsize, shscnndx;
2635 	Word		flags = ofl->ofl_flags, ehdrsz = ehdr->e_ehsize;
2636 	Boolean		nobits;
2637 	Off		offset;
2638 
2639 	/*
2640 	 * Loop through the segment descriptors and pick out what we need.
2641 	 */
2642 	DBG_CALL(Dbg_seg_title());
2643 	for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2644 		Phdr *	phdr = &(sgp->sg_phdr);
2645 		Xword 	p_align;
2646 
2647 		segndx++;
2648 
2649 		/*
2650 		 * If an interpreter is required generate a PT_INTERP and
2651 		 * PT_PHDR program header entry.  The PT_PHDR entry describes
2652 		 * the program header table itself.  This information will be
2653 		 * passed via the aux vector to the interpreter (ld.so.1).
2654 		 * The program header array is actually part of the first
2655 		 * loadable segment (and the PT_PHDR entry is the first entry),
2656 		 * therefore its virtual address isn't known until the first
2657 		 * loadable segment is processed.
2658 		 */
2659 		if (phdr->p_type == PT_PHDR) {
2660 			if (ofl->ofl_osinterp) {
2661 				phdr->p_offset = ehdr->e_phoff;
2662 				phdr->p_filesz = phdr->p_memsz = phdrsz;
2663 				DBG_CALL(Dbg_seg_entry(ofl->ofl_e_machine,
2664 				    segndx, sgp));
2665 				ofl->ofl_phdr[phdrndx++] = *phdr;
2666 			}
2667 			continue;
2668 		}
2669 		if (phdr->p_type == PT_INTERP) {
2670 			if (ofl->ofl_osinterp) {
2671 				Shdr *	shdr = ofl->ofl_osinterp->os_shdr;
2672 
2673 				phdr->p_vaddr = phdr->p_memsz = 0;
2674 				phdr->p_offset = shdr->sh_offset;
2675 				phdr->p_filesz = shdr->sh_size;
2676 				DBG_CALL(Dbg_seg_entry(ofl->ofl_e_machine,
2677 				    segndx, sgp));
2678 				ofl->ofl_phdr[phdrndx++] = *phdr;
2679 			}
2680 			continue;
2681 		}
2682 
2683 		/*
2684 		 * If we are creating a PT_SUNWDTRACE segment,
2685 		 * just remember where the program header is.
2686 		 *
2687 		 * It's actual values will be assigned after
2688 		 * update_osym() has completed and the symbol
2689 		 * table addresses have been udpated.
2690 		 */
2691 		if (phdr->p_type == PT_SUNWDTRACE) {
2692 			if ((ofl->ofl_dtracesym) &&
2693 			    ((flags & FLG_OF_RELOBJ) == 0)) {
2694 				dtracephdr = &ofl->ofl_phdr[phdrndx];
2695 				ofl->ofl_phdr[phdrndx++] = *phdr;
2696 			}
2697 			continue;
2698 		}
2699 
2700 		/*
2701 		 * If a hardware/software capabilities section is required,
2702 		 * generate the PT_SUNWCAP header.  Note, as this comes before
2703 		 * the first loadable segment, we don't yet know its real
2704 		 * virtual address.  This is updated later.
2705 		 */
2706 		if (phdr->p_type == PT_SUNWCAP) {
2707 			if (ofl->ofl_oscap) {
2708 				Shdr *	shdr = ofl->ofl_oscap->os_shdr;
2709 
2710 				phdr->p_vaddr = shdr->sh_addr;
2711 				phdr->p_offset = shdr->sh_offset;
2712 				phdr->p_filesz = shdr->sh_size;
2713 				phdr->p_flags = PF_R;
2714 				DBG_CALL(Dbg_seg_entry(ofl->ofl_e_machine,
2715 				    segndx, sgp));
2716 				capndx = phdrndx;
2717 				ofl->ofl_phdr[phdrndx++] = *phdr;
2718 			}
2719 			continue;
2720 		}
2721 
2722 		/*
2723 		 * As the dynamic program header occurs after the loadable
2724 		 * headers in the segment descriptor table, all the address
2725 		 * information for the .dynamic output section will have been
2726 		 * figured out by now.
2727 		 */
2728 		if (phdr->p_type == PT_DYNAMIC) {
2729 			if ((flags & (FLG_OF_DYNAMIC | FLG_OF_RELOBJ)) ==
2730 			    FLG_OF_DYNAMIC) {
2731 				Shdr *	shdr = ofl->ofl_osdynamic->os_shdr;
2732 
2733 				phdr->p_vaddr = shdr->sh_addr;
2734 				phdr->p_offset = shdr->sh_offset;
2735 				phdr->p_filesz = shdr->sh_size;
2736 				phdr->p_flags = M_DATASEG_PERM;
2737 				DBG_CALL(Dbg_seg_entry(ofl->ofl_e_machine,
2738 					segndx, sgp));
2739 				ofl->ofl_phdr[phdrndx++] = *phdr;
2740 			}
2741 			continue;
2742 		}
2743 #if defined(__x86) && defined(_ELF64)
2744 		if (phdr->p_type == PT_SUNW_UNWIND) {
2745 			Shdr	    *shdr;
2746 			if (ofl->ofl_unwindhdr == 0)
2747 				continue;
2748 			shdr = ofl->ofl_unwindhdr->os_shdr;
2749 
2750 			phdr->p_flags = PF_R;
2751 			phdr->p_vaddr = shdr->sh_addr;
2752 			phdr->p_memsz = shdr->sh_size;
2753 			phdr->p_filesz = shdr->sh_size;
2754 			phdr->p_offset = shdr->sh_offset;
2755 			phdr->p_align = shdr->sh_addralign;
2756 			phdr->p_paddr = 0;
2757 			ofl->ofl_phdr[phdrndx++] = *phdr;
2758 			continue;
2759 		}
2760 #endif
2761 		if (phdr->p_type == PT_TLS) {
2762 			Os_desc	*tlsosp;
2763 			Shdr	*firstshdr = 0, *lastfilshdr, *lastmemshdr;
2764 
2765 			if (ofl->ofl_ostlsseg.head == NULL)
2766 				continue;
2767 
2768 			for (LIST_TRAVERSE(&ofl->ofl_ostlsseg, lnp2, tlsosp)) {
2769 				Shdr	*tlsshdr = tlsosp->os_shdr;
2770 
2771 				if (firstshdr == 0) {
2772 					firstshdr = lastfilshdr = lastmemshdr =
2773 					    tlsosp->os_shdr;
2774 					continue;
2775 				}
2776 
2777 				if (tlsshdr->sh_type == SHT_NOBITS)
2778 					lastmemshdr = tlsshdr;
2779 				else
2780 					lastfilshdr = tlsshdr;
2781 			}
2782 
2783 			phdr->p_flags = PF_R | PF_W;
2784 			phdr->p_vaddr = firstshdr->sh_addr;
2785 			phdr->p_offset = firstshdr->sh_offset;
2786 			phdr->p_align = firstshdr->sh_addralign;
2787 			phdr->p_filesz = lastfilshdr->sh_offset +
2788 			    lastfilshdr->sh_size - phdr->p_offset;
2789 			phdr->p_memsz = lastmemshdr->sh_offset +
2790 			    lastmemshdr->sh_size - phdr->p_offset;
2791 
2792 			DBG_CALL(Dbg_seg_entry(ofl->ofl_e_machine,
2793 			    segndx, sgp));
2794 
2795 			ofl->ofl_tlsphdr = phdr;
2796 			ofl->ofl_phdr[phdrndx++] = *phdr;
2797 			continue;
2798 		}
2799 
2800 		/*
2801 		 * If this is an empty segment declaration, it will occur after
2802 		 * all other loadable segments, make sure the previous segment
2803 		 * doesn't overlap. We do not do the check if we are generating
2804 		 * a relocatable file.
2805 		 */
2806 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
2807 		    (sgp->sg_flags & FLG_SG_EMPTY)) {
2808 			int i;
2809 			Addr	v_e;
2810 
2811 			vaddr = phdr->p_vaddr;
2812 			phdr->p_memsz = sgp->sg_length;
2813 			DBG_CALL(Dbg_seg_entry(ofl->ofl_e_machine,
2814 				segndx, sgp));
2815 			ofl->ofl_phdr[phdrndx++] = *phdr;
2816 
2817 			if (phdr->p_type != PT_LOAD)
2818 				continue;
2819 
2820 			v_e = vaddr + phdr->p_memsz;
2821 			/*
2822 			 * Check overlaps
2823 			 */
2824 			for (i = 0; i < phdrndx - 1; i++) {
2825 				Addr 	p_s = (ofl->ofl_phdr[i]).p_vaddr;
2826 				Addr 	p_e;
2827 
2828 				if ((ofl->ofl_phdr[i]).p_type != PT_LOAD)
2829 					continue;
2830 
2831 				p_e = p_s + (ofl->ofl_phdr[i]).p_memsz;
2832 				if (((p_s <= vaddr) && (p_e > vaddr)) ||
2833 				    ((vaddr <= p_s) && (v_e > p_s)))
2834 					eprintf(ERR_WARNING,
2835 					    MSG_INTL(MSG_UPD_SEGOVERLAP),
2836 					    ofl->ofl_name,
2837 					    EC_ADDR(p_e),
2838 					    sgp->sg_name,
2839 					    EC_ADDR(vaddr));
2840 			}
2841 			continue;
2842 		}
2843 
2844 		/*
2845 		 * Having processed any of the special program headers any
2846 		 * remaining headers will be built to express individual
2847 		 * segments.  Segments are only built if they have output
2848 		 * section descriptors associated with them (ie. some form of
2849 		 * input section has been matched to this segment).
2850 		 */
2851 		osecs = sgp->sg_osdescs;
2852 		if (osecs.head == NULL)
2853 			continue;
2854 
2855 		/*
2856 		 * Determine the segments offset and size from the section
2857 		 * information provided from elf_update().
2858 		 * Allow for multiple NOBITS sections.
2859 		 */
2860 		hshdr = ((Os_desc *)osecs.head->data)->os_shdr;
2861 
2862 		phdr->p_filesz = 0;
2863 		phdr->p_memsz = 0;
2864 		phdr->p_offset = offset = hshdr->sh_offset;
2865 		nobits = (hshdr->sh_type == SHT_NOBITS);
2866 		for (LIST_TRAVERSE(&osecs, lnp2, osp)) {
2867 			Shdr *	shdr = osp->os_shdr;
2868 
2869 			p_align = 0;
2870 			if (shdr->sh_addralign > p_align)
2871 				p_align = shdr->sh_addralign;
2872 			offset = (Off)S_ROUND(offset, shdr->sh_addralign);
2873 			offset += shdr->sh_size;
2874 			if (shdr->sh_type != SHT_NOBITS) {
2875 				if (nobits) {
2876 					eprintf(ERR_FATAL,
2877 					    MSG_INTL(MSG_UPD_NOBITS));
2878 					return (S_ERROR);
2879 				}
2880 				phdr->p_filesz = offset - phdr->p_offset;
2881 			} else
2882 				nobits = TRUE;
2883 		}
2884 		phdr->p_memsz = offset - hshdr->sh_offset;
2885 
2886 		/*
2887 		 * If this is PT_SUNWBSS, set alignment
2888 		 */
2889 		if (phdr->p_type == PT_SUNWBSS)
2890 			phdr->p_align = p_align;
2891 
2892 		/*
2893 		 * If this is the first loadable segment of a dynamic object,
2894 		 * or an interpretor has been specified (a static object built
2895 		 * with an interpretor will still be given a PT_HDR entry), then
2896 		 * compensate for the elf header and program header array.  Both
2897 		 * of these are actually part of the loadable segment as they
2898 		 * may be inspected by the interpretor.  Adjust the segments
2899 		 * size and offset accordingly.
2900 		 */
2901 		if ((_phdr == 0) && (phdr->p_type == PT_LOAD) &&
2902 		    ((ofl->ofl_osinterp) || (flags & FLG_OF_DYNAMIC)) &&
2903 		    (!(ofl->ofl_flags1 & FLG_OF1_NOHDR))) {
2904 			size = (Addr)S_ROUND((phdrsz + ehdrsz),
2905 			    hshdr->sh_addralign);
2906 			phdr->p_offset -= size;
2907 			phdr->p_filesz += size;
2908 			phdr->p_memsz += size;
2909 		}
2910 
2911 		/*
2912 		 * If a segment size symbol is required (specified via a
2913 		 * mapfile) update its value.
2914 		 */
2915 		if (sgp->sg_sizesym != NULL)
2916 			sgp->sg_sizesym->sd_sym->st_value = phdr->p_memsz;
2917 
2918 		/*
2919 		 * If no file content has been assigned to this segment (it
2920 		 * only contains no-bits sections), then reset the offset for
2921 		 * consistency.
2922 		 */
2923 		if (phdr->p_filesz == 0)
2924 			phdr->p_offset = 0;
2925 
2926 		/*
2927 		 * If a virtual address has been specified for this segment
2928 		 * (presumably from a map file) use it and make sure the
2929 		 * previous segment does not run into this segment.
2930 		 */
2931 		if ((phdr->p_type == PT_LOAD) ||
2932 		    (phdr->p_type == PT_SUNWBSS)) {
2933 			if ((sgp->sg_flags & FLG_SG_VADDR)) {
2934 				if (_phdr && (vaddr > phdr->p_vaddr) &&
2935 				    (phdr->p_type == PT_LOAD))
2936 					eprintf(ERR_WARNING,
2937 					    MSG_INTL(MSG_UPD_SEGOVERLAP),
2938 					    ofl->ofl_name, EC_ADDR(vaddr),
2939 					    sgp->sg_name,
2940 					    EC_ADDR(phdr->p_vaddr));
2941 				vaddr = phdr->p_vaddr;
2942 				phdr->p_align = 0;
2943 			} else {
2944 				vaddr = phdr->p_vaddr =
2945 				    (Addr)S_ROUND(vaddr, phdr->p_align);
2946 			}
2947 		}
2948 
2949 		/*
2950 		 * Adjust the address offset and p_align if needed.
2951 		 */
2952 		if (!(ofl->ofl_flags1 & (FLG_OF1_NOHDR | FLG_OF1_VADDR))) {
2953 			if (phdr->p_align != 0)
2954 				vaddr += phdr->p_offset % phdr->p_align;
2955 			else
2956 				vaddr += phdr->p_offset;
2957 			phdr->p_vaddr = vaddr;
2958 		}
2959 
2960 		/*
2961 		 * If an interpreter is required set the virtual address of the
2962 		 * PT_PHDR program header now that we know the virtual address
2963 		 * of the loadable segment that contains it.  Update the
2964 		 * PT_SUNWCAP header similarly.
2965 		 */
2966 		if ((_phdr == 0) && (phdr->p_type == PT_LOAD)) {
2967 			_phdr = phdr;
2968 
2969 			if (!(ofl->ofl_flags1 & FLG_OF1_NOHDR)) {
2970 				if (ofl->ofl_osinterp)
2971 					ofl->ofl_phdr[0].p_vaddr =
2972 					    vaddr + ehdrsz;
2973 
2974 				if (ofl->ofl_oscap)
2975 				    ofl->ofl_phdr[capndx].p_vaddr = vaddr +
2976 					ofl->ofl_phdr[capndx].p_offset;
2977 
2978 				/*
2979 				 * Finally, if we're creating a dynamic object
2980 				 * (or a static object in which an interpretor
2981 				 * is specified) update the vaddr to reflect
2982 				 * the address of the first section within this
2983 				 * segment.
2984 				 */
2985 				if ((ofl->ofl_osinterp) ||
2986 				    (flags & FLG_OF_DYNAMIC))
2987 					vaddr += size;
2988 			} else {
2989 				/*
2990 				 * If the FLG_OF1_NOHDR flag was set, PT_PHDR
2991 				 * will not be part of any loadable segment.
2992 				 */
2993 				ofl->ofl_phdr[0].p_vaddr = 0;
2994 				ofl->ofl_phdr[0].p_memsz = 0;
2995 				ofl->ofl_phdr[0].p_flags = 0;
2996 			}
2997 		}
2998 
2999 		/*
3000 		 * Save the address of the first executable section for default
3001 		 * use as the execution entry point.  This may get overridden in
3002 		 * update_oehdr().
3003 		 */
3004 		if (!(flags & FLG_OF_RELOBJ) && !(ehdr->e_entry) &&
3005 		    (phdr->p_flags & PF_X))
3006 			ehdr->e_entry = vaddr;
3007 
3008 		DBG_CALL(Dbg_seg_entry(ofl->ofl_e_machine, segndx, sgp));
3009 
3010 		/*
3011 		 * Traverse the output section descriptors for this segment so
3012 		 * that we can update the section headers addresses.  We've
3013 		 * calculated the virtual address of the initial section within
3014 		 * this segment, so each successive section can be calculated
3015 		 * based on their offsets from each other.
3016 		 */
3017 		secndx = 0;
3018 		hshdr = 0;
3019 		for (LIST_TRAVERSE(&(sgp->sg_osdescs), lnp2, osp)) {
3020 			Shdr *	shdr = osp->os_shdr;
3021 
3022 			if (shdr->sh_link)
3023 			    shdr->sh_link =
3024 				translate_link(osp, shdr->sh_link,
3025 				MSG_INTL(MSG_FIL_INVSHLINK));
3026 
3027 			if (shdr->sh_info && (shdr->sh_flags & SHF_INFO_LINK))
3028 			    shdr->sh_info =
3029 				translate_link(osp, shdr->sh_info,
3030 				MSG_INTL(MSG_FIL_INVSHINFO));
3031 
3032 			if (!(flags & FLG_OF_RELOBJ) &&
3033 			    (phdr->p_type == PT_LOAD) ||
3034 			    (phdr->p_type == PT_SUNWBSS)) {
3035 				if (hshdr)
3036 					vaddr += (shdr->sh_offset -
3037 					    hshdr->sh_offset);
3038 
3039 				shdr->sh_addr = vaddr;
3040 				hshdr = shdr;
3041 			}
3042 
3043 			DBG_CALL(Dbg_seg_os(ofl, osp, secndx));
3044 			secndx++;
3045 		}
3046 
3047 		/*
3048 		 * Establish the virtual address of the end of the last section
3049 		 * in this segment so that the next segments offset can be
3050 		 * calculated from this.
3051 		 */
3052 		if (hshdr)
3053 			vaddr += hshdr->sh_size;
3054 
3055 		/*
3056 		 * Output sections for this segment complete.  Adjust the
3057 		 * virtual offset for the last sections size, and make sure we
3058 		 * haven't exceeded any maximum segment length specification.
3059 		 */
3060 		if ((sgp->sg_length != 0) && (sgp->sg_length < phdr->p_memsz)) {
3061 			eprintf(ERR_FATAL, MSG_INTL(MSG_UPD_LARGSIZE),
3062 			    ofl->ofl_name, sgp->sg_name,
3063 			    EC_XWORD(phdr->p_memsz),
3064 			    EC_XWORD(sgp->sg_length));
3065 			return (S_ERROR);
3066 		}
3067 
3068 		if (phdr->p_type == PT_NOTE) {
3069 			phdr->p_vaddr = 0;
3070 			phdr->p_paddr = 0;
3071 			phdr->p_align = 0;
3072 			phdr->p_memsz = 0;
3073 		}
3074 		if ((phdr->p_type != PT_NULL) && !(flags & FLG_OF_RELOBJ))
3075 			ofl->ofl_phdr[phdrndx++] = *phdr;
3076 	}
3077 
3078 	/*
3079 	 * Update any new output sections.  When building the initial output
3080 	 * image, a number of sections were created but left uninitialized (eg.
3081 	 * .dynsym, .dynstr, .symtab, .symtab, etc.).  Here we update these
3082 	 * sections with the appropriate data.  Other sections may still be
3083 	 * modified via reloc_process().
3084 	 *
3085 	 * Copy the interpretor name into the .interp section.
3086 	 */
3087 	if (ofl->ofl_interp)
3088 		(void) strcpy((char *)ofl->ofl_osinterp->os_outdata->d_buf,
3089 		    ofl->ofl_interp);
3090 
3091 	/*
3092 	 * Update the .shstrtab, .strtab and .dynstr sections.
3093 	 */
3094 	update_ostrtab(ofl->ofl_osshstrtab, ofl->ofl_shdrsttab);
3095 	update_ostrtab(ofl->ofl_osstrtab, ofl->ofl_strtab);
3096 	update_ostrtab(ofl->ofl_osdynstr, ofl->ofl_dynstrtab);
3097 
3098 	/*
3099 	 * Build any output symbol tables, the symbols information is copied
3100 	 * and updated into the new output image.
3101 	 */
3102 	if ((etext = update_osym(ofl)) == (Addr)S_ERROR)
3103 		return (S_ERROR);
3104 
3105 	/*
3106 	 * If we have a PT_SUNWDTRACE phdr, update it now with the address of
3107 	 * the symbol.  It's only now been updated via update_sym().
3108 	 */
3109 	if (dtracephdr && ofl->ofl_dtracesym) {
3110 		Phdr		*pphdr;
3111 		Sym_desc	*sdp = ofl->ofl_dtracesym;
3112 
3113 		dtracephdr->p_vaddr = sdp->sd_sym->st_value;
3114 		dtracephdr->p_memsz = sdp->sd_sym->st_size;
3115 
3116 		/*
3117 		 * Take permisions of the segment the symbol is associated with.
3118 		 */
3119 		pphdr = &sdp->sd_isc->is_osdesc->os_sgdesc->sg_phdr;
3120 		assert(pphdr);
3121 		dtracephdr->p_flags = pphdr->p_flags;
3122 	}
3123 
3124 	/*
3125 	 * Update the GROUP sections.
3126 	 */
3127 	if (update_ogroup(ofl) == S_ERROR)
3128 		return (S_ERROR);
3129 
3130 	/*
3131 	 * Update Move Table.
3132 	 */
3133 	if (ofl->ofl_osmove || ofl->ofl_issunwdata1) {
3134 		if (update_move(ofl) == S_ERROR)
3135 			return (S_ERROR);
3136 	}
3137 
3138 	/*
3139 	 * Build any output headers, version information, dynamic structure and
3140 	 * syminfo structure.
3141 	 */
3142 	if (update_oehdr(ofl) == S_ERROR)
3143 		return (S_ERROR);
3144 	if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == FLG_OF_VERDEF)
3145 		if (update_overdef(ofl) == S_ERROR)
3146 			return (S_ERROR);
3147 	if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
3148 		if (update_overneed(ofl) == S_ERROR)
3149 			return (S_ERROR);
3150 	if (flags & FLG_OF_DYNAMIC) {
3151 		if (update_odynamic(ofl) == S_ERROR)
3152 			return (S_ERROR);
3153 		if (ofl->ofl_ossyminfo)
3154 			if (update_osyminfo(ofl) == S_ERROR)
3155 				return (S_ERROR);
3156 	}
3157 
3158 	/*
3159 	 * Emit Strtab diagnostics.
3160 	 */
3161 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_osshstrtab, ofl->ofl_shdrsttab));
3162 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_osstrtab, ofl->ofl_strtab));
3163 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_osdynstr, ofl->ofl_dynstrtab));
3164 
3165 	/*
3166 	 * Initialize the section headers string table index within the elf
3167 	 * header.
3168 	 */
3169 	/* LINTED */
3170 	if ((shscnndx = elf_ndxscn(ofl->ofl_osshstrtab->os_scn)) <
3171 	    SHN_LORESERVE) {
3172 		ofl->ofl_ehdr->e_shstrndx =
3173 		    /* LINTED */
3174 		    (Half)shscnndx;
3175 	} else {
3176 		/*
3177 		 * If the STRTAB section index doesn't fit into
3178 		 * e_shstrndx, then we store it in 'shdr[0].st_link'.
3179 		 */
3180 		Elf_Scn	*scn;
3181 		Shdr	*shdr0;
3182 		if ((scn = elf_getscn(ofl->ofl_elf, 0)) == NULL) {
3183 			eprintf(ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
3184 				ofl->ofl_name);
3185 			return (S_ERROR);
3186 		}
3187 		if ((shdr0 = elf_getshdr(scn)) == NULL) {
3188 			eprintf(ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
3189 			    ofl->ofl_name);
3190 			return (S_ERROR);
3191 		}
3192 		ofl->ofl_ehdr->e_shstrndx = SHN_XINDEX;
3193 		shdr0->sh_link = shscnndx;
3194 	}
3195 
3196 	return ((uintptr_t)etext);
3197 }
3198