binutils/backport-Add-support-for-AArch64-EFI-efi-aarch64.patch
Chenxi Mao c2c2e8be79 Fix AArch64 PE section address overlap issue
After 31c209ebf59 delivered, MokManager.efi has below error during shim invoked:
[Bds]Booting suse-secureboot
Loading driver at 0x0002FDBF000 EntryPoint=0x0002FDDD000
2 sections contain entry point
Failed to load image: Unsupported
Failed to start MokManager: Unsupported

The root cause is the AArch64 PE section address overlapped.
Sections
================================================================================
Name      RWX  VirtSize   VirtAddr   RawAddr   RawSize   Entropy  md5
/4        R--  0x16c34    0x5000     0x400     0x17c00     4.84 bdfa950df3517b30bc1ba386b19b322b
.text     R-X  0x5c88c    0x1c000    0x18000   0x5d000     6.32 b52855acbce7b2ea150c30bc4186898d

Reason:
The 0x5000 + 0x17c00 is lager than 0x1c000 which is an unsupported/illegal format.

To fix this issue, there are 3 patches need to be applied from upstream:
d91c67e8730 Re: Add support for AArch64 EFI (efi-*-aarch64)
32384aa396e Re: AArch64: Add support for AArch64 EFI (efi-*-aarch64)
5bb067dba don't over-align file positions of PE executable sections

After above changes, the PE section address are correct.

Sections
================================================================================
Name      RWX  VirtSize   VirtAddr   RawAddr   RawSize   Entropy  md5
/4        R--  0x16c34    0x5000     0x400     0x16e00     4.97 4facea77c0e1db16428ec65d790b13e3
.text     R-X  0x5c88c    0x1c000    0x17200   0x5ca00     6.34 107cbdfa866047ff7a0463c71bbd2745

References: bsn#351
Change-Id: I2e4563b129e30ff55f2146526fc37776dcaf40dc
Signed-off-by: Chenxi Mao <chenxi.mao@suse.com>
2022-10-08 09:55:16 +08:00

159 lines
4.3 KiB
Diff

From fd932228d9104001abbf6a1c8ef1bb030ab7a21d Mon Sep 17 00:00:00 2001
From: Alan Modra <amodra@gmail.com>
Date: Tue, 7 Dec 2021 12:36:31 +1030
Subject: [PATCH 2/3] Add support for AArch64 EFI (efi-*-aarch64)
Commit b69c9d41e8 was broken in multiple ways regarding the realloc
of the target string, most notably in that "-little" wasn't actually
appended to the input_target or output_target. This caused asan
errors and "FAIL: Check if efi app format is recognized". I also
noticed that the input_target string wasn't being copied but rather
the output_target when dealing with the input target. Fix that too.
PR 26206
* objcopy.c (convert_efi_target): Rewrite. Allocate modified
target strings here..
(copy_main): ..rather than here. Do handle input_target,
not output_target for input.
References: bsn#351
Signed-off-by: Chenxi Mao <chenxi.mao@suse.com>
---
binutils/objcopy.c | 86 +++++++++++++++++++++-------------------------
1 file changed, 40 insertions(+), 46 deletions(-)
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index 242b1052..cbff93b3 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -4969,32 +4969,55 @@ set_pe_subsystem (const char *s)
/* Convert EFI target to PEI target. */
-static void
-convert_efi_target (char *efi)
+static int
+convert_efi_target (char **targ)
{
- efi[0] = 'p';
- efi[1] = 'e';
- efi[2] = 'i';
+ size_t len;
+ char *pei;
+ char *efi = *targ + 4;
+ int subsys = -1;
+
+ if (startswith (efi, "app-"))
+ subsys = IMAGE_SUBSYSTEM_EFI_APPLICATION;
+ else if (startswith (efi, "bsdrv-"))
+ {
+ subsys = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
+ efi += 2;
+ }
+ else if (startswith (efi, "rtdrv-"))
+ {
+ subsys = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
+ efi += 2;
+ }
+ else
+ return subsys;
+
+ len = strlen (efi);
+ pei = xmalloc (len + sizeof ("-little"));
+ memcpy (pei, efi, len + 1);
+ pei[0] = 'p';
+ pei[1] = 'e';
+ pei[2] = 'i';
if (strcmp (efi + 4, "ia32") == 0)
{
/* Change ia32 to i386. */
- efi[5]= '3';
- efi[6]= '8';
- efi[7]= '6';
+ pei[5]= '3';
+ pei[6]= '8';
+ pei[7]= '6';
}
else if (strcmp (efi + 4, "x86_64") == 0)
{
/* Change x86_64 to x86-64. */
- efi[7] = '-';
+ pei[7] = '-';
}
else if (strcmp (efi + 4, "aarch64") == 0)
{
/* Change aarch64 to aarch64-little. */
- efi = (char *) xrealloc (efi, strlen (efi) + 7);
- char *t = "aarch64-little";
- strcpy (efi + 4, t);
+ memcpy (pei + 4 + sizeof ("aarch64") - 1, "-little", sizeof ("-little"));
}
+ *targ = pei;
+ return subsys;
}
/* Allocate and return a pointer to a struct section_add, initializing the
@@ -5877,53 +5900,24 @@ copy_main (int argc, char *argv[])
if (input_target != NULL
&& startswith (input_target, "efi-"))
{
- char *efi;
-
- efi = xstrdup (output_target + 4);
- if (startswith (efi, "bsdrv-")
- || startswith (efi, "rtdrv-"))
- efi += 2;
- else if (!startswith (efi, "app-"))
+ if (convert_efi_target (&input_target) < 0)
fatal (_("unknown input EFI target: %s"), input_target);
-
- input_target = efi;
- convert_efi_target (efi);
}
/* Convert output EFI target to PEI target. */
if (output_target != NULL
&& startswith (output_target, "efi-"))
{
- char *efi;
+ int subsys = convert_efi_target (&output_target);
- efi = xstrdup (output_target + 4);
- if (startswith (efi, "app-"))
- {
- if (pe_subsystem == -1)
- pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
- }
- else if (startswith (efi, "bsdrv-"))
- {
- if (pe_subsystem == -1)
- pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
- efi += 2;
- }
- else if (startswith (efi, "rtdrv-"))
- {
- if (pe_subsystem == -1)
- pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
- efi += 2;
- }
- else
+ if (subsys < 0)
fatal (_("unknown output EFI target: %s"), output_target);
-
+ if (pe_subsystem == -1)
+ pe_subsystem = subsys;
if (pe_file_alignment == (bfd_vma) -1)
pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
if (pe_section_alignment == (bfd_vma) -1)
pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
-
- output_target = efi;
- convert_efi_target (efi);
}
/* If there is no destination file, or the source and destination files
--
2.30.2