From 4a88cd08da9318d5d29cad4f9807ec395b341f68 Mon Sep 17 00:00:00 2001 From: Kevin Enderby Date: Tue, 25 Mar 2014 00:05:50 +0000 Subject: [PATCH] Fix crashes when assembler directives are used that are not for Mach-O object files by generating an error instead. rdar://16335232 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204687 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/ARM/AsmParser/ARMAsmParser.cpp | 65 ++++++++++++++++++++++- test/MC/MachO/ARM/bad-darwin-directives.s | 24 +++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 test/MC/MachO/ARM/bad-darwin-directives.s diff --git a/lib/Target/ARM/AsmParser/ARMAsmParser.cpp b/lib/Target/ARM/AsmParser/ARMAsmParser.cpp index d9d12672a10..d5897720b1f 100644 --- a/lib/Target/ARM/AsmParser/ARMAsmParser.cpp +++ b/lib/Target/ARM/AsmParser/ARMAsmParser.cpp @@ -8297,6 +8297,14 @@ bool ARMAsmParser::parseDirectiveUnreq(SMLoc L) { /// parseDirectiveArch /// ::= .arch token bool ARMAsmParser::parseDirectiveArch(SMLoc L) { + const MCAsmInfo *MAI = getParser().getStreamer().getContext().getAsmInfo(); + bool isMachO = MAI->hasSubsectionsViaSymbols(); + if (isMachO) { + Error(L, ".arch directive not valid for Mach-O"); + Parser.eatToEndOfStatement(); + return false; + } + StringRef Arch = getParser().parseStringToEndOfStatement().trim(); unsigned ID = StringSwitch(Arch) @@ -8320,9 +8328,16 @@ bool ARMAsmParser::parseDirectiveArch(SMLoc L) { /// ::= .eabi_attribute int, int [, "str"] /// ::= .eabi_attribute Tag_name, int [, "str"] bool ARMAsmParser::parseDirectiveEabiAttr(SMLoc L) { + const MCAsmInfo *MAI = getParser().getStreamer().getContext().getAsmInfo(); + bool isMachO = MAI->hasSubsectionsViaSymbols(); + if (isMachO) { + Error(L, ".eabi_attribute directive not valid for Mach-O"); + Parser.eatToEndOfStatement(); + return false; + } + int64_t Tag; SMLoc TagLoc; - TagLoc = Parser.getTok().getLoc(); if (Parser.getTok().is(AsmToken::Identifier)) { StringRef Name = Parser.getTok().getIdentifier(); @@ -8426,6 +8441,14 @@ bool ARMAsmParser::parseDirectiveEabiAttr(SMLoc L) { /// parseDirectiveCPU /// ::= .cpu str bool ARMAsmParser::parseDirectiveCPU(SMLoc L) { + const MCAsmInfo *MAI = getParser().getStreamer().getContext().getAsmInfo(); + bool isMachO = MAI->hasSubsectionsViaSymbols(); + if (isMachO) { + Error(L, ".cpu directive not valid for Mach-O"); + Parser.eatToEndOfStatement(); + return false; + } + StringRef CPU = getParser().parseStringToEndOfStatement().trim(); getTargetStreamer().emitTextAttribute(ARMBuildAttrs::CPU_name, CPU); return false; @@ -8434,6 +8457,14 @@ bool ARMAsmParser::parseDirectiveCPU(SMLoc L) { /// parseDirectiveFPU /// ::= .fpu str bool ARMAsmParser::parseDirectiveFPU(SMLoc L) { + const MCAsmInfo *MAI = getParser().getStreamer().getContext().getAsmInfo(); + bool isMachO = MAI->hasSubsectionsViaSymbols(); + if (isMachO) { + Error(L, ".fpu directive not valid for Mach-O"); + Parser.eatToEndOfStatement(); + return false; + } + StringRef FPU = getParser().parseStringToEndOfStatement().trim(); unsigned ID = StringSwitch(FPU) @@ -8453,6 +8484,14 @@ bool ARMAsmParser::parseDirectiveFPU(SMLoc L) { /// parseDirectiveFnStart /// ::= .fnstart bool ARMAsmParser::parseDirectiveFnStart(SMLoc L) { + const MCAsmInfo *MAI = getParser().getStreamer().getContext().getAsmInfo(); + bool isMachO = MAI->hasSubsectionsViaSymbols(); + if (isMachO) { + Error(L, ".fnstart directive not valid for Mach-O"); + Parser.eatToEndOfStatement(); + return false; + } + if (UC.hasFnStart()) { Error(L, ".fnstart starts before the end of previous one"); UC.emitFnStartLocNotes(); @@ -8732,6 +8771,14 @@ bool ARMAsmParser::parseDirectiveRegSave(SMLoc L, bool IsVector) { /// ::= .inst.n opcode [, ...] /// ::= .inst.w opcode [, ...] bool ARMAsmParser::parseDirectiveInst(SMLoc Loc, char Suffix) { + const MCAsmInfo *MAI = getParser().getStreamer().getContext().getAsmInfo(); + bool isMachO = MAI->hasSubsectionsViaSymbols(); + if (isMachO) { + Error(Loc, ".inst directive not valid for Mach-O"); + Parser.eatToEndOfStatement(); + return false; + } + int Width; if (isThumb()) { @@ -8980,6 +9027,14 @@ bool ARMAsmParser::parseDirectiveUnwindRaw(SMLoc L) { /// parseDirectiveTLSDescSeq /// ::= .tlsdescseq tls-variable bool ARMAsmParser::parseDirectiveTLSDescSeq(SMLoc L) { + const MCAsmInfo *MAI = getParser().getStreamer().getContext().getAsmInfo(); + bool isMachO = MAI->hasSubsectionsViaSymbols(); + if (isMachO) { + Error(L, ".tlsdescseq directive not valid for Mach-O"); + Parser.eatToEndOfStatement(); + return false; + } + if (getLexer().isNot(AsmToken::Identifier)) { TokError("expected variable after '.tlsdescseq' directive"); Parser.eatToEndOfStatement(); @@ -9067,6 +9122,14 @@ bool ARMAsmParser::parseDirectiveMovSP(SMLoc L) { /// parseDirectiveObjectArch /// ::= .object_arch name bool ARMAsmParser::parseDirectiveObjectArch(SMLoc L) { + const MCAsmInfo *MAI = getParser().getStreamer().getContext().getAsmInfo(); + bool isMachO = MAI->hasSubsectionsViaSymbols(); + if (isMachO) { + Error(L, ".object_arch directive not valid for Mach-O"); + Parser.eatToEndOfStatement(); + return false; + } + if (getLexer().isNot(AsmToken::Identifier)) { Error(getLexer().getLoc(), "unexpected token"); Parser.eatToEndOfStatement(); diff --git a/test/MC/MachO/ARM/bad-darwin-directives.s b/test/MC/MachO/ARM/bad-darwin-directives.s new file mode 100644 index 00000000000..0499e406df3 --- /dev/null +++ b/test/MC/MachO/ARM/bad-darwin-directives.s @@ -0,0 +1,24 @@ +@ RUN: not llvm-mc -n -triple armv7-apple-darwin10 %s -filetype=obj -o - 2> %t.err > %t +@ RUN: FileCheck --check-prefix=CHECK-ERROR < %t.err %s +@ rdar://16335232 + +.eabi_attribute 8, 1 +@ CHECK-ERROR: error: .eabi_attribute directive not valid for Mach-O + +.cpu +@ CHECK-ERROR: error: .cpu directive not valid for Mach-O + +.fpu neon +@ CHECK-ERROR: error: .fpu directive not valid for Mach-O + +.arch armv7 +@ CHECK-ERROR: error: .arch directive not valid for Mach-O + +.fnstart +@ CHECK-ERROR: error: .fnstart directive not valid for Mach-O + +.tlsdescseq +@ CHECK-ERROR: error: .tlsdescseq directive not valid for Mach-O + +.object_arch armv7 +@ CHECK-ERROR: error: .object_arch directive not valid for Mach-O