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