[PATCH] D34204: [LLD][LinkerScript] Allow non-alloc sections to be assigned to segments.

bd1976 llvm via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 15 06:22:58 PDT 2017


Hi Rafael,

It is a bit odd.

Our platform has this requirement for historical reasons, mostly either:

1. There is a mismatch between the flags assigned to sections by the
compiler and the sections to segments mapping.
2. The section header flags are used to control the behaviour of binary
manipulation tools.

These phdrs mostly get an address of 0 and an image size of 0.

Having said that the current lld behaviour is *very* odd.
If you assign a section to a phdr in the linker script it really should end
up in that phdr.
Any other behaviour is confusing to the user and more difficult to
implement.

On Wed, Jun 14, 2017 at 6:36 PM, Rafael Avila de Espindola via llvm-commits
<llvm-commits at lists.llvm.org> wrote:

> This sounds odd. What is the use of this feature?
>
> Cheers,
> Rafael
>
> Andrew Ng via Phabricator <reviews at reviews.llvm.org> writes:
>
> > andrewng created this revision.
> > Herald added a subscriber: emaste.
> >
> > This patch makes changes to allow sections without the SHF_ALLOC bit to
> be
> > assigned to segments in a linker script.
> >
> > The assignment of output sections to segments is performed in
> > LinkerScript::createPhdrs. Previously, this function would bail as soon
> as it
> > encountered an output section which did not have the SHF_ALLOC bit set,
> thus
> > preventing any output section without SHF_ALLOC from being assigned to a
> > segment.
> >
> > This restriction has now been removed from LinkerScript::createPhdrs and
> instead
> > a check for SHF_ALLOC has been added to LinkerScript::
> adjustSectionsAfterSorting
> > to not propagate program headers to sections without SHF_ALLOC.
> >
> >
> > https://reviews.llvm.org/D34204
> >
> > Files:
> >   ELF/LinkerScript.cpp
> >   test/ELF/linkerscript/non-alloc-segment.s
> >
> >
> > Index: test/ELF/linkerscript/non-alloc-segment.s
> > ===================================================================
> > --- /dev/null
> > +++ test/ELF/linkerscript/non-alloc-segment.s
> > @@ -0,0 +1,28 @@
> > +# REQUIRES: x86
> > +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
> > +
> > +## Test that non-alloc section .foo can be assigned to a segment.
> > +# RUN: echo "PHDRS {text PT_LOAD; foo 0x12345678;} \
> > +# RUN:       SECTIONS { \
> > +# RUN:           .text : {*(.text .text*)} :text \
> > +# RUN:           .foo : {*(.foo)} :foo \
> > +# RUN:       }" > %t.script
> > +# RUN: ld.lld -o %t --script %t.script %t.o
> > +# RUN: llvm-readobj -elf-output-style=GNU -s -l %t | FileCheck %s
> > +
> > +# CHECK: Program Headers:
> > +# CHECK-NEXT:  Type
> > +# CHECK-NEXT:  LOAD
> > +# CHECK-NEXT:  <unknown>: 0x12345678
> > +
> > +# CHECK:      Section to Segment mapping:
> > +# CHECK-NEXT:  Segment Sections...
> > +# CHECK-NEXT:   00     .text
> > +# CHECK-NEXT:   01     .foo
> > +
> > +.global _start
> > +_start:
> > + nop
> > +
> > +.section .foo
> > + .long 0
> > Index: ELF/LinkerScript.cpp
> > ===================================================================
> > --- ELF/LinkerScript.cpp
> > +++ ELF/LinkerScript.cpp
> > @@ -753,10 +753,15 @@
> >      if (!Cmd)
> >        continue;
> >
> > -    if (Cmd->Phdrs.empty())
> > -      Cmd->Phdrs = DefPhdrs;
> > -    else
> > +    if (Cmd->Phdrs.empty()) {
> > +      OutputSection *Sec = Cmd->Sec;
> > +      // Only propagate program headers to sections that are allocated.
> > +      if (Sec && (Sec->Flags & SHF_ALLOC))
> > +        Cmd->Phdrs = DefPhdrs;
> > +    }
> > +    else {
> >        DefPhdrs = Cmd->Phdrs;
> > +    }
> >    }
> >
> >    removeEmptyCommands();
> > @@ -975,8 +980,6 @@
> >    // Add output sections to program headers.
> >    for (OutputSectionCommand *Cmd : OutputSectionCommands) {
> >      OutputSection *Sec = Cmd->Sec;
> > -    if (!(Sec->Flags & SHF_ALLOC))
> > -      break;
> >
> >      // Assign headers specified by linker script
> >      for (size_t Id : getPhdrIndices(Sec)) {
> >
> >
> > Index: test/ELF/linkerscript/non-alloc-segment.s
> > ===================================================================
> > --- /dev/null
> > +++ test/ELF/linkerscript/non-alloc-segment.s
> > @@ -0,0 +1,28 @@
> > +# REQUIRES: x86
> > +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
> > +
> > +## Test that non-alloc section .foo can be assigned to a segment.
> > +# RUN: echo "PHDRS {text PT_LOAD; foo 0x12345678;} \
> > +# RUN:       SECTIONS { \
> > +# RUN:           .text : {*(.text .text*)} :text \
> > +# RUN:           .foo : {*(.foo)} :foo \
> > +# RUN:       }" > %t.script
> > +# RUN: ld.lld -o %t --script %t.script %t.o
> > +# RUN: llvm-readobj -elf-output-style=GNU -s -l %t | FileCheck %s
> > +
> > +# CHECK: Program Headers:
> > +# CHECK-NEXT:  Type
> > +# CHECK-NEXT:  LOAD
> > +# CHECK-NEXT:  <unknown>: 0x12345678
> > +
> > +# CHECK:      Section to Segment mapping:
> > +# CHECK-NEXT:  Segment Sections...
> > +# CHECK-NEXT:   00     .text
> > +# CHECK-NEXT:   01     .foo
> > +
> > +.global _start
> > +_start:
> > + nop
> > +
> > +.section .foo
> > + .long 0
> > Index: ELF/LinkerScript.cpp
> > ===================================================================
> > --- ELF/LinkerScript.cpp
> > +++ ELF/LinkerScript.cpp
> > @@ -753,10 +753,15 @@
> >      if (!Cmd)
> >        continue;
> >
> > -    if (Cmd->Phdrs.empty())
> > -      Cmd->Phdrs = DefPhdrs;
> > -    else
> > +    if (Cmd->Phdrs.empty()) {
> > +      OutputSection *Sec = Cmd->Sec;
> > +      // Only propagate program headers to sections that are allocated.
> > +      if (Sec && (Sec->Flags & SHF_ALLOC))
> > +        Cmd->Phdrs = DefPhdrs;
> > +    }
> > +    else {
> >        DefPhdrs = Cmd->Phdrs;
> > +    }
> >    }
> >
> >    removeEmptyCommands();
> > @@ -975,8 +980,6 @@
> >    // Add output sections to program headers.
> >    for (OutputSectionCommand *Cmd : OutputSectionCommands) {
> >      OutputSection *Sec = Cmd->Sec;
> > -    if (!(Sec->Flags & SHF_ALLOC))
> > -      break;
> >
> >      // Assign headers specified by linker script
> >      for (size_t Id : getPhdrIndices(Sec)) {
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170615/ef9bb229/attachment.html>


More information about the llvm-commits mailing list