<div dir="ltr"><div dir="ltr" style="font-size:12.8px">Hi Rafael, <div><br></div><div>It is a bit odd. <div><div><br></div><div>Our platform has this requirement for historical reasons, mostly either:</div><div><br></div><div>1. There is a mismatch between the flags assigned to sections by the compiler and the sections to segments mapping.</div><div>2. The section header flags are used to control the behaviour of binary manipulation tools.</div><div><br></div><div>These phdrs mostly get an address of 0 and an image size of 0.<br></div><div><br></div><div>Having said that the current lld behaviour is *very* odd.</div><div>If you assign a section to a phdr in the linker script it really should end up in that phdr.</div><div>Any other behaviour is confusing to the user and more difficult to implement.</div></div></div></div><div class="gmail-yj6qo gmail-ajU" style="margin:2px 0px 0px;font-size:12.8px"><div id="gmail-:n2" class="gmail-ajR" tabindex="0"><img class="gmail-ajT" src="https://ssl.gstatic.com/ui/v1/icons/mail/images/cleardot.gif" style="opacity: 0.3;"></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 14, 2017 at 6:36 PM, Rafael Avila de Espindola via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This sounds odd. What is the use of this feature?<br>
<br>
Cheers,<br>
Rafael<br>
<div><div class="h5"><br>
Andrew Ng via Phabricator <<a href="mailto:reviews@reviews.llvm.org">reviews@reviews.llvm.org</a>> writes:<br>
<br>
> andrewng created this revision.<br>
> Herald added a subscriber: emaste.<br>
><br>
> This patch makes changes to allow sections without the SHF_ALLOC bit to be<br>
> assigned to segments in a linker script.<br>
><br>
> The assignment of output sections to segments is performed in<br>
> LinkerScript::createPhdrs. Previously, this function would bail as soon as it<br>
> encountered an output section which did not have the SHF_ALLOC bit set, thus<br>
> preventing any output section without SHF_ALLOC from being assigned to a<br>
> segment.<br>
><br>
> This restriction has now been removed from LinkerScript::createPhdrs and instead<br>
> a check for SHF_ALLOC has been added to LinkerScript::<wbr>adjustSectionsAfterSorting<br>
> to not propagate program headers to sections without SHF_ALLOC.<br>
><br>
><br>
> <a href="https://reviews.llvm.org/D34204" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D34204</a><br>
><br>
> Files:<br>
>   ELF/LinkerScript.cpp<br>
>   test/ELF/linkerscript/non-<wbr>alloc-segment.s<br>
><br>
><br>
> Index: test/ELF/linkerscript/non-<wbr>alloc-segment.s<br>
> ==============================<wbr>==============================<wbr>=======<br>
> --- /dev/null<br>
> +++ test/ELF/linkerscript/non-<wbr>alloc-segment.s<br>
> @@ -0,0 +1,28 @@<br>
> +# REQUIRES: x86<br>
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o<br>
> +<br>
> +## Test that non-alloc section .foo can be assigned to a segment.<br>
> +# RUN: echo "PHDRS {text PT_LOAD; foo 0x12345678;} \<br>
> +# RUN:       SECTIONS { \<br>
> +# RUN:           .text : {*(.text .text*)} :text \<br>
> +# RUN:           .foo : {*(.foo)} :foo \<br>
> +# RUN:       }" > %t.script<br>
> +# RUN: ld.lld -o %t --script %t.script %t.o<br>
> +# RUN: llvm-readobj -elf-output-style=GNU -s -l %t | FileCheck %s<br>
> +<br>
> +# CHECK: Program Headers:<br>
> +# CHECK-NEXT:  Type<br>
> +# CHECK-NEXT:  LOAD<br>
> +# CHECK-NEXT:  <unknown>: 0x12345678<br>
> +<br>
> +# CHECK:      Section to Segment mapping:<br>
> +# CHECK-NEXT:  Segment Sections...<br>
> +# CHECK-NEXT:   00     .text<br>
> +# CHECK-NEXT:   01     .foo<br>
> +<br>
> +.global _start<br>
> +_start:<br>
> + nop<br>
> +<br>
> +.section .foo<br>
> + .long 0<br>
> Index: ELF/LinkerScript.cpp<br>
> ==============================<wbr>==============================<wbr>=======<br>
> --- ELF/LinkerScript.cpp<br>
> +++ ELF/LinkerScript.cpp<br>
> @@ -753,10 +753,15 @@<br>
>      if (!Cmd)<br>
>        continue;<br>
><br>
> -    if (Cmd->Phdrs.empty())<br>
> -      Cmd->Phdrs = DefPhdrs;<br>
> -    else<br>
> +    if (Cmd->Phdrs.empty()) {<br>
> +      OutputSection *Sec = Cmd->Sec;<br>
> +      // Only propagate program headers to sections that are allocated.<br>
> +      if (Sec && (Sec->Flags & SHF_ALLOC))<br>
> +        Cmd->Phdrs = DefPhdrs;<br>
> +    }<br>
> +    else {<br>
>        DefPhdrs = Cmd->Phdrs;<br>
> +    }<br>
>    }<br>
><br>
>    removeEmptyCommands();<br>
> @@ -975,8 +980,6 @@<br>
>    // Add output sections to program headers.<br>
>    for (OutputSectionCommand *Cmd : OutputSectionCommands) {<br>
>      OutputSection *Sec = Cmd->Sec;<br>
> -    if (!(Sec->Flags & SHF_ALLOC))<br>
> -      break;<br>
><br>
>      // Assign headers specified by linker script<br>
>      for (size_t Id : getPhdrIndices(Sec)) {<br>
><br>
><br>
> Index: test/ELF/linkerscript/non-<wbr>alloc-segment.s<br>
> ==============================<wbr>==============================<wbr>=======<br>
> --- /dev/null<br>
> +++ test/ELF/linkerscript/non-<wbr>alloc-segment.s<br>
> @@ -0,0 +1,28 @@<br>
> +# REQUIRES: x86<br>
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o<br>
> +<br>
> +## Test that non-alloc section .foo can be assigned to a segment.<br>
> +# RUN: echo "PHDRS {text PT_LOAD; foo 0x12345678;} \<br>
> +# RUN:       SECTIONS { \<br>
> +# RUN:           .text : {*(.text .text*)} :text \<br>
> +# RUN:           .foo : {*(.foo)} :foo \<br>
> +# RUN:       }" > %t.script<br>
> +# RUN: ld.lld -o %t --script %t.script %t.o<br>
> +# RUN: llvm-readobj -elf-output-style=GNU -s -l %t | FileCheck %s<br>
> +<br>
> +# CHECK: Program Headers:<br>
> +# CHECK-NEXT:  Type<br>
> +# CHECK-NEXT:  LOAD<br>
> +# CHECK-NEXT:  <unknown>: 0x12345678<br>
> +<br>
> +# CHECK:      Section to Segment mapping:<br>
> +# CHECK-NEXT:  Segment Sections...<br>
> +# CHECK-NEXT:   00     .text<br>
> +# CHECK-NEXT:   01     .foo<br>
> +<br>
> +.global _start<br>
> +_start:<br>
> + nop<br>
> +<br>
> +.section .foo<br>
> + .long 0<br>
> Index: ELF/LinkerScript.cpp<br>
> ==============================<wbr>==============================<wbr>=======<br>
> --- ELF/LinkerScript.cpp<br>
> +++ ELF/LinkerScript.cpp<br>
> @@ -753,10 +753,15 @@<br>
>      if (!Cmd)<br>
>        continue;<br>
><br>
> -    if (Cmd->Phdrs.empty())<br>
> -      Cmd->Phdrs = DefPhdrs;<br>
> -    else<br>
> +    if (Cmd->Phdrs.empty()) {<br>
> +      OutputSection *Sec = Cmd->Sec;<br>
> +      // Only propagate program headers to sections that are allocated.<br>
> +      if (Sec && (Sec->Flags & SHF_ALLOC))<br>
> +        Cmd->Phdrs = DefPhdrs;<br>
> +    }<br>
> +    else {<br>
>        DefPhdrs = Cmd->Phdrs;<br>
> +    }<br>
>    }<br>
><br>
>    removeEmptyCommands();<br>
> @@ -975,8 +980,6 @@<br>
>    // Add output sections to program headers.<br>
>    for (OutputSectionCommand *Cmd : OutputSectionCommands) {<br>
>      OutputSection *Sec = Cmd->Sec;<br>
> -    if (!(Sec->Flags & SHF_ALLOC))<br>
> -      break;<br>
><br>
>      // Assign headers specified by linker script<br>
>      for (size_t Id : getPhdrIndices(Sec)) {<br>
</div></div>______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>