38 template<
bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
39 void addDirectiveHandler(
StringRef Directive) {
41 this, HandleDirective<ELFAsmParser, HandlerMethod>);
43 getParser().addDirectiveHandler(Directive, Handler);
50 ELFAsmParser() { BracketExpressionsSupported =
true; }
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");
157 bool ParseSectionArguments(
bool IsPush,
SMLoc loc);
158 unsigned parseSunStyleSectionFlags();
160 bool parseMergeSize(int64_t &
Size);
163 bool maybeParseUniqueID(int64_t &UniqueID);
170 bool ELFAsmParser::ParseDirectiveSymbolAttribute(
StringRef Directive,
SMLoc) {
183 if (getParser().parseIdentifier(Name))
184 return TokError(
"expected identifier in directive");
186 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
188 getStreamer().EmitSymbolAttribute(Sym, Attr);
194 return TokError(
"unexpected token in directive");
205 const MCExpr *Subsection =
nullptr;
207 if (getParser().parseExpression(Subsection))
212 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
220 if (getParser().parseIdentifier(Name))
221 return TokError(
"expected identifier in directive");
222 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
225 return TokError(
"unexpected token in directive");
229 if (getParser().parseExpression(Expr))
233 return TokError(
"unexpected token in directive");
236 getStreamer().emitELFSize(Sym, Expr);
243 SMLoc FirstLoc = getLexer().getLoc();
247 SectionName = getTok().getIdentifier();
252 while (!getParser().hasPendingError()) {
253 SMLoc PrevLoc = getLexer().getLoc();
260 CurSize = getTok().getIdentifier().size() + 2;
263 CurSize = getTok().getIdentifier().size();
266 CurSize = getTok().getString().size();
273 if (PrevLoc.
getPointer() + CurSize != getTok().getLoc().getPointer())
289 for (
char i : flagsStr) {
328 *UseLastGroup =
true;
338 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
346 StringRef flagId = getTok().getIdentifier();
347 if (flagId ==
"alloc")
349 else if (flagId ==
"execinstr")
351 else if (flagId ==
"write")
353 else if (flagId ==
"tls")
368 bool ELFAsmParser::ParseDirectivePushSection(
StringRef s,
SMLoc loc) {
369 getStreamer().PushSection();
371 if (ParseSectionArguments(
true, loc)) {
372 getStreamer().PopSection();
380 if (!getStreamer().PopSection())
381 return TokError(
".popsection without corresponding .pushsection");
386 return ParseSectionArguments(
false, loc);
397 return TokError(
"expected '@<type>', '%<type>' or \"<type>\"");
399 return TokError(
"expected '%<type>' or \"<type>\"");
404 TypeName = getTok().getString();
406 }
else if (getParser().parseIdentifier(TypeName))
407 return TokError(
"expected identifier in directive");
411 bool ELFAsmParser::parseMergeSize(int64_t &Size) {
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) {
425 return TokError(
"expected group name");
428 GroupName = getTok().getString();
430 }
else if (getParser().parseIdentifier(GroupName)) {
431 return TokError(
"invalid group name");
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) {
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));
454 return TokError(
"symbol is not in a section: " + Name);
458 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
464 if (getParser().parseIdentifier(UniqueStr))
465 return TokError(
"expected identifier in directive");
466 if (UniqueStr !=
"unique")
467 return TokError(
"expected 'unique'");
469 return TokError(
"expected commma");
471 if (getParser().parseAbsoluteExpression(UniqueID))
474 return TokError(
"unique id must be positive");
476 return TokError(
"unique id is too large");
484 bool ELFAsmParser::ParseSectionArguments(
bool IsPush,
SMLoc loc) {
487 if (ParseSectionName(SectionName))
488 return TokError(
"expected identifier in directive");
494 const MCExpr *Subsection =
nullptr;
495 bool UseLastGroup =
false;
497 int64_t UniqueID = ~0;
500 if (
hasPrefix(SectionName,
".rodata.") || SectionName ==
".rodata1")
502 else if (SectionName ==
".fini" || SectionName ==
".init" ||
505 else if (
hasPrefix(SectionName,
".data.") || SectionName ==
".data1" ||
507 hasPrefix(SectionName,
".init_array.") ||
508 hasPrefix(SectionName,
".fini_array.") ||
509 hasPrefix(SectionName,
".preinit_array."))
511 else if (
hasPrefix(SectionName,
".tdata.") ||
519 if (getParser().parseExpression(Subsection))
529 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
531 return TokError(
"expected string in directive");
532 extraFlags = parseSunStyleSectionFlags();
534 StringRef FlagsStr = getTok().getStringContents();
539 if (extraFlags == -1U)
540 return TokError(
"unknown flag");
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))
553 if (TypeName.
empty()) {
555 return TokError(
"Mergeable section must specify the type");
557 return TokError(
"Group section must specify the type");
559 return TokError(
"unexpected token in directive");
563 if (parseMergeSize(Size))
566 if (parseGroup(GroupName))
569 if (parseMetadataSym(Associated))
571 if (maybeParseUniqueID(UniqueID))
577 return TokError(
"unexpected token in directive");
582 if (TypeName.
empty()) {
585 else if (
hasPrefix(SectionName,
".init_array."))
587 else if (
hasPrefix(SectionName,
".bss."))
589 else if (
hasPrefix(SectionName,
".tbss."))
591 else if (
hasPrefix(SectionName,
".fini_array."))
593 else if (
hasPrefix(SectionName,
".preinit_array."))
596 if (TypeName ==
"init_array")
598 else if (TypeName ==
"fini_array")
600 else if (TypeName ==
"preinit_array")
602 else if (TypeName ==
"nobits")
604 else if (TypeName ==
"progbits")
606 else if (TypeName ==
"note")
608 else if (TypeName ==
"unwind")
610 else if (TypeName ==
"llvm_odrtab")
612 else if (TypeName ==
"llvm_linker_options")
614 else if (TypeName ==
"llvm_call_graph_profile")
617 return TokError(
"unknown section type");
623 cast_or_null<MCSectionELF>(CurrentSection.first))
624 if (
const MCSymbol *Group = Section->getGroup()) {
625 GroupName = Group->getName();
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");
642 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
643 getStreamer().EmitLabel(SectionStartSymbol);
652 bool ELFAsmParser::ParseDirectivePrevious(
StringRef DirName,
SMLoc) {
654 if (PreviousSection.first ==
nullptr)
655 return TokError(
".previous without corresponding .section");
656 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
668 .
Cases(
"STT_GNU_IFUNC",
"gnu_indirect_function",
682 if (getParser().parseIdentifier(Name))
683 return TokError(
"expected identifier in directive");
686 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
700 if (!getLexer().getAllowAtInIdentifier())
701 return TokError(
"expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', " 702 "'%<type>' or \"<type>\"");
704 return TokError(
"expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', " 705 "'%<type>' or \"<type>\"");
712 SMLoc TypeLoc = getLexer().getLoc();
715 if (getParser().parseIdentifier(Type))
716 return TokError(
"expected symbol type in directive");
720 return Error(TypeLoc,
"unsupported attribute in '.type' directive");
723 return TokError(
"unexpected token in '.type' directive");
726 getStreamer().EmitSymbolAttribute(Sym, Attr);
735 return TokError(
"unexpected token in '.ident' directive");
742 return TokError(
"unexpected token in '.ident' directive");
745 getStreamer().EmitIdent(Data);
753 if (getParser().parseIdentifier(Name))
754 return TokError(
"expected identifier in directive");
757 return TokError(
"expected a comma");
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");
773 return TokError(
"expected a '@' in the name");
775 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
776 getStreamer().emitELFSymverDirective(AliasName, Sym);
784 return TokError(
"unexpected token in '.version' directive");
792 getStreamer().PushSection();
793 getStreamer().SwitchSection(Note);
794 getStreamer().EmitIntValue(Data.
size()+1, 4);
795 getStreamer().EmitIntValue(0, 4);
796 getStreamer().EmitIntValue(1, 4);
797 getStreamer().EmitBytes(Data);
798 getStreamer().EmitIntValue(0, 1);
799 getStreamer().EmitValueToAlignment(4);
800 getStreamer().PopSection();
810 if (getParser().parseIdentifier(AliasName))
811 return TokError(
"expected identifier in directive");
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);
831 const MCExpr *Subsection =
nullptr;
833 if (getParser().parseExpression(Subsection))
838 return TokError(
"unexpected token in directive");
842 getStreamer().SubSection(Subsection);
850 SMLoc FromLoc = getLexer().getLoc();
851 if (getParser().parseIdentifier(From))
852 return TokError(
"expected identifier in directive");
855 return TokError(
"expected a comma");
859 SMLoc ToLoc = getLexer().getLoc();
860 if (getParser().parseIdentifier(To))
861 return TokError(
"expected identifier in directive");
864 return TokError(
"expected a comma");
868 if (getParser().parseIntToken(
869 Count,
"expected integer count in '.cg_profile' directive"))
873 return TokError(
"unexpected token in directive");
875 MCSymbol *FromSym = getContext().getOrCreateSymbol(From);
876 MCSymbol *ToSym = getContext().getOrCreateSymbol(To);
878 getStreamer().emitCGProfileEntry(
890 return new ELFAsmParser;
constexpr bool isUInt< 32 >(uint64_t x)
Instances of this class represent a uniqued identifier for a section in the current translation unit...
static SectionKind getData()
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Compute iterated dominance frontiers using a linear time algorithm.
.type _foo, STT_OBJECT # aka
bool is(AsmToken::TokenKind K) const
Check if the current token has kind K.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Generic assembler parser interface, for use by target specific assembly parsers.
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup)
static MCSymbolAttr MCAttrForString(StringRef Type)
.type _foo, STT_NOTYPE # aka
MCAsmParserExtension * createELFAsmParser()
static SectionKind getBSS()
Generic assembler lexer interface, for use by target specific assembly lexers.
All sections with the "d" flag are grouped together by the linker to form the data section and the dp...
static bool hasPrefix(StringRef SectionName, StringRef Prefix)
Base class for the full range of assembler expressions which are needed for parsing.
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(T Value)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
.type _foo, STT_GNU_IFUNC
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
static SectionKind getThreadData()
const char * getPointer() const
A switch()-like statement whose cases are string literals.
The instances of the Type class are immutable: once they are created, they are never changed...
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
SectionKind - This is a simple POD value that classifies the properties of a section.
bool getAllowAtInIdentifier()
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
static SectionKind getThreadBSS()
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
.type _foo, STT_TLS # aka
static SectionKind getReadOnlyWithRel()
MCSymbol * getBeginSymbol()
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(StringLiteral S, T Value)
.type _foo, STT_COMMON # aka
.type _foo, STT_FUNC # aka
This represents a section on linux, lots of unix variants and some bare metal systems.
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
bool isNot(AsmToken::TokenKind K) const
Check if the current token has kind K.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
StringRef - Represent a constant reference to a string, i.e.
All sections with the "c" flag are grouped together by the linker to form the constant pool and the c...
Represents a location in source code.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
static SectionKind getReadOnly()
static SectionKind getText()
void setBeginSymbol(MCSymbol *Sym)