[PATCH] D24858: [ELF] - Linkerscript: implement DEFINED() command.
Rafael Avila de Espindola via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 23 06:14:43 PDT 2016
Lgtm
On September 23, 2016 8:10:21 AM EDT, George Rimar <grimar at accesssoftek.com> wrote:
>grimar created this revision.
>grimar added reviewers: ruiu, rafael.
>grimar added subscribers: llvm-commits, grimar, davide, evgeny777.
>
>DEFINED(symbol)
>Return 1 if symbol is in the linker global symbol table and is defined
>before
>the statement using DEFINED in the script, otherwise return 0.
>
>Can be used to define default values for symbols. Found it in the wild
>here:
>https://searchcode.com/file/103745382/board/bf561-ezkit/u-boot.lds.S
>
>Patch implements it.
>
>https://reviews.llvm.org/D24858
>
>Files:
> ELF/LinkerScript.cpp
> ELF/LinkerScript.h
> test/ELF/linkerscript/define.s
>
>Index: test/ELF/linkerscript/define.s
>===================================================================
>--- test/ELF/linkerscript/define.s
>+++ test/ELF/linkerscript/define.s
>@@ -0,0 +1,25 @@
>+# REQUIRES: x86
>+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
>+
>+# RUN: echo "SECTIONS \
>+# RUN: { \
>+# RUN: . = DEFINED(defined) ? 0x11000 : .; \
>+# RUN: .foo : { *(.foo*) } \
>+# RUN: . = DEFINED(notdefined) ? 0x12000 : 0x13000; \
>+# RUN: .bar : { *(.bar*) } \
>+# RUN: }" > %t.script
>+# RUN: ld.lld -o %t1 --script %t.script %t
>+# RUN: llvm-objdump -section-headers %t1 | FileCheck %s
>+
>+# CHECK: 1 .foo 00000008 0000000000011000 DATA
>+# CHECK: 2 .bar 00000008 0000000000013000 DATA
>+# CHECK: 3 .text 00000000 0000000000013008 TEXT DATA
>+
>+.global defined
>+defined = 0
>+
>+.section .foo,"a"
>+.quad 1
>+
>+.section .bar,"a"
>+.quad 1
>Index: ELF/LinkerScript.h
>===================================================================
>--- ELF/LinkerScript.h
>+++ ELF/LinkerScript.h
>@@ -157,6 +157,7 @@
> virtual uint64_t getOutputSectionAlign(StringRef Name) = 0;
> virtual uint64_t getHeaderSize() = 0;
> virtual uint64_t getSymbolValue(StringRef S) = 0;
>+ virtual bool isDefined(StringRef S) = 0;
> };
>
> // ScriptConfiguration holds linker script parse results.
>@@ -203,6 +204,7 @@
> uint64_t getOutputSectionAlign(StringRef Name) override;
> uint64_t getHeaderSize() override;
> uint64_t getSymbolValue(StringRef S) override;
>+ bool isDefined(StringRef S) override;
>
> std::vector<OutputSectionBase<ELFT> *> *OutputSections;
>
>Index: ELF/LinkerScript.cpp
>===================================================================
>--- ELF/LinkerScript.cpp
>+++ ELF/LinkerScript.cpp
>@@ -756,6 +756,10 @@
> return 0;
> }
>
>+template <class ELFT> bool LinkerScript<ELFT>::isDefined(StringRef S)
>{
>+ return Symtab<ELFT>::X->find(S) != nullptr;
>+}
>+
>// Returns indices of ELF headers containing specific section,
>identified
>// by Name. Each index is a zero based number of ELF header listed
>within
> // PHDRS {} script block.
>@@ -1484,6 +1488,14 @@
> expect(")");
> return [=](uint64_t Dot) { return getConstant(Tok); };
> }
>+ if (Tok == "DEFINED") {
>+ expect("(");
>+ StringRef Tok = next();
>+ expect(")");
>+ return [=](uint64_t Dot) {
>+ return ScriptBase->isDefined(Tok) ? 1 : 0;
>+ };
>+ }
> if (Tok == "SEGMENT_START") {
> expect("(");
> next();
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160923/9e3b4859/attachment.html>
More information about the llvm-commits
mailing list