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