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