ehdr_start_test.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // ehdr_start_test.cc -- test for __ehdr_start linker-defined symbol.
  2. // Copyright (C) 2014-2022 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. // The goal of this program is to produce as many different types of
  18. // relocations as we can in a stand-alone program that does not use
  19. // TLS. This program is compiled without optimization.
  20. #include "config.h"
  21. #include <cassert>
  22. #include <cstdio>
  23. #include "elfcpp.h"
  24. #ifdef EHDR_START_WEAK
  25. #define WEAK_ATTR __attribute__ ((weak))
  26. #else
  27. #define WEAK_ATTR
  28. #endif
  29. extern char __ehdr_start[] WEAK_ATTR;
  30. int
  31. main() {
  32. printf("&__ehdr_start = %p\n", &__ehdr_start);
  33. #ifdef EHDR_START_UNDEF
  34. assert(&__ehdr_start == 0);
  35. #else
  36. assert(&__ehdr_start != NULL);
  37. printf("ELF header: \\x%02x%c%c%c\n", __ehdr_start[0], __ehdr_start[1],
  38. __ehdr_start[2], __ehdr_start[3]);
  39. #ifdef EHDR_START_USER_DEF
  40. assert(__ehdr_start[0] == 'a'
  41. && __ehdr_start[1] == 'b'
  42. && __ehdr_start[2] == 'c'
  43. && __ehdr_start[3] == 'd');
  44. #else
  45. assert(__ehdr_start[elfcpp::EI_MAG0] == elfcpp::ELFMAG0
  46. && __ehdr_start[elfcpp::EI_MAG1] == elfcpp::ELFMAG1
  47. && __ehdr_start[elfcpp::EI_MAG2] == elfcpp::ELFMAG2
  48. && __ehdr_start[elfcpp::EI_MAG3] == elfcpp::ELFMAG3);
  49. #endif
  50. #endif
  51. return 0;
  52. }