]>
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
;
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 : Disassembler(filename
)
19 , logger(log4cxx::Logger::getLogger("LLVMDisassembler"))
20 , triple("unknown-unknown-unknown")
22 LOG4CXX_DEBUG(logger
, "Handling file" << filename
);
23 auto result
= createBinary(filename
);
26 if ((ec
= result
.getError())) {
27 LOG4CXX_ERROR(logger
, "Failed to load Binary" << ec
.message());
32 binary
.reset(result
.get());
34 o
= dyn_cast
<ObjectFile
>(binary
.get());
36 triple
.setArch(Triple::ArchType(o
->getArch()));
37 std::string
tripleName(triple
.getTriple());
39 LOG4CXX_INFO(logger
, "Architecture " << tripleName
);
43 target
= TargetRegistry::lookupTarget("", triple
, es
);
45 LOG4CXX_ERROR(logger
, es
);
49 LOG4CXX_INFO(logger
, "Target " << target
->getName());
51 MRI
.reset(target
->createMCRegInfo(tripleName
));
53 LOG4CXX_ERROR(logger
, "no register info for target " << tripleName
);
57 // Set up disassembler.
58 AsmInfo
.reset(target
->createMCAsmInfo(*MRI
, tripleName
));
60 LOG4CXX_ERROR(logger
, "no assembly info for target " << tripleName
);
64 STI
.reset(target
->createMCSubtargetInfo(tripleName
, "", ""));
66 LOG4CXX_ERROR(logger
, "no subtarget info for target " << tripleName
);
70 MII
.reset(target
->createMCInstrInfo());
72 LOG4CXX_ERROR(logger
, "no instruction info for target " << tripleName
);
76 MOFI
.reset(new MCObjectFileInfo
);
77 MCContext
Ctx(AsmInfo
.get(), MRI
.get(), MOFI
.get());
79 DisAsm
.reset(target
->createMCDisassembler(*STI
, Ctx
));
81 LOG4CXX_ERROR(logger
, "no disassembler for target " << tripleName
);
85 target
->createMCRelocationInfo(tripleName
, Ctx
));
88 MCObjectSymbolizer::createObjectSymbolizer(Ctx
, std::move(RelInfo
), o
));
90 DisAsm
->setSymbolizer(std::move(Symzer
));
95 MIA
.reset(target
->createMCInstrAnalysis(MII
.get()));
97 LOG4CXX_ERROR(logger
, "no instruction analysis for target " << tripleName
);
101 int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
102 IP
.reset(target
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
, *MII
, *MRI
, *STI
));
104 LOG4CXX_ERROR(logger
, "no instruction printer for target " << tripleName
);
108 IP
->setPrintImmHex(llvm::HexStyle::C
);
109 IP
->setPrintImmHex(true);
111 std::unique_ptr
<MCObjectDisassembler
> OD(
112 new MCObjectDisassembler(*o
, *DisAsm
, *MIA
));
113 Mod
.reset(OD
->buildModule(false));
120 LLVMDisassembler::~LLVMDisassembler() {
121 std::for_each(functions
.begin(), functions
.end(),
122 [](std::pair
<uint64_t,LLVMFunction
*> it
) {
125 std::for_each(blocks
.begin(), blocks
.end(),
126 [](std::pair
<uint64_t, LLVMBasicBlock
*> it
) {
131 void LLVMDisassembler::disassemble() {
132 std::stack
<LLVMFunction
*> remaining_functions
;
133 std::stack
<LLVMBasicBlock
*> remaining_blocks
;
134 SectionRef text_section
= sections
[".text"];
136 // Assume all function symbols actually start a real function
137 for (auto x
= symbols
.begin(); x
!= symbols
.end(); ++x
) {
140 SymbolRef::Type symbol_type
;
143 if (text_section
.containsSymbol(x
->second
, contains
) || !contains
)
146 if (x
->second
.getType(symbol_type
)
147 || SymbolRef::ST_Function
!= symbol_type
)
150 if (!x
->second
.getAddress(result
)) {
151 LLVMFunction
* fun
= new LLVMFunction(x
->first
, result
);
152 remaining_functions
.push(fun
);
153 functions
.insert(std::make_pair(result
, fun
));
154 LOG4CXX_DEBUG(logger
, "Disasembling " << x
->first
);
158 if (binary
->isELF()) {
159 bool is64bit
= (binary
->getData()[4] == 0x02);
162 for (int i(0); i
< (is64bit
? 8 : 4); ++i
) {
163 if (binary
->isLittleEndian()) {
164 entry
|= (unsigned int)((unsigned char)binary
->getData()[0x18 + i
]) << 8*i
;
167 entry
|= (unsigned char)binary
->getData()[0x18 + i
];
170 if (functions
.find(entry
) == functions
.end()) {
171 LOG4CXX_DEBUG(logger
, "Adding entry at: " << std::hex
<< entry
);
173 s
<< "<_start 0x" << std::hex
<< entry
<< ">";
174 LLVMFunction
* fun
= new LLVMFunction(s
.str(), entry
);
175 functions
.insert(std::make_pair(entry
, fun
));
176 remaining_functions
.push(fun
);
180 if (functions
.empty()) {
182 text_section
.getAddress(text_entry
);
183 LOG4CXX_INFO(logger
, "No Symbols found, starting at the beginning of the text segment");
186 s
<< "<Unnamed 0x" << std::hex
<< text_entry
<< ">";
187 LLVMFunction
* fun
= new LLVMFunction(s
.str(), text_entry
);
188 functions
.insert(std::make_pair(text_entry
, fun
));
189 remaining_functions
.push(fun
);
193 text_section
.getContents(bytes
);
194 StringRefMemoryObject
ref(bytes
);
196 while (remaining_functions
.size()) {
197 LLVMFunction
* current_function
= remaining_functions
.top();
198 remaining_functions
.pop();
200 LOG4CXX_DEBUG(logger
, "Handling function " << current_function
->getName());
202 LLVMBasicBlock
* block
= new LLVMBasicBlock(current_function
->getStartAddress(), this);
203 remaining_blocks
.push(block
);
204 blocks
.insert(std::make_pair(block
->getStartAddress(), block
));
206 while (remaining_blocks
.size()) {
207 LLVMBasicBlock
* current_block
= remaining_blocks
.top();
208 remaining_blocks
.pop();
210 LOG4CXX_DEBUG(logger
, "Handling Block starting at " << std::hex
<< current_block
->getStartAddress());
213 uint64_t base_address
;
214 text_section
.getAddress(base_address
);
215 uint64_t current_address
= current_block
->getStartAddress() - base_address
;
219 llvm::raw_string_ostream
s(buf
);
221 if(llvm::MCDisassembler::Success
==
222 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
225 if (MIA
->evaluateBranch(inst
, current_address
, inst_size
, jmptarget
)) {
226 jmptarget
+= base_address
;
227 if (!MIA
->isIndirectBranch(inst
)) {
228 if (MIA
->isCall(inst
)) {
229 if (functions
.find(jmptarget
) == functions
.end()) {
231 s
<< "<Unnamed 0x" << std::hex
<< jmptarget
<< ">";
232 LLVMFunction
* fun
= new LLVMFunction(s
.str(), jmptarget
);
233 functions
.insert(std::make_pair(jmptarget
, fun
));
234 remaining_functions
.push(fun
);
237 current_block
->setNextBlock(0, jmptarget
);
238 if (blocks
.find(jmptarget
) == blocks
.end()) {
239 LLVMBasicBlock
* block
= new LLVMBasicBlock(jmptarget
, this);
240 blocks
.insert(std::make_pair(block
->getStartAddress(), block
));
241 remaining_blocks
.push(block
);
243 if (MIA
->isConditionalBranch(inst
)) {
244 jmptarget
= base_address
+ current_address
+ inst_size
;
245 current_block
->setNextBlock(1, jmptarget
);
246 if (blocks
.find(jmptarget
) == blocks
.end()) {
247 LLVMBasicBlock
* block
= new LLVMBasicBlock(jmptarget
, this);
248 blocks
.insert(std::make_pair(block
->getStartAddress(), block
));
249 remaining_blocks
.push(block
);
260 if (inst_size
== 0 || MIA
->isTerminator(inst
) || MIA
->isBranch(inst
)) {
261 current_block
->setEndAddress(current_address
+ base_address
+ inst_size
);
262 LOG4CXX_DEBUG(logger
, "Finished Block at " << std::hex
<<
263 current_block
->getEndAddress());
266 current_address
+= inst_size
;
269 LOG4CXX_DEBUG(logger
, "Finished function " << current_function
->getName());
272 // Split blocks where jumps are going inside the block
273 for (auto it
= blocks
.begin(); it
!= blocks
.end(); ++it
) {
274 LLVMBasicBlock
* current_block
= it
->second
;
276 uint64_t base_address
;
277 text_section
.getAddress(base_address
);
278 uint64_t current_address
= current_block
->getStartAddress() - base_address
;
279 while(current_block
->getEndAddress() - base_address
> current_address
) {
282 llvm::raw_string_ostream
s(buf
);
284 if(llvm::MCDisassembler::Success
==
285 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
286 auto other
= blocks
.find(current_address
+ inst_size
+ base_address
);
288 if (other
!= blocks
.end()) {
289 uint64_t endaddress
= current_address
+ inst_size
+ base_address
;
290 if (endaddress
!= current_block
->getEndAddress()) {
291 LOG4CXX_DEBUG(logger
, "Shortening block starting at "
293 << current_block
->getStartAddress()
296 current_block
->setEndAddress(endaddress
);
297 current_block
->setNextBlock(0, other
->first
);
298 current_block
->setNextBlock(1, 0);
304 current_address
+= inst_size
;
309 void LLVMDisassembler::readSymbols() {
311 symbol_iterator
si(o
->symbol_begin()), se(o
->symbol_end());
312 for (; si
!= se
; ++si
) {
314 if ((ec
= si
->getName(name
))) {
315 LOG4CXX_ERROR(logger
, ec
.message());
318 LOG4CXX_DEBUG(logger
, "Added symbol " << name
.str());
319 symbols
.insert(make_pair(name
.str(), *si
));
323 void LLVMDisassembler::readSections() {
325 section_iterator
i(o
->section_begin()), e(o
->section_end());
326 for (; i
!= e
; ++i
) {
328 if ((ec
= i
->getName(name
))) {
329 LOG4CXX_ERROR(logger
, ec
.message());
332 LOG4CXX_DEBUG(logger
, "Added section " << name
.str());
333 sections
.insert(make_pair(name
.str(), *i
));
338 void LLVMDisassembler::forEachFunction(std::function
<void (uint64_t, Function
*)> callback
) {
339 std::for_each(functions
.begin(), functions
.end(),
340 [&](std::pair
<uint64_t, LLVMFunction
*> x
) {
341 callback(x
.first
, x
.second
);
345 void LLVMDisassembler::printEachInstruction(uint64_t start
, uint64_t end
,
346 std::function
<void (uint8_t*, size_t, const std::string
&)> fun
) {
347 SectionRef text_section
= sections
[".text"];
348 uint64_t base_address
;
349 text_section
.getAddress(base_address
);
350 uint64_t current_address
= start
- base_address
;
353 text_section
.getContents(bytes
);
354 StringRefMemoryObject
ref(bytes
);
356 while (current_address
< end
- base_address
) {
360 llvm::raw_string_ostream
s(buf
);
362 if(llvm::MCDisassembler::Success
==
363 DisAsm
->getInstruction(inst
, inst_size
, ref
, current_address
, nulls(), nulls())) {
365 uint8_t bytes
[inst_size
+2];
366 ref
.readBytes(current_address
, inst_size
, bytes
);
369 if (MIA
->evaluateBranch(inst
, current_address
, inst_size
, jmptarget
)) {
370 std::stringstream stream
;
371 stream
<< std::hex
<< (base_address
+ jmptarget
);
372 IP
->printInst(&inst
, s
, stream
.str());
374 IP
->printInst(&inst
, s
, "");
376 fun(bytes
, inst_size
, s
.str());
378 LOG4CXX_WARN(logger
, "Invalid byte at" << std::hex
<< current_address
+ base_address
);
379 fun(NULL
, 0, "Invalid Byte");
383 current_address
+= inst_size
;