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