35 template<
bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
38 this, HandleDirective<ELFAsmParser, HandlerMethod>);
40 getParser().addDirectiveHandler(
Directive, Handler);
43 bool ParseSectionSwitch(
StringRef Section,
unsigned Type,
unsigned Flags,
47 ELFAsmParser() { BracketExpressionsSupported =
true; }
53 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(
".data");
54 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(
".text");
55 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(
".bss");
56 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(
".rodata");
57 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(
".tdata");
58 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(
".tbss");
60 &ELFAsmParser::ParseSectionDirectiveDataRel>(
".data.rel");
62 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(
".data.rel.ro");
64 &ELFAsmParser::ParseSectionDirectiveEhFrame>(
".eh_frame");
65 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(
".section");
67 &ELFAsmParser::ParseDirectivePushSection>(
".pushsection");
68 addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(
".popsection");
69 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(
".size");
70 addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(
".previous");
71 addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(
".type");
72 addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(
".ident");
73 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(
".symver");
74 addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(
".version");
75 addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(
".weakref");
76 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(
".weak");
77 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(
".local");
79 &ELFAsmParser::ParseDirectiveSymbolAttribute>(
".protected");
81 &ELFAsmParser::ParseDirectiveSymbolAttribute>(
".internal");
83 &ELFAsmParser::ParseDirectiveSymbolAttribute>(
".hidden");
84 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(
".subsection");
85 addDirectiveHandler<&ELFAsmParser::ParseDirectiveCGProfile>(
".cg_profile");
154 bool ParseSectionArguments(
bool IsPush,
SMLoc loc);
155 unsigned parseSunStyleSectionFlags();
157 bool parseMergeSize(int64_t &Size);
158 bool parseGroup(
StringRef &GroupName,
bool &IsComdat);
160 bool maybeParseUniqueID(int64_t &UniqueID);
180 if (getParser().parseIdentifier(Name))
181 return TokError(
"expected identifier");
183 if (getParser().discardLTOSymbol(Name)) {
189 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
191 getStreamer().emitSymbolAttribute(Sym, Attr);
197 return TokError(
"expected comma");
206 bool ELFAsmParser::ParseSectionSwitch(
StringRef Section,
unsigned Type,
208 const MCExpr *Subsection =
nullptr;
210 if (getParser().parseExpression(Subsection))
215 getStreamer().switchSection(getContext().getELFSection(Section,
Type, Flags),
223 if (getParser().parseIdentifier(Name))
224 return TokError(
"expected identifier");
225 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
228 return TokError(
"expected comma");
232 if (getParser().parseExpression(Expr))
236 return TokError(
"unexpected token");
239 getStreamer().emitELFSize(Sym, Expr);
246 SMLoc FirstLoc = getLexer().getLoc();
255 while (!getParser().hasPendingError()) {
256 SMLoc PrevLoc = getLexer().getLoc();
263 CurSize = getTok().getIdentifier().size() + 2;
266 CurSize = getTok().getIdentifier().size();
269 CurSize = getTok().getString().size();
276 if (PrevLoc.
getPointer() + CurSize != getTok().getLoc().getPointer())
286 bool *UseLastGroup) {
293 for (
char i : flagsStr) {
335 if (TT.isOSSolaris())
341 *UseLastGroup =
true;
351 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
359 StringRef flagId = getTok().getIdentifier();
360 if (flagId ==
"alloc")
362 else if (flagId ==
"execinstr")
364 else if (flagId ==
"write")
366 else if (flagId ==
"tls")
382 getStreamer().pushSection();
384 if (ParseSectionArguments(
true, loc)) {
385 getStreamer().popSection();
393 if (!getStreamer().popSection())
394 return TokError(
".popsection without corresponding .pushsection");
399 return ParseSectionArguments(
false, loc);
410 return TokError(
"expected '@<type>', '%<type>' or \"<type>\"");
412 return TokError(
"expected '%<type>' or \"<type>\"");
419 }
else if (getParser().parseIdentifier(
TypeName))
420 return TokError(
"expected identifier");
424 bool ELFAsmParser::parseMergeSize(int64_t &Size) {
426 return TokError(
"expected the entry size");
428 if (getParser().parseAbsoluteExpression(Size))
431 return TokError(
"entry size must be positive");
435 bool ELFAsmParser::parseGroup(
StringRef &GroupName,
bool &IsComdat) {
438 return TokError(
"expected group name");
441 GroupName = getTok().getString();
443 }
else if (getParser().parseIdentifier(GroupName)) {
444 return TokError(
"invalid group name");
449 if (getParser().parseIdentifier(Linkage))
450 return TokError(
"invalid linkage");
451 if (Linkage !=
"comdat")
452 return TokError(
"Linkage must be 'comdat'");
460 bool ELFAsmParser::parseLinkedToSym(
MCSymbolELF *&LinkedToSym) {
463 return TokError(
"expected linked-to symbol");
467 if (getParser().parseIdentifier(Name)) {
468 if (getParser().getTok().getString() ==
"0") {
470 LinkedToSym =
nullptr;
473 return TokError(
"invalid linked-to symbol");
475 LinkedToSym = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
477 return Error(StartLoc,
"linked-to symbol is not in a section: " + Name);
481 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
487 if (getParser().parseIdentifier(UniqueStr))
488 return TokError(
"expected identifier");
489 if (UniqueStr !=
"unique")
490 return TokError(
"expected 'unique'");
492 return TokError(
"expected commma");
494 if (getParser().parseAbsoluteExpression(UniqueID))
497 return TokError(
"unique id must be positive");
498 if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
499 return TokError(
"unique id is too large");
525 bool ELFAsmParser::ParseSectionArguments(
bool IsPush,
SMLoc loc) {
529 return TokError(
"expected identifier");
534 bool IsComdat =
false;
536 unsigned extraFlags = 0;
537 const MCExpr *Subsection =
nullptr;
538 bool UseLastGroup =
false;
540 int64_t UniqueID = ~0;
561 if (getParser().parseExpression(Subsection))
570 return TokError(
"expected string");
571 extraFlags = parseSunStyleSectionFlags();
573 StringRef FlagsStr = getTok().getStringContents();
579 if (extraFlags == -1U)
580 return TokError(
"unknown flag");
585 if (Group && UseLastGroup)
586 return TokError(
"Section cannot specifiy a group name while also acting "
587 "as a member of the last group");
589 if (maybeParseSectionType(
TypeName))
595 return TokError(
"Mergeable section must specify the type");
597 return TokError(
"Group section must specify the type");
599 return TokError(
"expected end of directive");
603 if (parseMergeSize(Size))
606 if (parseGroup(GroupName, IsComdat))
609 if (parseLinkedToSym(LinkedToSym))
611 if (maybeParseUniqueID(UniqueID))
617 return TokError(
"expected end of directive");
640 else if (
TypeName ==
"preinit_array")
652 else if (
TypeName ==
"llvm_linker_options")
654 else if (
TypeName ==
"llvm_call_graph_profile")
656 else if (
TypeName ==
"llvm_dependent_libraries")
658 else if (
TypeName ==
"llvm_sympart")
660 else if (
TypeName ==
"llvm_bb_addr_map")
662 else if (
TypeName ==
"llvm_offloading")
665 return TokError(
"unknown section type");
671 cast_or_null<MCSectionELF>(CurrentSection.first))
673 GroupName = Group->getName();
674 IsComdat =
Section->isComdat();
681 IsComdat, UniqueID, LinkedToSym);
682 getStreamer().switchSection(Section, Subsection);
690 utohexstr(
Section->getType()));
691 if ((extraFlags || Size || !
TypeName.empty()) &&
Section->getFlags() != Flags)
693 utohexstr(
Section->getFlags()));
694 if ((extraFlags || Size || !
TypeName.empty()) &&
695 Section->getEntrySize() != Size)
699 if (getContext().getGenDwarfForAssembly() &&
702 bool InsertResult = getContext().addGenDwarfSection(Section);
705 Warning(loc,
"DWARF2 only supports one section per compilation unit");
707 if (!
Section->getBeginSymbol()) {
708 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
709 getStreamer().emitLabel(SectionStartSymbol);
710 Section->setBeginSymbol(SectionStartSymbol);
718 bool ELFAsmParser::ParseDirectivePrevious(
StringRef DirName,
SMLoc) {
720 if (PreviousSection.first ==
nullptr)
721 return TokError(
".previous without corresponding .section");
722 getStreamer().switchSection(PreviousSection.first, PreviousSection.second);
734 .
Cases(
"STT_GNU_IFUNC",
"gnu_indirect_function",
748 if (getParser().parseIdentifier(Name))
749 return TokError(
"expected identifier");
752 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
766 if (!getLexer().getAllowAtInIdentifier())
767 return TokError(
"expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
768 "'%<type>' or \"<type>\"");
770 return TokError(
"expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
771 "'%<type>' or \"<type>\"");
778 SMLoc TypeLoc = getLexer().getLoc();
781 if (getParser().parseIdentifier(
Type))
782 return TokError(
"expected symbol type");
786 return Error(TypeLoc,
"unsupported attribute");
789 return TokError(
"expected end of directive");
792 getStreamer().emitSymbolAttribute(Sym, Attr);
801 return TokError(
"expected string");
808 return TokError(
"expected end of directive");
811 getStreamer().emitIdent(
Data);
819 if (getParser().parseIdentifier(OriginalName))
820 return TokError(
"expected identifier");
823 return TokError(
"expected a comma");
829 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
830 getLexer().setAllowAtInIdentifier(
true);
832 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
834 if (getParser().parseIdentifier(Name))
835 return TokError(
"expected identifier");
837 if (!
Name.contains(
'@'))
838 return TokError(
"expected a '@' in the name");
839 bool KeepOriginalSym = !
Name.contains(
"@@@");
841 if (getParser().parseIdentifier(Action) || Action !=
"remove")
842 return TokError(
"expected 'remove'");
843 KeepOriginalSym =
false;
847 getStreamer().emitELFSymverDirective(
848 getContext().getOrCreateSymbol(OriginalName), Name, KeepOriginalSym);
856 return TokError(
"expected string");
864 getStreamer().pushSection();
865 getStreamer().switchSection(Note);
866 getStreamer().emitInt32(
Data.size() + 1);
867 getStreamer().emitInt32(0);
868 getStreamer().emitInt32(1);
869 getStreamer().emitBytes(
Data);
870 getStreamer().emitInt8(0);
871 getStreamer().emitValueToAlignment(
Align(4));
872 getStreamer().popSection();
882 if (getParser().parseIdentifier(AliasName))
883 return TokError(
"expected identifier");
886 return TokError(
"expected a comma");
891 if (getParser().parseIdentifier(Name))
892 return TokError(
"expected identifier");
894 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
896 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
898 getStreamer().emitWeakReference(Alias, Sym);
903 const MCExpr *Subsection =
nullptr;
905 if (getParser().parseExpression(Subsection))
910 return TokError(
"expected end of directive");
914 getStreamer().subSection(Subsection);
925 return new ELFAsmParser;