justsyms_1.cc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // justsyms_1.cc -- test --just-symbols for gold
  2. // Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@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. // The Linux kernel builds an object file using a linker script, and
  18. // then links against that object file using the -R option. This is a
  19. // test for that usage.
  20. #include <cassert>
  21. #include <csignal>
  22. #include <cstddef>
  23. #include <cstdlib>
  24. #include <stdint.h>
  25. extern char justsyms_string[];
  26. // We expect to get a SIGSEGV.
  27. static void
  28. handle_sigsegv(int)
  29. {
  30. exit(0);
  31. }
  32. int
  33. main(int, char**)
  34. {
  35. // The linker script should arrange for this symbol to be exactly at
  36. // address 0x10000.
  37. assert(reinterpret_cast<uintptr_t>(justsyms_string) == 0x100);
  38. // However, since the file was linked with --just-symbols, we should
  39. // not be able to actually access the symbol.
  40. signal(SIGSEGV, handle_sigsegv);
  41. char c = justsyms_string[0];
  42. exit(c == '\0' ? 1 : c);
  43. }