forked from buildroot/buildroot
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
package/elf2flt: update RISC-V 64-bits support
Remove the old elf2flt 0002-elf2flt-add-riscv-64-bits-support.patch patch file for riscv64 architecture and replace it with 3 patches: (1) The first patch fixes the data section alignment (2) The second patch fixes a bug with the handling of the eh_frame section causing text and data section overlap problems. (3) The third patch adds a simpler riscv64 flat bin relocation support. These 3 patches are submitted to the upstream elf2flt project as pull request #22: uclinux-dev/elf2flt#22 Signed-off-by: Niklas Cassel <niklas.cassel@wdc.com> Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com> Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
- Loading branch information
1 parent
75301bf
commit 32f93b0
Showing
3 changed files
with
181 additions
and
53 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
package/elf2flt/0002-elf2flt.ld-reinstate-32-byte-alignment-for-.data-sec.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
From 85ba5664eb368eb1cbd2c30b7cd574acd75edd4c Mon Sep 17 00:00:00 2001 | ||
From: Niklas Cassel <niklas.cassel@wdc.com> | ||
Date: Mon, 4 Apr 2022 15:30:24 +0200 | ||
Subject: [PATCH] elf2flt.ld: reinstate 32 byte alignment for .data section | ||
|
||
Commit 8a3e74446fe7 ("allow to build arm flat binaries") moved the | ||
following commands: | ||
. = ALIGN(0x20) ; | ||
@SYMBOL_PREFIX@_etext = . ; | ||
from the .text section to the top level in the SECTIONS node. | ||
|
||
The .text output section is being directed to a memory region using the | ||
"> flatmem :text" output section attribute. Commands in the top level in | ||
the SECTIONS node are not. | ||
|
||
This means that the ALIGN() command is no longer being appended to the | ||
flatmem memory region, it will simply update the Location Counter. | ||
|
||
The heuristic for placing an output section is described here: | ||
https://sourceware.org/binutils/docs-2.38/ld.html#Output-Section-Address | ||
|
||
"If an output memory region is set for the section then it is added to this | ||
region and its address will be the next free address in that region." | ||
|
||
Since the .data section is being directed to the same memory region as the | ||
.text section, this means that the Location Counter is not used when | ||
assigning an address to the .data output section, it will simply use the | ||
next free address. | ||
|
||
No longer directing these commands to the flatmem memory region means that | ||
the .data output section is no longer aligned to a 32 byte boundary. | ||
|
||
Before commit 8a3e74446fe7 ("allow to build arm flat binaries"): | ||
$ readelf -S busybox_unstripped.gdb | grep data | ||
[ 3] .data PROGBITS 0000000000035ac0 00036ac0 | ||
$ readelf -s busybox_unstripped.gdb | grep _etext | ||
19286: 0000000000035ac0 0 NOTYPE GLOBAL DEFAULT 1 _etext | ||
|
||
After commit 8a3e74446fe7 ("allow to build arm flat binaries"): | ||
$ readelf -S busybox_unstripped.gdb | grep data | ||
[ 3] .data PROGBITS 0000000000035ab0 00036ab0 | ||
$ readelf -s busybox_unstripped.gdb | grep _etext | ||
19287: 0000000000035ac0 0 NOTYPE GLOBAL DEFAULT 3 _etext | ||
|
||
The .data output section has to be aligned to a 32 byte boundary, see the | ||
FLAT_DATA_ALIGN 0x20 macro and its usage in fs/binfmt_flat.c: | ||
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/binfmt_flat.c?h=v5.17#n59 | ||
|
||
Readd an explicit ALIGN attribute on the .data section itself, since the | ||
linker will obey this attribute regardless if being directed to a memory | ||
region or not. Also remove the ALIGN() command before the .data section, | ||
since this misleads the reader to think that the Location Counter is used | ||
when assigning an address to the .data section, when it actually is not. | ||
|
||
Fixes: 8a3e74446fe7 ("allow to build arm flat binaries") | ||
Signed-off-by: Niklas Cassel <niklas.cassel@wdc.com> | ||
--- | ||
elf2flt.ld.in | 5 +---- | ||
1 file changed, 1 insertion(+), 4 deletions(-) | ||
|
||
diff --git a/elf2flt.ld.in b/elf2flt.ld.in | ||
index 0df999d..e5aea14 100644 | ||
--- a/elf2flt.ld.in | ||
+++ b/elf2flt.ld.in | ||
@@ -94,12 +94,9 @@ W_RODAT: *(.gnu.linkonce.r*) | ||
*(.ARM.exidx* .gnu.linkonce.armexidx.*) | ||
} > flatmem | ||
@SYMBOL_PREFIX@__exidx_end = .; | ||
- | ||
- . = ALIGN(0x20) ; | ||
@SYMBOL_PREFIX@_etext = . ; | ||
|
||
- .data : { | ||
- . = ALIGN(0x4) ; | ||
+ .data ALIGN(0x20): { | ||
@SYMBOL_PREFIX@_sdata = . ; | ||
@SYMBOL_PREFIX@__data_start = . ; | ||
@SYMBOL_PREFIX@data_start = . ; | ||
-- | ||
2.35.1 | ||
|
73 changes: 73 additions & 0 deletions
73
package/elf2flt/0003-elf2flt-fix-.eh_frame-section-handling.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
From 8b7fdb1dedfb8a6e858b46e5af33029fe0462ab8 Mon Sep 17 00:00:00 2001 | ||
From: Damien Le Moal <damien.lemoal@opensource.wdc.com> | ||
Date: Tue, 10 May 2022 23:14:36 +0900 | ||
Subject: [PATCH] elf2flt: fix .eh_frame section handling | ||
|
||
elf2flt.ld linker script positions the .eh_frame section in an output | ||
section after the .text and .data output sections. | ||
|
||
However, when elf2flt.c is supplied the ELF linked using the elf2flt.ld | ||
linker script, it only looks at the flags for each input section, and | ||
then puts it in either a bFLT text, data or bss output section. | ||
|
||
Commit ba379d08bb7 ("elf2flt: fix for segfault on some ARM ELFs") | ||
modified the section scanning loop of elf2flt main() function to put | ||
read-only relocation data sections in the bFLT text output section so | ||
that the .ARM.exidx section is placed in the .text flat output section. | ||
Previously a read-only relocation data section would be put in the data | ||
output section. | ||
|
||
On ARM, the .eh_frame section does not have the SEC_RELOC flag set, so | ||
it will still end up in the data output section. However, on | ||
architectures that generates the .eh_frame section with the SEC_RELOC | ||
flag set, this section will now be placed in the text output section. | ||
|
||
The logic in elf2flt will handle all sections in order, and since the | ||
input order is .text, .data, and .eh_frame, putting .eh_frame in text | ||
output section does not work, since elf2flt.c has already put the .data | ||
input section in the bFLT data output section. This leads to the | ||
following print (example for riscv64 architecture): | ||
|
||
buildroot/output/host/riscv64-buildroot-linux-uclibc/bin/elf2flt: | ||
ERROR: text=0x3bab8 overlaps data=0x33f60 ? | ||
|
||
The way that elf2flt is written, we cannot append to the text output | ||
section after an input section has been added to the data output | ||
section. It might be possible to change this, but that would require | ||
moving all the code the was already placed in the data output section. | ||
|
||
Instead, let's allow putting a read-only relocation data section in the | ||
text output section (so that .ARM.exidx will still be placed correctly), | ||
but only if there has not yet been anything placed in the data output | ||
section. | ||
|
||
That way .ARM.exidx will still be placed correctly, and .eh_frame will | ||
be placed correctly in the .data output section, regardless if it has | ||
flag SEC_RELOC set or not. | ||
|
||
Fixes: ba379d08bb7 ("elf2flt: fix for segfault on some ARM ELFs") | ||
Signed-off-by: Damien Le Moal <damien.lemoal@opensource.wdc.com> | ||
Signed-off-by: Niklas Cassel <niklas.cassel@wdc.com> | ||
--- | ||
elf2flt.c | 5 +++-- | ||
1 file changed, 3 insertions(+), 2 deletions(-) | ||
|
||
diff --git a/elf2flt.c b/elf2flt.c | ||
index 7ac0617..da25e93 100644 | ||
--- a/elf2flt.c | ||
+++ b/elf2flt.c | ||
@@ -1877,8 +1877,9 @@ int main(int argc, char *argv[]) | ||
bfd_vma sec_vma; | ||
|
||
if ((s->flags & SEC_CODE) || | ||
- ((s->flags & (SEC_DATA | SEC_READONLY | SEC_RELOC)) == | ||
- (SEC_DATA | SEC_READONLY | SEC_RELOC))) { | ||
+ (((s->flags & (SEC_DATA | SEC_READONLY | SEC_RELOC)) == | ||
+ (SEC_DATA | SEC_READONLY | SEC_RELOC)) && | ||
+ !data_len)) { | ||
vma = &text_vma; | ||
len = &text_len; | ||
} else if (s->flags & SEC_DATA) { | ||
-- | ||
2.35.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters