public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [ld] section address : ALIGN(align) and the maximum of input section alignments
@ 2020-02-26  5:23 Fangrui Song
  2020-02-26  6:31 ` Fangrui Song
  0 siblings, 1 reply; 11+ messages in thread
From: Fangrui Song @ 2020-02-26  5:23 UTC (permalink / raw)
  To: binutils

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

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-03-10 19:00 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-26  5:23 [ld] section address : ALIGN(align) and the maximum of input section alignments Fangrui Song
2020-02-26  6:31 ` Fangrui Song
2020-03-03  5:46   ` Fangrui Song
2020-03-03 22:39   ` Alan Modra
2020-03-04  0:03     ` Fangrui Song
     [not found]     ` <BY5PR07MB7316674DD98011D2AD812A63CBE40@BY5PR07MB7316.namprd07.prod.outlook.com>
2020-03-04  5:56       ` Alan Modra
2020-03-04  6:40         ` Fangrui Song
2020-03-04  8:04           ` Alan Modra
2020-03-05  6:41             ` Fangrui Song
2020-03-05 11:21               ` Alan Modra
2020-03-10 19:00                 ` Fangrui Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).