Skip to content

Commit 1b8a280

Browse files
authored
read: Introduce new enum for section IDs in a .dwp index (gimli-rs#716)
Keeping this type distinct from the `SectionId` enum ensures that the compiler will report an error if someone adds incomplete support for new split DWARF sections. The existing `SectionId::dwo_name` method did not match the list of sections actually supported elsewhere in gimli. I've added the one section ID that was missing (`.debug_macinfo.dwo`), but I have not removed the section IDs which are only present there: `.debug_str.dwo`, `.debug_cu_index`, and `.debug_tu_index`.
1 parent e095972 commit 1b8a280

File tree

5 files changed

+109
-67
lines changed

5 files changed

+109
-67
lines changed

crates/examples/src/bin/dwarfdump.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ where
968968
writeln!(
969969
w,
970970
" {}: offset = 0x{:x}, size = 0x{:x}",
971-
section.section.dwo_name().unwrap(),
971+
section.section.dwo_name(),
972972
section.offset,
973973
section.size
974974
)?;

src/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ impl SectionId {
339339
// GNU split-dwarf extension to DWARF4.
340340
SectionId::DebugLoc => ".debug_loc.dwo",
341341
SectionId::DebugLocLists => ".debug_loclists.dwo",
342+
SectionId::DebugMacinfo => ".debug_macinfo.dwo",
342343
SectionId::DebugMacro => ".debug_macro.dwo",
343344
SectionId::DebugRngLists => ".debug_rnglists.dwo",
344345
SectionId::DebugStr => ".debug_str.dwo",

src/read/dwarf.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use crate::read::{
1313
DebugAddr, DebugAranges, DebugCuIndex, DebugInfo, DebugInfoUnitHeadersIter, DebugLine,
1414
DebugLineStr, DebugLoc, DebugLocLists, DebugRanges, DebugRngLists, DebugStr, DebugStrOffsets,
1515
DebugTuIndex, DebugTypes, DebugTypesUnitHeadersIter, DebuggingInformationEntry, EntriesCursor,
16-
EntriesRaw, EntriesTree, Error, IncompleteLineProgram, LocListIter, LocationLists, Range,
17-
RangeLists, RawLocListIter, RawRngListIter, Reader, ReaderOffset, ReaderOffsetId, Result,
18-
RngListIter, Section, UnitHeader, UnitIndex, UnitIndexSectionIterator, UnitOffset, UnitType,
16+
EntriesRaw, EntriesTree, Error, IncompleteLineProgram, IndexSectionId, LocListIter,
17+
LocationLists, Range, RangeLists, RawLocListIter, RawRngListIter, Reader, ReaderOffset,
18+
ReaderOffsetId, Result, RngListIter, Section, UnitHeader, UnitIndex, UnitIndexSectionIterator,
19+
UnitOffset, UnitType,
1920
};
2021

2122
/// All of the commonly used DWARF sections.
@@ -1013,42 +1014,41 @@ impl<R: Reader> DwarfPackage<R> {
10131014
let mut types_size = 0;
10141015
for section in sections {
10151016
match section.section {
1016-
SectionId::DebugAbbrev => {
1017+
IndexSectionId::DebugAbbrev => {
10171018
abbrev_offset = section.offset;
10181019
abbrev_size = section.size;
10191020
}
1020-
SectionId::DebugInfo => {
1021+
IndexSectionId::DebugInfo => {
10211022
info_offset = section.offset;
10221023
info_size = section.size;
10231024
}
1024-
SectionId::DebugLine => {
1025+
IndexSectionId::DebugLine => {
10251026
line_offset = section.offset;
10261027
line_size = section.size;
10271028
}
1028-
SectionId::DebugLoc => {
1029+
IndexSectionId::DebugLoc => {
10291030
loc_offset = section.offset;
10301031
loc_size = section.size;
10311032
}
1032-
SectionId::DebugLocLists => {
1033+
IndexSectionId::DebugLocLists => {
10331034
loclists_offset = section.offset;
10341035
loclists_size = section.size;
10351036
}
1036-
SectionId::DebugStrOffsets => {
1037+
IndexSectionId::DebugStrOffsets => {
10371038
str_offsets_offset = section.offset;
10381039
str_offsets_size = section.size;
10391040
}
1040-
SectionId::DebugRngLists => {
1041+
IndexSectionId::DebugRngLists => {
10411042
rnglists_offset = section.offset;
10421043
rnglists_size = section.size;
10431044
}
1044-
SectionId::DebugTypes => {
1045+
IndexSectionId::DebugTypes => {
10451046
types_offset = section.offset;
10461047
types_size = section.size;
10471048
}
1048-
SectionId::DebugMacro | SectionId::DebugMacinfo => {
1049+
IndexSectionId::DebugMacro | IndexSectionId::DebugMacinfo => {
10491050
// These are valid but we can't parse these yet.
10501051
}
1051-
_ => return Err(Error::UnknownSection(section.section)),
10521052
}
10531053
}
10541054

src/read/index.rs

Lines changed: 94 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub struct UnitIndex<R: Reader> {
129129
hash_ids: R,
130130
hash_rows: R,
131131
// Only `section_count` values are valid.
132-
sections: [SectionId; SECTION_COUNT_MAX as usize],
132+
sections: [IndexSectionId; SECTION_COUNT_MAX as usize],
133133
offsets: R,
134134
sizes: R,
135135
}
@@ -144,7 +144,7 @@ impl<R: Reader> UnitIndex<R> {
144144
slot_count: 0,
145145
hash_ids: input.clone(),
146146
hash_rows: input.clone(),
147-
sections: [SectionId::DebugAbbrev; SECTION_COUNT_MAX as usize],
147+
sections: [IndexSectionId::DebugAbbrev; SECTION_COUNT_MAX as usize],
148148
offsets: input.clone(),
149149
sizes: input.clone(),
150150
});
@@ -173,33 +173,33 @@ impl<R: Reader> UnitIndex<R> {
173173
let hash_ids = input.split(R::Offset::from_u64(u64::from(slot_count) * 8)?)?;
174174
let hash_rows = input.split(R::Offset::from_u64(u64::from(slot_count) * 4)?)?;
175175

176-
let mut sections = [SectionId::DebugAbbrev; SECTION_COUNT_MAX as usize];
176+
let mut sections = [IndexSectionId::DebugAbbrev; SECTION_COUNT_MAX as usize];
177177
if section_count > SECTION_COUNT_MAX.into() {
178178
return Err(Error::InvalidIndexSectionCount);
179179
}
180180
for i in 0..section_count {
181181
let section = input.read_u32()?;
182182
sections[i as usize] = if version == 2 {
183183
match constants::DwSectV2(section) {
184-
constants::DW_SECT_V2_INFO => SectionId::DebugInfo,
185-
constants::DW_SECT_V2_TYPES => SectionId::DebugTypes,
186-
constants::DW_SECT_V2_ABBREV => SectionId::DebugAbbrev,
187-
constants::DW_SECT_V2_LINE => SectionId::DebugLine,
188-
constants::DW_SECT_V2_LOC => SectionId::DebugLoc,
189-
constants::DW_SECT_V2_STR_OFFSETS => SectionId::DebugStrOffsets,
190-
constants::DW_SECT_V2_MACINFO => SectionId::DebugMacinfo,
191-
constants::DW_SECT_V2_MACRO => SectionId::DebugMacro,
184+
constants::DW_SECT_V2_INFO => IndexSectionId::DebugInfo,
185+
constants::DW_SECT_V2_TYPES => IndexSectionId::DebugTypes,
186+
constants::DW_SECT_V2_ABBREV => IndexSectionId::DebugAbbrev,
187+
constants::DW_SECT_V2_LINE => IndexSectionId::DebugLine,
188+
constants::DW_SECT_V2_LOC => IndexSectionId::DebugLoc,
189+
constants::DW_SECT_V2_STR_OFFSETS => IndexSectionId::DebugStrOffsets,
190+
constants::DW_SECT_V2_MACINFO => IndexSectionId::DebugMacinfo,
191+
constants::DW_SECT_V2_MACRO => IndexSectionId::DebugMacro,
192192
section => return Err(Error::UnknownIndexSectionV2(section)),
193193
}
194194
} else {
195195
match constants::DwSect(section) {
196-
constants::DW_SECT_INFO => SectionId::DebugInfo,
197-
constants::DW_SECT_ABBREV => SectionId::DebugAbbrev,
198-
constants::DW_SECT_LINE => SectionId::DebugLine,
199-
constants::DW_SECT_LOCLISTS => SectionId::DebugLocLists,
200-
constants::DW_SECT_STR_OFFSETS => SectionId::DebugStrOffsets,
201-
constants::DW_SECT_MACRO => SectionId::DebugMacro,
202-
constants::DW_SECT_RNGLISTS => SectionId::DebugRngLists,
196+
constants::DW_SECT_INFO => IndexSectionId::DebugInfo,
197+
constants::DW_SECT_ABBREV => IndexSectionId::DebugAbbrev,
198+
constants::DW_SECT_LINE => IndexSectionId::DebugLine,
199+
constants::DW_SECT_LOCLISTS => IndexSectionId::DebugLocLists,
200+
constants::DW_SECT_STR_OFFSETS => IndexSectionId::DebugStrOffsets,
201+
constants::DW_SECT_MACRO => IndexSectionId::DebugMacro,
202+
constants::DW_SECT_RNGLISTS => IndexSectionId::DebugRngLists,
203203
section => return Err(Error::UnknownIndexSection(section)),
204204
}
205205
};
@@ -305,7 +305,7 @@ impl<R: Reader> UnitIndex<R> {
305305
/// An iterator over the section offsets and sizes for a row in a `UnitIndex`.
306306
#[derive(Debug, Clone)]
307307
pub struct UnitIndexSectionIterator<'index, R: Reader> {
308-
sections: slice::Iter<'index, SectionId>,
308+
sections: slice::Iter<'index, IndexSectionId>,
309309
offsets: R,
310310
sizes: R,
311311
}
@@ -330,13 +330,57 @@ impl<'index, R: Reader> Iterator for UnitIndexSectionIterator<'index, R> {
330330
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
331331
pub struct UnitIndexSection {
332332
/// The section kind.
333-
pub section: SectionId,
333+
pub section: IndexSectionId,
334334
/// The base offset of the unit's contribution to the section.
335335
pub offset: u32,
336336
/// The size of the unit's contribution to the section.
337337
pub size: u32,
338338
}
339339

340+
/// Section kinds which are permitted in a `.dwp` index.
341+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
342+
pub enum IndexSectionId {
343+
/// The `.debug_abbrev.dwo` section.
344+
DebugAbbrev,
345+
/// The `.debug_info.dwo` section.
346+
DebugInfo,
347+
/// The `.debug_line.dwo` section.
348+
DebugLine,
349+
/// The `.debug_loc.dwo` section.
350+
DebugLoc,
351+
/// The `.debug_loclists.dwo` section.
352+
DebugLocLists,
353+
/// The `.debug_macinfo.dwo` section.
354+
DebugMacinfo,
355+
/// The `.debug_macro.dwo` section.
356+
DebugMacro,
357+
/// The `.debug_rnglists.dwo` section.
358+
DebugRngLists,
359+
/// The `.debug_str_offsets.dwo` section.
360+
DebugStrOffsets,
361+
/// The `.debug_types.dwo` section.
362+
DebugTypes,
363+
}
364+
365+
impl IndexSectionId {
366+
/// Returns the ELF section name for this kind, when found in a .dwo or .dwp file.
367+
pub fn dwo_name(self) -> &'static str {
368+
let section_id = match self {
369+
IndexSectionId::DebugAbbrev => SectionId::DebugAbbrev,
370+
IndexSectionId::DebugInfo => SectionId::DebugInfo,
371+
IndexSectionId::DebugLine => SectionId::DebugLine,
372+
IndexSectionId::DebugLoc => SectionId::DebugLoc,
373+
IndexSectionId::DebugLocLists => SectionId::DebugLocLists,
374+
IndexSectionId::DebugMacro => SectionId::DebugMacro,
375+
IndexSectionId::DebugMacinfo => SectionId::DebugMacinfo,
376+
IndexSectionId::DebugRngLists => SectionId::DebugRngLists,
377+
IndexSectionId::DebugStrOffsets => SectionId::DebugStrOffsets,
378+
IndexSectionId::DebugTypes => SectionId::DebugTypes,
379+
};
380+
section_id.dwo_name().unwrap()
381+
}
382+
}
383+
340384
#[cfg(test)]
341385
mod tests {
342386
use super::*;
@@ -437,26 +481,26 @@ mod tests {
437481
assert_eq!(
438482
index.sections,
439483
[
440-
SectionId::DebugInfo,
441-
SectionId::DebugTypes,
442-
SectionId::DebugAbbrev,
443-
SectionId::DebugLine,
444-
SectionId::DebugLoc,
445-
SectionId::DebugStrOffsets,
446-
SectionId::DebugMacinfo,
447-
SectionId::DebugMacro,
484+
IndexSectionId::DebugInfo,
485+
IndexSectionId::DebugTypes,
486+
IndexSectionId::DebugAbbrev,
487+
IndexSectionId::DebugLine,
488+
IndexSectionId::DebugLoc,
489+
IndexSectionId::DebugStrOffsets,
490+
IndexSectionId::DebugMacinfo,
491+
IndexSectionId::DebugMacro,
448492
]
449493
);
450494
#[rustfmt::skip]
451495
let expect = [
452-
UnitIndexSection { section: SectionId::DebugInfo, offset: 11, size: 21 },
453-
UnitIndexSection { section: SectionId::DebugTypes, offset: 12, size: 22 },
454-
UnitIndexSection { section: SectionId::DebugAbbrev, offset: 13, size: 23 },
455-
UnitIndexSection { section: SectionId::DebugLine, offset: 14, size: 24 },
456-
UnitIndexSection { section: SectionId::DebugLoc, offset: 15, size: 25 },
457-
UnitIndexSection { section: SectionId::DebugStrOffsets, offset: 16, size: 26 },
458-
UnitIndexSection { section: SectionId::DebugMacinfo, offset: 17, size: 27 },
459-
UnitIndexSection { section: SectionId::DebugMacro, offset: 18, size: 28 },
496+
UnitIndexSection { section: IndexSectionId::DebugInfo, offset: 11, size: 21 },
497+
UnitIndexSection { section: IndexSectionId::DebugTypes, offset: 12, size: 22 },
498+
UnitIndexSection { section: IndexSectionId::DebugAbbrev, offset: 13, size: 23 },
499+
UnitIndexSection { section: IndexSectionId::DebugLine, offset: 14, size: 24 },
500+
UnitIndexSection { section: IndexSectionId::DebugLoc, offset: 15, size: 25 },
501+
UnitIndexSection { section: IndexSectionId::DebugStrOffsets, offset: 16, size: 26 },
502+
UnitIndexSection { section: IndexSectionId::DebugMacinfo, offset: 17, size: 27 },
503+
UnitIndexSection { section: IndexSectionId::DebugMacro, offset: 18, size: 28 },
460504
];
461505
let mut sections = index.sections(1).unwrap();
462506
for section in &expect {
@@ -492,24 +536,24 @@ mod tests {
492536
assert_eq!(
493537
index.sections[..7],
494538
[
495-
SectionId::DebugInfo,
496-
SectionId::DebugAbbrev,
497-
SectionId::DebugLine,
498-
SectionId::DebugLocLists,
499-
SectionId::DebugStrOffsets,
500-
SectionId::DebugMacro,
501-
SectionId::DebugRngLists,
539+
IndexSectionId::DebugInfo,
540+
IndexSectionId::DebugAbbrev,
541+
IndexSectionId::DebugLine,
542+
IndexSectionId::DebugLocLists,
543+
IndexSectionId::DebugStrOffsets,
544+
IndexSectionId::DebugMacro,
545+
IndexSectionId::DebugRngLists,
502546
]
503547
);
504548
#[rustfmt::skip]
505549
let expect = [
506-
UnitIndexSection { section: SectionId::DebugInfo, offset: 11, size: 21 },
507-
UnitIndexSection { section: SectionId::DebugAbbrev, offset: 12, size: 22 },
508-
UnitIndexSection { section: SectionId::DebugLine, offset: 13, size: 23 },
509-
UnitIndexSection { section: SectionId::DebugLocLists, offset: 14, size: 24 },
510-
UnitIndexSection { section: SectionId::DebugStrOffsets, offset: 15, size: 25 },
511-
UnitIndexSection { section: SectionId::DebugMacro, offset: 16, size: 26 },
512-
UnitIndexSection { section: SectionId::DebugRngLists, offset: 17, size: 27 },
550+
UnitIndexSection { section: IndexSectionId::DebugInfo, offset: 11, size: 21 },
551+
UnitIndexSection { section: IndexSectionId::DebugAbbrev, offset: 12, size: 22 },
552+
UnitIndexSection { section: IndexSectionId::DebugLine, offset: 13, size: 23 },
553+
UnitIndexSection { section: IndexSectionId::DebugLocLists, offset: 14, size: 24 },
554+
UnitIndexSection { section: IndexSectionId::DebugStrOffsets, offset: 15, size: 25 },
555+
UnitIndexSection { section: IndexSectionId::DebugMacro, offset: 16, size: 26 },
556+
UnitIndexSection { section: IndexSectionId::DebugRngLists, offset: 17, size: 27 },
513557
];
514558
let mut sections = index.sections(1).unwrap();
515559
for section in &expect {

src/read/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,6 @@ pub enum Error {
438438
InvalidIndexSlotCount,
439439
/// Invalid hash row in `.dwp` index.
440440
InvalidIndexRow,
441-
/// Unknown section type.
442-
UnknownSection(SectionId),
443441
/// Unknown section type in `.dwp` index.
444442
UnknownIndexSection(constants::DwSect),
445443
/// Unknown section type in version 2 `.dwp` index.
@@ -591,7 +589,6 @@ impl Error {
591589
Error::InvalidIndexSectionCount => "Invalid section count in `.dwp` index.",
592590
Error::InvalidIndexSlotCount => "Invalid slot count in `.dwp` index.",
593591
Error::InvalidIndexRow => "Invalid hash row in `.dwp` index.",
594-
Error::UnknownSection(_) => "Unknown section type.",
595592
Error::UnknownIndexSection(_) => "Unknown section type in `.dwp` index.",
596593
Error::UnknownIndexSectionV2(_) => "Unknown section type in version 2 `.dwp` index.",
597594
}

0 commit comments

Comments
 (0)