Skip to content

Commit

Permalink
elf2flt.ld: reinstate 32 byte alignment for .data section
Browse files Browse the repository at this point in the history
Commit 8a3e744 ("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 8a3e744 ("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 8a3e744 ("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.

Signed-off-by: Niklas Cassel <[email protected]>

As Niklas points out this patch is no longer strictly necessary.

That being said, having the ALIGN attribute on the output of the data
section declaration itself is safer, as it means that the output section
will be aligned regardless if it is redirected or not.

Signed-off-by: Greg Ungerer <[email protected]>
  • Loading branch information
floatious authored and gregungerer committed Sep 12, 2023
1 parent 26dfb54 commit 679c94a
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions elf2flt.ld.in
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,10 @@ W_RODAT: *(.gnu.linkonce.r*)
*(.call_table_data)
*(.call_table_text)

. = ALIGN(0x20) ;
@SYMBOL_PREFIX@_etext = . ;
} > flatmem :text

.data : {
. = ALIGN(0x4) ;
.data ALIGN(0x20): {
@SYMBOL_PREFIX@_sdata = . ;
@SYMBOL_PREFIX@__data_start = . ;
@SYMBOL_PREFIX@data_start = . ;
Expand Down

0 comments on commit 679c94a

Please sign in to comment.