A thought on the representation of assembly . . .
[aesalon.git] / src / monitor / asm / Disassembler.cpp
blobd420ce7185bdacb50ea13a02314db2200d7b6855
1 #include <iostream>
2 #include "Disassembler.h"
3 #include "platform/BidirectionalPipe.h"
4 #include "platform/ArgumentList.h"
5 #include "misc/String.h"
6 #include "Message.h"
8 namespace Aesalon {
9 namespace Monitor {
10 namespace ASM {
12 Disassembler::Disassembler(Misc::SmartPointer<ELF::Parser> elf_parser) : elf_parser(elf_parser) {
13 Platform::ArgumentList al;
14 al.add_argument("/usr/bin/objdump"); /* NOTE: hardcoded path . . . */
15 al.add_argument("-dMintel");
16 al.add_argument("--section=.text");
17 al.add_argument(elf_parser->get_filename());
19 bi_pipe = new Platform::BidirectionalPipe(al, true);
21 Message::Message(Aesalon::Monitor::Message::DEBUG_MESSAGE, "Beginning disassembly of target");
22 parse_objdump_output();
23 Message::Message(Aesalon::Monitor::Message::DEBUG_MESSAGE, "Disassembly of target completed");
24 bi_pipe = NULL;
27 void Disassembler::parse_objdump_output() {
28 std::string line;
29 Misc::SmartPointer<ELF::Symbol> symbol = NULL;
30 while(bi_pipe->is_open()) {
31 line = bi_pipe->get_string();
32 if(line == "") continue;
34 /*std::cout << "parsing objdump line \"" << line << "\"\n";*/
36 Word address = 0;
37 Misc::String::to<Word>(line, address, true);
38 if(address < elf_parser->get_section(".text")->get_virtual_address()) continue;
40 line.erase(0, line.find(" "));
41 while(line[0] == ' ') line.erase(0, 1);
43 if(line[0] == '<') {
44 line.erase(0, 1);
45 line.erase(line.find(">"));
46 /* NOTE: ignores all symbols beginning with __ . . . */
47 if(line.substr(0, 2) == "__") symbol = NULL;
48 else {
49 symbol = elf_parser->get_symbol(line);
50 /*std::cout << "\tSymbol name is \"" << line << "\"\n";*/
52 continue;
54 /* it's an instruction . . . */
55 if(!symbol.is_valid()) continue; /* Continue if there's no resolved symbol ATM . . . */
56 if(line.find("<") != std::string::npos) line.erase(line.find("<"));
57 bool finished = false;
58 while(!finished) {
59 Byte value = 0;
60 Misc::String::to<Byte>(line.substr(0, 2), value, true);
61 if(value == 0) break;
62 line.erase(0, 1);
64 while(line.length() && line[line.length()-1] == ' ') line.erase(line.length()-1);
65 while(line.length() && (line[0] == ' ' || line[0] == '\t')) {
66 line.erase(0, 1);
68 if(line == "") continue;
69 std::cout << "\tAssembly instruction is \"" << line << "\"\n";
70 /* Now parse the instruction and push it onto the InstructionList for the symbol . . . */
71 if(!symbol_to_il[symbol->get_symbol_name()].is_valid())
72 symbol_to_il[symbol->get_symbol_name()] = new InstructionList(symbol->get_address());
74 symbol_to_il[symbol->get_symbol_name()]->add_instruction(new Instruction(line));
78 } // namespace ASM
79 } // namespace Monitor
80 } // namespace Aesalon