Skip to content

Commit

Permalink
Make our dummy .reloc sections not depend on section order.
Browse files Browse the repository at this point in the history
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@9fb55de
Co-authored-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Callum Farmer <gmbr3@opensuse.org>
  • Loading branch information
gmbr3 and vathpela committed Oct 24, 2024
1 parent 57e8966 commit b3814a0
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 30 deletions.
8 changes: 4 additions & 4 deletions gnuefi/crt0-efi-aarch64-local.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions gnuefi/crt0-efi-aarch64.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions gnuefi/crt0-efi-arm.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions gnuefi/crt0-efi-ia32-local.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions gnuefi/crt0-efi-ia32.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gnuefi/crt0-efi-ia64.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions gnuefi/crt0-efi-loongarch64.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions gnuefi/crt0-efi-riscv64-local.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions gnuefi/crt0-efi-riscv64.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions gnuefi/crt0-efi-x86_64.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b3814a0

Please sign in to comment.