RFC: Padding segments to eliminate gaps

Nick Clifton nickc@redhat.com
Fri Jan 24 12:14:36 GMT 2025


Hi Florian,

>>    Instead I have gone with a second method - adding a new option to
>>    objcopy that inserts padding sections that fill up segments so that
>>    they end at the start of the next segments.  The new sections are
>>    SHT_NOBITS so that they do not take up any extra space in the file
>>    (well apart from their section headers), and using objcopy means that
>>    the transformation can be performed post-link, possibly after
>>    stripping or other kinds of file munging.
> 
> Will those objects have the same fate?  In these sense that future
> tooling is likely to corrupt them?

No.  Or at least not with the tests I have performed.  I have been able
to strip the augmented binaries using the binutils, the elfutils and the
llvm versions of strip and they continue to work and to be correctly
padded.


> Could you show the readelf -SW output?  I'm kind of surprised that a
> NOBITS section appears in the readelf -lW output.  But reading the ELF
> specification, it sort of makes sense.

Sure, the output is attached.  I am also attaching the augmented binary
(in a compressed form) in case you wish to examine or experiment with it.


> Is there a chance you could teach the link editor to insert this
> padding?   

It would be a lot harder and less flexible. One of benefits of using
objcopy in this way is that it can be used on binaries that were not
created by ld.  So if gold or lld or mold have been used to create an
executable they can still be augmented by using objcopy.

Cheers
   Nick


-------------- next part --------------
A non-text attachment was scrubbed...
Name: a.stripped.xz
Type: application/x-xz
Size: 2520 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20250124/49fbcf9d/attachment.xz>
-------------- next part --------------
There are 41 section headers, starting at offset 0x3c50:

Section Headers:
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .note.gnu.property NOTE            0000000000400318 000318 000040 00   A  0   0  8
  [ 2] .note.gnu.build-id NOTE            0000000000400358 000358 000024 00   A  0   0  4
  [ 3] .note.ABI-tag     NOTE            000000000040037c 00037c 000020 00   A  0   0  4
  [ 4] .init             PROGBITS        0000000000401000 001000 00001b 00  AX  0   0  4
  [ 5] .plt              PROGBITS        0000000000401020 001020 000020 10  AX  0   0 16
  [ 6] .text             PROGBITS        0000000000401040 001040 000106 00  AX  0   0 16
  [ 7] .fini             PROGBITS        0000000000401148 001148 00000d 00  AX  0   0  4
  [ 8] .interp           PROGBITS        0000000000402000 002000 00001c 00   A  0   0  1
  [ 9] .gnu.hash         GNU_HASH        0000000000402020 002020 00001c 00   A 10   0  8
  [10] .dynsym           DYNSYM          0000000000402040 002040 000060 18   A 11   1  8
  [11] .dynstr           STRTAB          00000000004020a0 0020a0 00004a 00   A  0   0  1
  [12] .gnu.version      VERSYM          00000000004020ea 0020ea 000008 02   A 10   0  2
  [13] .gnu.version_r    VERNEED         00000000004020f8 0020f8 000030 00   A 11   1  8
  [14] .rela.dyn         RELA            0000000000402128 002128 000030 18   A 10   0  8
  [15] .rela.plt         RELA            0000000000402158 002158 000018 18  AI 10  23  8
  [16] .rodata           PROGBITS        0000000000402170 002170 000020 00   A  0   0  8
  [17] .eh_frame_hdr     PROGBITS        0000000000402190 002190 00002c 00   A  0   0  4
  [18] .eh_frame         PROGBITS        00000000004021c0 0021c0 00008c 00   A  0   0  8
  [19] .init_array       INIT_ARRAY      0000000000403df8 002df8 000008 08  WA  0   0  8
  [20] .fini_array       FINI_ARRAY      0000000000403e00 002e00 000008 08  WA  0   0  8
  [21] .dynamic          DYNAMIC         0000000000403e08 002e08 0001d0 10  WA 11   0  8
  [22] .got              PROGBITS        0000000000403fd8 002fd8 000010 08  WA  0   0  8
  [23] .got.plt          PROGBITS        0000000000403fe8 002fe8 000020 08  WA  0   0  8
  [24] .data             PROGBITS        0000000000404008 003008 000018 00  WA  0   0  8
  [25] .bss              NOBITS          0000000000404020 003020 000008 00  WA  0   0  1
  [26] .comment          PROGBITS        0000000000000000 003020 00005c 01  MS  0   0  1
  [27] .annobin.notes    STRTAB          0000000000000000 00307c 00014f 01  MS  0   0  1
  [28] .gnu.build.attributes NOTE            0000000000406028 0031cc 000144 00      0   0  4
  [29] .debug_aranges    PROGBITS        0000000000000000 003310 000030 00      0   0  1
  [30] .debug_info       PROGBITS        0000000000000000 003340 0000ac 00      0   0  1
  [31] .debug_abbrev     PROGBITS        0000000000000000 0033ec 00008d 00      0   0  1
  [32] .debug_line       PROGBITS        0000000000000000 003479 000054 00      0   0  1
  [33] .debug_str        PROGBITS        0000000000000000 0034cd 00005c 01  MS  0   0  1
  [34] .debug_line_str   PROGBITS        0000000000000000 003529 00005c 01  MS  0   0  1
  [35] .segment.pad.2    NOBITS          000000000040039c 00039c 000c64 00   A  0   0  1
  [36] .segment.pad.3    NOBITS          0000000000401155 001155 000eab 00  AX  0   0  1
  [37] .segment.pad.4    NOBITS          000000000040224c 00224c 000db4 00   A  0   0  1
  [38] .symtab           SYMTAB          0000000000000000 003588 000360 18     39  18  8
  [39] .strtab           STRTAB          0000000000000000 0038e8 0001ae 00      0   0  1
  [40] .shstrtab         STRTAB          0000000000000000 003a96 0001b8 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  D (mbind), l (large), p (processor specific)

Elf file type is EXEC (Executable file)
Entry point 0x401040
There are 10 program headers, starting at offset 64

Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  PHDR           0x000040 0x0000000000400040 0x0000000000400040 0x000230 0x000230 R   0x8
  INTERP         0x002000 0x0000000000402000 0x0000000000402000 0x00001c 0x00001c R   0x1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x000000 0x0000000000400000 0x0000000000400000 0x00039c 0x001000 R   0x1000
  LOAD           0x001000 0x0000000000401000 0x0000000000401000 0x000155 0x001000 R E 0x1000
  LOAD           0x002000 0x0000000000402000 0x0000000000402000 0x00024c 0x001000 R   0x1000
  LOAD           0x002df8 0x0000000000403df8 0x0000000000403df8 0x000228 0x000230 RW  0x1000
  DYNAMIC        0x002e08 0x0000000000403e08 0x0000000000403e08 0x0001d0 0x0001d0 RW  0x8
  NOTE           0x000318 0x0000000000400318 0x0000000000400318 0x000040 0x000040 R   0x8
  NOTE           0x000358 0x0000000000400358 0x0000000000400358 0x000044 0x000044 R   0x4
  GNU_PROPERTY   0x000318 0x0000000000400318 0x0000000000400318 0x000040 0x000040 R   0x8

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .interp 
   02     .note.gnu.property .note.gnu.build-id .note.ABI-tag .segment.pad.2 
   03     .init .plt .text .fini .segment.pad.3 
   04     .interp .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .rodata .eh_frame_hdr .eh_frame .segment.pad.4 
   05     .init_array .fini_array .dynamic .got .got.plt .data .bss 
   06     .dynamic 
   07     .note.gnu.property 
   08     .note.gnu.build-id .note.ABI-tag 
   09     .note.gnu.property 


More information about the Binutils mailing list