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