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@9fb55dee2b
Co-authored-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
This commit is contained in:
Callum Farmer 2024-10-24 11:15:19 +01:00
parent 57e8966b5d
commit ae94e383a3
No known key found for this signature in database
GPG Key ID: 9A5B19E18CD0013C
10 changed files with 36 additions and 29 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
.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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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