44# Built on top of Unicorn emulator (www.unicorn-engine.org)
55
66
7+ import io
78import struct
89from elftools .elf .elffile import ELFFile
910
@@ -65,7 +66,8 @@ def __init__(self, ql:Qiling):
6566 self .filetype = self .guess_filetype ()
6667
6768 if self .filetype == 'elf' :
68- self .elf = ELFFile (open (self .path , 'rb' ))
69+ with open (self .path , 'rb' ) as infile :
70+ self .elf = ELFFile (io .BytesIO (infile .read ()))
6971
7072 elif self .filetype == 'bin' :
7173 self .map_address = self .argv [1 ]
@@ -88,12 +90,10 @@ def guess_filetype(self):
8890 def reset (self ):
8991 if self .filetype == 'elf' :
9092 for segment in self .elf .iter_segments ():
91- if segment ['p_type' ] != 'PT_LOAD' :
92- continue
93-
94- for section in self .elf .iter_sections ():
95- if segment .section_in_segment (section ):
96- self .ql .mem .write (section .header ['sh_addr' ], section .data ())
93+ if segment ['p_type' ] == 'PT_LOAD' :
94+ for section in self .elf .iter_sections ():
95+ if segment .section_in_segment (section ):
96+ self .ql .mem .write (section .header ['sh_addr' ], section .data ())
9797
9898 # TODO: load symbol table
9999
@@ -110,25 +110,35 @@ def reset(self):
110110 self .ql .reg .write ('pc' , self .entry_point )
111111
112112 def run (self ):
113- ## Load memory / mmio / peripheral from profile
113+ def readint (raw ):
114+ if raw .startswith ('0o' ):
115+ return int (raw , 8 )
116+
117+ elif raw .startswith ('0x' ):
118+ return int (raw , 16 )
119+
120+ else :
121+ return int (raw , 10 )
122+
114123 for section_name in self .ql .profile .sections ():
115124 section = self .ql .profile [section_name ]
116125 if section ['type' ] == 'memory' :
117- size = eval (section ['size' ])
118- base = eval (section ['base' ])
126+ size = readint (section ['size' ])
127+ base = readint (section ['base' ])
119128 self .ql .mem .map (base , size , info = f'[{ section_name } ]' )
129+
120130 if section_name == 'FLASH' :
121131 self .ql .hw .setup_remap (0 , base , size , info = f'[CODE]' )
122132
123133 if section ['type' ] == 'bitband' :
124- size = eval (section ['size' ]) * 32
125- base = eval (section ['base' ])
126- alias = eval (section ['alias' ])
134+ size = readint (section ['size' ]) * 32
135+ base = readint (section ['base' ])
136+ alias = readint (section ['alias' ])
127137 self .ql .hw .setup_bitband (base , alias , size , info = f'[{ section_name } ]' )
128138
129139 if section ['type' ] == 'mmio' :
130- size = eval (section ['size' ])
131- base = eval (section ['base' ])
140+ size = readint (section ['size' ])
141+ base = readint (section ['base' ])
132142 self .ql .hw .setup_mmio (base , size , info = f'[{ section_name } ]' )
133143
134144 if section ['type' ] == 'core periperal' :
0 commit comments