binutils/backport-don-t-over-align-file-positions-of-PE-executable-sec.patch

112 lines
3.9 KiB
Diff
Raw Normal View History

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:00:40 +08:00
From 5bb067dba365e713bf988a06f7ed1c352aab52c4 Mon Sep 17 00:00:00 2001
From: Jan Beulich <jbeulich@suse.com>
Date: Thu, 19 May 2022 12:43:10 +0200
Subject: [PATCH 3/3] don't over-align file positions of PE executable sections
When a sufficiently small alignment was specified via --file-alignment,
individual section alignment shouldn't affect placement within the file.
This involves first of all clearing D_PAGED for images when section and
file alignment together don't permit paging of the image. The involved
comparison against COFF_PAGE_SIZE in turn helped point out (through a
compiler warning) that 'page_size' should be of unsigned type (as in
particular FileAlignment is). This yet in turn pointed out a dubious
error condition (which is being deleted).
For the D_PAGED case I think the enforced file alignment may still be
too high, but I'm wary of changing that logic without knowing of
possible corner cases.
Furthermore file positions in PE should be independent of the alignment
recorded in section headers anyway. Otherwise there are e.g. anomalies
following commit 6f8f6017a0c4 ("PR27567, Linking PE files adds alignment
section flags to executables") in that linking would use information a
subsequent processing step (e.g. stripping) wouldn't have available
anymore, and hence a binary could change in that 2nd step for no actual
reason. (Similarly stripping a binary linked with a linker pre-dating
that commit would change the binary again when stripping it a 2nd time.)
References: bsn#351
Signed-off-by: Chenxi Mao <chenxi.mao@suse.com>
---
bfd/coffcode.h | 29 ++++++++++++++---------------
1 file changed, 14 insertions(+), 15 deletions(-)
diff --git a/bfd/coffcode.h b/bfd/coffcode.h
index a5a4979f..d222c88d 100644
--- a/bfd/coffcode.h
+++ b/bfd/coffcode.h
@@ -2952,7 +2952,7 @@ coff_compute_section_file_positions (bfd * abfd)
#endif
#ifdef COFF_IMAGE_WITH_PE
- int page_size;
+ unsigned int page_size;
if (coff_data (abfd)->link_info
|| (pe_data (abfd) && pe_data (abfd)->pe_opthdr.FileAlignment))
@@ -2963,22 +2963,12 @@ coff_compute_section_file_positions (bfd * abfd)
This repairs 'ld -r' for arm-wince-pe target. */
if (page_size == 0)
page_size = 1;
-
- /* PR 17512: file: 0ac816d3. */
- if (page_size < 0)
- {
- bfd_set_error (bfd_error_file_too_big);
- _bfd_error_handler
- /* xgettext:c-format */
- (_("%pB: page size is too large (0x%x)"), abfd, page_size);
- return false;
- }
}
else
page_size = PE_DEF_FILE_ALIGNMENT;
#else
#ifdef COFF_PAGE_SIZE
- int page_size = COFF_PAGE_SIZE;
+ unsigned int page_size = COFF_PAGE_SIZE;
#endif
#endif
@@ -3060,9 +3050,10 @@ coff_compute_section_file_positions (bfd * abfd)
bfd_size_type amt;
#ifdef COFF_PAGE_SIZE
- /* Clear D_PAGED if section alignment is smaller than
- COFF_PAGE_SIZE. */
- if (pe_data (abfd)->pe_opthdr.SectionAlignment < COFF_PAGE_SIZE)
+ /* Clear D_PAGED if section / file alignment aren't suitable for
+ paging at COFF_PAGE_SIZE granularity. */
+ if (pe_data (abfd)->pe_opthdr.SectionAlignment < COFF_PAGE_SIZE
+ || page_size < COFF_PAGE_SIZE)
abfd->flags &= ~D_PAGED;
#endif
@@ -3183,7 +3174,11 @@ coff_compute_section_file_positions (bfd * abfd)
padding the previous section up if necessary. */
old_sofar = sofar;
+#ifdef COFF_IMAGE_WITH_PE
+ sofar = BFD_ALIGN (sofar, page_size);
+#else
sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
+#endif
#ifdef RS6000COFF_C
/* Make sure the file offset and the vma of .text/.data are at the
@@ -3259,7 +3254,11 @@ coff_compute_section_file_positions (bfd * abfd)
else
{
old_sofar = sofar;
+#ifdef COFF_IMAGE_WITH_PE
+ sofar = BFD_ALIGN (sofar, page_size);
+#else
sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
+#endif
align_adjust = sofar != old_sofar;
current->size += sofar - old_sofar;
}
--
2.30.2