xref: /titanic_51/usr/src/cmd/sgs/libld/common/syms.c (revision 495db6fbcd9e8ab893e91f6f5627df6e0cec63cf)
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  *
27  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /*
32  * Symbol table management routines
33  */
34 
35 #define	ELF_TARGET_AMD64
36 
37 #include	<stdio.h>
38 #include	<string.h>
39 #include	<debug.h>
40 #include	"msg.h"
41 #include	"_libld.h"
42 
43 /*
44  * AVL tree comparator function:
45  *
46  * The primary key is the symbol name hash with a secondary key of the symbol
47  * name itself.
48  */
49 int
50 ld_sym_avl_comp(const void *elem1, const void *elem2)
51 {
52 	Sym_avlnode	*sav1 = (Sym_avlnode *)elem1;
53 	Sym_avlnode	*sav2 = (Sym_avlnode *)elem2;
54 	int		res;
55 
56 	res = sav1->sav_hash - sav2->sav_hash;
57 
58 	if (res < 0)
59 		return (-1);
60 	if (res > 0)
61 		return (1);
62 
63 	/*
64 	 * Hash is equal - now compare name
65 	 */
66 	res = strcmp(sav1->sav_name, sav2->sav_name);
67 	if (res == 0)
68 		return (0);
69 	if (res > 0)
70 		return (1);
71 	return (-1);
72 }
73 
74 /*
75  * Focal point for verifying symbol names.
76  */
77 inline static const char *
78 string(Ofl_desc *ofl, Ifl_desc *ifl, Sym *sym, const char *strs, size_t strsize,
79     int symndx, Word shndx, Word symsecndx, const char *symsecname,
80     const char *strsecname, Word *flags)
81 {
82 	Word	name = sym->st_name;
83 
84 	if (name) {
85 		if ((ifl->ifl_flags & FLG_IF_HSTRTAB) == 0) {
86 			eprintf(ofl->ofl_lml, ERR_FATAL,
87 			    MSG_INTL(MSG_FIL_NOSTRTABLE), ifl->ifl_name,
88 			    EC_WORD(symsecndx), symsecname, symndx,
89 			    EC_XWORD(name));
90 			return (NULL);
91 		}
92 		if (name >= (Word)strsize) {
93 			eprintf(ofl->ofl_lml, ERR_FATAL,
94 			    MSG_INTL(MSG_FIL_EXCSTRTABLE), ifl->ifl_name,
95 			    EC_WORD(symsecndx), symsecname, symndx,
96 			    EC_XWORD(name), strsecname, EC_XWORD(strsize));
97 			return (NULL);
98 		}
99 	}
100 
101 	/*
102 	 * Determine if we're dealing with a register and if so validate it.
103 	 * If it's a scratch register, a fabricated name will be returned.
104 	 */
105 	if (ld_targ.t_ms.ms_is_regsym != NULL) {
106 		const char *regname = (*ld_targ.t_ms.ms_is_regsym)(ofl, ifl,
107 		    sym, strs, symndx, shndx, symsecname, flags);
108 
109 		if (regname == (const char *)S_ERROR) {
110 			return (NULL);
111 		}
112 		if (regname)
113 			return (regname);
114 	}
115 
116 	/*
117 	 * If this isn't a register, but we have a global symbol with a null
118 	 * name, we're not going to be able to hash this, search for it, or
119 	 * do anything interesting.  However, we've been accepting a symbol of
120 	 * this kind for ages now, so give the user a warning (rather than a
121 	 * fatal error), just in case this instance exists somewhere in the
122 	 * world and hasn't, as yet, been a problem.
123 	 */
124 	if ((name == 0) && (ELF_ST_BIND(sym->st_info) != STB_LOCAL)) {
125 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_NONAMESYM),
126 		    ifl->ifl_name, EC_WORD(symsecndx), symsecname, symndx,
127 		    EC_XWORD(name));
128 	}
129 	return (strs + name);
130 }
131 
132 /*
133  * For producing symbol names strings to use in error messages.
134  * If the symbol has a non-null name, then the string returned by
135  * this function is the output from demangle(), surrounded by
136  * single quotes. For null names, a descriptive string giving
137  * the symbol section and index is generated.
138  *
139  * This function uses an internal static buffer to hold the resulting
140  * string. The value returned is usable by the caller until the next
141  * call, at which point it is overwritten.
142  */
143 static const char *
144 demangle_symname(const char *name, const char *symtab_name, Word symndx)
145 {
146 #define	INIT_BUFSIZE 256
147 
148 	static char	*buf;
149 	static size_t	bufsize = 0;
150 	size_t		len;
151 	int		use_name;
152 
153 	use_name = (name != NULL) && (*name != '\0');
154 
155 	if (use_name) {
156 		name = demangle(name);
157 		len = strlen(name) + 2;   /* Include room for quotes */
158 	} else {
159 		name = MSG_ORIG(MSG_STR_EMPTY);
160 		len = strlen(symtab_name) + 2 + CONV_INV_BUFSIZE;
161 	}
162 	len++;			/* Null termination */
163 
164 	/* If our buffer is too small, double it until it is big enough */
165 	if (len > bufsize) {
166 		size_t	new_bufsize = bufsize;
167 		char	*new_buf;
168 
169 		if (new_bufsize == 0)
170 			new_bufsize = INIT_BUFSIZE;
171 		while (len > new_bufsize)
172 			new_bufsize *= 2;
173 		if ((new_buf = libld_malloc(new_bufsize)) == NULL)
174 			return (name);
175 		buf = new_buf;
176 		bufsize = new_bufsize;
177 	}
178 
179 	if (use_name) {
180 		(void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_SYMNAM), name);
181 	} else {
182 		(void) snprintf(buf, bufsize, MSG_ORIG(MSG_FMT_NULLSYMNAM),
183 		    symtab_name, EC_WORD(symndx));
184 	}
185 
186 	return (buf);
187 
188 #undef INIT_BUFSIZE
189 }
190 
191 /*
192  * Shared objects can be built that define specific symbols that can not be
193  * directly bound to.  These objects have a syminfo section (and an associated
194  * DF_1_NODIRECT dynamic flags entry).  Scan this table looking for symbols
195  * that can't be bound to directly, and if this files symbol is presently
196  * referenced, mark it so that we don't directly bind to it.
197  */
198 uintptr_t
199 ld_sym_nodirect(Is_desc *isp, Ifl_desc *ifl, Ofl_desc *ofl)
200 {
201 	Shdr		*sifshdr, *symshdr;
202 	Syminfo		*sifdata;
203 	Sym		*symdata;
204 	char		*strdata;
205 	ulong_t		cnt, _cnt;
206 
207 	/*
208 	 * Get the syminfo data, and determine the number of entries.
209 	 */
210 	sifshdr = isp->is_shdr;
211 	sifdata = (Syminfo *)isp->is_indata->d_buf;
212 	cnt =  sifshdr->sh_size / sifshdr->sh_entsize;
213 
214 	/*
215 	 * Get the associated symbol table.
216 	 */
217 	if ((sifshdr->sh_link == 0) || (sifshdr->sh_link >= ifl->ifl_shnum)) {
218 		/*
219 		 * Broken input file
220 		 */
221 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
222 		    ifl->ifl_name, isp->is_name, EC_XWORD(sifshdr->sh_link));
223 		ofl->ofl_flags |= FLG_OF_FATAL;
224 		return (0);
225 	}
226 	symshdr = ifl->ifl_isdesc[sifshdr->sh_link]->is_shdr;
227 	symdata = ifl->ifl_isdesc[sifshdr->sh_link]->is_indata->d_buf;
228 
229 	/*
230 	 * Get the string table associated with the symbol table.
231 	 */
232 	strdata = ifl->ifl_isdesc[symshdr->sh_link]->is_indata->d_buf;
233 
234 	/*
235 	 * Traverse the syminfo data for symbols that can't be directly
236 	 * bound to.
237 	 */
238 	for (_cnt = 1, sifdata++; _cnt < cnt; _cnt++, sifdata++) {
239 		Sym		*sym;
240 		char		*str;
241 		Sym_desc	*sdp;
242 
243 		if ((sifdata->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0)
244 			continue;
245 
246 		sym = (Sym *)(symdata + _cnt);
247 		str = (char *)(strdata + sym->st_name);
248 
249 		if ((sdp = ld_sym_find(str, SYM_NOHASH, NULL, ofl)) != NULL) {
250 			if (ifl != sdp->sd_file)
251 				continue;
252 
253 			sdp->sd_flags1 &= ~FLG_SY1_DIR;
254 			sdp->sd_flags1 |= FLG_SY1_NDIR;
255 		}
256 	}
257 	return (0);
258 }
259 
260 /*
261  * If, during symbol processing, it is necessary to update a local symbols
262  * contents before we have generated the symbol tables in the output image,
263  * create a new symbol structure and copy the original symbol contents.  While
264  * we are processing the input files, their local symbols are part of the
265  * read-only mapped image.  Commonly, these symbols are copied to the new output
266  * file image and then updated to reflect their new address and any change in
267  * attributes.  However, sometimes during relocation counting, it is necessary
268  * to adjust the symbols information.  This routine provides for the generation
269  * of a new symbol image so that this update can be performed.
270  * All global symbols are copied to an internal symbol table to improve locality
271  * of reference and hence performance, and thus this copying is not necessary.
272  */
273 uintptr_t
274 ld_sym_copy(Sym_desc *sdp)
275 {
276 	Sym	*nsym;
277 
278 	if (sdp->sd_flags & FLG_SY_CLEAN) {
279 		if ((nsym = libld_malloc(sizeof (Sym))) == NULL)
280 			return (S_ERROR);
281 		*nsym = *(sdp->sd_sym);
282 		sdp->sd_sym = nsym;
283 		sdp->sd_flags &= ~FLG_SY_CLEAN;
284 	}
285 	return (1);
286 }
287 
288 /*
289  * Finds a given name in the link editors internal symbol table.  If no
290  * hash value is specified it is calculated.  A pointer to the located
291  * Sym_desc entry is returned, or NULL if the symbol is not found.
292  */
293 Sym_desc *
294 ld_sym_find(const char *name, Word hash, avl_index_t *where, Ofl_desc *ofl)
295 {
296 	Sym_avlnode	qsav, *sav;
297 
298 	if (hash == SYM_NOHASH)
299 		/* LINTED */
300 		hash = (Word)elf_hash((const char *)name);
301 	qsav.sav_hash = hash;
302 	qsav.sav_name = name;
303 
304 	/*
305 	 * Perform search for symbol in AVL tree.  Note that the 'where' field
306 	 * is passed in from the caller.  If a 'where' is present, it can be
307 	 * used in subsequent 'ld_sym_enter()' calls if required.
308 	 */
309 	sav = avl_find(&ofl->ofl_symavl, &qsav, where);
310 
311 	/*
312 	 * If symbol was not found in the avl tree, return null to show that.
313 	 */
314 	if (sav == NULL)
315 		return (NULL);
316 
317 	/*
318 	 * Return symbol found.
319 	 */
320 	return (sav->sav_symdesc);
321 }
322 
323 /*
324  * Enter a new symbol into the link editors internal symbol table.
325  * If the symbol is from an input file, information regarding the input file
326  * and input section is also recorded.  Otherwise (file == NULL) the symbol
327  * has been internally generated (ie. _etext, _edata, etc.).
328  */
329 Sym_desc *
330 ld_sym_enter(const char *name, Sym *osym, Word hash, Ifl_desc *ifl,
331     Ofl_desc *ofl, Word ndx, Word shndx, Word sdflags, Half sdflags1,
332     avl_index_t *where)
333 {
334 	Sym_desc	*sdp;
335 	Sym_aux		*sap;
336 	Sym_avlnode	*savl;
337 	char		*_name;
338 	Sym		*nsym;
339 	Half		etype;
340 	uchar_t		vis;
341 	avl_index_t	_where;
342 
343 	/*
344 	 * Establish the file type.
345 	 */
346 	if (ifl)
347 		etype = ifl->ifl_ehdr->e_type;
348 	else
349 		etype = ET_NONE;
350 
351 	ofl->ofl_entercnt++;
352 
353 	/*
354 	 * Allocate a Sym Descriptor, Auxiliary Descriptor, and a Sym AVLNode -
355 	 * contiguously.
356 	 */
357 	if ((savl = libld_calloc(sizeof (Sym_avlnode) + sizeof (Sym_desc) +
358 	    sizeof (Sym_aux), 1)) == NULL)
359 		return ((Sym_desc *)S_ERROR);
360 	sdp = (Sym_desc *)((uintptr_t)savl + sizeof (Sym_avlnode));
361 	sap = (Sym_aux *)((uintptr_t)sdp + sizeof (Sym_desc));
362 
363 	savl->sav_symdesc = sdp;
364 	sdp->sd_file = ifl;
365 	sdp->sd_aux = sap;
366 	savl->sav_hash = sap->sa_hash = hash;
367 
368 	/*
369 	 * Copy the symbol table entry from the input file into the internal
370 	 * entry and have the symbol descriptor use it.
371 	 */
372 	sdp->sd_sym = nsym = &sap->sa_sym;
373 	*nsym = *osym;
374 	sdp->sd_shndx = shndx;
375 	sdp->sd_flags |= sdflags;
376 	sdp->sd_flags1 |= sdflags1;
377 
378 	if ((_name = libld_malloc(strlen(name) + 1)) == NULL)
379 		return ((Sym_desc *)S_ERROR);
380 	savl->sav_name = sdp->sd_name = (const char *)strcpy(_name, name);
381 
382 	/*
383 	 * Enter Symbol in AVL tree.
384 	 */
385 	if (where == 0) {
386 		/* LINTED */
387 		Sym_avlnode	*_savl;
388 		/*
389 		 * If a previous ld_sym_find() hasn't initialized 'where' do it
390 		 * now.
391 		 */
392 		where = &_where;
393 		_savl = avl_find(&ofl->ofl_symavl, savl, where);
394 		assert(_savl == NULL);
395 	}
396 	avl_insert(&ofl->ofl_symavl, savl, *where);
397 
398 	/*
399 	 * Record the section index.  This is possible because the
400 	 * `ifl_isdesc' table is filled before we start symbol processing.
401 	 */
402 	if ((sdflags & FLG_SY_SPECSEC) || (nsym->st_shndx == SHN_UNDEF))
403 		sdp->sd_isc = NULL;
404 	else {
405 		sdp->sd_isc = ifl->ifl_isdesc[shndx];
406 
407 		/*
408 		 * If this symbol is from a relocatable object, make sure that
409 		 * it is still associated with a section.  For example, an
410 		 * unknown section type (SHT_NULL) would have been rejected on
411 		 * input with a warning.  Here, we make the use of the symbol
412 		 * fatal.  A symbol descriptor is still returned, so that the
413 		 * caller can continue processing all symbols, and hence flush
414 		 * out as many error conditions as possible.
415 		 */
416 		if ((etype == ET_REL) && (sdp->sd_isc == NULL)) {
417 			eprintf(ofl->ofl_lml, ERR_FATAL,
418 			    MSG_INTL(MSG_SYM_INVSEC), name, ifl->ifl_name,
419 			    EC_XWORD(shndx));
420 			ofl->ofl_flags |= FLG_OF_FATAL;
421 			return (sdp);
422 		}
423 	}
424 
425 	/*
426 	 * Mark any COMMON symbols as 'tentative'.
427 	 */
428 	if (sdflags & FLG_SY_SPECSEC) {
429 		if (nsym->st_shndx == SHN_COMMON)
430 			sdp->sd_flags |= FLG_SY_TENTSYM;
431 #if	defined(_ELF64)
432 		else if ((ld_targ.t_m.m_mach == EM_AMD64) &&
433 		    (nsym->st_shndx == SHN_X86_64_LCOMMON))
434 			sdp->sd_flags |= FLG_SY_TENTSYM;
435 #endif
436 	}
437 
438 	/*
439 	 * Establish the symbols visibility and reference.
440 	 */
441 	vis = ELF_ST_VISIBILITY(nsym->st_other);
442 
443 	if ((etype == ET_NONE) || (etype == ET_REL)) {
444 		switch (vis) {
445 		case STV_DEFAULT:
446 			sdp->sd_flags1 |= FLG_SY1_DEFAULT;
447 			break;
448 		case STV_INTERNAL:
449 		case STV_HIDDEN:
450 			sdp->sd_flags1 |= FLG_SY1_HIDDEN;
451 			break;
452 		case STV_PROTECTED:
453 			sdp->sd_flags1 |= FLG_SY1_PROTECT;
454 			break;
455 		case STV_EXPORTED:
456 			sdp->sd_flags1 |= FLG_SY1_EXPORT;
457 			break;
458 		case STV_SINGLETON:
459 			sdp->sd_flags1 |= (FLG_SY1_SINGLE | FLG_SY1_NDIR);
460 			ofl->ofl_flags1 |= (FLG_OF1_NDIRECT | FLG_OF1_NGLBDIR);
461 			break;
462 		case STV_ELIMINATE:
463 			sdp->sd_flags1 |= (FLG_SY1_HIDDEN | FLG_SY1_ELIM);
464 			break;
465 		default:
466 			assert(vis <= STV_ELIMINATE);
467 		}
468 
469 		sdp->sd_ref = REF_REL_NEED;
470 
471 		/*
472 		 * Under -Bnodirect, all exported interfaces that have not
473 		 * explicitly been defined protected or directly bound to, are
474 		 * tagged to prevent direct binding.
475 		 */
476 		if ((ofl->ofl_flags1 & FLG_OF1_ALNODIR) &&
477 		    ((sdp->sd_flags1 & (FLG_SY1_PROTECT | FLG_SY1_DIR)) == 0) &&
478 		    (nsym->st_shndx != SHN_UNDEF)) {
479 			sdp->sd_flags1 |= FLG_SY1_NDIR;
480 		}
481 	} else {
482 		sdp->sd_ref = REF_DYN_SEEN;
483 
484 		/*
485 		 * Record the binding file for this symbol in the sa_bindto
486 		 * field.  If this symbol is ever overridden by a REF_REL_NEED
487 		 * definition, sa_bindto is used when building a 'translator'.
488 		 */
489 		if (nsym->st_shndx != SHN_UNDEF)
490 			sdp->sd_aux->sa_bindto = ifl;
491 
492 		/*
493 		 * If this is a protected symbol, remember this.  Note, this
494 		 * state is different from the FLG_SY1_PROTECT used to establish
495 		 * a symbol definitions visibility.  This state is used to warn
496 		 * against possible copy relocations against this referenced
497 		 * symbol.
498 		 */
499 		if (vis == STV_PROTECTED)
500 			sdp->sd_flags |= FLG_SY_PROT;
501 
502 		/*
503 		 * If this is a SINGLETON definition, then indicate the symbol
504 		 * can not be directly bound to, and retain the visibility.
505 		 * This visibility will be inherited by any references made to
506 		 * this symbol.
507 		 */
508 		if ((vis == STV_SINGLETON) && (nsym->st_shndx != SHN_UNDEF))
509 			sdp->sd_flags1 |= (FLG_SY1_SINGLE | FLG_SY1_NDIR);
510 
511 		/*
512 		 * If the new symbol is from a shared library and is associated
513 		 * with a SHT_NOBITS section then this symbol originated from a
514 		 * tentative symbol.
515 		 */
516 		if (sdp->sd_isc &&
517 		    (sdp->sd_isc->is_shdr->sh_type == SHT_NOBITS))
518 			sdp->sd_flags |= FLG_SY_TENTSYM;
519 	}
520 
521 	/*
522 	 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF so as to
523 	 * simplify future processing.
524 	 */
525 	if (nsym->st_shndx == SHN_SUNW_IGNORE) {
526 		sdp->sd_shndx = shndx = SHN_UNDEF;
527 		sdp->sd_flags |= FLG_SY_REDUCED;
528 		sdp->sd_flags1 |=
529 		    (FLG_SY1_HIDDEN | FLG_SY1_IGNORE | FLG_SY1_ELIM);
530 	}
531 
532 	/*
533 	 * If this is an undefined, or common symbol from a relocatable object
534 	 * determine whether it is a global or weak reference (see build_osym(),
535 	 * where REF_DYN_NEED definitions are returned back to undefines).
536 	 */
537 	if ((etype == ET_REL) &&
538 	    (ELF_ST_BIND(nsym->st_info) == STB_GLOBAL) &&
539 	    ((nsym->st_shndx == SHN_UNDEF) || ((sdflags & FLG_SY_SPECSEC) &&
540 #if	defined(_ELF64)
541 	    ((nsym->st_shndx == SHN_COMMON) ||
542 	    ((ld_targ.t_m.m_mach == EM_AMD64) &&
543 	    (nsym->st_shndx == SHN_X86_64_LCOMMON))))))
544 #else
545 	/* BEGIN CSTYLED */
546 	    (nsym->st_shndx == SHN_COMMON))))
547 	/* END CSTYLED */
548 #endif
549 		sdp->sd_flags |= FLG_SY_GLOBREF;
550 
551 	/*
552 	 * Record the input filename on the referenced or defined files list
553 	 * for possible later diagnostics.  The `sa_rfile' pointer contains the
554 	 * name of the file that first referenced this symbol and is used to
555 	 * generate undefined symbol diagnostics (refer to sym_undef_entry()).
556 	 * Note that this entry can be overridden if a reference from a
557 	 * relocatable object is found after a reference from a shared object
558 	 * (refer to sym_override()).
559 	 * The `sa_dfiles' list is used to maintain the list of files that
560 	 * define the same symbol.  This list can be used for two reasons:
561 	 *
562 	 *   -	To save the first definition of a symbol that is not available
563 	 *	for this link-edit.
564 	 *
565 	 *   -	To save all definitions of a symbol when the -m option is in
566 	 *	effect.  This is optional as it is used to list multiple
567 	 *	(interposed) definitions of a symbol (refer to ldmap_out()),
568 	 *	and can be quite expensive.
569 	 */
570 	if (nsym->st_shndx == SHN_UNDEF) {
571 		sap->sa_rfile = ifl->ifl_name;
572 	} else {
573 		if (sdp->sd_ref == REF_DYN_SEEN) {
574 			/*
575 			 * A symbol is determined to be unavailable if it
576 			 * belongs to a version of a shared object that this
577 			 * user does not wish to use, or if it belongs to an
578 			 * implicit shared object.
579 			 */
580 			if (ifl->ifl_vercnt) {
581 				Ver_index	*vip;
582 				Half		vndx = ifl->ifl_versym[ndx];
583 
584 				sap->sa_dverndx = vndx;
585 				vip = &ifl->ifl_verndx[vndx];
586 				if (!(vip->vi_flags & FLG_VER_AVAIL)) {
587 					sdp->sd_flags |= FLG_SY_NOTAVAIL;
588 					sap->sa_vfile = ifl->ifl_name;
589 				}
590 			}
591 			if (!(ifl->ifl_flags & FLG_IF_NEEDED))
592 				sdp->sd_flags |= FLG_SY_NOTAVAIL;
593 
594 		} else if (etype == ET_REL) {
595 			/*
596 			 * If this symbol has been obtained from a versioned
597 			 * input relocatable object then the new symbol must be
598 			 * promoted to the versioning of the output file.
599 			 */
600 			if (ifl->ifl_versym)
601 				ld_vers_promote(sdp, ndx, ifl, ofl);
602 		}
603 
604 		if ((ofl->ofl_flags & FLG_OF_GENMAP) &&
605 		    ((sdflags & FLG_SY_SPECSEC) == 0))
606 			if (aplist_append(&sap->sa_dfiles, ifl->ifl_name,
607 			    AL_CNT_SDP_DFILES) == NULL)
608 				return ((Sym_desc *)S_ERROR);
609 	}
610 
611 	/*
612 	 * Provided we're not processing a mapfile, diagnose the entered symbol.
613 	 * Mapfile processing requires the symbol to be updated with additional
614 	 * information, therefore the diagnosing of the symbol is deferred until
615 	 * later (see Dbg_map_symbol()).
616 	 */
617 	if ((ifl == NULL) || ((ifl->ifl_flags & FLG_IF_MAPFILE) == 0))
618 		DBG_CALL(Dbg_syms_entered(ofl, nsym, sdp));
619 	return (sdp);
620 }
621 
622 /*
623  * Add a special symbol to the symbol table.  Takes special symbol name with
624  * and without underscores.  This routine is called, after all other symbol
625  * resolution has completed, to generate a reserved absolute symbol (the
626  * underscore version).  Special symbols are updated with the appropriate
627  * values in update_osym().  If the user has already defined this symbol
628  * issue a warning and leave the symbol as is.  If the non-underscore symbol
629  * is referenced then turn it into a weak alias of the underscored symbol.
630  *
631  * The bits in flags_u are OR'd into the flags field of the symbol
632  * for the underscored symbol.
633  *
634  * If this is a global symbol, and it hasn't explicitly been defined as being
635  * directly bound to, indicate that it can't be directly bound to.
636  * Historically, most special symbols only have meaning to the object in which
637  * they exist, however, they've always been global.  To ensure compatibility
638  * with any unexpected use presently in effect, ensure these symbols don't get
639  * directly bound to.  Note, that establishing this state here isn't sufficient
640  * to create a syminfo table, only if a syminfo table is being created by some
641  * other symbol directives will the nodirect binding be recorded.  This ensures
642  * we don't create syminfo sections for all objects we create, as this might add
643  * unnecessary bloat to users who haven't explicitly requested extra symbol
644  * information.
645  */
646 static uintptr_t
647 sym_add_spec(const char *name, const char *uname, Word sdaux_id,
648     Word flags_u, Half flags1, Ofl_desc *ofl)
649 {
650 	Sym_desc	*sdp;
651 	Sym_desc 	*usdp;
652 	Sym		*sym;
653 	Word		hash;
654 	avl_index_t	where;
655 
656 	/* LINTED */
657 	hash = (Word)elf_hash(uname);
658 	if (usdp = ld_sym_find(uname, hash, &where, ofl)) {
659 		/*
660 		 * If the underscore symbol exists and is undefined, or was
661 		 * defined in a shared library, convert it to a local symbol.
662 		 * Otherwise leave it as is and warn the user.
663 		 */
664 		if ((usdp->sd_shndx == SHN_UNDEF) ||
665 		    (usdp->sd_ref != REF_REL_NEED)) {
666 			usdp->sd_ref = REF_REL_NEED;
667 			usdp->sd_shndx = usdp->sd_sym->st_shndx = SHN_ABS;
668 			usdp->sd_flags |= FLG_SY_SPECSEC | flags_u;
669 			usdp->sd_sym->st_info =
670 			    ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
671 			usdp->sd_isc = NULL;
672 			usdp->sd_sym->st_size = 0;
673 			usdp->sd_sym->st_value = 0;
674 			/* LINTED */
675 			usdp->sd_aux->sa_symspec = (Half)sdaux_id;
676 
677 			/*
678 			 * If a user hasn't specifically indicated that the
679 			 * scope of this symbol be made local, then leave it
680 			 * as global (ie. prevent automatic scoping).  The GOT
681 			 * should be defined protected, whereas all other
682 			 * special symbols are tagged as no-direct.
683 			 */
684 			if (((usdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) &&
685 			    (flags1 & FLG_SY1_DEFAULT)) {
686 				usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
687 				if (sdaux_id == SDAUX_ID_GOT) {
688 					usdp->sd_flags1 &= ~FLG_SY1_NDIR;
689 					usdp->sd_flags1 |= FLG_SY1_PROTECT;
690 					usdp->sd_sym->st_other = STV_PROTECTED;
691 				} else if (
692 				    ((usdp->sd_flags1 & FLG_SY1_DIR) == 0) &&
693 				    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
694 					usdp->sd_flags1 |= FLG_SY1_NDIR;
695 				}
696 			}
697 			usdp->sd_flags1 |= flags1;
698 
699 			/*
700 			 * If the reference originated from a mapfile ensure
701 			 * we mark the symbol as used.
702 			 */
703 			if (usdp->sd_flags & FLG_SY_MAPREF)
704 				usdp->sd_flags |= FLG_SY_MAPUSED;
705 
706 			DBG_CALL(Dbg_syms_updated(ofl, usdp, uname));
707 		} else
708 			eprintf(ofl->ofl_lml, ERR_WARNING,
709 			    MSG_INTL(MSG_SYM_RESERVE), uname,
710 			    usdp->sd_file->ifl_name);
711 	} else {
712 		/*
713 		 * If the symbol does not exist create it.
714 		 */
715 		if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
716 			return (S_ERROR);
717 		sym->st_shndx = SHN_ABS;
718 		sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
719 		sym->st_size = 0;
720 		sym->st_value = 0;
721 		DBG_CALL(Dbg_syms_created(ofl->ofl_lml, uname));
722 		if ((usdp = ld_sym_enter(uname, sym, hash, (Ifl_desc *)NULL,
723 		    ofl, 0, SHN_ABS, FLG_SY_SPECSEC | flags_u, 0, &where)) ==
724 		    (Sym_desc *)S_ERROR)
725 			return (S_ERROR);
726 		usdp->sd_ref = REF_REL_NEED;
727 		/* LINTED */
728 		usdp->sd_aux->sa_symspec = (Half)sdaux_id;
729 
730 		usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
731 
732 		if (sdaux_id == SDAUX_ID_GOT) {
733 			usdp->sd_flags1 |= FLG_SY1_PROTECT;
734 			usdp->sd_sym->st_other = STV_PROTECTED;
735 		} else if ((flags1 & FLG_SY1_DEFAULT) &&
736 		    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
737 			usdp->sd_flags1 |= FLG_SY1_NDIR;
738 		}
739 		usdp->sd_flags1 |= flags1;
740 	}
741 
742 	if (name && (sdp = ld_sym_find(name, SYM_NOHASH, NULL, ofl)) &&
743 	    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
744 		uchar_t	bind;
745 
746 		/*
747 		 * If the non-underscore symbol exists and is undefined
748 		 * convert it to be a local.  If the underscore has
749 		 * sa_symspec set (ie. it was created above) then simulate this
750 		 * as a weak alias.
751 		 */
752 		sdp->sd_ref = REF_REL_NEED;
753 		sdp->sd_shndx = sdp->sd_sym->st_shndx = SHN_ABS;
754 		sdp->sd_flags |= FLG_SY_SPECSEC;
755 		sdp->sd_isc = NULL;
756 		sdp->sd_sym->st_size = 0;
757 		sdp->sd_sym->st_value = 0;
758 		/* LINTED */
759 		sdp->sd_aux->sa_symspec = (Half)sdaux_id;
760 		if (usdp->sd_aux->sa_symspec) {
761 			usdp->sd_aux->sa_linkndx = 0;
762 			sdp->sd_aux->sa_linkndx = 0;
763 			bind = STB_WEAK;
764 		} else
765 			bind = STB_GLOBAL;
766 		sdp->sd_sym->st_info = ELF_ST_INFO(bind, STT_OBJECT);
767 
768 		/*
769 		 * If a user hasn't specifically indicated the scope of this
770 		 * symbol be made local then leave it as global (ie. prevent
771 		 * automatic scoping).  The GOT should be defined protected,
772 		 * whereas all other special symbols are tagged as no-direct.
773 		 */
774 		if (((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) &&
775 		    (flags1 & FLG_SY1_DEFAULT)) {
776 			sdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
777 			if (sdaux_id == SDAUX_ID_GOT) {
778 				sdp->sd_flags1 &= ~FLG_SY1_NDIR;
779 				sdp->sd_flags1 |= FLG_SY1_PROTECT;
780 				sdp->sd_sym->st_other = STV_PROTECTED;
781 			} else if (((sdp->sd_flags1 & FLG_SY1_DIR) == 0) &&
782 			    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
783 				sdp->sd_flags1 |= FLG_SY1_NDIR;
784 			}
785 		}
786 		sdp->sd_flags1 |= flags1;
787 
788 		/*
789 		 * If the reference originated from a mapfile ensure
790 		 * we mark the symbol as used.
791 		 */
792 		if (sdp->sd_flags & FLG_SY_MAPREF)
793 			sdp->sd_flags |= FLG_SY_MAPUSED;
794 
795 		DBG_CALL(Dbg_syms_updated(ofl, sdp, name));
796 	}
797 	return (1);
798 }
799 
800 
801 /*
802  * Print undefined symbols.
803  */
804 static Boolean	undef_title = TRUE;
805 
806 static void
807 sym_undef_title(Ofl_desc *ofl)
808 {
809 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_SYM_FMT_UNDEF),
810 	    MSG_INTL(MSG_SYM_UNDEF_ITM_11),
811 	    MSG_INTL(MSG_SYM_UNDEF_ITM_21),
812 	    MSG_INTL(MSG_SYM_UNDEF_ITM_12),
813 	    MSG_INTL(MSG_SYM_UNDEF_ITM_22));
814 
815 	undef_title = FALSE;
816 }
817 
818 /*
819  * Undefined symbols can fall into one of four types:
820  *
821  *  -	the symbol is really undefined (SHN_UNDEF).
822  *
823  *  -	versioning has been enabled, however this symbol has not been assigned
824  *	to one of the defined versions.
825  *
826  *  -	the symbol has been defined by an implicitly supplied library, ie. one
827  *	which was encounted because it was NEEDED by another library, rather
828  * 	than from a command line supplied library which would become the only
829  *	dependency of the output file being produced.
830  *
831  *  -	the symbol has been defined by a version of a shared object that is
832  *	not permitted for this link-edit.
833  *
834  * In all cases the file who made the first reference to this symbol will have
835  * been recorded via the `sa_rfile' pointer.
836  */
837 typedef enum {
838 	UNDEF,		NOVERSION,	IMPLICIT,	NOTAVAIL,
839 	BNDLOCAL
840 } Type;
841 
842 static const Msg format[] = {
843 	MSG_SYM_UND_UNDEF,		/* MSG_INTL(MSG_SYM_UND_UNDEF) */
844 	MSG_SYM_UND_NOVER,		/* MSG_INTL(MSG_SYM_UND_NOVER) */
845 	MSG_SYM_UND_IMPL,		/* MSG_INTL(MSG_SYM_UND_IMPL) */
846 	MSG_SYM_UND_NOTA,		/* MSG_INTL(MSG_SYM_UND_NOTA) */
847 	MSG_SYM_UND_BNDLOCAL		/* MSG_INTL(MSG_SYM_UND_BNDLOCAL) */
848 };
849 
850 static void
851 sym_undef_entry(Ofl_desc *ofl, Sym_desc *sdp, Type type)
852 {
853 	const char	*name1, *name2, *name3;
854 	Ifl_desc	*ifl = sdp->sd_file;
855 	Sym_aux		*sap = sdp->sd_aux;
856 
857 	if (undef_title)
858 		sym_undef_title(ofl);
859 
860 	switch (type) {
861 	case UNDEF:
862 	case BNDLOCAL:
863 		name1 = sap->sa_rfile;
864 		break;
865 	case NOVERSION:
866 		name1 = ifl->ifl_name;
867 		break;
868 	case IMPLICIT:
869 		name1 = sap->sa_rfile;
870 		name2 = ifl->ifl_name;
871 		break;
872 	case NOTAVAIL:
873 		name1 = sap->sa_rfile;
874 		name2 = sap->sa_vfile;
875 		name3 = ifl->ifl_verndx[sap->sa_dverndx].vi_name;
876 		break;
877 	default:
878 		return;
879 	}
880 
881 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(format[type]),
882 	    demangle(sdp->sd_name), name1, name2, name3);
883 }
884 
885 /*
886  * At this point all symbol input processing has been completed, therefore
887  * complete the symbol table entries by generating any necessary internal
888  * symbols.
889  */
890 uintptr_t
891 ld_sym_spec(Ofl_desc *ofl)
892 {
893 	Sym_desc	*sdp;
894 
895 	if (ofl->ofl_flags & FLG_OF_RELOBJ)
896 		return (1);
897 
898 	DBG_CALL(Dbg_syms_spec_title(ofl->ofl_lml));
899 
900 	if (sym_add_spec(MSG_ORIG(MSG_SYM_ETEXT), MSG_ORIG(MSG_SYM_ETEXT_U),
901 	    SDAUX_ID_ETEXT, 0, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
902 	    ofl) == S_ERROR)
903 		return (S_ERROR);
904 	if (sym_add_spec(MSG_ORIG(MSG_SYM_EDATA), MSG_ORIG(MSG_SYM_EDATA_U),
905 	    SDAUX_ID_EDATA, 0, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
906 	    ofl) == S_ERROR)
907 		return (S_ERROR);
908 	if (sym_add_spec(MSG_ORIG(MSG_SYM_END), MSG_ORIG(MSG_SYM_END_U),
909 	    SDAUX_ID_END, FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
910 	    ofl) == S_ERROR)
911 		return (S_ERROR);
912 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_END), MSG_ORIG(MSG_SYM_L_END_U),
913 	    SDAUX_ID_END, 0, FLG_SY1_HIDDEN, ofl) == S_ERROR)
914 		return (S_ERROR);
915 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_START), MSG_ORIG(MSG_SYM_L_START_U),
916 	    SDAUX_ID_START, 0, FLG_SY1_HIDDEN, ofl) == S_ERROR)
917 		return (S_ERROR);
918 
919 	/*
920 	 * Historically we've always produced a _DYNAMIC symbol, even for
921 	 * static executables (in which case its value will be 0).
922 	 */
923 	if (sym_add_spec(MSG_ORIG(MSG_SYM_DYNAMIC), MSG_ORIG(MSG_SYM_DYNAMIC_U),
924 	    SDAUX_ID_DYN, FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
925 	    ofl) == S_ERROR)
926 		return (S_ERROR);
927 
928 	if (OFL_ALLOW_DYNSYM(ofl))
929 		if (sym_add_spec(MSG_ORIG(MSG_SYM_PLKTBL),
930 		    MSG_ORIG(MSG_SYM_PLKTBL_U), SDAUX_ID_PLT,
931 		    FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
932 		    ofl) == S_ERROR)
933 			return (S_ERROR);
934 
935 	/*
936 	 * A GOT reference will be accompanied by the associated GOT symbol.
937 	 * Make sure it gets assigned the appropriate special attributes.
938 	 */
939 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U),
940 	    SYM_NOHASH, NULL, ofl)) != 0) && (sdp->sd_ref != REF_DYN_SEEN)) {
941 		if (sym_add_spec(MSG_ORIG(MSG_SYM_GOFTBL),
942 		    MSG_ORIG(MSG_SYM_GOFTBL_U), SDAUX_ID_GOT, FLG_SY_DYNSORT,
943 		    (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF), ofl) == S_ERROR)
944 			return (S_ERROR);
945 	}
946 
947 	return (1);
948 }
949 
950 /*
951  * This routine checks to see if a symbols visibility needs to be reduced to
952  * either SYMBOLIC or LOCAL.  This routine can be called from either
953  * reloc_init() or sym_validate().
954  */
955 void
956 ld_sym_adjust_vis(Sym_desc *sdp, Ofl_desc *ofl)
957 {
958 	ofl_flag_t	oflags = ofl->ofl_flags;
959 	Sym		*sym = sdp->sd_sym;
960 
961 	if ((sdp->sd_ref == REF_REL_NEED) &&
962 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
963 		/*
964 		 * If auto-reduction/elimination is enabled, reduce any
965 		 * non-versioned global symbols.  This routine is called either
966 		 * from any initial relocation processing that references this
967 		 * symbol, or from the symbol validation processing.
968 		 *
969 		 * A symbol is a candidate for auto-reduction/elimination if:
970 		 *
971 		 *  -	the symbol wasn't explicitly defined within a mapfile
972 		 *	(in which case all the necessary state has been applied
973 		 *	to the symbol), or
974 		 *  -	the symbol isn't one of the family of reserved
975 		 *	special symbols (ie. _end, _etext, etc.), or
976 		 *  -	the symbol isn't a SINGLETON, or
977 		 *  -	the symbol wasn't explicitly defined within a version
978 		 *	definition associated with an input relocatable object.
979 		 *
980 		 * Indicate that the symbol has been reduced as it may be
981 		 * necessary to print these symbols later.
982 		 */
983 		if ((oflags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) &&
984 		    ((sdp->sd_flags1 & MSK_SY1_NOAUTO) == 0)) {
985 			if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) {
986 				sdp->sd_flags |= FLG_SY_REDUCED;
987 				sdp->sd_flags1 |= FLG_SY1_HIDDEN;
988 			}
989 
990 			if (oflags & (FLG_OF_REDLSYM | FLG_OF_AUTOELM)) {
991 				sdp->sd_flags1 |= FLG_SY1_ELIM;
992 				sym->st_other = STV_ELIMINATE |
993 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
994 			} else if (ELF_ST_VISIBILITY(sym->st_other) !=
995 			    STV_INTERNAL)
996 				sym->st_other = STV_HIDDEN |
997 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
998 		}
999 
1000 		/*
1001 		 * If -Bsymbolic is in effect, and the symbol hasn't explicitly
1002 		 * been defined nodirect (via a mapfile), then bind the global
1003 		 * symbol symbolically and assign the STV_PROTECTED visibility
1004 		 * attribute.
1005 		 */
1006 		if ((oflags & FLG_OF_SYMBOLIC) &&
1007 		    ((sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_NDIR)) == 0)) {
1008 			sdp->sd_flags1 |= FLG_SY1_PROTECT;
1009 			if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT)
1010 				sym->st_other = STV_PROTECTED |
1011 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
1012 		}
1013 	}
1014 
1015 	/*
1016 	 * Indicate that this symbol has had it's visibility checked so that
1017 	 * we don't need to do this investigation again.
1018 	 */
1019 	sdp->sd_flags |= FLG_SY_VISIBLE;
1020 }
1021 
1022 /*
1023  * Make sure a symbol definition is local to the object being built.
1024  */
1025 inline static int
1026 ensure_sym_local(Ofl_desc *ofl, Sym_desc *sdp, const char *str)
1027 {
1028 	if (sdp->sd_sym->st_shndx == SHN_UNDEF) {
1029 		if (str) {
1030 			eprintf(ofl->ofl_lml, ERR_FATAL,
1031 			    MSG_INTL(MSG_SYM_UNDEF), str,
1032 			    demangle((char *)sdp->sd_name));
1033 		}
1034 		return (1);
1035 	}
1036 	if (sdp->sd_ref != REF_REL_NEED) {
1037 		if (str) {
1038 			eprintf(ofl->ofl_lml, ERR_FATAL,
1039 			    MSG_INTL(MSG_SYM_EXTERN), str,
1040 			    demangle((char *)sdp->sd_name),
1041 			    sdp->sd_file->ifl_name);
1042 		}
1043 		return (1);
1044 	}
1045 
1046 	sdp->sd_flags |= FLG_SY_UPREQD;
1047 	if (sdp->sd_isc) {
1048 		sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1049 		sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1050 	}
1051 	return (0);
1052 }
1053 
1054 /*
1055  * Make sure all the symbol definitions required for initarray, finiarray, or
1056  * preinitarray's are local to the object being built.
1057  */
1058 static int
1059 ensure_array_local(Ofl_desc *ofl, APlist *apl, const char *str)
1060 {
1061 	Aliste		idx;
1062 	Sym_desc	*sdp;
1063 	int		ret = 0;
1064 
1065 	for (APLIST_TRAVERSE(apl, idx, sdp))
1066 		ret += ensure_sym_local(ofl, sdp, str);
1067 
1068 	return (ret);
1069 }
1070 
1071 /*
1072  * After all symbol table input processing has been finished, and all relocation
1073  * counting has been carried out (ie. no more symbols will be read, generated,
1074  * or modified), validate and count the relevant entries:
1075  *
1076  *	-	check and print any undefined symbols remaining.  Note that
1077  *		if a symbol has been defined by virtue of the inclusion of
1078  *		an implicit shared library, it is still classed as undefined.
1079  *
1080  * 	-	count the number of global needed symbols together with the
1081  *		size of their associated name strings (if scoping has been
1082  *		indicated these symbols may be reduced to locals).
1083  *
1084  *	-	establish the size and alignment requirements for the global
1085  *		.bss section (the alignment of this section is based on the
1086  *		first symbol that it will contain).
1087  */
1088 uintptr_t
1089 ld_sym_validate(Ofl_desc *ofl)
1090 {
1091 	Sym_avlnode	*sav;
1092 	Sym_desc	*sdp;
1093 	Sym		*sym;
1094 	ofl_flag_t	oflags = ofl->ofl_flags;
1095 	ofl_flag_t	undef = 0, needed = 0, verdesc = 0;
1096 	Xword		bssalign = 0, tlsalign = 0;
1097 	Xword		bsssize = 0, tlssize = 0;
1098 #if	defined(_ELF64)
1099 	Xword		lbssalign = 0, lbsssize = 0;
1100 #endif
1101 	int		ret;
1102 	int		allow_ldynsym;
1103 	uchar_t		type;
1104 
1105 	DBG_CALL(Dbg_basic_validate(ofl->ofl_lml));
1106 
1107 	/*
1108 	 * If a symbol is undefined and this link-edit calls for no undefined
1109 	 * symbols to remain (this is the default case when generating an
1110 	 * executable but can be enforced for any object using -z defs), the
1111 	 * symbol is classified as undefined and a fatal error condition will
1112 	 * be indicated.
1113 	 *
1114 	 * If the symbol is undefined and we're creating a shared object with
1115 	 * the -Bsymbolic flag, then the symbol is also classified as undefined
1116 	 * and a warning condition will be indicated.
1117 	 */
1118 	if ((oflags & (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC)) ==
1119 	    (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC))
1120 		undef = FLG_OF_WARN;
1121 	if (oflags & FLG_OF_NOUNDEF)
1122 		undef = FLG_OF_FATAL;
1123 
1124 	/*
1125 	 * If the symbol is referenced from an implicitly included shared object
1126 	 * (ie. it's not on the NEEDED list) then the symbol is also classified
1127 	 * as undefined and a fatal error condition will be indicated.
1128 	 */
1129 	if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ))
1130 		needed = FLG_OF_FATAL;
1131 
1132 	/*
1133 	 * If the output image is being versioned, then all symbol definitions
1134 	 * must be associated with a version.  Any symbol that isn't associated
1135 	 * with a version is classified as undefined, and a fatal error
1136 	 * condition is indicated.
1137 	 */
1138 	if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL))
1139 		verdesc = FLG_OF_FATAL;
1140 
1141 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1142 
1143 	if (allow_ldynsym) {
1144 		/*
1145 		 * Normally, we disallow symbols with 0 size from appearing
1146 		 * in a dyn[sym|tls]sort section. However, there are some
1147 		 * symbols that serve special purposes that we want to exempt
1148 		 * from this rule. Look them up, and set their
1149 		 * FLG_SY_DYNSORT flag.
1150 		 */
1151 		static const char *special[] = {
1152 			MSG_ORIG(MSG_SYM_INIT_U),	/* _init */
1153 			MSG_ORIG(MSG_SYM_FINI_U),	/* _fini */
1154 			MSG_ORIG(MSG_SYM_START),	/* _start */
1155 			NULL
1156 		};
1157 		int i;
1158 
1159 		for (i = 0; special[i] != NULL; i++) {
1160 			if (((sdp = ld_sym_find(special[i],
1161 			    SYM_NOHASH, NULL, ofl)) != NULL) &&
1162 			    (sdp->sd_sym->st_size == 0)) {
1163 				if (ld_sym_copy(sdp) == S_ERROR)
1164 					return (S_ERROR);
1165 				sdp->sd_flags |= FLG_SY_DYNSORT;
1166 			}
1167 		}
1168 	}
1169 
1170 	/*
1171 	 * Collect and validate the globals from the internal symbol table.
1172 	 */
1173 	for (sav = avl_first(&ofl->ofl_symavl); sav;
1174 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
1175 		Is_desc		*isp;
1176 		int		undeferr = 0;
1177 		uchar_t		vis;
1178 
1179 		sdp = sav->sav_symdesc;
1180 
1181 		/*
1182 		 * If undefined symbols are allowed ignore any symbols that are
1183 		 * not needed.
1184 		 */
1185 		if (!(oflags & FLG_OF_NOUNDEF) &&
1186 		    (sdp->sd_ref == REF_DYN_SEEN))
1187 			continue;
1188 
1189 		/*
1190 		 * If the symbol originates from an external or parent mapfile
1191 		 * reference and hasn't been matched to a reference from a
1192 		 * relocatable object, ignore it.
1193 		 */
1194 		if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) &&
1195 		    ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) {
1196 			sdp->sd_flags |= FLG_SY_INVALID;
1197 			continue;
1198 		}
1199 
1200 		sym = sdp->sd_sym;
1201 		type = ELF_ST_TYPE(sym->st_info);
1202 
1203 		/*
1204 		 * Sanity check TLS.
1205 		 */
1206 		if ((type == STT_TLS) && (sym->st_size != 0) &&
1207 		    (sym->st_shndx != SHN_UNDEF) &&
1208 		    (sym->st_shndx != SHN_COMMON)) {
1209 			Is_desc		*isp = sdp->sd_isc;
1210 			Ifl_desc	*ifl = sdp->sd_file;
1211 
1212 			if ((isp == NULL) || (isp->is_shdr == NULL) ||
1213 			    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
1214 				eprintf(ofl->ofl_lml, ERR_FATAL,
1215 				    MSG_INTL(MSG_SYM_TLS),
1216 				    demangle(sdp->sd_name), ifl->ifl_name);
1217 				ofl->ofl_flags |= FLG_OF_FATAL;
1218 				continue;
1219 			}
1220 		}
1221 
1222 		if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)
1223 			ld_sym_adjust_vis(sdp, ofl);
1224 
1225 		if ((sdp->sd_flags & FLG_SY_REDUCED) &&
1226 		    (oflags & FLG_OF_PROCRED)) {
1227 			DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL,
1228 			    sdp, 0, 0));
1229 		}
1230 
1231 		/*
1232 		 * Record any STV_SINGLETON existence.
1233 		 */
1234 		if ((vis = ELF_ST_VISIBILITY(sym->st_other)) == STV_SINGLETON)
1235 			ofl->ofl_dtflags_1 |= DF_1_SINGLETON;
1236 
1237 		/*
1238 		 * If building a shared object or executable, and this is a
1239 		 * non-weak UNDEF symbol with reduced visibility (STV_*), then
1240 		 * give a fatal error.
1241 		 */
1242 		if (((oflags & FLG_OF_RELOBJ) == 0) &&
1243 		    (sym->st_shndx == SHN_UNDEF) &&
1244 		    (ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
1245 			if (vis && (vis != STV_SINGLETON)) {
1246 				sym_undef_entry(ofl, sdp, BNDLOCAL);
1247 				ofl->ofl_flags |= FLG_OF_FATAL;
1248 				continue;
1249 			}
1250 		}
1251 
1252 		/*
1253 		 * If this symbol is defined in a non-allocatable section,
1254 		 * reduce it to local symbol.
1255 		 */
1256 		if (((isp = sdp->sd_isc) != 0) && isp->is_shdr &&
1257 		    ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) {
1258 			sdp->sd_flags |= FLG_SY_REDUCED;
1259 			sdp->sd_flags1 |= FLG_SY1_HIDDEN;
1260 		}
1261 
1262 		/*
1263 		 * If this symbol originated as a SHN_SUNW_IGNORE, it will have
1264 		 * been processed as an SHN_UNDEF.  Return the symbol to its
1265 		 * original index for validation, and propagation to the output
1266 		 * file.
1267 		 */
1268 		if (sdp->sd_flags1 & FLG_SY1_IGNORE)
1269 			sdp->sd_shndx = SHN_SUNW_IGNORE;
1270 
1271 		if (undef) {
1272 			/*
1273 			 * If a non-weak reference remains undefined, or if a
1274 			 * mapfile reference is not bound to the relocatable
1275 			 * objects that make up the object being built, we have
1276 			 * a fatal error.
1277 			 *
1278 			 * The exceptions are symbols which are defined to be
1279 			 * found in the parent (FLG_SY_PARENT), which is really
1280 			 * only meaningful for direct binding, or are defined
1281 			 * external (FLG_SY_EXTERN) so as to suppress -zdefs
1282 			 * errors.
1283 			 *
1284 			 * Register symbols are always allowed to be UNDEF.
1285 			 *
1286 			 * Note that we don't include references created via -u
1287 			 * in the same shared object binding test.  This is for
1288 			 * backward compatibility, in that a number of archive
1289 			 * makefile rules used -u to cause archive extraction.
1290 			 * These same rules have been cut and pasted to apply
1291 			 * to shared objects, and thus although the -u reference
1292 			 * is redundant, flagging it as fatal could cause some
1293 			 * build to fail.  Also we have documented the use of
1294 			 * -u as a mechanism to cause binding to weak version
1295 			 * definitions, thus giving users an error condition
1296 			 * would be incorrect.
1297 			 */
1298 			if (!(sdp->sd_flags & FLG_SY_REGSYM) &&
1299 			    ((sym->st_shndx == SHN_UNDEF) &&
1300 			    ((ELF_ST_BIND(sym->st_info) != STB_WEAK) &&
1301 			    ((sdp->sd_flags &
1302 			    (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) ||
1303 			    (((sdp->sd_flags &
1304 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
1305 			    FLG_SY_MAPREF) &&
1306 			    ((sdp->sd_flags1 & (FLG_SY1_HIDDEN |
1307 			    FLG_SY1_PROTECT)) == 0)))) {
1308 				sym_undef_entry(ofl, sdp, UNDEF);
1309 				ofl->ofl_flags |= undef;
1310 				undeferr = 1;
1311 			}
1312 
1313 		} else {
1314 			/*
1315 			 * For building things like shared objects (or anything
1316 			 * -znodefs), undefined symbols are allowed.
1317 			 *
1318 			 * If a mapfile reference remains undefined the user
1319 			 * would probably like a warning at least (they've
1320 			 * usually mis-spelt the reference).  Refer to the above
1321 			 * comments for discussion on -u references, which
1322 			 * are not tested for in the same manner.
1323 			 */
1324 			if ((sdp->sd_flags &
1325 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
1326 			    FLG_SY_MAPREF) {
1327 				sym_undef_entry(ofl, sdp, UNDEF);
1328 				ofl->ofl_flags |= FLG_OF_WARN;
1329 				undeferr = 1;
1330 			}
1331 		}
1332 
1333 		/*
1334 		 * If this symbol comes from a dependency mark the dependency
1335 		 * as required (-z ignore can result in unused dependencies
1336 		 * being dropped).  If we need to record dependency versioning
1337 		 * information indicate what version of the needed shared object
1338 		 * this symbol is part of.  Flag the symbol as undefined if it
1339 		 * has not been made available to us.
1340 		 */
1341 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1342 		    (!(sdp->sd_flags & FLG_SY_REFRSD))) {
1343 			sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD;
1344 
1345 			/*
1346 			 * Capture that we've bound to a symbol that doesn't
1347 			 * allow being directly bound to.
1348 			 */
1349 			if (sdp->sd_flags1 & FLG_SY1_NDIR)
1350 				ofl->ofl_flags1 |= FLG_OF1_NGLBDIR;
1351 
1352 			if (sdp->sd_file->ifl_vercnt) {
1353 				int		vndx;
1354 				Ver_index	*vip;
1355 
1356 				vndx = sdp->sd_aux->sa_dverndx;
1357 				vip = &sdp->sd_file->ifl_verndx[vndx];
1358 				if (vip->vi_flags & FLG_VER_AVAIL) {
1359 					vip->vi_flags |= FLG_VER_REFER;
1360 				} else {
1361 					sym_undef_entry(ofl, sdp, NOTAVAIL);
1362 					ofl->ofl_flags |= FLG_OF_FATAL;
1363 					continue;
1364 				}
1365 			}
1366 		}
1367 
1368 		/*
1369 		 * Test that we do not bind to symbol supplied from an implicit
1370 		 * shared object.  If a binding is from a weak reference it can
1371 		 * be ignored.
1372 		 */
1373 		if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) &&
1374 		    (sdp->sd_ref == REF_DYN_NEED) &&
1375 		    (sdp->sd_flags & FLG_SY_NOTAVAIL)) {
1376 			sym_undef_entry(ofl, sdp, IMPLICIT);
1377 			ofl->ofl_flags |= needed;
1378 			continue;
1379 		}
1380 
1381 		/*
1382 		 * Test that a symbol isn't going to be reduced to local scope
1383 		 * which actually wants to bind to a shared object - if so it's
1384 		 * a fatal error.
1385 		 */
1386 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1387 		    (sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_PROTECT))) {
1388 			sym_undef_entry(ofl, sdp, BNDLOCAL);
1389 			ofl->ofl_flags |= FLG_OF_FATAL;
1390 			continue;
1391 		}
1392 
1393 		/*
1394 		 * If the output image is to be versioned then all symbol
1395 		 * definitions must be associated with a version.  Remove any
1396 		 * versioning that might be left associated with an undefined
1397 		 * symbol.
1398 		 */
1399 		if (verdesc && (sdp->sd_ref == REF_REL_NEED)) {
1400 			if (sym->st_shndx == SHN_UNDEF) {
1401 				if (sdp->sd_aux && sdp->sd_aux->sa_overndx)
1402 					sdp->sd_aux->sa_overndx = 0;
1403 			} else {
1404 				if ((!(sdp->sd_flags1 & FLG_SY1_HIDDEN)) &&
1405 				    sdp->sd_aux &&
1406 				    (sdp->sd_aux->sa_overndx == 0)) {
1407 					sym_undef_entry(ofl, sdp, NOVERSION);
1408 					ofl->ofl_flags |= verdesc;
1409 					continue;
1410 				}
1411 			}
1412 		}
1413 
1414 		/*
1415 		 * If we don't need the symbol there's no need to process it
1416 		 * any further.
1417 		 */
1418 		if (sdp->sd_ref == REF_DYN_SEEN)
1419 			continue;
1420 
1421 		/*
1422 		 * Calculate the size and alignment requirements for the global
1423 		 * .bss and .tls sections.  If we're building a relocatable
1424 		 * object only account for scoped COMMON symbols (these will
1425 		 * be converted to .bss references).
1426 		 *
1427 		 * When -z nopartial is in effect, partially initialized
1428 		 * symbols are directed to the special .data section
1429 		 * created for that purpose (ofl->ofl_isparexpn).
1430 		 * Otherwise, partially initialized symbols go to .bss.
1431 		 *
1432 		 * Also refer to make_mvsections() in sunwmove.c
1433 		 */
1434 		if ((sym->st_shndx == SHN_COMMON) &&
1435 		    (((oflags & FLG_OF_RELOBJ) == 0) ||
1436 		    ((sdp->sd_flags1 & FLG_SY1_HIDDEN) &&
1437 		    (oflags & FLG_OF_PROCRED)))) {
1438 			if ((sdp->sd_move == NULL) ||
1439 			    ((sdp->sd_flags & FLG_SY_PAREXPN) == 0)) {
1440 				Xword * size, * align;
1441 
1442 				if (type != STT_TLS) {
1443 					size = &bsssize;
1444 					align = &bssalign;
1445 				} else {
1446 					size = &tlssize;
1447 					align = &tlsalign;
1448 				}
1449 				*size = (Xword)S_ROUND(*size, sym->st_value) +
1450 				    sym->st_size;
1451 				if (sym->st_value > *align)
1452 					*align = sym->st_value;
1453 			}
1454 		}
1455 
1456 #if	defined(_ELF64)
1457 		/*
1458 		 * Calculate the size and alignment requirement for the global
1459 		 * .lbss. TLS or partially initialized symbols do not need to be
1460 		 * considered yet.
1461 		 */
1462 		if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1463 		    (sym->st_shndx == SHN_X86_64_LCOMMON)) {
1464 			lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) +
1465 			    sym->st_size;
1466 			if (sym->st_value > lbssalign)
1467 				lbssalign = sym->st_value;
1468 		}
1469 #endif
1470 
1471 		/*
1472 		 * If a symbol was referenced via the command line
1473 		 * (ld -u <>, ...), then this counts as a reference against the
1474 		 * symbol. Mark any section that symbol is defined in.
1475 		 */
1476 		if (((isp = sdp->sd_isc) != 0) &&
1477 		    (sdp->sd_flags & FLG_SY_CMDREF)) {
1478 			isp->is_flags |= FLG_IS_SECTREF;
1479 			isp->is_file->ifl_flags |= FLG_IF_FILEREF;
1480 		}
1481 
1482 		/*
1483 		 * Update the symbol count and the associated name string size.
1484 		 */
1485 		if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) &&
1486 		    (oflags & FLG_OF_PROCRED)) {
1487 			/*
1488 			 * If any reductions are being processed, keep a count
1489 			 * of eliminated symbols, and if the symbol is being
1490 			 * reduced to local, count it's size for the .symtab.
1491 			 */
1492 			if (sdp->sd_flags1 & FLG_SY1_ELIM) {
1493 				ofl->ofl_elimcnt++;
1494 			} else {
1495 				ofl->ofl_scopecnt++;
1496 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1497 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
1498 				    sdp->sd_name) == -1))
1499 					return (S_ERROR);
1500 				if (allow_ldynsym && sym->st_name &&
1501 				    ldynsym_symtype[type]) {
1502 					ofl->ofl_dynscopecnt++;
1503 					if (st_insert(ofl->ofl_dynstrtab,
1504 					    sdp->sd_name) == -1)
1505 						return (S_ERROR);
1506 					/* Include it in sort section? */
1507 					DYNSORT_COUNT(sdp, sym, type, ++);
1508 				}
1509 			}
1510 		} else {
1511 			ofl->ofl_globcnt++;
1512 
1513 			/*
1514 			 * Check to see if this global variable should
1515 			 * go into a sort section. Sort sections require
1516 			 * a .SUNW_ldynsym section, so, don't check
1517 			 * unless a .SUNW_ldynsym is allowed.
1518 			 */
1519 			if (allow_ldynsym) {
1520 				DYNSORT_COUNT(sdp, sym, type, ++);
1521 			}
1522 
1523 			/*
1524 			 * If global direct bindings are in effect, or this
1525 			 * symbol has bound to a dependency which was specified
1526 			 * as requiring direct bindings, and it hasn't
1527 			 * explicitly been defined as a non-direct binding
1528 			 * symbol, mark it.
1529 			 */
1530 			if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp &&
1531 			    (isp->is_file->ifl_flags & FLG_IF_DIRECT))) &&
1532 			    ((sdp->sd_flags1 & FLG_SY1_NDIR) == 0))
1533 				sdp->sd_flags1 |= FLG_SY1_DIR;
1534 
1535 			/*
1536 			 * Insert the symbol name.
1537 			 */
1538 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1539 			    sym->st_name) {
1540 				if (st_insert(ofl->ofl_strtab,
1541 				    sdp->sd_name) == -1)
1542 					return (S_ERROR);
1543 
1544 				if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
1545 				    (st_insert(ofl->ofl_dynstrtab,
1546 				    sdp->sd_name) == -1))
1547 					return (S_ERROR);
1548 			}
1549 
1550 			/*
1551 			 * If this section offers a global symbol - record that
1552 			 * fact.
1553 			 */
1554 			if (isp) {
1555 				isp->is_flags |= FLG_IS_SECTREF;
1556 				isp->is_file->ifl_flags |= FLG_IF_FILEREF;
1557 			}
1558 		}
1559 	}
1560 
1561 	/*
1562 	 * If we've encountered a fatal error during symbol validation then
1563 	 * return now.
1564 	 */
1565 	if (ofl->ofl_flags & FLG_OF_FATAL)
1566 		return (1);
1567 
1568 	/*
1569 	 * Now that symbol resolution is completed, scan any register symbols.
1570 	 * From now on, we're only interested in those that contribute to the
1571 	 * output file.
1572 	 */
1573 	if (ofl->ofl_regsyms) {
1574 		int	ndx;
1575 
1576 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1577 			if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
1578 				continue;
1579 			if (sdp->sd_ref != REF_REL_NEED) {
1580 				ofl->ofl_regsyms[ndx] = NULL;
1581 				continue;
1582 			}
1583 
1584 			ofl->ofl_regsymcnt++;
1585 			if (sdp->sd_sym->st_name == 0)
1586 				sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY);
1587 
1588 			if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) ||
1589 			    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL))
1590 				ofl->ofl_lregsymcnt++;
1591 		}
1592 	}
1593 
1594 	/*
1595 	 * Generate the .bss section now that we know its size and alignment.
1596 	 */
1597 	if (bsssize) {
1598 		if (ld_make_bss(ofl, bsssize, bssalign,
1599 		    ld_targ.t_id.id_bss) == S_ERROR)
1600 			return (S_ERROR);
1601 	}
1602 	if (tlssize) {
1603 		if (ld_make_bss(ofl, tlssize, tlsalign,
1604 		    ld_targ.t_id.id_tlsbss) == S_ERROR)
1605 			return (S_ERROR);
1606 	}
1607 #if	defined(_ELF64)
1608 	if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1609 	    lbsssize && !(oflags & FLG_OF_RELOBJ)) {
1610 		if (ld_make_bss(ofl, lbsssize, lbssalign,
1611 		    ld_targ.t_id.id_lbss) == S_ERROR)
1612 			return (S_ERROR);
1613 	}
1614 #endif
1615 	/*
1616 	 * Determine what entry point symbol we need, and if found save its
1617 	 * symbol descriptor so that we can update the ELF header entry with the
1618 	 * symbols value later (see update_oehdr).  Make sure the symbol is
1619 	 * tagged to ensure its update in case -s is in effect.  Use any -e
1620 	 * option first, or the default entry points `_start' and `main'.
1621 	 */
1622 	ret = 0;
1623 	if (ofl->ofl_entry) {
1624 		if ((sdp = ld_sym_find(ofl->ofl_entry, SYM_NOHASH,
1625 		    NULL, ofl)) == NULL) {
1626 			eprintf(ofl->ofl_lml, ERR_FATAL,
1627 			    MSG_INTL(MSG_ARG_NOENTRY), ofl->ofl_entry);
1628 			ret++;
1629 		} else if (ensure_sym_local(ofl, sdp,
1630 		    MSG_INTL(MSG_SYM_ENTRY)) != 0) {
1631 			ret++;
1632 		} else {
1633 			ofl->ofl_entry = (void *)sdp;
1634 		}
1635 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START),
1636 	    SYM_NOHASH, NULL, ofl)) != NULL) && (ensure_sym_local(ofl,
1637 	    sdp, 0) == 0)) {
1638 		ofl->ofl_entry = (void *)sdp;
1639 
1640 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN),
1641 	    SYM_NOHASH, NULL, ofl)) != NULL) && (ensure_sym_local(ofl,
1642 	    sdp, 0) == 0)) {
1643 		ofl->ofl_entry = (void *)sdp;
1644 	}
1645 
1646 	/*
1647 	 * If ld -zdtrace=<sym> was given, then validate that the symbol is
1648 	 * defined within the current object being built.
1649 	 */
1650 	if ((sdp = ofl->ofl_dtracesym) != 0)
1651 		ret += ensure_sym_local(ofl, sdp, MSG_ORIG(MSG_STR_DTRACE));
1652 
1653 	/*
1654 	 * If any initarray, finiarray or preinitarray functions have been
1655 	 * requested, make sure they are defined within the current object
1656 	 * being built.
1657 	 */
1658 	if (ofl->ofl_initarray) {
1659 		ret += ensure_array_local(ofl, ofl->ofl_initarray,
1660 		    MSG_ORIG(MSG_SYM_INITARRAY));
1661 	}
1662 	if (ofl->ofl_finiarray) {
1663 		ret += ensure_array_local(ofl, ofl->ofl_finiarray,
1664 		    MSG_ORIG(MSG_SYM_FINIARRAY));
1665 	}
1666 	if (ofl->ofl_preiarray) {
1667 		ret += ensure_array_local(ofl, ofl->ofl_preiarray,
1668 		    MSG_ORIG(MSG_SYM_PREINITARRAY));
1669 	}
1670 
1671 	if (ret)
1672 		return (S_ERROR);
1673 
1674 	/*
1675 	 * If we're required to record any needed dependencies versioning
1676 	 * information calculate it now that all symbols have been validated.
1677 	 */
1678 	if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
1679 		return (ld_vers_check_need(ofl));
1680 	else
1681 		return (1);
1682 }
1683 
1684 /*
1685  * qsort(3c) comparison function.  As an optimization for associating weak
1686  * symbols to their strong counterparts sort global symbols according to their
1687  * section index, address and binding.
1688  */
1689 static int
1690 compare(const void *sdpp1, const void *sdpp2)
1691 {
1692 	Sym_desc	*sdp1 = *((Sym_desc **)sdpp1);
1693 	Sym_desc	*sdp2 = *((Sym_desc **)sdpp2);
1694 	Sym		*sym1, *sym2;
1695 	uchar_t		bind1, bind2;
1696 
1697 	/*
1698 	 * Symbol descriptors may be zero, move these to the front of the
1699 	 * sorted array.
1700 	 */
1701 	if (sdp1 == NULL)
1702 		return (-1);
1703 	if (sdp2 == NULL)
1704 		return (1);
1705 
1706 	sym1 = sdp1->sd_sym;
1707 	sym2 = sdp2->sd_sym;
1708 
1709 	/*
1710 	 * Compare the symbols section index.  This is important when sorting
1711 	 * the symbol tables of relocatable objects.  In this case, a symbols
1712 	 * value is the offset within the associated section, and thus many
1713 	 * symbols can have the same value, but are effectively different
1714 	 * addresses.
1715 	 */
1716 	if (sym1->st_shndx > sym2->st_shndx)
1717 		return (1);
1718 	if (sym1->st_shndx < sym2->st_shndx)
1719 		return (-1);
1720 
1721 	/*
1722 	 * Compare the symbols value (address).
1723 	 */
1724 	if (sym1->st_value > sym2->st_value)
1725 		return (1);
1726 	if (sym1->st_value < sym2->st_value)
1727 		return (-1);
1728 
1729 	bind1 = ELF_ST_BIND(sym1->st_info);
1730 	bind2 = ELF_ST_BIND(sym2->st_info);
1731 
1732 	/*
1733 	 * If two symbols have the same address place the weak symbol before
1734 	 * any strong counterpart.
1735 	 */
1736 	if (bind1 > bind2)
1737 		return (-1);
1738 	if (bind1 < bind2)
1739 		return (1);
1740 
1741 	return (0);
1742 }
1743 
1744 /*
1745  * Issue a MSG_SYM_BADADDR error from ld_sym_process(). This error
1746  * is issued when a symbol address/size is not contained by the
1747  * target section.
1748  *
1749  * Such objects are at least partially corrupt, and the user would
1750  * be well advised to be skeptical of them, and to ask their compiler
1751  * supplier to fix the problem. However, a distinction needs to be
1752  * made between symbols that reference readonly text, and those that
1753  * access writable data. Other than throwing off profiling results,
1754  * the readonly section case is less serious. We have encountered
1755  * such objects in the field. In order to allow existing objects
1756  * to continue working, we issue a warning rather than a fatal error
1757  * if the symbol is against readonly text. Other cases are fatal.
1758  */
1759 static void
1760 issue_badaddr_msg(Ifl_desc *ifl, Ofl_desc *ofl, Sym_desc *sdp,
1761     Sym *sym, Word shndx)
1762 {
1763 	ofl_flag_t	flag;
1764 	Error		err;
1765 	const char	*msg;
1766 
1767 	if ((sdp->sd_isc->is_shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) ==
1768 	    SHF_ALLOC) {
1769 		msg = MSG_INTL(MSG_SYM_BADADDR_ROTXT);
1770 		flag = FLG_OF_WARN;
1771 		err = ERR_WARNING;
1772 	} else {
1773 		msg = MSG_INTL(MSG_SYM_BADADDR);
1774 		flag = FLG_OF_FATAL;
1775 		err = ERR_FATAL;
1776 	}
1777 
1778 	eprintf(ofl->ofl_lml, err, msg, demangle(sdp->sd_name),
1779 	    ifl->ifl_name, shndx, sdp->sd_isc->is_name,
1780 	    EC_XWORD(sdp->sd_isc->is_shdr->sh_size),
1781 	    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
1782 	ofl->ofl_flags |= flag;
1783 }
1784 
1785 
1786 /*
1787  * Process the symbol table for the specified input file.  At this point all
1788  * input sections from this input file have been assigned an input section
1789  * descriptor which is saved in the `ifl_isdesc' array.
1790  *
1791  *	-	local symbols are saved (as is) if the input file is a
1792  *		relocatable object
1793  *
1794  *	-	global symbols are added to the linkers internal symbol
1795  *		table if they are not already present, otherwise a symbol
1796  *		resolution function is called upon to resolve the conflict.
1797  */
1798 uintptr_t
1799 ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1800 {
1801 	/*
1802 	 * This macro tests the given symbol to see if it is out of
1803 	 * range relative to the section it references.
1804 	 *
1805 	 * entry:
1806 	 *	- ifl is a relative object (ET_REL)
1807 	 *	_sdp - Symbol descriptor
1808 	 *	_sym - Symbol
1809 	 *	_type - Symbol type
1810 	 *
1811 	 * The following are tested:
1812 	 *	- Symbol length is non-zero
1813 	 *	- Symbol type is a type that references code or data
1814 	 *	- Referenced section is not 0 (indicates an UNDEF symbol)
1815 	 *	  and is not in the range of special values above SHN_LORESERVE
1816 	 *	  (excluding SHN_XINDEX, which is OK).
1817 	 *	- We have a valid section header for the target section
1818 	 *
1819 	 * If the above are all true, and the symbol position is not
1820 	 * contained by the target section, this macro evaluates to
1821 	 * True (1). Otherwise, False(0).
1822 	 */
1823 #define	SYM_LOC_BADADDR(_sdp, _sym, _type) \
1824 	(_sym->st_size && dynsymsort_symtype[_type] && \
1825 	(_sym->st_shndx != SHN_UNDEF) && \
1826 	((_sym->st_shndx < SHN_LORESERVE) || \
1827 		(_sym->st_shndx == SHN_XINDEX)) && \
1828 	_sdp->sd_isc && _sdp->sd_isc->is_shdr && \
1829 	((_sym->st_value + _sym->st_size) > _sdp->sd_isc->is_shdr->sh_size))
1830 
1831 	Conv_inv_buf_t	inv_buf;
1832 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
1833 	Word		*symshndx = NULL;
1834 	Shdr		*shdr = isc->is_shdr;
1835 	Sym_desc	*sdp;
1836 	size_t		strsize;
1837 	char		*strs;
1838 	uchar_t		type, bind;
1839 	Word		ndx, hash, local, total;
1840 	uchar_t		osabi = ifl->ifl_ehdr->e_ident[EI_OSABI];
1841 	Half		mach = ifl->ifl_ehdr->e_machine;
1842 	Half		etype = ifl->ifl_ehdr->e_type;
1843 	int		etype_rel;
1844 	const char	*symsecname, *strsecname;
1845 	Word		symsecndx;
1846 	avl_index_t	where;
1847 	int		test_gnu_hidden_bit, weak;
1848 
1849 	/*
1850 	 * Its possible that a file may contain more that one symbol table,
1851 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
1852 	 * table (here, we assume .dynsym comes before .symtab).
1853 	 */
1854 	if (ifl->ifl_symscnt)
1855 		return (1);
1856 
1857 	if (isc->is_symshndx)
1858 		symshndx = isc->is_symshndx->is_indata->d_buf;
1859 
1860 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
1861 
1862 	symsecndx = isc->is_scnndx;
1863 	if (isc->is_name)
1864 		symsecname = isc->is_name;
1865 	else
1866 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
1867 
1868 	/*
1869 	 * From the symbol tables section header information determine which
1870 	 * strtab table is needed to locate the actual symbol names.
1871 	 */
1872 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
1873 		ndx = shdr->sh_link;
1874 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
1875 			eprintf(ofl->ofl_lml, ERR_FATAL,
1876 			    MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
1877 			    EC_WORD(symsecndx), symsecname, EC_XWORD(ndx));
1878 			return (S_ERROR);
1879 		}
1880 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
1881 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
1882 		if (ifl->ifl_isdesc[ndx]->is_name)
1883 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
1884 		else
1885 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
1886 	} else {
1887 		/*
1888 		 * There is no string table section in this input file
1889 		 * although there are symbols in this symbol table section.
1890 		 * This means that these symbols do not have names.
1891 		 * Currently, only scratch register symbols are allowed
1892 		 * not to have names.
1893 		 */
1894 		strsize = 0;
1895 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
1896 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
1897 	}
1898 
1899 	/*
1900 	 * Determine the number of local symbols together with the total
1901 	 * number we have to process.
1902 	 */
1903 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
1904 	local = shdr->sh_info;
1905 
1906 	/*
1907 	 * Allocate a symbol table index array and a local symbol array
1908 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
1909 	 * array).  If we are dealing with a relocatable object, allocate the
1910 	 * local symbol descriptors.  If this isn't a relocatable object we
1911 	 * still have to process any shared object locals to determine if any
1912 	 * register symbols exist.  Although these aren't added to the output
1913 	 * image, they are used as part of symbol resolution.
1914 	 */
1915 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
1916 	    sizeof (Sym_desc *)))) == NULL)
1917 		return (S_ERROR);
1918 	etype_rel = (etype == ET_REL);
1919 	if (etype_rel && local) {
1920 		if ((ifl->ifl_locs =
1921 		    libld_calloc(sizeof (Sym_desc), local)) == NULL)
1922 			return (S_ERROR);
1923 		/* LINTED */
1924 		ifl->ifl_locscnt = (Word)local;
1925 	}
1926 	ifl->ifl_symscnt = total;
1927 
1928 	/*
1929 	 * If there are local symbols to save add them to the symbol table
1930 	 * index array.
1931 	 */
1932 	if (local) {
1933 		int		allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1934 		Sym_desc	*last_file_sdp = NULL;
1935 		int		last_file_ndx = 0;
1936 
1937 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
1938 			Word		shndx, sdflags = FLG_SY_CLEAN;
1939 			const char	*name;
1940 			Sym_desc	*rsdp;
1941 			int		shndx_bad = 0;
1942 			int		symtab_enter = 1;
1943 
1944 			/*
1945 			 * Determine and validate the associated section index.
1946 			 */
1947 			if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
1948 				shndx = symshndx[ndx];
1949 			} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
1950 				sdflags |= FLG_SY_SPECSEC;
1951 			} else if (shndx > ifl->ifl_ehdr->e_shnum) {
1952 				/* We need the name before we can issue error */
1953 				shndx_bad = 1;
1954 			}
1955 
1956 			/*
1957 			 * Check if st_name has a valid value or not.
1958 			 */
1959 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
1960 			    shndx, symsecndx, symsecname, strsecname,
1961 			    &sdflags)) == NULL) {
1962 				ofl->ofl_flags |= FLG_OF_FATAL;
1963 				continue;
1964 			}
1965 
1966 			/*
1967 			 * Now that we have the name, if the section index
1968 			 * was bad, report it.
1969 			 */
1970 			if (shndx_bad) {
1971 				eprintf(ofl->ofl_lml, ERR_WARNING,
1972 				    MSG_INTL(MSG_SYM_INVSHNDX),
1973 				    demangle_symname(name, symsecname, ndx),
1974 				    ifl->ifl_name,
1975 				    conv_sym_shndx(osabi, mach, sym->st_shndx,
1976 				    CONV_FMT_DECIMAL, &inv_buf));
1977 				continue;
1978 			}
1979 
1980 			/*
1981 			 * If this local symbol table originates from a shared
1982 			 * object, then we're only interested in recording
1983 			 * register symbols.  As local symbol descriptors aren't
1984 			 * allocated for shared objects, one will be allocated
1985 			 * to associated with the register symbol.  This symbol
1986 			 * won't become part of the output image, but we must
1987 			 * process it to test for register conflicts.
1988 			 */
1989 			rsdp = sdp = NULL;
1990 			if (sdflags & FLG_SY_REGSYM) {
1991 				/*
1992 				 * The presence of FLG_SY_REGSYM means that
1993 				 * the pointers in ld_targ.t_ms are non-NULL.
1994 				 */
1995 				rsdp = (*ld_targ.t_ms.ms_reg_find)(sym, ofl);
1996 				if (rsdp != 0) {
1997 					/*
1998 					 * The fact that another register def-
1999 					 * inition has been found is fatal.
2000 					 * Call the verification routine to get
2001 					 * the error message and move on.
2002 					 */
2003 					(void) (*ld_targ.t_ms.ms_reg_check)
2004 					    (rsdp, sym, name, ifl, ofl);
2005 					continue;
2006 				}
2007 
2008 				if (etype == ET_DYN) {
2009 					if ((sdp = libld_calloc(
2010 					    sizeof (Sym_desc), 1)) == NULL)
2011 						return (S_ERROR);
2012 					sdp->sd_ref = REF_DYN_SEEN;
2013 
2014 					/* Will not appear in output object */
2015 					symtab_enter = 0;
2016 				}
2017 			} else if (etype == ET_DYN)
2018 				continue;
2019 
2020 			/*
2021 			 * Fill in the remaining symbol descriptor information.
2022 			 */
2023 			if (sdp == NULL) {
2024 				sdp = &(ifl->ifl_locs[ndx]);
2025 				sdp->sd_ref = REF_REL_NEED;
2026 			}
2027 			if (rsdp == NULL) {
2028 				sdp->sd_name = name;
2029 				sdp->sd_sym = sym;
2030 				sdp->sd_shndx = shndx;
2031 				sdp->sd_flags = sdflags;
2032 				sdp->sd_file = ifl;
2033 				ifl->ifl_oldndx[ndx] = sdp;
2034 			}
2035 
2036 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
2037 
2038 			/*
2039 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
2040 			 * so as to simplify future processing.
2041 			 */
2042 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
2043 				sdp->sd_shndx = shndx = SHN_UNDEF;
2044 				sdp->sd_flags1 |=
2045 				    (FLG_SY1_IGNORE | FLG_SY1_ELIM);
2046 			}
2047 
2048 			/*
2049 			 * Process any register symbols.
2050 			 */
2051 			if (sdp->sd_flags & FLG_SY_REGSYM) {
2052 				/*
2053 				 * Add a diagnostic to indicate we've caught a
2054 				 * register symbol, as this can be useful if a
2055 				 * register conflict is later discovered.
2056 				 */
2057 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
2058 
2059 				/*
2060 				 * If this register symbol hasn't already been
2061 				 * recorded, enter it now.
2062 				 *
2063 				 * The presence of FLG_SY_REGSYM means that
2064 				 * the pointers in ld_targ.t_ms are non-NULL.
2065 				 */
2066 				if ((rsdp == NULL) &&
2067 				    ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) ==
2068 				    0))
2069 					return (S_ERROR);
2070 			}
2071 
2072 			/*
2073 			 * Assign an input section.
2074 			 */
2075 			if ((sym->st_shndx != SHN_UNDEF) &&
2076 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
2077 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
2078 
2079 			/*
2080 			 * If this symbol falls within the range of a section
2081 			 * being discarded, then discard the symbol itself.
2082 			 * There is no reason to keep this local symbol.
2083 			 */
2084 			if (sdp->sd_isc &&
2085 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
2086 				sdp->sd_flags |= FLG_SY_ISDISC;
2087 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
2088 				continue;
2089 			}
2090 
2091 			/*
2092 			 * Skip any section symbols as new versions of these
2093 			 * will be created.
2094 			 */
2095 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
2096 				if (sym->st_shndx == SHN_UNDEF) {
2097 					eprintf(ofl->ofl_lml, ERR_WARNING,
2098 					    MSG_INTL(MSG_SYM_INVSHNDX),
2099 					    demangle_symname(name, symsecname,
2100 					    ndx), ifl->ifl_name,
2101 					    conv_sym_shndx(osabi, mach,
2102 					    sym->st_shndx, CONV_FMT_DECIMAL,
2103 					    &inv_buf));
2104 				}
2105 				continue;
2106 			}
2107 
2108 			/*
2109 			 * For a relocatable object, if this symbol is defined
2110 			 * and has non-zero length and references an address
2111 			 * within an associated section, then check its extents
2112 			 * to make sure the section boundaries encompass it.
2113 			 * If they don't, the ELF file is corrupt.
2114 			 */
2115 			if (etype_rel) {
2116 				if (SYM_LOC_BADADDR(sdp, sym, type)) {
2117 					issue_badaddr_msg(ifl, ofl, sdp,
2118 					    sym, shndx);
2119 					if (ofl->ofl_flags & FLG_OF_FATAL)
2120 						continue;
2121 				}
2122 
2123 				/*
2124 				 * We have observed relocatable objects
2125 				 * containing identical adjacent STT_FILE
2126 				 * symbols. Discard any other than the first,
2127 				 * as they are all equivalent and the extras
2128 				 * do not add information.
2129 				 *
2130 				 * For the purpose of this test, we assume
2131 				 * that only the symbol type and the string
2132 				 * table offset (st_name) matter.
2133 				 */
2134 				if (type == STT_FILE) {
2135 					int toss = (last_file_sdp != NULL) &&
2136 					    ((ndx - 1) == last_file_ndx) &&
2137 					    (sym->st_name ==
2138 					    last_file_sdp->sd_sym->st_name);
2139 
2140 					last_file_sdp = sdp;
2141 					last_file_ndx = ndx;
2142 					if (toss) {
2143 						sdp->sd_flags |= FLG_SY_INVALID;
2144 						DBG_CALL(Dbg_syms_dup_discarded(
2145 						    ofl->ofl_lml, ndx, sdp));
2146 						continue;
2147 					}
2148 				}
2149 			}
2150 
2151 
2152 			/*
2153 			 * Sanity check for TLS
2154 			 */
2155 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
2156 			    (sym->st_shndx != SHN_COMMON))) {
2157 				Is_desc	*isp = sdp->sd_isc;
2158 
2159 				if ((isp == NULL) || (isp->is_shdr == NULL) ||
2160 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
2161 					eprintf(ofl->ofl_lml, ERR_FATAL,
2162 					    MSG_INTL(MSG_SYM_TLS),
2163 					    demangle(sdp->sd_name),
2164 					    ifl->ifl_name);
2165 					ofl->ofl_flags |= FLG_OF_FATAL;
2166 					continue;
2167 				}
2168 			}
2169 
2170 			/*
2171 			 * Carry our some basic sanity checks (these are just
2172 			 * some of the erroneous symbol entries we've come
2173 			 * across, there's probably a lot more).  The symbol
2174 			 * will not be carried forward to the output file, which
2175 			 * won't be a problem unless a relocation is required
2176 			 * against it.
2177 			 */
2178 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
2179 			    ((sym->st_shndx == SHN_COMMON)) ||
2180 			    ((type == STT_FILE) &&
2181 			    (sym->st_shndx != SHN_ABS))) ||
2182 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == NULL))) {
2183 				eprintf(ofl->ofl_lml, ERR_WARNING,
2184 				    MSG_INTL(MSG_SYM_INVSHNDX),
2185 				    demangle_symname(name, symsecname, ndx),
2186 				    ifl->ifl_name,
2187 				    conv_sym_shndx(osabi, mach, sym->st_shndx,
2188 				    CONV_FMT_DECIMAL, &inv_buf));
2189 				sdp->sd_isc = NULL;
2190 				sdp->sd_flags |= FLG_SY_INVALID;
2191 				continue;
2192 			}
2193 
2194 			/*
2195 			 * As these local symbols will become part of the output
2196 			 * image, record their number and name string size.
2197 			 * Globals are counted after all input file processing
2198 			 * (and hence symbol resolution) is complete during
2199 			 * sym_validate().
2200 			 */
2201 			if (!(ofl->ofl_flags & FLG_OF_REDLSYM) &&
2202 			    symtab_enter) {
2203 				ofl->ofl_locscnt++;
2204 
2205 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
2206 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
2207 				    sdp->sd_name) == -1))
2208 					return (S_ERROR);
2209 
2210 				if (allow_ldynsym && sym->st_name &&
2211 				    ldynsym_symtype[type]) {
2212 					ofl->ofl_dynlocscnt++;
2213 					if (st_insert(ofl->ofl_dynstrtab,
2214 					    sdp->sd_name) == -1)
2215 						return (S_ERROR);
2216 					/* Include it in sort section? */
2217 					DYNSORT_COUNT(sdp, sym, type, ++);
2218 				}
2219 			}
2220 		}
2221 	}
2222 
2223 	/*
2224 	 * The GNU ld interprets the top bit of the 16-bit Versym value
2225 	 * (0x8000) as the "hidden" bit. If this bit is set, the linker
2226 	 * is supposed to act as if that symbol does not exist. The Solaris
2227 	 * linker does not support this mechanism, or the model of interface
2228 	 * evolution that it allows, but we honor it in GNU ld produced
2229 	 * objects in order to interoperate with them.
2230 	 *
2231 	 * Determine if we should honor the GNU hidden bit for this file.
2232 	 */
2233 	test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) &&
2234 	    (ifl->ifl_versym != NULL);
2235 
2236 	/*
2237 	 * Now scan the global symbols entering them in the internal symbol
2238 	 * table or resolving them as necessary.
2239 	 */
2240 	sym = (Sym *)isc->is_indata->d_buf;
2241 	sym += local;
2242 	weak = 0;
2243 	/* LINTED */
2244 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
2245 		const char	*name;
2246 		Word		shndx, sdflags = 0;
2247 		int		shndx_bad = 0;
2248 
2249 		/*
2250 		 * Determine and validate the associated section index.
2251 		 */
2252 		if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
2253 			shndx = symshndx[ndx];
2254 		} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
2255 			sdflags |= FLG_SY_SPECSEC;
2256 		} else if (shndx > ifl->ifl_ehdr->e_shnum) {
2257 			/* We need the name before we can issue error */
2258 			shndx_bad = 1;
2259 		}
2260 
2261 		/*
2262 		 * Check if st_name has a valid value or not.
2263 		 */
2264 		if ((name = string(ofl, ifl, sym, strs, strsize, ndx, shndx,
2265 		    symsecndx, symsecname, strsecname, &sdflags)) == NULL) {
2266 			ofl->ofl_flags |= FLG_OF_FATAL;
2267 			continue;
2268 		}
2269 
2270 		/*
2271 		 * Now that we have the name, if the section index
2272 		 * was bad, report it.
2273 		 */
2274 		if (shndx_bad) {
2275 			eprintf(ofl->ofl_lml, ERR_WARNING,
2276 			    MSG_INTL(MSG_SYM_INVSHNDX),
2277 			    demangle_symname(name, symsecname, ndx),
2278 			    ifl->ifl_name,
2279 			    conv_sym_shndx(osabi, mach, sym->st_shndx,
2280 			    CONV_FMT_DECIMAL, &inv_buf));
2281 			continue;
2282 		}
2283 
2284 
2285 		/*
2286 		 * Test for the GNU hidden bit, and ignore symbols that
2287 		 * have it set.
2288 		 */
2289 		if (test_gnu_hidden_bit &&
2290 		    ((ifl->ifl_versym[ndx] & 0x8000) != 0))
2291 			continue;
2292 
2293 		/*
2294 		 * The linker itself will generate symbols for _end, _etext,
2295 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
2296 		 * bother entering these symbols from shared objects.  This
2297 		 * results in some wasted resolution processing, which is hard
2298 		 * to feel, but if nothing else, pollutes diagnostic relocation
2299 		 * output.
2300 		 */
2301 		if (name[0] && (etype == ET_DYN) && (sym->st_size == 0) &&
2302 		    (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) &&
2303 		    (name[0] == '_') && ((name[1] == 'e') ||
2304 		    (name[1] == 'D') || (name[1] == 'P')) &&
2305 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
2306 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
2307 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
2308 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
2309 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
2310 			ifl->ifl_oldndx[ndx] = 0;
2311 			continue;
2312 		}
2313 
2314 		/*
2315 		 * The '-z wrap=XXX' option emulates the GNU ld --wrap=XXX
2316 		 * option. When XXX is the symbol to be wrapped:
2317 		 *
2318 		 * -	An undefined reference to XXX is converted to __wrap_XXX
2319 		 * -	An undefined reference to __real_XXX is converted to XXX
2320 		 *
2321 		 * The idea is that the user can supply a wrapper function
2322 		 * __wrap_XXX that does some work, and then uses the name
2323 		 * __real_XXX to pass the call on to the real function. The
2324 		 * wrapper objects are linked with the original unmodified
2325 		 * objects to produce a wrapped version of the output object.
2326 		 */
2327 		if (ofl->ofl_wrap && name[0] && (shndx == SHN_UNDEF)) {
2328 			WrapSymNode wsn, *wsnp;
2329 
2330 			/*
2331 			 * If this is the __real_XXX form, advance the
2332 			 * pointer to reference the wrapped name.
2333 			 */
2334 			wsn.wsn_name = name;
2335 			if ((*name == '_') &&
2336 			    (strncmp(name, MSG_ORIG(MSG_STR_UU_REAL_U),
2337 			    MSG_STR_UU_REAL_U_SIZE) == 0))
2338 				wsn.wsn_name += MSG_STR_UU_REAL_U_SIZE;
2339 
2340 			/*
2341 			 * Is this symbol in the wrap AVL tree? If so, map
2342 			 * XXX to __wrap_XXX, and __real_XXX to XXX. Note that
2343 			 * wsn.wsn_name will equal the current value of name
2344 			 * if the __real_ prefix is not present.
2345 			 */
2346 			if ((wsnp = avl_find(ofl->ofl_wrap, &wsn, 0)) != NULL) {
2347 				const char *old_name = name;
2348 
2349 				name = (wsn.wsn_name == name) ?
2350 				    wsnp->wsn_wrapname : wsn.wsn_name;
2351 				DBG_CALL(Dbg_syms_wrap(ofl->ofl_lml, ndx,
2352 				    old_name, name));
2353 			}
2354 		}
2355 
2356 		/*
2357 		 * Determine and validate the symbols binding.
2358 		 */
2359 		bind = ELF_ST_BIND(sym->st_info);
2360 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
2361 			eprintf(ofl->ofl_lml, ERR_WARNING,
2362 			    MSG_INTL(MSG_SYM_NONGLOB),
2363 			    demangle_symname(name, symsecname, ndx),
2364 			    ifl->ifl_name,
2365 			    conv_sym_info_bind(bind, 0, &inv_buf));
2366 			continue;
2367 		}
2368 		if (bind == STB_WEAK)
2369 			weak++;
2370 
2371 		/*
2372 		 * If this symbol falls within the range of a section being
2373 		 * discarded, then discard the symbol itself.
2374 		 */
2375 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
2376 		    (sym->st_shndx != SHN_UNDEF)) {
2377 			Is_desc	*isp;
2378 
2379 			if (shndx >= ifl->ifl_shnum) {
2380 				/*
2381 				 * Carry our some basic sanity checks
2382 				 * The symbol will not be carried forward to
2383 				 * the output file, which won't be a problem
2384 				 * unless a relocation is required against it.
2385 				 */
2386 				eprintf(ofl->ofl_lml, ERR_WARNING,
2387 				    MSG_INTL(MSG_SYM_INVSHNDX),
2388 				    demangle_symname(name, symsecname, ndx),
2389 				    ifl->ifl_name,
2390 				    conv_sym_shndx(osabi, mach, sym->st_shndx,
2391 				    CONV_FMT_DECIMAL, &inv_buf));
2392 				continue;
2393 			}
2394 
2395 			isp = ifl->ifl_isdesc[shndx];
2396 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
2397 				if ((sdp =
2398 				    libld_calloc(sizeof (Sym_desc), 1)) == NULL)
2399 					return (S_ERROR);
2400 
2401 				/*
2402 				 * Create a dummy symbol entry so that if we
2403 				 * find any references to this discarded symbol
2404 				 * we can compensate.
2405 				 */
2406 				sdp->sd_name = name;
2407 				sdp->sd_sym = sym;
2408 				sdp->sd_file = ifl;
2409 				sdp->sd_isc = isp;
2410 				sdp->sd_flags = FLG_SY_ISDISC;
2411 				ifl->ifl_oldndx[ndx] = sdp;
2412 
2413 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
2414 				continue;
2415 			}
2416 		}
2417 
2418 		/*
2419 		 * If the symbol does not already exist in the internal symbol
2420 		 * table add it, otherwise resolve the conflict.  If the symbol
2421 		 * from this file is kept, retain its symbol table index for
2422 		 * possible use in associating a global alias.
2423 		 */
2424 		/* LINTED */
2425 		hash = (Word)elf_hash((const char *)name);
2426 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
2427 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
2428 			if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, ndx,
2429 			    shndx, sdflags, 0, &where)) == (Sym_desc *)S_ERROR)
2430 				return (S_ERROR);
2431 
2432 		} else if (ld_sym_resolve(sdp, sym, ifl, ofl, ndx, shndx,
2433 		    sdflags) == S_ERROR)
2434 			return (S_ERROR);
2435 
2436 		/*
2437 		 * After we've compared a defined symbol in one shared
2438 		 * object, flag the symbol so we don't compare it again.
2439 		 */
2440 		if ((etype == ET_DYN) && (sym->st_shndx != SHN_UNDEF) &&
2441 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
2442 			sdp->sd_flags |= FLG_SY_SOFOUND;
2443 
2444 		/*
2445 		 * If the symbol is accepted from this file retain the symbol
2446 		 * index for possible use in aliasing.
2447 		 */
2448 		if (sdp->sd_file == ifl)
2449 			sdp->sd_symndx = ndx;
2450 
2451 		ifl->ifl_oldndx[ndx] = sdp;
2452 
2453 		/*
2454 		 * If we've accepted a register symbol, continue to validate
2455 		 * it.
2456 		 */
2457 		if (sdp->sd_flags & FLG_SY_REGSYM) {
2458 			Sym_desc	*rsdp;
2459 
2460 			/*
2461 			 * The presence of FLG_SY_REGSYM means that
2462 			 * the pointers in ld_targ.t_ms are non-NULL.
2463 			 */
2464 			rsdp = (*ld_targ.t_ms.ms_reg_find)(sdp->sd_sym, ofl);
2465 			if (rsdp == NULL) {
2466 				if ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 0)
2467 					return (S_ERROR);
2468 			} else if (rsdp != sdp) {
2469 				(void) (*ld_targ.t_ms.ms_reg_check)(rsdp,
2470 				    sdp->sd_sym, sdp->sd_name, ifl, ofl);
2471 			}
2472 		}
2473 
2474 		/*
2475 		 * For a relocatable object, if this symbol is defined
2476 		 * and has non-zero length and references an address
2477 		 * within an associated section, then check its extents
2478 		 * to make sure the section boundaries encompass it.
2479 		 * If they don't, the ELF file is corrupt. Note that this
2480 		 * global symbol may have come from another file to satisfy
2481 		 * an UNDEF symbol of the same name from this one. In that
2482 		 * case, we don't check it, because it was already checked
2483 		 * as part of its own file.
2484 		 */
2485 		if (etype_rel && (sdp->sd_file == ifl)) {
2486 			Sym *tsym = sdp->sd_sym;
2487 
2488 			if (SYM_LOC_BADADDR(sdp, tsym,
2489 			    ELF_ST_TYPE(tsym->st_info))) {
2490 				issue_badaddr_msg(ifl, ofl, sdp,
2491 				    tsym, tsym->st_shndx);
2492 				continue;
2493 			}
2494 		}
2495 	}
2496 
2497 	/*
2498 	 * Associate weak (alias) symbols to their non-weak counterparts by
2499 	 * scaning the global symbols one more time.
2500 	 *
2501 	 * This association is needed when processing the symbols from a shared
2502 	 * object dependency when a a weak definition satisfies a reference:
2503 	 *
2504 	 *  -	When building a dynamic executable, if a referenced symbol is a
2505 	 *	data item, the symbol data is copied to the executables address
2506 	 *	space.  In this copy-relocation case, we must also reassociate
2507 	 *	the alias symbol with its new location in the executable.
2508 	 *
2509 	 *  -	If the referenced symbol is a function then we may need to
2510 	 *	promote the symbols binding from undefined weak to undefined,
2511 	 *	otherwise the run-time linker will not generate the correct
2512 	 *	relocation error should the symbol not be found.
2513 	 *
2514 	 * Weak alias association is also required when a local dynsym table
2515 	 * is being created.  This table should only contain one instance of a
2516 	 * symbol that is associated to a given address.
2517 	 *
2518 	 * The true association between a weak/strong symbol pair is that both
2519 	 * symbol entries are identical, thus first we create a sorted symbol
2520 	 * list keyed off of the symbols section index and value.  If the symbol
2521 	 * belongs to the same section and has the same value, then the chances
2522 	 * are that the rest of the symbols data is the same.  This list is then
2523 	 * scanned for weak symbols, and if one is found then any strong
2524 	 * association will exist in the entries that follow.  Thus we just have
2525 	 * to scan one (typically a single alias) or more (in the uncommon
2526 	 * instance of multiple weak to strong associations) entries to
2527 	 * determine if a match exists.
2528 	 */
2529 	if (weak && (OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) &&
2530 	    (total > local)) {
2531 		static Sym_desc	**sort;
2532 		static size_t	osize = 0;
2533 		size_t		nsize = (total - local) * sizeof (Sym_desc *);
2534 
2535 		/*
2536 		 * As we might be processing many input files, and many symbols,
2537 		 * try and reuse a static sort buffer.  Note, presently we're
2538 		 * playing the game of never freeing any buffers as there's a
2539 		 * belief this wastes time.
2540 		 */
2541 		if ((osize == 0) || (nsize > osize)) {
2542 			if ((sort = libld_malloc(nsize)) == NULL)
2543 				return (S_ERROR);
2544 			osize = nsize;
2545 		}
2546 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], nsize);
2547 
2548 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
2549 
2550 		for (ndx = 0; ndx < (total - local); ndx++) {
2551 			Sym_desc	*wsdp = sort[ndx];
2552 			Sym		*wsym;
2553 			int		sndx;
2554 
2555 			/*
2556 			 * Ignore any empty symbol descriptor, or the case where
2557 			 * the symbol has been resolved to a different file.
2558 			 */
2559 			if ((wsdp == NULL) || (wsdp->sd_file != ifl))
2560 				continue;
2561 
2562 			wsym = wsdp->sd_sym;
2563 
2564 			if ((wsym->st_shndx == SHN_UNDEF) ||
2565 			    (wsdp->sd_flags & FLG_SY_SPECSEC) ||
2566 			    (ELF_ST_BIND(wsym->st_info) != STB_WEAK))
2567 				continue;
2568 
2569 			/*
2570 			 * We have a weak symbol, if it has a strong alias it
2571 			 * will have been sorted to one of the following sort
2572 			 * table entries.  Note that we could have multiple weak
2573 			 * symbols aliased to one strong (if this occurs then
2574 			 * the strong symbol only maintains one alias back to
2575 			 * the last weak).
2576 			 */
2577 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
2578 				Sym_desc	*ssdp = sort[sndx];
2579 				Sym		*ssym;
2580 				int		w_dynbits, s_dynbits;
2581 
2582 				/*
2583 				 * Ignore any empty symbol descriptor, or the
2584 				 * case where the symbol has been resolved to a
2585 				 * different file.
2586 				 */
2587 				if ((ssdp == NULL) || (ssdp->sd_file != ifl))
2588 					continue;
2589 
2590 				ssym = ssdp->sd_sym;
2591 
2592 				if (ssym->st_shndx == SHN_UNDEF)
2593 					continue;
2594 
2595 				if ((ssym->st_shndx != wsym->st_shndx) ||
2596 				    (ssym->st_value != wsym->st_value))
2597 					break;
2598 
2599 				if ((ssym->st_size != wsym->st_size) ||
2600 				    (ssdp->sd_flags & FLG_SY_SPECSEC) ||
2601 				    (ELF_ST_BIND(ssym->st_info) == STB_WEAK))
2602 					continue;
2603 
2604 				/*
2605 				 * If a sharable object, set link fields so
2606 				 * that they reference each other.`
2607 				 */
2608 				if (etype == ET_DYN) {
2609 					ssdp->sd_aux->sa_linkndx =
2610 					    (Word)wsdp->sd_symndx;
2611 					wsdp->sd_aux->sa_linkndx =
2612 					    (Word)ssdp->sd_symndx;
2613 				}
2614 
2615 				/*
2616 				 * Determine which of these two symbols go into
2617 				 * the sort section.  If a mapfile has made
2618 				 * explicit settings of the FLG_SY_*DYNSORT
2619 				 * flags for both symbols, then we do what they
2620 				 * say.  If one has the DYNSORT flags set, we
2621 				 * set the NODYNSORT bit in the other.  And if
2622 				 * neither has an explicit setting, then we
2623 				 * favor the weak symbol because they usually
2624 				 * lack the leading underscore.
2625 				 */
2626 				w_dynbits = wsdp->sd_flags &
2627 				    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2628 				s_dynbits = ssdp->sd_flags &
2629 				    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2630 				if (!(w_dynbits && s_dynbits)) {
2631 					if (s_dynbits) {
2632 						if (s_dynbits == FLG_SY_DYNSORT)
2633 							wsdp->sd_flags |=
2634 							    FLG_SY_NODYNSORT;
2635 					} else if (w_dynbits !=
2636 					    FLG_SY_NODYNSORT) {
2637 						ssdp->sd_flags |=
2638 						    FLG_SY_NODYNSORT;
2639 					}
2640 				}
2641 				break;
2642 			}
2643 		}
2644 	}
2645 	return (1);
2646 
2647 #undef SYM_LOC_BADADDR
2648 }
2649 
2650 /*
2651  * Add an undefined symbol to the symbol table.  The reference originates from
2652  * the location identifed by the message id (mid).  These references can
2653  * originate from command line options such as -e, -u, -initarray, etc.
2654  * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated
2655  * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)).
2656  */
2657 Sym_desc *
2658 ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid)
2659 {
2660 	Sym		*sym;
2661 	Ifl_desc	*ifl = NULL, *_ifl;
2662 	Sym_desc	*sdp;
2663 	Word		hash;
2664 	Aliste		idx;
2665 	avl_index_t	where;
2666 	const char	*reference = MSG_INTL(mid);
2667 
2668 	/*
2669 	 * As an optimization, determine whether we've already generated this
2670 	 * reference.  If the symbol doesn't already exist we'll create it.
2671 	 * Or if the symbol does exist from a different source, we'll resolve
2672 	 * the conflict.
2673 	 */
2674 	/* LINTED */
2675 	hash = (Word)elf_hash(name);
2676 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) {
2677 		if ((sdp->sd_sym->st_shndx == SHN_UNDEF) &&
2678 		    (sdp->sd_file->ifl_name == reference))
2679 			return (sdp);
2680 	}
2681 
2682 	/*
2683 	 * Determine whether a pseudo input file descriptor exists to represent
2684 	 * the command line, as any global symbol needs an input file descriptor
2685 	 * during any symbol resolution (refer to map_ifl() which provides a
2686 	 * similar method for adding symbols from mapfiles).
2687 	 */
2688 	for (APLIST_TRAVERSE(ofl->ofl_objs, idx, _ifl))
2689 		if (strcmp(_ifl->ifl_name, reference) == 0) {
2690 			ifl = _ifl;
2691 			break;
2692 		}
2693 
2694 	/*
2695 	 * If no descriptor exists create one.
2696 	 */
2697 	if (ifl == NULL) {
2698 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) == NULL)
2699 			return ((Sym_desc *)S_ERROR);
2700 		ifl->ifl_name = reference;
2701 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
2702 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr), 1)) == NULL)
2703 			return ((Sym_desc *)S_ERROR);
2704 		ifl->ifl_ehdr->e_type = ET_REL;
2705 
2706 		if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
2707 			return ((Sym_desc *)S_ERROR);
2708 	}
2709 
2710 	/*
2711 	 * Allocate a symbol structure and add it to the global symbol table.
2712 	 */
2713 	if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
2714 		return ((Sym_desc *)S_ERROR);
2715 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
2716 	sym->st_shndx = SHN_UNDEF;
2717 
2718 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
2719 	if (sdp == NULL) {
2720 		DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
2721 		if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
2722 		    0, 0, &where)) == (Sym_desc *)S_ERROR)
2723 			return ((Sym_desc *)S_ERROR);
2724 	} else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0,
2725 	    SHN_UNDEF, 0) == S_ERROR)
2726 		return ((Sym_desc *)S_ERROR);
2727 
2728 	sdp->sd_flags &= ~FLG_SY_CLEAN;
2729 	sdp->sd_flags |= FLG_SY_CMDREF;
2730 
2731 	return (sdp);
2732 }
2733 
2734 /*
2735  * STT_SECTION symbols have their st_name field set to NULL, and consequently
2736  * have no name. Generate a name suitable for diagnostic use for such a symbol.
2737  * The resulting name will be of the form:
2738  *
2739  *	"XXX (section)"
2740  *
2741  * where XXX is the name of the section.
2742  *
2743  * Diagnostics for STT_SECTION symbols tend to come in clusters,
2744  * so we use a static variable to retain the last string we generate. If
2745  * another one comes along for the same section before some other section
2746  * intervenes, we will reuse the string.
2747  *
2748  * entry:
2749  *	isc - Input section assocated with the symbol.
2750  *	fmt - NULL, or format string to use.
2751  *
2752  * exit:
2753  *	Returns the allocated string, or NULL on allocation failure.
2754  */
2755 const const char *
2756 ld_stt_section_sym_name(Is_desc *isp)
2757 {
2758 	static const char	*last_fmt;
2759 	static Is_desc		*last_isp = NULL;
2760 	static char		*namestr;
2761 
2762 	const char		*fmt;
2763 	size_t			len;
2764 
2765 	if ((isp != NULL) && (isp->is_name != NULL)) {
2766 		fmt = (isp->is_flags & FLG_IS_GNSTRMRG) ?
2767 		    MSG_INTL(MSG_STR_SECTION_MSTR) : MSG_INTL(MSG_STR_SECTION);
2768 
2769 		if ((last_isp == isp) && (last_fmt == fmt))
2770 			return (namestr);
2771 
2772 		len = strlen(fmt) + strlen(isp->is_name) + 1;
2773 
2774 		if ((namestr = libld_malloc(len)) == 0)
2775 			return (NULL);
2776 		(void) snprintf(namestr, len, fmt, isp->is_name);
2777 
2778 		/* Remember for next time */
2779 		last_fmt = fmt;
2780 		last_isp = isp;
2781 
2782 		return (namestr);
2783 	}
2784 
2785 	return (NULL);
2786 }
2787