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