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