Image.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  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 met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Debug.h>
  27. #include <AK/Demangle.h>
  28. #include <AK/Memory.h>
  29. #include <AK/QuickSort.h>
  30. #include <AK/StringBuilder.h>
  31. #include <AK/StringView.h>
  32. #include <LibELF/Image.h>
  33. #include <LibELF/Validation.h>
  34. namespace ELF {
  35. Image::Image(ReadonlyBytes bytes, bool verbose_logging)
  36. : m_buffer(bytes.data())
  37. , m_size(bytes.size())
  38. , m_verbose_logging(verbose_logging)
  39. {
  40. parse();
  41. }
  42. Image::Image(const u8* buffer, size_t size, bool verbose_logging)
  43. : Image(ReadonlyBytes { buffer, size }, verbose_logging)
  44. {
  45. }
  46. Image::~Image()
  47. {
  48. }
  49. #if ELF_IMAGE_DEBUG
  50. static const char* object_file_type_to_string(Elf32_Half type)
  51. {
  52. switch (type) {
  53. case ET_NONE:
  54. return "None";
  55. case ET_REL:
  56. return "Relocatable";
  57. case ET_EXEC:
  58. return "Executable";
  59. case ET_DYN:
  60. return "Shared object";
  61. case ET_CORE:
  62. return "Core";
  63. default:
  64. return "(?)";
  65. }
  66. }
  67. #endif
  68. StringView Image::section_index_to_string(unsigned index) const
  69. {
  70. ASSERT(m_valid);
  71. if (index == SHN_UNDEF)
  72. return "Undefined";
  73. if (index >= SHN_LORESERVE)
  74. return "Reserved";
  75. return section(index).name();
  76. }
  77. unsigned Image::symbol_count() const
  78. {
  79. ASSERT(m_valid);
  80. if (!section_count())
  81. return 0;
  82. return section(m_symbol_table_section_index).entry_count();
  83. }
  84. void Image::dump() const
  85. {
  86. #if ELF_IMAGE_DEBUG
  87. dbgln("ELF::Image({:p}) {{", this);
  88. dbgln(" is_valid: {}", is_valid());
  89. if (!is_valid()) {
  90. dbgln("}}");
  91. return;
  92. }
  93. dbgln(" type: {}", object_file_type_to_string(header().e_type));
  94. dbgln(" machine: {}", header().e_machine);
  95. dbgln(" entry: {:x}", header().e_entry);
  96. dbgln(" shoff: {}", header().e_shoff);
  97. dbgln(" shnum: {}", header().e_shnum);
  98. dbgln(" phoff: {}", header().e_phoff);
  99. dbgln(" phnum: {}", header().e_phnum);
  100. dbgln(" shstrndx: {}", header().e_shstrndx);
  101. for_each_program_header([&](const ProgramHeader& program_header) {
  102. dbgln(" Program Header {}: {{", program_header.index());
  103. dbgln(" type: {:x}", program_header.type());
  104. dbgln(" offset: {:x}", program_header.offset());
  105. dbgln(" flags: {:x}", program_header.flags());
  106. dbgln(" }}");
  107. return IterationDecision::Continue;
  108. });
  109. for (unsigned i = 0; i < header().e_shnum; ++i) {
  110. auto& section = this->section(i);
  111. dbgln(" Section {}: {{", i);
  112. dbgln(" name: {}", section.name());
  113. dbgln(" type: {:x}", section.type());
  114. dbgln(" offset: {:x}", section.offset());
  115. dbgln(" size: {}", section.size());
  116. dbgln(" ");
  117. dbgln(" }}");
  118. }
  119. dbgln("Symbol count: {} (table is {})", symbol_count(), m_symbol_table_section_index);
  120. for (unsigned i = 1; i < symbol_count(); ++i) {
  121. auto& sym = symbol(i);
  122. dbgln("Symbol @{}:", i);
  123. dbgln(" Name: {}", sym.name());
  124. dbgln(" In section: {}", section_index_to_string(sym.section_index()));
  125. dbgln(" Value: {}", sym.value());
  126. dbgln(" Size: {}", sym.size());
  127. }
  128. dbgln("}}");
  129. #endif
  130. }
  131. unsigned Image::section_count() const
  132. {
  133. ASSERT(m_valid);
  134. return header().e_shnum;
  135. }
  136. unsigned Image::program_header_count() const
  137. {
  138. ASSERT(m_valid);
  139. return header().e_phnum;
  140. }
  141. bool Image::parse()
  142. {
  143. if (m_size < sizeof(Elf32_Ehdr) || !validate_elf_header(header(), m_size, m_verbose_logging)) {
  144. if (m_verbose_logging)
  145. dbgln("ELF::Image::parse(): ELF Header not valid");
  146. return m_valid = false;
  147. }
  148. if (!validate_program_headers(header(), m_size, m_buffer, m_size, nullptr, m_verbose_logging)) {
  149. if (m_verbose_logging)
  150. dbgln("ELF::Image::parse(): ELF Program Headers not valid");
  151. return m_valid = false;
  152. }
  153. m_valid = true;
  154. // First locate the string tables.
  155. for (unsigned i = 0; i < section_count(); ++i) {
  156. auto& sh = section_header(i);
  157. if (sh.sh_type == SHT_SYMTAB) {
  158. if (m_symbol_table_section_index && m_symbol_table_section_index != i)
  159. return m_valid = false;
  160. m_symbol_table_section_index = i;
  161. }
  162. if (sh.sh_type == SHT_STRTAB && i != header().e_shstrndx) {
  163. if (section_header_table_string(sh.sh_name) == ELF_STRTAB)
  164. m_string_table_section_index = i;
  165. }
  166. }
  167. // Then create a name-to-index map.
  168. for (unsigned i = 0; i < section_count(); ++i) {
  169. auto section = this->section(i);
  170. m_sections.set(section.name(), move(i));
  171. }
  172. return m_valid;
  173. }
  174. StringView Image::table_string(unsigned table_index, unsigned offset) const
  175. {
  176. ASSERT(m_valid);
  177. auto& sh = section_header(table_index);
  178. if (sh.sh_type != SHT_STRTAB)
  179. return nullptr;
  180. size_t computed_offset = sh.sh_offset + offset;
  181. if (computed_offset >= m_size) {
  182. if (m_verbose_logging)
  183. dbgln("SHENANIGANS! Image::table_string() computed offset outside image.");
  184. return {};
  185. }
  186. size_t max_length = min(m_size - computed_offset, (size_t)PAGE_SIZE);
  187. size_t length = strnlen(raw_data(sh.sh_offset + offset), max_length);
  188. return { raw_data(sh.sh_offset + offset), length };
  189. }
  190. StringView Image::section_header_table_string(unsigned offset) const
  191. {
  192. ASSERT(m_valid);
  193. return table_string(header().e_shstrndx, offset);
  194. }
  195. StringView Image::table_string(unsigned offset) const
  196. {
  197. ASSERT(m_valid);
  198. return table_string(m_string_table_section_index, offset);
  199. }
  200. const char* Image::raw_data(unsigned offset) const
  201. {
  202. ASSERT(offset < m_size); // Callers must check indices into raw_data()'s result are also in bounds.
  203. return reinterpret_cast<const char*>(m_buffer) + offset;
  204. }
  205. const Elf32_Ehdr& Image::header() const
  206. {
  207. ASSERT(m_size >= sizeof(Elf32_Ehdr));
  208. return *reinterpret_cast<const Elf32_Ehdr*>(raw_data(0));
  209. }
  210. const Elf32_Phdr& Image::program_header_internal(unsigned index) const
  211. {
  212. ASSERT(m_valid);
  213. ASSERT(index < header().e_phnum);
  214. return *reinterpret_cast<const Elf32_Phdr*>(raw_data(header().e_phoff + (index * sizeof(Elf32_Phdr))));
  215. }
  216. const Elf32_Shdr& Image::section_header(unsigned index) const
  217. {
  218. ASSERT(m_valid);
  219. ASSERT(index < header().e_shnum);
  220. return *reinterpret_cast<const Elf32_Shdr*>(raw_data(header().e_shoff + (index * header().e_shentsize)));
  221. }
  222. Image::Symbol Image::symbol(unsigned index) const
  223. {
  224. ASSERT(m_valid);
  225. ASSERT(index < symbol_count());
  226. auto* raw_syms = reinterpret_cast<const Elf32_Sym*>(raw_data(section(m_symbol_table_section_index).offset()));
  227. return Symbol(*this, index, raw_syms[index]);
  228. }
  229. Image::Section Image::section(unsigned index) const
  230. {
  231. ASSERT(m_valid);
  232. ASSERT(index < section_count());
  233. return Section(*this, index);
  234. }
  235. Image::ProgramHeader Image::program_header(unsigned index) const
  236. {
  237. ASSERT(m_valid);
  238. ASSERT(index < program_header_count());
  239. return ProgramHeader(*this, index);
  240. }
  241. Image::Relocation Image::RelocationSection::relocation(unsigned index) const
  242. {
  243. ASSERT(index < relocation_count());
  244. auto* rels = reinterpret_cast<const Elf32_Rel*>(m_image.raw_data(offset()));
  245. return Relocation(m_image, rels[index]);
  246. }
  247. Image::RelocationSection Image::Section::relocations() const
  248. {
  249. StringBuilder builder;
  250. builder.append(".rel");
  251. builder.append(name());
  252. auto relocation_section = m_image.lookup_section(builder.to_string());
  253. if (relocation_section.type() != SHT_REL)
  254. return static_cast<const RelocationSection>(m_image.section(0));
  255. #if ELF_IMAGE_DEBUG
  256. dbgln("Found relocations for {} in {}", name(), relocation_section.name());
  257. #endif
  258. return static_cast<const RelocationSection>(relocation_section);
  259. }
  260. Image::Section Image::lookup_section(const String& name) const
  261. {
  262. ASSERT(m_valid);
  263. if (auto it = m_sections.find(name); it != m_sections.end())
  264. return section((*it).value);
  265. return section(0);
  266. }
  267. StringView Image::Symbol::raw_data() const
  268. {
  269. auto section = this->section();
  270. return { section.raw_data() + (value() - section.address()), size() };
  271. }
  272. Optional<Image::Symbol> Image::find_demangled_function(const String& name) const
  273. {
  274. Optional<Image::Symbol> found;
  275. for_each_symbol([&](const Image::Symbol& symbol) {
  276. if (symbol.type() != STT_FUNC)
  277. return IterationDecision::Continue;
  278. if (symbol.is_undefined())
  279. return IterationDecision::Continue;
  280. auto demangled = demangle(symbol.name());
  281. auto index_of_paren = demangled.index_of("(");
  282. if (index_of_paren.has_value()) {
  283. demangled = demangled.substring(0, index_of_paren.value());
  284. }
  285. if (demangled != name)
  286. return IterationDecision::Continue;
  287. found = symbol;
  288. return IterationDecision::Break;
  289. });
  290. return found;
  291. }
  292. Optional<Image::Symbol> Image::find_symbol(u32 address, u32* out_offset) const
  293. {
  294. auto symbol_count = this->symbol_count();
  295. if (!symbol_count)
  296. return {};
  297. SortedSymbol* sorted_symbols = nullptr;
  298. if (m_sorted_symbols.is_empty()) {
  299. m_sorted_symbols.ensure_capacity(symbol_count);
  300. for_each_symbol([this](const auto& symbol) {
  301. m_sorted_symbols.append({ symbol.value(), symbol.name(), {}, symbol });
  302. return IterationDecision::Continue;
  303. });
  304. quick_sort(m_sorted_symbols, [](auto& a, auto& b) {
  305. return a.address < b.address;
  306. });
  307. }
  308. sorted_symbols = m_sorted_symbols.data();
  309. for (size_t i = 0; i < symbol_count; ++i) {
  310. if (sorted_symbols[i].address > address) {
  311. if (i == 0)
  312. return {};
  313. auto& symbol = sorted_symbols[i - 1];
  314. if (out_offset)
  315. *out_offset = address - symbol.address;
  316. return symbol.symbol;
  317. }
  318. }
  319. return {};
  320. }
  321. String Image::symbolicate(u32 address, u32* out_offset) const
  322. {
  323. auto symbol_count = this->symbol_count();
  324. if (!symbol_count) {
  325. if (out_offset)
  326. *out_offset = 0;
  327. return "??";
  328. }
  329. SortedSymbol* sorted_symbols = nullptr;
  330. if (m_sorted_symbols.is_empty()) {
  331. m_sorted_symbols.ensure_capacity(symbol_count);
  332. for_each_symbol([this](const auto& symbol) {
  333. m_sorted_symbols.append({ symbol.value(), symbol.name(), {}, symbol });
  334. return IterationDecision::Continue;
  335. });
  336. quick_sort(m_sorted_symbols, [](auto& a, auto& b) {
  337. return a.address < b.address;
  338. });
  339. }
  340. sorted_symbols = m_sorted_symbols.data();
  341. for (size_t i = 0; i < symbol_count; ++i) {
  342. if (sorted_symbols[i].address > address) {
  343. if (i == 0) {
  344. if (out_offset)
  345. *out_offset = 0;
  346. return "!!";
  347. }
  348. auto& symbol = sorted_symbols[i - 1];
  349. auto& demangled_name = symbol.demangled_name;
  350. if (demangled_name.is_null()) {
  351. demangled_name = demangle(symbol.name);
  352. }
  353. if (out_offset) {
  354. *out_offset = address - symbol.address;
  355. return demangled_name;
  356. }
  357. return String::format("%s +0x%x", demangled_name.characters(), address - symbol.address);
  358. }
  359. }
  360. if (out_offset)
  361. *out_offset = 0;
  362. return "??";
  363. }
  364. } // end namespace ELF