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

Fangrui Song i@maskray.me
Wed Mar 4 06:40:00 GMT 2020


On Tue, Mar 3, 2020 at 9:56 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Tue, Mar 03, 2020 at 03:58:12PM -0800, Fangrui Song wrote:
> > Thanks. For the example below, do you agree with the comments in a.x below?
> >
> > % cat a.s
> > .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
> > % as a.s -o a.o
> >
> > % cat a.x
> > SECTIONS {
> >   .text 0x10000 : { *(.text) }
> >   /* sh_addr is 0x10010. Specifying both address and ALIGN should be
> > disallowed. */
> >   .data.rel.ro . : ALIGN(16) { *(.data.rel.ro) }
> >
> >   .data 0x20000 : { *(.data) }
> >   /* sh_addr is 0x20001. Should there be a warning that sh_addralign
> > is 8? Even --warn-section-align does not warn. */
>
> That is a bug.
>
> The ELF gABI says in part of sh_addralign:  "The value of sh_addr must
> be congruent to 0, modulo the value of sh_addralign."
>
>         * elf.c (elf_fake_sections): Ensure sh_addralign is such that
>         sh_addr mod sh_addalign is zero.
>
> diff --git a/bfd/elf.c b/bfd/elf.c
> index fcd84d2d17..c4d6718aaa 100644
> --- a/bfd/elf.c
> +++ b/bfd/elf.c
> @@ -3192,6 +3192,7 @@ elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
>    unsigned int sh_type;
>    const char *name = asect->name;
>    bfd_boolean delay_st_name_p = FALSE;
> +  bfd_vma mask;
>
>    if (arg->failed)
>      {
> @@ -3291,7 +3292,10 @@ elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
>        arg->failed = TRUE;
>        return;
>      }
> -  this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
> +  /* Set sh_addralign to the highest power of two given by alignment
> +     consistent with the section VMA.  Linker scripts can force VMA.  */
> +  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
> +  this_hdr->sh_addralign = mask & -mask;
>    /* The sh_entsize and sh_info fields may have been set already by
>       copy_private_section_data.  */
>
>
> >   .data2 . : { *(.data2) }
> >   /* sh_addr is 0x20020. The input section alignment wins. */
> >   .data3 : ALIGN(16) { *(.data3) }
> >   /* sh_addr is 0x20030. Specifying both address and ALIGN should be
> > disallowed. */
> >   .bss . : ALIGN(16) { *(.bss) }
> > }
> >
> > % ld.bfd -T a.x a.s -o a
> >
> > If specifying both Output Section Address and ALIGN is disallowed,
> > there should be no "changing start of section" warning when
> > --warn-section-align is not specified. Is my understanding correct?
> >
> > BTW, I filed a bug about duplicate "changing start of section"
> > warnings https://sourceware.org/bugzilla/show_bug.cgi?id=25570

The implementation is complex. For users to understand, I think it
will be helpful to have something more detailed in
https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address

If my understanding is correct
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=233bf4f847b136705247e2f7f11bae41c72448a4
 makes the output section address override sh_addralign computed from
the maximum of input section alignments. So, generally the rules are:

* The max of ALIGN and (the maximum of input section alignments) is taken.
* The output section address overrides the above. If sh_addr %
alignment != 0, set sh_addralign to the largest alignment that makes
sh_addr%alignment=0
  In this case, should the linker emit a warning?
* ALIGN and the output section address cannot be specified at the same
time. This is considered a linker script "undefined behavior". Users
should not rely on a particular result.

--warn-section-align may be out of place. It can be noisy for normal
output section descriptions like    .foo : ALIGN(16) { ... }  without
a preceding dot advancing to a multiple of 16.



More information about the Binutils mailing list