ELFAsmParser.cpp 30 KB

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