[ELF] Reject error-prone meta characters in input section description

The lexer is overly permissive. When parsing file patterns in an input
section description and there is a missing `)`, we would accept many
non-sensible tokens (e.g. `}`) as patterns, leading to confusion, e.g.
`*(SORT_BY_ALIGNMENT(SORT_BY_NAME(.text*)) } PROVIDE_HIDDEN(__code_end = .)`
(#81804).

Ideally, the lexer should be stateful to report more errors like GNU ld
and get rid of hacks like `ScriptLexer::maybeSplitExpr`, but that would
require a large rewrite of the lexer. For now, just reject certain
non-wildcard meta characters to detect common mistakes.

Pull Request: https://github.com/llvm/llvm-project/pull/84130
This commit is contained in:
Fangrui Song
2024-03-06 17:19:59 -08:00
committed by GitHub
parent 318bff6811
commit 551e20d190
2 changed files with 37 additions and 11 deletions

View File

@@ -717,9 +717,19 @@ SmallVector<SectionPattern, 0> ScriptParser::readInputSectionsList() {
StringMatcher SectionMatcher;
// Break if the next token is ), EXCLUDE_FILE, or SORT*.
while (!errorCount() && peek() != ")" && peek() != "EXCLUDE_FILE" &&
peekSortKind() == SortSectionPolicy::Default)
while (!errorCount() && peekSortKind() == SortSectionPolicy::Default) {
StringRef s = peek();
if (s == ")" || s == "EXCLUDE_FILE")
break;
// Detect common mistakes when certain non-wildcard meta characters are
// used without a closing ')'.
if (!s.empty() && strchr("(){}", s[0])) {
skip();
setError("section pattern is expected");
break;
}
SectionMatcher.addPattern(unquote(next()));
}
if (!SectionMatcher.empty())
ret.push_back({std::move(excludeFilePat), std::move(SectionMatcher)});