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