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