Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 06a9119

Browse files
committed
MCParser: diagnose missing directional labels more clearly.
Before, ELF at least managed a diagnostic but it was a completely untraceable "undefined symbol" error. MachO had a variety of even worse behaviours: crash, emit corrupt file, or an equally bad message. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265984 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 8922930 commit 06a9119

File tree

5 files changed

+45
-19
lines changed

5 files changed

+45
-19
lines changed

lib/MC/MCParser/AsmParser.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ class AsmParser : public MCAsmParser {
146146
/// \brief List of bodies of anonymous macros.
147147
std::deque<MCAsmMacro> MacroLikeBodies;
148148

149+
/// \brief List of forward directional labels for diagnosis at the end.
150+
SmallVector<std::pair<SMLoc, MCSymbol *>, 4> DirectionalLabels;
151+
149152
/// Boolean tracking whether macro substitution is enabled.
150153
unsigned MacrosEnabledFlag : 1;
151154

@@ -683,18 +686,28 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
683686
// Targets that don't do subsections via symbols may not want this, though,
684687
// so conservatively exclude them. Only do this if we're finalizing, though,
685688
// as otherwise we won't necessarilly have seen everything yet.
686-
if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
687-
for (const auto &TableEntry : getContext().getSymbols()) {
688-
MCSymbol *Sym = TableEntry.getValue();
689-
// Variable symbols may not be marked as defined, so check those
690-
// explicitly. If we know it's a variable, we have a definition for
691-
// the purposes of this check.
692-
if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
693-
// FIXME: We would really like to refer back to where the symbol was
694-
// first referenced for a source location. We need to add something
695-
// to track that. Currently, we just point to the end of the file.
696-
return Error(getLexer().getLoc(), "assembler local symbol '" +
697-
Sym->getName() + "' not defined");
689+
if (!NoFinalize) {
690+
if (MAI.hasSubsectionsViaSymbols()) {
691+
for (const auto &TableEntry : getContext().getSymbols()) {
692+
MCSymbol *Sym = TableEntry.getValue();
693+
// Variable symbols may not be marked as defined, so check those
694+
// explicitly. If we know it's a variable, we have a definition for
695+
// the purposes of this check.
696+
if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined())
697+
// FIXME: We would really like to refer back to where the symbol was
698+
// first referenced for a source location. We need to add something
699+
// to track that. Currently, we just point to the end of the file.
700+
HadError |=
701+
Error(getLexer().getLoc(), "assembler local symbol '" +
702+
Sym->getName() + "' not defined");
703+
}
704+
}
705+
706+
// Temporary symbols like the ones for directional jumps don't go in the
707+
// symbol table. They also need to be diagnosed in all (final) cases.
708+
for (std::pair<SMLoc, MCSymbol *> &LocSym : DirectionalLabels) {
709+
if (LocSym.second->isUndefined())
710+
HadError |= Error(LocSym.first, "directional label undefined");
698711
}
699712
}
700713

@@ -904,7 +917,8 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
904917
Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
905918
Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
906919
if (IDVal == "b" && Sym->isUndefined())
907-
return Error(Loc, "invalid reference to undefined symbol");
920+
return Error(Loc, "directional label undefined");
921+
DirectionalLabels.push_back(std::make_pair(Loc, Sym));
908922
EndLoc = Lexer.getTok().getEndLoc();
909923
Lex(); // Eat identifier.
910924
}

test/MC/AArch64/error-location.s

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
// CHECK: <unknown>:0: error: Common symbol 'common' cannot be used in assignment expr
2424
.set v3, common
2525

26-
// CHECK: <unknown>:0: error: Undefined temporary symbol
27-
.word 5f
28-
2926
// CHECK: <unknown>:0: error: symbol 'undef' could not be evaluated in a subtraction expression
3027
.set v2, a-undef
3128

test/MC/ARM/error-location.s

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
@ CHECK: <unknown>:0: error: Common symbol 'common' cannot be used in assignment expr
2424
.set v3, common
2525

26-
@ CHECK: <unknown>:0: error: Undefined temporary symbol
27-
.word 5f
28-
2926
@ CHECK: <unknown>:0: error: symbol 'undef' could not be evaluated in a subtraction expression
3027
.set v2, a-undef
3128

test/MC/ELF/undefined-directional.s

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: not llvm-mc -triple x86_64-linux-gnu -filetype=obj -o /dev/null %s 2>&1 | FileCheck %s
2+
3+
// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: directional label undefined
4+
jmp 1b
5+
// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: directional label undefined
6+
jmp 1f
7+
// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: directional label undefined
8+
jmp 2f
9+

test/MC/MachO/undefined-directional.s

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: not llvm-mc -triple x86_64-apple-macosx -filetype=obj -o /dev/null %s 2>&1 | FileCheck %s
2+
3+
// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: directional label undefined
4+
jmp 1b
5+
// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: directional label undefined
6+
jmp 1f
7+
// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: directional label undefined
8+
jmp 2f
9+

0 commit comments

Comments
 (0)