blob: b3d5f11e0eb62faa2b3df5d8296a5a82c3a41f8d [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"
Rafael Espindola88182132010-10-27 15:18:17 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000016#include "llvm/MC/MCParser/MCAsmLexer.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000017#include "llvm/MC/MCSectionELF.h"
18#include "llvm/MC/MCStreamer.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000019using namespace llvm;
20
21namespace {
22
23class ELFAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000024 template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
25 void AddDirectiveHandler(StringRef Directive) {
26 getParser().AddDirectiveHandler(this, Directive,
27 HandleDirective<ELFAsmParser, Handler>);
28 }
29
Daniel Dunbar5146a092010-07-12 21:23:32 +000030 bool ParseSectionSwitch(StringRef Section, unsigned Type,
31 unsigned Flags, SectionKind Kind);
32
33public:
34 ELFAsmParser() {}
35
36 virtual void Initialize(MCAsmParser &Parser) {
37 // Call the base implementation.
38 this->MCAsmParserExtension::Initialize(Parser);
39
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000040 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
41 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
Matt Flemingf525c2a2010-07-20 21:12:46 +000042 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
43 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
44 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
45 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
46 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
48 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
49 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000050 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
51 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000052 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
Michael J. Spencere90ea132010-10-09 03:47:55 +000053 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
Rafael Espindola61e3b912010-10-26 19:35:47 +000054 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
Rafael Espindola88182132010-10-27 15:18:17 +000055 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
Rafael Espindola484291c2010-11-01 14:28:48 +000056 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
Daniel Dunbar5146a092010-07-12 21:23:32 +000057 }
58
Rafael Espindolad80781b2010-09-15 21:48:40 +000059 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
60 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000061 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindola0453dd92010-09-27 21:40:27 +000062 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
Daniel Dunbar5146a092010-07-12 21:23:32 +000063 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
64 SectionKind::getDataRel());
65 }
66 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindola0453dd92010-09-27 21:40:27 +000067 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
Daniel Dunbar5146a092010-07-12 21:23:32 +000068 MCSectionELF::SHF_EXECINSTR |
69 MCSectionELF::SHF_ALLOC, SectionKind::getText());
70 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000071 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindola0453dd92010-09-27 21:40:27 +000072 return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +000073 MCSectionELF::SHF_WRITE |
74 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
75 }
76 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
77 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
78 MCSectionELF::SHF_ALLOC,
79 SectionKind::getReadOnly());
80 }
81 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
82 return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
83 MCSectionELF::SHF_ALLOC |
84 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
85 SectionKind::getThreadData());
86 }
87 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
88 return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
89 MCSectionELF::SHF_ALLOC |
90 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
91 SectionKind::getThreadBSS());
92 }
93 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
94 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
95 MCSectionELF::SHF_ALLOC |
96 MCSectionELF::SHF_WRITE,
97 SectionKind::getDataRel());
98 }
99 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
100 return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
101 MCSectionELF::SHF_ALLOC |
102 MCSectionELF::SHF_WRITE,
103 SectionKind::getReadOnlyWithRel());
104 }
105 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
106 return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
107 MCSectionELF::SHF_ALLOC |
108 MCSectionELF::SHF_WRITE,
109 SectionKind::getReadOnlyWithRelLocal());
110 }
111 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
112 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
113 MCSectionELF::SHF_ALLOC |
114 MCSectionELF::SHF_WRITE,
115 SectionKind::getDataRel());
116 }
Eli Friedman21444ef2010-07-17 04:29:04 +0000117 bool ParseDirectiveSection(StringRef, SMLoc);
118 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000119 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000120 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000121 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000122 bool ParseDirectiveSymver(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000123 bool ParseDirectiveWeakref(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000124
125private:
126 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000127};
128
129}
130
131bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
132 unsigned Flags, SectionKind Kind) {
133 if (getLexer().isNot(AsmToken::EndOfStatement))
134 return TokError("unexpected token in section switching directive");
135 Lex();
136
137 getStreamer().SwitchSection(getContext().getELFSection(
138 Section, Type, Flags, Kind));
139
140 return false;
141}
142
Eli Friedman21444ef2010-07-17 04:29:04 +0000143bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000144 StringRef Name;
145 if (getParser().ParseIdentifier(Name))
146 return TokError("expected identifier in directive");
147 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
148
149 if (getLexer().isNot(AsmToken::Comma))
150 return TokError("unexpected token in directive");
151 Lex();
152
153 const MCExpr *Expr;
154 if (getParser().ParseExpression(Expr))
155 return true;
156
157 if (getLexer().isNot(AsmToken::EndOfStatement))
158 return TokError("unexpected token in directive");
159
160 getStreamer().EmitELFSize(Sym, Expr);
161 return false;
162}
163
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000164bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
165 // A section name can contain -, so we cannot just use
166 // ParseIdentifier.
167 SMLoc FirstLoc = getLexer().getLoc();
168 unsigned Size = 0;
169
170 for (;;) {
171 StringRef Tmp;
172 unsigned CurSize;
173
174 SMLoc PrevLoc = getLexer().getLoc();
175 if (getLexer().is(AsmToken::Minus)) {
176 CurSize = 1;
177 Lex(); // Consume the "-".
178 } else if (!getParser().ParseIdentifier(Tmp))
179 CurSize = Tmp.size();
180 else
181 break;
182
183 Size += CurSize;
184 SectionName = StringRef(FirstLoc.getPointer(), Size);
185
186 // Make sure the following token is adjacent.
187 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
188 break;
189 }
190 if (Size == 0)
191 return true;
192
193 return false;
194}
195
Eli Friedman21444ef2010-07-17 04:29:04 +0000196// FIXME: This is a work in progress.
197bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
198 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000199
200 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000201 return TokError("expected identifier in directive");
202
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000203 StringRef FlagsStr;
Eli Friedman21444ef2010-07-17 04:29:04 +0000204 StringRef TypeName;
205 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000206 StringRef GroupName;
Eli Friedman21444ef2010-07-17 04:29:04 +0000207 if (getLexer().is(AsmToken::Comma)) {
208 Lex();
209
210 if (getLexer().isNot(AsmToken::String))
211 return TokError("expected string in directive");
212
213 FlagsStr = getTok().getStringContents();
214 Lex();
215
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000216 bool Mergeable = FlagsStr.find('M') != StringRef::npos;
217 bool Group = FlagsStr.find('G') != StringRef::npos;
Eli Friedman21444ef2010-07-17 04:29:04 +0000218
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000219 if (getLexer().isNot(AsmToken::Comma)) {
220 if (Mergeable)
221 return TokError("Mergeable section must specify the type");
222 if (Group)
223 return TokError("Group section must specify the type");
224 } else {
225 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000226 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
227 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000228
229 Lex();
230 if (getParser().ParseIdentifier(TypeName))
231 return TokError("expected identifier in directive");
232
233 if (Mergeable) {
234 if (getLexer().isNot(AsmToken::Comma))
235 return TokError("expected the entry size");
236 Lex();
237 if (getParser().ParseAbsoluteExpression(Size))
238 return true;
239 if (Size <= 0)
240 return TokError("entry size must be positive");
241 }
242
243 if (Group) {
244 if (getLexer().isNot(AsmToken::Comma))
245 return TokError("expected group name");
246 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000247 if (getParser().ParseIdentifier(GroupName))
248 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000249 if (getLexer().is(AsmToken::Comma)) {
250 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000251 StringRef Linkage;
252 if (getParser().ParseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000253 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000254 if (Linkage != "comdat")
255 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000256 }
257 }
258 }
259 }
260
261 if (getLexer().isNot(AsmToken::EndOfStatement))
262 return TokError("unexpected token in directive");
263
264 unsigned Flags = 0;
Rafael Espindola4fa34782010-10-27 18:45:20 +0000265 unsigned Type = MCSectionELF::SHT_NULL;
266
267 // Set the defaults first.
Rafael Espindola4faf7c72010-11-08 02:47:59 +0000268 if (SectionName == ".fini" || SectionName == ".init" || SectionName == ".rodata") {
Rafael Espindola4fa34782010-10-27 18:45:20 +0000269 Type = MCSectionELF::SHT_PROGBITS;
270 Flags |= MCSectionELF::SHF_ALLOC;
Rafael Espindola4faf7c72010-11-08 02:47:59 +0000271 }
272 if (SectionName == ".fini" || SectionName == ".init") {
Rafael Espindola4fa34782010-10-27 18:45:20 +0000273 Flags |= MCSectionELF::SHF_EXECINSTR;
274 }
275
Eli Friedman21444ef2010-07-17 04:29:04 +0000276 for (unsigned i = 0; i < FlagsStr.size(); i++) {
277 switch (FlagsStr[i]) {
278 case 'a':
279 Flags |= MCSectionELF::SHF_ALLOC;
280 break;
281 case 'x':
282 Flags |= MCSectionELF::SHF_EXECINSTR;
283 break;
284 case 'w':
285 Flags |= MCSectionELF::SHF_WRITE;
286 break;
287 case 'M':
288 Flags |= MCSectionELF::SHF_MERGE;
289 break;
290 case 'S':
291 Flags |= MCSectionELF::SHF_STRINGS;
292 break;
293 case 'T':
294 Flags |= MCSectionELF::SHF_TLS;
295 break;
296 case 'c':
297 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
298 break;
299 case 'd':
300 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
301 break;
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000302 case 'G':
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000303 Flags |= MCSectionELF::SHF_GROUP;
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000304 break;
Eli Friedman21444ef2010-07-17 04:29:04 +0000305 default:
306 return TokError("unknown flag");
307 }
308 }
309
Eli Friedman21444ef2010-07-17 04:29:04 +0000310 if (!TypeName.empty()) {
311 if (TypeName == "init_array")
312 Type = MCSectionELF::SHT_INIT_ARRAY;
313 else if (TypeName == "fini_array")
314 Type = MCSectionELF::SHT_FINI_ARRAY;
315 else if (TypeName == "preinit_array")
316 Type = MCSectionELF::SHT_PREINIT_ARRAY;
317 else if (TypeName == "nobits")
318 Type = MCSectionELF::SHT_NOBITS;
319 else if (TypeName == "progbits")
320 Type = MCSectionELF::SHT_PROGBITS;
321 else
322 return TokError("unknown section type");
323 }
324
325 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
326 ? SectionKind::getText()
327 : SectionKind::getDataRel();
328 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000329 Flags, Kind, Size,
330 GroupName));
Eli Friedman21444ef2010-07-17 04:29:04 +0000331 return false;
332}
333
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000334bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
335 const MCSection *PreviousSection = getStreamer().getPreviousSection();
336 if (PreviousSection != NULL)
337 getStreamer().SwitchSection(PreviousSection);
338
339 return false;
340}
341
Michael J. Spencere90ea132010-10-09 03:47:55 +0000342/// ParseDirectiveELFType
343/// ::= .type identifier , @attribute
344bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
345 StringRef Name;
346 if (getParser().ParseIdentifier(Name))
347 return TokError("expected identifier in directive");
348
349 // Handle the identifier as the key symbol.
350 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
351
352 if (getLexer().isNot(AsmToken::Comma))
353 return TokError("unexpected token in '.type' directive");
354 Lex();
355
Rafael Espindolad4a35262010-11-12 15:47:08 +0000356 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
357 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000358 Lex();
359
360 StringRef Type;
361 SMLoc TypeLoc;
362
363 TypeLoc = getLexer().getLoc();
364 if (getParser().ParseIdentifier(Type))
365 return TokError("expected symbol type in directive");
366
367 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
368 .Case("function", MCSA_ELF_TypeFunction)
369 .Case("object", MCSA_ELF_TypeObject)
370 .Case("tls_object", MCSA_ELF_TypeTLS)
371 .Case("common", MCSA_ELF_TypeCommon)
372 .Case("notype", MCSA_ELF_TypeNoType)
373 .Default(MCSA_Invalid);
374
375 if (Attr == MCSA_Invalid)
376 return Error(TypeLoc, "unsupported attribute in '.type' directive");
377
378 if (getLexer().isNot(AsmToken::EndOfStatement))
379 return TokError("unexpected token in '.type' directive");
380
381 Lex();
382
383 getStreamer().EmitSymbolAttribute(Sym, Attr);
384
385 return false;
386}
387
Rafael Espindola61e3b912010-10-26 19:35:47 +0000388/// ParseDirectiveIdent
389/// ::= .ident string
390bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
391 if (getLexer().isNot(AsmToken::String))
392 return TokError("unexpected token in '.ident' directive");
393
394 StringRef Data = getTok().getIdentifier();
395
396 Lex();
397
398 const MCSection *OldSection = getStreamer().getCurrentSection();
399 const MCSection *Comment =
400 getContext().getELFSection(".comment", MCSectionELF::SHT_PROGBITS,
401 MCSectionELF::SHF_MERGE |
402 MCSectionELF::SHF_STRINGS,
403 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000404 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000405
406 static bool First = true;
407
408 getStreamer().SwitchSection(Comment);
409 if (First)
410 getStreamer().EmitIntValue(0, 1);
411 First = false;
412 getStreamer().EmitBytes(Data, 0);
413 getStreamer().EmitIntValue(0, 1);
414 getStreamer().SwitchSection(OldSection);
415 return false;
416}
417
Rafael Espindola88182132010-10-27 15:18:17 +0000418/// ParseDirectiveSymver
419/// ::= .symver foo, bar2@zed
420bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
421 StringRef Name;
422 if (getParser().ParseIdentifier(Name))
423 return TokError("expected identifier in directive");
424
425 if (getLexer().isNot(AsmToken::Comma))
426 return TokError("expected a comma");
427
428 Lex();
429
430 StringRef AliasName;
431 if (getParser().ParseIdentifier(AliasName))
432 return TokError("expected identifier in directive");
433
434 if (AliasName.find('@') == StringRef::npos)
435 return TokError("expected a '@' in the name");
436
437 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
438 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
439 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
440
441 getStreamer().EmitAssignment(Alias, Value);
442 return false;
443}
444
Rafael Espindola484291c2010-11-01 14:28:48 +0000445/// ParseDirectiveWeakref
446/// ::= .weakref foo, bar
447bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
448 // FIXME: Share code with the other alias building directives.
449
450 StringRef AliasName;
451 if (getParser().ParseIdentifier(AliasName))
452 return TokError("expected identifier in directive");
453
454 if (getLexer().isNot(AsmToken::Comma))
455 return TokError("expected a comma");
456
457 Lex();
458
459 StringRef Name;
460 if (getParser().ParseIdentifier(Name))
461 return TokError("expected identifier in directive");
462
463 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
464
465 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
466
467 getStreamer().EmitWeakReference(Alias, Sym);
468 return false;
469}
470
Daniel Dunbar5146a092010-07-12 21:23:32 +0000471namespace llvm {
472
473MCAsmParserExtension *createELFAsmParser() {
474 return new ELFAsmParser;
475}
476
477}