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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use goblin::{elf, mach};
use object::{Object, ObjectTarget};
pub trait DwarfData {
fn has_dwarf_data(&self) -> bool;
fn get_dwarf_section<'input>(
&'input self,
section: DwarfSection,
) -> Option<DwarfSectionData<'input>>;
}
impl<'input> DwarfData for Object<'input> {
fn has_dwarf_data(&self) -> bool {
match self.target {
ObjectTarget::Elf(ref elf) => has_elf_section(elf, DwarfSection::DebugInfo),
ObjectTarget::MachOSingle(ref macho) => has_macho_segment(macho, "__DWARF"),
ObjectTarget::MachOFat(_, ref macho) => has_macho_segment(macho, "__DWARF"),
_ => false,
}
}
fn get_dwarf_section<'data>(
&'data self,
section: DwarfSection,
) -> Option<DwarfSectionData<'data>> {
match self.target {
ObjectTarget::Elf(ref elf) => read_elf_dwarf_section(elf, self.as_bytes(), section),
ObjectTarget::MachOSingle(ref macho) => read_macho_dwarf_section(macho, section),
ObjectTarget::MachOFat(_, ref macho) => read_macho_dwarf_section(macho, section),
_ => None,
}
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
pub enum DwarfSection {
EhFrame,
DebugFrame,
DebugAbbrev,
DebugAranges,
DebugLine,
DebugLoc,
DebugPubNames,
DebugRanges,
DebugStr,
DebugInfo,
DebugTypes,
}
impl DwarfSection {
pub fn elf_name(&self) -> &'static str {
match *self {
DwarfSection::EhFrame => ".eh_frame",
DwarfSection::DebugFrame => ".debug_frame",
DwarfSection::DebugAbbrev => ".debug_abbrev",
DwarfSection::DebugAranges => ".debug_aranges",
DwarfSection::DebugLine => ".debug_line",
DwarfSection::DebugLoc => ".debug_loc",
DwarfSection::DebugPubNames => ".debug_pubnames",
DwarfSection::DebugRanges => ".debug_ranges",
DwarfSection::DebugStr => ".debug_str",
DwarfSection::DebugInfo => ".debug_info",
DwarfSection::DebugTypes => ".debug_types",
}
}
pub fn macho_name(&self) -> &'static str {
match *self {
DwarfSection::EhFrame => "__eh_frame",
DwarfSection::DebugFrame => "__debug_frame",
DwarfSection::DebugAbbrev => "__debug_abbrev",
DwarfSection::DebugAranges => "__debug_aranges",
DwarfSection::DebugLine => "__debug_line",
DwarfSection::DebugLoc => "__debug_loc",
DwarfSection::DebugPubNames => "__debug_pubnames",
DwarfSection::DebugRanges => "__debug_ranges",
DwarfSection::DebugStr => "__debug_str",
DwarfSection::DebugInfo => "__debug_info",
DwarfSection::DebugTypes => "__debug_types",
}
}
pub fn name(&self) -> &'static str {
match *self {
DwarfSection::EhFrame => "eh_frame",
DwarfSection::DebugFrame => "debug_frame",
DwarfSection::DebugAbbrev => "debug_abbrev",
DwarfSection::DebugAranges => "debug_aranges",
DwarfSection::DebugLine => "debug_line",
DwarfSection::DebugLoc => "debug_loc",
DwarfSection::DebugPubNames => "debug_pubnames",
DwarfSection::DebugRanges => "debug_ranges",
DwarfSection::DebugStr => "debug_str",
DwarfSection::DebugInfo => "debug_info",
DwarfSection::DebugTypes => "debug_types",
}
}
}
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct DwarfSectionData<'data> {
section: DwarfSection,
data: &'data [u8],
offset: u64,
}
impl<'data> DwarfSectionData<'data> {
pub fn new(section: DwarfSection, data: &'data [u8], offset: u64) -> DwarfSectionData<'data> {
DwarfSectionData {
section: section,
data: data,
offset: offset,
}
}
pub fn as_bytes(&self) -> &'data [u8] {
self.data
}
pub fn offset(&self) -> u64 {
self.offset
}
pub fn section(&self) -> DwarfSection {
self.section
}
}
fn read_elf_dwarf_section<'data>(
elf: &elf::Elf<'data>,
data: &'data [u8],
sect: DwarfSection,
) -> Option<DwarfSectionData<'data>> {
let section_name = sect.elf_name();
for header in &elf.section_headers {
if let Some(Ok(name)) = elf.shdr_strtab.get(header.sh_name) {
if name == section_name {
let sec_data = &data[header.sh_offset as usize..][..header.sh_size as usize];
return Some(DwarfSectionData::new(sect, sec_data, header.sh_offset));
}
}
}
None
}
fn read_macho_dwarf_section<'data>(
macho: &mach::MachO<'data>,
sect: DwarfSection,
) -> Option<DwarfSectionData<'data>> {
let dwarf_segment = if sect == DwarfSection::EhFrame {
"__TEXT"
} else {
"__DWARF"
};
let dwarf_section_name = sect.macho_name();
for segment in &macho.segments {
if_chain! {
if let Ok(seg) = segment.name();
if dwarf_segment == seg;
then {
for section in segment {
if_chain! {
if let Ok((section, data)) = section;
if let Ok(name) = section.name();
if name == dwarf_section_name;
then {
return Some(DwarfSectionData::new(sect, data, section.offset as u64));
}
}
}
}
}
}
None
}
fn has_elf_section(elf: &elf::Elf, section: DwarfSection) -> bool {
for header in &elf.section_headers {
if let Some(Ok(name)) = elf.shdr_strtab.get(header.sh_name) {
if name == section.elf_name() {
return true;
}
}
}
false
}
fn has_macho_segment(macho: &mach::MachO, name: &str) -> bool {
for segment in &macho.segments {
if segment.name().map(|seg| seg == name).unwrap_or(false) {
return true;
}
}
false
}