This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Re: [ld] section address : ALIGN(align) and the maximum of input section alignments
- From: Fangrui Song <i at maskray dot me>
- To: Alan Modra <amodra at gmail dot com>
- Cc: binutils <binutils at sourceware dot org>
- Date: Mon, 2 Mar 2020 21:46:01 -0800
- Subject: Re: [ld] section address : ALIGN(align) and the maximum of input section alignments
- Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=maskray.me; dmarc=pass action=none header.from=maskray.me; dkim=pass header.d=maskray.me; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=V+TZ6efBZ6+lMyf5NmwCZCs+yf8B5TsWuYk/F04uOmA=; b=HonA3aIUCU12DULQo4qgfzu608gUF5Z4oHnmSDM0DU8F/attqndPRgQyyYyEms/ntTN0to3WzcknvHFQqtVUGzlVugZ0sjfEgl4WXTEP64n8H9znrcpFX4R0PIu270myMyYDzrfcaYnLQ+r714+eJxGMkk4Jmuk7/0u17+2t45t09A8YwMmLfJLPPrapV/cJzu4bCWzuYJNKHeQkkEiqcQZobFpAhmQXo9gztu1quneX8eTo31AAoeCvtbtdLilVtBb4AnG7jQYmP1qHaEvd4LK3pSHTcrIOxRUkAgDLXJRKlvOnijZRgYnNv0R3WbtCLj8u1hAYWt7qniPUdHEyRQ==
- Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LsL3lJkv0Wh8nVckinhXvp1DXStBDzzOT1n2c4a5/HSdJvl3kN8J9pek85J1S58V8JIx46uJnEIV1I2fhpYOpiVw2V/IRvZcxqwsiVwbko7n9p1AFpLdmZnAXJPbVlXfQUzvngEFhmpV4fsBxe47Hl6dSfUwy/4xXI2KBd+UNt4/aPR33qA9qCm4pRv8dr132PltguDIUYvzURI01GIy1Qb4PYVmzw/Qen4C2Fzx/u1YuOsnjTnyZ8FHPT71EjhkpaDI5tt7HxdfaVmTz2ywiesZ23m4/J3yloLC+PWim8LmriHTsgxMn2IALdqUuT0R2CLpJTP4jCBJQu60QjNiZw==
- References: <20200226052300.wazzfmivxta63vef@gmail.com> <20200226063003.4uvshiarho3wbkrl@google.com>
On Tue, Feb 25, 2020 at 10:30 PM Fangrui Song <i@maskray.me> wrote:
>
> On 2020-02-25, Fangrui Song wrote:
> >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
>
> When ADDR is set, ALIGN is used while max_input_alignment is ignored.
>
> ld/ldlang.c:5582
>
> if (os->addr_tree == NULL) {
> ...
> newdot = os->region->current;
> // maximum of ALIGN and input section alignments
> section_alignment = os->bfd_section->alignment_power;
> }
> else
> // ALIGN
> section_alignment = exp_get_power (os->section_alignment, "section alignment");
>
> Alan, is the `else` branch supposed to use `section_alignment = os->bfd_section->alignment_power;` as well?
Ping :)