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