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