elf_mem_image.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #pragma once
  2. // Copyright (c) 2008, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // ---
  31. // Author: Paul Pluzhnikov
  32. //
  33. // Allow dynamic symbol lookup for in-memory Elf images.
  34. #ifndef BASE_ELF_MEM_IMAGE_H_
  35. #define BASE_ELF_MEM_IMAGE_H_
  36. #include "config.h"
  37. #include <features.h> // for __GLIBC__
  38. // Maybe one day we can rewrite this file not to require the elf
  39. // symbol extensions in glibc, but for right now we need them.
  40. #if (defined(__ELF__) && defined(__GLIBC__) && !defined(__native_client__)) || defined(_musl_)
  41. #define HAVE_ELF_MEM_IMAGE 1
  42. #include <stdlib.h>
  43. #include <link.h> // for ElfW
  44. namespace base {
  45. // An in-memory ELF image (may not exist on disk).
  46. class ElfMemImage {
  47. public:
  48. // Sentinel: there could never be an elf image at this address.
  49. static const void *const kInvalidBase;
  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_ != NULL; }
  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 != 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 != 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 base
  113. #endif // __ELF__ and __GLIBC__ and !__native_client__
  114. #endif // BASE_ELF_MEM_IMAGE_H_