|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Cross Platform and Multi Architecture Advanced Binary Emulation Framework |
| 4 | +# |
| 5 | + |
| 6 | +from qiling import Qiling |
| 7 | +from qiling.cc import QlCC, intel, arm, mips |
| 8 | +from qiling.const import QL_ARCH |
| 9 | +from qiling.loader.loader import QlLoader |
| 10 | +from qiling.os.fcall import QlFunctionCall |
| 11 | +from qiling.os.memory import QlMemoryHeap |
| 12 | +from qiling.os.os import QlOs |
| 13 | + |
| 14 | + |
| 15 | +class QLOsBare(QlOs): |
| 16 | + """ QLOsBare for bare barines. |
| 17 | +
|
| 18 | + For bare binary such as u-boot, it's ready to be mapped and executed directly, |
| 19 | + where there is(may be) no concept of os? Currently, some functionalities such as |
| 20 | + resolve_fcall_params(), heap or add_fs_mapper() are based on os. To keep the |
| 21 | + consistence of api usage, QLOsBare is introduced and placed at its loader temporarily. |
| 22 | + """ |
| 23 | + def __init__(self, ql: Qiling): |
| 24 | + super(QLOsBare, self).__init__(ql) |
| 25 | + |
| 26 | + self.ql = ql |
| 27 | + |
| 28 | + cc: QlCC = { |
| 29 | + QL_ARCH.X86 : intel.cdecl, |
| 30 | + QL_ARCH.X8664 : intel.amd64, |
| 31 | + QL_ARCH.ARM : arm.aarch32, |
| 32 | + QL_ARCH.ARM64 : arm.aarch64, |
| 33 | + QL_ARCH.MIPS : mips.mipso32 |
| 34 | + }[ql.archtype](ql) |
| 35 | + |
| 36 | + self.fcall = QlFunctionCall(ql, cc) |
| 37 | + |
| 38 | + def run(self): |
| 39 | + self.entry_point = self.ql.entry_point if self.ql.entry_point else self.ql.loader.load_address |
| 40 | + self.exit_point = self.ql.exit_point if self.ql.exit_point else self.ql.loader.load_address + len(self.ql.code) |
| 41 | + |
| 42 | + self.ql.emu_start(self.entry_point, self.exit_point, self.ql.timeout, self.ql.count) |
| 43 | + |
| 44 | +class QlLoaderBLOB(QlLoader): |
| 45 | + def __init__(self, ql: Qiling): |
| 46 | + super().__init__(ql) |
| 47 | + |
| 48 | + self.load_address = 0 |
| 49 | + |
| 50 | + def run(self): |
| 51 | + # setup bare os |
| 52 | + self.ql._os = QLOsBare(self.ql) |
| 53 | + |
| 54 | + self.load_address = self.ql.os.entry_point # for consistency |
| 55 | + |
| 56 | + self.ql.mem.map(self.ql.os.entry_point, self.ql.os.code_ram_size, info="[code]") |
| 57 | + self.ql.mem.write(self.ql.os.entry_point, self.ql.code) |
| 58 | + |
| 59 | + heap_address = self.ql.os.entry_point + self.ql.os.code_ram_size |
| 60 | + heap_size = int(self.ql.os.profile.get("CODE", "heap_size"), 16) |
| 61 | + self.ql.os.heap = QlMemoryHeap(self.ql, heap_address, heap_address + heap_size) |
| 62 | + |
| 63 | + self.ql.reg.arch_sp = heap_address - 0x1000 |
| 64 | + |
| 65 | + return |
0 commit comments