]>
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);
320 for (int i(0); i
< (is64bit
? 8 : 4); ++i
) {
321 if (binary
->isLittleEndian()) {
322 entry
|= (unsigned int)((unsigned char)binary
->getData()[0x18 + i
]) << 8*i
;
325 entry
|= (unsigned char)binary
->getData()[0x18 + i
];
328 LOG4CXX_DEBUG(logger
, "Adding entry at: " << std::hex
<< entry
);
330 s
<< "<_start 0x" << std::hex
<< entry
<< ">";
332 disassembleFunctionAt(entry
, s
.str());
335 if (functions
.empty()) {
337 text_section
.getAddress(text_entry
);
338 LOG4CXX_INFO(logger
, "No Symbols found, starting at the beginning of the text segment");
339 disassembleFunctionAt(text_entry
);
343 template <typename ELFT
>
344 void LLVMDisassembler
<ELFT
>::splitBlocks(LLVMFunction
* function
) {
345 SectionRef text_section
= sections
[".text"];
347 text_section
.getContents(bytes
);
348 StringRefMemoryObject
ref(bytes
);
350 // Split blocks where jumps are going inside the block
351 for (auto it
= function
->blocks().begin();
352 it
!= function
->blocks().end();
354 BasicBlock
* current_block
= it
->second
;
356 uint64_t base_address
;
357 text_section
.getAddress(base_address
);
358 uint64_t current_address
= current_block
->getStartAddress() - base_address
;
359 while(current_block
->getEndAddress() - base_address
> current_address
) {
362 llvm::raw_string_ostream
s(buf
);
364 if(llvm::MCDisassembler::Success
==
365 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
366 // See if some other block starts here
367 auto other
= blocks
.find(current_address
+ inst_size
+ base_address
);
369 // Special case, other block starts here but we are at the end anyway
370 if (other
!= blocks
.end()) {
371 uint64_t endaddress
= current_address
+ inst_size
+ base_address
;
372 if (endaddress
!= current_block
->getEndAddress()) {
373 LOG4CXX_DEBUG(logger
, "Shortening block starting at "
375 << current_block
->getStartAddress()
378 function
->addBasicBlock(other
->second
);
379 current_block
->setEndAddress(endaddress
);
380 current_block
->setNextBlock(0, other
->first
);
381 current_block
->setNextBlock(1, 0);
387 current_address
+= inst_size
;
392 template <typename ELFT
>
393 void LLVMDisassembler
<ELFT
>::readSymbols() {
395 symbol_iterator
si(o
->symbol_begin()), se(o
->symbol_end());
396 for (; si
!= se
; ++si
) {
398 if ((ec
= si
->getName(name
))) {
399 LOG4CXX_ERROR(logger
, ec
.message());
402 LOG4CXX_DEBUG(logger
, "Added symbol " << name
.str());
403 symbols
.insert(make_pair(name
.str(), *si
));
407 template <typename ELFT
>
408 void LLVMDisassembler
<ELFT
>::readSections() {
410 section_iterator
i(o
->section_begin()), e(o
->section_end());
411 for (; i
!= e
; ++i
) {
413 if ((ec
= i
->getName(name
))) {
414 LOG4CXX_ERROR(logger
, ec
.message());
417 LOG4CXX_DEBUG(logger
, "Added section " << name
.str());
418 sections
.insert(make_pair(name
.str(), *i
));
423 template <typename ELFT
>
424 void LLVMDisassembler
<ELFT
>::forEachFunction(std::function
<void (uint64_t, Function
*)> callback
) {
425 std::for_each(functions
.begin(), functions
.end(),
426 [&](std::pair
<uint64_t, LLVMFunction
*> x
) {
427 callback(x
.first
, x
.second
);
431 template <typename ELFT
>
432 void LLVMDisassembler
<ELFT
>::printEachInstruction(uint64_t start
, uint64_t end
,
433 std::function
<void (uint8_t*, size_t,
434 const std::string
&)> fun
) {
435 SectionRef text_section
= sections
[".text"];
436 uint64_t base_address
;
437 text_section
.getAddress(base_address
);
438 uint64_t current_address
= start
- base_address
;
441 text_section
.getContents(bytes
);
442 StringRefMemoryObject
ref(bytes
);
444 while (current_address
< end
- base_address
) {
448 llvm::raw_string_ostream
s(buf
);
450 if(llvm::MCDisassembler::Success
==
451 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
453 uint8_t bytes
[inst_size
+2];
454 ref
.readBytes(current_address
, inst_size
, bytes
);
457 if (MIA
->evaluateBranch(inst
, current_address
, inst_size
, jmptarget
)) {
458 std::stringstream stream
;
459 stream
<< std::hex
<< (base_address
+ jmptarget
);
460 IP
->printInst(&inst
, s
, stream
.str());
462 IP
->printInst(&inst
, s
, "");
464 fun(bytes
, inst_size
, s
.str());
466 LOG4CXX_WARN(logger
, "Invalid byte at" << std::hex
<< current_address
+ base_address
);
467 fun(NULL
, 0, "Invalid Byte");
471 current_address
+= inst_size
;