blob: f982fdaecb1232f59a927f7f74f97368ff5d59e6 [file] [log] [blame]
Daniel Dunbar5146a092010-07-12 21:23:32 +00001//===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCParser/MCAsmParserExtension.h"
Eli Friedmandc1ad222010-07-17 06:27:28 +000011#include "llvm/ADT/Twine.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000012#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000013#include "llvm/MC/MCContext.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000014#include "llvm/MC/MCParser/MCAsmLexer.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000015#include "llvm/MC/MCSectionELF.h"
16#include "llvm/MC/MCStreamer.h"
Daniel Dunbarf21e4e92010-07-18 18:31:42 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000018using namespace llvm;
19
20namespace {
21
22class ELFAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000023 template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
24 void AddDirectiveHandler(StringRef Directive) {
25 getParser().AddDirectiveHandler(this, Directive,
26 HandleDirective<ELFAsmParser, Handler>);
27 }
28
Daniel Dunbar5146a092010-07-12 21:23:32 +000029 bool ParseSectionSwitch(StringRef Section, unsigned Type,
30 unsigned Flags, SectionKind Kind);
31
32public:
33 ELFAsmParser() {}
34
35 virtual void Initialize(MCAsmParser &Parser) {
36 // Call the base implementation.
37 this->MCAsmParserExtension::Initialize(Parser);
38
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000039 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
40 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
Matt Flemingf525c2a2010-07-20 21:12:46 +000041 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");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000049 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
50 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
51 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveLEB128>(".sleb128");
52 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveLEB128>(".uleb128");
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000053 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
Daniel Dunbar5146a092010-07-12 21:23:32 +000054 }
55
56 bool ParseSectionDirectiveData(StringRef, SMLoc) {
57 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
58 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
59 SectionKind::getDataRel());
60 }
61 bool ParseSectionDirectiveText(StringRef, SMLoc) {
62 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
63 MCSectionELF::SHF_EXECINSTR |
64 MCSectionELF::SHF_ALLOC, SectionKind::getText());
65 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000066 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
67 return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
68 MCSectionELF::SHF_WRITE |
69 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
70 }
71 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
72 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
73 MCSectionELF::SHF_ALLOC,
74 SectionKind::getReadOnly());
75 }
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());
81 }
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());
87 }
88 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
89 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
90 MCSectionELF::SHF_ALLOC |
91 MCSectionELF::SHF_WRITE,
92 SectionKind::getDataRel());
93 }
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());
99 }
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());
105 }
106 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
107 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
108 MCSectionELF::SHF_ALLOC |
109 MCSectionELF::SHF_WRITE,
110 SectionKind::getDataRel());
111 }
Eli Friedmandc1ad222010-07-17 06:27:28 +0000112 bool ParseDirectiveLEB128(StringRef, SMLoc);
Eli Friedman21444ef2010-07-17 04:29:04 +0000113 bool ParseDirectiveSection(StringRef, SMLoc);
114 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000115 bool ParseDirectivePrevious(StringRef, SMLoc);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000116};
117
118}
119
120bool 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");
124 Lex();
125
126 getStreamer().SwitchSection(getContext().getELFSection(
127 Section, Type, Flags, Kind));
128
129 return false;
130}
131
Eli Friedman21444ef2010-07-17 04:29:04 +0000132bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000133 StringRef Name;
134 if (getParser().ParseIdentifier(Name))
135 return TokError("expected identifier in directive");
136 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
137
138 if (getLexer().isNot(AsmToken::Comma))
139 return TokError("unexpected token in directive");
140 Lex();
141
142 const MCExpr *Expr;
143 if (getParser().ParseExpression(Expr))
144 return true;
145
146 if (getLexer().isNot(AsmToken::EndOfStatement))
147 return TokError("unexpected token in directive");
148
149 getStreamer().EmitELFSize(Sym, Expr);
150 return false;
151}
152
Eli Friedman21444ef2010-07-17 04:29:04 +0000153// FIXME: This is a work in progress.
154bool 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");
159
160 std::string FlagsStr;
161 StringRef TypeName;
162 int64_t Size = 0;
163 if (getLexer().is(AsmToken::Comma)) {
164 Lex();
165
166 if (getLexer().isNot(AsmToken::String))
167 return TokError("expected string in directive");
168
169 FlagsStr = getTok().getStringContents();
170 Lex();
171
172 AsmToken::TokenKind TypeStartToken;
173 if (getContext().getAsmInfo().getCommentString()[0] == '@')
174 TypeStartToken = AsmToken::Percent;
175 else
176 TypeStartToken = AsmToken::At;
177
178 if (getLexer().is(AsmToken::Comma)) {
179 Lex();
180 if (getLexer().is(TypeStartToken)) {
181 Lex();
182 if (getParser().ParseIdentifier(TypeName))
183 return TokError("expected identifier in directive");
184
185 if (getLexer().is(AsmToken::Comma)) {
186 Lex();
187
188 if (getParser().ParseAbsoluteExpression(Size))
189 return true;
190
191 if (Size <= 0)
192 return TokError("section size must be positive");
193 }
194 }
195 }
196 }
197
198 if (getLexer().isNot(AsmToken::EndOfStatement))
199 return TokError("unexpected token in directive");
200
201 unsigned Flags = 0;
202 for (unsigned i = 0; i < FlagsStr.size(); i++) {
203 switch (FlagsStr[i]) {
204 case 'a':
205 Flags |= MCSectionELF::SHF_ALLOC;
206 break;
207 case 'x':
208 Flags |= MCSectionELF::SHF_EXECINSTR;
209 break;
210 case 'w':
211 Flags |= MCSectionELF::SHF_WRITE;
212 break;
213 case 'M':
214 Flags |= MCSectionELF::SHF_MERGE;
215 break;
216 case 'S':
217 Flags |= MCSectionELF::SHF_STRINGS;
218 break;
219 case 'T':
220 Flags |= MCSectionELF::SHF_TLS;
221 break;
222 case 'c':
223 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
224 break;
225 case 'd':
226 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
227 break;
228 default:
229 return TokError("unknown flag");
230 }
231 }
232
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;
245 else
246 return TokError("unknown section type");
247 }
248
249 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
250 ? SectionKind::getText()
251 : SectionKind::getDataRel();
252 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
253 Flags, Kind, false));
254 return false;
255}
256
Eli Friedmandc1ad222010-07-17 06:27:28 +0000257bool ELFAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
258 int64_t Value;
259 if (getParser().ParseAbsoluteExpression(Value))
260 return true;
261
262 if (getLexer().isNot(AsmToken::EndOfStatement))
263 return TokError("unexpected token in directive");
264
265 // FIXME: Add proper MC support.
266 if (getContext().getAsmInfo().hasLEB128()) {
267 if (DirName[1] == 's')
268 getStreamer().EmitRawText("\t.sleb128\t" + Twine(Value));
269 else
270 getStreamer().EmitRawText("\t.uleb128\t" + Twine(Value));
271 return false;
272 }
273 // FIXME: This shouldn't be an error!
274 return TokError("LEB128 not supported yet");
275}
276
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000277bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
278 const MCSection *PreviousSection = getStreamer().getPreviousSection();
279 if (PreviousSection != NULL)
280 getStreamer().SwitchSection(PreviousSection);
281
282 return false;
283}
284
Daniel Dunbar5146a092010-07-12 21:23:32 +0000285namespace llvm {
286
287MCAsmParserExtension *createELFAsmParser() {
288 return new ELFAsmParser;
289}
290
291}