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