Skip to content

Commit

Permalink
Couple of small changes
Browse files Browse the repository at this point in the history
Also avoids two unnecessary allocations in handle_split_dwarf.
  • Loading branch information
bjorn3 committed Jan 3, 2025
1 parent 8f2d98c commit 62c5764
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
21 changes: 8 additions & 13 deletions src/symbolize/gimli/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl<'a> Object<'a> {
let section = self.section_header(".gnu_debuglink")?;
let data = section.data(self.endian, self.data).ok()?;
let len = data.iter().position(|x| *x == 0)?;
let filename = &data[..len];
let filename = OsStr::from_bytes(&data[..len]);
let offset = (len + 1 + 3) & !3;
let crc_bytes = data
.get(offset..offset + 4)
Expand All @@ -332,7 +332,7 @@ impl<'a> Object<'a> {
let section = self.section_header(".gnu_debugaltlink")?;
let data = section.data(self.endian, self.data).ok()?;
let len = data.iter().position(|x| *x == 0)?;
let filename = &data[..len];
let filename = OsStr::from_bytes(&data[..len]);
let build_id = &data[len + 1..];
let path_sup = locate_debugaltlink(path, filename, build_id)?;
Some((path_sup, build_id))
Expand Down Expand Up @@ -460,12 +460,12 @@ fn locate_build_id(build_id: &[u8]) -> Option<PathBuf> {
/// gdb also allows the user to customize the debug search path, but we don't.
///
/// gdb also supports debuginfod, but we don't yet.
fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
fn locate_debuglink(path: &Path, filename: &OsStr) -> Option<PathBuf> {
let path = fs::canonicalize(path).ok()?;
let parent = path.parent()?;
let mut f =
PathBuf::with_capacity(DEBUG_PATH.len() + parent.as_os_str().len() + filename.len() + 2);
let filename = Path::new(OsStr::from_bytes(filename));
let filename = Path::new(filename);

// Try "/parent/filename" if it differs from "path"
f.push(parent);
Expand Down Expand Up @@ -509,8 +509,8 @@ fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
/// gdb also allows the user to customize the debug search path, but we don't.
///
/// gdb also supports debuginfod, but we don't yet.
fn locate_debugaltlink(path: &Path, filename: &[u8], build_id: &[u8]) -> Option<PathBuf> {
let filename = Path::new(OsStr::from_bytes(filename));
fn locate_debugaltlink(path: &Path, filename: &OsStr, build_id: &[u8]) -> Option<PathBuf> {
let filename = Path::new(filename);
if filename.is_absolute() {
if filename.is_file() {
return Some(filename.into());
Expand All @@ -528,11 +528,6 @@ fn locate_debugaltlink(path: &Path, filename: &[u8], build_id: &[u8]) -> Option<
locate_build_id(build_id)
}

fn convert_path<R: gimli::Reader>(r: &R) -> Result<PathBuf, gimli::Error> {
let bytes = r.to_slice()?;
Ok(PathBuf::from(OsStr::from_bytes(&bytes)))
}

pub(super) fn handle_split_dwarf<'data>(
package: Option<&gimli::DwarfPackage<EndianSlice<'data, Endian>>>,
stash: &'data Stash,
Expand All @@ -546,10 +541,10 @@ pub(super) fn handle_split_dwarf<'data>(

let mut path = PathBuf::new();
if let Some(p) = load.comp_dir.as_ref() {
path.push(convert_path(p).ok()?);
path.push(OsStr::from_bytes(&p));
}

path.push(convert_path(load.path.as_ref()?).ok()?);
path.push(OsStr::from_bytes(&load.path.as_ref()?));

if let Some(map_dwo) = super::mmap(&path) {
let map_dwo = stash.cache_mmap(map_dwo);
Expand Down
6 changes: 3 additions & 3 deletions src/symbolize/gimli/xcoff.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::mystd::ffi::{OsStr, OsString};
use super::mystd::ffi::OsStr;
use super::mystd::os::unix::ffi::OsStrExt;
use super::mystd::path::Path;
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash};
Expand All @@ -19,7 +19,7 @@ type Xcoff = object::xcoff::FileHeader32;
type Xcoff = object::xcoff::FileHeader64;

impl Mapping {
pub fn new(path: &Path, member_name: &OsString) -> Option<Mapping> {
pub fn new(path: &Path, member_name: &OsStr) -> Option<Mapping> {
let map = super::mmap(path)?;
Mapping::mk(map, |data, stash| {
if member_name.is_empty() {
Expand Down Expand Up @@ -81,7 +81,7 @@ pub fn parse_xcoff(data: &[u8]) -> Option<Image> {
}
}

pub fn parse_image(path: &Path, member_name: &OsString) -> Option<Image> {
pub fn parse_image(path: &Path, member_name: &OsStr) -> Option<Image> {
let map = super::mmap(path)?;
let data = map.deref();
if member_name.is_empty() {
Expand Down

0 comments on commit 62c5764

Please sign in to comment.