Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : use crate::{common::into_optional, common::FromFFI, generic};
4 : use std::marker::PhantomData;
5 : use std::option::Option;
6 : use std::path::Path;
7 :
8 : pub mod compilation_unit;
9 : pub mod function;
10 : pub mod types;
11 : pub mod variable;
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 0 : }
58 : }
59 : impl From<Format> for u32 {
60 0 : fn from(value: Format) -> u32 {
61 0 : match value {
62 0 : Format::ELF => 0x00000000,
63 0 : Format::MACHO => 0x00000001,
64 0 : Format::PE => 0x00000002,
65 0 : Format::UNKNOWN(_) => 0,
66 : }
67 0 : }
68 : }
69 :
70 : #[allow(non_camel_case_types)]
71 0 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
72 : pub enum Arch {
73 : X64,
74 : X86,
75 : AARCH64,
76 : ARM,
77 : UNKNOWN(u32),
78 : }
79 :
80 : impl From<u32> for Arch {
81 0 : fn from(value: u32) -> Self {
82 0 : match value {
83 0 : 0x00000001 => Arch::X64,
84 0 : 0x00000002 => Arch::X86,
85 0 : 0x00000003 => Arch::AARCH64,
86 0 : 0x00000004 => Arch::ARM,
87 0 : _ => Arch::UNKNOWN(value),
88 : }
89 0 : }
90 : }
91 : impl From<Arch> for u32 {
92 0 : fn from(value: Arch) -> u32 {
93 0 : match value {
94 0 : Arch::X64 => 0x00000001,
95 0 : Arch::X86 => 0x00000002,
96 0 : Arch::AARCH64 => 0x00000003,
97 0 : Arch::ARM => 0x00000004,
98 0 : Arch::UNKNOWN(_) => 0,
99 : }
100 0 : }
101 : }
102 :
103 : impl<'a> Editor<'a> {
104 : /// Instantiate an editor for the given binary object
105 0 : pub fn from_binary(bin: &'a mut dyn generic::Binary) -> Option<Editor<'a>> {
106 0 : into_optional(ffi::DWARF_Editor::from_binary(bin.as_pin_mut_generic()))
107 0 : }
108 :
109 : /// Instantiate an editor for the given format and arch
110 0 : pub fn create(fmt: Format, arch: Arch) -> Option<Editor<'static>> {
111 0 : into_optional(ffi::DWARF_Editor::create(fmt.into(), arch.into()))
112 0 : }
113 :
114 : /// Create a new compilation unit
115 0 : pub fn create_compile_unit(&mut self) -> Option<CompilationUnit<'_>> {
116 0 : into_optional(self.ptr.pin_mut().create_compilation_unit())
117 0 : }
118 :
119 : /// Write the DWARF file to the specified output
120 0 : pub fn write<P: AsRef<Path>>(&mut self, output: P) {
121 0 : self.ptr.pin_mut().write(output.as_ref().to_str().unwrap())
122 0 : }
123 : }
|