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