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