ELFAsmParser.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. //===- ELFAsmParser.cpp - ELF 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/BinaryFormat/ELF.h"
  11. #include "llvm/MC/MCAsmInfo.h"
  12. #include "llvm/MC/MCContext.h"
  13. #include "llvm/MC/MCDirectives.h"
  14. #include "llvm/MC/MCExpr.h"
  15. #include "llvm/MC/MCParser/MCAsmLexer.h"
  16. #include "llvm/MC/MCParser/MCAsmParser.h"
  17. #include "llvm/MC/MCParser/MCAsmParserExtension.h"
  18. #include "llvm/MC/MCSection.h"
  19. #include "llvm/MC/MCSectionELF.h"
  20. #include "llvm/MC/MCStreamer.h"
  21. #include "llvm/MC/MCSymbol.h"
  22. #include "llvm/MC/MCSymbolELF.h"
  23. #include "llvm/MC/SectionKind.h"
  24. #include "llvm/Support/Casting.h"
  25. #include "llvm/Support/MathExtras.h"
  26. #include "llvm/Support/SMLoc.h"
  27. #include <cassert>
  28. #include <cstdint>
  29. #include <utility>
  30. using namespace llvm;
  31. namespace {
  32. class ELFAsmParser : public MCAsmParserExtension {
  33. template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
  34. void addDirectiveHandler(StringRef Directive) {
  35. MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
  36. this, HandleDirective<ELFAsmParser, HandlerMethod>);
  37. getParser().addDirectiveHandler(Directive, Handler);
  38. }
  39. bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
  40. SectionKind Kind);
  41. public:
  42. ELFAsmParser() { BracketExpressionsSupported = true; }
  43. void Initialize(MCAsmParser &Parser) override {
  44. // Call the base implementation.
  45. this->MCAsmParserExtension::Initialize(Parser);
  46. addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
  47. addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
  48. addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
  49. addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
  50. addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
  51. addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
  52. addDirectiveHandler<
  53. &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
  54. addDirectiveHandler<
  55. &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
  56. addDirectiveHandler<
  57. &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
  58. addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
  59. addDirectiveHandler<
  60. &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
  61. addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
  62. addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
  63. addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
  64. addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
  65. addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
  66. addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
  67. addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
  68. addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
  69. addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
  70. addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
  71. addDirectiveHandler<
  72. &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
  73. addDirectiveHandler<
  74. &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
  75. addDirectiveHandler<
  76. &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
  77. addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
  78. addDirectiveHandler<&ELFAsmParser::ParseDirectiveCGProfile>(".cg_profile");
  79. }
  80. // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
  81. // the best way for us to get access to it?
  82. bool ParseSectionDirectiveData(StringRef, SMLoc) {
  83. return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
  84. ELF::SHF_WRITE | ELF::SHF_ALLOC,
  85. SectionKind::getData());
  86. }
  87. bool ParseSectionDirectiveText(StringRef, SMLoc) {
  88. return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
  89. ELF::SHF_EXECINSTR |
  90. ELF::SHF_ALLOC, SectionKind::getText());
  91. }
  92. bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
  93. return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
  94. ELF::SHF_WRITE |
  95. ELF::SHF_ALLOC, SectionKind::getBSS());
  96. }
  97. bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
  98. return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
  99. ELF::SHF_ALLOC,
  100. SectionKind::getReadOnly());
  101. }
  102. bool ParseSectionDirectiveTData(StringRef, SMLoc) {
  103. return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
  104. ELF::SHF_ALLOC |
  105. ELF::SHF_TLS | ELF::SHF_WRITE,
  106. SectionKind::getThreadData());
  107. }
  108. bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
  109. return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
  110. ELF::SHF_ALLOC |
  111. ELF::SHF_TLS | ELF::SHF_WRITE,
  112. SectionKind::getThreadBSS());
  113. }
  114. bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
  115. return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
  116. ELF::SHF_ALLOC | ELF::SHF_WRITE,
  117. SectionKind::getData());
  118. }
  119. bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
  120. return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
  121. ELF::SHF_ALLOC |
  122. ELF::SHF_WRITE,
  123. SectionKind::getReadOnlyWithRel());
  124. }
  125. bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
  126. return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
  127. ELF::SHF_ALLOC | ELF::SHF_WRITE,
  128. SectionKind::getData());
  129. }
  130. bool ParseDirectivePushSection(StringRef, SMLoc);
  131. bool ParseDirectivePopSection(StringRef, SMLoc);
  132. bool ParseDirectiveSection(StringRef, SMLoc);
  133. bool ParseDirectiveSize(StringRef, SMLoc);
  134. bool ParseDirectivePrevious(StringRef, SMLoc);
  135. bool ParseDirectiveType(StringRef, SMLoc);
  136. bool ParseDirectiveIdent(StringRef, SMLoc);
  137. bool ParseDirectiveSymver(StringRef, SMLoc);
  138. bool ParseDirectiveVersion(StringRef, SMLoc);
  139. bool ParseDirectiveWeakref(StringRef, SMLoc);
  140. bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
  141. bool ParseDirectiveSubsection(StringRef, SMLoc);
  142. bool ParseDirectiveCGProfile(StringRef, SMLoc);
  143. private:
  144. bool ParseSectionName(StringRef &SectionName);
  145. bool ParseSectionArguments(bool IsPush, SMLoc loc);
  146. unsigned parseSunStyleSectionFlags();
  147. bool maybeParseSectionType(StringRef &TypeName);
  148. bool parseMergeSize(int64_t &Size);
  149. bool parseGroup(StringRef &GroupName);
  150. bool parseLinkedToSym(MCSymbolELF *&LinkedToSym);
  151. bool maybeParseUniqueID(int64_t &UniqueID);
  152. };
  153. } // end anonymous namespace
  154. /// ParseDirectiveSymbolAttribute
  155. /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
  156. bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
  157. MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
  158. .Case(".weak", MCSA_Weak)
  159. .Case(".local", MCSA_Local)
  160. .Case(".hidden", MCSA_Hidden)
  161. .Case(".internal", MCSA_Internal)
  162. .Case(".protected", MCSA_Protected)
  163. .Default(MCSA_Invalid);
  164. assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
  165. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  166. while (true) {
  167. StringRef Name;
  168. if (getParser().parseIdentifier(Name))
  169. return TokError("expected identifier in directive");
  170. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  171. getStreamer().emitSymbolAttribute(Sym, Attr);
  172. if (getLexer().is(AsmToken::EndOfStatement))
  173. break;
  174. if (getLexer().isNot(AsmToken::Comma))
  175. return TokError("unexpected token in directive");
  176. Lex();
  177. }
  178. }
  179. Lex();
  180. return false;
  181. }
  182. bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
  183. unsigned Flags, SectionKind Kind) {
  184. const MCExpr *Subsection = nullptr;
  185. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  186. if (getParser().parseExpression(Subsection))
  187. return true;
  188. }
  189. Lex();
  190. getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
  191. Subsection);
  192. return false;
  193. }
  194. bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
  195. StringRef Name;
  196. if (getParser().parseIdentifier(Name))
  197. return TokError("expected identifier in directive");
  198. MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
  199. if (getLexer().isNot(AsmToken::Comma))
  200. return TokError("unexpected token in directive");
  201. Lex();
  202. const MCExpr *Expr;
  203. if (getParser().parseExpression(Expr))
  204. return true;
  205. if (getLexer().isNot(AsmToken::EndOfStatement))
  206. return TokError("unexpected token in directive");
  207. Lex();
  208. getStreamer().emitELFSize(Sym, Expr);
  209. return false;
  210. }
  211. bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
  212. // A section name can contain -, so we cannot just use
  213. // parseIdentifier.
  214. SMLoc FirstLoc = getLexer().getLoc();
  215. unsigned Size = 0;
  216. if (getLexer().is(AsmToken::String)) {
  217. SectionName = getTok().getIdentifier();
  218. Lex();
  219. return false;
  220. }
  221. while (!getParser().hasPendingError()) {
  222. SMLoc PrevLoc = getLexer().getLoc();
  223. if (getLexer().is(AsmToken::Comma) ||
  224. getLexer().is(AsmToken::EndOfStatement))
  225. break;
  226. unsigned CurSize;
  227. if (getLexer().is(AsmToken::String)) {
  228. CurSize = getTok().getIdentifier().size() + 2;
  229. Lex();
  230. } else if (getLexer().is(AsmToken::Identifier)) {
  231. CurSize = getTok().getIdentifier().size();
  232. Lex();
  233. } else {
  234. CurSize = getTok().getString().size();
  235. Lex();
  236. }
  237. Size += CurSize;
  238. SectionName = StringRef(FirstLoc.getPointer(), Size);
  239. // Make sure the following token is adjacent.
  240. if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
  241. break;
  242. }
  243. if (Size == 0)
  244. return true;
  245. return false;
  246. }
  247. static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
  248. unsigned flags = 0;
  249. // If a valid numerical value is set for the section flag, use it verbatim
  250. if (!flagsStr.getAsInteger(0, flags))
  251. return flags;
  252. for (char i : flagsStr) {
  253. switch (i) {
  254. case 'a':
  255. flags |= ELF::SHF_ALLOC;
  256. break;
  257. case 'e':
  258. flags |= ELF::SHF_EXCLUDE;
  259. break;
  260. case 'x':
  261. flags |= ELF::SHF_EXECINSTR;
  262. break;
  263. case 'w':
  264. flags |= ELF::SHF_WRITE;
  265. break;
  266. case 'o':
  267. flags |= ELF::SHF_LINK_ORDER;
  268. break;
  269. case 'M':
  270. flags |= ELF::SHF_MERGE;
  271. break;
  272. case 'S':
  273. flags |= ELF::SHF_STRINGS;
  274. break;
  275. case 'T':
  276. flags |= ELF::SHF_TLS;
  277. break;
  278. case 'c':
  279. flags |= ELF::XCORE_SHF_CP_SECTION;
  280. break;
  281. case 'd':
  282. flags |= ELF::XCORE_SHF_DP_SECTION;
  283. break;
  284. case 'y':
  285. flags |= ELF::SHF_ARM_PURECODE;
  286. break;
  287. case 's':
  288. flags |= ELF::SHF_HEX_GPREL;
  289. break;
  290. case 'G':
  291. flags |= ELF::SHF_GROUP;
  292. break;
  293. case '?':
  294. *UseLastGroup = true;
  295. break;
  296. default:
  297. return -1U;
  298. }
  299. }
  300. return flags;
  301. }
  302. unsigned ELFAsmParser::parseSunStyleSectionFlags() {
  303. unsigned flags = 0;
  304. while (getLexer().is(AsmToken::Hash)) {
  305. Lex(); // Eat the #.
  306. if (!getLexer().is(AsmToken::Identifier))
  307. return -1U;
  308. StringRef flagId = getTok().getIdentifier();
  309. if (flagId == "alloc")
  310. flags |= ELF::SHF_ALLOC;
  311. else if (flagId == "execinstr")
  312. flags |= ELF::SHF_EXECINSTR;
  313. else if (flagId == "write")
  314. flags |= ELF::SHF_WRITE;
  315. else if (flagId == "tls")
  316. flags |= ELF::SHF_TLS;
  317. else
  318. return -1U;
  319. Lex(); // Eat the flag.
  320. if (!getLexer().is(AsmToken::Comma))
  321. break;
  322. Lex(); // Eat the comma.
  323. }
  324. return flags;
  325. }
  326. bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
  327. getStreamer().PushSection();
  328. if (ParseSectionArguments(/*IsPush=*/true, loc)) {
  329. getStreamer().PopSection();
  330. return true;
  331. }
  332. return false;
  333. }
  334. bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
  335. if (!getStreamer().PopSection())
  336. return TokError(".popsection without corresponding .pushsection");
  337. return false;
  338. }
  339. bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
  340. return ParseSectionArguments(/*IsPush=*/false, loc);
  341. }
  342. bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
  343. MCAsmLexer &L = getLexer();
  344. if (L.isNot(AsmToken::Comma))
  345. return false;
  346. Lex();
  347. if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
  348. L.isNot(AsmToken::String)) {
  349. if (L.getAllowAtInIdentifier())
  350. return TokError("expected '@<type>', '%<type>' or \"<type>\"");
  351. else
  352. return TokError("expected '%<type>' or \"<type>\"");
  353. }
  354. if (!L.is(AsmToken::String))
  355. Lex();
  356. if (L.is(AsmToken::Integer)) {
  357. TypeName = getTok().getString();
  358. Lex();
  359. } else if (getParser().parseIdentifier(TypeName))
  360. return TokError("expected identifier in directive");
  361. return false;
  362. }
  363. bool ELFAsmParser::parseMergeSize(int64_t &Size) {
  364. if (getLexer().isNot(AsmToken::Comma))
  365. return TokError("expected the entry size");
  366. Lex();
  367. if (getParser().parseAbsoluteExpression(Size))
  368. return true;
  369. if (Size <= 0)
  370. return TokError("entry size must be positive");
  371. return false;
  372. }
  373. bool ELFAsmParser::parseGroup(StringRef &GroupName) {
  374. MCAsmLexer &L = getLexer();
  375. if (L.isNot(AsmToken::Comma))
  376. return TokError("expected group name");
  377. Lex();
  378. if (L.is(AsmToken::Integer)) {
  379. GroupName = getTok().getString();
  380. Lex();
  381. } else if (getParser().parseIdentifier(GroupName)) {
  382. return TokError("invalid group name");
  383. }
  384. if (L.is(AsmToken::Comma)) {
  385. Lex();
  386. StringRef Linkage;
  387. if (getParser().parseIdentifier(Linkage))
  388. return TokError("invalid linkage");
  389. if (Linkage != "comdat")
  390. return TokError("Linkage must be 'comdat'");
  391. }
  392. return false;
  393. }
  394. bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) {
  395. MCAsmLexer &L = getLexer();
  396. if (L.isNot(AsmToken::Comma))
  397. return TokError("expected linked-to symbol");
  398. Lex();
  399. StringRef Name;
  400. SMLoc StartLoc = L.getLoc();
  401. if (getParser().parseIdentifier(Name)) {
  402. if (getParser().getTok().getString() == "0") {
  403. getParser().Lex();
  404. LinkedToSym = nullptr;
  405. return false;
  406. }
  407. return TokError("invalid linked-to symbol");
  408. }
  409. LinkedToSym = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
  410. if (!LinkedToSym || !LinkedToSym->isInSection())
  411. return Error(StartLoc, "linked-to symbol is not in a section: " + Name);
  412. return false;
  413. }
  414. bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
  415. MCAsmLexer &L = getLexer();
  416. if (L.isNot(AsmToken::Comma))
  417. return false;
  418. Lex();
  419. StringRef UniqueStr;
  420. if (getParser().parseIdentifier(UniqueStr))
  421. return TokError("expected identifier in directive");
  422. if (UniqueStr != "unique")
  423. return TokError("expected 'unique'");
  424. if (L.isNot(AsmToken::Comma))
  425. return TokError("expected commma");
  426. Lex();
  427. if (getParser().parseAbsoluteExpression(UniqueID))
  428. return true;
  429. if (UniqueID < 0)
  430. return TokError("unique id must be positive");
  431. if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
  432. return TokError("unique id is too large");
  433. return false;
  434. }
  435. static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
  436. return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back();
  437. }
  438. bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
  439. StringRef SectionName;
  440. if (ParseSectionName(SectionName))
  441. return TokError("expected identifier in directive");
  442. StringRef TypeName;
  443. int64_t Size = 0;
  444. StringRef GroupName;
  445. unsigned Flags = 0;
  446. const MCExpr *Subsection = nullptr;
  447. bool UseLastGroup = false;
  448. MCSymbolELF *LinkedToSym = nullptr;
  449. int64_t UniqueID = ~0;
  450. // Set the defaults first.
  451. if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
  452. Flags |= ELF::SHF_ALLOC;
  453. else if (SectionName == ".fini" || SectionName == ".init" ||
  454. hasPrefix(SectionName, ".text."))
  455. Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
  456. else if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
  457. hasPrefix(SectionName, ".bss.") ||
  458. hasPrefix(SectionName, ".init_array.") ||
  459. hasPrefix(SectionName, ".fini_array.") ||
  460. hasPrefix(SectionName, ".preinit_array."))
  461. Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE;
  462. else if (hasPrefix(SectionName, ".tdata.") ||
  463. hasPrefix(SectionName, ".tbss."))
  464. Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS;
  465. if (getLexer().is(AsmToken::Comma)) {
  466. Lex();
  467. if (IsPush && getLexer().isNot(AsmToken::String)) {
  468. if (getParser().parseExpression(Subsection))
  469. return true;
  470. if (getLexer().isNot(AsmToken::Comma))
  471. goto EndStmt;
  472. Lex();
  473. }
  474. unsigned extraFlags;
  475. if (getLexer().isNot(AsmToken::String)) {
  476. if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
  477. || getLexer().isNot(AsmToken::Hash))
  478. return TokError("expected string in directive");
  479. extraFlags = parseSunStyleSectionFlags();
  480. } else {
  481. StringRef FlagsStr = getTok().getStringContents();
  482. Lex();
  483. extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
  484. }
  485. if (extraFlags == -1U)
  486. return TokError("unknown flag");
  487. Flags |= extraFlags;
  488. bool Mergeable = Flags & ELF::SHF_MERGE;
  489. bool Group = Flags & ELF::SHF_GROUP;
  490. if (Group && UseLastGroup)
  491. return TokError("Section cannot specifiy a group name while also acting "
  492. "as a member of the last group");
  493. if (maybeParseSectionType(TypeName))
  494. return true;
  495. MCAsmLexer &L = getLexer();
  496. if (TypeName.empty()) {
  497. if (Mergeable)
  498. return TokError("Mergeable section must specify the type");
  499. if (Group)
  500. return TokError("Group section must specify the type");
  501. if (L.isNot(AsmToken::EndOfStatement))
  502. return TokError("unexpected token in directive");
  503. }
  504. if (Mergeable)
  505. if (parseMergeSize(Size))
  506. return true;
  507. if (Group)
  508. if (parseGroup(GroupName))
  509. return true;
  510. if (Flags & ELF::SHF_LINK_ORDER)
  511. if (parseLinkedToSym(LinkedToSym))
  512. return true;
  513. if (maybeParseUniqueID(UniqueID))
  514. return true;
  515. }
  516. EndStmt:
  517. if (getLexer().isNot(AsmToken::EndOfStatement))
  518. return TokError("unexpected token in directive");
  519. Lex();
  520. unsigned Type = ELF::SHT_PROGBITS;
  521. if (TypeName.empty()) {
  522. if (SectionName.startswith(".note"))
  523. Type = ELF::SHT_NOTE;
  524. else if (hasPrefix(SectionName, ".init_array."))
  525. Type = ELF::SHT_INIT_ARRAY;
  526. else if (hasPrefix(SectionName, ".bss."))
  527. Type = ELF::SHT_NOBITS;
  528. else if (hasPrefix(SectionName, ".tbss."))
  529. Type = ELF::SHT_NOBITS;
  530. else if (hasPrefix(SectionName, ".fini_array."))
  531. Type = ELF::SHT_FINI_ARRAY;
  532. else if (hasPrefix(SectionName, ".preinit_array."))
  533. Type = ELF::SHT_PREINIT_ARRAY;
  534. } else {
  535. if (TypeName == "init_array")
  536. Type = ELF::SHT_INIT_ARRAY;
  537. else if (TypeName == "fini_array")
  538. Type = ELF::SHT_FINI_ARRAY;
  539. else if (TypeName == "preinit_array")
  540. Type = ELF::SHT_PREINIT_ARRAY;
  541. else if (TypeName == "nobits")
  542. Type = ELF::SHT_NOBITS;
  543. else if (TypeName == "progbits")
  544. Type = ELF::SHT_PROGBITS;
  545. else if (TypeName == "note")
  546. Type = ELF::SHT_NOTE;
  547. else if (TypeName == "unwind")
  548. Type = ELF::SHT_X86_64_UNWIND;
  549. else if (TypeName == "llvm_odrtab")
  550. Type = ELF::SHT_LLVM_ODRTAB;
  551. else if (TypeName == "llvm_linker_options")
  552. Type = ELF::SHT_LLVM_LINKER_OPTIONS;
  553. else if (TypeName == "llvm_call_graph_profile")
  554. Type = ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
  555. else if (TypeName == "llvm_dependent_libraries")
  556. Type = ELF::SHT_LLVM_DEPENDENT_LIBRARIES;
  557. else if (TypeName == "llvm_sympart")
  558. Type = ELF::SHT_LLVM_SYMPART;
  559. else if (TypeName == "llvm_bb_addr_map")
  560. Type = ELF::SHT_LLVM_BB_ADDR_MAP;
  561. else if (TypeName.getAsInteger(0, Type))
  562. return TokError("unknown section type");
  563. }
  564. if (UseLastGroup) {
  565. MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
  566. if (const MCSectionELF *Section =
  567. cast_or_null<MCSectionELF>(CurrentSection.first))
  568. if (const MCSymbol *Group = Section->getGroup()) {
  569. GroupName = Group->getName();
  570. Flags |= ELF::SHF_GROUP;
  571. }
  572. }
  573. MCSectionELF *Section = getContext().getELFSection(
  574. SectionName, Type, Flags, Size, GroupName, UniqueID, LinkedToSym);
  575. getStreamer().SwitchSection(Section, Subsection);
  576. // x86-64 psABI names SHT_X86_64_UNWIND as the canonical type for .eh_frame,
  577. // but GNU as emits SHT_PROGBITS .eh_frame for .cfi_* directives. Don't error
  578. // for SHT_PROGBITS .eh_frame
  579. if (Section->getType() != Type &&
  580. !(SectionName == ".eh_frame" && Type == ELF::SHT_PROGBITS))
  581. Error(loc, "changed section type for " + SectionName + ", expected: 0x" +
  582. utohexstr(Section->getType()));
  583. // Check that flags are used consistently. However, the GNU assembler permits
  584. // to leave out in subsequent uses of the same sections; for compatibility,
  585. // do likewise.
  586. if ((Flags || Size || !TypeName.empty()) && Section->getFlags() != Flags)
  587. Error(loc, "changed section flags for " + SectionName + ", expected: 0x" +
  588. utohexstr(Section->getFlags()));
  589. if ((Flags || Size || !TypeName.empty()) && Section->getEntrySize() != Size)
  590. Error(loc, "changed section entsize for " + SectionName +
  591. ", expected: " + Twine(Section->getEntrySize()));
  592. if (getContext().getGenDwarfForAssembly() &&
  593. (Section->getFlags() & ELF::SHF_ALLOC) &&
  594. (Section->getFlags() & ELF::SHF_EXECINSTR)) {
  595. bool InsertResult = getContext().addGenDwarfSection(Section);
  596. if (InsertResult) {
  597. if (getContext().getDwarfVersion() <= 2)
  598. Warning(loc, "DWARF2 only supports one section per compilation unit");
  599. if (!Section->getBeginSymbol()) {
  600. MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
  601. getStreamer().emitLabel(SectionStartSymbol);
  602. Section->setBeginSymbol(SectionStartSymbol);
  603. }
  604. }
  605. }
  606. return false;
  607. }
  608. bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
  609. MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
  610. if (PreviousSection.first == nullptr)
  611. return TokError(".previous without corresponding .section");
  612. getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
  613. return false;
  614. }
  615. static MCSymbolAttr MCAttrForString(StringRef Type) {
  616. return StringSwitch<MCSymbolAttr>(Type)
  617. .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
  618. .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
  619. .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
  620. .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
  621. .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
  622. .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
  623. MCSA_ELF_TypeIndFunction)
  624. .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
  625. .Default(MCSA_Invalid);
  626. }
  627. /// ParseDirectiveELFType
  628. /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
  629. /// ::= .type identifier , #attribute
  630. /// ::= .type identifier , @attribute
  631. /// ::= .type identifier , %attribute
  632. /// ::= .type identifier , "attribute"
  633. bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
  634. StringRef Name;
  635. if (getParser().parseIdentifier(Name))
  636. return TokError("expected identifier in directive");
  637. // Handle the identifier as the key symbol.
  638. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  639. // NOTE the comma is optional in all cases. It is only documented as being
  640. // optional in the first case, however, GAS will silently treat the comma as
  641. // optional in all cases. Furthermore, although the documentation states that
  642. // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
  643. // accepts both the upper case name as well as the lower case aliases.
  644. if (getLexer().is(AsmToken::Comma))
  645. Lex();
  646. if (getLexer().isNot(AsmToken::Identifier) &&
  647. getLexer().isNot(AsmToken::Hash) &&
  648. getLexer().isNot(AsmToken::Percent) &&
  649. getLexer().isNot(AsmToken::String)) {
  650. if (!getLexer().getAllowAtInIdentifier())
  651. return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
  652. "'%<type>' or \"<type>\"");
  653. else if (getLexer().isNot(AsmToken::At))
  654. return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
  655. "'%<type>' or \"<type>\"");
  656. }
  657. if (getLexer().isNot(AsmToken::String) &&
  658. getLexer().isNot(AsmToken::Identifier))
  659. Lex();
  660. SMLoc TypeLoc = getLexer().getLoc();
  661. StringRef Type;
  662. if (getParser().parseIdentifier(Type))
  663. return TokError("expected symbol type in directive");
  664. MCSymbolAttr Attr = MCAttrForString(Type);
  665. if (Attr == MCSA_Invalid)
  666. return Error(TypeLoc, "unsupported attribute in '.type' directive");
  667. if (getLexer().isNot(AsmToken::EndOfStatement))
  668. return TokError("unexpected token in '.type' directive");
  669. Lex();
  670. getStreamer().emitSymbolAttribute(Sym, Attr);
  671. return false;
  672. }
  673. /// ParseDirectiveIdent
  674. /// ::= .ident string
  675. bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
  676. if (getLexer().isNot(AsmToken::String))
  677. return TokError("unexpected token in '.ident' directive");
  678. StringRef Data = getTok().getIdentifier();
  679. Lex();
  680. if (getLexer().isNot(AsmToken::EndOfStatement))
  681. return TokError("unexpected token in '.ident' directive");
  682. Lex();
  683. getStreamer().emitIdent(Data);
  684. return false;
  685. }
  686. /// ParseDirectiveSymver
  687. /// ::= .symver foo, bar2@zed
  688. bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
  689. StringRef Name;
  690. if (getParser().parseIdentifier(Name))
  691. return TokError("expected identifier in directive");
  692. if (getLexer().isNot(AsmToken::Comma))
  693. return TokError("expected a comma");
  694. // ARM assembly uses @ for a comment...
  695. // except when parsing the second parameter of the .symver directive.
  696. // Force the next symbol to allow @ in the identifier, which is
  697. // required for this directive and then reset it to its initial state.
  698. const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
  699. getLexer().setAllowAtInIdentifier(true);
  700. Lex();
  701. getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
  702. StringRef AliasName;
  703. if (getParser().parseIdentifier(AliasName))
  704. return TokError("expected identifier in directive");
  705. if (AliasName.find('@') == StringRef::npos)
  706. return TokError("expected a '@' in the name");
  707. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  708. getStreamer().emitELFSymverDirective(AliasName, Sym);
  709. return false;
  710. }
  711. /// ParseDirectiveVersion
  712. /// ::= .version string
  713. bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
  714. if (getLexer().isNot(AsmToken::String))
  715. return TokError("unexpected token in '.version' directive");
  716. StringRef Data = getTok().getIdentifier();
  717. Lex();
  718. MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
  719. getStreamer().PushSection();
  720. getStreamer().SwitchSection(Note);
  721. getStreamer().emitInt32(Data.size() + 1); // namesz
  722. getStreamer().emitInt32(0); // descsz = 0 (no description).
  723. getStreamer().emitInt32(1); // type = NT_VERSION
  724. getStreamer().emitBytes(Data); // name
  725. getStreamer().emitInt8(0); // NUL
  726. getStreamer().emitValueToAlignment(4);
  727. getStreamer().PopSection();
  728. return false;
  729. }
  730. /// ParseDirectiveWeakref
  731. /// ::= .weakref foo, bar
  732. bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
  733. // FIXME: Share code with the other alias building directives.
  734. StringRef AliasName;
  735. if (getParser().parseIdentifier(AliasName))
  736. return TokError("expected identifier in directive");
  737. if (getLexer().isNot(AsmToken::Comma))
  738. return TokError("expected a comma");
  739. Lex();
  740. StringRef Name;
  741. if (getParser().parseIdentifier(Name))
  742. return TokError("expected identifier in directive");
  743. MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
  744. MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
  745. getStreamer().emitWeakReference(Alias, Sym);
  746. return false;
  747. }
  748. bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
  749. const MCExpr *Subsection = nullptr;
  750. if (getLexer().isNot(AsmToken::EndOfStatement)) {
  751. if (getParser().parseExpression(Subsection))
  752. return true;
  753. }
  754. if (getLexer().isNot(AsmToken::EndOfStatement))
  755. return TokError("unexpected token in directive");
  756. Lex();
  757. getStreamer().SubSection(Subsection);
  758. return false;
  759. }
  760. bool ELFAsmParser::ParseDirectiveCGProfile(StringRef S, SMLoc Loc) {
  761. return MCAsmParserExtension::ParseDirectiveCGProfile(S, Loc);
  762. }
  763. namespace llvm {
  764. MCAsmParserExtension *createELFAsmParser() {
  765. return new ELFAsmParser;
  766. }
  767. } // end namespace llvm