[PATCH RFC] bfd/ELF: restrict file alignment for object files
Jan Beulich
jbeulich@suse.com
Fri Aug 23 10:28:16 GMT 2024
While for executables properly aligning sections within the file can be
quite relevant, the same is of pretty little importance for relocatable
object files. However, compensate this a little for sections with fixed
entry sizes, such that they're at least aligned to what conceivably is
the granularity within those elements (allowing e.g. REL/RELA sections
to still be mmap()-ed and then accessed without needing to deal with
mis-alignment of fields).
---
RFC: Question is whether some minimal alignment (e.g. the architecture's
word size) should still be retained, then perhaps replacing the
sh_entsize special treatment. Else question is whether e.g. the
symbol tables can also be written at misaligned file positions
(assuming the sh_entsize-based alignment then also would be left
out).
The Arm64 test being adjusted has an imo misleading name: I first
thought I broke something. Only when going back to the commit that
introduced it, I found that the test is about the _absence_ of tail
padding. Would people mind if I inserted e.g. "no" on the #name line?
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -4567,8 +4567,19 @@ _bfd_elf_assign_file_position_for_sectio
file_ptr offset,
bool align)
{
- if (align && i_shdrp->sh_addralign > 1)
- offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
+ file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
+
+ if (align && salign > 1)
+ offset = BFD_ALIGN (offset, salign);
+ else if (!align && i_shdrp->sh_entsize > 1)
+ {
+ /* To help efficient processing of sections with fixed element sizes,
+ align them to the minimal granularity we can infer from element
+ size itself and the specified address alignment. */
+ file_ptr ealign = i_shdrp->sh_entsize & -i_shdrp->sh_entsize;
+
+ offset = BFD_ALIGN (offset, salign && salign < ealign ? salign : ealign);
+ }
i_shdrp->sh_offset = offset;
if (i_shdrp->bfd_section != NULL)
i_shdrp->bfd_section->filepos = offset;
@@ -6796,7 +6807,7 @@ assign_file_positions_except_relocs (bfd
hdr->sh_offset = -1;
}
else
- off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
+ off = _bfd_elf_assign_file_position_for_section (hdr, off, false);
}
elf_next_file_pos (abfd) = off;
@@ -7079,7 +7090,9 @@ _bfd_elf_assign_file_positions_for_non_l
sec->contents = NULL;
}
- off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
+ off = _bfd_elf_assign_file_position_for_section (shdrp, off,
+ (abfd->flags & (EXEC_P | DYNAMIC))
+ || bfd_get_format (abfd) == bfd_core);
}
}
--- a/binutils/testsuite/binutils-all/i386/compressed-1b.d
+++ b/binutils/testsuite/binutils-all/i386/compressed-1b.d
@@ -10,9 +10,9 @@ There are 5 section headers, starting at
Section Headers:
\[Nr\] Name Type Addr Off Size ES Flg Lk Inf Al
\[ 0\] NULL 00000000 000000 000000 00 0 0 0
- \[ 1\] .text PROGBITS 00000000 000040 00001b 00 AX 0 0 16
- \[ 2\] .data PROGBITS 00000000 00005b 000000 00 WA 0 0 1
- \[ 3\] .bss NOBITS 00000000 00005b 000000 00 WA 0 0 1
- \[ 4\] .shstrtab STRTAB 00000000 [0-9a-f]+ 00001c 00 . 0 0 1
+ \[ 1\] .text PROGBITS 00000000 0000.. 00001b 00 AX 0 0 16
+ \[ 2\] .data PROGBITS 00000000 0000.. 000000 00 WA 0 0 1
+ \[ 3\] .bss NOBITS 00000000 0000.. 000000 00 WA 0 0 1
+ \[ 4\] .shstrtab STRTAB 00000000 0000.. 00001c 00 .. 0 0 1
Key to Flags:
#...
--- a/binutils/testsuite/binutils-all/i386/compressed-1c.d
+++ b/binutils/testsuite/binutils-all/i386/compressed-1c.d
@@ -10,9 +10,9 @@ There are 5 section headers, starting at
Section Headers:
\[Nr\] Name Type Addr Off Size ES Flg Lk Inf Al
\[ 0\] NULL 00000000 000000 000000 00 0 0 0
- \[ 1\] .text PROGBITS 00000000 000040 00001b 00 AX 0 0 16
- \[ 2\] .data PROGBITS 00000000 00005b 000000 00 WA 0 0 1
- \[ 3\] .bss NOBITS 00000000 00005b 000000 00 WA 0 0 1
- \[ 4\] .shstrtab STRTAB 00000000 [0-9a-f]+ 00001c 00 .* 0 0 1
+ \[ 1\] .text PROGBITS 00000000 0000.. 00001b 00 AX 0 0 16
+ \[ 2\] .data PROGBITS 00000000 0000.. 000000 00 WA 0 0 1
+ \[ 3\] .bss NOBITS 00000000 0000.. 000000 00 WA 0 0 1
+ \[ 4\] .shstrtab STRTAB 00000000 0000.. 00001c 00 .. 0 0 1
Key to Flags:
#...
--- a/binutils/testsuite/binutils-all/readelf.s
+++ b/binutils/testsuite/binutils-all/readelf.s
@@ -10,7 +10,7 @@ Section Headers:
# MIPS targets put .rela.text here.
#...
+\[ .\] .* +PROGBITS +00000000 0000(3c|40|44|48|50) 0000(04|10) 00 +WA +0 +0 +(.|..)
- +\[ .\] .* +NOBITS +00000000 0000(40|44|48|4c|60) 000000 00 +WA +0 +0 +(.|..)
+ +\[ .\] .* +NOBITS +00000000 0+[0-9a-f]+ 000000 00 +WA +0 +0 +(.|..)
# ARM targets put .ARM.attributes here.
# MIPS targets put .reginfo, .mdebug, .MIPS.abiflags and .gnu.attributes here.
# v850 targets put .call_table_data and .call_table_text here.
--- a/gas/testsuite/gas/aarch64/tail_padding.d
+++ b/gas/testsuite/gas/aarch64/tail_padding.d
@@ -11,10 +11,10 @@ Section Headers:
Size EntSize Flags Link Info Align
\[ 0\] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
- \[ 1\] \.text PROGBITS 0000000000000000 00000040
+ \[ 1\] \.text PROGBITS 0000000000000000 000000..
0000000000000000 0000000000000000 AX 0 0 1
- \[ 2\] \.data PROGBITS 0000000000000000 00000040
+ \[ 2\] \.data PROGBITS 0000000000000000 000000..
0000000000000008 0000000000000000 WA 0 0 64
- \[ 3\] \.bss NOBITS 0000000000000000 00000080
+ \[ 3\] \.bss NOBITS 0000000000000000 000000..
000000000000000c 0000000000000000 WA 0 0 64
#...
--- a/gas/testsuite/gas/arm/ehabi-pacbti-m.d
+++ b/gas/testsuite/gas/arm/ehabi-pacbti-m.d
@@ -4,7 +4,7 @@
#readelf: -u
#target: [is_elf_format]
-Unwind section '.ARM.exidx' at offset 0x5c contains 1 entry:
+Unwind section '.ARM.exidx' at offset 0x[0-9a-f]+ contains 1 entry:
0x0: @0x0
Compact model index: 1
--- a/gas/testsuite/gas/ia64/group-1.d
+++ b/gas/testsuite/gas/ia64/group-1.d
@@ -8,15 +8,15 @@ Section Headers:
Size EntSize Flags Link Info Align
\[ 0\] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
- \[ 1\] \.group GROUP 0000000000000000 00000040
+ \[ 1\] \.group GROUP 0000000000000000 [0-9a-f]+
0000000000000008 0000000000000004 6 6 4
- \[ 2\] \.text PROGBITS 0000000000000000 00000050
+ \[ 2\] \.text PROGBITS 0000000000000000 [0-9a-f]+
0000000000000000 0000000000000000 AX 0 0 16
- \[ 3\] \.data PROGBITS 0000000000000000 00000050
+ \[ 3\] \.data PROGBITS 0000000000000000 [0-9a-f]+
0000000000000000 0000000000000000 WA 0 0 1
- \[ 4\] \.bss NOBITS 0000000000000000 00000050
+ \[ 4\] \.bss NOBITS 0000000000000000 [0-9a-f]+
0000000000000000 0000000000000000 WA 0 0 1
- \[ 5\] \.text PROGBITS 0000000000000000 00000050
+ \[ 5\] \.text PROGBITS 0000000000000000 [0-9a-f]+
0000000000000010 0000000000000000 AXG 0 0 16
\[ 6\] \.symtab SYMTAB 0000000000000000 .*
00000000000000c0 0000000000000018 7 8 8
--- a/gas/testsuite/gas/ia64/group-2.d
+++ b/gas/testsuite/gas/ia64/group-2.d
@@ -9,19 +9,19 @@ Section Headers:
Size EntSize Flags Link Info Align
\[ 0\] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
- \[ 1\] \.group GROUP 0000000000000000 00000040
+ \[ 1\] \.group GROUP 0000000000000000 [0-9a-f]+
0000000000000014 0000000000000004 9 5 4
- \[ 2\] \.text PROGBITS 0000000000000000 00000060
+ \[ 2\] \.text PROGBITS 0000000000000000 [0-9a-f]+
0000000000000000 0000000000000000 AX 0 0 16
- \[ 3\] \.data PROGBITS 0000000000000000 00000060
+ \[ 3\] \.data PROGBITS 0000000000000000 [0-9a-f]+
0000000000000000 0000000000000000 WA 0 0 1
- \[ 4\] \.bss NOBITS 0000000000000000 00000060
+ \[ 4\] \.bss NOBITS 0000000000000000 [0-9a-f]+
0000000000000000 0000000000000000 WA 0 0 1
- \[ 5\] \.gnu\.linkonce\.t\.f PROGBITS 0000000000000000 00000060
+ \[ 5\] \.gnu\.linkonce\.t\.f PROGBITS 0000000000000000 [0-9a-f]+
0000000000000000 0000000000000000 AXG 0 0 16
- \[ 6\] \.gnu\.linkonce\.ia6 PROGBITS 0000000000000000 00000060
+ \[ 6\] \.gnu\.linkonce\.ia6 PROGBITS 0000000000000000 [0-9a-f]+
0000000000000010 0000000000000000 AG 0 0 8
- \[ 7\] \.gnu\.linkonce\.ia6 IA_64_UNWIND 0000000000000000 00000070
+ \[ 7\] \.gnu\.linkonce\.ia6 IA_64_UNWIND 0000000000000000 [0-9a-f]+
0000000000000018 0000000000000000 ALG 5 5 8
\[ 8\] \.rela\.gnu\.linkonc RELA 0000000000000000 .*
0000000000000048 0000000000000018 IG 9 7 8
--- a/gas/testsuite/gas/mmix/bspec-2.d
+++ b/gas/testsuite/gas/mmix/bspec-2.d
@@ -1,11 +1,11 @@
#readelf: -Sr -T -x1 -x4
There are 11 section headers, starting at offset .*:
#...
- \[ 4\] \.MMIX\.spec_data\.2 PROGBITS 0+ 0+48
+ \[ 4\] \.MMIX\.spec_data\.2 PROGBITS 0+ [0-9a-f]+
0+10 0+ 0 0 8
\[ 5\] \.rela\.MMIX\.spec_d RELA 0+ .*
+0+30 0+18 +I +8 +4 +8
- \[ 6\] \.MMIX\.spec_data\.3 PROGBITS 0+ 0+58
+ \[ 6\] \.MMIX\.spec_data\.3 PROGBITS 0+ [0-9a-f]+
0+8 0+ 0 0 8
\[ 7\] \.rela\.MMIX\.spec_d RELA 0+ .*
+0+18 +0+18 +I +8 +6 +8
--- a/ld/testsuite/ld-x86-64/pr27590.rd
+++ b/ld/testsuite/ld-x86-64/pr27590.rd
@@ -1,11 +1,11 @@
#...
- \[[ 0-9]+\] .gnu.debuglto_.debug_info PROGBITS +0+ 0+28a 0+42 00 +E 0 +0 1
- \[[ 0-9]+\] .rela.gnu.debuglto_.debug_info RELA +0+ 0+810 0+f0 18 +I 26 17 8
- \[[ 0-9]+\] .gnu.debuglto_.debug_abbrev PROGBITS +0+ 0+2cc 0+26 00 +E 0 +0 1
- \[[ 0-9]+\] .gnu.debuglto_.debug_macro PROGBITS +0+ 0+2f2 0+2a 00 +E 0 +0 1
- \[[ 0-9]+\] .rela.gnu.debuglto_.debug_macro RELA +0+ 0+900 0+60 18 +I 26 20 8
- \[[ 0-9]+\] .gnu.debuglto_.debug_macro PROGBITS +0+ 0+31c 0+10 00 GE 0 +0 1
- \[[ 0-9]+\] .rela.gnu.debuglto_.debug_macro RELA +0+ 0+960 0+30 18 IG 26 22 8
- \[[ 0-9]+\] .gnu.debuglto_.debug_line PROGBITS +0+ 0+32c 0+8a 00 +E 0 +0 1
- \[[ 0-9]+\] .gnu.debuglto_.debug_str PROGBITS +0+ 0+3b6 0+15c 01 MSE 0 +0 1
+ \[[ 0-9]+\] .gnu.debuglto_.debug_info PROGBITS +0+ [0-9a-f]+ 0+42 00 +E 0 +0 1
+ \[[ 0-9]+\] .rela.gnu.debuglto_.debug_info RELA +0+ [0-9a-f]+ 0+f0 18 +I 26 17 8
+ \[[ 0-9]+\] .gnu.debuglto_.debug_abbrev PROGBITS +0+ [0-9a-f]+ 0+26 00 +E 0 +0 1
+ \[[ 0-9]+\] .gnu.debuglto_.debug_macro PROGBITS +0+ [0-9a-f]+ 0+2a 00 +E 0 +0 1
+ \[[ 0-9]+\] .rela.gnu.debuglto_.debug_macro RELA +0+ [0-9a-f]+ 0+60 18 +I 26 20 8
+ \[[ 0-9]+\] .gnu.debuglto_.debug_macro PROGBITS +0+ [0-9a-f]+ 0+10 00 GE 0 +0 1
+ \[[ 0-9]+\] .rela.gnu.debuglto_.debug_macro RELA +0+ [0-9a-f]+ 0+30 18 IG 26 22 8
+ \[[ 0-9]+\] .gnu.debuglto_.debug_line PROGBITS +0+ [0-9a-f]+ 0+8a 00 +E 0 +0 1
+ \[[ 0-9]+\] .gnu.debuglto_.debug_str PROGBITS +0+ [0-9a-f]+ 0+15c 01 MSE 0 +0 1
#pass
More information about the Binutils
mailing list