| 6 |
| 7 | #include <dlfcn.h> |
| 8 | #include <mach-o/getsect.h> |
| 9 | #include <uuid/uuid.h> |
| 10 |
| 11 | #include "base/strings/string_number_conversions.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 are created by the concatenation of the module's GUID (Windows) / |
| 21 | // UUID (Mac) and an "age" field that indicates how many times that GUID/UUID |
| 22 | // has been reused. In Windows binaries, the "age" field is present in the |
| 23 | // module header, but on the Mac, UUIDs are never reused and so the "age" value |
| 24 | // appended to the UUID is always 0. |
| 25 | std::string GetUniqueId(const void* module_addr) { |
| 26 | const mach_header_64* mach_header = |
| 27 | reinterpret_cast<const mach_header_64*>(module_addr); |
| 28 | DCHECK_EQ(MH_MAGIC_64, mach_header->magic); |
| 29 |
| 30 | size_t offset = sizeof(mach_header_64); |
| 31 | size_t offset_limit = sizeof(mach_header_64) + mach_header->sizeofcmds; |
| 32 |
| 33 | for (uint32_t i = 0; i < mach_header->ncmds; ++i) { |
| 34 | if (offset + sizeof(load_command) >= offset_limit) |
| 35 | return std::string(); |
| 36 |
| 37 | const load_command* current_cmd = reinterpret_cast<const load_command*>( |
| 38 | reinterpret_cast<const uint8_t*>(mach_header) + offset); |
| 39 |
| 40 | if (offset + current_cmd->cmdsize > offset_limit) { |
| 41 | // This command runs off the end of the command list. This is malformed. |
| 42 | return std::string(); |
| 43 | } |
| 44 |
| 45 | if (current_cmd->cmd == LC_UUID) { |
| 46 | if (current_cmd->cmdsize < sizeof(uuid_command)) { |
| 47 | // This "UUID command" is too small. This is malformed. |
| 48 | return std::string(); |
| 49 | } |
| 50 |
| 51 | const uuid_command* uuid_cmd = |
| 52 | reinterpret_cast<const uuid_command*>(current_cmd); |
| 53 | static_assert(sizeof(uuid_cmd->uuid) == sizeof(uuid_t), |
| 54 | "UUID field of UUID command should be 16 bytes."); |
| 55 | // The ID is comprised of the UUID concatenated with the Mac's "age" value |
| 56 | // which is always 0. |
| 57 | return HexEncode(&uuid_cmd->uuid, sizeof(uuid_cmd->uuid)) + "0"; |
| 58 | } |
| 59 | offset += current_cmd->cmdsize; |
| 60 | } |
| 61 | return std::string(); |
| 62 | } |
| 63 |
| 64 | // Returns the size of the _TEXT segment of the module loaded at |module_addr|. |
| 65 | size_t GetModuleTextSize(const void* module_addr) { |
| 66 | const mach_header_64* mach_header = |
| 67 | reinterpret_cast<const mach_header_64*>(module_addr); |
| 68 | DCHECK_EQ(MH_MAGIC_64, mach_header->magic); |
| 69 | unsigned long module_size; |
| 70 | getsegmentdata(mach_header, SEG_TEXT, &module_size); |
| 71 | return module_size; |
| 72 | } |
| 73 |
| 74 | } // namespace |
| 75 |
| 76 | class MacModule : public ModuleCache::Module { |
| 77 | public: |
| 78 | MacModule(const Dl_info& dl_info) |
| 79 | : base_address_(reinterpret_cast<uintptr_t>(dl_info.dli_fbase)), |
| 80 | id_(GetUniqueId(dl_info.dli_fbase)), |
| 81 | debug_basename_(FilePath(dl_info.dli_fname).BaseName()), |
| 82 | size_(GetModuleTextSize(dl_info.dli_fbase)) {} |
| 83 |
| 84 | MacModule(const MacModule&) = delete; |
| 85 | MacModule& operator=(const MacModule&) = delete; |
| 86 |
| 87 | // ModuleCache::Module |
| 88 | uintptr_t GetBaseAddress() const override { return base_address_; } |
| 89 | std::string GetId() const override { return id_; } |
| 90 | FilePath GetDebugBasename() const override { return debug_basename_; } |
| 91 | size_t GetSize() const override { return size_; } |
| 92 | bool IsNative() const override { return true; } |
| 93 |
| 94 | private: |
| 95 | uintptr_t base_address_; |
| 96 | std::string id_; |
| 97 | FilePath debug_basename_; |
| 98 | size_t size_; |
| 99 | }; |
| 100 |
| 101 | // static |
| 102 | std::unique_ptr<const ModuleCache::Module> ModuleCache::CreateModuleForAddress( |
| 103 | uintptr_t address) { |
| 104 | Dl_info info; |
| 105 | if (!dladdr(reinterpret_cast<const void*>(address), &info)) |
| 106 | return nullptr; |
| 107 | return std::make_unique<MacModule>(info); |
| 108 | } |
| 109 |
| 110 | } // namespace base |
| 111 |