From 3e49afe9160213a33f8fa5387374e41ab76cdfa0 Mon Sep 17 00:00:00 2001 From: Xi Ruoyao Date: Mon, 6 Nov 2023 20:36:51 +0800 Subject: [PATCH 1/2] elf: Update LoongArch relocation types per LoongArch ABI v2.20 - R_LARCH_DELETE is reserved by the ABI now (the BFD linker will stop using it for relaxation as well). - R_LARCH_CALL36 is added for function calls under medium code model. Link: https://github.com/loongson/la-abi-specs/releases/v2.20 Link: https://sourceware.org/pipermail/binutils/2023-October/129938.html --- crates/examples/src/readobj/elf.rs | 1 + src/elf.rs | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/examples/src/readobj/elf.rs b/crates/examples/src/readobj/elf.rs index dfd1f83d..46e4eb93 100644 --- a/crates/examples/src/readobj/elf.rs +++ b/crates/examples/src/readobj/elf.rs @@ -3170,6 +3170,7 @@ const FLAGS_R_LOONGARCH: &[Flag] = &flags!( R_LARCH_ADD_ULEB128, R_LARCH_SUB_ULEB128, R_LARCH_64_PCREL, + R_LARCH_CALL36, ); const FLAGS_NT_CORE: &[Flag] = &flags!( NT_PRSTATUS, diff --git a/src/elf.rs b/src/elf.rs index 28cc67ab..5f5b298e 100644 --- a/src/elf.rs +++ b/src/elf.rs @@ -6055,7 +6055,7 @@ pub const R_LARCH_32_PCREL: u32 = 99; /// Paired with a normal relocation at the same address to indicate the /// instruction can be relaxed pub const R_LARCH_RELAX: u32 = 100; -/// Delete an instruction, should be only used internally by the linker +/// Reserved pub const R_LARCH_DELETE: u32 = 101; /// Delete some bytes to ensure the instruction at PC + A aligned to /// `A.next_power_of_two()`-byte boundary @@ -6074,6 +6074,9 @@ pub const R_LARCH_ADD_ULEB128: u32 = 107; pub const R_LARCH_SUB_ULEB128: u32 = 108; /// 64-bit PC relative pub const R_LARCH_64_PCREL: u32 = 109; +/// 18..=37 bits of `S + A - PC` into the `pcaddu18i` instruction at `PC`, +/// and 2..=17 bits of `S + A - PC` into the `jirl` instruction at `PC + 4` +pub const R_LARCH_CALL36: u32 = 110; // Xtensa values Rel*::r_type`. pub const R_XTENSA_NONE: u32 = 0; From 3e9b28188328d74fe70fb8030c0b6c19c228aafa Mon Sep 17 00:00:00 2001 From: Xi Ruoyao Date: Mon, 6 Nov 2023 21:11:00 +0800 Subject: [PATCH 2/2] elf: Handle 64-bit PC-relative relocation writing LoongArch ELF object --- src/write/elf/object.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/write/elf/object.rs b/src/write/elf/object.rs index 421d23a8..7963c337 100644 --- a/src/write/elf/object.rs +++ b/src/write/elf/object.rs @@ -616,6 +616,7 @@ impl<'a> Object<'a> { (RelocationKind::Absolute, _, 32) => elf::R_LARCH_32, (RelocationKind::Absolute, _, 64) => elf::R_LARCH_64, (RelocationKind::Relative, _, 32) => elf::R_LARCH_32_PCREL, + (RelocationKind::Relative, _, 64) => elf::R_LARCH_64_PCREL, (RelocationKind::Relative, RelocationEncoding::LoongArchBranch, 16) | ( RelocationKind::PltRelative,