blob: 62cf6c6e7a0f4d043db34f09ef9ac8822e8b9adb [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");
Daniel Dunbar5146a092010-07-12 21:23:32 +000056 }
57
Rafael Espindolad80781b2010-09-15 21:48:40 +000058 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
59 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000060 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindola0453dd92010-09-27 21:40:27 +000061 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
Daniel Dunbar5146a092010-07-12 21:23:32 +000062 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
63 SectionKind::getDataRel());
64 }
65 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindola0453dd92010-09-27 21:40:27 +000066 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
Daniel Dunbar5146a092010-07-12 21:23:32 +000067 MCSectionELF::SHF_EXECINSTR |
68 MCSectionELF::SHF_ALLOC, SectionKind::getText());
69 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000070 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindola0453dd92010-09-27 21:40:27 +000071 return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +000072 MCSectionELF::SHF_WRITE |
73 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
74 }
75 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
76 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
77 MCSectionELF::SHF_ALLOC,
78 SectionKind::getReadOnly());
79 }
80 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
81 return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
82 MCSectionELF::SHF_ALLOC |
83 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
84 SectionKind::getThreadData());
85 }
86 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
87 return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
88 MCSectionELF::SHF_ALLOC |
89 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
90 SectionKind::getThreadBSS());
91 }
92 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
93 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
94 MCSectionELF::SHF_ALLOC |
95 MCSectionELF::SHF_WRITE,
96 SectionKind::getDataRel());
97 }
98 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
99 return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
100 MCSectionELF::SHF_ALLOC |
101 MCSectionELF::SHF_WRITE,
102 SectionKind::getReadOnlyWithRel());
103 }
104 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
105 return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
106 MCSectionELF::SHF_ALLOC |
107 MCSectionELF::SHF_WRITE,
108 SectionKind::getReadOnlyWithRelLocal());
109 }
110 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
111 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
112 MCSectionELF::SHF_ALLOC |
113 MCSectionELF::SHF_WRITE,
114 SectionKind::getDataRel());
115 }
Eli Friedman21444ef2010-07-17 04:29:04 +0000116 bool ParseDirectiveSection(StringRef, SMLoc);
117 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000118 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000119 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000120 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000121 bool ParseDirectiveSymver(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000122
123private:
124 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000125};
126
127}
128
129bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
130 unsigned Flags, SectionKind Kind) {
131 if (getLexer().isNot(AsmToken::EndOfStatement))
132 return TokError("unexpected token in section switching directive");
133 Lex();
134
135 getStreamer().SwitchSection(getContext().getELFSection(
136 Section, Type, Flags, Kind));
137
138 return false;
139}
140
Eli Friedman21444ef2010-07-17 04:29:04 +0000141bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000142 StringRef Name;
143 if (getParser().ParseIdentifier(Name))
144 return TokError("expected identifier in directive");
145 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
146
147 if (getLexer().isNot(AsmToken::Comma))
148 return TokError("unexpected token in directive");
149 Lex();
150
151 const MCExpr *Expr;
152 if (getParser().ParseExpression(Expr))
153 return true;
154
155 if (getLexer().isNot(AsmToken::EndOfStatement))
156 return TokError("unexpected token in directive");
157
158 getStreamer().EmitELFSize(Sym, Expr);
159 return false;
160}
161
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000162bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
163 // A section name can contain -, so we cannot just use
164 // ParseIdentifier.
165 SMLoc FirstLoc = getLexer().getLoc();
166 unsigned Size = 0;
167
168 for (;;) {
169 StringRef Tmp;
170 unsigned CurSize;
171
172 SMLoc PrevLoc = getLexer().getLoc();
173 if (getLexer().is(AsmToken::Minus)) {
174 CurSize = 1;
175 Lex(); // Consume the "-".
176 } else if (!getParser().ParseIdentifier(Tmp))
177 CurSize = Tmp.size();
178 else
179 break;
180
181 Size += CurSize;
182 SectionName = StringRef(FirstLoc.getPointer(), Size);
183
184 // Make sure the following token is adjacent.
185 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
186 break;
187 }
188 if (Size == 0)
189 return true;
190
191 return false;
192}
193
Eli Friedman21444ef2010-07-17 04:29:04 +0000194// FIXME: This is a work in progress.
195bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
196 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000197
198 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000199 return TokError("expected identifier in directive");
200
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000201 StringRef FlagsStr;
Eli Friedman21444ef2010-07-17 04:29:04 +0000202 StringRef TypeName;
203 int64_t Size = 0;
204 if (getLexer().is(AsmToken::Comma)) {
205 Lex();
206
207 if (getLexer().isNot(AsmToken::String))
208 return TokError("expected string in directive");
209
210 FlagsStr = getTok().getStringContents();
211 Lex();
212
213 AsmToken::TokenKind TypeStartToken;
214 if (getContext().getAsmInfo().getCommentString()[0] == '@')
215 TypeStartToken = AsmToken::Percent;
216 else
217 TypeStartToken = AsmToken::At;
218
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000219 bool Mergeable = FlagsStr.find('M') != StringRef::npos;
220 bool Group = FlagsStr.find('G') != StringRef::npos;
Eli Friedman21444ef2010-07-17 04:29:04 +0000221
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000222 if (getLexer().isNot(AsmToken::Comma)) {
223 if (Mergeable)
224 return TokError("Mergeable section must specify the type");
225 if (Group)
226 return TokError("Group section must specify the type");
227 } else {
228 Lex();
229 if (getLexer().isNot(TypeStartToken))
230 return TokError("expected the type");
231
232 Lex();
233 if (getParser().ParseIdentifier(TypeName))
234 return TokError("expected identifier in directive");
235
236 if (Mergeable) {
237 if (getLexer().isNot(AsmToken::Comma))
238 return TokError("expected the entry size");
239 Lex();
240 if (getParser().ParseAbsoluteExpression(Size))
241 return true;
242 if (Size <= 0)
243 return TokError("entry size must be positive");
244 }
245
246 if (Group) {
247 if (getLexer().isNot(AsmToken::Comma))
248 return TokError("expected group name");
249 Lex();
250 StringRef GroupName;
251 if (getParser().ParseIdentifier(GroupName))
252 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000253 if (getLexer().is(AsmToken::Comma)) {
254 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000255 StringRef Linkage;
256 if (getParser().ParseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000257 return true;
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000258 if (Linkage != "comdat" && Linkage != ".gnu.linkonce")
259 return TokError("Linkage must be 'comdat' or '.gnu.linkonce'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000260 }
261 }
262 }
263 }
264
265 if (getLexer().isNot(AsmToken::EndOfStatement))
266 return TokError("unexpected token in directive");
267
268 unsigned Flags = 0;
Rafael Espindola4fa34782010-10-27 18:45:20 +0000269 unsigned Type = MCSectionELF::SHT_NULL;
270
271 // Set the defaults first.
272 if (SectionName == ".fini" || SectionName == ".init") {
273 Type = MCSectionELF::SHT_PROGBITS;
274 Flags |= MCSectionELF::SHF_ALLOC;
275 Flags |= MCSectionELF::SHF_EXECINSTR;
276 }
277
Eli Friedman21444ef2010-07-17 04:29:04 +0000278 for (unsigned i = 0; i < FlagsStr.size(); i++) {
279 switch (FlagsStr[i]) {
280 case 'a':
281 Flags |= MCSectionELF::SHF_ALLOC;
282 break;
283 case 'x':
284 Flags |= MCSectionELF::SHF_EXECINSTR;
285 break;
286 case 'w':
287 Flags |= MCSectionELF::SHF_WRITE;
288 break;
289 case 'M':
290 Flags |= MCSectionELF::SHF_MERGE;
291 break;
292 case 'S':
293 Flags |= MCSectionELF::SHF_STRINGS;
294 break;
295 case 'T':
296 Flags |= MCSectionELF::SHF_TLS;
297 break;
298 case 'c':
299 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
300 break;
301 case 'd':
302 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
303 break;
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000304 case 'G':
305 break;
Eli Friedman21444ef2010-07-17 04:29:04 +0000306 default:
307 return TokError("unknown flag");
308 }
309 }
310
Eli Friedman21444ef2010-07-17 04:29:04 +0000311 if (!TypeName.empty()) {
312 if (TypeName == "init_array")
313 Type = MCSectionELF::SHT_INIT_ARRAY;
314 else if (TypeName == "fini_array")
315 Type = MCSectionELF::SHT_FINI_ARRAY;
316 else if (TypeName == "preinit_array")
317 Type = MCSectionELF::SHT_PREINIT_ARRAY;
318 else if (TypeName == "nobits")
319 Type = MCSectionELF::SHT_NOBITS;
320 else if (TypeName == "progbits")
321 Type = MCSectionELF::SHT_PROGBITS;
322 else
323 return TokError("unknown section type");
324 }
325
326 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
327 ? SectionKind::getText()
328 : SectionKind::getDataRel();
329 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Jan Wen Voung12ad94e2010-09-30 02:41:46 +0000330 Flags, Kind, false,
331 Size));
Eli Friedman21444ef2010-07-17 04:29:04 +0000332 return false;
333}
334
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000335bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
336 const MCSection *PreviousSection = getStreamer().getPreviousSection();
337 if (PreviousSection != NULL)
338 getStreamer().SwitchSection(PreviousSection);
339
340 return false;
341}
342
Michael J. Spencere90ea132010-10-09 03:47:55 +0000343/// ParseDirectiveELFType
344/// ::= .type identifier , @attribute
345bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
346 StringRef Name;
347 if (getParser().ParseIdentifier(Name))
348 return TokError("expected identifier in directive");
349
350 // Handle the identifier as the key symbol.
351 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
352
353 if (getLexer().isNot(AsmToken::Comma))
354 return TokError("unexpected token in '.type' directive");
355 Lex();
356
357 if (getLexer().isNot(AsmToken::At))
358 return TokError("expected '@' before type");
359 Lex();
360
361 StringRef Type;
362 SMLoc TypeLoc;
363
364 TypeLoc = getLexer().getLoc();
365 if (getParser().ParseIdentifier(Type))
366 return TokError("expected symbol type in directive");
367
368 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
369 .Case("function", MCSA_ELF_TypeFunction)
370 .Case("object", MCSA_ELF_TypeObject)
371 .Case("tls_object", MCSA_ELF_TypeTLS)
372 .Case("common", MCSA_ELF_TypeCommon)
373 .Case("notype", MCSA_ELF_TypeNoType)
374 .Default(MCSA_Invalid);
375
376 if (Attr == MCSA_Invalid)
377 return Error(TypeLoc, "unsupported attribute in '.type' directive");
378
379 if (getLexer().isNot(AsmToken::EndOfStatement))
380 return TokError("unexpected token in '.type' directive");
381
382 Lex();
383
384 getStreamer().EmitSymbolAttribute(Sym, Attr);
385
386 return false;
387}
388
Rafael Espindola61e3b912010-10-26 19:35:47 +0000389/// ParseDirectiveIdent
390/// ::= .ident string
391bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
392 if (getLexer().isNot(AsmToken::String))
393 return TokError("unexpected token in '.ident' directive");
394
395 StringRef Data = getTok().getIdentifier();
396
397 Lex();
398
399 const MCSection *OldSection = getStreamer().getCurrentSection();
400 const MCSection *Comment =
401 getContext().getELFSection(".comment", MCSectionELF::SHT_PROGBITS,
402 MCSectionELF::SHF_MERGE |
403 MCSectionELF::SHF_STRINGS,
404 SectionKind::getReadOnly(),
405 false, 1);
406
407 static bool First = true;
408
409 getStreamer().SwitchSection(Comment);
410 if (First)
411 getStreamer().EmitIntValue(0, 1);
412 First = false;
413 getStreamer().EmitBytes(Data, 0);
414 getStreamer().EmitIntValue(0, 1);
415 getStreamer().SwitchSection(OldSection);
416 return false;
417}
418
Rafael Espindola88182132010-10-27 15:18:17 +0000419/// ParseDirectiveSymver
420/// ::= .symver foo, bar2@zed
421bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
422 StringRef Name;
423 if (getParser().ParseIdentifier(Name))
424 return TokError("expected identifier in directive");
425
426 if (getLexer().isNot(AsmToken::Comma))
427 return TokError("expected a comma");
428
429 Lex();
430
431 StringRef AliasName;
432 if (getParser().ParseIdentifier(AliasName))
433 return TokError("expected identifier in directive");
434
435 if (AliasName.find('@') == StringRef::npos)
436 return TokError("expected a '@' in the name");
437
438 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
439 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
440 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
441
442 getStreamer().EmitAssignment(Alias, Value);
443 return false;
444}
445
Daniel Dunbar5146a092010-07-12 21:23:32 +0000446namespace llvm {
447
448MCAsmParserExtension *createELFAsmParser() {
449 return new ELFAsmParser;
450}
451
452}