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 Sep 18, 2024
1 parent 7b24b30 commit 38f37e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
26 changes: 10 additions & 16 deletions src/symbolize/gimli/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,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 @@ -276,7 +276,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 @@ -304,7 +304,7 @@ fn decompress_zlib(input: &[u8], output: &mut [u8]) -> Option<()> {
}
}

const DEBUG_PATH: &[u8] = b"/usr/lib/debug";
const DEBUG_PATH: &str = "/usr/lib/debug";

fn debug_path_exists() -> bool {
cfg_if::cfg_if! {
Expand All @@ -314,7 +314,7 @@ fn debug_path_exists() -> bool {

let mut exists = DEBUG_PATH_EXISTS.load(Ordering::Relaxed);
if exists == 0 {
exists = if Path::new(OsStr::from_bytes(DEBUG_PATH)).is_dir() {
exists = if Path::new(OsStr::new(DEBUG_PATH)).is_dir() {
1
} else {
2
Expand Down Expand Up @@ -377,13 +377,12 @@ fn hex(byte: u8) -> u8 {
/// 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::from(OsString::with_capacity(
DEBUG_PATH.len() + parent.as_os_str().len() + filename.len() + 2,
));
let filename = Path::new(OsStr::from_bytes(filename));

// Try "/parent/filename" if it differs from "path"
f.push(parent);
Expand All @@ -408,7 +407,7 @@ fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
let mut s = OsString::from(f);
s.clear();
f = PathBuf::from(s);
f.push(OsStr::from_bytes(DEBUG_PATH));
f.push(OsStr::new(DEBUG_PATH));
f.push(parent.strip_prefix("/").unwrap());
f.push(filename);
if f.is_file() {
Expand All @@ -431,8 +430,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 @@ -450,11 +449,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 @@ -468,10 +462,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.slice()));
}

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::{gimli, Context, Endian, EndianSlice, Mapping, Path, Stash, Vec};
use alloc::sync::Arc;
Expand All @@ -17,7 +17,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 @@ -79,7 +79,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 38f37e2

Please sign in to comment.