weak_undef_test.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // weak_undef_test.cc -- test handling of weak undefined symbols for gold
  2. // Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. // Written by Cary Coutant <ccoutant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. // We test that we correctly deal with weak undefined symbols.
  18. // We need to make sure that a weak undefined symbol in the main
  19. // program is resolved to zero by the linker and that no dynamic
  20. // relocation is generated. We also make sure that a weak undefined
  21. // symbol in a shared library can resolve to a symbol in the main
  22. // program.
  23. // This file will be linked with a shared library that does not
  24. // define the symbol, so that the symbol remains undefined.
  25. // Through the use of LD_LIBRARY_PATH, the program will load
  26. // an alternate shared library that does define the symbol,
  27. // so that we can detect whether the symbol was left for runtime
  28. // resolution.
  29. // Similarly, this file will be linked with a shared library that
  30. // does define a different symbol, and loaded with an alternate
  31. // shared library that does not define that symbol. We check that
  32. // the weak reference remains weak, and that it is resolved to
  33. // zero at runtime.
  34. #include <cstdio>
  35. #include "weak_undef.h"
  36. extern int no_such_symbol_ __attribute__ ((weak));
  37. extern int link_time_only __attribute__ ((weak));
  38. int *p1 = &no_such_symbol_;
  39. int *p2 = &link_time_only;
  40. int v2 = 42;
  41. int
  42. main()
  43. {
  44. int status = 0;
  45. int v;
  46. if ((v = t1()) != 2)
  47. {
  48. fprintf(stderr, "FAILED weak undef test 1: %s\n",
  49. "bound to wrong library");
  50. status = 1;
  51. }
  52. if ((v = t2()) != 42)
  53. {
  54. fprintf(stderr, "FAILED weak undef test 2: expected %d, got %d\n",
  55. 42, v);
  56. status = 1;
  57. }
  58. if ((v = t3()) != 42)
  59. {
  60. fprintf(stderr, "FAILED weak undef test 3: expected %d, got %d\n",
  61. 42, v);
  62. status = 1;
  63. }
  64. if (&no_such_symbol_ != NULL)
  65. {
  66. fprintf(stderr, "FAILED weak undef test 4: %s\n",
  67. "&no_such_symbol_ is not NULL");
  68. status = 1;
  69. }
  70. if (p1 != NULL)
  71. {
  72. fprintf(stderr, "FAILED weak undef test 5: %s\n",
  73. "p1 is not NULL");
  74. status = 1;
  75. }
  76. if (p2 != NULL)
  77. {
  78. fprintf(stderr, "FAILED weak undef test 6: %s\n",
  79. "p2 is not NULL");
  80. status = 1;
  81. }
  82. return status;
  83. }