Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 38f37e2

Browse files
committedSep 18, 2024·
Couple of small changes
Also avoids two unnecessary allocations in handle_split_dwarf.
1 parent 7b24b30 commit 38f37e2

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed
 

‎src/symbolize/gimli/elf.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl<'a> Object<'a> {
261261
let section = self.section_header(".gnu_debuglink")?;
262262
let data = section.data(self.endian, self.data).ok()?;
263263
let len = data.iter().position(|x| *x == 0)?;
264-
let filename = &data[..len];
264+
let filename = OsStr::from_bytes(&data[..len]);
265265
let offset = (len + 1 + 3) & !3;
266266
let crc_bytes = data
267267
.get(offset..offset + 4)
@@ -276,7 +276,7 @@ impl<'a> Object<'a> {
276276
let section = self.section_header(".gnu_debugaltlink")?;
277277
let data = section.data(self.endian, self.data).ok()?;
278278
let len = data.iter().position(|x| *x == 0)?;
279-
let filename = &data[..len];
279+
let filename = OsStr::from_bytes(&data[..len]);
280280
let build_id = &data[len + 1..];
281281
let path_sup = locate_debugaltlink(path, filename, build_id)?;
282282
Some((path_sup, build_id))
@@ -304,7 +304,7 @@ fn decompress_zlib(input: &[u8], output: &mut [u8]) -> Option<()> {
304304
}
305305
}
306306

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

309309
fn debug_path_exists() -> bool {
310310
cfg_if::cfg_if! {
@@ -314,7 +314,7 @@ fn debug_path_exists() -> bool {
314314

315315
let mut exists = DEBUG_PATH_EXISTS.load(Ordering::Relaxed);
316316
if exists == 0 {
317-
exists = if Path::new(OsStr::from_bytes(DEBUG_PATH)).is_dir() {
317+
exists = if Path::new(OsStr::new(DEBUG_PATH)).is_dir() {
318318
1
319319
} else {
320320
2
@@ -377,13 +377,12 @@ fn hex(byte: u8) -> u8 {
377377
/// gdb also allows the user to customize the debug search path, but we don't.
378378
///
379379
/// gdb also supports debuginfod, but we don't yet.
380-
fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
380+
fn locate_debuglink(path: &Path, filename: &OsStr) -> Option<PathBuf> {
381381
let path = fs::canonicalize(path).ok()?;
382382
let parent = path.parent()?;
383383
let mut f = PathBuf::from(OsString::with_capacity(
384384
DEBUG_PATH.len() + parent.as_os_str().len() + filename.len() + 2,
385385
));
386-
let filename = Path::new(OsStr::from_bytes(filename));
387386

388387
// Try "/parent/filename" if it differs from "path"
389388
f.push(parent);
@@ -408,7 +407,7 @@ fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
408407
let mut s = OsString::from(f);
409408
s.clear();
410409
f = PathBuf::from(s);
411-
f.push(OsStr::from_bytes(DEBUG_PATH));
410+
f.push(OsStr::new(DEBUG_PATH));
412411
f.push(parent.strip_prefix("/").unwrap());
413412
f.push(filename);
414413
if f.is_file() {
@@ -431,8 +430,8 @@ fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
431430
/// gdb also allows the user to customize the debug search path, but we don't.
432431
///
433432
/// gdb also supports debuginfod, but we don't yet.
434-
fn locate_debugaltlink(path: &Path, filename: &[u8], build_id: &[u8]) -> Option<PathBuf> {
435-
let filename = Path::new(OsStr::from_bytes(filename));
433+
fn locate_debugaltlink(path: &Path, filename: &OsStr, build_id: &[u8]) -> Option<PathBuf> {
434+
let filename = Path::new(filename);
436435
if filename.is_absolute() {
437436
if filename.is_file() {
438437
return Some(filename.into());
@@ -450,11 +449,6 @@ fn locate_debugaltlink(path: &Path, filename: &[u8], build_id: &[u8]) -> Option<
450449
locate_build_id(build_id)
451450
}
452451

453-
fn convert_path<R: gimli::Reader>(r: &R) -> Result<PathBuf, gimli::Error> {
454-
let bytes = r.to_slice()?;
455-
Ok(PathBuf::from(OsStr::from_bytes(&bytes)))
456-
}
457-
458452
pub(super) fn handle_split_dwarf<'data>(
459453
package: Option<&gimli::DwarfPackage<EndianSlice<'data, Endian>>>,
460454
stash: &'data Stash,
@@ -468,10 +462,10 @@ pub(super) fn handle_split_dwarf<'data>(
468462

469463
let mut path = PathBuf::new();
470464
if let Some(p) = load.comp_dir.as_ref() {
471-
path.push(convert_path(p).ok()?);
465+
path.push(OsStr::from_bytes(&p.slice()));
472466
}
473467

474-
path.push(convert_path(load.path.as_ref()?).ok()?);
468+
path.push(OsStr::from_bytes(&load.path.as_ref()?));
475469

476470
if let Some(map_dwo) = super::mmap(&path) {
477471
let map_dwo = stash.cache_mmap(map_dwo);

‎src/symbolize/gimli/xcoff.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::mystd::ffi::{OsStr, OsString};
1+
use super::mystd::ffi::OsStr;
22
use super::mystd::os::unix::ffi::OsStrExt;
33
use super::{gimli, Context, Endian, EndianSlice, Mapping, Path, Stash, Vec};
44
use alloc::sync::Arc;
@@ -17,7 +17,7 @@ type Xcoff = object::xcoff::FileHeader32;
1717
type Xcoff = object::xcoff::FileHeader64;
1818

1919
impl Mapping {
20-
pub fn new(path: &Path, member_name: &OsString) -> Option<Mapping> {
20+
pub fn new(path: &Path, member_name: &OsStr) -> Option<Mapping> {
2121
let map = super::mmap(path)?;
2222
Mapping::mk(map, |data, stash| {
2323
if member_name.is_empty() {
@@ -79,7 +79,7 @@ pub fn parse_xcoff(data: &[u8]) -> Option<Image> {
7979
}
8080
}
8181

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

0 commit comments

Comments
 (0)
Please sign in to comment.