xref: /titanic_41/usr/src/cmd/sgs/libld/common/syms.c (revision e4d060fb4c00d44cd578713eb9a921f594b733b8)
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 2010 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 		 * A symbol is a candidate for auto-reduction/elimination if:
963 		 *
964 		 *  -	the symbol wasn't explicitly defined within a mapfile
965 		 *	(in which case all the necessary state has been applied
966 		 *	to the symbol), or
967 		 *  -	the symbol isn't one of the family of reserved
968 		 *	special symbols (ie. _end, _etext, etc.), or
969 		 *  -	the symbol isn't a SINGLETON, or
970 		 *  -	the symbol wasn't explicitly defined within a version
971 		 *	definition associated with an input relocatable object.
972 		 *
973 		 * Indicate that the symbol has been reduced as it may be
974 		 * necessary to print these symbols later.
975 		 */
976 		if ((oflags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) &&
977 		    ((sdp->sd_flags & MSK_SY_NOAUTO) == 0)) {
978 			if ((sdp->sd_flags & FLG_SY_HIDDEN) == 0) {
979 				sdp->sd_flags |=
980 				    (FLG_SY_REDUCED | FLG_SY_HIDDEN);
981 			}
982 
983 			if (oflags & (FLG_OF_REDLSYM | FLG_OF_AUTOELM)) {
984 				sdp->sd_flags |= FLG_SY_ELIM;
985 				sym->st_other = STV_ELIMINATE |
986 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
987 			} else if (ELF_ST_VISIBILITY(sym->st_other) !=
988 			    STV_INTERNAL)
989 				sym->st_other = STV_HIDDEN |
990 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
991 		}
992 
993 		/*
994 		 * If -Bsymbolic is in effect, and the symbol hasn't explicitly
995 		 * been defined nodirect (via a mapfile), then bind the global
996 		 * symbol symbolically and assign the STV_PROTECTED visibility
997 		 * attribute.
998 		 */
999 		if ((oflags & FLG_OF_SYMBOLIC) &&
1000 		    ((sdp->sd_flags & (FLG_SY_HIDDEN | FLG_SY_NDIR)) == 0)) {
1001 			sdp->sd_flags |= FLG_SY_PROTECT;
1002 			if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT)
1003 				sym->st_other = STV_PROTECTED |
1004 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
1005 		}
1006 	}
1007 
1008 	/*
1009 	 * Indicate that this symbol has had it's visibility checked so that
1010 	 * we don't need to do this investigation again.
1011 	 */
1012 	sdp->sd_flags |= FLG_SY_VISIBLE;
1013 }
1014 
1015 /*
1016  * Make sure a symbol definition is local to the object being built.
1017  */
1018 inline static int
1019 ensure_sym_local(Ofl_desc *ofl, Sym_desc *sdp, const char *str)
1020 {
1021 	if (sdp->sd_sym->st_shndx == SHN_UNDEF) {
1022 		if (str) {
1023 			eprintf(ofl->ofl_lml, ERR_FATAL,
1024 			    MSG_INTL(MSG_SYM_UNDEF), str,
1025 			    demangle((char *)sdp->sd_name));
1026 		}
1027 		return (1);
1028 	}
1029 	if (sdp->sd_ref != REF_REL_NEED) {
1030 		if (str) {
1031 			eprintf(ofl->ofl_lml, ERR_FATAL,
1032 			    MSG_INTL(MSG_SYM_EXTERN), str,
1033 			    demangle((char *)sdp->sd_name),
1034 			    sdp->sd_file->ifl_name);
1035 		}
1036 		return (1);
1037 	}
1038 
1039 	sdp->sd_flags |= FLG_SY_UPREQD;
1040 	if (sdp->sd_isc) {
1041 		sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1042 		sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1043 	}
1044 	return (0);
1045 }
1046 
1047 /*
1048  * Make sure all the symbol definitions required for initarray, finiarray, or
1049  * preinitarray's are local to the object being built.
1050  */
1051 static int
1052 ensure_array_local(Ofl_desc *ofl, APlist *apl, const char *str)
1053 {
1054 	Aliste		idx;
1055 	Sym_desc	*sdp;
1056 	int		ret = 0;
1057 
1058 	for (APLIST_TRAVERSE(apl, idx, sdp))
1059 		ret += ensure_sym_local(ofl, sdp, str);
1060 
1061 	return (ret);
1062 }
1063 
1064 /*
1065  * After all symbol table input processing has been finished, and all relocation
1066  * counting has been carried out (ie. no more symbols will be read, generated,
1067  * or modified), validate and count the relevant entries:
1068  *
1069  *	-	check and print any undefined symbols remaining.  Note that
1070  *		if a symbol has been defined by virtue of the inclusion of
1071  *		an implicit shared library, it is still classed as undefined.
1072  *
1073  * 	-	count the number of global needed symbols together with the
1074  *		size of their associated name strings (if scoping has been
1075  *		indicated these symbols may be reduced to locals).
1076  *
1077  *	-	establish the size and alignment requirements for the global
1078  *		.bss section (the alignment of this section is based on the
1079  *		first symbol that it will contain).
1080  */
1081 uintptr_t
1082 ld_sym_validate(Ofl_desc *ofl)
1083 {
1084 	Sym_avlnode	*sav;
1085 	Sym_desc	*sdp;
1086 	Sym		*sym;
1087 	ofl_flag_t	oflags = ofl->ofl_flags;
1088 	ofl_flag_t	undef = 0, needed = 0, verdesc = 0;
1089 	Xword		bssalign = 0, tlsalign = 0;
1090 	Boolean		need_bss, need_tlsbss;
1091 	Xword		bsssize = 0, tlssize = 0;
1092 #if	defined(_ELF64)
1093 	Xword		lbssalign = 0, lbsssize = 0;
1094 	Boolean		need_lbss;
1095 #endif
1096 	int		ret;
1097 	int		allow_ldynsym;
1098 	uchar_t		type;
1099 
1100 	DBG_CALL(Dbg_basic_validate(ofl->ofl_lml));
1101 
1102 	/*
1103 	 * The need_XXX booleans are used to determine whether we need to
1104 	 * create each type of bss section. We used to create these sections
1105 	 * if the sum of the required sizes for each type were non-zero.
1106 	 * However, it is possible for a compiler to generate COMMON variables
1107 	 * of zero-length and this tricks that logic --- even zero-length
1108 	 * symbols need an output section.
1109 	 */
1110 	need_bss = need_tlsbss = FALSE;
1111 #if	defined(_ELF64)
1112 	need_lbss = FALSE;
1113 #endif
1114 
1115 	/*
1116 	 * If a symbol is undefined and this link-edit calls for no undefined
1117 	 * symbols to remain (this is the default case when generating an
1118 	 * executable but can be enforced for any object using -z defs), the
1119 	 * symbol is classified as undefined and a fatal error condition will
1120 	 * be indicated.
1121 	 *
1122 	 * If the symbol is undefined and we're creating a shared object with
1123 	 * the -Bsymbolic flag, then the symbol is also classified as undefined
1124 	 * and a warning condition will be indicated.
1125 	 */
1126 	if ((oflags & (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC)) ==
1127 	    (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC))
1128 		undef = FLG_OF_WARN;
1129 	if (oflags & FLG_OF_NOUNDEF)
1130 		undef = FLG_OF_FATAL;
1131 
1132 	/*
1133 	 * If the symbol is referenced from an implicitly included shared object
1134 	 * (ie. it's not on the NEEDED list) then the symbol is also classified
1135 	 * as undefined and a fatal error condition will be indicated.
1136 	 */
1137 	if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ))
1138 		needed = FLG_OF_FATAL;
1139 
1140 	/*
1141 	 * If the output image is being versioned, then all symbol definitions
1142 	 * must be associated with a version.  Any symbol that isn't associated
1143 	 * with a version is classified as undefined, and a fatal error
1144 	 * condition is indicated.
1145 	 */
1146 	if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL))
1147 		verdesc = FLG_OF_FATAL;
1148 
1149 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1150 
1151 	if (allow_ldynsym) {
1152 		/*
1153 		 * Normally, we disallow symbols with 0 size from appearing
1154 		 * in a dyn[sym|tls]sort section. However, there are some
1155 		 * symbols that serve special purposes that we want to exempt
1156 		 * from this rule. Look them up, and set their
1157 		 * FLG_SY_DYNSORT flag.
1158 		 */
1159 		static const char *special[] = {
1160 			MSG_ORIG(MSG_SYM_INIT_U),	/* _init */
1161 			MSG_ORIG(MSG_SYM_FINI_U),	/* _fini */
1162 			MSG_ORIG(MSG_SYM_START),	/* _start */
1163 			NULL
1164 		};
1165 		int i;
1166 
1167 		for (i = 0; special[i] != NULL; i++) {
1168 			if (((sdp = ld_sym_find(special[i],
1169 			    SYM_NOHASH, NULL, ofl)) != NULL) &&
1170 			    (sdp->sd_sym->st_size == 0)) {
1171 				if (ld_sym_copy(sdp) == S_ERROR)
1172 					return (S_ERROR);
1173 				sdp->sd_flags |= FLG_SY_DYNSORT;
1174 			}
1175 		}
1176 	}
1177 
1178 	/*
1179 	 * Collect and validate the globals from the internal symbol table.
1180 	 */
1181 	for (sav = avl_first(&ofl->ofl_symavl); sav;
1182 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
1183 		Is_desc		*isp;
1184 		int		undeferr = 0;
1185 		uchar_t		vis;
1186 
1187 		sdp = sav->sav_sdp;
1188 
1189 		/*
1190 		 * If undefined symbols are allowed ignore any symbols that are
1191 		 * not needed.
1192 		 */
1193 		if (!(oflags & FLG_OF_NOUNDEF) &&
1194 		    (sdp->sd_ref == REF_DYN_SEEN))
1195 			continue;
1196 
1197 		/*
1198 		 * If the symbol originates from an external or parent mapfile
1199 		 * reference and hasn't been matched to a reference from a
1200 		 * relocatable object, ignore it.
1201 		 */
1202 		if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) &&
1203 		    ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) {
1204 			sdp->sd_flags |= FLG_SY_INVALID;
1205 			continue;
1206 		}
1207 
1208 		sym = sdp->sd_sym;
1209 		type = ELF_ST_TYPE(sym->st_info);
1210 
1211 		/*
1212 		 * Sanity check TLS.
1213 		 */
1214 		if ((type == STT_TLS) && (sym->st_size != 0) &&
1215 		    (sym->st_shndx != SHN_UNDEF) &&
1216 		    (sym->st_shndx != SHN_COMMON)) {
1217 			Is_desc		*isp = sdp->sd_isc;
1218 			Ifl_desc	*ifl = sdp->sd_file;
1219 
1220 			if ((isp == NULL) || (isp->is_shdr == NULL) ||
1221 			    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
1222 				eprintf(ofl->ofl_lml, ERR_FATAL,
1223 				    MSG_INTL(MSG_SYM_TLS),
1224 				    demangle(sdp->sd_name), ifl->ifl_name);
1225 				ofl->ofl_flags |= FLG_OF_FATAL;
1226 				continue;
1227 			}
1228 		}
1229 
1230 		if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)
1231 			ld_sym_adjust_vis(sdp, ofl);
1232 
1233 		if ((sdp->sd_flags & FLG_SY_REDUCED) &&
1234 		    (oflags & FLG_OF_PROCRED)) {
1235 			DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL,
1236 			    sdp, 0, 0));
1237 		}
1238 
1239 		/*
1240 		 * Record any STV_SINGLETON existence.
1241 		 */
1242 		if ((vis = ELF_ST_VISIBILITY(sym->st_other)) == STV_SINGLETON)
1243 			ofl->ofl_dtflags_1 |= DF_1_SINGLETON;
1244 
1245 		/*
1246 		 * If building a shared object or executable, and this is a
1247 		 * non-weak UNDEF symbol with reduced visibility (STV_*), then
1248 		 * give a fatal error.
1249 		 */
1250 		if (((oflags & FLG_OF_RELOBJ) == 0) &&
1251 		    (sym->st_shndx == SHN_UNDEF) &&
1252 		    (ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
1253 			if (vis && (vis != STV_SINGLETON)) {
1254 				sym_undef_entry(ofl, sdp, BNDLOCAL);
1255 				ofl->ofl_flags |= FLG_OF_FATAL;
1256 				continue;
1257 			}
1258 		}
1259 
1260 		/*
1261 		 * If this symbol is defined in a non-allocatable section,
1262 		 * reduce it to local symbol.
1263 		 */
1264 		if (((isp = sdp->sd_isc) != 0) && isp->is_shdr &&
1265 		    ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) {
1266 			sdp->sd_flags |= (FLG_SY_REDUCED | FLG_SY_HIDDEN);
1267 		}
1268 
1269 		/*
1270 		 * If this symbol originated as a SHN_SUNW_IGNORE, it will have
1271 		 * been processed as an SHN_UNDEF.  Return the symbol to its
1272 		 * original index for validation, and propagation to the output
1273 		 * file.
1274 		 */
1275 		if (sdp->sd_flags & FLG_SY_IGNORE)
1276 			sdp->sd_shndx = SHN_SUNW_IGNORE;
1277 
1278 		if (undef) {
1279 			/*
1280 			 * If a non-weak reference remains undefined, or if a
1281 			 * mapfile reference is not bound to the relocatable
1282 			 * objects that make up the object being built, we have
1283 			 * a fatal error.
1284 			 *
1285 			 * The exceptions are symbols which are defined to be
1286 			 * found in the parent (FLG_SY_PARENT), which is really
1287 			 * only meaningful for direct binding, or are defined
1288 			 * external (FLG_SY_EXTERN) so as to suppress -zdefs
1289 			 * errors.
1290 			 *
1291 			 * Register symbols are always allowed to be UNDEF.
1292 			 *
1293 			 * Note that we don't include references created via -u
1294 			 * in the same shared object binding test.  This is for
1295 			 * backward compatibility, in that a number of archive
1296 			 * makefile rules used -u to cause archive extraction.
1297 			 * These same rules have been cut and pasted to apply
1298 			 * to shared objects, and thus although the -u reference
1299 			 * is redundant, flagging it as fatal could cause some
1300 			 * build to fail.  Also we have documented the use of
1301 			 * -u as a mechanism to cause binding to weak version
1302 			 * definitions, thus giving users an error condition
1303 			 * would be incorrect.
1304 			 */
1305 			if (!(sdp->sd_flags & FLG_SY_REGSYM) &&
1306 			    ((sym->st_shndx == SHN_UNDEF) &&
1307 			    ((ELF_ST_BIND(sym->st_info) != STB_WEAK) &&
1308 			    ((sdp->sd_flags &
1309 			    (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) ||
1310 			    ((sdp->sd_flags &
1311 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED | FLG_SY_HIDDEN |
1312 			    FLG_SY_PROTECT)) == FLG_SY_MAPREF))) {
1313 				sym_undef_entry(ofl, sdp, UNDEF);
1314 				ofl->ofl_flags |= undef;
1315 				undeferr = 1;
1316 			}
1317 
1318 		} else {
1319 			/*
1320 			 * For building things like shared objects (or anything
1321 			 * -znodefs), undefined symbols are allowed.
1322 			 *
1323 			 * If a mapfile reference remains undefined the user
1324 			 * would probably like a warning at least (they've
1325 			 * usually mis-spelt the reference).  Refer to the above
1326 			 * comments for discussion on -u references, which
1327 			 * are not tested for in the same manner.
1328 			 */
1329 			if ((sdp->sd_flags &
1330 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
1331 			    FLG_SY_MAPREF) {
1332 				sym_undef_entry(ofl, sdp, UNDEF);
1333 				ofl->ofl_flags |= FLG_OF_WARN;
1334 				undeferr = 1;
1335 			}
1336 		}
1337 
1338 		/*
1339 		 * If this symbol comes from a dependency mark the dependency
1340 		 * as required (-z ignore can result in unused dependencies
1341 		 * being dropped).  If we need to record dependency versioning
1342 		 * information indicate what version of the needed shared object
1343 		 * this symbol is part of.  Flag the symbol as undefined if it
1344 		 * has not been made available to us.
1345 		 */
1346 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1347 		    (!(sdp->sd_flags & FLG_SY_REFRSD))) {
1348 			sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD;
1349 
1350 			/*
1351 			 * Capture that we've bound to a symbol that doesn't
1352 			 * allow being directly bound to.
1353 			 */
1354 			if (sdp->sd_flags & FLG_SY_NDIR)
1355 				ofl->ofl_flags1 |= FLG_OF1_NGLBDIR;
1356 
1357 			if (sdp->sd_file->ifl_vercnt) {
1358 				int		vndx;
1359 				Ver_index	*vip;
1360 
1361 				vndx = sdp->sd_aux->sa_dverndx;
1362 				vip = &sdp->sd_file->ifl_verndx[vndx];
1363 				if (vip->vi_flags & FLG_VER_AVAIL) {
1364 					vip->vi_flags |= FLG_VER_REFER;
1365 				} else {
1366 					sym_undef_entry(ofl, sdp, NOTAVAIL);
1367 					ofl->ofl_flags |= FLG_OF_FATAL;
1368 					continue;
1369 				}
1370 			}
1371 		}
1372 
1373 		/*
1374 		 * Test that we do not bind to symbol supplied from an implicit
1375 		 * shared object.  If a binding is from a weak reference it can
1376 		 * be ignored.
1377 		 */
1378 		if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) &&
1379 		    (sdp->sd_ref == REF_DYN_NEED) &&
1380 		    (sdp->sd_flags & FLG_SY_NOTAVAIL)) {
1381 			sym_undef_entry(ofl, sdp, IMPLICIT);
1382 			ofl->ofl_flags |= needed;
1383 			continue;
1384 		}
1385 
1386 		/*
1387 		 * Test that a symbol isn't going to be reduced to local scope
1388 		 * which actually wants to bind to a shared object - if so it's
1389 		 * a fatal error.
1390 		 */
1391 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1392 		    (sdp->sd_flags & (FLG_SY_HIDDEN | FLG_SY_PROTECT))) {
1393 			sym_undef_entry(ofl, sdp, BNDLOCAL);
1394 			ofl->ofl_flags |= FLG_OF_FATAL;
1395 			continue;
1396 		}
1397 
1398 		/*
1399 		 * If the output image is to be versioned then all symbol
1400 		 * definitions must be associated with a version.  Remove any
1401 		 * versioning that might be left associated with an undefined
1402 		 * symbol.
1403 		 */
1404 		if (verdesc && (sdp->sd_ref == REF_REL_NEED)) {
1405 			if (sym->st_shndx == SHN_UNDEF) {
1406 				if (sdp->sd_aux && sdp->sd_aux->sa_overndx)
1407 					sdp->sd_aux->sa_overndx = 0;
1408 			} else {
1409 				if ((!(sdp->sd_flags & FLG_SY_HIDDEN)) &&
1410 				    sdp->sd_aux &&
1411 				    (sdp->sd_aux->sa_overndx == 0)) {
1412 					sym_undef_entry(ofl, sdp, NOVERSION);
1413 					ofl->ofl_flags |= verdesc;
1414 					continue;
1415 				}
1416 			}
1417 		}
1418 
1419 		/*
1420 		 * If we don't need the symbol there's no need to process it
1421 		 * any further.
1422 		 */
1423 		if (sdp->sd_ref == REF_DYN_SEEN)
1424 			continue;
1425 
1426 		/*
1427 		 * Calculate the size and alignment requirements for the global
1428 		 * .bss and .tls sections.  If we're building a relocatable
1429 		 * object only account for scoped COMMON symbols (these will
1430 		 * be converted to .bss references).
1431 		 *
1432 		 * When -z nopartial is in effect, partially initialized
1433 		 * symbols are directed to the special .data section
1434 		 * created for that purpose (ofl->ofl_isparexpn).
1435 		 * Otherwise, partially initialized symbols go to .bss.
1436 		 *
1437 		 * Also refer to make_mvsections() in sunwmove.c
1438 		 */
1439 		if ((sym->st_shndx == SHN_COMMON) &&
1440 		    (((oflags & FLG_OF_RELOBJ) == 0) ||
1441 		    ((sdp->sd_flags & FLG_SY_HIDDEN) &&
1442 		    (oflags & FLG_OF_PROCRED)))) {
1443 			if ((sdp->sd_move == NULL) ||
1444 			    ((sdp->sd_flags & FLG_SY_PAREXPN) == 0)) {
1445 				if (type != STT_TLS) {
1446 					need_bss = TRUE;
1447 					bsssize = (Xword)S_ROUND(bsssize,
1448 					    sym->st_value) + sym->st_size;
1449 					if (sym->st_value > bssalign)
1450 						bssalign = sym->st_value;
1451 				} else {
1452 					need_tlsbss = TRUE;
1453 					tlssize = (Xword)S_ROUND(tlssize,
1454 					    sym->st_value) + sym->st_size;
1455 					if (sym->st_value > tlsalign)
1456 						tlsalign = sym->st_value;
1457 				}
1458 			}
1459 		}
1460 
1461 #if	defined(_ELF64)
1462 		/*
1463 		 * Calculate the size and alignment requirement for the global
1464 		 * .lbss. TLS or partially initialized symbols do not need to be
1465 		 * considered yet.
1466 		 */
1467 		if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1468 		    (sym->st_shndx == SHN_X86_64_LCOMMON)) {
1469 			need_lbss = TRUE;
1470 			lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) +
1471 			    sym->st_size;
1472 			if (sym->st_value > lbssalign)
1473 				lbssalign = sym->st_value;
1474 		}
1475 #endif
1476 
1477 		/*
1478 		 * If a symbol was referenced via the command line
1479 		 * (ld -u <>, ...), then this counts as a reference against the
1480 		 * symbol. Mark any section that symbol is defined in.
1481 		 */
1482 		if (((isp = sdp->sd_isc) != 0) &&
1483 		    (sdp->sd_flags & FLG_SY_CMDREF)) {
1484 			isp->is_flags |= FLG_IS_SECTREF;
1485 			isp->is_file->ifl_flags |= FLG_IF_FILEREF;
1486 		}
1487 
1488 		/*
1489 		 * Update the symbol count and the associated name string size.
1490 		 */
1491 		if ((sdp->sd_flags & FLG_SY_HIDDEN) &&
1492 		    (oflags & FLG_OF_PROCRED)) {
1493 			/*
1494 			 * If any reductions are being processed, keep a count
1495 			 * of eliminated symbols, and if the symbol is being
1496 			 * reduced to local, count it's size for the .symtab.
1497 			 */
1498 			if (sdp->sd_flags & FLG_SY_ELIM) {
1499 				ofl->ofl_elimcnt++;
1500 			} else {
1501 				ofl->ofl_scopecnt++;
1502 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1503 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
1504 				    sdp->sd_name) == -1))
1505 					return (S_ERROR);
1506 				if (allow_ldynsym && sym->st_name &&
1507 				    ldynsym_symtype[type]) {
1508 					ofl->ofl_dynscopecnt++;
1509 					if (st_insert(ofl->ofl_dynstrtab,
1510 					    sdp->sd_name) == -1)
1511 						return (S_ERROR);
1512 					/* Include it in sort section? */
1513 					DYNSORT_COUNT(sdp, sym, type, ++);
1514 				}
1515 			}
1516 		} else {
1517 			ofl->ofl_globcnt++;
1518 
1519 			/*
1520 			 * Check to see if this global variable should go into
1521 			 * a sort section. Sort sections require a
1522 			 * .SUNW_ldynsym section, so, don't check unless a
1523 			 * .SUNW_ldynsym is allowed.
1524 			 */
1525 			if (allow_ldynsym)
1526 				DYNSORT_COUNT(sdp, sym, type, ++);
1527 
1528 			/*
1529 			 * If global direct bindings are in effect, or this
1530 			 * symbol has bound to a dependency which was specified
1531 			 * as requiring direct bindings, and it hasn't
1532 			 * explicitly been defined as a non-direct binding
1533 			 * symbol, mark it.
1534 			 */
1535 			if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp &&
1536 			    (isp->is_file->ifl_flags & FLG_IF_DIRECT))) &&
1537 			    ((sdp->sd_flags & FLG_SY_NDIR) == 0))
1538 				sdp->sd_flags |= FLG_SY_DIR;
1539 
1540 			/*
1541 			 * Insert the symbol name.
1542 			 */
1543 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1544 			    sym->st_name) {
1545 				if (st_insert(ofl->ofl_strtab,
1546 				    sdp->sd_name) == -1)
1547 					return (S_ERROR);
1548 
1549 				if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
1550 				    (st_insert(ofl->ofl_dynstrtab,
1551 				    sdp->sd_name) == -1))
1552 					return (S_ERROR);
1553 			}
1554 
1555 			/*
1556 			 * If this section offers a global symbol - record that
1557 			 * fact.
1558 			 */
1559 			if (isp) {
1560 				isp->is_flags |= FLG_IS_SECTREF;
1561 				isp->is_file->ifl_flags |= FLG_IF_FILEREF;
1562 			}
1563 		}
1564 	}
1565 
1566 	/*
1567 	 * If we've encountered a fatal error during symbol validation then
1568 	 * return now.
1569 	 */
1570 	if (ofl->ofl_flags & FLG_OF_FATAL)
1571 		return (1);
1572 
1573 	/*
1574 	 * Now that symbol resolution is completed, scan any register symbols.
1575 	 * From now on, we're only interested in those that contribute to the
1576 	 * output file.
1577 	 */
1578 	if (ofl->ofl_regsyms) {
1579 		int	ndx;
1580 
1581 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1582 			if ((sdp = ofl->ofl_regsyms[ndx]) == NULL)
1583 				continue;
1584 			if (sdp->sd_ref != REF_REL_NEED) {
1585 				ofl->ofl_regsyms[ndx] = NULL;
1586 				continue;
1587 			}
1588 
1589 			ofl->ofl_regsymcnt++;
1590 			if (sdp->sd_sym->st_name == 0)
1591 				sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY);
1592 
1593 			if ((sdp->sd_flags & FLG_SY_HIDDEN) ||
1594 			    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL))
1595 				ofl->ofl_lregsymcnt++;
1596 		}
1597 	}
1598 
1599 	/*
1600 	 * Generate the .bss section now that we know its size and alignment.
1601 	 */
1602 	if (need_bss) {
1603 		if (ld_make_bss(ofl, bsssize, bssalign,
1604 		    ld_targ.t_id.id_bss) == S_ERROR)
1605 			return (S_ERROR);
1606 	}
1607 	if (need_tlsbss) {
1608 		if (ld_make_bss(ofl, tlssize, tlsalign,
1609 		    ld_targ.t_id.id_tlsbss) == S_ERROR)
1610 			return (S_ERROR);
1611 	}
1612 #if	defined(_ELF64)
1613 	if ((ld_targ.t_m.m_mach == EM_AMD64) &&
1614 	    need_lbss && !(oflags & FLG_OF_RELOBJ)) {
1615 		if (ld_make_bss(ofl, lbsssize, lbssalign,
1616 		    ld_targ.t_id.id_lbss) == S_ERROR)
1617 			return (S_ERROR);
1618 	}
1619 #endif
1620 	/*
1621 	 * Determine what entry point symbol we need, and if found save its
1622 	 * symbol descriptor so that we can update the ELF header entry with the
1623 	 * symbols value later (see update_oehdr).  Make sure the symbol is
1624 	 * tagged to ensure its update in case -s is in effect.  Use any -e
1625 	 * option first, or the default entry points `_start' and `main'.
1626 	 */
1627 	ret = 0;
1628 	if (ofl->ofl_entry) {
1629 		if ((sdp = ld_sym_find(ofl->ofl_entry, SYM_NOHASH,
1630 		    NULL, ofl)) == NULL) {
1631 			eprintf(ofl->ofl_lml, ERR_FATAL,
1632 			    MSG_INTL(MSG_ARG_NOENTRY), ofl->ofl_entry);
1633 			ret++;
1634 		} else if (ensure_sym_local(ofl, sdp,
1635 		    MSG_INTL(MSG_SYM_ENTRY)) != 0) {
1636 			ret++;
1637 		} else {
1638 			ofl->ofl_entry = (void *)sdp;
1639 		}
1640 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START),
1641 	    SYM_NOHASH, NULL, ofl)) != NULL) && (ensure_sym_local(ofl,
1642 	    sdp, 0) == 0)) {
1643 		ofl->ofl_entry = (void *)sdp;
1644 
1645 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN),
1646 	    SYM_NOHASH, NULL, ofl)) != NULL) && (ensure_sym_local(ofl,
1647 	    sdp, 0) == 0)) {
1648 		ofl->ofl_entry = (void *)sdp;
1649 	}
1650 
1651 	/*
1652 	 * If ld -zdtrace=<sym> was given, then validate that the symbol is
1653 	 * defined within the current object being built.
1654 	 */
1655 	if ((sdp = ofl->ofl_dtracesym) != 0)
1656 		ret += ensure_sym_local(ofl, sdp, MSG_ORIG(MSG_STR_DTRACE));
1657 
1658 	/*
1659 	 * If any initarray, finiarray or preinitarray functions have been
1660 	 * requested, make sure they are defined within the current object
1661 	 * being built.
1662 	 */
1663 	if (ofl->ofl_initarray) {
1664 		ret += ensure_array_local(ofl, ofl->ofl_initarray,
1665 		    MSG_ORIG(MSG_SYM_INITARRAY));
1666 	}
1667 	if (ofl->ofl_finiarray) {
1668 		ret += ensure_array_local(ofl, ofl->ofl_finiarray,
1669 		    MSG_ORIG(MSG_SYM_FINIARRAY));
1670 	}
1671 	if (ofl->ofl_preiarray) {
1672 		ret += ensure_array_local(ofl, ofl->ofl_preiarray,
1673 		    MSG_ORIG(MSG_SYM_PREINITARRAY));
1674 	}
1675 
1676 	if (ret)
1677 		return (S_ERROR);
1678 
1679 	/*
1680 	 * If we're required to record any needed dependencies versioning
1681 	 * information calculate it now that all symbols have been validated.
1682 	 */
1683 	if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
1684 		return (ld_vers_check_need(ofl));
1685 	else
1686 		return (1);
1687 }
1688 
1689 /*
1690  * qsort(3c) comparison function.  As an optimization for associating weak
1691  * symbols to their strong counterparts sort global symbols according to their
1692  * section index, address and binding.
1693  */
1694 static int
1695 compare(const void *sdpp1, const void *sdpp2)
1696 {
1697 	Sym_desc	*sdp1 = *((Sym_desc **)sdpp1);
1698 	Sym_desc	*sdp2 = *((Sym_desc **)sdpp2);
1699 	Sym		*sym1, *sym2;
1700 	uchar_t		bind1, bind2;
1701 
1702 	/*
1703 	 * Symbol descriptors may be zero, move these to the front of the
1704 	 * sorted array.
1705 	 */
1706 	if (sdp1 == NULL)
1707 		return (-1);
1708 	if (sdp2 == NULL)
1709 		return (1);
1710 
1711 	sym1 = sdp1->sd_sym;
1712 	sym2 = sdp2->sd_sym;
1713 
1714 	/*
1715 	 * Compare the symbols section index.  This is important when sorting
1716 	 * the symbol tables of relocatable objects.  In this case, a symbols
1717 	 * value is the offset within the associated section, and thus many
1718 	 * symbols can have the same value, but are effectively different
1719 	 * addresses.
1720 	 */
1721 	if (sym1->st_shndx > sym2->st_shndx)
1722 		return (1);
1723 	if (sym1->st_shndx < sym2->st_shndx)
1724 		return (-1);
1725 
1726 	/*
1727 	 * Compare the symbols value (address).
1728 	 */
1729 	if (sym1->st_value > sym2->st_value)
1730 		return (1);
1731 	if (sym1->st_value < sym2->st_value)
1732 		return (-1);
1733 
1734 	bind1 = ELF_ST_BIND(sym1->st_info);
1735 	bind2 = ELF_ST_BIND(sym2->st_info);
1736 
1737 	/*
1738 	 * If two symbols have the same address place the weak symbol before
1739 	 * any strong counterpart.
1740 	 */
1741 	if (bind1 > bind2)
1742 		return (-1);
1743 	if (bind1 < bind2)
1744 		return (1);
1745 
1746 	return (0);
1747 }
1748 
1749 /*
1750  * Issue a MSG_SYM_BADADDR error from ld_sym_process(). This error
1751  * is issued when a symbol address/size is not contained by the
1752  * target section.
1753  *
1754  * Such objects are at least partially corrupt, and the user would
1755  * be well advised to be skeptical of them, and to ask their compiler
1756  * supplier to fix the problem. However, a distinction needs to be
1757  * made between symbols that reference readonly text, and those that
1758  * access writable data. Other than throwing off profiling results,
1759  * the readonly section case is less serious. We have encountered
1760  * such objects in the field. In order to allow existing objects
1761  * to continue working, we issue a warning rather than a fatal error
1762  * if the symbol is against readonly text. Other cases are fatal.
1763  */
1764 static void
1765 issue_badaddr_msg(Ifl_desc *ifl, Ofl_desc *ofl, Sym_desc *sdp,
1766     Sym *sym, Word shndx)
1767 {
1768 	ofl_flag_t	flag;
1769 	Error		err;
1770 	const char	*msg;
1771 
1772 	if ((sdp->sd_isc->is_shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) ==
1773 	    SHF_ALLOC) {
1774 		msg = MSG_INTL(MSG_SYM_BADADDR_ROTXT);
1775 		flag = FLG_OF_WARN;
1776 		err = ERR_WARNING;
1777 	} else {
1778 		msg = MSG_INTL(MSG_SYM_BADADDR);
1779 		flag = FLG_OF_FATAL;
1780 		err = ERR_FATAL;
1781 	}
1782 
1783 	eprintf(ofl->ofl_lml, err, msg, demangle(sdp->sd_name),
1784 	    ifl->ifl_name, shndx, sdp->sd_isc->is_name,
1785 	    EC_XWORD(sdp->sd_isc->is_shdr->sh_size),
1786 	    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
1787 	ofl->ofl_flags |= flag;
1788 }
1789 
1790 
1791 /*
1792  * Process the symbol table for the specified input file.  At this point all
1793  * input sections from this input file have been assigned an input section
1794  * descriptor which is saved in the `ifl_isdesc' array.
1795  *
1796  *	-	local symbols are saved (as is) if the input file is a
1797  *		relocatable object
1798  *
1799  *	-	global symbols are added to the linkers internal symbol
1800  *		table if they are not already present, otherwise a symbol
1801  *		resolution function is called upon to resolve the conflict.
1802  */
1803 uintptr_t
1804 ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1805 {
1806 	/*
1807 	 * This macro tests the given symbol to see if it is out of
1808 	 * range relative to the section it references.
1809 	 *
1810 	 * entry:
1811 	 *	- ifl is a relative object (ET_REL)
1812 	 *	_sdp - Symbol descriptor
1813 	 *	_sym - Symbol
1814 	 *	_type - Symbol type
1815 	 *
1816 	 * The following are tested:
1817 	 *	- Symbol length is non-zero
1818 	 *	- Symbol type is a type that references code or data
1819 	 *	- Referenced section is not 0 (indicates an UNDEF symbol)
1820 	 *	  and is not in the range of special values above SHN_LORESERVE
1821 	 *	  (excluding SHN_XINDEX, which is OK).
1822 	 *	- We have a valid section header for the target section
1823 	 *
1824 	 * If the above are all true, and the symbol position is not
1825 	 * contained by the target section, this macro evaluates to
1826 	 * True (1). Otherwise, False(0).
1827 	 */
1828 #define	SYM_LOC_BADADDR(_sdp, _sym, _type) \
1829 	(_sym->st_size && dynsymsort_symtype[_type] && \
1830 	(_sym->st_shndx != SHN_UNDEF) && \
1831 	((_sym->st_shndx < SHN_LORESERVE) || \
1832 		(_sym->st_shndx == SHN_XINDEX)) && \
1833 	_sdp->sd_isc && _sdp->sd_isc->is_shdr && \
1834 	((_sym->st_value + _sym->st_size) > _sdp->sd_isc->is_shdr->sh_size))
1835 
1836 	Conv_inv_buf_t	inv_buf;
1837 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
1838 	Word		*symshndx = NULL;
1839 	Shdr		*shdr = isc->is_shdr;
1840 	Sym_desc	*sdp;
1841 	size_t		strsize;
1842 	char		*strs;
1843 	uchar_t		type, bind;
1844 	Word		ndx, hash, local, total;
1845 	uchar_t		osabi = ifl->ifl_ehdr->e_ident[EI_OSABI];
1846 	Half		mach = ifl->ifl_ehdr->e_machine;
1847 	Half		etype = ifl->ifl_ehdr->e_type;
1848 	int		etype_rel;
1849 	const char	*symsecname, *strsecname;
1850 	Word		symsecndx;
1851 	avl_index_t	where;
1852 	int		test_gnu_hidden_bit, weak;
1853 
1854 	/*
1855 	 * Its possible that a file may contain more that one symbol table,
1856 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
1857 	 * table (here, we assume .dynsym comes before .symtab).
1858 	 */
1859 	if (ifl->ifl_symscnt)
1860 		return (1);
1861 
1862 	if (isc->is_symshndx)
1863 		symshndx = isc->is_symshndx->is_indata->d_buf;
1864 
1865 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
1866 
1867 	symsecndx = isc->is_scnndx;
1868 	if (isc->is_name)
1869 		symsecname = isc->is_name;
1870 	else
1871 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
1872 
1873 	/*
1874 	 * From the symbol tables section header information determine which
1875 	 * strtab table is needed to locate the actual symbol names.
1876 	 */
1877 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
1878 		ndx = shdr->sh_link;
1879 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
1880 			eprintf(ofl->ofl_lml, ERR_FATAL,
1881 			    MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
1882 			    EC_WORD(symsecndx), symsecname, EC_XWORD(ndx));
1883 			return (S_ERROR);
1884 		}
1885 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
1886 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
1887 		if (ifl->ifl_isdesc[ndx]->is_name)
1888 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
1889 		else
1890 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
1891 	} else {
1892 		/*
1893 		 * There is no string table section in this input file
1894 		 * although there are symbols in this symbol table section.
1895 		 * This means that these symbols do not have names.
1896 		 * Currently, only scratch register symbols are allowed
1897 		 * not to have names.
1898 		 */
1899 		strsize = 0;
1900 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
1901 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
1902 	}
1903 
1904 	/*
1905 	 * Determine the number of local symbols together with the total
1906 	 * number we have to process.
1907 	 */
1908 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
1909 	local = shdr->sh_info;
1910 
1911 	/*
1912 	 * Allocate a symbol table index array and a local symbol array
1913 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
1914 	 * array).  If we are dealing with a relocatable object, allocate the
1915 	 * local symbol descriptors.  If this isn't a relocatable object we
1916 	 * still have to process any shared object locals to determine if any
1917 	 * register symbols exist.  Although these aren't added to the output
1918 	 * image, they are used as part of symbol resolution.
1919 	 */
1920 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
1921 	    sizeof (Sym_desc *)))) == NULL)
1922 		return (S_ERROR);
1923 	etype_rel = (etype == ET_REL);
1924 	if (etype_rel && local) {
1925 		if ((ifl->ifl_locs =
1926 		    libld_calloc(sizeof (Sym_desc), local)) == NULL)
1927 			return (S_ERROR);
1928 		/* LINTED */
1929 		ifl->ifl_locscnt = (Word)local;
1930 	}
1931 	ifl->ifl_symscnt = total;
1932 
1933 	/*
1934 	 * If there are local symbols to save add them to the symbol table
1935 	 * index array.
1936 	 */
1937 	if (local) {
1938 		int		allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1939 		Sym_desc	*last_file_sdp = NULL;
1940 		int		last_file_ndx = 0;
1941 
1942 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
1943 			sd_flag_t	sdflags = FLG_SY_CLEAN;
1944 			Word		shndx;
1945 			const char	*name;
1946 			Sym_desc	*rsdp;
1947 			int		shndx_bad = 0;
1948 			int		symtab_enter = 1;
1949 
1950 			/*
1951 			 * Determine and validate the associated section index.
1952 			 */
1953 			if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
1954 				shndx = symshndx[ndx];
1955 			} else if ((shndx = sym->st_shndx) >= SHN_LORESERVE) {
1956 				sdflags |= FLG_SY_SPECSEC;
1957 			} else if (shndx > ifl->ifl_ehdr->e_shnum) {
1958 				/* We need the name before we can issue error */
1959 				shndx_bad = 1;
1960 			}
1961 
1962 			/*
1963 			 * Check if st_name has a valid value or not.
1964 			 */
1965 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
1966 			    shndx, symsecndx, symsecname, strsecname,
1967 			    &sdflags)) == NULL) {
1968 				ofl->ofl_flags |= FLG_OF_FATAL;
1969 				continue;
1970 			}
1971 
1972 			/*
1973 			 * Now that we have the name, if the section index
1974 			 * was bad, report it.
1975 			 */
1976 			if (shndx_bad) {
1977 				eprintf(ofl->ofl_lml, ERR_WARNING,
1978 				    MSG_INTL(MSG_SYM_INVSHNDX),
1979 				    demangle_symname(name, symsecname, ndx),
1980 				    ifl->ifl_name,
1981 				    conv_sym_shndx(osabi, mach, sym->st_shndx,
1982 				    CONV_FMT_DECIMAL, &inv_buf));
1983 				continue;
1984 			}
1985 
1986 			/*
1987 			 * If this local symbol table originates from a shared
1988 			 * object, then we're only interested in recording
1989 			 * register symbols.  As local symbol descriptors aren't
1990 			 * allocated for shared objects, one will be allocated
1991 			 * to associated with the register symbol.  This symbol
1992 			 * won't become part of the output image, but we must
1993 			 * process it to test for register conflicts.
1994 			 */
1995 			rsdp = sdp = NULL;
1996 			if (sdflags & FLG_SY_REGSYM) {
1997 				/*
1998 				 * The presence of FLG_SY_REGSYM means that
1999 				 * the pointers in ld_targ.t_ms are non-NULL.
2000 				 */
2001 				rsdp = (*ld_targ.t_ms.ms_reg_find)(sym, ofl);
2002 				if (rsdp != 0) {
2003 					/*
2004 					 * The fact that another register def-
2005 					 * inition has been found is fatal.
2006 					 * Call the verification routine to get
2007 					 * the error message and move on.
2008 					 */
2009 					(void) (*ld_targ.t_ms.ms_reg_check)
2010 					    (rsdp, sym, name, ifl, ofl);
2011 					continue;
2012 				}
2013 
2014 				if (etype == ET_DYN) {
2015 					if ((sdp = libld_calloc(
2016 					    sizeof (Sym_desc), 1)) == NULL)
2017 						return (S_ERROR);
2018 					sdp->sd_ref = REF_DYN_SEEN;
2019 
2020 					/* Will not appear in output object */
2021 					symtab_enter = 0;
2022 				}
2023 			} else if (etype == ET_DYN)
2024 				continue;
2025 
2026 			/*
2027 			 * Fill in the remaining symbol descriptor information.
2028 			 */
2029 			if (sdp == NULL) {
2030 				sdp = &(ifl->ifl_locs[ndx]);
2031 				sdp->sd_ref = REF_REL_NEED;
2032 				sdp->sd_symndx = ndx;
2033 			}
2034 			if (rsdp == NULL) {
2035 				sdp->sd_name = name;
2036 				sdp->sd_sym = sym;
2037 				sdp->sd_shndx = shndx;
2038 				sdp->sd_flags = sdflags;
2039 				sdp->sd_file = ifl;
2040 				ifl->ifl_oldndx[ndx] = sdp;
2041 			}
2042 
2043 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
2044 
2045 			/*
2046 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
2047 			 * so as to simplify future processing.
2048 			 */
2049 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
2050 				sdp->sd_shndx = shndx = SHN_UNDEF;
2051 				sdp->sd_flags |= (FLG_SY_IGNORE | FLG_SY_ELIM);
2052 			}
2053 
2054 			/*
2055 			 * Process any register symbols.
2056 			 */
2057 			if (sdp->sd_flags & FLG_SY_REGSYM) {
2058 				/*
2059 				 * Add a diagnostic to indicate we've caught a
2060 				 * register symbol, as this can be useful if a
2061 				 * register conflict is later discovered.
2062 				 */
2063 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
2064 
2065 				/*
2066 				 * If this register symbol hasn't already been
2067 				 * recorded, enter it now.
2068 				 *
2069 				 * The presence of FLG_SY_REGSYM means that
2070 				 * the pointers in ld_targ.t_ms are non-NULL.
2071 				 */
2072 				if ((rsdp == NULL) &&
2073 				    ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) ==
2074 				    0))
2075 					return (S_ERROR);
2076 			}
2077 
2078 			/*
2079 			 * Assign an input section.
2080 			 */
2081 			if ((sym->st_shndx != SHN_UNDEF) &&
2082 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
2083 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
2084 
2085 			/*
2086 			 * If this symbol falls within the range of a section
2087 			 * being discarded, then discard the symbol itself.
2088 			 * There is no reason to keep this local symbol.
2089 			 */
2090 			if (sdp->sd_isc &&
2091 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
2092 				sdp->sd_flags |= FLG_SY_ISDISC;
2093 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
2094 				continue;
2095 			}
2096 
2097 			/*
2098 			 * Skip any section symbols as new versions of these
2099 			 * will be created.
2100 			 */
2101 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
2102 				if (sym->st_shndx == SHN_UNDEF) {
2103 					eprintf(ofl->ofl_lml, ERR_WARNING,
2104 					    MSG_INTL(MSG_SYM_INVSHNDX),
2105 					    demangle_symname(name, symsecname,
2106 					    ndx), ifl->ifl_name,
2107 					    conv_sym_shndx(osabi, mach,
2108 					    sym->st_shndx, CONV_FMT_DECIMAL,
2109 					    &inv_buf));
2110 				}
2111 				continue;
2112 			}
2113 
2114 			/*
2115 			 * For a relocatable object, if this symbol is defined
2116 			 * and has non-zero length and references an address
2117 			 * within an associated section, then check its extents
2118 			 * to make sure the section boundaries encompass it.
2119 			 * If they don't, the ELF file is corrupt.
2120 			 */
2121 			if (etype_rel) {
2122 				if (SYM_LOC_BADADDR(sdp, sym, type)) {
2123 					issue_badaddr_msg(ifl, ofl, sdp,
2124 					    sym, shndx);
2125 					if (ofl->ofl_flags & FLG_OF_FATAL)
2126 						continue;
2127 				}
2128 
2129 				/*
2130 				 * We have observed relocatable objects
2131 				 * containing identical adjacent STT_FILE
2132 				 * symbols. Discard any other than the first,
2133 				 * as they are all equivalent and the extras
2134 				 * do not add information.
2135 				 *
2136 				 * For the purpose of this test, we assume
2137 				 * that only the symbol type and the string
2138 				 * table offset (st_name) matter.
2139 				 */
2140 				if (type == STT_FILE) {
2141 					int toss = (last_file_sdp != NULL) &&
2142 					    ((ndx - 1) == last_file_ndx) &&
2143 					    (sym->st_name ==
2144 					    last_file_sdp->sd_sym->st_name);
2145 
2146 					last_file_sdp = sdp;
2147 					last_file_ndx = ndx;
2148 					if (toss) {
2149 						sdp->sd_flags |= FLG_SY_INVALID;
2150 						DBG_CALL(Dbg_syms_dup_discarded(
2151 						    ofl->ofl_lml, ndx, sdp));
2152 						continue;
2153 					}
2154 				}
2155 			}
2156 
2157 
2158 			/*
2159 			 * Sanity check for TLS
2160 			 */
2161 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
2162 			    (sym->st_shndx != SHN_COMMON))) {
2163 				Is_desc	*isp = sdp->sd_isc;
2164 
2165 				if ((isp == NULL) || (isp->is_shdr == NULL) ||
2166 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
2167 					eprintf(ofl->ofl_lml, ERR_FATAL,
2168 					    MSG_INTL(MSG_SYM_TLS),
2169 					    demangle(sdp->sd_name),
2170 					    ifl->ifl_name);
2171 					ofl->ofl_flags |= FLG_OF_FATAL;
2172 					continue;
2173 				}
2174 			}
2175 
2176 			/*
2177 			 * Carry our some basic sanity checks (these are just
2178 			 * some of the erroneous symbol entries we've come
2179 			 * across, there's probably a lot more).  The symbol
2180 			 * will not be carried forward to the output file, which
2181 			 * won't be a problem unless a relocation is required
2182 			 * against it.
2183 			 */
2184 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
2185 			    ((sym->st_shndx == SHN_COMMON)) ||
2186 			    ((type == STT_FILE) &&
2187 			    (sym->st_shndx != SHN_ABS))) ||
2188 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == NULL))) {
2189 				eprintf(ofl->ofl_lml, ERR_WARNING,
2190 				    MSG_INTL(MSG_SYM_INVSHNDX),
2191 				    demangle_symname(name, symsecname, ndx),
2192 				    ifl->ifl_name,
2193 				    conv_sym_shndx(osabi, mach, sym->st_shndx,
2194 				    CONV_FMT_DECIMAL, &inv_buf));
2195 				sdp->sd_isc = NULL;
2196 				sdp->sd_flags |= FLG_SY_INVALID;
2197 				continue;
2198 			}
2199 
2200 			/*
2201 			 * As these local symbols will become part of the output
2202 			 * image, record their number and name string size.
2203 			 * Globals are counted after all input file processing
2204 			 * (and hence symbol resolution) is complete during
2205 			 * sym_validate().
2206 			 */
2207 			if (!(ofl->ofl_flags & FLG_OF_REDLSYM) &&
2208 			    symtab_enter) {
2209 				ofl->ofl_locscnt++;
2210 
2211 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
2212 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
2213 				    sdp->sd_name) == -1))
2214 					return (S_ERROR);
2215 
2216 				if (allow_ldynsym && sym->st_name &&
2217 				    ldynsym_symtype[type]) {
2218 					ofl->ofl_dynlocscnt++;
2219 					if (st_insert(ofl->ofl_dynstrtab,
2220 					    sdp->sd_name) == -1)
2221 						return (S_ERROR);
2222 					/* Include it in sort section? */
2223 					DYNSORT_COUNT(sdp, sym, type, ++);
2224 				}
2225 			}
2226 		}
2227 	}
2228 
2229 	/*
2230 	 * The GNU ld interprets the top bit of the 16-bit Versym value
2231 	 * (0x8000) as the "hidden" bit. If this bit is set, the linker
2232 	 * is supposed to act as if that symbol does not exist. The Solaris
2233 	 * linker does not support this mechanism, or the model of interface
2234 	 * evolution that it allows, but we honor it in GNU ld produced
2235 	 * objects in order to interoperate with them.
2236 	 *
2237 	 * Determine if we should honor the GNU hidden bit for this file.
2238 	 */
2239 	test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) &&
2240 	    (ifl->ifl_versym != NULL);
2241 
2242 	/*
2243 	 * Now scan the global symbols entering them in the internal symbol
2244 	 * table or resolving them as necessary.
2245 	 */
2246 	sym = (Sym *)isc->is_indata->d_buf;
2247 	sym += local;
2248 	weak = 0;
2249 	/* LINTED */
2250 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
2251 		const char	*name;
2252 		sd_flag_t	sdflags = 0;
2253 		Word		shndx;
2254 		int		shndx_bad = 0;
2255 		Sym		*nsym = sym;
2256 
2257 		/*
2258 		 * Determine and validate the associated section index.
2259 		 */
2260 		if (symshndx && (nsym->st_shndx == SHN_XINDEX)) {
2261 			shndx = symshndx[ndx];
2262 		} else if ((shndx = nsym->st_shndx) >= SHN_LORESERVE) {
2263 			sdflags |= FLG_SY_SPECSEC;
2264 		} else if (shndx > ifl->ifl_ehdr->e_shnum) {
2265 			/* We need the name before we can issue error */
2266 			shndx_bad = 1;
2267 		}
2268 
2269 		/*
2270 		 * Check if st_name has a valid value or not.
2271 		 */
2272 		if ((name = string(ofl, ifl, nsym, strs, strsize, ndx, shndx,
2273 		    symsecndx, symsecname, strsecname, &sdflags)) == NULL) {
2274 			ofl->ofl_flags |= FLG_OF_FATAL;
2275 			continue;
2276 		}
2277 
2278 		/*
2279 		 * Now that we have the name, if the section index
2280 		 * was bad, report it.
2281 		 */
2282 		if (shndx_bad) {
2283 			eprintf(ofl->ofl_lml, ERR_WARNING,
2284 			    MSG_INTL(MSG_SYM_INVSHNDX),
2285 			    demangle_symname(name, symsecname, ndx),
2286 			    ifl->ifl_name,
2287 			    conv_sym_shndx(osabi, mach, nsym->st_shndx,
2288 			    CONV_FMT_DECIMAL, &inv_buf));
2289 			continue;
2290 		}
2291 
2292 
2293 		/*
2294 		 * Test for the GNU hidden bit, and ignore symbols that
2295 		 * have it set.
2296 		 */
2297 		if (test_gnu_hidden_bit &&
2298 		    ((ifl->ifl_versym[ndx] & 0x8000) != 0))
2299 			continue;
2300 
2301 		/*
2302 		 * The linker itself will generate symbols for _end, _etext,
2303 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
2304 		 * bother entering these symbols from shared objects.  This
2305 		 * results in some wasted resolution processing, which is hard
2306 		 * to feel, but if nothing else, pollutes diagnostic relocation
2307 		 * output.
2308 		 */
2309 		if (name[0] && (etype == ET_DYN) && (nsym->st_size == 0) &&
2310 		    (ELF_ST_TYPE(nsym->st_info) == STT_OBJECT) &&
2311 		    (name[0] == '_') && ((name[1] == 'e') ||
2312 		    (name[1] == 'D') || (name[1] == 'P')) &&
2313 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
2314 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
2315 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
2316 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
2317 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
2318 			ifl->ifl_oldndx[ndx] = 0;
2319 			continue;
2320 		}
2321 
2322 		/*
2323 		 * The '-z wrap=XXX' option emulates the GNU ld --wrap=XXX
2324 		 * option. When XXX is the symbol to be wrapped:
2325 		 *
2326 		 * -	An undefined reference to XXX is converted to __wrap_XXX
2327 		 * -	An undefined reference to __real_XXX is converted to XXX
2328 		 *
2329 		 * The idea is that the user can supply a wrapper function
2330 		 * __wrap_XXX that does some work, and then uses the name
2331 		 * __real_XXX to pass the call on to the real function. The
2332 		 * wrapper objects are linked with the original unmodified
2333 		 * objects to produce a wrapped version of the output object.
2334 		 */
2335 		if (ofl->ofl_wrap && name[0] && (shndx == SHN_UNDEF)) {
2336 			WrapSymNode wsn, *wsnp;
2337 
2338 			/*
2339 			 * If this is the __real_XXX form, advance the
2340 			 * pointer to reference the wrapped name.
2341 			 */
2342 			wsn.wsn_name = name;
2343 			if ((*name == '_') &&
2344 			    (strncmp(name, MSG_ORIG(MSG_STR_UU_REAL_U),
2345 			    MSG_STR_UU_REAL_U_SIZE) == 0))
2346 				wsn.wsn_name += MSG_STR_UU_REAL_U_SIZE;
2347 
2348 			/*
2349 			 * Is this symbol in the wrap AVL tree? If so, map
2350 			 * XXX to __wrap_XXX, and __real_XXX to XXX. Note that
2351 			 * wsn.wsn_name will equal the current value of name
2352 			 * if the __real_ prefix is not present.
2353 			 */
2354 			if ((wsnp = avl_find(ofl->ofl_wrap, &wsn, 0)) != NULL) {
2355 				const char *old_name = name;
2356 
2357 				name = (wsn.wsn_name == name) ?
2358 				    wsnp->wsn_wrapname : wsn.wsn_name;
2359 				DBG_CALL(Dbg_syms_wrap(ofl->ofl_lml, ndx,
2360 				    old_name, name));
2361 			}
2362 		}
2363 
2364 		/*
2365 		 * Determine and validate the symbols binding.
2366 		 */
2367 		bind = ELF_ST_BIND(nsym->st_info);
2368 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
2369 			eprintf(ofl->ofl_lml, ERR_WARNING,
2370 			    MSG_INTL(MSG_SYM_NONGLOB),
2371 			    demangle_symname(name, symsecname, ndx),
2372 			    ifl->ifl_name,
2373 			    conv_sym_info_bind(bind, 0, &inv_buf));
2374 			continue;
2375 		}
2376 		if (bind == STB_WEAK)
2377 			weak++;
2378 
2379 		/*
2380 		 * If this symbol falls within the range of a section being
2381 		 * discarded, then discard the symbol itself.
2382 		 */
2383 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
2384 		    (nsym->st_shndx != SHN_UNDEF)) {
2385 			Is_desc	*isp;
2386 
2387 			if (shndx >= ifl->ifl_shnum) {
2388 				/*
2389 				 * Carry our some basic sanity checks
2390 				 * The symbol will not be carried forward to
2391 				 * the output file, which won't be a problem
2392 				 * unless a relocation is required against it.
2393 				 */
2394 				eprintf(ofl->ofl_lml, ERR_WARNING,
2395 				    MSG_INTL(MSG_SYM_INVSHNDX),
2396 				    demangle_symname(name, symsecname, ndx),
2397 				    ifl->ifl_name,
2398 				    conv_sym_shndx(osabi, mach, nsym->st_shndx,
2399 				    CONV_FMT_DECIMAL, &inv_buf));
2400 				continue;
2401 			}
2402 
2403 			isp = ifl->ifl_isdesc[shndx];
2404 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
2405 				if ((sdp =
2406 				    libld_calloc(sizeof (Sym_desc), 1)) == NULL)
2407 					return (S_ERROR);
2408 
2409 				/*
2410 				 * Create a dummy symbol entry so that if we
2411 				 * find any references to this discarded symbol
2412 				 * we can compensate.
2413 				 */
2414 				sdp->sd_name = name;
2415 				sdp->sd_sym = nsym;
2416 				sdp->sd_file = ifl;
2417 				sdp->sd_isc = isp;
2418 				sdp->sd_flags = FLG_SY_ISDISC;
2419 				ifl->ifl_oldndx[ndx] = sdp;
2420 
2421 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp));
2422 				continue;
2423 			}
2424 		}
2425 
2426 		/*
2427 		 * If the symbol does not already exist in the internal symbol
2428 		 * table add it, otherwise resolve the conflict.  If the symbol
2429 		 * from this file is kept, retain its symbol table index for
2430 		 * possible use in associating a global alias.
2431 		 */
2432 		/* LINTED */
2433 		hash = (Word)elf_hash((const char *)name);
2434 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
2435 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
2436 			if ((sdp = ld_sym_enter(name, nsym, hash, ifl, ofl, ndx,
2437 			    shndx, sdflags, &where)) == (Sym_desc *)S_ERROR)
2438 				return (S_ERROR);
2439 
2440 		} else if (ld_sym_resolve(sdp, nsym, ifl, ofl, ndx, shndx,
2441 		    sdflags) == S_ERROR)
2442 			return (S_ERROR);
2443 
2444 		/*
2445 		 * After we've compared a defined symbol in one shared
2446 		 * object, flag the symbol so we don't compare it again.
2447 		 */
2448 		if ((etype == ET_DYN) && (nsym->st_shndx != SHN_UNDEF) &&
2449 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
2450 			sdp->sd_flags |= FLG_SY_SOFOUND;
2451 
2452 		/*
2453 		 * If the symbol is accepted from this file retain the symbol
2454 		 * index for possible use in aliasing.
2455 		 */
2456 		if (sdp->sd_file == ifl)
2457 			sdp->sd_symndx = ndx;
2458 
2459 		ifl->ifl_oldndx[ndx] = sdp;
2460 
2461 		/*
2462 		 * If we've accepted a register symbol, continue to validate
2463 		 * it.
2464 		 */
2465 		if (sdp->sd_flags & FLG_SY_REGSYM) {
2466 			Sym_desc	*rsdp;
2467 
2468 			/*
2469 			 * The presence of FLG_SY_REGSYM means that
2470 			 * the pointers in ld_targ.t_ms are non-NULL.
2471 			 */
2472 			rsdp = (*ld_targ.t_ms.ms_reg_find)(sdp->sd_sym, ofl);
2473 			if (rsdp == NULL) {
2474 				if ((*ld_targ.t_ms.ms_reg_enter)(sdp, ofl) == 0)
2475 					return (S_ERROR);
2476 			} else if (rsdp != sdp) {
2477 				(void) (*ld_targ.t_ms.ms_reg_check)(rsdp,
2478 				    sdp->sd_sym, sdp->sd_name, ifl, ofl);
2479 			}
2480 		}
2481 
2482 		/*
2483 		 * For a relocatable object, if this symbol is defined
2484 		 * and has non-zero length and references an address
2485 		 * within an associated section, then check its extents
2486 		 * to make sure the section boundaries encompass it.
2487 		 * If they don't, the ELF file is corrupt. Note that this
2488 		 * global symbol may have come from another file to satisfy
2489 		 * an UNDEF symbol of the same name from this one. In that
2490 		 * case, we don't check it, because it was already checked
2491 		 * as part of its own file.
2492 		 */
2493 		if (etype_rel && (sdp->sd_file == ifl)) {
2494 			Sym *tsym = sdp->sd_sym;
2495 
2496 			if (SYM_LOC_BADADDR(sdp, tsym,
2497 			    ELF_ST_TYPE(tsym->st_info))) {
2498 				issue_badaddr_msg(ifl, ofl, sdp,
2499 				    tsym, tsym->st_shndx);
2500 				continue;
2501 			}
2502 		}
2503 	}
2504 
2505 	/*
2506 	 * Associate weak (alias) symbols to their non-weak counterparts by
2507 	 * scaning the global symbols one more time.
2508 	 *
2509 	 * This association is needed when processing the symbols from a shared
2510 	 * object dependency when a a weak definition satisfies a reference:
2511 	 *
2512 	 *  -	When building a dynamic executable, if a referenced symbol is a
2513 	 *	data item, the symbol data is copied to the executables address
2514 	 *	space.  In this copy-relocation case, we must also reassociate
2515 	 *	the alias symbol with its new location in the executable.
2516 	 *
2517 	 *  -	If the referenced symbol is a function then we may need to
2518 	 *	promote the symbols binding from undefined weak to undefined,
2519 	 *	otherwise the run-time linker will not generate the correct
2520 	 *	relocation error should the symbol not be found.
2521 	 *
2522 	 * Weak alias association is also required when a local dynsym table
2523 	 * is being created.  This table should only contain one instance of a
2524 	 * symbol that is associated to a given address.
2525 	 *
2526 	 * The true association between a weak/strong symbol pair is that both
2527 	 * symbol entries are identical, thus first we create a sorted symbol
2528 	 * list keyed off of the symbols section index and value.  If the symbol
2529 	 * belongs to the same section and has the same value, then the chances
2530 	 * are that the rest of the symbols data is the same.  This list is then
2531 	 * scanned for weak symbols, and if one is found then any strong
2532 	 * association will exist in the entries that follow.  Thus we just have
2533 	 * to scan one (typically a single alias) or more (in the uncommon
2534 	 * instance of multiple weak to strong associations) entries to
2535 	 * determine if a match exists.
2536 	 */
2537 	if (weak && (OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) &&
2538 	    (total > local)) {
2539 		static Sym_desc	**sort;
2540 		static size_t	osize = 0;
2541 		size_t		nsize = (total - local) * sizeof (Sym_desc *);
2542 
2543 		/*
2544 		 * As we might be processing many input files, and many symbols,
2545 		 * try and reuse a static sort buffer.  Note, presently we're
2546 		 * playing the game of never freeing any buffers as there's a
2547 		 * belief this wastes time.
2548 		 */
2549 		if ((osize == 0) || (nsize > osize)) {
2550 			if ((sort = libld_malloc(nsize)) == NULL)
2551 				return (S_ERROR);
2552 			osize = nsize;
2553 		}
2554 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], nsize);
2555 
2556 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
2557 
2558 		for (ndx = 0; ndx < (total - local); ndx++) {
2559 			Sym_desc	*wsdp = sort[ndx];
2560 			Sym		*wsym;
2561 			int		sndx;
2562 
2563 			/*
2564 			 * Ignore any empty symbol descriptor, or the case where
2565 			 * the symbol has been resolved to a different file.
2566 			 */
2567 			if ((wsdp == NULL) || (wsdp->sd_file != ifl))
2568 				continue;
2569 
2570 			wsym = wsdp->sd_sym;
2571 
2572 			if ((wsym->st_shndx == SHN_UNDEF) ||
2573 			    (wsdp->sd_flags & FLG_SY_SPECSEC) ||
2574 			    (ELF_ST_BIND(wsym->st_info) != STB_WEAK))
2575 				continue;
2576 
2577 			/*
2578 			 * We have a weak symbol, if it has a strong alias it
2579 			 * will have been sorted to one of the following sort
2580 			 * table entries.  Note that we could have multiple weak
2581 			 * symbols aliased to one strong (if this occurs then
2582 			 * the strong symbol only maintains one alias back to
2583 			 * the last weak).
2584 			 */
2585 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
2586 				Sym_desc	*ssdp = sort[sndx];
2587 				Sym		*ssym;
2588 				sd_flag_t	w_dynbits, s_dynbits;
2589 
2590 				/*
2591 				 * Ignore any empty symbol descriptor, or the
2592 				 * case where the symbol has been resolved to a
2593 				 * different file.
2594 				 */
2595 				if ((ssdp == NULL) || (ssdp->sd_file != ifl))
2596 					continue;
2597 
2598 				ssym = ssdp->sd_sym;
2599 
2600 				if (ssym->st_shndx == SHN_UNDEF)
2601 					continue;
2602 
2603 				if ((ssym->st_shndx != wsym->st_shndx) ||
2604 				    (ssym->st_value != wsym->st_value))
2605 					break;
2606 
2607 				if ((ssym->st_size != wsym->st_size) ||
2608 				    (ssdp->sd_flags & FLG_SY_SPECSEC) ||
2609 				    (ELF_ST_BIND(ssym->st_info) == STB_WEAK))
2610 					continue;
2611 
2612 				/*
2613 				 * If a sharable object, set link fields so
2614 				 * that they reference each other.`
2615 				 */
2616 				if (etype == ET_DYN) {
2617 					ssdp->sd_aux->sa_linkndx =
2618 					    (Word)wsdp->sd_symndx;
2619 					wsdp->sd_aux->sa_linkndx =
2620 					    (Word)ssdp->sd_symndx;
2621 				}
2622 
2623 				/*
2624 				 * Determine which of these two symbols go into
2625 				 * the sort section.  If a mapfile has made
2626 				 * explicit settings of the FLG_SY_*DYNSORT
2627 				 * flags for both symbols, then we do what they
2628 				 * say.  If one has the DYNSORT flags set, we
2629 				 * set the NODYNSORT bit in the other.  And if
2630 				 * neither has an explicit setting, then we
2631 				 * favor the weak symbol because they usually
2632 				 * lack the leading underscore.
2633 				 */
2634 				w_dynbits = wsdp->sd_flags &
2635 				    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2636 				s_dynbits = ssdp->sd_flags &
2637 				    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2638 				if (!(w_dynbits && s_dynbits)) {
2639 					if (s_dynbits) {
2640 						if (s_dynbits == FLG_SY_DYNSORT)
2641 							wsdp->sd_flags |=
2642 							    FLG_SY_NODYNSORT;
2643 					} else if (w_dynbits !=
2644 					    FLG_SY_NODYNSORT) {
2645 						ssdp->sd_flags |=
2646 						    FLG_SY_NODYNSORT;
2647 					}
2648 				}
2649 				break;
2650 			}
2651 		}
2652 	}
2653 	return (1);
2654 
2655 #undef SYM_LOC_BADADDR
2656 }
2657 
2658 /*
2659  * Add an undefined symbol to the symbol table.  The reference originates from
2660  * the location identifed by the message id (mid).  These references can
2661  * originate from command line options such as -e, -u, -initarray, etc.
2662  * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated
2663  * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)).
2664  */
2665 Sym_desc *
2666 ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid)
2667 {
2668 	Sym		*sym;
2669 	Ifl_desc	*ifl = NULL, *_ifl;
2670 	Sym_desc	*sdp;
2671 	Word		hash;
2672 	Aliste		idx;
2673 	avl_index_t	where;
2674 	const char	*reference = MSG_INTL(mid);
2675 
2676 	/*
2677 	 * As an optimization, determine whether we've already generated this
2678 	 * reference.  If the symbol doesn't already exist we'll create it.
2679 	 * Or if the symbol does exist from a different source, we'll resolve
2680 	 * the conflict.
2681 	 */
2682 	/* LINTED */
2683 	hash = (Word)elf_hash(name);
2684 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) {
2685 		if ((sdp->sd_sym->st_shndx == SHN_UNDEF) &&
2686 		    (sdp->sd_file->ifl_name == reference))
2687 			return (sdp);
2688 	}
2689 
2690 	/*
2691 	 * Determine whether a pseudo input file descriptor exists to represent
2692 	 * the command line, as any global symbol needs an input file descriptor
2693 	 * during any symbol resolution (refer to map_ifl() which provides a
2694 	 * similar method for adding symbols from mapfiles).
2695 	 */
2696 	for (APLIST_TRAVERSE(ofl->ofl_objs, idx, _ifl))
2697 		if (strcmp(_ifl->ifl_name, reference) == 0) {
2698 			ifl = _ifl;
2699 			break;
2700 		}
2701 
2702 	/*
2703 	 * If no descriptor exists create one.
2704 	 */
2705 	if (ifl == NULL) {
2706 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) == NULL)
2707 			return ((Sym_desc *)S_ERROR);
2708 		ifl->ifl_name = reference;
2709 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
2710 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr), 1)) == NULL)
2711 			return ((Sym_desc *)S_ERROR);
2712 		ifl->ifl_ehdr->e_type = ET_REL;
2713 
2714 		if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
2715 			return ((Sym_desc *)S_ERROR);
2716 	}
2717 
2718 	/*
2719 	 * Allocate a symbol structure and add it to the global symbol table.
2720 	 */
2721 	if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
2722 		return ((Sym_desc *)S_ERROR);
2723 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
2724 	sym->st_shndx = SHN_UNDEF;
2725 
2726 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
2727 	if (sdp == NULL) {
2728 		DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
2729 		if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
2730 		    0, &where)) == (Sym_desc *)S_ERROR)
2731 			return ((Sym_desc *)S_ERROR);
2732 	} else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0,
2733 	    SHN_UNDEF, 0) == S_ERROR)
2734 		return ((Sym_desc *)S_ERROR);
2735 
2736 	sdp->sd_flags &= ~FLG_SY_CLEAN;
2737 	sdp->sd_flags |= FLG_SY_CMDREF;
2738 
2739 	return (sdp);
2740 }
2741 
2742 /*
2743  * STT_SECTION symbols have their st_name field set to NULL, and consequently
2744  * have no name. Generate a name suitable for diagnostic use for such a symbol.
2745  * The resulting name will be of the form:
2746  *
2747  *	"XXX (section)"
2748  *
2749  * where XXX is the name of the section.
2750  *
2751  * Diagnostics for STT_SECTION symbols tend to come in clusters,
2752  * so we use a static variable to retain the last string we generate. If
2753  * another one comes along for the same section before some other section
2754  * intervenes, we will reuse the string.
2755  *
2756  * entry:
2757  *	isc - Input section assocated with the symbol.
2758  *	fmt - NULL, or format string to use.
2759  *
2760  * exit:
2761  *	Returns the allocated string, or NULL on allocation failure.
2762  */
2763 const const char *
2764 ld_stt_section_sym_name(Is_desc *isp)
2765 {
2766 	static const char	*last_fmt;
2767 	static Is_desc		*last_isp = NULL;
2768 	static char		*namestr;
2769 
2770 	const char		*fmt;
2771 	size_t			len;
2772 
2773 	if ((isp != NULL) && (isp->is_name != NULL)) {
2774 		fmt = (isp->is_flags & FLG_IS_GNSTRMRG) ?
2775 		    MSG_INTL(MSG_STR_SECTION_MSTR) : MSG_INTL(MSG_STR_SECTION);
2776 
2777 		if ((last_isp == isp) && (last_fmt == fmt))
2778 			return (namestr);
2779 
2780 		len = strlen(fmt) + strlen(isp->is_name) + 1;
2781 
2782 		if ((namestr = libld_malloc(len)) == 0)
2783 			return (NULL);
2784 		(void) snprintf(namestr, len, fmt, isp->is_name);
2785 
2786 		/* Remember for next time */
2787 		last_fmt = fmt;
2788 		last_isp = isp;
2789 
2790 		return (namestr);
2791 	}
2792 
2793 	return (NULL);
2794 }
2795