Skip to content

Commit

Permalink
disasm: For ELF files, disassemble .text section
Browse files Browse the repository at this point in the history
Since disasm is built in lagom, this requires adding LibELF to lagom.
  • Loading branch information
nico authored and awesomekling committed Aug 9, 2020
1 parent 44a7765 commit 9c136be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Meta/Lagom/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ endif()

file(GLOB AK_SOURCES "../../AK/*.cpp")
file(GLOB LIBCORE_SOURCES "../../Libraries/LibCore/*.cpp")
file(GLOB LIBELF_SOURCES "../../Libraries/LibELF/*.cpp")
file(GLOB LIBGEMINI_SOURCES "../../Libraries/LibGemini/*.cpp")
file(GLOB LIBGFX_SOURCES "../../Libraries/LibGfx/*.cpp")
file(GLOB LIBIPC_SOURCES "../../Libraries/LibIPC/*.cpp")
Expand All @@ -60,7 +61,7 @@ file(GLOB SHELL_SOURCES "../../Shell/*.cpp")
file(GLOB SHELL_TESTS "../../Shell/Tests/*.sh")

set(LAGOM_CORE_SOURCES ${AK_SOURCES} ${LIBCORE_SOURCES})
set(LAGOM_MORE_SOURCES ${LIBIPC_SOURCES} ${LIBLINE_SOURCES} ${LIBJS_SOURCES} ${LIBJS_SUBDIR_SOURCES} ${LIBX86_SOURCES} ${LIBCRYPTO_SOURCES} ${LIBCOMPRESS_SOURCES} ${LIBCRYPTO_SUBDIR_SOURCES} ${LIBTLS_SOURCES} ${LIBMARKDOWN_SOURCES} ${LIBGEMINI_SOURCES} ${LIBGFX_SOURCES})
set(LAGOM_MORE_SOURCES ${LIBELF_SOURCES} ${LIBIPC_SOURCES} ${LIBLINE_SOURCES} ${LIBJS_SOURCES} ${LIBJS_SUBDIR_SOURCES} ${LIBX86_SOURCES} ${LIBCRYPTO_SOURCES} ${LIBCOMPRESS_SOURCES} ${LIBCRYPTO_SUBDIR_SOURCES} ${LIBTLS_SOURCES} ${LIBMARKDOWN_SOURCES} ${LIBGEMINI_SOURCES} ${LIBGFX_SOURCES})

include_directories (../../)
include_directories (../../Libraries/)
Expand Down
23 changes: 21 additions & 2 deletions Userland/disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
#include <AK/LogStream.h>
#include <AK/MappedFile.h>
#include <LibCore/ArgsParser.h>
#include <LibELF/Loader.h>
#include <LibX86/Disassembler.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char** argv)
{
Expand All @@ -44,15 +46,32 @@ int main(int argc, char** argv)
return 1;
}

X86::SimpleInstructionStream stream((const u8*)file.data(), file.size());
const u8* asm_data = (const u8*)file.data();
size_t asm_size = file.size();
size_t file_offset = 0;
if (asm_size >= 4 && strncmp((const char*)asm_data, "\u007fELF", 4) == 0) {
if (auto elf = ELF::Loader::create(asm_data, asm_size)) {
elf->image().for_each_section_of_type(SHT_PROGBITS, [&](const ELF::Image::Section& section) {
// FIXME: Disassemble all SHT_PROGBITS sections, not just .text.
if (section.name() != ".text")
return IterationDecision::Continue;
asm_data = (const u8*)section.raw_data();
asm_size = section.size();
file_offset = section.address();
return IterationDecision::Break;
});
}
}

X86::SimpleInstructionStream stream(asm_data, asm_size);
X86::Disassembler disassembler(stream);

for (;;) {
auto offset = stream.offset();
auto insn = disassembler.next();
if (!insn.has_value())
break;
out() << String::format("%08x", offset) << " " << insn.value().to_string(offset);
out() << String::format("%08x", file_offset + offset) << " " << insn.value().to_string(offset);
}

return 0;
Expand Down

0 comments on commit 9c136be

Please sign in to comment.