Skip to main content

gimli/
common.rs

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