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