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
, bool &IsComdat
);
161 bool parseLinkedToSym(MCSymbolELF
*&LinkedToSym
);
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 if (getParser().discardLTOSymbol(Name
)) {
186 if (getLexer().is(AsmToken::EndOfStatement
))
191 MCSymbol
*Sym
= getContext().getOrCreateSymbol(Name
);
193 getStreamer().emitSymbolAttribute(Sym
, Attr
);
195 if (getLexer().is(AsmToken::EndOfStatement
))
198 if (getLexer().isNot(AsmToken::Comma
))
199 return TokError("unexpected token in directive");
208 bool ELFAsmParser::ParseSectionSwitch(StringRef Section
, unsigned Type
,
209 unsigned Flags
, SectionKind Kind
) {
210 const MCExpr
*Subsection
= nullptr;
211 if (getLexer().isNot(AsmToken::EndOfStatement
)) {
212 if (getParser().parseExpression(Subsection
))
217 getStreamer().SwitchSection(getContext().getELFSection(Section
, Type
, Flags
),
223 bool ELFAsmParser::ParseDirectiveSize(StringRef
, SMLoc
) {
225 if (getParser().parseIdentifier(Name
))
226 return TokError("expected identifier in directive");
227 MCSymbolELF
*Sym
= cast
<MCSymbolELF
>(getContext().getOrCreateSymbol(Name
));
229 if (getLexer().isNot(AsmToken::Comma
))
230 return TokError("unexpected token in directive");
234 if (getParser().parseExpression(Expr
))
237 if (getLexer().isNot(AsmToken::EndOfStatement
))
238 return TokError("unexpected token in directive");
241 getStreamer().emitELFSize(Sym
, Expr
);
245 bool ELFAsmParser::ParseSectionName(StringRef
&SectionName
) {
246 // A section name can contain -, so we cannot just use
248 SMLoc FirstLoc
= getLexer().getLoc();
251 if (getLexer().is(AsmToken::String
)) {
252 SectionName
= getTok().getIdentifier();
257 while (!getParser().hasPendingError()) {
258 SMLoc PrevLoc
= getLexer().getLoc();
259 if (getLexer().is(AsmToken::Comma
) ||
260 getLexer().is(AsmToken::EndOfStatement
))
264 if (getLexer().is(AsmToken::String
)) {
265 CurSize
= getTok().getIdentifier().size() + 2;
267 } else if (getLexer().is(AsmToken::Identifier
)) {
268 CurSize
= getTok().getIdentifier().size();
271 CurSize
= getTok().getString().size();
275 SectionName
= StringRef(FirstLoc
.getPointer(), Size
);
277 // Make sure the following token is adjacent.
278 if (PrevLoc
.getPointer() + CurSize
!= getTok().getLoc().getPointer())
287 static unsigned parseSectionFlags(StringRef flagsStr
, bool *UseLastGroup
) {
290 // If a valid numerical value is set for the section flag, use it verbatim
291 if (!flagsStr
.getAsInteger(0, flags
))
294 for (char i
: flagsStr
) {
297 flags
|= ELF::SHF_ALLOC
;
300 flags
|= ELF::SHF_EXCLUDE
;
303 flags
|= ELF::SHF_EXECINSTR
;
306 flags
|= ELF::SHF_WRITE
;
309 flags
|= ELF::SHF_LINK_ORDER
;
312 flags
|= ELF::SHF_MERGE
;
315 flags
|= ELF::SHF_STRINGS
;
318 flags
|= ELF::SHF_TLS
;
321 flags
|= ELF::XCORE_SHF_CP_SECTION
;
324 flags
|= ELF::XCORE_SHF_DP_SECTION
;
327 flags
|= ELF::SHF_ARM_PURECODE
;
330 flags
|= ELF::SHF_HEX_GPREL
;
333 flags
|= ELF::SHF_GROUP
;
336 flags
|= ELF::SHF_GNU_RETAIN
;
339 *UseLastGroup
= true;
349 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
351 while (getLexer().is(AsmToken::Hash
)) {
354 if (!getLexer().is(AsmToken::Identifier
))
357 StringRef flagId
= getTok().getIdentifier();
358 if (flagId
== "alloc")
359 flags
|= ELF::SHF_ALLOC
;
360 else if (flagId
== "execinstr")
361 flags
|= ELF::SHF_EXECINSTR
;
362 else if (flagId
== "write")
363 flags
|= ELF::SHF_WRITE
;
364 else if (flagId
== "tls")
365 flags
|= ELF::SHF_TLS
;
369 Lex(); // Eat the flag.
371 if (!getLexer().is(AsmToken::Comma
))
373 Lex(); // Eat the comma.
379 bool ELFAsmParser::ParseDirectivePushSection(StringRef s
, SMLoc loc
) {
380 getStreamer().PushSection();
382 if (ParseSectionArguments(/*IsPush=*/true, loc
)) {
383 getStreamer().PopSection();
390 bool ELFAsmParser::ParseDirectivePopSection(StringRef
, SMLoc
) {
391 if (!getStreamer().PopSection())
392 return TokError(".popsection without corresponding .pushsection");
396 bool ELFAsmParser::ParseDirectiveSection(StringRef
, SMLoc loc
) {
397 return ParseSectionArguments(/*IsPush=*/false, loc
);
400 bool ELFAsmParser::maybeParseSectionType(StringRef
&TypeName
) {
401 MCAsmLexer
&L
= getLexer();
402 if (L
.isNot(AsmToken::Comma
))
405 if (L
.isNot(AsmToken::At
) && L
.isNot(AsmToken::Percent
) &&
406 L
.isNot(AsmToken::String
)) {
407 if (L
.getAllowAtInIdentifier())
408 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
410 return TokError("expected '%<type>' or \"<type>\"");
412 if (!L
.is(AsmToken::String
))
414 if (L
.is(AsmToken::Integer
)) {
415 TypeName
= getTok().getString();
417 } else if (getParser().parseIdentifier(TypeName
))
418 return TokError("expected identifier in directive");
422 bool ELFAsmParser::parseMergeSize(int64_t &Size
) {
423 if (getLexer().isNot(AsmToken::Comma
))
424 return TokError("expected the entry size");
426 if (getParser().parseAbsoluteExpression(Size
))
429 return TokError("entry size must be positive");
433 bool ELFAsmParser::parseGroup(StringRef
&GroupName
, bool &IsComdat
) {
434 MCAsmLexer
&L
= getLexer();
435 if (L
.isNot(AsmToken::Comma
))
436 return TokError("expected group name");
438 if (L
.is(AsmToken::Integer
)) {
439 GroupName
= getTok().getString();
441 } else if (getParser().parseIdentifier(GroupName
)) {
442 return TokError("invalid group name");
444 if (L
.is(AsmToken::Comma
)) {
447 if (getParser().parseIdentifier(Linkage
))
448 return TokError("invalid linkage");
449 if (Linkage
!= "comdat")
450 return TokError("Linkage must be 'comdat'");
458 bool ELFAsmParser::parseLinkedToSym(MCSymbolELF
*&LinkedToSym
) {
459 MCAsmLexer
&L
= getLexer();
460 if (L
.isNot(AsmToken::Comma
))
461 return TokError("expected linked-to symbol");
464 SMLoc StartLoc
= L
.getLoc();
465 if (getParser().parseIdentifier(Name
)) {
466 if (getParser().getTok().getString() == "0") {
468 LinkedToSym
= nullptr;
471 return TokError("invalid linked-to symbol");
473 LinkedToSym
= dyn_cast_or_null
<MCSymbolELF
>(getContext().lookupSymbol(Name
));
474 if (!LinkedToSym
|| !LinkedToSym
->isInSection())
475 return Error(StartLoc
, "linked-to symbol is not in a section: " + Name
);
479 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID
) {
480 MCAsmLexer
&L
= getLexer();
481 if (L
.isNot(AsmToken::Comma
))
485 if (getParser().parseIdentifier(UniqueStr
))
486 return TokError("expected identifier in directive");
487 if (UniqueStr
!= "unique")
488 return TokError("expected 'unique'");
489 if (L
.isNot(AsmToken::Comma
))
490 return TokError("expected commma");
492 if (getParser().parseAbsoluteExpression(UniqueID
))
495 return TokError("unique id must be positive");
496 if (!isUInt
<32>(UniqueID
) || UniqueID
== ~0U)
497 return TokError("unique id is too large");
501 static bool hasPrefix(StringRef SectionName
, StringRef Prefix
) {
502 return SectionName
.startswith(Prefix
) || SectionName
== Prefix
.drop_back();
505 static bool allowSectionTypeMismatch(const Triple
&TT
, StringRef SectionName
,
507 if (TT
.getArch() == Triple::x86_64
) {
508 // x86-64 psABI names SHT_X86_64_UNWIND as the canonical type for .eh_frame,
509 // but GNU as emits SHT_PROGBITS .eh_frame for .cfi_* directives. Don't
510 // error for SHT_PROGBITS .eh_frame
511 return SectionName
== ".eh_frame" && Type
== ELF::SHT_PROGBITS
;
514 // MIPS .debug_* sections should have SHT_MIPS_DWARF section type to
515 // distinguish among sections contain DWARF and ECOFF debug formats,
516 // but in assembly files these sections have SHT_PROGBITS type.
517 return hasPrefix(SectionName
, ".debug_") && Type
== ELF::SHT_PROGBITS
;
522 bool ELFAsmParser::ParseSectionArguments(bool IsPush
, SMLoc loc
) {
523 StringRef SectionName
;
525 if (ParseSectionName(SectionName
))
526 return TokError("expected identifier in directive");
531 bool IsComdat
= false;
533 unsigned extraFlags
= 0;
534 const MCExpr
*Subsection
= nullptr;
535 bool UseLastGroup
= false;
536 MCSymbolELF
*LinkedToSym
= nullptr;
537 int64_t UniqueID
= ~0;
539 // Set the defaults first.
540 if (hasPrefix(SectionName
, ".rodata.") || SectionName
== ".rodata1")
541 Flags
|= ELF::SHF_ALLOC
;
542 else if (SectionName
== ".fini" || SectionName
== ".init" ||
543 hasPrefix(SectionName
, ".text."))
544 Flags
|= ELF::SHF_ALLOC
| ELF::SHF_EXECINSTR
;
545 else if (hasPrefix(SectionName
, ".data.") || SectionName
== ".data1" ||
546 hasPrefix(SectionName
, ".bss.") ||
547 hasPrefix(SectionName
, ".init_array.") ||
548 hasPrefix(SectionName
, ".fini_array.") ||
549 hasPrefix(SectionName
, ".preinit_array."))
550 Flags
|= ELF::SHF_ALLOC
| ELF::SHF_WRITE
;
551 else if (hasPrefix(SectionName
, ".tdata.") ||
552 hasPrefix(SectionName
, ".tbss."))
553 Flags
|= ELF::SHF_ALLOC
| ELF::SHF_WRITE
| ELF::SHF_TLS
;
555 if (getLexer().is(AsmToken::Comma
)) {
558 if (IsPush
&& getLexer().isNot(AsmToken::String
)) {
559 if (getParser().parseExpression(Subsection
))
561 if (getLexer().isNot(AsmToken::Comma
))
566 if (getLexer().isNot(AsmToken::String
)) {
567 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
568 || getLexer().isNot(AsmToken::Hash
))
569 return TokError("expected string in directive");
570 extraFlags
= parseSunStyleSectionFlags();
572 StringRef FlagsStr
= getTok().getStringContents();
574 extraFlags
= parseSectionFlags(FlagsStr
, &UseLastGroup
);
577 if (extraFlags
== -1U)
578 return TokError("unknown flag");
581 bool Mergeable
= Flags
& ELF::SHF_MERGE
;
582 bool Group
= Flags
& ELF::SHF_GROUP
;
583 if (Group
&& UseLastGroup
)
584 return TokError("Section cannot specifiy a group name while also acting "
585 "as a member of the last group");
587 if (maybeParseSectionType(TypeName
))
590 MCAsmLexer
&L
= getLexer();
591 if (TypeName
.empty()) {
593 return TokError("Mergeable section must specify the type");
595 return TokError("Group section must specify the type");
596 if (L
.isNot(AsmToken::EndOfStatement
))
597 return TokError("unexpected token in directive");
601 if (parseMergeSize(Size
))
604 if (parseGroup(GroupName
, IsComdat
))
606 if (Flags
& ELF::SHF_LINK_ORDER
)
607 if (parseLinkedToSym(LinkedToSym
))
609 if (maybeParseUniqueID(UniqueID
))
614 if (getLexer().isNot(AsmToken::EndOfStatement
))
615 return TokError("unexpected token in directive");
618 unsigned Type
= ELF::SHT_PROGBITS
;
620 if (TypeName
.empty()) {
621 if (SectionName
.startswith(".note"))
622 Type
= ELF::SHT_NOTE
;
623 else if (hasPrefix(SectionName
, ".init_array."))
624 Type
= ELF::SHT_INIT_ARRAY
;
625 else if (hasPrefix(SectionName
, ".bss."))
626 Type
= ELF::SHT_NOBITS
;
627 else if (hasPrefix(SectionName
, ".tbss."))
628 Type
= ELF::SHT_NOBITS
;
629 else if (hasPrefix(SectionName
, ".fini_array."))
630 Type
= ELF::SHT_FINI_ARRAY
;
631 else if (hasPrefix(SectionName
, ".preinit_array."))
632 Type
= ELF::SHT_PREINIT_ARRAY
;
634 if (TypeName
== "init_array")
635 Type
= ELF::SHT_INIT_ARRAY
;
636 else if (TypeName
== "fini_array")
637 Type
= ELF::SHT_FINI_ARRAY
;
638 else if (TypeName
== "preinit_array")
639 Type
= ELF::SHT_PREINIT_ARRAY
;
640 else if (TypeName
== "nobits")
641 Type
= ELF::SHT_NOBITS
;
642 else if (TypeName
== "progbits")
643 Type
= ELF::SHT_PROGBITS
;
644 else if (TypeName
== "note")
645 Type
= ELF::SHT_NOTE
;
646 else if (TypeName
== "unwind")
647 Type
= ELF::SHT_X86_64_UNWIND
;
648 else if (TypeName
== "llvm_odrtab")
649 Type
= ELF::SHT_LLVM_ODRTAB
;
650 else if (TypeName
== "llvm_linker_options")
651 Type
= ELF::SHT_LLVM_LINKER_OPTIONS
;
652 else if (TypeName
== "llvm_call_graph_profile")
653 Type
= ELF::SHT_LLVM_CALL_GRAPH_PROFILE
;
654 else if (TypeName
== "llvm_dependent_libraries")
655 Type
= ELF::SHT_LLVM_DEPENDENT_LIBRARIES
;
656 else if (TypeName
== "llvm_sympart")
657 Type
= ELF::SHT_LLVM_SYMPART
;
658 else if (TypeName
== "llvm_bb_addr_map")
659 Type
= ELF::SHT_LLVM_BB_ADDR_MAP
;
660 else if (TypeName
.getAsInteger(0, Type
))
661 return TokError("unknown section type");
665 MCSectionSubPair CurrentSection
= getStreamer().getCurrentSection();
666 if (const MCSectionELF
*Section
=
667 cast_or_null
<MCSectionELF
>(CurrentSection
.first
))
668 if (const MCSymbol
*Group
= Section
->getGroup()) {
669 GroupName
= Group
->getName();
670 IsComdat
= Section
->isComdat();
671 Flags
|= ELF::SHF_GROUP
;
675 MCSectionELF
*Section
=
676 getContext().getELFSection(SectionName
, Type
, Flags
, Size
, GroupName
,
677 IsComdat
, UniqueID
, LinkedToSym
);
678 getStreamer().SwitchSection(Section
, Subsection
);
679 if (Section
->getType() != Type
&&
680 !allowSectionTypeMismatch(getContext().getTargetTriple(), SectionName
,
682 Error(loc
, "changed section type for " + SectionName
+ ", expected: 0x" +
683 utohexstr(Section
->getType()));
684 // Check that flags are used consistently. However, the GNU assembler permits
685 // to leave out in subsequent uses of the same sections; for compatibility,
687 if ((extraFlags
|| Size
|| !TypeName
.empty()) && Section
->getFlags() != Flags
)
688 Error(loc
, "changed section flags for " + SectionName
+ ", expected: 0x" +
689 utohexstr(Section
->getFlags()));
690 if ((extraFlags
|| Size
|| !TypeName
.empty()) &&
691 Section
->getEntrySize() != Size
)
692 Error(loc
, "changed section entsize for " + SectionName
+
693 ", expected: " + Twine(Section
->getEntrySize()));
695 if (getContext().getGenDwarfForAssembly() &&
696 (Section
->getFlags() & ELF::SHF_ALLOC
) &&
697 (Section
->getFlags() & ELF::SHF_EXECINSTR
)) {
698 bool InsertResult
= getContext().addGenDwarfSection(Section
);
700 if (getContext().getDwarfVersion() <= 2)
701 Warning(loc
, "DWARF2 only supports one section per compilation unit");
703 if (!Section
->getBeginSymbol()) {
704 MCSymbol
*SectionStartSymbol
= getContext().createTempSymbol();
705 getStreamer().emitLabel(SectionStartSymbol
);
706 Section
->setBeginSymbol(SectionStartSymbol
);
714 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName
, SMLoc
) {
715 MCSectionSubPair PreviousSection
= getStreamer().getPreviousSection();
716 if (PreviousSection
.first
== nullptr)
717 return TokError(".previous without corresponding .section");
718 getStreamer().SwitchSection(PreviousSection
.first
, PreviousSection
.second
);
723 static MCSymbolAttr
MCAttrForString(StringRef Type
) {
724 return StringSwitch
<MCSymbolAttr
>(Type
)
725 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction
)
726 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject
)
727 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS
)
728 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon
)
729 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType
)
730 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
731 MCSA_ELF_TypeIndFunction
)
732 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject
)
733 .Default(MCSA_Invalid
);
736 /// ParseDirectiveELFType
737 /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
738 /// ::= .type identifier , #attribute
739 /// ::= .type identifier , @attribute
740 /// ::= .type identifier , %attribute
741 /// ::= .type identifier , "attribute"
742 bool ELFAsmParser::ParseDirectiveType(StringRef
, SMLoc
) {
744 if (getParser().parseIdentifier(Name
))
745 return TokError("expected identifier in directive");
747 // Handle the identifier as the key symbol.
748 MCSymbol
*Sym
= getContext().getOrCreateSymbol(Name
);
750 // NOTE the comma is optional in all cases. It is only documented as being
751 // optional in the first case, however, GAS will silently treat the comma as
752 // optional in all cases. Furthermore, although the documentation states that
753 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
754 // accepts both the upper case name as well as the lower case aliases.
755 if (getLexer().is(AsmToken::Comma
))
758 if (getLexer().isNot(AsmToken::Identifier
) &&
759 getLexer().isNot(AsmToken::Hash
) &&
760 getLexer().isNot(AsmToken::Percent
) &&
761 getLexer().isNot(AsmToken::String
)) {
762 if (!getLexer().getAllowAtInIdentifier())
763 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
764 "'%<type>' or \"<type>\"");
765 else if (getLexer().isNot(AsmToken::At
))
766 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
767 "'%<type>' or \"<type>\"");
770 if (getLexer().isNot(AsmToken::String
) &&
771 getLexer().isNot(AsmToken::Identifier
))
774 SMLoc TypeLoc
= getLexer().getLoc();
777 if (getParser().parseIdentifier(Type
))
778 return TokError("expected symbol type in directive");
780 MCSymbolAttr Attr
= MCAttrForString(Type
);
781 if (Attr
== MCSA_Invalid
)
782 return Error(TypeLoc
, "unsupported attribute in '.type' directive");
784 if (getLexer().isNot(AsmToken::EndOfStatement
))
785 return TokError("unexpected token in '.type' directive");
788 getStreamer().emitSymbolAttribute(Sym
, Attr
);
793 /// ParseDirectiveIdent
794 /// ::= .ident string
795 bool ELFAsmParser::ParseDirectiveIdent(StringRef
, SMLoc
) {
796 if (getLexer().isNot(AsmToken::String
))
797 return TokError("unexpected token in '.ident' directive");
799 StringRef Data
= getTok().getIdentifier();
803 if (getLexer().isNot(AsmToken::EndOfStatement
))
804 return TokError("unexpected token in '.ident' directive");
807 getStreamer().emitIdent(Data
);
811 /// ParseDirectiveSymver
812 /// ::= .symver foo, bar2@zed
813 bool ELFAsmParser::ParseDirectiveSymver(StringRef
, SMLoc
) {
814 StringRef OriginalName
, Name
, Action
;
815 if (getParser().parseIdentifier(OriginalName
))
816 return TokError("expected identifier in directive");
818 if (getLexer().isNot(AsmToken::Comma
))
819 return TokError("expected a comma");
821 // ARM assembly uses @ for a comment...
822 // except when parsing the second parameter of the .symver directive.
823 // Force the next symbol to allow @ in the identifier, which is
824 // required for this directive and then reset it to its initial state.
825 const bool AllowAtInIdentifier
= getLexer().getAllowAtInIdentifier();
826 getLexer().setAllowAtInIdentifier(true);
828 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier
);
830 if (getParser().parseIdentifier(Name
))
831 return TokError("expected identifier in directive");
833 if (Name
.find('@') == StringRef::npos
)
834 return TokError("expected a '@' in the name");
835 bool KeepOriginalSym
= !Name
.contains("@@@");
836 if (parseOptionalToken(AsmToken::Comma
)) {
837 if (getParser().parseIdentifier(Action
) || Action
!= "remove")
838 return TokError("expected 'remove'");
839 KeepOriginalSym
= false;
841 (void)parseOptionalToken(AsmToken::EndOfStatement
);
843 getStreamer().emitELFSymverDirective(
844 getContext().getOrCreateSymbol(OriginalName
), Name
, KeepOriginalSym
);
848 /// ParseDirectiveVersion
849 /// ::= .version string
850 bool ELFAsmParser::ParseDirectiveVersion(StringRef
, SMLoc
) {
851 if (getLexer().isNot(AsmToken::String
))
852 return TokError("unexpected token in '.version' directive");
854 StringRef Data
= getTok().getIdentifier();
858 MCSection
*Note
= getContext().getELFSection(".note", ELF::SHT_NOTE
, 0);
860 getStreamer().PushSection();
861 getStreamer().SwitchSection(Note
);
862 getStreamer().emitInt32(Data
.size() + 1); // namesz
863 getStreamer().emitInt32(0); // descsz = 0 (no description).
864 getStreamer().emitInt32(1); // type = NT_VERSION
865 getStreamer().emitBytes(Data
); // name
866 getStreamer().emitInt8(0); // NUL
867 getStreamer().emitValueToAlignment(4);
868 getStreamer().PopSection();
872 /// ParseDirectiveWeakref
873 /// ::= .weakref foo, bar
874 bool ELFAsmParser::ParseDirectiveWeakref(StringRef
, SMLoc
) {
875 // FIXME: Share code with the other alias building directives.
878 if (getParser().parseIdentifier(AliasName
))
879 return TokError("expected identifier in directive");
881 if (getLexer().isNot(AsmToken::Comma
))
882 return TokError("expected a comma");
887 if (getParser().parseIdentifier(Name
))
888 return TokError("expected identifier in directive");
890 MCSymbol
*Alias
= getContext().getOrCreateSymbol(AliasName
);
892 MCSymbol
*Sym
= getContext().getOrCreateSymbol(Name
);
894 getStreamer().emitWeakReference(Alias
, Sym
);
898 bool ELFAsmParser::ParseDirectiveSubsection(StringRef
, SMLoc
) {
899 const MCExpr
*Subsection
= nullptr;
900 if (getLexer().isNot(AsmToken::EndOfStatement
)) {
901 if (getParser().parseExpression(Subsection
))
905 if (getLexer().isNot(AsmToken::EndOfStatement
))
906 return TokError("unexpected token in directive");
910 getStreamer().SubSection(Subsection
);
914 bool ELFAsmParser::ParseDirectiveCGProfile(StringRef S
, SMLoc Loc
) {
915 return MCAsmParserExtension::ParseDirectiveCGProfile(S
, Loc
);
920 MCAsmParserExtension
*createELFAsmParser() {
921 return new ELFAsmParser
;
924 } // end namespace llvm