xref: /titanic_50/usr/src/cmd/sgs/libld/common/syms.c (revision 554ff184129088135ad2643c1c9832174a17be88)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	Copyright (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  *
27  * Copyright 2005 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 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 const char *
77 string(Ifl_desc *ifl, Sym *sym, const char *strs, size_t strsize, int symndx,
78     Word shndx, const char *symsecname, const char *strsecname, Word * flags)
79 {
80 	const char	*regname;
81 	Word		name = sym->st_name;
82 
83 	if (name) {
84 		if ((ifl->ifl_flags & FLG_IF_HSTRTAB) == 0) {
85 			eprintf(ERR_FATAL, MSG_INTL(MSG_FIL_NOSTRTABLE),
86 			    ifl->ifl_name, symsecname, symndx, EC_XWORD(name));
87 			return (0);
88 		}
89 		if (name >= (Word)strsize) {
90 			eprintf(ERR_FATAL, MSG_INTL(MSG_FIL_EXCSTRTABLE),
91 			    ifl->ifl_name, symsecname, symndx, EC_XWORD(name),
92 			    strsecname, EC_XWORD(strsize));
93 			return (0);
94 		}
95 	}
96 
97 	/*
98 	 * Determine if we're dealing with a register and if so validate it.
99 	 * If it's a scratch register, a fabricated name will be returned.
100 	 */
101 	if ((regname = is_regsym(ifl, sym, strs, symndx, shndx,
102 	    symsecname, flags)) == (const char *)S_ERROR) {
103 		return (0);
104 	}
105 	if (regname)
106 		return (regname);
107 
108 	/*
109 	 * If this isn't a register, but we have a global symbol with a null
110 	 * name, we're not going to be able to hash this, search for it, or
111 	 * do anything interesting.  However, we've been accepting a symbol of
112 	 * this kind for ages now, so give the user a warning (rather than a
113 	 * fatal error), just in case this instance exists somewhere in the
114 	 * world and hasn't, as yet, been a problem.
115 	 */
116 	if ((name == 0) && (ELF_ST_BIND(sym->st_info) != STB_LOCAL)) {
117 		eprintf(ERR_WARNING, MSG_INTL(MSG_FIL_NONAMESYM),
118 		    ifl->ifl_name, symsecname, symndx, EC_XWORD(name));
119 	}
120 	return (strs + name);
121 }
122 
123 /*
124  * Shared objects can be built that define specific symbols that can not be
125  * directly bound to.  These objects have a syminfo section (and an associated
126  * DF_1_NODIRECT dynamic flags entry).  Scan this table looking for symbols
127  * that can't be bound to directly, and if this files symbol is presently
128  * referenced, mark it so that we don't directly bind to it.
129  */
130 uintptr_t
131 sym_nodirect(Is_desc * isp, Ifl_desc * ifl, Ofl_desc * ofl)
132 {
133 	Shdr		*sifshdr, *symshdr;
134 	Syminfo		*sifdata;
135 	Sym		*symdata;
136 	char		*strdata;
137 	ulong_t		cnt, _cnt;
138 
139 	/*
140 	 * Get the syminfo data, and determine the number of entries.
141 	 */
142 	sifshdr = isp->is_shdr;
143 	sifdata = (Syminfo *)isp->is_indata->d_buf;
144 	cnt =  sifshdr->sh_size / sifshdr->sh_entsize;
145 
146 	/*
147 	 * Get the associated symbol table.
148 	 */
149 	symshdr = ifl->ifl_isdesc[sifshdr->sh_link]->is_shdr;
150 	symdata = ifl->ifl_isdesc[sifshdr->sh_link]->is_indata->d_buf;
151 
152 	/*
153 	 * Get the string table associated with the symbol table.
154 	 */
155 	strdata = ifl->ifl_isdesc[symshdr->sh_link]->is_indata->d_buf;
156 
157 	/*
158 	 * Traverse the syminfo data for symbols that can't be directly
159 	 * bound to.
160 	 */
161 	for (_cnt = 1, sifdata++; _cnt < cnt; _cnt++, sifdata++) {
162 		Sym		*sym;
163 		char		*str;
164 		Sym_desc	*sdp;
165 
166 		if (((sifdata->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0) ||
167 		    (sifdata->si_boundto < SYMINFO_BT_LOWRESERVE))
168 			continue;
169 
170 		sym = (Sym *)(symdata + _cnt);
171 		str = (char *)(strdata + sym->st_name);
172 
173 		if (sdp = sym_find(str, SYM_NOHASH, 0, ofl)) {
174 			if (ifl != sdp->sd_file)
175 				continue;
176 
177 			sdp->sd_flags1 &= ~FLG_SY1_DIR;
178 			sdp->sd_flags1 |= FLG_SY1_NDIR;
179 		}
180 	}
181 	return (0);
182 }
183 
184 /*
185  * If, during symbol processing, it is necessary to update a local symbols
186  * contents before we have generated the symbol tables in the output image,
187  * create a new symbol structure and copy the original symbol contents.  While
188  * we are processing the input files, their local symbols are part of the
189  * read-only mapped image.  Commonly, these symbols are copied to the new output
190  * file image and then updated to reflect their new address and any change in
191  * attributes.  However, sometimes during relocation counting, it is necessary
192  * to adjust the symbols information.  This routine provides for the generation
193  * of a new symbol image so that this update can be performed.
194  * All global symbols are copied to an internal symbol table to improve locality
195  * of reference and hence performance, and thus this copying is not necessary.
196  */
197 uintptr_t
198 sym_copy(Sym_desc *sdp)
199 {
200 	Sym	*nsym;
201 
202 	if (sdp->sd_flags & FLG_SY_CLEAN) {
203 		if ((nsym = libld_malloc(sizeof (Sym))) == 0)
204 			return (S_ERROR);
205 		*nsym = *(sdp->sd_sym);
206 		sdp->sd_sym = nsym;
207 		sdp->sd_flags &= ~FLG_SY_CLEAN;
208 	}
209 	return (1);
210 }
211 
212 /*
213  * Finds a given name in the link editors internal symbol table.  If no
214  * hash value is specified it is calculated.  A pointer to the located
215  * Sym_desc entry is returned, or NULL if the symbol is not found.
216  */
217 Sym_desc *
218 sym_find(const char *name, Word hash, avl_index_t *where, Ofl_desc *ofl)
219 {
220 	Sym_avlnode	qsav;
221 	Sym_avlnode	*sav;
222 
223 	if (hash == SYM_NOHASH)
224 		/* LINTED */
225 		hash = (Word)elf_hash((const char *)name);
226 	qsav.sav_hash = hash;
227 	qsav.sav_name = name;
228 
229 	/*
230 	 * Perform search for symbol in AVL tree.  Note that the 'where' field
231 	 * is passed in from the caller.  If a 'where' is present, it can be
232 	 * used in subsequent 'sym_enter()' calls if required.
233 	 */
234 	sav = avl_find(&ofl->ofl_symavl, &qsav, where);
235 
236 	/*
237 	 * If symbol was not found in the avl tree, return null to show that.
238 	 */
239 	if (sav == 0)
240 		return (0);
241 
242 	/*
243 	 * Return symbol found.
244 	 */
245 	return (sav->sav_symdesc);
246 }
247 
248 
249 /*
250  * Enter a new symbol into the link editors internal symbol table.
251  * If the symbol is from an input file, information regarding the input file
252  * and input section is also recorded.  Otherwise (file == NULL) the symbol
253  * has been internally generated (ie. _etext, _edata, etc.).
254  */
255 Sym_desc *
256 sym_enter(const char *name, Sym *osym, Word hash, Ifl_desc *ifl, Ofl_desc *ofl,
257     Word ndx, Word shndx, Word sdflags, Half sdflags1, avl_index_t *where)
258 {
259 	Sym_desc	*sdp;
260 	Sym_aux		*sap;
261 	Sym_avlnode	*savl;
262 	char		*_name;
263 	Sym		*nsym;
264 	Half		etype;
265 	Ehdr		*ehdr;
266 	avl_index_t	_where;
267 
268 	/*
269 	 * Establish the file type.
270 	 */
271 	if (ifl)
272 		etype = ifl->ifl_ehdr->e_type;
273 	else
274 		etype = ET_NONE;
275 
276 	ofl->ofl_entercnt++;
277 
278 	/*
279 	 * Allocate a Sym Descriptor, Auxiliary Descriptor, and a Sym AVLNode -
280 	 * contiguously.
281 	 */
282 	if ((savl = libld_calloc(sizeof (Sym_avlnode) + sizeof (Sym_desc) +
283 	    sizeof (Sym_aux), 1)) == 0)
284 		return ((Sym_desc *)S_ERROR);
285 	sdp = (Sym_desc *)((uintptr_t)savl + sizeof (Sym_avlnode));
286 	sap = (Sym_aux *)((uintptr_t)sdp + sizeof (Sym_desc));
287 
288 	savl->sav_symdesc = sdp;
289 	sdp->sd_file = ifl;
290 	sdp->sd_aux = sap;
291 	savl->sav_hash = sap->sa_hash = hash;
292 
293 
294 	/*
295 	 * Copy the symbol table entry from the input file into the internal
296 	 * entry and have the symbol descriptor use it.
297 	 */
298 	sdp->sd_sym = nsym = &sap->sa_sym;
299 	*nsym = *osym;
300 	sdp->sd_shndx = shndx;
301 	sdp->sd_flags |= sdflags;
302 	sdp->sd_flags1 |= sdflags1;
303 
304 	if ((_name = libld_malloc(strlen(name) + 1)) == 0)
305 		return ((Sym_desc *)S_ERROR);
306 	savl->sav_name = sdp->sd_name = (const char *)strcpy(_name, name);
307 
308 	/*
309 	 * Enter Symbol in AVL tree.
310 	 */
311 	if (where == 0) {
312 		/* LINTED */
313 		Sym_avlnode	*_savl;
314 		/*
315 		 * If a previous sym_find() hasn't initialized 'where' do it
316 		 * now.
317 		 */
318 		where = &_where;
319 		_savl = avl_find(&ofl->ofl_symavl, savl, where);
320 		assert(_savl == 0);
321 	}
322 	avl_insert(&ofl->ofl_symavl, savl, *where);
323 
324 	/*
325 	 * Record the section index.  This is possible because the
326 	 * `ifl_isdesc' table is filled before we start symbol processing.
327 	 */
328 	if ((sdflags & FLG_SY_SPECSEC) || (shndx == SHN_UNDEF))
329 		sdp->sd_isc = NULL;
330 	else {
331 		sdp->sd_isc = ifl->ifl_isdesc[shndx];
332 
333 		/*
334 		 * If this symbol is from a relocatable object, make sure that
335 		 * it is still associated with a section.  For example, an
336 		 * unknown section type (SHT_NULL) would have been rejected on
337 		 * input with a warning.  Here, we make the use of the symbol
338 		 * fatal.  A symbol descriptor is still returned, so that the
339 		 * caller can continue processing all symbols, and hence flush
340 		 * out as many error conditions as possible.
341 		 */
342 		if ((etype == ET_REL) && (sdp->sd_isc == 0)) {
343 			eprintf(ERR_FATAL, MSG_INTL(MSG_SYM_INVSEC), name,
344 			    ifl->ifl_name, EC_XWORD(shndx));
345 			ofl->ofl_flags |= FLG_OF_FATAL;
346 			return (sdp);
347 		}
348 	}
349 
350 	/*
351 	 * Mark any COMMON symbols as 'tentative'.
352 	 */
353 	if ((sdflags & FLG_SY_SPECSEC) && (shndx == SHN_COMMON))
354 		sdp->sd_flags |= FLG_SY_TENTSYM;
355 
356 	/*
357 	 * Establish the symbols reference & visibility.
358 	 */
359 	if ((etype == ET_NONE) || (etype == ET_REL)) {
360 		sdp->sd_ref = REF_REL_NEED;
361 
362 		/*
363 		 * Under -Bnodirect, all exported interfaces are tagged to
364 		 * prevent direct binding to them.
365 		 */
366 		if ((ofl->ofl_flags1 & FLG_OF1_ALNODIR) && (shndx != SHN_UNDEF))
367 			sdp->sd_flags1 |= FLG_SY1_NDIR;
368 
369 	} else {
370 		sdp->sd_ref = REF_DYN_SEEN;
371 
372 		/*
373 		 * Record the binding file for this symbol in the sa_bindto
374 		 * field.  If this symbol is ever overridden by a REF_REL_NEED
375 		 * definition, sa_bindto is used when building a 'translator'.
376 		 */
377 		if (shndx != SHN_UNDEF)
378 			sdp->sd_aux->sa_bindto = ifl;
379 
380 		/*
381 		 * If this is a protected symbol, mark it.
382 		 */
383 		if (ELF_ST_VISIBILITY(nsym->st_other) == STV_PROTECTED)
384 			sdp->sd_flags |= FLG_SY_PROT;
385 
386 		/*
387 		 * Mask out any visibility info from a DYN symbol.
388 		 */
389 		nsym->st_other = nsym->st_other & ~MSK_SYM_VISIBILITY;
390 
391 		/*
392 		 * If the new symbol is from a shared library and it
393 		 * is associated with a SHT_NOBITS section then this
394 		 * symbol originated from a tentative symbol.
395 		 */
396 		if (sdp->sd_isc &&
397 		    (sdp->sd_isc->is_shdr->sh_type == SHT_NOBITS))
398 			sdp->sd_flags |= FLG_SY_TENTSYM;
399 	}
400 
401 	/*
402 	 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF so as to
403 	 * simplify fucture processing.
404 	 */
405 	if (shndx == SHN_SUNW_IGNORE) {
406 		sdp->sd_shndx = shndx = SHN_UNDEF;
407 		sdp->sd_flags |= FLG_SY_REDUCED;
408 		sdp->sd_flags1 |=
409 		    (FLG_SY1_IGNORE | FLG_SY1_LOCL | FLG_SY1_ELIM);
410 	}
411 
412 	/*
413 	 * If this is an undefined, or common symbol from a relocatable object
414 	 * determine whether it is a global or weak reference (see build_osym(),
415 	 * where REF_DYN_NEED definitions are returned back to undefines).
416 	 */
417 	if ((etype == ET_REL) && ((shndx == SHN_UNDEF) ||
418 	    ((sdflags & FLG_SY_SPECSEC) && (shndx == SHN_COMMON))) &&
419 	    (ELF_ST_BIND(nsym->st_info) == STB_GLOBAL))
420 		sdp->sd_flags |= FLG_SY_GLOBREF;
421 
422 	/*
423 	 * Record the input filename on the referenced or defined files list
424 	 * for possible later diagnostics.  The `sa_rfile' pointer contains the
425 	 * name of the file that first referenced this symbol and is used to
426 	 * generate undefined symbol diagnostics (refer to sym_undef_entry()).
427 	 * Note that this entry can be overridden if a reference from a
428 	 * relocatable object is found after a reference from a shared object
429 	 * (refer to sym_override()).
430 	 * The `sa_dfiles' list is used to maintain the list of files that
431 	 * define the same symbol.  This list can be used for two reasons:
432 	 *
433 	 *   o	To save the first definition of a symbol that is not available
434 	 *	for this link-edit.
435 	 *
436 	 *   o	To save all definitions of a symbol when the -m option is in
437 	 *	effect.  This is optional as it is used to list multiple
438 	 *	(interposed) definitions of a symbol (refer to ldmap_out()),
439 	 *	and can be quite expensive.
440 	 */
441 	if (shndx == SHN_UNDEF) {
442 		sap->sa_rfile = ifl->ifl_name;
443 	} else {
444 		if (sdp->sd_ref == REF_DYN_SEEN) {
445 			/*
446 			 * A symbol is determined to be unavailable if it
447 			 * belongs to a version of a shared object that this
448 			 * user does not wish to use, or if it belongs to an
449 			 * implicit shared object.
450 			 */
451 			if (ifl->ifl_vercnt) {
452 				Ver_index *	vip;
453 				Half		vndx = ifl->ifl_versym[ndx];
454 
455 				sap->sa_dverndx = vndx;
456 				vip = &ifl->ifl_verndx[vndx];
457 				if (!(vip->vi_flags & FLG_VER_AVAIL)) {
458 					sdp->sd_flags |= FLG_SY_NOTAVAIL;
459 					sap->sa_vfile = ifl->ifl_name;
460 				}
461 			}
462 			if (!(ifl->ifl_flags & FLG_IF_NEEDED))
463 				sdp->sd_flags |= FLG_SY_NOTAVAIL;
464 
465 		} else if (etype == ET_REL) {
466 			/*
467 			 * If this symbol has been obtained from a versioned
468 			 * input relocatable object then the new symbol must be
469 			 * promoted to the versioning of the output file.
470 			 */
471 			if (ifl->ifl_versym)
472 				vers_promote(sdp, ndx, ifl, ofl);
473 		}
474 
475 		if ((ofl->ofl_flags & FLG_OF_GENMAP) &&
476 		    ((sdflags & FLG_SY_SPECSEC) == 0))
477 			if (list_appendc(&sap->sa_dfiles, ifl->ifl_name) == 0)
478 				return ((Sym_desc *)S_ERROR);
479 	}
480 
481 	if (sdp->sd_file)
482 		ehdr = sdp->sd_file->ifl_ehdr;
483 	else
484 		ehdr = &def_ehdr;
485 	DBG_CALL(Dbg_syms_entered(ehdr, nsym, sdp));
486 
487 	return (sdp);
488 }
489 
490 /*
491  * Add a special symbol to the symbol table.  Takes special symbol name with
492  * and without underscores.  This routine is called, after all other symbol
493  * resolution has completed, to generate a reserved absolute symbol (the
494  * underscore version).  Special symbols are updated with the appropriate
495  * values in sym_update().  If the user has already defined this symbol
496  * issue a warning and leave the symbol as is.  If the non-underscore symbol
497  * is referenced then turn it into a weak alias of the underscored symbol.
498  *
499  * If this is a global symbol, and it hasn't explicitly been defined as being
500  * directly bound to, indicate that it can't be directly bound to.
501  * Historically, most special symbols only have meaning to the object in which
502  * they exist, however, they've always been global.  To insure compatibility
503  * with any unexpected use presently in effect, insure these symbols don't get
504  * directly bound to.  Note, that establishing this state here isn't sufficient
505  * to create a syminfo table, only if a syminfo table is being created by some
506  * other symbol directives will the nodirect binding be recorded.  This insures
507  * we don't create syminfo sections for all objects we create, as this might add
508  * unnecessary bloat to users who haven't explicitly requested extra symbol
509  * information.
510  */
511 static uintptr_t
512 sym_add_spec(const char *name, const char *uname, Word sdaux_id,
513     Half flags1, Ofl_desc *ofl)
514 {
515 	Sym_desc	*sdp;
516 	Sym_desc 	*usdp;
517 	Sym		*sym;
518 	Word		hash;
519 	avl_index_t	where;
520 
521 	/* LINTED */
522 	hash = (Word)elf_hash(uname);
523 	if (usdp = sym_find(uname, hash, &where, ofl)) {
524 		/*
525 		 * If the underscore symbol exists and is undefined, or was
526 		 * defined in a shared library, convert it to a local symbol.
527 		 * Otherwise leave it as is and warn the user.
528 		 */
529 		if ((usdp->sd_shndx == SHN_UNDEF) ||
530 		    (usdp->sd_ref != REF_REL_NEED)) {
531 			usdp->sd_ref = REF_REL_NEED;
532 			usdp->sd_shndx = usdp->sd_sym->st_shndx = SHN_ABS;
533 			usdp->sd_flags |= FLG_SY_SPECSEC;
534 			usdp->sd_sym->st_info =
535 			    ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
536 			usdp->sd_isc = NULL;
537 			usdp->sd_sym->st_size = 0;
538 			usdp->sd_sym->st_value = 0;
539 			/* LINTED */
540 			usdp->sd_aux->sa_symspec = (Half)sdaux_id;
541 
542 			/*
543 			 * If a user hasn't specifically indicated the scope of
544 			 * this symbol be made local then leave it as global
545 			 * (ie. prevent automatic scoping).
546 			 */
547 			if (!(usdp->sd_flags1 & FLG_SY1_LOCL) &&
548 			    (flags1 & FLG_SY1_GLOB)) {
549 				usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
550 				if ((usdp->sd_flags1 & FLG_SY1_DIR) == 0)
551 					usdp->sd_flags1 |= FLG_SY1_NDIR;
552 			}
553 			usdp->sd_flags1 |= flags1;
554 
555 			/*
556 			 * If the reference originated from a mapfile insure
557 			 * we mark the symbol as used.
558 			 */
559 			if (usdp->sd_flags & FLG_SY_MAPREF)
560 				usdp->sd_flags |= FLG_SY_MAPUSED;
561 
562 			DBG_CALL(Dbg_syms_updated((ofl->ofl_ehdr) ?
563 			    ofl->ofl_ehdr : &def_ehdr, usdp, uname));
564 		} else
565 			eprintf(ERR_WARNING, MSG_INTL(MSG_SYM_RESERVE), uname,
566 			    usdp->sd_file->ifl_name);
567 	} else {
568 		/*
569 		 * If the symbol does not exist create it.
570 		 */
571 		if ((sym = libld_calloc(sizeof (Sym), 1)) == 0)
572 			return (S_ERROR);
573 		sym->st_shndx = SHN_ABS;
574 		sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT);
575 		sym->st_size = 0;
576 		sym->st_value = 0;
577 		DBG_CALL(Dbg_syms_created(uname));
578 		if ((usdp = sym_enter(uname, sym, hash, (Ifl_desc *)NULL,
579 		    ofl, 0, SHN_ABS, FLG_SY_SPECSEC, 0, &where)) ==
580 		    (Sym_desc *)S_ERROR)
581 			return (S_ERROR);
582 		usdp->sd_ref = REF_REL_NEED;
583 		/* LINTED */
584 		usdp->sd_aux->sa_symspec = (Half)sdaux_id;
585 
586 		usdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
587 		if (flags1 & FLG_SY1_GLOB)
588 			usdp->sd_flags1 |= FLG_SY1_NDIR;
589 		usdp->sd_flags1 |= flags1;
590 	}
591 
592 	if (name && (sdp = sym_find(name, SYM_NOHASH, 0, ofl)) &&
593 	    (sdp->sd_shndx == SHN_UNDEF)) {
594 		uchar_t	bind;
595 
596 		/*
597 		 * If the non-underscore symbol exists and is undefined
598 		 * convert it to be a local.  If the underscore has
599 		 * sa_symspec set (ie. it was created above) then simulate this
600 		 * as a weak alias.
601 		 */
602 		sdp->sd_ref = REF_REL_NEED;
603 		sdp->sd_shndx = sdp->sd_sym->st_shndx = SHN_ABS;
604 		sdp->sd_flags |= FLG_SY_SPECSEC;
605 		sdp->sd_isc = NULL;
606 		sdp->sd_sym->st_size = 0;
607 		sdp->sd_sym->st_value = 0;
608 		/* LINTED */
609 		sdp->sd_aux->sa_symspec = (Half)sdaux_id;
610 		if (usdp->sd_aux->sa_symspec) {
611 			usdp->sd_aux->sa_linkndx = 0;
612 			sdp->sd_aux->sa_linkndx = 0;
613 			bind = STB_WEAK;
614 		} else
615 			bind = STB_GLOBAL;
616 		sdp->sd_sym->st_info = ELF_ST_INFO(bind, STT_OBJECT);
617 
618 		/*
619 		 * If a user hasn't specifically indicated the scope of
620 		 * this symbol be made local then leave it as global
621 		 * (ie. prevent automatic scoping).
622 		 */
623 		if (!(sdp->sd_flags1 & FLG_SY1_LOCL) &&
624 		    (flags1 & FLG_SY1_GLOB)) {
625 			sdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
626 			if ((sdp->sd_flags1 & FLG_SY1_DIR) == 0)
627 				sdp->sd_flags1 |= FLG_SY1_NDIR;
628 		}
629 		sdp->sd_flags1 |= flags1;
630 
631 		/*
632 		 * If the reference originated from a mapfile insure
633 		 * we mark the symbol as used.
634 		 */
635 		if (sdp->sd_flags & FLG_SY_MAPREF)
636 			sdp->sd_flags |= FLG_SY_MAPUSED;
637 
638 		DBG_CALL(Dbg_syms_updated((ofl->ofl_ehdr) ? ofl->ofl_ehdr :
639 		    &def_ehdr, sdp, name));
640 	}
641 	return (1);
642 }
643 
644 
645 /*
646  * Print undefined symbols.
647  */
648 static Boolean	undef_title = TRUE;
649 
650 void
651 sym_undef_title()
652 {
653 	eprintf(ERR_NONE, MSG_INTL(MSG_SYM_FMT_UNDEF),
654 		MSG_INTL(MSG_SYM_UNDEF_ITM_11),
655 		MSG_INTL(MSG_SYM_UNDEF_ITM_21),
656 		MSG_INTL(MSG_SYM_UNDEF_ITM_12),
657 		MSG_INTL(MSG_SYM_UNDEF_ITM_22));
658 
659 	undef_title = FALSE;
660 }
661 
662 /*
663  * Undefined symbols can fall into one of four types:
664  *
665  *  o	the symbol is really undefined (SHN_UNDEF).
666  *
667  *  o	versioning has been enabled, however this symbol has not been assigned
668  *	to one of the defined versions.
669  *
670  *  o	the symbol has been defined by an implicitly supplied library, ie. one
671  *	which was encounted because it was NEEDED by another library, rather
672  * 	than from a command line supplied library which would become the only
673  *	dependency of the output file being produced.
674  *
675  *  o	the symbol has been defined by a version of a shared object that is
676  *	not permitted for this link-edit.
677  *
678  * In all cases the file who made the first reference to this symbol will have
679  * been recorded via the `sa_rfile' pointer.
680  */
681 typedef enum {
682 	UNDEF,		NOVERSION,	IMPLICIT,	NOTAVAIL,
683 	BNDLOCAL
684 } Type;
685 
686 static const Msg format[] = {
687 	MSG_SYM_UND_UNDEF,		/* MSG_INTL(MSG_SYM_UND_UNDEF) */
688 	MSG_SYM_UND_NOVER,		/* MSG_INTL(MSG_SYM_UND_NOVER) */
689 	MSG_SYM_UND_IMPL,		/* MSG_INTL(MSG_SYM_UND_IMPL) */
690 	MSG_SYM_UND_NOTA,		/* MSG_INTL(MSG_SYM_UND_NOTA) */
691 	MSG_SYM_UND_BNDLOCAL		/* MSG_INTL(MSG_SYM_UND_BNDLOCAL) */
692 };
693 
694 void
695 sym_undef_entry(Sym_desc *sdp, Type type)
696 {
697 	const char	*name1, *name2, *name3;
698 	Ifl_desc	*ifl = sdp->sd_file;
699 	Sym_aux		*sap = sdp->sd_aux;
700 
701 	if (undef_title)
702 		sym_undef_title();
703 
704 	switch (type) {
705 	case UNDEF:
706 	case BNDLOCAL:
707 		name1 = sap->sa_rfile;
708 		break;
709 	case NOVERSION:
710 		name1 = ifl->ifl_name;
711 		break;
712 	case IMPLICIT:
713 		name1 = sap->sa_rfile;
714 		name2 = ifl->ifl_name;
715 		break;
716 	case NOTAVAIL:
717 		name1 = sap->sa_rfile;
718 		name2 = sap->sa_vfile;
719 		name3 = ifl->ifl_verndx[sap->sa_dverndx].vi_name;
720 		break;
721 	default:
722 		return;
723 	}
724 
725 	eprintf(ERR_NONE, MSG_INTL(format[type]), demangle(sdp->sd_name),
726 	    name1, name2, name3);
727 }
728 
729 /*
730  * At this point all symbol input processing has been completed, therefore
731  * complete the symbol table entries by generating any necessary internal
732  * symbols.
733  */
734 uintptr_t
735 sym_spec(Ofl_desc *ofl)
736 {
737 	Word		flags = ofl->ofl_flags;
738 
739 	if (!(flags & FLG_OF_RELOBJ)) {
740 
741 		DBG_CALL(Dbg_syms_spec_title());
742 
743 		if (sym_add_spec(MSG_ORIG(MSG_SYM_ETEXT),
744 		    MSG_ORIG(MSG_SYM_ETEXT_U), SDAUX_ID_ETEXT,
745 		    FLG_SY1_GLOB, ofl) == S_ERROR)
746 			return (S_ERROR);
747 		if (sym_add_spec(MSG_ORIG(MSG_SYM_EDATA),
748 		    MSG_ORIG(MSG_SYM_EDATA_U), SDAUX_ID_EDATA,
749 		    FLG_SY1_GLOB, ofl) == S_ERROR)
750 			return (S_ERROR);
751 		if (sym_add_spec(MSG_ORIG(MSG_SYM_END),
752 		    MSG_ORIG(MSG_SYM_END_U), SDAUX_ID_END,
753 		    FLG_SY1_GLOB, ofl) == S_ERROR)
754 			return (S_ERROR);
755 		if (sym_add_spec(MSG_ORIG(MSG_SYM_L_END),
756 		    MSG_ORIG(MSG_SYM_L_END_U), SDAUX_ID_END,
757 		    FLG_SY1_LOCL, ofl) == S_ERROR)
758 			return (S_ERROR);
759 		if (sym_add_spec(MSG_ORIG(MSG_SYM_L_START),
760 		    MSG_ORIG(MSG_SYM_L_START_U), SDAUX_ID_START,
761 		    FLG_SY1_LOCL, ofl) == S_ERROR)
762 			return (S_ERROR);
763 
764 		/*
765 		 * Historically we've always produced a _DYNAMIC symbol, even
766 		 * for static executables (in which case its value will be 0).
767 		 */
768 		if (sym_add_spec(MSG_ORIG(MSG_SYM_DYNAMIC),
769 		    MSG_ORIG(MSG_SYM_DYNAMIC_U), SDAUX_ID_DYN,
770 		    FLG_SY1_GLOB, ofl) == S_ERROR)
771 			return (S_ERROR);
772 
773 		if ((flags & (FLG_OF_DYNAMIC | FLG_OF_RELOBJ)) ==
774 		    FLG_OF_DYNAMIC)
775 			if (sym_add_spec(MSG_ORIG(MSG_SYM_PLKTBL),
776 			    MSG_ORIG(MSG_SYM_PLKTBL_U), SDAUX_ID_PLT,
777 			    FLG_SY1_GLOB, ofl) == S_ERROR)
778 				return (S_ERROR);
779 
780 		if (sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH, 0, ofl))
781 			if (sym_add_spec(MSG_ORIG(MSG_SYM_GOFTBL),
782 			    MSG_ORIG(MSG_SYM_GOFTBL_U), SDAUX_ID_GOT,
783 			    FLG_SY1_GLOB, ofl) == S_ERROR)
784 				return (S_ERROR);
785 	}
786 	return (1);
787 }
788 
789 /*
790  * This routine checks to see if a symbols visibility needs to be reduced to
791  * either SYMBOLIC or LOCAL.  This routine can be called from either
792  * reloc_init() or sym_validate().
793  */
794 void
795 sym_adjust_vis(Sym_desc *sdp, Ofl_desc *ofl)
796 {
797 	Word	symvis, oflags = ofl->ofl_flags, oflags1 = ofl->ofl_flags1;
798 	Sym	*sym = sdp->sd_sym;
799 
800 	if ((sdp->sd_ref == REF_REL_NEED) && (sdp->sd_shndx != SHN_UNDEF)) {
801 		/*
802 		 * If scoping is enabled, reduce any nonversioned global
803 		 * symbols (any symbol that has been processed for relocations
804 		 * will have already had this same reduction test applied).
805 		 * Indicate that the symbol has been reduced as it may be
806 		 * necessary to print these symbols later.
807 		 */
808 		if (((oflags & FLG_OF_AUTOLCL) ||
809 		    (oflags1 & FLG_OF1_AUTOELM)) &&
810 		    ((sdp->sd_flags1 & MSK_SY1_DEFINED) == 0)) {
811 
812 			sdp->sd_flags |= FLG_SY_REDUCED;
813 			sdp->sd_flags1 |= FLG_SY1_LOCL;
814 
815 			if (ELF_ST_VISIBILITY(sym->st_other) != STV_INTERNAL)
816 				sym->st_other = STV_HIDDEN |
817 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
818 
819 			if (ofl->ofl_flags1 &
820 			    (FLG_OF1_REDLSYM | FLG_OF1_AUTOELM))
821 				sdp->sd_flags1 |= FLG_SY1_ELIM;
822 		}
823 
824 		/*
825 		 * If '-Bsymbolic' is in effect - then bind all global symbols
826 		 * 'symbolically' and assign the STV_PROTECTED visibility
827 		 * attribute.
828 		 */
829 		if ((oflags & FLG_OF_SYMBOLIC) &&
830 		    ((sdp->sd_flags1 & FLG_SY1_LOCL) == 0)) {
831 
832 			sdp->sd_flags1 |= FLG_SY1_PROT;
833 			if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT)
834 				sym->st_other = STV_PROTECTED |
835 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
836 		}
837 	}
838 
839 	/*
840 	 * Check to see if the symbol visibility needs to be adjusted due to any
841 	 * STV_* symbol attributes being set.
842 	 *
843 	 *    STV_PROTECTED ==	symbolic binding
844 	 *    STV_INTERNAL ==	reduce to local
845 	 *    STV_HIDDEN ==	reduce to local
846 	 *
847 	 * Note, UNDEF symbols can be assigned a visibility, thus the refencing
848 	 * code can be dependent on this visibility.  Here, by only ignoring
849 	 * REF_DYN_SEEN symbol definitions we can be assigning a visibility to
850 	 * REF_DYN_NEED.  If the protected, or local assignment is made to
851 	 * a REF_DYN_NEED symbol, it will be caught later as an illegal
852 	 * visibility.
853 	 */
854 	if (!(oflags & FLG_OF_RELOBJ) && (sdp->sd_ref != REF_DYN_SEEN) &&
855 	    (symvis = ELF_ST_VISIBILITY(sym->st_other))) {
856 		if (symvis == STV_PROTECTED)
857 			sdp->sd_flags1 |= FLG_SY1_PROT;
858 		else if ((symvis == STV_INTERNAL) || (symvis == STV_HIDDEN))
859 			sdp->sd_flags1 |= FLG_SY1_LOCL;
860 	}
861 
862 	/*
863 	 * Indicate that this symbol has had it's visibility checked so that
864 	 * we don't need to do this investigation again.
865 	 */
866 	sdp->sd_flags |= FLG_SY_VISIBLE;
867 }
868 
869 /*
870  * After all symbol table input processing has been finished, and all relocation
871  * counting has been carried out (ie. no more symbols will be read, generated,
872  * or modified), validate and count the relevant entries:
873  *
874  *	o	check and print any undefined symbols remaining.  Note that
875  *		if a symbol has been defined by virtue of the inclusion of
876  *		an implicit shared library, it is still classed as undefined.
877  *
878  * 	o	count the number of global needed symbols together with the
879  *		size of their associated name strings (if scoping has been
880  *		indicated these symbols may be reduced to locals).
881  *
882  *	o	establish the size and alignment requirements for the global
883  *		.bss section (the alignment of this section is based on the
884  *		first symbol that it will contain).
885  */
886 uintptr_t
887 sym_validate(Ofl_desc *ofl)
888 {
889 	Sym_avlnode	*sav;
890 	Sym_desc	*sdp;
891 	Sym		*sym;
892 	Word		oflags = ofl->ofl_flags;
893 	Word		undef = 0, needed = 0, verdesc = 0;
894 	Xword		bssalign = 0, tlsalign = 0;
895 	Xword		bsssize = 0, tlssize = 0;
896 
897 	/*
898 	 * If a symbol is undefined and this link-edit calls for no undefined
899 	 * symbols to remain (this is the default case when generating an
900 	 * executable but can be enforced for any object using -z defs), the
901 	 * symbol is classified as undefined and a fatal error condition will
902 	 * be indicated.
903 	 *
904 	 * If the symbol is undefined and we're creating a shared object with
905 	 * the -Bsymbolic flag, then the symbol is also classified as undefined
906 	 * and a warning condition will be indicated.
907 	 */
908 	if ((oflags & (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC)) ==
909 	    (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC))
910 		undef = FLG_OF_WARN;
911 	if (oflags & FLG_OF_NOUNDEF)
912 		undef = FLG_OF_FATAL;
913 
914 	/*
915 	 * If the symbol is referenced from an implicitly included shared object
916 	 * (ie. it's not on the NEEDED list) then the symbol is also classified
917 	 * as undefined and a fatal error condition will be indicated.
918 	 */
919 	if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ))
920 		needed = FLG_OF_FATAL;
921 
922 	/*
923 	 * If the output image is being versioned all symbol definitions must be
924 	 * associated with a version.  Any symbol that isn't is classified as
925 	 * undefined and a fatal error condition will be indicated.
926 	 */
927 	if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL))
928 		verdesc = FLG_OF_FATAL;
929 
930 	/*
931 	 * Collect and validate the globals from the internal symbol table.
932 	 */
933 	for (sav = avl_first(&ofl->ofl_symavl); sav;
934 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
935 		Is_desc *	isp;
936 		int		shndx, undeferr = 0;
937 
938 		sdp = sav->sav_symdesc;
939 
940 		/*
941 		 * If undefined symbols are allowed ignore any symbols that are
942 		 * not needed.
943 		 */
944 		if (!(oflags & FLG_OF_NOUNDEF) &&
945 		    (sdp->sd_ref == REF_DYN_SEEN))
946 			continue;
947 
948 		/*
949 		 * If the symbol originates from an external or parent mapfile
950 		 * reference and hasn't been matched to a reference from a
951 		 * relocatable object, ignore it.
952 		 */
953 		if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) &&
954 		    ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) {
955 			sdp->sd_flags |= FLG_SY_INVALID;
956 			continue;
957 		}
958 
959 		sym = sdp->sd_sym;
960 		shndx = sdp->sd_shndx;
961 
962 		/*
963 		 * Sanity check TLS.
964 		 */
965 		if ((ELF_ST_TYPE(sym->st_info) == STT_TLS) &&
966 		    (sym->st_size != 0) && (shndx != SHN_UNDEF) &&
967 		    (shndx != SHN_COMMON)) {
968 			Is_desc *	isp = sdp->sd_isc;
969 			Ifl_desc *	ifl = sdp->sd_file;
970 
971 			if ((isp == 0) || (isp->is_shdr == 0) ||
972 			    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
973 				eprintf(ERR_FATAL, MSG_INTL(MSG_SYM_TLS),
974 				    demangle(sdp->sd_name), ifl->ifl_name);
975 				ofl->ofl_flags |= FLG_OF_FATAL;
976 				continue;
977 			}
978 		}
979 
980 		if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)
981 			sym_adjust_vis(sdp, ofl);
982 
983 		if ((sdp->sd_flags & FLG_SY_REDUCED) &&
984 		    (oflags & FLG_OF_PROCRED)) {
985 			DBG_CALL(Dbg_syms_reduce(DBG_SYM_REDUCE_GLOBAL,
986 			    (ofl->ofl_ehdr) ? ofl->ofl_ehdr :
987 			    &def_ehdr, sdp, 0, 0));
988 		}
989 
990 		/*
991 		 * If building a shared object or executable, and this is a
992 		 * non-weak UNDEF symbol with reduced visibility (STV_*), then
993 		 * give a fatal error.
994 		 */
995 		if (!(oflags & FLG_OF_RELOBJ) &&
996 		    ELF_ST_VISIBILITY(sym->st_other) && (shndx == SHN_UNDEF) &&
997 		    (ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
998 			sym_undef_entry(sdp, BNDLOCAL);
999 			ofl->ofl_flags |= FLG_OF_FATAL;
1000 			continue;
1001 		}
1002 
1003 		/*
1004 		 * If this symbol is defined in a non-allocatable section,
1005 		 * reduce it to local symbol.
1006 		 */
1007 		if (((isp = sdp->sd_isc) != 0) && isp->is_shdr &&
1008 		    ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) {
1009 			sdp->sd_flags |= FLG_SY_REDUCED;
1010 			sdp->sd_flags1 |= FLG_SY1_LOCL;
1011 		}
1012 
1013 		/*
1014 		 * If this symbol originated as a SHN_SUNW_IGNORE, it will have
1015 		 * been processed as an SHN_UNDEF.  Return the symbol to its
1016 		 * original index for validation, and propagation to the output
1017 		 * file.
1018 		 */
1019 		if (sdp->sd_flags1 & FLG_SY1_IGNORE)
1020 			sdp->sd_shndx = shndx = SHN_SUNW_IGNORE;
1021 
1022 		if (undef) {
1023 			/*
1024 			 * If an non-weak reference remains undefined, or if a
1025 			 * mapfile reference is not bound to the relocatable
1026 			 * objects that make up the object being built, we have
1027 			 * a fatal error.
1028 			 *
1029 			 * The exceptions are symbols which are defined to be
1030 			 * found in the parent (FLG_SY_PARENT), which is really
1031 			 * only meaningful for direct binding, or are defined
1032 			 * external (FLG_SY_EXTERN) so as to suppress -zdefs
1033 			 * errors.
1034 			 *
1035 			 * Register symbols are always allowed to be UNDEF.
1036 			 *
1037 			 * Note that we don't include references created via -u
1038 			 * in the same shared object binding test.  This is for
1039 			 * backward compatibility, in that a number of archive
1040 			 * makefile rules used -u to cause archive extraction.
1041 			 * These same rules have been cut and pasted to apply
1042 			 * to shared objects, and thus although the -u reference
1043 			 * is redundant, flagging it as fatal could cause some
1044 			 * build to fail.  Also we have documented the use of
1045 			 * -u as a mechanism to cause binding to weak version
1046 			 * definitions, thus giving users an error condition
1047 			 * would be incorrect.
1048 			 */
1049 			if (!(sdp->sd_flags & FLG_SY_REGSYM) &&
1050 			    ((shndx == SHN_UNDEF) &&
1051 			    ((ELF_ST_BIND(sym->st_info) != STB_WEAK) &&
1052 			    ((sdp->sd_flags &
1053 			    (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) ||
1054 			    (((sdp->sd_flags &
1055 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
1056 			    FLG_SY_MAPREF) &&
1057 			    ((sdp->sd_flags1 & (FLG_SY1_LOCL |
1058 			    FLG_SY1_PROT)) == 0)))) {
1059 				sym_undef_entry(sdp, UNDEF);
1060 				ofl->ofl_flags |= undef;
1061 				undeferr = 1;
1062 			}
1063 
1064 		} else {
1065 			/*
1066 			 * For building things like shared objects (or anything
1067 			 * -znodefs), undefined symbols are allowed.
1068 			 *
1069 			 * If a mapfile reference remains undefined the user
1070 			 * would probably like a warning at least (they've
1071 			 * usually mis-spelt the reference).  Refer to the above
1072 			 * comments for discussion on -u references, which
1073 			 * are not tested for in the same manner.
1074 			 */
1075 			if ((sdp->sd_flags &
1076 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
1077 			    FLG_SY_MAPREF) {
1078 				sym_undef_entry(sdp, UNDEF);
1079 				ofl->ofl_flags |= FLG_OF_WARN;
1080 				undeferr = 1;
1081 			}
1082 		}
1083 
1084 		/*
1085 		 * If this symbol comes from a dependency mark the dependency
1086 		 * as required (-z ignore can result in unused dependencies
1087 		 * being dropped).  If we need to record dependency versioning
1088 		 * information indicate what version of the needed shared object
1089 		 * this symbol is part of.  Flag the symbol as undefined if it
1090 		 * has not been made available to us.
1091 		 */
1092 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1093 		    (!(sdp->sd_flags & FLG_SY_REFRSD))) {
1094 			sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD;
1095 
1096 			/*
1097 			 * Capture that we've bound to a symbol that doesn't
1098 			 * allow being directly bound to.
1099 			 */
1100 			if (sdp->sd_flags1 & FLG_SY1_NDIR)
1101 				ofl->ofl_flags1 |= FLG_OF1_NDIRECT;
1102 
1103 			if (sdp->sd_file->ifl_vercnt) {
1104 				int		vndx;
1105 				Ver_index *	vip;
1106 
1107 				vndx = sdp->sd_aux->sa_dverndx;
1108 				vip = &sdp->sd_file->ifl_verndx[vndx];
1109 				if (vip->vi_flags & FLG_VER_AVAIL) {
1110 					vip->vi_flags |= FLG_VER_REFER;
1111 				} else {
1112 					sym_undef_entry(sdp, NOTAVAIL);
1113 					ofl->ofl_flags |= FLG_OF_FATAL;
1114 					continue;
1115 				}
1116 			}
1117 		}
1118 
1119 		/*
1120 		 * Test that we do not bind to symbol supplied from an implicit
1121 		 * shared object.  If a binding is from a weak reference it can
1122 		 * be ignored.
1123 		 */
1124 		if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) &&
1125 		    (sdp->sd_ref == REF_DYN_NEED) &&
1126 		    (sdp->sd_flags & FLG_SY_NOTAVAIL)) {
1127 			sym_undef_entry(sdp, IMPLICIT);
1128 			ofl->ofl_flags |= needed;
1129 			continue;
1130 		}
1131 
1132 		/*
1133 		 * Test that a symbol isn't going to be reduced to local scope
1134 		 * which actually wants to bind to a shared object - if so it's
1135 		 * a fatal error.
1136 		 */
1137 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1138 		    (sdp->sd_flags1 & (FLG_SY1_LOCL | FLG_SY1_PROT))) {
1139 			sym_undef_entry(sdp, BNDLOCAL);
1140 			ofl->ofl_flags |= FLG_OF_FATAL;
1141 			continue;
1142 		}
1143 
1144 		/*
1145 		 * If the output image is to be versioned then all symbol
1146 		 * definitions must be associated with a version.
1147 		 */
1148 		if (verdesc && (sdp->sd_ref == REF_REL_NEED) &&
1149 		    (shndx != SHN_UNDEF) &&
1150 		    (!(sdp->sd_flags1 & FLG_SY1_LOCL)) &&
1151 		    (sdp->sd_aux->sa_overndx == 0)) {
1152 			sym_undef_entry(sdp, NOVERSION);
1153 			ofl->ofl_flags |= verdesc;
1154 			continue;
1155 		}
1156 
1157 		/*
1158 		 * If we don't need the symbol there's no need to process it
1159 		 * any further.
1160 		 */
1161 		if (sdp->sd_ref == REF_DYN_SEEN)
1162 			continue;
1163 
1164 		/*
1165 		 * Calculate the size and alignment requirements for the global
1166 		 * .bss and .tls sections.  If we're building a relocatable
1167 		 * object only account for scoped COMMON symbols (these will
1168 		 * be converted to .bss references).
1169 		 *
1170 		 * For partially initialized symbol,
1171 		 *  if it is expanded, it goes to sunwdata1.
1172 		 *  if it is local, it goes to .bss.
1173 		 *  if the output is shared object, it goes to .sunwbss.
1174 		 *
1175 		 * Also refer to make_mvsections() in sunwmove.c
1176 		 */
1177 		if ((shndx == SHN_COMMON) && ((!(oflags & FLG_OF_RELOBJ)) ||
1178 		    ((sdp->sd_flags1 & FLG_SY1_LOCL) &&
1179 		    (oflags & FLG_OF_PROCRED)))) {
1180 			int countbss = 0;
1181 
1182 			if (sdp->sd_psyminfo == 0) {
1183 				countbss = 1;
1184 			} else if ((sdp->sd_flags & FLG_SY_PAREXPN) != 0) {
1185 				countbss = 0;
1186 			} else if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1187 				countbss = 1;
1188 			} else if ((ofl->ofl_flags & FLG_OF_SHAROBJ) != 0) {
1189 				countbss = 0;
1190 			} else
1191 				countbss = 1;
1192 
1193 			if (countbss) {
1194 				Xword * size, * align;
1195 
1196 				if (ELF_ST_TYPE(sym->st_info) != STT_TLS) {
1197 					size = &bsssize;
1198 					align = &bssalign;
1199 				} else {
1200 					size = &tlssize;
1201 					align = &tlsalign;
1202 				}
1203 				*size = (Xword)S_ROUND(*size, sym->st_value) +
1204 					sym->st_size;
1205 				if (sym->st_value > *align)
1206 					*align = sym->st_value;
1207 			}
1208 		}
1209 
1210 		/*
1211 		 * If a symbol was referenced via the command line
1212 		 * (ld -u <>, ...), then this counts as a reference against the
1213 		 * symbol. Mark any section that symbol is defined in.
1214 		 */
1215 		if (((isp = sdp->sd_isc) != 0) &&
1216 		    (sdp->sd_flags & FLG_SY_CMDREF)) {
1217 			isp->is_flags |= FLG_IS_SECTREF;
1218 			isp->is_file->ifl_flags |= FLG_IF_FILEREF;
1219 		}
1220 
1221 		/*
1222 		 * Update the symbol count and the associated name string size.
1223 		 * If scoping is in effect for this symbol assign it will be
1224 		 * assigned to the .symtab/.strtab sections.
1225 		 */
1226 		if ((sdp->sd_flags1 & FLG_SY1_LOCL) &&
1227 		    (oflags & FLG_OF_PROCRED)) {
1228 			/*
1229 			 * If symbol gets eliminated count it.
1230 			 *
1231 			 * If symbol gets reduced to local,
1232 			 * count it's size for the .symtab.
1233 			 */
1234 			if (sdp->sd_flags1 & FLG_SY1_ELIM) {
1235 				ofl->ofl_elimcnt++;
1236 			} else {
1237 				ofl->ofl_scopecnt++;
1238 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1239 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
1240 				    sdp->sd_name) == -1))
1241 					return (S_ERROR);
1242 			}
1243 		} else {
1244 			ofl->ofl_globcnt++;
1245 
1246 			/*
1247 			 * If global direct bindings are in effect, or this
1248 			 * symbol has bound to a dependency which was specified
1249 			 * as requiring direct bindings, and it hasn't
1250 			 * explicitly been defined as a non-direct binding
1251 			 * symbol, mark it.
1252 			 */
1253 			if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp &&
1254 			    (isp->is_file->ifl_flags & FLG_IF_DIRECT))) &&
1255 			    ((sdp->sd_flags1 & FLG_SY1_NDIR) == 0))
1256 				sdp->sd_flags1 |= FLG_SY1_DIR;
1257 
1258 			/*
1259 			 * Insert the symbol name.
1260 			 */
1261 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1262 			    sym->st_name) {
1263 				if (st_insert(ofl->ofl_strtab,
1264 				    sdp->sd_name) == -1)
1265 					return (S_ERROR);
1266 
1267 				if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
1268 				    (st_insert(ofl->ofl_dynstrtab,
1269 				    sdp->sd_name) == -1))
1270 					return (S_ERROR);
1271 			}
1272 
1273 			/*
1274 			 * If this section offers a global symbol - record that
1275 			 * fact.
1276 			 */
1277 			if (isp) {
1278 				isp->is_flags |= FLG_IS_SECTREF;
1279 				isp->is_file->ifl_flags |= FLG_IF_FILEREF;
1280 			}
1281 		}
1282 	}
1283 
1284 	/*
1285 	 * If we've encountered a fatal error during symbol validation then
1286 	 * return now.
1287 	 */
1288 	if (ofl->ofl_flags & FLG_OF_FATAL)
1289 		return (1);
1290 
1291 	/*
1292 	 * Now that symbol resolution is completed, scan any register symbols.
1293 	 * From now on, we're only interested in those that contribute to the
1294 	 * output file.
1295 	 */
1296 	if (ofl->ofl_regsyms) {
1297 		int	ndx;
1298 
1299 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1300 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1301 				continue;
1302 			if (sdp->sd_ref != REF_REL_NEED) {
1303 				ofl->ofl_regsyms[ndx] = 0;
1304 				continue;
1305 			}
1306 
1307 			ofl->ofl_regsymcnt++;
1308 			if (sdp->sd_sym->st_name == 0)
1309 				sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY);
1310 
1311 			if ((sdp->sd_flags1 & FLG_SY1_LOCL) ||
1312 			    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL))
1313 				ofl->ofl_lregsymcnt++;
1314 		}
1315 	}
1316 
1317 	/*
1318 	 * Generate the .bss section now that we know its size and alignment.
1319 	 */
1320 	if (bsssize || !(oflags & FLG_OF_RELOBJ)) {
1321 		if (make_bss(ofl, bsssize, bssalign, 0) == S_ERROR)
1322 			return (S_ERROR);
1323 	}
1324 	if (tlssize) {
1325 		if (make_bss(ofl, tlssize, tlsalign, 1) == S_ERROR)
1326 			return (S_ERROR);
1327 	}
1328 
1329 	/*
1330 	 * Determine what entry point symbol we need, and if found save its
1331 	 * symbol descriptor so that we can update the ELF header entry with the
1332 	 * symbols value later (see update_oehdr).  Make sure the symbol is
1333 	 * tagged to insure its update in case -s is in effect.  Use any -e
1334 	 * option first, or the default entry points `_start' and `main'.
1335 	 */
1336 	if (ofl->ofl_entry) {
1337 		if (((sdp = sym_find(ofl->ofl_entry, SYM_NOHASH, 0, ofl)) ==
1338 		    NULL) || (sdp->sd_ref != REF_REL_NEED)) {
1339 			eprintf(ERR_FATAL, MSG_INTL(MSG_SYM_ENTRY),
1340 			    demangle((char *)ofl->ofl_entry));
1341 			return (S_ERROR);
1342 		}
1343 		ofl->ofl_entry = (void *)sdp;
1344 		sdp->sd_flags |= FLG_SY_UPREQD;
1345 		if (sdp->sd_isc) {
1346 			sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1347 			sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1348 		}
1349 	} else if (((sdp = sym_find(MSG_ORIG(MSG_SYM_START),
1350 	    SYM_NOHASH, 0, ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED)) {
1351 		ofl->ofl_entry = (void *)sdp;
1352 		sdp->sd_flags |= FLG_SY_UPREQD;
1353 		if (sdp->sd_isc) {
1354 			sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1355 			sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1356 		}
1357 	} else if (((sdp = sym_find(MSG_ORIG(MSG_SYM_MAIN),
1358 	    SYM_NOHASH, 0, ofl)) != NULL) && (sdp->sd_ref == REF_REL_NEED)) {
1359 		ofl->ofl_entry = (void *)sdp;
1360 		sdp->sd_flags |= FLG_SY_UPREQD;
1361 		if (sdp->sd_isc) {
1362 			sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1363 			sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1364 		}
1365 	}
1366 
1367 	/*
1368 	 * If ld -zdtrace=<sym> was given, then validate that the
1369 	 * symbol is defined within the current object being built.
1370 	 */
1371 	if ((sdp = ofl->ofl_dtracesym) != 0) {
1372 		if ((sdp->sd_ref != REF_REL_NEED) ||
1373 		    (sdp->sd_shndx == SHN_UNDEF)) {
1374 			eprintf(ERR_FATAL, MSG_INTL(MSG_SYM_DTRACE),
1375 				demangle((char *)sdp->sd_name));
1376 			return (S_ERROR);
1377 		}
1378 		sdp->sd_flags |= FLG_SY_UPREQD;
1379 		if (sdp->sd_isc) {
1380 			sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
1381 			sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
1382 		}
1383 	}
1384 
1385 	/*
1386 	 * If we're required to record any needed dependencies versioning
1387 	 * information calculate it now that all symbols have been validated.
1388 	 */
1389 	if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
1390 		return (vers_check_need(ofl));
1391 	else
1392 		return (1);
1393 }
1394 
1395 /*
1396  * qsort(3c) comparison function.  As an optimization for associating weak
1397  * symbols to their strong counterparts sort global symbols according to their
1398  * address and binding.
1399  */
1400 static int
1401 compare(const void * sdpp1, const void * sdpp2)
1402 {
1403 	Sym_desc *	sdp1 = *((Sym_desc **)sdpp1);
1404 	Sym_desc *	sdp2 = *((Sym_desc **)sdpp2);
1405 	Sym *		sym1, * sym2;
1406 	uchar_t		bind1, bind2;
1407 
1408 	/*
1409 	 * Symbol descriptors may be zero, move these to the front of the
1410 	 * sorted array.
1411 	 */
1412 	if (sdp1 == 0)
1413 		return (-1);
1414 	if (sdp2 == 0)
1415 		return (1);
1416 
1417 	sym1 = sdp1->sd_sym;
1418 	sym2 = sdp2->sd_sym;
1419 
1420 	/*
1421 	 * Compare the symbols value (address).
1422 	 */
1423 	if (sym1->st_value > sym2->st_value)
1424 		return (1);
1425 	if (sym1->st_value < sym2->st_value)
1426 		return (-1);
1427 
1428 	bind1 = ELF_ST_BIND(sym1->st_info);
1429 	bind2 = ELF_ST_BIND(sym2->st_info);
1430 
1431 	/*
1432 	 * If two symbols have the same address place the weak symbol before
1433 	 * any strong counterpart.
1434 	 */
1435 	if (bind1 > bind2)
1436 		return (-1);
1437 	if (bind1 < bind2)
1438 		return (1);
1439 
1440 	return (0);
1441 }
1442 
1443 
1444 /*
1445  * Process the symbol table for the specified input file.  At this point all
1446  * input sections from this input file have been assigned an input section
1447  * descriptor which is saved in the `ifl_isdesc' array.
1448  *
1449  *	o	local symbols are saved (as is) if the input file is a
1450  *		relocatable object
1451  *
1452  *	o	global symbols are added to the linkers internal symbol
1453  *		table if they are not already present, otherwise a symbol
1454  *		resolution function is called upon to resolve the conflict.
1455  */
1456 uintptr_t
1457 sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1458 {
1459 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
1460 	Word		*symshndx = 0;
1461 	Shdr		*shdr = isc->is_shdr;
1462 	Sym_desc	*sdp;
1463 	size_t		strsize;
1464 	char		*strs;
1465 	uchar_t		type, bind;
1466 	Word		ndx, hash, local, total;
1467 	Half		etype = ifl->ifl_ehdr->e_type;
1468 	const char	*symsecname, *strsecname;
1469 	avl_index_t	where;
1470 
1471 	/*
1472 	 * Its possible that a file may contain more that one symbol table,
1473 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
1474 	 * table (here, we assume .dynsym comes before .symtab).
1475 	 */
1476 	if (ifl->ifl_symscnt)
1477 		return (1);
1478 
1479 	if (isc->is_symshndx)
1480 		symshndx = isc->is_symshndx->is_indata->d_buf;
1481 
1482 	DBG_CALL(Dbg_syms_process(ifl));
1483 
1484 	if (isc->is_name)
1485 		symsecname = isc->is_name;
1486 	else
1487 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
1488 
1489 	/*
1490 	 * From the symbol tables section header information determine which
1491 	 * strtab table is needed to locate the actual symbol names.
1492 	 */
1493 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
1494 		ndx = shdr->sh_link;
1495 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
1496 			eprintf(ERR_FATAL, MSG_INTL(MSG_FIL_INVSHLINK),
1497 			    ifl->ifl_name, symsecname, EC_XWORD(ndx));
1498 			return (S_ERROR);
1499 		}
1500 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
1501 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
1502 		if (ifl->ifl_isdesc[ndx]->is_name)
1503 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
1504 		else
1505 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
1506 	} else {
1507 		/*
1508 		 * There is no string table section in this input file
1509 		 * although there are symbols in this symbol table section.
1510 		 * This means that these symbols do not have names.
1511 		 * Currently, only scratch register symbols are allowed
1512 		 * not to have names.
1513 		 */
1514 		strsize = 0;
1515 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
1516 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
1517 	}
1518 
1519 	/*
1520 	 * Determine the number of local symbols together with the total
1521 	 * number we have to process.
1522 	 */
1523 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
1524 	local = shdr->sh_info;
1525 
1526 	/*
1527 	 * Allocate a symbol table index array and a local symbol array
1528 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
1529 	 * array).  If we are dealing with a relocatable object, allocate the
1530 	 * local symbol descriptors.  If this isn't a relocatable object we
1531 	 * still have to process any shared object locals to determine if any
1532 	 * register symbols exist.  Although these aren't added to the output
1533 	 * image, they are used as part of symbol resolution.
1534 	 */
1535 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
1536 	    sizeof (Sym_desc *)))) == 0)
1537 		return (S_ERROR);
1538 	if ((etype == ET_REL) && local) {
1539 		if ((ifl->ifl_locs =
1540 		    libld_calloc(sizeof (Sym_desc), local)) == 0)
1541 			return (S_ERROR);
1542 		/* LINTED */
1543 		ifl->ifl_locscnt = (Word)local;
1544 	}
1545 	ifl->ifl_symscnt = total;
1546 
1547 	/*
1548 	 * If there are local symbols to save add them to the symbol table
1549 	 * index array.
1550 	 */
1551 	if (local) {
1552 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
1553 			Word		shndx, sdflags = FLG_SY_CLEAN;
1554 			const char	*name;
1555 			Sym_desc	*rsdp;
1556 
1557 			/*
1558 			 * Determine the associated section index.
1559 			 */
1560 			if (symshndx && (sym->st_shndx == SHN_XINDEX))
1561 				shndx = symshndx[ndx];
1562 			else if ((shndx = sym->st_shndx) >= SHN_LORESERVE)
1563 				sdflags |= FLG_SY_SPECSEC;
1564 
1565 			/*
1566 			 * Check if st_name has a valid value or not.
1567 			 */
1568 			if ((name = string(ifl, sym, strs, strsize, ndx,
1569 			    shndx, symsecname, strsecname, &sdflags)) == 0) {
1570 				ofl->ofl_flags |= FLG_OF_FATAL;
1571 				continue;
1572 			}
1573 
1574 			/*
1575 			 * If this local symbol table originates from a shared
1576 			 * object, then we're only interested in recording
1577 			 * register symbols.  As local symbol descriptors aren't
1578 			 * allocated for shared objects, one will be allocated
1579 			 * to associated with the register symbol.  This symbol
1580 			 * won't become part of the output image, but we must
1581 			 * process it to test for register conflicts.
1582 			 */
1583 			rsdp = sdp = 0;
1584 			if (sdflags & FLG_SY_REGSYM) {
1585 				if ((rsdp = reg_find(sym, ofl)) != 0) {
1586 					/*
1587 					 * The fact that another register def-
1588 					 * inition has been found is fatal.
1589 					 * Call the verification routine to get
1590 					 * the error message and move on.
1591 					 */
1592 					(void) reg_check(rsdp, sym, name,
1593 					    ifl, ofl);
1594 					continue;
1595 				}
1596 
1597 				if (etype == ET_DYN) {
1598 					if ((sdp = libld_calloc(
1599 					    sizeof (Sym_desc), 1)) == 0)
1600 						return (S_ERROR);
1601 					sdp->sd_ref = REF_DYN_SEEN;
1602 				}
1603 			} else if (etype == ET_DYN)
1604 				continue;
1605 
1606 			/*
1607 			 * Fill in the remaining symbol descriptor information.
1608 			 */
1609 			if (sdp == 0) {
1610 				sdp = &(ifl->ifl_locs[ndx]);
1611 				sdp->sd_ref = REF_REL_NEED;
1612 			}
1613 			if (rsdp == 0) {
1614 				sdp->sd_name = name;
1615 				sdp->sd_sym = sym;
1616 				sdp->sd_shndx = shndx;
1617 				sdp->sd_flags = sdflags;
1618 				sdp->sd_file = ifl;
1619 				ifl->ifl_oldndx[ndx] = sdp;
1620 			}
1621 
1622 			DBG_CALL(Dbg_syms_entry(ndx, sdp));
1623 
1624 			/*
1625 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
1626 			 * so as to simplify future processing.
1627 			 */
1628 			if (shndx == SHN_SUNW_IGNORE) {
1629 				sdp->sd_shndx = shndx = SHN_UNDEF;
1630 				sdp->sd_flags1 |=
1631 				    (FLG_SY1_IGNORE | FLG_SY1_ELIM);
1632 			}
1633 
1634 			/*
1635 			 * Process any register symbols.
1636 			 */
1637 			if (sdp->sd_flags & FLG_SY_REGSYM) {
1638 				/*
1639 				 * Add a diagnostic to indicate we've caught a
1640 				 * register symbol, as this can be useful if a
1641 				 * register conflict is later discovered.
1642 				 */
1643 				DBG_CALL(Dbg_syms_entered(ifl->ifl_ehdr,
1644 				    sym, sdp));
1645 
1646 				/*
1647 				 * If this register symbol hasn't already been
1648 				 * recorded, enter it now.
1649 				 */
1650 				if ((rsdp == 0) && (reg_enter(sdp, ofl) == 0))
1651 					return (S_ERROR);
1652 			}
1653 
1654 			/*
1655 			 * Assign an input section.
1656 			 */
1657 			if ((shndx != SHN_UNDEF) &&
1658 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
1659 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
1660 
1661 			/*
1662 			 * If this symbol falls within the range of a section
1663 			 * being discarded, then discard the symbol itself.
1664 			 * There is no reason to keep this local symbol.
1665 			 */
1666 			if (sdp->sd_isc &&
1667 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
1668 				sdp->sd_flags |= FLG_SY_ISDISC;
1669 				DBG_CALL(Dbg_syms_discarded(sdp, sdp->sd_isc));
1670 				continue;
1671 			}
1672 
1673 			/*
1674 			 * Skip any section symbols as new versions of these
1675 			 * will be created.
1676 			 */
1677 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
1678 				if (shndx == SHN_UNDEF) {
1679 					eprintf(ERR_WARNING,
1680 					    MSG_INTL(MSG_SYM_INVSHNDX),
1681 					    demangle(sdp->sd_name),
1682 					    ifl->ifl_name,
1683 					    conv_shndx_str(shndx));
1684 				}
1685 				continue;
1686 			}
1687 
1688 			/*
1689 			 * Sanity check for TLS
1690 			 */
1691 			if ((sym->st_size != 0) &&
1692 			    ((type == STT_TLS) && (shndx != SHN_COMMON))) {
1693 				Is_desc	*isp = sdp->sd_isc;
1694 
1695 				if ((isp == 0) || (isp->is_shdr == 0) ||
1696 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
1697 					eprintf(ERR_FATAL,
1698 					    MSG_INTL(MSG_SYM_TLS),
1699 					    demangle(sdp->sd_name),
1700 					    ifl->ifl_name);
1701 					ofl->ofl_flags |= FLG_OF_FATAL;
1702 					continue;
1703 				}
1704 			}
1705 
1706 			/*
1707 			 * Carry our some basic sanity checks (these are just
1708 			 * some of the erroneous symbol entries we've come
1709 			 * across, there's probably a lot more).  The symbol
1710 			 * will not be carried forward to the output file, which
1711 			 * won't be a problem unless a relocation is required
1712 			 * against it.
1713 			 */
1714 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
1715 			    ((shndx == SHN_COMMON)) ||
1716 			    ((type == STT_FILE) && (shndx != SHN_ABS))) ||
1717 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == 0))) {
1718 				eprintf(ERR_WARNING, MSG_INTL(MSG_SYM_INVSHNDX),
1719 				    demangle(sdp->sd_name), ifl->ifl_name,
1720 				    conv_shndx_str(shndx));
1721 				sdp->sd_isc = NULL;
1722 				sdp->sd_flags |= FLG_SY_INVALID;
1723 				continue;
1724 			}
1725 
1726 			/*
1727 			 * As these local symbols will become part of the output
1728 			 * image, record their number and name string size.
1729 			 * Globals are counted after all input file processing
1730 			 * (and hence symbol resolution) is complete during
1731 			 * sym_validate().
1732 			 */
1733 			if (!(ofl->ofl_flags1 & FLG_OF1_REDLSYM)) {
1734 				ofl->ofl_locscnt++;
1735 
1736 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1737 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
1738 				    sdp->sd_name) == -1))
1739 					return (S_ERROR);
1740 			}
1741 		}
1742 	}
1743 
1744 	/*
1745 	 * Now scan the global symbols entering them in the internal symbol
1746 	 * table or resolving them as necessary.
1747 	 */
1748 	sym = (Sym *)isc->is_indata->d_buf;
1749 	sym += local;
1750 	/* LINTED */
1751 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
1752 		const char	*name;
1753 		Word		shndx, sdflags = 0;
1754 
1755 		/*
1756 		 * Determine the associated section index.
1757 		 */
1758 		if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
1759 			shndx = symshndx[ndx];
1760 		} else {
1761 			shndx = sym->st_shndx;
1762 			if (sym->st_shndx >= SHN_LORESERVE)
1763 				sdflags |= FLG_SY_SPECSEC;
1764 		}
1765 
1766 		/*
1767 		 * Check if st_name has a valid value or not.
1768 		 */
1769 		if ((name = string(ifl, sym, strs, strsize, ndx, shndx,
1770 		    symsecname, strsecname, &sdflags)) == 0) {
1771 			ofl->ofl_flags |= FLG_OF_FATAL;
1772 			continue;
1773 		}
1774 
1775 		/*
1776 		 * The linker itself will generate symbols for _end, _etext,
1777 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
1778 		 * bother entering these symbols from shared objects.  This
1779 		 * results in some wasted resolution processing, which is hard
1780 		 * to feel, but if nothing else, pollutes diagnostic relocation
1781 		 * output.
1782 		 */
1783 		if (name[0] && (etype == ET_DYN) && (sym->st_size == 0) &&
1784 		    (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) &&
1785 		    (name[0] == '_') && ((name[1] == 'e') ||
1786 		    (name[1] == 'D') || (name[1] == 'P')) &&
1787 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
1788 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
1789 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
1790 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
1791 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
1792 			ifl->ifl_oldndx[ndx] = 0;
1793 			continue;
1794 		}
1795 
1796 		/*
1797 		 * Determine and validate the symbols binding.
1798 		 */
1799 		bind = ELF_ST_BIND(sym->st_info);
1800 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
1801 			eprintf(ERR_WARNING, MSG_INTL(MSG_SYM_NONGLOB),
1802 			    demangle(name), ifl->ifl_name,
1803 			    conv_info_bind_str(bind));
1804 			continue;
1805 		}
1806 
1807 		/*
1808 		 * If this symbol falls within the range of a section being
1809 		 * discarded, then discard the symbol itself.
1810 		 */
1811 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
1812 		    (shndx != SHN_UNDEF)) {
1813 			Is_desc	*isp;
1814 
1815 			if (shndx >= ifl->ifl_shnum) {
1816 				/*
1817 				 * Carry our some basic sanity checks
1818 				 * The symbol will not be carried forward to
1819 				 * the output file, which won't be a problem
1820 				 * unless a relocation is required against it.
1821 				 */
1822 				eprintf(ERR_WARNING, MSG_INTL(MSG_SYM_INVSHNDX),
1823 				    demangle(name), ifl->ifl_name,
1824 				    conv_shndx_str(shndx));
1825 				continue;
1826 			}
1827 
1828 			isp = ifl->ifl_isdesc[shndx];
1829 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
1830 				if ((sdp =
1831 				    libld_calloc(sizeof (Sym_desc), 1)) == 0)
1832 					return (S_ERROR);
1833 
1834 				/*
1835 				 * Create a dummy symbol entry so that if we
1836 				 * find any references to this discarded symbol
1837 				 * we can compensate.
1838 				 */
1839 				sdp->sd_name = name;
1840 				sdp->sd_sym = sym;
1841 				sdp->sd_file = ifl;
1842 				sdp->sd_isc = isp;
1843 				sdp->sd_flags = FLG_SY_ISDISC;
1844 				ifl->ifl_oldndx[ndx] = sdp;
1845 
1846 				DBG_CALL(Dbg_syms_discarded(sdp, sdp->sd_isc));
1847 				continue;
1848 			}
1849 		}
1850 
1851 		/*
1852 		 * If the symbol does not already exist in the internal symbol
1853 		 * table add it, otherwise resolve the conflict.  If the symbol
1854 		 * from this file is kept, retain its symbol table index for
1855 		 * possible use in associating a global alias.
1856 		 */
1857 		/* LINTED */
1858 		hash = (Word)elf_hash((const char *)name);
1859 		if ((sdp = sym_find(name, hash, &where, ofl)) == NULL) {
1860 			DBG_CALL(Dbg_syms_global(ndx, name));
1861 			if ((sdp = sym_enter(name, sym, hash, ifl, ofl, ndx,
1862 			    shndx, sdflags, 0, &where)) == (Sym_desc *)S_ERROR)
1863 				return (S_ERROR);
1864 
1865 		} else if (sym_resolve(sdp, sym, ifl, ofl, ndx, shndx,
1866 		    sdflags) == S_ERROR)
1867 			return (S_ERROR);
1868 
1869 		/*
1870 		 * After we've compared a defined symbol in one shared
1871 		 * object, flag the symbol so we don't compare it again.
1872 		 */
1873 		if ((etype == ET_DYN) && (shndx != SHN_UNDEF) &&
1874 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
1875 			sdp->sd_flags |= FLG_SY_SOFOUND;
1876 
1877 		/*
1878 		 * If the symbol is accepted from this file retain the symbol
1879 		 * index for possible use in aliasing.
1880 		 */
1881 		if (sdp->sd_file == ifl)
1882 			sdp->sd_symndx = ndx;
1883 
1884 		ifl->ifl_oldndx[ndx] = sdp;
1885 
1886 		/*
1887 		 * If we've accepted a register symbol, continue to validate
1888 		 * it.
1889 		 */
1890 		if (sdp->sd_flags & FLG_SY_REGSYM) {
1891 			Sym_desc	*rsdp;
1892 
1893 			if ((rsdp = reg_find(sdp->sd_sym, ofl)) == 0) {
1894 				if (reg_enter(sdp, ofl) == 0)
1895 					return (S_ERROR);
1896 			} else if (rsdp != sdp) {
1897 				(void) reg_check(rsdp, sdp->sd_sym,
1898 				    sdp->sd_name, ifl, ofl);
1899 			}
1900 		}
1901 	}
1902 
1903 	/*
1904 	 * If this is a shared object scan the globals one more time and
1905 	 * associate any weak/global associations.  This association is needed
1906 	 * should the weak definition satisfy a reference in the dynamic
1907 	 * executable:
1908 	 *
1909 	 *  o	if the symbol is a data item it will be copied to the
1910 	 *	executables address space, thus we must also reassociate the
1911 	 *	alias symbol with its new location in the executable.
1912 	 *
1913 	 *  o	if the symbol is a function then we may need to promote	the
1914 	 *	symbols binding from undefined weak to undefined, otherwise the
1915 	 *	run-time linker will not generate the correct relocation error
1916 	 *	should the symbol not be found.
1917 	 *
1918 	 * The true association between a weak/strong symbol pair is that both
1919 	 * symbol entries are identical, thus first we created a sorted symbol
1920 	 * list keyed off of the symbols value (if the value is the same chances
1921 	 * are the rest of the symbols data is).  This list is then scanned for
1922 	 * weak symbols, and if one is found then any strong association will
1923 	 * exist in the following entries.  Thus we just have to scan one
1924 	 * (typical single alias) or more (in the uncommon instance of multiple
1925 	 * weak to strong associations) entries to determine if a match exists.
1926 	 */
1927 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
1928 		Sym_desc **	sort;
1929 		size_t		size = (total - local) * sizeof (Sym_desc *);
1930 
1931 		if ((sort = libld_malloc(size)) == 0)
1932 			return (S_ERROR);
1933 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], size);
1934 
1935 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
1936 
1937 		for (ndx = 0; ndx < (total - local); ndx++) {
1938 			Sym_desc *	wsdp = sort[ndx];
1939 			Sym *		wsym;
1940 			int		sndx;
1941 
1942 			if (wsdp == 0)
1943 				continue;
1944 
1945 			wsym = wsdp->sd_sym;
1946 
1947 			if ((ELF_ST_BIND(wsym->st_info) != STB_WEAK) ||
1948 			    (wsdp->sd_shndx == SHN_UNDEF) ||
1949 			    (wsdp->sd_flags & FLG_SY_SPECSEC))
1950 				continue;
1951 
1952 			/*
1953 			 * We have a weak symbol, if it has a strong alias it
1954 			 * will have been sorted to one of the following sort
1955 			 * table entries.  Note that we could have multiple weak
1956 			 * symbols aliased to one strong (if this occurs then
1957 			 * the strong symbol only maintains one alias back to
1958 			 * the last weak).
1959 			 */
1960 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
1961 				Sym_desc *	ssdp = sort[sndx];
1962 				Sym *		ssym;
1963 
1964 				if (ssdp == 0)
1965 					break;
1966 
1967 				ssym = ssdp->sd_sym;
1968 
1969 				if (wsym->st_value != ssym->st_value)
1970 					break;
1971 
1972 				if ((ssdp->sd_file == ifl) &&
1973 				    (wsdp->sd_file == ifl) &&
1974 				    (wsym->st_size == ssym->st_size) &&
1975 				    (ssdp->sd_shndx != SHN_UNDEF) &&
1976 				    (ELF_ST_BIND(ssym->st_info) != STB_WEAK) &&
1977 				    ((ssdp->sd_flags & FLG_SY_SPECSEC) == 0)) {
1978 					ssdp->sd_aux->sa_linkndx =
1979 					    (Word)wsdp->sd_symndx;
1980 					wsdp->sd_aux->sa_linkndx =
1981 					    (Word)ssdp->sd_symndx;
1982 					break;
1983 				}
1984 			}
1985 		}
1986 	}
1987 	return (1);
1988 }
1989 
1990 /*
1991  * Add an undefined symbol to the symbol table (ie. from -u name option)
1992  */
1993 Sym_desc *
1994 sym_add_u(const char *name, Ofl_desc *ofl)
1995 {
1996 	Sym		*sym;
1997 	Ifl_desc	*ifl = 0, *_ifl;
1998 	Sym_desc	*sdp;
1999 	Word		hash;
2000 	Listnode	*lnp;
2001 	avl_index_t	where;
2002 	const char	*cmdline = MSG_INTL(MSG_STR_COMMAND);
2003 
2004 	/*
2005 	 * If the symbol reference already exists indicate that a reference
2006 	 * also came from the command line.
2007 	 */
2008 	/* LINTED */
2009 	hash = (Word)elf_hash(name);
2010 	if (sdp = sym_find(name, hash, &where, ofl)) {
2011 		if (sdp->sd_ref == REF_DYN_SEEN)
2012 			sdp->sd_ref = REF_DYN_NEED;
2013 		return (sdp);
2014 	}
2015 
2016 	/*
2017 	 * Determine whether a pseudo input file descriptor exists to represent
2018 	 * the command line, as any global symbol needs an input file descriptor
2019 	 * during any symbol resolution (refer to map_ifl() which provides a
2020 	 * similar method for adding symbols from mapfiles).
2021 	 */
2022 	for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, _ifl))
2023 		if (strcmp(_ifl->ifl_name, cmdline) == 0) {
2024 			ifl = _ifl;
2025 			break;
2026 		}
2027 
2028 	/*
2029 	 * If no descriptor exists create one.
2030 	 */
2031 	if (ifl == 0) {
2032 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) ==
2033 		    (Ifl_desc *)0)
2034 			return ((Sym_desc *)S_ERROR);
2035 		ifl->ifl_name = cmdline;
2036 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
2037 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr),
2038 		    1)) == 0)
2039 			return ((Sym_desc *)S_ERROR);
2040 		ifl->ifl_ehdr->e_type = ET_REL;
2041 
2042 		if (list_appendc(&ofl->ofl_objs, ifl) == 0)
2043 			return ((Sym_desc *)S_ERROR);
2044 	}
2045 
2046 	/*
2047 	 * Allocate a symbol structure and add it to the global symbol table.
2048 	 */
2049 	if ((sym = libld_calloc(sizeof (Sym), 1)) == 0)
2050 		return ((Sym_desc *)S_ERROR);
2051 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
2052 	sym->st_shndx = SHN_UNDEF;
2053 
2054 	DBG_CALL(Dbg_syms_process(ifl));
2055 	DBG_CALL(Dbg_syms_global(0, name));
2056 	sdp = sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF, 0, 0, &where);
2057 	sdp->sd_flags &= ~FLG_SY_CLEAN;
2058 	sdp->sd_flags |= FLG_SY_CMDREF;
2059 
2060 	return (sdp);
2061 }
2062