[PATCH] D98306: [ELF] Support . and $ in expressions
Fangrui Song via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 9 16:44:47 PST 2021
MaskRay created this revision.
MaskRay added reviewers: grimar, jhenderson, peter.smith.
Herald added subscribers: luismarques, steven.zhang, s.egerton, PkmX, simoncook, arichardson, emaste.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
GNU ld supports `.` and `$` in symbol names while LLD doesn't support them in
`readPrimary` expressions. Using `.` can result in such an error:
https://github.com/ClangBuiltLinux/linux/issues/1318
ld.lld: error: ./arch/powerpc/kernel/vmlinux.lds:255: malformed number: .TOC.
>>> __toc_ptr = (DEFINED (.TOC.) ? .TOC. : ADDR (.got)) + 0x8000;
Allow `.` (ppc64 special symbol `.TOC.`) and `$` (RISC-V special symbol `__global_pointer$`).
Change `diag[3-5].test` to use an invalid character `^`.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D98306
Files:
lld/ELF/ScriptParser.cpp
lld/test/ELF/linkerscript/diag3.test
lld/test/ELF/linkerscript/diag4.test
lld/test/ELF/linkerscript/diag5.test
lld/test/ELF/linkerscript/symbol-name.test
Index: lld/test/ELF/linkerscript/symbol-name.test
===================================================================
--- /dev/null
+++ lld/test/ELF/linkerscript/symbol-name.test
@@ -0,0 +1,8 @@
+# REQUIRES: x86
+## Test that . and $ can be used by symbol names in expressions.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64 /dev/null -o %t.o
+# RUN: ld.lld -T %s %t.o -o /dev/null
+
+a0 = DEFINED(.TOC.) ? .TOC. : 0;
+a1 = DEFINED(__global_pointer$) ? __global_pointer$ : 0;
Index: lld/test/ELF/linkerscript/diag5.test
===================================================================
--- lld/test/ELF/linkerscript/diag5.test
+++ lld/test/ELF/linkerscript/diag5.test
@@ -6,9 +6,9 @@
SECTIONS {
.text : { *(.text) }
.keep : { *(.keep) }
- boom .temp : { *(.temp) }
+ boom ^temp : { *(.temp) }
}
-# CHECK: 9: malformed number: .temp
-# CHECK-NEXT: >>> boom .temp : { *(.temp) }
+# CHECK: 9: malformed number: ^temp
+# CHECK-NEXT: >>> boom ^temp : { *(.temp) }
# CHECK-NEXT: >>> ^
Index: lld/test/ELF/linkerscript/diag4.test
===================================================================
--- lld/test/ELF/linkerscript/diag4.test
+++ lld/test/ELF/linkerscript/diag4.test
@@ -6,9 +6,9 @@
SECTIONS {
.text : { *(.text) }
.keep : { *(.keep) }
- boom .temp : { *(.temp) }
+ boom ^temp : { *(.temp) }
}
-# CHECK: 9: malformed number: .temp
-# CHECK-NEXT: >>> boom .temp : { *(.temp) }
-# CHECK-NEXT: >>> ^
\ No newline at end of file
+# CHECK: 9: malformed number: ^temp
+# CHECK-NEXT: >>> boom ^temp : { *(.temp) }
+# CHECK-NEXT: >>> ^
Index: lld/test/ELF/linkerscript/diag3.test
===================================================================
--- lld/test/ELF/linkerscript/diag3.test
+++ lld/test/ELF/linkerscript/diag3.test
@@ -5,9 +5,9 @@
SECTIONS {
.text : { *(.text) }
.keep : { *(.keep) }
- boom .temp : { *(.temp) }
+ boom ^temp : { *(.temp) }
}
-# CHECK: 8: malformed number: .temp
-# CHECK-NEXT: >>> boom .temp : { *(.temp) }
+# CHECK: 8: malformed number: ^temp
+# CHECK-NEXT: >>> boom ^temp : { *(.temp) }
# CHECK-NEXT: >>> ^
Index: lld/ELF/ScriptParser.cpp
===================================================================
--- lld/ELF/ScriptParser.cpp
+++ lld/ELF/ScriptParser.cpp
@@ -1234,6 +1234,13 @@
error(location + ": undefined section " + cmd->name);
}
+static bool isValidSymbolName(StringRef s) {
+ auto valid = [](char c) {
+ return isAlnum(c) || c == '$' || c == '.' || c == '_';
+ };
+ return !s.empty() && !isDigit(s[0]) && llvm::all_of(s, valid);
+}
+
Expr ScriptParser::readPrimary() {
if (peek() == "(")
return readParenExpr();
@@ -1408,7 +1415,7 @@
return [=] { return *val; };
// Tok is a symbol name.
- if (!isValidCIdentifier(tok))
+ if (!isValidSymbolName(tok))
setError("malformed number: " + tok);
script->referencedSymbols.push_back(tok);
return [=] { return script->getSymbolValue(tok, location); };
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98306.329496.patch
Type: text/x-patch
Size: 2997 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210310/d17033c0/attachment.bin>
More information about the llvm-commits
mailing list