blob: eb394150dda65fa3eb0bb31f76e5441b4f4ebd78 [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 {
Eli Bendersky171192f2013-01-16 00:50:52 +000025 template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000026 void AddDirectiveHandler(StringRef Directive) {
Eli Bendersky171192f2013-01-16 00:50:52 +000027 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
28 this, HandleDirective<ELFAsmParser, HandlerMethod>);
29
30 getParser().AddDirectiveHandler(Directive, Handler);
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000031 }
32
Daniel Dunbar5146a092010-07-12 21:23:32 +000033 bool ParseSectionSwitch(StringRef Section, unsigned Type,
34 unsigned Flags, SectionKind Kind);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +000035 bool SeenIdent;
Daniel Dunbar5146a092010-07-12 21:23:32 +000036
37public:
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +000038 ELFAsmParser() : SeenIdent(false) {
39 BracketExpressionsSupported = true;
40 }
Daniel Dunbar5146a092010-07-12 21:23:32 +000041
42 virtual void Initialize(MCAsmParser &Parser) {
43 // Call the base implementation.
44 this->MCAsmParserExtension::Initialize(Parser);
45
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000046 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
Matt Flemingf525c2a2010-07-20 21:12:46 +000048 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
49 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
50 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
51 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
Jim Grosbachcc866d52011-07-25 17:11:29 +000052 AddDirectiveHandler<
53 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
54 AddDirectiveHandler<
55 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
56 AddDirectiveHandler<
57 &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
58 AddDirectiveHandler<
59 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000060 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
Jim Grosbachcc866d52011-07-25 17:11:29 +000061 AddDirectiveHandler<
62 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
Rafael Espindola7768a9d2011-02-16 01:08:29 +000063 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000064 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000065 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
Michael J. Spencere90ea132010-10-09 03:47:55 +000066 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
Rafael Espindola61e3b912010-10-26 19:35:47 +000067 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
Rafael Espindola88182132010-10-27 15:18:17 +000068 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +000069 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
Rafael Espindola484291c2010-11-01 14:28:48 +000070 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
Jim Grosbachf2a35fb2011-07-25 17:55:35 +000071 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
72 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
73 AddDirectiveHandler<
74 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
75 AddDirectiveHandler<
76 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
77 AddDirectiveHandler<
78 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
Daniel Dunbar5146a092010-07-12 21:23:32 +000079 }
80
Rafael Espindolad80781b2010-09-15 21:48:40 +000081 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
82 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000083 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000084 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000085 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Daniel Dunbar5146a092010-07-12 21:23:32 +000086 SectionKind::getDataRel());
87 }
88 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000089 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000090 ELF::SHF_EXECINSTR |
91 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbar5146a092010-07-12 21:23:32 +000092 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000093 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000094 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000095 ELF::SHF_WRITE |
96 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Flemingf525c2a2010-07-20 21:12:46 +000097 }
98 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000099 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000100 ELF::SHF_ALLOC,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000101 SectionKind::getReadOnly());
102 }
103 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000104 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000105 ELF::SHF_ALLOC |
106 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000107 SectionKind::getThreadData());
108 }
109 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000110 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000111 ELF::SHF_ALLOC |
112 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000113 SectionKind::getThreadBSS());
114 }
115 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000116 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000117 ELF::SHF_ALLOC |
118 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000119 SectionKind::getDataRel());
120 }
121 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000122 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000123 ELF::SHF_ALLOC |
124 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000125 SectionKind::getReadOnlyWithRel());
126 }
127 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000128 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000129 ELF::SHF_ALLOC |
130 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000131 SectionKind::getReadOnlyWithRelLocal());
132 }
133 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000134 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000135 ELF::SHF_ALLOC |
136 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000137 SectionKind::getDataRel());
138 }
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000139 bool ParseDirectivePushSection(StringRef, SMLoc);
140 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman21444ef2010-07-17 04:29:04 +0000141 bool ParseDirectiveSection(StringRef, SMLoc);
142 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000143 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000144 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000145 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000146 bool ParseDirectiveSymver(StringRef, SMLoc);
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000147 bool ParseDirectiveVersion(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000148 bool ParseDirectiveWeakref(StringRef, SMLoc);
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000149 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000150
151private:
152 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000153};
154
155}
156
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000157/// ParseDirectiveSymbolAttribute
158/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
159bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
160 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
161 .Case(".weak", MCSA_Weak)
162 .Case(".local", MCSA_Local)
163 .Case(".hidden", MCSA_Hidden)
164 .Case(".internal", MCSA_Internal)
165 .Case(".protected", MCSA_Protected)
166 .Default(MCSA_Invalid);
167 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
168 if (getLexer().isNot(AsmToken::EndOfStatement)) {
169 for (;;) {
170 StringRef Name;
171
172 if (getParser().ParseIdentifier(Name))
173 return TokError("expected identifier in directive");
174
175 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
176
177 getStreamer().EmitSymbolAttribute(Sym, Attr);
178
179 if (getLexer().is(AsmToken::EndOfStatement))
180 break;
181
182 if (getLexer().isNot(AsmToken::Comma))
183 return TokError("unexpected token in directive");
184 Lex();
185 }
186 }
187
188 Lex();
189 return false;
190}
191
Daniel Dunbar5146a092010-07-12 21:23:32 +0000192bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
193 unsigned Flags, SectionKind Kind) {
194 if (getLexer().isNot(AsmToken::EndOfStatement))
195 return TokError("unexpected token in section switching directive");
196 Lex();
197
198 getStreamer().SwitchSection(getContext().getELFSection(
199 Section, Type, Flags, Kind));
200
201 return false;
202}
203
Eli Friedman21444ef2010-07-17 04:29:04 +0000204bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000205 StringRef Name;
206 if (getParser().ParseIdentifier(Name))
207 return TokError("expected identifier in directive");
Dmitri Gribenko2de05722012-09-10 21:26:47 +0000208 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000209
210 if (getLexer().isNot(AsmToken::Comma))
211 return TokError("unexpected token in directive");
212 Lex();
213
214 const MCExpr *Expr;
215 if (getParser().ParseExpression(Expr))
216 return true;
217
218 if (getLexer().isNot(AsmToken::EndOfStatement))
219 return TokError("unexpected token in directive");
220
221 getStreamer().EmitELFSize(Sym, Expr);
222 return false;
223}
224
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000225bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
226 // A section name can contain -, so we cannot just use
227 // ParseIdentifier.
228 SMLoc FirstLoc = getLexer().getLoc();
229 unsigned Size = 0;
230
Rafael Espindola184640e2011-01-24 18:02:54 +0000231 if (getLexer().is(AsmToken::String)) {
232 SectionName = getTok().getIdentifier();
233 Lex();
234 return false;
235 }
236
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000237 for (;;) {
238 StringRef Tmp;
239 unsigned CurSize;
240
241 SMLoc PrevLoc = getLexer().getLoc();
242 if (getLexer().is(AsmToken::Minus)) {
243 CurSize = 1;
244 Lex(); // Consume the "-".
Rafael Espindola184640e2011-01-24 18:02:54 +0000245 } else if (getLexer().is(AsmToken::String)) {
246 CurSize = getTok().getIdentifier().size() + 2;
247 Lex();
248 } else if (getLexer().is(AsmToken::Identifier)) {
249 CurSize = getTok().getIdentifier().size();
250 Lex();
251 } else {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000252 break;
Rafael Espindola184640e2011-01-24 18:02:54 +0000253 }
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000254
255 Size += CurSize;
256 SectionName = StringRef(FirstLoc.getPointer(), Size);
257
258 // Make sure the following token is adjacent.
259 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
260 break;
261 }
262 if (Size == 0)
263 return true;
264
265 return false;
266}
267
Rafael Espindola25958732010-11-24 21:57:39 +0000268static SectionKind computeSectionKind(unsigned Flags) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000269 if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola25958732010-11-24 21:57:39 +0000270 return SectionKind::getText();
Rafael Espindola1c130262011-01-23 04:43:11 +0000271 if (Flags & ELF::SHF_TLS)
Rafael Espindola25958732010-11-24 21:57:39 +0000272 return SectionKind::getThreadData();
273 return SectionKind::getDataRel();
274}
275
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000276static int parseSectionFlags(StringRef flagsStr) {
277 int flags = 0;
278
279 for (unsigned i = 0; i < flagsStr.size(); i++) {
280 switch (flagsStr[i]) {
281 case 'a':
Rafael Espindola1c130262011-01-23 04:43:11 +0000282 flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000283 break;
284 case 'x':
Rafael Espindola1c130262011-01-23 04:43:11 +0000285 flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000286 break;
287 case 'w':
Rafael Espindola1c130262011-01-23 04:43:11 +0000288 flags |= ELF::SHF_WRITE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000289 break;
290 case 'M':
Rafael Espindola1c130262011-01-23 04:43:11 +0000291 flags |= ELF::SHF_MERGE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000292 break;
293 case 'S':
Rafael Espindola1c130262011-01-23 04:43:11 +0000294 flags |= ELF::SHF_STRINGS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000295 break;
296 case 'T':
Rafael Espindola1c130262011-01-23 04:43:11 +0000297 flags |= ELF::SHF_TLS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000298 break;
299 case 'c':
Rafael Espindola1c130262011-01-23 04:43:11 +0000300 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000301 break;
302 case 'd':
Rafael Espindola1c130262011-01-23 04:43:11 +0000303 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000304 break;
305 case 'G':
Rafael Espindola1c130262011-01-23 04:43:11 +0000306 flags |= ELF::SHF_GROUP;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000307 break;
308 default:
309 return -1;
310 }
311 }
312
313 return flags;
314}
315
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000316bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
317 getStreamer().PushSection();
318
319 if (ParseDirectiveSection(s, loc)) {
320 getStreamer().PopSection();
321 return true;
322 }
323
324 return false;
325}
326
327bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
328 if (!getStreamer().PopSection())
329 return TokError(".popsection without corresponding .pushsection");
330 return false;
331}
332
Eli Friedman21444ef2010-07-17 04:29:04 +0000333// FIXME: This is a work in progress.
334bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
335 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000336
337 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000338 return TokError("expected identifier in directive");
339
Eli Friedman21444ef2010-07-17 04:29:04 +0000340 StringRef TypeName;
341 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000342 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000343 unsigned Flags = 0;
344
345 // Set the defaults first.
346 if (SectionName == ".fini" || SectionName == ".init" ||
347 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000348 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000349 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000350 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000351
Eli Friedman21444ef2010-07-17 04:29:04 +0000352 if (getLexer().is(AsmToken::Comma)) {
353 Lex();
354
355 if (getLexer().isNot(AsmToken::String))
356 return TokError("expected string in directive");
357
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000358 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000359 Lex();
360
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000361 int extraFlags = parseSectionFlags(FlagsStr);
362 if (extraFlags < 0)
363 return TokError("unknown flag");
364 Flags |= extraFlags;
365
Rafael Espindola1c130262011-01-23 04:43:11 +0000366 bool Mergeable = Flags & ELF::SHF_MERGE;
367 bool Group = Flags & ELF::SHF_GROUP;
Eli Friedman21444ef2010-07-17 04:29:04 +0000368
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000369 if (getLexer().isNot(AsmToken::Comma)) {
370 if (Mergeable)
371 return TokError("Mergeable section must specify the type");
372 if (Group)
373 return TokError("Group section must specify the type");
374 } else {
375 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000376 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
377 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000378
379 Lex();
380 if (getParser().ParseIdentifier(TypeName))
381 return TokError("expected identifier in directive");
382
383 if (Mergeable) {
384 if (getLexer().isNot(AsmToken::Comma))
385 return TokError("expected the entry size");
386 Lex();
387 if (getParser().ParseAbsoluteExpression(Size))
388 return true;
389 if (Size <= 0)
390 return TokError("entry size must be positive");
391 }
392
393 if (Group) {
394 if (getLexer().isNot(AsmToken::Comma))
395 return TokError("expected group name");
396 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000397 if (getParser().ParseIdentifier(GroupName))
398 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000399 if (getLexer().is(AsmToken::Comma)) {
400 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000401 StringRef Linkage;
402 if (getParser().ParseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000403 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000404 if (Linkage != "comdat")
405 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000406 }
407 }
408 }
409 }
410
411 if (getLexer().isNot(AsmToken::EndOfStatement))
412 return TokError("unexpected token in directive");
413
Rafael Espindolac85dca62011-01-23 04:28:49 +0000414 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000415
Eli Friedman21444ef2010-07-17 04:29:04 +0000416 if (!TypeName.empty()) {
417 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000418 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000419 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000420 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000421 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000422 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000423 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000424 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000425 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000426 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000427 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000428 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000429 else if (TypeName == "unwind")
430 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000431 else
432 return TokError("unknown section type");
433 }
434
Rafael Espindola25958732010-11-24 21:57:39 +0000435 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000436 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000437 Flags, Kind, Size,
438 GroupName));
Eli Friedman21444ef2010-07-17 04:29:04 +0000439 return false;
440}
441
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000442bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
443 const MCSection *PreviousSection = getStreamer().getPreviousSection();
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000444 if (PreviousSection == NULL)
445 return TokError(".previous without corresponding .section");
446 getStreamer().SwitchSection(PreviousSection);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000447
448 return false;
449}
450
Michael J. Spencere90ea132010-10-09 03:47:55 +0000451/// ParseDirectiveELFType
452/// ::= .type identifier , @attribute
453bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
454 StringRef Name;
455 if (getParser().ParseIdentifier(Name))
456 return TokError("expected identifier in directive");
457
458 // Handle the identifier as the key symbol.
459 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
460
461 if (getLexer().isNot(AsmToken::Comma))
462 return TokError("unexpected token in '.type' directive");
463 Lex();
464
Rafael Espindolad4a35262010-11-12 15:47:08 +0000465 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
466 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000467 Lex();
468
469 StringRef Type;
470 SMLoc TypeLoc;
471
472 TypeLoc = getLexer().getLoc();
473 if (getParser().ParseIdentifier(Type))
474 return TokError("expected symbol type in directive");
475
476 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
477 .Case("function", MCSA_ELF_TypeFunction)
478 .Case("object", MCSA_ELF_TypeObject)
479 .Case("tls_object", MCSA_ELF_TypeTLS)
480 .Case("common", MCSA_ELF_TypeCommon)
481 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000482 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Roman Divackya0c17a42011-12-12 17:34:04 +0000483 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000484 .Default(MCSA_Invalid);
485
486 if (Attr == MCSA_Invalid)
487 return Error(TypeLoc, "unsupported attribute in '.type' directive");
488
489 if (getLexer().isNot(AsmToken::EndOfStatement))
490 return TokError("unexpected token in '.type' directive");
491
492 Lex();
493
494 getStreamer().EmitSymbolAttribute(Sym, Attr);
495
496 return false;
497}
498
Rafael Espindola61e3b912010-10-26 19:35:47 +0000499/// ParseDirectiveIdent
500/// ::= .ident string
501bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
502 if (getLexer().isNot(AsmToken::String))
503 return TokError("unexpected token in '.ident' directive");
504
505 StringRef Data = getTok().getIdentifier();
506
507 Lex();
508
Rafael Espindola61e3b912010-10-26 19:35:47 +0000509 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000510 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000511 ELF::SHF_MERGE |
512 ELF::SHF_STRINGS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000513 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000514 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000515
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000516 getStreamer().PushSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000517 getStreamer().SwitchSection(Comment);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000518 if (!SeenIdent) {
Rafael Espindola61e3b912010-10-26 19:35:47 +0000519 getStreamer().EmitIntValue(0, 1);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000520 SeenIdent = true;
521 }
Eric Christopher68ca5622013-01-09 01:57:54 +0000522 getStreamer().EmitBytes(Data);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000523 getStreamer().EmitIntValue(0, 1);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000524 getStreamer().PopSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000525 return false;
526}
527
Rafael Espindola88182132010-10-27 15:18:17 +0000528/// ParseDirectiveSymver
529/// ::= .symver foo, bar2@zed
530bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
531 StringRef Name;
532 if (getParser().ParseIdentifier(Name))
533 return TokError("expected identifier in directive");
534
535 if (getLexer().isNot(AsmToken::Comma))
536 return TokError("expected a comma");
537
538 Lex();
539
540 StringRef AliasName;
541 if (getParser().ParseIdentifier(AliasName))
542 return TokError("expected identifier in directive");
543
544 if (AliasName.find('@') == StringRef::npos)
545 return TokError("expected a '@' in the name");
546
547 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
548 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
549 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
550
551 getStreamer().EmitAssignment(Alias, Value);
552 return false;
553}
554
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000555/// ParseDirectiveVersion
556/// ::= .version string
557bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
558 if (getLexer().isNot(AsmToken::String))
559 return TokError("unexpected token in '.version' directive");
560
561 StringRef Data = getTok().getIdentifier();
562
563 Lex();
564
565 const MCSection *Note =
566 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
567 SectionKind::getReadOnly());
568
569 getStreamer().PushSection();
570 getStreamer().SwitchSection(Note);
571 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
572 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
573 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christopher68ca5622013-01-09 01:57:54 +0000574 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000575 getStreamer().EmitIntValue(0, 1); // terminate the string.
576 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
577 getStreamer().PopSection();
578 return false;
579}
580
Rafael Espindola484291c2010-11-01 14:28:48 +0000581/// ParseDirectiveWeakref
582/// ::= .weakref foo, bar
583bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
584 // FIXME: Share code with the other alias building directives.
585
586 StringRef AliasName;
587 if (getParser().ParseIdentifier(AliasName))
588 return TokError("expected identifier in directive");
589
590 if (getLexer().isNot(AsmToken::Comma))
591 return TokError("expected a comma");
592
593 Lex();
594
595 StringRef Name;
596 if (getParser().ParseIdentifier(Name))
597 return TokError("expected identifier in directive");
598
599 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
600
601 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
602
603 getStreamer().EmitWeakReference(Alias, Sym);
604 return false;
605}
606
Daniel Dunbar5146a092010-07-12 21:23:32 +0000607namespace llvm {
608
609MCAsmParserExtension *createELFAsmParser() {
610 return new ELFAsmParser;
611}
612
613}