Skip to content

Commit

Permalink
disassemble: approximate output formatting of objdump
Browse files Browse the repository at this point in the history
Make output formatting more similar to that of objdump.
It makes the result prettier, and opens possibilities for
direct output comparison.
  • Loading branch information
batuzovk committed May 31, 2024
1 parent fe90363 commit a40ea76
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions disassemble/disassemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
#include "InstructionDecoder.h"

#include <iostream>
#include <iomanip>
using namespace std;
using namespace Dyninst;
using namespace ParseAPI;
using namespace InstructionAPI;

static const int l1_width = 7;

int main(int argc, char** argv) {
if(argc != 2) {
printf("Usage: %s <binary path>\n", argv[0]);
Expand Down Expand Up @@ -48,30 +51,47 @@ int main(int argc, char** argv) {
cout << error;
return -1;
}
auto fit = all.begin();
Function* f = *fit;
// create an Instruction decoder which will convert the binary opcodes to strings
InstructionDecoder decoder(f->isrc()->getPtrToInstruction(f->addr()),
InstructionDecoder::maxInstructionLength, f->region()->getArch());
for(; fit != all.end(); ++fit) {
InstructionDecoder decoder((const void *)nullptr, 1, sts->getArch());
for(auto fit = all.begin(); fit != all.end(); ++fit) {
Function* f = *fit;
// get address of entry point for current function
Address crtAddr = f->addr();
int instr_count = 0;
instr = decoder.decode((unsigned char*)f->isrc()->getPtrToInstruction(crtAddr));
auto fbl = f->blocks().end();
fbl--;
Block* b = *fbl;
Address lastAddr = b->end();
// if current function has zero instructions, d o n t output it
if(crtAddr == lastAddr)
continue;
cout << "\n\n\"" << f->name() << "\" :";
cout << "\n\n" << hex << setfill('0') << setw(2 * sts->getAddressWidth()) << f->addr() << " <" << f->name() << ">:\n";
while(crtAddr < lastAddr) {
// decode current instruction
instr = decoder.decode((unsigned char*)f->isrc()->getPtrToInstruction(crtAddr));
cout << "\n" << hex << crtAddr;
cout << ": \"" << instr.format() << "\"";
const unsigned char *instr_ptr = (const unsigned char *)f->isrc()->getPtrToInstruction(crtAddr);
instr = decoder.decode(instr_ptr);

// failed to decode the instruction
if (instr.size() == 0)
break;

// pretty print it
cout << hex << setfill(' ') << setw(8) << crtAddr << ": ";
for (size_t i = 0; i < instr.size() && i < l1_width; i++) {
cout << hex << setfill('0') << setw(2) << (unsigned)instr_ptr[i] << " ";
}
for (size_t i = min(instr.size(), (size_t)l1_width); i < 8; i++) {
cout << " ";
}
cout << instr.format() << "\n";
if (instr.size() > l1_width) {
cout << hex << setfill(' ') << setw(8) << crtAddr + l1_width << ": ";
for (size_t i = l1_width; i < instr.size(); i++) {
cout << hex << setfill('0') << setw(2) << (unsigned)instr_ptr[i] << " ";
}
cout << "\n";
}

// go to the address of the next instruction
crtAddr += instr.size();
instr_count++;
Expand Down

0 comments on commit a40ea76

Please # to comment.