public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Fangrui Song <i@maskray.me>
To: binutils <binutils@sourceware.org>
Subject: [ld] section address : ALIGN(align) and the maximum of input section alignments
Date: Wed, 26 Feb 2020 05:23:00 -0000	[thread overview]
Message-ID: <20200226052300.wazzfmivxta63vef@gmail.com> (raw)

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

             reply	other threads:[~2020-02-26  5:23 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-26  5:23 Fangrui Song [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200226052300.wazzfmivxta63vef@gmail.com \
    --to=i@maskray.me \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).