[ld] section address : ALIGN(align) and the maximum of input section alignments

Fangrui Song i@maskray.me
Wed Feb 26 05:23:00 GMT 2020


https://sourceware.org/binutils/docs/ld/Output-Section-Description.html
I have observed some strange rules while playing with the output section address and ALIGN.

cat > a.s <<e
  .globl _start; _start: ret
  .section .data.rel.ro,"aw"; .balign 8; .byte 0
  .data; .byte 0
  .section .data2,"aw"; .balign 8; .byte 0
  .section .data3,"aw"; .balign 32; .byte 0
  .bss; .balign 32; .byte 0
e

cat > a.x <<e
  SECTIONS {
    .text 0x10000 : { *(.text) }
    /* sh_addr is aligned to 16. */
    .data.rel.ro . : ALIGN(16) { *(.data.rel.ro) }

    .data 0x20000 : { *(.data) }
    /* The output section address is set without ALIGN. sh_addr is set to Dot, ignoring alignment. */
    /* sh_addralign is the maximum of input section alignments, 8. */
    .data2 . : { *(.data2) }
    /* sh_addr is aligned to 32. */
    .data3 : ALIGN(16) { *(.data3) }
    /* sh_addr is aligned to 16, ???????????? */
    .bss . : ALIGN(16) { *(.bss) }
  }
e

as a.s -o a.o
ld.bfd -T a.x a.o -o a

% readelf -WS a

Section Headers:
   [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
   [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
   [ 1] .text             PROGBITS        0000000000010000 001000 000001 00  AX  0   0  1
   [ 2] .data.rel.ro      PROGBITS        0000000000010010 001010 000001 00  WA  0   0 16
   [ 3] .data             PROGBITS        0000000000020000 002000 000001 00  WA  0   0  1
   [ 4] .data2            PROGBITS        0000000000020008 002008 000001 00  WA  0   0  8
   [ 5] .data3            PROGBITS        0000000000020020 002020 000001 00  WA  0   0 32
   [ 6] .bss              NOBITS          0000000000020030 002021 000011 00  WA  0   0 32
...


Why doesn't `.bss . : ALIGN(16)` respect the maximum of input section alignments (32)?

(I have verified that .bss being SHT_NOBITS is unrelated.)

The rule of sh_addr computation appears to be:

* ADDR is unset, ALIGN is unset => max_input_alignment
* ADDR is unset, ALIGN is set => max(ALIGN, max_input_alignment)
* ADDR is set, ALIGN is unset => max_input_alignment
* ADDR is set, ALIGN is set => ALIGN



More information about the Binutils mailing list