Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : use std::path::Path;
4 : use std::option::Option;
5 : use std::marker::PhantomData;
6 : use crate::{common::FromFFI, generic, common::into_optional};
7 :
8 : pub mod compilation_unit;
9 : pub mod function;
10 : pub mod variable;
11 : pub mod types;
12 :
13 : #[doc(inline)]
14 : pub use compilation_unit::CompilationUnit;
15 :
16 : #[doc(inline)]
17 : pub use types::Type;
18 :
19 : #[doc(inline)]
20 : pub use variable::Variable;
21 :
22 : #[doc(inline)]
23 : pub use function::Function;
24 :
25 : /// This structure exposes the main API to create DWARF information
26 : pub struct Editor<'a> {
27 : ptr: cxx::UniquePtr<ffi::DWARF_Editor>,
28 : _owner: PhantomData<&'a ()>,
29 : }
30 :
31 : impl FromFFI<ffi::DWARF_Editor> for Editor<'_> {
32 0 : fn from_ffi(ptr: cxx::UniquePtr<ffi::DWARF_Editor>) -> Self {
33 0 : Self {
34 0 : ptr,
35 0 : _owner: PhantomData,
36 0 : }
37 0 : }
38 : }
39 :
40 : #[allow(non_camel_case_types)]
41 0 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
42 : pub enum Format {
43 : ELF,
44 : MACHO,
45 : PE,
46 : UNKNOWN(u32),
47 : }
48 :
49 : impl From<u32> for Format {
50 0 : fn from(value: u32) -> Self {
51 0 : match value {
52 0 : 0x00000000 => Format::ELF,
53 0 : 0x00000001 => Format::MACHO,
54 0 : 0x00000002 => Format::PE,
55 0 : _ => Format::UNKNOWN(value),
56 :
57 : }
58 0 : }
59 : }
60 : impl From<Format> for u32 {
61 0 : fn from(value: Format) -> u32 {
62 0 : match value {
63 0 : Format::ELF => 0x00000000,
64 0 : Format::MACHO => 0x00000001,
65 0 : Format::PE => 0x00000002,
66 0 : Format::UNKNOWN(_) => 0,
67 :
68 : }
69 0 : }
70 : }
71 :
72 : #[allow(non_camel_case_types)]
73 0 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
74 : pub enum Arch {
75 : X64,
76 : X86,
77 : AARCH64,
78 : ARM,
79 : UNKNOWN(u32),
80 : }
81 :
82 : impl From<u32> for Arch {
83 0 : fn from(value: u32) -> Self {
84 0 : match value {
85 0 : 0x00000000 => Arch::X64,
86 0 : 0x00000001 => Arch::X86,
87 0 : 0x00000002 => Arch::AARCH64,
88 0 : 0x00000003 => Arch::ARM,
89 0 : _ => Arch::UNKNOWN(value),
90 :
91 : }
92 0 : }
93 : }
94 : impl From<Arch> for u32 {
95 0 : fn from(value: Arch) -> u32 {
96 0 : match value {
97 0 : Arch::X64 => 0x00000000,
98 0 : Arch::X86 => 0x00000001,
99 0 : Arch::AARCH64 => 0x00000002,
100 0 : Arch::ARM => 0x00000003,
101 0 : Arch::UNKNOWN(_) => 0,
102 :
103 : }
104 0 : }
105 : }
106 :
107 : impl<'a> Editor<'a> {
108 : /// Instantiate an editor for the given binary object
109 0 : pub fn from_binary(bin: &'a mut dyn generic::Binary) -> Option<Editor<'a>> {
110 0 : into_optional(ffi::DWARF_Editor::from_binary(bin.as_pin_mut_generic()))
111 0 : }
112 :
113 : /// Instantiate an editor for the given format and arch
114 0 : pub fn create(fmt: Format, arch: Arch) -> Option<Editor<'static>> {
115 0 : into_optional(ffi::DWARF_Editor::create(fmt.into(), arch.into()))
116 0 : }
117 :
118 : /// Create a new compilation unit
119 0 : pub fn create_compile_unit(&mut self) -> Option<CompilationUnit> {
120 0 : into_optional(self.ptr.pin_mut().create_compilation_unit())
121 0 : }
122 :
123 : /// Write the DWARF file to the specified output
124 0 : pub fn write<P: AsRef<Path>>(&mut self, output: P) {
125 0 : self.ptr.pin_mut().write(output.as_ref().to_str().unwrap())
126 0 : }
127 : }
128 :
|