]>
git.siccegge.de Git - frida/frida.git/blob - src/disassembler/llvm/LLVMDisassembler.cxx
c44c60c83e779f5114556c8562f10f838877b634
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
;
13 * TODO: fallback code falls die Datei kein ELF/PE/COFF/MacO/.. binary
14 * ist sondern z.B. einfach nur Instruktionen oder ein Bootsektor oder
17 LLVMDisassembler::LLVMDisassembler(const std::string
& filename
,
18 InformationManager
* manager
)
19 : Disassembler(filename
, manager
)
20 , logger(log4cxx::Logger::getLogger("LLVMDisassembler"))
21 , triple("unknown-unknown-unknown")
24 LOG4CXX_DEBUG(logger
, "Handling file" << filename
);
25 auto result
= createBinary(filename
);
28 if ((ec
= result
.getError())) {
29 LOG4CXX_ERROR(logger
, "Failed to load Binary" << ec
.message());
34 binary
.reset(result
.get());
36 o
= dyn_cast
<ObjectFile
>(binary
.get());
38 triple
.setArch(Triple::ArchType(o
->getArch()));
39 std::string
tripleName(triple
.getTriple());
41 LOG4CXX_INFO(logger
, "Architecture " << tripleName
);
45 target
= TargetRegistry::lookupTarget("", triple
, es
);
47 LOG4CXX_ERROR(logger
, es
);
51 LOG4CXX_INFO(logger
, "Target " << target
->getName());
53 MRI
.reset(target
->createMCRegInfo(tripleName
));
55 LOG4CXX_ERROR(logger
, "no register info for target " << tripleName
);
59 // Set up disassembler.
60 AsmInfo
.reset(target
->createMCAsmInfo(*MRI
, tripleName
));
62 LOG4CXX_ERROR(logger
, "no assembly info for target " << tripleName
);
66 STI
.reset(target
->createMCSubtargetInfo(tripleName
, "", ""));
68 LOG4CXX_ERROR(logger
, "no subtarget info for target " << tripleName
);
72 MII
.reset(target
->createMCInstrInfo());
74 LOG4CXX_ERROR(logger
, "no instruction info for target " << tripleName
);
78 MOFI
.reset(new MCObjectFileInfo
);
79 MCContext
Ctx(AsmInfo
.get(), MRI
.get(), MOFI
.get());
81 DisAsm
.reset(target
->createMCDisassembler(*STI
, Ctx
));
83 LOG4CXX_ERROR(logger
, "no disassembler for target " << tripleName
);
87 target
->createMCRelocationInfo(tripleName
, Ctx
));
90 MCObjectSymbolizer::createObjectSymbolizer(Ctx
, std::move(RelInfo
), o
));
92 DisAsm
->setSymbolizer(std::move(Symzer
));
97 MIA
.reset(target
->createMCInstrAnalysis(MII
.get()));
99 LOG4CXX_ERROR(logger
, "no instruction analysis for target " << tripleName
);
103 int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
104 IP
.reset(target
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
, *MII
, *MRI
, *STI
));
106 LOG4CXX_ERROR(logger
, "no instruction printer for target " << tripleName
);
110 IP
->setPrintImmHex(llvm::HexStyle::C
);
111 IP
->setPrintImmHex(true);
113 std::unique_ptr
<MCObjectDisassembler
> OD(
114 new MCObjectDisassembler(*o
, *DisAsm
, *MIA
));
115 Mod
.reset(OD
->buildModule(false));
118 void LLVMDisassembler::start() {
124 LLVMDisassembler::~LLVMDisassembler() {
125 std::for_each(functions
.begin(), functions
.end(),
126 [](std::pair
<uint64_t,LLVMFunction
*> it
) {
129 std::for_each(blocks
.begin(), blocks
.end(),
130 [](std::pair
<uint64_t, LLVMBasicBlock
*> it
) {
135 Function
* LLVMDisassembler::disassembleFunctionAt(uint64_t address
, const std::string
& name
) {
136 SectionRef text_section
= sections
[".text"];
137 uint64_t base_address
, size
;
138 text_section
.getAddress(base_address
);
139 text_section
.getSize(size
);
141 if (address
< base_address
||
142 address
>= base_address
+ size
) {
146 if (functions
.find(address
) != functions
.end()) {
147 return functions
[address
];
150 LLVMFunction
* function
;
153 s
<< "<Unnamed 0x" << std::hex
<< address
<< ">";
154 function
= new LLVMFunction(s
.str(), address
);
156 function
= new LLVMFunction(name
, address
);
158 functions
.insert(std::make_pair(address
, function
));
160 disassembleFunction(function
);
165 void LLVMDisassembler::disassembleFunction(LLVMFunction
* function
) {
166 std::stack
<LLVMBasicBlock
*> remaining_blocks
;
167 SectionRef text_section
= sections
[".text"];
169 text_section
.getContents(bytes
);
170 StringRefMemoryObject
ref(bytes
);
172 LOG4CXX_DEBUG(logger
, "Handling function " << function
->getName());
174 LLVMBasicBlock
* block
= new LLVMBasicBlock(function
->getStartAddress(), this);
175 remaining_blocks
.push(block
);
176 blocks
.insert(std::make_pair(block
->getStartAddress(), block
));
177 function
->addBasicBlock(block
);
179 while (remaining_blocks
.size()) {
180 LLVMBasicBlock
* current_block
= remaining_blocks
.top();
181 remaining_blocks
.pop();
183 LOG4CXX_DEBUG(logger
, "Handling Block starting at " << std::hex
<< current_block
->getStartAddress());
186 uint64_t base_address
;
187 text_section
.getAddress(base_address
);
188 uint64_t current_address
= current_block
->getStartAddress() - base_address
;
192 llvm::raw_string_ostream
s(buf
);
194 if(llvm::MCDisassembler::Success
==
195 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
198 if (MIA
->evaluateBranch(inst
, current_address
, inst_size
, jmptarget
)) {
199 jmptarget
+= base_address
;
200 if (!MIA
->isIndirectBranch(inst
)) {
201 if (MIA
->isCall(inst
)) {
202 if (functions
.find(jmptarget
) == functions
.end()) {
203 disassembleFunctionAt(jmptarget
);
206 current_block
->setNextBlock(0, jmptarget
);
207 if (blocks
.find(jmptarget
) == blocks
.end()) {
208 LLVMBasicBlock
* block
= new LLVMBasicBlock(jmptarget
, this);
209 blocks
.insert(std::make_pair(block
->getStartAddress(), block
));
210 function
->addBasicBlock(block
);
211 remaining_blocks
.push(block
);
213 if (MIA
->isConditionalBranch(inst
)) {
214 jmptarget
= base_address
+ current_address
+ inst_size
;
215 current_block
->setNextBlock(1, jmptarget
);
216 if (blocks
.find(jmptarget
) == blocks
.end()) {
217 LLVMBasicBlock
* block
= new LLVMBasicBlock(jmptarget
, this);
218 blocks
.insert(std::make_pair(block
->getStartAddress(), block
));
219 function
->addBasicBlock(block
);
220 remaining_blocks
.push(block
);
231 if (inst_size
== 0 || MIA
->isTerminator(inst
) || MIA
->isBranch(inst
)) {
232 current_block
->setEndAddress(current_address
+ base_address
+ inst_size
);
233 LOG4CXX_DEBUG(logger
, "Finished Block at " << std::hex
<<
234 current_block
->getEndAddress());
237 current_address
+= inst_size
;
240 splitBlocks(function
);
241 LOG4CXX_DEBUG(logger
, "Finished function " << function
->getName());
242 manager
->signal_new_function(function
);
245 void LLVMDisassembler::disassemble() {
246 SectionRef text_section
= sections
[".text"];
247 std::vector
<LLVMFunction
*> remaining_functions
;
249 // Assume all function symbols actually start a real function
250 for (auto x
= symbols
.begin(); x
!= symbols
.end(); ++x
) {
253 SymbolRef::Type symbol_type
;
256 if (text_section
.containsSymbol(x
->second
, contains
) || !contains
)
259 if (x
->second
.getType(symbol_type
)
260 || SymbolRef::ST_Function
!= symbol_type
)
263 if (!x
->second
.getAddress(result
)) {
264 LLVMFunction
* fun
= new LLVMFunction(x
->first
, result
);
265 remaining_functions
.push_back(fun
);
266 functions
.insert(std::make_pair(result
, fun
));
267 LOG4CXX_DEBUG(logger
, "Disasembling " << x
->first
);
271 for (LLVMFunction
* function
: remaining_functions
) {
272 disassembleFunction(function
);
275 if (binary
->isELF()) {
276 bool is64bit
= (binary
->getData()[4] == 0x02);
279 for (int i(0); i
< (is64bit
? 8 : 4); ++i
) {
280 if (binary
->isLittleEndian()) {
281 entry
|= (unsigned int)((unsigned char)binary
->getData()[0x18 + i
]) << 8*i
;
284 entry
|= (unsigned char)binary
->getData()[0x18 + i
];
287 LOG4CXX_DEBUG(logger
, "Adding entry at: " << std::hex
<< entry
);
289 s
<< "<_start 0x" << std::hex
<< entry
<< ">";
291 disassembleFunctionAt(entry
, s
.str());
294 if (functions
.empty()) {
296 text_section
.getAddress(text_entry
);
297 LOG4CXX_INFO(logger
, "No Symbols found, starting at the beginning of the text segment");
298 disassembleFunctionAt(text_entry
);
302 void LLVMDisassembler::splitBlocks(LLVMFunction
* function
) {
303 SectionRef text_section
= sections
[".text"];
305 text_section
.getContents(bytes
);
306 StringRefMemoryObject
ref(bytes
);
308 // Split blocks where jumps are going inside the block
309 for (auto it
= function
->blocks().begin();
310 it
!= function
->blocks().end();
312 BasicBlock
* current_block
= it
->second
;
314 uint64_t base_address
;
315 text_section
.getAddress(base_address
);
316 uint64_t current_address
= current_block
->getStartAddress() - base_address
;
317 while(current_block
->getEndAddress() - base_address
> current_address
) {
320 llvm::raw_string_ostream
s(buf
);
322 if(llvm::MCDisassembler::Success
==
323 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
324 // See if some other block starts here
325 auto other
= blocks
.find(current_address
+ inst_size
+ base_address
);
327 // Special case, other block starts here but we are at the end anyway
328 if (other
!= blocks
.end()) {
329 uint64_t endaddress
= current_address
+ inst_size
+ base_address
;
330 if (endaddress
!= current_block
->getEndAddress()) {
331 LOG4CXX_DEBUG(logger
, "Shortening block starting at "
333 << current_block
->getStartAddress()
336 function
->addBasicBlock(other
->second
);
337 current_block
->setEndAddress(endaddress
);
338 current_block
->setNextBlock(0, other
->first
);
339 current_block
->setNextBlock(1, 0);
345 current_address
+= inst_size
;
350 void LLVMDisassembler::readSymbols() {
352 symbol_iterator
si(o
->symbol_begin()), se(o
->symbol_end());
353 for (; si
!= se
; ++si
) {
355 if ((ec
= si
->getName(name
))) {
356 LOG4CXX_ERROR(logger
, ec
.message());
359 LOG4CXX_DEBUG(logger
, "Added symbol " << name
.str());
360 symbols
.insert(make_pair(name
.str(), *si
));
364 void LLVMDisassembler::readSections() {
366 section_iterator
i(o
->section_begin()), e(o
->section_end());
367 for (; i
!= e
; ++i
) {
369 if ((ec
= i
->getName(name
))) {
370 LOG4CXX_ERROR(logger
, ec
.message());
373 LOG4CXX_DEBUG(logger
, "Added section " << name
.str());
374 sections
.insert(make_pair(name
.str(), *i
));
379 void LLVMDisassembler::forEachFunction(std::function
<void (uint64_t, Function
*)> callback
) {
380 std::for_each(functions
.begin(), functions
.end(),
381 [&](std::pair
<uint64_t, LLVMFunction
*> x
) {
382 callback(x
.first
, x
.second
);
386 void LLVMDisassembler::printEachInstruction(uint64_t start
, uint64_t end
,
387 std::function
<void (uint8_t*, size_t,
388 const std::string
&)> fun
) {
389 SectionRef text_section
= sections
[".text"];
390 uint64_t base_address
;
391 text_section
.getAddress(base_address
);
392 uint64_t current_address
= start
- base_address
;
395 text_section
.getContents(bytes
);
396 StringRefMemoryObject
ref(bytes
);
398 while (current_address
< end
- base_address
) {
402 llvm::raw_string_ostream
s(buf
);
404 if(llvm::MCDisassembler::Success
==
405 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
407 uint8_t bytes
[inst_size
+2];
408 ref
.readBytes(current_address
, inst_size
, bytes
);
411 if (MIA
->evaluateBranch(inst
, current_address
, inst_size
, jmptarget
)) {
412 std::stringstream stream
;
413 stream
<< std::hex
<< (base_address
+ jmptarget
);
414 IP
->printInst(&inst
, s
, stream
.str());
416 IP
->printInst(&inst
, s
, "");
418 fun(bytes
, inst_size
, s
.str());
420 LOG4CXX_WARN(logger
, "Invalid byte at" << std::hex
<< current_address
+ base_address
);
421 fun(NULL
, 0, "Invalid Byte");
425 current_address
+= inst_size
;