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