37 template<
bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
40 this, HandleDirective<ELFAsmParser, HandlerMethod>);
42 getParser().addDirectiveHandler(Directive, Handler);
49 ELFAsmParser() { BracketExpressionsSupported =
true; }
55 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(
".data");
56 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(
".text");
57 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(
".bss");
58 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(
".rodata");
59 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(
".tdata");
60 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(
".tbss");
62 &ELFAsmParser::ParseSectionDirectiveDataRel>(
".data.rel");
64 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(
".data.rel.ro");
66 &ELFAsmParser::ParseSectionDirectiveEhFrame>(
".eh_frame");
67 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(
".section");
69 &ELFAsmParser::ParseDirectivePushSection>(
".pushsection");
70 addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(
".popsection");
71 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(
".size");
72 addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(
".previous");
73 addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(
".type");
74 addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(
".ident");
75 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(
".symver");
76 addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(
".version");
77 addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(
".weakref");
78 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(
".weak");
79 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(
".local");
81 &ELFAsmParser::ParseDirectiveSymbolAttribute>(
".protected");
83 &ELFAsmParser::ParseDirectiveSymbolAttribute>(
".internal");
85 &ELFAsmParser::ParseDirectiveSymbolAttribute>(
".hidden");
86 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(
".subsection");
87 addDirectiveHandler<&ELFAsmParser::ParseDirectiveCGProfile>(
".cg_profile");
156 bool ParseSectionArguments(
bool IsPush,
SMLoc loc);
157 unsigned parseSunStyleSectionFlags();
159 bool parseMergeSize(int64_t &
Size);
162 bool maybeParseUniqueID(int64_t &UniqueID);
169 bool ELFAsmParser::ParseDirectiveSymbolAttribute(
StringRef Directive,
SMLoc) {
182 if (getParser().parseIdentifier(Name))
183 return TokError(
"expected identifier in directive");
185 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
187 getStreamer().EmitSymbolAttribute(Sym, Attr);
193 return TokError(
"unexpected token in directive");
204 const MCExpr *Subsection =
nullptr;
206 if (getParser().parseExpression(Subsection))
211 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
219 if (getParser().parseIdentifier(Name))
220 return TokError(
"expected identifier in directive");
221 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
224 return TokError(
"unexpected token in directive");
228 if (getParser().parseExpression(Expr))
232 return TokError(
"unexpected token in directive");
235 getStreamer().emitELFSize(Sym, Expr);
242 SMLoc FirstLoc = getLexer().getLoc();
246 SectionName = getTok().getIdentifier();
251 while (!getParser().hasPendingError()) {
252 SMLoc PrevLoc = getLexer().getLoc();
259 CurSize = getTok().getIdentifier().size() + 2;
262 CurSize = getTok().getIdentifier().size();
265 CurSize = getTok().getString().size();
272 if (PrevLoc.
getPointer() + CurSize != getTok().getLoc().getPointer())
288 for (
char i : flagsStr) {
330 *UseLastGroup =
true;
340 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
348 StringRef flagId = getTok().getIdentifier();
349 if (flagId ==
"alloc")
351 else if (flagId ==
"execinstr")
353 else if (flagId ==
"write")
355 else if (flagId ==
"tls")
370 bool ELFAsmParser::ParseDirectivePushSection(
StringRef s,
SMLoc loc) {
371 getStreamer().PushSection();
373 if (ParseSectionArguments(
true, loc)) {
374 getStreamer().PopSection();
382 if (!getStreamer().PopSection())
383 return TokError(
".popsection without corresponding .pushsection");
388 return ParseSectionArguments(
false, loc);
399 return TokError(
"expected '@<type>', '%<type>' or \"<type>\"");
401 return TokError(
"expected '%<type>' or \"<type>\"");
406 TypeName = getTok().getString();
408 }
else if (getParser().parseIdentifier(TypeName))
409 return TokError(
"expected identifier in directive");
413 bool ELFAsmParser::parseMergeSize(int64_t &Size) {
415 return TokError(
"expected the entry size");
417 if (getParser().parseAbsoluteExpression(Size))
420 return TokError(
"entry size must be positive");
424 bool ELFAsmParser::parseGroup(
StringRef &GroupName) {
427 return TokError(
"expected group name");
430 GroupName = getTok().getString();
432 }
else if (getParser().parseIdentifier(GroupName)) {
433 return TokError(
"invalid group name");
438 if (getParser().parseIdentifier(Linkage))
439 return TokError(
"invalid linkage");
440 if (Linkage !=
"comdat")
441 return TokError(
"Linkage must be 'comdat'");
446 bool ELFAsmParser::parseMetadataSym(
MCSymbolELF *&Associated) {
449 return TokError(
"expected metadata symbol");
452 if (getParser().parseIdentifier(Name))
453 return TokError(
"invalid metadata symbol");
454 Associated = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
456 return TokError(
"symbol is not in a section: " + Name);
460 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
466 if (getParser().parseIdentifier(UniqueStr))
467 return TokError(
"expected identifier in directive");
468 if (UniqueStr !=
"unique")
469 return TokError(
"expected 'unique'");
471 return TokError(
"expected commma");
473 if (getParser().parseAbsoluteExpression(UniqueID))
476 return TokError(
"unique id must be positive");
478 return TokError(
"unique id is too large");
486 bool ELFAsmParser::ParseSectionArguments(
bool IsPush,
SMLoc loc) {
489 if (ParseSectionName(SectionName))
490 return TokError(
"expected identifier in directive");
496 const MCExpr *Subsection =
nullptr;
497 bool UseLastGroup =
false;
499 int64_t UniqueID = ~0;
502 if (
hasPrefix(SectionName,
".rodata.") || SectionName ==
".rodata1")
504 else if (SectionName ==
".fini" || SectionName ==
".init" ||
507 else if (
hasPrefix(SectionName,
".data.") || SectionName ==
".data1" ||
509 hasPrefix(SectionName,
".init_array.") ||
510 hasPrefix(SectionName,
".fini_array.") ||
511 hasPrefix(SectionName,
".preinit_array."))
513 else if (
hasPrefix(SectionName,
".tdata.") ||
521 if (getParser().parseExpression(Subsection))
531 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
533 return TokError(
"expected string in directive");
534 extraFlags = parseSunStyleSectionFlags();
536 StringRef FlagsStr = getTok().getStringContents();
541 if (extraFlags == -1U)
542 return TokError(
"unknown flag");
547 if (Group && UseLastGroup)
548 return TokError(
"Section cannot specifiy a group name while also acting " 549 "as a member of the last group");
551 if (maybeParseSectionType(TypeName))
555 if (TypeName.
empty()) {
557 return TokError(
"Mergeable section must specify the type");
559 return TokError(
"Group section must specify the type");
561 return TokError(
"unexpected token in directive");
565 if (parseMergeSize(Size))
568 if (parseGroup(GroupName))
571 if (parseMetadataSym(Associated))
573 if (maybeParseUniqueID(UniqueID))
579 return TokError(
"unexpected token in directive");
584 if (TypeName.
empty()) {
587 else if (
hasPrefix(SectionName,
".init_array."))
589 else if (
hasPrefix(SectionName,
".bss."))
591 else if (
hasPrefix(SectionName,
".tbss."))
593 else if (
hasPrefix(SectionName,
".fini_array."))
595 else if (
hasPrefix(SectionName,
".preinit_array."))
598 if (TypeName ==
"init_array")
600 else if (TypeName ==
"fini_array")
602 else if (TypeName ==
"preinit_array")
604 else if (TypeName ==
"nobits")
606 else if (TypeName ==
"progbits")
608 else if (TypeName ==
"note")
610 else if (TypeName ==
"unwind")
612 else if (TypeName ==
"llvm_odrtab")
614 else if (TypeName ==
"llvm_linker_options")
616 else if (TypeName ==
"llvm_call_graph_profile")
618 else if (TypeName ==
"llvm_dependent_libraries")
620 else if (TypeName ==
"llvm_sympart")
623 return TokError(
"unknown section type");
629 cast_or_null<MCSectionELF>(CurrentSection.first))
630 if (
const MCSymbol *Group = Section->getGroup()) {
631 GroupName = Group->getName();
637 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
638 UniqueID, Associated);
639 getStreamer().SwitchSection(ELFSection, Subsection);
641 if (getContext().getGenDwarfForAssembly()) {
642 bool InsertResult = getContext().addGenDwarfSection(ELFSection);
644 if (getContext().getDwarfVersion() <= 2)
645 Warning(loc,
"DWARF2 only supports one section per compilation unit");
648 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
649 getStreamer().EmitLabel(SectionStartSymbol);
658 bool ELFAsmParser::ParseDirectivePrevious(
StringRef DirName,
SMLoc) {
660 if (PreviousSection.first ==
nullptr)
661 return TokError(
".previous without corresponding .section");
662 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
674 .
Cases(
"STT_GNU_IFUNC",
"gnu_indirect_function",
688 if (getParser().parseIdentifier(Name))
689 return TokError(
"expected identifier in directive");
692 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
706 if (!getLexer().getAllowAtInIdentifier())
707 return TokError(
"expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', " 708 "'%<type>' or \"<type>\"");
710 return TokError(
"expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', " 711 "'%<type>' or \"<type>\"");
718 SMLoc TypeLoc = getLexer().getLoc();
721 if (getParser().parseIdentifier(Type))
722 return TokError(
"expected symbol type in directive");
726 return Error(TypeLoc,
"unsupported attribute in '.type' directive");
729 return TokError(
"unexpected token in '.type' directive");
732 getStreamer().EmitSymbolAttribute(Sym, Attr);
741 return TokError(
"unexpected token in '.ident' directive");
748 return TokError(
"unexpected token in '.ident' directive");
751 getStreamer().EmitIdent(Data);
759 if (getParser().parseIdentifier(Name))
760 return TokError(
"expected identifier in directive");
763 return TokError(
"expected a comma");
769 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
770 getLexer().setAllowAtInIdentifier(
true);
772 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
775 if (getParser().parseIdentifier(AliasName))
776 return TokError(
"expected identifier in directive");
779 return TokError(
"expected a '@' in the name");
781 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
782 getStreamer().emitELFSymverDirective(AliasName, Sym);
790 return TokError(
"unexpected token in '.version' directive");
798 getStreamer().PushSection();
799 getStreamer().SwitchSection(Note);
800 getStreamer().EmitIntValue(Data.
size()+1, 4);
801 getStreamer().EmitIntValue(0, 4);
802 getStreamer().EmitIntValue(1, 4);
803 getStreamer().EmitBytes(Data);
804 getStreamer().EmitIntValue(0, 1);
805 getStreamer().EmitValueToAlignment(4);
806 getStreamer().PopSection();
816 if (getParser().parseIdentifier(AliasName))
817 return TokError(
"expected identifier in directive");
820 return TokError(
"expected a comma");
825 if (getParser().parseIdentifier(Name))
826 return TokError(
"expected identifier in directive");
828 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
830 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
832 getStreamer().EmitWeakReference(Alias, Sym);
837 const MCExpr *Subsection =
nullptr;
839 if (getParser().parseExpression(Subsection))
844 return TokError(
"unexpected token in directive");
848 getStreamer().SubSection(Subsection);
856 SMLoc FromLoc = getLexer().getLoc();
857 if (getParser().parseIdentifier(From))
858 return TokError(
"expected identifier in directive");
861 return TokError(
"expected a comma");
865 SMLoc ToLoc = getLexer().getLoc();
866 if (getParser().parseIdentifier(To))
867 return TokError(
"expected identifier in directive");
870 return TokError(
"expected a comma");
874 if (getParser().parseIntToken(
875 Count,
"expected integer count in '.cg_profile' directive"))
879 return TokError(
"unexpected token in directive");
881 MCSymbol *FromSym = getContext().getOrCreateSymbol(From);
882 MCSymbol *ToSym = getContext().getOrCreateSymbol(To);
884 getStreamer().emitCGProfileEntry(
896 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)
This class represents lattice values for constants.
.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 ...
All sections with the "d" flag are grouped together by the linker to form the data section and the dp...
Generic assembler parser interface, for use by target specific assembly parsers.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup)
static MCSymbolAttr MCAttrForString(StringRef Type)
.type _foo, STT_NOTYPE # aka
All sections with the "c" flag are grouped together by the linker to form the constant pool and the c...
StringSwitch & Case(StringLiteral S, T Value)
MCAsmParserExtension * createELFAsmParser()
static SectionKind getBSS()
Generic assembler lexer interface, for use by target specific assembly lexers.
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 R Default(T Value)
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Directive
IDs for all OpenMP directives.
.type _foo, STT_GNU_IFUNC
LLVM_NODISCARD size_t size() const
size - Get the string size.
static SectionKind getThreadData()
const char * getPointer() const
A switch()-like statement whose cases are string literals.
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
The instances of the Type class are immutable: once they are created, they are never changed...
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
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()
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
BlockVerifier::State From
.type _foo, STT_TLS # aka
static SectionKind getReadOnlyWithRel()
MCSymbol * getBeginSymbol()
.type _foo, STT_COMMON # aka
Linkage
Describes symbol linkage.
.type _foo, STT_FUNC # aka
This represents a section on linux, lots of unix variants and some bare metal systems.
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...
LLVM_NODISCARD StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
StringRef - Represent a constant reference to a string, i.e.
Represents a location in source code.
static SectionKind getReadOnly()
static SectionKind getText()
void setBeginSymbol(MCSymbol *Sym)