weak_undef_file2.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // weak_undef_file2.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. // the weak undefined symbol referenced by the main program.
  22. // The main program will be linked with a library that does not
  23. // provide this definition, so that the symbol remains undefined.
  24. // Through the use of the embedded RPATH, the program will load
  25. // this 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. int no_such_symbol_ = 2;
  32. extern int v2 __attribute__ ((weak));
  33. int *v3 = &v2;
  34. int
  35. t1()
  36. {
  37. return no_such_symbol_;
  38. }
  39. // Test that a weak reference from a shared library to a symbol
  40. // defined in the main program does get resolved.
  41. int
  42. t2()
  43. {
  44. return (&v2 == NULL) ? -1 : v2;
  45. }
  46. // Test that a weak reference from a shared library to a symbol
  47. // defined in the main program does get resolved.
  48. int
  49. t3()
  50. {
  51. return (v3 == NULL) ? -1 : *v3;
  52. }