OLD | NEW |
1 // resolve.cc -- symbol resolution for gold | 1 // resolve.cc -- symbol resolution for gold |
2 | 2 |
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc. | 3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
4 // Written by Ian Lance Taylor <iant@google.com>. | 4 // Written by Ian Lance Taylor <iant@google.com>. |
5 | 5 |
6 // This file is part of gold. | 6 // This file is part of gold. |
7 | 7 |
8 // This program is free software; you can redistribute it and/or modify | 8 // This program is free software; you can redistribute it and/or modify |
9 // it under the terms of the GNU General Public License as published by | 9 // it under the terms of the GNU General Public License as published by |
10 // the Free Software Foundation; either version 3 of the License, or | 10 // the Free Software Foundation; either version 3 of the License, or |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 return; | 295 return; |
296 } | 296 } |
297 } | 297 } |
298 | 298 |
299 unsigned int frombits = symbol_to_bits(sym.get_st_bind(), | 299 unsigned int frombits = symbol_to_bits(sym.get_st_bind(), |
300 object->is_dynamic(), | 300 object->is_dynamic(), |
301 st_shndx, is_ordinary, | 301 st_shndx, is_ordinary, |
302 sym.get_st_type()); | 302 sym.get_st_type()); |
303 | 303 |
304 bool adjust_common_sizes; | 304 bool adjust_common_sizes; |
| 305 typename Sized_symbol<size>::Size_type tosize = to->symsize(); |
305 if (Symbol_table::should_override(to, frombits, object, | 306 if (Symbol_table::should_override(to, frombits, object, |
306 &adjust_common_sizes)) | 307 &adjust_common_sizes)) |
307 { | 308 { |
308 typename Sized_symbol<size>::Size_type tosize = to->symsize(); | |
309 | |
310 this->override(to, sym, st_shndx, is_ordinary, object, version); | 309 this->override(to, sym, st_shndx, is_ordinary, object, version); |
311 | |
312 if (adjust_common_sizes && tosize > to->symsize()) | 310 if (adjust_common_sizes && tosize > to->symsize()) |
313 to->set_symsize(tosize); | 311 to->set_symsize(tosize); |
314 } | 312 } |
315 else | 313 else |
316 { | 314 { |
317 if (adjust_common_sizes && sym.get_st_size() > to->symsize()) | 315 if (adjust_common_sizes && sym.get_st_size() > tosize) |
318 to->set_symsize(sym.get_st_size()); | 316 to->set_symsize(sym.get_st_size()); |
319 // The ELF ABI says that even for a reference to a symbol we | 317 // The ELF ABI says that even for a reference to a symbol we |
320 // merge the visibility. | 318 // merge the visibility. |
321 to->override_visibility(sym.get_st_visibility()); | 319 to->override_visibility(sym.get_st_visibility()); |
322 } | 320 } |
323 | 321 |
| 322 if (adjust_common_sizes && parameters->options().warn_common()) |
| 323 { |
| 324 if (tosize > sym.get_st_size()) |
| 325 Symbol_table::report_resolve_problem(false, |
| 326 _("common of '%s' overriding " |
| 327 "smaller common"), |
| 328 to, object); |
| 329 else if (tosize < sym.get_st_size()) |
| 330 Symbol_table::report_resolve_problem(false, |
| 331 _("common of '%s' overidden by " |
| 332 "larger common"), |
| 333 to, object); |
| 334 else |
| 335 Symbol_table::report_resolve_problem(false, |
| 336 _("multiple common of '%s'"), |
| 337 to, object); |
| 338 } |
| 339 |
324 // A new weak undefined reference, merging with an old weak | 340 // A new weak undefined reference, merging with an old weak |
325 // reference, could be a One Definition Rule (ODR) violation -- | 341 // reference, could be a One Definition Rule (ODR) violation -- |
326 // especially if the types or sizes of the references differ. We'll | 342 // especially if the types or sizes of the references differ. We'll |
327 // store such pairs and look them up later to make sure they | 343 // store such pairs and look them up later to make sure they |
328 // actually refer to the same lines of code. (Note: not all ODR | 344 // actually refer to the same lines of code. (Note: not all ODR |
329 // violations can be found this way, and not everything this finds | 345 // violations can be found this way, and not everything this finds |
330 // is an ODR violation. But it's helpful to warn about.) | 346 // is an ODR violation. But it's helpful to warn about.) |
331 bool to_is_ordinary; | 347 bool to_is_ordinary; |
332 if (parameters->options().detect_odr_violations() | 348 if (parameters->options().detect_odr_violations() |
333 && sym.get_st_bind() == elfcpp::STB_WEAK | 349 && sym.get_st_bind() == elfcpp::STB_WEAK |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 case DEF * 16 + DEF: | 431 case DEF * 16 + DEF: |
416 // Two definitions of the same symbol. | 432 // Two definitions of the same symbol. |
417 | 433 |
418 // If either symbol is defined by an object included using | 434 // If either symbol is defined by an object included using |
419 // --just-symbols, then don't warn. This is for compatibility | 435 // --just-symbols, then don't warn. This is for compatibility |
420 // with the GNU linker. FIXME: This is a hack. | 436 // with the GNU linker. FIXME: This is a hack. |
421 if ((to->source() == Symbol::FROM_OBJECT && to->object()->just_symbols()) | 437 if ((to->source() == Symbol::FROM_OBJECT && to->object()->just_symbols()) |
422 || object->just_symbols()) | 438 || object->just_symbols()) |
423 return false; | 439 return false; |
424 | 440 |
425 // FIXME: Do a better job of reporting locations. | 441 Symbol_table::report_resolve_problem(true, |
426 gold_error(_("%s: multiple definition of %s"), | 442 » » » » » _("multiple definition of '%s'"), |
427 » » object != NULL ? object->name().c_str() : _("command line"), | 443 » » » » » to, object); |
428 » » to->demangled_name().c_str()); | |
429 gold_error(_("%s: previous definition here"), | |
430 » » (to->source() == Symbol::FROM_OBJECT | |
431 » » ? to->object()->name().c_str() | |
432 » » : _("command line"))); | |
433 return false; | 444 return false; |
434 | 445 |
435 case WEAK_DEF * 16 + DEF: | 446 case WEAK_DEF * 16 + DEF: |
436 // We've seen a weak definition, and now we see a strong | 447 // We've seen a weak definition, and now we see a strong |
437 // definition. In the original SVR4 linker, this was treated as | 448 // definition. In the original SVR4 linker, this was treated as |
438 // a multiple definition error. In the Solaris linker and the | 449 // a multiple definition error. In the Solaris linker and the |
439 // GNU linker, a weak definition followed by a regular | 450 // GNU linker, a weak definition followed by a regular |
440 // definition causes the weak definition to be overridden. We | 451 // definition causes the weak definition to be overridden. We |
441 // are currently compatible with the GNU linker. In the future | 452 // are currently compatible with the GNU linker. In the future |
442 // we should add a target specific option to change this. | 453 // we should add a target specific option to change this. |
(...skipping 14 matching lines...) Expand all Loading... |
457 case DYN_WEAK_UNDEF * 16 + DEF: | 468 case DYN_WEAK_UNDEF * 16 + DEF: |
458 // We've seen an undefined reference, and now we see a | 469 // We've seen an undefined reference, and now we see a |
459 // definition. We use the definition. | 470 // definition. We use the definition. |
460 return true; | 471 return true; |
461 | 472 |
462 case COMMON * 16 + DEF: | 473 case COMMON * 16 + DEF: |
463 case WEAK_COMMON * 16 + DEF: | 474 case WEAK_COMMON * 16 + DEF: |
464 case DYN_COMMON * 16 + DEF: | 475 case DYN_COMMON * 16 + DEF: |
465 case DYN_WEAK_COMMON * 16 + DEF: | 476 case DYN_WEAK_COMMON * 16 + DEF: |
466 // We've seen a common symbol and now we see a definition. The | 477 // We've seen a common symbol and now we see a definition. The |
467 // definition overrides. FIXME: We should optionally issue, version a | 478 // definition overrides. |
468 // warning. | 479 if (parameters->options().warn_common()) |
| 480 » Symbol_table::report_resolve_problem(false, |
| 481 » » » » » _("definition of '%s' overriding " |
| 482 » » » » » "common"), |
| 483 » » » » » to, object); |
469 return true; | 484 return true; |
470 | 485 |
471 case DEF * 16 + WEAK_DEF: | 486 case DEF * 16 + WEAK_DEF: |
472 case WEAK_DEF * 16 + WEAK_DEF: | 487 case WEAK_DEF * 16 + WEAK_DEF: |
473 // We've seen a definition and now we see a weak definition. We | 488 // We've seen a definition and now we see a weak definition. We |
474 // ignore the new weak definition. | 489 // ignore the new weak definition. |
475 return false; | 490 return false; |
476 | 491 |
477 case DYN_DEF * 16 + WEAK_DEF: | 492 case DYN_DEF * 16 + WEAK_DEF: |
478 case DYN_WEAK_DEF * 16 + WEAK_DEF: | 493 case DYN_WEAK_DEF * 16 + WEAK_DEF: |
479 // We've seen a dynamic definition and now we see a regular weak | 494 // We've seen a dynamic definition and now we see a regular weak |
480 // definition. The regular weak definition overrides. | 495 // definition. The regular weak definition overrides. |
481 return true; | 496 return true; |
482 | 497 |
483 case UNDEF * 16 + WEAK_DEF: | 498 case UNDEF * 16 + WEAK_DEF: |
484 case WEAK_UNDEF * 16 + WEAK_DEF: | 499 case WEAK_UNDEF * 16 + WEAK_DEF: |
485 case DYN_UNDEF * 16 + WEAK_DEF: | 500 case DYN_UNDEF * 16 + WEAK_DEF: |
486 case DYN_WEAK_UNDEF * 16 + WEAK_DEF: | 501 case DYN_WEAK_UNDEF * 16 + WEAK_DEF: |
487 // A weak definition of a currently undefined symbol. | 502 // A weak definition of a currently undefined symbol. |
488 return true; | 503 return true; |
489 | 504 |
490 case COMMON * 16 + WEAK_DEF: | 505 case COMMON * 16 + WEAK_DEF: |
491 case WEAK_COMMON * 16 + WEAK_DEF: | 506 case WEAK_COMMON * 16 + WEAK_DEF: |
492 // A weak definition does not override a common definition. | 507 // A weak definition does not override a common definition. |
493 return false; | 508 return false; |
494 | 509 |
495 case DYN_COMMON * 16 + WEAK_DEF: | 510 case DYN_COMMON * 16 + WEAK_DEF: |
496 case DYN_WEAK_COMMON * 16 + WEAK_DEF: | 511 case DYN_WEAK_COMMON * 16 + WEAK_DEF: |
497 // A weak definition does override a definition in a dynamic | 512 // A weak definition does override a definition in a dynamic |
498 // object. FIXME: We should optionally issue a warning. | 513 // object. |
| 514 if (parameters->options().warn_common()) |
| 515 » Symbol_table::report_resolve_problem(false, |
| 516 » » » » » _("definition of '%s' overriding " |
| 517 » » » » » "dynamic common definition"), |
| 518 » » » » » to, object); |
499 return true; | 519 return true; |
500 | 520 |
501 case DEF * 16 + DYN_DEF: | 521 case DEF * 16 + DYN_DEF: |
502 case WEAK_DEF * 16 + DYN_DEF: | 522 case WEAK_DEF * 16 + DYN_DEF: |
503 case DYN_DEF * 16 + DYN_DEF: | 523 case DYN_DEF * 16 + DYN_DEF: |
504 case DYN_WEAK_DEF * 16 + DYN_DEF: | 524 case DYN_WEAK_DEF * 16 + DYN_DEF: |
505 // Ignore a dynamic definition if we already have a definition. | 525 // Ignore a dynamic definition if we already have a definition. |
506 return false; | 526 return false; |
507 | 527 |
508 case UNDEF * 16 + DYN_DEF: | 528 case UNDEF * 16 + DYN_DEF: |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_UNDEF: | 624 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_UNDEF: |
605 case COMMON * 16 + DYN_WEAK_UNDEF: | 625 case COMMON * 16 + DYN_WEAK_UNDEF: |
606 case WEAK_COMMON * 16 + DYN_WEAK_UNDEF: | 626 case WEAK_COMMON * 16 + DYN_WEAK_UNDEF: |
607 case DYN_COMMON * 16 + DYN_WEAK_UNDEF: | 627 case DYN_COMMON * 16 + DYN_WEAK_UNDEF: |
608 case DYN_WEAK_COMMON * 16 + DYN_WEAK_UNDEF: | 628 case DYN_WEAK_COMMON * 16 + DYN_WEAK_UNDEF: |
609 // A new weak dynamic undefined reference tells us nothing. | 629 // A new weak dynamic undefined reference tells us nothing. |
610 return false; | 630 return false; |
611 | 631 |
612 case DEF * 16 + COMMON: | 632 case DEF * 16 + COMMON: |
613 // A common symbol does not override a definition. | 633 // A common symbol does not override a definition. |
| 634 if (parameters->options().warn_common()) |
| 635 Symbol_table::report_resolve_problem(false, |
| 636 _("common '%s' overridden by " |
| 637 "previous definition"), |
| 638 to, object); |
614 return false; | 639 return false; |
615 | 640 |
616 case WEAK_DEF * 16 + COMMON: | 641 case WEAK_DEF * 16 + COMMON: |
617 case DYN_DEF * 16 + COMMON: | 642 case DYN_DEF * 16 + COMMON: |
618 case DYN_WEAK_DEF * 16 + COMMON: | 643 case DYN_WEAK_DEF * 16 + COMMON: |
619 // A common symbol does override a weak definition or a dynamic | 644 // A common symbol does override a weak definition or a dynamic |
620 // definition. | 645 // definition. |
621 return true; | 646 return true; |
622 | 647 |
623 case UNDEF * 16 + COMMON: | 648 case UNDEF * 16 + COMMON: |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 case DYN_WEAK_COMMON * 16 + DYN_WEAK_COMMON: | 734 case DYN_WEAK_COMMON * 16 + DYN_WEAK_COMMON: |
710 // Set the size to the maximum. | 735 // Set the size to the maximum. |
711 *adjust_common_sizes = true; | 736 *adjust_common_sizes = true; |
712 return false; | 737 return false; |
713 | 738 |
714 default: | 739 default: |
715 gold_unreachable(); | 740 gold_unreachable(); |
716 } | 741 } |
717 } | 742 } |
718 | 743 |
| 744 // Issue an error or warning due to symbol resolution. IS_ERROR |
| 745 // indicates an error rather than a warning. MSG is the error |
| 746 // message; it is expected to have a %s for the symbol name. TO is |
| 747 // the existing symbol. OBJECT is where the new symbol was found. |
| 748 |
| 749 // FIXME: We should have better location information here. When the |
| 750 // symbol is defined, we should be able to pull the location from the |
| 751 // debug info if there is any. |
| 752 |
| 753 void |
| 754 Symbol_table::report_resolve_problem(bool is_error, const char* msg, |
| 755 const Symbol* to, Object* object) |
| 756 { |
| 757 std::string demangled(to->demangled_name()); |
| 758 size_t len = strlen(msg) + demangled.length() + 10; |
| 759 char* buf = new char[len]; |
| 760 snprintf(buf, len, msg, demangled.c_str()); |
| 761 |
| 762 const char* objname; |
| 763 if (object != NULL) |
| 764 objname = object->name().c_str(); |
| 765 else |
| 766 objname = _("command line"); |
| 767 |
| 768 if (is_error) |
| 769 gold_error("%s: %s", objname, buf); |
| 770 else |
| 771 gold_warning("%s: %s", objname, buf); |
| 772 |
| 773 delete[] buf; |
| 774 |
| 775 if (to->source() == Symbol::FROM_OBJECT) |
| 776 objname = to->object()->name().c_str(); |
| 777 else |
| 778 objname = _("command line"); |
| 779 gold_info("%s: %s: previous definition here", program_name, objname); |
| 780 } |
| 781 |
719 // A special case of should_override which is only called for a strong | 782 // A special case of should_override which is only called for a strong |
720 // defined symbol from a regular object file. This is used when | 783 // defined symbol from a regular object file. This is used when |
721 // defining special symbols. | 784 // defining special symbols. |
722 | 785 |
723 bool | 786 bool |
724 Symbol_table::should_override_with_special(const Symbol* to) | 787 Symbol_table::should_override_with_special(const Symbol* to) |
725 { | 788 { |
726 bool adjust_common_sizes; | 789 bool adjust_common_sizes; |
727 unsigned int frombits = global_flag | regular_flag | def_flag; | 790 unsigned int frombits = global_flag | regular_flag | def_flag; |
728 bool ret = Symbol_table::should_override(to, frombits, NULL, | 791 bool ret = Symbol_table::should_override(to, frombits, NULL, |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
889 #endif | 952 #endif |
890 | 953 |
891 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | 954 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) |
892 template | 955 template |
893 void | 956 void |
894 Symbol_table::override_with_special<64>(Sized_symbol<64>*, | 957 Symbol_table::override_with_special<64>(Sized_symbol<64>*, |
895 const Sized_symbol<64>*); | 958 const Sized_symbol<64>*); |
896 #endif | 959 #endif |
897 | 960 |
898 } // End namespace gold. | 961 } // End namespace gold. |
OLD | NEW |