[PATCH] D29273: [ELF] - Added support of linkerscript's "/DISCARD/" for --emit-relocs

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 17 10:30:03 PST 2017


LGTM

George Rimar via Phabricator <reviews at reviews.llvm.org> writes:

> grimar updated this revision to Diff 88745.
> grimar added a comment.
>
> - Renamed testcases.
>
>
> https://reviews.llvm.org/D29273
>
> Files:
>   ELF/InputFiles.cpp
>   ELF/InputSection.h
>   ELF/LinkerScript.cpp
>   test/ELF/linkerscript/emit-relocs-discard.s
>   test/ELF/linkerscript/emit-relocs-ehframe-discard.s
>
> Index: test/ELF/linkerscript/emit-relocs-ehframe-discard.s
> ===================================================================
> --- test/ELF/linkerscript/emit-relocs-ehframe-discard.s
> +++ test/ELF/linkerscript/emit-relocs-ehframe-discard.s
> @@ -0,0 +1,11 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
> +# RUN: echo "SECTIONS { /DISCARD/ : { *(.eh_frame) } }" > %t.script
> +# RUN: ld.lld --emit-relocs --script %t.script %t1.o -o %t
> +# RUN: llvm-objdump -section-headers %t | FileCheck %s
> +
> +# CHECK-NOT: .rela.eh_frame
> +
> +.section .foo,"ax", at progbits
> +.cfi_startproc
> +.cfi_endproc
> Index: test/ELF/linkerscript/emit-relocs-discard.s
> ===================================================================
> --- test/ELF/linkerscript/emit-relocs-discard.s
> +++ test/ELF/linkerscript/emit-relocs-discard.s
> @@ -0,0 +1,14 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
> +# RUN: echo "SECTIONS { /DISCARD/ : { *(.bbb) } }" > %t.script
> +# RUN: ld.lld --emit-relocs --script %t.script %t.o -o %t1
> +# RUN: llvm-readobj -r %t1 | FileCheck %s
> +
> +# CHECK:      Relocations [
> +# CHECK-NEXT: ]
> +
> +.section .aaa,"", at progbits
> +.Lfoo:
> +
> +.section .bbb,"", at progbits
> +.long .Lfoo
> Index: ELF/LinkerScript.cpp
> ===================================================================
> --- ELF/LinkerScript.cpp
> +++ ELF/LinkerScript.cpp
> @@ -277,11 +277,7 @@
>    for (InputSectionBase<ELFT> *S : V) {
>      S->Live = false;
>      reportDiscarded(S);
> -
> -    InputSection<ELFT> *IS = dyn_cast<InputSection<ELFT>>(S);
> -    if (!IS || IS->DependentSections.empty())
> -      continue;
> -    discard(IS->DependentSections);
> +    discard(S->DependentSections);
>    }
>  }
>  
> Index: ELF/InputSection.h
> ===================================================================
> --- ELF/InputSection.h
> +++ ELF/InputSection.h
> @@ -128,6 +128,9 @@
>    // this but instead this->Repl.
>    InputSectionBase<ELFT> *Repl;
>  
> +  // InputSections that are dependent on us (reverse dependency for GC)
> +  llvm::TinyPtrVector<InputSectionBase<ELFT> *> DependentSections;
> +
>    // Returns the size of this section (even if this is a common or BSS.)
>    size_t getSize() const;
>  
> @@ -281,9 +284,6 @@
>    // to. The writer sets a value.
>    uint64_t OutSecOff = 0;
>  
> -  // InputSections that are dependent on us (reverse dependency for GC)
> -  llvm::TinyPtrVector<InputSectionBase<ELFT> *> DependentSections;
> -
>    static bool classof(const InputSectionData *S);
>  
>    InputSectionBase<ELFT> *getRelocatedSection();
> Index: ELF/InputFiles.cpp
> ===================================================================
> --- ELF/InputFiles.cpp
> +++ ELF/InputFiles.cpp
> @@ -322,8 +322,7 @@
>        if (Sec.sh_link >= Sections.size())
>          fatal(toString(this) + ": invalid sh_link index: " +
>                Twine(Sec.sh_link));
> -      auto *IS = cast<InputSection<ELFT>>(Sections[Sec.sh_link]);
> -      IS->DependentSections.push_back(Sections[I]);
> +      Sections[Sec.sh_link]->DependentSections.push_back(Sections[I]);
>      }
>    }
>  }
> @@ -407,8 +406,12 @@
>      // from the output, so returning `nullptr` for the normal case.
>      // However, if -emit-relocs is given, we need to leave them in the output.
>      // (Some post link analysis tools need this information.)
> -    if (Config->EmitRelocs)
> -      return make<InputSection<ELFT>>(this, &Sec, Name);
> +    if (Config->EmitRelocs) {
> +      InputSection<ELFT> *RelocSec = make<InputSection<ELFT>>(this, &Sec, Name);
> +      // We will not emit relocation section if target was discarded.
> +      Target->DependentSections.push_back(RelocSec);
> +      return RelocSec;
> +    }
>      return nullptr;
>    }
>    }


More information about the llvm-commits mailing list