Skip to content

Commit a0fb196

Browse files
Merge of #666 - Minor gimli cleanup
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.
2 parents 59cbaf5 + b9e231e commit a0fb196

12 files changed

+65
-56
lines changed

src/symbolize/gimli/coff.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use super::{gimli, Context, Endian, EndianSlice, Mapping, Path, Stash, Vec};
1+
use super::mystd::path::Path;
2+
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash};
23
use alloc::sync::Arc;
4+
use alloc::vec::Vec;
35
use core::convert::TryFrom;
46
use object::pe::{ImageDosHeader, ImageSymbol};
57
use object::read::coff::ImageSymbol as _;

src/symbolize/gimli/elf.rs

+23-34
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#![allow(clippy::useless_conversion)]
22

3-
use super::mystd::ffi::{OsStr, OsString};
3+
use super::mystd::ffi::OsStr;
44
use super::mystd::fs;
5-
use super::mystd::os::unix::ffi::{OsStrExt, OsStringExt};
5+
use super::mystd::os::unix::ffi::OsStrExt;
66
use super::mystd::path::{Path, PathBuf};
77
use super::Either;
8-
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash, Vec};
8+
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash};
9+
use alloc::string::String;
910
use alloc::sync::Arc;
11+
use alloc::vec::Vec;
1012
use core::convert::{TryFrom, TryInto};
1113
use core::str;
1214
#[cfg(feature = "ruzstd")]
@@ -393,7 +395,7 @@ fn decompress_zstd(mut input: &[u8], mut output: &mut [u8]) -> Option<()> {
393395
Some(())
394396
}
395397

396-
const DEBUG_PATH: &[u8] = b"/usr/lib/debug";
398+
const DEBUG_PATH: &str = "/usr/lib/debug";
397399

398400
fn debug_path_exists() -> bool {
399401
cfg_if::cfg_if! {
@@ -403,7 +405,7 @@ fn debug_path_exists() -> bool {
403405

404406
let mut exists = DEBUG_PATH_EXISTS.load(Ordering::Relaxed);
405407
if exists == 0 {
406-
exists = if Path::new(OsStr::from_bytes(DEBUG_PATH)).is_dir() {
408+
exists = if Path::new(DEBUG_PATH).is_dir() {
407409
1
408410
} else {
409411
2
@@ -422,8 +424,8 @@ fn debug_path_exists() -> bool {
422424
/// The format of build id paths is documented at:
423425
/// https://sourceware.org/gdb/onlinedocs/gdb/Separate-Debug-Files.html
424426
fn locate_build_id(build_id: &[u8]) -> Option<PathBuf> {
425-
const BUILD_ID_PATH: &[u8] = b"/usr/lib/debug/.build-id/";
426-
const BUILD_ID_SUFFIX: &[u8] = b".debug";
427+
const BUILD_ID_PATH: &str = "/usr/lib/debug/.build-id/";
428+
const BUILD_ID_SUFFIX: &str = ".debug";
427429

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

436438
let mut path =
437-
Vec::with_capacity(BUILD_ID_PATH.len() + BUILD_ID_SUFFIX.len() + build_id.len() * 2 + 1);
438-
path.extend(BUILD_ID_PATH);
439-
path.push(hex(build_id[0] >> 4));
440-
path.push(hex(build_id[0] & 0xf));
441-
path.push(b'/');
439+
String::with_capacity(BUILD_ID_PATH.len() + BUILD_ID_SUFFIX.len() + build_id.len() * 2 + 1);
440+
path.push_str(BUILD_ID_PATH);
441+
path.push(char::from_digit((build_id[0] >> 4) as u32, 16)?);
442+
path.push(char::from_digit((build_id[0] & 0xf) as u32, 16)?);
443+
path.push('/');
442444
for byte in &build_id[1..] {
443-
path.push(hex(byte >> 4));
444-
path.push(hex(byte & 0xf));
445-
}
446-
path.extend(BUILD_ID_SUFFIX);
447-
Some(PathBuf::from(OsString::from_vec(path)))
448-
}
449-
450-
fn hex(byte: u8) -> u8 {
451-
if byte < 10 {
452-
b'0' + byte
453-
} else {
454-
b'a' + byte - 10
445+
path.push(char::from_digit((byte >> 4) as u32, 16)?);
446+
path.push(char::from_digit((byte & 0xf) as u32, 16)?);
455447
}
448+
path.push_str(BUILD_ID_SUFFIX);
449+
Some(PathBuf::from(path))
456450
}
457451

458452
/// Locate a file specified in a `.gnu_debuglink` section.
@@ -469,9 +463,8 @@ fn hex(byte: u8) -> u8 {
469463
fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
470464
let path = fs::canonicalize(path).ok()?;
471465
let parent = path.parent()?;
472-
let mut f = PathBuf::from(OsString::with_capacity(
473-
DEBUG_PATH.len() + parent.as_os_str().len() + filename.len() + 2,
474-
));
466+
let mut f =
467+
PathBuf::with_capacity(DEBUG_PATH.len() + parent.as_os_str().len() + filename.len() + 2);
475468
let filename = Path::new(OsStr::from_bytes(filename));
476469

477470
// Try "/parent/filename" if it differs from "path"
@@ -482,9 +475,7 @@ fn locate_debuglink(path: &Path, filename: &[u8]) -> Option<PathBuf> {
482475
}
483476

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

495486
if debug_path_exists() {
496487
// Try "/usr/lib/debug/parent/filename"
497-
let mut s = OsString::from(f);
498-
s.clear();
499-
f = PathBuf::from(s);
500-
f.push(OsStr::from_bytes(DEBUG_PATH));
488+
f.clear();
489+
f.push(DEBUG_PATH);
501490
f.push(parent.strip_prefix("/").unwrap());
502491
f.push(filename);
503492
if f.is_file() {

src/symbolize/gimli/libs_aix.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use super::mystd::borrow::ToOwned;
21
use super::mystd::env;
3-
use super::mystd::ffi::{CStr, OsStr};
2+
use super::mystd::ffi::OsStr;
43
use super::mystd::io::Error;
54
use super::mystd::os::unix::prelude::*;
65
use super::xcoff;
7-
use super::{Library, LibrarySegment, Vec};
6+
use super::{Library, LibrarySegment};
7+
use alloc::borrow::ToOwned;
88
use alloc::vec;
9+
use alloc::vec::Vec;
10+
use core::ffi::CStr;
911
use core::mem;
1012

1113
const EXE_IMAGE_BASE: u64 = 0x100000000;

src/symbolize/gimli/libs_dl_iterate_phdr.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// and typically implement an API called `dl_iterate_phdr` to load
33
// native libraries.
44

5-
use super::mystd::borrow::ToOwned;
65
use super::mystd::env;
7-
use super::mystd::ffi::{CStr, OsStr};
6+
use super::mystd::ffi::{OsStr, OsString};
87
use super::mystd::os::unix::prelude::*;
9-
use super::{parse_running_mmaps, Library, LibrarySegment, OsString, Vec};
8+
use super::{parse_running_mmaps, Library, LibrarySegment};
9+
use alloc::borrow::ToOwned;
10+
use alloc::vec::Vec;
11+
use core::ffi::CStr;
1012
use core::slice;
1113

1214
struct CallbackData {

src/symbolize/gimli/libs_haiku.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
// that section. All the read-only segments of the ELF-binary are in
66
// that part of the address space.
77

8-
use super::mystd::borrow::ToOwned;
9-
use super::mystd::ffi::{CStr, OsStr};
10-
use super::mystd::mem::MaybeUninit;
8+
use super::mystd::ffi::OsStr;
119
use super::mystd::os::unix::prelude::*;
12-
use super::{Library, LibrarySegment, Vec};
10+
use super::{Library, LibrarySegment};
11+
use alloc::borrow::ToOwned;
12+
use alloc::vec::Vec;
13+
use core::ffi::CStr;
14+
use core::mem::MaybeUninit;
1315

1416
pub(super) fn native_libraries() -> Vec<Library> {
1517
let mut libraries: Vec<Library> = Vec::new();

src/symbolize/gimli/libs_illumos.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use super::mystd::borrow::ToOwned;
2-
use super::mystd::ffi::{CStr, OsStr};
1+
use super::mystd::ffi::OsStr;
32
use super::mystd::os::unix::prelude::*;
4-
use super::{Library, LibrarySegment, Vec};
3+
use super::{Library, LibrarySegment};
4+
use alloc::borrow::ToOwned;
5+
use alloc::vec::Vec;
6+
use core::ffi::CStr;
57
use core::mem;
68
use object::NativeEndian;
79

src/symbolize/gimli/libs_libnx.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use super::{Library, LibrarySegment, Vec};
1+
use super::{Library, LibrarySegment};
2+
use alloc::vec::Vec;
23

34
// DevkitA64 doesn't natively support debug info, but the build system will
45
// place debug info at the path `romfs:/debug_info.elf`.

src/symbolize/gimli/libs_macos.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#![allow(deprecated)]
22

3-
use super::mystd::ffi::{CStr, OsStr};
3+
use super::mystd::ffi::OsStr;
44
use super::mystd::os::unix::prelude::*;
55
use super::mystd::prelude::v1::*;
66
use super::{Library, LibrarySegment};
77
use core::convert::TryInto;
8+
use core::ffi::CStr;
89
use core::mem;
910

1011
// FIXME: replace with ptr::from_ref once MSRV is high enough

src/symbolize/gimli/libs_windows.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::super::super::windows_sys::*;
2+
use super::mystd::ffi::OsString;
23
use super::mystd::os::windows::prelude::*;
3-
use super::{coff, mmap, Library, LibrarySegment, OsString};
4+
use super::{coff, mmap, Library, LibrarySegment};
45
use alloc::vec;
56
use alloc::vec::Vec;
67
use core::mem;

src/symbolize/gimli/macho.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use super::{gimli, Box, Context, Endian, EndianSlice, Mapping, Path, Stash, Vec};
1+
use super::mystd::path::Path;
2+
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash};
3+
use alloc::boxed::Box;
24
use alloc::sync::Arc;
5+
use alloc::vec::Vec;
36
use core::convert::TryInto;
47
use object::macho;
58
use object::read::macho::{MachHeader, Nlist, Section, Segment as _};

src/symbolize/gimli/parse_running_mmaps_unix.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// in `mod libs_dl_iterate_phdr` (e.g. linux, freebsd, ...); it may be more
33
// general purpose, but it hasn't been tested elsewhere.
44

5+
use super::mystd::ffi::OsString;
56
use super::mystd::fs::File;
67
use super::mystd::io::Read;
7-
use super::mystd::str::FromStr;
8-
use super::{OsString, String, Vec};
8+
use alloc::string::String;
9+
use alloc::vec::Vec;
10+
use core::str::FromStr;
911

1012
#[derive(PartialEq, Eq, Debug)]
1113
pub(super) struct MapsEntry {

src/symbolize/gimli/xcoff.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use super::mystd::ffi::{OsStr, OsString};
22
use super::mystd::os::unix::ffi::OsStrExt;
3-
use super::mystd::str;
4-
use super::{gimli, Context, Endian, EndianSlice, Mapping, Path, Stash, Vec};
3+
use super::mystd::path::Path;
4+
use super::{gimli, Context, Endian, EndianSlice, Mapping, Stash};
55
use alloc::sync::Arc;
6+
use alloc::vec::Vec;
67
use core::ops::Deref;
8+
use core::str;
79
use object::read::archive::ArchiveFile;
810
use object::read::xcoff::{FileHeader, SectionHeader, XcoffFile, XcoffSymbol};
911
use object::Object as _;

0 commit comments

Comments
 (0)