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