Skip to content

Commit 016f80a

Browse files
Merge of #673 - Various cleanups
* Avoid two unnecessary allocations in `handle_split_dwarf`. * Couple of other minor changes.
2 parents f8cc6ac + 62c5764 commit 016f80a

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

src/symbolize/gimli/elf.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl<'a> Object<'a> {
317317
let section = self.section_header(".gnu_debuglink")?;
318318
let data = section.data(self.endian, self.data).ok()?;
319319
let len = data.iter().position(|x| *x == 0)?;
320-
let filename = &data[..len];
320+
let filename = OsStr::from_bytes(&data[..len]);
321321
let offset = (len + 1 + 3) & !3;
322322
let crc_bytes = data
323323
.get(offset..offset + 4)
@@ -332,7 +332,7 @@ impl<'a> Object<'a> {
332332
let section = self.section_header(".gnu_debugaltlink")?;
333333
let data = section.data(self.endian, self.data).ok()?;
334334
let len = data.iter().position(|x| *x == 0)?;
335-
let filename = &data[..len];
335+
let filename = OsStr::from_bytes(&data[..len]);
336336
let build_id = &data[len + 1..];
337337
let path_sup = locate_debugaltlink(path, filename, build_id)?;
338338
Some((path_sup, build_id))
@@ -460,12 +460,12 @@ fn locate_build_id(build_id: &[u8]) -> Option<PathBuf> {
460460
/// gdb also allows the user to customize the debug search path, but we don't.
461461
///
462462
/// gdb also supports debuginfod, but we don't yet.
463-
fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
463+
fn locate_debuglink(path: &Path, filename: &OsStr) -> Option<PathBuf> {
464464
let path = fs::canonicalize(path).ok()?;
465465
let parent = path.parent()?;
466466
let mut f =
467467
PathBuf::with_capacity(DEBUG_PATH.len() + parent.as_os_str().len() + filename.len() + 2);
468-
let filename = Path::new(OsStr::from_bytes(filename));
468+
let filename = Path::new(filename);
469469

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

531-
fn convert_path<R: gimli::Reader>(r: &R) -> Result<PathBuf, gimli::Error> {
532-
let bytes = r.to_slice()?;
533-
Ok(PathBuf::from(OsStr::from_bytes(&bytes)))
534-
}
535-
536531
pub(super) fn handle_split_dwarf<'data>(
537532
package: Option<&gimli::DwarfPackage<EndianSlice<'data, Endian>>>,
538533
stash: &'data Stash,
@@ -546,10 +541,10 @@ pub(super) fn handle_split_dwarf<'data>(
546541

547542
let mut path = PathBuf::new();
548543
if let Some(p) = load.comp_dir.as_ref() {
549-
path.push(convert_path(p).ok()?);
544+
path.push(OsStr::from_bytes(&p));
550545
}
551546

552-
path.push(convert_path(load.path.as_ref()?).ok()?);
547+
path.push(OsStr::from_bytes(&load.path.as_ref()?));
553548

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

src/symbolize/gimli/libs_aix.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
use super::mystd::env;
22
use super::mystd::ffi::OsStr;
3-
use super::mystd::io::Error;
43
use super::mystd::os::unix::prelude::*;
54
use super::xcoff;
65
use super::{Library, LibrarySegment};
76
use alloc::borrow::ToOwned;
87
use alloc::vec;
98
use alloc::vec::Vec;
10-
use core::ffi::CStr;
9+
use core::ffi::{c_int, CStr};
1110
use core::mem;
1211

1312
const EXE_IMAGE_BASE: u64 = 0x100000000;
1413

14+
extern "C" {
15+
#[link_name = "_Errno"]
16+
fn errno_location() -> *mut c_int;
17+
}
18+
19+
fn errno() -> i32 {
20+
unsafe { (*errno_location()) as i32 }
21+
}
22+
1523
/// On AIX, we use `loadquery` with `L_GETINFO` flag to query libraries mmapped.
1624
/// See https://www.ibm.com/docs/en/aix/7.2?topic=l-loadquery-subroutine for
1725
/// detailed information of `loadquery`.
@@ -28,15 +36,14 @@ pub(super) fn native_libraries() -> Vec<Library> {
2836
{
2937
break;
3038
} else {
31-
match Error::last_os_error().raw_os_error() {
32-
Some(libc::ENOMEM) => {
39+
match errno() {
40+
libc::ENOMEM => {
3341
buffer.resize(buffer.len() * 2, mem::zeroed::<libc::ld_info>());
3442
}
35-
Some(_) => {
43+
_ => {
3644
// If other error occurs, return empty libraries.
3745
return Vec::new();
3846
}
39-
_ => unreachable!(),
4047
}
4148
}
4249
}

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::mystd::path::Path;
44
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash};
@@ -19,7 +19,7 @@ type Xcoff = object::xcoff::FileHeader32;
1919
type Xcoff = object::xcoff::FileHeader64;
2020

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

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

0 commit comments

Comments
 (0)