]>
git.siccegge.de Git - frida/frida.git/blob - src/disassembler/llvm/LLVMDisassembler.cxx
25a4ecc25303087bee7c3b3e5265c5aa3bc91257
1 #include "disassembler/llvm/LLVMDisassembler.hxx"
2 #include "disassembler/llvm/LLVMBasicBlock.hxx"
3 #include "disassembler/llvm/LLVMFunction.hxx"
9 using namespace llvm::object
;
12 * TODO: fallback code falls die Datei kein ELF/PE/COFF/MacO/.. binary
13 * ist sondern z.B. einfach nur Instruktionen oder ein Bootsektor oder
16 LLVMDisassembler::LLVMDisassembler(const std::string
& filename
)
17 : Disassembler(filename
)
18 , logger(log4cxx::Logger::getLogger("LLVMDisassembler"))
19 , triple("unknown-unknown-unknown")
21 LOG4CXX_DEBUG(logger
, "Handling file" << filename
);
22 auto result
= createBinary(filename
);
25 if ((ec
= result
.getError())) {
26 LOG4CXX_ERROR(logger
, "Failed to load Binary" << ec
.message());
31 binary
.reset(result
.get());
33 o
= dyn_cast
<ObjectFile
>(binary
.get());
35 triple
.setArch(Triple::ArchType(o
->getArch()));
36 std::string
tripleName(triple
.getTriple());
38 LOG4CXX_INFO(logger
, "Architecture " << tripleName
);
42 target
= TargetRegistry::lookupTarget("", triple
, es
);
44 LOG4CXX_ERROR(logger
, es
);
48 LOG4CXX_INFO(logger
, "Target " << target
->getName());
50 MRI
.reset(target
->createMCRegInfo(tripleName
));
52 LOG4CXX_ERROR(logger
, "no register info for target " << tripleName
);
56 // Set up disassembler.
57 AsmInfo
.reset(target
->createMCAsmInfo(*MRI
, tripleName
));
59 LOG4CXX_ERROR(logger
, "no assembly info for target " << tripleName
);
63 STI
.reset(target
->createMCSubtargetInfo(tripleName
, "", ""));
65 LOG4CXX_ERROR(logger
, "no subtarget info for target " << tripleName
);
69 MII
.reset(target
->createMCInstrInfo());
71 LOG4CXX_ERROR(logger
, "no instruction info for target " << tripleName
);
75 DisAsm
.reset(target
->createMCDisassembler(*STI
));
77 LOG4CXX_ERROR(logger
, "no disassembler for target " << tripleName
);
81 MOFI
.reset(new MCObjectFileInfo
);
82 Ctx
.reset(new MCContext(AsmInfo
.get(), MRI
.get(), MOFI
.get()));
84 target
->createMCRelocationInfo(tripleName
, *Ctx
.get()));
87 MCObjectSymbolizer::createObjectSymbolizer(*Ctx
.get(), RelInfo
, o
));
89 DisAsm
->setSymbolizer(Symzer
);
92 MIA
.reset(target
->createMCInstrAnalysis(MII
.get()));
94 int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
95 IP
.reset(target
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
, *MII
, *MRI
, *STI
));
97 LOG4CXX_ERROR(logger
, "no instruction printer for target " << tripleName
);
101 IP
->setPrintImmHex(llvm::HexStyle::C
);
102 IP
->setPrintImmHex(true);
104 OwningPtr
<MCObjectDisassembler
> OD(
105 new MCObjectDisassembler(*o
, *DisAsm
, *MIA
));
106 Mod
.reset(OD
->buildModule(false));
113 LLVMDisassembler::~LLVMDisassembler() {
114 std::for_each(functions
.begin(), functions
.end(),
115 [](std::pair
<uint64_t,LLVMFunction
*> it
) {
118 std::for_each(blocks
.begin(), blocks
.end(),
119 [](std::pair
<uint64_t, LLVMBasicBlock
*> it
) {
124 void LLVMDisassembler::disassemble() {
125 std::stack
<LLVMFunction
*> remaining_functions
;
126 std::stack
<LLVMBasicBlock
*> remaining_blocks
;
127 SectionRef text_section
= sections
[".text"];
129 for (auto x
= symbols
.begin(); x
!= symbols
.end(); ++x
) {
132 SymbolRef::Type symbol_type
;
135 if (text_section
.containsSymbol(x
->second
, contains
) || !contains
)
138 if (x
->second
.getType(symbol_type
)
139 || SymbolRef::ST_Function
!= symbol_type
)
142 if (!x
->second
.getAddress(result
)) {
143 LLVMFunction
* fun
= new LLVMFunction(x
->first
, result
);
144 remaining_functions
.push(fun
);
145 functions
.insert(std::make_pair(result
, fun
));
146 LOG4CXX_DEBUG(logger
, "Disasembling " << x
->first
);
151 text_section
.getContents(bytes
);
152 StringRefMemoryObject
ref(bytes
);
154 while (remaining_functions
.size()) {
155 LLVMFunction
* current_function
= remaining_functions
.top();
156 remaining_functions
.pop();
158 LOG4CXX_DEBUG(logger
, "Handling function " << current_function
->getName());
160 // if ("_start" != current_function->getName())
163 LLVMBasicBlock
* block
= new LLVMBasicBlock(current_function
->getStartAddress(), this);
164 remaining_blocks
.push(block
);
165 blocks
.insert(std::make_pair(block
->getStartAddress(), block
));
167 while (remaining_blocks
.size()) {
168 LLVMBasicBlock
* current_block
= remaining_blocks
.top();
169 remaining_blocks
.pop();
171 LOG4CXX_DEBUG(logger
, "Handling Block starting at " << std::hex
<< current_block
->getStartAddress());
174 uint64_t base_address
;
175 text_section
.getAddress(base_address
);
176 uint64_t current_address
= current_block
->getStartAddress() - base_address
;
180 llvm::raw_string_ostream
s(buf
);
182 if(llvm::MCDisassembler::Success
==
183 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
185 uint8_t bytes
[inst_size
+2];
186 ref
.readBytes(current_address
, inst_size
, bytes
);
188 for(uint8_t* cur
= bytes
; cur
< bytes
+ inst_size
; ++cur
) {
194 IP
->printInst(&inst
, s
, "");
196 LOG4CXX_DEBUG(logger
, std::hex
<< current_address
+ base_address
<< s
.str());
199 if (MIA
->evaluateBranch(inst
, current_address
, inst_size
, jmptarget
)) {
200 jmptarget
+= base_address
;
201 if (!MIA
->isIndirectBranch(inst
)) {
202 if (MIA
->isCall(inst
)) {
203 if (functions
.find(jmptarget
) == functions
.end()) {
205 s
<< "<Unnamed 0x" << std::hex
<< jmptarget
<< ">";
206 LLVMFunction
* fun
= new LLVMFunction(s
.str(), jmptarget
);
207 functions
.insert(std::make_pair(jmptarget
, fun
));
208 remaining_functions
.push(fun
);
211 if (blocks
.find(jmptarget
) == blocks
.end()) {
212 LLVMBasicBlock
* block
= new LLVMBasicBlock(jmptarget
, this);
213 blocks
.insert(std::make_pair(block
->getStartAddress(), block
));
214 current_block
->setNextBlock(0, block
->getStartAddress());
215 remaining_blocks
.push(block
);
217 if (MIA
->isConditionalBranch(inst
)) {
218 jmptarget
= base_address
+ current_address
+ inst_size
;
219 if (blocks
.find(jmptarget
) == blocks
.end()) {
220 LLVMBasicBlock
* block
= new LLVMBasicBlock(jmptarget
, this);
221 blocks
.insert(std::make_pair(block
->getStartAddress(), block
));
222 current_block
->setNextBlock(1, block
->getStartAddress());
223 remaining_blocks
.push(block
);
234 if (inst_size
== 0 || MIA
->isTerminator(inst
) || MIA
->isBranch(inst
)) {
235 current_block
->setEndAddress(current_address
+ base_address
+ inst_size
);
236 LOG4CXX_DEBUG(logger
, "Finished Block at " << std::hex
<<
237 current_block
->getEndAddress());
240 current_address
+= inst_size
;
243 LOG4CXX_DEBUG(logger
, "Finished function " << current_function
->getName());
246 // Split blocks where jumps are going inside the block
247 for (auto it
= blocks
.begin(); it
!= blocks
.end(); ++it
) {
248 LLVMBasicBlock
* current_block
= it
->second
;
250 uint64_t base_address
;
251 text_section
.getAddress(base_address
);
252 uint64_t current_address
= current_block
->getStartAddress() - base_address
;
253 while(current_block
->getEndAddress() - base_address
!= current_address
) {
256 llvm::raw_string_ostream
s(buf
);
258 if(llvm::MCDisassembler::Success
==
259 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
260 auto other
= blocks
.find(current_address
+ inst_size
+ base_address
);
262 if (other
!= blocks
.end()) {
263 LOG4CXX_DEBUG(logger
, "Shortening block starting at "
265 << current_block
->getStartAddress()
268 current_block
->setEndAddress(current_address
+ inst_size
+ base_address
);
269 current_block
->setNextBlock(0, other
->first
);
270 current_block
->setNextBlock(1, 0);
275 current_address
+= inst_size
;
280 void LLVMDisassembler::readSymbols() {
282 symbol_iterator
si(o
->symbol_begin()), se(o
->symbol_end());
283 for (; si
!= se
; ++si
) {
285 if ((ec
= si
->getName(name
))) {
286 LOG4CXX_ERROR(logger
, ec
.message());
289 LOG4CXX_DEBUG(logger
, "Added symbol " << name
.str());
290 symbols
.insert(make_pair(name
.str(), *si
));
294 void LLVMDisassembler::readSections() {
296 section_iterator
i(o
->section_begin()), e(o
->section_end());
297 for (; i
!= e
; ++i
) {
299 if ((ec
= i
->getName(name
))) {
300 LOG4CXX_ERROR(logger
, ec
.message());
303 LOG4CXX_DEBUG(logger
, "Added section " << name
.str());
304 sections
.insert(make_pair(name
.str(), *i
));
309 void LLVMDisassembler::forEachFunction(std::function
<void (uint64_t, Function
*)> callback
) {
310 std::for_each(functions
.begin(), functions
.end(),
311 [&](std::pair
<uint64_t, LLVMFunction
*> x
) {
312 callback(x
.first
, x
.second
);
316 void LLVMDisassembler::printEachInstruction(uint64_t start
, uint64_t end
,
317 std::function
<void (uint8_t*, size_t, const std::string
&)> fun
) {
318 SectionRef text_section
= sections
[".text"];
319 uint64_t base_address
;
320 text_section
.getAddress(base_address
);
321 uint64_t current_address
= start
- base_address
;
324 text_section
.getContents(bytes
);
325 StringRefMemoryObject
ref(bytes
);
327 while (current_address
< end
- base_address
) {
331 llvm::raw_string_ostream
s(buf
);
333 if(llvm::MCDisassembler::Success
==
334 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
336 uint8_t bytes
[inst_size
+2];
337 ref
.readBytes(current_address
, inst_size
, bytes
);
339 IP
->printInst(&inst
, s
, "");
340 fun(bytes
, inst_size
, s
.str());
342 fun(NULL
, 0, "Invalid Byte");
346 current_address
+= inst_size
;