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