xref: /titanic_44/usr/src/cmd/sgs/libld/common/syms.c (revision 0eb822a1c0c2bea495647510b75f77f0e57633eb)
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 ensure compatibility
515  * with any unexpected use presently in effect, ensure 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 ensures
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 ensure
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 ensure
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 	if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) {
749 
750 		DBG_CALL(Dbg_syms_spec_title(ofl->ofl_lml));
751 
752 		if (sym_add_spec(MSG_ORIG(MSG_SYM_ETEXT),
753 		    MSG_ORIG(MSG_SYM_ETEXT_U), SDAUX_ID_ETEXT,
754 		    FLG_SY1_GLOB, ofl) == S_ERROR)
755 			return (S_ERROR);
756 		if (sym_add_spec(MSG_ORIG(MSG_SYM_EDATA),
757 		    MSG_ORIG(MSG_SYM_EDATA_U), SDAUX_ID_EDATA,
758 		    FLG_SY1_GLOB, ofl) == S_ERROR)
759 			return (S_ERROR);
760 		if (sym_add_spec(MSG_ORIG(MSG_SYM_END),
761 		    MSG_ORIG(MSG_SYM_END_U), SDAUX_ID_END,
762 		    FLG_SY1_GLOB, ofl) == S_ERROR)
763 			return (S_ERROR);
764 		if (sym_add_spec(MSG_ORIG(MSG_SYM_L_END),
765 		    MSG_ORIG(MSG_SYM_L_END_U), SDAUX_ID_END,
766 		    FLG_SY1_LOCL, ofl) == S_ERROR)
767 			return (S_ERROR);
768 		if (sym_add_spec(MSG_ORIG(MSG_SYM_L_START),
769 		    MSG_ORIG(MSG_SYM_L_START_U), SDAUX_ID_START,
770 		    FLG_SY1_LOCL, ofl) == S_ERROR)
771 			return (S_ERROR);
772 
773 		/*
774 		 * Historically we've always produced a _DYNAMIC symbol, even
775 		 * for static executables (in which case its value will be 0).
776 		 */
777 		if (sym_add_spec(MSG_ORIG(MSG_SYM_DYNAMIC),
778 		    MSG_ORIG(MSG_SYM_DYNAMIC_U), SDAUX_ID_DYN,
779 		    FLG_SY1_GLOB, ofl) == S_ERROR)
780 			return (S_ERROR);
781 
782 		if (OFL_ALLOW_DYNSYM(ofl))
783 			if (sym_add_spec(MSG_ORIG(MSG_SYM_PLKTBL),
784 			    MSG_ORIG(MSG_SYM_PLKTBL_U), SDAUX_ID_PLT,
785 			    FLG_SY1_GLOB, ofl) == S_ERROR)
786 				return (S_ERROR);
787 
788 		if (ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH, 0, ofl))
789 			if (sym_add_spec(MSG_ORIG(MSG_SYM_GOFTBL),
790 			    MSG_ORIG(MSG_SYM_GOFTBL_U), SDAUX_ID_GOT,
791 			    FLG_SY1_GLOB, ofl) == S_ERROR)
792 				return (S_ERROR);
793 	}
794 	return (1);
795 }
796 
797 /*
798  * This routine checks to see if a symbols visibility needs to be reduced to
799  * either SYMBOLIC or LOCAL.  This routine can be called from either
800  * reloc_init() or sym_validate().
801  */
802 void
803 ld_sym_adjust_vis(Sym_desc *sdp, Ofl_desc *ofl)
804 {
805 	Word	symvis, oflags = ofl->ofl_flags, oflags1 = ofl->ofl_flags1;
806 	Sym	*sym = sdp->sd_sym;
807 
808 	if ((sdp->sd_ref == REF_REL_NEED) &&
809 	    (sdp->sd_sym->st_shndx != SHN_UNDEF)) {
810 		/*
811 		 * If scoping is enabled, reduce any nonversioned global
812 		 * symbols (any symbol that has been processed for relocations
813 		 * will have already had this same reduction test applied).
814 		 * Indicate that the symbol has been reduced as it may be
815 		 * necessary to print these symbols later.
816 		 */
817 		if (((oflags & FLG_OF_AUTOLCL) ||
818 		    (oflags1 & FLG_OF1_AUTOELM)) &&
819 		    ((sdp->sd_flags1 & MSK_SY1_DEFINED) == 0)) {
820 
821 			sdp->sd_flags |= FLG_SY_REDUCED;
822 			sdp->sd_flags1 |= FLG_SY1_LOCL;
823 
824 			if (ELF_ST_VISIBILITY(sym->st_other) != STV_INTERNAL)
825 				sym->st_other = STV_HIDDEN |
826 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
827 
828 			if (ofl->ofl_flags1 &
829 			    (FLG_OF1_REDLSYM | FLG_OF1_AUTOELM))
830 				sdp->sd_flags1 |= FLG_SY1_ELIM;
831 		}
832 
833 		/*
834 		 * If '-Bsymbolic' is in effect - then bind all global symbols
835 		 * 'symbolically' and assign the STV_PROTECTED visibility
836 		 * attribute.
837 		 */
838 		if ((oflags & FLG_OF_SYMBOLIC) &&
839 		    ((sdp->sd_flags1 & FLG_SY1_LOCL) == 0)) {
840 
841 			sdp->sd_flags1 |= FLG_SY1_PROT;
842 			if (ELF_ST_VISIBILITY(sym->st_other) == STV_DEFAULT)
843 				sym->st_other = STV_PROTECTED |
844 				    (sym->st_other & ~MSK_SYM_VISIBILITY);
845 		}
846 	}
847 
848 	/*
849 	 * Check to see if the symbol visibility needs to be adjusted due to any
850 	 * STV_* symbol attributes being set.
851 	 *
852 	 *    STV_PROTECTED ==	symbolic binding
853 	 *    STV_INTERNAL ==	reduce to local
854 	 *    STV_HIDDEN ==	reduce to local
855 	 *
856 	 * Note, UNDEF symbols can be assigned a visibility, thus the refencing
857 	 * code can be dependent on this visibility.  Here, by only ignoring
858 	 * REF_DYN_SEEN symbol definitions we can be assigning a visibility to
859 	 * REF_DYN_NEED.  If the protected, or local assignment is made to
860 	 * a REF_DYN_NEED symbol, it will be caught later as an illegal
861 	 * visibility.
862 	 */
863 	if (!(oflags & FLG_OF_RELOBJ) && (sdp->sd_ref != REF_DYN_SEEN) &&
864 	    (symvis = ELF_ST_VISIBILITY(sym->st_other))) {
865 		if (symvis == STV_PROTECTED)
866 			sdp->sd_flags1 |= FLG_SY1_PROT;
867 		else if ((symvis == STV_INTERNAL) || (symvis == STV_HIDDEN))
868 			sdp->sd_flags1 |= FLG_SY1_LOCL;
869 	}
870 
871 	/*
872 	 * Indicate that this symbol has had it's visibility checked so that
873 	 * we don't need to do this investigation again.
874 	 */
875 	sdp->sd_flags |= FLG_SY_VISIBLE;
876 }
877 
878 /*
879  * Make sure a symbol definition is local to the object being built.
880  */
881 static int
882 ensure_sym_local(Ofl_desc *ofl, Sym_desc *sdp, const char *str)
883 {
884 	if (sdp->sd_sym->st_shndx == SHN_UNDEF) {
885 		if (str) {
886 			eprintf(ofl->ofl_lml, ERR_FATAL,
887 			    MSG_INTL(MSG_SYM_UNDEF), str,
888 			    demangle((char *)sdp->sd_name));
889 		}
890 		return (1);
891 	}
892 	if (sdp->sd_ref != REF_REL_NEED) {
893 		if (str) {
894 			eprintf(ofl->ofl_lml, ERR_FATAL,
895 			    MSG_INTL(MSG_SYM_EXTERN), str,
896 			    demangle((char *)sdp->sd_name),
897 			    sdp->sd_file->ifl_name);
898 		}
899 		return (1);
900 	}
901 
902 	sdp->sd_flags |= FLG_SY_UPREQD;
903 	if (sdp->sd_isc) {
904 		sdp->sd_isc->is_flags |= FLG_IS_SECTREF;
905 		sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF;
906 	}
907 	return (0);
908 }
909 
910 /*
911  * Make sure all the symbol definitions required for initarray, finiarray, or
912  * preinitarray's are local to the object being built.
913  */
914 static int
915 ensure_array_local(Ofl_desc *ofl, List *list, const char *str)
916 {
917 	Listnode	*lnp;
918 	Sym_desc	*sdp;
919 	int		ret = 0;
920 
921 	for (LIST_TRAVERSE(list, lnp, sdp))
922 		ret += ensure_sym_local(ofl, sdp, str);
923 
924 	return (ret);
925 }
926 
927 /*
928  * After all symbol table input processing has been finished, and all relocation
929  * counting has been carried out (ie. no more symbols will be read, generated,
930  * or modified), validate and count the relevant entries:
931  *
932  *	o	check and print any undefined symbols remaining.  Note that
933  *		if a symbol has been defined by virtue of the inclusion of
934  *		an implicit shared library, it is still classed as undefined.
935  *
936  * 	o	count the number of global needed symbols together with the
937  *		size of their associated name strings (if scoping has been
938  *		indicated these symbols may be reduced to locals).
939  *
940  *	o	establish the size and alignment requirements for the global
941  *		.bss section (the alignment of this section is based on the
942  *		first symbol that it will contain).
943  */
944 uintptr_t
945 ld_sym_validate(Ofl_desc *ofl)
946 {
947 	Sym_avlnode	*sav;
948 	Sym_desc	*sdp;
949 	Sym		*sym;
950 	Word		oflags = ofl->ofl_flags;
951 	Word		undef = 0, needed = 0, verdesc = 0;
952 	Xword		bssalign = 0, tlsalign = 0;
953 	Xword		bsssize = 0, tlssize = 0;
954 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
955 	Xword		lbssalign = 0, lbsssize = 0;
956 #endif
957 	int		ret;
958 	int		allow_ldynsym;
959 
960 	/*
961 	 * If a symbol is undefined and this link-edit calls for no undefined
962 	 * symbols to remain (this is the default case when generating an
963 	 * executable but can be enforced for any object using -z defs), the
964 	 * symbol is classified as undefined and a fatal error condition will
965 	 * be indicated.
966 	 *
967 	 * If the symbol is undefined and we're creating a shared object with
968 	 * the -Bsymbolic flag, then the symbol is also classified as undefined
969 	 * and a warning condition will be indicated.
970 	 */
971 	if ((oflags & (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC)) ==
972 	    (FLG_OF_SHAROBJ | FLG_OF_SYMBOLIC))
973 		undef = FLG_OF_WARN;
974 	if (oflags & FLG_OF_NOUNDEF)
975 		undef = FLG_OF_FATAL;
976 
977 	/*
978 	 * If the symbol is referenced from an implicitly included shared object
979 	 * (ie. it's not on the NEEDED list) then the symbol is also classified
980 	 * as undefined and a fatal error condition will be indicated.
981 	 */
982 	if ((oflags & FLG_OF_NOUNDEF) || !(oflags & FLG_OF_SHAROBJ))
983 		needed = FLG_OF_FATAL;
984 
985 	/*
986 	 * If the output image is being versioned all symbol definitions must be
987 	 * associated with a version.  Any symbol that isn't is classified as
988 	 * undefined and a fatal error condition will be indicated.
989 	 */
990 	if ((oflags & FLG_OF_VERDEF) && (ofl->ofl_vercnt > VER_NDX_GLOBAL))
991 		verdesc = FLG_OF_FATAL;
992 
993 	allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
994 	/*
995 	 * Collect and validate the globals from the internal symbol table.
996 	 */
997 	for (sav = avl_first(&ofl->ofl_symavl); sav;
998 	    sav = AVL_NEXT(&ofl->ofl_symavl, sav)) {
999 		Is_desc *	isp;
1000 		int		undeferr = 0;
1001 
1002 		sdp = sav->sav_symdesc;
1003 
1004 		/*
1005 		 * If undefined symbols are allowed ignore any symbols that are
1006 		 * not needed.
1007 		 */
1008 		if (!(oflags & FLG_OF_NOUNDEF) &&
1009 		    (sdp->sd_ref == REF_DYN_SEEN))
1010 			continue;
1011 
1012 		/*
1013 		 * If the symbol originates from an external or parent mapfile
1014 		 * reference and hasn't been matched to a reference from a
1015 		 * relocatable object, ignore it.
1016 		 */
1017 		if ((sdp->sd_flags & (FLG_SY_EXTERN | FLG_SY_PARENT)) &&
1018 		    ((sdp->sd_flags & FLG_SY_MAPUSED) == 0)) {
1019 			sdp->sd_flags |= FLG_SY_INVALID;
1020 			continue;
1021 		}
1022 
1023 		sym = sdp->sd_sym;
1024 
1025 		/*
1026 		 * Sanity check TLS.
1027 		 */
1028 		if ((ELF_ST_TYPE(sym->st_info) == STT_TLS) &&
1029 		    (sym->st_size != 0) && (sym->st_shndx != SHN_UNDEF) &&
1030 		    (sym->st_shndx != SHN_COMMON)) {
1031 			Is_desc *	isp = sdp->sd_isc;
1032 			Ifl_desc *	ifl = sdp->sd_file;
1033 
1034 			if ((isp == 0) || (isp->is_shdr == 0) ||
1035 			    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
1036 				eprintf(ofl->ofl_lml, ERR_FATAL,
1037 				    MSG_INTL(MSG_SYM_TLS),
1038 				    demangle(sdp->sd_name), ifl->ifl_name);
1039 				ofl->ofl_flags |= FLG_OF_FATAL;
1040 				continue;
1041 			}
1042 		}
1043 
1044 		if ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)
1045 			ld_sym_adjust_vis(sdp, ofl);
1046 
1047 		if ((sdp->sd_flags & FLG_SY_REDUCED) &&
1048 		    (oflags & FLG_OF_PROCRED)) {
1049 			DBG_CALL(Dbg_syms_reduce(ofl, DBG_SYM_REDUCE_GLOBAL,
1050 			    sdp, 0, 0));
1051 		}
1052 
1053 		/*
1054 		 * If building a shared object or executable, and this is a
1055 		 * non-weak UNDEF symbol with reduced visibility (STV_*), then
1056 		 * give a fatal error.
1057 		 */
1058 		if (!(oflags & FLG_OF_RELOBJ) &&
1059 		    ELF_ST_VISIBILITY(sym->st_other) &&
1060 		    (sym->st_shndx == SHN_UNDEF) &&
1061 		    (ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
1062 			sym_undef_entry(ofl, sdp, BNDLOCAL);
1063 			ofl->ofl_flags |= FLG_OF_FATAL;
1064 			continue;
1065 		}
1066 
1067 		/*
1068 		 * If this symbol is defined in a non-allocatable section,
1069 		 * reduce it to local symbol.
1070 		 */
1071 		if (((isp = sdp->sd_isc) != 0) && isp->is_shdr &&
1072 		    ((isp->is_shdr->sh_flags & SHF_ALLOC) == 0)) {
1073 			sdp->sd_flags |= FLG_SY_REDUCED;
1074 			sdp->sd_flags1 |= FLG_SY1_LOCL;
1075 		}
1076 
1077 		/*
1078 		 * If this symbol originated as a SHN_SUNW_IGNORE, it will have
1079 		 * been processed as an SHN_UNDEF.  Return the symbol to its
1080 		 * original index for validation, and propagation to the output
1081 		 * file.
1082 		 */
1083 		if (sdp->sd_flags1 & FLG_SY1_IGNORE)
1084 			sdp->sd_shndx = SHN_SUNW_IGNORE;
1085 
1086 		if (undef) {
1087 			/*
1088 			 * If an non-weak reference remains undefined, or if a
1089 			 * mapfile reference is not bound to the relocatable
1090 			 * objects that make up the object being built, we have
1091 			 * a fatal error.
1092 			 *
1093 			 * The exceptions are symbols which are defined to be
1094 			 * found in the parent (FLG_SY_PARENT), which is really
1095 			 * only meaningful for direct binding, or are defined
1096 			 * external (FLG_SY_EXTERN) so as to suppress -zdefs
1097 			 * errors.
1098 			 *
1099 			 * Register symbols are always allowed to be UNDEF.
1100 			 *
1101 			 * Note that we don't include references created via -u
1102 			 * in the same shared object binding test.  This is for
1103 			 * backward compatibility, in that a number of archive
1104 			 * makefile rules used -u to cause archive extraction.
1105 			 * These same rules have been cut and pasted to apply
1106 			 * to shared objects, and thus although the -u reference
1107 			 * is redundant, flagging it as fatal could cause some
1108 			 * build to fail.  Also we have documented the use of
1109 			 * -u as a mechanism to cause binding to weak version
1110 			 * definitions, thus giving users an error condition
1111 			 * would be incorrect.
1112 			 */
1113 			if (!(sdp->sd_flags & FLG_SY_REGSYM) &&
1114 			    ((sym->st_shndx == SHN_UNDEF) &&
1115 			    ((ELF_ST_BIND(sym->st_info) != STB_WEAK) &&
1116 			    ((sdp->sd_flags &
1117 			    (FLG_SY_PARENT | FLG_SY_EXTERN)) == 0)) ||
1118 			    (((sdp->sd_flags &
1119 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
1120 			    FLG_SY_MAPREF) &&
1121 			    ((sdp->sd_flags1 & (FLG_SY1_LOCL |
1122 			    FLG_SY1_PROT)) == 0)))) {
1123 				sym_undef_entry(ofl, sdp, UNDEF);
1124 				ofl->ofl_flags |= undef;
1125 				undeferr = 1;
1126 			}
1127 
1128 		} else {
1129 			/*
1130 			 * For building things like shared objects (or anything
1131 			 * -znodefs), undefined symbols are allowed.
1132 			 *
1133 			 * If a mapfile reference remains undefined the user
1134 			 * would probably like a warning at least (they've
1135 			 * usually mis-spelt the reference).  Refer to the above
1136 			 * comments for discussion on -u references, which
1137 			 * are not tested for in the same manner.
1138 			 */
1139 			if ((sdp->sd_flags &
1140 			    (FLG_SY_MAPREF | FLG_SY_MAPUSED)) ==
1141 			    FLG_SY_MAPREF) {
1142 				sym_undef_entry(ofl, sdp, UNDEF);
1143 				ofl->ofl_flags |= FLG_OF_WARN;
1144 				undeferr = 1;
1145 			}
1146 		}
1147 
1148 		/*
1149 		 * If this symbol comes from a dependency mark the dependency
1150 		 * as required (-z ignore can result in unused dependencies
1151 		 * being dropped).  If we need to record dependency versioning
1152 		 * information indicate what version of the needed shared object
1153 		 * this symbol is part of.  Flag the symbol as undefined if it
1154 		 * has not been made available to us.
1155 		 */
1156 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1157 		    (!(sdp->sd_flags & FLG_SY_REFRSD))) {
1158 			sdp->sd_file->ifl_flags |= FLG_IF_DEPREQD;
1159 
1160 			/*
1161 			 * Capture that we've bound to a symbol that doesn't
1162 			 * allow being directly bound to.
1163 			 */
1164 			if (sdp->sd_flags1 & FLG_SY1_NDIR)
1165 				ofl->ofl_flags1 |= FLG_OF1_NDIRECT;
1166 
1167 			if (sdp->sd_file->ifl_vercnt) {
1168 				int		vndx;
1169 				Ver_index *	vip;
1170 
1171 				vndx = sdp->sd_aux->sa_dverndx;
1172 				vip = &sdp->sd_file->ifl_verndx[vndx];
1173 				if (vip->vi_flags & FLG_VER_AVAIL) {
1174 					vip->vi_flags |= FLG_VER_REFER;
1175 				} else {
1176 					sym_undef_entry(ofl, sdp, NOTAVAIL);
1177 					ofl->ofl_flags |= FLG_OF_FATAL;
1178 					continue;
1179 				}
1180 			}
1181 		}
1182 
1183 		/*
1184 		 * Test that we do not bind to symbol supplied from an implicit
1185 		 * shared object.  If a binding is from a weak reference it can
1186 		 * be ignored.
1187 		 */
1188 		if (needed && !undeferr && (sdp->sd_flags & FLG_SY_GLOBREF) &&
1189 		    (sdp->sd_ref == REF_DYN_NEED) &&
1190 		    (sdp->sd_flags & FLG_SY_NOTAVAIL)) {
1191 			sym_undef_entry(ofl, sdp, IMPLICIT);
1192 			ofl->ofl_flags |= needed;
1193 			continue;
1194 		}
1195 
1196 		/*
1197 		 * Test that a symbol isn't going to be reduced to local scope
1198 		 * which actually wants to bind to a shared object - if so it's
1199 		 * a fatal error.
1200 		 */
1201 		if ((sdp->sd_ref == REF_DYN_NEED) &&
1202 		    (sdp->sd_flags1 & (FLG_SY1_LOCL | FLG_SY1_PROT))) {
1203 			sym_undef_entry(ofl, sdp, BNDLOCAL);
1204 			ofl->ofl_flags |= FLG_OF_FATAL;
1205 			continue;
1206 		}
1207 
1208 		/*
1209 		 * If the output image is to be versioned then all symbol
1210 		 * definitions must be associated with a version.
1211 		 */
1212 		if (verdesc && (sdp->sd_ref == REF_REL_NEED) &&
1213 		    (sym->st_shndx != SHN_UNDEF) &&
1214 		    (!(sdp->sd_flags1 & FLG_SY1_LOCL)) &&
1215 		    (sdp->sd_aux->sa_overndx == 0)) {
1216 			sym_undef_entry(ofl, sdp, NOVERSION);
1217 			ofl->ofl_flags |= verdesc;
1218 			continue;
1219 		}
1220 
1221 		/*
1222 		 * If we don't need the symbol there's no need to process it
1223 		 * any further.
1224 		 */
1225 		if (sdp->sd_ref == REF_DYN_SEEN)
1226 			continue;
1227 
1228 		/*
1229 		 * Calculate the size and alignment requirements for the global
1230 		 * .bss and .tls sections.  If we're building a relocatable
1231 		 * object only account for scoped COMMON symbols (these will
1232 		 * be converted to .bss references).
1233 		 *
1234 		 * For partially initialized symbol,
1235 		 *  if it is expanded, it goes to sunwdata1.
1236 		 *  if it is local, it goes to .bss.
1237 		 *  if the output is shared object, it goes to .sunwbss.
1238 		 *
1239 		 * Also refer to make_mvsections() in sunwmove.c
1240 		 */
1241 		if ((sym->st_shndx == SHN_COMMON) &&
1242 		    (((oflags & FLG_OF_RELOBJ) == 0) ||
1243 		    ((sdp->sd_flags1 & FLG_SY1_LOCL) &&
1244 		    (oflags & FLG_OF_PROCRED)))) {
1245 			int countbss = 0;
1246 
1247 			if (sdp->sd_psyminfo == 0) {
1248 				countbss = 1;
1249 			} else if ((sdp->sd_flags & FLG_SY_PAREXPN) != 0) {
1250 				countbss = 0;
1251 			} else if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
1252 				countbss = 1;
1253 			} else if ((ofl->ofl_flags & FLG_OF_SHAROBJ) != 0) {
1254 				countbss = 0;
1255 			} else
1256 				countbss = 1;
1257 
1258 			if (countbss) {
1259 				Xword * size, * align;
1260 
1261 				if (ELF_ST_TYPE(sym->st_info) != STT_TLS) {
1262 					size = &bsssize;
1263 					align = &bssalign;
1264 				} else {
1265 					size = &tlssize;
1266 					align = &tlsalign;
1267 				}
1268 				*size = (Xword)S_ROUND(*size, sym->st_value) +
1269 				    sym->st_size;
1270 				if (sym->st_value > *align)
1271 					*align = sym->st_value;
1272 			}
1273 		}
1274 
1275 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
1276 		/*
1277 		 * Calculate the size and alignment requirement for the global
1278 		 * .lbss. TLS or partially initialized symbols do not need to be
1279 		 * considered yet.
1280 		 */
1281 		if (sym->st_shndx == SHN_X86_64_LCOMMON) {
1282 			lbsssize = (Xword)S_ROUND(lbsssize, sym->st_value) +
1283 				sym->st_size;
1284 			if (sym->st_value > lbssalign)
1285 				lbssalign = sym->st_value;
1286 		}
1287 #endif
1288 
1289 		/*
1290 		 * If a symbol was referenced via the command line
1291 		 * (ld -u <>, ...), then this counts as a reference against the
1292 		 * symbol. Mark any section that symbol is defined in.
1293 		 */
1294 		if (((isp = sdp->sd_isc) != 0) &&
1295 		    (sdp->sd_flags & FLG_SY_CMDREF)) {
1296 			isp->is_flags |= FLG_IS_SECTREF;
1297 			isp->is_file->ifl_flags |= FLG_IF_FILEREF;
1298 		}
1299 
1300 		/*
1301 		 * Update the symbol count and the associated name string size.
1302 		 * If scoping is in effect for this symbol assign it will be
1303 		 * assigned to the .symtab/.strtab sections.
1304 		 */
1305 		if ((sdp->sd_flags1 & FLG_SY1_LOCL) &&
1306 		    (oflags & FLG_OF_PROCRED)) {
1307 			/*
1308 			 * If symbol gets eliminated count it.
1309 			 *
1310 			 * If symbol gets reduced to local,
1311 			 * count it's size for the .symtab.
1312 			 */
1313 			if (sdp->sd_flags1 & FLG_SY1_ELIM) {
1314 				ofl->ofl_elimcnt++;
1315 			} else {
1316 				ofl->ofl_scopecnt++;
1317 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1318 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
1319 				    sdp->sd_name) == -1))
1320 					return (S_ERROR);
1321 				if (allow_ldynsym && sym->st_name &&
1322 				    (ELF_ST_TYPE(sym->st_info) == STT_FUNC)) {
1323 					ofl->ofl_dynscopecnt++;
1324 					if (st_insert(ofl->ofl_dynstrtab,
1325 					    sdp->sd_name) == -1)
1326 						return (S_ERROR);
1327 				}
1328 			}
1329 		} else {
1330 			ofl->ofl_globcnt++;
1331 
1332 			/*
1333 			 * If global direct bindings are in effect, or this
1334 			 * symbol has bound to a dependency which was specified
1335 			 * as requiring direct bindings, and it hasn't
1336 			 * explicitly been defined as a non-direct binding
1337 			 * symbol, mark it.
1338 			 */
1339 			if (((ofl->ofl_dtflags_1 & DF_1_DIRECT) || (isp &&
1340 			    (isp->is_file->ifl_flags & FLG_IF_DIRECT))) &&
1341 			    ((sdp->sd_flags1 & FLG_SY1_NDIR) == 0))
1342 				sdp->sd_flags1 |= FLG_SY1_DIR;
1343 
1344 			/*
1345 			 * Insert the symbol name.
1346 			 */
1347 			if (((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1348 			    sym->st_name) {
1349 				if (st_insert(ofl->ofl_strtab,
1350 				    sdp->sd_name) == -1)
1351 					return (S_ERROR);
1352 
1353 				if (!(ofl->ofl_flags & FLG_OF_RELOBJ) &&
1354 				    (st_insert(ofl->ofl_dynstrtab,
1355 				    sdp->sd_name) == -1))
1356 					return (S_ERROR);
1357 			}
1358 
1359 			/*
1360 			 * If this section offers a global symbol - record that
1361 			 * fact.
1362 			 */
1363 			if (isp) {
1364 				isp->is_flags |= FLG_IS_SECTREF;
1365 				isp->is_file->ifl_flags |= FLG_IF_FILEREF;
1366 			}
1367 		}
1368 	}
1369 
1370 	/*
1371 	 * If we've encountered a fatal error during symbol validation then
1372 	 * return now.
1373 	 */
1374 	if (ofl->ofl_flags & FLG_OF_FATAL)
1375 		return (1);
1376 
1377 	/*
1378 	 * Now that symbol resolution is completed, scan any register symbols.
1379 	 * From now on, we're only interested in those that contribute to the
1380 	 * output file.
1381 	 */
1382 	if (ofl->ofl_regsyms) {
1383 		int	ndx;
1384 
1385 		for (ndx = 0; ndx < ofl->ofl_regsymsno; ndx++) {
1386 			if ((sdp = ofl->ofl_regsyms[ndx]) == 0)
1387 				continue;
1388 			if (sdp->sd_ref != REF_REL_NEED) {
1389 				ofl->ofl_regsyms[ndx] = 0;
1390 				continue;
1391 			}
1392 
1393 			ofl->ofl_regsymcnt++;
1394 			if (sdp->sd_sym->st_name == 0)
1395 				sdp->sd_name = MSG_ORIG(MSG_STR_EMPTY);
1396 
1397 			if ((sdp->sd_flags1 & FLG_SY1_LOCL) ||
1398 			    (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL))
1399 				ofl->ofl_lregsymcnt++;
1400 		}
1401 	}
1402 
1403 	/*
1404 	 * Generate the .bss section now that we know its size and alignment.
1405 	 */
1406 	if (bsssize || !(oflags & FLG_OF_RELOBJ)) {
1407 		if (ld_make_bss(ofl, bsssize, bssalign, MAKE_BSS) == S_ERROR)
1408 			return (S_ERROR);
1409 	}
1410 	if (tlssize) {
1411 		if (ld_make_bss(ofl, tlssize, tlsalign, MAKE_TLS) == S_ERROR)
1412 			return (S_ERROR);
1413 	}
1414 #if	(defined(__i386) || defined(__amd64)) && defined(_ELF64)
1415 	if (lbsssize && !(oflags & FLG_OF_RELOBJ)) {
1416 		if (ld_make_bss(ofl, lbsssize, lbssalign, MAKE_LBSS) == S_ERROR)
1417 			return (S_ERROR);
1418 	}
1419 #endif
1420 
1421 	/*
1422 	 * Determine what entry point symbol we need, and if found save its
1423 	 * symbol descriptor so that we can update the ELF header entry with the
1424 	 * symbols value later (see update_oehdr).  Make sure the symbol is
1425 	 * tagged to ensure its update in case -s is in effect.  Use any -e
1426 	 * option first, or the default entry points `_start' and `main'.
1427 	 */
1428 	ret = 0;
1429 	if (ofl->ofl_entry) {
1430 		if (((sdp = ld_sym_find(ofl->ofl_entry, SYM_NOHASH, 0,
1431 		    ofl)) != NULL) && (ensure_sym_local(ofl,
1432 		    sdp, MSG_INTL(MSG_SYM_ENTRY)) == 0)) {
1433 			ofl->ofl_entry = (void *)sdp;
1434 		} else {
1435 			ret++;
1436 		}
1437 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_START),
1438 	    SYM_NOHASH, 0, ofl)) != NULL) && (ensure_sym_local(ofl,
1439 	    sdp, 0) == 0)) {
1440 		ofl->ofl_entry = (void *)sdp;
1441 
1442 	} else if (((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_MAIN),
1443 	    SYM_NOHASH, 0, ofl)) != NULL) && (ensure_sym_local(ofl,
1444 	    sdp, 0) == 0)) {
1445 		ofl->ofl_entry = (void *)sdp;
1446 	}
1447 
1448 	/*
1449 	 * If ld -zdtrace=<sym> was given, then validate that the symbol is
1450 	 * defined within the current object being built.
1451 	 */
1452 	if ((sdp = ofl->ofl_dtracesym) != 0) {
1453 		ret += ensure_sym_local(ofl, sdp, MSG_ORIG(MSG_STR_DTRACE));
1454 	}
1455 
1456 	/*
1457 	 * If any initarray, finiarray or preinitarray functions have been
1458 	 * requested, make sure they are defined within the current object
1459 	 * being built.
1460 	 */
1461 	if (ofl->ofl_initarray.head) {
1462 		ret += ensure_array_local(ofl, &ofl->ofl_initarray,
1463 		    MSG_ORIG(MSG_SYM_INITARRAY));
1464 	}
1465 	if (ofl->ofl_finiarray.head) {
1466 		ret += ensure_array_local(ofl, &ofl->ofl_finiarray,
1467 		    MSG_ORIG(MSG_SYM_FINIARRAY));
1468 	}
1469 	if (ofl->ofl_preiarray.head) {
1470 		ret += ensure_array_local(ofl, &ofl->ofl_preiarray,
1471 		    MSG_ORIG(MSG_SYM_PREINITARRAY));
1472 	}
1473 
1474 	if (ret)
1475 		return (S_ERROR);
1476 
1477 	/*
1478 	 * If we're required to record any needed dependencies versioning
1479 	 * information calculate it now that all symbols have been validated.
1480 	 */
1481 	if ((oflags & (FLG_OF_VERNEED | FLG_OF_NOVERSEC)) == FLG_OF_VERNEED)
1482 		return (ld_vers_check_need(ofl));
1483 	else
1484 		return (1);
1485 }
1486 
1487 /*
1488  * qsort(3c) comparison function.  As an optimization for associating weak
1489  * symbols to their strong counterparts sort global symbols according to their
1490  * address and binding.
1491  */
1492 static int
1493 compare(const void * sdpp1, const void * sdpp2)
1494 {
1495 	Sym_desc *	sdp1 = *((Sym_desc **)sdpp1);
1496 	Sym_desc *	sdp2 = *((Sym_desc **)sdpp2);
1497 	Sym *		sym1, * sym2;
1498 	uchar_t		bind1, bind2;
1499 
1500 	/*
1501 	 * Symbol descriptors may be zero, move these to the front of the
1502 	 * sorted array.
1503 	 */
1504 	if (sdp1 == 0)
1505 		return (-1);
1506 	if (sdp2 == 0)
1507 		return (1);
1508 
1509 	sym1 = sdp1->sd_sym;
1510 	sym2 = sdp2->sd_sym;
1511 
1512 	/*
1513 	 * Compare the symbols value (address).
1514 	 */
1515 	if (sym1->st_value > sym2->st_value)
1516 		return (1);
1517 	if (sym1->st_value < sym2->st_value)
1518 		return (-1);
1519 
1520 	bind1 = ELF_ST_BIND(sym1->st_info);
1521 	bind2 = ELF_ST_BIND(sym2->st_info);
1522 
1523 	/*
1524 	 * If two symbols have the same address place the weak symbol before
1525 	 * any strong counterpart.
1526 	 */
1527 	if (bind1 > bind2)
1528 		return (-1);
1529 	if (bind1 < bind2)
1530 		return (1);
1531 
1532 	return (0);
1533 }
1534 
1535 
1536 /*
1537  * Process the symbol table for the specified input file.  At this point all
1538  * input sections from this input file have been assigned an input section
1539  * descriptor which is saved in the `ifl_isdesc' array.
1540  *
1541  *	o	local symbols are saved (as is) if the input file is a
1542  *		relocatable object
1543  *
1544  *	o	global symbols are added to the linkers internal symbol
1545  *		table if they are not already present, otherwise a symbol
1546  *		resolution function is called upon to resolve the conflict.
1547  */
1548 uintptr_t
1549 ld_sym_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1550 {
1551 	Sym		*sym = (Sym *)isc->is_indata->d_buf;
1552 	Word		*symshndx = 0;
1553 	Shdr		*shdr = isc->is_shdr;
1554 	Sym_desc	*sdp;
1555 	size_t		strsize;
1556 	char		*strs;
1557 	uchar_t		type, bind;
1558 	Word		ndx, hash, local, total;
1559 	Half		etype = ifl->ifl_ehdr->e_type;
1560 	const char	*symsecname, *strsecname;
1561 	avl_index_t	where;
1562 
1563 	/*
1564 	 * Its possible that a file may contain more that one symbol table,
1565 	 * ie. .dynsym and .symtab in a shared library.  Only process the first
1566 	 * table (here, we assume .dynsym comes before .symtab).
1567 	 */
1568 	if (ifl->ifl_symscnt)
1569 		return (1);
1570 
1571 	if (isc->is_symshndx)
1572 		symshndx = isc->is_symshndx->is_indata->d_buf;
1573 
1574 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
1575 
1576 	if (isc->is_name)
1577 		symsecname = isc->is_name;
1578 	else
1579 		symsecname = MSG_ORIG(MSG_STR_EMPTY);
1580 
1581 	/*
1582 	 * From the symbol tables section header information determine which
1583 	 * strtab table is needed to locate the actual symbol names.
1584 	 */
1585 	if (ifl->ifl_flags & FLG_IF_HSTRTAB) {
1586 		ndx = shdr->sh_link;
1587 		if ((ndx == 0) || (ndx >= ifl->ifl_shnum)) {
1588 			eprintf(ofl->ofl_lml, ERR_FATAL,
1589 			    MSG_INTL(MSG_FIL_INVSHLINK),
1590 			    ifl->ifl_name, symsecname, EC_XWORD(ndx));
1591 			return (S_ERROR);
1592 		}
1593 		strsize = ifl->ifl_isdesc[ndx]->is_shdr->sh_size;
1594 		strs = ifl->ifl_isdesc[ndx]->is_indata->d_buf;
1595 		if (ifl->ifl_isdesc[ndx]->is_name)
1596 			strsecname = ifl->ifl_isdesc[ndx]->is_name;
1597 		else
1598 			strsecname = MSG_ORIG(MSG_STR_EMPTY);
1599 	} else {
1600 		/*
1601 		 * There is no string table section in this input file
1602 		 * although there are symbols in this symbol table section.
1603 		 * This means that these symbols do not have names.
1604 		 * Currently, only scratch register symbols are allowed
1605 		 * not to have names.
1606 		 */
1607 		strsize = 0;
1608 		strs = (char *)MSG_ORIG(MSG_STR_EMPTY);
1609 		strsecname = MSG_ORIG(MSG_STR_EMPTY);
1610 	}
1611 
1612 	/*
1613 	 * Determine the number of local symbols together with the total
1614 	 * number we have to process.
1615 	 */
1616 	total = (Word)(shdr->sh_size / shdr->sh_entsize);
1617 	local = shdr->sh_info;
1618 
1619 	/*
1620 	 * Allocate a symbol table index array and a local symbol array
1621 	 * (global symbols are processed and added to the ofl->ofl_symbkt[]
1622 	 * array).  If we are dealing with a relocatable object, allocate the
1623 	 * local symbol descriptors.  If this isn't a relocatable object we
1624 	 * still have to process any shared object locals to determine if any
1625 	 * register symbols exist.  Although these aren't added to the output
1626 	 * image, they are used as part of symbol resolution.
1627 	 */
1628 	if ((ifl->ifl_oldndx = libld_malloc((size_t)(total *
1629 	    sizeof (Sym_desc *)))) == 0)
1630 		return (S_ERROR);
1631 	if ((etype == ET_REL) && local) {
1632 		if ((ifl->ifl_locs =
1633 		    libld_calloc(sizeof (Sym_desc), local)) == 0)
1634 			return (S_ERROR);
1635 		/* LINTED */
1636 		ifl->ifl_locscnt = (Word)local;
1637 	}
1638 	ifl->ifl_symscnt = total;
1639 
1640 	/*
1641 	 * If there are local symbols to save add them to the symbol table
1642 	 * index array.
1643 	 */
1644 	if (local) {
1645 		int allow_ldynsym = OFL_ALLOW_LDYNSYM(ofl);
1646 		for (sym++, ndx = 1; ndx < local; sym++, ndx++) {
1647 			Word		shndx, sdflags = FLG_SY_CLEAN;
1648 			const char	*name;
1649 			Sym_desc	*rsdp;
1650 
1651 			/*
1652 			 * Determine the associated section index.
1653 			 */
1654 			if (symshndx && (sym->st_shndx == SHN_XINDEX))
1655 				shndx = symshndx[ndx];
1656 			else if ((shndx = sym->st_shndx) >= SHN_LORESERVE)
1657 				sdflags |= FLG_SY_SPECSEC;
1658 
1659 			/*
1660 			 * Check if st_name has a valid value or not.
1661 			 */
1662 			if ((name = string(ofl, ifl, sym, strs, strsize, ndx,
1663 			    shndx, symsecname, strsecname, &sdflags)) == 0) {
1664 				ofl->ofl_flags |= FLG_OF_FATAL;
1665 				continue;
1666 			}
1667 
1668 			/*
1669 			 * If this local symbol table originates from a shared
1670 			 * object, then we're only interested in recording
1671 			 * register symbols.  As local symbol descriptors aren't
1672 			 * allocated for shared objects, one will be allocated
1673 			 * to associated with the register symbol.  This symbol
1674 			 * won't become part of the output image, but we must
1675 			 * process it to test for register conflicts.
1676 			 */
1677 			rsdp = sdp = 0;
1678 			if (sdflags & FLG_SY_REGSYM) {
1679 				if ((rsdp = ld_reg_find(sym, ofl)) != 0) {
1680 					/*
1681 					 * The fact that another register def-
1682 					 * inition has been found is fatal.
1683 					 * Call the verification routine to get
1684 					 * the error message and move on.
1685 					 */
1686 					(void) ld_reg_check(rsdp, sym, name,
1687 					    ifl, ofl);
1688 					continue;
1689 				}
1690 
1691 				if (etype == ET_DYN) {
1692 					if ((sdp = libld_calloc(
1693 					    sizeof (Sym_desc), 1)) == 0)
1694 						return (S_ERROR);
1695 					sdp->sd_ref = REF_DYN_SEEN;
1696 				}
1697 			} else if (etype == ET_DYN)
1698 				continue;
1699 
1700 			/*
1701 			 * Fill in the remaining symbol descriptor information.
1702 			 */
1703 			if (sdp == 0) {
1704 				sdp = &(ifl->ifl_locs[ndx]);
1705 				sdp->sd_ref = REF_REL_NEED;
1706 			}
1707 			if (rsdp == 0) {
1708 				sdp->sd_name = name;
1709 				sdp->sd_sym = sym;
1710 				sdp->sd_shndx = shndx;
1711 				sdp->sd_flags = sdflags;
1712 				sdp->sd_file = ifl;
1713 				ifl->ifl_oldndx[ndx] = sdp;
1714 			}
1715 
1716 			DBG_CALL(Dbg_syms_entry(ofl->ofl_lml, ndx, sdp));
1717 
1718 			/*
1719 			 * Reclassify any SHN_SUNW_IGNORE symbols to SHN_UNDEF
1720 			 * so as to simplify future processing.
1721 			 */
1722 			if (sym->st_shndx == SHN_SUNW_IGNORE) {
1723 				sdp->sd_shndx = shndx = SHN_UNDEF;
1724 				sdp->sd_flags1 |=
1725 				    (FLG_SY1_IGNORE | FLG_SY1_ELIM);
1726 			}
1727 
1728 			/*
1729 			 * Process any register symbols.
1730 			 */
1731 			if (sdp->sd_flags & FLG_SY_REGSYM) {
1732 				/*
1733 				 * Add a diagnostic to indicate we've caught a
1734 				 * register symbol, as this can be useful if a
1735 				 * register conflict is later discovered.
1736 				 */
1737 				DBG_CALL(Dbg_syms_entered(ofl, sym, sdp));
1738 
1739 				/*
1740 				 * If this register symbol hasn't already been
1741 				 * recorded, enter it now.
1742 				 */
1743 				if ((rsdp == 0) &&
1744 				    (ld_reg_enter(sdp, ofl) == 0))
1745 					return (S_ERROR);
1746 			}
1747 
1748 			/*
1749 			 * Assign an input section.
1750 			 */
1751 			if ((sym->st_shndx != SHN_UNDEF) &&
1752 			    ((sdp->sd_flags & FLG_SY_SPECSEC) == 0))
1753 				sdp->sd_isc = ifl->ifl_isdesc[shndx];
1754 
1755 			/*
1756 			 * If this symbol falls within the range of a section
1757 			 * being discarded, then discard the symbol itself.
1758 			 * There is no reason to keep this local symbol.
1759 			 */
1760 			if (sdp->sd_isc &&
1761 			    (sdp->sd_isc->is_flags & FLG_IS_DISCARD)) {
1762 				sdp->sd_flags |= FLG_SY_ISDISC;
1763 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml,
1764 				    sdp, sdp->sd_isc));
1765 				continue;
1766 			}
1767 
1768 			/*
1769 			 * Skip any section symbols as new versions of these
1770 			 * will be created.
1771 			 */
1772 			if ((type = ELF_ST_TYPE(sym->st_info)) == STT_SECTION) {
1773 				if (sym->st_shndx == SHN_UNDEF) {
1774 					eprintf(ofl->ofl_lml, ERR_WARNING,
1775 					    MSG_INTL(MSG_SYM_INVSHNDX),
1776 					    demangle(sdp->sd_name),
1777 					    ifl->ifl_name,
1778 					    conv_sym_shndx(sym->st_shndx));
1779 				}
1780 				continue;
1781 			}
1782 
1783 			/*
1784 			 * Sanity check for TLS
1785 			 */
1786 			if ((sym->st_size != 0) && ((type == STT_TLS) &&
1787 			    (sym->st_shndx != SHN_COMMON))) {
1788 				Is_desc	*isp = sdp->sd_isc;
1789 
1790 				if ((isp == 0) || (isp->is_shdr == 0) ||
1791 				    ((isp->is_shdr->sh_flags & SHF_TLS) == 0)) {
1792 					eprintf(ofl->ofl_lml, ERR_FATAL,
1793 					    MSG_INTL(MSG_SYM_TLS),
1794 					    demangle(sdp->sd_name),
1795 					    ifl->ifl_name);
1796 					ofl->ofl_flags |= FLG_OF_FATAL;
1797 					continue;
1798 				}
1799 			}
1800 
1801 			/*
1802 			 * Carry our some basic sanity checks (these are just
1803 			 * some of the erroneous symbol entries we've come
1804 			 * across, there's probably a lot more).  The symbol
1805 			 * will not be carried forward to the output file, which
1806 			 * won't be a problem unless a relocation is required
1807 			 * against it.
1808 			 */
1809 			if (((sdp->sd_flags & FLG_SY_SPECSEC) &&
1810 			    ((sym->st_shndx == SHN_COMMON)) ||
1811 			    ((type == STT_FILE) &&
1812 			    (sym->st_shndx != SHN_ABS))) ||
1813 			    (sdp->sd_isc && (sdp->sd_isc->is_osdesc == 0))) {
1814 				eprintf(ofl->ofl_lml, ERR_WARNING,
1815 				    MSG_INTL(MSG_SYM_INVSHNDX),
1816 				    demangle(sdp->sd_name), ifl->ifl_name,
1817 				    conv_sym_shndx(sym->st_shndx));
1818 				sdp->sd_isc = NULL;
1819 				sdp->sd_flags |= FLG_SY_INVALID;
1820 				continue;
1821 			}
1822 
1823 			/*
1824 			 * As these local symbols will become part of the output
1825 			 * image, record their number and name string size.
1826 			 * Globals are counted after all input file processing
1827 			 * (and hence symbol resolution) is complete during
1828 			 * sym_validate().
1829 			 */
1830 			if (!(ofl->ofl_flags1 & FLG_OF1_REDLSYM)) {
1831 				ofl->ofl_locscnt++;
1832 
1833 				if ((((sdp->sd_flags & FLG_SY_REGSYM) == 0) ||
1834 				    sym->st_name) && (st_insert(ofl->ofl_strtab,
1835 				    sdp->sd_name) == -1))
1836 					return (S_ERROR);
1837 
1838 				if (allow_ldynsym && sym->st_name &&
1839 				    ((type == STT_FUNC) ||
1840 				    (type == STT_FILE))) {
1841 					ofl->ofl_dynlocscnt++;
1842 					if (st_insert(ofl->ofl_dynstrtab,
1843 					    sdp->sd_name) == -1)
1844 						return (S_ERROR);
1845 				}
1846 			}
1847 		}
1848 	}
1849 
1850 	/*
1851 	 * Now scan the global symbols entering them in the internal symbol
1852 	 * table or resolving them as necessary.
1853 	 */
1854 	sym = (Sym *)isc->is_indata->d_buf;
1855 	sym += local;
1856 	/* LINTED */
1857 	for (ndx = (int)local; ndx < total; sym++, ndx++) {
1858 		const char	*name;
1859 		Word		shndx, sdflags = 0;
1860 
1861 		/*
1862 		 * Determine the associated section index.
1863 		 */
1864 		if (symshndx && (sym->st_shndx == SHN_XINDEX)) {
1865 			shndx = symshndx[ndx];
1866 		} else {
1867 			shndx = sym->st_shndx;
1868 			if (sym->st_shndx >= SHN_LORESERVE)
1869 				sdflags |= FLG_SY_SPECSEC;
1870 		}
1871 
1872 		/*
1873 		 * Check if st_name has a valid value or not.
1874 		 */
1875 		if ((name = string(ofl, ifl, sym, strs, strsize, ndx, shndx,
1876 		    symsecname, strsecname, &sdflags)) == 0) {
1877 			ofl->ofl_flags |= FLG_OF_FATAL;
1878 			continue;
1879 		}
1880 
1881 		/*
1882 		 * The linker itself will generate symbols for _end, _etext,
1883 		 * _edata, _DYNAMIC and _PROCEDURE_LINKAGE_TABLE_, so don't
1884 		 * bother entering these symbols from shared objects.  This
1885 		 * results in some wasted resolution processing, which is hard
1886 		 * to feel, but if nothing else, pollutes diagnostic relocation
1887 		 * output.
1888 		 */
1889 		if (name[0] && (etype == ET_DYN) && (sym->st_size == 0) &&
1890 		    (ELF_ST_TYPE(sym->st_info) == STT_OBJECT) &&
1891 		    (name[0] == '_') && ((name[1] == 'e') ||
1892 		    (name[1] == 'D') || (name[1] == 'P')) &&
1893 		    ((strcmp(name, MSG_ORIG(MSG_SYM_ETEXT_U)) == 0) ||
1894 		    (strcmp(name, MSG_ORIG(MSG_SYM_EDATA_U)) == 0) ||
1895 		    (strcmp(name, MSG_ORIG(MSG_SYM_END_U)) == 0) ||
1896 		    (strcmp(name, MSG_ORIG(MSG_SYM_DYNAMIC_U)) == 0) ||
1897 		    (strcmp(name, MSG_ORIG(MSG_SYM_PLKTBL_U)) == 0))) {
1898 			ifl->ifl_oldndx[ndx] = 0;
1899 			continue;
1900 		}
1901 
1902 		/*
1903 		 * Determine and validate the symbols binding.
1904 		 */
1905 		bind = ELF_ST_BIND(sym->st_info);
1906 		if ((bind != STB_GLOBAL) && (bind != STB_WEAK)) {
1907 			eprintf(ofl->ofl_lml, ERR_WARNING,
1908 			    MSG_INTL(MSG_SYM_NONGLOB), demangle(name),
1909 			    ifl->ifl_name, conv_sym_info_bind(bind, 0));
1910 			continue;
1911 		}
1912 
1913 		/*
1914 		 * If this symbol falls within the range of a section being
1915 		 * discarded, then discard the symbol itself.
1916 		 */
1917 		if (((sdflags & FLG_SY_SPECSEC) == 0) &&
1918 		    (sym->st_shndx != SHN_UNDEF)) {
1919 			Is_desc	*isp;
1920 
1921 			if (shndx >= ifl->ifl_shnum) {
1922 				/*
1923 				 * Carry our some basic sanity checks
1924 				 * The symbol will not be carried forward to
1925 				 * the output file, which won't be a problem
1926 				 * unless a relocation is required against it.
1927 				 */
1928 				eprintf(ofl->ofl_lml, ERR_WARNING,
1929 				    MSG_INTL(MSG_SYM_INVSHNDX), demangle(name),
1930 				    ifl->ifl_name,
1931 				    conv_sym_shndx(sym->st_shndx));
1932 				continue;
1933 			}
1934 
1935 			isp = ifl->ifl_isdesc[shndx];
1936 			if (isp && (isp->is_flags & FLG_IS_DISCARD)) {
1937 				if ((sdp =
1938 				    libld_calloc(sizeof (Sym_desc), 1)) == 0)
1939 					return (S_ERROR);
1940 
1941 				/*
1942 				 * Create a dummy symbol entry so that if we
1943 				 * find any references to this discarded symbol
1944 				 * we can compensate.
1945 				 */
1946 				sdp->sd_name = name;
1947 				sdp->sd_sym = sym;
1948 				sdp->sd_file = ifl;
1949 				sdp->sd_isc = isp;
1950 				sdp->sd_flags = FLG_SY_ISDISC;
1951 				ifl->ifl_oldndx[ndx] = sdp;
1952 
1953 				DBG_CALL(Dbg_syms_discarded(ofl->ofl_lml, sdp,
1954 				    sdp->sd_isc));
1955 				continue;
1956 			}
1957 		}
1958 
1959 		/*
1960 		 * If the symbol does not already exist in the internal symbol
1961 		 * table add it, otherwise resolve the conflict.  If the symbol
1962 		 * from this file is kept, retain its symbol table index for
1963 		 * possible use in associating a global alias.
1964 		 */
1965 		/* LINTED */
1966 		hash = (Word)elf_hash((const char *)name);
1967 		if ((sdp = ld_sym_find(name, hash, &where, ofl)) == NULL) {
1968 			DBG_CALL(Dbg_syms_global(ofl->ofl_lml, ndx, name));
1969 			if ((sdp = ld_sym_enter(name, sym, hash, ifl, ofl, ndx,
1970 			    shndx, sdflags, 0, &where)) == (Sym_desc *)S_ERROR)
1971 				return (S_ERROR);
1972 
1973 		} else if (ld_sym_resolve(sdp, sym, ifl, ofl, ndx, shndx,
1974 		    sdflags) == S_ERROR)
1975 			return (S_ERROR);
1976 
1977 		/*
1978 		 * After we've compared a defined symbol in one shared
1979 		 * object, flag the symbol so we don't compare it again.
1980 		 */
1981 		if ((etype == ET_DYN) && (sym->st_shndx != SHN_UNDEF) &&
1982 		    ((sdp->sd_flags & FLG_SY_SOFOUND) == 0))
1983 			sdp->sd_flags |= FLG_SY_SOFOUND;
1984 
1985 		/*
1986 		 * If the symbol is accepted from this file retain the symbol
1987 		 * index for possible use in aliasing.
1988 		 */
1989 		if (sdp->sd_file == ifl)
1990 			sdp->sd_symndx = ndx;
1991 
1992 		ifl->ifl_oldndx[ndx] = sdp;
1993 
1994 		/*
1995 		 * If we've accepted a register symbol, continue to validate
1996 		 * it.
1997 		 */
1998 		if (sdp->sd_flags & FLG_SY_REGSYM) {
1999 			Sym_desc	*rsdp;
2000 
2001 			if ((rsdp = ld_reg_find(sdp->sd_sym, ofl)) == 0) {
2002 				if (ld_reg_enter(sdp, ofl) == 0)
2003 					return (S_ERROR);
2004 			} else if (rsdp != sdp) {
2005 				(void) ld_reg_check(rsdp, sdp->sd_sym,
2006 				    sdp->sd_name, ifl, ofl);
2007 			}
2008 		}
2009 	}
2010 
2011 	/*
2012 	 * If this is a shared object scan the globals one more time and
2013 	 * associate any weak/global associations.  This association is needed
2014 	 * should the weak definition satisfy a reference in the dynamic
2015 	 * executable:
2016 	 *
2017 	 *  o	if the symbol is a data item it will be copied to the
2018 	 *	executables address space, thus we must also reassociate the
2019 	 *	alias symbol with its new location in the executable.
2020 	 *
2021 	 *  o	if the symbol is a function then we may need to promote	the
2022 	 *	symbols binding from undefined weak to undefined, otherwise the
2023 	 *	run-time linker will not generate the correct relocation error
2024 	 *	should the symbol not be found.
2025 	 *
2026 	 * The true association between a weak/strong symbol pair is that both
2027 	 * symbol entries are identical, thus first we created a sorted symbol
2028 	 * list keyed off of the symbols value (if the value is the same chances
2029 	 * are the rest of the symbols data is).  This list is then scanned for
2030 	 * weak symbols, and if one is found then any strong association will
2031 	 * exist in the following entries.  Thus we just have to scan one
2032 	 * (typical single alias) or more (in the uncommon instance of multiple
2033 	 * weak to strong associations) entries to determine if a match exists.
2034 	 */
2035 	if (ifl->ifl_ehdr->e_type == ET_DYN) {
2036 		Sym_desc **	sort;
2037 		size_t		size = (total - local) * sizeof (Sym_desc *);
2038 
2039 		if ((sort = libld_malloc(size)) == 0)
2040 			return (S_ERROR);
2041 		(void) memcpy((void *)sort, &ifl->ifl_oldndx[local], size);
2042 
2043 		qsort(sort, (total - local), sizeof (Sym_desc *), compare);
2044 
2045 		for (ndx = 0; ndx < (total - local); ndx++) {
2046 			Sym_desc *	wsdp = sort[ndx];
2047 			Sym *		wsym;
2048 			int		sndx;
2049 
2050 			if (wsdp == 0)
2051 				continue;
2052 
2053 			wsym = wsdp->sd_sym;
2054 
2055 			if ((ELF_ST_BIND(wsym->st_info) != STB_WEAK) ||
2056 			    (wsdp->sd_sym->st_shndx == SHN_UNDEF) ||
2057 			    (wsdp->sd_flags & FLG_SY_SPECSEC))
2058 				continue;
2059 
2060 			/*
2061 			 * We have a weak symbol, if it has a strong alias it
2062 			 * will have been sorted to one of the following sort
2063 			 * table entries.  Note that we could have multiple weak
2064 			 * symbols aliased to one strong (if this occurs then
2065 			 * the strong symbol only maintains one alias back to
2066 			 * the last weak).
2067 			 */
2068 			for (sndx = ndx + 1; sndx < (total - local); sndx++) {
2069 				Sym_desc *	ssdp = sort[sndx];
2070 				Sym *		ssym;
2071 
2072 				if (ssdp == 0)
2073 					break;
2074 
2075 				ssym = ssdp->sd_sym;
2076 
2077 				if (wsym->st_value != ssym->st_value)
2078 					break;
2079 
2080 				if ((ssdp->sd_file == ifl) &&
2081 				    (wsdp->sd_file == ifl) &&
2082 				    (wsym->st_size == ssym->st_size) &&
2083 				    (ssdp->sd_sym->st_shndx != SHN_UNDEF) &&
2084 				    (ELF_ST_BIND(ssym->st_info) != STB_WEAK) &&
2085 				    ((ssdp->sd_flags & FLG_SY_SPECSEC) == 0)) {
2086 					ssdp->sd_aux->sa_linkndx =
2087 					    (Word)wsdp->sd_symndx;
2088 					wsdp->sd_aux->sa_linkndx =
2089 					    (Word)ssdp->sd_symndx;
2090 					break;
2091 				}
2092 			}
2093 		}
2094 	}
2095 	return (1);
2096 }
2097 
2098 /*
2099  * Add an undefined symbol to the symbol table (ie. from -u name option)
2100  */
2101 Sym_desc *
2102 ld_sym_add_u(const char *name, Ofl_desc *ofl)
2103 {
2104 	Sym		*sym;
2105 	Ifl_desc	*ifl = 0, *_ifl;
2106 	Sym_desc	*sdp;
2107 	Word		hash;
2108 	Listnode	*lnp;
2109 	avl_index_t	where;
2110 	const char	*cmdline = MSG_INTL(MSG_STR_COMMAND);
2111 
2112 	/*
2113 	 * If the symbol reference already exists indicate that a reference
2114 	 * also came from the command line.
2115 	 */
2116 	/* LINTED */
2117 	hash = (Word)elf_hash(name);
2118 	if (sdp = ld_sym_find(name, hash, &where, ofl)) {
2119 		if (sdp->sd_ref == REF_DYN_SEEN)
2120 			sdp->sd_ref = REF_DYN_NEED;
2121 		return (sdp);
2122 	}
2123 
2124 	/*
2125 	 * Determine whether a pseudo input file descriptor exists to represent
2126 	 * the command line, as any global symbol needs an input file descriptor
2127 	 * during any symbol resolution (refer to map_ifl() which provides a
2128 	 * similar method for adding symbols from mapfiles).
2129 	 */
2130 	for (LIST_TRAVERSE(&ofl->ofl_objs, lnp, _ifl))
2131 		if (strcmp(_ifl->ifl_name, cmdline) == 0) {
2132 			ifl = _ifl;
2133 			break;
2134 		}
2135 
2136 	/*
2137 	 * If no descriptor exists create one.
2138 	 */
2139 	if (ifl == 0) {
2140 		if ((ifl = libld_calloc(sizeof (Ifl_desc), 1)) ==
2141 		    (Ifl_desc *)0)
2142 			return ((Sym_desc *)S_ERROR);
2143 		ifl->ifl_name = cmdline;
2144 		ifl->ifl_flags = FLG_IF_NEEDED | FLG_IF_FILEREF;
2145 		if ((ifl->ifl_ehdr = libld_calloc(sizeof (Ehdr),
2146 		    1)) == 0)
2147 			return ((Sym_desc *)S_ERROR);
2148 		ifl->ifl_ehdr->e_type = ET_REL;
2149 
2150 		if (list_appendc(&ofl->ofl_objs, ifl) == 0)
2151 			return ((Sym_desc *)S_ERROR);
2152 	}
2153 
2154 	/*
2155 	 * Allocate a symbol structure and add it to the global symbol table.
2156 	 */
2157 	if ((sym = libld_calloc(sizeof (Sym), 1)) == 0)
2158 		return ((Sym_desc *)S_ERROR);
2159 	sym->st_info = ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
2160 	sym->st_shndx = SHN_UNDEF;
2161 
2162 	DBG_CALL(Dbg_syms_process(ofl->ofl_lml, ifl));
2163 	DBG_CALL(Dbg_syms_global(ofl->ofl_lml, 0, name));
2164 	sdp = ld_sym_enter(name, sym, hash, ifl, ofl, 0, SHN_UNDEF,
2165 	    0, 0, &where);
2166 	sdp->sd_flags &= ~FLG_SY_CLEAN;
2167 	sdp->sd_flags |= FLG_SY_CMDREF;
2168 
2169 	return (sdp);
2170 }
2171