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");
88 addDirectiveHandler<&ELFAsmParser::ParseDirectiveCGProfile>(".cg_profile");
91 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
92 // the best way for us to get access to it?
93 bool ParseSectionDirectiveData(StringRef, SMLoc) {
94 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
95 ELF::SHF_WRITE | ELF::SHF_ALLOC,
96 SectionKind::getData());
98 bool ParseSectionDirectiveText(StringRef, SMLoc) {
99 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
101 ELF::SHF_ALLOC, SectionKind::getText());
103 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
104 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
106 ELF::SHF_ALLOC, SectionKind::getBSS());
108 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
109 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
111 SectionKind::getReadOnly());
113 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
114 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
116 ELF::SHF_TLS | ELF::SHF_WRITE,
117 SectionKind::getThreadData());
119 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
120 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
122 ELF::SHF_TLS | ELF::SHF_WRITE,
123 SectionKind::getThreadBSS());
125 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
126 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
127 ELF::SHF_ALLOC | ELF::SHF_WRITE,
128 SectionKind::getData());
130 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
131 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
134 SectionKind::getReadOnlyWithRel());
136 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
137 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
138 ELF::SHF_ALLOC | ELF::SHF_WRITE,
139 SectionKind::getData());
141 bool ParseDirectivePushSection(StringRef, SMLoc);
142 bool ParseDirectivePopSection(StringRef, SMLoc);
143 bool ParseDirectiveSection(StringRef, SMLoc);
144 bool ParseDirectiveSize(StringRef, SMLoc);
145 bool ParseDirectivePrevious(StringRef, SMLoc);
146 bool ParseDirectiveType(StringRef, SMLoc);
147 bool ParseDirectiveIdent(StringRef, SMLoc);
148 bool ParseDirectiveSymver(StringRef, SMLoc);
149 bool ParseDirectiveVersion(StringRef, SMLoc);
150 bool ParseDirectiveWeakref(StringRef, SMLoc);
151 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
152 bool ParseDirectiveSubsection(StringRef, SMLoc);
153 bool ParseDirectiveCGProfile(StringRef, SMLoc);
156 bool ParseSectionName(StringRef &SectionName);
157 bool ParseSectionArguments(bool IsPush, SMLoc loc);
158 unsigned parseSunStyleSectionFlags();
159 bool maybeParseSectionType(StringRef &TypeName);
160 bool parseMergeSize(int64_t &Size);
161 bool parseGroup(StringRef &GroupName);
162 bool parseMetadataSym(MCSymbolELF *&Associated);
163 bool maybeParseUniqueID(int64_t &UniqueID);
166 } // end anonymous namespace
168 /// ParseDirectiveSymbolAttribute
169 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
170 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
171 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
172 .Case(".weak", MCSA_Weak)
173 .Case(".local", MCSA_Local)
174 .Case(".hidden", MCSA_Hidden)
175 .Case(".internal", MCSA_Internal)
176 .Case(".protected", MCSA_Protected)
177 .Default(MCSA_Invalid);
178 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
179 if (getLexer().isNot(AsmToken::EndOfStatement)) {
183 if (getParser().parseIdentifier(Name))
184 return TokError("expected identifier in directive");
186 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
188 getStreamer().EmitSymbolAttribute(Sym, Attr);
190 if (getLexer().is(AsmToken::EndOfStatement))
193 if (getLexer().isNot(AsmToken::Comma))
194 return TokError("unexpected token in directive");
203 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
204 unsigned Flags, SectionKind Kind) {
205 const MCExpr *Subsection = nullptr;
206 if (getLexer().isNot(AsmToken::EndOfStatement)) {
207 if (getParser().parseExpression(Subsection))
212 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
218 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
220 if (getParser().parseIdentifier(Name))
221 return TokError("expected identifier in directive");
222 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
224 if (getLexer().isNot(AsmToken::Comma))
225 return TokError("unexpected token in directive");
229 if (getParser().parseExpression(Expr))
232 if (getLexer().isNot(AsmToken::EndOfStatement))
233 return TokError("unexpected token in directive");
236 getStreamer().emitELFSize(Sym, Expr);
240 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
241 // A section name can contain -, so we cannot just use
243 SMLoc FirstLoc = getLexer().getLoc();
246 if (getLexer().is(AsmToken::String)) {
247 SectionName = getTok().getIdentifier();
252 while (!getParser().hasPendingError()) {
253 SMLoc PrevLoc = getLexer().getLoc();
254 if (getLexer().is(AsmToken::Comma) ||
255 getLexer().is(AsmToken::EndOfStatement))
259 if (getLexer().is(AsmToken::String)) {
260 CurSize = getTok().getIdentifier().size() + 2;
262 } else if (getLexer().is(AsmToken::Identifier)) {
263 CurSize = getTok().getIdentifier().size();
266 CurSize = getTok().getString().size();
270 SectionName = StringRef(FirstLoc.getPointer(), Size);
272 // Make sure the following token is adjacent.
273 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
282 static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
285 // If a valid numerical value is set for the section flag, use it verbatim
286 if (!flagsStr.getAsInteger(0, flags))
289 for (char i : flagsStr) {
292 flags |= ELF::SHF_ALLOC;
295 flags |= ELF::SHF_EXCLUDE;
298 flags |= ELF::SHF_EXECINSTR;
301 flags |= ELF::SHF_WRITE;
304 flags |= ELF::SHF_LINK_ORDER;
307 flags |= ELF::SHF_MERGE;
310 flags |= ELF::SHF_STRINGS;
313 flags |= ELF::SHF_TLS;
316 flags |= ELF::XCORE_SHF_CP_SECTION;
319 flags |= ELF::XCORE_SHF_DP_SECTION;
322 flags |= ELF::SHF_ARM_PURECODE;
325 flags |= ELF::SHF_HEX_GPREL;
328 flags |= ELF::SHF_GROUP;
331 *UseLastGroup = true;
341 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
343 while (getLexer().is(AsmToken::Hash)) {
346 if (!getLexer().is(AsmToken::Identifier))
349 StringRef flagId = getTok().getIdentifier();
350 if (flagId == "alloc")
351 flags |= ELF::SHF_ALLOC;
352 else if (flagId == "execinstr")
353 flags |= ELF::SHF_EXECINSTR;
354 else if (flagId == "write")
355 flags |= ELF::SHF_WRITE;
356 else if (flagId == "tls")
357 flags |= ELF::SHF_TLS;
361 Lex(); // Eat the flag.
363 if (!getLexer().is(AsmToken::Comma))
365 Lex(); // Eat the comma.
371 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
372 getStreamer().PushSection();
374 if (ParseSectionArguments(/*IsPush=*/true, loc)) {
375 getStreamer().PopSection();
382 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
383 if (!getStreamer().PopSection())
384 return TokError(".popsection without corresponding .pushsection");
388 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
389 return ParseSectionArguments(/*IsPush=*/false, loc);
392 bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
393 MCAsmLexer &L = getLexer();
394 if (L.isNot(AsmToken::Comma))
397 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
398 L.isNot(AsmToken::String)) {
399 if (L.getAllowAtInIdentifier())
400 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
402 return TokError("expected '%<type>' or \"<type>\"");
404 if (!L.is(AsmToken::String))
406 if (L.is(AsmToken::Integer)) {
407 TypeName = getTok().getString();
409 } else if (getParser().parseIdentifier(TypeName))
410 return TokError("expected identifier in directive");
414 bool ELFAsmParser::parseMergeSize(int64_t &Size) {
415 if (getLexer().isNot(AsmToken::Comma))
416 return TokError("expected the entry size");
418 if (getParser().parseAbsoluteExpression(Size))
421 return TokError("entry size must be positive");
425 bool ELFAsmParser::parseGroup(StringRef &GroupName) {
426 MCAsmLexer &L = getLexer();
427 if (L.isNot(AsmToken::Comma))
428 return TokError("expected group name");
430 if (L.is(AsmToken::Integer)) {
431 GroupName = getTok().getString();
433 } else if (getParser().parseIdentifier(GroupName)) {
434 return TokError("invalid group name");
436 if (L.is(AsmToken::Comma)) {
439 if (getParser().parseIdentifier(Linkage))
440 return TokError("invalid linkage");
441 if (Linkage != "comdat")
442 return TokError("Linkage must be 'comdat'");
447 bool ELFAsmParser::parseMetadataSym(MCSymbolELF *&Associated) {
448 MCAsmLexer &L = getLexer();
449 if (L.isNot(AsmToken::Comma))
450 return TokError("expected metadata symbol");
453 if (getParser().parseIdentifier(Name))
454 return TokError("invalid metadata symbol");
455 Associated = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
456 if (!Associated || !Associated->isInSection())
457 return TokError("symbol is not in a section: " + Name);
461 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
462 MCAsmLexer &L = getLexer();
463 if (L.isNot(AsmToken::Comma))
467 if (getParser().parseIdentifier(UniqueStr))
468 return TokError("expected identifier in directive");
469 if (UniqueStr != "unique")
470 return TokError("expected 'unique'");
471 if (L.isNot(AsmToken::Comma))
472 return TokError("expected commma");
474 if (getParser().parseAbsoluteExpression(UniqueID))
477 return TokError("unique id must be positive");
478 if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
479 return TokError("unique id is too large");
483 static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
484 return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back();
487 bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
488 StringRef SectionName;
490 if (ParseSectionName(SectionName))
491 return TokError("expected identifier in directive");
497 const MCExpr *Subsection = nullptr;
498 bool UseLastGroup = false;
499 MCSymbolELF *Associated = nullptr;
500 int64_t UniqueID = ~0;
502 // Set the defaults first.
503 if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
504 Flags |= ELF::SHF_ALLOC;
505 else if (SectionName == ".fini" || SectionName == ".init" ||
506 hasPrefix(SectionName, ".text."))
507 Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
508 else if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
509 hasPrefix(SectionName, ".bss.") ||
510 hasPrefix(SectionName, ".init_array.") ||
511 hasPrefix(SectionName, ".fini_array.") ||
512 hasPrefix(SectionName, ".preinit_array."))
513 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE;
514 else if (hasPrefix(SectionName, ".tdata.") ||
515 hasPrefix(SectionName, ".tbss."))
516 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS;
518 if (getLexer().is(AsmToken::Comma)) {
521 if (IsPush && getLexer().isNot(AsmToken::String)) {
522 if (getParser().parseExpression(Subsection))
524 if (getLexer().isNot(AsmToken::Comma))
531 if (getLexer().isNot(AsmToken::String)) {
532 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
533 || getLexer().isNot(AsmToken::Hash))
534 return TokError("expected string in directive");
535 extraFlags = parseSunStyleSectionFlags();
537 StringRef FlagsStr = getTok().getStringContents();
539 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
542 if (extraFlags == -1U)
543 return TokError("unknown flag");
546 bool Mergeable = Flags & ELF::SHF_MERGE;
547 bool Group = Flags & ELF::SHF_GROUP;
548 if (Group && UseLastGroup)
549 return TokError("Section cannot specifiy a group name while also acting "
550 "as a member of the last group");
552 if (maybeParseSectionType(TypeName))
555 MCAsmLexer &L = getLexer();
556 if (TypeName.empty()) {
558 return TokError("Mergeable section must specify the type");
560 return TokError("Group section must specify the type");
561 if (L.isNot(AsmToken::EndOfStatement))
562 return TokError("unexpected token in directive");
566 if (parseMergeSize(Size))
569 if (parseGroup(GroupName))
571 if (Flags & ELF::SHF_LINK_ORDER)
572 if (parseMetadataSym(Associated))
574 if (maybeParseUniqueID(UniqueID))
579 if (getLexer().isNot(AsmToken::EndOfStatement))
580 return TokError("unexpected token in directive");
583 unsigned Type = ELF::SHT_PROGBITS;
585 if (TypeName.empty()) {
586 if (SectionName.startswith(".note"))
587 Type = ELF::SHT_NOTE;
588 else if (hasPrefix(SectionName, ".init_array."))
589 Type = ELF::SHT_INIT_ARRAY;
590 else if (hasPrefix(SectionName, ".bss."))
591 Type = ELF::SHT_NOBITS;
592 else if (hasPrefix(SectionName, ".tbss."))
593 Type = ELF::SHT_NOBITS;
594 else if (hasPrefix(SectionName, ".fini_array."))
595 Type = ELF::SHT_FINI_ARRAY;
596 else if (hasPrefix(SectionName, ".preinit_array."))
597 Type = ELF::SHT_PREINIT_ARRAY;
599 if (TypeName == "init_array")
600 Type = ELF::SHT_INIT_ARRAY;
601 else if (TypeName == "fini_array")
602 Type = ELF::SHT_FINI_ARRAY;
603 else if (TypeName == "preinit_array")
604 Type = ELF::SHT_PREINIT_ARRAY;
605 else if (TypeName == "nobits")
606 Type = ELF::SHT_NOBITS;
607 else if (TypeName == "progbits")
608 Type = ELF::SHT_PROGBITS;
609 else if (TypeName == "note")
610 Type = ELF::SHT_NOTE;
611 else if (TypeName == "unwind")
612 Type = ELF::SHT_X86_64_UNWIND;
613 else if (TypeName == "llvm_odrtab")
614 Type = ELF::SHT_LLVM_ODRTAB;
615 else if (TypeName == "llvm_linker_options")
616 Type = ELF::SHT_LLVM_LINKER_OPTIONS;
617 else if (TypeName == "llvm_call_graph_profile")
618 Type = ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
619 else if (TypeName.getAsInteger(0, Type))
620 return TokError("unknown section type");
624 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
625 if (const MCSectionELF *Section =
626 cast_or_null<MCSectionELF>(CurrentSection.first))
627 if (const MCSymbol *Group = Section->getGroup()) {
628 GroupName = Group->getName();
629 Flags |= ELF::SHF_GROUP;
633 MCSection *ELFSection =
634 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
635 UniqueID, Associated);
636 getStreamer().SwitchSection(ELFSection, Subsection);
638 if (getContext().getGenDwarfForAssembly()) {
639 bool InsertResult = getContext().addGenDwarfSection(ELFSection);
641 if (getContext().getDwarfVersion() <= 2)
642 Warning(loc, "DWARF2 only supports one section per compilation unit");
644 if (!ELFSection->getBeginSymbol()) {
645 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
646 getStreamer().EmitLabel(SectionStartSymbol);
647 ELFSection->setBeginSymbol(SectionStartSymbol);
655 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
656 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
657 if (PreviousSection.first == nullptr)
658 return TokError(".previous without corresponding .section");
659 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
664 static MCSymbolAttr MCAttrForString(StringRef Type) {
665 return StringSwitch<MCSymbolAttr>(Type)
666 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
667 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
668 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
669 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
670 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
671 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
672 MCSA_ELF_TypeIndFunction)
673 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
674 .Default(MCSA_Invalid);
677 /// ParseDirectiveELFType
678 /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
679 /// ::= .type identifier , #attribute
680 /// ::= .type identifier , @attribute
681 /// ::= .type identifier , %attribute
682 /// ::= .type identifier , "attribute"
683 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
685 if (getParser().parseIdentifier(Name))
686 return TokError("expected identifier in directive");
688 // Handle the identifier as the key symbol.
689 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
691 // NOTE the comma is optional in all cases. It is only documented as being
692 // optional in the first case, however, GAS will silently treat the comma as
693 // optional in all cases. Furthermore, although the documentation states that
694 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
695 // accepts both the upper case name as well as the lower case aliases.
696 if (getLexer().is(AsmToken::Comma))
699 if (getLexer().isNot(AsmToken::Identifier) &&
700 getLexer().isNot(AsmToken::Hash) &&
701 getLexer().isNot(AsmToken::Percent) &&
702 getLexer().isNot(AsmToken::String)) {
703 if (!getLexer().getAllowAtInIdentifier())
704 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
705 "'%<type>' or \"<type>\"");
706 else if (getLexer().isNot(AsmToken::At))
707 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
708 "'%<type>' or \"<type>\"");
711 if (getLexer().isNot(AsmToken::String) &&
712 getLexer().isNot(AsmToken::Identifier))
715 SMLoc TypeLoc = getLexer().getLoc();
718 if (getParser().parseIdentifier(Type))
719 return TokError("expected symbol type in directive");
721 MCSymbolAttr Attr = MCAttrForString(Type);
722 if (Attr == MCSA_Invalid)
723 return Error(TypeLoc, "unsupported attribute in '.type' directive");
725 if (getLexer().isNot(AsmToken::EndOfStatement))
726 return TokError("unexpected token in '.type' directive");
729 getStreamer().EmitSymbolAttribute(Sym, Attr);
734 /// ParseDirectiveIdent
735 /// ::= .ident string
736 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
737 if (getLexer().isNot(AsmToken::String))
738 return TokError("unexpected token in '.ident' directive");
740 StringRef Data = getTok().getIdentifier();
744 if (getLexer().isNot(AsmToken::EndOfStatement))
745 return TokError("unexpected token in '.ident' directive");
748 getStreamer().EmitIdent(Data);
752 /// ParseDirectiveSymver
753 /// ::= .symver foo, bar2@zed
754 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
756 if (getParser().parseIdentifier(Name))
757 return TokError("expected identifier in directive");
759 if (getLexer().isNot(AsmToken::Comma))
760 return TokError("expected a comma");
762 // ARM assembly uses @ for a comment...
763 // except when parsing the second parameter of the .symver directive.
764 // Force the next symbol to allow @ in the identifier, which is
765 // required for this directive and then reset it to its initial state.
766 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
767 getLexer().setAllowAtInIdentifier(true);
769 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
772 if (getParser().parseIdentifier(AliasName))
773 return TokError("expected identifier in directive");
775 if (AliasName.find('@') == StringRef::npos)
776 return TokError("expected a '@' in the name");
778 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
779 getStreamer().emitELFSymverDirective(AliasName, Sym);
783 /// ParseDirectiveVersion
784 /// ::= .version string
785 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
786 if (getLexer().isNot(AsmToken::String))
787 return TokError("unexpected token in '.version' directive");
789 StringRef Data = getTok().getIdentifier();
793 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
795 getStreamer().PushSection();
796 getStreamer().SwitchSection(Note);
797 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
798 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
799 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
800 getStreamer().EmitBytes(Data); // name.
801 getStreamer().EmitIntValue(0, 1); // terminate the string.
802 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
803 getStreamer().PopSection();
807 /// ParseDirectiveWeakref
808 /// ::= .weakref foo, bar
809 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
810 // FIXME: Share code with the other alias building directives.
813 if (getParser().parseIdentifier(AliasName))
814 return TokError("expected identifier in directive");
816 if (getLexer().isNot(AsmToken::Comma))
817 return TokError("expected a comma");
822 if (getParser().parseIdentifier(Name))
823 return TokError("expected identifier in directive");
825 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
827 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
829 getStreamer().EmitWeakReference(Alias, Sym);
833 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
834 const MCExpr *Subsection = nullptr;
835 if (getLexer().isNot(AsmToken::EndOfStatement)) {
836 if (getParser().parseExpression(Subsection))
840 if (getLexer().isNot(AsmToken::EndOfStatement))
841 return TokError("unexpected token in directive");
845 getStreamer().SubSection(Subsection);
849 /// ParseDirectiveCGProfile
850 /// ::= .cg_profile identifier, identifier, <number>
851 bool ELFAsmParser::ParseDirectiveCGProfile(StringRef, SMLoc) {
853 SMLoc FromLoc = getLexer().getLoc();
854 if (getParser().parseIdentifier(From))
855 return TokError("expected identifier in directive");
857 if (getLexer().isNot(AsmToken::Comma))
858 return TokError("expected a comma");
862 SMLoc ToLoc = getLexer().getLoc();
863 if (getParser().parseIdentifier(To))
864 return TokError("expected identifier in directive");
866 if (getLexer().isNot(AsmToken::Comma))
867 return TokError("expected a comma");
871 if (getParser().parseIntToken(
872 Count, "expected integer count in '.cg_profile' directive"))
875 if (getLexer().isNot(AsmToken::EndOfStatement))
876 return TokError("unexpected token in directive");
878 MCSymbol *FromSym = getContext().getOrCreateSymbol(From);
879 MCSymbol *ToSym = getContext().getOrCreateSymbol(To);
881 getStreamer().emitCGProfileEntry(
882 MCSymbolRefExpr::create(FromSym, MCSymbolRefExpr::VK_None, getContext(),
884 MCSymbolRefExpr::create(ToSym, MCSymbolRefExpr::VK_None, getContext(),
892 MCAsmParserExtension *createELFAsmParser() {
893 return new ELFAsmParser;
896 } // end namespace llvm