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