Skip to content

Commit

Permalink
Merge of #666 - Minor gimli cleanup
Browse files Browse the repository at this point in the history
Noticed while considering `backtrace_in_libstd`.
Figured it was worth making it more clear which imports *needed* std,
and removing some pointless casting and such.
  • Loading branch information
workingjubilee authored Jan 3, 2025
2 parents 59cbaf5 + b9e231e commit a0fb196
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 56 deletions.
4 changes: 3 additions & 1 deletion src/symbolize/gimli/coff.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::{gimli, Context, Endian, EndianSlice, Mapping, Path, Stash, Vec};
use super::mystd::path::Path;
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash};
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::convert::TryFrom;
use object::pe::{ImageDosHeader, ImageSymbol};
use object::read::coff::ImageSymbol as _;
Expand Down
57 changes: 23 additions & 34 deletions src/symbolize/gimli/elf.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#![allow(clippy::useless_conversion)]

use super::mystd::ffi::{OsStr, OsString};
use super::mystd::ffi::OsStr;
use super::mystd::fs;
use super::mystd::os::unix::ffi::{OsStrExt, OsStringExt};
use super::mystd::os::unix::ffi::OsStrExt;
use super::mystd::path::{Path, PathBuf};
use super::Either;
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash, Vec};
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash};
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::convert::{TryFrom, TryInto};
use core::str;
#[cfg(feature = "ruzstd")]
Expand Down Expand Up @@ -393,7 +395,7 @@ fn decompress_zstd(mut input: &[u8], mut output: &mut [u8]) -> Option<()> {
Some(())
}

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 @@ -403,7 +405,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(DEBUG_PATH).is_dir() {
1
} else {
2
Expand All @@ -422,8 +424,8 @@ fn debug_path_exists() -> bool {
/// The format of build id paths is documented at:
/// https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
fn locate_build_id(build_id: &[u8]) -> Option<PathBuf> {
const BUILD_ID_PATH: &[u8] = b"/usr/lib/debug/.build-id/";
const BUILD_ID_SUFFIX: &[u8] = b".debug";
const BUILD_ID_PATH: &str = "/usr/lib/debug/.build-id/";
const BUILD_ID_SUFFIX: &str = ".debug";

if build_id.len() < 2 {
return None;
Expand All @@ -434,25 +436,17 @@ fn locate_build_id(build_id: &[u8]) -> Option<PathBuf> {
}

let mut path =
Vec::with_capacity(BUILD_ID_PATH.len() + BUILD_ID_SUFFIX.len() + build_id.len() * 2 + 1);
path.extend(BUILD_ID_PATH);
path.push(hex(build_id[0] >> 4));
path.push(hex(build_id[0] & 0xf));
path.push(b'/');
String::with_capacity(BUILD_ID_PATH.len() + BUILD_ID_SUFFIX.len() + build_id.len() * 2 + 1);
path.push_str(BUILD_ID_PATH);
path.push(char::from_digit((build_id[0] >> 4) as u32, 16)?);
path.push(char::from_digit((build_id[0] & 0xf) as u32, 16)?);
path.push('/');
for byte in &build_id[1..] {
path.push(hex(byte >> 4));
path.push(hex(byte & 0xf));
}
path.extend(BUILD_ID_SUFFIX);
Some(PathBuf::from(OsString::from_vec(path)))
}

fn hex(byte: u8) -> u8 {
if byte < 10 {
b'0' + byte
} else {
b'a' + byte - 10
path.push(char::from_digit((byte >> 4) as u32, 16)?);
path.push(char::from_digit((byte & 0xf) as u32, 16)?);
}
path.push_str(BUILD_ID_SUFFIX);
Some(PathBuf::from(path))
}

/// Locate a file specified in a `.gnu_debuglink` section.
Expand All @@ -469,9 +463,8 @@ fn hex(byte: u8) -> u8 {
fn locate_debuglink(path: &Path, filename: &[u8]) -> 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 mut f =
PathBuf::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"
Expand All @@ -482,9 +475,7 @@ fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
}

// Try "/parent/.debug/filename"
let mut s = OsString::from(f);
s.clear();
f = PathBuf::from(s);
f.clear();
f.push(parent);
f.push(".debug");
f.push(filename);
Expand All @@ -494,10 +485,8 @@ fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {

if debug_path_exists() {
// Try "/usr/lib/debug/parent/filename"
let mut s = OsString::from(f);
s.clear();
f = PathBuf::from(s);
f.push(OsStr::from_bytes(DEBUG_PATH));
f.clear();
f.push(DEBUG_PATH);
f.push(parent.strip_prefix("/").unwrap());
f.push(filename);
if f.is_file() {
Expand Down
8 changes: 5 additions & 3 deletions src/symbolize/gimli/libs_aix.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use super::mystd::borrow::ToOwned;
use super::mystd::env;
use super::mystd::ffi::{CStr, OsStr};
use super::mystd::ffi::OsStr;
use super::mystd::io::Error;
use super::mystd::os::unix::prelude::*;
use super::xcoff;
use super::{Library, LibrarySegment, Vec};
use super::{Library, LibrarySegment};
use alloc::borrow::ToOwned;
use alloc::vec;
use alloc::vec::Vec;
use core::ffi::CStr;
use core::mem;

const EXE_IMAGE_BASE: u64 = 0x100000000;
Expand Down
8 changes: 5 additions & 3 deletions src/symbolize/gimli/libs_dl_iterate_phdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// and typically implement an API called `dl_iterate_phdr` to load
// native libraries.

use super::mystd::borrow::ToOwned;
use super::mystd::env;
use super::mystd::ffi::{CStr, OsStr};
use super::mystd::ffi::{OsStr, OsString};
use super::mystd::os::unix::prelude::*;
use super::{parse_running_mmaps, Library, LibrarySegment, OsString, Vec};
use super::{parse_running_mmaps, Library, LibrarySegment};
use alloc::borrow::ToOwned;
use alloc::vec::Vec;
use core::ffi::CStr;
use core::slice;

struct CallbackData {
Expand Down
10 changes: 6 additions & 4 deletions src/symbolize/gimli/libs_haiku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
// that section. All the read-only segments of the ELF-binary are in
// that part of the address space.

use super::mystd::borrow::ToOwned;
use super::mystd::ffi::{CStr, OsStr};
use super::mystd::mem::MaybeUninit;
use super::mystd::ffi::OsStr;
use super::mystd::os::unix::prelude::*;
use super::{Library, LibrarySegment, Vec};
use super::{Library, LibrarySegment};
use alloc::borrow::ToOwned;
use alloc::vec::Vec;
use core::ffi::CStr;
use core::mem::MaybeUninit;

pub(super) fn native_libraries() -> Vec<Library> {
let mut libraries: Vec<Library> = Vec::new();
Expand Down
8 changes: 5 additions & 3 deletions src/symbolize/gimli/libs_illumos.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::mystd::borrow::ToOwned;
use super::mystd::ffi::{CStr, OsStr};
use super::mystd::ffi::OsStr;
use super::mystd::os::unix::prelude::*;
use super::{Library, LibrarySegment, Vec};
use super::{Library, LibrarySegment};
use alloc::borrow::ToOwned;
use alloc::vec::Vec;
use core::ffi::CStr;
use core::mem;
use object::NativeEndian;

Expand Down
3 changes: 2 additions & 1 deletion src/symbolize/gimli/libs_libnx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{Library, LibrarySegment, Vec};
use super::{Library, LibrarySegment};
use alloc::vec::Vec;

// DevkitA64 doesn't natively support debug info, but the build system will
// place debug info at the path `romfs:/debug_info.elf`.
Expand Down
3 changes: 2 additions & 1 deletion src/symbolize/gimli/libs_macos.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#![allow(deprecated)]

use super::mystd::ffi::{CStr, OsStr};
use super::mystd::ffi::OsStr;
use super::mystd::os::unix::prelude::*;
use super::mystd::prelude::v1::*;
use super::{Library, LibrarySegment};
use core::convert::TryInto;
use core::ffi::CStr;
use core::mem;

// FIXME: replace with ptr::from_ref once MSRV is high enough
Expand Down
3 changes: 2 additions & 1 deletion src/symbolize/gimli/libs_windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::super::super::windows_sys::*;
use super::mystd::ffi::OsString;
use super::mystd::os::windows::prelude::*;
use super::{coff, mmap, Library, LibrarySegment, OsString};
use super::{coff, mmap, Library, LibrarySegment};
use alloc::vec;
use alloc::vec::Vec;
use core::mem;
Expand Down
5 changes: 4 additions & 1 deletion src/symbolize/gimli/macho.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::{gimli, Box, Context, Endian, EndianSlice, Mapping, Path, Stash, Vec};
use super::mystd::path::Path;
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash};
use alloc::boxed::Box;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::convert::TryInto;
use object::macho;
use object::read::macho::{MachHeader, Nlist, Section, Segment as _};
Expand Down
6 changes: 4 additions & 2 deletions src/symbolize/gimli/parse_running_mmaps_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// in `mod libs_dl_iterate_phdr` (e.g. linux, freebsd, ...); it may be more
// general purpose, but it hasn't been tested elsewhere.

use super::mystd::ffi::OsString;
use super::mystd::fs::File;
use super::mystd::io::Read;
use super::mystd::str::FromStr;
use super::{OsString, String, Vec};
use alloc::string::String;
use alloc::vec::Vec;
use core::str::FromStr;

#[derive(PartialEq, Eq, Debug)]
pub(super) struct MapsEntry {
Expand Down
6 changes: 4 additions & 2 deletions src/symbolize/gimli/xcoff.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use super::mystd::ffi::{OsStr, OsString};
use super::mystd::os::unix::ffi::OsStrExt;
use super::mystd::str;
use super::{gimli, Context, Endian, EndianSlice, Mapping, Path, Stash, Vec};
use super::mystd::path::Path;
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash};
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::ops::Deref;
use core::str;
use object::read::archive::ArchiveFile;
use object::read::xcoff::{FileHeader, SectionHeader, XcoffFile, XcoffSymbol};
use object::Object as _;
Expand Down

0 comments on commit a0fb196

Please sign in to comment.