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 template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
24 void AddDirectiveHandler(StringRef Directive) {
25 getParser().AddDirectiveHandler(this, Directive,
26 HandleDirective<ELFAsmParser, Handler>);
29 bool ParseSectionSwitch(StringRef Section, unsigned Type,
30 unsigned Flags, SectionKind Kind);
35 virtual void Initialize(MCAsmParser &Parser) {
36 // Call the base implementation.
37 this->MCAsmParserExtension::Initialize(Parser);
39 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
40 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
41 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
42 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
43 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
44 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
45 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
46 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
48 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
49 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
50 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
51 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveLEB128>(".sleb128");
52 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveLEB128>(".uleb128");
53 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
56 bool ParseSectionDirectiveData(StringRef, SMLoc) {
57 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
58 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
59 SectionKind::getDataRel());
61 bool ParseSectionDirectiveText(StringRef, SMLoc) {
62 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
63 MCSectionELF::SHF_EXECINSTR |
64 MCSectionELF::SHF_ALLOC, SectionKind::getText());
66 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
67 return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
68 MCSectionELF::SHF_WRITE |
69 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
71 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
72 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
73 MCSectionELF::SHF_ALLOC,
74 SectionKind::getReadOnly());
76 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
77 return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
78 MCSectionELF::SHF_ALLOC |
79 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
80 SectionKind::getThreadData());
82 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
83 return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
84 MCSectionELF::SHF_ALLOC |
85 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
86 SectionKind::getThreadBSS());
88 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
89 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
90 MCSectionELF::SHF_ALLOC |
91 MCSectionELF::SHF_WRITE,
92 SectionKind::getDataRel());
94 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
95 return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
96 MCSectionELF::SHF_ALLOC |
97 MCSectionELF::SHF_WRITE,
98 SectionKind::getReadOnlyWithRel());
100 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
101 return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
102 MCSectionELF::SHF_ALLOC |
103 MCSectionELF::SHF_WRITE,
104 SectionKind::getReadOnlyWithRelLocal());
106 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
107 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
108 MCSectionELF::SHF_ALLOC |
109 MCSectionELF::SHF_WRITE,
110 SectionKind::getDataRel());
112 bool ParseDirectiveLEB128(StringRef, SMLoc);
113 bool ParseDirectiveSection(StringRef, SMLoc);
114 bool ParseDirectiveSize(StringRef, SMLoc);
115 bool ParseDirectivePrevious(StringRef, SMLoc);
120 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
121 unsigned Flags, SectionKind Kind) {
122 if (getLexer().isNot(AsmToken::EndOfStatement))
123 return TokError("unexpected token in section switching directive");
126 getStreamer().SwitchSection(getContext().getELFSection(
127 Section, Type, Flags, Kind));
132 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
134 if (getParser().ParseIdentifier(Name))
135 return TokError("expected identifier in directive");
136 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
138 if (getLexer().isNot(AsmToken::Comma))
139 return TokError("unexpected token in directive");
143 if (getParser().ParseExpression(Expr))
146 if (getLexer().isNot(AsmToken::EndOfStatement))
147 return TokError("unexpected token in directive");
149 getStreamer().EmitELFSize(Sym, Expr);
153 // FIXME: This is a work in progress.
154 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
155 StringRef SectionName;
156 // FIXME: This doesn't parse section names like ".note.GNU-stack" correctly.
157 if (getParser().ParseIdentifier(SectionName))
158 return TokError("expected identifier in directive");
160 std::string FlagsStr;
163 if (getLexer().is(AsmToken::Comma)) {
166 if (getLexer().isNot(AsmToken::String))
167 return TokError("expected string in directive");
169 FlagsStr = getTok().getStringContents();
172 AsmToken::TokenKind TypeStartToken;
173 if (getContext().getAsmInfo().getCommentString()[0] == '@')
174 TypeStartToken = AsmToken::Percent;
176 TypeStartToken = AsmToken::At;
178 if (getLexer().is(AsmToken::Comma)) {
180 if (getLexer().is(TypeStartToken)) {
182 if (getParser().ParseIdentifier(TypeName))
183 return TokError("expected identifier in directive");
185 if (getLexer().is(AsmToken::Comma)) {
188 if (getParser().ParseAbsoluteExpression(Size))
192 return TokError("section size must be positive");
198 if (getLexer().isNot(AsmToken::EndOfStatement))
199 return TokError("unexpected token in directive");
202 for (unsigned i = 0; i < FlagsStr.size(); i++) {
203 switch (FlagsStr[i]) {
205 Flags |= MCSectionELF::SHF_ALLOC;
208 Flags |= MCSectionELF::SHF_EXECINSTR;
211 Flags |= MCSectionELF::SHF_WRITE;
214 Flags |= MCSectionELF::SHF_MERGE;
217 Flags |= MCSectionELF::SHF_STRINGS;
220 Flags |= MCSectionELF::SHF_TLS;
223 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
226 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
229 return TokError("unknown flag");
233 unsigned Type = MCSectionELF::SHT_NULL;
234 if (!TypeName.empty()) {
235 if (TypeName == "init_array")
236 Type = MCSectionELF::SHT_INIT_ARRAY;
237 else if (TypeName == "fini_array")
238 Type = MCSectionELF::SHT_FINI_ARRAY;
239 else if (TypeName == "preinit_array")
240 Type = MCSectionELF::SHT_PREINIT_ARRAY;
241 else if (TypeName == "nobits")
242 Type = MCSectionELF::SHT_NOBITS;
243 else if (TypeName == "progbits")
244 Type = MCSectionELF::SHT_PROGBITS;
246 return TokError("unknown section type");
249 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
250 ? SectionKind::getText()
251 : SectionKind::getDataRel();
252 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
253 Flags, Kind, false));
257 bool ELFAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
259 if (getParser().ParseAbsoluteExpression(Value))
262 if (getLexer().isNot(AsmToken::EndOfStatement))
263 return TokError("unexpected token in directive");
265 // FIXME: Add proper MC support.
266 if (getContext().getAsmInfo().hasLEB128()) {
267 if (DirName[1] == 's')
268 getStreamer().EmitRawText("\t.sleb128\t" + Twine(Value));
270 getStreamer().EmitRawText("\t.uleb128\t" + Twine(Value));
273 // FIXME: This shouldn't be an error!
274 return TokError("LEB128 not supported yet");
277 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
278 const MCSection *PreviousSection = getStreamer().getPreviousSection();
279 if (PreviousSection != NULL)
280 getStreamer().SwitchSection(PreviousSection);
287 MCAsmParserExtension *createELFAsmParser() {
288 return new ELFAsmParser;