xref: /illumos-gate/usr/src/cmd/sgs/libld/common/update.c (revision d6bb6a8465e557cb946ef49d56ed3202f6218652)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  *	Copyright (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * Update the new output file image, perform virtual address, offset and
33  * displacement calculations on the program headers and sections headers,
34  * and generate any new output section information.
35  */
36 #include	<stdio.h>
37 #include	<string.h>
38 #include	<debug.h>
39 #include	"msg.h"
40 #include	"_libld.h"
41 
42 /*
43  * Comparison routine used by qsort() for sorting of the global yymbol list
44  * based off of the hashbuckets the symbol will eventually be deposited in.
45  */
46 static int
47 sym_hash_compare(Sym_s_list * s1, Sym_s_list * s2)
48 {
49 	return (s1->sl_hval - s2->sl_hval);
50 }
51 
52 /*
53  * Build and update any output symbol tables.  Here we work on all the symbol
54  * tables at once to reduce the duplication of symbol and string manipulation.
55  * Symbols and their associated strings are copied from the read-only input
56  * file images to the output image and their values and index's updated in the
57  * output image.
58  */
59 static Addr
60 update_osym(Ofl_desc *ofl)
61 {
62 	Listnode	*lnp1, *lnp2;
63 	Sym_desc	*sdp;
64 	Sym_avlnode	*sav;
65 	Sg_desc		*sgp, *tsgp = 0, *dsgp = 0, *esgp = 0;
66 	Os_desc		*osp;
67 	Ifl_desc	*ifl;
68 	Word		bssndx, etext_ndx, edata_ndx = 0, end_ndx, start_ndx;
69 	Word		end_abs = 0, etext_abs = 0, edata_abs;
70 	Word		tlsbssndx = 0, sunwbssndx = 0, sunwdata1ndx;
71 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
72 	Word		lbssndx = 0;
73 	Addr		lbssaddr = 0;
74 #endif
75 	Addr		bssaddr, etext = 0, edata = 0, end = 0, start = 0;
76 	Addr		tlsbssaddr = 0;
77 	Addr 		sunwbssaddr = 0, sunwdata1addr;
78 	int		start_set = 0;
79 	Sym		_sym = {0}, *sym, *symtab = 0, *dynsym = 0;
80 	Word		symtab_ndx = 0;	/* index into .symtab */
81 	Word		dynsym_ndx = 0;	/* index into .dynsym */
82 	Word		scopesym_ndx = 0; /* index into scoped symbols */
83 	Word		*symndx = 0;	/* Symbol index (for relocation use) */
84 	Word		*symshndx = 0;	/* .symtab_shndx table */
85 	Word		*dynshndx = 0;	/* .dynsym_shndx table */
86 	Str_tbl		*shstrtab;
87 	Str_tbl		*strtab;
88 	Str_tbl		*dynstr;
89 	Word		*hashtab;	/* hash table pointer */
90 	Word		*hashbkt;	/* hash table bucket pointer */
91 	Word		*hashchain;	/* hash table chain pointer */
92 	Word		hashval;	/* value of hash function */
93 	Wk_desc		*wkp;
94 	List		weak = {NULL, NULL};
95 	Word		flags = ofl->ofl_flags;
96 	Word		dtflags_1 = ofl->ofl_dtflags_1;
97 	Versym		*versym;
98 	Gottable	*gottable;	/* used for display got debugging */
99 					/*	information */
100 	Gottable	*_gottable;
101 	Syminfo		*syminfo;
102 	Sym_s_list	*sorted_syms;	/* table to hold sorted symbols */
103 	Word		ssndx;		/* global index into sorted_syms */
104 	Word		scndx;		/* scoped index into sorted_syms */
105 	uint_t		stoff;		/* string offset */
106 
107 	/*
108 	 * Initialize pointers to the symbol table entries and the symbol
109 	 * table strings.  Skip the first symbol entry and the first string
110 	 * table byte.  Note that if we are not generating any output symbol
111 	 * tables we must still generate and update an internal copies so
112 	 * that the relocation phase has the correct information.
113 	 */
114 	if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ) ||
115 	    ((flags & FLG_OF_STATIC) && ofl->ofl_osversym)) {
116 		symtab = (Sym *)ofl->ofl_ossymtab->os_outdata->d_buf;
117 		symtab[symtab_ndx++] = _sym;
118 		if (ofl->ofl_ossymshndx)
119 		    symshndx = (Word *)ofl->ofl_ossymshndx->os_outdata->d_buf;
120 	}
121 	if ((flags & FLG_OF_DYNAMIC) && !(flags & FLG_OF_RELOBJ)) {
122 		dynsym = (Sym *)ofl->ofl_osdynsym->os_outdata->d_buf;
123 		dynsym[dynsym_ndx++] = _sym;
124 		/*
125 		 * Initialize the hash table.
126 		 */
127 		hashtab = (Word *)(ofl->ofl_oshash->os_outdata->d_buf);
128 		hashbkt = &hashtab[2];
129 		hashchain = &hashtab[2 + ofl->ofl_hashbkts];
130 		hashtab[0] = ofl->ofl_hashbkts;
131 		hashtab[1] = ofl->ofl_dynshdrcnt + ofl->ofl_globcnt +
132 		    ofl->ofl_lregsymcnt + 1;
133 		if (ofl->ofl_osdynshndx)
134 		    dynshndx = (Word *)ofl->ofl_osdynshndx->os_outdata->d_buf;
135 	}
136 
137 	/*
138 	 * symndx is the symbol index to be used for relocation processing.  It
139 	 * points to the relevant symtab's (.dynsym or .symtab) symbol ndx.
140 	 */
141 	if (dynsym)
142 		symndx = &dynsym_ndx;
143 	else
144 		symndx = &symtab_ndx;
145 
146 	/*
147 	 * If we have version definitions initialize the version symbol index
148 	 * table.  There is one entry for each symbol which contains the symbols
149 	 * version index.
150 	 */
151 	if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == FLG_OF_VERDEF) {
152 		versym = (Versym *)ofl->ofl_osversym->os_outdata->d_buf;
153 		versym[0] = 0;
154 	} else
155 		versym = 0;
156 
157 	/*
158 	 * If syminfo section exists be prepared to fill it in.
159 	 */
160 	if (ofl->ofl_ossyminfo) {
161 		syminfo = ofl->ofl_ossyminfo->os_outdata->d_buf;
162 		syminfo[0].si_flags = SYMINFO_CURRENT;
163 	} else
164 		syminfo = 0;
165 
166 	/*
167 	 * Setup our string tables.
168 	 */
169 	shstrtab = ofl->ofl_shdrsttab;
170 	strtab = ofl->ofl_strtab;
171 	dynstr = ofl->ofl_dynstrtab;
172 
173 	DBG_CALL(Dbg_syms_sec_title(ofl->ofl_lml));
174 
175 	/*
176 	 * Add the output file name to the first .symtab symbol.
177 	 */
178 	if (symtab) {
179 		(void) st_setstring(strtab, ofl->ofl_name, &stoff);
180 		sym = &symtab[symtab_ndx++];
181 		/* LINTED */
182 		sym->st_name = stoff;
183 		sym->st_value = 0;
184 		sym->st_size = 0;
185 		sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE);
186 		sym->st_other = 0;
187 		sym->st_shndx = SHN_ABS;
188 
189 		if (versym && !dynsym)
190 			versym[1] = 0;
191 	}
192 
193 	/*
194 	 * If we are to display GOT summary information, then allocate
195 	 * the buffer to 'cache' the GOT symbols into now.
196 	 */
197 	if (DBG_ENABLED) {
198 		if ((_gottable = gottable = libld_calloc(ofl->ofl_gotcnt,
199 		    sizeof (Gottable))) == 0)
200 		return ((Addr)S_ERROR);
201 	}
202 
203 	/*
204 	 * Traverse the program headers.  Determine the last executable segment
205 	 * and the last data segment so that we can update etext and edata. If
206 	 * we have empty segments (reservations) record them for setting _end.
207 	 */
208 	for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
209 		Phdr *		phd = &(sgp->sg_phdr);
210 
211 		if (phd->p_type == PT_LOAD) {
212 			if (sgp->sg_osdescs.head != NULL) {
213 			    Word _flags = phd->p_flags & (PF_W | PF_R);
214 			    if (_flags == PF_R) {
215 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
216 				Os_desc * osp = sgp->sg_osdescs.head->data;
217 				Shdr * shdr = osp->os_shdr;
218 				if (((shdr->sh_flags & SHF_AMD64_LARGE)) == 0)
219 				    tsgp = sgp;
220 #else
221 				tsgp = sgp;
222 #endif
223 			    } else if (_flags == (PF_W | PF_R))
224 				dsgp = sgp;
225 			} else if (sgp->sg_flags & FLG_SG_EMPTY)
226 			    esgp = sgp;
227 		}
228 
229 		/*
230 		 * Generate a section symbol for each output section.
231 		 */
232 		for (LIST_TRAVERSE(&(sgp->sg_osdescs), lnp2, osp)) {
233 			Word	sectndx;
234 
235 			sym = &_sym;
236 			sym->st_value = osp->os_shdr->sh_addr;
237 			sym->st_info = ELF_ST_INFO(STB_LOCAL, STT_SECTION);
238 			/* LINTED */
239 			sectndx = elf_ndxscn(osp->os_scn);
240 
241 			if (symtab) {
242 				if (sectndx >= SHN_LORESERVE) {
243 					symshndx[symtab_ndx] = sectndx;
244 					sym->st_shndx = SHN_XINDEX;
245 				} else {
246 					/* LINTED */
247 					sym->st_shndx = (Half)sectndx;
248 				}
249 				symtab[symtab_ndx++] = *sym;
250 			}
251 
252 			if (dynsym && (osp->os_flags & FLG_OS_OUTREL))
253 				dynsym[dynsym_ndx++] = *sym;
254 
255 			if ((dynsym == 0) || (osp->os_flags & FLG_OS_OUTREL)) {
256 				if (versym)
257 					versym[*symndx - 1] = 0;
258 				osp->os_scnsymndx = *symndx - 1;
259 				DBG_CALL(Dbg_syms_sec_entry(ofl->ofl_lml,
260 				    osp->os_scnsymndx, sgp, osp));
261 			}
262 
263 			/*
264 			 * Generate the .shstrtab for this section.
265 			 */
266 			(void) st_setstring(shstrtab, osp->os_name, &stoff);
267 			osp->os_shdr->sh_name = (Word)stoff;
268 
269 			/*
270 			 * Find the section index for our special symbols.
271 			 */
272 			if (sgp == tsgp) {
273 				/* LINTED */
274 				etext_ndx = elf_ndxscn(osp->os_scn);
275 			} else if (dsgp == sgp) {
276 				if (osp->os_shdr->sh_type != SHT_NOBITS) {
277 					/* LINTED */
278 					edata_ndx = elf_ndxscn(osp->os_scn);
279 				}
280 			}
281 
282 			if (start_set == 0) {
283 				start = sgp->sg_phdr.p_vaddr;
284 				/* LINTED */
285 				start_ndx = elf_ndxscn(osp->os_scn);
286 				start_set++;
287 			}
288 		}
289 	}
290 
291 	/*
292 	 * Add local register symbols to the .dynsym.  These are required as
293 	 * DT_REGISTER .dynamic entries must have a symbol to reference.
294 	 */
295 	if (ofl->ofl_regsyms && dynsym) {
296 		int	ndx;
297 
298 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
299 			Sym_desc *	rsdp;
300 
301 			if ((rsdp = ofl->ofl_regsyms[ndx]) == 0)
302 				continue;
303 
304 			if (((rsdp->sd_flags1 & FLG_SY1_LOCL) == 0) &&
305 			    (ELF_ST_BIND(rsdp->sd_sym->st_info) != STB_LOCAL))
306 				continue;
307 
308 			dynsym[dynsym_ndx] = *(rsdp->sd_sym);
309 			rsdp->sd_symndx = *symndx;
310 
311 			if (dynsym[dynsym_ndx].st_name) {
312 				(void) st_setstring(dynstr, rsdp->sd_name,
313 				    &stoff);
314 				dynsym[dynsym_ndx].st_name = stoff;
315 			}
316 			dynsym_ndx++;
317 		}
318 	}
319 
320 	/*
321 	 * Having traversed all the output segments, warn the user if the
322 	 * traditional text or data segments don't exist.  Otherwise from these
323 	 * segments establish the values for `etext', `edata', `end', `END',
324 	 * and `START'.
325 	 */
326 	if (!(flags & FLG_OF_RELOBJ)) {
327 		Sg_desc *	sgp;
328 
329 		if (tsgp)
330 			etext = tsgp->sg_phdr.p_vaddr + tsgp->sg_phdr.p_filesz;
331 		else {
332 			etext = (Addr)0;
333 			etext_ndx = SHN_ABS;
334 			etext_abs = 1;
335 			if (ofl->ofl_flags & FLG_OF_VERBOSE)
336 				eprintf(ofl->ofl_lml, ERR_WARNING,
337 				    MSG_INTL(MSG_UPD_NOREADSEG));
338 		}
339 		if (dsgp) {
340 			edata = dsgp->sg_phdr.p_vaddr + dsgp->sg_phdr.p_filesz;
341 		} else {
342 			edata = (Addr)0;
343 			edata_ndx = SHN_ABS;
344 			edata_abs = 1;
345 			if (ofl->ofl_flags & FLG_OF_VERBOSE)
346 				eprintf(ofl->ofl_lml, ERR_WARNING,
347 				    MSG_INTL(MSG_UPD_NORDWRSEG));
348 		}
349 
350 		if (dsgp == 0) {
351 			if (tsgp)
352 				sgp = tsgp;
353 			else
354 				sgp = 0;
355 		} else if (tsgp == 0)
356 			sgp = dsgp;
357 		else if (dsgp->sg_phdr.p_vaddr > tsgp->sg_phdr.p_vaddr)
358 			sgp = dsgp;
359 		else if (dsgp->sg_phdr.p_vaddr < tsgp->sg_phdr.p_vaddr)
360 			sgp = tsgp;
361 		else {
362 			/*
363 			 * One of the segments must be of zero size.
364 			 */
365 			if (tsgp->sg_phdr.p_memsz)
366 				sgp = tsgp;
367 			else
368 				sgp = dsgp;
369 		}
370 
371 		if (esgp && (esgp->sg_phdr.p_vaddr > sgp->sg_phdr.p_vaddr))
372 			sgp = esgp;
373 
374 		if (sgp) {
375 			end = sgp->sg_phdr.p_vaddr + sgp->sg_phdr.p_memsz;
376 
377 			/*
378 			 * If we're dealing with a memory reservation there are
379 			 * no sections to establish an index for _end, so assign
380 			 * it as an absolute.
381 			 */
382 			if (sgp->sg_osdescs.tail) {
383 				Os_desc *	tosp;
384 
385 				tosp = (Os_desc *)sgp->sg_osdescs.tail->data;
386 				/* LINTED */
387 				end_ndx = elf_ndxscn(tosp->os_scn);
388 			} else {
389 				end_ndx = SHN_ABS;
390 				end_abs = 1;
391 			}
392 		} else {
393 			end = (Addr) 0;
394 			end_ndx = SHN_ABS;
395 			end_abs = 1;
396 			eprintf(ofl->ofl_lml, ERR_WARNING,
397 			    MSG_INTL(MSG_UPD_NOSEG));
398 		}
399 	}
400 
401 	DBG_CALL(Dbg_syms_up_title(ofl->ofl_lml));
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 (ld_assign_got(ofl, 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_ENABLED) {
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 (ld_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 = ld_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 (ld_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 = ld_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 (ld_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, sdp));
705 			DBG_CALL(Dbg_syms_ignore(ofl, 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 (ld_assign_got(ofl, 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_ENABLED) {
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, 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(ofl->ofl_lml, ERR_WARNING,
1217 				    MSG_INTL(MSG_SYM_NOTNULL),
1218 				    demangle(name), sdp->sd_file->ifl_name);
1219 			}
1220 
1221 			/*
1222 			 * Undefined weak global, if we are generating a static
1223 			 * executable, output as an absolute zero.  Otherwise
1224 			 * leave it as is, ld.so.1 will skip symbols of this
1225 			 * type (this technique allows applications and
1226 			 * libraries to test for the existence of a symbol as an
1227 			 * indication of the presence or absence of certain
1228 			 * functionality).
1229 			 */
1230 			if (((flags & (FLG_OF_STATIC | FLG_OF_EXEC)) ==
1231 			    (FLG_OF_STATIC | FLG_OF_EXEC)) &&
1232 			    (ELF_ST_BIND(sym->st_info) == STB_WEAK)) {
1233 				sdp->sd_flags |= FLG_SY_SPECSEC;
1234 				sdp->sd_shndx = sectndx = SHN_ABS;
1235 			}
1236 		} else if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1237 		    (sectndx == SHN_COMMON)) {
1238 			/* COMMONs have already been processed */
1239 			/* EMPTY */
1240 			;
1241 		} else {
1242 			if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
1243 			    (sectndx == SHN_ABS))
1244 				spec = sdp->sd_aux->sa_symspec;
1245 
1246 			/* LINTED */
1247 			if (sdp->sd_flags & FLG_SY_COMMEXP) {
1248 				/*
1249 				 * This is (or was) a COMMON symbol which was
1250 				 * processed above - no processing
1251 				 * required here.
1252 				 */
1253 				;
1254 			} else if (sdp->sd_ref == REF_DYN_NEED) {
1255 				unsigned char	type, bind;
1256 
1257 				sectndx = SHN_UNDEF;
1258 				sym->st_value = 0;
1259 				sym->st_size = 0;
1260 
1261 				/*
1262 				 * Make sure this undefined symbol is returned
1263 				 * to the same binding as was defined in the
1264 				 * original relocatable object reference.
1265 				 */
1266 				type = ELF_ST_TYPE(sym-> st_info);
1267 				if (sdp->sd_flags & FLG_SY_GLOBREF)
1268 					bind = STB_GLOBAL;
1269 				else
1270 					bind = STB_WEAK;
1271 
1272 				sym->st_info = ELF_ST_INFO(bind, type);
1273 
1274 			} else if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
1275 			    (sdp->sd_ref == REF_REL_NEED)) {
1276 				osp = sdp->sd_isc->is_osdesc;
1277 				/* LINTED */
1278 				sectndx = elf_ndxscn(osp->os_scn);
1279 
1280 				/*
1281 				 * In an executable, the new symbol value is the
1282 				 * old value (offset into defining section) plus
1283 				 * virtual address of defining section.  In a
1284 				 * relocatable, the new value is the old value
1285 				 * plus the displacement of the section within
1286 				 * the file.
1287 				 */
1288 				/* LINTED */
1289 				sym->st_value +=
1290 				    (Off)_elf_getxoff(sdp->sd_isc->is_indata);
1291 
1292 				if (!(flags & FLG_OF_RELOBJ)) {
1293 					sym->st_value += osp->os_shdr->sh_addr;
1294 					/*
1295 					 * TLS symbols are relative to
1296 					 * the TLS segment.
1297 					 */
1298 					if ((ELF_ST_TYPE(sym->st_info) ==
1299 					    STT_TLS) && (ofl->ofl_tlsphdr))
1300 						sym->st_value -=
1301 						    ofl->ofl_tlsphdr->p_vaddr;
1302 				}
1303 			}
1304 		}
1305 
1306 		if (spec) {
1307 			switch (spec) {
1308 			case SDAUX_ID_ETEXT:
1309 				sym->st_value = etext;
1310 				sectndx = etext_ndx;
1311 				if (etext_abs)
1312 					sdp->sd_flags |= FLG_SY_SPECSEC;
1313 				else
1314 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1315 				break;
1316 			case SDAUX_ID_EDATA:
1317 				sym->st_value = edata;
1318 				sectndx = edata_ndx;
1319 				if (edata_abs)
1320 					sdp->sd_flags |= FLG_SY_SPECSEC;
1321 				else
1322 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1323 				break;
1324 			case SDAUX_ID_END:
1325 				sym->st_value = end;
1326 				sectndx = end_ndx;
1327 				if (end_abs)
1328 					sdp->sd_flags |= FLG_SY_SPECSEC;
1329 				else
1330 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1331 				break;
1332 			case SDAUX_ID_START:
1333 				sym->st_value = start;
1334 				sectndx = start_ndx;
1335 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1336 				break;
1337 			case SDAUX_ID_DYN:
1338 				if (flags & FLG_OF_DYNAMIC) {
1339 					sym->st_value = ofl->
1340 					    ofl_osdynamic->os_shdr->sh_addr;
1341 					/* LINTED */
1342 					sectndx = elf_ndxscn(
1343 					    ofl->ofl_osdynamic->os_scn);
1344 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1345 				}
1346 				break;
1347 			case SDAUX_ID_PLT:
1348 				if (ofl->ofl_osplt) {
1349 					sym->st_value = ofl->
1350 					    ofl_osplt->os_shdr->sh_addr;
1351 					/* LINTED */
1352 					sectndx = elf_ndxscn(
1353 					    ofl->ofl_osplt->os_scn);
1354 					sdp->sd_flags &= ~FLG_SY_SPECSEC;
1355 				}
1356 				break;
1357 			case SDAUX_ID_GOT:
1358 				/*
1359 				 * Symbol bias for negative growing tables is
1360 				 * stored in symbol's value during
1361 				 * allocate_got().
1362 				 */
1363 				sym->st_value += ofl->
1364 				    ofl_osgot->os_shdr->sh_addr;
1365 				/* LINTED */
1366 				sectndx = elf_ndxscn(ofl->
1367 				    ofl_osgot->os_scn);
1368 				sdp->sd_flags &= ~FLG_SY_SPECSEC;
1369 				break;
1370 			default:
1371 				/* NOTHING */
1372 				;
1373 			}
1374 		}
1375 
1376 		/*
1377 		 * If a plt index has been assigned to an undefined function,
1378 		 * update the symbols value to the appropriate .plt address.
1379 		 */
1380 		if ((flags & FLG_OF_DYNAMIC) && (flags & FLG_OF_EXEC) &&
1381 		    (sdp->sd_file) &&
1382 		    (sdp->sd_file->ifl_ehdr->e_type == ET_DYN) &&
1383 		    (ELF_ST_TYPE(sym->st_info) == STT_FUNC) &&
1384 		    !(flags & FLG_OF_BFLAG)) {
1385 			if (sap->sa_PLTndx)
1386 				sym->st_value = ld_calc_plt_addr(sdp, ofl);
1387 		}
1388 
1389 		/*
1390 		 * Finish updating the symbols.
1391 		 */
1392 
1393 		/*
1394 		 * Sym Update: if scoped local - set local binding
1395 		 */
1396 		if (local)
1397 			sym->st_info = ELF_ST_INFO(STB_LOCAL,
1398 			    ELF_ST_TYPE(sym->st_info));
1399 
1400 		/*
1401 		 * Sym Updated: If both the .symtab and .dynsym
1402 		 * are present then we've actually updated the information in
1403 		 * the .dynsym, therefore copy this same information to the
1404 		 * .symtab entry.
1405 		 */
1406 		sdp->sd_shndx = sectndx;
1407 		if (enter_in_symtab && dynsym && !local) {
1408 			symtab[symtab_ndx].st_value = sym->st_value;
1409 			symtab[symtab_ndx].st_size = sym->st_size;
1410 			symtab[symtab_ndx].st_info = sym->st_info;
1411 			symtab[symtab_ndx].st_other = sym->st_other;
1412 		}
1413 
1414 
1415 		if (enter_in_symtab) {
1416 			Word	_symndx;
1417 
1418 			if (local)
1419 				_symndx = scopesym_ndx++;
1420 			else
1421 				_symndx = symtab_ndx++;
1422 			if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
1423 			    (sectndx >= SHN_LORESERVE)) {
1424 				assert(symshndx != 0);
1425 				symshndx[_symndx] = sectndx;
1426 				symtab[_symndx].st_shndx = SHN_XINDEX;
1427 			} else {
1428 				/* LINTED */
1429 				symtab[_symndx].st_shndx = (Half)sectndx;
1430 			}
1431 		}
1432 
1433 		if (dynsym && !local) {
1434 			if (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) &&
1435 			    (sectndx >= SHN_LORESERVE)) {
1436 				assert(dynshndx != 0);
1437 				dynshndx[dynsym_ndx] = sectndx;
1438 				dynsym[dynsym_ndx].st_shndx = SHN_XINDEX;
1439 			} else {
1440 				/* LINTED */
1441 				dynsym[dynsym_ndx].st_shndx = (Half)sectndx;
1442 			}
1443 			dynsym_ndx++;
1444 		}
1445 
1446 		DBG_CALL(Dbg_syms_new(ofl, sym, sdp));
1447 	}
1448 
1449 	/*
1450 	 * Now that all the symbols have been processed update any weak symbols
1451 	 * information (ie. copy all information except `st_name').  As both
1452 	 * symbols will be represented in the output, return the weak symbol to
1453 	 * its correct type.
1454 	 */
1455 	for (LIST_TRAVERSE(&weak, lnp1, wkp)) {
1456 		Sym_desc *	sdp, * _sdp;
1457 		Sym *		sym, * _sym, * __sym;
1458 		unsigned char	bind;
1459 
1460 		sdp = wkp->wk_weak;
1461 		_sdp = wkp->wk_alias;
1462 		_sym = _sdp->sd_sym;
1463 
1464 		sdp->sd_flags |= FLG_SY_WEAKDEF;
1465 
1466 		/*
1467 		 * If the symbol definition has been scoped then assign it to
1468 		 * be local, otherwise if it's from a shared object then we need
1469 		 * to maintain the binding of the original reference.
1470 		 */
1471 		if (sdp->sd_flags1 & FLG_SY1_LOCL) {
1472 			if (flags & FLG_OF_PROCRED)
1473 				bind = STB_LOCAL;
1474 			else
1475 				bind = STB_WEAK;
1476 		} else if ((sdp->sd_ref == REF_DYN_NEED) &&
1477 		    (sdp->sd_flags & FLG_SY_GLOBREF))
1478 			bind = STB_GLOBAL;
1479 		else
1480 			bind = STB_WEAK;
1481 
1482 		DBG_CALL(Dbg_syms_old(ofl, sdp));
1483 		if ((sym = wkp->wk_symtab) != 0) {
1484 			sym = wkp->wk_symtab;
1485 			sym->st_value = _sym->st_value;
1486 			sym->st_size = _sym->st_size;
1487 			sym->st_other = _sym->st_other;
1488 			sym->st_shndx = _sym->st_shndx;
1489 			sym->st_info = ELF_ST_INFO(bind,
1490 			    ELF_ST_TYPE(sym->st_info));
1491 			__sym = sym;
1492 		}
1493 		if ((sym = wkp->wk_dynsym) != 0) {
1494 			sym = wkp->wk_dynsym;
1495 			sym->st_value = _sym->st_value;
1496 			sym->st_size = _sym->st_size;
1497 			sym->st_other = _sym->st_other;
1498 			sym->st_shndx = _sym->st_shndx;
1499 			sym->st_info = ELF_ST_INFO(bind,
1500 			    ELF_ST_TYPE(sym->st_info));
1501 			__sym = sym;
1502 		}
1503 		DBG_CALL(Dbg_syms_new(ofl, __sym, sdp));
1504 	}
1505 
1506 	/*
1507 	 * Now display GOT debugging information if required.
1508 	 */
1509 	DBG_CALL(Dbg_got_display(ofl, gottable));
1510 
1511 	/*
1512 	 * Update the section headers information.
1513 	 */
1514 	if (symtab) {
1515 		Shdr *	shdr = ofl->ofl_ossymtab->os_shdr;
1516 
1517 		shdr->sh_info = ofl->ofl_shdrcnt + ofl->ofl_locscnt +
1518 			ofl->ofl_scopecnt + 2;
1519 		/* LINTED */
1520 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osstrtab->os_scn);
1521 		if (symshndx) {
1522 			shdr = ofl->ofl_ossymshndx->os_shdr;
1523 			shdr->sh_link =
1524 				(Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
1525 		}
1526 	}
1527 	if (dynsym) {
1528 		Shdr *	shdr = ofl->ofl_osdynsym->os_shdr;
1529 
1530 		shdr->sh_info = ofl->ofl_dynshdrcnt + ofl->ofl_lregsymcnt + 1;
1531 		/* LINTED */
1532 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
1533 
1534 		ofl->ofl_oshash->os_shdr->sh_link =
1535 		    /* LINTED */
1536 		    (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
1537 		if (dynshndx) {
1538 			shdr = ofl->ofl_osdynshndx->os_shdr;
1539 			shdr->sh_link =
1540 				(Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn);
1541 		}
1542 	}
1543 
1544 	/*
1545 	 * Used by ld.so.1 only.
1546 	 */
1547 	return (etext);
1548 }
1549 
1550 /*
1551  * Build the dynamic section.
1552  */
1553 static int
1554 update_odynamic(Ofl_desc *ofl)
1555 {
1556 	Listnode	*lnp;
1557 	Ifl_desc	*ifl;
1558 	Sym_desc	*sdp;
1559 	Shdr		*shdr;
1560 	Dyn		*_dyn = (Dyn *)ofl->ofl_osdynamic->os_outdata->d_buf;
1561 	Dyn		*dyn;
1562 	Str_tbl		*dynstr;
1563 	uint_t		stoff;
1564 	Word		flags = ofl->ofl_flags;
1565 
1566 	dynstr = ofl->ofl_dynstrtab;
1567 	ofl->ofl_osdynamic->os_shdr->sh_link =
1568 	    /* LINTED */
1569 	    (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
1570 
1571 	dyn = _dyn;
1572 
1573 	for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, ifl)) {
1574 		if ((ifl->ifl_flags &
1575 		    (FLG_IF_IGNORE | FLG_IF_DEPREQD)) == FLG_IF_IGNORE)
1576 			continue;
1577 
1578 		/*
1579 		 * Create and set up the DT_POSFLAG_1 entry here if required.
1580 		 */
1581 		if ((ifl->ifl_flags & (FLG_IF_LAZYLD|FLG_IF_GRPPRM)) &&
1582 		    (ifl->ifl_flags & (FLG_IF_NEEDED))) {
1583 			dyn->d_tag = DT_POSFLAG_1;
1584 			if (ifl->ifl_flags & FLG_IF_LAZYLD)
1585 				dyn->d_un.d_val = DF_P1_LAZYLOAD;
1586 			if (ifl->ifl_flags & FLG_IF_GRPPRM)
1587 				dyn->d_un.d_val |= DF_P1_GROUPPERM;
1588 			dyn++;
1589 		}
1590 
1591 		if (ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NEEDSTR))
1592 			dyn->d_tag = DT_NEEDED;
1593 		else
1594 			continue;
1595 
1596 		(void) st_setstring(dynstr, ifl->ifl_soname, &stoff);
1597 		dyn->d_un.d_val = stoff;
1598 		/* LINTED */
1599 		ifl->ifl_neededndx = (Half)(((uintptr_t)dyn - (uintptr_t)_dyn) /
1600 		    sizeof (Dyn));
1601 		dyn++;
1602 	}
1603 
1604 	if (ofl->ofl_dtsfltrs) {
1605 		Dfltr_desc *	dftp;
1606 		Aliste		off;
1607 
1608 		for (ALIST_TRAVERSE(ofl->ofl_dtsfltrs, off, dftp)) {
1609 			if (dftp->dft_flag == FLG_SY_AUXFLTR)
1610 				dyn->d_tag = DT_SUNW_AUXILIARY;
1611 			else
1612 				dyn->d_tag = DT_SUNW_FILTER;
1613 
1614 			(void) st_setstring(dynstr, dftp->dft_str, &stoff);
1615 			dyn->d_un.d_val = stoff;
1616 			dftp->dft_ndx = (Half)(((uintptr_t)dyn -
1617 			    (uintptr_t)_dyn) / sizeof (Dyn));
1618 			dyn++;
1619 		}
1620 	}
1621 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_INIT_U),
1622 	    SYM_NOHASH, 0, ofl)) != NULL) &&
1623 		sdp->sd_ref == REF_REL_NEED) {
1624 		dyn->d_tag = DT_INIT;
1625 		dyn->d_un.d_ptr = sdp->sd_sym->st_value;
1626 		dyn++;
1627 	}
1628 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_FINI_U),
1629 	    SYM_NOHASH, 0, ofl)) != NULL) &&
1630 		sdp->sd_ref == REF_REL_NEED) {
1631 		dyn->d_tag = DT_FINI;
1632 		dyn->d_un.d_ptr = sdp->sd_sym->st_value;
1633 		dyn++;
1634 	}
1635 	if (ofl->ofl_soname) {
1636 		dyn->d_tag = DT_SONAME;
1637 		(void) st_setstring(dynstr, ofl->ofl_soname, &stoff);
1638 		dyn->d_un.d_val = stoff;
1639 		dyn++;
1640 	}
1641 	if (ofl->ofl_filtees) {
1642 		if (flags & FLG_OF_AUX) {
1643 			dyn->d_tag = DT_AUXILIARY;
1644 		} else {
1645 			dyn->d_tag = DT_FILTER;
1646 		}
1647 		(void) st_setstring(dynstr, ofl->ofl_filtees, &stoff);
1648 		dyn->d_un.d_val = stoff;
1649 		dyn++;
1650 	}
1651 	if (ofl->ofl_rpath) {
1652 		(void) st_setstring(dynstr, ofl->ofl_rpath, &stoff);
1653 		dyn->d_tag = DT_RUNPATH;
1654 		dyn->d_un.d_val = stoff;
1655 		dyn++;
1656 		dyn->d_tag = DT_RPATH;
1657 		dyn->d_un.d_val = stoff;
1658 		dyn++;
1659 	}
1660 	if (ofl->ofl_config) {
1661 		dyn->d_tag = DT_CONFIG;
1662 		(void) st_setstring(dynstr, ofl->ofl_config, &stoff);
1663 		dyn->d_un.d_val = stoff;
1664 		dyn++;
1665 	}
1666 	if (ofl->ofl_depaudit) {
1667 		dyn->d_tag = DT_DEPAUDIT;
1668 		(void) st_setstring(dynstr, ofl->ofl_depaudit, &stoff);
1669 		dyn->d_un.d_val = stoff;
1670 		dyn++;
1671 	}
1672 	if (ofl->ofl_audit) {
1673 		dyn->d_tag = DT_AUDIT;
1674 		(void) st_setstring(dynstr, ofl->ofl_audit, &stoff);
1675 		dyn->d_un.d_val = stoff;
1676 		dyn++;
1677 	}
1678 
1679 	/*
1680 	 * The following DT_* entries do not apply to relocatable objects.
1681 	 */
1682 	if (!(flags & FLG_OF_RELOBJ)) {
1683 
1684 		dyn->d_tag = DT_HASH;
1685 		dyn->d_un.d_ptr = ofl->ofl_oshash->os_shdr->sh_addr;
1686 		dyn++;
1687 
1688 		shdr = ofl->ofl_osdynstr->os_shdr;
1689 		dyn->d_tag = DT_STRTAB;
1690 		dyn->d_un.d_ptr = shdr->sh_addr;
1691 		dyn++;
1692 
1693 		dyn->d_tag = DT_STRSZ;
1694 		dyn->d_un.d_ptr = shdr->sh_size;
1695 		dyn++;
1696 
1697 		shdr = ofl->ofl_osdynsym->os_shdr;
1698 		dyn->d_tag = DT_SYMTAB;
1699 		dyn->d_un.d_ptr = shdr->sh_addr;
1700 		dyn++;
1701 
1702 		dyn->d_tag = DT_SYMENT;
1703 		dyn->d_un.d_ptr = shdr->sh_entsize;
1704 		dyn++;
1705 
1706 		/*
1707 		 * Reserve the DT_CHECKSUM entry.  Its value will be filled in
1708 		 * after the complete image is built.
1709 		 */
1710 		dyn->d_tag = DT_CHECKSUM;
1711 		ofl->ofl_checksum = &dyn->d_un.d_val;
1712 		dyn++;
1713 
1714 		if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) ==
1715 		    FLG_OF_VERDEF) {
1716 			shdr = ofl->ofl_osverdef->os_shdr;
1717 			dyn->d_tag = DT_VERDEF;
1718 			dyn->d_un.d_ptr = shdr->sh_addr;
1719 			dyn++;
1720 			dyn->d_tag = DT_VERDEFNUM;
1721 			dyn->d_un.d_ptr = shdr->sh_info;
1722 			dyn++;
1723 		}
1724 		if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) ==
1725 		    FLG_OF_VERNEED) {
1726 			shdr = ofl->ofl_osverneed->os_shdr;
1727 			dyn->d_tag = DT_VERNEED;
1728 			dyn->d_un.d_ptr = shdr->sh_addr;
1729 			dyn++;
1730 			dyn->d_tag = DT_VERNEEDNUM;
1731 			dyn->d_un.d_ptr = shdr->sh_info;
1732 			dyn++;
1733 		}
1734 		if ((ofl->ofl_flags1 & FLG_OF1_RELCNT) &&
1735 		    ofl->ofl_relocrelcnt) {
1736 			dyn->d_tag = M_REL_DT_COUNT;
1737 			dyn->d_un.d_val = ofl->ofl_relocrelcnt;
1738 			dyn++;
1739 		}
1740 		if (flags & FLG_OF_TEXTREL) {
1741 			/*
1742 			 * Only the presence of this entry is used in this
1743 			 * implementation, not the value stored.
1744 			 */
1745 			dyn->d_tag = DT_TEXTREL;
1746 			dyn->d_un.d_val = 0;
1747 			dyn++;
1748 		}
1749 
1750 		if (ofl->ofl_osfiniarray) {
1751 			shdr = ofl->ofl_osfiniarray->os_shdr;
1752 
1753 			dyn->d_tag = DT_FINI_ARRAY;
1754 			dyn->d_un.d_ptr = shdr->sh_addr;
1755 			dyn++;
1756 
1757 			dyn->d_tag = DT_FINI_ARRAYSZ;
1758 			dyn->d_un.d_val = shdr->sh_size;
1759 			dyn++;
1760 		}
1761 
1762 		if (ofl->ofl_osinitarray) {
1763 			shdr = ofl->ofl_osinitarray->os_shdr;
1764 
1765 			dyn->d_tag = DT_INIT_ARRAY;
1766 			dyn->d_un.d_ptr = shdr->sh_addr;
1767 			dyn++;
1768 
1769 			dyn->d_tag = DT_INIT_ARRAYSZ;
1770 			dyn->d_un.d_val = shdr->sh_size;
1771 			dyn++;
1772 		}
1773 
1774 		if (ofl->ofl_ospreinitarray) {
1775 			shdr = ofl->ofl_ospreinitarray->os_shdr;
1776 
1777 			dyn->d_tag = DT_PREINIT_ARRAY;
1778 			dyn->d_un.d_ptr = shdr->sh_addr;
1779 			dyn++;
1780 
1781 			dyn->d_tag = DT_PREINIT_ARRAYSZ;
1782 			dyn->d_un.d_val = shdr->sh_size;
1783 			dyn++;
1784 		}
1785 
1786 		if (ofl->ofl_pltcnt) {
1787 			shdr =  ofl->ofl_osplt->os_relosdesc->os_shdr;
1788 
1789 			dyn->d_tag = DT_PLTRELSZ;
1790 			dyn->d_un.d_ptr = shdr->sh_size;
1791 			dyn++;
1792 			dyn->d_tag = DT_PLTREL;
1793 			dyn->d_un.d_ptr = M_REL_DT_TYPE;
1794 			dyn++;
1795 			dyn->d_tag = DT_JMPREL;
1796 			dyn->d_un.d_ptr = shdr->sh_addr;
1797 			dyn++;
1798 		}
1799 		if (ofl->ofl_pltpad) {
1800 			shdr =  ofl->ofl_osplt->os_shdr;
1801 
1802 			dyn->d_tag = DT_PLTPAD;
1803 			if (ofl->ofl_pltcnt)
1804 				dyn->d_un.d_ptr = shdr->sh_addr +
1805 					M_PLT_RESERVSZ +
1806 					ofl->ofl_pltcnt * M_PLT_ENTSIZE;
1807 			else
1808 				dyn->d_un.d_ptr = shdr->sh_addr;
1809 			dyn++;
1810 			dyn->d_tag = DT_PLTPADSZ;
1811 			dyn->d_un.d_val = ofl->ofl_pltpad *
1812 				M_PLT_ENTSIZE;
1813 			dyn++;
1814 		}
1815 		if (ofl->ofl_relocsz) {
1816 			dyn->d_tag = M_REL_DT_TYPE;
1817 			dyn->d_un.d_ptr = ofl->ofl_osrelhead->os_shdr->sh_addr;
1818 			dyn++;
1819 			dyn->d_tag = M_REL_DT_SIZE;
1820 			dyn->d_un.d_ptr = ofl->ofl_relocsz;
1821 			dyn++;
1822 			dyn->d_tag = M_REL_DT_ENT;
1823 			if (ofl->ofl_osrelhead->os_shdr->sh_type == SHT_REL)
1824 				dyn->d_un.d_ptr = sizeof (Rel);
1825 			else
1826 				dyn->d_un.d_ptr = sizeof (Rela);
1827 			dyn++;
1828 		}
1829 		if (ofl->ofl_ossyminfo) {
1830 			shdr = ofl->ofl_ossyminfo->os_shdr;
1831 			dyn->d_tag = DT_SYMINFO;
1832 			dyn->d_un.d_ptr = shdr->sh_addr;
1833 			dyn++;
1834 			dyn->d_tag = DT_SYMINSZ;
1835 			dyn->d_un.d_val = shdr->sh_size;
1836 			dyn++;
1837 			dyn->d_tag = DT_SYMINENT;
1838 			dyn->d_un.d_val = sizeof (Syminfo);
1839 			dyn++;
1840 		}
1841 		if (ofl->ofl_osmove) {
1842 			Os_desc *	osp;
1843 
1844 			dyn->d_tag = DT_MOVEENT;
1845 			osp = ofl->ofl_osmove;
1846 			dyn->d_un.d_val = osp->os_shdr->sh_entsize;
1847 			dyn++;
1848 			dyn->d_tag = DT_MOVESZ;
1849 			dyn->d_un.d_val = osp->os_shdr->sh_size;
1850 			dyn++;
1851 			dyn->d_tag = DT_MOVETAB;
1852 			dyn->d_un.d_val = osp->os_shdr->sh_addr;
1853 			dyn++;
1854 		}
1855 		if (ofl->ofl_regsymcnt) {
1856 			int	ndx;
1857 
1858 			for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1859 				if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1860 					continue;
1861 
1862 				dyn->d_tag = M_DT_REGISTER;
1863 				dyn->d_un.d_val = sdp->sd_symndx;
1864 				dyn++;
1865 			}
1866 		}
1867 
1868 		for (LIST_TRAVERSE(&ofl->ofl_rtldinfo, lnp, sdp)) {
1869 			dyn->d_tag = DT_SUNW_RTLDINF;
1870 			dyn->d_un.d_ptr = sdp->sd_sym->st_value;
1871 			dyn++;
1872 		}
1873 
1874 		if (ofl->ofl_osdynamic->os_sgdesc &&
1875 		    (ofl->ofl_osdynamic->os_sgdesc->sg_phdr.p_flags & PF_W)) {
1876 			if (ofl->ofl_osinterp) {
1877 				dyn->d_tag = DT_DEBUG;
1878 				dyn->d_un.d_ptr = 0;
1879 				dyn++;
1880 			}
1881 
1882 			dyn->d_tag = DT_FEATURE_1;
1883 			if (ofl->ofl_osmove)
1884 				dyn->d_un.d_val = 0;
1885 			else
1886 				dyn->d_un.d_val = DTF_1_PARINIT;
1887 			dyn++;
1888 		}
1889 
1890 		if (ofl->ofl_oscap) {
1891 			dyn->d_tag = DT_SUNW_CAP;
1892 			dyn->d_un.d_val = ofl->ofl_oscap->os_shdr->sh_addr;
1893 			dyn++;
1894 		}
1895 	}
1896 
1897 	if (flags & FLG_OF_SYMBOLIC) {
1898 		dyn->d_tag = DT_SYMBOLIC;
1899 		dyn->d_un.d_val = 0;
1900 		dyn++;
1901 	}
1902 	dyn->d_tag = DT_FLAGS;
1903 	dyn->d_un.d_val = ofl->ofl_dtflags;
1904 	dyn++;
1905 
1906 	/*
1907 	 * If -Bdirect was specified, but some NODIRECT symbols were specified
1908 	 * via a mapfile, or -znodirect was used on the command line, then
1909 	 * clear the DF_1_DIRECT flag.  The resultant object will use per-symbol
1910 	 * direct bindings rather than be enabled for global direct bindings.
1911 	 */
1912 	if (ofl->ofl_flags1 & FLG_OF1_NDIRECT)
1913 		ofl->ofl_dtflags_1 &= ~DF_1_DIRECT;
1914 
1915 	dyn->d_tag = DT_FLAGS_1;
1916 	dyn->d_un.d_val = ofl->ofl_dtflags_1;
1917 	dyn++;
1918 
1919 	ld_mach_update_odynamic(ofl, &dyn);
1920 
1921 	dyn->d_tag = DT_NULL;
1922 	dyn->d_un.d_val = 0;
1923 
1924 	return (1);
1925 }
1926 
1927 /*
1928  * Build the version definition section
1929  */
1930 static int
1931 update_overdef(Ofl_desc *ofl)
1932 {
1933 	Listnode	*lnp1, *lnp2;
1934 	Ver_desc	*vdp, *_vdp;
1935 	Verdef		*vdf, *_vdf;
1936 	int		num = 0;
1937 	Os_desc		*strosp, *symosp;
1938 
1939 	/*
1940 	 * Traverse the version descriptors and update the version structures
1941 	 * to point to the dynstr name in preparation for building the version
1942 	 * section structure.
1943 	 */
1944 	for (LIST_TRAVERSE(&ofl->ofl_verdesc, lnp1, vdp)) {
1945 		Sym_desc *	sdp;
1946 
1947 		if (vdp->vd_flags & VER_FLG_BASE) {
1948 			const char	*name = vdp->vd_name;
1949 			uint_t		stoff;
1950 
1951 			/*
1952 			 * Create a new string table entry to represent the base
1953 			 * version name (there is no corresponding symbol for
1954 			 * this).
1955 			 */
1956 			if (!(ofl->ofl_flags & FLG_OF_DYNAMIC)) {
1957 				(void) st_setstring(ofl->ofl_strtab,
1958 					name, &stoff);
1959 				/* LINTED */
1960 				vdp->vd_name = (const char *)(uintptr_t)stoff;
1961 			} else {
1962 				(void) st_setstring(ofl->ofl_dynstrtab,
1963 					name, &stoff);
1964 				/* LINTED */
1965 				vdp->vd_name = (const char *)(uintptr_t)stoff;
1966 			}
1967 		} else {
1968 			sdp = ld_sym_find(vdp->vd_name, vdp->vd_hash, 0, ofl);
1969 			/* LINTED */
1970 			vdp->vd_name = (const char *)
1971 				(uintptr_t)sdp->sd_sym->st_name;
1972 		}
1973 	}
1974 
1975 	_vdf = vdf = (Verdef *)ofl->ofl_osverdef->os_outdata->d_buf;
1976 
1977 	/*
1978 	 * Traverse the version descriptors and update the version section to
1979 	 * reflect each version and its associated dependencies.
1980 	 */
1981 	for (LIST_TRAVERSE(&ofl->ofl_verdesc, lnp1, vdp)) {
1982 		Half		cnt = 1;
1983 		Verdaux *	vdap, * _vdap;
1984 
1985 		_vdap = vdap = (Verdaux *)(vdf + 1);
1986 
1987 		vdf->vd_version = VER_DEF_CURRENT;
1988 		vdf->vd_flags	= vdp->vd_flags & MSK_VER_USER;
1989 		vdf->vd_ndx	= vdp->vd_ndx;
1990 		vdf->vd_hash	= vdp->vd_hash;
1991 
1992 		/* LINTED */
1993 		vdap->vda_name = (uintptr_t)vdp->vd_name;
1994 		vdap++;
1995 		/* LINTED */
1996 		_vdap->vda_next = (Word)((uintptr_t)vdap - (uintptr_t)_vdap);
1997 
1998 		/*
1999 		 * Traverse this versions dependency list generating the
2000 		 * appropriate version dependency entries.
2001 		 */
2002 		for (LIST_TRAVERSE(&vdp->vd_deps, lnp2, _vdp)) {
2003 			/* LINTED */
2004 			vdap->vda_name = (uintptr_t)_vdp->vd_name;
2005 			_vdap = vdap;
2006 			vdap++, cnt++;
2007 			/* LINTED */
2008 			_vdap->vda_next = (Word)((uintptr_t)vdap -
2009 			    (uintptr_t)_vdap);
2010 		}
2011 		_vdap->vda_next = 0;
2012 
2013 		/*
2014 		 * Record the versions auxiliary array offset and the associated
2015 		 * dependency count.
2016 		 */
2017 		/* LINTED */
2018 		vdf->vd_aux = (Word)((uintptr_t)(vdf + 1) - (uintptr_t)vdf);
2019 		vdf->vd_cnt = cnt;
2020 
2021 		/*
2022 		 * Record the next versions offset and update the version
2023 		 * pointer.  Remember the previous version offset as the very
2024 		 * last structures next pointer should be null.
2025 		 */
2026 		_vdf = vdf;
2027 		vdf = (Verdef *)vdap, num++;
2028 		/* LINTED */
2029 		_vdf->vd_next = (Word)((uintptr_t)vdf - (uintptr_t)_vdf);
2030 	}
2031 	_vdf->vd_next = 0;
2032 
2033 	/*
2034 	 * Record the string table association with the version definition
2035 	 * section, and the symbol table associated with the version symbol
2036 	 * table (the actual contents of the version symbol table are filled
2037 	 * in during symbol update).
2038 	 */
2039 	if ((ofl->ofl_flags & FLG_OF_RELOBJ) ||
2040 	    (ofl->ofl_flags & FLG_OF_STATIC)) {
2041 		strosp = ofl->ofl_osstrtab;
2042 		symosp = ofl->ofl_ossymtab;
2043 	} else {
2044 		strosp = ofl->ofl_osdynstr;
2045 		symosp = ofl->ofl_osdynsym;
2046 	}
2047 	/* LINTED */
2048 	ofl->ofl_osverdef->os_shdr->sh_link = (Word)elf_ndxscn(strosp->os_scn);
2049 	/* LINTED */
2050 	ofl->ofl_osversym->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
2051 
2052 	/*
2053 	 * The version definition sections `info' field is used to indicate the
2054 	 * number of entries in this section.
2055 	 */
2056 	ofl->ofl_osverdef->os_shdr->sh_info = num;
2057 
2058 	return (1);
2059 }
2060 
2061 /*
2062  * Build the version needed section
2063  */
2064 static int
2065 update_overneed(Ofl_desc *ofl)
2066 {
2067 	Listnode	*lnp;
2068 	Ifl_desc	*ifl;
2069 	Verneed		*vnd, *_vnd;
2070 	Str_tbl		*dynstr;
2071 	Word		num = 0, cnt = 0;
2072 
2073 	dynstr = ofl->ofl_dynstrtab;
2074 	_vnd = vnd = (Verneed *)ofl->ofl_osverneed->os_outdata->d_buf;
2075 
2076 	/*
2077 	 * Traverse the shared object list looking for dependencies that have
2078 	 * versions defined within them.
2079 	 */
2080 	for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, ifl)) {
2081 		Half		_cnt;
2082 		Vernaux		*_vnap, *vnap;
2083 		Sdf_desc	*sdf = ifl->ifl_sdfdesc;
2084 		uint_t		stoff;
2085 
2086 		if (!(ifl->ifl_flags & FLG_IF_VERNEED))
2087 			continue;
2088 
2089 		vnd->vn_version = VER_NEED_CURRENT;
2090 
2091 		(void) st_setstring(dynstr, ifl->ifl_soname, &stoff);
2092 		vnd->vn_file = stoff;
2093 
2094 		_vnap = vnap = (Vernaux *)(vnd + 1);
2095 
2096 		if (sdf && (sdf->sdf_flags & FLG_SDF_SPECVER)) {
2097 			Sdv_desc *	sdv;
2098 			Listnode *	lnp2;
2099 
2100 			/*
2101 			 * If version needed definitions were specified in
2102 			 * a mapfile ($VERSION=*) then record those
2103 			 * definitions.
2104 			 */
2105 			for (LIST_TRAVERSE(&sdf->sdf_verneed, lnp2, sdv)) {
2106 				(void) st_setstring(dynstr,
2107 					sdv->sdv_name, &stoff);
2108 				vnap->vna_name = stoff;
2109 				/* LINTED */
2110 				vnap->vna_hash = (Word)elf_hash(sdv->sdv_name);
2111 				vnap->vna_flags = 0;
2112 				vnap->vna_other = 0;
2113 				_vnap = vnap;
2114 				vnap++;
2115 				cnt++;
2116 				/* LINTED */
2117 				_vnap->vna_next = (Word)((uintptr_t)vnap -
2118 				    (uintptr_t)_vnap);
2119 			}
2120 		} else {
2121 
2122 			/*
2123 			 * Traverse the version index list recording
2124 			 * each version as a needed dependency.
2125 			 */
2126 			for (cnt = _cnt = 0; _cnt <= ifl->ifl_vercnt;
2127 			    _cnt++) {
2128 				Ver_index *	vip = &ifl->ifl_verndx[_cnt];
2129 
2130 				if (vip->vi_flags & FLG_VER_REFER) {
2131 					(void) st_setstring(dynstr,
2132 						vip->vi_name, &stoff);
2133 					vnap->vna_name = stoff;
2134 					if (vip->vi_desc) {
2135 					    vnap->vna_hash =
2136 						vip->vi_desc->vd_hash;
2137 					    vnap->vna_flags =
2138 						vip->vi_desc->vd_flags;
2139 					} else {
2140 					    vnap->vna_hash = 0;
2141 					    vnap->vna_flags = 0;
2142 					}
2143 					vnap->vna_other = 0;
2144 
2145 					_vnap = vnap;
2146 					vnap++, cnt++;
2147 					_vnap->vna_next =
2148 						/* LINTED */
2149 						(Word)((uintptr_t)vnap -
2150 						    (uintptr_t)_vnap);
2151 				}
2152 			}
2153 		}
2154 		_vnap->vna_next = 0;
2155 
2156 		/*
2157 		 * Record the versions auxiliary array offset and
2158 		 * the associated dependency count.
2159 		 */
2160 		/* LINTED */
2161 		vnd->vn_aux = (Word)((uintptr_t)(vnd + 1) - (uintptr_t)vnd);
2162 		/* LINTED */
2163 		vnd->vn_cnt = (Half)cnt;
2164 
2165 		/*
2166 		 * Record the next versions offset and update the version
2167 		 * pointer.  Remember the previous version offset as the very
2168 		 * last structures next pointer should be null.
2169 		 */
2170 		_vnd = vnd;
2171 		vnd = (Verneed *)vnap, num++;
2172 		/* LINTED */
2173 		_vnd->vn_next = (Word)((uintptr_t)vnd - (uintptr_t)_vnd);
2174 	}
2175 	_vnd->vn_next = 0;
2176 
2177 	/*
2178 	 * Record association on string table section and use the
2179 	 * `info' field to indicate the number of entries in this
2180 	 * section.
2181 	 */
2182 	ofl->ofl_osverneed->os_shdr->sh_link =
2183 	    /* LINTED */
2184 	    (Word)elf_ndxscn(ofl->ofl_osdynstr->os_scn);
2185 	ofl->ofl_osverneed->os_shdr->sh_info = num;
2186 
2187 	return (1);
2188 }
2189 
2190 
2191 /*
2192  * Update syminfo section.
2193  */
2194 static uintptr_t
2195 update_osyminfo(Ofl_desc * ofl)
2196 {
2197 	Os_desc *	symosp, * infosp = ofl->ofl_ossyminfo;
2198 	Syminfo *	sip = infosp->os_outdata->d_buf;
2199 	Shdr *		shdr = infosp->os_shdr;
2200 	char		*strtab;
2201 	Listnode *	lnp;
2202 	Sym_desc *	sdp;
2203 	Aliste		off;
2204 	Sfltr_desc *	sftp;
2205 
2206 	if (ofl->ofl_flags & FLG_OF_RELOBJ) {
2207 		symosp = ofl->ofl_ossymtab;
2208 		strtab = ofl->ofl_osstrtab->os_outdata->d_buf;
2209 	} else {
2210 		symosp = ofl->ofl_osdynsym;
2211 		strtab = ofl->ofl_osdynstr->os_outdata->d_buf;
2212 	}
2213 
2214 	/* LINTED */
2215 	infosp->os_shdr->sh_link = (Word)elf_ndxscn(symosp->os_scn);
2216 	if (ofl->ofl_osdynamic)
2217 		infosp->os_shdr->sh_info =
2218 		    /* LINTED */
2219 		    (Word)elf_ndxscn(ofl->ofl_osdynamic->os_scn);
2220 
2221 	/*
2222 	 * Update any references with the index into the dynamic table.
2223 	 */
2224 	for (LIST_TRAVERSE(&ofl->ofl_syminfsyms, lnp, sdp)) {
2225 		Ifl_desc *	ifl;
2226 		if (sdp->sd_aux && sdp->sd_aux->sa_bindto)
2227 			ifl = sdp->sd_aux->sa_bindto;
2228 		else
2229 			ifl = sdp->sd_file;
2230 		sip[sdp->sd_symndx].si_boundto = ifl->ifl_neededndx;
2231 	}
2232 
2233 	/*
2234 	 * Update any filtee references with the index into the dynamic table.
2235 	 */
2236 	for (ALIST_TRAVERSE(ofl->ofl_symfltrs, off, sftp)) {
2237 		Dfltr_desc *	dftp;
2238 
2239 		/* LINTED */
2240 		dftp = (Dfltr_desc *)((char *)ofl->ofl_dtsfltrs +
2241 		    sftp->sft_off);
2242 		sip[sftp->sft_sdp->sd_symndx].si_boundto = dftp->dft_ndx;
2243 	}
2244 
2245 	/*
2246 	 * Display debugging information about section.
2247 	 */
2248 	DBG_CALL(Dbg_syminfo_title(ofl->ofl_lml));
2249 	if (DBG_ENABLED) {
2250 		Word	_cnt, cnt = shdr->sh_size / shdr->sh_entsize;
2251 		Sym *	symtab = symosp->os_outdata->d_buf;
2252 		Dyn *	dyn;
2253 
2254 		if (ofl->ofl_osdynamic)
2255 			dyn = ofl->ofl_osdynamic->os_outdata->d_buf;
2256 		else
2257 			dyn = 0;
2258 
2259 		for (_cnt = 1; _cnt < cnt; _cnt++) {
2260 			if (sip[_cnt].si_flags || sip[_cnt].si_boundto)
2261 				/* LINTED */
2262 				DBG_CALL(Dbg_syminfo_entry(ofl->ofl_lml, _cnt,
2263 				    &sip[_cnt], &symtab[_cnt], strtab, dyn));
2264 		}
2265 	}
2266 	return (1);
2267 }
2268 
2269 /*
2270  * Build the output elf header.
2271  */
2272 static uintptr_t
2273 update_oehdr(Ofl_desc * ofl)
2274 {
2275 	Ehdr	*ehdr = ofl->ofl_nehdr;
2276 
2277 	/*
2278 	 * If an entry point symbol has already been established (refer
2279 	 * sym_validate()) simply update the elf header entry point with the
2280 	 * symbols value.  If no entry point is defined it will have been filled
2281 	 * with the start address of the first section within the text segment
2282 	 * (refer update_outfile()).
2283 	 */
2284 	if (ofl->ofl_entry)
2285 		ehdr->e_entry =
2286 			((Sym_desc *)(ofl->ofl_entry))->sd_sym->st_value;
2287 
2288 	/*
2289 	 * Note. it may be necessary to update the `e_flags' field in the
2290 	 * machine dependent section.
2291 	 */
2292 	ehdr->e_ident[EI_DATA] = M_DATA;
2293 	ehdr->e_machine = ofl->ofl_dehdr->e_machine;
2294 	ehdr->e_flags = ofl->ofl_dehdr->e_flags;
2295 	ehdr->e_version = ofl->ofl_dehdr->e_version;
2296 
2297 	if (ehdr->e_machine != M_MACH) {
2298 		if (ehdr->e_machine != M_MACHPLUS)
2299 			return (S_ERROR);
2300 		if ((ehdr->e_flags & M_FLAGSPLUS) == 0)
2301 			return (S_ERROR);
2302 	}
2303 
2304 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
2305 		ehdr->e_type = ET_DYN;
2306 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
2307 		ehdr->e_type = ET_REL;
2308 	else
2309 		ehdr->e_type = ET_EXEC;
2310 
2311 	return (1);
2312 }
2313 
2314 /*
2315  * Perform move table expansion.
2316  */
2317 static uintptr_t
2318 expand_move(Ofl_desc *ofl, Sym_desc *sdp, Move *u1)
2319 {
2320 	Move		*mv;
2321 	Os_desc		*osp;
2322 	unsigned char	*taddr, *taddr0;
2323 	Sxword		offset;
2324 	int		i;
2325 	Addr		base1;
2326 	unsigned int	stride;
2327 
2328 	osp = ofl->ofl_issunwdata1->is_osdesc;
2329 	base1 = (Addr)(osp->os_shdr->sh_addr +
2330 		ofl->ofl_issunwdata1->is_indata->d_off);
2331 	taddr0 = taddr = osp->os_outdata->d_buf;
2332 	mv = u1;
2333 
2334 	offset = sdp->sd_sym->st_value - base1;
2335 	taddr += offset;
2336 	taddr = taddr + mv->m_poffset;
2337 	for (i = 0; i < mv->m_repeat; i++) {
2338 		/* LINTED */
2339 		DBG_CALL(Dbg_move_expand(ofl->ofl_lml, mv,
2340 		    (Addr)(taddr - taddr0)));
2341 		stride = (unsigned int)mv->m_stride + 1;
2342 		/* LINTED */
2343 		switch (ELF_M_SIZE(mv->m_info)) {
2344 		case 1:
2345 			/* LINTED */
2346 			*taddr = (unsigned char)mv->m_value;
2347 			taddr += stride;
2348 			break;
2349 		case 2:
2350 			/* LINTED */
2351 			*((Half *)taddr) = (Half)mv->m_value;
2352 			taddr += 2*stride;
2353 			break;
2354 		case 4:
2355 			/* LINTED */
2356 			*((Word *)taddr) = (Word)mv->m_value;
2357 			taddr += 4*stride;
2358 			break;
2359 		case 8:
2360 			/* LINTED */
2361 			*((unsigned long long *)taddr) =
2362 				mv->m_value;
2363 			taddr += 8*stride;
2364 			break;
2365 		default:
2366 			/*
2367 			 * Should never come here since this is already
2368 			 * checked at sunwmove_preprocess().
2369 			 */
2370 			return (S_ERROR);
2371 		}
2372 	}
2373 	return (1);
2374 }
2375 
2376 /*
2377  * Update Move sections.
2378  */
2379 static uintptr_t
2380 update_move(Ofl_desc *ofl)
2381 {
2382 	Word		ndx = 0;
2383 	Is_desc *	isp;
2384 	Word		flags = ofl->ofl_flags;
2385 	Move *		mv1, * mv2;
2386 	Listnode *	lnp1;
2387 	Psym_info *	psym;
2388 
2389 	/*
2390 	 * Determine the index of the symbol table that will be referenced by
2391 	 * the relocation entries.
2392 	 */
2393 	if ((flags & (FLG_OF_DYNAMIC|FLG_OF_RELOBJ)) == FLG_OF_DYNAMIC)
2394 		/* LINTED */
2395 		ndx = (Word) elf_ndxscn(ofl->ofl_osdynsym->os_scn);
2396 	else if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ))
2397 		/* LINTED */
2398 		ndx = (Word) elf_ndxscn(ofl->ofl_ossymtab->os_scn);
2399 
2400 	/*
2401 	 * update sh_link and mv pointer for updating move table.
2402 	 */
2403 	if (ofl->ofl_osmove) {
2404 		ofl->ofl_osmove->os_shdr->sh_link = ndx;
2405 		mv1 = (Move *) ofl->ofl_osmove->os_outdata->d_buf;
2406 	}
2407 
2408 	/*
2409 	 * Update symbol entry index
2410 	 */
2411 	for (LIST_TRAVERSE(&ofl->ofl_parsym, lnp1, psym)) {
2412 		Listnode *	lnp2;
2413 		Mv_itm *	mvp;
2414 		Sym_desc 	*sdp;
2415 
2416 		/*
2417 		 * Expand move table
2418 		 */
2419 		if (psym->psym_symd->sd_flags & FLG_SY_PAREXPN) {
2420 			const char	*s;
2421 
2422 			if (ofl->ofl_flags & FLG_OF_STATIC)
2423 				s = MSG_INTL(MSG_PSYM_EXPREASON1);
2424 			else if (ofl->ofl_flags1 & FLG_OF1_NOPARTI)
2425 				s = MSG_INTL(MSG_PSYM_EXPREASON2);
2426 			else
2427 				s = MSG_INTL(MSG_PSYM_EXPREASON3);
2428 			DBG_CALL(Dbg_move_parexpn(ofl->ofl_lml,
2429 			    psym->psym_symd->sd_name, s));
2430 			for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvp)) {
2431 				if ((mvp->mv_flag & FLG_MV_OUTSECT) == 0)
2432 					continue;
2433 				mv2 = mvp->mv_ientry;
2434 				sdp = psym->psym_symd;
2435 				DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0,
2436 				    mv2, sdp));
2437 				(void) expand_move(ofl, sdp, mv2);
2438 			}
2439 			continue;
2440 		}
2441 
2442 		/*
2443 		 * Process move table
2444 		 */
2445 		DBG_CALL(Dbg_move_outmove(ofl->ofl_lml,
2446 		    psym->psym_symd->sd_name));
2447 		for (LIST_TRAVERSE(&(psym->psym_mvs), lnp2, mvp)) {
2448 			int	idx = 1;
2449 			if ((mvp->mv_flag & FLG_MV_OUTSECT) == 0)
2450 				continue;
2451 			isp = mvp->mv_isp;
2452 			mv2 = mvp->mv_ientry;
2453 			sdp = isp->is_file->ifl_oldndx[
2454 				ELF_M_SYM(mv2->m_info)];
2455 
2456 			DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 0, mv2, sdp));
2457 			*mv1 = *mv2;
2458 			if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) {
2459 				if (ELF_ST_BIND(sdp->sd_sym->st_info) ==
2460 				    STB_LOCAL) {
2461 				    Half	symbssndx =
2462 					ofl->ofl_isbss->is_osdesc->os_scnsymndx;
2463 				    mv1->m_info =
2464 					/* LINTED */
2465 					ELF_M_INFO(symbssndx, mv2->m_info);
2466 				    if (ELF_ST_TYPE(sdp->sd_sym->st_info) !=
2467 				    STT_SECTION) {
2468 					mv1->m_poffset = sdp->sd_sym->st_value -
2469 					ofl->ofl_isbss->
2470 						is_osdesc->os_shdr->sh_addr +
2471 					mv2->m_poffset;
2472 				    }
2473 				} else {
2474 				    mv1->m_info =
2475 					/* LINTED */
2476 					ELF_M_INFO(sdp->sd_symndx, mv2->m_info);
2477 				}
2478 			} else {
2479 				Boolean 	isredloc = FALSE;
2480 
2481 				if ((ELF_ST_BIND(sdp->sd_sym->st_info) ==
2482 				    STB_LOCAL) &&
2483 				    (ofl->ofl_flags1 & FLG_OF1_REDLSYM))
2484 					isredloc = TRUE;
2485 
2486 				if (isredloc && !(sdp->sd_psyminfo)) {
2487 					Word symndx =
2488 					sdp->sd_isc->is_osdesc->os_scnsymndx;
2489 					mv1->m_info =
2490 					/* LINTED */
2491 					ELF_M_INFO(symndx, mv2->m_info);
2492 					mv1->m_poffset += sdp->sd_sym->st_value;
2493 				} else {
2494 					if (isredloc)
2495 					    DBG_CALL(Dbg_syms_reduce(ofl,
2496 						DBG_SYM_REDUCE_RETAIN, sdp,
2497 						idx, ofl->ofl_osmove->os_name));
2498 
2499 					mv1->m_info =
2500 					/* LINTED */
2501 					ELF_M_INFO(sdp->sd_symndx, mv2->m_info);
2502 				}
2503 			}
2504 			DBG_CALL(Dbg_move_entry1(ofl->ofl_lml, 1, mv1, sdp));
2505 			mv1++;
2506 			idx++;
2507 		}
2508 	}
2509 	return (1);
2510 }
2511 
2512 
2513 /*
2514  * Scan through the SHT_GROUP output sections.  Update their
2515  * sh_link/sh_info fields as well as the section contents.
2516  */
2517 static uintptr_t
2518 update_ogroup(Ofl_desc * ofl)
2519 {
2520 	Listnode	*lnp;
2521 	Os_desc		*osp;
2522 	uintptr_t	error = 0;
2523 
2524 	for (LIST_TRAVERSE(&ofl->ofl_osgroups, lnp, osp)) {
2525 		Is_desc		*isp;
2526 		Ifl_desc	*ifl;
2527 		Shdr		*shdr = osp->os_shdr;
2528 		Sym_desc	*sdp;
2529 		Xword		i, grpcnt;
2530 		Word		*gdata;
2531 
2532 		/*
2533 		 * Since input GROUP sections always create unique
2534 		 * output GROUP sections - we know there is only one
2535 		 * item on the list.
2536 		 */
2537 		isp = (Is_desc *)osp->os_isdescs.head->data;
2538 
2539 		ifl = isp->is_file;
2540 		sdp = ifl->ifl_oldndx[isp->is_shdr->sh_info];
2541 		shdr->sh_link = (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn);
2542 		shdr->sh_info = sdp->sd_symndx;
2543 
2544 		/*
2545 		 * Scan through the group data section and update
2546 		 * all of the links to new values.
2547 		 */
2548 		grpcnt = shdr->sh_size / shdr->sh_entsize;
2549 		gdata = (Word *)osp->os_outdata->d_buf;
2550 		for (i = 1; i < grpcnt; i++) {
2551 			Is_desc	*	_isp;
2552 			Os_desc	*	_osp;
2553 
2554 			/*
2555 			 * Perform a sanity check that the section index
2556 			 * stored in the SHT_GROUP section is valid
2557 			 * for the file it came from.
2558 			 */
2559 			if (gdata[i] >= ifl->ifl_shnum) {
2560 				eprintf(ofl->ofl_lml, ERR_FATAL,
2561 				    MSG_INTL(MSG_GRP_INVALNDX), isp->is_name,
2562 				    ifl->ifl_name, i, gdata[i]);
2563 				error = S_ERROR;
2564 				gdata[i] = 0;
2565 				continue;
2566 			}
2567 
2568 			_isp = ifl->ifl_isdesc[gdata[i]];
2569 
2570 			/*
2571 			 * If the referenced section didn't make it to the
2572 			 * output file - just zero out the entry.
2573 			 */
2574 			if ((_osp = _isp->is_osdesc) == 0)
2575 				gdata[i] = 0;
2576 			else
2577 				gdata[i] = (Word)elf_ndxscn(_osp->os_scn);
2578 		}
2579 	}
2580 	return (error);
2581 }
2582 
2583 static void
2584 update_ostrtab(Os_desc *osp, Str_tbl *stp)
2585 {
2586 	Elf_Data	*data;
2587 	if (osp == 0)
2588 		return;
2589 
2590 	data = osp->os_outdata;
2591 	assert(data->d_size == st_getstrtab_sz(stp));
2592 	(void) st_setstrbuf(stp, data->d_buf, (uint_t)data->d_size);
2593 }
2594 
2595 /*
2596  * Translate the shdr->sh_{link, info} from its input section value to that
2597  * of the corresponding shdr->sh_{link, info} output section value.
2598  */
2599 static Word
2600 translate_link(Ofl_desc *ofl, Os_desc *osp, Word link, const char *msg)
2601 {
2602 	Is_desc *	isp;
2603 	Ifl_desc *	ifl;
2604 
2605 	/*
2606 	 * Don't translate the special section numbers.
2607 	 */
2608 	if (link >= SHN_LORESERVE)
2609 		return (link);
2610 
2611 	/*
2612 	 * Does this output section translate back to an input file.  If not
2613 	 * then there is no translation to do.  In this case we will assume that
2614 	 * if sh_link has a value, it's the right value.
2615 	 */
2616 	isp = (Is_desc *)osp->os_isdescs.head->data;
2617 	if ((ifl = isp->is_file) == NULL)
2618 		return (link);
2619 
2620 	/*
2621 	 * Sanity check to make sure that the sh_{link, info} value
2622 	 * is within range for the input file.
2623 	 */
2624 	if (link >= ifl->ifl_shnum) {
2625 		eprintf(ofl->ofl_lml, ERR_WARNING, msg, ifl->ifl_name,
2626 		    isp->is_name, EC_XWORD(link));
2627 		return (link);
2628 	}
2629 
2630 	/*
2631 	 * Follow the link to the input section.
2632 	 */
2633 	if ((isp = ifl->ifl_isdesc[link]) == 0)
2634 		return (0);
2635 	if ((osp = isp->is_osdesc) == 0)
2636 		return (0);
2637 
2638 	/* LINTED */
2639 	return ((Word)elf_ndxscn(osp->os_scn));
2640 }
2641 
2642 /*
2643  * Having created all of the necessary sections, segments, and associated
2644  * headers, fill in the program headers and update any other data in the
2645  * output image.  Some general rules:
2646  *
2647  *  o	If an interpretor is required always generate a PT_PHDR entry as
2648  *	well.  It is this entry that triggers the kernel into passing the
2649  *	interpretor an aux vector instead of just a file descriptor.
2650  *
2651  *  o	When generating an image that will be interpreted (ie. a dynamic
2652  *	executable, a shared object, or a static executable that has been
2653  *	provided with an interpretor - weird, but possible), make the initial
2654  *	loadable segment include both the ehdr and phdr[].  Both of these
2655  *	tables are used by the interpretor therefore it seems more intuitive
2656  *	to explicitly defined them as part of the mapped image rather than
2657  *	relying on page rounding by the interpretor to allow their access.
2658  *
2659  *  o	When generating a static image that does not require an interpretor
2660  *	have the first loadable segment indicate the address of the first
2661  *	.section as the start address (things like /kernel/unix and ufsboot
2662  *	expect this behavior).
2663  */
2664 uintptr_t
2665 ld_update_outfile(Ofl_desc *ofl)
2666 {
2667 	Addr		size, etext, vaddr = ofl->ofl_segorigin;
2668 	Listnode	*lnp1, *lnp2;
2669 	Sg_desc		*sgp;
2670 	Os_desc		*osp;
2671 	int		phdrndx = 0, capndx = 0, segndx = -1, secndx;
2672 	Ehdr		*ehdr = ofl->ofl_nehdr;
2673 	List		osecs;
2674 	Shdr		*hshdr;
2675 	Phdr		*_phdr = 0, *dtracephdr = 0;
2676 	Word		phdrsz = ehdr->e_phnum *ehdr->e_phentsize, shscnndx;
2677 	Word		flags = ofl->ofl_flags, ehdrsz = ehdr->e_ehsize;
2678 	Boolean		nobits;
2679 	Off		offset;
2680 
2681 	/*
2682 	 * Loop through the segment descriptors and pick out what we need.
2683 	 */
2684 	DBG_CALL(Dbg_seg_title(ofl->ofl_lml));
2685 	for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) {
2686 		Phdr *	phdr = &(sgp->sg_phdr);
2687 		Xword 	p_align;
2688 
2689 		segndx++;
2690 
2691 		/*
2692 		 * If an interpreter is required generate a PT_INTERP and
2693 		 * PT_PHDR program header entry.  The PT_PHDR entry describes
2694 		 * the program header table itself.  This information will be
2695 		 * passed via the aux vector to the interpreter (ld.so.1).
2696 		 * The program header array is actually part of the first
2697 		 * loadable segment (and the PT_PHDR entry is the first entry),
2698 		 * therefore its virtual address isn't known until the first
2699 		 * loadable segment is processed.
2700 		 */
2701 		if (phdr->p_type == PT_PHDR) {
2702 			if (ofl->ofl_osinterp) {
2703 				phdr->p_offset = ehdr->e_phoff;
2704 				phdr->p_filesz = phdr->p_memsz = phdrsz;
2705 				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
2706 				ofl->ofl_phdr[phdrndx++] = *phdr;
2707 			}
2708 			continue;
2709 		}
2710 		if (phdr->p_type == PT_INTERP) {
2711 			if (ofl->ofl_osinterp) {
2712 				Shdr *	shdr = ofl->ofl_osinterp->os_shdr;
2713 
2714 				phdr->p_vaddr = phdr->p_memsz = 0;
2715 				phdr->p_offset = shdr->sh_offset;
2716 				phdr->p_filesz = shdr->sh_size;
2717 				DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
2718 				ofl->ofl_phdr[phdrndx++] = *phdr;
2719 			}
2720 			continue;
2721 		}
2722 
2723 		/*
2724 		 * If we are creating a PT_SUNWDTRACE segment,
2725 		 * just remember where the program header is.
2726 		 *
2727 		 * It's actual values will be assigned after
2728 		 * update_osym() has completed and the symbol
2729 		 * table addresses have been udpated.
2730 		 */
2731 		if (phdr->p_type == PT_SUNWDTRACE) {
2732 			if ((ofl->ofl_dtracesym) &&
2733 			    ((flags & FLG_OF_RELOBJ) == 0)) {
2734 				dtracephdr = &ofl->ofl_phdr[phdrndx];
2735 				ofl->ofl_phdr[phdrndx++] = *phdr;
2736 			}
2737 			continue;
2738 		}
2739 
2740 		/*
2741 		 * If a hardware/software capabilities section is required,
2742 		 * generate the PT_SUNWCAP header.  Note, as this comes before
2743 		 * the first loadable segment, we don't yet know its real
2744 		 * virtual address.  This is updated later.
2745 		 */
2746 		if (phdr->p_type == PT_SUNWCAP) {
2747 			if (ofl->ofl_oscap) {
2748 				Shdr *	shdr = ofl->ofl_oscap->os_shdr;
2749 
2750 				phdr->p_vaddr = shdr->sh_addr;
2751 				phdr->p_offset = shdr->sh_offset;
2752 				phdr->p_filesz = shdr->sh_size;
2753 				phdr->p_flags = PF_R;
2754 				DBG_CALL(Dbg_seg_entry(ofl, 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, segndx, sgp));
2777 				ofl->ofl_phdr[phdrndx++] = *phdr;
2778 			}
2779 			continue;
2780 		}
2781 #if	defined(__x86) && defined(_ELF64)
2782 		if (phdr->p_type == PT_SUNW_UNWIND) {
2783 			Shdr	    *shdr;
2784 			if (ofl->ofl_unwindhdr == 0)
2785 				continue;
2786 			shdr = ofl->ofl_unwindhdr->os_shdr;
2787 
2788 			phdr->p_flags = PF_R;
2789 			phdr->p_vaddr = shdr->sh_addr;
2790 			phdr->p_memsz = shdr->sh_size;
2791 			phdr->p_filesz = shdr->sh_size;
2792 			phdr->p_offset = shdr->sh_offset;
2793 			phdr->p_align = shdr->sh_addralign;
2794 			phdr->p_paddr = 0;
2795 			ofl->ofl_phdr[phdrndx++] = *phdr;
2796 			continue;
2797 		}
2798 #endif
2799 		if (phdr->p_type == PT_TLS) {
2800 			Os_desc	*tlsosp;
2801 			Shdr	*firstshdr = 0, *lastfilshdr, *lastmemshdr;
2802 
2803 			if (ofl->ofl_ostlsseg.head == NULL)
2804 				continue;
2805 
2806 			for (LIST_TRAVERSE(&ofl->ofl_ostlsseg, lnp2, tlsosp)) {
2807 				Shdr	*tlsshdr = tlsosp->os_shdr;
2808 
2809 				if (firstshdr == 0) {
2810 					firstshdr = lastfilshdr = lastmemshdr =
2811 					    tlsosp->os_shdr;
2812 					continue;
2813 				}
2814 
2815 				if (tlsshdr->sh_type == SHT_NOBITS)
2816 					lastmemshdr = tlsshdr;
2817 				else
2818 					lastfilshdr = tlsshdr;
2819 			}
2820 
2821 			phdr->p_flags = PF_R | PF_W;
2822 			phdr->p_vaddr = firstshdr->sh_addr;
2823 			phdr->p_offset = firstshdr->sh_offset;
2824 			phdr->p_align = firstshdr->sh_addralign;
2825 			phdr->p_filesz = lastfilshdr->sh_offset +
2826 			    lastfilshdr->sh_size - phdr->p_offset;
2827 			phdr->p_memsz = lastmemshdr->sh_offset +
2828 			    lastmemshdr->sh_size - phdr->p_offset;
2829 
2830 			DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
2831 
2832 			ofl->ofl_tlsphdr = phdr;
2833 			ofl->ofl_phdr[phdrndx++] = *phdr;
2834 			continue;
2835 		}
2836 
2837 		/*
2838 		 * If this is an empty segment declaration, it will occur after
2839 		 * all other loadable segments, make sure the previous segment
2840 		 * doesn't overlap. We do not do the check if we are generating
2841 		 * a relocatable file.
2842 		 */
2843 		if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
2844 		    (sgp->sg_flags & FLG_SG_EMPTY)) {
2845 			int i;
2846 			Addr	v_e;
2847 
2848 			vaddr = phdr->p_vaddr;
2849 			phdr->p_memsz = sgp->sg_length;
2850 			DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
2851 			ofl->ofl_phdr[phdrndx++] = *phdr;
2852 
2853 			if (phdr->p_type != PT_LOAD)
2854 				continue;
2855 
2856 			v_e = vaddr + phdr->p_memsz;
2857 			/*
2858 			 * Check overlaps
2859 			 */
2860 			for (i = 0; i < phdrndx - 1; i++) {
2861 				Addr 	p_s = (ofl->ofl_phdr[i]).p_vaddr;
2862 				Addr 	p_e;
2863 
2864 				if ((ofl->ofl_phdr[i]).p_type != PT_LOAD)
2865 					continue;
2866 
2867 				p_e = p_s + (ofl->ofl_phdr[i]).p_memsz;
2868 				if (((p_s <= vaddr) && (p_e > vaddr)) ||
2869 				    ((vaddr <= p_s) && (v_e > p_s)))
2870 					eprintf(ofl->ofl_lml, ERR_WARNING,
2871 					    MSG_INTL(MSG_UPD_SEGOVERLAP),
2872 					    ofl->ofl_name, EC_ADDR(p_e),
2873 					    sgp->sg_name, EC_ADDR(vaddr));
2874 			}
2875 			continue;
2876 		}
2877 
2878 		/*
2879 		 * Having processed any of the special program headers any
2880 		 * remaining headers will be built to express individual
2881 		 * segments.  Segments are only built if they have output
2882 		 * section descriptors associated with them (ie. some form of
2883 		 * input section has been matched to this segment).
2884 		 */
2885 		osecs = sgp->sg_osdescs;
2886 		if (osecs.head == NULL)
2887 			continue;
2888 
2889 		/*
2890 		 * Determine the segments offset and size from the section
2891 		 * information provided from elf_update().
2892 		 * Allow for multiple NOBITS sections.
2893 		 */
2894 		osp = (Os_desc *)osecs.head->data;
2895 		hshdr = osp->os_shdr;
2896 
2897 		phdr->p_filesz = 0;
2898 		phdr->p_memsz = 0;
2899 		phdr->p_offset = offset = hshdr->sh_offset;
2900 		nobits = ((hshdr->sh_type == SHT_NOBITS) &&
2901 			((sgp->sg_flags & FLG_SG_PHREQ) == 0));
2902 		for (LIST_TRAVERSE(&osecs, lnp2, osp)) {
2903 			Shdr *	shdr = osp->os_shdr;
2904 
2905 			p_align = 0;
2906 			if (shdr->sh_addralign > p_align)
2907 				p_align = shdr->sh_addralign;
2908 			offset = (Off)S_ROUND(offset, shdr->sh_addralign);
2909 			offset += shdr->sh_size;
2910 			if (shdr->sh_type != SHT_NOBITS) {
2911 				if (nobits) {
2912 					eprintf(ofl->ofl_lml, ERR_FATAL,
2913 					    MSG_INTL(MSG_UPD_NOBITS));
2914 					return (S_ERROR);
2915 				}
2916 				phdr->p_filesz = offset - phdr->p_offset;
2917 			} else if ((sgp->sg_flags & FLG_SG_PHREQ) == 0)
2918 				nobits = TRUE;
2919 		}
2920 		phdr->p_memsz = offset - hshdr->sh_offset;
2921 
2922 		/*
2923 		 * If this is PT_SUNWBSS, set alignment
2924 		 */
2925 		if (phdr->p_type == PT_SUNWBSS)
2926 			phdr->p_align = p_align;
2927 
2928 		/*
2929 		 * If this is the first loadable segment of a dynamic object,
2930 		 * or an interpretor has been specified (a static object built
2931 		 * with an interpretor will still be given a PT_HDR entry), then
2932 		 * compensate for the elf header and program header array.  Both
2933 		 * of these are actually part of the loadable segment as they
2934 		 * may be inspected by the interpretor.  Adjust the segments
2935 		 * size and offset accordingly.
2936 		 */
2937 		if ((_phdr == 0) && (phdr->p_type == PT_LOAD) &&
2938 		    ((ofl->ofl_osinterp) || (flags & FLG_OF_DYNAMIC)) &&
2939 		    (!(ofl->ofl_flags1 & FLG_OF1_NOHDR))) {
2940 			size = (Addr)S_ROUND((phdrsz + ehdrsz),
2941 			    hshdr->sh_addralign);
2942 			phdr->p_offset -= size;
2943 			phdr->p_filesz += size;
2944 			phdr->p_memsz += size;
2945 		}
2946 
2947 		/*
2948 		 * If a segment size symbol is required (specified via a
2949 		 * mapfile) update its value.
2950 		 */
2951 		if (sgp->sg_sizesym != NULL)
2952 			sgp->sg_sizesym->sd_sym->st_value = phdr->p_memsz;
2953 
2954 		/*
2955 		 * If no file content has been assigned to this segment (it
2956 		 * only contains no-bits sections), then reset the offset for
2957 		 * consistency.
2958 		 */
2959 		if (phdr->p_filesz == 0)
2960 			phdr->p_offset = 0;
2961 
2962 		/*
2963 		 * If a virtual address has been specified for this segment
2964 		 * (presumably from a map file) use it and make sure the
2965 		 * previous segment does not run into this segment.
2966 		 */
2967 		if ((phdr->p_type == PT_LOAD) ||
2968 		    (phdr->p_type == PT_SUNWBSS)) {
2969 			if ((sgp->sg_flags & FLG_SG_VADDR)) {
2970 				if (_phdr && (vaddr > phdr->p_vaddr) &&
2971 				    (phdr->p_type == PT_LOAD))
2972 					eprintf(ofl->ofl_lml, ERR_WARNING,
2973 					    MSG_INTL(MSG_UPD_SEGOVERLAP),
2974 					    ofl->ofl_name, EC_ADDR(vaddr),
2975 					    sgp->sg_name,
2976 					    EC_ADDR(phdr->p_vaddr));
2977 				vaddr = phdr->p_vaddr;
2978 				phdr->p_align = 0;
2979 			} else {
2980 				vaddr = phdr->p_vaddr =
2981 				    (Addr)S_ROUND(vaddr, phdr->p_align);
2982 			}
2983 		}
2984 
2985 		/*
2986 		 * Adjust the address offset and p_align if needed.
2987 		 */
2988 		if (!(ofl->ofl_flags1 & (FLG_OF1_NOHDR | FLG_OF1_VADDR))) {
2989 			if (phdr->p_align != 0)
2990 				vaddr += phdr->p_offset % phdr->p_align;
2991 			else
2992 				vaddr += phdr->p_offset;
2993 			phdr->p_vaddr = vaddr;
2994 		}
2995 
2996 		/*
2997 		 * If an interpreter is required set the virtual address of the
2998 		 * PT_PHDR program header now that we know the virtual address
2999 		 * of the loadable segment that contains it.  Update the
3000 		 * PT_SUNWCAP header similarly.
3001 		 */
3002 		if ((_phdr == 0) && (phdr->p_type == PT_LOAD)) {
3003 			_phdr = phdr;
3004 
3005 			if (!(ofl->ofl_flags1 & FLG_OF1_NOHDR)) {
3006 				if (ofl->ofl_osinterp)
3007 					ofl->ofl_phdr[0].p_vaddr =
3008 					    vaddr + ehdrsz;
3009 
3010 				if (ofl->ofl_oscap)
3011 				    ofl->ofl_phdr[capndx].p_vaddr = vaddr +
3012 					ofl->ofl_phdr[capndx].p_offset;
3013 
3014 				/*
3015 				 * Finally, if we're creating a dynamic object
3016 				 * (or a static object in which an interpretor
3017 				 * is specified) update the vaddr to reflect
3018 				 * the address of the first section within this
3019 				 * segment.
3020 				 */
3021 				if ((ofl->ofl_osinterp) ||
3022 				    (flags & FLG_OF_DYNAMIC))
3023 					vaddr += size;
3024 			} else {
3025 				/*
3026 				 * If the FLG_OF1_NOHDR flag was set, PT_PHDR
3027 				 * will not be part of any loadable segment.
3028 				 */
3029 				ofl->ofl_phdr[0].p_vaddr = 0;
3030 				ofl->ofl_phdr[0].p_memsz = 0;
3031 				ofl->ofl_phdr[0].p_flags = 0;
3032 			}
3033 		}
3034 
3035 		/*
3036 		 * Save the address of the first executable section for default
3037 		 * use as the execution entry point.  This may get overridden in
3038 		 * update_oehdr().
3039 		 */
3040 		if (!(flags & FLG_OF_RELOBJ) && !(ehdr->e_entry) &&
3041 		    (phdr->p_flags & PF_X))
3042 			ehdr->e_entry = vaddr;
3043 
3044 		DBG_CALL(Dbg_seg_entry(ofl, segndx, sgp));
3045 
3046 		/*
3047 		 * Traverse the output section descriptors for this segment so
3048 		 * that we can update the section headers addresses.  We've
3049 		 * calculated the virtual address of the initial section within
3050 		 * this segment, so each successive section can be calculated
3051 		 * based on their offsets from each other.
3052 		 */
3053 		secndx = 0;
3054 		hshdr = 0;
3055 		for (LIST_TRAVERSE(&(sgp->sg_osdescs), lnp2, osp)) {
3056 			Shdr *	shdr = osp->os_shdr;
3057 
3058 			if (shdr->sh_link)
3059 			    shdr->sh_link =
3060 				translate_link(ofl, osp, shdr->sh_link,
3061 				MSG_INTL(MSG_FIL_INVSHLINK));
3062 
3063 			if (shdr->sh_info && (shdr->sh_flags & SHF_INFO_LINK))
3064 			    shdr->sh_info =
3065 				translate_link(ofl, osp, shdr->sh_info,
3066 				MSG_INTL(MSG_FIL_INVSHINFO));
3067 
3068 			if (!(flags & FLG_OF_RELOBJ) &&
3069 			    (phdr->p_type == PT_LOAD) ||
3070 			    (phdr->p_type == PT_SUNWBSS)) {
3071 				if (hshdr)
3072 					vaddr += (shdr->sh_offset -
3073 					    hshdr->sh_offset);
3074 
3075 				shdr->sh_addr = vaddr;
3076 				hshdr = shdr;
3077 			}
3078 
3079 			DBG_CALL(Dbg_seg_os(ofl, osp, secndx));
3080 			secndx++;
3081 		}
3082 
3083 		/*
3084 		 * Establish the virtual address of the end of the last section
3085 		 * in this segment so that the next segments offset can be
3086 		 * calculated from this.
3087 		 */
3088 		if (hshdr)
3089 			vaddr += hshdr->sh_size;
3090 
3091 		/*
3092 		 * Output sections for this segment complete.  Adjust the
3093 		 * virtual offset for the last sections size, and make sure we
3094 		 * haven't exceeded any maximum segment length specification.
3095 		 */
3096 		if ((sgp->sg_length != 0) && (sgp->sg_length < phdr->p_memsz)) {
3097 			eprintf(ofl->ofl_lml, ERR_FATAL,
3098 			    MSG_INTL(MSG_UPD_LARGSIZE), ofl->ofl_name,
3099 			    sgp->sg_name, EC_XWORD(phdr->p_memsz),
3100 			    EC_XWORD(sgp->sg_length));
3101 			return (S_ERROR);
3102 		}
3103 
3104 		if (phdr->p_type == PT_NOTE) {
3105 			phdr->p_vaddr = 0;
3106 			phdr->p_paddr = 0;
3107 			phdr->p_align = 0;
3108 			phdr->p_memsz = 0;
3109 		}
3110 		if ((phdr->p_type != PT_NULL) && !(flags & FLG_OF_RELOBJ))
3111 			ofl->ofl_phdr[phdrndx++] = *phdr;
3112 	}
3113 
3114 	/*
3115 	 * Update any new output sections.  When building the initial output
3116 	 * image, a number of sections were created but left uninitialized (eg.
3117 	 * .dynsym, .dynstr, .symtab, .symtab, etc.).  Here we update these
3118 	 * sections with the appropriate data.  Other sections may still be
3119 	 * modified via reloc_process().
3120 	 *
3121 	 * Copy the interpretor name into the .interp section.
3122 	 */
3123 	if (ofl->ofl_interp)
3124 		(void) strcpy((char *)ofl->ofl_osinterp->os_outdata->d_buf,
3125 		    ofl->ofl_interp);
3126 
3127 	/*
3128 	 * Update the .shstrtab, .strtab and .dynstr sections.
3129 	 */
3130 	update_ostrtab(ofl->ofl_osshstrtab, ofl->ofl_shdrsttab);
3131 	update_ostrtab(ofl->ofl_osstrtab, ofl->ofl_strtab);
3132 	update_ostrtab(ofl->ofl_osdynstr, ofl->ofl_dynstrtab);
3133 
3134 	/*
3135 	 * Build any output symbol tables, the symbols information is copied
3136 	 * and updated into the new output image.
3137 	 */
3138 	if ((etext = update_osym(ofl)) == (Addr)S_ERROR)
3139 		return (S_ERROR);
3140 
3141 	/*
3142 	 * If we have a PT_SUNWDTRACE phdr, update it now with the address of
3143 	 * the symbol.  It's only now been updated via update_sym().
3144 	 */
3145 	if (dtracephdr && ofl->ofl_dtracesym) {
3146 		Phdr		*pphdr;
3147 		Sym_desc	*sdp = ofl->ofl_dtracesym;
3148 
3149 		dtracephdr->p_vaddr = sdp->sd_sym->st_value;
3150 		dtracephdr->p_memsz = sdp->sd_sym->st_size;
3151 
3152 		/*
3153 		 * Take permisions of the segment the symbol is associated with.
3154 		 */
3155 		pphdr = &sdp->sd_isc->is_osdesc->os_sgdesc->sg_phdr;
3156 		assert(pphdr);
3157 		dtracephdr->p_flags = pphdr->p_flags;
3158 	}
3159 
3160 	/*
3161 	 * Update the GROUP sections.
3162 	 */
3163 	if (update_ogroup(ofl) == S_ERROR)
3164 		return (S_ERROR);
3165 
3166 	/*
3167 	 * Update Move Table.
3168 	 */
3169 	if (ofl->ofl_osmove || ofl->ofl_issunwdata1) {
3170 		if (update_move(ofl) == S_ERROR)
3171 			return (S_ERROR);
3172 	}
3173 
3174 	/*
3175 	 * Build any output headers, version information, dynamic structure and
3176 	 * syminfo structure.
3177 	 */
3178 	if (update_oehdr(ofl) == S_ERROR)
3179 		return (S_ERROR);
3180 	if ((flags & (FLG_OF_VERDEF | FLG_OF_NOVERSEC)) == FLG_OF_VERDEF)
3181 		if (update_overdef(ofl) == S_ERROR)
3182 			return (S_ERROR);
3183 	if ((flags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
3184 		if (update_overneed(ofl) == S_ERROR)
3185 			return (S_ERROR);
3186 	if (flags & FLG_OF_DYNAMIC) {
3187 		if (update_odynamic(ofl) == S_ERROR)
3188 			return (S_ERROR);
3189 		if (ofl->ofl_ossyminfo)
3190 			if (update_osyminfo(ofl) == S_ERROR)
3191 				return (S_ERROR);
3192 	}
3193 
3194 	/*
3195 	 * Emit Strtab diagnostics.
3196 	 */
3197 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osshstrtab,
3198 	    ofl->ofl_shdrsttab));
3199 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osstrtab,
3200 	    ofl->ofl_strtab));
3201 	DBG_CALL(Dbg_sec_strtab(ofl->ofl_lml, ofl->ofl_osdynstr,
3202 	    ofl->ofl_dynstrtab));
3203 
3204 	/*
3205 	 * Initialize the section headers string table index within the elf
3206 	 * header.
3207 	 */
3208 	/* LINTED */
3209 	if ((shscnndx = elf_ndxscn(ofl->ofl_osshstrtab->os_scn)) <
3210 	    SHN_LORESERVE) {
3211 		ofl->ofl_nehdr->e_shstrndx =
3212 		    /* LINTED */
3213 		    (Half)shscnndx;
3214 	} else {
3215 		/*
3216 		 * If the STRTAB section index doesn't fit into
3217 		 * e_shstrndx, then we store it in 'shdr[0].st_link'.
3218 		 */
3219 		Elf_Scn	*scn;
3220 		Shdr	*shdr0;
3221 
3222 		if ((scn = elf_getscn(ofl->ofl_elf, 0)) == NULL) {
3223 			eprintf(ofl->ofl_lml, ERR_ELF,
3224 			    MSG_INTL(MSG_ELF_GETSCN), ofl->ofl_name);
3225 			return (S_ERROR);
3226 		}
3227 		if ((shdr0 = elf_getshdr(scn)) == NULL) {
3228 			eprintf(ofl->ofl_lml, ERR_ELF,
3229 			    MSG_INTL(MSG_ELF_GETSHDR), ofl->ofl_name);
3230 			return (S_ERROR);
3231 		}
3232 		ofl->ofl_nehdr->e_shstrndx = SHN_XINDEX;
3233 		shdr0->sh_link = shscnndx;
3234 	}
3235 
3236 	return ((uintptr_t)etext);
3237 }
3238