ELFAsmParser.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/MC/MCParser/MCAsmParserExtension.h"
  10. #include "llvm/ADT/StringSwitch.h"
  11. #include "llvm/ADT/Twine.h"
  12. #include "llvm/MC/MCAsmInfo.h"
  13. #include "llvm/MC/MCContext.h"
  14. #include "llvm/MC/MCExpr.h"
  15. #include "llvm/MC/MCParser/MCAsmLexer.h"
  16. #include "llvm/MC/MCSectionELF.h"
  17. #include "llvm/MC/MCStreamer.h"
  18. #include "llvm/MC/MCSymbolELF.h"
  19. #include "llvm/Support/ELF.h"
  20. using namespace llvm;
  21. namespace {
  22. class ELFAsmParser : public MCAsmParserExtension {
  23. template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
  24. void addDirectiveHandler(StringRef Directive) {
  25. MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
  26. this, HandleDirective<ELFAsmParser, HandlerMethod>);
  27. getParser().addDirectiveHandler(Directive, Handler);
  28. }
  29. bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
  30. SectionKind Kind);
  31. public:
  32. ELFAsmParser() { BracketExpressionsSupported = true; }
  33. void Initialize(MCAsmParser &Parser) override {
  34. // Call the base implementation.
  35. this->MCAsmParserExtension::Initialize(Parser);
  36. addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
  37. addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
  38. addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
  39. addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
  40. addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
  41. addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
  42. addDirectiveHandler<
  43. &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
  44. addDirectiveHandler<
  45. &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
  46. addDirectiveHandler<
  47. &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
  48. addDirectiveHandler<
  49. &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
  50. addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
  51. addDirectiveHandler<
  52. &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
  53. addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
  54. addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
  55. addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
  56. addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
  57. addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
  58. addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
  59. addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
  60. addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
  61. addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
  62. addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
  63. addDirectiveHandler<
  64. &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
  65. addDirectiveHandler<
  66. &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
  67. addDirectiveHandler<
  68. &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
  69. addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
  70. }
  71. // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
  72. // the best way for us to get access to it?
  73. bool ParseSectionDirectiveData(StringRef, SMLoc) {
  74. return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
  75. ELF::SHF_WRITE |ELF::SHF_ALLOC,
  76. SectionKind::getDataRel());
  77. }
  78. bool ParseSectionDirectiveText(StringRef, SMLoc) {
  79. return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
  80. ELF::SHF_EXECINSTR |
  81. ELF::SHF_ALLOC, SectionKind::getText());
  82. }
  83. bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
  84. return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
  85. ELF::SHF_WRITE |
  86. ELF::SHF_ALLOC, SectionKind::getBSS());
  87. }
  88. bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
  89. return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
  90. ELF::SHF_ALLOC,
  91. SectionKind::getReadOnly());
  92. }
  93. bool ParseSectionDirectiveTData(StringRef, SMLoc) {
  94. return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
  95. ELF::SHF_ALLOC |
  96. ELF::SHF_TLS | ELF::SHF_WRITE,
  97. SectionKind::getThreadData());
  98. }
  99. bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
  100. return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
  101. ELF::SHF_ALLOC |
  102. ELF::SHF_TLS | ELF::SHF_WRITE,
  103. SectionKind::getThreadBSS());
  104. }
  105. bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
  106. return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
  107. ELF::SHF_ALLOC |
  108. ELF::SHF_WRITE,
  109. SectionKind::getDataRel());
  110. }
  111. bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
  112. return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
  113. ELF::SHF_ALLOC |
  114. ELF::SHF_WRITE,
  115. SectionKind::getReadOnlyWithRel());
  116. }
  117. bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
  118. return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
  119. ELF::SHF_ALLOC |
  120. ELF::SHF_WRITE,
  121. SectionKind::getReadOnlyWithRelLocal());
  122. }
  123. bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
  124. return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
  125. ELF::SHF_ALLOC |
  126. ELF::SHF_WRITE,
  127. SectionKind::getDataRel());
  128. }
  129. bool ParseDirectivePushSection(StringRef, SMLoc);
  130. bool ParseDirectivePopSection(StringRef, SMLoc);
  131. bool ParseDirectiveSection(StringRef, SMLoc);
  132. bool ParseDirectiveSize(StringRef, SMLoc);
  133. bool ParseDirectivePrevious(StringRef, SMLoc);
  134. bool ParseDirectiveType(StringRef, SMLoc);
  135. bool ParseDirectiveIdent(StringRef, SMLoc);
  136. bool ParseDirectiveSymver(StringRef, SMLoc);
  137. bool ParseDirectiveVersion(StringRef, SMLoc);
  138. bool ParseDirectiveWeakref(StringRef, SMLoc);
  139. bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
  140. bool ParseDirectiveSubsection(StringRef, SMLoc);
  141. private:
  142. bool ParseSectionName(StringRef &SectionName);
  143. bool ParseSectionArguments(bool IsPush, SMLoc loc);
  144. unsigned parseSunStyleSectionFlags();
  145. };
  146. }
  147. /// ParseDirectiveSymbolAttribute
  148. /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
  149. bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
  150. MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
  151. .Case(".weak", MCSA_Weak)
  152. .Case(".local", MCSA_Local)
  153. .Case(".hidden", MCSA_Hidden)
  154. .Case(".internal", MCSA_Internal)
  155. .Case(".protected", MCSA_Protected)
  156. .Default(MCSA_Invalid);
  157. assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
  158. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  159. for (;;) {
  160. StringRef Name;
  161. if (getParser().parseIdentifier(Name))
  162. return TokError("expected identifier in directive");
  163. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  164. getStreamer().EmitSymbolAttribute(Sym, Attr);
  165. if (getLexer().is(AsmToken::EndOfStatement))
  166. break;
  167. if (getLexer().isNot(AsmToken::Comma))
  168. return TokError("unexpected token in directive");
  169. Lex();
  170. }
  171. }
  172. Lex();
  173. return false;
  174. }
  175. bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
  176. unsigned Flags, SectionKind Kind) {
  177. const MCExpr *Subsection = nullptr;
  178. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  179. if (getParser().parseExpression(Subsection))
  180. return true;
  181. }
  182. getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
  183. Subsection);
  184. return false;
  185. }
  186. bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
  187. StringRef Name;
  188. if (getParser().parseIdentifier(Name))
  189. return TokError("expected identifier in directive");
  190. MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
  191. if (getLexer().isNot(AsmToken::Comma))
  192. return TokError("unexpected token in directive");
  193. Lex();
  194. const MCExpr *Expr;
  195. if (getParser().parseExpression(Expr))
  196. return true;
  197. if (getLexer().isNot(AsmToken::EndOfStatement))
  198. return TokError("unexpected token in directive");
  199. getStreamer().emitELFSize(Sym, Expr);
  200. return false;
  201. }
  202. bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
  203. // A section name can contain -, so we cannot just use
  204. // parseIdentifier.
  205. SMLoc FirstLoc = getLexer().getLoc();
  206. unsigned Size = 0;
  207. if (getLexer().is(AsmToken::String)) {
  208. SectionName = getTok().getIdentifier();
  209. Lex();
  210. return false;
  211. }
  212. for (;;) {
  213. unsigned CurSize;
  214. SMLoc PrevLoc = getLexer().getLoc();
  215. if (getLexer().is(AsmToken::Minus)) {
  216. CurSize = 1;
  217. Lex(); // Consume the "-".
  218. } else if (getLexer().is(AsmToken::String)) {
  219. CurSize = getTok().getIdentifier().size() + 2;
  220. Lex();
  221. } else if (getLexer().is(AsmToken::Identifier)) {
  222. CurSize = getTok().getIdentifier().size();
  223. Lex();
  224. } else {
  225. break;
  226. }
  227. Size += CurSize;
  228. SectionName = StringRef(FirstLoc.getPointer(), Size);
  229. // Make sure the following token is adjacent.
  230. if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
  231. break;
  232. }
  233. if (Size == 0)
  234. return true;
  235. return false;
  236. }
  237. static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
  238. unsigned flags = 0;
  239. for (unsigned i = 0; i < flagsStr.size(); i++) {
  240. switch (flagsStr[i]) {
  241. case 'a':
  242. flags |= ELF::SHF_ALLOC;
  243. break;
  244. case 'e':
  245. flags |= ELF::SHF_EXCLUDE;
  246. break;
  247. case 'x':
  248. flags |= ELF::SHF_EXECINSTR;
  249. break;
  250. case 'w':
  251. flags |= ELF::SHF_WRITE;
  252. break;
  253. case 'M':
  254. flags |= ELF::SHF_MERGE;
  255. break;
  256. case 'S':
  257. flags |= ELF::SHF_STRINGS;
  258. break;
  259. case 'T':
  260. flags |= ELF::SHF_TLS;
  261. break;
  262. case 'c':
  263. flags |= ELF::XCORE_SHF_CP_SECTION;
  264. break;
  265. case 'd':
  266. flags |= ELF::XCORE_SHF_DP_SECTION;
  267. break;
  268. case 'G':
  269. flags |= ELF::SHF_GROUP;
  270. break;
  271. case '?':
  272. *UseLastGroup = true;
  273. break;
  274. default:
  275. return -1U;
  276. }
  277. }
  278. return flags;
  279. }
  280. unsigned ELFAsmParser::parseSunStyleSectionFlags() {
  281. unsigned flags = 0;
  282. while (getLexer().is(AsmToken::Hash)) {
  283. Lex(); // Eat the #.
  284. if (!getLexer().is(AsmToken::Identifier))
  285. return -1U;
  286. StringRef flagId = getTok().getIdentifier();
  287. if (flagId == "alloc")
  288. flags |= ELF::SHF_ALLOC;
  289. else if (flagId == "execinstr")
  290. flags |= ELF::SHF_EXECINSTR;
  291. else if (flagId == "write")
  292. flags |= ELF::SHF_WRITE;
  293. else if (flagId == "tls")
  294. flags |= ELF::SHF_TLS;
  295. else
  296. return -1U;
  297. Lex(); // Eat the flag.
  298. if (!getLexer().is(AsmToken::Comma))
  299. break;
  300. Lex(); // Eat the comma.
  301. }
  302. return flags;
  303. }
  304. bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
  305. getStreamer().PushSection();
  306. if (ParseSectionArguments(/*IsPush=*/true, loc)) {
  307. getStreamer().PopSection();
  308. return true;
  309. }
  310. return false;
  311. }
  312. bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
  313. if (!getStreamer().PopSection())
  314. return TokError(".popsection without corresponding .pushsection");
  315. return false;
  316. }
  317. // FIXME: This is a work in progress.
  318. bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
  319. return ParseSectionArguments(/*IsPush=*/false, loc);
  320. }
  321. bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
  322. StringRef SectionName;
  323. if (ParseSectionName(SectionName))
  324. return TokError("expected identifier in directive");
  325. StringRef TypeName;
  326. int64_t Size = 0;
  327. StringRef GroupName;
  328. unsigned Flags = 0;
  329. const MCExpr *Subsection = nullptr;
  330. bool UseLastGroup = false;
  331. StringRef UniqueStr;
  332. int64_t UniqueID = ~0;
  333. // Set the defaults first.
  334. if (SectionName == ".fini" || SectionName == ".init" ||
  335. SectionName == ".rodata")
  336. Flags |= ELF::SHF_ALLOC;
  337. if (SectionName == ".fini" || SectionName == ".init")
  338. Flags |= ELF::SHF_EXECINSTR;
  339. if (getLexer().is(AsmToken::Comma)) {
  340. Lex();
  341. if (IsPush && getLexer().isNot(AsmToken::String)) {
  342. if (getParser().parseExpression(Subsection))
  343. return true;
  344. if (getLexer().isNot(AsmToken::Comma))
  345. goto EndStmt;
  346. Lex();
  347. }
  348. unsigned extraFlags;
  349. if (getLexer().isNot(AsmToken::String)) {
  350. if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
  351. || getLexer().isNot(AsmToken::Hash))
  352. return TokError("expected string in directive");
  353. extraFlags = parseSunStyleSectionFlags();
  354. } else {
  355. StringRef FlagsStr = getTok().getStringContents();
  356. Lex();
  357. extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
  358. }
  359. if (extraFlags == -1U)
  360. return TokError("unknown flag");
  361. Flags |= extraFlags;
  362. bool Mergeable = Flags & ELF::SHF_MERGE;
  363. bool Group = Flags & ELF::SHF_GROUP;
  364. if (Group && UseLastGroup)
  365. return TokError("Section cannot specifiy a group name while also acting "
  366. "as a member of the last group");
  367. if (getLexer().isNot(AsmToken::Comma)) {
  368. if (Mergeable)
  369. return TokError("Mergeable section must specify the type");
  370. if (Group)
  371. return TokError("Group section must specify the type");
  372. } else {
  373. Lex();
  374. if (getLexer().is(AsmToken::At) || getLexer().is(AsmToken::Percent) ||
  375. getLexer().is(AsmToken::String)) {
  376. if (!getLexer().is(AsmToken::String))
  377. Lex();
  378. } else
  379. return TokError("expected '@<type>', '%<type>' or \"<type>\"");
  380. if (getParser().parseIdentifier(TypeName))
  381. return TokError("expected identifier in directive");
  382. if (Mergeable) {
  383. if (getLexer().isNot(AsmToken::Comma))
  384. return TokError("expected the entry size");
  385. Lex();
  386. if (getParser().parseAbsoluteExpression(Size))
  387. return true;
  388. if (Size <= 0)
  389. return TokError("entry size must be positive");
  390. }
  391. if (Group) {
  392. if (getLexer().isNot(AsmToken::Comma))
  393. return TokError("expected group name");
  394. Lex();
  395. if (getParser().parseIdentifier(GroupName))
  396. return true;
  397. if (getLexer().is(AsmToken::Comma)) {
  398. Lex();
  399. StringRef Linkage;
  400. if (getParser().parseIdentifier(Linkage))
  401. return true;
  402. if (Linkage != "comdat")
  403. return TokError("Linkage must be 'comdat'");
  404. }
  405. }
  406. if (getLexer().is(AsmToken::Comma)) {
  407. Lex();
  408. if (getParser().parseIdentifier(UniqueStr))
  409. return TokError("expected identifier in directive");
  410. if (UniqueStr != "unique")
  411. return TokError("expected 'unique'");
  412. if (getLexer().isNot(AsmToken::Comma))
  413. return TokError("expected commma");
  414. Lex();
  415. if (getParser().parseAbsoluteExpression(UniqueID))
  416. return true;
  417. if (UniqueID < 0)
  418. return TokError("unique id must be positive");
  419. if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
  420. return TokError("unique id is too large");
  421. }
  422. }
  423. }
  424. EndStmt:
  425. if (getLexer().isNot(AsmToken::EndOfStatement))
  426. return TokError("unexpected token in directive");
  427. unsigned Type = ELF::SHT_PROGBITS;
  428. if (TypeName.empty()) {
  429. if (SectionName.startswith(".note"))
  430. Type = ELF::SHT_NOTE;
  431. else if (SectionName == ".init_array")
  432. Type = ELF::SHT_INIT_ARRAY;
  433. else if (SectionName == ".fini_array")
  434. Type = ELF::SHT_FINI_ARRAY;
  435. else if (SectionName == ".preinit_array")
  436. Type = ELF::SHT_PREINIT_ARRAY;
  437. } else {
  438. if (TypeName == "init_array")
  439. Type = ELF::SHT_INIT_ARRAY;
  440. else if (TypeName == "fini_array")
  441. Type = ELF::SHT_FINI_ARRAY;
  442. else if (TypeName == "preinit_array")
  443. Type = ELF::SHT_PREINIT_ARRAY;
  444. else if (TypeName == "nobits")
  445. Type = ELF::SHT_NOBITS;
  446. else if (TypeName == "progbits")
  447. Type = ELF::SHT_PROGBITS;
  448. else if (TypeName == "note")
  449. Type = ELF::SHT_NOTE;
  450. else if (TypeName == "unwind")
  451. Type = ELF::SHT_X86_64_UNWIND;
  452. else
  453. return TokError("unknown section type");
  454. }
  455. if (UseLastGroup) {
  456. MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
  457. if (const MCSectionELF *Section =
  458. cast_or_null<MCSectionELF>(CurrentSection.first))
  459. if (const MCSymbol *Group = Section->getGroup()) {
  460. GroupName = Group->getName();
  461. Flags |= ELF::SHF_GROUP;
  462. }
  463. }
  464. MCSection *ELFSection = getContext().getELFSection(SectionName, Type, Flags,
  465. Size, GroupName, UniqueID);
  466. getStreamer().SwitchSection(ELFSection, Subsection);
  467. if (getContext().getGenDwarfForAssembly()) {
  468. bool InsertResult = getContext().addGenDwarfSection(ELFSection);
  469. if (InsertResult) {
  470. if (getContext().getDwarfVersion() <= 2)
  471. Warning(loc, "DWARF2 only supports one section per compilation unit");
  472. if (!ELFSection->getBeginSymbol()) {
  473. MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
  474. getStreamer().EmitLabel(SectionStartSymbol);
  475. ELFSection->setBeginSymbol(SectionStartSymbol);
  476. }
  477. }
  478. }
  479. return false;
  480. }
  481. bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
  482. MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
  483. if (PreviousSection.first == nullptr)
  484. return TokError(".previous without corresponding .section");
  485. getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
  486. return false;
  487. }
  488. static MCSymbolAttr MCAttrForString(StringRef Type) {
  489. return StringSwitch<MCSymbolAttr>(Type)
  490. .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
  491. .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
  492. .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
  493. .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
  494. .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
  495. .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
  496. MCSA_ELF_TypeIndFunction)
  497. .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
  498. .Default(MCSA_Invalid);
  499. }
  500. /// ParseDirectiveELFType
  501. /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
  502. /// ::= .type identifier , #attribute
  503. /// ::= .type identifier , @attribute
  504. /// ::= .type identifier , %attribute
  505. /// ::= .type identifier , "attribute"
  506. bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
  507. StringRef Name;
  508. if (getParser().parseIdentifier(Name))
  509. return TokError("expected identifier in directive");
  510. // Handle the identifier as the key symbol.
  511. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  512. // NOTE the comma is optional in all cases. It is only documented as being
  513. // optional in the first case, however, GAS will silently treat the comma as
  514. // optional in all cases. Furthermore, although the documentation states that
  515. // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
  516. // accepts both the upper case name as well as the lower case aliases.
  517. if (getLexer().is(AsmToken::Comma))
  518. Lex();
  519. if (getLexer().isNot(AsmToken::Identifier) &&
  520. getLexer().isNot(AsmToken::Hash) &&
  521. getLexer().isNot(AsmToken::Percent) &&
  522. getLexer().isNot(AsmToken::String)) {
  523. if (!getLexer().getAllowAtInIdentifier())
  524. return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
  525. "'%<type>' or \"<type>\"");
  526. else if (getLexer().isNot(AsmToken::At))
  527. return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
  528. "'%<type>' or \"<type>\"");
  529. }
  530. if (getLexer().isNot(AsmToken::String) &&
  531. getLexer().isNot(AsmToken::Identifier))
  532. Lex();
  533. SMLoc TypeLoc = getLexer().getLoc();
  534. StringRef Type;
  535. if (getParser().parseIdentifier(Type))
  536. return TokError("expected symbol type in directive");
  537. MCSymbolAttr Attr = MCAttrForString(Type);
  538. if (Attr == MCSA_Invalid)
  539. return Error(TypeLoc, "unsupported attribute in '.type' directive");
  540. if (getLexer().isNot(AsmToken::EndOfStatement))
  541. return TokError("unexpected token in '.type' directive");
  542. Lex();
  543. getStreamer().EmitSymbolAttribute(Sym, Attr);
  544. return false;
  545. }
  546. /// ParseDirectiveIdent
  547. /// ::= .ident string
  548. bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
  549. if (getLexer().isNot(AsmToken::String))
  550. return TokError("unexpected token in '.ident' directive");
  551. StringRef Data = getTok().getIdentifier();
  552. Lex();
  553. getStreamer().EmitIdent(Data);
  554. return false;
  555. }
  556. /// ParseDirectiveSymver
  557. /// ::= .symver foo, bar2@zed
  558. bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
  559. StringRef Name;
  560. if (getParser().parseIdentifier(Name))
  561. return TokError("expected identifier in directive");
  562. if (getLexer().isNot(AsmToken::Comma))
  563. return TokError("expected a comma");
  564. // ARM assembly uses @ for a comment...
  565. // except when parsing the second parameter of the .symver directive.
  566. // Force the next symbol to allow @ in the identifier, which is
  567. // required for this directive and then reset it to its initial state.
  568. const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
  569. getLexer().setAllowAtInIdentifier(true);
  570. Lex();
  571. getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
  572. StringRef AliasName;
  573. if (getParser().parseIdentifier(AliasName))
  574. return TokError("expected identifier in directive");
  575. if (AliasName.find('@') == StringRef::npos)
  576. return TokError("expected a '@' in the name");
  577. MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
  578. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  579. const MCExpr *Value = MCSymbolRefExpr::create(Sym, getContext());
  580. getStreamer().EmitAssignment(Alias, Value);
  581. return false;
  582. }
  583. /// ParseDirectiveVersion
  584. /// ::= .version string
  585. bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
  586. if (getLexer().isNot(AsmToken::String))
  587. return TokError("unexpected token in '.version' directive");
  588. StringRef Data = getTok().getIdentifier();
  589. Lex();
  590. MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
  591. getStreamer().PushSection();
  592. getStreamer().SwitchSection(Note);
  593. getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
  594. getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
  595. getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
  596. getStreamer().EmitBytes(Data); // name.
  597. getStreamer().EmitIntValue(0, 1); // terminate the string.
  598. getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
  599. getStreamer().PopSection();
  600. return false;
  601. }
  602. /// ParseDirectiveWeakref
  603. /// ::= .weakref foo, bar
  604. bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
  605. // FIXME: Share code with the other alias building directives.
  606. StringRef AliasName;
  607. if (getParser().parseIdentifier(AliasName))
  608. return TokError("expected identifier in directive");
  609. if (getLexer().isNot(AsmToken::Comma))
  610. return TokError("expected a comma");
  611. Lex();
  612. StringRef Name;
  613. if (getParser().parseIdentifier(Name))
  614. return TokError("expected identifier in directive");
  615. MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
  616. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  617. getStreamer().EmitWeakReference(Alias, Sym);
  618. return false;
  619. }
  620. bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
  621. const MCExpr *Subsection = nullptr;
  622. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  623. if (getParser().parseExpression(Subsection))
  624. return true;
  625. }
  626. if (getLexer().isNot(AsmToken::EndOfStatement))
  627. return TokError("unexpected token in directive");
  628. getStreamer().SubSection(Subsection);
  629. return false;
  630. }
  631. namespace llvm {
  632. MCAsmParserExtension *createELFAsmParser() {
  633. return new ELFAsmParser;
  634. }
  635. }