1 /// Whether the format of a compilation unit is 32- or 64-bit.
2 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3 pub enum Format {
4     /// 64-bit DWARF
5     Dwarf64 = 8,
6     /// 32-bit DWARF
7     Dwarf32 = 4,
8 }
9 
10 impl Format {
11     /// Return the serialized size of an initial length field for the format.
12     #[inline]
initial_length_size(self) -> u813     pub fn initial_length_size(self) -> u8 {
14         match self {
15             Format::Dwarf32 => 4,
16             Format::Dwarf64 => 12,
17         }
18     }
19 
20     /// Return the natural word size for the format
21     #[inline]
word_size(self) -> u822     pub fn word_size(self) -> u8 {
23         match self {
24             Format::Dwarf32 => 4,
25             Format::Dwarf64 => 8,
26         }
27     }
28 }
29 
30 /// Encoding parameters that are commonly used for multiple DWARF sections.
31 ///
32 /// This is intended to be small enough to pass by value.
33 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
34 // `address_size` and `format` are used more often than `version`, so keep
35 // them first.
36 #[repr(C)]
37 pub struct Encoding {
38     /// The size of an address.
39     pub address_size: u8,
40 
41     // The size of a segment selector.
42     // TODO: pub segment_size: u8,
43     /// Whether the DWARF format is 32- or 64-bit.
44     pub format: Format,
45 
46     /// The DWARF version of the header.
47     pub version: u16,
48 }
49 
50 /// Encoding parameters for a line number program.
51 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
52 pub struct LineEncoding {
53     /// The size in bytes of the smallest target machine instruction.
54     pub minimum_instruction_length: u8,
55 
56     /// The maximum number of individual operations that may be encoded in an
57     /// instruction.
58     pub maximum_operations_per_instruction: u8,
59 
60     /// The initial value of the `is_stmt` register.
61     pub default_is_stmt: bool,
62 
63     /// The minimum value which a special opcode can add to the line register.
64     pub line_base: i8,
65 
66     /// The range of values which a special opcode can add to the line register.
67     pub line_range: u8,
68 }
69 
70 impl Default for LineEncoding {
default() -> Self71     fn default() -> Self {
72         // Values from LLVM.
73         LineEncoding {
74             minimum_instruction_length: 1,
75             maximum_operations_per_instruction: 1,
76             default_is_stmt: true,
77             line_base: -5,
78             line_range: 14,
79         }
80     }
81 }
82 
83 /// A DWARF register number.
84 ///
85 /// The meaning of this value is ABI dependent. This is generally encoded as
86 /// a ULEB128, but supported architectures need 16 bits at most.
87 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
88 pub struct Register(pub u16);
89 
90 /// An offset into the `.debug_abbrev` section.
91 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
92 pub struct DebugAbbrevOffset<T = usize>(pub T);
93 
94 /// An offset to a set of entries in the `.debug_addr` section.
95 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
96 pub struct DebugAddrBase<T = usize>(pub T);
97 
98 /// An index into a set of addresses in the `.debug_addr` section.
99 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
100 pub struct DebugAddrIndex<T = usize>(pub T);
101 
102 /// An offset into the `.debug_aranges` section.
103 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
104 pub struct DebugArangesOffset<T = usize>(pub T);
105 
106 /// An offset into the `.debug_info` section.
107 #[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
108 pub struct DebugInfoOffset<T = usize>(pub T);
109 
110 /// An offset into the `.debug_line` section.
111 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
112 pub struct DebugLineOffset<T = usize>(pub T);
113 
114 /// An offset into the `.debug_line_str` section.
115 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
116 pub struct DebugLineStrOffset<T = usize>(pub T);
117 
118 /// An offset into either the `.debug_loc` section or the `.debug_loclists` section,
119 /// depending on the version of the unit the offset was contained in.
120 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
121 pub struct LocationListsOffset<T = usize>(pub T);
122 
123 /// An offset to a set of location list offsets in the `.debug_loclists` section.
124 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
125 pub struct DebugLocListsBase<T = usize>(pub T);
126 
127 /// An index into a set of location list offsets in the `.debug_loclists` section.
128 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
129 pub struct DebugLocListsIndex<T = usize>(pub T);
130 
131 /// An offset into the `.debug_macinfo` section.
132 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
133 pub struct DebugMacinfoOffset<T = usize>(pub T);
134 
135 /// An offset into the `.debug_macro` section.
136 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
137 pub struct DebugMacroOffset<T = usize>(pub T);
138 
139 /// An offset into either the `.debug_ranges` section or the `.debug_rnglists` section,
140 /// depending on the version of the unit the offset was contained in.
141 ///
142 /// If this is from a DWARF 4 DWO file, then it must additionally be offset by the
143 /// value of `DW_AT_GNU_ranges_base`. You can use `Dwarf::ranges_offset_from_raw` to do this.
144 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
145 pub struct RawRangeListsOffset<T = usize>(pub T);
146 
147 /// An offset into either the `.debug_ranges` section or the `.debug_rnglists` section,
148 /// depending on the version of the unit the offset was contained in.
149 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
150 pub struct RangeListsOffset<T = usize>(pub T);
151 
152 /// An offset to a set of range list offsets in the `.debug_rnglists` section.
153 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
154 pub struct DebugRngListsBase<T = usize>(pub T);
155 
156 /// An index into a set of range list offsets in the `.debug_rnglists` section.
157 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
158 pub struct DebugRngListsIndex<T = usize>(pub T);
159 
160 /// An offset into the `.debug_str` section.
161 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
162 pub struct DebugStrOffset<T = usize>(pub T);
163 
164 /// An offset to a set of entries in the `.debug_str_offsets` section.
165 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
166 pub struct DebugStrOffsetsBase<T = usize>(pub T);
167 
168 /// An index into a set of entries in the `.debug_str_offsets` section.
169 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
170 pub struct DebugStrOffsetsIndex<T = usize>(pub T);
171 
172 /// An offset into the `.debug_types` section.
173 #[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
174 pub struct DebugTypesOffset<T = usize>(pub T);
175 
176 /// A type signature as used in the `.debug_types` section.
177 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
178 pub struct DebugTypeSignature(pub u64);
179 
180 /// An offset into the `.debug_frame` section.
181 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
182 pub struct DebugFrameOffset<T = usize>(pub T);
183 
184 impl<T> From<T> for DebugFrameOffset<T> {
185     #[inline]
from(o: T) -> Self186     fn from(o: T) -> Self {
187         DebugFrameOffset(o)
188     }
189 }
190 
191 /// An offset into the `.eh_frame` section.
192 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
193 pub struct EhFrameOffset<T = usize>(pub T);
194 
195 impl<T> From<T> for EhFrameOffset<T> {
196     #[inline]
from(o: T) -> Self197     fn from(o: T) -> Self {
198         EhFrameOffset(o)
199     }
200 }
201 
202 /// An offset into the `.debug_info` or `.debug_types` sections.
203 #[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
204 pub enum UnitSectionOffset<T = usize> {
205     /// An offset into the `.debug_info` section.
206     DebugInfoOffset(DebugInfoOffset<T>),
207     /// An offset into the `.debug_types` section.
208     DebugTypesOffset(DebugTypesOffset<T>),
209 }
210 
211 impl<T> From<DebugInfoOffset<T>> for UnitSectionOffset<T> {
from(offset: DebugInfoOffset<T>) -> Self212     fn from(offset: DebugInfoOffset<T>) -> Self {
213         UnitSectionOffset::DebugInfoOffset(offset)
214     }
215 }
216 
217 impl<T> From<DebugTypesOffset<T>> for UnitSectionOffset<T> {
from(offset: DebugTypesOffset<T>) -> Self218     fn from(offset: DebugTypesOffset<T>) -> Self {
219         UnitSectionOffset::DebugTypesOffset(offset)
220     }
221 }
222 
223 impl<T> UnitSectionOffset<T>
224 where
225     T: Clone,
226 {
227     /// Returns the `DebugInfoOffset` inside, or `None` otherwise.
as_debug_info_offset(&self) -> Option<DebugInfoOffset<T>>228     pub fn as_debug_info_offset(&self) -> Option<DebugInfoOffset<T>> {
229         match self {
230             UnitSectionOffset::DebugInfoOffset(offset) => Some(offset.clone()),
231             UnitSectionOffset::DebugTypesOffset(_) => None,
232         }
233     }
234     /// Returns the `DebugTypesOffset` inside, or `None` otherwise.
as_debug_types_offset(&self) -> Option<DebugTypesOffset<T>>235     pub fn as_debug_types_offset(&self) -> Option<DebugTypesOffset<T>> {
236         match self {
237             UnitSectionOffset::DebugInfoOffset(_) => None,
238             UnitSectionOffset::DebugTypesOffset(offset) => Some(offset.clone()),
239         }
240     }
241 }
242 
243 /// An identifier for a DWARF section.
244 #[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
245 pub enum SectionId {
246     /// The `.debug_abbrev` section.
247     DebugAbbrev,
248     /// The `.debug_addr` section.
249     DebugAddr,
250     /// The `.debug_aranges` section.
251     DebugAranges,
252     /// The `.debug_cu_index` section.
253     DebugCuIndex,
254     /// The `.debug_frame` section.
255     DebugFrame,
256     /// The `.eh_frame` section.
257     EhFrame,
258     /// The `.eh_frame_hdr` section.
259     EhFrameHdr,
260     /// The `.debug_info` section.
261     DebugInfo,
262     /// The `.debug_line` section.
263     DebugLine,
264     /// The `.debug_line_str` section.
265     DebugLineStr,
266     /// The `.debug_loc` section.
267     DebugLoc,
268     /// The `.debug_loclists` section.
269     DebugLocLists,
270     /// The `.debug_macinfo` section.
271     DebugMacinfo,
272     /// The `.debug_macro` section.
273     DebugMacro,
274     /// The `.debug_pubnames` section.
275     DebugPubNames,
276     /// The `.debug_pubtypes` section.
277     DebugPubTypes,
278     /// The `.debug_ranges` section.
279     DebugRanges,
280     /// The `.debug_rnglists` section.
281     DebugRngLists,
282     /// The `.debug_str` section.
283     DebugStr,
284     /// The `.debug_str_offsets` section.
285     DebugStrOffsets,
286     /// The `.debug_tu_index` section.
287     DebugTuIndex,
288     /// The `.debug_types` section.
289     DebugTypes,
290 }
291 
292 impl SectionId {
293     /// Returns the ELF section name for this kind.
name(self) -> &'static str294     pub fn name(self) -> &'static str {
295         match self {
296             SectionId::DebugAbbrev => ".debug_abbrev",
297             SectionId::DebugAddr => ".debug_addr",
298             SectionId::DebugAranges => ".debug_aranges",
299             SectionId::DebugCuIndex => ".debug_cu_index",
300             SectionId::DebugFrame => ".debug_frame",
301             SectionId::EhFrame => ".eh_frame",
302             SectionId::EhFrameHdr => ".eh_frame_hdr",
303             SectionId::DebugInfo => ".debug_info",
304             SectionId::DebugLine => ".debug_line",
305             SectionId::DebugLineStr => ".debug_line_str",
306             SectionId::DebugLoc => ".debug_loc",
307             SectionId::DebugLocLists => ".debug_loclists",
308             SectionId::DebugMacinfo => ".debug_macinfo",
309             SectionId::DebugMacro => ".debug_macro",
310             SectionId::DebugPubNames => ".debug_pubnames",
311             SectionId::DebugPubTypes => ".debug_pubtypes",
312             SectionId::DebugRanges => ".debug_ranges",
313             SectionId::DebugRngLists => ".debug_rnglists",
314             SectionId::DebugStr => ".debug_str",
315             SectionId::DebugStrOffsets => ".debug_str_offsets",
316             SectionId::DebugTuIndex => ".debug_tu_index",
317             SectionId::DebugTypes => ".debug_types",
318         }
319     }
320 
321     /// Returns the ELF section name for this kind, when found in a .dwo or .dwp file.
dwo_name(self) -> Option<&'static str>322     pub fn dwo_name(self) -> Option<&'static str> {
323         Some(match self {
324             SectionId::DebugAbbrev => ".debug_abbrev.dwo",
325             SectionId::DebugCuIndex => ".debug_cu_index",
326             SectionId::DebugInfo => ".debug_info.dwo",
327             SectionId::DebugLine => ".debug_line.dwo",
328             // The debug_loc section can be present in the dwo when using the
329             // GNU split-dwarf extension to DWARF4.
330             SectionId::DebugLoc => ".debug_loc.dwo",
331             SectionId::DebugLocLists => ".debug_loclists.dwo",
332             SectionId::DebugMacro => ".debug_macro.dwo",
333             SectionId::DebugRngLists => ".debug_rnglists.dwo",
334             SectionId::DebugStr => ".debug_str.dwo",
335             SectionId::DebugStrOffsets => ".debug_str_offsets.dwo",
336             SectionId::DebugTuIndex => ".debug_tu_index",
337             SectionId::DebugTypes => ".debug_types.dwo",
338             _ => return None,
339         })
340     }
341 }
342 
343 /// An optionally-provided implementation-defined compilation unit ID to enable
344 /// split DWARF and linking a split compilation unit back together.
345 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
346 pub struct DwoId(pub u64);
347 
348 /// The "type" of file with DWARF debugging information. This determines, among other things,
349 /// which files DWARF sections should be loaded from.
350 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
351 pub enum DwarfFileType {
352     /// A normal executable or object file.
353     Main,
354     /// A .dwo split DWARF file.
355     Dwo,
356     // TODO: Supplementary files, .dwps?
357 }
358 
359 impl Default for DwarfFileType {
default() -> Self360     fn default() -> Self {
361         DwarfFileType::Main
362     }
363 }
364