Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
7e156f2e
Commit
7e156f2e
authored
Sep 30, 2021
by
George Hotz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
mips startup stub
parent
139155c4
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
74 additions
and
33 deletions
+74
-33
maketests.py
mipsevm/maketests.py
+32
-25
run.py
risc/run.py
+18
-8
startup.bin
risc/startup.bin
+0
-0
startup.s
risc/startup.s
+22
-0
startup.sh
risc/startup.sh
+2
-0
No files found.
mipsevm/maketests.py
View file @
7e156f2e
#!/usr/bin/env python3
import
os
import
sys
import
tempfile
from
capstone
import
*
md
=
Cs
(
CS_ARCH_MIPS
,
CS_MODE_32
+
CS_MODE_BIG_ENDIAN
)
from
elftools.elf.elffile
import
ELFFile
os
.
makedirs
(
"/tmp/mips"
,
exist_ok
=
True
)
path
=
"/Users/kafka/fun/mips/mips-gcc-4.8.1/bin/"
# Tests from:
# https://github.com/grantae/OpenMIPS/blob/master/software/test/macro/tests/addiu/src/os/khi/addiu.asm
for
d
in
os
.
listdir
(
"test/"
):
if
not
d
.
endswith
(
".asm"
):
continue
print
(
"building"
,
d
)
# which mips is go
os
.
system
(
"
%
s/mips-elf-as -defsym big_endian=1 -march=mips32r2 -o /tmp/mips/
%
s test/
%
s"
%
(
path
,
d
,
d
))
elffile
=
ELFFile
(
open
(
"/tmp/mips/"
+
d
,
"rb"
))
#print(elffile)
for
sec
in
elffile
.
iter_sections
():
#print(sec, sec.name, sec.data())
if
sec
.
name
==
".test"
:
with
open
(
"test/bin/"
+
(
d
.
replace
(
".asm"
,
".bin"
)),
"wb"
)
as
f
:
# jump to 0xdead0000 when done
#data = b"\x24\x1f\xde\xad\x00\x1f\xfc\x00" + sec.data()
data
=
sec
.
data
()
for
dd
in
md
.
disasm
(
data
,
0
):
print
(
dd
)
f
.
write
(
data
)
md
=
Cs
(
CS_ARCH_MIPS
,
CS_MODE_32
+
CS_MODE_BIG_ENDIAN
)
def
maketest
(
d
,
out
):
with
tempfile
.
NamedTemporaryFile
()
as
nf
:
path
=
"/Users/kafka/fun/mips/mips-gcc-4.8.1/bin/"
print
(
"building"
,
d
,
"->"
,
out
)
# which mips is go
ret
=
os
.
system
(
"
%
s/mips-elf-as -defsym big_endian=1 -march=mips32r2 -o
%
s
%
s"
%
(
path
,
nf
.
name
,
d
))
assert
(
ret
==
0
)
nf
.
seek
(
0
)
elffile
=
ELFFile
(
nf
)
#print(elffile)
for
sec
in
elffile
.
iter_sections
():
#print(sec, sec.name, sec.data())
if
sec
.
name
==
".test"
:
with
open
(
out
,
"wb"
)
as
f
:
# jump to 0xdead0000 when done
#data = b"\x24\x1f\xde\xad\x00\x1f\xfc\x00" + sec.data()
data
=
sec
.
data
()
for
dd
in
md
.
disasm
(
data
,
0
):
print
(
dd
)
f
.
write
(
data
)
if
__name__
==
"__main__"
:
os
.
makedirs
(
"/tmp/mips"
,
exist_ok
=
True
)
if
len
(
sys
.
argv
)
>
2
:
maketest
(
sys
.
argv
[
1
],
sys
.
argv
[
2
])
else
:
for
d
in
os
.
listdir
(
"test/"
):
if
not
d
.
endswith
(
".asm"
):
continue
maketest
(
"test/"
+
d
,
"test/bin/"
+
(
d
.
replace
(
".asm"
,
".bin"
)))
\ No newline at end of file
risc/run.py
View file @
7e156f2e
...
...
@@ -8,6 +8,7 @@ import traceback
from
elftools.elf.elffile
import
ELFFile
from
capstone
import
*
md
=
Cs
(
CS_ARCH_MIPS
,
CS_MODE_32
+
CS_MODE_BIG_ENDIAN
)
tracelevel
=
int
(
os
.
getenv
(
"TRACE"
,
0
))
from
termcolor
import
colored
,
cprint
from
hexdump
import
hexdump
...
...
@@ -54,7 +55,7 @@ def hook_code_simple(uc, address, size, user_data):
dat
=
"EMPTY BASIC BLOCK?!?"
#instructions_seen.add(dat.mnemonic)
#print(sorted(list(instructions_seen)))
print
(
"
%10
d(
%2
d):
%8
x
%-80
s
%
s"
%
(
icount
,
newicount
,
address
,
r
[
address
],
dat
))
print
(
"
%10
d(
%2
d):
%8
x
%-80
s
%
s"
%
(
icount
,
newicount
,
address
,
r
[
address
]
if
address
in
r
else
"UNKNOWN"
,
dat
))
icount
+=
newicount
bcount
+=
1
return
True
...
...
@@ -66,7 +67,6 @@ def hook_code_simple(uc, address, size, user_data):
def
start_instrumenting
():
global
instrumenting
,
instrumenting_all
if
not
instrumenting
:
tracelevel
=
int
(
os
.
getenv
(
"TRACE"
,
0
))
if
tracelevel
>=
2
:
mu
.
hook_add
(
UC_HOOK_CODE
,
hook_code_simple
,
user_data
=
mu
)
elif
tracelevel
==
1
:
...
...
@@ -304,19 +304,26 @@ for seg in elffile.iter_segments():
mu
.
mem_write
(
seg
.
header
.
p_vaddr
,
seg
.
data
())
entry
=
elffile
.
header
.
e_entry
print
(
"entrypoint:
%
x"
%
entry
)
print
(
"entrypoint:
0x
%
x"
%
entry
)
#hexdump(mu.mem_read(entry, 0x10))
"""
mu.reg_write(UC_MIPS_REG_SP, stack_start-0x2000)
# http://articles.manugarg.com/aboutelfauxiliaryvectors.html
_AT_PAGESZ = 6
mu
.
mem_write
(
stack_start
-
0x2000
,
struct
.
pack
(
">IIIIII
II
"
,
1
,
# argc
stack_start
-
0x1000
,
0
,
# argv
stack_start
-
0x400
,
0
,
# envp
mu.mem_write(stack_start-0x2000, struct.pack(">IIIIII",
0
, # argc
0, # argv
0, # envp
_AT_PAGESZ, 0x1000, 0)) # auxv
mu.mem_write(stack_start-0x400, b"GOGC=off
\x00
")
"""
# moved to MIPS
start
=
open
(
"startup.bin"
,
"rb"
)
.
read
()
+
struct
.
pack
(
">I"
,
entry
)
mu
.
mem_write
(
0
,
start
)
entry
=
0
r
=
RangeTree
()
for
section
in
elffile
.
iter_sections
():
...
...
@@ -359,8 +366,11 @@ mu.hook_add(UC_HOOK_MEM_FETCH_UNMAPPED, hook_mem_invalid)
mu
.
hook_add
(
UC_HOOK_INTR
,
hook_interrupt
)
#mu.hook_add(UC_HOOK_INSN, hook_interrupt, None, 1, 0, 0x0c000000)
if
tracelevel
==
4
:
start_instrumenting
()
try
:
mu
.
emu_start
(
entry
,
0
)
mu
.
emu_start
(
entry
,
SIZE
)
except
unicorn
.
UcError
:
pass
...
...
risc/startup.bin
0 → 100644
View file @
7e156f2e
File added
risc/startup.s
0 → 100644
View file @
7e156f2e
.section .test, "x"
.balign 4
.set noreorder
.global test
.ent test
test:
lui $sp, 0x7fff
ori $sp, 0xd000
ori $t0, $0, 6
sw $t0, 0xC($sp)
ori $t0, $0, 0x1000
sw $t0, 0x10($sp)
lw $ra, dat($0)
jr $ra
nop
dat:
.end test
risc/startup.sh
0 → 100755
View file @
7e156f2e
#!/bin/bash -e
../mipsevm/maketests.py ../risc/startup.s ../risc/startup.bin
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment