Skip to content

Commit 1c1af76

Browse files
authored
Move the ELF bindings to linux-raw-sys. (#817)
There are uses for the ELF bindings outside of this crate, so move them into the linux-raw-sys crate.
1 parent 38fa9ba commit 1c1af76

8 files changed

Lines changed: 18 additions & 233 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ once_cell = { version = "1.5.2", optional = true }
3636
# libc backend can be selected via adding `--cfg=rustix_use_libc` to
3737
# `RUSTFLAGS` or enabling the `use-libc` cargo feature.
3838
[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]
39-
linux-raw-sys = { version = "0.4.3", default-features = false, features = ["general", "errno", "ioctl", "no_std"] }
39+
linux-raw-sys = { version = "0.4.7", default-features = false, features = ["general", "errno", "ioctl", "no_std", "elf"] }
4040
libc_errno = { package = "errno", version = "0.3.1", default-features = false, optional = true }
4141
libc = { version = "0.2.147", default-features = false, features = ["extra_traits"], optional = true }
4242

src/backend/linux_raw/elf.rs

Lines changed: 0 additions & 184 deletions
This file was deleted.

src/backend/linux_raw/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
#[macro_use]
1818
mod arch;
1919
mod conv;
20-
#[cfg(any(
21-
feature = "param",
22-
feature = "runtime",
23-
feature = "time",
24-
target_arch = "x86"
25-
))]
26-
mod elf;
2720
mod reg;
2821
#[cfg(any(feature = "time", target_arch = "x86"))]
2922
mod vdso;

src/backend/linux_raw/param/auxv.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
#![allow(unsafe_code)]
77

88
use crate::backend::c;
9-
use crate::backend::elf::*;
109
use crate::fd::OwnedFd;
1110
#[cfg(feature = "param")]
1211
use crate::ffi::CStr;
1312
use crate::fs::{Mode, OFlags};
1413
use crate::utils::{as_ptr, check_raw_pointer};
1514
#[cfg(feature = "alloc")]
1615
use alloc::vec::Vec;
17-
use core::ffi::c_void;
1816
use core::mem::size_of;
1917
use core::ptr::{null_mut, read_unaligned, NonNull};
2018
use core::sync::atomic::Ordering::Relaxed;
2119
use core::sync::atomic::{AtomicPtr, AtomicUsize};
20+
use linux_raw_sys::elf::*;
2221
use linux_raw_sys::general::{
2322
AT_BASE, AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_NULL, AT_PAGESZ, AT_SYSINFO_EHDR,
2423
};
@@ -382,19 +381,6 @@ unsafe fn check_elf_base(base: *const Elf_Ehdr) -> Option<NonNull<Elf_Ehdr>> {
382381
Some(NonNull::new_unchecked(as_ptr(hdr) as *mut _))
383382
}
384383

385-
// ELF ABI
386-
387-
#[repr(C)]
388-
#[derive(Copy, Clone)]
389-
struct Elf_auxv_t {
390-
a_type: usize,
391-
392-
// Some of the values in the auxv array are pointers, so we make `a_val` a
393-
// pointer, in order to preserve their provenance. For the values which are
394-
// integers, we cast this to `usize`.
395-
a_val: *const c_void,
396-
}
397-
398384
// Aux reading utilities
399385

400386
// Read auxv records from an array in memory.

src/backend/linux_raw/param/init.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
#![allow(unsafe_code)]
77

88
use crate::backend::c;
9-
use crate::backend::elf::*;
109
#[cfg(feature = "param")]
1110
use crate::ffi::CStr;
1211
use core::ffi::c_void;
1312
use core::ptr::{null_mut, read, NonNull};
1413
use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
14+
use linux_raw_sys::elf::*;
1515
use linux_raw_sys::general::{
1616
AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_NULL, AT_PAGESZ, AT_SYSINFO_EHDR,
1717
};
@@ -147,16 +147,3 @@ unsafe fn init_from_auxp(mut auxp: *const Elf_auxv_t) {
147147
auxp = auxp.add(1);
148148
}
149149
}
150-
151-
// ELF ABI
152-
153-
#[repr(C)]
154-
#[derive(Copy, Clone)]
155-
struct Elf_auxv_t {
156-
a_type: usize,
157-
158-
// Some of the values in the auxv array are pointers, so we make `a_val` a
159-
// pointer, in order to preserve their provenance. For the values which are
160-
// integers, we cast this to `usize`.
161-
a_val: *mut c_void,
162-
}

src/backend/linux_raw/param/libc_auxv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
#![allow(unsafe_code)]
77

88
use crate::backend::c;
9-
use crate::backend::elf::*;
109
#[cfg(feature = "param")]
1110
use crate::ffi::CStr;
1211
#[cfg(not(feature = "runtime"))]
1312
use core::ptr::null;
13+
use linux_raw_sys::elf::*;
1414

1515
// `getauxval` wasn't supported in glibc until 2.16. Also this lets us use
1616
// `*mut` as the return type to preserve strict provenance.

src/backend/linux_raw/runtime/tls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#![allow(unsafe_code)]
88

99
use crate::backend::c;
10-
use crate::backend::elf::*;
1110
use crate::backend::param::auxv::exe_phdrs;
1211
use core::ptr::{null, NonNull};
12+
use linux_raw_sys::elf::*;
1313

1414
/// For use with [`set_thread_area`].
1515
///

src/backend/linux_raw/vdso.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
#![allow(unsafe_code)]
1414

1515
use super::c;
16-
use super::elf::*;
1716
use crate::ffi::CStr;
1817
use crate::utils::check_raw_pointer;
1918
use core::ffi::c_void;
2019
use core::mem::size_of;
2120
use core::ptr::{null, null_mut};
21+
use linux_raw_sys::elf::*;
2222

2323
pub(super) struct Vdso {
2424
// Load information
@@ -143,28 +143,31 @@ fn init_from_sysinfo_ehdr() -> Option<Vdso> {
143143
match d.d_tag {
144144
DT_STRTAB => {
145145
vdso.symstrings =
146-
check_raw_pointer::<u8>(vdso.addr_from_elf(d.d_val)? as *mut _)?.as_ptr();
146+
check_raw_pointer::<u8>(vdso.addr_from_elf(d.d_un.d_ptr)? as *mut _)?
147+
.as_ptr();
147148
}
148149
DT_SYMTAB => {
149150
vdso.symtab =
150-
check_raw_pointer::<Elf_Sym>(vdso.addr_from_elf(d.d_val)? as *mut _)?
151+
check_raw_pointer::<Elf_Sym>(vdso.addr_from_elf(d.d_un.d_ptr)? as *mut _)?
151152
.as_ptr();
152153
}
153154
DT_HASH => {
154-
hash =
155-
check_raw_pointer::<u32>(vdso.addr_from_elf(d.d_val)? as *mut _)?.as_ptr();
155+
hash = check_raw_pointer::<u32>(vdso.addr_from_elf(d.d_un.d_ptr)? as *mut _)?
156+
.as_ptr();
156157
}
157158
DT_VERSYM => {
158159
vdso.versym =
159-
check_raw_pointer::<u16>(vdso.addr_from_elf(d.d_val)? as *mut _)?.as_ptr();
160+
check_raw_pointer::<u16>(vdso.addr_from_elf(d.d_un.d_ptr)? as *mut _)?
161+
.as_ptr();
160162
}
161163
DT_VERDEF => {
162-
vdso.verdef =
163-
check_raw_pointer::<Elf_Verdef>(vdso.addr_from_elf(d.d_val)? as *mut _)?
164-
.as_ptr();
164+
vdso.verdef = check_raw_pointer::<Elf_Verdef>(
165+
vdso.addr_from_elf(d.d_un.d_ptr)? as *mut _,
166+
)?
167+
.as_ptr();
165168
}
166169
DT_SYMENT => {
167-
if d.d_val != size_of::<Elf_Sym>() {
170+
if d.d_un.d_val != size_of::<Elf_Sym>() as _ {
168171
return None; // Failed
169172
}
170173
}

0 commit comments

Comments
 (0)