From b3814a00e30e6837f7eac7f213452b012d1a25f6 Mon Sep 17 00:00:00 2001 From: Callum Farmer Date: Thu, 24 Oct 2024 11:15:19 +0100 Subject: [PATCH] Make our dummy .reloc sections not depend on section order. Currently on x64 we manually build a dummy .reloc table entry by using a symbol in .text and subtracting its address from another symbol that's inside the .reloc section. On ia32 we just use its location. In either case, if the linker puts either section in a location we're not expecting, the .reloc table winds up having invalid values, and the PE loader will fail to load the binary. This changes it to be two symbols that are both in .text, making the result unrelated to the section order or location. It's not clear to me that these .reloc entries are actually necessary at all, but I'm going to leave them in place for now, in case they are. Ref: rhboot/gnu-efi@9fb55dee2bc6bcbafc3223b61c82ea53f361eabe Co-authored-by: Peter Jones Signed-off-by: Callum Farmer --- gnuefi/crt0-efi-aarch64-local.S | 8 ++++---- gnuefi/crt0-efi-aarch64.S | 8 ++++---- gnuefi/crt0-efi-arm.S | 7 ++++--- gnuefi/crt0-efi-ia32-local.S | 7 ++++--- gnuefi/crt0-efi-ia32.S | 7 ++++--- gnuefi/crt0-efi-ia64.S | 2 +- gnuefi/crt0-efi-loongarch64.S | 7 ++++--- gnuefi/crt0-efi-riscv64-local.S | 7 ++++--- gnuefi/crt0-efi-riscv64.S | 7 ++++--- gnuefi/crt0-efi-x86_64.S | 7 ++++--- 10 files changed, 37 insertions(+), 30 deletions(-) diff --git a/gnuefi/crt0-efi-aarch64-local.S b/gnuefi/crt0-efi-aarch64-local.S index 8ce3d28..b157ea7 100644 --- a/gnuefi/crt0-efi-aarch64-local.S +++ b/gnuefi/crt0-efi-aarch64-local.S @@ -172,12 +172,12 @@ _start: // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable: .data -dummy: .4byte 0 +dummy0: .4byte 0 +dummy1: .4byte 0 #define IMAGE_REL_ABSOLUTE 0 - .section .reloc, "a" -label1: - .4byte dummy-label1 // Page RVA + .section .reloc, "a", %progbits + .4byte dummy1 - dummy0 // Page RVA .4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy diff --git a/gnuefi/crt0-efi-aarch64.S b/gnuefi/crt0-efi-aarch64.S index 6decdbc..df2584f 100644 --- a/gnuefi/crt0-efi-aarch64.S +++ b/gnuefi/crt0-efi-aarch64.S @@ -43,12 +43,12 @@ _start: // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable: .data -dummy: .4byte 0 +dummy0: .4byte 0 +dummy1: .4byte 0 #define IMAGE_REL_ABSOLUTE 0 - .section .reloc, "a" -label1: - .4byte dummy-label1 // Page RVA + .section .reloc, "a", %progbits + .4byte dummy1 - dummy0 // Page RVA .4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy diff --git a/gnuefi/crt0-efi-arm.S b/gnuefi/crt0-efi-arm.S index ad02ca1..495beed 100644 --- a/gnuefi/crt0-efi-arm.S +++ b/gnuefi/crt0-efi-arm.S @@ -177,11 +177,12 @@ _start: // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable: .data -dummy: .4byte 0 +dummy0: .4byte 0 +dummy1: .4byte 0 #define IMAGE_REL_ABSOLUTE 0 - .section .areloc - .4byte dummy // Page RVA + .section .areloc, "a", %progbits + .4byte dummy1 - dummy0 // Page RVA .4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy diff --git a/gnuefi/crt0-efi-ia32-local.S b/gnuefi/crt0-efi-ia32-local.S index a13be2d..f07cb71 100644 --- a/gnuefi/crt0-efi-ia32-local.S +++ b/gnuefi/crt0-efi-ia32-local.S @@ -163,11 +163,12 @@ _start: // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable: .data -dummy: .4byte 0 +dummy0: .4byte 0 +dummy1: .4byte 0 #define IMAGE_REL_ABSOLUTE 0 - .section .reloc - .4byte dummy // Page RVA + .section .reloc, "a", %progbits + .4byte dummy1 - dummy0 // Page RVA .4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy diff --git a/gnuefi/crt0-efi-ia32.S b/gnuefi/crt0-efi-ia32.S index e7023ab..b7ab6dc 100644 --- a/gnuefi/crt0-efi-ia32.S +++ b/gnuefi/crt0-efi-ia32.S @@ -68,11 +68,12 @@ _start: // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable: .data -dummy: .4byte 0 +dummy0: .4byte 0 +dummy1: .4byte 0 #define IMAGE_REL_ABSOLUTE 0 - .section .reloc - .4byte dummy // Page RVA + .section .reloc, "a", %progbits + .4byte dummy1 - dummy0 // Page RVA .4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy diff --git a/gnuefi/crt0-efi-ia64.S b/gnuefi/crt0-efi-ia64.S index 30714d3..fc8a467 100644 --- a/gnuefi/crt0-efi-ia64.S +++ b/gnuefi/crt0-efi-ia64.S @@ -80,7 +80,7 @@ _start_plabel: #define IMAGE_REL_BASED_DIR64 10 - .section .reloc, "a" + .section .reloc, "a", %progbits data4 _start_plabel // Page RVA data4 12 // Block Size (2*4+2*2), must be aligned by 32 Bits data2 (IMAGE_REL_BASED_DIR64<<12) + 0 // reloc for plabel's entry point diff --git a/gnuefi/crt0-efi-loongarch64.S b/gnuefi/crt0-efi-loongarch64.S index af939ff..f094bf9 100644 --- a/gnuefi/crt0-efi-loongarch64.S +++ b/gnuefi/crt0-efi-loongarch64.S @@ -45,12 +45,13 @@ _start: // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable: .data -dummy: .4byte 0 +dummy0: .4byte 0 +dummy1: .4byte 0 #define IMAGE_REL_ABSOLUTE 0 - .section .reloc, "a" + .section .reloc, "a", %progbits label1: - .4byte dummy-label1 // Page RVA + .4byte dummy1 - dummy0 // Page RVA .4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy diff --git a/gnuefi/crt0-efi-riscv64-local.S b/gnuefi/crt0-efi-riscv64-local.S index 4b34101..b96df9e 100644 --- a/gnuefi/crt0-efi-riscv64-local.S +++ b/gnuefi/crt0-efi-riscv64-local.S @@ -170,12 +170,13 @@ _start: // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable: .data -dummy: .4byte 0 +dummy0: .4byte 0 +dummy1: .4byte 0 #define IMAGE_REL_ABSOLUTE 0 - .section .reloc, "a" + .section .reloc, "a", %progbits label1: - .4byte dummy-label1 // Page RVA + .4byte dummy1 - dummy0 // Page RVA .4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy diff --git a/gnuefi/crt0-efi-riscv64.S b/gnuefi/crt0-efi-riscv64.S index bc86a62..648ea40 100644 --- a/gnuefi/crt0-efi-riscv64.S +++ b/gnuefi/crt0-efi-riscv64.S @@ -38,12 +38,13 @@ _start: // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable: .data -dummy: .4byte 0 +dummy0: .4byte 0 +dummy1: .4byte 0 #define IMAGE_REL_ABSOLUTE 0 - .section .reloc, "a" + .section .reloc, "a", %progbits label1: - .4byte dummy-label1 // Page RVA + .4byte dummy1 - dummy0 // Page RVA .4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy diff --git a/gnuefi/crt0-efi-x86_64.S b/gnuefi/crt0-efi-x86_64.S index b1f599f..f3d0712 100644 --- a/gnuefi/crt0-efi-x86_64.S +++ b/gnuefi/crt0-efi-x86_64.S @@ -66,12 +66,13 @@ _start: // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable: .data -dummy: .4byte 0 +dummy0: .4byte 0 +dummy1: .4byte 0 #define IMAGE_REL_ABSOLUTE 0 - .section .reloc, "a" + .section .reloc, "a", %progbits label1: - .4byte dummy-label1 // Page RVA + .4byte dummy1 - dummy0 // Page RVA .4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy