| 6 |
| 7 | #include <dlfcn.h> |
| 8 | #include <elf.h> |
| 9 |
| 10 | #include "base/debug/elf_reader.h" |
| 11 | #include "build/build_config.h" |
| 12 |
| 13 | namespace base { |
| 14 |
| 15 | namespace { |
| 16 |
| 17 | // Returns the unique build ID for a module loaded at |module_addr|. Returns the |
| 18 | // empty string if the function fails to get the build ID. |
| 19 | // |
| 20 | // Build IDs follow a cross-platform format consisting of several fields |
| 21 | // concatenated together: |
| 22 | // - the module's unique ID, and |
| 23 | // - the age suffix for incremental builds. |
| 24 | // |
| 25 | // On POSIX, the unique ID is read from the ELF binary located at |module_addr|. |
| 26 | // The age field is always 0. |
| 27 | std::string GetUniqueBuildId(const void* module_addr) { |
| 28 | base::debug::ElfBuildIdBuffer build_id; |
| 29 | size_t build_id_length = |
| 30 | base::debug::ReadElfBuildId(module_addr, true, build_id); |
| 31 | if (!build_id_length) |
| 32 | return std::string(); |
| 33 |
| 34 | // Append 0 for the age value. |
| 35 | return std::string(build_id, build_id_length) + "0"; |
| 36 | } |
| 37 |
| 38 | // Returns the offset from |module_addr| to the first byte following the last |
| 39 | // executable segment from the ELF file mapped at |module_addr|. |
| 40 | // It's defined this way so that any executable address from this module is in |
| 41 | // range [addr, addr + GetLastExecutableOffset(addr)). |
| 42 | // If no executable segment is found, returns 0. |
| 43 | size_t GetLastExecutableOffset(const void* module_addr) { |
| 44 | size_t max_offset = 0; |
| 45 | for (const Phdr& header : base::debug::GetElfProgramHeaders(module_addr)) { |
| 46 | if (header.p_type != PT_LOAD || !(header.p_flags & PF_X)) |
| 47 | continue; |
| 48 |
| 49 | max_offset = std::max(max_offset, |
| 50 | static_cast<size_t>(header.p_vaddr + header.p_memsz)); |
| 51 | } |
| 52 |
| 53 | return max_offset; |
| 54 | } |
| 55 |
| 56 | class PosixModule : public ModuleCache::Module { |
| 57 | public: |
| 58 | PosixModule(const Dl_info& dl_info); |
| 59 |
| 60 | PosixModule(const PosixModule&) = delete; |
| 61 | PosixModule& operator=(const PosixModule&) = delete; |
| 62 |
| 63 | // ModuleCache::Module |
| 64 | uintptr_t GetBaseAddress() const override { return base_address_; } |
| 65 | std::string GetId() const override { return id_; } |
| 66 | FilePath GetDebugBasename() const override { return debug_basename_; } |
| 67 | size_t GetSize() const override { return size_; } |
| 68 | bool IsNative() const override { return true; } |
| 69 |
| 70 | private: |
| 71 | uintptr_t base_address_; |
| 72 | std::string id_; |
| 73 | FilePath debug_basename_; |
| 74 | size_t size_; |
| 75 | }; |
| 76 |
| 77 | PosixModule::PosixModule(const Dl_info& dl_info) |
| 78 | : base_address_(reinterpret_cast<uintptr_t>(dl_info.dli_fbase)), |
| 79 | id_(GetUniqueBuildId(dl_info.dli_fbase)), |
| 80 | debug_basename_(FilePath(dl_info.dli_fname).BaseName()), |
| 81 | size_(GetLastExecutableOffset(dl_info.dli_fbase)) {} |
| 82 |
| 83 | } // namespace |
| 84 |
| 85 | // static |
| 86 | std::unique_ptr<const ModuleCache::Module> ModuleCache::CreateModuleForAddress( |
| 87 | uintptr_t address) { |
| 88 | #if defined(ARCH_CPU_ARM64) |
| 89 | // arm64 has execute-only memory (XOM) protecting code pages from being read. |
| 90 | // PosixModule reads executable pages in order to extract module info. This |
| 91 | // may result in a crash if the module is mapped as XOM |
| 92 | // (https://crbug.com/957801). |
| 93 | return nullptr; |
| 94 | #else |
| 95 | Dl_info info; |
| 96 | if (!dladdr(reinterpret_cast<const void*>(address), &info)) |
| 97 | return nullptr; |
| 98 |
| 99 | return std::make_unique<PosixModule>(info); |
| 100 | #endif |
| 101 | } |
| 102 |
| 103 | } // namespace base |
| 104 |