Skip to content

Commit 15d4f1d

Browse files
authored
Merge pull request #975 from xwings/dev
baremetal, interpreter and gpos now formed
2 parents 73af577 + 0165002 commit 15d4f1d

File tree

3 files changed

+66
-65
lines changed

3 files changed

+66
-65
lines changed

qiling/core.py

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from .hw.hw import QlHwManager
2020
from .loader.loader import QlLoader
2121

22-
from .const import QL_ARCH_ENDIAN, QL_ENDIAN, QL_OS, QL_VERBOSE, QL_ARCH_INTERPRETER, QL_ARCH_BAREMETAL
22+
from .const import QL_ARCH_ENDIAN, QL_ENDIAN, QL_OS, QL_VERBOSE, QL_ARCH_INTERPRETER, QL_ARCH_BAREMETAL, QL_OS_ALL
2323
from .exception import QlErrorFileNotFound, QlErrorArch, QlErrorOsType, QlErrorOutput
2424
from .utils import *
2525
from .core_struct import QlCoreStructs
@@ -68,9 +68,10 @@ def __init__(
6868
self._code = code
6969
self._ostype = ostype
7070
self._archtype = archtype
71-
self._interpreter = False,
72-
self._baremetal = False,
73-
self._archendian = None
71+
self._interpreter = False
72+
self._baremetal = False
73+
self._gpos = False
74+
self._archendian = QL_ENDIAN.EL
7475
self._archbit = None
7576
self._pointersize = None
7677
self._profile = profile
@@ -107,14 +108,12 @@ def __init__(
107108
self.count = None
108109
self._initial_sp = None
109110

110-
111111
"""
112112
Qiling Framework Core Engine
113113
"""
114114
###############
115115
# code_exec() #
116116
###############
117-
118117
if self._code or (self._archtype and type(self._archtype) == str):
119118
if (self._archtype and type(self._archtype) == str):
120119
self._archtype= arch_convert(self._archtype.lower())
@@ -125,35 +124,32 @@ def __init__(
125124
self._ostype = ostype_convert(self._ostype.lower())
126125

127126
if self._code == None:
128-
self._code = "qiling"
127+
self._code = "qilingcode"
129128
if self._argv is None:
130129
self._argv = ["qilingcode"]
131130
if self._rootfs is None:
132131
self._rootfs = "."
133132

134133
self._interpreter = True if self._archtype in (QL_ARCH_INTERPRETER) else False
135134
self._baremetal = True if self._archtype in (QL_ARCH_BAREMETAL) else False
135+
self._path = (str(self._argv[0]))
136+
self._targetname = ntpath.basename(self._argv[0])
136137

137-
# file check
138-
if self._code is None:
138+
##############
139+
# File check #
140+
##############
141+
if (not self._interpreter and not self._baremetal) and self._code == None:
139142
if not os.path.exists(str(self._argv[0])):
140143
raise QlErrorFileNotFound("Target binary not found: %s" %(self._argv[0]))
141144
if not os.path.exists(self._rootfs):
142145
raise QlErrorFileNotFound("Target rootfs not found")
143-
144-
self._path = (str(self._argv[0]))
145-
self._targetname = ntpath.basename(self._argv[0])
146-
147-
##########
148-
# Loader #
149-
##########
150-
if self._code is None:
146+
151147
guessed_archtype, guessed_ostype, guessed_archendian = ql_guess_emu_env(self._path)
148+
152149
if self._ostype is None:
153150
self._ostype = guessed_ostype
154151
if self._archtype is None:
155152
self._archtype = guessed_archtype
156-
if self.archendian is None:
157153
self._archendian = guessed_archendian
158154

159155
if not ql_is_valid_ostype(self._ostype):
@@ -162,6 +158,13 @@ def __init__(
162158
if not ql_is_valid_arch(self._archtype):
163159
raise QlErrorArch("Invalid Arch %s" % self._archtype)
164160

161+
162+
163+
164+
#######################################
165+
# Loader and General Purpose OS check #
166+
#######################################
167+
self._gpos = True if self._ostype in (QL_OS_ALL) else False
165168
self._loader = loader_setup(self._ostype, self)
166169

167170
#####################
@@ -170,7 +173,6 @@ def __init__(
170173
self._profile, debugmsg = profile_setup(self)
171174

172175
# Log's configuration
173-
174176
self._log_file_fd, self._log_filter = ql_setup_logger(self,
175177
self._log_file,
176178
self._console,
@@ -189,25 +191,23 @@ def __init__(
189191
self._archbit = ql_get_arch_bits(self._archtype)
190192
self._pointersize = (self.archbit // 8)
191193

192-
# Endian for shellcode needs to set manually
193-
if self._code:
194-
self._archendian = QL_ENDIAN.EL
195-
if bigendian == True and self._archtype in (QL_ARCH_ENDIAN):
196-
self._archendian = QL_ENDIAN.EB
194+
195+
if bigendian == True and self._archtype in (QL_ARCH_ENDIAN):
196+
self._archendian = QL_ENDIAN.EB
197+
197198

198199
# Once we finish setting up archendian and arcbit, we can init QlCoreStructs.
199200
QlCoreStructs.__init__(self, self._archendian, self._archbit)
200201

201202
##############
202203
# Components #
203204
##############
204-
205-
if not self._interpreter:
205+
if self._gpos or self._baremetal:
206206
self._mem = component_setup("os", "memory", self)
207207
self._reg = component_setup("arch", "register", self)
208-
209-
if self._baremetal:
210-
self._hw = component_setup("hw", "hw", self)
208+
209+
if self._baremetal:
210+
self._hw = component_setup("hw", "hw", self)
211211

212212
self._arch = arch_setup(self.archtype, self)
213213

@@ -216,29 +216,27 @@ def __init__(
216216
QlCoreHooks.__init__(self, self.uc)
217217

218218
# Setup Outpt
219-
if not self._interpreter:
219+
if self._gpos or self._baremetal:
220220
self.arch.utils.setup_output()
221221

222-
if not self._interpreter:
223-
if not self._baremetal:
224-
self._os = os_setup(self.archtype, self.ostype, self)
222+
if self._gpos:
223+
self._os = os_setup(self.archtype, self.ostype, self)
225224

226-
if stdin is not None:
227-
self._os.stdin = stdin
225+
if stdin is not None:
226+
self._os.stdin = stdin
228227

229-
if stdout is not None:
230-
self._os.stdout = stdout
228+
if stdout is not None:
229+
self._os.stdout = stdout
231230

232-
if stderr is not None:
233-
self._os.stderr = stderr
231+
if stderr is not None:
232+
self._os.stderr = stderr
234233

235234
# Run the loader
236235
self.loader.run()
237236

238-
if not self._interpreter:
239-
if not self._baremetal:
240-
# Add extra guard options when configured to do so
241-
self._init_stop_guard()
237+
if self._gpos:
238+
# Add extra guard options when configured to do so
239+
self._init_stop_guard()
242240

243241
#####################
244242
# Qiling Components #
@@ -655,6 +653,18 @@ def baremetal(self, b):
655653
self._baremetal = b
656654

657655

656+
@property
657+
def gpos(self) -> bool:
658+
""" Raw uc instance.
659+
660+
Type: Ucgit
661+
"""
662+
return self._gpos
663+
664+
@gpos.setter
665+
def gpos(self, o):
666+
self._gpos = o
667+
658668
@property
659669
def stop_options(self) -> "QlStopOptions":
660670
""" The stop options configured:
@@ -736,33 +746,25 @@ def run(self, begin=None, end=None, timeout=0, count=0, code = None):
736746
if self._debugger != False and self._debugger != None:
737747
self._debugger = debugger_setup(self._debugger, self)
738748

739-
if not self.interpreter:
740-
if not self.baremetal:
741-
self.write_exit_trap()
742-
# patch binary
743-
self.__enable_bin_patch()
744-
745-
# emulate the binary
746-
self.os.run()
747-
748749
if self.interpreter:
749-
if code == None:
750-
return self.arch.run(self._code)
751-
else:
752-
return self.arch.run(code)
753-
754-
if self.baremetal:
750+
return self.arch.run(code)
751+
elif self.baremetal:
755752
self.__enable_bin_patch()
756753
if self.count <= 0:
757754
self.count = -1
758-
self.arch.run(count=self.count, end=self.exit_point)
759-
755+
self.arch.run(count=self.count, end=self.exit_point)
756+
else:
757+
self.write_exit_trap()
758+
# patch binary
759+
self.__enable_bin_patch()
760+
# emulate the binary
761+
self.os.run()
762+
760763
# run debugger
761764
if self._debugger != False and self._debugger != None:
762765
self._debugger.run()
763766

764767

765-
766768
# patch code to memory address
767769
def patch(self, addr, code, file_name=b''):
768770
if file_name == b'':

qiling/debugger/gdb/gdb.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ def __init__(self, ql: Qiling, ip: str = '127.0.01', port: int = 9999):
5151
self.ip = ip
5252
self.port = port
5353

54-
5554
if self.ql.baremetal:
5655
load_address = self.ql.loader.load_address
5756
exit_point = load_address + os.path.getsize(ql.path)
58-
elif self.ql.code:
57+
elif self.ql.code and ql.gpos:
5958
load_address = self.ql.os.entry_point
6059
exit_point = load_address + len(ql.code)
6160
else:

tests/test_elf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,13 +480,13 @@ def test_elf_linux_arm64_static(self):
480480
del ql
481481

482482

483-
def test_elf_linux_mips32_static(self):
483+
def test_elf_linux_mips32eb_static(self):
484484
ql = Qiling(["../examples/rootfs/mips32_linux/bin/mips32_hello_static"], "../examples/rootfs/mips32_linux")
485485
ql.run()
486486
del ql
487487

488488

489-
def test_elf_linux_mips32(self):
489+
def test_elf_linux_mips32eb(self):
490490
def random_generator(size=6, chars=string.ascii_uppercase + string.digits):
491491
return ''.join(random.choice(chars) for x in range(size))
492492

0 commit comments

Comments
 (0)