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