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