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