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_GROUP;
328 *UseLastGroup = true;
338 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
340 while (getLexer().is(AsmToken::Hash)) {
343 if (!getLexer().is(AsmToken::Identifier))
346 StringRef flagId = getTok().getIdentifier();
347 if (flagId == "alloc")
348 flags |= ELF::SHF_ALLOC;
349 else if (flagId == "execinstr")
350 flags |= ELF::SHF_EXECINSTR;
351 else if (flagId == "write")
352 flags |= ELF::SHF_WRITE;
353 else if (flagId == "tls")
354 flags |= ELF::SHF_TLS;
358 Lex(); // Eat the flag.
360 if (!getLexer().is(AsmToken::Comma))
362 Lex(); // Eat the comma.
368 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
369 getStreamer().PushSection();
371 if (ParseSectionArguments(/*IsPush=*/true, loc)) {
372 getStreamer().PopSection();
379 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
380 if (!getStreamer().PopSection())
381 return TokError(".popsection without corresponding .pushsection");
385 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
386 return ParseSectionArguments(/*IsPush=*/false, loc);
389 bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
390 MCAsmLexer &L = getLexer();
391 if (L.isNot(AsmToken::Comma))
394 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
395 L.isNot(AsmToken::String)) {
396 if (L.getAllowAtInIdentifier())
397 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
399 return TokError("expected '%<type>' or \"<type>\"");
401 if (!L.is(AsmToken::String))
403 if (L.is(AsmToken::Integer)) {
404 TypeName = getTok().getString();
406 } else if (getParser().parseIdentifier(TypeName))
407 return TokError("expected identifier in directive");
411 bool ELFAsmParser::parseMergeSize(int64_t &Size) {
412 if (getLexer().isNot(AsmToken::Comma))
413 return TokError("expected the entry size");
415 if (getParser().parseAbsoluteExpression(Size))
418 return TokError("entry size must be positive");
422 bool ELFAsmParser::parseGroup(StringRef &GroupName) {
423 MCAsmLexer &L = getLexer();
424 if (L.isNot(AsmToken::Comma))
425 return TokError("expected group name");
427 if (L.is(AsmToken::Integer)) {
428 GroupName = getTok().getString();
430 } else if (getParser().parseIdentifier(GroupName)) {
431 return TokError("invalid group name");
433 if (L.is(AsmToken::Comma)) {
436 if (getParser().parseIdentifier(Linkage))
437 return TokError("invalid linkage");
438 if (Linkage != "comdat")
439 return TokError("Linkage must be 'comdat'");
444 bool ELFAsmParser::parseMetadataSym(MCSymbolELF *&Associated) {
445 MCAsmLexer &L = getLexer();
446 if (L.isNot(AsmToken::Comma))
447 return TokError("expected metadata symbol");
450 if (getParser().parseIdentifier(Name))
451 return TokError("invalid metadata symbol");
452 Associated = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
453 if (!Associated || !Associated->isInSection())
454 return TokError("symbol is not in a section: " + Name);
458 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
459 MCAsmLexer &L = getLexer();
460 if (L.isNot(AsmToken::Comma))
464 if (getParser().parseIdentifier(UniqueStr))
465 return TokError("expected identifier in directive");
466 if (UniqueStr != "unique")
467 return TokError("expected 'unique'");
468 if (L.isNot(AsmToken::Comma))
469 return TokError("expected commma");
471 if (getParser().parseAbsoluteExpression(UniqueID))
474 return TokError("unique id must be positive");
475 if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
476 return TokError("unique id is too large");
480 static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
481 return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back();
484 bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
485 StringRef SectionName;
487 if (ParseSectionName(SectionName))
488 return TokError("expected identifier in directive");
494 const MCExpr *Subsection = nullptr;
495 bool UseLastGroup = false;
496 MCSymbolELF *Associated = nullptr;
497 int64_t UniqueID = ~0;
499 // Set the defaults first.
500 if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
501 Flags |= ELF::SHF_ALLOC;
502 else if (SectionName == ".fini" || SectionName == ".init" ||
503 hasPrefix(SectionName, ".text."))
504 Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
505 else if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
506 hasPrefix(SectionName, ".bss.") ||
507 hasPrefix(SectionName, ".init_array.") ||
508 hasPrefix(SectionName, ".fini_array.") ||
509 hasPrefix(SectionName, ".preinit_array."))
510 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE;
511 else if (hasPrefix(SectionName, ".tdata.") ||
512 hasPrefix(SectionName, ".tbss."))
513 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS;
515 if (getLexer().is(AsmToken::Comma)) {
518 if (IsPush && getLexer().isNot(AsmToken::String)) {
519 if (getParser().parseExpression(Subsection))
521 if (getLexer().isNot(AsmToken::Comma))
528 if (getLexer().isNot(AsmToken::String)) {
529 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
530 || getLexer().isNot(AsmToken::Hash))
531 return TokError("expected string in directive");
532 extraFlags = parseSunStyleSectionFlags();
534 StringRef FlagsStr = getTok().getStringContents();
536 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
539 if (extraFlags == -1U)
540 return TokError("unknown flag");
543 bool Mergeable = Flags & ELF::SHF_MERGE;
544 bool Group = Flags & ELF::SHF_GROUP;
545 if (Group && UseLastGroup)
546 return TokError("Section cannot specifiy a group name while also acting "
547 "as a member of the last group");
549 if (maybeParseSectionType(TypeName))
552 MCAsmLexer &L = getLexer();
553 if (TypeName.empty()) {
555 return TokError("Mergeable section must specify the type");
557 return TokError("Group section must specify the type");
558 if (L.isNot(AsmToken::EndOfStatement))
559 return TokError("unexpected token in directive");
563 if (parseMergeSize(Size))
566 if (parseGroup(GroupName))
568 if (Flags & ELF::SHF_LINK_ORDER)
569 if (parseMetadataSym(Associated))
571 if (maybeParseUniqueID(UniqueID))
576 if (getLexer().isNot(AsmToken::EndOfStatement))
577 return TokError("unexpected token in directive");
580 unsigned Type = ELF::SHT_PROGBITS;
582 if (TypeName.empty()) {
583 if (SectionName.startswith(".note"))
584 Type = ELF::SHT_NOTE;
585 else if (hasPrefix(SectionName, ".init_array."))
586 Type = ELF::SHT_INIT_ARRAY;
587 else if (hasPrefix(SectionName, ".bss."))
588 Type = ELF::SHT_NOBITS;
589 else if (hasPrefix(SectionName, ".tbss."))
590 Type = ELF::SHT_NOBITS;
591 else if (hasPrefix(SectionName, ".fini_array."))
592 Type = ELF::SHT_FINI_ARRAY;
593 else if (hasPrefix(SectionName, ".preinit_array."))
594 Type = ELF::SHT_PREINIT_ARRAY;
596 if (TypeName == "init_array")
597 Type = ELF::SHT_INIT_ARRAY;
598 else if (TypeName == "fini_array")
599 Type = ELF::SHT_FINI_ARRAY;
600 else if (TypeName == "preinit_array")
601 Type = ELF::SHT_PREINIT_ARRAY;
602 else if (TypeName == "nobits")
603 Type = ELF::SHT_NOBITS;
604 else if (TypeName == "progbits")
605 Type = ELF::SHT_PROGBITS;
606 else if (TypeName == "note")
607 Type = ELF::SHT_NOTE;
608 else if (TypeName == "unwind")
609 Type = ELF::SHT_X86_64_UNWIND;
610 else if (TypeName == "llvm_odrtab")
611 Type = ELF::SHT_LLVM_ODRTAB;
612 else if (TypeName == "llvm_linker_options")
613 Type = ELF::SHT_LLVM_LINKER_OPTIONS;
614 else if (TypeName == "llvm_call_graph_profile")
615 Type = ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
616 else if (TypeName.getAsInteger(0, Type))
617 return TokError("unknown section type");
621 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
622 if (const MCSectionELF *Section =
623 cast_or_null<MCSectionELF>(CurrentSection.first))
624 if (const MCSymbol *Group = Section->getGroup()) {
625 GroupName = Group->getName();
626 Flags |= ELF::SHF_GROUP;
630 MCSection *ELFSection =
631 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
632 UniqueID, Associated);
633 getStreamer().SwitchSection(ELFSection, Subsection);
635 if (getContext().getGenDwarfForAssembly()) {
636 bool InsertResult = getContext().addGenDwarfSection(ELFSection);
638 if (getContext().getDwarfVersion() <= 2)
639 Warning(loc, "DWARF2 only supports one section per compilation unit");
641 if (!ELFSection->getBeginSymbol()) {
642 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
643 getStreamer().EmitLabel(SectionStartSymbol);
644 ELFSection->setBeginSymbol(SectionStartSymbol);
652 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
653 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
654 if (PreviousSection.first == nullptr)
655 return TokError(".previous without corresponding .section");
656 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
661 static MCSymbolAttr MCAttrForString(StringRef Type) {
662 return StringSwitch<MCSymbolAttr>(Type)
663 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
664 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
665 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
666 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
667 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
668 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
669 MCSA_ELF_TypeIndFunction)
670 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
671 .Default(MCSA_Invalid);
674 /// ParseDirectiveELFType
675 /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
676 /// ::= .type identifier , #attribute
677 /// ::= .type identifier , @attribute
678 /// ::= .type identifier , %attribute
679 /// ::= .type identifier , "attribute"
680 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
682 if (getParser().parseIdentifier(Name))
683 return TokError("expected identifier in directive");
685 // Handle the identifier as the key symbol.
686 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
688 // NOTE the comma is optional in all cases. It is only documented as being
689 // optional in the first case, however, GAS will silently treat the comma as
690 // optional in all cases. Furthermore, although the documentation states that
691 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
692 // accepts both the upper case name as well as the lower case aliases.
693 if (getLexer().is(AsmToken::Comma))
696 if (getLexer().isNot(AsmToken::Identifier) &&
697 getLexer().isNot(AsmToken::Hash) &&
698 getLexer().isNot(AsmToken::Percent) &&
699 getLexer().isNot(AsmToken::String)) {
700 if (!getLexer().getAllowAtInIdentifier())
701 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
702 "'%<type>' or \"<type>\"");
703 else if (getLexer().isNot(AsmToken::At))
704 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
705 "'%<type>' or \"<type>\"");
708 if (getLexer().isNot(AsmToken::String) &&
709 getLexer().isNot(AsmToken::Identifier))
712 SMLoc TypeLoc = getLexer().getLoc();
715 if (getParser().parseIdentifier(Type))
716 return TokError("expected symbol type in directive");
718 MCSymbolAttr Attr = MCAttrForString(Type);
719 if (Attr == MCSA_Invalid)
720 return Error(TypeLoc, "unsupported attribute in '.type' directive");
722 if (getLexer().isNot(AsmToken::EndOfStatement))
723 return TokError("unexpected token in '.type' directive");
726 getStreamer().EmitSymbolAttribute(Sym, Attr);
731 /// ParseDirectiveIdent
732 /// ::= .ident string
733 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
734 if (getLexer().isNot(AsmToken::String))
735 return TokError("unexpected token in '.ident' directive");
737 StringRef Data = getTok().getIdentifier();
741 if (getLexer().isNot(AsmToken::EndOfStatement))
742 return TokError("unexpected token in '.ident' directive");
745 getStreamer().EmitIdent(Data);
749 /// ParseDirectiveSymver
750 /// ::= .symver foo, bar2@zed
751 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
753 if (getParser().parseIdentifier(Name))
754 return TokError("expected identifier in directive");
756 if (getLexer().isNot(AsmToken::Comma))
757 return TokError("expected a comma");
759 // ARM assembly uses @ for a comment...
760 // except when parsing the second parameter of the .symver directive.
761 // Force the next symbol to allow @ in the identifier, which is
762 // required for this directive and then reset it to its initial state.
763 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
764 getLexer().setAllowAtInIdentifier(true);
766 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
769 if (getParser().parseIdentifier(AliasName))
770 return TokError("expected identifier in directive");
772 if (AliasName.find('@') == StringRef::npos)
773 return TokError("expected a '@' in the name");
775 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
776 getStreamer().emitELFSymverDirective(AliasName, Sym);
780 /// ParseDirectiveVersion
781 /// ::= .version string
782 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
783 if (getLexer().isNot(AsmToken::String))
784 return TokError("unexpected token in '.version' directive");
786 StringRef Data = getTok().getIdentifier();
790 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
792 getStreamer().PushSection();
793 getStreamer().SwitchSection(Note);
794 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
795 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
796 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
797 getStreamer().EmitBytes(Data); // name.
798 getStreamer().EmitIntValue(0, 1); // terminate the string.
799 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
800 getStreamer().PopSection();
804 /// ParseDirectiveWeakref
805 /// ::= .weakref foo, bar
806 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
807 // FIXME: Share code with the other alias building directives.
810 if (getParser().parseIdentifier(AliasName))
811 return TokError("expected identifier in directive");
813 if (getLexer().isNot(AsmToken::Comma))
814 return TokError("expected a comma");
819 if (getParser().parseIdentifier(Name))
820 return TokError("expected identifier in directive");
822 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
824 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
826 getStreamer().EmitWeakReference(Alias, Sym);
830 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
831 const MCExpr *Subsection = nullptr;
832 if (getLexer().isNot(AsmToken::EndOfStatement)) {
833 if (getParser().parseExpression(Subsection))
837 if (getLexer().isNot(AsmToken::EndOfStatement))
838 return TokError("unexpected token in directive");
842 getStreamer().SubSection(Subsection);
846 /// ParseDirectiveCGProfile
847 /// ::= .cg_profile identifier, identifier, <number>
848 bool ELFAsmParser::ParseDirectiveCGProfile(StringRef, SMLoc) {
850 SMLoc FromLoc = getLexer().getLoc();
851 if (getParser().parseIdentifier(From))
852 return TokError("expected identifier in directive");
854 if (getLexer().isNot(AsmToken::Comma))
855 return TokError("expected a comma");
859 SMLoc ToLoc = getLexer().getLoc();
860 if (getParser().parseIdentifier(To))
861 return TokError("expected identifier in directive");
863 if (getLexer().isNot(AsmToken::Comma))
864 return TokError("expected a comma");
868 if (getParser().parseIntToken(
869 Count, "expected integer count in '.cg_profile' directive"))
872 if (getLexer().isNot(AsmToken::EndOfStatement))
873 return TokError("unexpected token in directive");
875 MCSymbol *FromSym = getContext().getOrCreateSymbol(From);
876 MCSymbol *ToSym = getContext().getOrCreateSymbol(To);
878 getStreamer().emitCGProfileEntry(
879 MCSymbolRefExpr::create(FromSym, MCSymbolRefExpr::VK_None, getContext(),
881 MCSymbolRefExpr::create(ToSym, MCSymbolRefExpr::VK_None, getContext(),
889 MCAsmParserExtension *createELFAsmParser() {
890 return new ELFAsmParser;
893 } // end namespace llvm