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