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");
155 bool ParseSectionArguments(
bool IsPush,
SMLoc loc);
156 unsigned parseSunStyleSectionFlags();
158 bool parseMergeSize(int64_t &
Size);
161 bool maybeParseUniqueID(int64_t &UniqueID);
168 bool ELFAsmParser::ParseDirectiveSymbolAttribute(
StringRef Directive,
SMLoc) {
181 if (getParser().parseIdentifier(Name))
182 return TokError(
"expected identifier in directive");
184 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
186 getStreamer().EmitSymbolAttribute(Sym, Attr);
192 return TokError(
"unexpected token in directive");
203 const MCExpr *Subsection =
nullptr;
205 if (getParser().parseExpression(Subsection))
210 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
218 if (getParser().parseIdentifier(Name))
219 return TokError(
"expected identifier in directive");
220 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
223 return TokError(
"unexpected token in directive");
227 if (getParser().parseExpression(Expr))
231 return TokError(
"unexpected token in directive");
234 getStreamer().emitELFSize(Sym, Expr);
241 SMLoc FirstLoc = getLexer().getLoc();
245 SectionName = getTok().getIdentifier();
251 SMLoc PrevLoc = getLexer().getLoc();
258 CurSize = getTok().getIdentifier().size() + 2;
261 CurSize = getTok().getIdentifier().size();
264 CurSize = getTok().getString().size();
271 if (PrevLoc.
getPointer() + CurSize != getTok().getLoc().getPointer())
287 for (
char i : flagsStr) {
326 *UseLastGroup =
true;
336 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
344 StringRef flagId = getTok().getIdentifier();
345 if (flagId ==
"alloc")
347 else if (flagId ==
"execinstr")
349 else if (flagId ==
"write")
351 else if (flagId ==
"tls")
366 bool ELFAsmParser::ParseDirectivePushSection(
StringRef s,
SMLoc loc) {
367 getStreamer().PushSection();
369 if (ParseSectionArguments(
true, loc)) {
370 getStreamer().PopSection();
378 if (!getStreamer().PopSection())
379 return TokError(
".popsection without corresponding .pushsection");
385 return ParseSectionArguments(
false, loc);
396 return TokError(
"expected '@<type>', '%<type>' or \"<type>\"");
398 return TokError(
"expected '%<type>' or \"<type>\"");
403 TypeName = getTok().getString();
405 }
else if (getParser().parseIdentifier(TypeName))
406 return TokError(
"expected identifier in directive");
410 bool ELFAsmParser::parseMergeSize(int64_t &Size) {
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) {
424 return TokError(
"expected group name");
426 if (getParser().parseIdentifier(GroupName))
431 if (getParser().parseIdentifier(Linkage))
433 if (Linkage !=
"comdat")
434 return TokError(
"Linkage must be 'comdat'");
439 bool ELFAsmParser::parseMetadataSym(
MCSymbolELF *&Associated) {
442 return TokError(
"expected metadata symbol");
445 if (getParser().parseIdentifier(Name))
447 Associated = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
449 return TokError(
"symbol is not in a section: " + Name);
453 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
459 if (getParser().parseIdentifier(UniqueStr))
460 return TokError(
"expected identifier in directive");
461 if (UniqueStr !=
"unique")
462 return TokError(
"expected 'unique'");
464 return TokError(
"expected commma");
466 if (getParser().parseAbsoluteExpression(UniqueID))
469 return TokError(
"unique id must be positive");
471 return TokError(
"unique id is too large");
479 bool ELFAsmParser::ParseSectionArguments(
bool IsPush,
SMLoc loc) {
482 if (ParseSectionName(SectionName))
483 return TokError(
"expected identifier in directive");
489 const MCExpr *Subsection =
nullptr;
490 bool UseLastGroup =
false;
493 int64_t UniqueID = ~0;
496 if (
hasPrefix(SectionName,
".rodata.") || SectionName ==
".rodata1")
498 if (SectionName ==
".fini" || SectionName ==
".init" ||
501 if (
hasPrefix(SectionName,
".data.") || SectionName ==
".data1" ||
503 hasPrefix(SectionName,
".init_array.") ||
504 hasPrefix(SectionName,
".fini_array.") ||
505 hasPrefix(SectionName,
".preinit_array."))
515 if (getParser().parseExpression(Subsection))
525 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
527 return TokError(
"expected string in directive");
528 extraFlags = parseSunStyleSectionFlags();
530 StringRef FlagsStr = getTok().getStringContents();
535 if (extraFlags == -1U)
536 return TokError(
"unknown flag");
541 if (Group && UseLastGroup)
542 return TokError(
"Section cannot specifiy a group name while also acting "
543 "as a member of the last group");
545 if (maybeParseSectionType(TypeName))
549 if (TypeName.
empty()) {
551 return TokError(
"Mergeable section must specify the type");
553 return TokError(
"Group section must specify the type");
555 return TokError(
"unexpected token in directive");
559 if (parseMergeSize(Size))
562 if (parseGroup(GroupName))
565 if (parseMetadataSym(Associated))
567 if (maybeParseUniqueID(UniqueID))
573 return TokError(
"unexpected token in directive");
578 if (TypeName.
empty()) {
581 else if (
hasPrefix(SectionName,
".init_array."))
583 else if (
hasPrefix(SectionName,
".bss."))
585 else if (
hasPrefix(SectionName,
".tbss."))
587 else if (
hasPrefix(SectionName,
".fini_array."))
589 else if (
hasPrefix(SectionName,
".preinit_array."))
592 if (TypeName ==
"init_array")
594 else if (TypeName ==
"fini_array")
596 else if (TypeName ==
"preinit_array")
598 else if (TypeName ==
"nobits")
600 else if (TypeName ==
"progbits")
602 else if (TypeName ==
"note")
604 else if (TypeName ==
"unwind")
606 else if (TypeName ==
"llvm_odrtab")
609 return TokError(
"unknown section type");
615 cast_or_null<MCSectionELF>(CurrentSection.first))
616 if (
const MCSymbol *Group = Section->getGroup()) {
617 GroupName = Group->getName();
623 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
624 UniqueID, Associated);
625 getStreamer().SwitchSection(ELFSection, Subsection);
627 if (getContext().getGenDwarfForAssembly()) {
628 bool InsertResult = getContext().addGenDwarfSection(ELFSection);
630 if (getContext().getDwarfVersion() <= 2)
631 Warning(loc,
"DWARF2 only supports one section per compilation unit");
634 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
635 getStreamer().EmitLabel(SectionStartSymbol);
646 if (PreviousSection.first ==
nullptr)
647 return TokError(
".previous without corresponding .section");
648 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
660 .
Cases(
"STT_GNU_IFUNC",
"gnu_indirect_function",
674 if (getParser().parseIdentifier(Name))
675 return TokError(
"expected identifier in directive");
678 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
692 if (!getLexer().getAllowAtInIdentifier())
693 return TokError(
"expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
694 "'%<type>' or \"<type>\"");
696 return TokError(
"expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
697 "'%<type>' or \"<type>\"");
704 SMLoc TypeLoc = getLexer().getLoc();
707 if (getParser().parseIdentifier(Type))
708 return TokError(
"expected symbol type in directive");
712 return Error(TypeLoc,
"unsupported attribute in '.type' directive");
715 return TokError(
"unexpected token in '.type' directive");
718 getStreamer().EmitSymbolAttribute(Sym, Attr);
727 return TokError(
"unexpected token in '.ident' directive");
734 return TokError(
"unexpected token in '.ident' directive");
737 getStreamer().EmitIdent(Data);
745 if (getParser().parseIdentifier(Name))
746 return TokError(
"expected identifier in directive");
749 return TokError(
"expected a comma");
755 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
756 getLexer().setAllowAtInIdentifier(
true);
758 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
761 if (getParser().parseIdentifier(AliasName))
762 return TokError(
"expected identifier in directive");
765 return TokError(
"expected a '@' in the name");
767 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
768 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
771 getStreamer().EmitAssignment(Alias, Value);
772 getStreamer().emitELFSymverDirective(Alias, Sym);
780 return TokError(
"unexpected token in '.version' directive");
782 StringRef Data = getTok().getIdentifier();
788 getStreamer().PushSection();
789 getStreamer().SwitchSection(Note);
790 getStreamer().EmitIntValue(Data.
size()+1, 4);
791 getStreamer().EmitIntValue(0, 4);
792 getStreamer().EmitIntValue(1, 4);
793 getStreamer().EmitBytes(Data);
794 getStreamer().EmitIntValue(0, 1);
795 getStreamer().EmitValueToAlignment(4);
796 getStreamer().PopSection();
806 if (getParser().parseIdentifier(AliasName))
807 return TokError(
"expected identifier in directive");
810 return TokError(
"expected a comma");
815 if (getParser().parseIdentifier(Name))
816 return TokError(
"expected identifier in directive");
818 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
820 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
822 getStreamer().EmitWeakReference(Alias, Sym);
827 const MCExpr *Subsection =
nullptr;
829 if (getParser().parseExpression(Subsection))
834 return TokError(
"unexpected token in directive");
838 getStreamer().SubSection(Subsection);
845 return new ELFAsmParser;
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.
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)
const char * getPointer() const
.type _foo, STT_OBJECT # aka
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.
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.
static bool hasPrefix(StringRef SectionName, StringRef Prefix)
Base class for the full range of assembler expressions which are needed for parsing.
LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(const T &Value) const
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(const char(&S)[N], const T &Value)
bool isNot(AsmToken::TokenKind K) const
Check if the current token has kind K.
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
static SectionKind getThreadData()
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
A switch()-like statement whose cases are string literals.
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.
The instances of the Type class are immutable: once they are created, they are never changed...
SectionKind - This is a simple POD value that classifies the properties of a section.
bool getAllowAtInIdentifier()
All sections with the "d" flag are grouped together by the linker to form the data section and the dp...
bool isInSection(bool SetUsed=true) const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
bool is(AsmToken::TokenKind K) const
Check if the current token has kind K.
static SectionKind getThreadBSS()
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
.type _foo, STT_TLS # aka
static SectionKind getReadOnlyWithRel()
MCSymbol * getBeginSymbol()
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
.type _foo, STT_COMMON # aka
static std::vector< std::string > Flags
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Cases(const char(&S0)[N0], const char(&S1)[N1], const T &Value)
.type _foo, STT_FUNC # aka
This represents a section on linux, lots of unix variants and some bare metal systems.
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
All sections with the "c" flag are grouped together by the linker to form the constant pool and the c...
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
std::string DirName(const std::string &FileName)
LLVM Value Representation.
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
StringRef - Represent a constant reference to a string, i.e.
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.
Represents a location in source code.
static SectionKind getReadOnly()
static SectionKind getText()
void setBeginSymbol(MCSymbol *Sym)