weak_undef_file1.cc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // weak_undef_file1.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 the symbol is resolved to zero
  19. // by the linker and that no dynamic relocation is generated.
  20. // This source is used to build a shared library that defines
  21. // an arbitrary symbol other than the weak undefined symbol
  22. // referenced by the main program. The main program will be
  23. // linked with this library so that the symbol remains undefined.
  24. // Through the use of the embedded RPATH, the program will load
  25. // an alternate shared library that does define the symbol,
  26. // so that we can detect whether the symbol was left for runtime
  27. // resolution.
  28. #include <cstdio>
  29. #include "weak_undef.h"
  30. int is_such_symbol_ = 1;
  31. // We also define a symbol that is not defined by the alternate
  32. // library. The main program contains a weak reference to this
  33. // symbol, and tests that the reference remains weak even after
  34. // the definition was found at link time.
  35. int link_time_only = 1;
  36. extern int v2 __attribute__ ((weak));
  37. int *v3 = &v2;
  38. int
  39. t1()
  40. {
  41. return is_such_symbol_;
  42. }
  43. // Test that a weak reference from a shared library to a symbol
  44. // defined in the main program does get resolved.
  45. int
  46. t2()
  47. {
  48. return (&v2 == NULL) ? -1 : v2;
  49. }
  50. // Test that a weak reference from a shared library to a symbol
  51. // defined in the main program does get resolved.
  52. int
  53. t3()
  54. {
  55. return (v3 == NULL) ? -1 : *v3;
  56. }