]>
git.siccegge.de Git - frida/frida.git/blob - src/disassembler/llvm/LLVMDisassembler.cxx
1 #include "disassembler/llvm/LLVMDisassembler.hxx"
2 #include "disassembler/llvm/LLVMBasicBlock.hxx"
3 #include "disassembler/llvm/LLVMFunction.hxx"
9 using namespace llvm::object
;
10 using std::error_code
;
15 Disassembler
* createLLVMDisassembler(const std::string
& filename
, InformationManager
* manager
) {
16 std::unique_ptr
<Binary
> o
;
17 o
.reset(createBinary(filename
).get());
18 const Binary
* op
= o
.get();
20 // ELFType<endian, maxalign, 64bit>
21 if (const ELF32LEObjectFile
* _
= dyn_cast
<ELF32LEObjectFile
>(op
)) {
23 return new LLVMDisassembler
<ELFType
<support::little
, 2, false>>(filename
, manager
);
25 if (const ELF64LEObjectFile
* _
= dyn_cast
<ELF64LEObjectFile
>(op
)) {
27 return new LLVMDisassembler
<ELFType
<support::little
, 2, true>>(filename
, manager
);
29 if (const ELF32BEObjectFile
* _
= dyn_cast
<ELF32BEObjectFile
>(op
)) {
31 return new LLVMDisassembler
<ELFType
<support::big
, 2, false>>(filename
, manager
);
33 if (const ELF64BEObjectFile
* _
= dyn_cast
<ELF64BEObjectFile
>(op
)) {
35 return new LLVMDisassembler
<ELFType
<support::big
, 2, true>>(filename
, manager
);
42 * TODO: fallback code falls die Datei kein ELF/PE/COFF/MacO/.. binary
43 * ist sondern z.B. einfach nur Instruktionen oder ein Bootsektor oder
46 template <typename ELFT
>
47 LLVMDisassembler
<ELFT
>::LLVMDisassembler(const std::string
& filename
,
48 InformationManager
* manager
)
49 : Disassembler(filename
, manager
)
50 , logger(log4cxx::Logger::getLogger("LLVMDisassembler"))
51 , triple("unknown-unknown-unknown")
54 LOG4CXX_DEBUG(logger
, "Handling file" << filename
);
55 auto result
= createBinary(filename
);
58 if ((ec
= result
.getError())) {
59 LOG4CXX_ERROR(logger
, "Failed to load Binary" << ec
.message());
64 binary
.reset(result
.get());
66 o
= dyn_cast
<ObjectFile
>(binary
.get());
68 triple
.setArch(Triple::ArchType(o
->getArch()));
69 std::string
tripleName(triple
.getTriple());
71 LOG4CXX_INFO(logger
, "Architecture " << tripleName
);
75 target
= TargetRegistry::lookupTarget("", triple
, es
);
77 LOG4CXX_ERROR(logger
, es
);
81 LOG4CXX_INFO(logger
, "Target " << target
->getName());
83 MRI
.reset(target
->createMCRegInfo(tripleName
));
85 LOG4CXX_ERROR(logger
, "no register info for target " << tripleName
);
89 // Set up disassembler.
90 AsmInfo
.reset(target
->createMCAsmInfo(*MRI
, tripleName
));
92 LOG4CXX_ERROR(logger
, "no assembly info for target " << tripleName
);
96 STI
.reset(target
->createMCSubtargetInfo(tripleName
, "", ""));
98 LOG4CXX_ERROR(logger
, "no subtarget info for target " << tripleName
);
102 MII
.reset(target
->createMCInstrInfo());
104 LOG4CXX_ERROR(logger
, "no instruction info for target " << tripleName
);
108 MOFI
.reset(new MCObjectFileInfo
);
109 MCContext
Ctx(AsmInfo
.get(), MRI
.get(), MOFI
.get());
111 DisAsm
.reset(target
->createMCDisassembler(*STI
, Ctx
));
113 LOG4CXX_ERROR(logger
, "no disassembler for target " << tripleName
);
117 target
->createMCRelocationInfo(tripleName
, Ctx
));
120 MCObjectSymbolizer::createObjectSymbolizer(Ctx
, std::move(RelInfo
), o
));
122 DisAsm
->setSymbolizer(std::move(Symzer
));
127 MIA
.reset(target
->createMCInstrAnalysis(MII
.get()));
129 LOG4CXX_ERROR(logger
, "no instruction analysis for target " << tripleName
);
133 int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
134 IP
.reset(target
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
, *MII
, *MRI
, *STI
));
136 LOG4CXX_ERROR(logger
, "no instruction printer for target " << tripleName
);
140 IP
->setPrintImmHex(llvm::HexStyle::C
);
141 IP
->setPrintImmHex(true);
143 std::unique_ptr
<MCObjectDisassembler
> OD(
144 new MCObjectDisassembler(*o
, *DisAsm
, *MIA
));
145 Mod
.reset(OD
->buildModule(false));
148 template <typename ELFT
>
149 void LLVMDisassembler
<ELFT
>::start() {
155 template <typename ELFT
>
156 LLVMDisassembler
<ELFT
>::~LLVMDisassembler() {
157 std::for_each(functions
.begin(), functions
.end(),
158 [](std::pair
<uint64_t,LLVMFunction
*> it
) {
161 std::for_each(blocks
.begin(), blocks
.end(),
162 [](std::pair
<uint64_t, LLVMBasicBlock
*> it
) {
167 template <typename ELFT
>
168 Function
* LLVMDisassembler
<ELFT
>::disassembleFunctionAt(uint64_t address
, const std::string
& name
) {
169 SectionRef text_section
= sections
[".text"];
170 uint64_t base_address
, size
;
171 text_section
.getAddress(base_address
);
172 text_section
.getSize(size
);
174 if (address
< base_address
||
175 address
>= base_address
+ size
) {
179 if (functions
.find(address
) != functions
.end()) {
180 return functions
[address
];
183 LLVMFunction
* function
;
186 s
<< "<Unnamed 0x" << std::hex
<< address
<< ">";
187 function
= new LLVMFunction(s
.str(), address
);
189 function
= new LLVMFunction(name
, address
);
191 functions
.insert(std::make_pair(address
, function
));
193 disassembleFunction(function
);
198 template <typename ELFT
>
199 void LLVMDisassembler
<ELFT
>::disassembleFunction(LLVMFunction
* function
) {
200 std::stack
<LLVMBasicBlock
*> remaining_blocks
;
201 SectionRef text_section
= sections
[".text"];
203 text_section
.getContents(bytes
);
204 StringRefMemoryObject
ref(bytes
);
206 LOG4CXX_DEBUG(logger
, "Handling function " << function
->getName());
208 LLVMBasicBlock
* block
= new LLVMBasicBlock(function
->getStartAddress(), this);
209 remaining_blocks
.push(block
);
210 blocks
.insert(std::make_pair(block
->getStartAddress(), block
));
211 function
->addBasicBlock(block
);
213 while (remaining_blocks
.size()) {
214 LLVMBasicBlock
* current_block
= remaining_blocks
.top();
215 remaining_blocks
.pop();
217 LOG4CXX_DEBUG(logger
, "Handling Block starting at " << std::hex
<< current_block
->getStartAddress());
220 uint64_t base_address
;
221 text_section
.getAddress(base_address
);
222 uint64_t current_address
= current_block
->getStartAddress() - base_address
;
226 llvm::raw_string_ostream
s(buf
);
228 if(llvm::MCDisassembler::Success
==
229 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
232 if (MIA
->evaluateBranch(inst
, current_address
, inst_size
, jmptarget
)) {
233 jmptarget
+= base_address
;
234 if (!MIA
->isIndirectBranch(inst
)) {
235 if (MIA
->isCall(inst
)) {
236 if (functions
.find(jmptarget
) == functions
.end()) {
237 disassembleFunctionAt(jmptarget
);
240 current_block
->setNextBlock(0, jmptarget
);
241 if (blocks
.find(jmptarget
) == blocks
.end()) {
242 LLVMBasicBlock
* block
= new LLVMBasicBlock(jmptarget
, this);
243 blocks
.insert(std::make_pair(block
->getStartAddress(), block
));
244 function
->addBasicBlock(block
);
245 remaining_blocks
.push(block
);
247 LOG4CXX_DEBUG(logger
, "Reusing Block starting at " << std::hex
<< current_block
->getStartAddress());
248 function
->addBasicBlock(blocks
.find(jmptarget
)->second
);
250 if (MIA
->isConditionalBranch(inst
)) {
251 jmptarget
= base_address
+ current_address
+ inst_size
;
252 current_block
->setNextBlock(1, jmptarget
);
253 if (blocks
.find(jmptarget
) == blocks
.end()) {
254 LLVMBasicBlock
* block
= new LLVMBasicBlock(jmptarget
, this);
255 blocks
.insert(std::make_pair(block
->getStartAddress(), block
));
256 function
->addBasicBlock(block
);
257 remaining_blocks
.push(block
);
259 LOG4CXX_DEBUG(logger
, "Reusing Block starting at " << std::hex
<< current_block
->getStartAddress());
260 function
->addBasicBlock(blocks
.find(jmptarget
)->second
);
271 if (inst_size
== 0 || MIA
->isTerminator(inst
) || MIA
->isBranch(inst
)) {
272 current_block
->setEndAddress(current_address
+ base_address
+ inst_size
);
273 LOG4CXX_DEBUG(logger
, "Finished Block at " << std::hex
<<
274 current_block
->getEndAddress());
277 current_address
+= inst_size
;
280 splitBlocks(function
);
281 LOG4CXX_DEBUG(logger
, "Finished function " << function
->getName());
282 manager
->signal_new_function(function
);
285 template <typename ELFT
>
286 void LLVMDisassembler
<ELFT
>::disassemble() {
287 SectionRef text_section
= sections
[".text"];
288 std::vector
<LLVMFunction
*> remaining_functions
;
290 // Assume all function symbols actually start a real function
291 for (auto x
= symbols
.begin(); x
!= symbols
.end(); ++x
) {
294 SymbolRef::Type symbol_type
;
297 if (text_section
.containsSymbol(x
->second
, contains
) || !contains
)
300 if (x
->second
.getType(symbol_type
)
301 || SymbolRef::ST_Function
!= symbol_type
)
304 if (!x
->second
.getAddress(result
)) {
305 LLVMFunction
* fun
= new LLVMFunction(x
->first
, result
);
306 remaining_functions
.push_back(fun
);
307 functions
.insert(std::make_pair(result
, fun
));
308 LOG4CXX_DEBUG(logger
, "Disasembling " << x
->first
);
312 for (LLVMFunction
* function
: remaining_functions
) {
313 disassembleFunction(function
);
316 if (binary
->isELF()) {
317 bool is64bit
= (binary
->getData()[4] == 0x02);
319 for (int i(0); i
< (is64bit
? 8 : 4); ++i
) {
320 if (binary
->isLittleEndian()) {
321 _entryAddress
|= (unsigned int)((unsigned char)binary
->getData()[0x18 + i
]) << 8*i
;
323 _entryAddress
= _entryAddress
<< 8;
324 _entryAddress
|= (unsigned char)binary
->getData()[0x18 + i
];
327 LOG4CXX_DEBUG(logger
, "Adding entryAddress at: " << std::hex
<< _entryAddress
);
329 s
<< "<_start 0x" << std::hex
<< _entryAddress
<< ">";
331 disassembleFunctionAt(_entryAddress
, s
.str());
334 if (functions
.empty()) {
336 text_section
.getAddress(text_entry
);
337 LOG4CXX_INFO(logger
, "No Symbols found, starting at the beginning of the text segment");
338 disassembleFunctionAt(text_entry
);
342 template <typename ELFT
>
343 void LLVMDisassembler
<ELFT
>::splitBlocks(LLVMFunction
* function
) {
344 SectionRef text_section
= sections
[".text"];
346 text_section
.getContents(bytes
);
347 StringRefMemoryObject
ref(bytes
);
349 // Split blocks where jumps are going inside the block
350 for (auto it
= function
->blocks().begin();
351 it
!= function
->blocks().end();
353 BasicBlock
* current_block
= it
->second
;
355 uint64_t base_address
;
356 text_section
.getAddress(base_address
);
357 uint64_t current_address
= current_block
->getStartAddress() - base_address
;
358 while(current_block
->getEndAddress() - base_address
> current_address
) {
361 llvm::raw_string_ostream
s(buf
);
363 if(llvm::MCDisassembler::Success
==
364 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
365 // See if some other block starts here
366 auto other
= blocks
.find(current_address
+ inst_size
+ base_address
);
368 // Special case, other block starts here but we are at the end anyway
369 if (other
!= blocks
.end()) {
370 uint64_t endaddress
= current_address
+ inst_size
+ base_address
;
371 if (endaddress
!= current_block
->getEndAddress()) {
372 LOG4CXX_DEBUG(logger
, "Shortening block starting at "
374 << current_block
->getStartAddress()
377 function
->addBasicBlock(other
->second
);
378 current_block
->setEndAddress(endaddress
);
379 current_block
->setNextBlock(0, other
->first
);
380 current_block
->setNextBlock(1, 0);
386 current_address
+= inst_size
;
391 template <typename ELFT
>
392 void LLVMDisassembler
<ELFT
>::readSymbols() {
394 symbol_iterator
si(o
->symbol_begin()), se(o
->symbol_end());
395 for (; si
!= se
; ++si
) {
397 if ((ec
= si
->getName(name
))) {
398 LOG4CXX_ERROR(logger
, ec
.message());
401 LOG4CXX_DEBUG(logger
, "Added symbol " << name
.str());
402 symbols
.insert(make_pair(name
.str(), *si
));
406 template <typename ELFT
>
407 void LLVMDisassembler
<ELFT
>::readSections() {
409 section_iterator
i(o
->section_begin()), e(o
->section_end());
410 for (; i
!= e
; ++i
) {
412 if ((ec
= i
->getName(name
))) {
413 LOG4CXX_ERROR(logger
, ec
.message());
416 LOG4CXX_DEBUG(logger
, "Added section " << name
.str());
417 sections
.insert(make_pair(name
.str(), *i
));
422 template <typename ELFT
>
423 void LLVMDisassembler
<ELFT
>::forEachFunction(std::function
<void (uint64_t, Function
*)> callback
) {
424 std::for_each(functions
.begin(), functions
.end(),
425 [&](std::pair
<uint64_t, LLVMFunction
*> x
) {
426 callback(x
.first
, x
.second
);
430 template <typename ELFT
>
431 void LLVMDisassembler
<ELFT
>::printEachInstruction(uint64_t start
, uint64_t end
,
432 std::function
<void (uint8_t*, size_t,
433 const std::string
&)> fun
) {
434 SectionRef text_section
= sections
[".text"];
435 uint64_t base_address
;
436 text_section
.getAddress(base_address
);
437 uint64_t current_address
= start
- base_address
;
440 text_section
.getContents(bytes
);
441 StringRefMemoryObject
ref(bytes
);
443 while (current_address
< end
- base_address
) {
447 llvm::raw_string_ostream
s(buf
);
449 if(llvm::MCDisassembler::Success
==
450 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
452 uint8_t bytes
[inst_size
+2];
453 ref
.readBytes(current_address
, inst_size
, bytes
);
456 if (MIA
->evaluateBranch(inst
, current_address
, inst_size
, jmptarget
)) {
457 std::stringstream stream
;
458 stream
<< std::hex
<< (base_address
+ jmptarget
);
459 IP
->printInst(&inst
, s
, stream
.str());
461 IP
->printInst(&inst
, s
, "");
463 fun(bytes
, inst_size
, s
.str());
465 LOG4CXX_WARN(logger
, "Invalid byte at" << std::hex
<< current_address
+ base_address
);
466 fun(NULL
, 0, "Invalid Byte");
470 current_address
+= inst_size
;