blob: d55de1f3fbe82239f26adc9b5303e1216d17a571 [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"
Rafael Espindolac85dca62011-01-23 04:28:49 +000019#include "llvm/Support/ELF.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000020using namespace llvm;
21
22namespace {
23
24class ELFAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000025 template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
26 void AddDirectiveHandler(StringRef Directive) {
27 getParser().AddDirectiveHandler(this, Directive,
28 HandleDirective<ELFAsmParser, Handler>);
29 }
30
Daniel Dunbar5146a092010-07-12 21:23:32 +000031 bool ParseSectionSwitch(StringRef Section, unsigned Type,
32 unsigned Flags, SectionKind Kind);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +000033 bool SeenIdent;
Daniel Dunbar5146a092010-07-12 21:23:32 +000034
35public:
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +000036 ELFAsmParser() : SeenIdent(false) {
37 BracketExpressionsSupported = true;
38 }
Daniel Dunbar5146a092010-07-12 21:23:32 +000039
40 virtual void Initialize(MCAsmParser &Parser) {
41 // Call the base implementation.
42 this->MCAsmParserExtension::Initialize(Parser);
43
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000044 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
45 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
Matt Flemingf525c2a2010-07-20 21:12:46 +000046 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
48 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
49 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
Jim Grosbachcc866d52011-07-25 17:11:29 +000050 AddDirectiveHandler<
51 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
52 AddDirectiveHandler<
53 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
54 AddDirectiveHandler<
55 &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
56 AddDirectiveHandler<
57 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000058 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
Jim Grosbachcc866d52011-07-25 17:11:29 +000059 AddDirectiveHandler<
60 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
Rafael Espindola7768a9d2011-02-16 01:08:29 +000061 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000062 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000063 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
Michael J. Spencere90ea132010-10-09 03:47:55 +000064 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
Rafael Espindola61e3b912010-10-26 19:35:47 +000065 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
Rafael Espindola88182132010-10-27 15:18:17 +000066 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +000067 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
Rafael Espindola484291c2010-11-01 14:28:48 +000068 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
Jim Grosbachf2a35fb2011-07-25 17:55:35 +000069 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
70 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
71 AddDirectiveHandler<
72 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
73 AddDirectiveHandler<
74 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
75 AddDirectiveHandler<
76 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
Daniel Dunbar5146a092010-07-12 21:23:32 +000077 }
78
Rafael Espindolad80781b2010-09-15 21:48:40 +000079 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
80 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000081 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000082 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000083 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Daniel Dunbar5146a092010-07-12 21:23:32 +000084 SectionKind::getDataRel());
85 }
86 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000087 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000088 ELF::SHF_EXECINSTR |
89 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbar5146a092010-07-12 21:23:32 +000090 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000091 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000092 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000093 ELF::SHF_WRITE |
94 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Flemingf525c2a2010-07-20 21:12:46 +000095 }
96 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000097 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000098 ELF::SHF_ALLOC,
Matt Flemingf525c2a2010-07-20 21:12:46 +000099 SectionKind::getReadOnly());
100 }
101 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000102 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000103 ELF::SHF_ALLOC |
104 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000105 SectionKind::getThreadData());
106 }
107 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000108 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000109 ELF::SHF_ALLOC |
110 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000111 SectionKind::getThreadBSS());
112 }
113 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000114 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000115 ELF::SHF_ALLOC |
116 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000117 SectionKind::getDataRel());
118 }
119 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000120 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000121 ELF::SHF_ALLOC |
122 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000123 SectionKind::getReadOnlyWithRel());
124 }
125 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000126 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000127 ELF::SHF_ALLOC |
128 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000129 SectionKind::getReadOnlyWithRelLocal());
130 }
131 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000132 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000133 ELF::SHF_ALLOC |
134 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000135 SectionKind::getDataRel());
136 }
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000137 bool ParseDirectivePushSection(StringRef, SMLoc);
138 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman21444ef2010-07-17 04:29:04 +0000139 bool ParseDirectiveSection(StringRef, SMLoc);
140 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000141 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000142 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000143 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000144 bool ParseDirectiveSymver(StringRef, SMLoc);
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000145 bool ParseDirectiveVersion(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000146 bool ParseDirectiveWeakref(StringRef, SMLoc);
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000147 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000148
149private:
150 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000151};
152
153}
154
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000155/// ParseDirectiveSymbolAttribute
156/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
157bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
158 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
159 .Case(".weak", MCSA_Weak)
160 .Case(".local", MCSA_Local)
161 .Case(".hidden", MCSA_Hidden)
162 .Case(".internal", MCSA_Internal)
163 .Case(".protected", MCSA_Protected)
164 .Default(MCSA_Invalid);
165 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
166 if (getLexer().isNot(AsmToken::EndOfStatement)) {
167 for (;;) {
168 StringRef Name;
169
170 if (getParser().ParseIdentifier(Name))
171 return TokError("expected identifier in directive");
172
173 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
174
175 getStreamer().EmitSymbolAttribute(Sym, Attr);
176
177 if (getLexer().is(AsmToken::EndOfStatement))
178 break;
179
180 if (getLexer().isNot(AsmToken::Comma))
181 return TokError("unexpected token in directive");
182 Lex();
183 }
184 }
185
186 Lex();
187 return false;
188}
189
Daniel Dunbar5146a092010-07-12 21:23:32 +0000190bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
191 unsigned Flags, SectionKind Kind) {
192 if (getLexer().isNot(AsmToken::EndOfStatement))
193 return TokError("unexpected token in section switching directive");
194 Lex();
195
196 getStreamer().SwitchSection(getContext().getELFSection(
197 Section, Type, Flags, Kind));
198
199 return false;
200}
201
Eli Friedman21444ef2010-07-17 04:29:04 +0000202bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000203 StringRef Name;
204 if (getParser().ParseIdentifier(Name))
205 return TokError("expected identifier in directive");
Dmitri Gribenko2de05722012-09-10 21:26:47 +0000206 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000207
208 if (getLexer().isNot(AsmToken::Comma))
209 return TokError("unexpected token in directive");
210 Lex();
211
212 const MCExpr *Expr;
213 if (getParser().ParseExpression(Expr))
214 return true;
215
216 if (getLexer().isNot(AsmToken::EndOfStatement))
217 return TokError("unexpected token in directive");
218
219 getStreamer().EmitELFSize(Sym, Expr);
220 return false;
221}
222
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000223bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
224 // A section name can contain -, so we cannot just use
225 // ParseIdentifier.
226 SMLoc FirstLoc = getLexer().getLoc();
227 unsigned Size = 0;
228
Rafael Espindola184640e2011-01-24 18:02:54 +0000229 if (getLexer().is(AsmToken::String)) {
230 SectionName = getTok().getIdentifier();
231 Lex();
232 return false;
233 }
234
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000235 for (;;) {
236 StringRef Tmp;
237 unsigned CurSize;
238
239 SMLoc PrevLoc = getLexer().getLoc();
240 if (getLexer().is(AsmToken::Minus)) {
241 CurSize = 1;
242 Lex(); // Consume the "-".
Rafael Espindola184640e2011-01-24 18:02:54 +0000243 } else if (getLexer().is(AsmToken::String)) {
244 CurSize = getTok().getIdentifier().size() + 2;
245 Lex();
246 } else if (getLexer().is(AsmToken::Identifier)) {
247 CurSize = getTok().getIdentifier().size();
248 Lex();
249 } else {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000250 break;
Rafael Espindola184640e2011-01-24 18:02:54 +0000251 }
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000252
253 Size += CurSize;
254 SectionName = StringRef(FirstLoc.getPointer(), Size);
255
256 // Make sure the following token is adjacent.
257 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
258 break;
259 }
260 if (Size == 0)
261 return true;
262
263 return false;
264}
265
Rafael Espindola25958732010-11-24 21:57:39 +0000266static SectionKind computeSectionKind(unsigned Flags) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000267 if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola25958732010-11-24 21:57:39 +0000268 return SectionKind::getText();
Rafael Espindola1c130262011-01-23 04:43:11 +0000269 if (Flags & ELF::SHF_TLS)
Rafael Espindola25958732010-11-24 21:57:39 +0000270 return SectionKind::getThreadData();
271 return SectionKind::getDataRel();
272}
273
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000274static int parseSectionFlags(StringRef flagsStr) {
275 int flags = 0;
276
277 for (unsigned i = 0; i < flagsStr.size(); i++) {
278 switch (flagsStr[i]) {
279 case 'a':
Rafael Espindola1c130262011-01-23 04:43:11 +0000280 flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000281 break;
282 case 'x':
Rafael Espindola1c130262011-01-23 04:43:11 +0000283 flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000284 break;
285 case 'w':
Rafael Espindola1c130262011-01-23 04:43:11 +0000286 flags |= ELF::SHF_WRITE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000287 break;
288 case 'M':
Rafael Espindola1c130262011-01-23 04:43:11 +0000289 flags |= ELF::SHF_MERGE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000290 break;
291 case 'S':
Rafael Espindola1c130262011-01-23 04:43:11 +0000292 flags |= ELF::SHF_STRINGS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000293 break;
294 case 'T':
Rafael Espindola1c130262011-01-23 04:43:11 +0000295 flags |= ELF::SHF_TLS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000296 break;
297 case 'c':
Rafael Espindola1c130262011-01-23 04:43:11 +0000298 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000299 break;
300 case 'd':
Rafael Espindola1c130262011-01-23 04:43:11 +0000301 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000302 break;
303 case 'G':
Rafael Espindola1c130262011-01-23 04:43:11 +0000304 flags |= ELF::SHF_GROUP;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000305 break;
306 default:
307 return -1;
308 }
309 }
310
311 return flags;
312}
313
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000314bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
315 getStreamer().PushSection();
316
317 if (ParseDirectiveSection(s, loc)) {
318 getStreamer().PopSection();
319 return true;
320 }
321
322 return false;
323}
324
325bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
326 if (!getStreamer().PopSection())
327 return TokError(".popsection without corresponding .pushsection");
328 return false;
329}
330
Eli Friedman21444ef2010-07-17 04:29:04 +0000331// FIXME: This is a work in progress.
332bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
333 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000334
335 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000336 return TokError("expected identifier in directive");
337
Eli Friedman21444ef2010-07-17 04:29:04 +0000338 StringRef TypeName;
339 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000340 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000341 unsigned Flags = 0;
342
343 // Set the defaults first.
344 if (SectionName == ".fini" || SectionName == ".init" ||
345 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000346 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000347 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000348 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000349
Eli Friedman21444ef2010-07-17 04:29:04 +0000350 if (getLexer().is(AsmToken::Comma)) {
351 Lex();
352
353 if (getLexer().isNot(AsmToken::String))
354 return TokError("expected string in directive");
355
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000356 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000357 Lex();
358
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000359 int extraFlags = parseSectionFlags(FlagsStr);
360 if (extraFlags < 0)
361 return TokError("unknown flag");
362 Flags |= extraFlags;
363
Rafael Espindola1c130262011-01-23 04:43:11 +0000364 bool Mergeable = Flags & ELF::SHF_MERGE;
365 bool Group = Flags & ELF::SHF_GROUP;
Eli Friedman21444ef2010-07-17 04:29:04 +0000366
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000367 if (getLexer().isNot(AsmToken::Comma)) {
368 if (Mergeable)
369 return TokError("Mergeable section must specify the type");
370 if (Group)
371 return TokError("Group section must specify the type");
372 } else {
373 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000374 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
375 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000376
377 Lex();
378 if (getParser().ParseIdentifier(TypeName))
379 return TokError("expected identifier in directive");
380
381 if (Mergeable) {
382 if (getLexer().isNot(AsmToken::Comma))
383 return TokError("expected the entry size");
384 Lex();
385 if (getParser().ParseAbsoluteExpression(Size))
386 return true;
387 if (Size <= 0)
388 return TokError("entry size must be positive");
389 }
390
391 if (Group) {
392 if (getLexer().isNot(AsmToken::Comma))
393 return TokError("expected group name");
394 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000395 if (getParser().ParseIdentifier(GroupName))
396 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000397 if (getLexer().is(AsmToken::Comma)) {
398 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000399 StringRef Linkage;
400 if (getParser().ParseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000401 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000402 if (Linkage != "comdat")
403 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000404 }
405 }
406 }
407 }
408
409 if (getLexer().isNot(AsmToken::EndOfStatement))
410 return TokError("unexpected token in directive");
411
Rafael Espindolac85dca62011-01-23 04:28:49 +0000412 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000413
Eli Friedman21444ef2010-07-17 04:29:04 +0000414 if (!TypeName.empty()) {
415 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000416 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000417 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000418 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000419 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000420 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000421 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000422 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000423 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000424 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000425 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000426 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000427 else if (TypeName == "unwind")
428 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000429 else
430 return TokError("unknown section type");
431 }
432
Rafael Espindola25958732010-11-24 21:57:39 +0000433 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000434 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000435 Flags, Kind, Size,
436 GroupName));
Eli Friedman21444ef2010-07-17 04:29:04 +0000437 return false;
438}
439
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000440bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
441 const MCSection *PreviousSection = getStreamer().getPreviousSection();
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000442 if (PreviousSection == NULL)
443 return TokError(".previous without corresponding .section");
444 getStreamer().SwitchSection(PreviousSection);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000445
446 return false;
447}
448
Michael J. Spencere90ea132010-10-09 03:47:55 +0000449/// ParseDirectiveELFType
450/// ::= .type identifier , @attribute
451bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
452 StringRef Name;
453 if (getParser().ParseIdentifier(Name))
454 return TokError("expected identifier in directive");
455
456 // Handle the identifier as the key symbol.
457 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
458
459 if (getLexer().isNot(AsmToken::Comma))
460 return TokError("unexpected token in '.type' directive");
461 Lex();
462
Rafael Espindolad4a35262010-11-12 15:47:08 +0000463 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
464 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000465 Lex();
466
467 StringRef Type;
468 SMLoc TypeLoc;
469
470 TypeLoc = getLexer().getLoc();
471 if (getParser().ParseIdentifier(Type))
472 return TokError("expected symbol type in directive");
473
474 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
475 .Case("function", MCSA_ELF_TypeFunction)
476 .Case("object", MCSA_ELF_TypeObject)
477 .Case("tls_object", MCSA_ELF_TypeTLS)
478 .Case("common", MCSA_ELF_TypeCommon)
479 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000480 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Roman Divackya0c17a42011-12-12 17:34:04 +0000481 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000482 .Default(MCSA_Invalid);
483
484 if (Attr == MCSA_Invalid)
485 return Error(TypeLoc, "unsupported attribute in '.type' directive");
486
487 if (getLexer().isNot(AsmToken::EndOfStatement))
488 return TokError("unexpected token in '.type' directive");
489
490 Lex();
491
492 getStreamer().EmitSymbolAttribute(Sym, Attr);
493
494 return false;
495}
496
Rafael Espindola61e3b912010-10-26 19:35:47 +0000497/// ParseDirectiveIdent
498/// ::= .ident string
499bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
500 if (getLexer().isNot(AsmToken::String))
501 return TokError("unexpected token in '.ident' directive");
502
503 StringRef Data = getTok().getIdentifier();
504
505 Lex();
506
Rafael Espindola61e3b912010-10-26 19:35:47 +0000507 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000508 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000509 ELF::SHF_MERGE |
510 ELF::SHF_STRINGS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000511 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000512 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000513
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000514 getStreamer().PushSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000515 getStreamer().SwitchSection(Comment);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000516 if (!SeenIdent) {
Rafael Espindola61e3b912010-10-26 19:35:47 +0000517 getStreamer().EmitIntValue(0, 1);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000518 SeenIdent = true;
519 }
Rafael Espindola61e3b912010-10-26 19:35:47 +0000520 getStreamer().EmitBytes(Data, 0);
521 getStreamer().EmitIntValue(0, 1);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000522 getStreamer().PopSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000523 return false;
524}
525
Rafael Espindola88182132010-10-27 15:18:17 +0000526/// ParseDirectiveSymver
527/// ::= .symver foo, bar2@zed
528bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
529 StringRef Name;
530 if (getParser().ParseIdentifier(Name))
531 return TokError("expected identifier in directive");
532
533 if (getLexer().isNot(AsmToken::Comma))
534 return TokError("expected a comma");
535
536 Lex();
537
538 StringRef AliasName;
539 if (getParser().ParseIdentifier(AliasName))
540 return TokError("expected identifier in directive");
541
542 if (AliasName.find('@') == StringRef::npos)
543 return TokError("expected a '@' in the name");
544
545 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
546 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
547 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
548
549 getStreamer().EmitAssignment(Alias, Value);
550 return false;
551}
552
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000553/// ParseDirectiveVersion
554/// ::= .version string
555bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
556 if (getLexer().isNot(AsmToken::String))
557 return TokError("unexpected token in '.version' directive");
558
559 StringRef Data = getTok().getIdentifier();
560
561 Lex();
562
563 const MCSection *Note =
564 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
565 SectionKind::getReadOnly());
566
567 getStreamer().PushSection();
568 getStreamer().SwitchSection(Note);
569 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
570 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
571 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
572 getStreamer().EmitBytes(Data, 0); // name.
573 getStreamer().EmitIntValue(0, 1); // terminate the string.
574 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
575 getStreamer().PopSection();
576 return false;
577}
578
Rafael Espindola484291c2010-11-01 14:28:48 +0000579/// ParseDirectiveWeakref
580/// ::= .weakref foo, bar
581bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
582 // FIXME: Share code with the other alias building directives.
583
584 StringRef AliasName;
585 if (getParser().ParseIdentifier(AliasName))
586 return TokError("expected identifier in directive");
587
588 if (getLexer().isNot(AsmToken::Comma))
589 return TokError("expected a comma");
590
591 Lex();
592
593 StringRef Name;
594 if (getParser().ParseIdentifier(Name))
595 return TokError("expected identifier in directive");
596
597 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
598
599 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
600
601 getStreamer().EmitWeakReference(Alias, Sym);
602 return false;
603}
604
Daniel Dunbar5146a092010-07-12 21:23:32 +0000605namespace llvm {
606
607MCAsmParserExtension *createELFAsmParser() {
608 return new ELFAsmParser;
609}
610
611}