xref: /titanic_51/usr/src/cmd/sgs/libld/common/syms.c (revision 0b240fcdeb4772e65fed050aee3e3dc63308ae72)
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 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
1915 			Word		shndx, sdflags = FLG_SY_CLEAN;
1916 			const char	*name;
1917 			Sym_desc	*rsdp;
1918 			int		shndx_bad = 0;
1919 
1920 			/*
1921 			 * Determine and validate the associated section index.
1922 			 */
1923 			if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
1924 				shndx = symshndx[ndx];
1925 			} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
1926 				sdflags |= FLG_SY_SPECSEC;
1927 			} else if (shndx > ifl->ifl_ehdr->e_shnum) {
1928 				/* We need the name before we can issue error */
1929 				shndx_bad = 1;
1930 			}
1931 
1932 			/*
1933 			 * Check if st_name has a valid value or not.
1934 			 */
1935 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
1936 			    shndx, symsecname, strsecname, &sdflags)) == 0) {
1937 				ofl->ofl_flags |= FLG_OF_FATAL;
1938 				continue;
1939 			}
1940 
1941 			/*
1942 			 * Now that we have the name, if the section index
1943 			 * was bad, report it.
1944 			 */
1945 			if (shndx_bad) {
1946 				eprintf(ofl->ofl_lml, ERR_WARNING,
1947 				    MSG_INTL(MSG_SYM_INVSHNDX),
1948 				    demangle_symname(name, isc->is_name, ndx),
1949 				    ifl->ifl_name,
1950 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
1951 				continue;
1952 			}
1953 
1954 			/*
1955 			 * If this local symbol table originates from a shared
1956 			 * object, then we're only interested in recording
1957 			 * register symbols.  As local symbol descriptors aren't
1958 			 * allocated for shared objects, one will be allocated
1959 			 * to associated with the register symbol.  This symbol
1960 			 * won't become part of the output image, but we must
1961 			 * process it to test for register conflicts.
1962 			 */
1963 			rsdp = sdp = 0;
1964 			if (sdflags & FLG_SY_REGSYM) {
1965 				/*
1966 				 * The presence of FLG_SY_REGSYM means that
1967 				 * the pointers in ld_targ.t_ms are non-NULL.
1968 				 */
1969 				rsdp = (*ld_targ.t_ms.ms_reg_find)(sym, ofl);
1970 				if (rsdp != 0) {
1971 					/*
1972 					 * The fact that another register def-
1973 					 * inition has been found is fatal.
1974 					 * Call the verification routine to get
1975 					 * the error message and move on.
1976 					 */
1977 					(void) (*ld_targ.t_ms.ms_reg_check)
1978 					    (rsdp, sym, name, ifl, ofl);
1979 					continue;
1980 				}
1981 
1982 				if (etype == ET_DYN) {
1983 					if ((sdp = libld_calloc(
1984 					    sizeof (Sym_desc), 1)) == 0)
1985 						return (S_ERROR);
1986 					sdp->sd_ref = REF_DYN_SEEN;
1987 				}
1988 			} else if (etype == ET_DYN)
1989 				continue;
1990 
1991 			/*
1992 			 * Fill in the remaining symbol descriptor information.
1993 			 */
1994 			if (sdp == 0) {
1995 				sdp = &(ifl->ifl_locs[ndx]);
1996 				sdp->sd_ref = REF_REL_NEED;
1997 			}
1998 			if (rsdp == 0) {
1999 				sdp->sd_name = name;
2000 				sdp->sd_sym = sym;
2001 				sdp->sd_shndx = shndx;
2002 				sdp->sd_flags = sdflags;
2003 				sdp->sd_file = ifl;
2004 				ifl->ifl_oldndx[ndx] = sdp;
2005 			}
2006 
2007 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
2008 
2009 			/*
2010 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
2011 			 * so as to simplify future processing.
2012 			 */
2013 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
2014 				sdp->sd_shndx = shndx = SHN_UNDEF;
2015 				sdp->sd_flags1 |=
2016 				    (FLG_SY1_IGNORE | FLG_SY1_ELIM);
2017 			}
2018 
2019 			/*
2020 			 * Process any register symbols.
2021 			 */
2022 			if (sdp->sd_flags & FLG_SY_REGSYM) {
2023 				/*
2024 				 * Add a diagnostic to indicate we've caught a
2025 				 * register symbol, as this can be useful if a
2026 				 * register conflict is later discovered.
2027 				 */
2028 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
2029 
2030 				/*
2031 				 * If this register symbol hasn't already been
2032 				 * recorded, enter it now.
2033 				 *
2034 				 * The presence of FLG_SY_REGSYM means that
2035 				 * the pointers in ld_targ.t_ms are non-NULL.
2036 				 */
2037 				if ((rsdp == 0) &&
2038 				    ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) ==
2039 				    0))
2040 					return (S_ERROR);
2041 			}
2042 
2043 			/*
2044 			 * Assign an input section.
2045 			 */
2046 			if ((sym->st_shndx != SHN_UNDEF) &&
2047 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
2048 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
2049 
2050 			/*
2051 			 * If this symbol falls within the range of a section
2052 			 * being discarded, then discard the symbol itself.
2053 			 * There is no reason to keep this local symbol.
2054 			 */
2055 			if (sdp->sd_isc &&
2056 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
2057 				sdp->sd_flags |= FLG_SY_ISDISC;
2058 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
2059 				continue;
2060 			}
2061 
2062 			/*
2063 			 * Skip any section symbols as new versions of these
2064 			 * will be created.
2065 			 */
2066 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
2067 				if (sym->st_shndx == SHN_UNDEF) {
2068 					eprintf(ofl->ofl_lml, ERR_WARNING,
2069 					    MSG_INTL(MSG_SYM_INVSHNDX),
2070 					    demangle_symname(name, isc->is_name,
2071 					    ndx), ifl->ifl_name,
2072 					    conv_sym_shndx(sym->st_shndx,
2073 					    &inv_buf));
2074 				}
2075 				continue;
2076 			}
2077 
2078 			/*
2079 			 * For a relocatable object, if this symbol is defined
2080 			 * and has non-zero length and references an address
2081 			 * within an associated section, then check its extents
2082 			 * to make sure the section boundaries encompass it.
2083 			 * If they don't, the ELF file is corrupt.
2084 			 */
2085 			if (etype_rel && SYM_LOC_BADADDR(sdp, sym, type)) {
2086 				issue_badaddr_msg(ifl, ofl, sdp, sym, shndx);
2087 				continue;
2088 			}
2089 
2090 			/*
2091 			 * Sanity check for TLS
2092 			 */
2093 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
2094 			    (sym->st_shndx != SHN_COMMON))) {
2095 				Is_desc	*isp = sdp->sd_isc;
2096 
2097 				if ((isp == 0) || (isp->is_shdr == 0) ||
2098 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
2099 					eprintf(ofl->ofl_lml, ERR_FATAL,
2100 					    MSG_INTL(MSG_SYM_TLS),
2101 					    demangle(sdp->sd_name),
2102 					    ifl->ifl_name);
2103 					ofl->ofl_flags |= FLG_OF_FATAL;
2104 					continue;
2105 				}
2106 			}
2107 
2108 			/*
2109 			 * Carry our some basic sanity checks (these are just
2110 			 * some of the erroneous symbol entries we've come
2111 			 * across, there's probably a lot more).  The symbol
2112 			 * will not be carried forward to the output file, which
2113 			 * won't be a problem unless a relocation is required
2114 			 * against it.
2115 			 */
2116 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
2117 			    ((sym->st_shndx == SHN_COMMON)) ||
2118 			    ((type == STT_FILE) &&
2119 			    (sym->st_shndx != SHN_ABS))) ||
2120 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == 0))) {
2121 				eprintf(ofl->ofl_lml, ERR_WARNING,
2122 				    MSG_INTL(MSG_SYM_INVSHNDX),
2123 				    demangle_symname(name, isc->is_name, ndx),
2124 				    ifl->ifl_name,
2125 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
2126 				sdp->sd_isc = NULL;
2127 				sdp->sd_flags |= FLG_SY_INVALID;
2128 				continue;
2129 			}
2130 
2131 			/*
2132 			 * As these local symbols will become part of the output
2133 			 * image, record their number and name string size.
2134 			 * Globals are counted after all input file processing
2135 			 * (and hence symbol resolution) is complete during
2136 			 * sym_validate().
2137 			 */
2138 			if (!(ofl->ofl_flags & FLG_OF_REDLSYM)) {
2139 				ofl->ofl_locscnt++;
2140 
2141 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
2142 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
2143 				    sdp->sd_name) == -1))
2144 					return (S_ERROR);
2145 
2146 				if (allow_ldynsym && sym->st_name &&
2147 				    ldynsym_symtype[type]) {
2148 					ofl->ofl_dynlocscnt++;
2149 					if (st_insert(ofl->ofl_dynstrtab,
2150 					    sdp->sd_name) == -1)
2151 						return (S_ERROR);
2152 					/* Include it in sort section? */
2153 					DYNSORT_COUNT(sdp, sym, type, ++);
2154 				}
2155 			}
2156 		}
2157 	}
2158 
2159 	/*
2160 	 * The GNU ld interprets the top bit of the 16-bit Versym value
2161 	 * (0x8000) as the "hidden" bit. If this bit is set, the linker
2162 	 * is supposed to act as if that symbol does not exist. The Solaris
2163 	 * linker does not support this mechanism, or the model of interface
2164 	 * evolution that it allows, but we honor it in GNU ld produced
2165 	 * objects in order to interoperate with them.
2166 	 *
2167 	 * Determine if we should honor the GNU hidden bit for this file.
2168 	 */
2169 	test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) &&
2170 	    (ifl->ifl_versym != NULL);
2171 
2172 	/*
2173 	 * Now scan the global symbols entering them in the internal symbol
2174 	 * table or resolving them as necessary.
2175 	 */
2176 	sym = (Sym *)isc->is_indata->d_buf;
2177 	sym += local;
2178 	/* LINTED */
2179 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
2180 		const char	*name;
2181 		Word		shndx, sdflags = 0;
2182 		int		shndx_bad = 0;
2183 
2184 		/*
2185 		 * Determine and validate the associated section index.
2186 		 */
2187 		if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
2188 			shndx = symshndx[ndx];
2189 		} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
2190 			sdflags |= FLG_SY_SPECSEC;
2191 		} else if (shndx > ifl->ifl_ehdr->e_shnum) {
2192 			/* We need the name before we can issue error */
2193 			shndx_bad = 1;
2194 		}
2195 
2196 		/*
2197 		 * Check if st_name has a valid value or not.
2198 		 */
2199 		if ((name = string(ofl, ifl, sym, strs, strsize, ndx, shndx,
2200 		    symsecname, strsecname, &sdflags)) == 0) {
2201 			ofl->ofl_flags |= FLG_OF_FATAL;
2202 			continue;
2203 		}
2204 
2205 		/*
2206 		 * Now that we have the name, if the section index
2207 		 * was bad, report it.
2208 		 */
2209 		if (shndx_bad) {
2210 			eprintf(ofl->ofl_lml, ERR_WARNING,
2211 			    MSG_INTL(MSG_SYM_INVSHNDX),
2212 			    demangle_symname(name, isc->is_name, ndx),
2213 			    ifl->ifl_name,
2214 			    conv_sym_shndx(sym->st_shndx, &inv_buf));
2215 			continue;
2216 		}
2217 
2218 
2219 		/*
2220 		 * Test for the GNU hidden bit, and ignore symbols that
2221 		 * have it set.
2222 		 */
2223 		if (test_gnu_hidden_bit &&
2224 		    ((ifl->ifl_versym[ndx] & 0x8000) != 0))
2225 			continue;
2226 
2227 		/*
2228 		 * The linker itself will generate symbols for _end, _etext,
2229 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
2230 		 * bother entering these symbols from shared objects.  This
2231 		 * results in some wasted resolution processing, which is hard
2232 		 * to feel, but if nothing else, pollutes diagnostic relocation
2233 		 * output.
2234 		 */
2235 		if (name[0] && (etype == ET_DYN) && (sym->st_size == 0) &&
2236 		    (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) &&
2237 		    (name[0] == '_') && ((name[1] == 'e') ||
2238 		    (name[1] == 'D') || (name[1] == 'P')) &&
2239 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
2240 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
2241 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
2242 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
2243 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
2244 			ifl->ifl_oldndx[ndx] = 0;
2245 			continue;
2246 		}
2247 
2248 		/*
2249 		 * Determine and validate the symbols binding.
2250 		 */
2251 		bind = ELF_ST_BIND(sym->st_info);
2252 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
2253 			eprintf(ofl->ofl_lml, ERR_WARNING,
2254 			    MSG_INTL(MSG_SYM_NONGLOB),
2255 			    demangle_symname(name, isc->is_name, ndx),
2256 			    ifl->ifl_name,
2257 			    conv_sym_info_bind(bind, 0, &inv_buf));
2258 			continue;
2259 		}
2260 
2261 		/*
2262 		 * If this symbol falls within the range of a section being
2263 		 * discarded, then discard the symbol itself.
2264 		 */
2265 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
2266 		    (sym->st_shndx != SHN_UNDEF)) {
2267 			Is_desc	*isp;
2268 
2269 			if (shndx >= ifl->ifl_shnum) {
2270 				/*
2271 				 * Carry our some basic sanity checks
2272 				 * The symbol will not be carried forward to
2273 				 * the output file, which won't be a problem
2274 				 * unless a relocation is required against it.
2275 				 */
2276 				eprintf(ofl->ofl_lml, ERR_WARNING,
2277 				    MSG_INTL(MSG_SYM_INVSHNDX),
2278 				    demangle_symname(name, isc->is_name, ndx),
2279 				    ifl->ifl_name,
2280 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
2281 				continue;
2282 			}
2283 
2284 			isp = ifl->ifl_isdesc[shndx];
2285 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
2286 				if ((sdp =
2287 				    libld_calloc(sizeof (Sym_desc), 1)) == 0)
2288 					return (S_ERROR);
2289 
2290 				/*
2291 				 * Create a dummy symbol entry so that if we
2292 				 * find any references to this discarded symbol
2293 				 * we can compensate.
2294 				 */
2295 				sdp->sd_name = name;
2296 				sdp->sd_sym = sym;
2297 				sdp->sd_file = ifl;
2298 				sdp->sd_isc = isp;
2299 				sdp->sd_flags = FLG_SY_ISDISC;
2300 				ifl->ifl_oldndx[ndx] = sdp;
2301 
2302 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
2303 				continue;
2304 			}
2305 		}
2306 
2307 		/*
2308 		 * If the symbol does not already exist in the internal symbol
2309 		 * table add it, otherwise resolve the conflict.  If the symbol
2310 		 * from this file is kept, retain its symbol table index for
2311 		 * possible use in associating a global alias.
2312 		 */
2313 		/* LINTED */
2314 		hash = (Word)elf_hash((const char *)name);
2315 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
2316 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
2317 			if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, ndx,
2318 			    shndx, sdflags, 0, &where)) == (Sym_desc *)S_ERROR)
2319 				return (S_ERROR);
2320 
2321 		} else if (ld_sym_resolve(sdp, sym, ifl, ofl, ndx, shndx,
2322 		    sdflags) == S_ERROR)
2323 			return (S_ERROR);
2324 
2325 		/*
2326 		 * After we've compared a defined symbol in one shared
2327 		 * object, flag the symbol so we don't compare it again.
2328 		 */
2329 		if ((etype == ET_DYN) && (sym->st_shndx != SHN_UNDEF) &&
2330 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
2331 			sdp->sd_flags |= FLG_SY_SOFOUND;
2332 
2333 		/*
2334 		 * If the symbol is accepted from this file retain the symbol
2335 		 * index for possible use in aliasing.
2336 		 */
2337 		if (sdp->sd_file == ifl)
2338 			sdp->sd_symndx = ndx;
2339 
2340 		ifl->ifl_oldndx[ndx] = sdp;
2341 
2342 		/*
2343 		 * If we've accepted a register symbol, continue to validate
2344 		 * it.
2345 		 */
2346 		if (sdp->sd_flags & FLG_SY_REGSYM) {
2347 			Sym_desc	*rsdp;
2348 
2349 			/*
2350 			 * The presence of FLG_SY_REGSYM means that
2351 			 * the pointers in ld_targ.t_ms are non-NULL.
2352 			 */
2353 			rsdp = (*ld_targ.t_ms.ms_reg_find)(sdp->sd_sym, ofl);
2354 			if (rsdp == 0) {
2355 				if ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 0)
2356 					return (S_ERROR);
2357 			} else if (rsdp != sdp) {
2358 				(void) (*ld_targ.t_ms.ms_reg_check)(rsdp,
2359 				    sdp->sd_sym, sdp->sd_name, ifl, ofl);
2360 			}
2361 		}
2362 
2363 		/*
2364 		 * For a relocatable object, if this symbol is defined
2365 		 * and has non-zero length and references an address
2366 		 * within an associated section, then check its extents
2367 		 * to make sure the section boundaries encompass it.
2368 		 * If they don't, the ELF file is corrupt. Note that this
2369 		 * global symbol may have come from another file to satisfy
2370 		 * an UNDEF symbol of the same name from this one. In that
2371 		 * case, we don't check it, because it was already checked
2372 		 * as part of its own file.
2373 		 */
2374 		if (etype_rel && (sdp->sd_file == ifl)) {
2375 			Sym *tsym = sdp->sd_sym;
2376 
2377 			if (SYM_LOC_BADADDR(sdp, tsym,
2378 			    ELF_ST_TYPE(tsym->st_info))) {
2379 				issue_badaddr_msg(ifl, ofl, sdp,
2380 				    tsym, tsym->st_shndx);
2381 				continue;
2382 			}
2383 		}
2384 	}
2385 
2386 	/*
2387 	 * If this is a shared object scan the globals one more time and
2388 	 * associate any weak/global associations.  This association is needed
2389 	 * should the weak definition satisfy a reference in the dynamic
2390 	 * executable:
2391 	 *
2392 	 *  o	if the symbol is a data item it will be copied to the
2393 	 *	executables address space, thus we must also reassociate the
2394 	 *	alias symbol with its new location in the executable.
2395 	 *
2396 	 *  o	if the symbol is a function then we may need to promote	the
2397 	 *	symbols binding from undefined weak to undefined, otherwise the
2398 	 *	run-time linker will not generate the correct relocation error
2399 	 *	should the symbol not be found.
2400 	 *
2401 	 * The true association between a weak/strong symbol pair is that both
2402 	 * symbol entries are identical, thus first we created a sorted symbol
2403 	 * list keyed off of the symbols value (if the value is the same chances
2404 	 * are the rest of the symbols data is).  This list is then scanned for
2405 	 * weak symbols, and if one is found then any strong association will
2406 	 * exist in the following entries.  Thus we just have to scan one
2407 	 * (typical single alias) or more (in the uncommon instance of multiple
2408 	 * weak to strong associations) entries to determine if a match exists.
2409 	 */
2410 	if ((OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) &&
2411 	    (total > local)) {
2412 		Sym_desc **	sort;
2413 		size_t		size = (total - local) * sizeof (Sym_desc *);
2414 
2415 		if ((sort = libld_malloc(size)) == 0)
2416 			return (S_ERROR);
2417 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], size);
2418 
2419 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
2420 
2421 		for (ndx = 0; ndx < (total - local); ndx++) {
2422 			Sym_desc *	wsdp = sort[ndx];
2423 			Sym *		wsym;
2424 			int		sndx;
2425 
2426 			if (wsdp == 0)
2427 				continue;
2428 
2429 			wsym = wsdp->sd_sym;
2430 
2431 			if ((ELF_ST_BIND(wsym->st_info) != STB_WEAK) ||
2432 			    (wsdp->sd_sym->st_shndx == SHN_UNDEF) ||
2433 			    (wsdp->sd_flags & FLG_SY_SPECSEC))
2434 				continue;
2435 
2436 			/*
2437 			 * We have a weak symbol, if it has a strong alias it
2438 			 * will have been sorted to one of the following sort
2439 			 * table entries.  Note that we could have multiple weak
2440 			 * symbols aliased to one strong (if this occurs then
2441 			 * the strong symbol only maintains one alias back to
2442 			 * the last weak).
2443 			 */
2444 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
2445 				Sym_desc *	ssdp = sort[sndx];
2446 				Sym *		ssym;
2447 
2448 				if (ssdp == 0)
2449 					break;
2450 
2451 				ssym = ssdp->sd_sym;
2452 
2453 				if (wsym->st_value != ssym->st_value)
2454 					break;
2455 
2456 				if ((ssdp->sd_file == ifl) &&
2457 				    (wsdp->sd_file == ifl) &&
2458 				    (wsym->st_size == ssym->st_size) &&
2459 				    (ssdp->sd_sym->st_shndx != SHN_UNDEF) &&
2460 				    (ELF_ST_BIND(ssym->st_info) != STB_WEAK) &&
2461 				    ((ssdp->sd_flags & FLG_SY_SPECSEC) == 0)) {
2462 					int w_dynbits, s_dynbits;
2463 
2464 					/*
2465 					 * If a sharable object, set link
2466 					 * fields so they reference each other
2467 					 */
2468 					if (etype == ET_DYN) {
2469 						ssdp->sd_aux->sa_linkndx =
2470 						    (Word)wsdp->sd_symndx;
2471 						wsdp->sd_aux->sa_linkndx =
2472 						    (Word)ssdp->sd_symndx;
2473 					}
2474 					/*
2475 					 * Determine which of these two symbols
2476 					 * go into the sort section. If the
2477 					 * mapfile has made explicit settings
2478 					 * of the FLG_SY_*DYNSORT flags for both
2479 					 * symbols, then we do what they say.
2480 					 * If one has the DYNSORT flags set,
2481 					 * we set the NODYNSORT bit in the
2482 					 * other. And if neither has an
2483 					 * explicit setting, then we favor the
2484 					 * weak symbol because they usually
2485 					 * lack the leading underscore.
2486 					 */
2487 					w_dynbits = wsdp->sd_flags &
2488 					    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2489 					s_dynbits = ssdp->sd_flags &
2490 					    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2491 					if (!(w_dynbits && s_dynbits)) {
2492 						if (s_dynbits) {
2493 							if (s_dynbits ==
2494 							    FLG_SY_DYNSORT)
2495 							wsdp->sd_flags |=
2496 							    FLG_SY_NODYNSORT;
2497 						} else if (w_dynbits !=
2498 						    FLG_SY_NODYNSORT) {
2499 							ssdp->sd_flags |=
2500 							    FLG_SY_NODYNSORT;
2501 						}
2502 					}
2503 					break;
2504 				}
2505 			}
2506 		}
2507 	}
2508 	return (1);
2509 
2510 #undef SYM_LOC_BADADDR
2511 }
2512 
2513 /*
2514  * Add an undefined symbol to the symbol table.  The reference originates from
2515  * the location identifed by the message id (mid).  These references can
2516  * originate from command line options such as -e, -u, -initarray, etc.
2517  * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated
2518  * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)).
2519  */
2520 Sym_desc *
2521 ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid)
2522 {
2523 	Sym		*sym;
2524 	Ifl_desc	*ifl = 0, *_ifl;
2525 	Sym_desc	*sdp;
2526 	Word		hash;
2527 	Listnode	*lnp;
2528 	avl_index_t	where;
2529 	const char	*reference = MSG_INTL(mid);
2530 
2531 	/*
2532 	 * As an optimization, determine whether we've already generated this
2533 	 * reference.  If the symbol doesn't already exist we'll create it.
2534 	 * Or if the symbol does exist from a different source, we'll resolve
2535 	 * the conflict.
2536 	 */
2537 	/* LINTED */
2538 	hash = (Word)elf_hash(name);
2539 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) {
2540 		if ((sdp->sd_sym->st_shndx == SHN_UNDEF) &&
2541 		    (sdp->sd_file->ifl_name == reference))
2542 			return (sdp);
2543 	}
2544 
2545 	/*
2546 	 * Determine whether a pseudo input file descriptor exists to represent
2547 	 * the command line, as any global symbol needs an input file descriptor
2548 	 * during any symbol resolution (refer to map_ifl() which provides a
2549 	 * similar method for adding symbols from mapfiles).
2550 	 */
2551 	for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, _ifl))
2552 		if (strcmp(_ifl->ifl_name, reference) == 0) {
2553 			ifl = _ifl;
2554 			break;
2555 		}
2556 
2557 	/*
2558 	 * If no descriptor exists create one.
2559 	 */
2560 	if (ifl == 0) {
2561 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) ==
2562 		    (Ifl_desc *)0)
2563 			return ((Sym_desc *)S_ERROR);
2564 		ifl->ifl_name = reference;
2565 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
2566 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr),
2567 		    1)) == 0)
2568 			return ((Sym_desc *)S_ERROR);
2569 		ifl->ifl_ehdr->e_type = ET_REL;
2570 
2571 		if (list_appendc(&ofl->ofl_objs, ifl) == 0)
2572 			return ((Sym_desc *)S_ERROR);
2573 	}
2574 
2575 	/*
2576 	 * Allocate a symbol structure and add it to the global symbol table.
2577 	 */
2578 	if ((sym = libld_calloc(sizeof (Sym), 1)) == 0)
2579 		return ((Sym_desc *)S_ERROR);
2580 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
2581 	sym->st_shndx = SHN_UNDEF;
2582 
2583 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
2584 	if (sdp == NULL) {
2585 		DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
2586 		if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
2587 		    0, 0, &where)) == (Sym_desc *)S_ERROR)
2588 			return ((Sym_desc *)S_ERROR);
2589 	} else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0,
2590 	    SHN_UNDEF, 0) == S_ERROR)
2591 		return ((Sym_desc *)S_ERROR);
2592 
2593 	sdp->sd_flags &= ~FLG_SY_CLEAN;
2594 	sdp->sd_flags |= FLG_SY_CMDREF;
2595 
2596 	return (sdp);
2597 }
2598