This commit is contained in:
nora 2023-02-12 13:53:07 +01:00
parent 5de9ea38ca
commit 9d16c87e50
5 changed files with 17 additions and 9 deletions

View file

@ -110,8 +110,7 @@ fn print_file(path: &str) -> anyhow::Result<()> {
.map(|sym| {
let name = sym_display_name(elf, sym)?;
let section = match sym.shndx.0 {
c::SHN_ABS => " ".to_string(),
c::SHN_COMMON => "".to_string(),
c::SHN_ABS | c::SHN_COMMON => String::new(),
_ => elf
.sh_string(elf.section_header(sym.shndx)?.name)?
.to_string(),

View file

@ -1,4 +1,5 @@
#![allow(non_upper_case_globals)]
#![allow(clippy::unreadable_literal)]
macro_rules! const_group_with_fmt {
(

View file

@ -1,6 +1,6 @@
//! Structures and parsers for ELF64. ELF32 can knock itself out.
//!
//! See https://man7.org/linux/man-pages/man5/elf.5.html
//! See <https://man7.org/linux/man-pages/man5/elf.5.html>
use crate::{
consts::{self as c, DynamicTag, ShType},
@ -11,7 +11,8 @@ use bstr::BStr;
use std::{
fmt::{Debug, Display},
mem, string,
mem,
string::{self, FromUtf8Error},
};
use bytemuck::{Pod, PodCastError, Zeroable};
@ -39,7 +40,7 @@ pub struct Offset(pub u64);
impl ToIdxUsize for Offset {
fn to_idx_usize(self) -> usize {
self.0 as usize
self.0.to_idx_usize()
}
}
@ -184,7 +185,7 @@ impl RelInfo {
}
pub fn r#type(&self) -> u32 {
(self.0 & 0xffffffff) as u32
(self.0 & 0xffff_ffff) as u32
}
}
@ -203,7 +204,11 @@ pub struct Dyn {
impl<'a> Elf<'a> {
pub fn new(data: &'a [u8]) -> Result<Self> {
let magic = data[..c::SELFMAG].try_into().unwrap();
let magic = data[..c::SELFMAG].try_into().map_err(|_| {
let mut padded = [0, 0, 0, 0];
padded.copy_from_slice(data);
ElfParseError::WrongMagic(padded)
})?;
if magic != *c::ELFMAG {
return Err(ElfParseError::WrongMagic(magic));
@ -275,7 +280,7 @@ impl<'a> Elf<'a> {
}
let name = name.to_vec();
Err(ElfParseError::SectionNotFound(
string::String::from_utf8(name).map_err(|err| err.into_bytes()),
string::String::from_utf8(name).map_err(FromUtf8Error::into_bytes),
))
}
@ -435,7 +440,7 @@ fn load_slice<T: Pod>(data: &[u8], amount_of_elems: usize) -> Result<&[T]> {
return Err(ElfParseError::FileTooSmall(size, data.len()));
}
let data_addr = data as *const [u8] as *const u8 as usize;
let data_addr = (data as *const [u8]).cast::<u8>() as usize;
let data_align = data_addr.trailing_zeros() as usize;
let data = &data[..size];

View file

@ -73,6 +73,7 @@ impl ToIdxUsize for usize {
}
}
impl ToIdxUsize for u64 {
#[allow(clippy::cast_possible_truncation)]
fn to_idx_usize(self) -> usize {
self as usize
}

View file

@ -1,3 +1,5 @@
#![allow(clippy::must_use_candidate, clippy::missing_errors_doc)]
use consts::{DynamicTag, ShType};
pub mod consts;