From 8e55d586e823898aa0e440b74611b2c7caf23aac Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 13 Feb 2024 19:52:29 +0000 Subject: [PATCH] linux elf relocation related structs addition. close #3577 (backport ) (cherry picked from commit 7763956f21d4dc663f4949583ac20eeb11d28b9f) --- libc-test/build.rs | 7 ++++ libc-test/semver/linux-aarch64.txt | 2 + libc-test/semver/linux-i686.txt | 2 + libc-test/semver/linux-powerpc64.txt | 2 + libc-test/semver/linux-riscv64gc.txt | 2 + libc-test/semver/linux-x86_64.txt | 2 + libc-test/semver/linux.txt | 13 +++++++ src/unix/linux_like/linux/mod.rs | 57 ++++++++++++++++++++++++++++ 8 files changed, 87 insertions(+) diff --git a/libc-test/build.rs b/libc-test/build.rs index c9ecc4b0376dd..7ea6a5b819381 100644 --- a/libc-test/build.rs +++ b/libc-test/build.rs @@ -3695,6 +3695,13 @@ fn test_linux(target: &str) { }); cfg.skip_type(move |ty| { + // FIXME: very recent additions to musl, not yet released. + if musl && (ty == "Elf32_Relr" || ty == "Elf64_Relr") { + return true; + } + if sparc64 && (ty == "Elf32_Rela" || ty == "Elf64_Rela") { + return true; + } match ty { // FIXME: `sighandler_t` type is incorrect, see: // https://github.com/rust-lang/libc/issues/1359 diff --git a/libc-test/semver/linux-aarch64.txt b/libc-test/semver/linux-aarch64.txt index 7e2566b6f0b1a..9dceaeccb819b 100644 --- a/libc-test/semver/linux-aarch64.txt +++ b/libc-test/semver/linux-aarch64.txt @@ -38,6 +38,8 @@ BPF_W BPF_X BPF_XOR CIBAUD +Elf32_Rela +Elf64_Rela FICLONE FICLONERANGE MADV_SOFT_OFFLINE diff --git a/libc-test/semver/linux-i686.txt b/libc-test/semver/linux-i686.txt index 73d4ca3554321..d914ca7f9285d 100644 --- a/libc-test/semver/linux-i686.txt +++ b/libc-test/semver/linux-i686.txt @@ -15,6 +15,8 @@ EFL EIP ES ESI +Elf32_Rela +Elf64_Rela FS GS KEYCTL_CAPABILITIES diff --git a/libc-test/semver/linux-powerpc64.txt b/libc-test/semver/linux-powerpc64.txt index 99be508e6bd59..77718d9ce47f0 100644 --- a/libc-test/semver/linux-powerpc64.txt +++ b/libc-test/semver/linux-powerpc64.txt @@ -2,6 +2,8 @@ B2500000 B3000000 B3500000 B4000000 +Elf32_Rela +Elf64_Rela KEYCTL_CAPABILITIES KEYCTL_CAPS0_BIG_KEY KEYCTL_CAPS0_CAPABILITIES diff --git a/libc-test/semver/linux-riscv64gc.txt b/libc-test/semver/linux-riscv64gc.txt index 28a115b234638..13f5b85196790 100644 --- a/libc-test/semver/linux-riscv64gc.txt +++ b/libc-test/semver/linux-riscv64gc.txt @@ -10,6 +10,8 @@ COMPAT_HWCAP_ISA_F COMPAT_HWCAP_ISA_I COMPAT_HWCAP_ISA_M COMPAT_HWCAP_ISA_V +Elf32_Rela +Elf64_Rela KEYCTL_CAPABILITIES KEYCTL_CAPS0_BIG_KEY KEYCTL_CAPS0_CAPABILITIES diff --git a/libc-test/semver/linux-x86_64.txt b/libc-test/semver/linux-x86_64.txt index a26f63d6d5a6c..f1ed29b8f299d 100644 --- a/libc-test/semver/linux-x86_64.txt +++ b/libc-test/semver/linux-x86_64.txt @@ -41,6 +41,8 @@ CIBAUD CS DS ES +Elf32_Rela +Elf64_Rela FS GS MADV_SOFT_OFFLINE diff --git a/libc-test/semver/linux.txt b/libc-test/semver/linux.txt index fff447de3c8fc..f549c977a60ba 100644 --- a/libc-test/semver/linux.txt +++ b/libc-test/semver/linux.txt @@ -418,6 +418,12 @@ EL2HLT EL2NSYNC EL3HLT EL3RST +ELF32_R_INFO +ELF32_R_SYM +ELF32_R_TYPE +ELF64_R_INFO +ELF64_R_SYM +ELF64_R_TYPE ELFCLASS32 ELFCLASS64 ELFCLASSNONE @@ -697,17 +703,24 @@ Elf32_Ehdr Elf32_Half Elf32_Off Elf32_Phdr +Elf32_Rel +Elf32_Relr Elf32_Section Elf32_Shdr +Elf32_Sword Elf32_Sym Elf32_Word +Elf32_Xword Elf64_Addr Elf64_Ehdr Elf64_Half Elf64_Off Elf64_Phdr +Elf64_Rel +Elf64_Relr Elf64_Section Elf64_Shdr +Elf64_Sword Elf64_Sxword Elf64_Sym Elf64_Word diff --git a/src/unix/linux_like/linux/mod.rs b/src/unix/linux_like/linux/mod.rs index 495db499b9a29..9cc2425fc6b6f 100644 --- a/src/unix/linux_like/linux/mod.rs +++ b/src/unix/linux_like/linux/mod.rs @@ -28,6 +28,8 @@ pub type Elf32_Half = u16; pub type Elf32_Word = u32; pub type Elf32_Off = u32; pub type Elf32_Addr = u32; +pub type Elf32_Xword = u64; +pub type Elf32_Sword = i32; pub type Elf64_Half = u16; pub type Elf64_Word = u32; @@ -35,10 +37,23 @@ pub type Elf64_Off = u64; pub type Elf64_Addr = u64; pub type Elf64_Xword = u64; pub type Elf64_Sxword = i64; +pub type Elf64_Sword = i32; pub type Elf32_Section = u16; pub type Elf64_Section = u16; +pub type Elf32_Relr = Elf32_Word; +pub type Elf64_Relr = Elf32_Xword; +pub type Elf32_Rel = __c_anonymous_elf32_rel; +pub type Elf64_Rel = __c_anonymous_elf64_rel; + +cfg_if! { + if #[cfg(not(target_arch = "sparc64"))] { + pub type Elf32_Rela = __c_anonymous_elf32_rela; + pub type Elf64_Rela = __c_anonymous_elf64_rela; + } +} + // linux/can.h pub type canid_t = u32; @@ -980,6 +995,24 @@ s! { } } +cfg_if! { + if #[cfg(not(target_arch = "sparc64"))] { + s!{ + pub struct __c_anonymous_elf32_rela { + pub r_offset: Elf32_Addr, + pub r_info: Elf32_Word, + pub r_addend: Elf32_Sword, + } + + pub struct __c_anonymous_elf64_rela { + pub r_offset: Elf64_Addr, + pub r_info: Elf64_Xword, + pub r_addend: Elf64_Sxword, + } + } + } +} + s_no_extra_traits! { pub struct sockaddr_nl { pub nl_family: ::sa_family_t, @@ -5353,6 +5386,30 @@ f! { pub fn BPF_JUMP(code: ::__u16, k: ::__u32, jt: ::__u8, jf: ::__u8) -> sock_filter { sock_filter{code: code, jt: jt, jf: jf, k: k} } + + pub fn ELF32_R_SYM(val: Elf32_Word) -> Elf32_Word { + val >> 8 + } + + pub fn ELF32_R_TYPE(val: Elf32_Word) -> Elf32_Word { + val & 0xff + } + + pub fn ELF32_R_INFO(sym: Elf32_Word, t: Elf32_Word) -> Elf32_Word { + sym << 8 + t & 0xff + } + + pub fn ELF64_R_SYM(val: Elf64_Xword) -> Elf64_Xword { + val >> 32 + } + + pub fn ELF64_R_TYPE(val: Elf64_Xword) -> Elf64_Xword { + val & 0xffffffff + } + + pub fn ELF64_R_INFO(sym: Elf64_Xword, t: Elf64_Xword) -> Elf64_Xword { + sym << 32 + t + } } safe_f! {