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