elf_mem_image.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright 2017 The Abseil Authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. // Allow dynamic symbol lookup for in-memory Elf images.
  17. #ifndef ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_
  18. #define ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_
  19. // Including this will define the __GLIBC__ macro if glibc is being
  20. // used.
  21. #include <climits>
  22. #include <cstdint>
  23. #include "absl/base/config.h"
  24. // Maybe one day we can rewrite this file not to require the elf
  25. // symbol extensions in glibc, but for right now we need them.
  26. #ifdef ABSL_HAVE_ELF_MEM_IMAGE
  27. #error ABSL_HAVE_ELF_MEM_IMAGE cannot be directly set
  28. #endif
  29. #if defined(__ELF__) && !defined(__OpenBSD__) && !defined(__QNX__) && \
  30. !defined(__native_client__) && !defined(__asmjs__) && \
  31. !defined(__wasm__) && !defined(__HAIKU__) && !defined(__sun) && \
  32. !defined(__VXWORKS__) && !defined(__hexagon__)
  33. #define ABSL_HAVE_ELF_MEM_IMAGE 1
  34. #endif
  35. #ifdef ABSL_HAVE_ELF_MEM_IMAGE
  36. #include <link.h> // for ElfW
  37. #if defined(__FreeBSD__) && !defined(ElfW)
  38. #define ElfW(x) __ElfN(x)
  39. #endif
  40. namespace absl {
  41. ABSL_NAMESPACE_BEGIN
  42. namespace debugging_internal {
  43. // An in-memory ELF image (may not exist on disk).
  44. class ElfMemImage {
  45. private:
  46. // Sentinel: there could never be an elf image at &kInvalidBaseSentinel.
  47. static const int kInvalidBaseSentinel;
  48. public:
  49. // Sentinel: there could never be an elf image at this address.
  50. static constexpr const void *const kInvalidBase =
  51. static_cast<const void*>(&kInvalidBaseSentinel);
  52. // Information about a single vdso symbol.
  53. // All pointers are into .dynsym, .dynstr, or .text of the VDSO.
  54. // Do not free() them or modify through them.
  55. struct SymbolInfo {
  56. const char *name; // E.g. "__vdso_getcpu"
  57. const char *version; // E.g. "LINUX_2.6", could be ""
  58. // for unversioned symbol.
  59. const void *address; // Relocated symbol address.
  60. const ElfW(Sym) *symbol; // Symbol in the dynamic symbol table.
  61. };
  62. // Supports iteration over all dynamic symbols.
  63. class SymbolIterator {
  64. public:
  65. friend class ElfMemImage;
  66. const SymbolInfo *operator->() const;
  67. const SymbolInfo &operator*() const;
  68. SymbolIterator& operator++();
  69. bool operator!=(const SymbolIterator &rhs) const;
  70. bool operator==(const SymbolIterator &rhs) const;
  71. private:
  72. SymbolIterator(const void *const image, uint32_t index);
  73. void Update(uint32_t incr);
  74. SymbolInfo info_;
  75. uint32_t index_;
  76. const void *const image_;
  77. };
  78. explicit ElfMemImage(const void *base);
  79. void Init(const void *base);
  80. bool IsPresent() const { return ehdr_ != nullptr; }
  81. const ElfW(Phdr)* GetPhdr(int index) const;
  82. const ElfW(Sym) * GetDynsym(uint32_t index) const;
  83. const ElfW(Versym)* GetVersym(uint32_t index) const;
  84. const ElfW(Verdef)* GetVerdef(int index) const;
  85. const ElfW(Verdaux)* GetVerdefAux(const ElfW(Verdef) *verdef) const;
  86. const char* GetDynstr(ElfW(Word) offset) const;
  87. const void* GetSymAddr(const ElfW(Sym) *sym) const;
  88. const char* GetVerstr(ElfW(Word) offset) const;
  89. uint32_t GetNumSymbols() const;
  90. SymbolIterator begin() const;
  91. SymbolIterator end() const;
  92. // Look up versioned dynamic symbol in the image.
  93. // Returns false if image is not present, or doesn't contain given
  94. // symbol/version/type combination.
  95. // If info_out is non-null, additional details are filled in.
  96. bool LookupSymbol(const char *name, const char *version,
  97. int symbol_type, SymbolInfo *info_out) const;
  98. // Find info about symbol (if any) which overlaps given address.
  99. // Returns true if symbol was found; false if image isn't present
  100. // or doesn't have a symbol overlapping given address.
  101. // If info_out is non-null, additional details are filled in.
  102. bool LookupSymbolByAddress(const void *address, SymbolInfo *info_out) const;
  103. private:
  104. const ElfW(Ehdr) *ehdr_;
  105. const ElfW(Sym) *dynsym_;
  106. const ElfW(Versym) *versym_;
  107. const ElfW(Verdef) *verdef_;
  108. const char *dynstr_;
  109. uint32_t num_syms_;
  110. size_t strsize_;
  111. size_t verdefnum_;
  112. ElfW(Addr) link_base_; // Link-time base (p_vaddr of first PT_LOAD).
  113. };
  114. } // namespace debugging_internal
  115. ABSL_NAMESPACE_END
  116. } // namespace absl
  117. #endif // ABSL_HAVE_ELF_MEM_IMAGE
  118. #endif // ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_