Repository: gimli-rs/addr2line
Pull request #215 (2021-05-02 03:27:08)

Update object/gimli deps


Patch:
diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
index e1e3250..915c021 100644
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -53,6 +53,19 @@ jobs:
           rustup default nightly
       - run: cargo bench
 
+  rustfmt:
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v2
+      - name: Install rustup
+        run: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile=minimal
+      - name: Install rust
+        run: |
+          rustup install stable
+          rustup default stable
+          rustup component add rustfmt
+      - run: cargo fmt --all -- --check
+
   coverage:
     runs-on: ubuntu-latest
     steps:
diff --git a/Cargo.toml b/Cargo.toml
index 30babbf..64f5b54 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -15,9 +15,9 @@ exclude = ["/benches/*", "/fixtures/*"]
 travis-ci = { repository = "gimli-rs/addr2line" }
 
 [dependencies]
-gimli = { version = "0.23", default-features = false, features = ["read"] }
+gimli = { version = "0.24", default-features = false, features = ["read"] }
 fallible-iterator = { version = "0.2", default-features = false, optional = true }
-object = { version = "0.23", default-features = false, features = ["read"], optional = true }
+object = { version = "0.24", default-features = false, features = ["read"], optional = true }
 smallvec = { version = "1", default-features = false, optional = true }
 rustc-demangle = { version = "0.1", optional = true }
 cpp_demangle = { version = "0.3", default-features = false, optional = true }
diff --git a/benches/bench.rs b/benches/bench.rs
index b2d828b..18b57b8 100644
--- a/benches/bench.rs
+++ b/benches/bench.rs
@@ -36,8 +36,7 @@ fn dwarf_load<'a>(object: &object::File<'a>) -> gimli::Dwarf<Cow<'a, [u8]>> {
             .map(|section| section.uncompressed_data().unwrap())
             .unwrap_or(Cow::Borrowed(&[][..])))
     };
-    let load_section_sup = |_| Ok(Cow::Borrowed(&[][..]));
-    gimli::Dwarf::load(&load_section, &load_section_sup).unwrap()
+    gimli::Dwarf::load(&load_section).unwrap()
 }
 
 fn dwarf_borrow<'a>(
diff --git a/examples/addr2line.rs b/examples/addr2line.rs
index 1e0f5a6..4b228a7 100644
--- a/examples/addr2line.rs
+++ b/examples/addr2line.rs
@@ -10,7 +10,6 @@ use std::borrow::Cow;
 use std::fs::File;
 use std::io::{BufRead, Lines, StdinLock, Write};
 use std::path::Path;
-use std::result;
 
 use clap::{App, Arg, Values};
 use fallible_iterator::FallibleIterator;
@@ -38,7 +37,7 @@ impl<'a> Iterator for Addrs<'a> {
     fn next(&mut self) -> Option<u64> {
         let text = match *self {
             Addrs::Args(ref mut vals) => vals.next().map(Cow::from),
-            Addrs::Stdin(ref mut lines) => lines.next().map(result::Result::unwrap).map(Cow::from),
+            Addrs::Stdin(ref mut lines) => lines.next().map(Result::unwrap).map(Cow::from),
         };
         text.as_ref()
             .map(Cow::as_ref)
@@ -201,24 +200,17 @@ fn main() {
     } else {
         None
     };
-    let mut load_sup_section = |id: gimli::SectionId| -> Result<_, _> {
-        if let Some(ref sup_object) = sup_object {
-            load_file_section(id, &sup_object, endian, &arena_data)
-        } else {
-            Ok(gimli::EndianSlice::new(&[][..], endian))
-        }
-    };
 
     let symbols = object.symbol_map();
-    let dwarf = gimli::Dwarf::load(&mut load_section, &mut load_sup_section).unwrap();
-    let dwarf_sup = Some(
-        gimli::Dwarf::load(&mut load_sup_section, |_| -> Result<_, _> {
-            Ok(gimli::EndianSlice::new(&[][..], endian))
-        })
-        .unwrap(),
-    );
+    let mut dwarf = gimli::Dwarf::load(&mut load_section).unwrap();
+    if let Some(ref sup_object) = sup_object {
+        let mut load_sup_section = |id: gimli::SectionId| -> Result<_, _> {
+            load_file_section(id, sup_object, endian, &arena_data)
+        };
+        dwarf.load_sup(&mut load_sup_section).unwrap();
+    }
 
-    let ctx = Context::from_dwarf_with_sup(dwarf, dwarf_sup).unwrap();
+    let ctx = Context::from_dwarf(dwarf).unwrap();
 
     let stdin = std::io::stdin();
     let addrs = matches
diff --git a/src/lib.rs b/src/lib.rs
index d3c7886..0657355 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -44,11 +44,13 @@ use alloc::boxed::Box;
 #[cfg(feature = "object")]
 use alloc::rc::Rc;
 use alloc::string::{String, ToString};
+use alloc::sync::Arc;
 use alloc::vec::Vec;
 
 use core::cmp::{self, Ordering};
 use core::iter;
 use core::mem;
+use core::num::NonZeroU64;
 use core::u64;
 
 use crate::lazy::LazyCell;
@@ -74,7 +76,6 @@ type Error = gimli::Error;
 /// when performing lookups for many addresses in the same executable.
 pub struct Context<R: gimli::Reader> {
     dwarf: ResDwarf<R>,
-    sup_dwarf: Option<ResDwarf<R>>,
 }
 
 /// The type of `Context` that supports the `new` method.
@@ -136,24 +137,11 @@ impl Context<gimli::EndianRcSlice<gimli::RunTimeEndian>> {
             Ok(gimli::EndianRcSlice::new(Rc::from(&*data), endian))
         }
 
-        let default_section = Ok(gimli::EndianRcSlice::new(Rc::from(&[][..]), endian));
-
-        let dwarf = gimli::Dwarf::load(
-            |id| load_section(id, file, endian),
-            |id| {
-                sup_file
-                    .map(|f| load_section(id, f, endian))
-                    .unwrap_or_else(|| default_section.clone())
-            },
-        )?;
-        let sup_dwarf = match sup_file {
-            Some(sup_file) => Some(gimli::Dwarf::load(
-                |id| load_section(id, sup_file, endian),
-                |_| default_section.clone(),
-            )?),
-            None => None,
-        };
-        Context::from_dwarf_with_sup(dwarf, sup_dwarf)
+        let mut dwarf = gimli::Dwarf::load(|id| load_section(id, file, endian))?;
+        if let Some(sup_file) = sup_file {
+            dwarf.load_sup(|id| load_section(id, sup_file, endian))?;
+        }
+        Context::from_dwarf(dwarf)
     }
 }
 
@@ -176,12 +164,12 @@ impl<R: gimli::Reader> Context<R> {
         Self::from_dwarf(gimli::Dwarf {
             debug_abbrev,
             debug_addr,
+            debug_aranges: default_section.clone().into(),
             debug_info,
             debug_line,
             debug_line_str,
             debug_str,
             debug_str_offsets,
-            debug_str_sup: default_section.clone().into(),
             debug_types: default_section.clone().into(),
             locations: gimli::LocationLists::new(
                 default_section.clone().into(),
@@ -189,28 +177,19 @@ impl<R: gimli::Reader> Context<R> {
             ),
             ranges: gimli::RangeLists::new(debug_ranges, debug_rnglists),
             file_type: gimli::DwarfFileType::Main,
+            sup: None,
         })
     }
 
     /// Construct a new `Context` from an existing [`gimli::Dwarf`] object.
     #[inline]
     pub fn from_dwarf(sections: gimli::Dwarf<R>) -> Result<Self, Error> {
-        Self::from_dwarf_with_sup(sections, None)
-    }
-
-    /// Construct a new `Context` from an existing [`gimli::Dwarf`] object.
-    ///
-    /// Optionally also use a supplementary object file.
-    pub fn from_dwarf_with_sup(
-        sections: gimli::Dwarf<R>,
-        sup_sections: Option<gimli::Dwarf<R>>,
-    ) -> Result<Self, Error> {
-        let dwarf = ResDwarf::parse(sections)?;
-        let sup_dwarf = match sup_sections {
-            Some(sup_sections) => Some(ResDwarf::parse(sup_sections)?),
+        let mut dwarf = ResDwarf::parse(Arc::new(sections))?;
+        dwarf.sup = match dwarf.sections.sup.clone() {
+            Some(sup_sections) => Some(Arc::new(ResDwarf::parse(sup_sections)?)),
             None => None,
         };
-        Ok(Context { dwarf, sup_dwarf })
+        Ok(Context { dwarf })
     }
 
     /// The dwarf sections associated with this `Context`.
@@ -289,7 +268,7 @@ impl<R: gimli::Reader> Context<R> {
     /// Find the DWARF unit corresponding to the given virtual memory address.
     pub fn find_dwarf_unit(&self, probe: u64) -> Option<&gimli::Unit<R>> {
         for unit in self.find_units(probe) {
-            match unit.find_function_or_location(probe, &self.dwarf, self.sup_dwarf.as_ref()) {
+            match unit.find_function_or_location(probe, &self.dwarf) {
                 Ok((Some(_), _)) | Ok((_, Some(_))) => return Some(&unit.dw_unit),
                 _ => {}
             }
@@ -328,7 +307,7 @@ impl<R: gimli::Reader> Context<R> {
     /// location, until an non-inline caller is reached.
     pub fn find_frames(&self, probe: u64) -> Result<FrameIter<R>, Error> {
         for unit in self.find_units(probe) {
-            match unit.find_function_or_location(probe, &self.dwarf, self.sup_dwarf.as_ref())? {
+            match unit.find_function_or_location(probe, &self.dwarf)? {
                 (Some(function), location) => {
                     let inlined_functions = function.find_inlined_functions(probe);
                     return Ok(FrameIter(FrameIterState::Frames(FrameIterFrames {
@@ -370,7 +349,7 @@ impl<R: gimli::Reader> Context<R> {
     #[doc(hidden)]
     pub fn parse_inlined_functions(&self) -> Result<(), Error> {
         for unit in &self.dwarf.units {
-            unit.parse_inlined_functions(&self.dwarf, self.sup_dwarf.as_ref())?;
+            unit.parse_inlined_functions(&self.dwarf)?;
         }
         Ok(())
     }
@@ -385,11 +364,12 @@ struct UnitRange {
 struct ResDwarf<R: gimli::Reader> {
     unit_ranges: Vec<UnitRange>,
     units: Vec<ResUnit<R>>,
-    sections: gimli::Dwarf<R>,
+    sections: Arc<gimli::Dwarf<R>>,
+    sup: Option<Arc<ResDwarf<R>>>,
 }
 
 impl<R: gimli::Reader> ResDwarf<R> {
-    fn parse(sections: gimli::Dwarf<R>) -> Result<Self, Error> {
+    fn parse(sections: Arc<gimli::Dwarf<R>>) -> Result<Self, Error> {
         let mut unit_ranges = Vec::new();
         let mut res_units = Vec::new();
         let mut units = sections.units();
@@ -479,6 +459,7 @@ impl<R: gimli::Reader> ResDwarf<R> {
             units: res_units,
             unit_ranges,
             sections,
+            sup: None,
         })
     }
 
@@ -548,10 +529,10 @@ impl<R: gimli::Reader> ResUnit<R> {
 
                     let address = row.address();
                     let file_index = row.file_index();
-                    let line = row.line().unwrap_or(0) as u32;
+                    let line = row.line().map(NonZeroU64::get).unwrap_or(0) as u32;
                     let column = match row.column() {
                         gimli::ColumnType::LeftEdge => 0,
-                        gimli::ColumnType::Column(x) => x as u32,
+                        gimli::ColumnType::Column(x) => x.get() as u32,
                     };
 
                     if let Some(last_row) = sequence_rows.last_mut() {
@@ -601,16 +582,12 @@ impl<R: gimli::Reader> ResUnit<R> {
             .map_err(Error::clone)
     }
 
-    fn parse_inlined_functions(
-        &self,
-        dwarf: &ResDwarf<R>,
-        sup_dwarf: Option<&ResDwarf<R>>,
-    ) -> Result<(), Error> {
+    fn parse_inlined_functions(&self, dwarf: &ResDwarf<R>) -> Result<(), Error> {
         self.funcs
             .borrow_with(|| Functions::parse(&self.dw_unit, dwarf))
             .as_ref()
             .map_err(Error::clone)?
-            .parse_inlined_functions(&self.dw_unit, dwarf, sup_dwarf)
+            .parse_inlined_functions(&self.dw_unit, dwarf)
     }
 
     fn find_location(
@@ -642,7 +619,6 @@ impl<R: gimli::Reader> ResUnit<R> {
         &self,
         probe: u64,
         dwarf: &ResDwarf<R>,
-        sup_dwarf: Option<&ResDwarf<R>>,
     ) -> Result<(Option<&Function<R>>, Option<Location<'_>>), Error> {
         let functions = self.parse_functions(dwarf)?;
         let function = match functions.find_address(probe) {
@@ -651,7 +627,7 @@ impl<R: gimli::Reader> ResUnit<R> {
                 let (offset, ref function) = functions.functions[function_index];
                 Some(
                     function
-                        .borrow_with(|| Function::parse(offset, &self.dw_unit, dwarf, sup_dwarf))
+                        .borrow_with(|| Function::parse(offset, &self.dw_unit, dwarf))
                         .as_ref()
                         .map_err(Error::clone)?,
                 )
@@ -922,7 +898,6 @@ fn name_attr<R>(
     attr: gimli::AttributeValue<R>,
     unit: &gimli::Unit<R>,
     dwarf: &ResDwarf<R>,
-    sup_dwarf: Option<&ResDwarf<R>>,
     recursion_limit: usize,
 ) -> Result<Option<R>, Error>
 where
@@ -933,27 +908,23 @@ where
     }
 
     match attr {
-        gimli::AttributeValue::UnitRef(offset) => {
-            name_entry(unit, offset, dwarf, sup_dwarf, recursion_limit)
-        }
+        gimli::AttributeValue::UnitRef(offset) => name_entry(unit, offset, dwarf, recursion_limit),
         gimli::AttributeValue::DebugInfoRef(dr) => {
             let res_unit = dwarf.find_unit(dr)?;
             name_entry(
                 &res_unit.dw_unit,
                 gimli::UnitOffset(dr.0 - res_unit.offset.0),
                 dwarf,
-                sup_dwarf,
                 recursion_limit,
             )
         }
         gimli::AttributeValue::DebugInfoRefSup(dr) => {
-            if let Some(sup_dwarf) = sup_dwarf {
+            if let Some(sup_dwarf) = dwarf.sup.as_ref() {
                 let res_unit = sup_dwarf.find_unit(dr)?;
                 name_entry(
                     &res_unit.dw_unit,
                     gimli::UnitOffset(dr.0 - res_unit.offset.0),
                     sup_dwarf,
-                    None,
                     recursion_limit,
                 )
             } else {
@@ -968,7 +939,6 @@ fn name_entry<R>(
     unit: &gimli::Unit<R>,
     offset: gimli::UnitOffset<R::Offset>,
     dwarf: &ResDwarf<R>,
-    sup_dwarf: Option<&ResDwarf<R>>,
     recursion_limit: usize,
 ) -> Result<Option<R>, Error>
 where
@@ -1010,7 +980,7 @@ where
     }
 
     if let Some(next) = next {
-        return name_attr(next, unit, dwarf, sup_dwarf, recursion_limit - 1);
+        return name_attr(next, unit, dwarf, recursion_limit - 1);
     }
 
     Ok(None)
@@ -1157,12 +1127,11 @@ impl<R: gimli::Reader> Functions<R> {
         &self,
         unit: &gimli::Unit<R>,
         dwarf: &ResDwarf<R>,
-        sup_dwarf: Option<&ResDwarf<R>>,
     ) -> Result<(), Error> {
         for function in &*self.functions {
             function
                 .1
-                .borrow_with(|| Function::parse(function.0, unit, dwarf, sup_dwarf))
+                .borrow_with(|| Function::parse(function.0, unit, dwarf))
                 .as_ref()
                 .map_err(Error::clone)?;
         }
@@ -1175,7 +1144,6 @@ impl<R: gimli::Reader> Function<R> {
         dw_die_offset: gimli::UnitOffset<R::Offset>,
         unit: &gimli::Unit<R>,
         dwarf: &ResDwarf<R>,
-        sup_dwarf: Option<&ResDwarf<R>>,
     ) -> Result<Self, Error> {
         let mut entries = unit.entries_raw(Some(dw_die_offset))?;
         let depth = entries.next_depth();
@@ -1199,7 +1167,7 @@ impl<R: gimli::Reader> Function<R> {
                         }
                         gimli::DW_AT_abstract_origin | gimli::DW_AT_specification => {
                             if name.is_none() {
-                                name = name_attr(attr.value(), unit, dwarf, sup_dwarf, 16)?;
+                                name = name_attr(attr.value(), unit, dwarf, 16)?;
                             }
                         }
                         _ => {}
@@ -1216,7 +1184,6 @@ impl<R: gimli::Reader> Function<R> {
             depth,
             unit,
             dwarf,
-            sup_dwarf,
             &mut inlined_functions,
             &mut inlined_addresses,
             0,
@@ -1259,7 +1226,6 @@ impl<R: gimli::Reader> Function<R> {
         depth: isize,
         unit: &gimli::Unit<R>,
         dwarf: &ResDwarf<R>,
-        sup_dwarf: Option<&ResDwarf<R>>,
         inlined_functions: &mut Vec<InlinedFunction<R>>,
         inlined_addresses: &mut Vec<InlinedFunctionAddress>,
         inlined_depth: usize,
@@ -1283,7 +1249,6 @@ impl<R: gimli::Reader> Function<R> {
                             next_depth,
                             unit,
                             dwarf,
-                            sup_dwarf,
                             inlined_functions,
                             inlined_addresses,
                             inlined_depth,
@@ -1374,7 +1339,6 @@ impl<R: gimli::Reader> InlinedFunction<R> {
         depth: isize,
         unit: &gimli::Unit<R>,
         dwarf: &ResDwarf<R>,
-        sup_dwarf: Option<&ResDwarf<R>>,
         inlined_functions: &mut Vec<InlinedFunction<R>>,
         inlined_addresses: &mut Vec<InlinedFunctionAddress>,
         inlined_depth: usize,
@@ -1413,7 +1377,7 @@ impl<R: gimli::Reader> InlinedFunction<R> {
                     }
                     gimli::DW_AT_abstract_origin | gimli::DW_AT_specification => {
                         if name.is_none() {
-                            name = name_attr(attr.value(), unit, dwarf, sup_dwarf, 16)?;
+                            name = name_attr(attr.value(), unit, dwarf, 16)?;
                         }
                     }
                     gimli::DW_AT_call_file => {
@@ -1455,7 +1419,6 @@ impl<R: gimli::Reader> InlinedFunction<R> {
             depth,
             unit,
             dwarf,
-            sup_dwarf,
             inlined_functions,
             inlined_addresses,
             inlined_depth + 1,


Test patch:
diff --git a/tests/parse.rs b/tests/parse.rs
index 69de8d0..91d66e3 100644
--- a/tests/parse.rs
+++ b/tests/parse.rs
@@ -40,8 +40,7 @@ fn dwarf_load<'a>(object: &object::File<'a>) -> gimli::Dwarf<Cow<'a, [u8]>> {
             .unwrap_or(&[][..]);
         Ok(Cow::Borrowed(data))
     };
-    let load_section_sup = |_| Ok(Cow::Borrowed(&[][..]));
-    gimli::Dwarf::load(&load_section, &load_section_sup).unwrap()
+    gimli::Dwarf::load(&load_section).unwrap()
 }
 
 fn dwarf_borrow<'a>(