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