[LLD] Fix incorrect behavior when using the "PROVIDE" directive #232318
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lld-elf
@llvm/pr-subscribers-lld
Author: Timur Baidusenov (bai-tim)
<details>
<summary>Changes</summary>
Symbols defined with --defsym are now considered defined before linker script commands are processed, so a PROVIDE directive can no longer override their value (GNU ld compatible behavior), fixing incorrect section sizes when -T and --defsym are used together.
---
Full diff: https://github.com/llvm/llvm-project/pull/232318.diff
4 Files Affected:
````````````diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index c69b0616a23678..43f5cc4cd3c021 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -1973,6 +1973,11 @@ bool LinkerScript::shouldAddProvideSym(StringRef symName) {
// undefined references from the RHS, the result of this function for a
// symbol must be the same for each call. We use unusedProvideSyms to not
// change the return value of a demoted symbol.
+ // Symbols defined with --defsym are considered defined before linker script
+ // commands are processed (GNU ld behavior), so a PROVIDE directive never
+ // defines such a symbol.
+ if (defsymSymbols.contains(symName))
+ return false;
Symbol *sym = ctx.symtab->find(symName);
if (!sym)
return false;
diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h
index 80c4f564afabc1..cda021a4cee19f 100644
--- a/lld/ELF/LinkerScript.h
+++ b/lld/ELF/LinkerScript.h
@@ -19,6 +19,7 @@
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Compiler.h"
#include <cstddef>
#include <cstdint>
@@ -433,6 +434,11 @@ class LinkerScript final {
// A list of symbols referenced by the script.
SmallVector<llvm::StringRef, 0> referencedSymbols;
+ // Symbols defined with --defsym. GNU ld treats them as defined before linker
+ // script commands are processed, so PROVIDE directives cannot override them.
+ // StringSet owns the stored strings, so they stay valid for the whole link.
+ llvm::StringSet<> defsymSymbols;
+
// Used to implement INSERT [AFTER|BEFORE]. Contains output sections that need
// to be reordered.
SmallVector<InsertCommand, 0> insertCommands;
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index 0c54fde4882926..400e0c64a03384 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -300,6 +300,14 @@ void ScriptParser::readDefsym() {
Expr e = readExpr();
if (!atEOF())
setError("EOF expected, but got " + next());
+ // Symbols defined with --defsym are treated as defined before linker script
+ // commands are processed, so that e.g. they take precedence over a PROVIDE
+ // directive (GNU ld compatible). The assignment itself is kept at its
+ // command-line position, and script assignments are still evaluated in the
+ // order they appear on the command line. defsymSymbols is a StringSet and
+ // stores an owned copy of the name, since the source string may come from
+ // the command line and does not necessarily outlive the link.
+ ctx.script->defsymSymbols.insert(name);
auto *cmd = make<SymbolAssignment>(name, e, 0, curBuf.filename.str());
ctx.script->sectionCommands.push_back(cmd);
}
diff --git a/lld/test/ELF/provide-defsym.s b/lld/test/ELF/provide-defsym.s
new file mode 100644
index 00000000000000..069cf72b90fcb9
--- /dev/null
+++ b/lld/test/ELF/provide-defsym.s
@@ -0,0 +1,39 @@
+# REQUIRES: x86
+
+## A symbol defined by --defsym takes precedence over a PROVIDE directive and
+## its value is used in linker script expressions, regardless of whether -T
+## or --defsym comes first on the command line (GNU ld compatible).
+
+# RUN: rm -rf %t && split-file %s %t && cd %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux a.s -o a.o
+# RUN: ld.lld -o t1 a.o -T script.ld --defsym=_a=20
+# RUN: ld.lld -o t2 a.o --defsym=_a=20 -T script.ld
+# RUN: ld.lld -o t3 a.o -T script.ld
+# RUN: llvm-readelf -s t1 | FileCheck %s --check-prefix=DEFSYM
+# RUN: llvm-readelf -s t2 | FileCheck %s --check-prefix=DEFSYM
+# RUN: llvm-readelf -W -S t1 | FileCheck %s --check-prefix=DEFSYM-SIZE
+# RUN: llvm-readelf -W -S t2 | FileCheck %s --check-prefix=DEFSYM-SIZE
+# RUN: llvm-readelf -W -S t3 | FileCheck %s --check-prefix=PROVIDE-SIZE
+
+# DEFSYM: 0000000000000014 0 NOTYPE GLOBAL DEFAULT ABS _a
+
+## _a * 1024 = 20 * 1024 = 0x5000.
+# DEFSYM-SIZE: .test {{.*}} 005000
+## _a * 1024 = 10 * 1024 = 0x2800.
+# PROVIDE-SIZE: .test {{.*}} 002800
+
+#--- a.s
+.text
+.globl _start
+_start:
+ ret
+
+#--- script.ld
+PROVIDE(_a = 10);
+SECTIONS
+{
+ .test :
+ {
+ . += _a * 1024;
+ }
+}
</details>
Could you please review @MaskRay @nga888 @keith @smithp35
I don't think this is the case https://sourceware.org/binutils/docs/ld/Options.html#index-symbols_002c-from-command-line
The linker processes ‘--defsym’ arguments and ‘-T’ arguments in order, placing ‘--defsym’ before ‘-T’ will define the symbol before the linker script from ‘-T’ is processed, while placing ‘--defsym’ after ‘-T’ will define the symbol after the linker script has been processed. This difference has consequences for expressions within the linker script that use the ‘--defsym’ symbols, which order is correct will depend on what you are trying to achieve.
As I understand it with --defsym before -T we have an equivalent linker script of
_a = 20;
PROVIDE(_a = 10);
SECTIONS
{
.test :
{
. += _a * 1024;
}
}
And with --defsym after -T we have an equivalent linker script of
PROVIDE(_a = 10);
SECTIONS
{
.text :
{
. += _a * 1024;
}
}
_a = 20;
It seems like GNU ld and lld process these differently. With GNU ld and _a = 20; at the end the symbol _a is set to 10, but _a = 20 is not a redefinition, it just updates the value. For GNU ld when the script is evaluated the second time the PROVIDE never executes because _a is defined so in effect _a is 20.
I'm not sure that behaviour from GNU ld is actually desired. It definitely behaves differently to a script of
_a = 10;
SECTIONS
{
.text :
{
. += _a * 1024;
}
}
_a = 20;
I think it would be worth checking with binutils what the intended behaviour is?
I don't think there is a need to repeat the same comment 3 times (modulo small wording changes).
From a quick example I think lld may be correct and ld.bfd could be wrong (using its documentation rather than its implementation). I've left a more detailed comment in the source.
From what I can tell both GNU ld and lld implement the ordering of --defsym correctly according to the ld.bfd documentation. In GNU ld's case it looks like a PROVIDE is only evaluated once, whereas in lld the PROVIDE assignment is evaluated each time.
I think if this were to be "fixed" to behave like GNU ld, then we should fix the linker script differences, which should cause the defsym ordering difference to disappear.
However I think LLD's behaviour may be closer to the intended behaviour than GNU ld, so I think we would want a consesnsus from LLD users/developers that this were the right thing to do first.
In your description:
fixing incorrect section sizes when -T and --defsym are used together.
I think that is specific to your test case, so I think that should be removed.
smithp35
bai-tim
Symbols defined with --defsym are now considered defined before linker script commands are processed, so a PROVIDE directive can no longer override their value (GNU ld compatible behavior).