COFFAsmParser.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/ADT/StringSwitch.h"
  10. #include "llvm/ADT/Triple.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/MCObjectFileInfo.h"
  16. #include "llvm/MC/MCParser/MCAsmLexer.h"
  17. #include "llvm/MC/MCParser/MCAsmParserExtension.h"
  18. #include "llvm/MC/MCParser/MCTargetAsmParser.h"
  19. #include "llvm/MC/MCRegisterInfo.h"
  20. #include "llvm/MC/MCSectionCOFF.h"
  21. #include "llvm/MC/MCStreamer.h"
  22. #include "llvm/MC/SectionKind.h"
  23. #include "llvm/Support/SMLoc.h"
  24. #include <cassert>
  25. #include <cstdint>
  26. #include <limits>
  27. #include <utility>
  28. using namespace llvm;
  29. namespace {
  30. class COFFAsmParser : public MCAsmParserExtension {
  31. template<bool (COFFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
  32. void addDirectiveHandler(StringRef Directive) {
  33. MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
  34. this, HandleDirective<COFFAsmParser, HandlerMethod>);
  35. getParser().addDirectiveHandler(Directive, Handler);
  36. }
  37. bool ParseSectionSwitch(StringRef Section,
  38. unsigned Characteristics,
  39. SectionKind Kind);
  40. bool ParseSectionSwitch(StringRef Section, unsigned Characteristics,
  41. SectionKind Kind, StringRef COMDATSymName,
  42. COFF::COMDATType Type);
  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. addDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveText>(".text");
  50. addDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveData>(".data");
  51. addDirectiveHandler<&COFFAsmParser::ParseSectionDirectiveBSS>(".bss");
  52. addDirectiveHandler<&COFFAsmParser::ParseDirectiveSection>(".section");
  53. addDirectiveHandler<&COFFAsmParser::ParseDirectiveDef>(".def");
  54. addDirectiveHandler<&COFFAsmParser::ParseDirectiveScl>(".scl");
  55. addDirectiveHandler<&COFFAsmParser::ParseDirectiveType>(".type");
  56. addDirectiveHandler<&COFFAsmParser::ParseDirectiveEndef>(".endef");
  57. addDirectiveHandler<&COFFAsmParser::ParseDirectiveSecRel32>(".secrel32");
  58. addDirectiveHandler<&COFFAsmParser::ParseDirectiveSymIdx>(".symidx");
  59. addDirectiveHandler<&COFFAsmParser::ParseDirectiveSafeSEH>(".safeseh");
  60. addDirectiveHandler<&COFFAsmParser::ParseDirectiveSecIdx>(".secidx");
  61. addDirectiveHandler<&COFFAsmParser::ParseDirectiveLinkOnce>(".linkonce");
  62. addDirectiveHandler<&COFFAsmParser::ParseDirectiveRVA>(".rva");
  63. addDirectiveHandler<&COFFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
  64. addDirectiveHandler<&COFFAsmParser::ParseDirectiveCGProfile>(".cg_profile");
  65. // Win64 EH directives.
  66. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartProc>(
  67. ".seh_proc");
  68. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProc>(
  69. ".seh_endproc");
  70. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndFuncletOrFunc>(
  71. ".seh_endfunclet");
  72. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartChained>(
  73. ".seh_startchained");
  74. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndChained>(
  75. ".seh_endchained");
  76. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveHandler>(
  77. ".seh_handler");
  78. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveHandlerData>(
  79. ".seh_handlerdata");
  80. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveAllocStack>(
  81. ".seh_stackalloc");
  82. addDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProlog>(
  83. ".seh_endprologue");
  84. }
  85. bool ParseSectionDirectiveText(StringRef, SMLoc) {
  86. return ParseSectionSwitch(".text",
  87. COFF::IMAGE_SCN_CNT_CODE
  88. | COFF::IMAGE_SCN_MEM_EXECUTE
  89. | COFF::IMAGE_SCN_MEM_READ,
  90. SectionKind::getText());
  91. }
  92. bool ParseSectionDirectiveData(StringRef, SMLoc) {
  93. return ParseSectionSwitch(".data", COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  94. COFF::IMAGE_SCN_MEM_READ |
  95. COFF::IMAGE_SCN_MEM_WRITE,
  96. SectionKind::getData());
  97. }
  98. bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
  99. return ParseSectionSwitch(".bss",
  100. COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
  101. | COFF::IMAGE_SCN_MEM_READ
  102. | COFF::IMAGE_SCN_MEM_WRITE,
  103. SectionKind::getBSS());
  104. }
  105. bool ParseDirectiveSection(StringRef, SMLoc);
  106. bool ParseDirectiveDef(StringRef, SMLoc);
  107. bool ParseDirectiveScl(StringRef, SMLoc);
  108. bool ParseDirectiveType(StringRef, SMLoc);
  109. bool ParseDirectiveEndef(StringRef, SMLoc);
  110. bool ParseDirectiveSecRel32(StringRef, SMLoc);
  111. bool ParseDirectiveSecIdx(StringRef, SMLoc);
  112. bool ParseDirectiveSafeSEH(StringRef, SMLoc);
  113. bool ParseDirectiveSymIdx(StringRef, SMLoc);
  114. bool parseCOMDATType(COFF::COMDATType &Type);
  115. bool ParseDirectiveLinkOnce(StringRef, SMLoc);
  116. bool ParseDirectiveRVA(StringRef, SMLoc);
  117. bool ParseDirectiveCGProfile(StringRef, SMLoc);
  118. // Win64 EH directives.
  119. bool ParseSEHDirectiveStartProc(StringRef, SMLoc);
  120. bool ParseSEHDirectiveEndProc(StringRef, SMLoc);
  121. bool ParseSEHDirectiveEndFuncletOrFunc(StringRef, SMLoc);
  122. bool ParseSEHDirectiveStartChained(StringRef, SMLoc);
  123. bool ParseSEHDirectiveEndChained(StringRef, SMLoc);
  124. bool ParseSEHDirectiveHandler(StringRef, SMLoc);
  125. bool ParseSEHDirectiveHandlerData(StringRef, SMLoc);
  126. bool ParseSEHDirectiveAllocStack(StringRef, SMLoc);
  127. bool ParseSEHDirectiveEndProlog(StringRef, SMLoc);
  128. bool ParseAtUnwindOrAtExcept(bool &unwind, bool &except);
  129. bool ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc);
  130. public:
  131. COFFAsmParser() = default;
  132. };
  133. } // end anonymous namespace.
  134. static SectionKind computeSectionKind(unsigned Flags) {
  135. if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE)
  136. return SectionKind::getText();
  137. if (Flags & COFF::IMAGE_SCN_MEM_READ &&
  138. (Flags & COFF::IMAGE_SCN_MEM_WRITE) == 0)
  139. return SectionKind::getReadOnly();
  140. return SectionKind::getData();
  141. }
  142. bool COFFAsmParser::ParseSectionFlags(StringRef SectionName,
  143. StringRef FlagsString, unsigned *Flags) {
  144. enum {
  145. None = 0,
  146. Alloc = 1 << 0,
  147. Code = 1 << 1,
  148. Load = 1 << 2,
  149. InitData = 1 << 3,
  150. Shared = 1 << 4,
  151. NoLoad = 1 << 5,
  152. NoRead = 1 << 6,
  153. NoWrite = 1 << 7,
  154. Discardable = 1 << 8,
  155. };
  156. bool ReadOnlyRemoved = false;
  157. unsigned SecFlags = None;
  158. for (char FlagChar : FlagsString) {
  159. switch (FlagChar) {
  160. case 'a':
  161. // Ignored.
  162. break;
  163. case 'b': // bss section
  164. SecFlags |= Alloc;
  165. if (SecFlags & InitData)
  166. return TokError("conflicting section flags 'b' and 'd'.");
  167. SecFlags &= ~Load;
  168. break;
  169. case 'd': // data section
  170. SecFlags |= InitData;
  171. if (SecFlags & Alloc)
  172. return TokError("conflicting section flags 'b' and 'd'.");
  173. SecFlags &= ~NoWrite;
  174. if ((SecFlags & NoLoad) == 0)
  175. SecFlags |= Load;
  176. break;
  177. case 'n': // section is not loaded
  178. SecFlags |= NoLoad;
  179. SecFlags &= ~Load;
  180. break;
  181. case 'D': // discardable
  182. SecFlags |= Discardable;
  183. break;
  184. case 'r': // read-only
  185. ReadOnlyRemoved = false;
  186. SecFlags |= NoWrite;
  187. if ((SecFlags & Code) == 0)
  188. SecFlags |= InitData;
  189. if ((SecFlags & NoLoad) == 0)
  190. SecFlags |= Load;
  191. break;
  192. case 's': // shared section
  193. SecFlags |= Shared | InitData;
  194. SecFlags &= ~NoWrite;
  195. if ((SecFlags & NoLoad) == 0)
  196. SecFlags |= Load;
  197. break;
  198. case 'w': // writable
  199. SecFlags &= ~NoWrite;
  200. ReadOnlyRemoved = true;
  201. break;
  202. case 'x': // executable section
  203. SecFlags |= Code;
  204. if ((SecFlags & NoLoad) == 0)
  205. SecFlags |= Load;
  206. if (!ReadOnlyRemoved)
  207. SecFlags |= NoWrite;
  208. break;
  209. case 'y': // not readable
  210. SecFlags |= NoRead | NoWrite;
  211. break;
  212. default:
  213. return TokError("unknown flag");
  214. }
  215. }
  216. *Flags = 0;
  217. if (SecFlags == None)
  218. SecFlags = InitData;
  219. if (SecFlags & Code)
  220. *Flags |= COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE;
  221. if (SecFlags & InitData)
  222. *Flags |= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA;
  223. if ((SecFlags & Alloc) && (SecFlags & Load) == 0)
  224. *Flags |= COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
  225. if (SecFlags & NoLoad)
  226. *Flags |= COFF::IMAGE_SCN_LNK_REMOVE;
  227. if ((SecFlags & Discardable) ||
  228. MCSectionCOFF::isImplicitlyDiscardable(SectionName))
  229. *Flags |= COFF::IMAGE_SCN_MEM_DISCARDABLE;
  230. if ((SecFlags & NoRead) == 0)
  231. *Flags |= COFF::IMAGE_SCN_MEM_READ;
  232. if ((SecFlags & NoWrite) == 0)
  233. *Flags |= COFF::IMAGE_SCN_MEM_WRITE;
  234. if (SecFlags & Shared)
  235. *Flags |= COFF::IMAGE_SCN_MEM_SHARED;
  236. return false;
  237. }
  238. /// ParseDirectiveSymbolAttribute
  239. /// ::= { ".weak", ... } [ identifier ( , identifier )* ]
  240. bool COFFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
  241. MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
  242. .Case(".weak", MCSA_Weak)
  243. .Default(MCSA_Invalid);
  244. assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
  245. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  246. while (true) {
  247. StringRef Name;
  248. if (getParser().parseIdentifier(Name))
  249. return TokError("expected identifier in directive");
  250. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  251. getStreamer().emitSymbolAttribute(Sym, Attr);
  252. if (getLexer().is(AsmToken::EndOfStatement))
  253. break;
  254. if (getLexer().isNot(AsmToken::Comma))
  255. return TokError("unexpected token in directive");
  256. Lex();
  257. }
  258. }
  259. Lex();
  260. return false;
  261. }
  262. bool COFFAsmParser::ParseDirectiveCGProfile(StringRef S, SMLoc Loc) {
  263. return MCAsmParserExtension::ParseDirectiveCGProfile(S, Loc);
  264. }
  265. bool COFFAsmParser::ParseSectionSwitch(StringRef Section,
  266. unsigned Characteristics,
  267. SectionKind Kind) {
  268. return ParseSectionSwitch(Section, Characteristics, Kind, "", (COFF::COMDATType)0);
  269. }
  270. bool COFFAsmParser::ParseSectionSwitch(StringRef Section,
  271. unsigned Characteristics,
  272. SectionKind Kind,
  273. StringRef COMDATSymName,
  274. COFF::COMDATType Type) {
  275. if (getLexer().isNot(AsmToken::EndOfStatement))
  276. return TokError("unexpected token in section switching directive");
  277. Lex();
  278. getStreamer().SwitchSection(getContext().getCOFFSection(
  279. Section, Characteristics, Kind, COMDATSymName, Type));
  280. return false;
  281. }
  282. bool COFFAsmParser::ParseSectionName(StringRef &SectionName) {
  283. if (!getLexer().is(AsmToken::Identifier) && !getLexer().is(AsmToken::String))
  284. return true;
  285. SectionName = getTok().getIdentifier();
  286. Lex();
  287. return false;
  288. }
  289. // .section name [, "flags"] [, identifier [ identifier ], identifier]
  290. //
  291. // Supported flags:
  292. // a: Ignored.
  293. // b: BSS section (uninitialized data)
  294. // d: data section (initialized data)
  295. // n: "noload" section (removed by linker)
  296. // D: Discardable section
  297. // r: Readable section
  298. // s: Shared section
  299. // w: Writable section
  300. // x: Executable section
  301. // y: Not-readable section (clears 'r')
  302. //
  303. // Subsections are not supported.
  304. bool COFFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
  305. StringRef SectionName;
  306. if (ParseSectionName(SectionName))
  307. return TokError("expected identifier in directive");
  308. unsigned Flags = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
  309. COFF::IMAGE_SCN_MEM_READ |
  310. COFF::IMAGE_SCN_MEM_WRITE;
  311. if (getLexer().is(AsmToken::Comma)) {
  312. Lex();
  313. if (getLexer().isNot(AsmToken::String))
  314. return TokError("expected string in directive");
  315. StringRef FlagsStr = getTok().getStringContents();
  316. Lex();
  317. if (ParseSectionFlags(SectionName, FlagsStr, &Flags))
  318. return true;
  319. }
  320. COFF::COMDATType Type = (COFF::COMDATType)0;
  321. StringRef COMDATSymName;
  322. if (getLexer().is(AsmToken::Comma)) {
  323. Type = COFF::IMAGE_COMDAT_SELECT_ANY;
  324. Lex();
  325. Flags |= COFF::IMAGE_SCN_LNK_COMDAT;
  326. if (!getLexer().is(AsmToken::Identifier))
  327. return TokError("expected comdat type such as 'discard' or 'largest' "
  328. "after protection bits");
  329. if (parseCOMDATType(Type))
  330. return true;
  331. if (getLexer().isNot(AsmToken::Comma))
  332. return TokError("expected comma in directive");
  333. Lex();
  334. if (getParser().parseIdentifier(COMDATSymName))
  335. return TokError("expected identifier in directive");
  336. }
  337. if (getLexer().isNot(AsmToken::EndOfStatement))
  338. return TokError("unexpected token in directive");
  339. SectionKind Kind = computeSectionKind(Flags);
  340. if (Kind.isText()) {
  341. const Triple &T = getContext().getTargetTriple();
  342. if (T.getArch() == Triple::arm || T.getArch() == Triple::thumb)
  343. Flags |= COFF::IMAGE_SCN_MEM_16BIT;
  344. }
  345. ParseSectionSwitch(SectionName, Flags, Kind, COMDATSymName, Type);
  346. return false;
  347. }
  348. bool COFFAsmParser::ParseDirectiveDef(StringRef, SMLoc) {
  349. StringRef SymbolName;
  350. if (getParser().parseIdentifier(SymbolName))
  351. return TokError("expected identifier in directive");
  352. MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName);
  353. getStreamer().BeginCOFFSymbolDef(Sym);
  354. Lex();
  355. return false;
  356. }
  357. bool COFFAsmParser::ParseDirectiveScl(StringRef, SMLoc) {
  358. int64_t SymbolStorageClass;
  359. if (getParser().parseAbsoluteExpression(SymbolStorageClass))
  360. return true;
  361. if (getLexer().isNot(AsmToken::EndOfStatement))
  362. return TokError("unexpected token in directive");
  363. Lex();
  364. getStreamer().EmitCOFFSymbolStorageClass(SymbolStorageClass);
  365. return false;
  366. }
  367. bool COFFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
  368. int64_t Type;
  369. if (getParser().parseAbsoluteExpression(Type))
  370. return true;
  371. if (getLexer().isNot(AsmToken::EndOfStatement))
  372. return TokError("unexpected token in directive");
  373. Lex();
  374. getStreamer().EmitCOFFSymbolType(Type);
  375. return false;
  376. }
  377. bool COFFAsmParser::ParseDirectiveEndef(StringRef, SMLoc) {
  378. Lex();
  379. getStreamer().EndCOFFSymbolDef();
  380. return false;
  381. }
  382. bool COFFAsmParser::ParseDirectiveSecRel32(StringRef, SMLoc) {
  383. StringRef SymbolID;
  384. if (getParser().parseIdentifier(SymbolID))
  385. return TokError("expected identifier in directive");
  386. int64_t Offset = 0;
  387. SMLoc OffsetLoc;
  388. if (getLexer().is(AsmToken::Plus)) {
  389. OffsetLoc = getLexer().getLoc();
  390. if (getParser().parseAbsoluteExpression(Offset))
  391. return true;
  392. }
  393. if (getLexer().isNot(AsmToken::EndOfStatement))
  394. return TokError("unexpected token in directive");
  395. if (Offset < 0 || Offset > std::numeric_limits<uint32_t>::max())
  396. return Error(
  397. OffsetLoc,
  398. "invalid '.secrel32' directive offset, can't be less "
  399. "than zero or greater than std::numeric_limits<uint32_t>::max()");
  400. MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
  401. Lex();
  402. getStreamer().EmitCOFFSecRel32(Symbol, Offset);
  403. return false;
  404. }
  405. bool COFFAsmParser::ParseDirectiveRVA(StringRef, SMLoc) {
  406. auto parseOp = [&]() -> bool {
  407. StringRef SymbolID;
  408. if (getParser().parseIdentifier(SymbolID))
  409. return TokError("expected identifier in directive");
  410. int64_t Offset = 0;
  411. SMLoc OffsetLoc;
  412. if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
  413. OffsetLoc = getLexer().getLoc();
  414. if (getParser().parseAbsoluteExpression(Offset))
  415. return true;
  416. }
  417. if (Offset < std::numeric_limits<int32_t>::min() ||
  418. Offset > std::numeric_limits<int32_t>::max())
  419. return Error(OffsetLoc, "invalid '.rva' directive offset, can't be less "
  420. "than -2147483648 or greater than "
  421. "2147483647");
  422. MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
  423. getStreamer().EmitCOFFImgRel32(Symbol, Offset);
  424. return false;
  425. };
  426. if (getParser().parseMany(parseOp))
  427. return addErrorSuffix(" in directive");
  428. return false;
  429. }
  430. bool COFFAsmParser::ParseDirectiveSafeSEH(StringRef, SMLoc) {
  431. StringRef SymbolID;
  432. if (getParser().parseIdentifier(SymbolID))
  433. return TokError("expected identifier in directive");
  434. if (getLexer().isNot(AsmToken::EndOfStatement))
  435. return TokError("unexpected token in directive");
  436. MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
  437. Lex();
  438. getStreamer().EmitCOFFSafeSEH(Symbol);
  439. return false;
  440. }
  441. bool COFFAsmParser::ParseDirectiveSecIdx(StringRef, SMLoc) {
  442. StringRef SymbolID;
  443. if (getParser().parseIdentifier(SymbolID))
  444. return TokError("expected identifier in directive");
  445. if (getLexer().isNot(AsmToken::EndOfStatement))
  446. return TokError("unexpected token in directive");
  447. MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
  448. Lex();
  449. getStreamer().EmitCOFFSectionIndex(Symbol);
  450. return false;
  451. }
  452. bool COFFAsmParser::ParseDirectiveSymIdx(StringRef, SMLoc) {
  453. StringRef SymbolID;
  454. if (getParser().parseIdentifier(SymbolID))
  455. return TokError("expected identifier in directive");
  456. if (getLexer().isNot(AsmToken::EndOfStatement))
  457. return TokError("unexpected token in directive");
  458. MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
  459. Lex();
  460. getStreamer().EmitCOFFSymbolIndex(Symbol);
  461. return false;
  462. }
  463. /// ::= [ identifier ]
  464. bool COFFAsmParser::parseCOMDATType(COFF::COMDATType &Type) {
  465. StringRef TypeId = getTok().getIdentifier();
  466. Type = StringSwitch<COFF::COMDATType>(TypeId)
  467. .Case("one_only", COFF::IMAGE_COMDAT_SELECT_NODUPLICATES)
  468. .Case("discard", COFF::IMAGE_COMDAT_SELECT_ANY)
  469. .Case("same_size", COFF::IMAGE_COMDAT_SELECT_SAME_SIZE)
  470. .Case("same_contents", COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH)
  471. .Case("associative", COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
  472. .Case("largest", COFF::IMAGE_COMDAT_SELECT_LARGEST)
  473. .Case("newest", COFF::IMAGE_COMDAT_SELECT_NEWEST)
  474. .Default((COFF::COMDATType)0);
  475. if (Type == 0)
  476. return TokError(Twine("unrecognized COMDAT type '" + TypeId + "'"));
  477. Lex();
  478. return false;
  479. }
  480. /// ParseDirectiveLinkOnce
  481. /// ::= .linkonce [ identifier ]
  482. bool COFFAsmParser::ParseDirectiveLinkOnce(StringRef, SMLoc Loc) {
  483. COFF::COMDATType Type = COFF::IMAGE_COMDAT_SELECT_ANY;
  484. if (getLexer().is(AsmToken::Identifier))
  485. if (parseCOMDATType(Type))
  486. return true;
  487. const MCSectionCOFF *Current =
  488. static_cast<const MCSectionCOFF *>(getStreamer().getCurrentSectionOnly());
  489. if (Type == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
  490. return Error(Loc, "cannot make section associative with .linkonce");
  491. if (Current->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT)
  492. return Error(Loc, Twine("section '") + Current->getName() +
  493. "' is already linkonce");
  494. Current->setSelection(Type);
  495. if (getLexer().isNot(AsmToken::EndOfStatement))
  496. return TokError("unexpected token in directive");
  497. return false;
  498. }
  499. bool COFFAsmParser::ParseSEHDirectiveStartProc(StringRef, SMLoc Loc) {
  500. StringRef SymbolID;
  501. if (getParser().parseIdentifier(SymbolID))
  502. return true;
  503. if (getLexer().isNot(AsmToken::EndOfStatement))
  504. return TokError("unexpected token in directive");
  505. MCSymbol *Symbol = getContext().getOrCreateSymbol(SymbolID);
  506. Lex();
  507. getStreamer().EmitWinCFIStartProc(Symbol, Loc);
  508. return false;
  509. }
  510. bool COFFAsmParser::ParseSEHDirectiveEndProc(StringRef, SMLoc Loc) {
  511. Lex();
  512. getStreamer().EmitWinCFIEndProc(Loc);
  513. return false;
  514. }
  515. bool COFFAsmParser::ParseSEHDirectiveEndFuncletOrFunc(StringRef, SMLoc Loc) {
  516. Lex();
  517. getStreamer().EmitWinCFIFuncletOrFuncEnd(Loc);
  518. return false;
  519. }
  520. bool COFFAsmParser::ParseSEHDirectiveStartChained(StringRef, SMLoc Loc) {
  521. Lex();
  522. getStreamer().EmitWinCFIStartChained(Loc);
  523. return false;
  524. }
  525. bool COFFAsmParser::ParseSEHDirectiveEndChained(StringRef, SMLoc Loc) {
  526. Lex();
  527. getStreamer().EmitWinCFIEndChained(Loc);
  528. return false;
  529. }
  530. bool COFFAsmParser::ParseSEHDirectiveHandler(StringRef, SMLoc Loc) {
  531. StringRef SymbolID;
  532. if (getParser().parseIdentifier(SymbolID))
  533. return true;
  534. if (getLexer().isNot(AsmToken::Comma))
  535. return TokError("you must specify one or both of @unwind or @except");
  536. Lex();
  537. bool unwind = false, except = false;
  538. if (ParseAtUnwindOrAtExcept(unwind, except))
  539. return true;
  540. if (getLexer().is(AsmToken::Comma)) {
  541. Lex();
  542. if (ParseAtUnwindOrAtExcept(unwind, except))
  543. return true;
  544. }
  545. if (getLexer().isNot(AsmToken::EndOfStatement))
  546. return TokError("unexpected token in directive");
  547. MCSymbol *handler = getContext().getOrCreateSymbol(SymbolID);
  548. Lex();
  549. getStreamer().EmitWinEHHandler(handler, unwind, except, Loc);
  550. return false;
  551. }
  552. bool COFFAsmParser::ParseSEHDirectiveHandlerData(StringRef, SMLoc Loc) {
  553. Lex();
  554. getStreamer().EmitWinEHHandlerData();
  555. return false;
  556. }
  557. bool COFFAsmParser::ParseSEHDirectiveAllocStack(StringRef, SMLoc Loc) {
  558. int64_t Size;
  559. if (getParser().parseAbsoluteExpression(Size))
  560. return true;
  561. if (getLexer().isNot(AsmToken::EndOfStatement))
  562. return TokError("unexpected token in directive");
  563. Lex();
  564. getStreamer().EmitWinCFIAllocStack(Size, Loc);
  565. return false;
  566. }
  567. bool COFFAsmParser::ParseSEHDirectiveEndProlog(StringRef, SMLoc Loc) {
  568. Lex();
  569. getStreamer().EmitWinCFIEndProlog(Loc);
  570. return false;
  571. }
  572. bool COFFAsmParser::ParseAtUnwindOrAtExcept(bool &unwind, bool &except) {
  573. StringRef identifier;
  574. if (getLexer().isNot(AsmToken::At))
  575. return TokError("a handler attribute must begin with '@'");
  576. SMLoc startLoc = getLexer().getLoc();
  577. Lex();
  578. if (getParser().parseIdentifier(identifier))
  579. return Error(startLoc, "expected @unwind or @except");
  580. if (identifier == "unwind")
  581. unwind = true;
  582. else if (identifier == "except")
  583. except = true;
  584. else
  585. return Error(startLoc, "expected @unwind or @except");
  586. return false;
  587. }
  588. namespace llvm {
  589. MCAsmParserExtension *createCOFFAsmParser() {
  590. return new COFFAsmParser;
  591. }
  592. } // end namespace llvm