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"
17 #include "llvm/ADT/Twine.h"
22 class ELFAsmParser : public MCAsmParserExtension {
23 bool ParseSectionSwitch(StringRef Section, unsigned Type,
24 unsigned Flags, SectionKind Kind);
29 virtual void Initialize(MCAsmParser &Parser) {
30 // Call the base implementation.
31 this->MCAsmParserExtension::Initialize(Parser);
33 Parser.AddDirectiveHandler(this, ".data", MCAsmParser::DirectiveHandler(
34 &ELFAsmParser::ParseSectionDirectiveData));
35 Parser.AddDirectiveHandler(this, ".text", MCAsmParser::DirectiveHandler(
36 &ELFAsmParser::ParseSectionDirectiveText));
37 Parser.AddDirectiveHandler(this, ".section", MCAsmParser::DirectiveHandler(
38 &ELFAsmParser::ParseDirectiveSection));
39 Parser.AddDirectiveHandler(this, ".size", MCAsmParser::DirectiveHandler(
40 &ELFAsmParser::ParseDirectiveSize));
41 Parser.AddDirectiveHandler(this, ".sleb128", MCAsmParser::DirectiveHandler(
42 &ELFAsmParser::ParseDirectiveLEB128));
43 Parser.AddDirectiveHandler(this, ".uleb128", MCAsmParser::DirectiveHandler(
44 &ELFAsmParser::ParseDirectiveLEB128));
47 bool ParseSectionDirectiveData(StringRef, SMLoc) {
48 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
49 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
50 SectionKind::getDataRel());
52 bool ParseSectionDirectiveText(StringRef, SMLoc) {
53 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
54 MCSectionELF::SHF_EXECINSTR |
55 MCSectionELF::SHF_ALLOC, SectionKind::getText());
57 bool ParseDirectiveLEB128(StringRef, SMLoc);
58 bool ParseDirectiveSection(StringRef, SMLoc);
59 bool ParseDirectiveSize(StringRef, SMLoc);
64 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
65 unsigned Flags, SectionKind Kind) {
66 if (getLexer().isNot(AsmToken::EndOfStatement))
67 return TokError("unexpected token in section switching directive");
70 getStreamer().SwitchSection(getContext().getELFSection(
71 Section, Type, Flags, Kind));
76 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
78 if (getParser().ParseIdentifier(Name))
79 return TokError("expected identifier in directive");
80 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
82 if (getLexer().isNot(AsmToken::Comma))
83 return TokError("unexpected token in directive");
87 if (getParser().ParseExpression(Expr))
90 if (getLexer().isNot(AsmToken::EndOfStatement))
91 return TokError("unexpected token in directive");
93 getStreamer().EmitELFSize(Sym, Expr);
97 // FIXME: This is a work in progress.
98 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
99 StringRef SectionName;
100 // FIXME: This doesn't parse section names like ".note.GNU-stack" correctly.
101 if (getParser().ParseIdentifier(SectionName))
102 return TokError("expected identifier in directive");
104 std::string FlagsStr;
107 if (getLexer().is(AsmToken::Comma)) {
110 if (getLexer().isNot(AsmToken::String))
111 return TokError("expected string in directive");
113 FlagsStr = getTok().getStringContents();
116 AsmToken::TokenKind TypeStartToken;
117 if (getContext().getAsmInfo().getCommentString()[0] == '@')
118 TypeStartToken = AsmToken::Percent;
120 TypeStartToken = AsmToken::At;
122 if (getLexer().is(AsmToken::Comma)) {
124 if (getLexer().is(TypeStartToken)) {
126 if (getParser().ParseIdentifier(TypeName))
127 return TokError("expected identifier in directive");
129 if (getLexer().is(AsmToken::Comma)) {
132 if (getParser().ParseAbsoluteExpression(Size))
136 return TokError("section size must be positive");
142 if (getLexer().isNot(AsmToken::EndOfStatement))
143 return TokError("unexpected token in directive");
146 for (unsigned i = 0; i < FlagsStr.size(); i++) {
147 switch (FlagsStr[i]) {
149 Flags |= MCSectionELF::SHF_ALLOC;
152 Flags |= MCSectionELF::SHF_EXECINSTR;
155 Flags |= MCSectionELF::SHF_WRITE;
158 Flags |= MCSectionELF::SHF_MERGE;
161 Flags |= MCSectionELF::SHF_STRINGS;
164 Flags |= MCSectionELF::SHF_TLS;
167 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
170 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
173 return TokError("unknown flag");
177 unsigned Type = MCSectionELF::SHT_NULL;
178 if (!TypeName.empty()) {
179 if (TypeName == "init_array")
180 Type = MCSectionELF::SHT_INIT_ARRAY;
181 else if (TypeName == "fini_array")
182 Type = MCSectionELF::SHT_FINI_ARRAY;
183 else if (TypeName == "preinit_array")
184 Type = MCSectionELF::SHT_PREINIT_ARRAY;
185 else if (TypeName == "nobits")
186 Type = MCSectionELF::SHT_NOBITS;
187 else if (TypeName == "progbits")
188 Type = MCSectionELF::SHT_PROGBITS;
190 return TokError("unknown section type");
193 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
194 ? SectionKind::getText()
195 : SectionKind::getDataRel();
196 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
197 Flags, Kind, false));
201 bool ELFAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
203 if (getParser().ParseAbsoluteExpression(Value))
206 if (getLexer().isNot(AsmToken::EndOfStatement))
207 return TokError("unexpected token in directive");
209 // FIXME: Add proper MC support.
210 if (getContext().getAsmInfo().hasLEB128()) {
211 if (DirName[1] == 's')
212 getStreamer().EmitRawText("\t.sleb128\t" + Twine(Value));
214 getStreamer().EmitRawText("\t.uleb128\t" + Twine(Value));
217 // FIXME: This shouldn't be an error!
218 return TokError("LEB128 not supported yet");
223 MCAsmParserExtension *createELFAsmParser() {
224 return new ELFAsmParser;