elfxx.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* libunwind - a platform-independent unwind library
  2. Copyright (C) 2003, 2005 Hewlett-Packard Co
  3. Copyright (C) 2007 David Mosberger-Tang
  4. Contributed by David Mosberger-Tang <dmosberger@gmail.com>
  5. This file is part of libunwind.
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be
  14. included in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  22. #include <elf.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <sys/mman.h>
  26. #include <sys/stat.h>
  27. #if ELF_CLASS == ELFCLASS32
  28. # define ELF_W(x) ELF32_##x
  29. # define Elf_W(x) Elf32_##x
  30. # define elf_w(x) _Uelf32_##x
  31. #else
  32. # define ELF_W(x) ELF64_##x
  33. # define Elf_W(x) Elf64_##x
  34. # define elf_w(x) _Uelf64_##x
  35. #endif
  36. #include "libunwind_i.h"
  37. extern int elf_w (get_proc_name) (unw_addr_space_t as,
  38. pid_t pid, unw_word_t ip,
  39. char *buf, size_t len,
  40. unw_word_t *offp);
  41. extern int elf_w (get_proc_name_in_image) (unw_addr_space_t as,
  42. struct elf_image *ei,
  43. unsigned long segbase,
  44. unsigned long mapoff,
  45. unw_word_t ip,
  46. char *buf, size_t buf_len, unw_word_t *offp);
  47. extern int elf_w (get_proc_name) (unw_addr_space_t as,
  48. pid_t pid, unw_word_t ip,
  49. char *buf, size_t len,
  50. unw_word_t *offp);
  51. static inline int
  52. elf_w (valid_object) (struct elf_image *ei)
  53. {
  54. if (ei->size <= EI_VERSION)
  55. return 0;
  56. return (memcmp (ei->image, ELFMAG, SELFMAG) == 0
  57. && ((uint8_t *) ei->image)[EI_CLASS] == ELF_CLASS
  58. && ((uint8_t *) ei->image)[EI_VERSION] != EV_NONE
  59. && ((uint8_t *) ei->image)[EI_VERSION] <= EV_CURRENT);
  60. }
  61. static inline int
  62. elf_map_image (struct elf_image *ei, const char *path)
  63. {
  64. #if HAVE_SGX
  65. return -1;
  66. #else
  67. struct stat stat;
  68. int fd;
  69. fd = open (path, O_RDONLY);
  70. if (fd < 0)
  71. return -1;
  72. if (fstat (fd, &stat) < 0)
  73. {
  74. close (fd);
  75. return -1;
  76. }
  77. ei->size = stat.st_size;
  78. ei->image = mmap (NULL, ei->size, PROT_READ, MAP_PRIVATE, fd, 0);
  79. close (fd);
  80. if (ei->image == MAP_FAILED)
  81. return -1;
  82. if (!elf_w (valid_object) (ei))
  83. {
  84. munmap(ei->image, ei->size);
  85. return -1;
  86. }
  87. return 0;
  88. #endif
  89. }