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