Skip to content

Commit d1a787b

Browse files
committed
uses ghidra backend for AVR lifter and disassembler
1 parent 1e3713f commit d1a787b

File tree

6 files changed

+43
-224
lines changed

6 files changed

+43
-224
lines changed

lib/bap_avr/bap_avr_target.ml

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
open Core_kernel
22
open Bap_core_theory
3+
module Dis = Bap.Std.Disasm_expert.Basic
4+
35

46
let package = "bap"
57

@@ -36,12 +38,42 @@ let parent = Theory.Target.declare ~package "avr"
3638
~byte:8
3739
~endianness:Theory.Endianness.le
3840

39-
4041
let atmega328 = Theory.Target.declare ~package "ATmega328"
4142
~parent
4243
~data
4344
~code
4445
~vars:(gpr @< [sp] @< flags @< [data] @< [code])
4546

47+
let pcode =
48+
Theory.Language.declare ~package:"bap" "pcode-avr"
49+
50+
let provide_decoding () =
51+
let open KB.Syntax in
52+
KB.promise Theory.Label.encoding @@ fun label ->
53+
Theory.Label.target label >>| fun t ->
54+
if Theory.Target.belongs parent t
55+
then pcode
56+
else Theory.Language.unknown
57+
58+
let enable_ghidra () =
59+
Dis.register pcode @@ fun _target ->
60+
Dis.create ~backend:"ghidra" "avr8:LE:16:atmega256"
61+
62+
let enable_loader () =
63+
let open Bap.Std in
64+
let open KB.Syntax in
65+
let request_arch doc =
66+
let open Ogre.Syntax in
67+
match Ogre.eval (Ogre.request Image.Scheme.arch) doc with
68+
| Error _ -> None
69+
| Ok arch -> arch in
70+
KB.promise Theory.Unit.target @@ fun unit ->
71+
KB.collect Image.Spec.slot unit >>| request_arch >>| function
72+
| Some "avr" -> atmega328
73+
| _ -> Theory.Target.unknown
74+
4675

47-
let llvm_avr16 = Theory.Language.declare ~package "llvm-avr16"
76+
let load () =
77+
enable_ghidra ();
78+
enable_loader ();
79+
provide_decoding ()

lib/bap_avr/bap_avr_target.mli

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
open Bap_core_theory
22

33
val parent : Theory.target
4-
val atmega328 : Theory.target
5-
val llvm_avr16 : Theory.language
64

7-
type r16 and r8
8-
9-
type 'a bitv = 'a Theory.Bitv.t Theory.Value.sort
10-
11-
val r16 : r16 bitv
12-
val r8 : r8 bitv
13-
14-
val code : (r16, r16) Theory.Mem.t Theory.var
15-
val data : (r16, r8) Theory.Mem.t Theory.var
16-
val gpr : r8 Theory.Bitv.t Theory.var list
17-
val sp : r16 Theory.Bitv.t Theory.var
18-
val flags : Theory.Bool.t Theory.var list
5+
val load : unit -> unit

oasis/avr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@ Library "bap-avr"
66
Build$: flag(everything) || flag(avr)
77
XMETADescription: common definitions for Avr targets
88
Path: lib/bap_avr
9-
BuildDepends: core_kernel, bap-knowledge, bap-core-theory
9+
BuildDepends: core_kernel, bap-knowledge, bap-core-theory, bap, ogre
1010
FindlibName: bap-avr
1111
Modules: Bap_avr_target
1212

1313
Library avr_plugin
1414
XMETADescription: provide Avr lifter
1515
Path: plugins/avr
1616
Build$: flag(everything) || flag(avr)
17-
BuildDepends: core_kernel, ppx_jane, ogre,
18-
bap-core-theory, bap-knowledge, bap-main,
19-
bap, bap-avr, bitvec
17+
BuildDepends: bap-main, bap-avr
2018
FindlibName: bap-plugin-avr
21-
InternalModules: Avr_main, Avr_lifter
19+
InternalModules: Avr_main
2220
XMETAExtraLines: tags="avr, lifter, atmega"

plugins/avr/avr_lifter.ml

Lines changed: 0 additions & 156 deletions
This file was deleted.

plugins/avr/avr_lifter.mli

Lines changed: 0 additions & 1 deletion
This file was deleted.

plugins/avr/avr_main.ml

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,12 @@
11
open Bap_main
2-
open Bap.Std
3-
open Bap_core_theory
4-
open KB.Syntax
5-
module CT = Theory
6-
7-
include Bap_main.Loggers()
8-
9-
module Target = Bap_avr_target
10-
module Dis = Disasm_expert.Basic
11-
12-
let provide_decoding () =
13-
KB.promise CT.Label.encoding @@ fun label ->
14-
CT.Label.target label >>| fun t ->
15-
if CT.Target.belongs Target.parent t
16-
then Target.llvm_avr16
17-
else CT.Language.unknown
18-
19-
let enable_llvm () =
20-
Dis.register Target.llvm_avr16 @@ fun _target ->
21-
Dis.create ~backend:"llvm" "avr"
22-
23-
let enable_loader () =
24-
let request_arch doc =
25-
let open Ogre.Syntax in
26-
match Ogre.eval (Ogre.request Image.Scheme.arch) doc with
27-
| Error _ -> assert false (* nothing could go wrong here! *)
28-
| Ok arch -> arch in
29-
KB.promise CT.Unit.target @@ fun unit ->
30-
KB.collect Image.Spec.slot unit >>| request_arch >>| function
31-
| Some "avr" -> Target.atmega328
32-
| _ -> CT.Target.unknown
33-
342

353
let main _ctxt =
36-
enable_llvm ();
37-
enable_loader ();
38-
provide_decoding ();
39-
Avr_lifter.load ();
4+
Bap_avr_target.load ();
405
Ok ()
416

42-
(* semantic tags that describe what our plugin is providing,
43-
setting them is important not only for introspection but
44-
for the proper function of the cache subsystem.
45-
*)
46-
let provides = [
47-
"avr";
48-
"lifter";
49-
]
507

51-
(* finally, let's register our extension and call the main function *)
528
let () = Bap_main.Extension.declare main
53-
~provides
9+
~provides:[
10+
"avr";
11+
"lifter";
12+
]

0 commit comments

Comments
 (0)