llvm-mc: Don't crash when using -n and we see a directive before the initial section.

- This is annoying, because we have to scatter this check everywhere that could emit real data, but I see no better solution.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@113552 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Daniel Dunbar
2010-09-09 22:42:59 +00:00
parent 93bd4d1e6b
commit 1ab6f2fa7a
2 changed files with 37 additions and 0 deletions

View File

@ -140,6 +140,8 @@ public:
/// } /// }
private: private:
void CheckForValidSection();
bool ParseStatement(); bool ParseStatement();
bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M); bool HandleMacroEntry(StringRef Name, SMLoc NameLoc, const Macro *M);
@ -399,6 +401,16 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
return HadError; return HadError;
} }
void AsmParser::CheckForValidSection() {
if (!getStreamer().getCurrentSection()) {
TokError("expected section directive before assembly directive");
Out.SwitchSection(Ctx.getMachOSection(
"__TEXT", "__text",
MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
0, SectionKind::getText()));
}
}
/// EatToEndOfStatement - Throw away the rest of the line for testing purposes. /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
void AsmParser::EatToEndOfStatement() { void AsmParser::EatToEndOfStatement() {
while (Lexer.isNot(AsmToken::EndOfStatement) && while (Lexer.isNot(AsmToken::EndOfStatement) &&
@ -759,6 +771,8 @@ bool AsmParser::ParseStatement() {
// See what kind of statement we have. // See what kind of statement we have.
switch (Lexer.getKind()) { switch (Lexer.getKind()) {
case AsmToken::Colon: { case AsmToken::Colon: {
CheckForValidSection();
// identifier ':' -> Label. // identifier ':' -> Label.
Lex(); Lex();
@ -911,6 +925,8 @@ bool AsmParser::ParseStatement() {
return false; return false;
} }
CheckForValidSection();
// Canonicalize the opcode to lower case. // Canonicalize the opcode to lower case.
SmallString<128> Opcode; SmallString<128> Opcode;
for (unsigned i = 0, e = IDVal.size(); i != e; ++i) for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
@ -1238,6 +1254,8 @@ bool AsmParser::ParseEscapedString(std::string &Data) {
/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ] /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) { bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
if (getLexer().isNot(AsmToken::EndOfStatement)) { if (getLexer().isNot(AsmToken::EndOfStatement)) {
CheckForValidSection();
for (;;) { for (;;) {
if (getLexer().isNot(AsmToken::String)) if (getLexer().isNot(AsmToken::String))
return TokError("expected string in '.ascii' or '.asciz' directive"); return TokError("expected string in '.ascii' or '.asciz' directive");
@ -1269,6 +1287,8 @@ bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
/// ::= (.byte | .short | ... ) [ expression (, expression)* ] /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
bool AsmParser::ParseDirectiveValue(unsigned Size) { bool AsmParser::ParseDirectiveValue(unsigned Size) {
if (getLexer().isNot(AsmToken::EndOfStatement)) { if (getLexer().isNot(AsmToken::EndOfStatement)) {
CheckForValidSection();
for (;;) { for (;;) {
const MCExpr *Value; const MCExpr *Value;
SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc(); SMLoc ATTRIBUTE_UNUSED StartLoc = getLexer().getLoc();
@ -1298,6 +1318,8 @@ bool AsmParser::ParseDirectiveValue(unsigned Size) {
/// ParseDirectiveSpace /// ParseDirectiveSpace
/// ::= .space expression [ , expression ] /// ::= .space expression [ , expression ]
bool AsmParser::ParseDirectiveSpace() { bool AsmParser::ParseDirectiveSpace() {
CheckForValidSection();
int64_t NumBytes; int64_t NumBytes;
if (ParseAbsoluteExpression(NumBytes)) if (ParseAbsoluteExpression(NumBytes))
return true; return true;
@ -1329,6 +1351,8 @@ bool AsmParser::ParseDirectiveSpace() {
/// ParseDirectiveFill /// ParseDirectiveFill
/// ::= .fill expression , expression , expression /// ::= .fill expression , expression , expression
bool AsmParser::ParseDirectiveFill() { bool AsmParser::ParseDirectiveFill() {
CheckForValidSection();
int64_t NumValues; int64_t NumValues;
if (ParseAbsoluteExpression(NumValues)) if (ParseAbsoluteExpression(NumValues))
return true; return true;
@ -1366,6 +1390,8 @@ bool AsmParser::ParseDirectiveFill() {
/// ParseDirectiveOrg /// ParseDirectiveOrg
/// ::= .org expression [ , expression ] /// ::= .org expression [ , expression ]
bool AsmParser::ParseDirectiveOrg() { bool AsmParser::ParseDirectiveOrg() {
CheckForValidSection();
const MCExpr *Offset; const MCExpr *Offset;
if (ParseExpression(Offset)) if (ParseExpression(Offset))
return true; return true;
@ -1396,6 +1422,8 @@ bool AsmParser::ParseDirectiveOrg() {
/// ParseDirectiveAlign /// ParseDirectiveAlign
/// ::= {.align, ...} expression [ , expression [ , expression ]] /// ::= {.align, ...} expression [ , expression [ , expression ]]
bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
CheckForValidSection();
SMLoc AlignmentLoc = getLexer().getLoc(); SMLoc AlignmentLoc = getLexer().getLoc();
int64_t Alignment; int64_t Alignment;
if (ParseAbsoluteExpression(Alignment)) if (ParseAbsoluteExpression(Alignment))
@ -1560,6 +1588,8 @@ bool AsmParser::ParseDirectiveELFType() {
/// ParseDirectiveComm /// ParseDirectiveComm
/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
bool AsmParser::ParseDirectiveComm(bool IsLocal) { bool AsmParser::ParseDirectiveComm(bool IsLocal) {
CheckForValidSection();
SMLoc IDLoc = getLexer().getLoc(); SMLoc IDLoc = getLexer().getLoc();
StringRef Name; StringRef Name;
if (ParseIdentifier(Name)) if (ParseIdentifier(Name))

View File

@ -0,0 +1,7 @@
// RUN: not llvm-mc -n -triple i386-unknown-unknown %s 2> %t
// RUN: FileCheck < %t %s
.globl a
// CHECK: error: expected section directive before assembly directive
.long 0