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