Skip to content

Commit

Permalink
Move the ELF bindings to linux-raw-sys. (#817)
Browse files Browse the repository at this point in the history
There are uses for the ELF bindings outside of this crate, so move them
into the linux-raw-sys crate.
  • Loading branch information
sunfishcode authored Sep 10, 2023
1 parent 38fa9ba commit 1c1af76
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 233 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ once_cell = { version = "1.5.2", optional = true }
# libc backend can be selected via adding `--cfg=rustix_use_libc` to
# `RUSTFLAGS` or enabling the `use-libc` cargo feature.
[target.'cfg(all(not(rustix_use_libc), not(miri), target_os = "linux", target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"))))'.dependencies]
linux-raw-sys = { version = "0.4.3", default-features = false, features = ["general", "errno", "ioctl", "no_std"] }
linux-raw-sys = { version = "0.4.7", default-features = false, features = ["general", "errno", "ioctl", "no_std", "elf"] }
libc_errno = { package = "errno", version = "0.3.1", default-features = false, optional = true }
libc = { version = "0.2.147", default-features = false, features = ["extra_traits"], optional = true }

Expand Down
184 changes: 0 additions & 184 deletions src/backend/linux_raw/elf.rs

This file was deleted.

7 changes: 0 additions & 7 deletions src/backend/linux_raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@
#[macro_use]
mod arch;
mod conv;
#[cfg(any(
feature = "param",
feature = "runtime",
feature = "time",
target_arch = "x86"
))]
mod elf;
mod reg;
#[cfg(any(feature = "time", target_arch = "x86"))]
mod vdso;
Expand Down
16 changes: 1 addition & 15 deletions src/backend/linux_raw/param/auxv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@
#![allow(unsafe_code)]

use crate::backend::c;
use crate::backend::elf::*;
use crate::fd::OwnedFd;
#[cfg(feature = "param")]
use crate::ffi::CStr;
use crate::fs::{Mode, OFlags};
use crate::utils::{as_ptr, check_raw_pointer};
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::ffi::c_void;
use core::mem::size_of;
use core::ptr::{null_mut, read_unaligned, NonNull};
use core::sync::atomic::Ordering::Relaxed;
use core::sync::atomic::{AtomicPtr, AtomicUsize};
use linux_raw_sys::elf::*;
use linux_raw_sys::general::{
AT_BASE, AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_NULL, AT_PAGESZ, AT_SYSINFO_EHDR,
};
Expand Down Expand Up @@ -382,19 +381,6 @@ unsafe fn check_elf_base(base: *const Elf_Ehdr) -> Option<NonNull<Elf_Ehdr>> {
Some(NonNull::new_unchecked(as_ptr(hdr) as *mut _))
}

// ELF ABI

#[repr(C)]
#[derive(Copy, Clone)]
struct Elf_auxv_t {
a_type: usize,

// Some of the values in the auxv array are pointers, so we make `a_val` a
// pointer, in order to preserve their provenance. For the values which are
// integers, we cast this to `usize`.
a_val: *const c_void,
}

// Aux reading utilities

// Read auxv records from an array in memory.
Expand Down
15 changes: 1 addition & 14 deletions src/backend/linux_raw/param/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#![allow(unsafe_code)]

use crate::backend::c;
use crate::backend::elf::*;
#[cfg(feature = "param")]
use crate::ffi::CStr;
use core::ffi::c_void;
use core::ptr::{null_mut, read, NonNull};
use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use linux_raw_sys::elf::*;
use linux_raw_sys::general::{
AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_NULL, AT_PAGESZ, AT_SYSINFO_EHDR,
};
Expand Down Expand Up @@ -147,16 +147,3 @@ unsafe fn init_from_auxp(mut auxp: *const Elf_auxv_t) {
auxp = auxp.add(1);
}
}

// ELF ABI

#[repr(C)]
#[derive(Copy, Clone)]
struct Elf_auxv_t {
a_type: usize,

// Some of the values in the auxv array are pointers, so we make `a_val` a
// pointer, in order to preserve their provenance. For the values which are
// integers, we cast this to `usize`.
a_val: *mut c_void,
}
2 changes: 1 addition & 1 deletion src/backend/linux_raw/param/libc_auxv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#![allow(unsafe_code)]

use crate::backend::c;
use crate::backend::elf::*;
#[cfg(feature = "param")]
use crate::ffi::CStr;
#[cfg(not(feature = "runtime"))]
use core::ptr::null;
use linux_raw_sys::elf::*;

// `getauxval` wasn't supported in glibc until 2.16. Also this lets us use
// `*mut` as the return type to preserve strict provenance.
Expand Down
2 changes: 1 addition & 1 deletion src/backend/linux_raw/runtime/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#![allow(unsafe_code)]

use crate::backend::c;
use crate::backend::elf::*;
use crate::backend::param::auxv::exe_phdrs;
use core::ptr::{null, NonNull};
use linux_raw_sys::elf::*;

/// For use with [`set_thread_area`].
///
Expand Down
23 changes: 13 additions & 10 deletions src/backend/linux_raw/vdso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
#![allow(unsafe_code)]

use super::c;
use super::elf::*;
use crate::ffi::CStr;
use crate::utils::check_raw_pointer;
use core::ffi::c_void;
use core::mem::size_of;
use core::ptr::{null, null_mut};
use linux_raw_sys::elf::*;

pub(super) struct Vdso {
// Load information
Expand Down Expand Up @@ -143,28 +143,31 @@ fn init_from_sysinfo_ehdr() -> Option<Vdso> {
match d.d_tag {
DT_STRTAB => {
vdso.symstrings =
check_raw_pointer::<u8>(vdso.addr_from_elf(d.d_val)? as *mut _)?.as_ptr();
check_raw_pointer::<u8>(vdso.addr_from_elf(d.d_un.d_ptr)? as *mut _)?
.as_ptr();
}
DT_SYMTAB => {
vdso.symtab =
check_raw_pointer::<Elf_Sym>(vdso.addr_from_elf(d.d_val)? as *mut _)?
check_raw_pointer::<Elf_Sym>(vdso.addr_from_elf(d.d_un.d_ptr)? as *mut _)?
.as_ptr();
}
DT_HASH => {
hash =
check_raw_pointer::<u32>(vdso.addr_from_elf(d.d_val)? as *mut _)?.as_ptr();
hash = check_raw_pointer::<u32>(vdso.addr_from_elf(d.d_un.d_ptr)? as *mut _)?
.as_ptr();
}
DT_VERSYM => {
vdso.versym =
check_raw_pointer::<u16>(vdso.addr_from_elf(d.d_val)? as *mut _)?.as_ptr();
check_raw_pointer::<u16>(vdso.addr_from_elf(d.d_un.d_ptr)? as *mut _)?
.as_ptr();
}
DT_VERDEF => {
vdso.verdef =
check_raw_pointer::<Elf_Verdef>(vdso.addr_from_elf(d.d_val)? as *mut _)?
.as_ptr();
vdso.verdef = check_raw_pointer::<Elf_Verdef>(
vdso.addr_from_elf(d.d_un.d_ptr)? as *mut _,
)?
.as_ptr();
}
DT_SYMENT => {
if d.d_val != size_of::<Elf_Sym>() {
if d.d_un.d_val != size_of::<Elf_Sym>() as _ {
return None; // Failed
}
}
Expand Down

0 comments on commit 1c1af76

Please sign in to comment.