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