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