1 //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/StringRef.h"
10 #include "llvm/ADT/StringSwitch.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCDirectives.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCParser/MCAsmLexer.h"
17 #include "llvm/MC/MCParser/MCAsmParser.h"
18 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
19 #include "llvm/MC/MCSection.h"
20 #include "llvm/MC/MCSectionELF.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/MC/MCSymbolELF.h"
24 #include "llvm/MC/SectionKind.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/MathExtras.h"
27 #include "llvm/Support/SMLoc.h"
36 class ELFAsmParser
: public MCAsmParserExtension
{
37 template<bool (ELFAsmParser::*HandlerMethod
)(StringRef
, SMLoc
)>
38 void addDirectiveHandler(StringRef Directive
) {
39 MCAsmParser::ExtensionDirectiveHandler Handler
= std::make_pair(
40 this, HandleDirective
<ELFAsmParser
, HandlerMethod
>);
42 getParser().addDirectiveHandler(Directive
, Handler
);
45 bool ParseSectionSwitch(StringRef Section
, unsigned Type
, unsigned Flags
,
49 ELFAsmParser() { BracketExpressionsSupported
= true; }
51 void Initialize(MCAsmParser
&Parser
) override
{
52 // Call the base implementation.
53 this->MCAsmParserExtension::Initialize(Parser
);
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");
90 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
91 // the best way for us to get access to it?
92 bool ParseSectionDirectiveData(StringRef
, SMLoc
) {
93 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS
,
94 ELF::SHF_WRITE
| ELF::SHF_ALLOC
,
95 SectionKind::getData());
97 bool ParseSectionDirectiveText(StringRef
, SMLoc
) {
98 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS
,
100 ELF::SHF_ALLOC
, SectionKind::getText());
102 bool ParseSectionDirectiveBSS(StringRef
, SMLoc
) {
103 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS
,
105 ELF::SHF_ALLOC
, SectionKind::getBSS());
107 bool ParseSectionDirectiveRoData(StringRef
, SMLoc
) {
108 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS
,
110 SectionKind::getReadOnly());
112 bool ParseSectionDirectiveTData(StringRef
, SMLoc
) {
113 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS
,
115 ELF::SHF_TLS
| ELF::SHF_WRITE
,
116 SectionKind::getThreadData());
118 bool ParseSectionDirectiveTBSS(StringRef
, SMLoc
) {
119 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS
,
121 ELF::SHF_TLS
| ELF::SHF_WRITE
,
122 SectionKind::getThreadBSS());
124 bool ParseSectionDirectiveDataRel(StringRef
, SMLoc
) {
125 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS
,
126 ELF::SHF_ALLOC
| ELF::SHF_WRITE
,
127 SectionKind::getData());
129 bool ParseSectionDirectiveDataRelRo(StringRef
, SMLoc
) {
130 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS
,
133 SectionKind::getReadOnlyWithRel());
135 bool ParseSectionDirectiveEhFrame(StringRef
, SMLoc
) {
136 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS
,
137 ELF::SHF_ALLOC
| ELF::SHF_WRITE
,
138 SectionKind::getData());
140 bool ParseDirectivePushSection(StringRef
, SMLoc
);
141 bool ParseDirectivePopSection(StringRef
, SMLoc
);
142 bool ParseDirectiveSection(StringRef
, SMLoc
);
143 bool ParseDirectiveSize(StringRef
, SMLoc
);
144 bool ParseDirectivePrevious(StringRef
, SMLoc
);
145 bool ParseDirectiveType(StringRef
, SMLoc
);
146 bool ParseDirectiveIdent(StringRef
, SMLoc
);
147 bool ParseDirectiveSymver(StringRef
, SMLoc
);
148 bool ParseDirectiveVersion(StringRef
, SMLoc
);
149 bool ParseDirectiveWeakref(StringRef
, SMLoc
);
150 bool ParseDirectiveSymbolAttribute(StringRef
, SMLoc
);
151 bool ParseDirectiveSubsection(StringRef
, SMLoc
);
152 bool ParseDirectiveCGProfile(StringRef
, SMLoc
);
155 bool ParseSectionName(StringRef
&SectionName
);
156 bool ParseSectionArguments(bool IsPush
, SMLoc loc
);
157 unsigned parseSunStyleSectionFlags();
158 bool maybeParseSectionType(StringRef
&TypeName
);
159 bool parseMergeSize(int64_t &Size
);
160 bool parseGroup(StringRef
&GroupName
);
161 bool parseMetadataSym(MCSymbolELF
*&Associated
);
162 bool maybeParseUniqueID(int64_t &UniqueID
);
165 } // end anonymous namespace
167 /// ParseDirectiveSymbolAttribute
168 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
169 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive
, SMLoc
) {
170 MCSymbolAttr Attr
= StringSwitch
<MCSymbolAttr
>(Directive
)
171 .Case(".weak", MCSA_Weak
)
172 .Case(".local", MCSA_Local
)
173 .Case(".hidden", MCSA_Hidden
)
174 .Case(".internal", MCSA_Internal
)
175 .Case(".protected", MCSA_Protected
)
176 .Default(MCSA_Invalid
);
177 assert(Attr
!= MCSA_Invalid
&& "unexpected symbol attribute directive!");
178 if (getLexer().isNot(AsmToken::EndOfStatement
)) {
182 if (getParser().parseIdentifier(Name
))
183 return TokError("expected identifier in directive");
185 MCSymbol
*Sym
= getContext().getOrCreateSymbol(Name
);
187 getStreamer().EmitSymbolAttribute(Sym
, Attr
);
189 if (getLexer().is(AsmToken::EndOfStatement
))
192 if (getLexer().isNot(AsmToken::Comma
))
193 return TokError("unexpected token in directive");
202 bool ELFAsmParser::ParseSectionSwitch(StringRef Section
, unsigned Type
,
203 unsigned Flags
, SectionKind Kind
) {
204 const MCExpr
*Subsection
= nullptr;
205 if (getLexer().isNot(AsmToken::EndOfStatement
)) {
206 if (getParser().parseExpression(Subsection
))
211 getStreamer().SwitchSection(getContext().getELFSection(Section
, Type
, Flags
),
217 bool ELFAsmParser::ParseDirectiveSize(StringRef
, SMLoc
) {
219 if (getParser().parseIdentifier(Name
))
220 return TokError("expected identifier in directive");
221 MCSymbolELF
*Sym
= cast
<MCSymbolELF
>(getContext().getOrCreateSymbol(Name
));
223 if (getLexer().isNot(AsmToken::Comma
))
224 return TokError("unexpected token in directive");
228 if (getParser().parseExpression(Expr
))
231 if (getLexer().isNot(AsmToken::EndOfStatement
))
232 return TokError("unexpected token in directive");
235 getStreamer().emitELFSize(Sym
, Expr
);
239 bool ELFAsmParser::ParseSectionName(StringRef
&SectionName
) {
240 // A section name can contain -, so we cannot just use
242 SMLoc FirstLoc
= getLexer().getLoc();
245 if (getLexer().is(AsmToken::String
)) {
246 SectionName
= getTok().getIdentifier();
251 while (!getParser().hasPendingError()) {
252 SMLoc PrevLoc
= getLexer().getLoc();
253 if (getLexer().is(AsmToken::Comma
) ||
254 getLexer().is(AsmToken::EndOfStatement
))
258 if (getLexer().is(AsmToken::String
)) {
259 CurSize
= getTok().getIdentifier().size() + 2;
261 } else if (getLexer().is(AsmToken::Identifier
)) {
262 CurSize
= getTok().getIdentifier().size();
265 CurSize
= getTok().getString().size();
269 SectionName
= StringRef(FirstLoc
.getPointer(), Size
);
271 // Make sure the following token is adjacent.
272 if (PrevLoc
.getPointer() + CurSize
!= getTok().getLoc().getPointer())
281 static unsigned parseSectionFlags(StringRef flagsStr
, bool *UseLastGroup
) {
284 // If a valid numerical value is set for the section flag, use it verbatim
285 if (!flagsStr
.getAsInteger(0, flags
))
288 for (char i
: flagsStr
) {
291 flags
|= ELF::SHF_ALLOC
;
294 flags
|= ELF::SHF_EXCLUDE
;
297 flags
|= ELF::SHF_EXECINSTR
;
300 flags
|= ELF::SHF_WRITE
;
303 flags
|= ELF::SHF_LINK_ORDER
;
306 flags
|= ELF::SHF_MERGE
;
309 flags
|= ELF::SHF_STRINGS
;
312 flags
|= ELF::SHF_TLS
;
315 flags
|= ELF::XCORE_SHF_CP_SECTION
;
318 flags
|= ELF::XCORE_SHF_DP_SECTION
;
321 flags
|= ELF::SHF_ARM_PURECODE
;
324 flags
|= ELF::SHF_HEX_GPREL
;
327 flags
|= ELF::SHF_GROUP
;
330 *UseLastGroup
= true;
340 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
342 while (getLexer().is(AsmToken::Hash
)) {
345 if (!getLexer().is(AsmToken::Identifier
))
348 StringRef flagId
= getTok().getIdentifier();
349 if (flagId
== "alloc")
350 flags
|= ELF::SHF_ALLOC
;
351 else if (flagId
== "execinstr")
352 flags
|= ELF::SHF_EXECINSTR
;
353 else if (flagId
== "write")
354 flags
|= ELF::SHF_WRITE
;
355 else if (flagId
== "tls")
356 flags
|= ELF::SHF_TLS
;
360 Lex(); // Eat the flag.
362 if (!getLexer().is(AsmToken::Comma
))
364 Lex(); // Eat the comma.
370 bool ELFAsmParser::ParseDirectivePushSection(StringRef s
, SMLoc loc
) {
371 getStreamer().PushSection();
373 if (ParseSectionArguments(/*IsPush=*/true, loc
)) {
374 getStreamer().PopSection();
381 bool ELFAsmParser::ParseDirectivePopSection(StringRef
, SMLoc
) {
382 if (!getStreamer().PopSection())
383 return TokError(".popsection without corresponding .pushsection");
387 bool ELFAsmParser::ParseDirectiveSection(StringRef
, SMLoc loc
) {
388 return ParseSectionArguments(/*IsPush=*/false, loc
);
391 bool ELFAsmParser::maybeParseSectionType(StringRef
&TypeName
) {
392 MCAsmLexer
&L
= getLexer();
393 if (L
.isNot(AsmToken::Comma
))
396 if (L
.isNot(AsmToken::At
) && L
.isNot(AsmToken::Percent
) &&
397 L
.isNot(AsmToken::String
)) {
398 if (L
.getAllowAtInIdentifier())
399 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
401 return TokError("expected '%<type>' or \"<type>\"");
403 if (!L
.is(AsmToken::String
))
405 if (L
.is(AsmToken::Integer
)) {
406 TypeName
= getTok().getString();
408 } else if (getParser().parseIdentifier(TypeName
))
409 return TokError("expected identifier in directive");
413 bool ELFAsmParser::parseMergeSize(int64_t &Size
) {
414 if (getLexer().isNot(AsmToken::Comma
))
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
) {
425 MCAsmLexer
&L
= getLexer();
426 if (L
.isNot(AsmToken::Comma
))
427 return TokError("expected group name");
429 if (L
.is(AsmToken::Integer
)) {
430 GroupName
= getTok().getString();
432 } else if (getParser().parseIdentifier(GroupName
)) {
433 return TokError("invalid group name");
435 if (L
.is(AsmToken::Comma
)) {
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
) {
447 MCAsmLexer
&L
= getLexer();
448 if (L
.isNot(AsmToken::Comma
))
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
));
455 if (!Associated
|| !Associated
->isInSection())
456 return TokError("symbol is not in a section: " + Name
);
460 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID
) {
461 MCAsmLexer
&L
= getLexer();
462 if (L
.isNot(AsmToken::Comma
))
466 if (getParser().parseIdentifier(UniqueStr
))
467 return TokError("expected identifier in directive");
468 if (UniqueStr
!= "unique")
469 return TokError("expected 'unique'");
470 if (L
.isNot(AsmToken::Comma
))
471 return TokError("expected commma");
473 if (getParser().parseAbsoluteExpression(UniqueID
))
476 return TokError("unique id must be positive");
477 if (!isUInt
<32>(UniqueID
) || UniqueID
== ~0U)
478 return TokError("unique id is too large");
482 static bool hasPrefix(StringRef SectionName
, StringRef Prefix
) {
483 return SectionName
.startswith(Prefix
) || SectionName
== Prefix
.drop_back();
486 bool ELFAsmParser::ParseSectionArguments(bool IsPush
, SMLoc loc
) {
487 StringRef SectionName
;
489 if (ParseSectionName(SectionName
))
490 return TokError("expected identifier in directive");
496 const MCExpr
*Subsection
= nullptr;
497 bool UseLastGroup
= false;
498 MCSymbolELF
*Associated
= nullptr;
499 int64_t UniqueID
= ~0;
501 // Set the defaults first.
502 if (hasPrefix(SectionName
, ".rodata.") || SectionName
== ".rodata1")
503 Flags
|= ELF::SHF_ALLOC
;
504 else if (SectionName
== ".fini" || SectionName
== ".init" ||
505 hasPrefix(SectionName
, ".text."))
506 Flags
|= ELF::SHF_ALLOC
| ELF::SHF_EXECINSTR
;
507 else if (hasPrefix(SectionName
, ".data.") || SectionName
== ".data1" ||
508 hasPrefix(SectionName
, ".bss.") ||
509 hasPrefix(SectionName
, ".init_array.") ||
510 hasPrefix(SectionName
, ".fini_array.") ||
511 hasPrefix(SectionName
, ".preinit_array."))
512 Flags
|= ELF::SHF_ALLOC
| ELF::SHF_WRITE
;
513 else if (hasPrefix(SectionName
, ".tdata.") ||
514 hasPrefix(SectionName
, ".tbss."))
515 Flags
|= ELF::SHF_ALLOC
| ELF::SHF_WRITE
| ELF::SHF_TLS
;
517 if (getLexer().is(AsmToken::Comma
)) {
520 if (IsPush
&& getLexer().isNot(AsmToken::String
)) {
521 if (getParser().parseExpression(Subsection
))
523 if (getLexer().isNot(AsmToken::Comma
))
530 if (getLexer().isNot(AsmToken::String
)) {
531 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
532 || getLexer().isNot(AsmToken::Hash
))
533 return TokError("expected string in directive");
534 extraFlags
= parseSunStyleSectionFlags();
536 StringRef FlagsStr
= getTok().getStringContents();
538 extraFlags
= parseSectionFlags(FlagsStr
, &UseLastGroup
);
541 if (extraFlags
== -1U)
542 return TokError("unknown flag");
545 bool Mergeable
= Flags
& ELF::SHF_MERGE
;
546 bool Group
= Flags
& ELF::SHF_GROUP
;
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
))
554 MCAsmLexer
&L
= getLexer();
555 if (TypeName
.empty()) {
557 return TokError("Mergeable section must specify the type");
559 return TokError("Group section must specify the type");
560 if (L
.isNot(AsmToken::EndOfStatement
))
561 return TokError("unexpected token in directive");
565 if (parseMergeSize(Size
))
568 if (parseGroup(GroupName
))
570 if (Flags
& ELF::SHF_LINK_ORDER
)
571 if (parseMetadataSym(Associated
))
573 if (maybeParseUniqueID(UniqueID
))
578 if (getLexer().isNot(AsmToken::EndOfStatement
))
579 return TokError("unexpected token in directive");
582 unsigned Type
= ELF::SHT_PROGBITS
;
584 if (TypeName
.empty()) {
585 if (SectionName
.startswith(".note"))
586 Type
= ELF::SHT_NOTE
;
587 else if (hasPrefix(SectionName
, ".init_array."))
588 Type
= ELF::SHT_INIT_ARRAY
;
589 else if (hasPrefix(SectionName
, ".bss."))
590 Type
= ELF::SHT_NOBITS
;
591 else if (hasPrefix(SectionName
, ".tbss."))
592 Type
= ELF::SHT_NOBITS
;
593 else if (hasPrefix(SectionName
, ".fini_array."))
594 Type
= ELF::SHT_FINI_ARRAY
;
595 else if (hasPrefix(SectionName
, ".preinit_array."))
596 Type
= ELF::SHT_PREINIT_ARRAY
;
598 if (TypeName
== "init_array")
599 Type
= ELF::SHT_INIT_ARRAY
;
600 else if (TypeName
== "fini_array")
601 Type
= ELF::SHT_FINI_ARRAY
;
602 else if (TypeName
== "preinit_array")
603 Type
= ELF::SHT_PREINIT_ARRAY
;
604 else if (TypeName
== "nobits")
605 Type
= ELF::SHT_NOBITS
;
606 else if (TypeName
== "progbits")
607 Type
= ELF::SHT_PROGBITS
;
608 else if (TypeName
== "note")
609 Type
= ELF::SHT_NOTE
;
610 else if (TypeName
== "unwind")
611 Type
= ELF::SHT_X86_64_UNWIND
;
612 else if (TypeName
== "llvm_odrtab")
613 Type
= ELF::SHT_LLVM_ODRTAB
;
614 else if (TypeName
== "llvm_linker_options")
615 Type
= ELF::SHT_LLVM_LINKER_OPTIONS
;
616 else if (TypeName
== "llvm_call_graph_profile")
617 Type
= ELF::SHT_LLVM_CALL_GRAPH_PROFILE
;
618 else if (TypeName
== "llvm_dependent_libraries")
619 Type
= ELF::SHT_LLVM_DEPENDENT_LIBRARIES
;
620 else if (TypeName
== "llvm_sympart")
621 Type
= ELF::SHT_LLVM_SYMPART
;
622 else if (TypeName
.getAsInteger(0, Type
))
623 return TokError("unknown section type");
627 MCSectionSubPair CurrentSection
= getStreamer().getCurrentSection();
628 if (const MCSectionELF
*Section
=
629 cast_or_null
<MCSectionELF
>(CurrentSection
.first
))
630 if (const MCSymbol
*Group
= Section
->getGroup()) {
631 GroupName
= Group
->getName();
632 Flags
|= ELF::SHF_GROUP
;
636 MCSection
*ELFSection
=
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");
647 if (!ELFSection
->getBeginSymbol()) {
648 MCSymbol
*SectionStartSymbol
= getContext().createTempSymbol();
649 getStreamer().EmitLabel(SectionStartSymbol
);
650 ELFSection
->setBeginSymbol(SectionStartSymbol
);
658 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName
, SMLoc
) {
659 MCSectionSubPair PreviousSection
= getStreamer().getPreviousSection();
660 if (PreviousSection
.first
== nullptr)
661 return TokError(".previous without corresponding .section");
662 getStreamer().SwitchSection(PreviousSection
.first
, PreviousSection
.second
);
667 static MCSymbolAttr
MCAttrForString(StringRef Type
) {
668 return StringSwitch
<MCSymbolAttr
>(Type
)
669 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction
)
670 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject
)
671 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS
)
672 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon
)
673 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType
)
674 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
675 MCSA_ELF_TypeIndFunction
)
676 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject
)
677 .Default(MCSA_Invalid
);
680 /// ParseDirectiveELFType
681 /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
682 /// ::= .type identifier , #attribute
683 /// ::= .type identifier , @attribute
684 /// ::= .type identifier , %attribute
685 /// ::= .type identifier , "attribute"
686 bool ELFAsmParser::ParseDirectiveType(StringRef
, SMLoc
) {
688 if (getParser().parseIdentifier(Name
))
689 return TokError("expected identifier in directive");
691 // Handle the identifier as the key symbol.
692 MCSymbol
*Sym
= getContext().getOrCreateSymbol(Name
);
694 // NOTE the comma is optional in all cases. It is only documented as being
695 // optional in the first case, however, GAS will silently treat the comma as
696 // optional in all cases. Furthermore, although the documentation states that
697 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
698 // accepts both the upper case name as well as the lower case aliases.
699 if (getLexer().is(AsmToken::Comma
))
702 if (getLexer().isNot(AsmToken::Identifier
) &&
703 getLexer().isNot(AsmToken::Hash
) &&
704 getLexer().isNot(AsmToken::Percent
) &&
705 getLexer().isNot(AsmToken::String
)) {
706 if (!getLexer().getAllowAtInIdentifier())
707 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
708 "'%<type>' or \"<type>\"");
709 else if (getLexer().isNot(AsmToken::At
))
710 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
711 "'%<type>' or \"<type>\"");
714 if (getLexer().isNot(AsmToken::String
) &&
715 getLexer().isNot(AsmToken::Identifier
))
718 SMLoc TypeLoc
= getLexer().getLoc();
721 if (getParser().parseIdentifier(Type
))
722 return TokError("expected symbol type in directive");
724 MCSymbolAttr Attr
= MCAttrForString(Type
);
725 if (Attr
== MCSA_Invalid
)
726 return Error(TypeLoc
, "unsupported attribute in '.type' directive");
728 if (getLexer().isNot(AsmToken::EndOfStatement
))
729 return TokError("unexpected token in '.type' directive");
732 getStreamer().EmitSymbolAttribute(Sym
, Attr
);
737 /// ParseDirectiveIdent
738 /// ::= .ident string
739 bool ELFAsmParser::ParseDirectiveIdent(StringRef
, SMLoc
) {
740 if (getLexer().isNot(AsmToken::String
))
741 return TokError("unexpected token in '.ident' directive");
743 StringRef Data
= getTok().getIdentifier();
747 if (getLexer().isNot(AsmToken::EndOfStatement
))
748 return TokError("unexpected token in '.ident' directive");
751 getStreamer().EmitIdent(Data
);
755 /// ParseDirectiveSymver
756 /// ::= .symver foo, bar2@zed
757 bool ELFAsmParser::ParseDirectiveSymver(StringRef
, SMLoc
) {
759 if (getParser().parseIdentifier(Name
))
760 return TokError("expected identifier in directive");
762 if (getLexer().isNot(AsmToken::Comma
))
763 return TokError("expected a comma");
765 // ARM assembly uses @ for a comment...
766 // except when parsing the second parameter of the .symver directive.
767 // Force the next symbol to allow @ in the identifier, which is
768 // required for this directive and then reset it to its initial state.
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");
778 if (AliasName
.find('@') == StringRef::npos
)
779 return TokError("expected a '@' in the name");
781 MCSymbol
*Sym
= getContext().getOrCreateSymbol(Name
);
782 getStreamer().emitELFSymverDirective(AliasName
, Sym
);
786 /// ParseDirectiveVersion
787 /// ::= .version string
788 bool ELFAsmParser::ParseDirectiveVersion(StringRef
, SMLoc
) {
789 if (getLexer().isNot(AsmToken::String
))
790 return TokError("unexpected token in '.version' directive");
792 StringRef Data
= getTok().getIdentifier();
796 MCSection
*Note
= getContext().getELFSection(".note", ELF::SHT_NOTE
, 0);
798 getStreamer().PushSection();
799 getStreamer().SwitchSection(Note
);
800 getStreamer().EmitIntValue(Data
.size()+1, 4); // namesz.
801 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
802 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
803 getStreamer().EmitBytes(Data
); // name.
804 getStreamer().EmitIntValue(0, 1); // terminate the string.
805 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
806 getStreamer().PopSection();
810 /// ParseDirectiveWeakref
811 /// ::= .weakref foo, bar
812 bool ELFAsmParser::ParseDirectiveWeakref(StringRef
, SMLoc
) {
813 // FIXME: Share code with the other alias building directives.
816 if (getParser().parseIdentifier(AliasName
))
817 return TokError("expected identifier in directive");
819 if (getLexer().isNot(AsmToken::Comma
))
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
);
836 bool ELFAsmParser::ParseDirectiveSubsection(StringRef
, SMLoc
) {
837 const MCExpr
*Subsection
= nullptr;
838 if (getLexer().isNot(AsmToken::EndOfStatement
)) {
839 if (getParser().parseExpression(Subsection
))
843 if (getLexer().isNot(AsmToken::EndOfStatement
))
844 return TokError("unexpected token in directive");
848 getStreamer().SubSection(Subsection
);
852 /// ParseDirectiveCGProfile
853 /// ::= .cg_profile identifier, identifier, <number>
854 bool ELFAsmParser::ParseDirectiveCGProfile(StringRef
, SMLoc
) {
856 SMLoc FromLoc
= getLexer().getLoc();
857 if (getParser().parseIdentifier(From
))
858 return TokError("expected identifier in directive");
860 if (getLexer().isNot(AsmToken::Comma
))
861 return TokError("expected a comma");
865 SMLoc ToLoc
= getLexer().getLoc();
866 if (getParser().parseIdentifier(To
))
867 return TokError("expected identifier in directive");
869 if (getLexer().isNot(AsmToken::Comma
))
870 return TokError("expected a comma");
874 if (getParser().parseIntToken(
875 Count
, "expected integer count in '.cg_profile' directive"))
878 if (getLexer().isNot(AsmToken::EndOfStatement
))
879 return TokError("unexpected token in directive");
881 MCSymbol
*FromSym
= getContext().getOrCreateSymbol(From
);
882 MCSymbol
*ToSym
= getContext().getOrCreateSymbol(To
);
884 getStreamer().emitCGProfileEntry(
885 MCSymbolRefExpr::create(FromSym
, MCSymbolRefExpr::VK_None
, getContext(),
887 MCSymbolRefExpr::create(ToSym
, MCSymbolRefExpr::VK_None
, getContext(),
895 MCAsmParserExtension
*createELFAsmParser() {
896 return new ELFAsmParser
;
899 } // end namespace llvm