xref: /illumos-gate/usr/src/cmd/sgs/libld/common/syms.c (revision 4703203d)
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 	Conv_inv_buf_t	inv_buf;
1701 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
1702 	Word		*symshndx = 0;
1703 	Shdr		*shdr = isc->is_shdr;
1704 	Sym_desc	*sdp;
1705 	size_t		strsize;
1706 	char		*strs;
1707 	uchar_t		type, bind;
1708 	Word		ndx, hash, local, total;
1709 	Half		etype = ifl->ifl_ehdr->e_type;
1710 	int		etype_rel;
1711 	const char	*symsecname, *strsecname;
1712 	avl_index_t	where;
1713 	int		test_gnu_hidden_bit;
1714 
1715 	/*
1716 	 * Its possible that a file may contain more that one symbol table,
1717 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
1718 	 * table (here, we assume .dynsym comes before .symtab).
1719 	 */
1720 	if (ifl->ifl_symscnt)
1721 		return (1);
1722 
1723 	if (isc->is_symshndx)
1724 		symshndx = isc->is_symshndx->is_indata->d_buf;
1725 
1726 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
1727 
1728 	if (isc->is_name)
1729 		symsecname = isc->is_name;
1730 	else
1731 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
1732 
1733 	/*
1734 	 * From the symbol tables section header information determine which
1735 	 * strtab table is needed to locate the actual symbol names.
1736 	 */
1737 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
1738 		ndx = shdr->sh_link;
1739 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
1740 			eprintf(ofl->ofl_lml, ERR_FATAL,
1741 			    MSG_INTL(MSG_FIL_INVSHLINK),
1742 			    ifl->ifl_name, symsecname, EC_XWORD(ndx));
1743 			return (S_ERROR);
1744 		}
1745 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
1746 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
1747 		if (ifl->ifl_isdesc[ndx]->is_name)
1748 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
1749 		else
1750 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
1751 	} else {
1752 		/*
1753 		 * There is no string table section in this input file
1754 		 * although there are symbols in this symbol table section.
1755 		 * This means that these symbols do not have names.
1756 		 * Currently, only scratch register symbols are allowed
1757 		 * not to have names.
1758 		 */
1759 		strsize = 0;
1760 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
1761 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
1762 	}
1763 
1764 	/*
1765 	 * Determine the number of local symbols together with the total
1766 	 * number we have to process.
1767 	 */
1768 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
1769 	local = shdr->sh_info;
1770 
1771 	/*
1772 	 * Allocate a symbol table index array and a local symbol array
1773 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
1774 	 * array).  If we are dealing with a relocatable object, allocate the
1775 	 * local symbol descriptors.  If this isn't a relocatable object we
1776 	 * still have to process any shared object locals to determine if any
1777 	 * register symbols exist.  Although these aren't added to the output
1778 	 * image, they are used as part of symbol resolution.
1779 	 */
1780 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
1781 	    sizeof (Sym_desc *)))) == 0)
1782 		return (S_ERROR);
1783 	etype_rel = (etype == ET_REL);
1784 	if (etype_rel && local) {
1785 		if ((ifl->ifl_locs =
1786 		    libld_calloc(sizeof (Sym_desc), local)) == 0)
1787 			return (S_ERROR);
1788 		/* LINTED */
1789 		ifl->ifl_locscnt = (Word)local;
1790 	}
1791 	ifl->ifl_symscnt = total;
1792 
1793 	/*
1794 	 * If there are local symbols to save add them to the symbol table
1795 	 * index array.
1796 	 */
1797 	if (local) {
1798 		int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1799 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
1800 			Word		shndx, sdflags = FLG_SY_CLEAN;
1801 			const char	*name;
1802 			Sym_desc	*rsdp;
1803 
1804 			/*
1805 			 * Determine the associated section index.
1806 			 */
1807 			if (symshndx && (sym->st_shndx == SHN_XINDEX))
1808 				shndx = symshndx[ndx];
1809 			else if ((shndx = sym->st_shndx) >= SHN_LORESERVE)
1810 				sdflags |= FLG_SY_SPECSEC;
1811 
1812 			/*
1813 			 * Check if st_name has a valid value or not.
1814 			 */
1815 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
1816 			    shndx, symsecname, strsecname, &sdflags)) == 0) {
1817 				ofl->ofl_flags |= FLG_OF_FATAL;
1818 				continue;
1819 			}
1820 
1821 			/*
1822 			 * If this local symbol table originates from a shared
1823 			 * object, then we're only interested in recording
1824 			 * register symbols.  As local symbol descriptors aren't
1825 			 * allocated for shared objects, one will be allocated
1826 			 * to associated with the register symbol.  This symbol
1827 			 * won't become part of the output image, but we must
1828 			 * process it to test for register conflicts.
1829 			 */
1830 			rsdp = sdp = 0;
1831 			if (sdflags & FLG_SY_REGSYM) {
1832 				if ((rsdp = ld_reg_find(sym, ofl)) != 0) {
1833 					/*
1834 					 * The fact that another register def-
1835 					 * inition has been found is fatal.
1836 					 * Call the verification routine to get
1837 					 * the error message and move on.
1838 					 */
1839 					(void) ld_reg_check(rsdp, sym, name,
1840 					    ifl, ofl);
1841 					continue;
1842 				}
1843 
1844 				if (etype == ET_DYN) {
1845 					if ((sdp = libld_calloc(
1846 					    sizeof (Sym_desc), 1)) == 0)
1847 						return (S_ERROR);
1848 					sdp->sd_ref = REF_DYN_SEEN;
1849 				}
1850 			} else if (etype == ET_DYN)
1851 				continue;
1852 
1853 			/*
1854 			 * Fill in the remaining symbol descriptor information.
1855 			 */
1856 			if (sdp == 0) {
1857 				sdp = &(ifl->ifl_locs[ndx]);
1858 				sdp->sd_ref = REF_REL_NEED;
1859 			}
1860 			if (rsdp == 0) {
1861 				sdp->sd_name = name;
1862 				sdp->sd_sym = sym;
1863 				sdp->sd_shndx = shndx;
1864 				sdp->sd_flags = sdflags;
1865 				sdp->sd_file = ifl;
1866 				ifl->ifl_oldndx[ndx] = sdp;
1867 			}
1868 
1869 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
1870 
1871 			/*
1872 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
1873 			 * so as to simplify future processing.
1874 			 */
1875 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
1876 				sdp->sd_shndx = shndx = SHN_UNDEF;
1877 				sdp->sd_flags1 |=
1878 				    (FLG_SY1_IGNORE | FLG_SY1_ELIM);
1879 			}
1880 
1881 			/*
1882 			 * Process any register symbols.
1883 			 */
1884 			if (sdp->sd_flags & FLG_SY_REGSYM) {
1885 				/*
1886 				 * Add a diagnostic to indicate we've caught a
1887 				 * register symbol, as this can be useful if a
1888 				 * register conflict is later discovered.
1889 				 */
1890 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
1891 
1892 				/*
1893 				 * If this register symbol hasn't already been
1894 				 * recorded, enter it now.
1895 				 */
1896 				if ((rsdp == 0) &&
1897 				    (ld_reg_enter(sdp, ofl) == 0))
1898 					return (S_ERROR);
1899 			}
1900 
1901 			/*
1902 			 * Assign an input section.
1903 			 */
1904 			if ((sym->st_shndx != SHN_UNDEF) &&
1905 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
1906 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
1907 
1908 			/*
1909 			 * If this symbol falls within the range of a section
1910 			 * being discarded, then discard the symbol itself.
1911 			 * There is no reason to keep this local symbol.
1912 			 */
1913 			if (sdp->sd_isc &&
1914 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
1915 				sdp->sd_flags |= FLG_SY_ISDISC;
1916 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml,
1917 				    sdp, sdp->sd_isc));
1918 				continue;
1919 			}
1920 
1921 			/*
1922 			 * Skip any section symbols as new versions of these
1923 			 * will be created.
1924 			 */
1925 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
1926 				if (sym->st_shndx == SHN_UNDEF) {
1927 					eprintf(ofl->ofl_lml, ERR_WARNING,
1928 					    MSG_INTL(MSG_SYM_INVSHNDX),
1929 					    demangle(sdp->sd_name),
1930 					    ifl->ifl_name,
1931 					    conv_sym_shndx(sym->st_shndx,
1932 					    &inv_buf));
1933 				}
1934 				continue;
1935 			}
1936 
1937 			/*
1938 			 * For a relocatable object, if this symbol is defined
1939 			 * and has non-zero length and references an address
1940 			 * within an associated section, then check its extents
1941 			 * to make sure the section boundaries encompass it.
1942 			 * If they don't, the ELF file is corrupt.
1943 			 */
1944 			if (etype_rel && SYM_LOC_BADADDR(sdp, sym, type)) {
1945 				issue_badaddr_msg(ifl, ofl, sdp, sym, shndx);
1946 				continue;
1947 			}
1948 
1949 			/*
1950 			 * Sanity check for TLS
1951 			 */
1952 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
1953 			    (sym->st_shndx != SHN_COMMON))) {
1954 				Is_desc	*isp = sdp->sd_isc;
1955 
1956 				if ((isp == 0) || (isp->is_shdr == 0) ||
1957 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
1958 					eprintf(ofl->ofl_lml, ERR_FATAL,
1959 					    MSG_INTL(MSG_SYM_TLS),
1960 					    demangle(sdp->sd_name),
1961 					    ifl->ifl_name);
1962 					ofl->ofl_flags |= FLG_OF_FATAL;
1963 					continue;
1964 				}
1965 			}
1966 
1967 			/*
1968 			 * Carry our some basic sanity checks (these are just
1969 			 * some of the erroneous symbol entries we've come
1970 			 * across, there's probably a lot more).  The symbol
1971 			 * will not be carried forward to the output file, which
1972 			 * won't be a problem unless a relocation is required
1973 			 * against it.
1974 			 */
1975 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
1976 			    ((sym->st_shndx == SHN_COMMON)) ||
1977 			    ((type == STT_FILE) &&
1978 			    (sym->st_shndx != SHN_ABS))) ||
1979 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == 0))) {
1980 				eprintf(ofl->ofl_lml, ERR_WARNING,
1981 				    MSG_INTL(MSG_SYM_INVSHNDX),
1982 				    demangle(sdp->sd_name), ifl->ifl_name,
1983 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
1984 				sdp->sd_isc = NULL;
1985 				sdp->sd_flags |= FLG_SY_INVALID;
1986 				continue;
1987 			}
1988 
1989 			/*
1990 			 * As these local symbols will become part of the output
1991 			 * image, record their number and name string size.
1992 			 * Globals are counted after all input file processing
1993 			 * (and hence symbol resolution) is complete during
1994 			 * sym_validate().
1995 			 */
1996 			if (!(ofl->ofl_flags1 & FLG_OF1_REDLSYM)) {
1997 				ofl->ofl_locscnt++;
1998 
1999 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
2000 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
2001 				    sdp->sd_name) == -1))
2002 					return (S_ERROR);
2003 
2004 				if (allow_ldynsym && sym->st_name &&
2005 				    ldynsym_symtype[type]) {
2006 					ofl->ofl_dynlocscnt++;
2007 					if (st_insert(ofl->ofl_dynstrtab,
2008 					    sdp->sd_name) == -1)
2009 						return (S_ERROR);
2010 					/* Include it in sort section? */
2011 					DYNSORT_COUNT(sdp, sym, type, ++);
2012 				}
2013 			}
2014 		}
2015 	}
2016 
2017 	/*
2018 	 * The GNU ld interprets the top bit of the 16-bit Versym value
2019 	 * (0x8000) as the "hidden" bit. If this bit is set, the linker
2020 	 * is supposed to act as if that symbol does not exist. The Solaris
2021 	 * linker does not support this mechanism, or the model of interface
2022 	 * evolution that it allows, but we honor it in GNU ld produced
2023 	 * objects in order to interoperate with them.
2024 	 *
2025 	 * Determine if we should honor the GNU hidden bit for this file.
2026 	 */
2027 	test_gnu_hidden_bit = ((ifl->ifl_flags & FLG_IF_GNUVER) != 0) &&
2028 	    (ifl->ifl_versym != NULL);
2029 
2030 	/*
2031 	 * Now scan the global symbols entering them in the internal symbol
2032 	 * table or resolving them as necessary.
2033 	 */
2034 	sym = (Sym *)isc->is_indata->d_buf;
2035 	sym += local;
2036 	/* LINTED */
2037 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
2038 		const char	*name;
2039 		Word		shndx, sdflags = 0;
2040 
2041 		/*
2042 		 * Determine the associated section index.
2043 		 */
2044 		if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
2045 			shndx = symshndx[ndx];
2046 		} else {
2047 			shndx = sym->st_shndx;
2048 			if (sym->st_shndx >= SHN_LORESERVE)
2049 				sdflags |= FLG_SY_SPECSEC;
2050 		}
2051 
2052 		/*
2053 		 * Check if st_name has a valid value or not.
2054 		 */
2055 		if ((name = string(ofl, ifl, sym, strs, strsize, ndx, shndx,
2056 		    symsecname, strsecname, &sdflags)) == 0) {
2057 			ofl->ofl_flags |= FLG_OF_FATAL;
2058 			continue;
2059 		}
2060 
2061 		/*
2062 		 * Test for the GNU hidden bit, and ignore symbols that
2063 		 * have it set.
2064 		 */
2065 		if (test_gnu_hidden_bit &&
2066 		    ((ifl->ifl_versym[ndx] & 0x8000) != 0))
2067 			continue;
2068 
2069 		/*
2070 		 * The linker itself will generate symbols for _end, _etext,
2071 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
2072 		 * bother entering these symbols from shared objects.  This
2073 		 * results in some wasted resolution processing, which is hard
2074 		 * to feel, but if nothing else, pollutes diagnostic relocation
2075 		 * output.
2076 		 */
2077 		if (name[0] && (etype == ET_DYN) && (sym->st_size == 0) &&
2078 		    (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) &&
2079 		    (name[0] == '_') && ((name[1] == 'e') ||
2080 		    (name[1] == 'D') || (name[1] == 'P')) &&
2081 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
2082 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
2083 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
2084 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
2085 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
2086 			ifl->ifl_oldndx[ndx] = 0;
2087 			continue;
2088 		}
2089 
2090 		/*
2091 		 * Determine and validate the symbols binding.
2092 		 */
2093 		bind = ELF_ST_BIND(sym->st_info);
2094 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
2095 			eprintf(ofl->ofl_lml, ERR_WARNING,
2096 			    MSG_INTL(MSG_SYM_NONGLOB), demangle(name),
2097 			    ifl->ifl_name,
2098 			    conv_sym_info_bind(bind, 0, &inv_buf));
2099 			continue;
2100 		}
2101 
2102 		/*
2103 		 * If this symbol falls within the range of a section being
2104 		 * discarded, then discard the symbol itself.
2105 		 */
2106 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
2107 		    (sym->st_shndx != SHN_UNDEF)) {
2108 			Is_desc	*isp;
2109 
2110 			if (shndx >= ifl->ifl_shnum) {
2111 				/*
2112 				 * Carry our some basic sanity checks
2113 				 * The symbol will not be carried forward to
2114 				 * the output file, which won't be a problem
2115 				 * unless a relocation is required against it.
2116 				 */
2117 				eprintf(ofl->ofl_lml, ERR_WARNING,
2118 				    MSG_INTL(MSG_SYM_INVSHNDX), demangle(name),
2119 				    ifl->ifl_name,
2120 				    conv_sym_shndx(sym->st_shndx, &inv_buf));
2121 				continue;
2122 			}
2123 
2124 			isp = ifl->ifl_isdesc[shndx];
2125 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
2126 				if ((sdp =
2127 				    libld_calloc(sizeof (Sym_desc), 1)) == 0)
2128 					return (S_ERROR);
2129 
2130 				/*
2131 				 * Create a dummy symbol entry so that if we
2132 				 * find any references to this discarded symbol
2133 				 * we can compensate.
2134 				 */
2135 				sdp->sd_name = name;
2136 				sdp->sd_sym = sym;
2137 				sdp->sd_file = ifl;
2138 				sdp->sd_isc = isp;
2139 				sdp->sd_flags = FLG_SY_ISDISC;
2140 				ifl->ifl_oldndx[ndx] = sdp;
2141 
2142 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp,
2143 				    sdp->sd_isc));
2144 				continue;
2145 			}
2146 		}
2147 
2148 		/*
2149 		 * If the symbol does not already exist in the internal symbol
2150 		 * table add it, otherwise resolve the conflict.  If the symbol
2151 		 * from this file is kept, retain its symbol table index for
2152 		 * possible use in associating a global alias.
2153 		 */
2154 		/* LINTED */
2155 		hash = (Word)elf_hash((const char *)name);
2156 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
2157 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
2158 			if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, ndx,
2159 			    shndx, sdflags, 0, &where)) == (Sym_desc *)S_ERROR)
2160 				return (S_ERROR);
2161 
2162 		} else if (ld_sym_resolve(sdp, sym, ifl, ofl, ndx, shndx,
2163 		    sdflags) == S_ERROR)
2164 			return (S_ERROR);
2165 
2166 		/*
2167 		 * After we've compared a defined symbol in one shared
2168 		 * object, flag the symbol so we don't compare it again.
2169 		 */
2170 		if ((etype == ET_DYN) && (sym->st_shndx != SHN_UNDEF) &&
2171 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
2172 			sdp->sd_flags |= FLG_SY_SOFOUND;
2173 
2174 		/*
2175 		 * If the symbol is accepted from this file retain the symbol
2176 		 * index for possible use in aliasing.
2177 		 */
2178 		if (sdp->sd_file == ifl)
2179 			sdp->sd_symndx = ndx;
2180 
2181 		ifl->ifl_oldndx[ndx] = sdp;
2182 
2183 		/*
2184 		 * If we've accepted a register symbol, continue to validate
2185 		 * it.
2186 		 */
2187 		if (sdp->sd_flags & FLG_SY_REGSYM) {
2188 			Sym_desc	*rsdp;
2189 
2190 			if ((rsdp = ld_reg_find(sdp->sd_sym, ofl)) == 0) {
2191 				if (ld_reg_enter(sdp, ofl) == 0)
2192 					return (S_ERROR);
2193 			} else if (rsdp != sdp) {
2194 				(void) ld_reg_check(rsdp, sdp->sd_sym,
2195 				    sdp->sd_name, ifl, ofl);
2196 			}
2197 		}
2198 
2199 		/*
2200 		 * For a relocatable object, if this symbol is defined
2201 		 * and has non-zero length and references an address
2202 		 * within an associated section, then check its extents
2203 		 * to make sure the section boundaries encompass it.
2204 		 * If they don't, the ELF file is corrupt. Note that this
2205 		 * global symbol may have come from another file to satisfy
2206 		 * an UNDEF symbol of the same name from this one. In that
2207 		 * case, we don't check it, because it was already checked
2208 		 * as part of its own file.
2209 		 */
2210 		if (etype_rel && (sdp->sd_file == ifl)) {
2211 			Sym *tsym = sdp->sd_sym;
2212 
2213 			if (SYM_LOC_BADADDR(sdp, tsym,
2214 			    ELF_ST_TYPE(tsym->st_info))) {
2215 				issue_badaddr_msg(ifl, ofl, sdp,
2216 				    tsym, tsym->st_shndx);
2217 				continue;
2218 			}
2219 		}
2220 	}
2221 
2222 	/*
2223 	 * If this is a shared object scan the globals one more time and
2224 	 * associate any weak/global associations.  This association is needed
2225 	 * should the weak definition satisfy a reference in the dynamic
2226 	 * executable:
2227 	 *
2228 	 *  o	if the symbol is a data item it will be copied to the
2229 	 *	executables address space, thus we must also reassociate the
2230 	 *	alias symbol with its new location in the executable.
2231 	 *
2232 	 *  o	if the symbol is a function then we may need to promote	the
2233 	 *	symbols binding from undefined weak to undefined, otherwise the
2234 	 *	run-time linker will not generate the correct relocation error
2235 	 *	should the symbol not be found.
2236 	 *
2237 	 * The true association between a weak/strong symbol pair is that both
2238 	 * symbol entries are identical, thus first we created a sorted symbol
2239 	 * list keyed off of the symbols value (if the value is the same chances
2240 	 * are the rest of the symbols data is).  This list is then scanned for
2241 	 * weak symbols, and if one is found then any strong association will
2242 	 * exist in the following entries.  Thus we just have to scan one
2243 	 * (typical single alias) or more (in the uncommon instance of multiple
2244 	 * weak to strong associations) entries to determine if a match exists.
2245 	 */
2246 	if ((OFL_ALLOW_LDYNSYM(ofl) || (etype == ET_DYN)) &&
2247 	    (total > local)) {
2248 		Sym_desc **	sort;
2249 		size_t		size = (total - local) * sizeof (Sym_desc *);
2250 
2251 		if ((sort = libld_malloc(size)) == 0)
2252 			return (S_ERROR);
2253 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], size);
2254 
2255 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
2256 
2257 		for (ndx = 0; ndx < (total - local); ndx++) {
2258 			Sym_desc *	wsdp = sort[ndx];
2259 			Sym *		wsym;
2260 			int		sndx;
2261 
2262 			if (wsdp == 0)
2263 				continue;
2264 
2265 			wsym = wsdp->sd_sym;
2266 
2267 			if ((ELF_ST_BIND(wsym->st_info) != STB_WEAK) ||
2268 			    (wsdp->sd_sym->st_shndx == SHN_UNDEF) ||
2269 			    (wsdp->sd_flags & FLG_SY_SPECSEC))
2270 				continue;
2271 
2272 			/*
2273 			 * We have a weak symbol, if it has a strong alias it
2274 			 * will have been sorted to one of the following sort
2275 			 * table entries.  Note that we could have multiple weak
2276 			 * symbols aliased to one strong (if this occurs then
2277 			 * the strong symbol only maintains one alias back to
2278 			 * the last weak).
2279 			 */
2280 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
2281 				Sym_desc *	ssdp = sort[sndx];
2282 				Sym *		ssym;
2283 
2284 				if (ssdp == 0)
2285 					break;
2286 
2287 				ssym = ssdp->sd_sym;
2288 
2289 				if (wsym->st_value != ssym->st_value)
2290 					break;
2291 
2292 				if ((ssdp->sd_file == ifl) &&
2293 				    (wsdp->sd_file == ifl) &&
2294 				    (wsym->st_size == ssym->st_size) &&
2295 				    (ssdp->sd_sym->st_shndx != SHN_UNDEF) &&
2296 				    (ELF_ST_BIND(ssym->st_info) != STB_WEAK) &&
2297 				    ((ssdp->sd_flags & FLG_SY_SPECSEC) == 0)) {
2298 					int w_dynbits, s_dynbits;
2299 
2300 					/*
2301 					 * If a sharable object, set link
2302 					 * fields so they reference each other
2303 					 */
2304 					if (etype == ET_DYN) {
2305 						ssdp->sd_aux->sa_linkndx =
2306 						    (Word)wsdp->sd_symndx;
2307 						wsdp->sd_aux->sa_linkndx =
2308 						    (Word)ssdp->sd_symndx;
2309 					}
2310 					/*
2311 					 * Determine which of these two symbols
2312 					 * go into the sort section. If the
2313 					 * mapfile has made explicit settings
2314 					 * of the FLG_SY_*DYNSORT flags for both
2315 					 * symbols, then we do what they say.
2316 					 * If one has the DYNSORT flags set,
2317 					 * we set the NODYNSORT bit in the
2318 					 * other. And if neither has an
2319 					 * explicit setting, then we favor the
2320 					 * weak symbol because they usually
2321 					 * lack the leading underscore.
2322 					 */
2323 					w_dynbits = wsdp->sd_flags &
2324 					    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2325 					s_dynbits = ssdp->sd_flags &
2326 					    (FLG_SY_DYNSORT | FLG_SY_NODYNSORT);
2327 					if (!(w_dynbits && s_dynbits)) {
2328 						if (s_dynbits) {
2329 							if (s_dynbits ==
2330 							    FLG_SY_DYNSORT)
2331 							wsdp->sd_flags |=
2332 							    FLG_SY_NODYNSORT;
2333 						} else if (w_dynbits !=
2334 						    FLG_SY_NODYNSORT) {
2335 							ssdp->sd_flags |=
2336 							    FLG_SY_NODYNSORT;
2337 						}
2338 					}
2339 					break;
2340 				}
2341 			}
2342 		}
2343 	}
2344 	return (1);
2345 
2346 #undef SYM_LOC_BADADDR
2347 }
2348 
2349 /*
2350  * Add an undefined symbol to the symbol table.  The reference originates from
2351  * the location identifed by the message id (mid).  These references can
2352  * originate from command line options such as -e, -u, -initarray, etc.
2353  * (identified with MSG_INTL(MSG_STR_COMMAND)), or from internally generated
2354  * TLS relocation references (identified with MSG_INTL(MSG_STR_TLSREL)).
2355  */
2356 Sym_desc *
2357 ld_sym_add_u(const char *name, Ofl_desc *ofl, Msg mid)
2358 {
2359 	Sym		*sym;
2360 	Ifl_desc	*ifl = 0, *_ifl;
2361 	Sym_desc	*sdp;
2362 	Word		hash;
2363 	Listnode	*lnp;
2364 	avl_index_t	where;
2365 	const char	*reference = MSG_INTL(mid);
2366 
2367 	/*
2368 	 * As an optimization, determine whether we've already generated this
2369 	 * reference.  If the symbol doesn't already exist we'll create it.
2370 	 * Or if the symbol does exist from a different source, we'll resolve
2371 	 * the conflict.
2372 	 */
2373 	/* LINTED */
2374 	hash = (Word)elf_hash(name);
2375 	if ((sdp = ld_sym_find(name, hash, &where, ofl)) != NULL) {
2376 		if ((sdp->sd_sym->st_shndx == SHN_UNDEF) &&
2377 		    (sdp->sd_file->ifl_name == reference))
2378 			return (sdp);
2379 	}
2380 
2381 	/*
2382 	 * Determine whether a pseudo input file descriptor exists to represent
2383 	 * the command line, as any global symbol needs an input file descriptor
2384 	 * during any symbol resolution (refer to map_ifl() which provides a
2385 	 * similar method for adding symbols from mapfiles).
2386 	 */
2387 	for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, _ifl))
2388 		if (strcmp(_ifl->ifl_name, reference) == 0) {
2389 			ifl = _ifl;
2390 			break;
2391 		}
2392 
2393 	/*
2394 	 * If no descriptor exists create one.
2395 	 */
2396 	if (ifl == 0) {
2397 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) ==
2398 		    (Ifl_desc *)0)
2399 			return ((Sym_desc *)S_ERROR);
2400 		ifl->ifl_name = reference;
2401 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
2402 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr),
2403 		    1)) == 0)
2404 			return ((Sym_desc *)S_ERROR);
2405 		ifl->ifl_ehdr->e_type = ET_REL;
2406 
2407 		if (list_appendc(&ofl->ofl_objs, ifl) == 0)
2408 			return ((Sym_desc *)S_ERROR);
2409 	}
2410 
2411 	/*
2412 	 * Allocate a symbol structure and add it to the global symbol table.
2413 	 */
2414 	if ((sym = libld_calloc(sizeof (Sym), 1)) == 0)
2415 		return ((Sym_desc *)S_ERROR);
2416 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
2417 	sym->st_shndx = SHN_UNDEF;
2418 
2419 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
2420 	if (sdp == NULL) {
2421 		DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
2422 		if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
2423 		    0, 0, &where)) == (Sym_desc *)S_ERROR)
2424 			return ((Sym_desc *)S_ERROR);
2425 	} else if (ld_sym_resolve(sdp, sym, ifl, ofl, 0,
2426 	    SHN_UNDEF, 0) == S_ERROR)
2427 		return ((Sym_desc *)S_ERROR);
2428 
2429 	sdp->sd_flags &= ~FLG_SY_CLEAN;
2430 	sdp->sd_flags |= FLG_SY_CMDREF;
2431 
2432 	return (sdp);
2433 }
2434