[PATCH] D83758: [lld][linkerscript] Fix handling of DEFINED.

Hafiz Abid Qadeer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 14 03:28:21 PDT 2020


abidh created this revision.
abidh added reviewers: MaskRay, ruiu.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D83758

Files:
  lld/ELF/ScriptParser.cpp
  lld/test/ELF/linkerscript/Inputs/extern_defined.s
  lld/test/ELF/linkerscript/extern_defined.test


Index: lld/test/ELF/linkerscript/extern_defined.test
===================================================================
--- /dev/null
+++ lld/test/ELF/linkerscript/extern_defined.test
@@ -0,0 +1,5 @@
+# RUN: llvm-mc -filetype=obj  %p/Inputs/extern_defined.s -o %t.o
+# RUN: ld.lld -o %t3 --script %s %t.o
+
+EXTERN(__symbol1)
+__my_start = DEFINED(__symbol1) ? __symbol1 : __my_start;
Index: lld/test/ELF/linkerscript/Inputs/extern_defined.s
===================================================================
--- /dev/null
+++ lld/test/ELF/linkerscript/Inputs/extern_defined.s
@@ -0,0 +1,6 @@
+  .section .text
+
+__my_start:
+  .global __my_start
+
+
Index: lld/ELF/ScriptParser.cpp
===================================================================
--- lld/ELF/ScriptParser.cpp
+++ lld/ELF/ScriptParser.cpp
@@ -1310,7 +1310,10 @@
   }
   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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83758.277754.patch
Type: text/x-patch
Size: 1148 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200714/51dae62d/attachment.bin>


More information about the llvm-commits mailing list