]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
bfd/ELF: restrict file alignment for object files
authorJan Beulich <jbeulich@suse.com>
Fri, 11 Oct 2024 06:19:34 +0000 (08:19 +0200)
committerJan Beulich <jbeulich@suse.com>
Fri, 11 Oct 2024 06:19:34 +0000 (08:19 +0200)
While for executables properly aligning sections within the file can be
quite relevant, the same is of pretty little importance for relocatable
object files. Avoid passing "true" into
_bfd_elf_assign_file_position_for_section() when dealing with object
files, but compensate minimally by applying log_file_align in such
cases as a cap to the alignment put in place.

12 files changed:
bfd/elf-bfd.h
bfd/elf.c
bfd/elflink.c
binutils/testsuite/binutils-all/i386/compressed-1b.d
binutils/testsuite/binutils-all/i386/compressed-1c.d
binutils/testsuite/binutils-all/readelf.s
gas/testsuite/gas/aarch64/tail_padding.d
gas/testsuite/gas/arm/ehabi-pacbti-m.d
gas/testsuite/gas/ia64/group-1.d
gas/testsuite/gas/ia64/group-2.d
gas/testsuite/gas/mmix/bspec-2.d
ld/testsuite/ld-x86-64/pr27590.rd

index b89d3dda05d337e7134dd93a30c4dc79c29a9e98..96cf11936aee0e4327892efc4c2535ed1f90e885 100644 (file)
@@ -2540,7 +2540,7 @@ extern long _bfd_elf_link_lookup_local_dynindx
 extern bool _bfd_elf_compute_section_file_positions
   (bfd *, struct bfd_link_info *);
 extern file_ptr _bfd_elf_assign_file_position_for_section
-  (Elf_Internal_Shdr *, file_ptr, bool);
+  (Elf_Internal_Shdr *, file_ptr, bool, unsigned char);
 extern bool _bfd_elf_modify_headers
   (bfd *, struct bfd_link_info *);
 
index 997befd56868c0d8582b3d0bf330e3a221dc7cb3..27d45fc17a589a1a6d88f99a26d9b0a0631f3ccb 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -4572,10 +4572,23 @@ align_file_position (file_ptr off, int align)
 file_ptr
 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
                                           file_ptr offset,
-                                          bool align)
+                                          bool align,
+                                          unsigned char log_file_align)
 {
-  if (align && i_shdrp->sh_addralign > 1)
-    offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign);
+  if (i_shdrp->sh_addralign > 1)
+    {
+      file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
+
+      if (align)
+       offset = BFD_ALIGN (offset, salign);
+      else if (log_file_align)
+       {
+         /* Heuristic: Cap alignment at log_file_align.  */
+         file_ptr falign = 1u << log_file_align;
+
+         offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
+       }
+    }
   i_shdrp->sh_offset = offset;
   if (i_shdrp->bfd_section != NULL)
     i_shdrp->bfd_section->filepos = offset;
@@ -4663,18 +4676,18 @@ _bfd_elf_compute_section_file_positions (bfd *abfd,
       off = elf_next_file_pos (abfd);
 
       hdr = & elf_symtab_hdr (abfd);
-      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
+      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
 
       if (elf_symtab_shndx_list (abfd) != NULL)
        {
          hdr = & elf_symtab_shndx_list (abfd)->hdr;
          if (hdr->sh_size != 0)
-           off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
+           off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
          /* FIXME: What about other symtab_shndx sections in the list ?  */
        }
 
       hdr = &elf_tdata (abfd)->strtab_hdr;
-      off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
+      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
 
       elf_next_file_pos (abfd) = off;
 
@@ -6548,8 +6561,8 @@ assign_file_positions_for_non_load_sections (bfd *abfd,
          else
            align = hdr->sh_addralign & -hdr->sh_addralign;
          off += vma_page_aligned_bias (hdr->sh_addr, off, align);
-         off = _bfd_elf_assign_file_position_for_section (hdr, off,
-                                                          false);
+         off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
+                                                          bed->s->log_file_align);
        }
       else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
                && hdr->bfd_section == NULL)
@@ -6566,7 +6579,7 @@ assign_file_positions_for_non_load_sections (bfd *abfd,
               || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
        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, true, 0);
     }
   elf_next_file_pos (abfd) = off;
 
@@ -6803,7 +6816,8 @@ assign_file_positions_except_relocs (bfd *abfd,
              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,
+                                                            0);
        }
 
       elf_next_file_pos (abfd) = off;
@@ -7018,7 +7032,7 @@ _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   Elf_Internal_Shdr **shdrpp, **end_shdrpp;
   Elf_Internal_Shdr *shdrp;
   Elf_Internal_Ehdr *i_ehdrp;
-  const struct elf_backend_data *bed;
+  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
 
   /* Skip non-load sections without section header.  */
   if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
@@ -7086,7 +7100,10 @@ _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
              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,
+                 bed->s->log_file_align);
        }
     }
 
@@ -7095,11 +7112,10 @@ _bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
   _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
   shdrp = &elf_tdata (abfd)->shstrtab_hdr;
   shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
-  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
+  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
 
   /* Place the section headers.  */
   i_ehdrp = elf_elfheader (abfd);
-  bed = get_elf_backend_data (abfd);
   off = align_file_position (off, 1 << bed->s->log_file_align);
   i_ehdrp->e_shoff = off;
   off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
index a498dbb12a6ae4ff7b65a2976b4fb9883c91c6f4..19a9aec3f9de01710e2905ace24a386a214456d1 100644 (file)
@@ -12926,7 +12926,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
     {
       file_ptr off = elf_next_file_pos (abfd);
 
-      _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
+      _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0);
 
       /* Note that at this point elf_next_file_pos (abfd) is
         incorrect.  We do not yet know the size of the .symtab section.
@@ -13370,7 +13370,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
              symtab_shndx_hdr->sh_size = amt;
 
              off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
-                                                              off, true);
+                                                              off, true, 0);
 
              if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
                  || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt))
@@ -13394,7 +13394,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
       symstrtab_hdr->sh_addralign = 1;
 
       off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
-                                                      off, true);
+                                                      off, true, 0);
       elf_next_file_pos (abfd) = off;
 
       if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
index 6e45e552fc8b4b488b6e64134d5723c5d0ba7e68..8b3f75ab84616ec62a3a04fc7222954ae6082944 100644 (file)
@@ -10,9 +10,9 @@ There are 5 section headers, starting at offset 0x[0-9a-f]+:
 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:
 #...
index 1e1a18e7786beebe4178964bf2f459d66e4856f6..f65e8cf2037e324fa43fd97c828f2253293c4e24 100644 (file)
@@ -10,9 +10,9 @@ There are 5 section headers, starting at offset 0x[0-9a-f]+:
 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:
 #...
index ff37acb4da86bdc4ce90ac69888d34f4c5cc5311..5a1f320c6e103d312e676ecbbf283369c231b903 100644 (file)
@@ -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.
index acb4ff2ff46eb7c1528113412393b6dc683bc009..12f2f6f06ac5878f16f78610f90c63c05164aa66 100644 (file)
@@ -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
 #...
index 1b13020835caee429429f809c62a732899c5f6bf..dca7abd5b1a56a680f69ac444085ca5799ab9662 100644 (file)
@@ -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
index a04a5ccf46e0f79df59e3da35ee94692b334e363..5c712ab5ddd51b926f982235371c26156ab1320f 100644 (file)
@@ -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
index 52a313a7d6fe662e64ad6bd9438cef5e64978177..91473dfb9534bb3bfccf912e8b110b5602b77083 100644 (file)
@@ -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
index 2ade9b6d17355c13581a896f0c22379d4b04fdba..02ec04946a5420577d15fbb7c7f3cfdf6a46873c 100644 (file)
@@ -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
index 19469848c4166517efe6e8655027ca2f3172a470..55ed17d6f7e46031eb01318c916d8b034bc9f814 100644 (file)
@@ -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