blob: 1970124b2d8f48113ff1ff09d28edae59e369213 [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"
Michael J. Spencere90ea132010-10-09 03:47:55 +000011#include "llvm/ADT/StringSwitch.h"
Eli Friedmandc1ad222010-07-17 06:27:28 +000012#include "llvm/ADT/Twine.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000013#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000014#include "llvm/MC/MCContext.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000015#include "llvm/MC/MCParser/MCAsmLexer.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000016#include "llvm/MC/MCSectionELF.h"
17#include "llvm/MC/MCStreamer.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");
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000051 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
Michael J. Spencere90ea132010-10-09 03:47:55 +000052 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
Rafael Espindola61e3b912010-10-26 19:35:47 +000053 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
Daniel Dunbar5146a092010-07-12 21:23:32 +000054 }
55
Rafael Espindolad80781b2010-09-15 21:48:40 +000056 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
57 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000058 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindola0453dd92010-09-27 21:40:27 +000059 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
Daniel Dunbar5146a092010-07-12 21:23:32 +000060 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
61 SectionKind::getDataRel());
62 }
63 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindola0453dd92010-09-27 21:40:27 +000064 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
Daniel Dunbar5146a092010-07-12 21:23:32 +000065 MCSectionELF::SHF_EXECINSTR |
66 MCSectionELF::SHF_ALLOC, SectionKind::getText());
67 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000068 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindola0453dd92010-09-27 21:40:27 +000069 return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +000070 MCSectionELF::SHF_WRITE |
71 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
72 }
73 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
74 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
75 MCSectionELF::SHF_ALLOC,
76 SectionKind::getReadOnly());
77 }
78 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
79 return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
80 MCSectionELF::SHF_ALLOC |
81 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
82 SectionKind::getThreadData());
83 }
84 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
85 return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
86 MCSectionELF::SHF_ALLOC |
87 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
88 SectionKind::getThreadBSS());
89 }
90 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
91 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
92 MCSectionELF::SHF_ALLOC |
93 MCSectionELF::SHF_WRITE,
94 SectionKind::getDataRel());
95 }
96 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
97 return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
98 MCSectionELF::SHF_ALLOC |
99 MCSectionELF::SHF_WRITE,
100 SectionKind::getReadOnlyWithRel());
101 }
102 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
103 return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
104 MCSectionELF::SHF_ALLOC |
105 MCSectionELF::SHF_WRITE,
106 SectionKind::getReadOnlyWithRelLocal());
107 }
108 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
109 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
110 MCSectionELF::SHF_ALLOC |
111 MCSectionELF::SHF_WRITE,
112 SectionKind::getDataRel());
113 }
Eli Friedman21444ef2010-07-17 04:29:04 +0000114 bool ParseDirectiveSection(StringRef, SMLoc);
115 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000116 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000117 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000118 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000119
120private:
121 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000122};
123
124}
125
126bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
127 unsigned Flags, SectionKind Kind) {
128 if (getLexer().isNot(AsmToken::EndOfStatement))
129 return TokError("unexpected token in section switching directive");
130 Lex();
131
132 getStreamer().SwitchSection(getContext().getELFSection(
133 Section, Type, Flags, Kind));
134
135 return false;
136}
137
Eli Friedman21444ef2010-07-17 04:29:04 +0000138bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000139 StringRef Name;
140 if (getParser().ParseIdentifier(Name))
141 return TokError("expected identifier in directive");
142 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
143
144 if (getLexer().isNot(AsmToken::Comma))
145 return TokError("unexpected token in directive");
146 Lex();
147
148 const MCExpr *Expr;
149 if (getParser().ParseExpression(Expr))
150 return true;
151
152 if (getLexer().isNot(AsmToken::EndOfStatement))
153 return TokError("unexpected token in directive");
154
155 getStreamer().EmitELFSize(Sym, Expr);
156 return false;
157}
158
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000159bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
160 // A section name can contain -, so we cannot just use
161 // ParseIdentifier.
162 SMLoc FirstLoc = getLexer().getLoc();
163 unsigned Size = 0;
164
165 for (;;) {
166 StringRef Tmp;
167 unsigned CurSize;
168
169 SMLoc PrevLoc = getLexer().getLoc();
170 if (getLexer().is(AsmToken::Minus)) {
171 CurSize = 1;
172 Lex(); // Consume the "-".
173 } else if (!getParser().ParseIdentifier(Tmp))
174 CurSize = Tmp.size();
175 else
176 break;
177
178 Size += CurSize;
179 SectionName = StringRef(FirstLoc.getPointer(), Size);
180
181 // Make sure the following token is adjacent.
182 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
183 break;
184 }
185 if (Size == 0)
186 return true;
187
188 return false;
189}
190
Eli Friedman21444ef2010-07-17 04:29:04 +0000191// FIXME: This is a work in progress.
192bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
193 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000194
195 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000196 return TokError("expected identifier in directive");
197
198 std::string FlagsStr;
199 StringRef TypeName;
200 int64_t Size = 0;
201 if (getLexer().is(AsmToken::Comma)) {
202 Lex();
203
204 if (getLexer().isNot(AsmToken::String))
205 return TokError("expected string in directive");
206
207 FlagsStr = getTok().getStringContents();
208 Lex();
209
210 AsmToken::TokenKind TypeStartToken;
211 if (getContext().getAsmInfo().getCommentString()[0] == '@')
212 TypeStartToken = AsmToken::Percent;
213 else
214 TypeStartToken = AsmToken::At;
215
216 if (getLexer().is(AsmToken::Comma)) {
217 Lex();
218 if (getLexer().is(TypeStartToken)) {
219 Lex();
220 if (getParser().ParseIdentifier(TypeName))
221 return TokError("expected identifier in directive");
222
223 if (getLexer().is(AsmToken::Comma)) {
224 Lex();
225
226 if (getParser().ParseAbsoluteExpression(Size))
227 return true;
228
229 if (Size <= 0)
230 return TokError("section size must be positive");
231 }
232 }
233 }
234 }
235
236 if (getLexer().isNot(AsmToken::EndOfStatement))
237 return TokError("unexpected token in directive");
238
239 unsigned Flags = 0;
240 for (unsigned i = 0; i < FlagsStr.size(); i++) {
241 switch (FlagsStr[i]) {
242 case 'a':
243 Flags |= MCSectionELF::SHF_ALLOC;
244 break;
245 case 'x':
246 Flags |= MCSectionELF::SHF_EXECINSTR;
247 break;
248 case 'w':
249 Flags |= MCSectionELF::SHF_WRITE;
250 break;
251 case 'M':
252 Flags |= MCSectionELF::SHF_MERGE;
253 break;
254 case 'S':
255 Flags |= MCSectionELF::SHF_STRINGS;
256 break;
257 case 'T':
258 Flags |= MCSectionELF::SHF_TLS;
259 break;
260 case 'c':
261 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
262 break;
263 case 'd':
264 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
265 break;
266 default:
267 return TokError("unknown flag");
268 }
269 }
270
271 unsigned Type = MCSectionELF::SHT_NULL;
272 if (!TypeName.empty()) {
273 if (TypeName == "init_array")
274 Type = MCSectionELF::SHT_INIT_ARRAY;
275 else if (TypeName == "fini_array")
276 Type = MCSectionELF::SHT_FINI_ARRAY;
277 else if (TypeName == "preinit_array")
278 Type = MCSectionELF::SHT_PREINIT_ARRAY;
279 else if (TypeName == "nobits")
280 Type = MCSectionELF::SHT_NOBITS;
281 else if (TypeName == "progbits")
282 Type = MCSectionELF::SHT_PROGBITS;
283 else
284 return TokError("unknown section type");
285 }
286
287 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
288 ? SectionKind::getText()
289 : SectionKind::getDataRel();
290 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Jan Wen Voung12ad94e2010-09-30 02:41:46 +0000291 Flags, Kind, false,
292 Size));
Eli Friedman21444ef2010-07-17 04:29:04 +0000293 return false;
294}
295
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000296bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
297 const MCSection *PreviousSection = getStreamer().getPreviousSection();
298 if (PreviousSection != NULL)
299 getStreamer().SwitchSection(PreviousSection);
300
301 return false;
302}
303
Michael J. Spencere90ea132010-10-09 03:47:55 +0000304/// ParseDirectiveELFType
305/// ::= .type identifier , @attribute
306bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
307 StringRef Name;
308 if (getParser().ParseIdentifier(Name))
309 return TokError("expected identifier in directive");
310
311 // Handle the identifier as the key symbol.
312 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
313
314 if (getLexer().isNot(AsmToken::Comma))
315 return TokError("unexpected token in '.type' directive");
316 Lex();
317
318 if (getLexer().isNot(AsmToken::At))
319 return TokError("expected '@' before type");
320 Lex();
321
322 StringRef Type;
323 SMLoc TypeLoc;
324
325 TypeLoc = getLexer().getLoc();
326 if (getParser().ParseIdentifier(Type))
327 return TokError("expected symbol type in directive");
328
329 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
330 .Case("function", MCSA_ELF_TypeFunction)
331 .Case("object", MCSA_ELF_TypeObject)
332 .Case("tls_object", MCSA_ELF_TypeTLS)
333 .Case("common", MCSA_ELF_TypeCommon)
334 .Case("notype", MCSA_ELF_TypeNoType)
335 .Default(MCSA_Invalid);
336
337 if (Attr == MCSA_Invalid)
338 return Error(TypeLoc, "unsupported attribute in '.type' directive");
339
340 if (getLexer().isNot(AsmToken::EndOfStatement))
341 return TokError("unexpected token in '.type' directive");
342
343 Lex();
344
345 getStreamer().EmitSymbolAttribute(Sym, Attr);
346
347 return false;
348}
349
Rafael Espindola61e3b912010-10-26 19:35:47 +0000350/// ParseDirectiveIdent
351/// ::= .ident string
352bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
353 if (getLexer().isNot(AsmToken::String))
354 return TokError("unexpected token in '.ident' directive");
355
356 StringRef Data = getTok().getIdentifier();
357
358 Lex();
359
360 const MCSection *OldSection = getStreamer().getCurrentSection();
361 const MCSection *Comment =
362 getContext().getELFSection(".comment", MCSectionELF::SHT_PROGBITS,
363 MCSectionELF::SHF_MERGE |
364 MCSectionELF::SHF_STRINGS,
365 SectionKind::getReadOnly(),
366 false, 1);
367
368 static bool First = true;
369
370 getStreamer().SwitchSection(Comment);
371 if (First)
372 getStreamer().EmitIntValue(0, 1);
373 First = false;
374 getStreamer().EmitBytes(Data, 0);
375 getStreamer().EmitIntValue(0, 1);
376 getStreamer().SwitchSection(OldSection);
377 return false;
378}
379
Daniel Dunbar5146a092010-07-12 21:23:32 +0000380namespace llvm {
381
382MCAsmParserExtension *createELFAsmParser() {
383 return new ELFAsmParser;
384}
385
386}