-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdisa_thumb.adb
374 lines (298 loc) · 12.3 KB
/
disa_thumb.adb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
------------------------------------------------------------------------------
-- --
-- GNATcoverage --
-- --
-- Copyright (C) 2008-2024, AdaCore --
-- --
-- GNATcoverage is free software; you can redistribute it and/or modify it --
-- under terms of the GNU General Public License as published by the Free --
-- Software Foundation; either version 3, or (at your option) any later --
-- version. This software is distributed in the hope that it will be useful --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details. You should have received a copy of the GNU --
-- General Public License distributed with this software; see file --
-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy --
-- of the license. --
------------------------------------------------------------------------------
with Interfaces; use Interfaces;
with Disa_Common;
package body Disa_Thumb is
function To_Insn16 (Insn_Bin : Binary_Content) return Unsigned_16 is
(Disa_Common.ELF_To_U16
(Slice (Insn_Bin, Insn_Bin.First, Insn_Bin.First + 1)));
function To_Insn32 (Insn_Bin : Binary_Content) return Unsigned_32 is
(Unsigned_32 (Disa_Common.ELF_To_U16
(Slice (Insn_Bin,
Insn_Bin.First + 2,
Insn_Bin.First + 3)))
+ Shift_Left (Unsigned_32 (Disa_Common.ELF_To_U16
(Slice (Insn_Bin,
Insn_Bin.First + 0,
Insn_Bin.First + 1))), 16));
function Is_32bit_Insn (Insn_Bin : Binary_Content) return Boolean is
(case Shift_Right (To_Insn16 (Insn_Bin), 11) is
when 2#00000# .. 2#11100# => False,
when others => True);
function Slice (U32 : Unsigned_32;
Shift : Natural;
Mask : Unsigned_32) return Unsigned_32 is
(Shift_Right (U32, Shift) and Mask);
function Sign_Extend
(U32 : Unsigned_32;
Width : Natural) return Unsigned_32
is (Shift_Right_Arithmetic (Shift_Left (U32, 32 - Width), 32 - Width));
function Get_Target (Immediate : Unsigned_32;
Shift : Natural;
Pc : Unsigned_32) return Unsigned_32
is (Pc + Shift_Left (Immediate, Shift) + 4);
-- The PC is always 2 instructions beyond the currently executing
-- instruction, hence the +4 offset.
----------------
-- Initialize --
----------------
overriding procedure Initialize
(Object : in out Thumb_Disassembler) is
begin
Object.Handle := Dis_Opcodes.Create_Thumb_Disassembler;
end Initialize;
--------------
-- Finalize --
--------------
overriding procedure Finalize
(Object : in out Thumb_Disassembler) is
begin
Dis_Opcodes.Delete_Disassembler (Object.Handle);
end Finalize;
---------------------
-- Get_Insn_Length --
---------------------
function Get_Insn_Length
(Self : Thumb_Disassembler;
Insn_Bin : Binary_Content) return Positive
is
pragma Unreferenced (Self);
begin
return (if Is_32bit_Insn (Insn_Bin) then 4 else 2);
end Get_Insn_Length;
----------------------
-- Disassemble_Insn --
----------------------
procedure Disassemble_Insn
(Self : Thumb_Disassembler;
Insn_Bin : Binary_Content;
Pc : Traces.Pc_Type;
Buffer : in out Highlighting.Buffer_Type;
Insn_Len : out Natural;
Sym : Symbolizer'Class)
is
begin
Disa_Common.Opcodes_Disassemble_Insn
(Self.Handle, Insn_Bin, Pc, Buffer, Insn_Len, Sym, 4);
end Disassemble_Insn;
-------------------------
-- Get_Insn_Properties --
-------------------------
procedure Get_Insn_Properties
(Self : Thumb_Disassembler;
Insn_Bin : Binary_Content;
Pc : Pc_Type;
Branch : out Branch_Kind;
Flag_Indir : out Boolean;
Flag_Cond : out Boolean;
Branch_Dest : out Dest;
FT_Dest : out Dest)
is
PC32 : constant Unsigned_32 := Unsigned_32 (Pc);
begin
Branch := Br_None;
Branch_Dest := (No_PC, No_PC);
FT_Dest := (No_PC, No_PC);
Flag_Indir := False;
Flag_Cond := False;
if Is_32bit_Insn (Insn_Bin) then
declare
Insn32 : constant Unsigned_32 := To_Insn32 (Insn_Bin);
Opcode : constant Unsigned_32 :=
Shift_Right (Insn32, 11 + 16) and 2#11#;
begin
case Opcode is
when 2#00# =>
null;
when 2#01# =>
if (Insn32 and 16#fff0_ffe0#) = 16#e8d0_f000# then
-- TBB/TBH(T15)
Branch := Br_Jmp;
Flag_Indir := True;
elsif (Insn32 and 16#ffd0_2000#) = 16#e890_0000# then
-- LDM/LDMIA/LDMFD (T2)
if (Insn32 and 2**15) = 2**15 then
-- This instruction writes in PC, so it's a control-flow
-- instruction.
Branch := Br_Jmp;
Flag_Indir := True;
end if;
end if;
when 2#10# =>
declare
Discr : constant Unsigned_32 :=
(Slice (Insn32, 12, 1)
or Shift_Left (Slice (Insn32, 14, 2#11#), 1));
S : constant Unsigned_32 := Slice (Insn32, 26, 1);
J1 : constant Unsigned_32 := Slice (Insn32, 13, 1);
J2 : constant Unsigned_32 := Slice (Insn32, 11, 1);
XJ1 : constant Unsigned_32 := J1 xor S xor 1;
XJ2 : constant Unsigned_32 := J2 xor S xor 1;
Imm6 : constant Unsigned_32 := Slice (Insn32, 16, 16#3f#);
Imm10 : constant Unsigned_32 := Slice (Insn32, 16, 16#3ff#);
Imm11 : constant Unsigned_32 := Slice (Insn32, 0, 16#7ff#);
Immediate : Unsigned_32;
begin
case Discr is
when 2#111# => -- BL(T1)
Branch := Br_Call;
Immediate := Sign_Extend
((Shift_Left (Imm11, 1)
or Shift_Left (Imm10, 12)
or Shift_Left (XJ1, 22)
or Shift_Left (XJ2, 23)
or Shift_Left (S, 24)),
25);
when 2#110# => -- BLX(T2)
Branch := Br_Call;
Immediate := Sign_Extend
((Shift_Left (Imm11, 1)
or Shift_Left (Imm6, 12)
or Shift_Left (J1, 19)
or Shift_Left (J2, 20)
or Shift_Left (S, 21)),
22);
when 2#100# => -- B(T3)
-- TODO??? This is BXJ when cond = 1111
Branch := Br_Jmp;
Flag_Cond := True;
Immediate := Sign_Extend
((Shift_Left (Imm11, 1)
or Shift_Left (Imm6, 12)
or Shift_Left (J1, 19)
or Shift_Left (J2, 20)
or Shift_Left (S, 21)),
22);
when 2#101# => -- B(T4)
Branch := Br_Jmp;
Immediate := Sign_Extend
((Shift_Left (Imm11, 1)
or Shift_Left (Imm10, 12)
or Shift_Left (XJ1, 22)
or Shift_Left (XJ2, 23)
or Shift_Left (S, 24)),
25);
-- B(T3)
when others =>
null;
end case;
if Branch /= Br_None then
Branch_Dest.Target :=
Pc_Type (Get_Target (Immediate, 0, PC32));
end if;
end;
when others => -- 2#11#
if (Insn32 and 16#ffff_2000#) = 16#e8bd_0000# then
-- POP (T2)
if (Insn32 and 16#0000_8000#) = 16#0000_8000# then
-- This instruction writes in PC, so it's a control-flow
-- instruction.
Branch := Br_Jmp;
Flag_Indir := True;
end if;
elsif (Insn32 and 16#fef0_0000#) = 16#f850_0000# then
-- POP (T3), LDR (immediate, T3 or T4)
if (Insn32 and 16#0000_f000#) = 16#0000_f000# then
-- This instruction writes in PC, so it's a control-flow
-- instruction.
Branch := Br_Jmp;
Flag_Indir := True;
end if;
end if;
end case;
end;
else
declare
Insn16 : constant Unsigned_16 := To_Insn16 (Insn_Bin);
Opcode : constant Unsigned_16 := Shift_Right (Insn16, 10);
begin
case Opcode is
when 2#010001# =>
if (Insn16 and 16#ff87#) = 16#4780# then
-- BLX(T1)
Branch := Br_Call;
Flag_Indir := True;
elsif (Insn16 and 16#ff87#) = 16#4700# then
-- BX(T1)
Branch := Br_Jmp;
Flag_Indir := False;
end if;
when 2#101100# .. 2#101111# =>
if (Insn16 and 16#0500#) = 16#0100# then
-- CBZ/CBNZ
Flag_Cond := True;
Branch := Br_Jmp;
declare
I : constant Unsigned_32 :=
Unsigned_32 (Shift_Right (Insn16, 9) and 1);
Imm5 : constant Unsigned_32 :=
Unsigned_32 (Shift_Right (Insn16, 3) and 16#1f#);
Immediate : constant Unsigned_32 :=
(Shift_Left (Imm5, 1)
or Shift_Left (I, 6));
begin
Branch_Dest.Target :=
Pc_Type (Get_Target (Immediate, 0, PC32));
end;
elsif (Shift_Right (Insn16, 8) and 16#f#) = 2#1101# then
-- POP (T1, P is set: write to PC = branch)
Branch := Br_Jmp;
Flag_Indir := True;
end if;
when 2#110100# .. 2#110111# =>
-- B(T1)
Flag_Cond := True;
Branch := Br_Jmp;
declare
Immediate : constant Unsigned_32 :=
Sign_Extend (Unsigned_32 (Insn16), 8);
begin
Branch_Dest.Target :=
Pc_Type (Get_Target (Immediate, 1, PC32));
end;
when 2#111000# .. 2#111001# =>
-- B(T2)
Flag_Cond := False;
Branch := Br_Jmp;
declare
Immediate : constant Unsigned_32 :=
Sign_Extend (Unsigned_32 (Insn16), 11);
begin
Branch_Dest.Target :=
Pc_Type (Get_Target (Immediate, 1, PC32));
end;
when others =>
null;
end case;
end;
end if;
FT_Dest.Target := Pc + Pc_Type (Get_Insn_Length (Self, Insn_Bin));
end Get_Insn_Properties;
----------------
-- Is_Padding --
----------------
function Is_Padding
(Self : Thumb_Disassembler;
Insn_Bin : Binary_Content;
Pc : Pc_Type) return Boolean
is
pragma Unreferenced (Self, Insn_Bin, Pc);
begin
return False;
end Is_Padding;
end Disa_Thumb;