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