|
6 | 6 | #include <dlfcn.h> |
7 | 7 | #include <link.h> |
8 | 8 |
|
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +#include <fstream> |
9 | 12 | #include <memory> |
10 | 13 |
|
11 | 14 | #include "Common.hpp" |
12 | 15 | #include "../../Utils.hpp" |
13 | 16 |
|
14 | 17 | namespace hat::process { |
15 | 18 |
|
| 19 | +#ifdef LIBHAT_LP64 |
| 20 | + using elf_ehdr_t = Elf64_Ehdr; |
| 21 | + using elf_shdr_t = Elf64_Shdr; |
| 22 | + static constexpr unsigned char elf_class = ELFCLASS64; |
| 23 | +#else |
| 24 | + using elf_ehdr_t = Elf32_Ehdr; |
| 25 | + using elf_shdr_t = Elf32_Shdr; |
| 26 | + static constexpr unsigned char elf_class = ELFCLASS32; |
| 27 | +#endif |
| 28 | + |
| 29 | + using Handle = std::unique_ptr<void, decltype([](void* handle) { |
| 30 | + dlclose(handle); |
| 31 | + })>; |
| 32 | + |
16 | 33 | hat::process::module get_process_module() { |
17 | 34 | const auto module = get_module({}); |
18 | 35 | if (!module) { |
@@ -51,6 +68,113 @@ namespace hat::process { |
51 | 68 | return {reinterpret_cast<std::byte*>(this->address()), max}; |
52 | 69 | } |
53 | 70 |
|
| 71 | + std::span<std::byte> module::get_section_data(std::string_view name) const { |
| 72 | + std::span<std::byte> data{}; |
| 73 | + this->for_each_section([&](auto section_name, auto section_data, auto) -> bool { |
| 74 | + if (section_name == name) { |
| 75 | + data = section_data; |
| 76 | + return false; |
| 77 | + } |
| 78 | + return true; |
| 79 | + }); |
| 80 | + return data; |
| 81 | + } |
| 82 | + |
| 83 | + void module::for_each_section(const std::function<bool(std::string_view, std::span<std::byte>, hat::protection)>& callback) const { |
| 84 | + auto phdrCallback = [&](const dl_phdr_info& info) { |
| 85 | + const auto addr = std::bit_cast<uintptr_t>(info.dlpi_addr); |
| 86 | + if (addr != this->address()) { |
| 87 | + return 0; |
| 88 | + } |
| 89 | + |
| 90 | + const char* path = (info.dlpi_name && *info.dlpi_name != '\0') |
| 91 | + ? info.dlpi_name : "/proc/self/exe"; |
| 92 | + std::ifstream file{path, std::ios::binary}; |
| 93 | + if (!file.is_open()) { |
| 94 | + return 0; // ????? |
| 95 | + } |
| 96 | + |
| 97 | + elf_ehdr_t ehdr{}; |
| 98 | + if (!file.read(reinterpret_cast<char*>(&ehdr), EI_NIDENT)) { |
| 99 | + return 0; // Invalid ELF |
| 100 | + } |
| 101 | + |
| 102 | + const auto& e_ident = ehdr.e_ident; |
| 103 | + if (e_ident[EI_MAG0] != ELFMAG0 || e_ident[EI_MAG1] != ELFMAG1 |
| 104 | + || e_ident[EI_MAG2] != ELFMAG2 || e_ident[EI_MAG3] != ELFMAG3 |
| 105 | + || e_ident[EI_CLASS] != elf_class) { |
| 106 | + return 0; // Invalid ELF |
| 107 | + } |
| 108 | + |
| 109 | + if (!file.read(reinterpret_cast<char*>(&ehdr) + EI_NIDENT, sizeof(ehdr) - EI_NIDENT)) { |
| 110 | + return 0; // Invalid ELF |
| 111 | + } |
| 112 | + |
| 113 | + if (!ehdr.e_shnum || ehdr.e_shentsize < sizeof(elf_shdr_t)) { |
| 114 | + return 0; // No section header table, or entries are smaller than expected. Nothing to do |
| 115 | + } |
| 116 | + |
| 117 | + std::vector<char> sections(ehdr.e_shnum * ehdr.e_shentsize); |
| 118 | + if (!file.seekg(static_cast<std::streamoff>(ehdr.e_shoff), std::ios::beg)) { |
| 119 | + return 0; |
| 120 | + } |
| 121 | + if (!file.read(sections.data(), static_cast<std::streamsize>(sections.size()))) { |
| 122 | + return 0; |
| 123 | + } |
| 124 | + |
| 125 | + elf_shdr_t shstrtab{}; |
| 126 | + if (ehdr.e_shstrndx >= ehdr.e_shnum) { |
| 127 | + return 0; |
| 128 | + } |
| 129 | + std::memcpy(&shstrtab, sections.data() + ehdr.e_shstrndx * ehdr.e_shentsize, sizeof(shstrtab)); |
| 130 | + |
| 131 | + std::vector<char> strings(shstrtab.sh_size); |
| 132 | + if (!file.seekg(static_cast<std::streamoff>(shstrtab.sh_offset), std::ios::beg)) { |
| 133 | + return 0; |
| 134 | + } |
| 135 | + if (!file.read(strings.data(), static_cast<std::streamsize>(strings.size()))) { |
| 136 | + return 0; |
| 137 | + } |
| 138 | + |
| 139 | + for (auto it = sections.begin(); it != sections.end(); it += ehdr.e_shentsize) { |
| 140 | + elf_shdr_t header{}; |
| 141 | + std::memcpy(&header, std::to_address(it), sizeof(header)); |
| 142 | + if (header.sh_type == SHT_NULL) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + if (!header.sh_addr || header.sh_name > strings.size() || (header.sh_flags & SHF_ALLOC) == 0) { |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + const char* cstr = strings.data() + header.sh_name; |
| 151 | + const size_t size = strnlen(cstr, strings.size() - header.sh_name); |
| 152 | + const std::string_view name{cstr, size}; |
| 153 | + |
| 154 | + const std::span data{ |
| 155 | + reinterpret_cast<std::byte*>(this->address()) + header.sh_addr, |
| 156 | + header.sh_size |
| 157 | + }; |
| 158 | + |
| 159 | + hat::protection prot = hat::protection::Read; |
| 160 | + if (header.sh_flags & SHF_WRITE) prot |= hat::protection::Write; |
| 161 | + if (header.sh_flags & SHF_EXECINSTR) prot |= hat::protection::Execute; |
| 162 | + |
| 163 | + if (!callback(name, data, prot)) { |
| 164 | + break; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return 1; |
| 169 | + }; |
| 170 | + |
| 171 | + dl_iterate_phdr( |
| 172 | + [](dl_phdr_info* info, size_t, void* data) { |
| 173 | + return (*static_cast<decltype(phdrCallback)*>(data))(*info); |
| 174 | + }, |
| 175 | + &phdrCallback); |
| 176 | + } |
| 177 | + |
54 | 178 | void module::for_each_segment(const std::function<bool(std::span<std::byte>, hat::protection)>& callback) const { |
55 | 179 | auto phdrCallback = [&](const dl_phdr_info& info) { |
56 | 180 | const auto addr = std::bit_cast<uintptr_t>(info.dlpi_addr); |
@@ -90,10 +214,6 @@ namespace hat::process { |
90 | 214 | } |
91 | 215 |
|
92 | 216 | std::optional<hat::process::module> get_module(const std::string_view name) { |
93 | | - using Handle = std::unique_ptr<void, decltype([](void* handle) { |
94 | | - dlclose(handle); |
95 | | - })>; |
96 | | - |
97 | 217 | const std::string buffer{name}; |
98 | 218 | const char* file = buffer.empty() ? nullptr : buffer.c_str(); |
99 | 219 |
|
|
0 commit comments