Skip to content

Commit 2d94822

Browse files
authored
adds armeb abi (#1325)
We reuse the existing arm and thumb abi but make sure that the correct architecture is passed to the memory-related operations.
1 parent 4adedbc commit 2d94822

File tree

2 files changed

+52
-49
lines changed

2 files changed

+52
-49
lines changed

opam/opam

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ depends: [
3232
"conf-bap-llvm" {>= "1.1"}
3333
"parsexp"
3434
"bap-signatures"
35-
"result"
3635
"z3"
3736
"conf-radare2"
3837
"yojson"
39-
"result" {= "1.4"}
38+
"result"
4039
]
4140
depopts: [
4241
"conf-ida"

plugins/arm/arm_gnueabi.ml

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,59 @@ let size = object(self)
1010
end
1111

1212

13-
let nats = Seq.unfold ~init:0 ~f:(fun n -> Some (n,n+1))
14-
let regs = ARM.CPU.[r0;r1;r2;r3] |> List.map ~f:Bil.var
15-
let mems = Seq.map nats ~f:(C.Abi.Stack.create `armv7)
16-
17-
let align ncrn t =
18-
if Size.equal (size#alignment t) `r64 then match ncrn with
19-
| [_;_;_;_] -> ncrn
20-
| [_;r2;r3] -> [r2;r3]
21-
| _ -> []
22-
else ncrn
23-
24-
let concat = List.reduce_exn ~f:Bil.concat
25-
26-
let retregs = function
27-
| #C.Type.scalar as t ->
28-
List.take regs (Size.in_bytes (size#scalar t) / 4), [], regs
29-
| t -> match size#bits (t :> C.Type.t) with
30-
| Some sz when sz <= 32 -> List.take regs 1,[],regs
31-
| _ -> List.take regs 1, List.take regs 1, List.tl_exn regs
32-
33-
let ret : C.Type.t -> 'a = function
34-
| `Void -> None,[],regs
35-
| t -> match retregs t with
36-
| [],_,rest -> None,[],rest
37-
| rets,hids,rest ->
38-
let data = C.Abi.data size t in
39-
Some (data, concat rets),
40-
List.map hids ~f:(fun reg -> t,(C.Data.Ptr data, reg)),
41-
rest
42-
43-
let args _ _ {C.Type.Proto.return; args=ps} =
44-
let return,hidden,regs = ret return in
45-
let _,_,params =
46-
List.fold ps ~init:(regs,mems,[]) ~f:(fun (regs,mems,args) (_,t) ->
47-
let words = Option.value (size#bits t) ~default:32 / 32 in
48-
let exps,regs = List.split_n (align regs t) words in
49-
let rest,mems = Seq.split_n mems (words - List.length exps) in
50-
regs,mems, (C.Abi.data size t, (concat (exps@rest))) :: args) in
51-
Some C.Abi.{return; hidden; params = List.rev params}
52-
53-
let abi = C.Abi.{
54-
insert_args = args;
55-
apply_attrs = fun _ -> ident
56-
}
57-
58-
let api arch = C.Abi.create_api_processor arch abi
13+
module Define(Arch : sig val name : arch end) = struct
14+
let arch = Arch.name
15+
let nats = Seq.unfold ~init:0 ~f:(fun n -> Some (n,n+1))
16+
let regs = ARM.CPU.[r0;r1;r2;r3] |> List.map ~f:Bil.var
17+
let mems = Seq.map nats ~f:(C.Abi.Stack.create arch)
18+
19+
let align ncrn t =
20+
if Size.equal (size#alignment t) `r64 then match ncrn with
21+
| [_;_;_;_] -> ncrn
22+
| [_;r2;r3] -> [r2;r3]
23+
| _ -> []
24+
else ncrn
25+
26+
let concat = List.reduce_exn ~f:Bil.concat
27+
28+
let retregs = function
29+
| #C.Type.scalar as t ->
30+
List.take regs (Size.in_bytes (size#scalar t) / 4), [], regs
31+
| t -> match size#bits (t :> C.Type.t) with
32+
| Some sz when sz <= 32 -> List.take regs 1,[],regs
33+
| _ -> List.take regs 1, List.take regs 1, List.tl_exn regs
34+
35+
let ret : C.Type.t -> 'a = function
36+
| `Void -> None,[],regs
37+
| t -> match retregs t with
38+
| [],_,rest -> None,[],rest
39+
| rets,hids,rest ->
40+
let data = C.Abi.data size t in
41+
Some (data, concat rets),
42+
List.map hids ~f:(fun reg -> t,(C.Data.Ptr data, reg)),
43+
rest
44+
45+
let args _ _ {C.Type.Proto.return; args=ps} =
46+
let return,hidden,regs = ret return in
47+
let _,_,params =
48+
List.fold ps ~init:(regs,mems,[]) ~f:(fun (regs,mems,args) (_,t) ->
49+
let words = Option.value (size#bits t) ~default:32 / 32 in
50+
let exps,regs = List.split_n (align regs t) words in
51+
let rest,mems = Seq.split_n mems (words - List.length exps) in
52+
regs,mems, (C.Abi.data size t, (concat (exps@rest))) :: args) in
53+
Some C.Abi.{return; hidden; params = List.rev params}
54+
55+
let abi = C.Abi.{
56+
insert_args = args;
57+
apply_attrs = fun _ -> ident
58+
}
59+
60+
let api size = C.Abi.create_api_processor size abi
61+
end
5962

6063
let main proj = match Project.arch proj with
61-
| #Arch.arm | #Arch.thumb ->
64+
| #Arch.arm | #Arch.thumb | #Arch.armeb | #Arch.thumbeb as arch ->
65+
let open Define(struct let name = arch end) in
6266
info "using armeabi ABI";
6367
C.Abi.register "eabi" abi;
6468
Bap_api.process (api size);

0 commit comments

Comments
 (0)