[lld] r276592 - [ELF] Fix the semantic of PROVIDE in linker scripts.

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 24 17:25:19 PDT 2016


Author: davide
Date: Sun Jul 24 19:25:18 2016
New Revision: 276592

URL: http://llvm.org/viewvc/llvm-project?rev=276592&view=rev
Log:
[ELF] Fix the semantic of PROVIDE in linker scripts.

PROVIDE request us to define a symbol only if it is referenced and is
not defined by any object included in the link. We created the
symbol in the symbol table no matter what.

Differential Revision:  https://reviews.llvm.org/D22739

Added:
    lld/trunk/test/ELF/linkerscript/linkerscript-symbolreferenced.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/test/ELF/linkerscript/linkerscript-symbols.s

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=276592&r1=276591&r2=276592&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Sun Jul 24 19:25:18 2016
@@ -350,7 +350,9 @@ template <class ELFT> void LinkerScript<
       continue;
 
     SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
-    if (!B || B->isUndefined())
+    // The semantic of PROVIDE is that of introducing a symbol only if
+    // it's not defined and there's at least a reference to it.
+    if ((!B && !Cmd->Provide) || (B && B->isUndefined()))
       Symtab<ELFT>::X->addAbsolute(Cmd->Name,
                                    Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT);
     else

Added: lld/trunk/test/ELF/linkerscript/linkerscript-symbolreferenced.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/linkerscript-symbolreferenced.s?rev=276592&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/linkerscript-symbolreferenced.s (added)
+++ lld/trunk/test/ELF/linkerscript/linkerscript-symbolreferenced.s Sun Jul 24 19:25:18 2016
@@ -0,0 +1,22 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+
+# Provide new symbol. The value should be 1, like set in PROVIDE()
+# RUN: echo "SECTIONS { PROVIDE(newsym = 1);}" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t
+# RUN: llvm-objdump -t %t1 | FileCheck --check-prefix=PROVIDE1 %s
+# PROVIDE1: 0000000000000001         *ABS*    00000000 newsym
+
+# Provide new symbol (hidden). The value should be 1
+# RUN: echo "SECTIONS { PROVIDE_HIDDEN(newsym = 1);}" > %t.script
+# RUN: ld.lld -o %t1 --script %t.script %t
+# RUN: llvm-objdump -t %t1 | FileCheck --check-prefix=HIDDEN1 %s
+# HIDDEN1: 0000000000000001         *ABS*    00000000 .hidden newsym
+
+.global _start
+_start:
+ nop
+
+.globl patatino
+patatino:
+  movl newsym, %eax

Modified: lld/trunk/test/ELF/linkerscript/linkerscript-symbols.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/linkerscript-symbols.s?rev=276592&r1=276591&r2=276592&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/linkerscript-symbols.s (original)
+++ lld/trunk/test/ELF/linkerscript/linkerscript-symbols.s Sun Jul 24 19:25:18 2016
@@ -9,17 +9,17 @@
 # RUN: llvm-objdump -t %t1 | FileCheck --check-prefix=SIMPLE %s
 # SIMPLE: 0000000000000121         *ABS*    00000000 text_end
 
-# Provide new symbol. The value should be 1, like set in PROVIDE()
+# The symbol is not referenced. Don't provide it.
 # RUN: echo "SECTIONS { PROVIDE(newsym = 1);}" > %t.script
 # RUN: ld.lld -o %t1 --script %t.script %t
 # RUN: llvm-objdump -t %t1 | FileCheck --check-prefix=PROVIDE1 %s
-# PROVIDE1: 0000000000000001         *ABS*    00000000 newsym
+# PROVIDE1-NOT: 0000000000000001         *ABS*    00000000 newsym
 
-# Provide new symbol (hidden). The value should be 1
+# The symbol is not referenced. Don't provide it.
 # RUN: echo "SECTIONS { PROVIDE_HIDDEN(newsym = 1);}" > %t.script
 # RUN: ld.lld -o %t1 --script %t.script %t
 # RUN: llvm-objdump -t %t1 | FileCheck --check-prefix=HIDDEN1 %s
-# HIDDEN1: 0000000000000001         *ABS*    00000000 .hidden newsym
+# HIDDEN1-NOT: 0000000000000001         *ABS*    00000000 .hidden newsym
 
 # Provide existing symbol. The value should be 0, even though we 
 # have value of 1 in PROVIDE()




More information about the llvm-commits mailing list