1 //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCDirectives.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCParser/MCAsmLexer.h"
18 #include "llvm/MC/MCParser/MCAsmParser.h"
19 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
20 #include "llvm/MC/MCSection.h"
21 #include "llvm/MC/MCSectionELF.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/MC/MCSymbolELF.h"
25 #include "llvm/MC/SectionKind.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/SMLoc.h"
37 class ELFAsmParser : public MCAsmParserExtension {
38 template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
39 void addDirectiveHandler(StringRef Directive) {
40 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
41 this, HandleDirective<ELFAsmParser, HandlerMethod>);
43 getParser().addDirectiveHandler(Directive, Handler);
46 bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
50 ELFAsmParser() { BracketExpressionsSupported = true; }
52 void Initialize(MCAsmParser &Parser) override {
53 // Call the base implementation.
54 this->MCAsmParserExtension::Initialize(Parser);
56 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
57 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
58 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
59 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
60 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
61 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
63 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
65 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
67 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
68 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
70 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
71 addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
72 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
73 addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
74 addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
75 addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
76 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
77 addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
78 addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
79 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
80 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
82 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
84 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
86 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
87 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
90 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
91 // the best way for us to get access to it?
92 bool ParseSectionDirectiveData(StringRef, SMLoc) {
93 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
94 ELF::SHF_WRITE | ELF::SHF_ALLOC,
95 SectionKind::getData());
97 bool ParseSectionDirectiveText(StringRef, SMLoc) {
98 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
100 ELF::SHF_ALLOC, SectionKind::getText());
102 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
103 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
105 ELF::SHF_ALLOC, SectionKind::getBSS());
107 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
108 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
110 SectionKind::getReadOnly());
112 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
113 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
115 ELF::SHF_TLS | ELF::SHF_WRITE,
116 SectionKind::getThreadData());
118 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
119 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
121 ELF::SHF_TLS | ELF::SHF_WRITE,
122 SectionKind::getThreadBSS());
124 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
125 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
126 ELF::SHF_ALLOC | ELF::SHF_WRITE,
127 SectionKind::getData());
129 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
130 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
133 SectionKind::getReadOnlyWithRel());
135 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
136 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
137 ELF::SHF_ALLOC | ELF::SHF_WRITE,
138 SectionKind::getData());
140 bool ParseDirectivePushSection(StringRef, SMLoc);
141 bool ParseDirectivePopSection(StringRef, SMLoc);
142 bool ParseDirectiveSection(StringRef, SMLoc);
143 bool ParseDirectiveSize(StringRef, SMLoc);
144 bool ParseDirectivePrevious(StringRef, SMLoc);
145 bool ParseDirectiveType(StringRef, SMLoc);
146 bool ParseDirectiveIdent(StringRef, SMLoc);
147 bool ParseDirectiveSymver(StringRef, SMLoc);
148 bool ParseDirectiveVersion(StringRef, SMLoc);
149 bool ParseDirectiveWeakref(StringRef, SMLoc);
150 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
151 bool ParseDirectiveSubsection(StringRef, SMLoc);
154 bool ParseSectionName(StringRef &SectionName);
155 bool ParseSectionArguments(bool IsPush, SMLoc loc);
156 unsigned parseSunStyleSectionFlags();
157 bool maybeParseSectionType(StringRef &TypeName);
158 bool parseMergeSize(int64_t &Size);
159 bool parseGroup(StringRef &GroupName);
160 bool parseMetadataSym(MCSymbolELF *&Associated);
161 bool maybeParseUniqueID(int64_t &UniqueID);
164 } // end anonymous namespace
166 /// ParseDirectiveSymbolAttribute
167 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
168 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
169 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
170 .Case(".weak", MCSA_Weak)
171 .Case(".local", MCSA_Local)
172 .Case(".hidden", MCSA_Hidden)
173 .Case(".internal", MCSA_Internal)
174 .Case(".protected", MCSA_Protected)
175 .Default(MCSA_Invalid);
176 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
177 if (getLexer().isNot(AsmToken::EndOfStatement)) {
181 if (getParser().parseIdentifier(Name))
182 return TokError("expected identifier in directive");
184 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
186 getStreamer().EmitSymbolAttribute(Sym, Attr);
188 if (getLexer().is(AsmToken::EndOfStatement))
191 if (getLexer().isNot(AsmToken::Comma))
192 return TokError("unexpected token in directive");
201 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
202 unsigned Flags, SectionKind Kind) {
203 const MCExpr *Subsection = nullptr;
204 if (getLexer().isNot(AsmToken::EndOfStatement)) {
205 if (getParser().parseExpression(Subsection))
210 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
216 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
218 if (getParser().parseIdentifier(Name))
219 return TokError("expected identifier in directive");
220 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
222 if (getLexer().isNot(AsmToken::Comma))
223 return TokError("unexpected token in directive");
227 if (getParser().parseExpression(Expr))
230 if (getLexer().isNot(AsmToken::EndOfStatement))
231 return TokError("unexpected token in directive");
234 getStreamer().emitELFSize(Sym, Expr);
238 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
239 // A section name can contain -, so we cannot just use
241 SMLoc FirstLoc = getLexer().getLoc();
244 if (getLexer().is(AsmToken::String)) {
245 SectionName = getTok().getIdentifier();
250 while (!getParser().hasPendingError()) {
251 SMLoc PrevLoc = getLexer().getLoc();
252 if (getLexer().is(AsmToken::Comma) ||
253 getLexer().is(AsmToken::EndOfStatement))
257 if (getLexer().is(AsmToken::String)) {
258 CurSize = getTok().getIdentifier().size() + 2;
260 } else if (getLexer().is(AsmToken::Identifier)) {
261 CurSize = getTok().getIdentifier().size();
264 CurSize = getTok().getString().size();
268 SectionName = StringRef(FirstLoc.getPointer(), Size);
270 // Make sure the following token is adjacent.
271 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
280 static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
283 // If a valid numerical value is set for the section flag, use it verbatim
284 if (!flagsStr.getAsInteger(0, flags))
287 for (char i : flagsStr) {
290 flags |= ELF::SHF_ALLOC;
293 flags |= ELF::SHF_EXCLUDE;
296 flags |= ELF::SHF_EXECINSTR;
299 flags |= ELF::SHF_WRITE;
302 flags |= ELF::SHF_LINK_ORDER;
305 flags |= ELF::SHF_MERGE;
308 flags |= ELF::SHF_STRINGS;
311 flags |= ELF::SHF_TLS;
314 flags |= ELF::XCORE_SHF_CP_SECTION;
317 flags |= ELF::XCORE_SHF_DP_SECTION;
320 flags |= ELF::SHF_ARM_PURECODE;
323 flags |= ELF::SHF_GROUP;
326 *UseLastGroup = true;
336 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
338 while (getLexer().is(AsmToken::Hash)) {
341 if (!getLexer().is(AsmToken::Identifier))
344 StringRef flagId = getTok().getIdentifier();
345 if (flagId == "alloc")
346 flags |= ELF::SHF_ALLOC;
347 else if (flagId == "execinstr")
348 flags |= ELF::SHF_EXECINSTR;
349 else if (flagId == "write")
350 flags |= ELF::SHF_WRITE;
351 else if (flagId == "tls")
352 flags |= ELF::SHF_TLS;
356 Lex(); // Eat the flag.
358 if (!getLexer().is(AsmToken::Comma))
360 Lex(); // Eat the comma.
366 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
367 getStreamer().PushSection();
369 if (ParseSectionArguments(/*IsPush=*/true, loc)) {
370 getStreamer().PopSection();
377 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
378 if (!getStreamer().PopSection())
379 return TokError(".popsection without corresponding .pushsection");
383 // FIXME: This is a work in progress.
384 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
385 return ParseSectionArguments(/*IsPush=*/false, loc);
388 bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
389 MCAsmLexer &L = getLexer();
390 if (L.isNot(AsmToken::Comma))
393 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
394 L.isNot(AsmToken::String)) {
395 if (L.getAllowAtInIdentifier())
396 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
398 return TokError("expected '%<type>' or \"<type>\"");
400 if (!L.is(AsmToken::String))
402 if (L.is(AsmToken::Integer)) {
403 TypeName = getTok().getString();
405 } else if (getParser().parseIdentifier(TypeName))
406 return TokError("expected identifier in directive");
410 bool ELFAsmParser::parseMergeSize(int64_t &Size) {
411 if (getLexer().isNot(AsmToken::Comma))
412 return TokError("expected the entry size");
414 if (getParser().parseAbsoluteExpression(Size))
417 return TokError("entry size must be positive");
421 bool ELFAsmParser::parseGroup(StringRef &GroupName) {
422 MCAsmLexer &L = getLexer();
423 if (L.isNot(AsmToken::Comma))
424 return TokError("expected group name");
426 if (L.is(AsmToken::Integer)) {
427 GroupName = getTok().getString();
429 } else if (getParser().parseIdentifier(GroupName)) {
430 return TokError("invalid group name");
432 if (L.is(AsmToken::Comma)) {
435 if (getParser().parseIdentifier(Linkage))
436 return TokError("invalid linkage");
437 if (Linkage != "comdat")
438 return TokError("Linkage must be 'comdat'");
443 bool ELFAsmParser::parseMetadataSym(MCSymbolELF *&Associated) {
444 MCAsmLexer &L = getLexer();
445 if (L.isNot(AsmToken::Comma))
446 return TokError("expected metadata symbol");
449 if (getParser().parseIdentifier(Name))
450 return TokError("invalid metadata symbol");
451 Associated = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
452 if (!Associated || !Associated->isInSection())
453 return TokError("symbol is not in a section: " + Name);
457 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
458 MCAsmLexer &L = getLexer();
459 if (L.isNot(AsmToken::Comma))
463 if (getParser().parseIdentifier(UniqueStr))
464 return TokError("expected identifier in directive");
465 if (UniqueStr != "unique")
466 return TokError("expected 'unique'");
467 if (L.isNot(AsmToken::Comma))
468 return TokError("expected commma");
470 if (getParser().parseAbsoluteExpression(UniqueID))
473 return TokError("unique id must be positive");
474 if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
475 return TokError("unique id is too large");
479 static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
480 return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back();
483 bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
484 StringRef SectionName;
486 if (ParseSectionName(SectionName))
487 return TokError("expected identifier in directive");
493 const MCExpr *Subsection = nullptr;
494 bool UseLastGroup = false;
495 MCSymbolELF *Associated = nullptr;
496 int64_t UniqueID = ~0;
498 // Set the defaults first.
499 if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
500 Flags |= ELF::SHF_ALLOC;
501 if (SectionName == ".fini" || SectionName == ".init" ||
502 hasPrefix(SectionName, ".text."))
503 Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
504 if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
505 hasPrefix(SectionName, ".bss.") ||
506 hasPrefix(SectionName, ".init_array.") ||
507 hasPrefix(SectionName, ".fini_array.") ||
508 hasPrefix(SectionName, ".preinit_array."))
509 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE;
510 if (hasPrefix(SectionName, ".tdata.") ||
511 hasPrefix(SectionName, ".tbss."))
512 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS;
514 if (getLexer().is(AsmToken::Comma)) {
517 if (IsPush && getLexer().isNot(AsmToken::String)) {
518 if (getParser().parseExpression(Subsection))
520 if (getLexer().isNot(AsmToken::Comma))
527 if (getLexer().isNot(AsmToken::String)) {
528 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
529 || getLexer().isNot(AsmToken::Hash))
530 return TokError("expected string in directive");
531 extraFlags = parseSunStyleSectionFlags();
533 StringRef FlagsStr = getTok().getStringContents();
535 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
538 if (extraFlags == -1U)
539 return TokError("unknown flag");
542 bool Mergeable = Flags & ELF::SHF_MERGE;
543 bool Group = Flags & ELF::SHF_GROUP;
544 if (Group && UseLastGroup)
545 return TokError("Section cannot specifiy a group name while also acting "
546 "as a member of the last group");
548 if (maybeParseSectionType(TypeName))
551 MCAsmLexer &L = getLexer();
552 if (TypeName.empty()) {
554 return TokError("Mergeable section must specify the type");
556 return TokError("Group section must specify the type");
557 if (L.isNot(AsmToken::EndOfStatement))
558 return TokError("unexpected token in directive");
562 if (parseMergeSize(Size))
565 if (parseGroup(GroupName))
567 if (Flags & ELF::SHF_LINK_ORDER)
568 if (parseMetadataSym(Associated))
570 if (maybeParseUniqueID(UniqueID))
575 if (getLexer().isNot(AsmToken::EndOfStatement))
576 return TokError("unexpected token in directive");
579 unsigned Type = ELF::SHT_PROGBITS;
581 if (TypeName.empty()) {
582 if (SectionName.startswith(".note"))
583 Type = ELF::SHT_NOTE;
584 else if (hasPrefix(SectionName, ".init_array."))
585 Type = ELF::SHT_INIT_ARRAY;
586 else if (hasPrefix(SectionName, ".bss."))
587 Type = ELF::SHT_NOBITS;
588 else if (hasPrefix(SectionName, ".tbss."))
589 Type = ELF::SHT_NOBITS;
590 else if (hasPrefix(SectionName, ".fini_array."))
591 Type = ELF::SHT_FINI_ARRAY;
592 else if (hasPrefix(SectionName, ".preinit_array."))
593 Type = ELF::SHT_PREINIT_ARRAY;
595 if (TypeName == "init_array")
596 Type = ELF::SHT_INIT_ARRAY;
597 else if (TypeName == "fini_array")
598 Type = ELF::SHT_FINI_ARRAY;
599 else if (TypeName == "preinit_array")
600 Type = ELF::SHT_PREINIT_ARRAY;
601 else if (TypeName == "nobits")
602 Type = ELF::SHT_NOBITS;
603 else if (TypeName == "progbits")
604 Type = ELF::SHT_PROGBITS;
605 else if (TypeName == "note")
606 Type = ELF::SHT_NOTE;
607 else if (TypeName == "unwind")
608 Type = ELF::SHT_X86_64_UNWIND;
609 else if (TypeName == "llvm_odrtab")
610 Type = ELF::SHT_LLVM_ODRTAB;
611 else if (TypeName.getAsInteger(0, Type))
612 return TokError("unknown section type");
616 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
617 if (const MCSectionELF *Section =
618 cast_or_null<MCSectionELF>(CurrentSection.first))
619 if (const MCSymbol *Group = Section->getGroup()) {
620 GroupName = Group->getName();
621 Flags |= ELF::SHF_GROUP;
625 MCSection *ELFSection =
626 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
627 UniqueID, Associated);
628 getStreamer().SwitchSection(ELFSection, Subsection);
630 if (getContext().getGenDwarfForAssembly()) {
631 bool InsertResult = getContext().addGenDwarfSection(ELFSection);
633 if (getContext().getDwarfVersion() <= 2)
634 Warning(loc, "DWARF2 only supports one section per compilation unit");
636 if (!ELFSection->getBeginSymbol()) {
637 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
638 getStreamer().EmitLabel(SectionStartSymbol);
639 ELFSection->setBeginSymbol(SectionStartSymbol);
647 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
648 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
649 if (PreviousSection.first == nullptr)
650 return TokError(".previous without corresponding .section");
651 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
656 static MCSymbolAttr MCAttrForString(StringRef Type) {
657 return StringSwitch<MCSymbolAttr>(Type)
658 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
659 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
660 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
661 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
662 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
663 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
664 MCSA_ELF_TypeIndFunction)
665 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
666 .Default(MCSA_Invalid);
669 /// ParseDirectiveELFType
670 /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
671 /// ::= .type identifier , #attribute
672 /// ::= .type identifier , @attribute
673 /// ::= .type identifier , %attribute
674 /// ::= .type identifier , "attribute"
675 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
677 if (getParser().parseIdentifier(Name))
678 return TokError("expected identifier in directive");
680 // Handle the identifier as the key symbol.
681 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
683 // NOTE the comma is optional in all cases. It is only documented as being
684 // optional in the first case, however, GAS will silently treat the comma as
685 // optional in all cases. Furthermore, although the documentation states that
686 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
687 // accepts both the upper case name as well as the lower case aliases.
688 if (getLexer().is(AsmToken::Comma))
691 if (getLexer().isNot(AsmToken::Identifier) &&
692 getLexer().isNot(AsmToken::Hash) &&
693 getLexer().isNot(AsmToken::Percent) &&
694 getLexer().isNot(AsmToken::String)) {
695 if (!getLexer().getAllowAtInIdentifier())
696 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
697 "'%<type>' or \"<type>\"");
698 else if (getLexer().isNot(AsmToken::At))
699 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
700 "'%<type>' or \"<type>\"");
703 if (getLexer().isNot(AsmToken::String) &&
704 getLexer().isNot(AsmToken::Identifier))
707 SMLoc TypeLoc = getLexer().getLoc();
710 if (getParser().parseIdentifier(Type))
711 return TokError("expected symbol type in directive");
713 MCSymbolAttr Attr = MCAttrForString(Type);
714 if (Attr == MCSA_Invalid)
715 return Error(TypeLoc, "unsupported attribute in '.type' directive");
717 if (getLexer().isNot(AsmToken::EndOfStatement))
718 return TokError("unexpected token in '.type' directive");
721 getStreamer().EmitSymbolAttribute(Sym, Attr);
726 /// ParseDirectiveIdent
727 /// ::= .ident string
728 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
729 if (getLexer().isNot(AsmToken::String))
730 return TokError("unexpected token in '.ident' directive");
732 StringRef Data = getTok().getIdentifier();
736 if (getLexer().isNot(AsmToken::EndOfStatement))
737 return TokError("unexpected token in '.ident' directive");
740 getStreamer().EmitIdent(Data);
744 /// ParseDirectiveSymver
745 /// ::= .symver foo, bar2@zed
746 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
748 if (getParser().parseIdentifier(Name))
749 return TokError("expected identifier in directive");
751 if (getLexer().isNot(AsmToken::Comma))
752 return TokError("expected a comma");
754 // ARM assembly uses @ for a comment...
755 // except when parsing the second parameter of the .symver directive.
756 // Force the next symbol to allow @ in the identifier, which is
757 // required for this directive and then reset it to its initial state.
758 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
759 getLexer().setAllowAtInIdentifier(true);
761 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
764 if (getParser().parseIdentifier(AliasName))
765 return TokError("expected identifier in directive");
767 if (AliasName.find('@') == StringRef::npos)
768 return TokError("expected a '@' in the name");
770 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
771 getStreamer().emitELFSymverDirective(AliasName, Sym);
775 /// ParseDirectiveVersion
776 /// ::= .version string
777 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
778 if (getLexer().isNot(AsmToken::String))
779 return TokError("unexpected token in '.version' directive");
781 StringRef Data = getTok().getIdentifier();
785 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
787 getStreamer().PushSection();
788 getStreamer().SwitchSection(Note);
789 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
790 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
791 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
792 getStreamer().EmitBytes(Data); // name.
793 getStreamer().EmitIntValue(0, 1); // terminate the string.
794 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
795 getStreamer().PopSection();
799 /// ParseDirectiveWeakref
800 /// ::= .weakref foo, bar
801 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
802 // FIXME: Share code with the other alias building directives.
805 if (getParser().parseIdentifier(AliasName))
806 return TokError("expected identifier in directive");
808 if (getLexer().isNot(AsmToken::Comma))
809 return TokError("expected a comma");
814 if (getParser().parseIdentifier(Name))
815 return TokError("expected identifier in directive");
817 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
819 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
821 getStreamer().EmitWeakReference(Alias, Sym);
825 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
826 const MCExpr *Subsection = nullptr;
827 if (getLexer().isNot(AsmToken::EndOfStatement)) {
828 if (getParser().parseExpression(Subsection))
832 if (getLexer().isNot(AsmToken::EndOfStatement))
833 return TokError("unexpected token in directive");
837 getStreamer().SubSection(Subsection);
843 MCAsmParserExtension *createELFAsmParser() {
844 return new ELFAsmParser;
847 } // end namespace llvm