elf_mem_image.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 The Abseil Authors.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      https://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 // Allow dynamic symbol lookup for in-memory Elf images.
00018 
00019 #ifndef ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_
00020 #define ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_
00021 
00022 // Including this will define the __GLIBC__ macro if glibc is being
00023 // used.
00024 #include <climits>
00025 
00026 // Maybe one day we can rewrite this file not to require the elf
00027 // symbol extensions in glibc, but for right now we need them.
00028 #ifdef ABSL_HAVE_ELF_MEM_IMAGE
00029 #error ABSL_HAVE_ELF_MEM_IMAGE cannot be directly set
00030 #endif
00031 
00032 #if defined(__ELF__) && defined(__GLIBC__) && !defined(__native_client__) && \
00033     !defined(__asmjs__) && !defined(__wasm__)
00034 #define ABSL_HAVE_ELF_MEM_IMAGE 1
00035 #endif
00036 
00037 #if ABSL_HAVE_ELF_MEM_IMAGE
00038 
00039 #include <link.h>  // for ElfW
00040 
00041 namespace absl {
00042 namespace debugging_internal {
00043 
00044 // An in-memory ELF image (may not exist on disk).
00045 class ElfMemImage {
00046  private:
00047   // Sentinel: there could never be an elf image at &kInvalidBaseSentinel.
00048   static const int kInvalidBaseSentinel;
00049 
00050  public:
00051   // Sentinel: there could never be an elf image at this address.
00052   static constexpr const void *const kInvalidBase =
00053     static_cast<const void*>(&kInvalidBaseSentinel);
00054 
00055   // Information about a single vdso symbol.
00056   // All pointers are into .dynsym, .dynstr, or .text of the VDSO.
00057   // Do not free() them or modify through them.
00058   struct SymbolInfo {
00059     const char      *name;      // E.g. "__vdso_getcpu"
00060     const char      *version;   // E.g. "LINUX_2.6", could be ""
00061                                 // for unversioned symbol.
00062     const void      *address;   // Relocated symbol address.
00063     const ElfW(Sym) *symbol;    // Symbol in the dynamic symbol table.
00064   };
00065 
00066   // Supports iteration over all dynamic symbols.
00067   class SymbolIterator {
00068    public:
00069     friend class ElfMemImage;
00070     const SymbolInfo *operator->() const;
00071     const SymbolInfo &operator*() const;
00072     SymbolIterator& operator++();
00073     bool operator!=(const SymbolIterator &rhs) const;
00074     bool operator==(const SymbolIterator &rhs) const;
00075    private:
00076     SymbolIterator(const void *const image, int index);
00077     void Update(int incr);
00078     SymbolInfo info_;
00079     int index_;
00080     const void *const image_;
00081   };
00082 
00083 
00084   explicit ElfMemImage(const void *base);
00085   void                 Init(const void *base);
00086   bool                 IsPresent() const { return ehdr_ != nullptr; }
00087   const ElfW(Phdr)*    GetPhdr(int index) const;
00088   const ElfW(Sym)*     GetDynsym(int index) const;
00089   const ElfW(Versym)*  GetVersym(int index) const;
00090   const ElfW(Verdef)*  GetVerdef(int index) const;
00091   const ElfW(Verdaux)* GetVerdefAux(const ElfW(Verdef) *verdef) const;
00092   const char*          GetDynstr(ElfW(Word) offset) const;
00093   const void*          GetSymAddr(const ElfW(Sym) *sym) const;
00094   const char*          GetVerstr(ElfW(Word) offset) const;
00095   int                  GetNumSymbols() const;
00096 
00097   SymbolIterator begin() const;
00098   SymbolIterator end() const;
00099 
00100   // Look up versioned dynamic symbol in the image.
00101   // Returns false if image is not present, or doesn't contain given
00102   // symbol/version/type combination.
00103   // If info_out is non-null, additional details are filled in.
00104   bool LookupSymbol(const char *name, const char *version,
00105                     int symbol_type, SymbolInfo *info_out) const;
00106 
00107   // Find info about symbol (if any) which overlaps given address.
00108   // Returns true if symbol was found; false if image isn't present
00109   // or doesn't have a symbol overlapping given address.
00110   // If info_out is non-null, additional details are filled in.
00111   bool LookupSymbolByAddress(const void *address, SymbolInfo *info_out) const;
00112 
00113  private:
00114   const ElfW(Ehdr) *ehdr_;
00115   const ElfW(Sym) *dynsym_;
00116   const ElfW(Versym) *versym_;
00117   const ElfW(Verdef) *verdef_;
00118   const ElfW(Word) *hash_;
00119   const char *dynstr_;
00120   size_t strsize_;
00121   size_t verdefnum_;
00122   ElfW(Addr) link_base_;     // Link-time base (p_vaddr of first PT_LOAD).
00123 };
00124 
00125 }  // namespace debugging_internal
00126 }  // namespace absl
00127 
00128 #endif  // ABSL_HAVE_ELF_MEM_IMAGE
00129 
00130 #endif  // ABSL_DEBUGGING_INTERNAL_ELF_MEM_IMAGE_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14