[PATCH] D26889: [ELF] - Implemented -no-rosegment.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 27 22:49:14 PST 2016


George Rimar <grimar at accesssoftek.com> writes:

I think you should also change compareSectionsNonScript. We don't have
to reorder ro/rx sections. It should use the new config variable and
that variable should be set when a linker script has SECTIONS.

> grimar created this revision.
> grimar added reviewers: ruiu, rafael.
> grimar added subscribers: llvm-commits, grimar, evgeny777, emaste.
>
> This is close to https://reviews.llvm.org/D26888, but implements only -no-rosegment flag.
> This allows to reduce the changes.
> So https://reviews.llvm.org/D26888 shows full version of functionality and can 
> be rebased if this one be landed.
>
>
> https://reviews.llvm.org/D26889
>
> Files:
>   ELF/Config.h
>   ELF/Driver.cpp
>   ELF/Options.td
>   ELF/Writer.cpp
>   test/ELF/segments.s
>
> Index: test/ELF/segments.s
> ===================================================================
> --- test/ELF/segments.s
> +++ test/ELF/segments.s
> @@ -0,0 +1,88 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
> +# RUN: ld.lld %t -o %t1
> +# RUN: llvm-readobj --program-headers %t1 | FileCheck --check-prefix=ROSEGMENT %s
> +
> +# ROSEGMENT:      ProgramHeader {
> +# ROSEGMENT:        Type: PT_LOAD
> +# ROSEGMENT-NEXT:    Offset: 0x0
> +# ROSEGMENT-NEXT:    VirtualAddress: 0x10000
> +# ROSEGMENT-NEXT:    PhysicalAddress: 0x10000
> +# ROSEGMENT-NEXT:    FileSize:
> +# ROSEGMENT-NEXT:    MemSize:
> +# ROSEGMENT-NEXT:    Flags [
> +# ROSEGMENT-NEXT:      PF_R
> +# ROSEGMENT-NEXT:    ]
> +# ROSEGMENT-NEXT:    Alignment: 4096
> +# ROSEGMENT-NEXT:  }
> +# ROSEGMENT-NEXT:  ProgramHeader {
> +# ROSEGMENT-NEXT:    Type: PT_LOAD
> +# ROSEGMENT-NEXT:    Offset: 0x1000
> +# ROSEGMENT-NEXT:    VirtualAddress: 0x11000
> +# ROSEGMENT-NEXT:    PhysicalAddress: 0x11000
> +# ROSEGMENT-NEXT:    FileSize:
> +# ROSEGMENT-NEXT:    MemSize:
> +# ROSEGMENT-NEXT:    Flags [
> +# ROSEGMENT-NEXT:      PF_R
> +# ROSEGMENT-NEXT:      PF_X
> +# ROSEGMENT-NEXT:    ]
> +# ROSEGMENT-NEXT:    Alignment: 4096
> +# ROSEGMENT-NEXT:  }
> +# ROSEGMENT-NEXT:  ProgramHeader {
> +# ROSEGMENT-NEXT:    Type: PT_LOAD
> +# ROSEGMENT-NEXT:    Offset: 0x2000
> +# ROSEGMENT-NEXT:    VirtualAddress: 0x12000
> +# ROSEGMENT-NEXT:    PhysicalAddress: 0x12000
> +# ROSEGMENT-NEXT:    FileSize: 1
> +# ROSEGMENT-NEXT:    MemSize: 1
> +# ROSEGMENT-NEXT:    Flags [
> +# ROSEGMENT-NEXT:      PF_R
> +# ROSEGMENT-NEXT:      PF_W
> +# ROSEGMENT-NEXT:    ]
> +# ROSEGMENT-NEXT:    Alignment: 4096
> +# ROSEGMENT-NEXT:  }
> +
> +# RUN: ld.lld -no-rosegment %t -o %t2
> +# RUN: llvm-readobj --program-headers %t2 | FileCheck --check-prefix=NOROSEGMENT %s
> +
> +# NOROSEGMENT:     ProgramHeader {
> +# NOROSEGMENT:       Type: PT_LOAD
> +# NOROSEGMENT-NEXT:   Offset: 0x0
> +# NOROSEGMENT-NEXT:   VirtualAddress: 0x10000
> +# NOROSEGMENT-NEXT:   PhysicalAddress: 0x10000
> +# NOROSEGMENT-NEXT:   FileSize:
> +# NOROSEGMENT-NEXT:   MemSize:
> +# NOROSEGMENT-NEXT:   Flags [
> +# NOROSEGMENT-NEXT:     PF_R
> +# NOROSEGMENT-NEXT:     PF_X
> +# NOROSEGMENT-NEXT:   ]
> +# NOROSEGMENT-NEXT:   Alignment: 4096
> +# NOROSEGMENT-NEXT: }
> +# NOROSEGMENT-NEXT: ProgramHeader {
> +# NOROSEGMENT-NEXT:   Type: PT_LOAD
> +# NOROSEGMENT-NEXT:   Offset: 0x1000
> +# NOROSEGMENT-NEXT:   VirtualAddress: 0x11000
> +# NOROSEGMENT-NEXT:   PhysicalAddress: 0x11000
> +# NOROSEGMENT-NEXT:   FileSize:
> +# NOROSEGMENT-NEXT:   MemSize:
> +# NOROSEGMENT-NEXT:   Flags [
> +# NOROSEGMENT-NEXT:     PF_R
> +# NOROSEGMENT-NEXT:     PF_W
> +# NOROSEGMENT-NEXT:   ]
> +# NOROSEGMENT-NEXT:   Alignment: 4096
> +# NOROSEGMENT-NEXT: }
> +# NOROSEGMENT-NEXT: ProgramHeader {
> +# NOROSEGMENT-NEXT:   Type: PT_GNU_STACK
> +
> +.global _start
> +_start:
> + nop
> +
> +.section .ro,"a"
> +nop
> +
> +.section .rw,"aw"
> +nop
> +
> +.section .rx,"ax"
> +nop
> Index: ELF/Writer.cpp
> ===================================================================
> --- ELF/Writer.cpp
> +++ ELF/Writer.cpp
> @@ -1095,6 +1095,15 @@
>    return F;
>  }
>  
> +// We apply -rosegment by default, -no-rosegment is used to avoid placing read
> +// only non-executable sections in their own segment.
> +template <class ELFT>
> +static bool shouldEmitLoad(typename ELFT::uint Old, typename ELFT::uint New) {
> +  if (Config->NoRosegment)
> +    return (Old & PF_W) != (New & PF_W);
> +  return Old != New;
> +}
> +

Instead of doing this, you can change computeFlags to use the new config
flag instead of HasSections.

I like the idea of implementing just --no-rosegment first. Thanks for
splitting the patch.

Cheers,
Rafael


More information about the llvm-commits mailing list