| 1 | //===- COFFAsmParser.cpp - COFF Assembly Parser ---------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/ADT/StringRef.h" |
| 10 | #include "llvm/ADT/StringSwitch.h" |
| 11 | #include "llvm/ADT/Twine.h" |
| 12 | #include "llvm/BinaryFormat/COFF.h" |
| 13 | #include "llvm/MC/MCContext.h" |
| 14 | #include "llvm/MC/MCDirectives.h" |
| 15 | #include "llvm/MC/MCParser/AsmLexer.h" |
| 16 | #include "llvm/MC/MCParser/MCAsmParserExtension.h" |
| 17 | #include "llvm/MC/MCSectionCOFF.h" |
| 18 | #include "llvm/MC/MCStreamer.h" |
| 19 | #include "llvm/Support/SMLoc.h" |
| 20 | #include "llvm/TargetParser/Triple.h" |
| 21 | #include <cassert> |
| 22 | #include <cstdint> |
| 23 | #include <limits> |
| 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | class COFFAsmParser : public MCAsmParserExtension { |
| 30 | template<bool (COFFAsmParser::*HandlerMethod)(StringRef, SMLoc)> |
| 31 | void addDirectiveHandler(StringRef Directive) { |
| 32 | MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair( |
| 33 | this, HandleDirective<COFFAsmParser, HandlerMethod>); |
| 34 | getParser().addDirectiveHandler(Directive, Handler); |
| 35 | } |
| 36 | |
| 37 | bool parseSectionSwitch(StringRef Section, unsigned Characteristics); |
| 38 | |
| 39 | bool parseSectionSwitch(StringRef Section, unsigned Characteristics, |
| 40 | StringRef COMDATSymName, COFF::COMDATType Type, |
| 41 | unsigned UniqueID); |
| 42 | |
| 43 | bool parseSectionName(StringRef &SectionName); |
| 44 | bool parseSectionFlags(StringRef SectionName, StringRef FlagsString, |
| 45 | unsigned *Flags); |
| 46 | void Initialize(MCAsmParser &Parser) override { |
| 47 | // Call the base implementation. |
| 48 | MCAsmParserExtension::Initialize(Parser); |
| 49 | |
| 50 | addDirectiveHandler<&COFFAsmParser::parseSectionDirectiveText>(Directive: ".text" ); |
| 51 | addDirectiveHandler<&COFFAsmParser::parseSectionDirectiveData>(Directive: ".data" ); |
| 52 | addDirectiveHandler<&COFFAsmParser::parseSectionDirectiveBSS>(Directive: ".bss" ); |
| 53 | addDirectiveHandler<&COFFAsmParser::parseDirectiveSection>(Directive: ".section" ); |
| 54 | addDirectiveHandler<&COFFAsmParser::parseDirectivePushSection>( |
| 55 | Directive: ".pushsection" ); |
| 56 | addDirectiveHandler<&COFFAsmParser::parseDirectivePopSection>( |
| 57 | Directive: ".popsection" ); |
| 58 | addDirectiveHandler<&COFFAsmParser::parseDirectiveDef>(Directive: ".def" ); |
| 59 | addDirectiveHandler<&COFFAsmParser::parseDirectiveScl>(Directive: ".scl" ); |
| 60 | addDirectiveHandler<&COFFAsmParser::parseDirectiveType>(Directive: ".type" ); |
| 61 | addDirectiveHandler<&COFFAsmParser::parseDirectiveEndef>(Directive: ".endef" ); |
| 62 | addDirectiveHandler<&COFFAsmParser::parseDirectiveSecRel32>(Directive: ".secrel32" ); |
| 63 | addDirectiveHandler<&COFFAsmParser::parseDirectiveSymIdx>(Directive: ".symidx" ); |
| 64 | addDirectiveHandler<&COFFAsmParser::parseDirectiveSafeSEH>(Directive: ".safeseh" ); |
| 65 | addDirectiveHandler<&COFFAsmParser::parseDirectiveSecIdx>(Directive: ".secidx" ); |
| 66 | addDirectiveHandler<&COFFAsmParser::parseDirectiveLinkOnce>(Directive: ".linkonce" ); |
| 67 | addDirectiveHandler<&COFFAsmParser::parseDirectiveRVA>(Directive: ".rva" ); |
| 68 | addDirectiveHandler<&COFFAsmParser::parseDirectiveSymbolAttribute>(Directive: ".weak" ); |
| 69 | addDirectiveHandler<&COFFAsmParser::parseDirectiveSymbolAttribute>( |
| 70 | Directive: ".weak_anti_dep" ); |
| 71 | addDirectiveHandler<&COFFAsmParser::parseDirectiveCGProfile>(Directive: ".cg_profile" ); |
| 72 | addDirectiveHandler<&COFFAsmParser::parseDirectiveSecNum>(Directive: ".secnum" ); |
| 73 | addDirectiveHandler<&COFFAsmParser::parseDirectiveSecOffset>(Directive: ".secoffset" ); |
| 74 | |
| 75 | // Win64 EH directives. |
| 76 | addDirectiveHandler<&COFFAsmParser::parseSEHDirectiveStartProc>( |
| 77 | Directive: ".seh_proc" ); |
| 78 | addDirectiveHandler<&COFFAsmParser::parseSEHDirectiveEndProc>( |
| 79 | Directive: ".seh_endproc" ); |
| 80 | addDirectiveHandler<&COFFAsmParser::parseSEHDirectiveEndFuncletOrFunc>( |
| 81 | Directive: ".seh_endfunclet" ); |
| 82 | addDirectiveHandler<&COFFAsmParser::parseSEHDirectiveSplitChained>( |
| 83 | Directive: ".seh_splitchained" ); |
| 84 | addDirectiveHandler<&COFFAsmParser::parseSEHDirectiveHandler>( |
| 85 | Directive: ".seh_handler" ); |
| 86 | addDirectiveHandler<&COFFAsmParser::parseSEHDirectiveHandlerData>( |
| 87 | Directive: ".seh_handlerdata" ); |
| 88 | addDirectiveHandler<&COFFAsmParser::parseSEHDirectiveAllocStack>( |
| 89 | Directive: ".seh_stackalloc" ); |
| 90 | addDirectiveHandler<&COFFAsmParser::parseSEHDirectiveEndProlog>( |
| 91 | Directive: ".seh_endprologue" ); |
| 92 | addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveBeginEpilog>( |
| 93 | Directive: ".seh_startepilogue" ); |
| 94 | addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndEpilog>( |
| 95 | Directive: ".seh_endepilogue" ); |
| 96 | addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveUnwindV2Start>( |
| 97 | Directive: ".seh_unwindv2start" ); |
| 98 | addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveUnwindVersion>( |
| 99 | Directive: ".seh_unwindversion" ); |
| 100 | } |
| 101 | |
| 102 | bool parseSectionDirectiveText(StringRef, SMLoc) { |
| 103 | return parseSectionSwitch(Section: ".text" , Characteristics: COFF::IMAGE_SCN_CNT_CODE | |
| 104 | COFF::IMAGE_SCN_MEM_EXECUTE | |
| 105 | COFF::IMAGE_SCN_MEM_READ); |
| 106 | } |
| 107 | |
| 108 | bool parseSectionDirectiveData(StringRef, SMLoc) { |
| 109 | return parseSectionSwitch(Section: ".data" , Characteristics: COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 110 | COFF::IMAGE_SCN_MEM_READ | |
| 111 | COFF::IMAGE_SCN_MEM_WRITE); |
| 112 | } |
| 113 | |
| 114 | bool parseSectionDirectiveBSS(StringRef, SMLoc) { |
| 115 | return parseSectionSwitch(Section: ".bss" , Characteristics: COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA | |
| 116 | COFF::IMAGE_SCN_MEM_READ | |
| 117 | COFF::IMAGE_SCN_MEM_WRITE); |
| 118 | } |
| 119 | |
| 120 | bool parseDirectiveSection(StringRef, SMLoc); |
| 121 | bool parseSectionArguments(StringRef, SMLoc); |
| 122 | bool parseDirectivePushSection(StringRef, SMLoc); |
| 123 | bool parseDirectivePopSection(StringRef, SMLoc); |
| 124 | bool parseDirectiveDef(StringRef, SMLoc); |
| 125 | bool parseDirectiveScl(StringRef, SMLoc); |
| 126 | bool parseDirectiveType(StringRef, SMLoc); |
| 127 | bool parseDirectiveEndef(StringRef, SMLoc); |
| 128 | bool parseDirectiveSecRel32(StringRef, SMLoc); |
| 129 | bool parseDirectiveSecIdx(StringRef, SMLoc); |
| 130 | bool parseDirectiveSafeSEH(StringRef, SMLoc); |
| 131 | bool parseDirectiveSymIdx(StringRef, SMLoc); |
| 132 | bool parseCOMDATType(COFF::COMDATType &Type); |
| 133 | bool parseDirectiveLinkOnce(StringRef, SMLoc); |
| 134 | bool parseDirectiveRVA(StringRef, SMLoc); |
| 135 | bool parseDirectiveCGProfile(StringRef, SMLoc); |
| 136 | bool parseDirectiveSecNum(StringRef, SMLoc); |
| 137 | bool parseDirectiveSecOffset(StringRef, SMLoc); |
| 138 | |
| 139 | // Win64 EH directives. |
| 140 | bool parseSEHDirectiveStartProc(StringRef, SMLoc); |
| 141 | bool parseSEHDirectiveEndProc(StringRef, SMLoc); |
| 142 | bool parseSEHDirectiveEndFuncletOrFunc(StringRef, SMLoc); |
| 143 | bool parseSEHDirectiveSplitChained(StringRef, SMLoc); |
| 144 | bool parseSEHDirectiveHandler(StringRef, SMLoc); |
| 145 | bool parseSEHDirectiveHandlerData(StringRef, SMLoc); |
| 146 | bool parseSEHDirectiveAllocStack(StringRef, SMLoc); |
| 147 | bool parseSEHDirectiveEndProlog(StringRef, SMLoc); |
| 148 | bool ParseSEHDirectiveBeginEpilog(StringRef, SMLoc); |
| 149 | bool ParseSEHDirectiveEndEpilog(StringRef, SMLoc); |
| 150 | bool ParseSEHDirectiveUnwindV2Start(StringRef, SMLoc); |
| 151 | bool ParseSEHDirectiveUnwindVersion(StringRef, SMLoc); |
| 152 | |
| 153 | bool parseAtUnwindOrAtExcept(bool &unwind, bool &except); |
| 154 | bool parseDirectiveSymbolAttribute(StringRef Directive, SMLoc); |
| 155 | |
| 156 | public: |
| 157 | COFFAsmParser() = default; |
| 158 | }; |
| 159 | |
| 160 | } // end anonymous namespace. |
| 161 | |
| 162 | bool COFFAsmParser::parseSectionFlags(StringRef SectionName, |
| 163 | StringRef FlagsString, unsigned *Flags) { |
| 164 | enum { |
| 165 | None = 0, |
| 166 | Alloc = 1 << 0, |
| 167 | Code = 1 << 1, |
| 168 | Load = 1 << 2, |
| 169 | InitData = 1 << 3, |
| 170 | Shared = 1 << 4, |
| 171 | NoLoad = 1 << 5, |
| 172 | NoRead = 1 << 6, |
| 173 | NoWrite = 1 << 7, |
| 174 | Discardable = 1 << 8, |
| 175 | Info = 1 << 9, |
| 176 | }; |
| 177 | |
| 178 | bool ReadOnlyRemoved = false; |
| 179 | unsigned SecFlags = None; |
| 180 | |
| 181 | for (char FlagChar : FlagsString) { |
| 182 | switch (FlagChar) { |
| 183 | case 'a': |
| 184 | // Ignored. |
| 185 | break; |
| 186 | |
| 187 | case 'b': // bss section |
| 188 | SecFlags |= Alloc; |
| 189 | if (SecFlags & InitData) |
| 190 | return TokError(Msg: "conflicting section flags 'b' and 'd'." ); |
| 191 | SecFlags &= ~Load; |
| 192 | break; |
| 193 | |
| 194 | case 'd': // data section |
| 195 | SecFlags |= InitData; |
| 196 | if (SecFlags & Alloc) |
| 197 | return TokError(Msg: "conflicting section flags 'b' and 'd'." ); |
| 198 | SecFlags &= ~NoWrite; |
| 199 | if ((SecFlags & NoLoad) == 0) |
| 200 | SecFlags |= Load; |
| 201 | break; |
| 202 | |
| 203 | case 'n': // section is not loaded |
| 204 | SecFlags |= NoLoad; |
| 205 | SecFlags &= ~Load; |
| 206 | break; |
| 207 | |
| 208 | case 'D': // discardable |
| 209 | SecFlags |= Discardable; |
| 210 | break; |
| 211 | |
| 212 | case 'r': // read-only |
| 213 | ReadOnlyRemoved = false; |
| 214 | SecFlags |= NoWrite; |
| 215 | if ((SecFlags & Code) == 0) |
| 216 | SecFlags |= InitData; |
| 217 | if ((SecFlags & NoLoad) == 0) |
| 218 | SecFlags |= Load; |
| 219 | break; |
| 220 | |
| 221 | case 's': // shared section |
| 222 | SecFlags |= Shared | InitData; |
| 223 | SecFlags &= ~NoWrite; |
| 224 | if ((SecFlags & NoLoad) == 0) |
| 225 | SecFlags |= Load; |
| 226 | break; |
| 227 | |
| 228 | case 'w': // writable |
| 229 | SecFlags &= ~NoWrite; |
| 230 | ReadOnlyRemoved = true; |
| 231 | break; |
| 232 | |
| 233 | case 'x': // executable section |
| 234 | SecFlags |= Code; |
| 235 | if ((SecFlags & NoLoad) == 0) |
| 236 | SecFlags |= Load; |
| 237 | if (!ReadOnlyRemoved) |
| 238 | SecFlags |= NoWrite; |
| 239 | break; |
| 240 | |
| 241 | case 'y': // not readable |
| 242 | SecFlags |= NoRead | NoWrite; |
| 243 | break; |
| 244 | |
| 245 | case 'i': // info |
| 246 | SecFlags |= Info; |
| 247 | break; |
| 248 | |
| 249 | default: |
| 250 | return TokError(Msg: "unknown flag" ); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | *Flags = 0; |
| 255 | |
| 256 | if (SecFlags == None) |
| 257 | SecFlags = InitData; |
| 258 | |
| 259 | if (SecFlags & Code) |
| 260 | *Flags |= COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE; |
| 261 | if (SecFlags & InitData) |
| 262 | *Flags |= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; |
| 263 | if ((SecFlags & Alloc) && (SecFlags & Load) == 0) |
| 264 | *Flags |= COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; |
| 265 | if (SecFlags & NoLoad) |
| 266 | *Flags |= COFF::IMAGE_SCN_LNK_REMOVE; |
| 267 | if ((SecFlags & Discardable) || |
| 268 | MCSectionCOFF::isImplicitlyDiscardable(Name: SectionName)) |
| 269 | *Flags |= COFF::IMAGE_SCN_MEM_DISCARDABLE; |
| 270 | if ((SecFlags & NoRead) == 0) |
| 271 | *Flags |= COFF::IMAGE_SCN_MEM_READ; |
| 272 | if ((SecFlags & NoWrite) == 0) |
| 273 | *Flags |= COFF::IMAGE_SCN_MEM_WRITE; |
| 274 | if (SecFlags & Shared) |
| 275 | *Flags |= COFF::IMAGE_SCN_MEM_SHARED; |
| 276 | if (SecFlags & Info) |
| 277 | *Flags |= COFF::IMAGE_SCN_LNK_INFO; |
| 278 | |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | /// ParseDirectiveSymbolAttribute |
| 283 | /// ::= { ".weak", ... } [ identifier ( , identifier )* ] |
| 284 | bool COFFAsmParser::parseDirectiveSymbolAttribute(StringRef Directive, SMLoc) { |
| 285 | MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive) |
| 286 | .Case(S: ".weak" , Value: MCSA_Weak) |
| 287 | .Case(S: ".weak_anti_dep" , Value: MCSA_WeakAntiDep) |
| 288 | .Default(Value: MCSA_Invalid); |
| 289 | assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!" ); |
| 290 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) { |
| 291 | while (true) { |
| 292 | MCSymbol *Sym; |
| 293 | |
| 294 | if (getParser().parseSymbol(Res&: Sym)) |
| 295 | return TokError(Msg: "expected identifier in directive" ); |
| 296 | |
| 297 | getStreamer().emitSymbolAttribute(Symbol: Sym, Attribute: Attr); |
| 298 | |
| 299 | if (getLexer().is(K: AsmToken::EndOfStatement)) |
| 300 | break; |
| 301 | |
| 302 | if (getLexer().isNot(K: AsmToken::Comma)) |
| 303 | return TokError(Msg: "unexpected token in directive" ); |
| 304 | Lex(); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | Lex(); |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | bool COFFAsmParser::parseDirectiveCGProfile(StringRef S, SMLoc Loc) { |
| 313 | return MCAsmParserExtension::parseDirectiveCGProfile(S, Loc); |
| 314 | } |
| 315 | |
| 316 | bool COFFAsmParser::parseSectionSwitch(StringRef Section, |
| 317 | unsigned Characteristics) { |
| 318 | return parseSectionSwitch(Section, Characteristics, COMDATSymName: "" , Type: (COFF::COMDATType)0, |
| 319 | UniqueID: MCSection::NonUniqueID); |
| 320 | } |
| 321 | |
| 322 | bool COFFAsmParser::parseSectionSwitch(StringRef Section, |
| 323 | unsigned Characteristics, |
| 324 | StringRef COMDATSymName, |
| 325 | COFF::COMDATType Type, |
| 326 | unsigned UniqueID) { |
| 327 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 328 | return TokError(Msg: "unexpected token in section switching directive" ); |
| 329 | Lex(); |
| 330 | |
| 331 | getStreamer().switchSection(Section: getContext().getCOFFSection( |
| 332 | Section, Characteristics, COMDATSymName, Selection: Type, UniqueID)); |
| 333 | |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | bool COFFAsmParser::parseSectionName(StringRef &SectionName) { |
| 338 | if (!getLexer().is(K: AsmToken::Identifier) && !getLexer().is(K: AsmToken::String)) |
| 339 | return true; |
| 340 | |
| 341 | SectionName = getTok().getIdentifier(); |
| 342 | Lex(); |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | bool COFFAsmParser::parseDirectiveSection(StringRef directive, SMLoc loc) { |
| 347 | return parseSectionArguments(directive, loc); |
| 348 | } |
| 349 | |
| 350 | // .section name [, "flags"] [, identifier [ identifier ], identifier] |
| 351 | // .pushsection <same as above> |
| 352 | // |
| 353 | // Supported flags: |
| 354 | // a: Ignored. |
| 355 | // b: BSS section (uninitialized data) |
| 356 | // d: data section (initialized data) |
| 357 | // n: "noload" section (removed by linker) |
| 358 | // D: Discardable section |
| 359 | // r: Readable section |
| 360 | // s: Shared section |
| 361 | // w: Writable section |
| 362 | // x: Executable section |
| 363 | // y: Not-readable section (clears 'r') |
| 364 | // |
| 365 | // Subsections are not supported. |
| 366 | bool COFFAsmParser::parseSectionArguments(StringRef, SMLoc) { |
| 367 | StringRef SectionName; |
| 368 | |
| 369 | if (parseSectionName(SectionName)) |
| 370 | return TokError(Msg: "expected identifier in directive" ); |
| 371 | |
| 372 | unsigned Flags = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 373 | COFF::IMAGE_SCN_MEM_READ | |
| 374 | COFF::IMAGE_SCN_MEM_WRITE; |
| 375 | |
| 376 | if (getLexer().is(K: AsmToken::Comma)) { |
| 377 | Lex(); |
| 378 | |
| 379 | if (getLexer().isNot(K: AsmToken::String)) |
| 380 | return TokError(Msg: "expected string in directive" ); |
| 381 | |
| 382 | StringRef FlagsStr = getTok().getStringContents(); |
| 383 | Lex(); |
| 384 | |
| 385 | if (parseSectionFlags(SectionName, FlagsString: FlagsStr, Flags: &Flags)) |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | COFF::COMDATType Type = (COFF::COMDATType)0; |
| 390 | StringRef COMDATSymName; |
| 391 | if (getLexer().is(K: AsmToken::Comma) && |
| 392 | getLexer().peekTok().getString() != "unique" ) { |
| 393 | Type = COFF::IMAGE_COMDAT_SELECT_ANY; |
| 394 | Lex(); |
| 395 | |
| 396 | Flags |= COFF::IMAGE_SCN_LNK_COMDAT; |
| 397 | |
| 398 | if (!getLexer().is(K: AsmToken::Identifier)) |
| 399 | return TokError(Msg: "expected comdat type such as 'discard' or 'largest' " |
| 400 | "after protection bits" ); |
| 401 | |
| 402 | if (parseCOMDATType(Type)) |
| 403 | return true; |
| 404 | |
| 405 | if (getLexer().isNot(K: AsmToken::Comma)) |
| 406 | return TokError(Msg: "expected comma in directive" ); |
| 407 | Lex(); |
| 408 | |
| 409 | if (getParser().parseIdentifier(Res&: COMDATSymName)) |
| 410 | return TokError(Msg: "expected identifier in directive" ); |
| 411 | } |
| 412 | |
| 413 | int64_t UniqueID = MCSection::NonUniqueID; |
| 414 | if (maybeParseUniqueID(UniqueID)) |
| 415 | return true; |
| 416 | |
| 417 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 418 | return TokError(Msg: "unexpected token in directive" ); |
| 419 | |
| 420 | if (Flags & COFF::IMAGE_SCN_CNT_CODE) { |
| 421 | const Triple &T = getContext().getTargetTriple(); |
| 422 | if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb) |
| 423 | Flags |= COFF::IMAGE_SCN_MEM_16BIT; |
| 424 | } |
| 425 | parseSectionSwitch(Section: SectionName, Characteristics: Flags, COMDATSymName, Type, UniqueID); |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | bool COFFAsmParser::parseDirectivePushSection(StringRef directive, SMLoc loc) { |
| 430 | getStreamer().pushSection(); |
| 431 | |
| 432 | if (parseSectionArguments(directive, loc)) { |
| 433 | getStreamer().popSection(); |
| 434 | return true; |
| 435 | } |
| 436 | |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | bool COFFAsmParser::parseDirectivePopSection(StringRef, SMLoc) { |
| 441 | if (!getStreamer().popSection()) |
| 442 | return TokError(Msg: ".popsection without corresponding .pushsection" ); |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | bool COFFAsmParser::parseDirectiveDef(StringRef, SMLoc) { |
| 447 | MCSymbol *Sym; |
| 448 | |
| 449 | if (getParser().parseSymbol(Res&: Sym)) |
| 450 | return TokError(Msg: "expected identifier in directive" ); |
| 451 | |
| 452 | getStreamer().beginCOFFSymbolDef(Symbol: Sym); |
| 453 | |
| 454 | Lex(); |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | bool COFFAsmParser::parseDirectiveScl(StringRef, SMLoc) { |
| 459 | int64_t SymbolStorageClass; |
| 460 | if (getParser().parseAbsoluteExpression(Res&: SymbolStorageClass)) |
| 461 | return true; |
| 462 | |
| 463 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 464 | return TokError(Msg: "unexpected token in directive" ); |
| 465 | |
| 466 | Lex(); |
| 467 | getStreamer().emitCOFFSymbolStorageClass(StorageClass: SymbolStorageClass); |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | bool COFFAsmParser::parseDirectiveType(StringRef, SMLoc) { |
| 472 | int64_t Type; |
| 473 | if (getParser().parseAbsoluteExpression(Res&: Type)) |
| 474 | return true; |
| 475 | |
| 476 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 477 | return TokError(Msg: "unexpected token in directive" ); |
| 478 | |
| 479 | Lex(); |
| 480 | getStreamer().emitCOFFSymbolType(Type); |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | bool COFFAsmParser::parseDirectiveEndef(StringRef, SMLoc) { |
| 485 | Lex(); |
| 486 | getStreamer().endCOFFSymbolDef(); |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | bool COFFAsmParser::parseDirectiveSecRel32(StringRef, SMLoc) { |
| 491 | MCSymbol *Symbol; |
| 492 | if (getParser().parseSymbol(Res&: Symbol)) |
| 493 | return TokError(Msg: "expected identifier in directive" ); |
| 494 | |
| 495 | int64_t Offset = 0; |
| 496 | SMLoc OffsetLoc; |
| 497 | if (getLexer().is(K: AsmToken::Plus)) { |
| 498 | OffsetLoc = getLexer().getLoc(); |
| 499 | if (getParser().parseAbsoluteExpression(Res&: Offset)) |
| 500 | return true; |
| 501 | } |
| 502 | |
| 503 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 504 | return TokError(Msg: "unexpected token in directive" ); |
| 505 | |
| 506 | if (Offset < 0 || Offset > std::numeric_limits<uint32_t>::max()) |
| 507 | return Error( |
| 508 | L: OffsetLoc, |
| 509 | Msg: "invalid '.secrel32' directive offset, can't be less " |
| 510 | "than zero or greater than std::numeric_limits<uint32_t>::max()" ); |
| 511 | |
| 512 | Lex(); |
| 513 | getStreamer().emitCOFFSecRel32(Symbol, Offset); |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | bool COFFAsmParser::parseDirectiveRVA(StringRef, SMLoc) { |
| 518 | auto parseOp = [&]() -> bool { |
| 519 | MCSymbol *Symbol; |
| 520 | if (getParser().parseSymbol(Res&: Symbol)) |
| 521 | return TokError(Msg: "expected identifier in directive" ); |
| 522 | |
| 523 | int64_t Offset = 0; |
| 524 | SMLoc OffsetLoc; |
| 525 | if (getLexer().is(K: AsmToken::Plus) || getLexer().is(K: AsmToken::Minus)) { |
| 526 | OffsetLoc = getLexer().getLoc(); |
| 527 | if (getParser().parseAbsoluteExpression(Res&: Offset)) |
| 528 | return true; |
| 529 | } |
| 530 | |
| 531 | if (Offset < std::numeric_limits<int32_t>::min() || |
| 532 | Offset > std::numeric_limits<int32_t>::max()) |
| 533 | return Error(L: OffsetLoc, Msg: "invalid '.rva' directive offset, can't be less " |
| 534 | "than -2147483648 or greater than " |
| 535 | "2147483647" ); |
| 536 | |
| 537 | getStreamer().emitCOFFImgRel32(Symbol, Offset); |
| 538 | return false; |
| 539 | }; |
| 540 | |
| 541 | if (getParser().parseMany(parseOne: parseOp)) |
| 542 | return addErrorSuffix(Suffix: " in directive" ); |
| 543 | return false; |
| 544 | } |
| 545 | |
| 546 | bool COFFAsmParser::parseDirectiveSafeSEH(StringRef, SMLoc) { |
| 547 | MCSymbol *Symbol; |
| 548 | if (getParser().parseSymbol(Res&: Symbol)) |
| 549 | return TokError(Msg: "expected identifier in directive" ); |
| 550 | |
| 551 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 552 | return TokError(Msg: "unexpected token in directive" ); |
| 553 | |
| 554 | Lex(); |
| 555 | getStreamer().emitCOFFSafeSEH(Symbol); |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | bool COFFAsmParser::parseDirectiveSecIdx(StringRef, SMLoc) { |
| 560 | MCSymbol *Symbol; |
| 561 | if (getParser().parseSymbol(Res&: Symbol)) |
| 562 | return TokError(Msg: "expected identifier in directive" ); |
| 563 | |
| 564 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 565 | return TokError(Msg: "unexpected token in directive" ); |
| 566 | |
| 567 | Lex(); |
| 568 | getStreamer().emitCOFFSectionIndex(Symbol); |
| 569 | return false; |
| 570 | } |
| 571 | |
| 572 | bool COFFAsmParser::parseDirectiveSymIdx(StringRef, SMLoc) { |
| 573 | MCSymbol *Symbol; |
| 574 | if (getParser().parseSymbol(Res&: Symbol)) |
| 575 | return TokError(Msg: "expected identifier in directive" ); |
| 576 | |
| 577 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 578 | return TokError(Msg: "unexpected token in directive" ); |
| 579 | |
| 580 | Lex(); |
| 581 | getStreamer().emitCOFFSymbolIndex(Symbol); |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | bool COFFAsmParser::parseDirectiveSecNum(StringRef, SMLoc) { |
| 586 | MCSymbol *Symbol; |
| 587 | if (getParser().parseSymbol(Res&: Symbol)) |
| 588 | return TokError(Msg: "expected identifier in directive" ); |
| 589 | |
| 590 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 591 | return TokError(Msg: "unexpected token in directive" ); |
| 592 | |
| 593 | Lex(); |
| 594 | getStreamer().emitCOFFSecNumber(Symbol); |
| 595 | return false; |
| 596 | } |
| 597 | |
| 598 | bool COFFAsmParser::parseDirectiveSecOffset(StringRef, SMLoc) { |
| 599 | MCSymbol *Symbol; |
| 600 | if (getParser().parseSymbol(Res&: Symbol)) |
| 601 | return TokError(Msg: "expected identifier in directive" ); |
| 602 | |
| 603 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 604 | return TokError(Msg: "unexpected token in directive" ); |
| 605 | |
| 606 | Lex(); |
| 607 | getStreamer().emitCOFFSecOffset(Symbol); |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | /// ::= [ identifier ] |
| 612 | bool COFFAsmParser::parseCOMDATType(COFF::COMDATType &Type) { |
| 613 | StringRef TypeId = getTok().getIdentifier(); |
| 614 | |
| 615 | Type = StringSwitch<COFF::COMDATType>(TypeId) |
| 616 | .Case(S: "one_only" , Value: COFF::IMAGE_COMDAT_SELECT_NODUPLICATES) |
| 617 | .Case(S: "discard" , Value: COFF::IMAGE_COMDAT_SELECT_ANY) |
| 618 | .Case(S: "same_size" , Value: COFF::IMAGE_COMDAT_SELECT_SAME_SIZE) |
| 619 | .Case(S: "same_contents" , Value: COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH) |
| 620 | .Case(S: "associative" , Value: COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) |
| 621 | .Case(S: "largest" , Value: COFF::IMAGE_COMDAT_SELECT_LARGEST) |
| 622 | .Case(S: "newest" , Value: COFF::IMAGE_COMDAT_SELECT_NEWEST) |
| 623 | .Default(Value: (COFF::COMDATType)0); |
| 624 | |
| 625 | if (Type == 0) |
| 626 | return TokError(Msg: Twine("unrecognized COMDAT type '" + TypeId + "'" )); |
| 627 | |
| 628 | Lex(); |
| 629 | |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | /// ParseDirectiveLinkOnce |
| 634 | /// ::= .linkonce [ identifier ] |
| 635 | bool COFFAsmParser::parseDirectiveLinkOnce(StringRef, SMLoc Loc) { |
| 636 | COFF::COMDATType Type = COFF::IMAGE_COMDAT_SELECT_ANY; |
| 637 | if (getLexer().is(K: AsmToken::Identifier)) |
| 638 | if (parseCOMDATType(Type)) |
| 639 | return true; |
| 640 | |
| 641 | const MCSectionCOFF *Current = |
| 642 | static_cast<const MCSectionCOFF *>(getStreamer().getCurrentSectionOnly()); |
| 643 | |
| 644 | if (Type == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) |
| 645 | return Error(L: Loc, Msg: "cannot make section associative with .linkonce" ); |
| 646 | |
| 647 | if (Current->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) |
| 648 | return Error(L: Loc, Msg: Twine("section '" ) + Current->getName() + |
| 649 | "' is already linkonce" ); |
| 650 | |
| 651 | Current->setSelection(Type); |
| 652 | |
| 653 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 654 | return TokError(Msg: "unexpected token in directive" ); |
| 655 | |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | bool COFFAsmParser::parseSEHDirectiveStartProc(StringRef, SMLoc Loc) { |
| 660 | MCSymbol *Symbol; |
| 661 | if (getParser().parseSymbol(Res&: Symbol)) |
| 662 | return true; |
| 663 | |
| 664 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 665 | return TokError(Msg: "unexpected token in directive" ); |
| 666 | |
| 667 | Lex(); |
| 668 | getStreamer().emitWinCFIStartProc(Symbol, Loc); |
| 669 | return false; |
| 670 | } |
| 671 | |
| 672 | bool COFFAsmParser::parseSEHDirectiveEndProc(StringRef, SMLoc Loc) { |
| 673 | Lex(); |
| 674 | getStreamer().emitWinCFIEndProc(Loc); |
| 675 | return false; |
| 676 | } |
| 677 | |
| 678 | bool COFFAsmParser::parseSEHDirectiveEndFuncletOrFunc(StringRef, SMLoc Loc) { |
| 679 | Lex(); |
| 680 | getStreamer().emitWinCFIFuncletOrFuncEnd(Loc); |
| 681 | return false; |
| 682 | } |
| 683 | |
| 684 | bool COFFAsmParser::parseSEHDirectiveSplitChained(StringRef, SMLoc Loc) { |
| 685 | Lex(); |
| 686 | getStreamer().emitWinCFISplitChained(Loc); |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | bool COFFAsmParser::parseSEHDirectiveHandler(StringRef, SMLoc Loc) { |
| 691 | MCSymbol *handler; |
| 692 | if (getParser().parseSymbol(Res&: handler)) |
| 693 | return true; |
| 694 | |
| 695 | if (getLexer().isNot(K: AsmToken::Comma)) |
| 696 | return TokError(Msg: "you must specify one or both of @unwind or @except" ); |
| 697 | Lex(); |
| 698 | bool unwind = false, except = false; |
| 699 | if (parseAtUnwindOrAtExcept(unwind, except)) |
| 700 | return true; |
| 701 | if (getLexer().is(K: AsmToken::Comma)) { |
| 702 | Lex(); |
| 703 | if (parseAtUnwindOrAtExcept(unwind, except)) |
| 704 | return true; |
| 705 | } |
| 706 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 707 | return TokError(Msg: "unexpected token in directive" ); |
| 708 | |
| 709 | Lex(); |
| 710 | getStreamer().emitWinEHHandler(Sym: handler, Unwind: unwind, Except: except, Loc); |
| 711 | return false; |
| 712 | } |
| 713 | |
| 714 | bool COFFAsmParser::parseSEHDirectiveHandlerData(StringRef, SMLoc Loc) { |
| 715 | Lex(); |
| 716 | getStreamer().emitWinEHHandlerData(); |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | bool COFFAsmParser::parseSEHDirectiveAllocStack(StringRef, SMLoc Loc) { |
| 721 | int64_t Size; |
| 722 | if (getParser().parseAbsoluteExpression(Res&: Size)) |
| 723 | return true; |
| 724 | |
| 725 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 726 | return TokError(Msg: "unexpected token in directive" ); |
| 727 | |
| 728 | Lex(); |
| 729 | getStreamer().emitWinCFIAllocStack(Size, Loc); |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | bool COFFAsmParser::parseSEHDirectiveEndProlog(StringRef, SMLoc Loc) { |
| 734 | Lex(); |
| 735 | getStreamer().emitWinCFIEndProlog(Loc); |
| 736 | return false; |
| 737 | } |
| 738 | |
| 739 | bool COFFAsmParser::ParseSEHDirectiveBeginEpilog(StringRef, SMLoc Loc) { |
| 740 | Lex(); |
| 741 | getStreamer().emitWinCFIBeginEpilogue(Loc); |
| 742 | return false; |
| 743 | } |
| 744 | |
| 745 | bool COFFAsmParser::ParseSEHDirectiveEndEpilog(StringRef, SMLoc Loc) { |
| 746 | Lex(); |
| 747 | getStreamer().emitWinCFIEndEpilogue(Loc); |
| 748 | return false; |
| 749 | } |
| 750 | |
| 751 | bool COFFAsmParser::ParseSEHDirectiveUnwindV2Start(StringRef, SMLoc Loc) { |
| 752 | Lex(); |
| 753 | getStreamer().emitWinCFIUnwindV2Start(Loc); |
| 754 | return false; |
| 755 | } |
| 756 | |
| 757 | bool COFFAsmParser::ParseSEHDirectiveUnwindVersion(StringRef, SMLoc Loc) { |
| 758 | int64_t Version; |
| 759 | if (getParser().parseIntToken(V&: Version, ErrMsg: "expected unwind version number" )) |
| 760 | return true; |
| 761 | |
| 762 | if ((Version < 1) || (Version > UINT8_MAX)) |
| 763 | return Error(L: Loc, Msg: "invalid unwind version" ); |
| 764 | |
| 765 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
| 766 | return TokError(Msg: "unexpected token in directive" ); |
| 767 | |
| 768 | Lex(); |
| 769 | getStreamer().emitWinCFIUnwindVersion(Version, Loc); |
| 770 | return false; |
| 771 | } |
| 772 | |
| 773 | bool COFFAsmParser::parseAtUnwindOrAtExcept(bool &unwind, bool &except) { |
| 774 | StringRef identifier; |
| 775 | if (getLexer().isNot(K: AsmToken::At) && getLexer().isNot(K: AsmToken::Percent)) |
| 776 | return TokError(Msg: "a handler attribute must begin with '@' or '%'" ); |
| 777 | SMLoc startLoc = getLexer().getLoc(); |
| 778 | Lex(); |
| 779 | if (getParser().parseIdentifier(Res&: identifier)) |
| 780 | return Error(L: startLoc, Msg: "expected @unwind or @except" ); |
| 781 | if (identifier == "unwind" ) |
| 782 | unwind = true; |
| 783 | else if (identifier == "except" ) |
| 784 | except = true; |
| 785 | else |
| 786 | return Error(L: startLoc, Msg: "expected @unwind or @except" ); |
| 787 | return false; |
| 788 | } |
| 789 | |
| 790 | MCAsmParserExtension *llvm::createCOFFAsmParser() { return new COFFAsmParser; } |
| 791 | |