xref: /titanic_44/usr/src/cmd/sgs/libld/common/syms.c (revision e3e7cd2990d896de7fff76bc91b8cf570470d6ba)
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, oflags1 = ofl->ofl_flags1;
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) ||
980 		    (oflags1 & FLG_OF1_AUTOELM)) &&
981 		    ((sdp->sd_flags1 & MSK_SY1_NOAUTO) == 0)) {
982 			if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) {
983 				sdp->sd_flags |= FLG_SY_REDUCED;
984 				sdp->sd_flags1 |= FLG_SY1_HIDDEN;
985 			}
986 
987 			if (oflags1 & (FLG_OF1_REDLSYM | FLG_OF1_AUTOELM)) {
988 				sdp->sd_flags1 |= FLG_SY1_ELIM;
989 				sym->st_other = STV_ELIMINATE |
990 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
991 			} else if (ELF_ST_VISIBILITY(sym->st_other) !=
992 			    STV_INTERNAL)
993 				sym->st_other = STV_HIDDEN |
994 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
995 		}
996 
997 		/*
998 		 * If -Bsymbolic is in effect, and the symbol hasn't explicitly
999 		 * been defined nodirect (via a mapfile), then bind the global
1000 		 * symbol symbolically and assign the STV_PROTECTED visibility
1001 		 * attribute.
1002 		 */
1003 		if ((oflags & FLG_OF_SYMBOLIC) &&
1004 		    ((sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_NDIR)) == 0)) {
1005 			sdp->sd_flags1 |= FLG_SY1_PROTECT;
1006 			if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT)
1007 				sym->st_other = STV_PROTECTED |
1008 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
1009 		}
1010 	}
1011 
1012 	/*
1013 	 * Indicate that this symbol has had it's visibility checked so that
1014 	 * we don't need to do this investigation again.
1015 	 */
1016 	sdp->sd_flags |= FLG_SY_VISIBLE;
1017 }
1018 
1019 /*
1020  * Make sure a symbol definition is local to the object being built.
1021  */
1022 static int
1023 ensure_sym_local(Ofl_desc *ofl, Sym_desc *sdp, const char *str)
1024 {
1025 	if (sdp->sd_sym->st_shndx == SHN_UNDEF) {
1026 		if (str) {
1027 			eprintf(ofl->ofl_lml, ERR_FATAL,
1028 			    MSG_INTL(MSG_SYM_UNDEF), str,
1029 			    demangle((char *)sdp->sd_name));
1030 		}
1031 		return (1);
1032 	}
1033 	if (sdp->sd_ref != REF_REL_NEED) {
1034 		if (str) {
1035 			eprintf(ofl->ofl_lml, ERR_FATAL,
1036 			    MSG_INTL(MSG_SYM_EXTERN), str,
1037 			    demangle((char *)sdp->sd_name),
1038 			    sdp->sd_file->ifl_name);
1039 		}
1040 		return (1);
1041 	}
1042 
1043 	sdp->sd_flags |= FLG_SY_UPREQD;
1044 	if (sdp->sd_isc) {
1045 		sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1046 		sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1047 	}
1048 	return (0);
1049 }
1050 
1051 /*
1052  * Make sure all the symbol definitions required for initarray, finiarray, or
1053  * preinitarray's are local to the object being built.
1054  */
1055 static int
1056 ensure_array_local(Ofl_desc *ofl, List *list, const char *str)
1057 {
1058 	Listnode	*lnp;
1059 	Sym_desc	*sdp;
1060 	int		ret = 0;
1061 
1062 	for (LIST_TRAVERSE(list, lnp, sdp))
1063 		ret += ensure_sym_local(ofl, sdp, str);
1064 
1065 	return (ret);
1066 }
1067 
1068 /*
1069  * After all symbol table input processing has been finished, and all relocation
1070  * counting has been carried out (ie. no more symbols will be read, generated,
1071  * or modified), validate and count the relevant entries:
1072  *
1073  *	o	check and print any undefined symbols remaining.  Note that
1074  *		if a symbol has been defined by virtue of the inclusion of
1075  *		an implicit shared library, it is still classed as undefined.
1076  *
1077  * 	o	count the number of global needed symbols together with the
1078  *		size of their associated name strings (if scoping has been
1079  *		indicated these symbols may be reduced to locals).
1080  *
1081  *	o	establish the size and alignment requirements for the global
1082  *		.bss section (the alignment of this section is based on the
1083  *		first symbol that it will contain).
1084  */
1085 uintptr_t
1086 ld_sym_validate(Ofl_desc *ofl)
1087 {
1088 	Sym_avlnode	*sav;
1089 	Sym_desc	*sdp;
1090 	Sym		*sym;
1091 	ofl_flag_t	oflags = ofl->ofl_flags;
1092 	ofl_flag_t	undef = 0, needed = 0, verdesc = 0;
1093 	Xword		bssalign = 0, tlsalign = 0;
1094 	Xword		bsssize = 0, tlssize = 0;
1095 #if	defined(_ELF64)
1096 	Xword		lbssalign = 0, lbsssize = 0;
1097 #endif
1098 	int		ret;
1099 	int		allow_ldynsym;
1100 	uchar_t		type;
1101 
1102 	/*
1103 	 * If a symbol is undefined and this link-edit calls for no undefined
1104 	 * symbols to remain (this is the default case when generating an
1105 	 * executable but can be enforced for any object using -z defs), the
1106 	 * symbol is classified as undefined and a fatal error condition will
1107 	 * be indicated.
1108 	 *
1109 	 * If the symbol is undefined and we're creating a shared object with
1110 	 * the -Bsymbolic flag, then the symbol is also classified as undefined
1111 	 * and a warning condition will be indicated.
1112 	 */
1113 	if ((oflags & (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC)) ==
1114 	    (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC))
1115 		undef = FLG_OF_WARN;
1116 	if (oflags & FLG_OF_NOUNDEF)
1117 		undef = FLG_OF_FATAL;
1118 
1119 	/*
1120 	 * If the symbol is referenced from an implicitly included shared object
1121 	 * (ie. it's not on the NEEDED list) then the symbol is also classified
1122 	 * as undefined and a fatal error condition will be indicated.
1123 	 */
1124 	if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ))
1125 		needed = FLG_OF_FATAL;
1126 
1127 	/*
1128 	 * If the output image is being versioned all symbol definitions must be
1129 	 * associated with a version.  Any symbol that isn't is classified as
1130 	 * undefined and a fatal error condition will be indicated.
1131 	 */
1132 	if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL))
1133 		verdesc = FLG_OF_FATAL;
1134 
1135 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1136 
1137 	if (allow_ldynsym) {
1138 		/*
1139 		 * Normally, we disallow symbols with 0 size from appearing
1140 		 * in a dyn[sym|tls]sort section. However, there are some
1141 		 * symbols that serve special purposes that we want to exempt
1142 		 * from this rule. Look them up, and set their
1143 		 * FLG_SY_DYNSORT flag.
1144 		 */
1145 		static const char *special[] = {
1146 			MSG_ORIG(MSG_SYM_INIT_U),	/* _init */
1147 			MSG_ORIG(MSG_SYM_FINI_U),	/* _fini */
1148 			MSG_ORIG(MSG_SYM_START),	/* _start */
1149 			NULL
1150 		};
1151 		int i;
1152 
1153 		for (i = 0; special[i] != NULL; i++) {
1154 			if (((sdp = ld_sym_find(special[i],
1155 			    SYM_NOHASH, 0, ofl)) != NULL) &&
1156 			    (sdp->sd_sym->st_size == 0)) {
1157 				if (ld_sym_copy(sdp) == S_ERROR)
1158 					return (S_ERROR);
1159 				sdp->sd_flags |= FLG_SY_DYNSORT;
1160 			}
1161 		}
1162 	}
1163 
1164 	/*
1165 	 * Collect and validate the globals from the internal symbol table.
1166 	 */
1167 	for (sav = avl_first(&ofl->ofl_symavl); sav;
1168 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
1169 		Is_desc		*isp;
1170 		int		undeferr = 0;
1171 		uchar_t		vis;
1172 
1173 		sdp = sav->sav_symdesc;
1174 
1175 		/*
1176 		 * If undefined symbols are allowed ignore any symbols that are
1177 		 * not needed.
1178 		 */
1179 		if (!(oflags & FLG_OF_NOUNDEF) &&
1180 		    (sdp->sd_ref == REF_DYN_SEEN))
1181 			continue;
1182 
1183 		/*
1184 		 * If the symbol originates from an external or parent mapfile
1185 		 * reference and hasn't been matched to a reference from a
1186 		 * relocatable object, ignore it.
1187 		 */
1188 		if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) &&
1189 		    ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) {
1190 			sdp->sd_flags |= FLG_SY_INVALID;
1191 			continue;
1192 		}
1193 
1194 		sym = sdp->sd_sym;
1195 		type = ELF_ST_TYPE(sym->st_info);
1196 
1197 		/*
1198 		 * Sanity check TLS.
1199 		 */
1200 		if ((type == STT_TLS) && (sym->st_size != 0) &&
1201 		    (sym->st_shndx != SHN_UNDEF) &&
1202 		    (sym->st_shndx != SHN_COMMON)) {
1203 			Is_desc		*isp = sdp->sd_isc;
1204 			Ifl_desc	*ifl = sdp->sd_file;
1205 
1206 			if ((isp == 0) || (isp->is_shdr == 0) ||
1207 			    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
1208 				eprintf(ofl->ofl_lml, ERR_FATAL,
1209 				    MSG_INTL(MSG_SYM_TLS),
1210 				    demangle(sdp->sd_name), ifl->ifl_name);
1211 				ofl->ofl_flags |= FLG_OF_FATAL;
1212 				continue;
1213 			}
1214 		}
1215 
1216 		if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)
1217 			ld_sym_adjust_vis(sdp, ofl);
1218 
1219 		if ((sdp->sd_flags & FLG_SY_REDUCED) &&
1220 		    (oflags & FLG_OF_PROCRED)) {
1221 			DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL,
1222 			    sdp, 0, 0));
1223 		}
1224 
1225 		/*
1226 		 * Record any STV_SINGLETON existence.
1227 		 */
1228 		if ((vis = ELF_ST_VISIBILITY(sym->st_other)) == STV_SINGLETON)
1229 			ofl->ofl_dtflags_1 |= DF_1_SINGLETON;
1230 
1231 		/*
1232 		 * If building a shared object or executable, and this is a
1233 		 * non-weak UNDEF symbol with reduced visibility (STV_*), then
1234 		 * give a fatal error.
1235 		 */
1236 		if (((oflags & FLG_OF_RELOBJ) == 0) &&
1237 		    (sym->st_shndx == SHN_UNDEF) &&
1238 		    (ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
1239 			if (vis && (vis != STV_SINGLETON)) {
1240 				sym_undef_entry(ofl, sdp, BNDLOCAL);
1241 				ofl->ofl_flags |= FLG_OF_FATAL;
1242 				continue;
1243 			}
1244 		}
1245 
1246 		/*
1247 		 * If this symbol is defined in a non-allocatable section,
1248 		 * reduce it to local symbol.
1249 		 */
1250 		if (((isp = sdp->sd_isc) != 0) && isp->is_shdr &&
1251 		    ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) {
1252 			sdp->sd_flags |= FLG_SY_REDUCED;
1253 			sdp->sd_flags1 |= FLG_SY1_HIDDEN;
1254 		}
1255 
1256 		/*
1257 		 * If this symbol originated as a SHN_SUNW_IGNORE, it will have
1258 		 * been processed as an SHN_UNDEF.  Return the symbol to its
1259 		 * original index for validation, and propagation to the output
1260 		 * file.
1261 		 */
1262 		if (sdp->sd_flags1 & FLG_SY1_IGNORE)
1263 			sdp->sd_shndx = SHN_SUNW_IGNORE;
1264 
1265 		if (undef) {
1266 			/*
1267 			 * If a non-weak reference remains undefined, or if a
1268 			 * mapfile reference is not bound to the relocatable
1269 			 * objects that make up the object being built, we have
1270 			 * a fatal error.
1271 			 *
1272 			 * The exceptions are symbols which are defined to be
1273 			 * found in the parent (FLG_SY_PARENT), which is really
1274 			 * only meaningful for direct binding, or are defined
1275 			 * external (FLG_SY_EXTERN) so as to suppress -zdefs
1276 			 * errors.
1277 			 *
1278 			 * Register symbols are always allowed to be UNDEF.
1279 			 *
1280 			 * Note that we don't include references created via -u
1281 			 * in the same shared object binding test.  This is for
1282 			 * backward compatibility, in that a number of archive
1283 			 * makefile rules used -u to cause archive extraction.
1284 			 * These same rules have been cut and pasted to apply
1285 			 * to shared objects, and thus although the -u reference
1286 			 * is redundant, flagging it as fatal could cause some
1287 			 * build to fail.  Also we have documented the use of
1288 			 * -u as a mechanism to cause binding to weak version
1289 			 * definitions, thus giving users an error condition
1290 			 * would be incorrect.
1291 			 */
1292 			if (!(sdp->sd_flags & FLG_SY_REGSYM) &&
1293 			    ((sym->st_shndx == SHN_UNDEF) &&
1294 			    ((ELF_ST_BIND(sym->st_info) != STB_WEAK) &&
1295 			    ((sdp->sd_flags &
1296 			    (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) ||
1297 			    (((sdp->sd_flags &
1298 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
1299 			    FLG_SY_MAPREF) &&
1300 			    ((sdp->sd_flags1 & (FLG_SY1_HIDDEN |
1301 			    FLG_SY1_PROTECT)) == 0)))) {
1302 				sym_undef_entry(ofl, sdp, UNDEF);
1303 				ofl->ofl_flags |= undef;
1304 				undeferr = 1;
1305 			}
1306 
1307 		} else {
1308 			/*
1309 			 * For building things like shared objects (or anything
1310 			 * -znodefs), undefined symbols are allowed.
1311 			 *
1312 			 * If a mapfile reference remains undefined the user
1313 			 * would probably like a warning at least (they've
1314 			 * usually mis-spelt the reference).  Refer to the above
1315 			 * comments for discussion on -u references, which
1316 			 * are not tested for in the same manner.
1317 			 */
1318 			if ((sdp->sd_flags &
1319 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
1320 			    FLG_SY_MAPREF) {
1321 				sym_undef_entry(ofl, sdp, UNDEF);
1322 				ofl->ofl_flags |= FLG_OF_WARN;
1323 				undeferr = 1;
1324 			}
1325 		}
1326 
1327 		/*
1328 		 * If this symbol comes from a dependency mark the dependency
1329 		 * as required (-z ignore can result in unused dependencies
1330 		 * being dropped).  If we need to record dependency versioning
1331 		 * information indicate what version of the needed shared object
1332 		 * this symbol is part of.  Flag the symbol as undefined if it
1333 		 * has not been made available to us.
1334 		 */
1335 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1336 		    (!(sdp->sd_flags & FLG_SY_REFRSD))) {
1337 			sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD;
1338 
1339 			/*
1340 			 * Capture that we've bound to a symbol that doesn't
1341 			 * allow being directly bound to.
1342 			 */
1343 			if (sdp->sd_flags1 & FLG_SY1_NDIR)
1344 				ofl->ofl_flags1 |= FLG_OF1_NDIRECT;
1345 
1346 			if (sdp->sd_file->ifl_vercnt) {
1347 				int		vndx;
1348 				Ver_index	*vip;
1349 
1350 				vndx = sdp->sd_aux->sa_dverndx;
1351 				vip = &sdp->sd_file->ifl_verndx[vndx];
1352 				if (vip->vi_flags & FLG_VER_AVAIL) {
1353 					vip->vi_flags |= FLG_VER_REFER;
1354 				} else {
1355 					sym_undef_entry(ofl, sdp, NOTAVAIL);
1356 					ofl->ofl_flags |= FLG_OF_FATAL;
1357 					continue;
1358 				}
1359 			}
1360 		}
1361 
1362 		/*
1363 		 * Test that we do not bind to symbol supplied from an implicit
1364 		 * shared object.  If a binding is from a weak reference it can
1365 		 * be ignored.
1366 		 */
1367 		if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) &&
1368 		    (sdp->sd_ref == REF_DYN_NEED) &&
1369 		    (sdp->sd_flags & FLG_SY_NOTAVAIL)) {
1370 			sym_undef_entry(ofl, sdp, IMPLICIT);
1371 			ofl->ofl_flags |= needed;
1372 			continue;
1373 		}
1374 
1375 		/*
1376 		 * Test that a symbol isn't going to be reduced to local scope
1377 		 * which actually wants to bind to a shared object - if so it's
1378 		 * a fatal error.
1379 		 */
1380 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1381 		    (sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_PROTECT))) {
1382 			sym_undef_entry(ofl, sdp, BNDLOCAL);
1383 			ofl->ofl_flags |= FLG_OF_FATAL;
1384 			continue;
1385 		}
1386 
1387 		/*
1388 		 * If the output image is to be versioned then all symbol
1389 		 * definitions must be associated with a version.
1390 		 */
1391 		if (verdesc && (sdp->sd_ref == REF_REL_NEED) &&
1392 		    (sym->st_shndx != SHN_UNDEF) &&
1393 		    (!(sdp->sd_flags1 & FLG_SY1_HIDDEN)) &&
1394 		    (sdp->sd_aux->sa_overndx == 0)) {
1395 			sym_undef_entry(ofl, sdp, NOVERSION);
1396 			ofl->ofl_flags |= verdesc;
1397 			continue;
1398 		}
1399 
1400 		/*
1401 		 * If we don't need the symbol there's no need to process it
1402 		 * any further.
1403 		 */
1404 		if (sdp->sd_ref == REF_DYN_SEEN)
1405 			continue;
1406 
1407 		/*
1408 		 * Calculate the size and alignment requirements for the global
1409 		 * .bss and .tls sections.  If we're building a relocatable
1410 		 * object only account for scoped COMMON symbols (these will
1411 		 * be converted to .bss references).
1412 		 *
1413 		 * For partially initialized symbol,
1414 		 *  if it is expanded, it goes to sunwdata1.
1415 		 *  if it is local, it goes to .bss.
1416 		 *  if the output is shared object, it goes to .sunwbss.
1417 		 *
1418 		 * Also refer to make_mvsections() in sunwmove.c
1419 		 */
1420 		if ((sym->st_shndx == SHN_COMMON) &&
1421 		    (((oflags & FLG_OF_RELOBJ) == 0) ||
1422 		    ((sdp->sd_flags1 & FLG_SY1_HIDDEN) &&
1423 		    (oflags & FLG_OF_PROCRED)))) {
1424 			int countbss = 0;
1425 
1426 			if (sdp->sd_psyminfo == 0) {
1427 				countbss = 1;
1428 			} else if ((sdp->sd_flags & FLG_SY_PAREXPN) != 0) {
1429 				countbss = 0;
1430 			} else if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1431 				countbss = 1;
1432 			} else if ((ofl->ofl_flags & FLG_OF_SHAROBJ) != 0) {
1433 				countbss = 0;
1434 			} else
1435 				countbss = 1;
1436 
1437 			if (countbss) {
1438 				Xword * size, * align;
1439 
1440 				if (type != STT_TLS) {
1441 					size = &bsssize;
1442 					align = &bssalign;
1443 				} else {
1444 					size = &tlssize;
1445 					align = &tlsalign;
1446 				}
1447 				*size = (Xword)S_ROUND(*size, sym->st_value) +
1448 				    sym->st_size;
1449 				if (sym->st_value > *align)
1450 					*align = sym->st_value;
1451 			}
1452 		}
1453 
1454 #if	defined(_ELF64)
1455 		/*
1456 		 * Calculate the size and alignment requirement for the global
1457 		 * .lbss. TLS or partially initialized symbols do not need to be
1458 		 * considered yet.
1459 		 */
1460 		if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1461 		    (sym->st_shndx == SHN_X86_64_LCOMMON)) {
1462 			lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) +
1463 			    sym->st_size;
1464 			if (sym->st_value > lbssalign)
1465 				lbssalign = sym->st_value;
1466 		}
1467 #endif
1468 
1469 		/*
1470 		 * If a symbol was referenced via the command line
1471 		 * (ld -u <>, ...), then this counts as a reference against the
1472 		 * symbol. Mark any section that symbol is defined in.
1473 		 */
1474 		if (((isp = sdp->sd_isc) != 0) &&
1475 		    (sdp->sd_flags & FLG_SY_CMDREF)) {
1476 			isp->is_flags |= FLG_IS_SECTREF;
1477 			isp->is_file->ifl_flags |= FLG_IF_FILEREF;
1478 		}
1479 
1480 		/*
1481 		 * Update the symbol count and the associated name string size.
1482 		 */
1483 		if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) &&
1484 		    (oflags & FLG_OF_PROCRED)) {
1485 			/*
1486 			 * If any reductions are being processed, keep a count
1487 			 * of eliminated symbols, and if the symbol is being
1488 			 * reduced to local, count it's size for the .symtab.
1489 			 */
1490 			if (sdp->sd_flags1 & FLG_SY1_ELIM) {
1491 				ofl->ofl_elimcnt++;
1492 			} else {
1493 				ofl->ofl_scopecnt++;
1494 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1495 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
1496 				    sdp->sd_name) == -1))
1497 					return (S_ERROR);
1498 				if (allow_ldynsym && sym->st_name &&
1499 				    ldynsym_symtype[type]) {
1500 					ofl->ofl_dynscopecnt++;
1501 					if (st_insert(ofl->ofl_dynstrtab,
1502 					    sdp->sd_name) == -1)
1503 						return (S_ERROR);
1504 					/* Include it in sort section? */
1505 					DYNSORT_COUNT(sdp, sym, type, ++);
1506 				}
1507 			}
1508 		} else {
1509 			ofl->ofl_globcnt++;
1510 
1511 			/*
1512 			 * Check to see if this global variable should
1513 			 * go into a sort section. Sort sections require
1514 			 * a .SUNW_ldynsym section, so, don't check
1515 			 * unless a .SUNW_ldynsym is allowed.
1516 			 */
1517 			if (allow_ldynsym) {
1518 				DYNSORT_COUNT(sdp, sym, type, ++);
1519 			}
1520 
1521 			/*
1522 			 * If global direct bindings are in effect, or this
1523 			 * symbol has bound to a dependency which was specified
1524 			 * as requiring direct bindings, and it hasn't
1525 			 * explicitly been defined as a non-direct binding
1526 			 * symbol, mark it.
1527 			 */
1528 			if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp &&
1529 			    (isp->is_file->ifl_flags & FLG_IF_DIRECT))) &&
1530 			    ((sdp->sd_flags1 & FLG_SY1_NDIR) == 0))
1531 				sdp->sd_flags1 |= FLG_SY1_DIR;
1532 
1533 			/*
1534 			 * Insert the symbol name.
1535 			 */
1536 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1537 			    sym->st_name) {
1538 				if (st_insert(ofl->ofl_strtab,
1539 				    sdp->sd_name) == -1)
1540 					return (S_ERROR);
1541 
1542 				if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
1543 				    (st_insert(ofl->ofl_dynstrtab,
1544 				    sdp->sd_name) == -1))
1545 					return (S_ERROR);
1546 			}
1547 
1548 			/*
1549 			 * If this section offers a global symbol - record that
1550 			 * fact.
1551 			 */
1552 			if (isp) {
1553 				isp->is_flags |= FLG_IS_SECTREF;
1554 				isp->is_file->ifl_flags |= FLG_IF_FILEREF;
1555 			}
1556 		}
1557 	}
1558 
1559 	/*
1560 	 * If we've encountered a fatal error during symbol validation then
1561 	 * return now.
1562 	 */
1563 	if (ofl->ofl_flags & FLG_OF_FATAL)
1564 		return (1);
1565 
1566 	/*
1567 	 * Now that symbol resolution is completed, scan any register symbols.
1568 	 * From now on, we're only interested in those that contribute to the
1569 	 * output file.
1570 	 */
1571 	if (ofl->ofl_regsyms) {
1572 		int	ndx;
1573 
1574 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1575 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1576 				continue;
1577 			if (sdp->sd_ref != REF_REL_NEED) {
1578 				ofl->ofl_regsyms[ndx] = 0;
1579 				continue;
1580 			}
1581 
1582 			ofl->ofl_regsymcnt++;
1583 			if (sdp->sd_sym->st_name == 0)
1584 				sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY);
1585 
1586 			if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) ||
1587 			    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL))
1588 				ofl->ofl_lregsymcnt++;
1589 		}
1590 	}
1591 
1592 	/*
1593 	 * Generate the .bss section now that we know its size and alignment.
1594 	 */
1595 	if (bsssize || !(oflags & FLG_OF_RELOBJ)) {
1596 		if (ld_make_bss(ofl, bsssize, bssalign, MAKE_BSS) == S_ERROR)
1597 			return (S_ERROR);
1598 	}
1599 	if (tlssize) {
1600 		if (ld_make_bss(ofl, tlssize, tlsalign, MAKE_TLS) == S_ERROR)
1601 			return (S_ERROR);
1602 	}
1603 #if	defined(_ELF64)
1604 	if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1605 	    lbsssize && !(oflags & FLG_OF_RELOBJ)) {
1606 		if (ld_make_bss(ofl, lbsssize, lbssalign, MAKE_LBSS) == S_ERROR)
1607 			return (S_ERROR);
1608 	}
1609 #endif
1610 
1611 	/*
1612 	 * Determine what entry point symbol we need, and if found save its
1613 	 * symbol descriptor so that we can update the ELF header entry with the
1614 	 * symbols value later (see update_oehdr).  Make sure the symbol is
1615 	 * tagged to ensure its update in case -s is in effect.  Use any -e
1616 	 * option first, or the default entry points `_start' and `main'.
1617 	 */
1618 	ret = 0;
1619 	if (ofl->ofl_entry) {
1620 		if ((sdp =
1621 		    ld_sym_find(ofl->ofl_entry, SYM_NOHASH, 0, ofl)) == NULL) {
1622 			eprintf(ofl->ofl_lml, ERR_FATAL,
1623 			    MSG_INTL(MSG_ARG_NOENTRY), ofl->ofl_entry);
1624 			ret++;
1625 		} else if (ensure_sym_local(ofl, sdp,
1626 		    MSG_INTL(MSG_SYM_ENTRY)) != 0) {
1627 			ret++;
1628 		} else {
1629 			ofl->ofl_entry = (void *)sdp;
1630 		}
1631 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START),
1632 	    SYM_NOHASH, 0, ofl)) != NULL) && (ensure_sym_local(ofl,
1633 	    sdp, 0) == 0)) {
1634 		ofl->ofl_entry = (void *)sdp;
1635 
1636 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN),
1637 	    SYM_NOHASH, 0, ofl)) != NULL) && (ensure_sym_local(ofl,
1638 	    sdp, 0) == 0)) {
1639 		ofl->ofl_entry = (void *)sdp;
1640 	}
1641 
1642 	/*
1643 	 * If ld -zdtrace=<sym> was given, then validate that the symbol is
1644 	 * defined within the current object being built.
1645 	 */
1646 	if ((sdp = ofl->ofl_dtracesym) != 0)
1647 		ret += ensure_sym_local(ofl, sdp, MSG_ORIG(MSG_STR_DTRACE));
1648 
1649 	/*
1650 	 * If any initarray, finiarray or preinitarray functions have been
1651 	 * requested, make sure they are defined within the current object
1652 	 * being built.
1653 	 */
1654 	if (ofl->ofl_initarray.head) {
1655 		ret += ensure_array_local(ofl, &ofl->ofl_initarray,
1656 		    MSG_ORIG(MSG_SYM_INITARRAY));
1657 	}
1658 	if (ofl->ofl_finiarray.head) {
1659 		ret += ensure_array_local(ofl, &ofl->ofl_finiarray,
1660 		    MSG_ORIG(MSG_SYM_FINIARRAY));
1661 	}
1662 	if (ofl->ofl_preiarray.head) {
1663 		ret += ensure_array_local(ofl, &ofl->ofl_preiarray,
1664 		    MSG_ORIG(MSG_SYM_PREINITARRAY));
1665 	}
1666 
1667 	if (ret)
1668 		return (S_ERROR);
1669 
1670 	/*
1671 	 * If we're required to record any needed dependencies versioning
1672 	 * information calculate it now that all symbols have been validated.
1673 	 */
1674 	if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
1675 		return (ld_vers_check_need(ofl));
1676 	else
1677 		return (1);
1678 }
1679 
1680 /*
1681  * qsort(3c) comparison function.  As an optimization for associating weak
1682  * symbols to their strong counterparts sort global symbols according to their
1683  * address and binding.
1684  */
1685 static int
1686 compare(const void * sdpp1, const void * sdpp2)
1687 {
1688 	Sym_desc *	sdp1 = *((Sym_desc **)sdpp1);
1689 	Sym_desc *	sdp2 = *((Sym_desc **)sdpp2);
1690 	Sym *		sym1, * sym2;
1691 	uchar_t		bind1, bind2;
1692 
1693 	/*
1694 	 * Symbol descriptors may be zero, move these to the front of the
1695 	 * sorted array.
1696 	 */
1697 	if (sdp1 == 0)
1698 		return (-1);
1699 	if (sdp2 == 0)
1700 		return (1);
1701 
1702 	sym1 = sdp1->sd_sym;
1703 	sym2 = sdp2->sd_sym;
1704 
1705 	/*
1706 	 * Compare the symbols value (address).
1707 	 */
1708 	if (sym1->st_value > sym2->st_value)
1709 		return (1);
1710 	if (sym1->st_value < sym2->st_value)
1711 		return (-1);
1712 
1713 	bind1 = ELF_ST_BIND(sym1->st_info);
1714 	bind2 = ELF_ST_BIND(sym2->st_info);
1715 
1716 	/*
1717 	 * If two symbols have the same address place the weak symbol before
1718 	 * any strong counterpart.
1719 	 */
1720 	if (bind1 > bind2)
1721 		return (-1);
1722 	if (bind1 < bind2)
1723 		return (1);
1724 
1725 	return (0);
1726 }
1727 
1728 
1729 /*
1730  * Issue a MSG_SYM_BADADDR error from ld_sym_process(). This error
1731  * is issued when a symbol address/size is not contained by the
1732  * target section.
1733  *
1734  * Such objects are at least partially corrupt, and the user would
1735  * be well advised to be skeptical of them, and to ask their compiler
1736  * supplier to fix the problem. However, a distinction needs to be
1737  * made between symbols that reference readonly text, and those that
1738  * access writable data. Other than throwing off profiling results,
1739  * the readonly section case is less serious. We have encountered
1740  * such objects in the field. In order to allow existing objects
1741  * to continue working, we issue a warning rather than a fatal error
1742  * if the symbol is against readonly text. Other cases are fatal.
1743  */
1744 static void
1745 issue_badaddr_msg(Ifl_desc *ifl, Ofl_desc *ofl, Sym_desc *sdp,
1746     Sym *sym, Word shndx)
1747 {
1748 	ofl_flag_t	flag;
1749 	Error		err;
1750 	const char	*msg;
1751 
1752 	if ((sdp->sd_isc->is_shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) ==
1753 	    SHF_ALLOC) {
1754 		msg = MSG_INTL(MSG_SYM_BADADDR_ROTXT);
1755 		flag = FLG_OF_WARN;
1756 		err = ERR_WARNING;
1757 	} else {
1758 		msg = MSG_INTL(MSG_SYM_BADADDR);
1759 		flag = FLG_OF_FATAL;
1760 		err = ERR_FATAL;
1761 	}
1762 
1763 	eprintf(ofl->ofl_lml, err, msg, demangle(sdp->sd_name),
1764 	    ifl->ifl_name, shndx, sdp->sd_isc->is_name,
1765 	    EC_XWORD(sdp->sd_isc->is_shdr->sh_size),
1766 	    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
1767 	ofl->ofl_flags |= flag;
1768 }
1769 
1770 
1771 /*
1772  * Process the symbol table for the specified input file.  At this point all
1773  * input sections from this input file have been assigned an input section
1774  * descriptor which is saved in the `ifl_isdesc' array.
1775  *
1776  *	o	local symbols are saved (as is) if the input file is a
1777  *		relocatable object
1778  *
1779  *	o	global symbols are added to the linkers internal symbol
1780  *		table if they are not already present, otherwise a symbol
1781  *		resolution function is called upon to resolve the conflict.
1782  */
1783 uintptr_t
1784 ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1785 {
1786 	/*
1787 	 * This macro tests the given symbol to see if it is out of
1788 	 * range relative to the section it references.
1789 	 *
1790 	 * entry:
1791 	 *	- ifl is a relative object (ET_REL)
1792 	 *	_sdp - Symbol descriptor
1793 	 *	_sym - Symbol
1794 	 *	_type - Symbol type
1795 	 *
1796 	 * The following are tested:
1797 	 *	- Symbol length is non-zero
1798 	 *	- Symbol type is a type that references code or data
1799 	 *	- Referenced section is not 0 (indicates an UNDEF symbol)
1800 	 *	  and is not in the range of special values above SHN_LORESERVE
1801 	 *	  (excluding SHN_XINDEX, which is OK).
1802 	 *	- We have a valid section header for the target section
1803 	 *
1804 	 * If the above are all true, and the symbol position is not
1805 	 * contained by the target section, this macro evaluates to
1806 	 * True (1). Otherwise, False(0).
1807 	 */
1808 #define	SYM_LOC_BADADDR(_sdp, _sym, _type) \
1809 	(_sym->st_size && dynsymsort_symtype[_type] && \
1810 	(_sym->st_shndx != SHN_UNDEF) && \
1811 	((_sym->st_shndx < SHN_LORESERVE) || \
1812 		(_sym->st_shndx == SHN_XINDEX)) && \
1813 	_sdp->sd_isc && _sdp->sd_isc->is_shdr && \
1814 	((_sym->st_value + _sym->st_size) > _sdp->sd_isc->is_shdr->sh_size))
1815 
1816 	Conv_inv_buf_t	inv_buf;
1817 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
1818 	Word		*symshndx = 0;
1819 	Shdr		*shdr = isc->is_shdr;
1820 	Sym_desc	*sdp;
1821 	size_t		strsize;
1822 	char		*strs;
1823 	uchar_t		type, bind;
1824 	Word		ndx, hash, local, total;
1825 	Half		etype = ifl->ifl_ehdr->e_type;
1826 	int		etype_rel;
1827 	const char	*symsecname, *strsecname;
1828 	avl_index_t	where;
1829 	int		test_gnu_hidden_bit;
1830 
1831 	/*
1832 	 * Its possible that a file may contain more that one symbol table,
1833 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
1834 	 * table (here, we assume .dynsym comes before .symtab).
1835 	 */
1836 	if (ifl->ifl_symscnt)
1837 		return (1);
1838 
1839 	if (isc->is_symshndx)
1840 		symshndx = isc->is_symshndx->is_indata->d_buf;
1841 
1842 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
1843 
1844 	if (isc->is_name)
1845 		symsecname = isc->is_name;
1846 	else
1847 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
1848 
1849 	/*
1850 	 * From the symbol tables section header information determine which
1851 	 * strtab table is needed to locate the actual symbol names.
1852 	 */
1853 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
1854 		ndx = shdr->sh_link;
1855 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
1856 			eprintf(ofl->ofl_lml, ERR_FATAL,
1857 			    MSG_INTL(MSG_FIL_INVSHLINK),
1858 			    ifl->ifl_name, symsecname, EC_XWORD(ndx));
1859 			return (S_ERROR);
1860 		}
1861 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
1862 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
1863 		if (ifl->ifl_isdesc[ndx]->is_name)
1864 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
1865 		else
1866 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
1867 	} else {
1868 		/*
1869 		 * There is no string table section in this input file
1870 		 * although there are symbols in this symbol table section.
1871 		 * This means that these symbols do not have names.
1872 		 * Currently, only scratch register symbols are allowed
1873 		 * not to have names.
1874 		 */
1875 		strsize = 0;
1876 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
1877 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
1878 	}
1879 
1880 	/*
1881 	 * Determine the number of local symbols together with the total
1882 	 * number we have to process.
1883 	 */
1884 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
1885 	local = shdr->sh_info;
1886 
1887 	/*
1888 	 * Allocate a symbol table index array and a local symbol array
1889 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
1890 	 * array).  If we are dealing with a relocatable object, allocate the
1891 	 * local symbol descriptors.  If this isn't a relocatable object we
1892 	 * still have to process any shared object locals to determine if any
1893 	 * register symbols exist.  Although these aren't added to the output
1894 	 * image, they are used as part of symbol resolution.
1895 	 */
1896 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
1897 	    sizeof (Sym_desc *)))) == 0)
1898 		return (S_ERROR);
1899 	etype_rel = (etype == ET_REL);
1900 	if (etype_rel && local) {
1901 		if ((ifl->ifl_locs =
1902 		    libld_calloc(sizeof (Sym_desc), local)) == 0)
1903 			return (S_ERROR);
1904 		/* LINTED */
1905 		ifl->ifl_locscnt = (Word)local;
1906 	}
1907 	ifl->ifl_symscnt = total;
1908 
1909 	/*
1910 	 * If there are local symbols to save add them to the symbol table
1911 	 * index array.
1912 	 */
1913 	if (local) {
1914 		int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1915 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
1916 			Word		shndx, sdflags = FLG_SY_CLEAN;
1917 			const char	*name;
1918 			Sym_desc	*rsdp;
1919 			int		shndx_bad = 0;
1920 
1921 			/*
1922 			 * Determine and validate the associated section index.
1923 			 */
1924 			if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
1925 				shndx = symshndx[ndx];
1926 			} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
1927 				sdflags |= FLG_SY_SPECSEC;
1928 			} else if (shndx > ifl->ifl_ehdr->e_shnum) {
1929 				/* We need the name before we can issue error */
1930 				shndx_bad = 1;
1931 			}
1932 
1933 			/*
1934 			 * Check if st_name has a valid value or not.
1935 			 */
1936 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
1937 			    shndx, symsecname, strsecname, &sdflags)) == 0) {
1938 				ofl->ofl_flags |= FLG_OF_FATAL;
1939 				continue;
1940 			}
1941 
1942 			/*
1943 			 * Now that we have the name, if the section index
1944 			 * was bad, report it.
1945 			 */
1946 			if (shndx_bad) {
1947 				eprintf(ofl->ofl_lml, ERR_WARNING,
1948 				    MSG_INTL(MSG_SYM_INVSHNDX),
1949 				    demangle_symname(name, isc->is_name, ndx),
1950 				    ifl->ifl_name,
1951 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
1952 				continue;
1953 			}
1954 
1955 			/*
1956 			 * If this local symbol table originates from a shared
1957 			 * object, then we're only interested in recording
1958 			 * register symbols.  As local symbol descriptors aren't
1959 			 * allocated for shared objects, one will be allocated
1960 			 * to associated with the register symbol.  This symbol
1961 			 * won't become part of the output image, but we must
1962 			 * process it to test for register conflicts.
1963 			 */
1964 			rsdp = sdp = 0;
1965 			if (sdflags & FLG_SY_REGSYM) {
1966 				/*
1967 				 * The presence of FLG_SY_REGSYM means that
1968 				 * the pointers in ld_targ.t_ms are non-NULL.
1969 				 */
1970 				rsdp = (*ld_targ.t_ms.ms_reg_find)(sym, ofl);
1971 				if (rsdp != 0) {
1972 					/*
1973 					 * The fact that another register def-
1974 					 * inition has been found is fatal.
1975 					 * Call the verification routine to get
1976 					 * the error message and move on.
1977 					 */
1978 					(void) (*ld_targ.t_ms.ms_reg_check)
1979 					    (rsdp, sym, name, ifl, ofl);
1980 					continue;
1981 				}
1982 
1983 				if (etype == ET_DYN) {
1984 					if ((sdp = libld_calloc(
1985 					    sizeof (Sym_desc), 1)) == 0)
1986 						return (S_ERROR);
1987 					sdp->sd_ref = REF_DYN_SEEN;
1988 				}
1989 			} else if (etype == ET_DYN)
1990 				continue;
1991 
1992 			/*
1993 			 * Fill in the remaining symbol descriptor information.
1994 			 */
1995 			if (sdp == 0) {
1996 				sdp = &(ifl->ifl_locs[ndx]);
1997 				sdp->sd_ref = REF_REL_NEED;
1998 			}
1999 			if (rsdp == 0) {
2000 				sdp->sd_name = name;
2001 				sdp->sd_sym = sym;
2002 				sdp->sd_shndx = shndx;
2003 				sdp->sd_flags = sdflags;
2004 				sdp->sd_file = ifl;
2005 				ifl->ifl_oldndx[ndx] = sdp;
2006 			}
2007 
2008 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
2009 
2010 			/*
2011 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
2012 			 * so as to simplify future processing.
2013 			 */
2014 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
2015 				sdp->sd_shndx = shndx = SHN_UNDEF;
2016 				sdp->sd_flags1 |=
2017 				    (FLG_SY1_IGNORE | FLG_SY1_ELIM);
2018 			}
2019 
2020 			/*
2021 			 * Process any register symbols.
2022 			 */
2023 			if (sdp->sd_flags & FLG_SY_REGSYM) {
2024 				/*
2025 				 * Add a diagnostic to indicate we've caught a
2026 				 * register symbol, as this can be useful if a
2027 				 * register conflict is later discovered.
2028 				 */
2029 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
2030 
2031 				/*
2032 				 * If this register symbol hasn't already been
2033 				 * recorded, enter it now.
2034 				 *
2035 				 * The presence of FLG_SY_REGSYM means that
2036 				 * the pointers in ld_targ.t_ms are non-NULL.
2037 				 */
2038 				if ((rsdp == 0) &&
2039 				    ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) ==
2040 				    0))
2041 					return (S_ERROR);
2042 			}
2043 
2044 			/*
2045 			 * Assign an input section.
2046 			 */
2047 			if ((sym->st_shndx != SHN_UNDEF) &&
2048 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
2049 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
2050 
2051 			/*
2052 			 * If this symbol falls within the range of a section
2053 			 * being discarded, then discard the symbol itself.
2054 			 * There is no reason to keep this local symbol.
2055 			 */
2056 			if (sdp->sd_isc &&
2057 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
2058 				sdp->sd_flags |= FLG_SY_ISDISC;
2059 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
2060 				continue;
2061 			}
2062 
2063 			/*
2064 			 * Skip any section symbols as new versions of these
2065 			 * will be created.
2066 			 */
2067 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
2068 				if (sym->st_shndx == SHN_UNDEF) {
2069 					eprintf(ofl->ofl_lml, ERR_WARNING,
2070 					    MSG_INTL(MSG_SYM_INVSHNDX),
2071 					    demangle_symname(name, isc->is_name,
2072 					    ndx), ifl->ifl_name,
2073 					    conv_sym_shndx(sym->st_shndx,
2074 					    &inv_buf));
2075 				}
2076 				continue;
2077 			}
2078 
2079 			/*
2080 			 * For a relocatable object, if this symbol is defined
2081 			 * and has non-zero length and references an address
2082 			 * within an associated section, then check its extents
2083 			 * to make sure the section boundaries encompass it.
2084 			 * If they don't, the ELF file is corrupt.
2085 			 */
2086 			if (etype_rel && SYM_LOC_BADADDR(sdp, sym, type)) {
2087 				issue_badaddr_msg(ifl, ofl, sdp, sym, shndx);
2088 				continue;
2089 			}
2090 
2091 			/*
2092 			 * Sanity check for TLS
2093 			 */
2094 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
2095 			    (sym->st_shndx != SHN_COMMON))) {
2096 				Is_desc	*isp = sdp->sd_isc;
2097 
2098 				if ((isp == 0) || (isp->is_shdr == 0) ||
2099 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
2100 					eprintf(ofl->ofl_lml, ERR_FATAL,
2101 					    MSG_INTL(MSG_SYM_TLS),
2102 					    demangle(sdp->sd_name),
2103 					    ifl->ifl_name);
2104 					ofl->ofl_flags |= FLG_OF_FATAL;
2105 					continue;
2106 				}
2107 			}
2108 
2109 			/*
2110 			 * Carry our some basic sanity checks (these are just
2111 			 * some of the erroneous symbol entries we've come
2112 			 * across, there's probably a lot more).  The symbol
2113 			 * will not be carried forward to the output file, which
2114 			 * won't be a problem unless a relocation is required
2115 			 * against it.
2116 			 */
2117 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
2118 			    ((sym->st_shndx == SHN_COMMON)) ||
2119 			    ((type == STT_FILE) &&
2120 			    (sym->st_shndx != SHN_ABS))) ||
2121 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == 0))) {
2122 				eprintf(ofl->ofl_lml, ERR_WARNING,
2123 				    MSG_INTL(MSG_SYM_INVSHNDX),
2124 				    demangle_symname(name, isc->is_name, ndx),
2125 				    ifl->ifl_name,
2126 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
2127 				sdp->sd_isc = NULL;
2128 				sdp->sd_flags |= FLG_SY_INVALID;
2129 				continue;
2130 			}
2131 
2132 			/*
2133 			 * As these local symbols will become part of the output
2134 			 * image, record their number and name string size.
2135 			 * Globals are counted after all input file processing
2136 			 * (and hence symbol resolution) is complete during
2137 			 * sym_validate().
2138 			 */
2139 			if (!(ofl->ofl_flags1 & FLG_OF1_REDLSYM)) {
2140 				ofl->ofl_locscnt++;
2141 
2142 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
2143 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
2144 				    sdp->sd_name) == -1))
2145 					return (S_ERROR);
2146 
2147 				if (allow_ldynsym && sym->st_name &&
2148 				    ldynsym_symtype[type]) {
2149 					ofl->ofl_dynlocscnt++;
2150 					if (st_insert(ofl->ofl_dynstrtab,
2151 					    sdp->sd_name) == -1)
2152 						return (S_ERROR);
2153 					/* Include it in sort section? */
2154 					DYNSORT_COUNT(sdp, sym, type, ++);
2155 				}
2156 			}
2157 		}
2158 	}
2159 
2160 	/*
2161 	 * The GNU ld interprets the top bit of the 16-bit Versym value
2162 	 * (0x8000) as the "hidden" bit. If this bit is set, the linker
2163 	 * is supposed to act as if that symbol does not exist. The Solaris
2164 	 * linker does not support this mechanism, or the model of interface
2165 	 * evolution that it allows, but we honor it in GNU ld produced
2166 	 * objects in order to interoperate with them.
2167 	 *
2168 	 * Determine if we should honor the GNU hidden bit for this file.
2169 	 */
2170 	test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) &&
2171 	    (ifl->ifl_versym != NULL);
2172 
2173 	/*
2174 	 * Now scan the global symbols entering them in the internal symbol
2175 	 * table or resolving them as necessary.
2176 	 */
2177 	sym = (Sym *)isc->is_indata->d_buf;
2178 	sym += local;
2179 	/* LINTED */
2180 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
2181 		const char	*name;
2182 		Word		shndx, sdflags = 0;
2183 		int		shndx_bad = 0;
2184 
2185 		/*
2186 		 * Determine and validate the associated section index.
2187 		 */
2188 		if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
2189 			shndx = symshndx[ndx];
2190 		} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
2191 			sdflags |= FLG_SY_SPECSEC;
2192 		} else if (shndx > ifl->ifl_ehdr->e_shnum) {
2193 			/* We need the name before we can issue error */
2194 			shndx_bad = 1;
2195 		}
2196 
2197 		/*
2198 		 * Check if st_name has a valid value or not.
2199 		 */
2200 		if ((name = string(ofl, ifl, sym, strs, strsize, ndx, shndx,
2201 		    symsecname, strsecname, &sdflags)) == 0) {
2202 			ofl->ofl_flags |= FLG_OF_FATAL;
2203 			continue;
2204 		}
2205 
2206 		/*
2207 		 * Now that we have the name, if the section index
2208 		 * was bad, report it.
2209 		 */
2210 		if (shndx_bad) {
2211 			eprintf(ofl->ofl_lml, ERR_WARNING,
2212 			    MSG_INTL(MSG_SYM_INVSHNDX),
2213 			    demangle_symname(name, isc->is_name, ndx),
2214 			    ifl->ifl_name,
2215 			    conv_sym_shndx(sym->st_shndx, &inv_buf));
2216 			continue;
2217 		}
2218 
2219 
2220 		/*
2221 		 * Test for the GNU hidden bit, and ignore symbols that
2222 		 * have it set.
2223 		 */
2224 		if (test_gnu_hidden_bit &&
2225 		    ((ifl->ifl_versym[ndx] & 0x8000) != 0))
2226 			continue;
2227 
2228 		/*
2229 		 * The linker itself will generate symbols for _end, _etext,
2230 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
2231 		 * bother entering these symbols from shared objects.  This
2232 		 * results in some wasted resolution processing, which is hard
2233 		 * to feel, but if nothing else, pollutes diagnostic relocation
2234 		 * output.
2235 		 */
2236 		if (name[0] && (etype == ET_DYN) && (sym->st_size == 0) &&
2237 		    (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) &&
2238 		    (name[0] == '_') && ((name[1] == 'e') ||
2239 		    (name[1] == 'D') || (name[1] == 'P')) &&
2240 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
2241 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
2242 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
2243 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
2244 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
2245 			ifl->ifl_oldndx[ndx] = 0;
2246 			continue;
2247 		}
2248 
2249 		/*
2250 		 * Determine and validate the symbols binding.
2251 		 */
2252 		bind = ELF_ST_BIND(sym->st_info);
2253 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
2254 			eprintf(ofl->ofl_lml, ERR_WARNING,
2255 			    MSG_INTL(MSG_SYM_NONGLOB),
2256 			    demangle_symname(name, isc->is_name, ndx),
2257 			    ifl->ifl_name,
2258 			    conv_sym_info_bind(bind, 0, &inv_buf));
2259 			continue;
2260 		}
2261 
2262 		/*
2263 		 * If this symbol falls within the range of a section being
2264 		 * discarded, then discard the symbol itself.
2265 		 */
2266 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
2267 		    (sym->st_shndx != SHN_UNDEF)) {
2268 			Is_desc	*isp;
2269 
2270 			if (shndx >= ifl->ifl_shnum) {
2271 				/*
2272 				 * Carry our some basic sanity checks
2273 				 * The symbol will not be carried forward to
2274 				 * the output file, which won't be a problem
2275 				 * unless a relocation is required against it.
2276 				 */
2277 				eprintf(ofl->ofl_lml, ERR_WARNING,
2278 				    MSG_INTL(MSG_SYM_INVSHNDX),
2279 				    demangle_symname(name, isc->is_name, ndx),
2280 				    ifl->ifl_name,
2281 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
2282 				continue;
2283 			}
2284 
2285 			isp = ifl->ifl_isdesc[shndx];
2286 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
2287 				if ((sdp =
2288 				    libld_calloc(sizeof (Sym_desc), 1)) == 0)
2289 					return (S_ERROR);
2290 
2291 				/*
2292 				 * Create a dummy symbol entry so that if we
2293 				 * find any references to this discarded symbol
2294 				 * we can compensate.
2295 				 */
2296 				sdp->sd_name = name;
2297 				sdp->sd_sym = sym;
2298 				sdp->sd_file = ifl;
2299 				sdp->sd_isc = isp;
2300 				sdp->sd_flags = FLG_SY_ISDISC;
2301 				ifl->ifl_oldndx[ndx] = sdp;
2302 
2303 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
2304 				continue;
2305 			}
2306 		}
2307 
2308 		/*
2309 		 * If the symbol does not already exist in the internal symbol
2310 		 * table add it, otherwise resolve the conflict.  If the symbol
2311 		 * from this file is kept, retain its symbol table index for
2312 		 * possible use in associating a global alias.
2313 		 */
2314 		/* LINTED */
2315 		hash = (Word)elf_hash((const char *)name);
2316 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
2317 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
2318 			if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, ndx,
2319 			    shndx, sdflags, 0, &where)) == (Sym_desc *)S_ERROR)
2320 				return (S_ERROR);
2321 
2322 		} else if (ld_sym_resolve(sdp, sym, ifl, ofl, ndx, shndx,
2323 		    sdflags) == S_ERROR)
2324 			return (S_ERROR);
2325 
2326 		/*
2327 		 * After we've compared a defined symbol in one shared
2328 		 * object, flag the symbol so we don't compare it again.
2329 		 */
2330 		if ((etype == ET_DYN) && (sym->st_shndx != SHN_UNDEF) &&
2331 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
2332 			sdp->sd_flags |= FLG_SY_SOFOUND;
2333 
2334 		/*
2335 		 * If the symbol is accepted from this file retain the symbol
2336 		 * index for possible use in aliasing.
2337 		 */
2338 		if (sdp->sd_file == ifl)
2339 			sdp->sd_symndx = ndx;
2340 
2341 		ifl->ifl_oldndx[ndx] = sdp;
2342 
2343 		/*
2344 		 * If we've accepted a register symbol, continue to validate
2345 		 * it.
2346 		 */
2347 		if (sdp->sd_flags & FLG_SY_REGSYM) {
2348 			Sym_desc	*rsdp;
2349 
2350 			/*
2351 			 * The presence of FLG_SY_REGSYM means that
2352 			 * the pointers in ld_targ.t_ms are non-NULL.
2353 			 */
2354 			rsdp = (*ld_targ.t_ms.ms_reg_find)(sdp->sd_sym, ofl);
2355 			if (rsdp == 0) {
2356 				if ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 0)
2357 					return (S_ERROR);
2358 			} else if (rsdp != sdp) {
2359 				(void) (*ld_targ.t_ms.ms_reg_check)(rsdp,
2360 				    sdp->sd_sym, sdp->sd_name, ifl, ofl);
2361 			}
2362 		}
2363 
2364 		/*
2365 		 * For a relocatable object, if this symbol is defined
2366 		 * and has non-zero length and references an address
2367 		 * within an associated section, then check its extents
2368 		 * to make sure the section boundaries encompass it.
2369 		 * If they don't, the ELF file is corrupt. Note that this
2370 		 * global symbol may have come from another file to satisfy
2371 		 * an UNDEF symbol of the same name from this one. In that
2372 		 * case, we don't check it, because it was already checked
2373 		 * as part of its own file.
2374 		 */
2375 		if (etype_rel && (sdp->sd_file == ifl)) {
2376 			Sym *tsym = sdp->sd_sym;
2377 
2378 			if (SYM_LOC_BADADDR(sdp, tsym,
2379 			    ELF_ST_TYPE(tsym->st_info))) {
2380 				issue_badaddr_msg(ifl, ofl, sdp,
2381 				    tsym, tsym->st_shndx);
2382 				continue;
2383 			}
2384 		}
2385 	}
2386 
2387 	/*
2388 	 * If this is a shared object scan the globals one more time and
2389 	 * associate any weak/global associations.  This association is needed
2390 	 * should the weak definition satisfy a reference in the dynamic
2391 	 * executable:
2392 	 *
2393 	 *  o	if the symbol is a data item it will be copied to the
2394 	 *	executables address space, thus we must also reassociate the
2395 	 *	alias symbol with its new location in the executable.
2396 	 *
2397 	 *  o	if the symbol is a function then we may need to promote	the
2398 	 *	symbols binding from undefined weak to undefined, otherwise the
2399 	 *	run-time linker will not generate the correct relocation error
2400 	 *	should the symbol not be found.
2401 	 *
2402 	 * The true association between a weak/strong symbol pair is that both
2403 	 * symbol entries are identical, thus first we created a sorted symbol
2404 	 * list keyed off of the symbols value (if the value is the same chances
2405 	 * are the rest of the symbols data is).  This list is then scanned for
2406 	 * weak symbols, and if one is found then any strong association will
2407 	 * exist in the following entries.  Thus we just have to scan one
2408 	 * (typical single alias) or more (in the uncommon instance of multiple
2409 	 * weak to strong associations) entries to determine if a match exists.
2410 	 */
2411 	if ((OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) &&
2412 	    (total > local)) {
2413 		Sym_desc **	sort;
2414 		size_t		size = (total - local) * sizeof (Sym_desc *);
2415 
2416 		if ((sort = libld_malloc(size)) == 0)
2417 			return (S_ERROR);
2418 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], size);
2419 
2420 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
2421 
2422 		for (ndx = 0; ndx < (total - local); ndx++) {
2423 			Sym_desc *	wsdp = sort[ndx];
2424 			Sym *		wsym;
2425 			int		sndx;
2426 
2427 			if (wsdp == 0)
2428 				continue;
2429 
2430 			wsym = wsdp->sd_sym;
2431 
2432 			if ((ELF_ST_BIND(wsym->st_info) != STB_WEAK) ||
2433 			    (wsdp->sd_sym->st_shndx == SHN_UNDEF) ||
2434 			    (wsdp->sd_flags & FLG_SY_SPECSEC))
2435 				continue;
2436 
2437 			/*
2438 			 * We have a weak symbol, if it has a strong alias it
2439 			 * will have been sorted to one of the following sort
2440 			 * table entries.  Note that we could have multiple weak
2441 			 * symbols aliased to one strong (if this occurs then
2442 			 * the strong symbol only maintains one alias back to
2443 			 * the last weak).
2444 			 */
2445 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
2446 				Sym_desc *	ssdp = sort[sndx];
2447 				Sym *		ssym;
2448 
2449 				if (ssdp == 0)
2450 					break;
2451 
2452 				ssym = ssdp->sd_sym;
2453 
2454 				if (wsym->st_value != ssym->st_value)
2455 					break;
2456 
2457 				if ((ssdp->sd_file == ifl) &&
2458 				    (wsdp->sd_file == ifl) &&
2459 				    (wsym->st_size == ssym->st_size) &&
2460 				    (ssdp->sd_sym->st_shndx != SHN_UNDEF) &&
2461 				    (ELF_ST_BIND(ssym->st_info) != STB_WEAK) &&
2462 				    ((ssdp->sd_flags & FLG_SY_SPECSEC) == 0)) {
2463 					int w_dynbits, s_dynbits;
2464 
2465 					/*
2466 					 * If a sharable object, set link
2467 					 * fields so they reference each other
2468 					 */
2469 					if (etype == ET_DYN) {
2470 						ssdp->sd_aux->sa_linkndx =
2471 						    (Word)wsdp->sd_symndx;
2472 						wsdp->sd_aux->sa_linkndx =
2473 						    (Word)ssdp->sd_symndx;
2474 					}
2475 					/*
2476 					 * Determine which of these two symbols
2477 					 * go into the sort section. If the
2478 					 * mapfile has made explicit settings
2479 					 * of the FLG_SY_*DYNSORT flags for both
2480 					 * symbols, then we do what they say.
2481 					 * If one has the DYNSORT flags set,
2482 					 * we set the NODYNSORT bit in the
2483 					 * other. And if neither has an
2484 					 * explicit setting, then we favor the
2485 					 * weak symbol because they usually
2486 					 * lack the leading underscore.
2487 					 */
2488 					w_dynbits = wsdp->sd_flags &
2489 					    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2490 					s_dynbits = ssdp->sd_flags &
2491 					    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2492 					if (!(w_dynbits && s_dynbits)) {
2493 						if (s_dynbits) {
2494 							if (s_dynbits ==
2495 							    FLG_SY_DYNSORT)
2496 							wsdp->sd_flags |=
2497 							    FLG_SY_NODYNSORT;
2498 						} else if (w_dynbits !=
2499 						    FLG_SY_NODYNSORT) {
2500 							ssdp->sd_flags |=
2501 							    FLG_SY_NODYNSORT;
2502 						}
2503 					}
2504 					break;
2505 				}
2506 			}
2507 		}
2508 	}
2509 	return (1);
2510 
2511 #undef SYM_LOC_BADADDR
2512 }
2513 
2514 /*
2515  * Add an undefined symbol to the symbol table.  The reference originates from
2516  * the location identifed by the message id (mid).  These references can
2517  * originate from command line options such as -e, -u, -initarray, etc.
2518  * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated
2519  * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)).
2520  */
2521 Sym_desc *
2522 ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid)
2523 {
2524 	Sym		*sym;
2525 	Ifl_desc	*ifl = 0, *_ifl;
2526 	Sym_desc	*sdp;
2527 	Word		hash;
2528 	Listnode	*lnp;
2529 	avl_index_t	where;
2530 	const char	*reference = MSG_INTL(mid);
2531 
2532 	/*
2533 	 * As an optimization, determine whether we've already generated this
2534 	 * reference.  If the symbol doesn't already exist we'll create it.
2535 	 * Or if the symbol does exist from a different source, we'll resolve
2536 	 * the conflict.
2537 	 */
2538 	/* LINTED */
2539 	hash = (Word)elf_hash(name);
2540 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) {
2541 		if ((sdp->sd_sym->st_shndx == SHN_UNDEF) &&
2542 		    (sdp->sd_file->ifl_name == reference))
2543 			return (sdp);
2544 	}
2545 
2546 	/*
2547 	 * Determine whether a pseudo input file descriptor exists to represent
2548 	 * the command line, as any global symbol needs an input file descriptor
2549 	 * during any symbol resolution (refer to map_ifl() which provides a
2550 	 * similar method for adding symbols from mapfiles).
2551 	 */
2552 	for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, _ifl))
2553 		if (strcmp(_ifl->ifl_name, reference) == 0) {
2554 			ifl = _ifl;
2555 			break;
2556 		}
2557 
2558 	/*
2559 	 * If no descriptor exists create one.
2560 	 */
2561 	if (ifl == 0) {
2562 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) ==
2563 		    (Ifl_desc *)0)
2564 			return ((Sym_desc *)S_ERROR);
2565 		ifl->ifl_name = reference;
2566 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
2567 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr),
2568 		    1)) == 0)
2569 			return ((Sym_desc *)S_ERROR);
2570 		ifl->ifl_ehdr->e_type = ET_REL;
2571 
2572 		if (list_appendc(&ofl->ofl_objs, ifl) == 0)
2573 			return ((Sym_desc *)S_ERROR);
2574 	}
2575 
2576 	/*
2577 	 * Allocate a symbol structure and add it to the global symbol table.
2578 	 */
2579 	if ((sym = libld_calloc(sizeof (Sym), 1)) == 0)
2580 		return ((Sym_desc *)S_ERROR);
2581 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
2582 	sym->st_shndx = SHN_UNDEF;
2583 
2584 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
2585 	if (sdp == NULL) {
2586 		DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
2587 		if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
2588 		    0, 0, &where)) == (Sym_desc *)S_ERROR)
2589 			return ((Sym_desc *)S_ERROR);
2590 	} else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0,
2591 	    SHN_UNDEF, 0) == S_ERROR)
2592 		return ((Sym_desc *)S_ERROR);
2593 
2594 	sdp->sd_flags &= ~FLG_SY_CLEAN;
2595 	sdp->sd_flags |= FLG_SY_CMDREF;
2596 
2597 	return (sdp);
2598 }
2599