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