[lld] 1f166ed - [lld][linkerscript] Fix handling of DEFINED.

Hafiz Abid Qadeer via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 28 13:19:17 PDT 2020


Author: Hafiz Abid Qadeer
Date: 2020-07-28T21:18:01+01:00
New Revision: 1f166edeb47ea3584f4f6a267a9054af994af45c

URL: https://github.com/llvm/llvm-project/commit/1f166edeb47ea3584f4f6a267a9054af994af45c
DIFF: https://github.com/llvm/llvm-project/commit/1f166edeb47ea3584f4f6a267a9054af994af45c.diff

LOG: [lld][linkerscript] Fix handling of DEFINED.

Current implementation did not check that symbols is actually defined. Only checked for presence.  GNU ld documentation says,

"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."

https://sourceware.org/binutils/docs/ld/Builtin-Functions.html#Builtin-Functions

Reviewed By: MaskRay

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

Added: 
    

Modified: 
    lld/ELF/ScriptParser.cpp
    lld/test/ELF/linkerscript/Inputs/define.s
    lld/test/ELF/linkerscript/define.test

Removed: 
    


################################################################################
diff  --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index 17ac7ff6d5f4..eae1d17b2f43 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -1311,7 +1311,10 @@ Expr ScriptParser::readPrimary() {
   }
   if (tok == "DEFINED") {
     StringRef name = readParenLiteral();
-    return [=] { return symtab->find(name) ? 1 : 0; };
+    return [=] {
+      Symbol *b = symtab->find(name);
+      return (b && b->isDefined()) ? 1 : 0;
+    };
   }
   if (tok == "LENGTH") {
     StringRef name = readParenLiteral();

diff  --git a/lld/test/ELF/linkerscript/Inputs/define.s b/lld/test/ELF/linkerscript/Inputs/define.s
index bc60a233dcb4..69f47a880316 100644
--- a/lld/test/ELF/linkerscript/Inputs/define.s
+++ b/lld/test/ELF/linkerscript/Inputs/define.s
@@ -6,3 +6,6 @@ defined = 0
 
 .section .bar,"a"
 .quad 1
+
+.section .test,"a"
+.quad 1

diff  --git a/lld/test/ELF/linkerscript/define.test b/lld/test/ELF/linkerscript/define.test
index 689476ba32ad..3ecaa11cc5b6 100644
--- a/lld/test/ELF/linkerscript/define.test
+++ b/lld/test/ELF/linkerscript/define.test
@@ -3,13 +3,17 @@
 # RUN: ld.lld -o %t --script %s %t.o
 # RUN: llvm-objdump --section-headers %t | FileCheck %s
 
+EXTERN(extern_defined)
 SECTIONS {
   . = DEFINED(defined) ? 0x11000 : .;
   .foo : { *(.foo*) }
   . = DEFINED(notdefined) ? 0x12000 : 0x13000;
   .bar : { *(.bar*) }
+  . = DEFINED(extern_defined) ? 0x14000 : 0x15000;
+  .test : { *(.test*) }
 }
 
 # CHECK: 1 .foo  00000008 0000000000011000 DATA
 # CHECK: 2 .bar  00000008 0000000000013000 DATA
-# CHECK: 3 .text 00000000 0000000000013008 TEXT
+# CHECK: 3 .test  00000008 0000000000015000 DATA
+# CHECK: 4 .text 00000000 0000000000015008 TEXT


        


More information about the llvm-commits mailing list