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