1 //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCParser/MCAsmLexer.h"
15 #include "llvm/MC/MCSectionELF.h"
16 #include "llvm/MC/MCStreamer.h"
21 class ELFAsmParser : public MCAsmParserExtension {
22 bool ParseSectionSwitch(StringRef Section, unsigned Type,
23 unsigned Flags, SectionKind Kind);
28 virtual void Initialize(MCAsmParser &Parser) {
29 // Call the base implementation.
30 this->MCAsmParserExtension::Initialize(Parser);
32 Parser.AddDirectiveHandler(this, ".data", MCAsmParser::DirectiveHandler(
33 &ELFAsmParser::ParseSectionDirectiveData));
34 Parser.AddDirectiveHandler(this, ".text", MCAsmParser::DirectiveHandler(
35 &ELFAsmParser::ParseSectionDirectiveText));
36 Parser.AddDirectiveHandler(this, ".section", MCAsmParser::DirectiveHandler(
37 &ELFAsmParser::ParseDirectiveSection));
38 Parser.AddDirectiveHandler(this, ".size", MCAsmParser::DirectiveHandler(
39 &ELFAsmParser::ParseDirectiveSize));
40 Parser.AddDirectiveHandler(this, ".sleb128", MCAsmParser::DirectiveHandler(
41 &ELFAsmParser::ParseDirectiveLEB128));
42 Parser.AddDirectiveHandler(this, ".uleb128", MCAsmParser::DirectiveHandler(
43 &ELFAsmParser::ParseDirectiveLEB128));
46 bool ParseSectionDirectiveData(StringRef, SMLoc) {
47 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
48 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
49 SectionKind::getDataRel());
51 bool ParseSectionDirectiveText(StringRef, SMLoc) {
52 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
53 MCSectionELF::SHF_EXECINSTR |
54 MCSectionELF::SHF_ALLOC, SectionKind::getText());
56 bool ParseDirectiveLEB128(StringRef, SMLoc);
57 bool ParseDirectiveSection(StringRef, SMLoc);
58 bool ParseDirectiveSize(StringRef, SMLoc);
63 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
64 unsigned Flags, SectionKind Kind) {
65 if (getLexer().isNot(AsmToken::EndOfStatement))
66 return TokError("unexpected token in section switching directive");
69 getStreamer().SwitchSection(getContext().getELFSection(
70 Section, Type, Flags, Kind));
75 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
77 if (getParser().ParseIdentifier(Name))
78 return TokError("expected identifier in directive");
79 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
81 if (getLexer().isNot(AsmToken::Comma))
82 return TokError("unexpected token in directive");
86 if (getParser().ParseExpression(Expr))
89 if (getLexer().isNot(AsmToken::EndOfStatement))
90 return TokError("unexpected token in directive");
92 getStreamer().EmitELFSize(Sym, Expr);
96 // FIXME: This is a work in progress.
97 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
98 StringRef SectionName;
99 // FIXME: This doesn't parse section names like ".note.GNU-stack" correctly.
100 if (getParser().ParseIdentifier(SectionName))
101 return TokError("expected identifier in directive");
103 std::string FlagsStr;
106 if (getLexer().is(AsmToken::Comma)) {
109 if (getLexer().isNot(AsmToken::String))
110 return TokError("expected string in directive");
112 FlagsStr = getTok().getStringContents();
115 AsmToken::TokenKind TypeStartToken;
116 if (getContext().getAsmInfo().getCommentString()[0] == '@')
117 TypeStartToken = AsmToken::Percent;
119 TypeStartToken = AsmToken::At;
121 if (getLexer().is(AsmToken::Comma)) {
123 if (getLexer().is(TypeStartToken)) {
125 if (getParser().ParseIdentifier(TypeName))
126 return TokError("expected identifier in directive");
128 if (getLexer().is(AsmToken::Comma)) {
131 if (getParser().ParseAbsoluteExpression(Size))
135 return TokError("section size must be positive");
141 if (getLexer().isNot(AsmToken::EndOfStatement))
142 return TokError("unexpected token in directive");
145 for (unsigned i = 0; i < FlagsStr.size(); i++) {
146 switch (FlagsStr[i]) {
148 Flags |= MCSectionELF::SHF_ALLOC;
151 Flags |= MCSectionELF::SHF_EXECINSTR;
154 Flags |= MCSectionELF::SHF_WRITE;
157 Flags |= MCSectionELF::SHF_MERGE;
160 Flags |= MCSectionELF::SHF_STRINGS;
163 Flags |= MCSectionELF::SHF_TLS;
166 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
169 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
172 return TokError("unknown flag");
176 unsigned Type = MCSectionELF::SHT_NULL;
177 if (!TypeName.empty()) {
178 if (TypeName == "init_array")
179 Type = MCSectionELF::SHT_INIT_ARRAY;
180 else if (TypeName == "fini_array")
181 Type = MCSectionELF::SHT_FINI_ARRAY;
182 else if (TypeName == "preinit_array")
183 Type = MCSectionELF::SHT_PREINIT_ARRAY;
184 else if (TypeName == "nobits")
185 Type = MCSectionELF::SHT_NOBITS;
186 else if (TypeName == "progbits")
187 Type = MCSectionELF::SHT_PROGBITS;
189 return TokError("unknown section type");
192 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
193 ? SectionKind::getText()
194 : SectionKind::getDataRel();
195 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
196 Flags, Kind, false));
200 bool ELFAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
202 if (getParser().ParseAbsoluteExpression(Value))
205 if (getLexer().isNot(AsmToken::EndOfStatement))
206 return TokError("unexpected token in directive");
208 // FIXME: Add proper MC support.
209 if (getContext().getAsmInfo().hasLEB128()) {
210 if (DirName[1] == 's')
211 getStreamer().EmitRawText("\t.sleb128\t" + Twine(Value));
213 getStreamer().EmitRawText("\t.uleb128\t" + Twine(Value));
216 // FIXME: This shouldn't be an error!
217 return TokError("LEB128 not supported yet");
222 MCAsmParserExtension *createELFAsmParser() {
223 return new ELFAsmParser;