diff --git a/examples/list_segments.rs b/examples/list_segments.rs index 60ee6be..19cf2b3 100644 --- a/examples/list_segments.rs +++ b/examples/list_segments.rs @@ -9,7 +9,7 @@ fn main() { println!( " {}: segment {}", seg.actual_virtual_memory_address(shlib), - seg.name().to_string_lossy() + seg.name() ); } }); diff --git a/src/lib.rs b/src/lib.rs index 1e0758c..3854664 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ //! for seg in shlib.segments() { //! println!(" {}: segment {}", //! seg.actual_virtual_memory_address(shlib), -//! seg.name().to_string_lossy()); +//! seg.name()); //! } //! }); //! } @@ -86,7 +86,7 @@ extern crate lazy_static; #[cfg(target_os = "linux")] extern crate libc; -use std::ffi::CStr; +use std::ffi::OsStr; use std::fmt::{self, Debug}; use std::ptr; @@ -203,7 +203,7 @@ pub trait Segment: Sized + Debug { type SharedLibrary: SharedLibrary; /// Get this segment's name. - fn name(&self) -> &CStr; + fn name(&self) -> &str; /// Returns `true` if this is a code segment. #[inline] @@ -310,7 +310,7 @@ pub trait SharedLibrary: Sized + Debug { type SegmentIter: Debug + Iterator; /// Get the name of this shared library. - fn name(&self) -> &CStr; + fn name(&self) -> &OsStr; /// Get the debug-id of this shared library if available. fn id(&self) -> Option; diff --git a/src/linux/mod.rs b/src/linux/mod.rs index 63523bb..29d9db6 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -7,7 +7,8 @@ use super::{Bias, IterationControl, SharedLibraryId, Svma}; use std::any::Any; use std::borrow::Cow; use std::env::current_exe; -use std::ffi::{CStr, CString}; +use std::ffi::{CStr, CString, OsStr}; +use std::os::unix::ffi::OsStrExt; use std::fmt; use std::isize; use std::marker::PhantomData; @@ -46,23 +47,23 @@ pub struct Segment<'a> { impl<'a> SegmentTrait for Segment<'a> { type SharedLibrary = ::linux::SharedLibrary<'a>; - fn name(&self) -> &CStr { + fn name(&self) -> &str { unsafe { match self.phdr.as_ref().unwrap().p_type { - libc::PT_NULL => CStr::from_ptr("NULL\0".as_ptr() as _), - libc::PT_LOAD => CStr::from_ptr("LOAD\0".as_ptr() as _), - libc::PT_DYNAMIC => CStr::from_ptr("DYNAMIC\0".as_ptr() as _), - libc::PT_INTERP => CStr::from_ptr("INTERP\0".as_ptr() as _), - libc::PT_NOTE => CStr::from_ptr("NOTE\0".as_ptr() as _), - libc::PT_SHLIB => CStr::from_ptr("SHLI\0".as_ptr() as _), - libc::PT_PHDR => CStr::from_ptr("PHDR\0".as_ptr() as _), - libc::PT_TLS => CStr::from_ptr("TLS\0".as_ptr() as _), - libc::PT_NUM => CStr::from_ptr("NUM\0".as_ptr() as _), - libc::PT_LOOS => CStr::from_ptr("LOOS\0".as_ptr() as _), - libc::PT_GNU_EH_FRAME => CStr::from_ptr("GNU_EH_FRAME\0".as_ptr() as _), - libc::PT_GNU_STACK => CStr::from_ptr("GNU_STACK\0".as_ptr() as _), - libc::PT_GNU_RELRO => CStr::from_ptr("GNU_RELRO\0".as_ptr() as _), - _ => CStr::from_ptr("(unknown segment type)\0".as_ptr() as _), + libc::PT_NULL => "NULL", + libc::PT_LOAD => "LOAD", + libc::PT_DYNAMIC => "DYNAMIC", + libc::PT_INTERP => "INTERP", + libc::PT_NOTE => "NOTE", + libc::PT_SHLIB => "SHLI", + libc::PT_PHDR => "PHDR", + libc::PT_TLS => "TLS", + libc::PT_NUM => "NUM", + libc::PT_LOOS => "LOOS", + libc::PT_GNU_EH_FRAME => "GNU_EH_FRAME", + libc::PT_GNU_STACK => "GNU_STACK", + libc::PT_GNU_RELRO => "GNU_RELRO", + _ => "(unknown segment type)", } } } @@ -202,8 +203,8 @@ impl<'a> SharedLibraryTrait for SharedLibrary<'a> { type SegmentIter = SegmentIter<'a>; #[inline] - fn name(&self) -> &CStr { - &*self.name + fn name(&self) -> &OsStr { + OsStr::from_bytes(self.name.to_bytes()) } fn id(&self) -> Option { @@ -376,11 +377,10 @@ mod tests { #[test] fn get_name() { use std::ffi::OsStr; - use std::os::unix::ffi::OsStrExt; let mut names = vec![]; linux::SharedLibrary::each(|shlib| { println!("{:?}", shlib); - let name = OsStr::from_bytes(shlib.name().to_bytes()); + let name = shlib.name(); if name != OsStr::new("") { names.push(name.to_str().unwrap().to_string()); } @@ -392,13 +392,11 @@ mod tests { #[test] fn get_id() { - use std::ffi::OsStr; - use std::os::unix::ffi::OsStrExt; use std::path::Path; use std::process::Command; linux::SharedLibrary::each(|shlib| { - let name = OsStr::from_bytes(shlib.name().to_bytes()); + let name = shlib.name(); let id = shlib.id(); if id.is_none() { println!("no id found for {:?}", name); @@ -433,7 +431,7 @@ mod tests { for seg in shlib.segments() { println!(" segment = {:?}", seg.name()); - found_load |= seg.name().to_bytes() == b"LOAD"; + found_load |= seg.name() == "LOAD"; } assert!(found_load); }); diff --git a/src/macos/mod.rs b/src/macos/mod.rs index 5568b88..8aac6e4 100644 --- a/src/macos/mod.rs +++ b/src/macos/mod.rs @@ -6,7 +6,8 @@ use super::{Bias, IterationControl, Svma, SharedLibraryId}; use super::Segment as SegmentTrait; use super::SharedLibrary as SharedLibraryTrait; -use std::ffi::CStr; +use std::ffi::{CStr, OsStr}; +use std::os::unix::ffi::OsStrExt; use std::marker::PhantomData; use std::sync::Mutex; use std::usize; @@ -36,16 +37,17 @@ impl<'a> SegmentTrait for Segment<'a> { type SharedLibrary = ::macos::SharedLibrary<'a>; #[inline] - fn name(&self) -> &CStr { - match *self { + fn name(&self) -> &str { + let cstr = match *self { Segment::Segment32(seg) => unsafe { CStr::from_ptr(seg.segname.as_ptr()) }, Segment::Segment64(seg) => unsafe { CStr::from_ptr(seg.segname.as_ptr()) }, - } + }; + cstr.to_str().unwrap_or("(invalid segment name)") } #[inline] fn is_code(&self) -> bool { - self.name().to_bytes() == b"__TEXT" + self.name().as_bytes() == b"__TEXT" } #[inline] @@ -201,8 +203,8 @@ impl<'a> SharedLibraryTrait for SharedLibrary<'a> { type SegmentIter = SegmentIter<'a>; #[inline] - fn name(&self) -> &CStr { - self.name + fn name(&self) -> &OsStr { + OsStr::from_bytes(self.name.to_bytes()) } fn id(&self) -> Option { @@ -335,8 +337,8 @@ mod tests { for seg in shlib.segments() { println!(" segment = {:?}", seg.name()); - found_text_or_pagezero |= seg.name().to_bytes() == b"__TEXT"; - found_text_or_pagezero |= seg.name().to_bytes() == b"__PAGEZERO"; + found_text_or_pagezero |= seg.name() == "__TEXT"; + found_text_or_pagezero |= seg.name() == "__PAGEZERO"; } assert!(found_text_or_pagezero); }); diff --git a/src/unsupported.rs b/src/unsupported.rs index 5da7d5f..1c4279a 100644 --- a/src/unsupported.rs +++ b/src/unsupported.rs @@ -5,7 +5,7 @@ use super::Segment as SegmentTrait; use super::SharedLibrary as SharedLibraryTrait; use super::{Bias, IterationControl, SharedLibraryId, Svma}; -use std::ffi::CStr; +use std::ffi::OsStr; use std::marker::PhantomData; use std::usize; @@ -19,7 +19,7 @@ impl<'a> SegmentTrait for Segment<'a> { type SharedLibrary = ::unsupported::SharedLibrary<'a>; #[inline] - fn name(&self) -> &CStr { + fn name(&self) -> &str { unreachable!() } @@ -60,7 +60,7 @@ impl<'a> SharedLibraryTrait for SharedLibrary<'a> { type SegmentIter = SegmentIter<'a>; #[inline] - fn name(&self) -> &CStr { + fn name(&self) -> &OsStr { unreachable!() }