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