1f4a2713aSLionel Sambuc //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc
10f4a2713aSLionel Sambuc #include "llvm/MC/MCParser/MCAsmParserExtension.h"
11f4a2713aSLionel Sambuc #include "llvm/ADT/StringSwitch.h"
12f4a2713aSLionel Sambuc #include "llvm/ADT/Twine.h"
13f4a2713aSLionel Sambuc #include "llvm/MC/MCAsmInfo.h"
14f4a2713aSLionel Sambuc #include "llvm/MC/MCContext.h"
15f4a2713aSLionel Sambuc #include "llvm/MC/MCExpr.h"
16f4a2713aSLionel Sambuc #include "llvm/MC/MCParser/MCAsmLexer.h"
17f4a2713aSLionel Sambuc #include "llvm/MC/MCSectionELF.h"
18f4a2713aSLionel Sambuc #include "llvm/MC/MCStreamer.h"
19f4a2713aSLionel Sambuc #include "llvm/MC/MCSymbol.h"
20f4a2713aSLionel Sambuc #include "llvm/Support/ELF.h"
21f4a2713aSLionel Sambuc using namespace llvm;
22f4a2713aSLionel Sambuc
23f4a2713aSLionel Sambuc namespace {
24f4a2713aSLionel Sambuc
25f4a2713aSLionel Sambuc class ELFAsmParser : public MCAsmParserExtension {
26f4a2713aSLionel Sambuc template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
addDirectiveHandler(StringRef Directive)27f4a2713aSLionel Sambuc void addDirectiveHandler(StringRef Directive) {
28f4a2713aSLionel Sambuc MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
29f4a2713aSLionel Sambuc this, HandleDirective<ELFAsmParser, HandlerMethod>);
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc getParser().addDirectiveHandler(Directive, Handler);
32f4a2713aSLionel Sambuc }
33f4a2713aSLionel Sambuc
34f4a2713aSLionel Sambuc bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
35f4a2713aSLionel Sambuc SectionKind Kind);
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc public:
ELFAsmParser()38f4a2713aSLionel Sambuc ELFAsmParser() { BracketExpressionsSupported = true; }
39f4a2713aSLionel Sambuc
Initialize(MCAsmParser & Parser)40*0a6a1f1dSLionel Sambuc void Initialize(MCAsmParser &Parser) override {
41f4a2713aSLionel Sambuc // Call the base implementation.
42f4a2713aSLionel Sambuc this->MCAsmParserExtension::Initialize(Parser);
43f4a2713aSLionel Sambuc
44f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
45f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
46f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
47f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
48f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
49f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
50f4a2713aSLionel Sambuc addDirectiveHandler<
51f4a2713aSLionel Sambuc &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
52f4a2713aSLionel Sambuc addDirectiveHandler<
53f4a2713aSLionel Sambuc &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
54f4a2713aSLionel Sambuc addDirectiveHandler<
55f4a2713aSLionel Sambuc &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
56f4a2713aSLionel Sambuc addDirectiveHandler<
57f4a2713aSLionel Sambuc &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
58f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
59f4a2713aSLionel Sambuc addDirectiveHandler<
60f4a2713aSLionel Sambuc &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
61f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
62f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
63f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
64f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
65f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
66f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
67f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
68f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
69f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
70f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
71f4a2713aSLionel Sambuc addDirectiveHandler<
72f4a2713aSLionel Sambuc &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
73f4a2713aSLionel Sambuc addDirectiveHandler<
74f4a2713aSLionel Sambuc &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
75f4a2713aSLionel Sambuc addDirectiveHandler<
76f4a2713aSLionel Sambuc &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
77f4a2713aSLionel Sambuc addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
78f4a2713aSLionel Sambuc }
79f4a2713aSLionel Sambuc
80f4a2713aSLionel Sambuc // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
81f4a2713aSLionel Sambuc // the best way for us to get access to it?
ParseSectionDirectiveData(StringRef,SMLoc)82f4a2713aSLionel Sambuc bool ParseSectionDirectiveData(StringRef, SMLoc) {
83f4a2713aSLionel Sambuc return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
84f4a2713aSLionel Sambuc ELF::SHF_WRITE |ELF::SHF_ALLOC,
85f4a2713aSLionel Sambuc SectionKind::getDataRel());
86f4a2713aSLionel Sambuc }
ParseSectionDirectiveText(StringRef,SMLoc)87f4a2713aSLionel Sambuc bool ParseSectionDirectiveText(StringRef, SMLoc) {
88f4a2713aSLionel Sambuc return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
89f4a2713aSLionel Sambuc ELF::SHF_EXECINSTR |
90f4a2713aSLionel Sambuc ELF::SHF_ALLOC, SectionKind::getText());
91f4a2713aSLionel Sambuc }
ParseSectionDirectiveBSS(StringRef,SMLoc)92f4a2713aSLionel Sambuc bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
93f4a2713aSLionel Sambuc return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
94f4a2713aSLionel Sambuc ELF::SHF_WRITE |
95f4a2713aSLionel Sambuc ELF::SHF_ALLOC, SectionKind::getBSS());
96f4a2713aSLionel Sambuc }
ParseSectionDirectiveRoData(StringRef,SMLoc)97f4a2713aSLionel Sambuc bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
98f4a2713aSLionel Sambuc return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
99f4a2713aSLionel Sambuc ELF::SHF_ALLOC,
100f4a2713aSLionel Sambuc SectionKind::getReadOnly());
101f4a2713aSLionel Sambuc }
ParseSectionDirectiveTData(StringRef,SMLoc)102f4a2713aSLionel Sambuc bool ParseSectionDirectiveTData(StringRef, SMLoc) {
103f4a2713aSLionel Sambuc return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
104f4a2713aSLionel Sambuc ELF::SHF_ALLOC |
105f4a2713aSLionel Sambuc ELF::SHF_TLS | ELF::SHF_WRITE,
106f4a2713aSLionel Sambuc SectionKind::getThreadData());
107f4a2713aSLionel Sambuc }
ParseSectionDirectiveTBSS(StringRef,SMLoc)108f4a2713aSLionel Sambuc bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
109f4a2713aSLionel Sambuc return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
110f4a2713aSLionel Sambuc ELF::SHF_ALLOC |
111f4a2713aSLionel Sambuc ELF::SHF_TLS | ELF::SHF_WRITE,
112f4a2713aSLionel Sambuc SectionKind::getThreadBSS());
113f4a2713aSLionel Sambuc }
ParseSectionDirectiveDataRel(StringRef,SMLoc)114f4a2713aSLionel Sambuc bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
115f4a2713aSLionel Sambuc return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
116f4a2713aSLionel Sambuc ELF::SHF_ALLOC |
117f4a2713aSLionel Sambuc ELF::SHF_WRITE,
118f4a2713aSLionel Sambuc SectionKind::getDataRel());
119f4a2713aSLionel Sambuc }
ParseSectionDirectiveDataRelRo(StringRef,SMLoc)120f4a2713aSLionel Sambuc bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
121f4a2713aSLionel Sambuc return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
122f4a2713aSLionel Sambuc ELF::SHF_ALLOC |
123f4a2713aSLionel Sambuc ELF::SHF_WRITE,
124f4a2713aSLionel Sambuc SectionKind::getReadOnlyWithRel());
125f4a2713aSLionel Sambuc }
ParseSectionDirectiveDataRelRoLocal(StringRef,SMLoc)126f4a2713aSLionel Sambuc bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
127f4a2713aSLionel Sambuc return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
128f4a2713aSLionel Sambuc ELF::SHF_ALLOC |
129f4a2713aSLionel Sambuc ELF::SHF_WRITE,
130f4a2713aSLionel Sambuc SectionKind::getReadOnlyWithRelLocal());
131f4a2713aSLionel Sambuc }
ParseSectionDirectiveEhFrame(StringRef,SMLoc)132f4a2713aSLionel Sambuc bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
133f4a2713aSLionel Sambuc return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
134f4a2713aSLionel Sambuc ELF::SHF_ALLOC |
135f4a2713aSLionel Sambuc ELF::SHF_WRITE,
136f4a2713aSLionel Sambuc SectionKind::getDataRel());
137f4a2713aSLionel Sambuc }
138f4a2713aSLionel Sambuc bool ParseDirectivePushSection(StringRef, SMLoc);
139f4a2713aSLionel Sambuc bool ParseDirectivePopSection(StringRef, SMLoc);
140f4a2713aSLionel Sambuc bool ParseDirectiveSection(StringRef, SMLoc);
141f4a2713aSLionel Sambuc bool ParseDirectiveSize(StringRef, SMLoc);
142f4a2713aSLionel Sambuc bool ParseDirectivePrevious(StringRef, SMLoc);
143f4a2713aSLionel Sambuc bool ParseDirectiveType(StringRef, SMLoc);
144f4a2713aSLionel Sambuc bool ParseDirectiveIdent(StringRef, SMLoc);
145f4a2713aSLionel Sambuc bool ParseDirectiveSymver(StringRef, SMLoc);
146f4a2713aSLionel Sambuc bool ParseDirectiveVersion(StringRef, SMLoc);
147f4a2713aSLionel Sambuc bool ParseDirectiveWeakref(StringRef, SMLoc);
148f4a2713aSLionel Sambuc bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
149f4a2713aSLionel Sambuc bool ParseDirectiveSubsection(StringRef, SMLoc);
150f4a2713aSLionel Sambuc
151f4a2713aSLionel Sambuc private:
152f4a2713aSLionel Sambuc bool ParseSectionName(StringRef &SectionName);
153*0a6a1f1dSLionel Sambuc bool ParseSectionArguments(bool IsPush, SMLoc loc);
154*0a6a1f1dSLionel Sambuc unsigned parseSunStyleSectionFlags();
155f4a2713aSLionel Sambuc };
156f4a2713aSLionel Sambuc
157f4a2713aSLionel Sambuc }
158f4a2713aSLionel Sambuc
159f4a2713aSLionel Sambuc /// ParseDirectiveSymbolAttribute
160f4a2713aSLionel Sambuc /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
ParseDirectiveSymbolAttribute(StringRef Directive,SMLoc)161f4a2713aSLionel Sambuc bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
162f4a2713aSLionel Sambuc MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
163f4a2713aSLionel Sambuc .Case(".weak", MCSA_Weak)
164f4a2713aSLionel Sambuc .Case(".local", MCSA_Local)
165f4a2713aSLionel Sambuc .Case(".hidden", MCSA_Hidden)
166f4a2713aSLionel Sambuc .Case(".internal", MCSA_Internal)
167f4a2713aSLionel Sambuc .Case(".protected", MCSA_Protected)
168f4a2713aSLionel Sambuc .Default(MCSA_Invalid);
169f4a2713aSLionel Sambuc assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
170f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::EndOfStatement)) {
171f4a2713aSLionel Sambuc for (;;) {
172f4a2713aSLionel Sambuc StringRef Name;
173f4a2713aSLionel Sambuc
174f4a2713aSLionel Sambuc if (getParser().parseIdentifier(Name))
175f4a2713aSLionel Sambuc return TokError("expected identifier in directive");
176f4a2713aSLionel Sambuc
177f4a2713aSLionel Sambuc MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
178f4a2713aSLionel Sambuc
179f4a2713aSLionel Sambuc getStreamer().EmitSymbolAttribute(Sym, Attr);
180f4a2713aSLionel Sambuc
181f4a2713aSLionel Sambuc if (getLexer().is(AsmToken::EndOfStatement))
182f4a2713aSLionel Sambuc break;
183f4a2713aSLionel Sambuc
184f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::Comma))
185f4a2713aSLionel Sambuc return TokError("unexpected token in directive");
186f4a2713aSLionel Sambuc Lex();
187f4a2713aSLionel Sambuc }
188f4a2713aSLionel Sambuc }
189f4a2713aSLionel Sambuc
190f4a2713aSLionel Sambuc Lex();
191f4a2713aSLionel Sambuc return false;
192f4a2713aSLionel Sambuc }
193f4a2713aSLionel Sambuc
ParseSectionSwitch(StringRef Section,unsigned Type,unsigned Flags,SectionKind Kind)194f4a2713aSLionel Sambuc bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
195f4a2713aSLionel Sambuc unsigned Flags, SectionKind Kind) {
196*0a6a1f1dSLionel Sambuc const MCExpr *Subsection = nullptr;
197f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::EndOfStatement)) {
198f4a2713aSLionel Sambuc if (getParser().parseExpression(Subsection))
199f4a2713aSLionel Sambuc return true;
200f4a2713aSLionel Sambuc }
201f4a2713aSLionel Sambuc
202f4a2713aSLionel Sambuc getStreamer().SwitchSection(getContext().getELFSection(
203f4a2713aSLionel Sambuc Section, Type, Flags, Kind),
204f4a2713aSLionel Sambuc Subsection);
205f4a2713aSLionel Sambuc
206f4a2713aSLionel Sambuc return false;
207f4a2713aSLionel Sambuc }
208f4a2713aSLionel Sambuc
ParseDirectiveSize(StringRef,SMLoc)209f4a2713aSLionel Sambuc bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
210f4a2713aSLionel Sambuc StringRef Name;
211f4a2713aSLionel Sambuc if (getParser().parseIdentifier(Name))
212f4a2713aSLionel Sambuc return TokError("expected identifier in directive");
213f4a2713aSLionel Sambuc MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
214f4a2713aSLionel Sambuc
215f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::Comma))
216f4a2713aSLionel Sambuc return TokError("unexpected token in directive");
217f4a2713aSLionel Sambuc Lex();
218f4a2713aSLionel Sambuc
219f4a2713aSLionel Sambuc const MCExpr *Expr;
220f4a2713aSLionel Sambuc if (getParser().parseExpression(Expr))
221f4a2713aSLionel Sambuc return true;
222f4a2713aSLionel Sambuc
223f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::EndOfStatement))
224f4a2713aSLionel Sambuc return TokError("unexpected token in directive");
225f4a2713aSLionel Sambuc
226f4a2713aSLionel Sambuc getStreamer().EmitELFSize(Sym, Expr);
227f4a2713aSLionel Sambuc return false;
228f4a2713aSLionel Sambuc }
229f4a2713aSLionel Sambuc
ParseSectionName(StringRef & SectionName)230f4a2713aSLionel Sambuc bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
231f4a2713aSLionel Sambuc // A section name can contain -, so we cannot just use
232f4a2713aSLionel Sambuc // parseIdentifier.
233f4a2713aSLionel Sambuc SMLoc FirstLoc = getLexer().getLoc();
234f4a2713aSLionel Sambuc unsigned Size = 0;
235f4a2713aSLionel Sambuc
236f4a2713aSLionel Sambuc if (getLexer().is(AsmToken::String)) {
237f4a2713aSLionel Sambuc SectionName = getTok().getIdentifier();
238f4a2713aSLionel Sambuc Lex();
239f4a2713aSLionel Sambuc return false;
240f4a2713aSLionel Sambuc }
241f4a2713aSLionel Sambuc
242f4a2713aSLionel Sambuc for (;;) {
243f4a2713aSLionel Sambuc unsigned CurSize;
244f4a2713aSLionel Sambuc
245f4a2713aSLionel Sambuc SMLoc PrevLoc = getLexer().getLoc();
246f4a2713aSLionel Sambuc if (getLexer().is(AsmToken::Minus)) {
247f4a2713aSLionel Sambuc CurSize = 1;
248f4a2713aSLionel Sambuc Lex(); // Consume the "-".
249f4a2713aSLionel Sambuc } else if (getLexer().is(AsmToken::String)) {
250f4a2713aSLionel Sambuc CurSize = getTok().getIdentifier().size() + 2;
251f4a2713aSLionel Sambuc Lex();
252f4a2713aSLionel Sambuc } else if (getLexer().is(AsmToken::Identifier)) {
253f4a2713aSLionel Sambuc CurSize = getTok().getIdentifier().size();
254f4a2713aSLionel Sambuc Lex();
255f4a2713aSLionel Sambuc } else {
256f4a2713aSLionel Sambuc break;
257f4a2713aSLionel Sambuc }
258f4a2713aSLionel Sambuc
259f4a2713aSLionel Sambuc Size += CurSize;
260f4a2713aSLionel Sambuc SectionName = StringRef(FirstLoc.getPointer(), Size);
261f4a2713aSLionel Sambuc
262f4a2713aSLionel Sambuc // Make sure the following token is adjacent.
263f4a2713aSLionel Sambuc if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
264f4a2713aSLionel Sambuc break;
265f4a2713aSLionel Sambuc }
266f4a2713aSLionel Sambuc if (Size == 0)
267f4a2713aSLionel Sambuc return true;
268f4a2713aSLionel Sambuc
269f4a2713aSLionel Sambuc return false;
270f4a2713aSLionel Sambuc }
271f4a2713aSLionel Sambuc
computeSectionKind(unsigned Flags,unsigned ElemSize)272*0a6a1f1dSLionel Sambuc static SectionKind computeSectionKind(unsigned Flags, unsigned ElemSize) {
273f4a2713aSLionel Sambuc if (Flags & ELF::SHF_EXECINSTR)
274f4a2713aSLionel Sambuc return SectionKind::getText();
275f4a2713aSLionel Sambuc if (Flags & ELF::SHF_TLS)
276f4a2713aSLionel Sambuc return SectionKind::getThreadData();
277*0a6a1f1dSLionel Sambuc if (Flags & ELF::SHF_MERGE) {
278*0a6a1f1dSLionel Sambuc if (Flags & ELF::SHF_STRINGS) {
279*0a6a1f1dSLionel Sambuc switch (ElemSize) {
280*0a6a1f1dSLionel Sambuc default:
281*0a6a1f1dSLionel Sambuc break;
282*0a6a1f1dSLionel Sambuc case 1:
283*0a6a1f1dSLionel Sambuc return SectionKind::getMergeable1ByteCString();
284*0a6a1f1dSLionel Sambuc case 2:
285*0a6a1f1dSLionel Sambuc return SectionKind::getMergeable2ByteCString();
286*0a6a1f1dSLionel Sambuc case 4:
287*0a6a1f1dSLionel Sambuc return SectionKind::getMergeable4ByteCString();
288*0a6a1f1dSLionel Sambuc }
289*0a6a1f1dSLionel Sambuc } else {
290*0a6a1f1dSLionel Sambuc switch (ElemSize) {
291*0a6a1f1dSLionel Sambuc default:
292*0a6a1f1dSLionel Sambuc break;
293*0a6a1f1dSLionel Sambuc case 4:
294*0a6a1f1dSLionel Sambuc return SectionKind::getMergeableConst4();
295*0a6a1f1dSLionel Sambuc case 8:
296*0a6a1f1dSLionel Sambuc return SectionKind::getMergeableConst8();
297*0a6a1f1dSLionel Sambuc case 16:
298*0a6a1f1dSLionel Sambuc return SectionKind::getMergeableConst16();
299*0a6a1f1dSLionel Sambuc }
300*0a6a1f1dSLionel Sambuc }
301*0a6a1f1dSLionel Sambuc }
302*0a6a1f1dSLionel Sambuc
303f4a2713aSLionel Sambuc return SectionKind::getDataRel();
304f4a2713aSLionel Sambuc }
305f4a2713aSLionel Sambuc
parseSectionFlags(StringRef flagsStr,bool * UseLastGroup)306f4a2713aSLionel Sambuc static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
307f4a2713aSLionel Sambuc unsigned flags = 0;
308f4a2713aSLionel Sambuc
309f4a2713aSLionel Sambuc for (unsigned i = 0; i < flagsStr.size(); i++) {
310f4a2713aSLionel Sambuc switch (flagsStr[i]) {
311f4a2713aSLionel Sambuc case 'a':
312f4a2713aSLionel Sambuc flags |= ELF::SHF_ALLOC;
313f4a2713aSLionel Sambuc break;
314f4a2713aSLionel Sambuc case 'e':
315f4a2713aSLionel Sambuc flags |= ELF::SHF_EXCLUDE;
316f4a2713aSLionel Sambuc break;
317f4a2713aSLionel Sambuc case 'x':
318f4a2713aSLionel Sambuc flags |= ELF::SHF_EXECINSTR;
319f4a2713aSLionel Sambuc break;
320f4a2713aSLionel Sambuc case 'w':
321f4a2713aSLionel Sambuc flags |= ELF::SHF_WRITE;
322f4a2713aSLionel Sambuc break;
323f4a2713aSLionel Sambuc case 'M':
324f4a2713aSLionel Sambuc flags |= ELF::SHF_MERGE;
325f4a2713aSLionel Sambuc break;
326f4a2713aSLionel Sambuc case 'S':
327f4a2713aSLionel Sambuc flags |= ELF::SHF_STRINGS;
328f4a2713aSLionel Sambuc break;
329f4a2713aSLionel Sambuc case 'T':
330f4a2713aSLionel Sambuc flags |= ELF::SHF_TLS;
331f4a2713aSLionel Sambuc break;
332f4a2713aSLionel Sambuc case 'c':
333f4a2713aSLionel Sambuc flags |= ELF::XCORE_SHF_CP_SECTION;
334f4a2713aSLionel Sambuc break;
335f4a2713aSLionel Sambuc case 'd':
336f4a2713aSLionel Sambuc flags |= ELF::XCORE_SHF_DP_SECTION;
337f4a2713aSLionel Sambuc break;
338f4a2713aSLionel Sambuc case 'G':
339f4a2713aSLionel Sambuc flags |= ELF::SHF_GROUP;
340f4a2713aSLionel Sambuc break;
341f4a2713aSLionel Sambuc case '?':
342f4a2713aSLionel Sambuc *UseLastGroup = true;
343f4a2713aSLionel Sambuc break;
344f4a2713aSLionel Sambuc default:
345f4a2713aSLionel Sambuc return -1U;
346f4a2713aSLionel Sambuc }
347f4a2713aSLionel Sambuc }
348f4a2713aSLionel Sambuc
349f4a2713aSLionel Sambuc return flags;
350f4a2713aSLionel Sambuc }
351f4a2713aSLionel Sambuc
parseSunStyleSectionFlags()352*0a6a1f1dSLionel Sambuc unsigned ELFAsmParser::parseSunStyleSectionFlags() {
353*0a6a1f1dSLionel Sambuc unsigned flags = 0;
354*0a6a1f1dSLionel Sambuc while (getLexer().is(AsmToken::Hash)) {
355*0a6a1f1dSLionel Sambuc Lex(); // Eat the #.
356*0a6a1f1dSLionel Sambuc
357*0a6a1f1dSLionel Sambuc if (!getLexer().is(AsmToken::Identifier))
358*0a6a1f1dSLionel Sambuc return -1U;
359*0a6a1f1dSLionel Sambuc
360*0a6a1f1dSLionel Sambuc StringRef flagId = getTok().getIdentifier();
361*0a6a1f1dSLionel Sambuc if (flagId == "alloc")
362*0a6a1f1dSLionel Sambuc flags |= ELF::SHF_ALLOC;
363*0a6a1f1dSLionel Sambuc else if (flagId == "execinstr")
364*0a6a1f1dSLionel Sambuc flags |= ELF::SHF_EXECINSTR;
365*0a6a1f1dSLionel Sambuc else if (flagId == "write")
366*0a6a1f1dSLionel Sambuc flags |= ELF::SHF_WRITE;
367*0a6a1f1dSLionel Sambuc else if (flagId == "tls")
368*0a6a1f1dSLionel Sambuc flags |= ELF::SHF_TLS;
369*0a6a1f1dSLionel Sambuc else
370*0a6a1f1dSLionel Sambuc return -1U;
371*0a6a1f1dSLionel Sambuc
372*0a6a1f1dSLionel Sambuc Lex(); // Eat the flag.
373*0a6a1f1dSLionel Sambuc
374*0a6a1f1dSLionel Sambuc if (!getLexer().is(AsmToken::Comma))
375*0a6a1f1dSLionel Sambuc break;
376*0a6a1f1dSLionel Sambuc Lex(); // Eat the comma.
377*0a6a1f1dSLionel Sambuc }
378*0a6a1f1dSLionel Sambuc return flags;
379*0a6a1f1dSLionel Sambuc }
380*0a6a1f1dSLionel Sambuc
381*0a6a1f1dSLionel Sambuc
ParseDirectivePushSection(StringRef s,SMLoc loc)382f4a2713aSLionel Sambuc bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
383f4a2713aSLionel Sambuc getStreamer().PushSection();
384f4a2713aSLionel Sambuc
385*0a6a1f1dSLionel Sambuc if (ParseSectionArguments(/*IsPush=*/true, loc)) {
386f4a2713aSLionel Sambuc getStreamer().PopSection();
387f4a2713aSLionel Sambuc return true;
388f4a2713aSLionel Sambuc }
389f4a2713aSLionel Sambuc
390f4a2713aSLionel Sambuc return false;
391f4a2713aSLionel Sambuc }
392f4a2713aSLionel Sambuc
ParseDirectivePopSection(StringRef,SMLoc)393f4a2713aSLionel Sambuc bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
394f4a2713aSLionel Sambuc if (!getStreamer().PopSection())
395f4a2713aSLionel Sambuc return TokError(".popsection without corresponding .pushsection");
396f4a2713aSLionel Sambuc return false;
397f4a2713aSLionel Sambuc }
398f4a2713aSLionel Sambuc
399f4a2713aSLionel Sambuc // FIXME: This is a work in progress.
ParseDirectiveSection(StringRef,SMLoc loc)400*0a6a1f1dSLionel Sambuc bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
401*0a6a1f1dSLionel Sambuc return ParseSectionArguments(/*IsPush=*/false, loc);
402f4a2713aSLionel Sambuc }
403f4a2713aSLionel Sambuc
ParseSectionArguments(bool IsPush,SMLoc loc)404*0a6a1f1dSLionel Sambuc bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
405f4a2713aSLionel Sambuc StringRef SectionName;
406f4a2713aSLionel Sambuc
407f4a2713aSLionel Sambuc if (ParseSectionName(SectionName))
408f4a2713aSLionel Sambuc return TokError("expected identifier in directive");
409f4a2713aSLionel Sambuc
410f4a2713aSLionel Sambuc StringRef TypeName;
411f4a2713aSLionel Sambuc int64_t Size = 0;
412f4a2713aSLionel Sambuc StringRef GroupName;
413f4a2713aSLionel Sambuc unsigned Flags = 0;
414*0a6a1f1dSLionel Sambuc const MCExpr *Subsection = nullptr;
415f4a2713aSLionel Sambuc bool UseLastGroup = false;
416f4a2713aSLionel Sambuc
417f4a2713aSLionel Sambuc // Set the defaults first.
418f4a2713aSLionel Sambuc if (SectionName == ".fini" || SectionName == ".init" ||
419f4a2713aSLionel Sambuc SectionName == ".rodata")
420f4a2713aSLionel Sambuc Flags |= ELF::SHF_ALLOC;
421f4a2713aSLionel Sambuc if (SectionName == ".fini" || SectionName == ".init")
422f4a2713aSLionel Sambuc Flags |= ELF::SHF_EXECINSTR;
423f4a2713aSLionel Sambuc
424f4a2713aSLionel Sambuc if (getLexer().is(AsmToken::Comma)) {
425f4a2713aSLionel Sambuc Lex();
426f4a2713aSLionel Sambuc
427f4a2713aSLionel Sambuc if (IsPush && getLexer().isNot(AsmToken::String)) {
428f4a2713aSLionel Sambuc if (getParser().parseExpression(Subsection))
429f4a2713aSLionel Sambuc return true;
430f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::Comma))
431f4a2713aSLionel Sambuc goto EndStmt;
432f4a2713aSLionel Sambuc Lex();
433f4a2713aSLionel Sambuc }
434f4a2713aSLionel Sambuc
435*0a6a1f1dSLionel Sambuc unsigned extraFlags;
436f4a2713aSLionel Sambuc
437*0a6a1f1dSLionel Sambuc if (getLexer().isNot(AsmToken::String)) {
438*0a6a1f1dSLionel Sambuc if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
439*0a6a1f1dSLionel Sambuc || getLexer().isNot(AsmToken::Hash))
440*0a6a1f1dSLionel Sambuc return TokError("expected string in directive");
441*0a6a1f1dSLionel Sambuc extraFlags = parseSunStyleSectionFlags();
442*0a6a1f1dSLionel Sambuc } else {
443f4a2713aSLionel Sambuc StringRef FlagsStr = getTok().getStringContents();
444f4a2713aSLionel Sambuc Lex();
445*0a6a1f1dSLionel Sambuc extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
446*0a6a1f1dSLionel Sambuc }
447f4a2713aSLionel Sambuc
448f4a2713aSLionel Sambuc if (extraFlags == -1U)
449f4a2713aSLionel Sambuc return TokError("unknown flag");
450f4a2713aSLionel Sambuc Flags |= extraFlags;
451f4a2713aSLionel Sambuc
452f4a2713aSLionel Sambuc bool Mergeable = Flags & ELF::SHF_MERGE;
453f4a2713aSLionel Sambuc bool Group = Flags & ELF::SHF_GROUP;
454f4a2713aSLionel Sambuc if (Group && UseLastGroup)
455f4a2713aSLionel Sambuc return TokError("Section cannot specifiy a group name while also acting "
456f4a2713aSLionel Sambuc "as a member of the last group");
457f4a2713aSLionel Sambuc
458f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::Comma)) {
459f4a2713aSLionel Sambuc if (Mergeable)
460f4a2713aSLionel Sambuc return TokError("Mergeable section must specify the type");
461f4a2713aSLionel Sambuc if (Group)
462f4a2713aSLionel Sambuc return TokError("Group section must specify the type");
463f4a2713aSLionel Sambuc } else {
464f4a2713aSLionel Sambuc Lex();
465f4a2713aSLionel Sambuc if (getLexer().is(AsmToken::At) || getLexer().is(AsmToken::Percent) ||
466f4a2713aSLionel Sambuc getLexer().is(AsmToken::String)) {
467f4a2713aSLionel Sambuc if (!getLexer().is(AsmToken::String))
468f4a2713aSLionel Sambuc Lex();
469f4a2713aSLionel Sambuc } else
470f4a2713aSLionel Sambuc return TokError("expected '@<type>', '%<type>' or \"<type>\"");
471f4a2713aSLionel Sambuc
472f4a2713aSLionel Sambuc if (getParser().parseIdentifier(TypeName))
473f4a2713aSLionel Sambuc return TokError("expected identifier in directive");
474f4a2713aSLionel Sambuc
475f4a2713aSLionel Sambuc if (Mergeable) {
476f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::Comma))
477f4a2713aSLionel Sambuc return TokError("expected the entry size");
478f4a2713aSLionel Sambuc Lex();
479f4a2713aSLionel Sambuc if (getParser().parseAbsoluteExpression(Size))
480f4a2713aSLionel Sambuc return true;
481f4a2713aSLionel Sambuc if (Size <= 0)
482f4a2713aSLionel Sambuc return TokError("entry size must be positive");
483f4a2713aSLionel Sambuc }
484f4a2713aSLionel Sambuc
485f4a2713aSLionel Sambuc if (Group) {
486f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::Comma))
487f4a2713aSLionel Sambuc return TokError("expected group name");
488f4a2713aSLionel Sambuc Lex();
489f4a2713aSLionel Sambuc if (getParser().parseIdentifier(GroupName))
490f4a2713aSLionel Sambuc return true;
491f4a2713aSLionel Sambuc if (getLexer().is(AsmToken::Comma)) {
492f4a2713aSLionel Sambuc Lex();
493f4a2713aSLionel Sambuc StringRef Linkage;
494f4a2713aSLionel Sambuc if (getParser().parseIdentifier(Linkage))
495f4a2713aSLionel Sambuc return true;
496f4a2713aSLionel Sambuc if (Linkage != "comdat")
497f4a2713aSLionel Sambuc return TokError("Linkage must be 'comdat'");
498f4a2713aSLionel Sambuc }
499f4a2713aSLionel Sambuc }
500f4a2713aSLionel Sambuc }
501f4a2713aSLionel Sambuc }
502f4a2713aSLionel Sambuc
503f4a2713aSLionel Sambuc EndStmt:
504f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::EndOfStatement))
505f4a2713aSLionel Sambuc return TokError("unexpected token in directive");
506f4a2713aSLionel Sambuc
507f4a2713aSLionel Sambuc unsigned Type = ELF::SHT_PROGBITS;
508f4a2713aSLionel Sambuc
509f4a2713aSLionel Sambuc if (TypeName.empty()) {
510f4a2713aSLionel Sambuc if (SectionName.startswith(".note"))
511f4a2713aSLionel Sambuc Type = ELF::SHT_NOTE;
512f4a2713aSLionel Sambuc else if (SectionName == ".init_array")
513f4a2713aSLionel Sambuc Type = ELF::SHT_INIT_ARRAY;
514f4a2713aSLionel Sambuc else if (SectionName == ".fini_array")
515f4a2713aSLionel Sambuc Type = ELF::SHT_FINI_ARRAY;
516f4a2713aSLionel Sambuc else if (SectionName == ".preinit_array")
517f4a2713aSLionel Sambuc Type = ELF::SHT_PREINIT_ARRAY;
518f4a2713aSLionel Sambuc } else {
519f4a2713aSLionel Sambuc if (TypeName == "init_array")
520f4a2713aSLionel Sambuc Type = ELF::SHT_INIT_ARRAY;
521f4a2713aSLionel Sambuc else if (TypeName == "fini_array")
522f4a2713aSLionel Sambuc Type = ELF::SHT_FINI_ARRAY;
523f4a2713aSLionel Sambuc else if (TypeName == "preinit_array")
524f4a2713aSLionel Sambuc Type = ELF::SHT_PREINIT_ARRAY;
525f4a2713aSLionel Sambuc else if (TypeName == "nobits")
526f4a2713aSLionel Sambuc Type = ELF::SHT_NOBITS;
527f4a2713aSLionel Sambuc else if (TypeName == "progbits")
528f4a2713aSLionel Sambuc Type = ELF::SHT_PROGBITS;
529f4a2713aSLionel Sambuc else if (TypeName == "note")
530f4a2713aSLionel Sambuc Type = ELF::SHT_NOTE;
531f4a2713aSLionel Sambuc else if (TypeName == "unwind")
532f4a2713aSLionel Sambuc Type = ELF::SHT_X86_64_UNWIND;
533f4a2713aSLionel Sambuc else
534f4a2713aSLionel Sambuc return TokError("unknown section type");
535f4a2713aSLionel Sambuc }
536f4a2713aSLionel Sambuc
537f4a2713aSLionel Sambuc if (UseLastGroup) {
538f4a2713aSLionel Sambuc MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
539f4a2713aSLionel Sambuc if (const MCSectionELF *Section =
540f4a2713aSLionel Sambuc cast_or_null<MCSectionELF>(CurrentSection.first))
541f4a2713aSLionel Sambuc if (const MCSymbol *Group = Section->getGroup()) {
542f4a2713aSLionel Sambuc GroupName = Group->getName();
543f4a2713aSLionel Sambuc Flags |= ELF::SHF_GROUP;
544f4a2713aSLionel Sambuc }
545f4a2713aSLionel Sambuc }
546f4a2713aSLionel Sambuc
547*0a6a1f1dSLionel Sambuc SectionKind Kind = computeSectionKind(Flags, Size);
548*0a6a1f1dSLionel Sambuc const MCSection *ELFSection = getContext().getELFSection(
549*0a6a1f1dSLionel Sambuc SectionName, Type, Flags, Kind, Size, GroupName);
550*0a6a1f1dSLionel Sambuc getStreamer().SwitchSection(ELFSection, Subsection);
551*0a6a1f1dSLionel Sambuc
552*0a6a1f1dSLionel Sambuc if (getContext().getGenDwarfForAssembly()) {
553*0a6a1f1dSLionel Sambuc auto &Sections = getContext().getGenDwarfSectionSyms();
554*0a6a1f1dSLionel Sambuc auto InsertResult = Sections.insert(
555*0a6a1f1dSLionel Sambuc std::make_pair(ELFSection, std::make_pair(nullptr, nullptr)));
556*0a6a1f1dSLionel Sambuc if (InsertResult.second) {
557*0a6a1f1dSLionel Sambuc if (getContext().getDwarfVersion() <= 2)
558*0a6a1f1dSLionel Sambuc Warning(loc, "DWARF2 only supports one section per compilation unit");
559*0a6a1f1dSLionel Sambuc
560*0a6a1f1dSLionel Sambuc MCSymbol *SectionStartSymbol = getContext().CreateTempSymbol();
561*0a6a1f1dSLionel Sambuc getStreamer().EmitLabel(SectionStartSymbol);
562*0a6a1f1dSLionel Sambuc InsertResult.first->second.first = SectionStartSymbol;
563*0a6a1f1dSLionel Sambuc }
564*0a6a1f1dSLionel Sambuc }
565*0a6a1f1dSLionel Sambuc
566f4a2713aSLionel Sambuc return false;
567f4a2713aSLionel Sambuc }
568f4a2713aSLionel Sambuc
ParseDirectivePrevious(StringRef DirName,SMLoc)569f4a2713aSLionel Sambuc bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
570f4a2713aSLionel Sambuc MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
571*0a6a1f1dSLionel Sambuc if (PreviousSection.first == nullptr)
572f4a2713aSLionel Sambuc return TokError(".previous without corresponding .section");
573f4a2713aSLionel Sambuc getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
574f4a2713aSLionel Sambuc
575f4a2713aSLionel Sambuc return false;
576f4a2713aSLionel Sambuc }
577f4a2713aSLionel Sambuc
MCAttrForString(StringRef Type)578*0a6a1f1dSLionel Sambuc static MCSymbolAttr MCAttrForString(StringRef Type) {
579*0a6a1f1dSLionel Sambuc return StringSwitch<MCSymbolAttr>(Type)
580*0a6a1f1dSLionel Sambuc .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
581*0a6a1f1dSLionel Sambuc .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
582*0a6a1f1dSLionel Sambuc .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
583*0a6a1f1dSLionel Sambuc .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
584*0a6a1f1dSLionel Sambuc .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
585*0a6a1f1dSLionel Sambuc .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
586*0a6a1f1dSLionel Sambuc MCSA_ELF_TypeIndFunction)
587*0a6a1f1dSLionel Sambuc .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
588*0a6a1f1dSLionel Sambuc .Default(MCSA_Invalid);
589*0a6a1f1dSLionel Sambuc }
590*0a6a1f1dSLionel Sambuc
591f4a2713aSLionel Sambuc /// ParseDirectiveELFType
592f4a2713aSLionel Sambuc /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
593f4a2713aSLionel Sambuc /// ::= .type identifier , #attribute
594f4a2713aSLionel Sambuc /// ::= .type identifier , @attribute
595f4a2713aSLionel Sambuc /// ::= .type identifier , %attribute
596f4a2713aSLionel Sambuc /// ::= .type identifier , "attribute"
ParseDirectiveType(StringRef,SMLoc)597f4a2713aSLionel Sambuc bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
598f4a2713aSLionel Sambuc StringRef Name;
599f4a2713aSLionel Sambuc if (getParser().parseIdentifier(Name))
600f4a2713aSLionel Sambuc return TokError("expected identifier in directive");
601f4a2713aSLionel Sambuc
602f4a2713aSLionel Sambuc // Handle the identifier as the key symbol.
603f4a2713aSLionel Sambuc MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
604f4a2713aSLionel Sambuc
605*0a6a1f1dSLionel Sambuc // NOTE the comma is optional in all cases. It is only documented as being
606*0a6a1f1dSLionel Sambuc // optional in the first case, however, GAS will silently treat the comma as
607*0a6a1f1dSLionel Sambuc // optional in all cases. Furthermore, although the documentation states that
608*0a6a1f1dSLionel Sambuc // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
609*0a6a1f1dSLionel Sambuc // accepts both the upper case name as well as the lower case aliases.
610*0a6a1f1dSLionel Sambuc if (getLexer().is(AsmToken::Comma))
611f4a2713aSLionel Sambuc Lex();
612f4a2713aSLionel Sambuc
613*0a6a1f1dSLionel Sambuc if (getLexer().isNot(AsmToken::Identifier) &&
614*0a6a1f1dSLionel Sambuc getLexer().isNot(AsmToken::Hash) && getLexer().isNot(AsmToken::At) &&
615*0a6a1f1dSLionel Sambuc getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::String))
616f4a2713aSLionel Sambuc return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
617f4a2713aSLionel Sambuc "'%<type>' or \"<type>\"");
618f4a2713aSLionel Sambuc
619*0a6a1f1dSLionel Sambuc if (getLexer().isNot(AsmToken::String) &&
620*0a6a1f1dSLionel Sambuc getLexer().isNot(AsmToken::Identifier))
621*0a6a1f1dSLionel Sambuc Lex();
622*0a6a1f1dSLionel Sambuc
623*0a6a1f1dSLionel Sambuc SMLoc TypeLoc = getLexer().getLoc();
624*0a6a1f1dSLionel Sambuc
625*0a6a1f1dSLionel Sambuc StringRef Type;
626*0a6a1f1dSLionel Sambuc if (getParser().parseIdentifier(Type))
627*0a6a1f1dSLionel Sambuc return TokError("expected symbol type in directive");
628*0a6a1f1dSLionel Sambuc
629*0a6a1f1dSLionel Sambuc MCSymbolAttr Attr = MCAttrForString(Type);
630f4a2713aSLionel Sambuc if (Attr == MCSA_Invalid)
631f4a2713aSLionel Sambuc return Error(TypeLoc, "unsupported attribute in '.type' directive");
632f4a2713aSLionel Sambuc
633f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::EndOfStatement))
634f4a2713aSLionel Sambuc return TokError("unexpected token in '.type' directive");
635f4a2713aSLionel Sambuc Lex();
636f4a2713aSLionel Sambuc
637f4a2713aSLionel Sambuc getStreamer().EmitSymbolAttribute(Sym, Attr);
638f4a2713aSLionel Sambuc
639f4a2713aSLionel Sambuc return false;
640f4a2713aSLionel Sambuc }
641f4a2713aSLionel Sambuc
642f4a2713aSLionel Sambuc /// ParseDirectiveIdent
643f4a2713aSLionel Sambuc /// ::= .ident string
ParseDirectiveIdent(StringRef,SMLoc)644f4a2713aSLionel Sambuc bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
645f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::String))
646f4a2713aSLionel Sambuc return TokError("unexpected token in '.ident' directive");
647f4a2713aSLionel Sambuc
648f4a2713aSLionel Sambuc StringRef Data = getTok().getIdentifier();
649f4a2713aSLionel Sambuc
650f4a2713aSLionel Sambuc Lex();
651f4a2713aSLionel Sambuc
652f4a2713aSLionel Sambuc getStreamer().EmitIdent(Data);
653f4a2713aSLionel Sambuc return false;
654f4a2713aSLionel Sambuc }
655f4a2713aSLionel Sambuc
656f4a2713aSLionel Sambuc /// ParseDirectiveSymver
657f4a2713aSLionel Sambuc /// ::= .symver foo, bar2@zed
ParseDirectiveSymver(StringRef,SMLoc)658f4a2713aSLionel Sambuc bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
659f4a2713aSLionel Sambuc StringRef Name;
660f4a2713aSLionel Sambuc if (getParser().parseIdentifier(Name))
661f4a2713aSLionel Sambuc return TokError("expected identifier in directive");
662f4a2713aSLionel Sambuc
663f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::Comma))
664f4a2713aSLionel Sambuc return TokError("expected a comma");
665f4a2713aSLionel Sambuc
666*0a6a1f1dSLionel Sambuc // ARM assembly uses @ for a comment...
667*0a6a1f1dSLionel Sambuc // except when parsing the second parameter of the .symver directive.
668*0a6a1f1dSLionel Sambuc // Force the next symbol to allow @ in the identifier, which is
669*0a6a1f1dSLionel Sambuc // required for this directive and then reset it to its initial state.
670*0a6a1f1dSLionel Sambuc const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
671*0a6a1f1dSLionel Sambuc getLexer().setAllowAtInIdentifier(true);
672f4a2713aSLionel Sambuc Lex();
673*0a6a1f1dSLionel Sambuc getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
674f4a2713aSLionel Sambuc
675f4a2713aSLionel Sambuc StringRef AliasName;
676f4a2713aSLionel Sambuc if (getParser().parseIdentifier(AliasName))
677f4a2713aSLionel Sambuc return TokError("expected identifier in directive");
678f4a2713aSLionel Sambuc
679f4a2713aSLionel Sambuc if (AliasName.find('@') == StringRef::npos)
680f4a2713aSLionel Sambuc return TokError("expected a '@' in the name");
681f4a2713aSLionel Sambuc
682f4a2713aSLionel Sambuc MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
683f4a2713aSLionel Sambuc MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
684f4a2713aSLionel Sambuc const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
685f4a2713aSLionel Sambuc
686f4a2713aSLionel Sambuc getStreamer().EmitAssignment(Alias, Value);
687f4a2713aSLionel Sambuc return false;
688f4a2713aSLionel Sambuc }
689f4a2713aSLionel Sambuc
690f4a2713aSLionel Sambuc /// ParseDirectiveVersion
691f4a2713aSLionel Sambuc /// ::= .version string
ParseDirectiveVersion(StringRef,SMLoc)692f4a2713aSLionel Sambuc bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
693f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::String))
694f4a2713aSLionel Sambuc return TokError("unexpected token in '.version' directive");
695f4a2713aSLionel Sambuc
696f4a2713aSLionel Sambuc StringRef Data = getTok().getIdentifier();
697f4a2713aSLionel Sambuc
698f4a2713aSLionel Sambuc Lex();
699f4a2713aSLionel Sambuc
700f4a2713aSLionel Sambuc const MCSection *Note =
701f4a2713aSLionel Sambuc getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
702f4a2713aSLionel Sambuc SectionKind::getReadOnly());
703f4a2713aSLionel Sambuc
704f4a2713aSLionel Sambuc getStreamer().PushSection();
705f4a2713aSLionel Sambuc getStreamer().SwitchSection(Note);
706f4a2713aSLionel Sambuc getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
707f4a2713aSLionel Sambuc getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
708f4a2713aSLionel Sambuc getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
709f4a2713aSLionel Sambuc getStreamer().EmitBytes(Data); // name.
710f4a2713aSLionel Sambuc getStreamer().EmitIntValue(0, 1); // terminate the string.
711f4a2713aSLionel Sambuc getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
712f4a2713aSLionel Sambuc getStreamer().PopSection();
713f4a2713aSLionel Sambuc return false;
714f4a2713aSLionel Sambuc }
715f4a2713aSLionel Sambuc
716f4a2713aSLionel Sambuc /// ParseDirectiveWeakref
717f4a2713aSLionel Sambuc /// ::= .weakref foo, bar
ParseDirectiveWeakref(StringRef,SMLoc)718f4a2713aSLionel Sambuc bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
719f4a2713aSLionel Sambuc // FIXME: Share code with the other alias building directives.
720f4a2713aSLionel Sambuc
721f4a2713aSLionel Sambuc StringRef AliasName;
722f4a2713aSLionel Sambuc if (getParser().parseIdentifier(AliasName))
723f4a2713aSLionel Sambuc return TokError("expected identifier in directive");
724f4a2713aSLionel Sambuc
725f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::Comma))
726f4a2713aSLionel Sambuc return TokError("expected a comma");
727f4a2713aSLionel Sambuc
728f4a2713aSLionel Sambuc Lex();
729f4a2713aSLionel Sambuc
730f4a2713aSLionel Sambuc StringRef Name;
731f4a2713aSLionel Sambuc if (getParser().parseIdentifier(Name))
732f4a2713aSLionel Sambuc return TokError("expected identifier in directive");
733f4a2713aSLionel Sambuc
734f4a2713aSLionel Sambuc MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
735f4a2713aSLionel Sambuc
736f4a2713aSLionel Sambuc MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
737f4a2713aSLionel Sambuc
738f4a2713aSLionel Sambuc getStreamer().EmitWeakReference(Alias, Sym);
739f4a2713aSLionel Sambuc return false;
740f4a2713aSLionel Sambuc }
741f4a2713aSLionel Sambuc
ParseDirectiveSubsection(StringRef,SMLoc)742f4a2713aSLionel Sambuc bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
743*0a6a1f1dSLionel Sambuc const MCExpr *Subsection = nullptr;
744f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::EndOfStatement)) {
745f4a2713aSLionel Sambuc if (getParser().parseExpression(Subsection))
746f4a2713aSLionel Sambuc return true;
747f4a2713aSLionel Sambuc }
748f4a2713aSLionel Sambuc
749f4a2713aSLionel Sambuc if (getLexer().isNot(AsmToken::EndOfStatement))
750f4a2713aSLionel Sambuc return TokError("unexpected token in directive");
751f4a2713aSLionel Sambuc
752f4a2713aSLionel Sambuc getStreamer().SubSection(Subsection);
753f4a2713aSLionel Sambuc return false;
754f4a2713aSLionel Sambuc }
755f4a2713aSLionel Sambuc
756f4a2713aSLionel Sambuc namespace llvm {
757f4a2713aSLionel Sambuc
createELFAsmParser()758f4a2713aSLionel Sambuc MCAsmParserExtension *createELFAsmParser() {
759f4a2713aSLionel Sambuc return new ELFAsmParser;
760f4a2713aSLionel Sambuc }
761f4a2713aSLionel Sambuc
762f4a2713aSLionel Sambuc }
763