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
use std::ops::DerefMut;
use std::result;
use std::vec::Vec;
use crate::common::SectionId;
use crate::write::{
DebugAbbrev, DebugFrame, DebugInfo, DebugInfoReference, DebugLine, DebugLineStr, DebugLoc,
DebugLocLists, DebugRanges, DebugRngLists, DebugStr, EhFrame, Writer,
};
macro_rules! define_section {
($name:ident, $offset:ident, $docs:expr) => {
#[doc=$docs]
#[derive(Debug, Default)]
pub struct $name<W: Writer>(pub W);
impl<W: Writer> $name<W> {
pub fn offset(&self) -> $offset {
$offset(self.len())
}
}
impl<W: Writer> From<W> for $name<W> {
#[inline]
fn from(w: W) -> Self {
$name(w)
}
}
impl<W: Writer> Deref for $name<W> {
type Target = W;
#[inline]
fn deref(&self) -> &W {
&self.0
}
}
impl<W: Writer> DerefMut for $name<W> {
#[inline]
fn deref_mut(&mut self) -> &mut W {
&mut self.0
}
}
impl<W: Writer> Section<W> for $name<W> {
#[inline]
fn id(&self) -> SectionId {
SectionId::$name
}
}
};
}
pub trait Section<W: Writer>: DerefMut<Target = W> {
fn id(&self) -> SectionId;
fn name(&self) -> &'static str {
self.id().name()
}
}
#[derive(Debug, Default)]
pub struct Sections<W: Writer> {
pub debug_abbrev: DebugAbbrev<W>,
pub debug_info: DebugInfo<W>,
pub debug_line: DebugLine<W>,
pub debug_line_str: DebugLineStr<W>,
pub debug_ranges: DebugRanges<W>,
pub debug_rnglists: DebugRngLists<W>,
pub debug_loc: DebugLoc<W>,
pub debug_loclists: DebugLocLists<W>,
pub debug_str: DebugStr<W>,
pub debug_frame: DebugFrame<W>,
pub eh_frame: EhFrame<W>,
pub(crate) debug_info_refs: Vec<DebugInfoReference>,
pub(crate) debug_loc_refs: Vec<DebugInfoReference>,
pub(crate) debug_loclists_refs: Vec<DebugInfoReference>,
}
impl<W: Writer + Clone> Sections<W> {
pub fn new(section: W) -> Self {
Sections {
debug_abbrev: DebugAbbrev(section.clone()),
debug_info: DebugInfo(section.clone()),
debug_line: DebugLine(section.clone()),
debug_line_str: DebugLineStr(section.clone()),
debug_ranges: DebugRanges(section.clone()),
debug_rnglists: DebugRngLists(section.clone()),
debug_loc: DebugLoc(section.clone()),
debug_loclists: DebugLocLists(section.clone()),
debug_str: DebugStr(section.clone()),
debug_frame: DebugFrame(section.clone()),
eh_frame: EhFrame(section.clone()),
debug_info_refs: Vec::new(),
debug_loc_refs: Vec::new(),
debug_loclists_refs: Vec::new(),
}
}
}
impl<W: Writer> Sections<W> {
pub fn for_each<F, E>(&self, mut f: F) -> result::Result<(), E>
where
F: FnMut(SectionId, &W) -> result::Result<(), E>,
{
macro_rules! f {
($s:expr) => {
f($s.id(), &$s)
};
}
f!(self.debug_abbrev)?;
f!(self.debug_str)?;
f!(self.debug_line_str)?;
f!(self.debug_line)?;
f!(self.debug_ranges)?;
f!(self.debug_rnglists)?;
f!(self.debug_loc)?;
f!(self.debug_loclists)?;
f!(self.debug_info)?;
f!(self.debug_frame)?;
f!(self.eh_frame)?;
Ok(())
}
pub fn for_each_mut<F, E>(&mut self, mut f: F) -> result::Result<(), E>
where
F: FnMut(SectionId, &mut W) -> result::Result<(), E>,
{
macro_rules! f {
($s:expr) => {
f($s.id(), &mut $s)
};
}
f!(self.debug_abbrev)?;
f!(self.debug_str)?;
f!(self.debug_line_str)?;
f!(self.debug_line)?;
f!(self.debug_ranges)?;
f!(self.debug_rnglists)?;
f!(self.debug_loc)?;
f!(self.debug_loclists)?;
f!(self.debug_info)?;
f!(self.debug_frame)?;
f!(self.eh_frame)?;
Ok(())
}
}