xref: /titanic_50/usr/src/cmd/sgs/libld/common/syms.c (revision ab4a9beb2e4d596be0b3288c7d92919e27781b57)
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 2007 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * Symbol table management routines
34  */
35 #include	<stdio.h>
36 #include	<string.h>
37 #include	<debug.h>
38 #include	"msg.h"
39 #include	"_libld.h"
40 
41 /*
42  * AVL tree comparator function:
43  *
44  *	The primary key is the 'sa_hashval' with a secondary
45  *	key of the symbol name itself.
46  */
47 int
48 ld_sym_avl_comp(const void *elem1, const void *elem2)
49 {
50 	int	res;
51 	Sym_avlnode	*sav1 = (Sym_avlnode *)elem1;
52 	Sym_avlnode	*sav2 = (Sym_avlnode *)elem2;
53 
54 	res = sav1->sav_hash - sav2->sav_hash;
55 
56 	if (res < 0)
57 		return (-1);
58 	if (res > 0)
59 		return (1);
60 
61 	/*
62 	 * Hash is equal - now compare name
63 	 */
64 	res = strcmp(sav1->sav_name, sav2->sav_name);
65 	if (res == 0)
66 		return (0);
67 	if (res > 0)
68 		return (1);
69 	return (-1);
70 }
71 
72 
73 /*
74  * Focal point for verifying symbol names.
75  */
76 static const char *
77 string(Ofl_desc *ofl, Ifl_desc *ifl, Sym *sym, const char *strs, size_t strsize,
78     int symndx, Word shndx, const char *symsecname, const char *strsecname,
79     Word *flags)
80 {
81 	const char	*regname;
82 	Word		name = sym->st_name;
83 
84 	if (name) {
85 		if ((ifl->ifl_flags & FLG_IF_HSTRTAB) == 0) {
86 			eprintf(ofl->ofl_lml, ERR_FATAL,
87 			    MSG_INTL(MSG_FIL_NOSTRTABLE), ifl->ifl_name,
88 			    symsecname, symndx, EC_XWORD(name));
89 			return (0);
90 		}
91 		if (name >= (Word)strsize) {
92 			eprintf(ofl->ofl_lml, ERR_FATAL,
93 			    MSG_INTL(MSG_FIL_EXCSTRTABLE), ifl->ifl_name,
94 			    symsecname, symndx, EC_XWORD(name),
95 			    strsecname, EC_XWORD(strsize));
96 			return (0);
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 ((regname = ld_is_regsym(ofl, ifl, sym, strs, symndx, shndx,
105 	    symsecname, flags)) == (const char *)S_ERROR) {
106 		return (0);
107 	}
108 	if (regname)
109 		return (regname);
110 
111 	/*
112 	 * If this isn't a register, but we have a global symbol with a null
113 	 * name, we're not going to be able to hash this, search for it, or
114 	 * do anything interesting.  However, we've been accepting a symbol of
115 	 * this kind for ages now, so give the user a warning (rather than a
116 	 * fatal error), just in case this instance exists somewhere in the
117 	 * world and hasn't, as yet, been a problem.
118 	 */
119 	if ((name == 0) && (ELF_ST_BIND(sym->st_info) != STB_LOCAL)) {
120 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_NONAMESYM),
121 		    ifl->ifl_name, symsecname, symndx, EC_XWORD(name));
122 	}
123 	return (strs + name);
124 }
125 
126 /*
127  * Shared objects can be built that define specific symbols that can not be
128  * directly bound to.  These objects have a syminfo section (and an associated
129  * DF_1_NODIRECT dynamic flags entry).  Scan this table looking for symbols
130  * that can't be bound to directly, and if this files symbol is presently
131  * referenced, mark it so that we don't directly bind to it.
132  */
133 uintptr_t
134 ld_sym_nodirect(Is_desc *isp, Ifl_desc *ifl, Ofl_desc *ofl)
135 {
136 	Shdr		*sifshdr, *symshdr;
137 	Syminfo		*sifdata;
138 	Sym		*symdata;
139 	char		*strdata;
140 	ulong_t		cnt, _cnt;
141 
142 	/*
143 	 * Get the syminfo data, and determine the number of entries.
144 	 */
145 	sifshdr = isp->is_shdr;
146 	sifdata = (Syminfo *)isp->is_indata->d_buf;
147 	cnt =  sifshdr->sh_size / sifshdr->sh_entsize;
148 
149 	/*
150 	 * Get the associated symbol table.
151 	 */
152 	symshdr = ifl->ifl_isdesc[sifshdr->sh_link]->is_shdr;
153 	symdata = ifl->ifl_isdesc[sifshdr->sh_link]->is_indata->d_buf;
154 
155 	/*
156 	 * Get the string table associated with the symbol table.
157 	 */
158 	strdata = ifl->ifl_isdesc[symshdr->sh_link]->is_indata->d_buf;
159 
160 	/*
161 	 * Traverse the syminfo data for symbols that can't be directly
162 	 * bound to.
163 	 */
164 	for (_cnt = 1, sifdata++; _cnt < cnt; _cnt++, sifdata++) {
165 		Sym		*sym;
166 		char		*str;
167 		Sym_desc	*sdp;
168 
169 		if (((sifdata->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0) ||
170 		    (sifdata->si_boundto < SYMINFO_BT_LOWRESERVE))
171 			continue;
172 
173 		sym = (Sym *)(symdata + _cnt);
174 		str = (char *)(strdata + sym->st_name);
175 
176 		if (sdp = ld_sym_find(str, SYM_NOHASH, 0, ofl)) {
177 			if (ifl != sdp->sd_file)
178 				continue;
179 
180 			sdp->sd_flags1 &= ~FLG_SY1_DIR;
181 			sdp->sd_flags1 |= FLG_SY1_NDIR;
182 		}
183 	}
184 	return (0);
185 }
186 
187 /*
188  * If, during symbol processing, it is necessary to update a local symbols
189  * contents before we have generated the symbol tables in the output image,
190  * create a new symbol structure and copy the original symbol contents.  While
191  * we are processing the input files, their local symbols are part of the
192  * read-only mapped image.  Commonly, these symbols are copied to the new output
193  * file image and then updated to reflect their new address and any change in
194  * attributes.  However, sometimes during relocation counting, it is necessary
195  * to adjust the symbols information.  This routine provides for the generation
196  * of a new symbol image so that this update can be performed.
197  * All global symbols are copied to an internal symbol table to improve locality
198  * of reference and hence performance, and thus this copying is not necessary.
199  */
200 uintptr_t
201 ld_sym_copy(Sym_desc *sdp)
202 {
203 	Sym	*nsym;
204 
205 	if (sdp->sd_flags & FLG_SY_CLEAN) {
206 		if ((nsym = libld_malloc(sizeof (Sym))) == 0)
207 			return (S_ERROR);
208 		*nsym = *(sdp->sd_sym);
209 		sdp->sd_sym = nsym;
210 		sdp->sd_flags &= ~FLG_SY_CLEAN;
211 	}
212 	return (1);
213 }
214 
215 /*
216  * Finds a given name in the link editors internal symbol table.  If no
217  * hash value is specified it is calculated.  A pointer to the located
218  * Sym_desc entry is returned, or NULL if the symbol is not found.
219  */
220 Sym_desc *
221 ld_sym_find(const char *name, Word hash, avl_index_t *where, Ofl_desc *ofl)
222 {
223 	Sym_avlnode	qsav;
224 	Sym_avlnode	*sav;
225 
226 	if (hash == SYM_NOHASH)
227 		/* LINTED */
228 		hash = (Word)elf_hash((const char *)name);
229 	qsav.sav_hash = hash;
230 	qsav.sav_name = name;
231 
232 	/*
233 	 * Perform search for symbol in AVL tree.  Note that the 'where' field
234 	 * is passed in from the caller.  If a 'where' is present, it can be
235 	 * used in subsequent 'sym_enter()' calls if required.
236 	 */
237 	sav = avl_find(&ofl->ofl_symavl, &qsav, where);
238 
239 	/*
240 	 * If symbol was not found in the avl tree, return null to show that.
241 	 */
242 	if (sav == 0)
243 		return (0);
244 
245 	/*
246 	 * Return symbol found.
247 	 */
248 	return (sav->sav_symdesc);
249 }
250 
251 
252 /*
253  * Enter a new symbol into the link editors internal symbol table.
254  * If the symbol is from an input file, information regarding the input file
255  * and input section is also recorded.  Otherwise (file == NULL) the symbol
256  * has been internally generated (ie. _etext, _edata, etc.).
257  */
258 Sym_desc *
259 ld_sym_enter(const char *name, Sym *osym, Word hash, Ifl_desc *ifl,
260     Ofl_desc *ofl, Word ndx, Word shndx, Word sdflags, Half sdflags1,
261     avl_index_t *where)
262 {
263 	Sym_desc	*sdp;
264 	Sym_aux		*sap;
265 	Sym_avlnode	*savl;
266 	char		*_name;
267 	Sym		*nsym;
268 	Half		etype;
269 	uchar_t		vis;
270 	avl_index_t	_where;
271 
272 	/*
273 	 * Establish the file type.
274 	 */
275 	if (ifl)
276 		etype = ifl->ifl_ehdr->e_type;
277 	else
278 		etype = ET_NONE;
279 
280 	ofl->ofl_entercnt++;
281 
282 	/*
283 	 * Allocate a Sym Descriptor, Auxiliary Descriptor, and a Sym AVLNode -
284 	 * contiguously.
285 	 */
286 	if ((savl = libld_calloc(sizeof (Sym_avlnode) + sizeof (Sym_desc) +
287 	    sizeof (Sym_aux), 1)) == 0)
288 		return ((Sym_desc *)S_ERROR);
289 	sdp = (Sym_desc *)((uintptr_t)savl + sizeof (Sym_avlnode));
290 	sap = (Sym_aux *)((uintptr_t)sdp + sizeof (Sym_desc));
291 
292 	savl->sav_symdesc = sdp;
293 	sdp->sd_file = ifl;
294 	sdp->sd_aux = sap;
295 	savl->sav_hash = sap->sa_hash = hash;
296 
297 	/*
298 	 * Copy the symbol table entry from the input file into the internal
299 	 * entry and have the symbol descriptor use it.
300 	 */
301 	sdp->sd_sym = nsym = &sap->sa_sym;
302 	*nsym = *osym;
303 	sdp->sd_shndx = shndx;
304 	sdp->sd_flags |= sdflags;
305 	sdp->sd_flags1 |= sdflags1;
306 
307 	if ((_name = libld_malloc(strlen(name) + 1)) == 0)
308 		return ((Sym_desc *)S_ERROR);
309 	savl->sav_name = sdp->sd_name = (const char *)strcpy(_name, name);
310 
311 	/*
312 	 * Enter Symbol in AVL tree.
313 	 */
314 	if (where == 0) {
315 		/* LINTED */
316 		Sym_avlnode	*_savl;
317 		/*
318 		 * If a previous ld_sym_find() hasn't initialized 'where' do it
319 		 * now.
320 		 */
321 		where = &_where;
322 		_savl = avl_find(&ofl->ofl_symavl, savl, where);
323 		assert(_savl == 0);
324 	}
325 	avl_insert(&ofl->ofl_symavl, savl, *where);
326 
327 	/*
328 	 * Record the section index.  This is possible because the
329 	 * `ifl_isdesc' table is filled before we start symbol processing.
330 	 */
331 	if ((sdflags & FLG_SY_SPECSEC) || (nsym->st_shndx == SHN_UNDEF))
332 		sdp->sd_isc = NULL;
333 	else {
334 		sdp->sd_isc = ifl->ifl_isdesc[shndx];
335 
336 		/*
337 		 * If this symbol is from a relocatable object, make sure that
338 		 * it is still associated with a section.  For example, an
339 		 * unknown section type (SHT_NULL) would have been rejected on
340 		 * input with a warning.  Here, we make the use of the symbol
341 		 * fatal.  A symbol descriptor is still returned, so that the
342 		 * caller can continue processing all symbols, and hence flush
343 		 * out as many error conditions as possible.
344 		 */
345 		if ((etype == ET_REL) && (sdp->sd_isc == 0)) {
346 			eprintf(ofl->ofl_lml, ERR_FATAL,
347 			    MSG_INTL(MSG_SYM_INVSEC), name, ifl->ifl_name,
348 			    EC_XWORD(shndx));
349 			ofl->ofl_flags |= FLG_OF_FATAL;
350 			return (sdp);
351 		}
352 	}
353 
354 	/*
355 	 * Mark any COMMON symbols as 'tentative'.
356 	 */
357 	if (sdflags & FLG_SY_SPECSEC) {
358 		if (nsym->st_shndx == SHN_COMMON)
359 			sdp->sd_flags |= FLG_SY_TENTSYM;
360 #if	defined(__x86) && defined(_ELF64)
361 		else if (nsym->st_shndx == SHN_X86_64_LCOMMON)
362 			sdp->sd_flags |= FLG_SY_TENTSYM;
363 #endif
364 	}
365 
366 	/*
367 	 * Establish the symbols visibility and reference.
368 	 */
369 	vis = ELF_ST_VISIBILITY(nsym->st_other);
370 
371 	if ((etype == ET_NONE) || (etype == ET_REL)) {
372 		switch (vis) {
373 		case STV_DEFAULT:
374 			sdp->sd_flags1 |= FLG_SY1_DEFAULT;
375 			break;
376 		case STV_INTERNAL:
377 		case STV_HIDDEN:
378 			sdp->sd_flags1 |= FLG_SY1_HIDDEN;
379 			break;
380 		case STV_PROTECTED:
381 			sdp->sd_flags1 |= FLG_SY1_PROTECT;
382 			break;
383 		case STV_EXPORTED:
384 			sdp->sd_flags1 |= FLG_SY1_EXPORT;
385 			break;
386 		case STV_SINGLETON:
387 			sdp->sd_flags1 |= (FLG_SY1_SINGLE | FLG_SY1_NDIR);
388 			ofl->ofl_flags1 |= FLG_OF1_NDIRECT;
389 			break;
390 		case STV_ELIMINATE:
391 			sdp->sd_flags1 |= FLG_SY1_ELIM;
392 			break;
393 		default:
394 			assert(vis <= STV_ELIMINATE);
395 		}
396 
397 		sdp->sd_ref = REF_REL_NEED;
398 
399 		/*
400 		 * Under -Bnodirect, all exported interfaces that have not
401 		 * explicitly been defined protected or directly bound to, are
402 		 * tagged to prevent direct binding.
403 		 */
404 		if ((ofl->ofl_flags1 & FLG_OF1_ALNODIR) &&
405 		    ((sdp->sd_flags1 & (FLG_SY1_PROTECT | FLG_SY1_DIR)) == 0) &&
406 		    (nsym->st_shndx != SHN_UNDEF)) {
407 			sdp->sd_flags1 |= FLG_SY1_NDIR;
408 		}
409 	} else {
410 		sdp->sd_ref = REF_DYN_SEEN;
411 
412 		/*
413 		 * Record the binding file for this symbol in the sa_bindto
414 		 * field.  If this symbol is ever overridden by a REF_REL_NEED
415 		 * definition, sa_bindto is used when building a 'translator'.
416 		 */
417 		if (nsym->st_shndx != SHN_UNDEF)
418 			sdp->sd_aux->sa_bindto = ifl;
419 
420 		/*
421 		 * If this is a protected symbol, remember this.  Note, this
422 		 * state is different from the FLG_SY1_PROTECT used to establish
423 		 * a symbol definitions visibility.  This state is used to warn
424 		 * against possible copy relocations against this referenced
425 		 * symbol.
426 		 */
427 		if (vis == STV_PROTECTED)
428 			sdp->sd_flags |= FLG_SY_PROT;
429 
430 		/*
431 		 * If this is a SINGLETON definition, then indicate the symbol
432 		 * can not be directly bound to, and retain the visibility.
433 		 * This visibility will be inherited by any references made to
434 		 * this symbol.
435 		 */
436 		if ((vis == STV_SINGLETON) && (nsym->st_shndx != SHN_UNDEF))
437 			sdp->sd_flags1 |= (FLG_SY1_SINGLE | FLG_SY1_NDIR);
438 
439 		/*
440 		 * If the new symbol is from a shared library and is associated
441 		 * with a SHT_NOBITS section then this symbol originated from a
442 		 * tentative symbol.
443 		 */
444 		if (sdp->sd_isc &&
445 		    (sdp->sd_isc->is_shdr->sh_type == SHT_NOBITS))
446 			sdp->sd_flags |= FLG_SY_TENTSYM;
447 	}
448 
449 	/*
450 	 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF so as to
451 	 * simplify future processing.
452 	 */
453 	if (nsym->st_shndx == SHN_SUNW_IGNORE) {
454 		sdp->sd_shndx = shndx = SHN_UNDEF;
455 		sdp->sd_flags |= FLG_SY_REDUCED;
456 		sdp->sd_flags1 |=
457 		    (FLG_SY1_HIDDEN | FLG_SY1_IGNORE | FLG_SY1_ELIM);
458 	}
459 
460 	/*
461 	 * If this is an undefined, or common symbol from a relocatable object
462 	 * determine whether it is a global or weak reference (see build_osym(),
463 	 * where REF_DYN_NEED definitions are returned back to undefines).
464 	 */
465 	if ((etype == ET_REL) &&
466 	    (ELF_ST_BIND(nsym->st_info) == STB_GLOBAL) &&
467 	    ((nsym->st_shndx == SHN_UNDEF) || ((sdflags & FLG_SY_SPECSEC) &&
468 #if	defined(__x86) && defined(_ELF64)
469 	    ((nsym->st_shndx == SHN_COMMON) ||
470 	    (nsym->st_shndx == SHN_X86_64_LCOMMON)))))
471 #else
472 	/* BEGIN CSTYLED */
473 	    (nsym->st_shndx == SHN_COMMON))))
474 	/* END CSTYLED */
475 #endif
476 		sdp->sd_flags |= FLG_SY_GLOBREF;
477 
478 	/*
479 	 * Record the input filename on the referenced or defined files list
480 	 * for possible later diagnostics.  The `sa_rfile' pointer contains the
481 	 * name of the file that first referenced this symbol and is used to
482 	 * generate undefined symbol diagnostics (refer to sym_undef_entry()).
483 	 * Note that this entry can be overridden if a reference from a
484 	 * relocatable object is found after a reference from a shared object
485 	 * (refer to sym_override()).
486 	 * The `sa_dfiles' list is used to maintain the list of files that
487 	 * define the same symbol.  This list can be used for two reasons:
488 	 *
489 	 *   o	To save the first definition of a symbol that is not available
490 	 *	for this link-edit.
491 	 *
492 	 *   o	To save all definitions of a symbol when the -m option is in
493 	 *	effect.  This is optional as it is used to list multiple
494 	 *	(interposed) definitions of a symbol (refer to ldmap_out()),
495 	 *	and can be quite expensive.
496 	 */
497 	if (nsym->st_shndx == SHN_UNDEF) {
498 		sap->sa_rfile = ifl->ifl_name;
499 	} else {
500 		if (sdp->sd_ref == REF_DYN_SEEN) {
501 			/*
502 			 * A symbol is determined to be unavailable if it
503 			 * belongs to a version of a shared object that this
504 			 * user does not wish to use, or if it belongs to an
505 			 * implicit shared object.
506 			 */
507 			if (ifl->ifl_vercnt) {
508 				Ver_index	*vip;
509 				Half		vndx = ifl->ifl_versym[ndx];
510 
511 				sap->sa_dverndx = vndx;
512 				vip = &ifl->ifl_verndx[vndx];
513 				if (!(vip->vi_flags & FLG_VER_AVAIL)) {
514 					sdp->sd_flags |= FLG_SY_NOTAVAIL;
515 					sap->sa_vfile = ifl->ifl_name;
516 				}
517 			}
518 			if (!(ifl->ifl_flags & FLG_IF_NEEDED))
519 				sdp->sd_flags |= FLG_SY_NOTAVAIL;
520 
521 		} else if (etype == ET_REL) {
522 			/*
523 			 * If this symbol has been obtained from a versioned
524 			 * input relocatable object then the new symbol must be
525 			 * promoted to the versioning of the output file.
526 			 */
527 			if (ifl->ifl_versym)
528 				ld_vers_promote(sdp, ndx, ifl, ofl);
529 		}
530 
531 		if ((ofl->ofl_flags & FLG_OF_GENMAP) &&
532 		    ((sdflags & FLG_SY_SPECSEC) == 0))
533 			if (list_appendc(&sap->sa_dfiles, ifl->ifl_name) == 0)
534 				return ((Sym_desc *)S_ERROR);
535 	}
536 
537 	/*
538 	 * Provided we're not processing a mapfile, diagnose the entered symbol.
539 	 * Mapfile processing requires the symbol to be updated with additional
540 	 * information, therefore the diagnosing of the symbol is deferred until
541 	 * later (see Dbg_map_symbol()).
542 	 */
543 	if ((ifl == 0) || ((ifl->ifl_flags & FLG_IF_MAPFILE) == 0))
544 		DBG_CALL(Dbg_syms_entered(ofl, nsym, sdp));
545 	return (sdp);
546 }
547 
548 /*
549  * Add a special symbol to the symbol table.  Takes special symbol name with
550  * and without underscores.  This routine is called, after all other symbol
551  * resolution has completed, to generate a reserved absolute symbol (the
552  * underscore version).  Special symbols are updated with the appropriate
553  * values in update_osym().  If the user has already defined this symbol
554  * issue a warning and leave the symbol as is.  If the non-underscore symbol
555  * is referenced then turn it into a weak alias of the underscored symbol.
556  *
557  * The bits in flags_u are OR'd into the flags field of the symbol
558  * for the underscored symbol.
559  *
560  * If this is a global symbol, and it hasn't explicitly been defined as being
561  * directly bound to, indicate that it can't be directly bound to.
562  * Historically, most special symbols only have meaning to the object in which
563  * they exist, however, they've always been global.  To ensure compatibility
564  * with any unexpected use presently in effect, ensure these symbols don't get
565  * directly bound to.  Note, that establishing this state here isn't sufficient
566  * to create a syminfo table, only if a syminfo table is being created by some
567  * other symbol directives will the nodirect binding be recorded.  This ensures
568  * we don't create syminfo sections for all objects we create, as this might add
569  * unnecessary bloat to users who haven't explicitly requested extra symbol
570  * information.
571  */
572 static uintptr_t
573 sym_add_spec(const char *name, const char *uname, Word sdaux_id,
574     Word flags_u, Half flags1, Ofl_desc *ofl)
575 {
576 	Sym_desc	*sdp;
577 	Sym_desc 	*usdp;
578 	Sym		*sym;
579 	Word		hash;
580 	avl_index_t	where;
581 
582 	/* LINTED */
583 	hash = (Word)elf_hash(uname);
584 	if (usdp = ld_sym_find(uname, hash, &where, ofl)) {
585 		/*
586 		 * If the underscore symbol exists and is undefined, or was
587 		 * defined in a shared library, convert it to a local symbol.
588 		 * Otherwise leave it as is and warn the user.
589 		 */
590 		if ((usdp->sd_shndx == SHN_UNDEF) ||
591 		    (usdp->sd_ref != REF_REL_NEED)) {
592 			usdp->sd_ref = REF_REL_NEED;
593 			usdp->sd_shndx = usdp->sd_sym->st_shndx = SHN_ABS;
594 			usdp->sd_flags |= FLG_SY_SPECSEC | flags_u;
595 			usdp->sd_sym->st_info =
596 			    ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
597 			usdp->sd_isc = NULL;
598 			usdp->sd_sym->st_size = 0;
599 			usdp->sd_sym->st_value = 0;
600 			/* LINTED */
601 			usdp->sd_aux->sa_symspec = (Half)sdaux_id;
602 
603 			/*
604 			 * If a user hasn't specifically indicated that the
605 			 * scope of this symbol be made local, then leave it
606 			 * as global (ie. prevent automatic scoping).  The GOT
607 			 * should be defined protected, whereas all other
608 			 * special symbols are tagged as no-direct.
609 			 */
610 			if (((usdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) &&
611 			    (flags1 & FLG_SY1_DEFAULT)) {
612 				usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
613 				if (sdaux_id == SDAUX_ID_GOT) {
614 					usdp->sd_flags1 &= ~FLG_SY1_NDIR;
615 					usdp->sd_flags1 |= FLG_SY1_PROTECT;
616 					usdp->sd_sym->st_other = STV_PROTECTED;
617 				} else if (
618 				    ((usdp->sd_flags1 & FLG_SY1_DIR) == 0) &&
619 				    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
620 					usdp->sd_flags1 |= FLG_SY1_NDIR;
621 				}
622 			}
623 			usdp->sd_flags1 |= flags1;
624 
625 			/*
626 			 * If the reference originated from a mapfile ensure
627 			 * we mark the symbol as used.
628 			 */
629 			if (usdp->sd_flags & FLG_SY_MAPREF)
630 				usdp->sd_flags |= FLG_SY_MAPUSED;
631 
632 			DBG_CALL(Dbg_syms_updated(ofl, usdp, uname));
633 		} else
634 			eprintf(ofl->ofl_lml, ERR_WARNING,
635 			    MSG_INTL(MSG_SYM_RESERVE), uname,
636 			    usdp->sd_file->ifl_name);
637 	} else {
638 		/*
639 		 * If the symbol does not exist create it.
640 		 */
641 		if ((sym = libld_calloc(sizeof (Sym), 1)) == 0)
642 			return (S_ERROR);
643 		sym->st_shndx = SHN_ABS;
644 		sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
645 		sym->st_size = 0;
646 		sym->st_value = 0;
647 		DBG_CALL(Dbg_syms_created(ofl->ofl_lml, uname));
648 		if ((usdp = ld_sym_enter(uname, sym, hash, (Ifl_desc *)NULL,
649 		    ofl, 0, SHN_ABS, FLG_SY_SPECSEC | flags_u, 0, &where)) ==
650 		    (Sym_desc *)S_ERROR)
651 			return (S_ERROR);
652 		usdp->sd_ref = REF_REL_NEED;
653 		/* LINTED */
654 		usdp->sd_aux->sa_symspec = (Half)sdaux_id;
655 
656 		usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
657 
658 		if (sdaux_id == SDAUX_ID_GOT) {
659 			usdp->sd_flags1 |= FLG_SY1_PROTECT;
660 			usdp->sd_sym->st_other = STV_PROTECTED;
661 		} else if ((flags1 & FLG_SY1_DEFAULT) &&
662 		    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
663 			usdp->sd_flags1 |= FLG_SY1_NDIR;
664 		}
665 		usdp->sd_flags1 |= flags1;
666 	}
667 
668 	if (name && (sdp = ld_sym_find(name, SYM_NOHASH, 0, ofl)) &&
669 	    (sdp->sd_sym->st_shndx == SHN_UNDEF)) {
670 		uchar_t	bind;
671 
672 		/*
673 		 * If the non-underscore symbol exists and is undefined
674 		 * convert it to be a local.  If the underscore has
675 		 * sa_symspec set (ie. it was created above) then simulate this
676 		 * as a weak alias.
677 		 */
678 		sdp->sd_ref = REF_REL_NEED;
679 		sdp->sd_shndx = sdp->sd_sym->st_shndx = SHN_ABS;
680 		sdp->sd_flags |= FLG_SY_SPECSEC;
681 		sdp->sd_isc = NULL;
682 		sdp->sd_sym->st_size = 0;
683 		sdp->sd_sym->st_value = 0;
684 		/* LINTED */
685 		sdp->sd_aux->sa_symspec = (Half)sdaux_id;
686 		if (usdp->sd_aux->sa_symspec) {
687 			usdp->sd_aux->sa_linkndx = 0;
688 			sdp->sd_aux->sa_linkndx = 0;
689 			bind = STB_WEAK;
690 		} else
691 			bind = STB_GLOBAL;
692 		sdp->sd_sym->st_info = ELF_ST_INFO(bind, STT_OBJECT);
693 
694 		/*
695 		 * If a user hasn't specifically indicated the scope of this
696 		 * symbol be made local then leave it as global (ie. prevent
697 		 * automatic scoping).  The GOT should be defined protected,
698 		 * whereas all other special symbols are tagged as no-direct.
699 		 */
700 		if (((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) &&
701 		    (flags1 & FLG_SY1_DEFAULT)) {
702 			sdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
703 			if (sdaux_id == SDAUX_ID_GOT) {
704 				sdp->sd_flags1 &= ~FLG_SY1_NDIR;
705 				sdp->sd_flags1 |= FLG_SY1_PROTECT;
706 				sdp->sd_sym->st_other = STV_PROTECTED;
707 			} else if (((sdp->sd_flags1 & FLG_SY1_DIR) == 0) &&
708 			    ((ofl->ofl_flags & FLG_OF_SYMBOLIC) == 0)) {
709 				sdp->sd_flags1 |= FLG_SY1_NDIR;
710 			}
711 		}
712 		sdp->sd_flags1 |= flags1;
713 
714 		/*
715 		 * If the reference originated from a mapfile ensure
716 		 * we mark the symbol as used.
717 		 */
718 		if (sdp->sd_flags & FLG_SY_MAPREF)
719 			sdp->sd_flags |= FLG_SY_MAPUSED;
720 
721 		DBG_CALL(Dbg_syms_updated(ofl, sdp, name));
722 	}
723 	return (1);
724 }
725 
726 
727 /*
728  * Print undefined symbols.
729  */
730 static Boolean	undef_title = TRUE;
731 
732 static void
733 sym_undef_title(Ofl_desc *ofl)
734 {
735 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_SYM_FMT_UNDEF),
736 	    MSG_INTL(MSG_SYM_UNDEF_ITM_11),
737 	    MSG_INTL(MSG_SYM_UNDEF_ITM_21),
738 	    MSG_INTL(MSG_SYM_UNDEF_ITM_12),
739 	    MSG_INTL(MSG_SYM_UNDEF_ITM_22));
740 
741 	undef_title = FALSE;
742 }
743 
744 /*
745  * Undefined symbols can fall into one of four types:
746  *
747  *  o	the symbol is really undefined (SHN_UNDEF).
748  *
749  *  o	versioning has been enabled, however this symbol has not been assigned
750  *	to one of the defined versions.
751  *
752  *  o	the symbol has been defined by an implicitly supplied library, ie. one
753  *	which was encounted because it was NEEDED by another library, rather
754  * 	than from a command line supplied library which would become the only
755  *	dependency of the output file being produced.
756  *
757  *  o	the symbol has been defined by a version of a shared object that is
758  *	not permitted for this link-edit.
759  *
760  * In all cases the file who made the first reference to this symbol will have
761  * been recorded via the `sa_rfile' pointer.
762  */
763 typedef enum {
764 	UNDEF,		NOVERSION,	IMPLICIT,	NOTAVAIL,
765 	BNDLOCAL
766 } Type;
767 
768 static const Msg format[] = {
769 	MSG_SYM_UND_UNDEF,		/* MSG_INTL(MSG_SYM_UND_UNDEF) */
770 	MSG_SYM_UND_NOVER,		/* MSG_INTL(MSG_SYM_UND_NOVER) */
771 	MSG_SYM_UND_IMPL,		/* MSG_INTL(MSG_SYM_UND_IMPL) */
772 	MSG_SYM_UND_NOTA,		/* MSG_INTL(MSG_SYM_UND_NOTA) */
773 	MSG_SYM_UND_BNDLOCAL		/* MSG_INTL(MSG_SYM_UND_BNDLOCAL) */
774 };
775 
776 static void
777 sym_undef_entry(Ofl_desc *ofl, Sym_desc *sdp, Type type)
778 {
779 	const char	*name1, *name2, *name3;
780 	Ifl_desc	*ifl = sdp->sd_file;
781 	Sym_aux		*sap = sdp->sd_aux;
782 
783 	if (undef_title)
784 		sym_undef_title(ofl);
785 
786 	switch (type) {
787 	case UNDEF:
788 	case BNDLOCAL:
789 		name1 = sap->sa_rfile;
790 		break;
791 	case NOVERSION:
792 		name1 = ifl->ifl_name;
793 		break;
794 	case IMPLICIT:
795 		name1 = sap->sa_rfile;
796 		name2 = ifl->ifl_name;
797 		break;
798 	case NOTAVAIL:
799 		name1 = sap->sa_rfile;
800 		name2 = sap->sa_vfile;
801 		name3 = ifl->ifl_verndx[sap->sa_dverndx].vi_name;
802 		break;
803 	default:
804 		return;
805 	}
806 
807 	eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(format[type]),
808 	    demangle(sdp->sd_name), name1, name2, name3);
809 }
810 
811 /*
812  * At this point all symbol input processing has been completed, therefore
813  * complete the symbol table entries by generating any necessary internal
814  * symbols.
815  */
816 uintptr_t
817 ld_sym_spec(Ofl_desc *ofl)
818 {
819 	Sym_desc	*sdp;
820 
821 	if (ofl->ofl_flags & FLG_OF_RELOBJ)
822 		return (1);
823 
824 	DBG_CALL(Dbg_syms_spec_title(ofl->ofl_lml));
825 
826 	if (sym_add_spec(MSG_ORIG(MSG_SYM_ETEXT), MSG_ORIG(MSG_SYM_ETEXT_U),
827 	    SDAUX_ID_ETEXT, 0, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
828 	    ofl) == S_ERROR)
829 		return (S_ERROR);
830 	if (sym_add_spec(MSG_ORIG(MSG_SYM_EDATA), MSG_ORIG(MSG_SYM_EDATA_U),
831 	    SDAUX_ID_EDATA, 0, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
832 	    ofl) == S_ERROR)
833 		return (S_ERROR);
834 	if (sym_add_spec(MSG_ORIG(MSG_SYM_END), MSG_ORIG(MSG_SYM_END_U),
835 	    SDAUX_ID_END, FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
836 	    ofl) == S_ERROR)
837 		return (S_ERROR);
838 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_END), MSG_ORIG(MSG_SYM_L_END_U),
839 	    SDAUX_ID_END, 0, FLG_SY1_HIDDEN, ofl) == S_ERROR)
840 		return (S_ERROR);
841 	if (sym_add_spec(MSG_ORIG(MSG_SYM_L_START), MSG_ORIG(MSG_SYM_L_START_U),
842 	    SDAUX_ID_START, 0, FLG_SY1_HIDDEN, ofl) == S_ERROR)
843 		return (S_ERROR);
844 
845 	/*
846 	 * Historically we've always produced a _DYNAMIC symbol, even for
847 	 * static executables (in which case its value will be 0).
848 	 */
849 	if (sym_add_spec(MSG_ORIG(MSG_SYM_DYNAMIC), MSG_ORIG(MSG_SYM_DYNAMIC_U),
850 	    SDAUX_ID_DYN, FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
851 	    ofl) == S_ERROR)
852 		return (S_ERROR);
853 
854 	if (OFL_ALLOW_DYNSYM(ofl))
855 		if (sym_add_spec(MSG_ORIG(MSG_SYM_PLKTBL),
856 		    MSG_ORIG(MSG_SYM_PLKTBL_U), SDAUX_ID_PLT,
857 		    FLG_SY_DYNSORT, (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF),
858 		    ofl) == S_ERROR)
859 			return (S_ERROR);
860 
861 	/*
862 	 * A GOT reference will be accompanied by the associated GOT symbol.
863 	 * Make sure it gets assigned the appropriate special attributes.
864 	 */
865 	if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U),
866 	    SYM_NOHASH, 0, ofl)) != 0) && (sdp->sd_ref != REF_DYN_SEEN)) {
867 		if (sym_add_spec(MSG_ORIG(MSG_SYM_GOFTBL),
868 		    MSG_ORIG(MSG_SYM_GOFTBL_U), SDAUX_ID_GOT, FLG_SY_DYNSORT,
869 		    (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF), ofl) == S_ERROR)
870 			return (S_ERROR);
871 	}
872 
873 	return (1);
874 }
875 
876 /*
877  * This routine checks to see if a symbols visibility needs to be reduced to
878  * either SYMBOLIC or LOCAL.  This routine can be called from either
879  * reloc_init() or sym_validate().
880  */
881 void
882 ld_sym_adjust_vis(Sym_desc *sdp, Ofl_desc *ofl)
883 {
884 	Word	oflags = ofl->ofl_flags, oflags1 = ofl->ofl_flags1;
885 	Sym	*sym = sdp->sd_sym;
886 
887 	if ((sdp->sd_ref == REF_REL_NEED) &&
888 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
889 		/*
890 		 * If auto-reduction/elimination is enabled, reduce any
891 		 * non-versioned global symbols.  This routine is called either
892 		 * from any initial relocation processing that references this
893 		 * symbol, or from the symbol validation processing.
894 		 *
895 		 * A symbol is a candidate for auto-reduction/elimination if:
896 		 *
897 		 *   .  the symbol wasn't explicitly defined within a mapfile
898 		 *	(in which case all the necessary state has been applied
899 		 *	to the symbol), or
900 		 *   .	the symbol isn't one of the family of reserved
901 		 *	special symbols (ie. _end, _etext, etc.), or
902 		 *   .	the symbol isn't a SINGLETON, or
903 		 *   .  the symbol wasn't explicitly defined within a version
904 		 *	definition associated with an input relocatable object.
905 		 *
906 		 * Indicate that the symbol has been reduced as it may be
907 		 * necessary to print these symbols later.
908 		 */
909 		if (((oflags & FLG_OF_AUTOLCL) ||
910 		    (oflags1 & FLG_OF1_AUTOELM)) &&
911 		    ((sdp->sd_flags1 & MSK_SY1_NOAUTO) == 0)) {
912 			if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) == 0) {
913 				sdp->sd_flags |= FLG_SY_REDUCED;
914 				sdp->sd_flags1 |= FLG_SY1_HIDDEN;
915 			}
916 
917 			if (ofl->ofl_flags1 &
918 			    (FLG_OF1_REDLSYM | FLG_OF1_AUTOELM)) {
919 				sdp->sd_flags1 |= FLG_SY1_ELIM;
920 				sym->st_other = STV_ELIMINATE |
921 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
922 			} else if (ELF_ST_VISIBILITY(sym->st_other) !=
923 			    STV_INTERNAL)
924 				sym->st_other = STV_HIDDEN |
925 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
926 		}
927 
928 		/*
929 		 * If -Bsymbolic is in effect, and the symbol hasn't explicitly
930 		 * been defined nodirect (via a mapfile), then bind the global
931 		 * symbol symbolically and assign the STV_PROTECTED visibility
932 		 * attribute.
933 		 */
934 		if ((oflags & FLG_OF_SYMBOLIC) &&
935 		    ((sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_NDIR)) == 0)) {
936 			sdp->sd_flags1 |= FLG_SY1_PROTECT;
937 			if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT)
938 				sym->st_other = STV_PROTECTED |
939 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
940 		}
941 	}
942 
943 	/*
944 	 * Indicate that this symbol has had it's visibility checked so that
945 	 * we don't need to do this investigation again.
946 	 */
947 	sdp->sd_flags |= FLG_SY_VISIBLE;
948 }
949 
950 /*
951  * Make sure a symbol definition is local to the object being built.
952  */
953 static int
954 ensure_sym_local(Ofl_desc *ofl, Sym_desc *sdp, const char *str)
955 {
956 	if (sdp->sd_sym->st_shndx == SHN_UNDEF) {
957 		if (str) {
958 			eprintf(ofl->ofl_lml, ERR_FATAL,
959 			    MSG_INTL(MSG_SYM_UNDEF), str,
960 			    demangle((char *)sdp->sd_name));
961 		}
962 		return (1);
963 	}
964 	if (sdp->sd_ref != REF_REL_NEED) {
965 		if (str) {
966 			eprintf(ofl->ofl_lml, ERR_FATAL,
967 			    MSG_INTL(MSG_SYM_EXTERN), str,
968 			    demangle((char *)sdp->sd_name),
969 			    sdp->sd_file->ifl_name);
970 		}
971 		return (1);
972 	}
973 
974 	sdp->sd_flags |= FLG_SY_UPREQD;
975 	if (sdp->sd_isc) {
976 		sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
977 		sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
978 	}
979 	return (0);
980 }
981 
982 /*
983  * Make sure all the symbol definitions required for initarray, finiarray, or
984  * preinitarray's are local to the object being built.
985  */
986 static int
987 ensure_array_local(Ofl_desc *ofl, List *list, const char *str)
988 {
989 	Listnode	*lnp;
990 	Sym_desc	*sdp;
991 	int		ret = 0;
992 
993 	for (LIST_TRAVERSE(list, lnp, sdp))
994 		ret += ensure_sym_local(ofl, sdp, str);
995 
996 	return (ret);
997 }
998 
999 /*
1000  * After all symbol table input processing has been finished, and all relocation
1001  * counting has been carried out (ie. no more symbols will be read, generated,
1002  * or modified), validate and count the relevant entries:
1003  *
1004  *	o	check and print any undefined symbols remaining.  Note that
1005  *		if a symbol has been defined by virtue of the inclusion of
1006  *		an implicit shared library, it is still classed as undefined.
1007  *
1008  * 	o	count the number of global needed symbols together with the
1009  *		size of their associated name strings (if scoping has been
1010  *		indicated these symbols may be reduced to locals).
1011  *
1012  *	o	establish the size and alignment requirements for the global
1013  *		.bss section (the alignment of this section is based on the
1014  *		first symbol that it will contain).
1015  */
1016 uintptr_t
1017 ld_sym_validate(Ofl_desc *ofl)
1018 {
1019 	Sym_avlnode	*sav;
1020 	Sym_desc	*sdp;
1021 	Sym		*sym;
1022 	Word		oflags = ofl->ofl_flags;
1023 	Word		undef = 0, needed = 0, verdesc = 0;
1024 	Xword		bssalign = 0, tlsalign = 0;
1025 	Xword		bsssize = 0, tlssize = 0;
1026 #if	defined(__x86) && defined(_ELF64)
1027 	Xword		lbssalign = 0, lbsssize = 0;
1028 #endif
1029 	int		ret;
1030 	int		allow_ldynsym;
1031 	uchar_t		type;
1032 
1033 	/*
1034 	 * If a symbol is undefined and this link-edit calls for no undefined
1035 	 * symbols to remain (this is the default case when generating an
1036 	 * executable but can be enforced for any object using -z defs), the
1037 	 * symbol is classified as undefined and a fatal error condition will
1038 	 * be indicated.
1039 	 *
1040 	 * If the symbol is undefined and we're creating a shared object with
1041 	 * the -Bsymbolic flag, then the symbol is also classified as undefined
1042 	 * and a warning condition will be indicated.
1043 	 */
1044 	if ((oflags & (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC)) ==
1045 	    (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC))
1046 		undef = FLG_OF_WARN;
1047 	if (oflags & FLG_OF_NOUNDEF)
1048 		undef = FLG_OF_FATAL;
1049 
1050 	/*
1051 	 * If the symbol is referenced from an implicitly included shared object
1052 	 * (ie. it's not on the NEEDED list) then the symbol is also classified
1053 	 * as undefined and a fatal error condition will be indicated.
1054 	 */
1055 	if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ))
1056 		needed = FLG_OF_FATAL;
1057 
1058 	/*
1059 	 * If the output image is being versioned all symbol definitions must be
1060 	 * associated with a version.  Any symbol that isn't is classified as
1061 	 * undefined and a fatal error condition will be indicated.
1062 	 */
1063 	if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL))
1064 		verdesc = FLG_OF_FATAL;
1065 
1066 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1067 
1068 	if (allow_ldynsym) {
1069 		/*
1070 		 * Normally, we disallow symbols with 0 size from appearing
1071 		 * in a dyn[sym|tls]sort section. However, there are some
1072 		 * symbols that serve special purposes that we want to exempt
1073 		 * from this rule. Look them up, and set their
1074 		 * FLG_SY_DYNSORT flag.
1075 		 */
1076 		static const char *special[] = {
1077 			MSG_ORIG(MSG_SYM_INIT_U),	/* _init */
1078 			MSG_ORIG(MSG_SYM_FINI_U),	/* _fini */
1079 			MSG_ORIG(MSG_SYM_START),	/* _start */
1080 			NULL
1081 		};
1082 		int i;
1083 
1084 		for (i = 0; special[i] != NULL; i++) {
1085 			if (((sdp = ld_sym_find(special[i],
1086 			    SYM_NOHASH, 0, ofl)) != NULL) &&
1087 			    (sdp->sd_sym->st_size == 0)) {
1088 				if (ld_sym_copy(sdp) == S_ERROR)
1089 					return (S_ERROR);
1090 				sdp->sd_flags |= FLG_SY_DYNSORT;
1091 			}
1092 		}
1093 	}
1094 
1095 	/*
1096 	 * Collect and validate the globals from the internal symbol table.
1097 	 */
1098 	for (sav = avl_first(&ofl->ofl_symavl); sav;
1099 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
1100 		Is_desc		*isp;
1101 		int		undeferr = 0;
1102 		uchar_t		vis;
1103 
1104 		sdp = sav->sav_symdesc;
1105 
1106 		/*
1107 		 * If undefined symbols are allowed ignore any symbols that are
1108 		 * not needed.
1109 		 */
1110 		if (!(oflags & FLG_OF_NOUNDEF) &&
1111 		    (sdp->sd_ref == REF_DYN_SEEN))
1112 			continue;
1113 
1114 		/*
1115 		 * If the symbol originates from an external or parent mapfile
1116 		 * reference and hasn't been matched to a reference from a
1117 		 * relocatable object, ignore it.
1118 		 */
1119 		if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) &&
1120 		    ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) {
1121 			sdp->sd_flags |= FLG_SY_INVALID;
1122 			continue;
1123 		}
1124 
1125 		sym = sdp->sd_sym;
1126 		type = ELF_ST_TYPE(sym->st_info);
1127 
1128 		/*
1129 		 * Sanity check TLS.
1130 		 */
1131 		if ((type == STT_TLS) && (sym->st_size != 0) &&
1132 		    (sym->st_shndx != SHN_UNDEF) &&
1133 		    (sym->st_shndx != SHN_COMMON)) {
1134 			Is_desc		*isp = sdp->sd_isc;
1135 			Ifl_desc	*ifl = sdp->sd_file;
1136 
1137 			if ((isp == 0) || (isp->is_shdr == 0) ||
1138 			    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
1139 				eprintf(ofl->ofl_lml, ERR_FATAL,
1140 				    MSG_INTL(MSG_SYM_TLS),
1141 				    demangle(sdp->sd_name), ifl->ifl_name);
1142 				ofl->ofl_flags |= FLG_OF_FATAL;
1143 				continue;
1144 			}
1145 		}
1146 
1147 		if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)
1148 			ld_sym_adjust_vis(sdp, ofl);
1149 
1150 		if ((sdp->sd_flags & FLG_SY_REDUCED) &&
1151 		    (oflags & FLG_OF_PROCRED)) {
1152 			DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL,
1153 			    sdp, 0, 0));
1154 		}
1155 
1156 		/*
1157 		 * Record any STV_SINGLETON existence.
1158 		 */
1159 		if ((vis = ELF_ST_VISIBILITY(sym->st_other)) == STV_SINGLETON)
1160 			ofl->ofl_dtflags_1 |= DF_1_SINGLETON;
1161 
1162 		/*
1163 		 * If building a shared object or executable, and this is a
1164 		 * non-weak UNDEF symbol with reduced visibility (STV_*), then
1165 		 * give a fatal error.
1166 		 */
1167 		if (((oflags & FLG_OF_RELOBJ) == 0) &&
1168 		    (sym->st_shndx == SHN_UNDEF) &&
1169 		    (ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
1170 			if (vis && (vis != STV_SINGLETON)) {
1171 				sym_undef_entry(ofl, sdp, BNDLOCAL);
1172 				ofl->ofl_flags |= FLG_OF_FATAL;
1173 				continue;
1174 			}
1175 		}
1176 
1177 		/*
1178 		 * If this symbol is defined in a non-allocatable section,
1179 		 * reduce it to local symbol.
1180 		 */
1181 		if (((isp = sdp->sd_isc) != 0) && isp->is_shdr &&
1182 		    ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) {
1183 			sdp->sd_flags |= FLG_SY_REDUCED;
1184 			sdp->sd_flags1 |= FLG_SY1_HIDDEN;
1185 		}
1186 
1187 		/*
1188 		 * If this symbol originated as a SHN_SUNW_IGNORE, it will have
1189 		 * been processed as an SHN_UNDEF.  Return the symbol to its
1190 		 * original index for validation, and propagation to the output
1191 		 * file.
1192 		 */
1193 		if (sdp->sd_flags1 & FLG_SY1_IGNORE)
1194 			sdp->sd_shndx = SHN_SUNW_IGNORE;
1195 
1196 		if (undef) {
1197 			/*
1198 			 * If a non-weak reference remains undefined, or if a
1199 			 * mapfile reference is not bound to the relocatable
1200 			 * objects that make up the object being built, we have
1201 			 * a fatal error.
1202 			 *
1203 			 * The exceptions are symbols which are defined to be
1204 			 * found in the parent (FLG_SY_PARENT), which is really
1205 			 * only meaningful for direct binding, or are defined
1206 			 * external (FLG_SY_EXTERN) so as to suppress -zdefs
1207 			 * errors.
1208 			 *
1209 			 * Register symbols are always allowed to be UNDEF.
1210 			 *
1211 			 * Note that we don't include references created via -u
1212 			 * in the same shared object binding test.  This is for
1213 			 * backward compatibility, in that a number of archive
1214 			 * makefile rules used -u to cause archive extraction.
1215 			 * These same rules have been cut and pasted to apply
1216 			 * to shared objects, and thus although the -u reference
1217 			 * is redundant, flagging it as fatal could cause some
1218 			 * build to fail.  Also we have documented the use of
1219 			 * -u as a mechanism to cause binding to weak version
1220 			 * definitions, thus giving users an error condition
1221 			 * would be incorrect.
1222 			 */
1223 			if (!(sdp->sd_flags & FLG_SY_REGSYM) &&
1224 			    ((sym->st_shndx == SHN_UNDEF) &&
1225 			    ((ELF_ST_BIND(sym->st_info) != STB_WEAK) &&
1226 			    ((sdp->sd_flags &
1227 			    (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) ||
1228 			    (((sdp->sd_flags &
1229 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
1230 			    FLG_SY_MAPREF) &&
1231 			    ((sdp->sd_flags1 & (FLG_SY1_HIDDEN |
1232 			    FLG_SY1_PROTECT)) == 0)))) {
1233 				sym_undef_entry(ofl, sdp, UNDEF);
1234 				ofl->ofl_flags |= undef;
1235 				undeferr = 1;
1236 			}
1237 
1238 		} else {
1239 			/*
1240 			 * For building things like shared objects (or anything
1241 			 * -znodefs), undefined symbols are allowed.
1242 			 *
1243 			 * If a mapfile reference remains undefined the user
1244 			 * would probably like a warning at least (they've
1245 			 * usually mis-spelt the reference).  Refer to the above
1246 			 * comments for discussion on -u references, which
1247 			 * are not tested for in the same manner.
1248 			 */
1249 			if ((sdp->sd_flags &
1250 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
1251 			    FLG_SY_MAPREF) {
1252 				sym_undef_entry(ofl, sdp, UNDEF);
1253 				ofl->ofl_flags |= FLG_OF_WARN;
1254 				undeferr = 1;
1255 			}
1256 		}
1257 
1258 		/*
1259 		 * If this symbol comes from a dependency mark the dependency
1260 		 * as required (-z ignore can result in unused dependencies
1261 		 * being dropped).  If we need to record dependency versioning
1262 		 * information indicate what version of the needed shared object
1263 		 * this symbol is part of.  Flag the symbol as undefined if it
1264 		 * has not been made available to us.
1265 		 */
1266 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1267 		    (!(sdp->sd_flags & FLG_SY_REFRSD))) {
1268 			sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD;
1269 
1270 			/*
1271 			 * Capture that we've bound to a symbol that doesn't
1272 			 * allow being directly bound to.
1273 			 */
1274 			if (sdp->sd_flags1 & FLG_SY1_NDIR)
1275 				ofl->ofl_flags1 |= FLG_OF1_NDIRECT;
1276 
1277 			if (sdp->sd_file->ifl_vercnt) {
1278 				int		vndx;
1279 				Ver_index	*vip;
1280 
1281 				vndx = sdp->sd_aux->sa_dverndx;
1282 				vip = &sdp->sd_file->ifl_verndx[vndx];
1283 				if (vip->vi_flags & FLG_VER_AVAIL) {
1284 					vip->vi_flags |= FLG_VER_REFER;
1285 				} else {
1286 					sym_undef_entry(ofl, sdp, NOTAVAIL);
1287 					ofl->ofl_flags |= FLG_OF_FATAL;
1288 					continue;
1289 				}
1290 			}
1291 		}
1292 
1293 		/*
1294 		 * Test that we do not bind to symbol supplied from an implicit
1295 		 * shared object.  If a binding is from a weak reference it can
1296 		 * be ignored.
1297 		 */
1298 		if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) &&
1299 		    (sdp->sd_ref == REF_DYN_NEED) &&
1300 		    (sdp->sd_flags & FLG_SY_NOTAVAIL)) {
1301 			sym_undef_entry(ofl, sdp, IMPLICIT);
1302 			ofl->ofl_flags |= needed;
1303 			continue;
1304 		}
1305 
1306 		/*
1307 		 * Test that a symbol isn't going to be reduced to local scope
1308 		 * which actually wants to bind to a shared object - if so it's
1309 		 * a fatal error.
1310 		 */
1311 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1312 		    (sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_PROTECT))) {
1313 			sym_undef_entry(ofl, sdp, BNDLOCAL);
1314 			ofl->ofl_flags |= FLG_OF_FATAL;
1315 			continue;
1316 		}
1317 
1318 		/*
1319 		 * If the output image is to be versioned then all symbol
1320 		 * definitions must be associated with a version.
1321 		 */
1322 		if (verdesc && (sdp->sd_ref == REF_REL_NEED) &&
1323 		    (sym->st_shndx != SHN_UNDEF) &&
1324 		    (!(sdp->sd_flags1 & FLG_SY1_HIDDEN)) &&
1325 		    (sdp->sd_aux->sa_overndx == 0)) {
1326 			sym_undef_entry(ofl, sdp, NOVERSION);
1327 			ofl->ofl_flags |= verdesc;
1328 			continue;
1329 		}
1330 
1331 		/*
1332 		 * If we don't need the symbol there's no need to process it
1333 		 * any further.
1334 		 */
1335 		if (sdp->sd_ref == REF_DYN_SEEN)
1336 			continue;
1337 
1338 		/*
1339 		 * Calculate the size and alignment requirements for the global
1340 		 * .bss and .tls sections.  If we're building a relocatable
1341 		 * object only account for scoped COMMON symbols (these will
1342 		 * be converted to .bss references).
1343 		 *
1344 		 * For partially initialized symbol,
1345 		 *  if it is expanded, it goes to sunwdata1.
1346 		 *  if it is local, it goes to .bss.
1347 		 *  if the output is shared object, it goes to .sunwbss.
1348 		 *
1349 		 * Also refer to make_mvsections() in sunwmove.c
1350 		 */
1351 		if ((sym->st_shndx == SHN_COMMON) &&
1352 		    (((oflags & FLG_OF_RELOBJ) == 0) ||
1353 		    ((sdp->sd_flags1 & FLG_SY1_HIDDEN) &&
1354 		    (oflags & FLG_OF_PROCRED)))) {
1355 			int countbss = 0;
1356 
1357 			if (sdp->sd_psyminfo == 0) {
1358 				countbss = 1;
1359 			} else if ((sdp->sd_flags & FLG_SY_PAREXPN) != 0) {
1360 				countbss = 0;
1361 			} else if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1362 				countbss = 1;
1363 			} else if ((ofl->ofl_flags & FLG_OF_SHAROBJ) != 0) {
1364 				countbss = 0;
1365 			} else
1366 				countbss = 1;
1367 
1368 			if (countbss) {
1369 				Xword * size, * align;
1370 
1371 				if (type != STT_TLS) {
1372 					size = &bsssize;
1373 					align = &bssalign;
1374 				} else {
1375 					size = &tlssize;
1376 					align = &tlsalign;
1377 				}
1378 				*size = (Xword)S_ROUND(*size, sym->st_value) +
1379 				    sym->st_size;
1380 				if (sym->st_value > *align)
1381 					*align = sym->st_value;
1382 			}
1383 		}
1384 
1385 #if	defined(__x86) && defined(_ELF64)
1386 		/*
1387 		 * Calculate the size and alignment requirement for the global
1388 		 * .lbss. TLS or partially initialized symbols do not need to be
1389 		 * considered yet.
1390 		 */
1391 		if (sym->st_shndx == SHN_X86_64_LCOMMON) {
1392 			lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) +
1393 			    sym->st_size;
1394 			if (sym->st_value > lbssalign)
1395 				lbssalign = sym->st_value;
1396 		}
1397 #endif
1398 
1399 		/*
1400 		 * If a symbol was referenced via the command line
1401 		 * (ld -u <>, ...), then this counts as a reference against the
1402 		 * symbol. Mark any section that symbol is defined in.
1403 		 */
1404 		if (((isp = sdp->sd_isc) != 0) &&
1405 		    (sdp->sd_flags & FLG_SY_CMDREF)) {
1406 			isp->is_flags |= FLG_IS_SECTREF;
1407 			isp->is_file->ifl_flags |= FLG_IF_FILEREF;
1408 		}
1409 
1410 		/*
1411 		 * Update the symbol count and the associated name string size.
1412 		 */
1413 		if ((sdp->sd_flags1 & (FLG_SY1_HIDDEN | FLG_SY1_ELIM)) &&
1414 		    (oflags & FLG_OF_PROCRED)) {
1415 			/*
1416 			 * If any reductions are being processed, keep a count
1417 			 * of eliminated symbols, and if the symbol is being
1418 			 * reduced to local, count it's size for the .symtab.
1419 			 */
1420 			if (sdp->sd_flags1 & FLG_SY1_ELIM) {
1421 				ofl->ofl_elimcnt++;
1422 			} else {
1423 				ofl->ofl_scopecnt++;
1424 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1425 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
1426 				    sdp->sd_name) == -1))
1427 					return (S_ERROR);
1428 				if (allow_ldynsym && sym->st_name &&
1429 				    ldynsym_symtype[type]) {
1430 					ofl->ofl_dynscopecnt++;
1431 					if (st_insert(ofl->ofl_dynstrtab,
1432 					    sdp->sd_name) == -1)
1433 						return (S_ERROR);
1434 					/* Include it in sort section? */
1435 					DYNSORT_COUNT(sdp, sym, type, ++);
1436 				}
1437 			}
1438 		} else {
1439 			ofl->ofl_globcnt++;
1440 
1441 			/*
1442 			 * Check to see if this global variable should
1443 			 * go into a sort section. Sort sections require
1444 			 * a .SUNW_ldynsym section, so, don't check
1445 			 * unless a .SUNW_ldynsym is allowed.
1446 			 */
1447 			if (allow_ldynsym) {
1448 				DYNSORT_COUNT(sdp, sym, type, ++);
1449 			}
1450 
1451 			/*
1452 			 * If global direct bindings are in effect, or this
1453 			 * symbol has bound to a dependency which was specified
1454 			 * as requiring direct bindings, and it hasn't
1455 			 * explicitly been defined as a non-direct binding
1456 			 * symbol, mark it.
1457 			 */
1458 			if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp &&
1459 			    (isp->is_file->ifl_flags & FLG_IF_DIRECT))) &&
1460 			    ((sdp->sd_flags1 & FLG_SY1_NDIR) == 0))
1461 				sdp->sd_flags1 |= FLG_SY1_DIR;
1462 
1463 			/*
1464 			 * Insert the symbol name.
1465 			 */
1466 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1467 			    sym->st_name) {
1468 				if (st_insert(ofl->ofl_strtab,
1469 				    sdp->sd_name) == -1)
1470 					return (S_ERROR);
1471 
1472 				if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
1473 				    (st_insert(ofl->ofl_dynstrtab,
1474 				    sdp->sd_name) == -1))
1475 					return (S_ERROR);
1476 			}
1477 
1478 			/*
1479 			 * If this section offers a global symbol - record that
1480 			 * fact.
1481 			 */
1482 			if (isp) {
1483 				isp->is_flags |= FLG_IS_SECTREF;
1484 				isp->is_file->ifl_flags |= FLG_IF_FILEREF;
1485 			}
1486 		}
1487 	}
1488 
1489 	/*
1490 	 * If we've encountered a fatal error during symbol validation then
1491 	 * return now.
1492 	 */
1493 	if (ofl->ofl_flags & FLG_OF_FATAL)
1494 		return (1);
1495 
1496 	/*
1497 	 * Now that symbol resolution is completed, scan any register symbols.
1498 	 * From now on, we're only interested in those that contribute to the
1499 	 * output file.
1500 	 */
1501 	if (ofl->ofl_regsyms) {
1502 		int	ndx;
1503 
1504 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1505 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1506 				continue;
1507 			if (sdp->sd_ref != REF_REL_NEED) {
1508 				ofl->ofl_regsyms[ndx] = 0;
1509 				continue;
1510 			}
1511 
1512 			ofl->ofl_regsymcnt++;
1513 			if (sdp->sd_sym->st_name == 0)
1514 				sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY);
1515 
1516 			if ((sdp->sd_flags1 & FLG_SY1_HIDDEN) ||
1517 			    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL))
1518 				ofl->ofl_lregsymcnt++;
1519 		}
1520 	}
1521 
1522 	/*
1523 	 * Generate the .bss section now that we know its size and alignment.
1524 	 */
1525 	if (bsssize || !(oflags & FLG_OF_RELOBJ)) {
1526 		if (ld_make_bss(ofl, bsssize, bssalign, MAKE_BSS) == S_ERROR)
1527 			return (S_ERROR);
1528 	}
1529 	if (tlssize) {
1530 		if (ld_make_bss(ofl, tlssize, tlsalign, MAKE_TLS) == S_ERROR)
1531 			return (S_ERROR);
1532 	}
1533 #if	defined(__x86) && defined(_ELF64)
1534 	if (lbsssize && !(oflags & FLG_OF_RELOBJ)) {
1535 		if (ld_make_bss(ofl, lbsssize, lbssalign, MAKE_LBSS) == S_ERROR)
1536 			return (S_ERROR);
1537 	}
1538 #endif
1539 
1540 	/*
1541 	 * Determine what entry point symbol we need, and if found save its
1542 	 * symbol descriptor so that we can update the ELF header entry with the
1543 	 * symbols value later (see update_oehdr).  Make sure the symbol is
1544 	 * tagged to ensure its update in case -s is in effect.  Use any -e
1545 	 * option first, or the default entry points `_start' and `main'.
1546 	 */
1547 	ret = 0;
1548 	if (ofl->ofl_entry) {
1549 		if ((sdp =
1550 		    ld_sym_find(ofl->ofl_entry, SYM_NOHASH, 0, ofl)) == NULL) {
1551 			eprintf(ofl->ofl_lml, ERR_FATAL,
1552 			    MSG_INTL(MSG_ARG_NOENTRY), ofl->ofl_entry);
1553 			ret++;
1554 		} else if (ensure_sym_local(ofl, sdp,
1555 		    MSG_INTL(MSG_SYM_ENTRY)) != 0) {
1556 			ret++;
1557 		} else {
1558 			ofl->ofl_entry = (void *)sdp;
1559 		}
1560 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START),
1561 	    SYM_NOHASH, 0, ofl)) != NULL) && (ensure_sym_local(ofl,
1562 	    sdp, 0) == 0)) {
1563 		ofl->ofl_entry = (void *)sdp;
1564 
1565 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN),
1566 	    SYM_NOHASH, 0, ofl)) != NULL) && (ensure_sym_local(ofl,
1567 	    sdp, 0) == 0)) {
1568 		ofl->ofl_entry = (void *)sdp;
1569 	}
1570 
1571 	/*
1572 	 * If ld -zdtrace=<sym> was given, then validate that the symbol is
1573 	 * defined within the current object being built.
1574 	 */
1575 	if ((sdp = ofl->ofl_dtracesym) != 0)
1576 		ret += ensure_sym_local(ofl, sdp, MSG_ORIG(MSG_STR_DTRACE));
1577 
1578 	/*
1579 	 * If any initarray, finiarray or preinitarray functions have been
1580 	 * requested, make sure they are defined within the current object
1581 	 * being built.
1582 	 */
1583 	if (ofl->ofl_initarray.head) {
1584 		ret += ensure_array_local(ofl, &ofl->ofl_initarray,
1585 		    MSG_ORIG(MSG_SYM_INITARRAY));
1586 	}
1587 	if (ofl->ofl_finiarray.head) {
1588 		ret += ensure_array_local(ofl, &ofl->ofl_finiarray,
1589 		    MSG_ORIG(MSG_SYM_FINIARRAY));
1590 	}
1591 	if (ofl->ofl_preiarray.head) {
1592 		ret += ensure_array_local(ofl, &ofl->ofl_preiarray,
1593 		    MSG_ORIG(MSG_SYM_PREINITARRAY));
1594 	}
1595 
1596 	if (ret)
1597 		return (S_ERROR);
1598 
1599 	/*
1600 	 * If we're required to record any needed dependencies versioning
1601 	 * information calculate it now that all symbols have been validated.
1602 	 */
1603 	if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
1604 		return (ld_vers_check_need(ofl));
1605 	else
1606 		return (1);
1607 }
1608 
1609 /*
1610  * qsort(3c) comparison function.  As an optimization for associating weak
1611  * symbols to their strong counterparts sort global symbols according to their
1612  * address and binding.
1613  */
1614 static int
1615 compare(const void * sdpp1, const void * sdpp2)
1616 {
1617 	Sym_desc *	sdp1 = *((Sym_desc **)sdpp1);
1618 	Sym_desc *	sdp2 = *((Sym_desc **)sdpp2);
1619 	Sym *		sym1, * sym2;
1620 	uchar_t		bind1, bind2;
1621 
1622 	/*
1623 	 * Symbol descriptors may be zero, move these to the front of the
1624 	 * sorted array.
1625 	 */
1626 	if (sdp1 == 0)
1627 		return (-1);
1628 	if (sdp2 == 0)
1629 		return (1);
1630 
1631 	sym1 = sdp1->sd_sym;
1632 	sym2 = sdp2->sd_sym;
1633 
1634 	/*
1635 	 * Compare the symbols value (address).
1636 	 */
1637 	if (sym1->st_value > sym2->st_value)
1638 		return (1);
1639 	if (sym1->st_value < sym2->st_value)
1640 		return (-1);
1641 
1642 	bind1 = ELF_ST_BIND(sym1->st_info);
1643 	bind2 = ELF_ST_BIND(sym2->st_info);
1644 
1645 	/*
1646 	 * If two symbols have the same address place the weak symbol before
1647 	 * any strong counterpart.
1648 	 */
1649 	if (bind1 > bind2)
1650 		return (-1);
1651 	if (bind1 < bind2)
1652 		return (1);
1653 
1654 	return (0);
1655 }
1656 
1657 
1658 /*
1659  * Issue a MSG_SYM_BADADDR error from ld_sym_process(). This error
1660  * is issued when a symbol address/size is not contained by the
1661  * target section.
1662  *
1663  * Such objects are at least partially corrupt, and the user would
1664  * be well advised to be skeptical of them, and to ask their compiler
1665  * supplier to fix the problem. However, a distinction needs to be
1666  * made between symbols that reference readonly text, and those that
1667  * access writable data. Other than throwing off profiling results,
1668  * the readonly section case is less serious. We have encountered
1669  * such objects in the field. In order to allow existing objects
1670  * to continue working, we issue a warning rather than a fatal error
1671  * if the symbol is against readonly text. Other cases are fatal.
1672  */
1673 static void
1674 issue_badaddr_msg(Ifl_desc *ifl, Ofl_desc *ofl, Sym_desc *sdp,
1675     Sym *sym, Word shndx)
1676 {
1677 	Lword	flag;
1678 	Error	err;
1679 	const char *msg;
1680 
1681 	if ((sdp->sd_isc->is_shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) ==
1682 	    SHF_ALLOC) {
1683 		msg = MSG_INTL(MSG_SYM_BADADDR_ROTXT);
1684 		flag = FLG_OF_WARN;
1685 		err = ERR_WARNING;
1686 	} else {
1687 		msg = MSG_INTL(MSG_SYM_BADADDR);
1688 		flag = FLG_OF_FATAL;
1689 		err = ERR_FATAL;
1690 	}
1691 
1692 	eprintf(ofl->ofl_lml, err, msg, demangle(sdp->sd_name),
1693 	    ifl->ifl_name, shndx, sdp->sd_isc->is_name,
1694 	    EC_XWORD(sdp->sd_isc->is_shdr->sh_size),
1695 	    EC_XWORD(sym->st_value), EC_XWORD(sym->st_size));
1696 	ofl->ofl_flags |= flag;
1697 }
1698 
1699 
1700 /*
1701  * Process the symbol table for the specified input file.  At this point all
1702  * input sections from this input file have been assigned an input section
1703  * descriptor which is saved in the `ifl_isdesc' array.
1704  *
1705  *	o	local symbols are saved (as is) if the input file is a
1706  *		relocatable object
1707  *
1708  *	o	global symbols are added to the linkers internal symbol
1709  *		table if they are not already present, otherwise a symbol
1710  *		resolution function is called upon to resolve the conflict.
1711  */
1712 uintptr_t
1713 ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1714 {
1715 	/*
1716 	 * This macro tests the given symbol to see if it is out of
1717 	 * range relative to the section it references.
1718 	 *
1719 	 * entry:
1720 	 *	- ifl is a relative object (ET_REL)
1721 	 *	_sdp - Symbol descriptor
1722 	 *	_sym - Symbol
1723 	 *	_type - Symbol type
1724 	 *
1725 	 * The following are tested:
1726 	 *	- Symbol length is non-zero
1727 	 *	- Symbol type is a type that references code or data
1728 	 *	- Referenced section is not 0 (indicates an UNDEF symbol)
1729 	 *	  and is not in the range of special values above SHN_LORESERVE
1730 	 *	  (excluding SHN_XINDEX, which is OK).
1731 	 *	- We have a valid section header for the target section
1732 	 *
1733 	 * If the above are all true, and the symbol position is not
1734 	 * contained by the target section, this macro evaluates to
1735 	 * True (1). Otherwise, False(0).
1736 	 */
1737 #define	SYM_LOC_BADADDR(_sdp, _sym, _type) \
1738 	(_sym->st_size && dynsymsort_symtype[_type] && \
1739 	(_sym->st_shndx != SHN_UNDEF) && \
1740 	((_sym->st_shndx < SHN_LORESERVE) || \
1741 		(_sym->st_shndx == SHN_XINDEX)) && \
1742 	_sdp->sd_isc && _sdp->sd_isc->is_shdr && \
1743 	((_sym->st_value + _sym->st_size) > _sdp->sd_isc->is_shdr->sh_size))
1744 
1745 	Conv_inv_buf_t	inv_buf;
1746 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
1747 	Word		*symshndx = 0;
1748 	Shdr		*shdr = isc->is_shdr;
1749 	Sym_desc	*sdp;
1750 	size_t		strsize;
1751 	char		*strs;
1752 	uchar_t		type, bind;
1753 	Word		ndx, hash, local, total;
1754 	Half		etype = ifl->ifl_ehdr->e_type;
1755 	int		etype_rel;
1756 	const char	*symsecname, *strsecname;
1757 	avl_index_t	where;
1758 	int		test_gnu_hidden_bit;
1759 
1760 	/*
1761 	 * Its possible that a file may contain more that one symbol table,
1762 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
1763 	 * table (here, we assume .dynsym comes before .symtab).
1764 	 */
1765 	if (ifl->ifl_symscnt)
1766 		return (1);
1767 
1768 	if (isc->is_symshndx)
1769 		symshndx = isc->is_symshndx->is_indata->d_buf;
1770 
1771 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
1772 
1773 	if (isc->is_name)
1774 		symsecname = isc->is_name;
1775 	else
1776 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
1777 
1778 	/*
1779 	 * From the symbol tables section header information determine which
1780 	 * strtab table is needed to locate the actual symbol names.
1781 	 */
1782 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
1783 		ndx = shdr->sh_link;
1784 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
1785 			eprintf(ofl->ofl_lml, ERR_FATAL,
1786 			    MSG_INTL(MSG_FIL_INVSHLINK),
1787 			    ifl->ifl_name, symsecname, EC_XWORD(ndx));
1788 			return (S_ERROR);
1789 		}
1790 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
1791 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
1792 		if (ifl->ifl_isdesc[ndx]->is_name)
1793 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
1794 		else
1795 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
1796 	} else {
1797 		/*
1798 		 * There is no string table section in this input file
1799 		 * although there are symbols in this symbol table section.
1800 		 * This means that these symbols do not have names.
1801 		 * Currently, only scratch register symbols are allowed
1802 		 * not to have names.
1803 		 */
1804 		strsize = 0;
1805 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
1806 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
1807 	}
1808 
1809 	/*
1810 	 * Determine the number of local symbols together with the total
1811 	 * number we have to process.
1812 	 */
1813 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
1814 	local = shdr->sh_info;
1815 
1816 	/*
1817 	 * Allocate a symbol table index array and a local symbol array
1818 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
1819 	 * array).  If we are dealing with a relocatable object, allocate the
1820 	 * local symbol descriptors.  If this isn't a relocatable object we
1821 	 * still have to process any shared object locals to determine if any
1822 	 * register symbols exist.  Although these aren't added to the output
1823 	 * image, they are used as part of symbol resolution.
1824 	 */
1825 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
1826 	    sizeof (Sym_desc *)))) == 0)
1827 		return (S_ERROR);
1828 	etype_rel = (etype == ET_REL);
1829 	if (etype_rel && local) {
1830 		if ((ifl->ifl_locs =
1831 		    libld_calloc(sizeof (Sym_desc), local)) == 0)
1832 			return (S_ERROR);
1833 		/* LINTED */
1834 		ifl->ifl_locscnt = (Word)local;
1835 	}
1836 	ifl->ifl_symscnt = total;
1837 
1838 	/*
1839 	 * If there are local symbols to save add them to the symbol table
1840 	 * index array.
1841 	 */
1842 	if (local) {
1843 		int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1844 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
1845 			Word		shndx, sdflags = FLG_SY_CLEAN;
1846 			const char	*name;
1847 			Sym_desc	*rsdp;
1848 
1849 			/*
1850 			 * Determine the associated section index.
1851 			 */
1852 			if (symshndx && (sym->st_shndx == SHN_XINDEX))
1853 				shndx = symshndx[ndx];
1854 			else if ((shndx = sym->st_shndx) >= SHN_LORESERVE)
1855 				sdflags |= FLG_SY_SPECSEC;
1856 
1857 			/*
1858 			 * Check if st_name has a valid value or not.
1859 			 */
1860 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
1861 			    shndx, symsecname, strsecname, &sdflags)) == 0) {
1862 				ofl->ofl_flags |= FLG_OF_FATAL;
1863 				continue;
1864 			}
1865 
1866 			/*
1867 			 * If this local symbol table originates from a shared
1868 			 * object, then we're only interested in recording
1869 			 * register symbols.  As local symbol descriptors aren't
1870 			 * allocated for shared objects, one will be allocated
1871 			 * to associated with the register symbol.  This symbol
1872 			 * won't become part of the output image, but we must
1873 			 * process it to test for register conflicts.
1874 			 */
1875 			rsdp = sdp = 0;
1876 			if (sdflags & FLG_SY_REGSYM) {
1877 				if ((rsdp = ld_reg_find(sym, ofl)) != 0) {
1878 					/*
1879 					 * The fact that another register def-
1880 					 * inition has been found is fatal.
1881 					 * Call the verification routine to get
1882 					 * the error message and move on.
1883 					 */
1884 					(void) ld_reg_check(rsdp, sym, name,
1885 					    ifl, ofl);
1886 					continue;
1887 				}
1888 
1889 				if (etype == ET_DYN) {
1890 					if ((sdp = libld_calloc(
1891 					    sizeof (Sym_desc), 1)) == 0)
1892 						return (S_ERROR);
1893 					sdp->sd_ref = REF_DYN_SEEN;
1894 				}
1895 			} else if (etype == ET_DYN)
1896 				continue;
1897 
1898 			/*
1899 			 * Fill in the remaining symbol descriptor information.
1900 			 */
1901 			if (sdp == 0) {
1902 				sdp = &(ifl->ifl_locs[ndx]);
1903 				sdp->sd_ref = REF_REL_NEED;
1904 			}
1905 			if (rsdp == 0) {
1906 				sdp->sd_name = name;
1907 				sdp->sd_sym = sym;
1908 				sdp->sd_shndx = shndx;
1909 				sdp->sd_flags = sdflags;
1910 				sdp->sd_file = ifl;
1911 				ifl->ifl_oldndx[ndx] = sdp;
1912 			}
1913 
1914 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
1915 
1916 			/*
1917 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
1918 			 * so as to simplify future processing.
1919 			 */
1920 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
1921 				sdp->sd_shndx = shndx = SHN_UNDEF;
1922 				sdp->sd_flags1 |=
1923 				    (FLG_SY1_IGNORE | FLG_SY1_ELIM);
1924 			}
1925 
1926 			/*
1927 			 * Process any register symbols.
1928 			 */
1929 			if (sdp->sd_flags & FLG_SY_REGSYM) {
1930 				/*
1931 				 * Add a diagnostic to indicate we've caught a
1932 				 * register symbol, as this can be useful if a
1933 				 * register conflict is later discovered.
1934 				 */
1935 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
1936 
1937 				/*
1938 				 * If this register symbol hasn't already been
1939 				 * recorded, enter it now.
1940 				 */
1941 				if ((rsdp == 0) &&
1942 				    (ld_reg_enter(sdp, ofl) == 0))
1943 					return (S_ERROR);
1944 			}
1945 
1946 			/*
1947 			 * Assign an input section.
1948 			 */
1949 			if ((sym->st_shndx != SHN_UNDEF) &&
1950 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
1951 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
1952 
1953 			/*
1954 			 * If this symbol falls within the range of a section
1955 			 * being discarded, then discard the symbol itself.
1956 			 * There is no reason to keep this local symbol.
1957 			 */
1958 			if (sdp->sd_isc &&
1959 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
1960 				sdp->sd_flags |= FLG_SY_ISDISC;
1961 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml,
1962 				    sdp, sdp->sd_isc));
1963 				continue;
1964 			}
1965 
1966 			/*
1967 			 * Skip any section symbols as new versions of these
1968 			 * will be created.
1969 			 */
1970 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
1971 				if (sym->st_shndx == SHN_UNDEF) {
1972 					eprintf(ofl->ofl_lml, ERR_WARNING,
1973 					    MSG_INTL(MSG_SYM_INVSHNDX),
1974 					    demangle(sdp->sd_name),
1975 					    ifl->ifl_name,
1976 					    conv_sym_shndx(sym->st_shndx,
1977 					    &inv_buf));
1978 				}
1979 				continue;
1980 			}
1981 
1982 			/*
1983 			 * For a relocatable object, if this symbol is defined
1984 			 * and has non-zero length and references an address
1985 			 * within an associated section, then check its extents
1986 			 * to make sure the section boundaries encompass it.
1987 			 * If they don't, the ELF file is corrupt.
1988 			 */
1989 			if (etype_rel && SYM_LOC_BADADDR(sdp, sym, type)) {
1990 				issue_badaddr_msg(ifl, ofl, sdp, sym, shndx);
1991 				continue;
1992 			}
1993 
1994 			/*
1995 			 * Sanity check for TLS
1996 			 */
1997 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
1998 			    (sym->st_shndx != SHN_COMMON))) {
1999 				Is_desc	*isp = sdp->sd_isc;
2000 
2001 				if ((isp == 0) || (isp->is_shdr == 0) ||
2002 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
2003 					eprintf(ofl->ofl_lml, ERR_FATAL,
2004 					    MSG_INTL(MSG_SYM_TLS),
2005 					    demangle(sdp->sd_name),
2006 					    ifl->ifl_name);
2007 					ofl->ofl_flags |= FLG_OF_FATAL;
2008 					continue;
2009 				}
2010 			}
2011 
2012 			/*
2013 			 * Carry our some basic sanity checks (these are just
2014 			 * some of the erroneous symbol entries we've come
2015 			 * across, there's probably a lot more).  The symbol
2016 			 * will not be carried forward to the output file, which
2017 			 * won't be a problem unless a relocation is required
2018 			 * against it.
2019 			 */
2020 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
2021 			    ((sym->st_shndx == SHN_COMMON)) ||
2022 			    ((type == STT_FILE) &&
2023 			    (sym->st_shndx != SHN_ABS))) ||
2024 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == 0))) {
2025 				eprintf(ofl->ofl_lml, ERR_WARNING,
2026 				    MSG_INTL(MSG_SYM_INVSHNDX),
2027 				    demangle(sdp->sd_name), ifl->ifl_name,
2028 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
2029 				sdp->sd_isc = NULL;
2030 				sdp->sd_flags |= FLG_SY_INVALID;
2031 				continue;
2032 			}
2033 
2034 			/*
2035 			 * As these local symbols will become part of the output
2036 			 * image, record their number and name string size.
2037 			 * Globals are counted after all input file processing
2038 			 * (and hence symbol resolution) is complete during
2039 			 * sym_validate().
2040 			 */
2041 			if (!(ofl->ofl_flags1 & FLG_OF1_REDLSYM)) {
2042 				ofl->ofl_locscnt++;
2043 
2044 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
2045 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
2046 				    sdp->sd_name) == -1))
2047 					return (S_ERROR);
2048 
2049 				if (allow_ldynsym && sym->st_name &&
2050 				    ldynsym_symtype[type]) {
2051 					ofl->ofl_dynlocscnt++;
2052 					if (st_insert(ofl->ofl_dynstrtab,
2053 					    sdp->sd_name) == -1)
2054 						return (S_ERROR);
2055 					/* Include it in sort section? */
2056 					DYNSORT_COUNT(sdp, sym, type, ++);
2057 				}
2058 			}
2059 		}
2060 	}
2061 
2062 	/*
2063 	 * The GNU ld interprets the top bit of the 16-bit Versym value
2064 	 * (0x8000) as the "hidden" bit. If this bit is set, the linker
2065 	 * is supposed to act as if that symbol does not exist. The Solaris
2066 	 * linker does not support this mechanism, or the model of interface
2067 	 * evolution that it allows, but we honor it in GNU ld produced
2068 	 * objects in order to interoperate with them.
2069 	 *
2070 	 * Determine if we should honor the GNU hidden bit for this file.
2071 	 */
2072 	test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) &&
2073 	    (ifl->ifl_versym != NULL);
2074 
2075 	/*
2076 	 * Now scan the global symbols entering them in the internal symbol
2077 	 * table or resolving them as necessary.
2078 	 */
2079 	sym = (Sym *)isc->is_indata->d_buf;
2080 	sym += local;
2081 	/* LINTED */
2082 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
2083 		const char	*name;
2084 		Word		shndx, sdflags = 0;
2085 
2086 		/*
2087 		 * Determine the associated section index.
2088 		 */
2089 		if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
2090 			shndx = symshndx[ndx];
2091 		} else {
2092 			shndx = sym->st_shndx;
2093 			if (sym->st_shndx >= SHN_LORESERVE)
2094 				sdflags |= FLG_SY_SPECSEC;
2095 		}
2096 
2097 		/*
2098 		 * Check if st_name has a valid value or not.
2099 		 */
2100 		if ((name = string(ofl, ifl, sym, strs, strsize, ndx, shndx,
2101 		    symsecname, strsecname, &sdflags)) == 0) {
2102 			ofl->ofl_flags |= FLG_OF_FATAL;
2103 			continue;
2104 		}
2105 
2106 		/*
2107 		 * Test for the GNU hidden bit, and ignore symbols that
2108 		 * have it set.
2109 		 */
2110 		if (test_gnu_hidden_bit &&
2111 		    ((ifl->ifl_versym[ndx] & 0x8000) != 0))
2112 			continue;
2113 
2114 		/*
2115 		 * The linker itself will generate symbols for _end, _etext,
2116 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
2117 		 * bother entering these symbols from shared objects.  This
2118 		 * results in some wasted resolution processing, which is hard
2119 		 * to feel, but if nothing else, pollutes diagnostic relocation
2120 		 * output.
2121 		 */
2122 		if (name[0] && (etype == ET_DYN) && (sym->st_size == 0) &&
2123 		    (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) &&
2124 		    (name[0] == '_') && ((name[1] == 'e') ||
2125 		    (name[1] == 'D') || (name[1] == 'P')) &&
2126 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
2127 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
2128 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
2129 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
2130 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
2131 			ifl->ifl_oldndx[ndx] = 0;
2132 			continue;
2133 		}
2134 
2135 		/*
2136 		 * Determine and validate the symbols binding.
2137 		 */
2138 		bind = ELF_ST_BIND(sym->st_info);
2139 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
2140 			eprintf(ofl->ofl_lml, ERR_WARNING,
2141 			    MSG_INTL(MSG_SYM_NONGLOB), demangle(name),
2142 			    ifl->ifl_name,
2143 			    conv_sym_info_bind(bind, 0, &inv_buf));
2144 			continue;
2145 		}
2146 
2147 		/*
2148 		 * If this symbol falls within the range of a section being
2149 		 * discarded, then discard the symbol itself.
2150 		 */
2151 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
2152 		    (sym->st_shndx != SHN_UNDEF)) {
2153 			Is_desc	*isp;
2154 
2155 			if (shndx >= ifl->ifl_shnum) {
2156 				/*
2157 				 * Carry our some basic sanity checks
2158 				 * The symbol will not be carried forward to
2159 				 * the output file, which won't be a problem
2160 				 * unless a relocation is required against it.
2161 				 */
2162 				eprintf(ofl->ofl_lml, ERR_WARNING,
2163 				    MSG_INTL(MSG_SYM_INVSHNDX), demangle(name),
2164 				    ifl->ifl_name,
2165 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
2166 				continue;
2167 			}
2168 
2169 			isp = ifl->ifl_isdesc[shndx];
2170 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
2171 				if ((sdp =
2172 				    libld_calloc(sizeof (Sym_desc), 1)) == 0)
2173 					return (S_ERROR);
2174 
2175 				/*
2176 				 * Create a dummy symbol entry so that if we
2177 				 * find any references to this discarded symbol
2178 				 * we can compensate.
2179 				 */
2180 				sdp->sd_name = name;
2181 				sdp->sd_sym = sym;
2182 				sdp->sd_file = ifl;
2183 				sdp->sd_isc = isp;
2184 				sdp->sd_flags = FLG_SY_ISDISC;
2185 				ifl->ifl_oldndx[ndx] = sdp;
2186 
2187 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp,
2188 				    sdp->sd_isc));
2189 				continue;
2190 			}
2191 		}
2192 
2193 		/*
2194 		 * If the symbol does not already exist in the internal symbol
2195 		 * table add it, otherwise resolve the conflict.  If the symbol
2196 		 * from this file is kept, retain its symbol table index for
2197 		 * possible use in associating a global alias.
2198 		 */
2199 		/* LINTED */
2200 		hash = (Word)elf_hash((const char *)name);
2201 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
2202 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
2203 			if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, ndx,
2204 			    shndx, sdflags, 0, &where)) == (Sym_desc *)S_ERROR)
2205 				return (S_ERROR);
2206 
2207 		} else if (ld_sym_resolve(sdp, sym, ifl, ofl, ndx, shndx,
2208 		    sdflags) == S_ERROR)
2209 			return (S_ERROR);
2210 
2211 		/*
2212 		 * After we've compared a defined symbol in one shared
2213 		 * object, flag the symbol so we don't compare it again.
2214 		 */
2215 		if ((etype == ET_DYN) && (sym->st_shndx != SHN_UNDEF) &&
2216 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
2217 			sdp->sd_flags |= FLG_SY_SOFOUND;
2218 
2219 		/*
2220 		 * If the symbol is accepted from this file retain the symbol
2221 		 * index for possible use in aliasing.
2222 		 */
2223 		if (sdp->sd_file == ifl)
2224 			sdp->sd_symndx = ndx;
2225 
2226 		ifl->ifl_oldndx[ndx] = sdp;
2227 
2228 		/*
2229 		 * If we've accepted a register symbol, continue to validate
2230 		 * it.
2231 		 */
2232 		if (sdp->sd_flags & FLG_SY_REGSYM) {
2233 			Sym_desc	*rsdp;
2234 
2235 			if ((rsdp = ld_reg_find(sdp->sd_sym, ofl)) == 0) {
2236 				if (ld_reg_enter(sdp, ofl) == 0)
2237 					return (S_ERROR);
2238 			} else if (rsdp != sdp) {
2239 				(void) ld_reg_check(rsdp, sdp->sd_sym,
2240 				    sdp->sd_name, ifl, ofl);
2241 			}
2242 		}
2243 
2244 		/*
2245 		 * For a relocatable object, if this symbol is defined
2246 		 * and has non-zero length and references an address
2247 		 * within an associated section, then check its extents
2248 		 * to make sure the section boundaries encompass it.
2249 		 * If they don't, the ELF file is corrupt. Note that this
2250 		 * global symbol may have come from another file to satisfy
2251 		 * an UNDEF symbol of the same name from this one. In that
2252 		 * case, we don't check it, because it was already checked
2253 		 * as part of its own file.
2254 		 */
2255 		if (etype_rel && (sdp->sd_file == ifl)) {
2256 			Sym *tsym = sdp->sd_sym;
2257 
2258 			if (SYM_LOC_BADADDR(sdp, tsym,
2259 			    ELF_ST_TYPE(tsym->st_info))) {
2260 				issue_badaddr_msg(ifl, ofl, sdp,
2261 				    tsym, tsym->st_shndx);
2262 				continue;
2263 			}
2264 		}
2265 	}
2266 
2267 	/*
2268 	 * If this is a shared object scan the globals one more time and
2269 	 * associate any weak/global associations.  This association is needed
2270 	 * should the weak definition satisfy a reference in the dynamic
2271 	 * executable:
2272 	 *
2273 	 *  o	if the symbol is a data item it will be copied to the
2274 	 *	executables address space, thus we must also reassociate the
2275 	 *	alias symbol with its new location in the executable.
2276 	 *
2277 	 *  o	if the symbol is a function then we may need to promote	the
2278 	 *	symbols binding from undefined weak to undefined, otherwise the
2279 	 *	run-time linker will not generate the correct relocation error
2280 	 *	should the symbol not be found.
2281 	 *
2282 	 * The true association between a weak/strong symbol pair is that both
2283 	 * symbol entries are identical, thus first we created a sorted symbol
2284 	 * list keyed off of the symbols value (if the value is the same chances
2285 	 * are the rest of the symbols data is).  This list is then scanned for
2286 	 * weak symbols, and if one is found then any strong association will
2287 	 * exist in the following entries.  Thus we just have to scan one
2288 	 * (typical single alias) or more (in the uncommon instance of multiple
2289 	 * weak to strong associations) entries to determine if a match exists.
2290 	 */
2291 	if ((OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) &&
2292 	    (total > local)) {
2293 		Sym_desc **	sort;
2294 		size_t		size = (total - local) * sizeof (Sym_desc *);
2295 
2296 		if ((sort = libld_malloc(size)) == 0)
2297 			return (S_ERROR);
2298 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], size);
2299 
2300 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
2301 
2302 		for (ndx = 0; ndx < (total - local); ndx++) {
2303 			Sym_desc *	wsdp = sort[ndx];
2304 			Sym *		wsym;
2305 			int		sndx;
2306 
2307 			if (wsdp == 0)
2308 				continue;
2309 
2310 			wsym = wsdp->sd_sym;
2311 
2312 			if ((ELF_ST_BIND(wsym->st_info) != STB_WEAK) ||
2313 			    (wsdp->sd_sym->st_shndx == SHN_UNDEF) ||
2314 			    (wsdp->sd_flags & FLG_SY_SPECSEC))
2315 				continue;
2316 
2317 			/*
2318 			 * We have a weak symbol, if it has a strong alias it
2319 			 * will have been sorted to one of the following sort
2320 			 * table entries.  Note that we could have multiple weak
2321 			 * symbols aliased to one strong (if this occurs then
2322 			 * the strong symbol only maintains one alias back to
2323 			 * the last weak).
2324 			 */
2325 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
2326 				Sym_desc *	ssdp = sort[sndx];
2327 				Sym *		ssym;
2328 
2329 				if (ssdp == 0)
2330 					break;
2331 
2332 				ssym = ssdp->sd_sym;
2333 
2334 				if (wsym->st_value != ssym->st_value)
2335 					break;
2336 
2337 				if ((ssdp->sd_file == ifl) &&
2338 				    (wsdp->sd_file == ifl) &&
2339 				    (wsym->st_size == ssym->st_size) &&
2340 				    (ssdp->sd_sym->st_shndx != SHN_UNDEF) &&
2341 				    (ELF_ST_BIND(ssym->st_info) != STB_WEAK) &&
2342 				    ((ssdp->sd_flags & FLG_SY_SPECSEC) == 0)) {
2343 					int w_dynbits, s_dynbits;
2344 
2345 					/*
2346 					 * If a sharable object, set link
2347 					 * fields so they reference each other
2348 					 */
2349 					if (etype == ET_DYN) {
2350 						ssdp->sd_aux->sa_linkndx =
2351 						    (Word)wsdp->sd_symndx;
2352 						wsdp->sd_aux->sa_linkndx =
2353 						    (Word)ssdp->sd_symndx;
2354 					}
2355 					/*
2356 					 * Determine which of these two symbols
2357 					 * go into the sort section. If the
2358 					 * mapfile has made explicit settings
2359 					 * of the FLG_SY_*DYNSORT flags for both
2360 					 * symbols, then we do what they say.
2361 					 * If one has the DYNSORT flags set,
2362 					 * we set the NODYNSORT bit in the
2363 					 * other. And if neither has an
2364 					 * explicit setting, then we favor the
2365 					 * weak symbol because they usually
2366 					 * lack the leading underscore.
2367 					 */
2368 					w_dynbits = wsdp->sd_flags &
2369 					    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2370 					s_dynbits = ssdp->sd_flags &
2371 					    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2372 					if (!(w_dynbits && s_dynbits)) {
2373 						if (s_dynbits) {
2374 							if (s_dynbits ==
2375 							    FLG_SY_DYNSORT)
2376 							wsdp->sd_flags |=
2377 							    FLG_SY_NODYNSORT;
2378 						} else if (w_dynbits !=
2379 						    FLG_SY_NODYNSORT) {
2380 							ssdp->sd_flags |=
2381 							    FLG_SY_NODYNSORT;
2382 						}
2383 					}
2384 					break;
2385 				}
2386 			}
2387 		}
2388 	}
2389 	return (1);
2390 
2391 #undef SYM_LOC_BADADDR
2392 }
2393 
2394 /*
2395  * Add an undefined symbol to the symbol table.  The reference originates from
2396  * the location identifed by the message id (mid).  These references can
2397  * originate from command line options such as -e, -u, -initarray, etc.
2398  * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated
2399  * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)).
2400  */
2401 Sym_desc *
2402 ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid)
2403 {
2404 	Sym		*sym;
2405 	Ifl_desc	*ifl = 0, *_ifl;
2406 	Sym_desc	*sdp;
2407 	Word		hash;
2408 	Listnode	*lnp;
2409 	avl_index_t	where;
2410 	const char	*reference = MSG_INTL(mid);
2411 
2412 	/*
2413 	 * As an optimization, determine whether we've already generated this
2414 	 * reference.  If the symbol doesn't already exist we'll create it.
2415 	 * Or if the symbol does exist from a different source, we'll resolve
2416 	 * the conflict.
2417 	 */
2418 	/* LINTED */
2419 	hash = (Word)elf_hash(name);
2420 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) {
2421 		if ((sdp->sd_sym->st_shndx == SHN_UNDEF) &&
2422 		    (sdp->sd_file->ifl_name == reference))
2423 			return (sdp);
2424 	}
2425 
2426 	/*
2427 	 * Determine whether a pseudo input file descriptor exists to represent
2428 	 * the command line, as any global symbol needs an input file descriptor
2429 	 * during any symbol resolution (refer to map_ifl() which provides a
2430 	 * similar method for adding symbols from mapfiles).
2431 	 */
2432 	for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, _ifl))
2433 		if (strcmp(_ifl->ifl_name, reference) == 0) {
2434 			ifl = _ifl;
2435 			break;
2436 		}
2437 
2438 	/*
2439 	 * If no descriptor exists create one.
2440 	 */
2441 	if (ifl == 0) {
2442 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) ==
2443 		    (Ifl_desc *)0)
2444 			return ((Sym_desc *)S_ERROR);
2445 		ifl->ifl_name = reference;
2446 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
2447 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr),
2448 		    1)) == 0)
2449 			return ((Sym_desc *)S_ERROR);
2450 		ifl->ifl_ehdr->e_type = ET_REL;
2451 
2452 		if (list_appendc(&ofl->ofl_objs, ifl) == 0)
2453 			return ((Sym_desc *)S_ERROR);
2454 	}
2455 
2456 	/*
2457 	 * Allocate a symbol structure and add it to the global symbol table.
2458 	 */
2459 	if ((sym = libld_calloc(sizeof (Sym), 1)) == 0)
2460 		return ((Sym_desc *)S_ERROR);
2461 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
2462 	sym->st_shndx = SHN_UNDEF;
2463 
2464 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
2465 	if (sdp == NULL) {
2466 		DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
2467 		if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
2468 		    0, 0, &where)) == (Sym_desc *)S_ERROR)
2469 			return ((Sym_desc *)S_ERROR);
2470 	} else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0,
2471 	    SHN_UNDEF, 0) == S_ERROR)
2472 		return ((Sym_desc *)S_ERROR);
2473 
2474 	sdp->sd_flags &= ~FLG_SY_CLEAN;
2475 	sdp->sd_flags |= FLG_SY_CMDREF;
2476 
2477 	return (sdp);
2478 }
2479