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