elf_mem_image.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "absl/base/config.h"
  23. // Maybe one day we can rewrite this file not to require the elf
  24. // symbol extensions in glibc, but for right now we need them.
  25. #ifdef ABSL_HAVE_ELF_MEM_IMAGE
  26. #error ABSL_HAVE_ELF_MEM_IMAGE cannot be directly set
  27. #endif
  28. #if defined(__ELF__) && !defined(__OpenBSD__) && !defined(__QNX__) && \
  29. !defined(__native_client__) && !defined(__asmjs__) && \
  30. !defined(__wasm__) && !defined(__HAIKU__)
  31. #define ABSL_HAVE_ELF_MEM_IMAGE 1
  32. #endif
  33. #ifdef ABSL_HAVE_ELF_MEM_IMAGE
  34. #include <link.h> // for ElfW
  35. #if defined(__FreeBSD__) && !defined(ElfW)
  36. #define ElfW(x) __ElfN(x)
  37. #endif
  38. namespace absl {
  39. ABSL_NAMESPACE_BEGIN
  40. namespace debugging_internal {
  41. // An in-memory ELF image (may not exist on disk).
  42. class ElfMemImage {
  43. private:
  44. // Sentinel: there could never be an elf image at &kInvalidBaseSentinel.
  45. static const int kInvalidBaseSentinel;
  46. public:
  47. // Sentinel: there could never be an elf image at this address.
  48. static constexpr const void *const kInvalidBase =
  49. static_cast<const void*>(&kInvalidBaseSentinel);
  50. // Information about a single vdso symbol.
  51. // All pointers are into .dynsym, .dynstr, or .text of the VDSO.
  52. // Do not free() them or modify through them.
  53. struct SymbolInfo {
  54. const char *name; // E.g. "__vdso_getcpu"
  55. const char *version; // E.g. "LINUX_2.6", could be ""
  56. // for unversioned symbol.
  57. const void *address; // Relocated symbol address.
  58. const ElfW(Sym) *symbol; // Symbol in the dynamic symbol table.
  59. };
  60. // Supports iteration over all dynamic symbols.
  61. class SymbolIterator {
  62. public:
  63. friend class ElfMemImage;
  64. const SymbolInfo *operator->() const;
  65. const SymbolInfo &operator*() const;
  66. SymbolIterator& operator++();
  67. bool operator!=(const SymbolIterator &rhs) const;
  68. bool operator==(const SymbolIterator &rhs) const;
  69. private:
  70. SymbolIterator(const void *const image, int index);
  71. void Update(int incr);
  72. SymbolInfo info_;
  73. int index_;
  74. const void *const image_;
  75. };
  76. explicit ElfMemImage(const void *base);
  77. void Init(const void *base);
  78. bool IsPresent() const { return ehdr_ != nullptr; }
  79. const ElfW(Phdr)* GetPhdr(int index) const;
  80. const ElfW(Sym)* GetDynsym(int index) const;
  81. const ElfW(Versym)* GetVersym(int index) const;
  82. const ElfW(Verdef)* GetVerdef(int index) const;
  83. const ElfW(Verdaux)* GetVerdefAux(const ElfW(Verdef) *verdef) const;
  84. const char* GetDynstr(ElfW(Word) offset) const;
  85. const void* GetSymAddr(const ElfW(Sym) *sym) const;
  86. const char* GetVerstr(ElfW(Word) offset) const;
  87. int GetNumSymbols() const;
  88. SymbolIterator begin() const;
  89. SymbolIterator end() const;
  90. // Look up versioned dynamic symbol in the image.
  91. // Returns false if image is not present, or doesn't contain given
  92. // symbol/version/type combination.
  93. // If info_out is non-null, additional details are filled in.
  94. bool LookupSymbol(const char *name, const char *version,
  95. int symbol_type, SymbolInfo *info_out) const;
  96. // Find info about symbol (if any) which overlaps given address.
  97. // Returns true if symbol was found; false if image isn't present
  98. // or doesn't have a symbol overlapping given address.
  99. // If info_out is non-null, additional details are filled in.
  100. bool LookupSymbolByAddress(const void *address, SymbolInfo *info_out) const;
  101. private:
  102. const ElfW(Ehdr) *ehdr_;
  103. const ElfW(Sym) *dynsym_;
  104. const ElfW(Versym) *versym_;
  105. const ElfW(Verdef) *verdef_;
  106. const ElfW(Word) *hash_;
  107. const char *dynstr_;
  108. size_t strsize_;
  109. size_t verdefnum_;
  110. ElfW(Addr) link_base_; // Link-time base (p_vaddr of first PT_LOAD).
  111. };
  112. } // namespace debugging_internal
  113. ABSL_NAMESPACE_END
  114. } // namespace absl
  115. #endif // ABSL_HAVE_ELF_MEM_IMAGE
  116. #endif // ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_