blob: ffc400b203f956e30a3d74cebf03906a5c80e99a [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");
Rafael Espindola484291c2010-11-01 14:28:48 +000067 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
Jim Grosbachf2a35fb2011-07-25 17:55:35 +000068 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
69 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
70 AddDirectiveHandler<
71 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
72 AddDirectiveHandler<
73 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
74 AddDirectiveHandler<
75 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
Daniel Dunbar5146a092010-07-12 21:23:32 +000076 }
77
Rafael Espindolad80781b2010-09-15 21:48:40 +000078 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
79 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000080 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000081 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000082 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Daniel Dunbar5146a092010-07-12 21:23:32 +000083 SectionKind::getDataRel());
84 }
85 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000086 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000087 ELF::SHF_EXECINSTR |
88 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbar5146a092010-07-12 21:23:32 +000089 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000090 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000091 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000092 ELF::SHF_WRITE |
93 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Flemingf525c2a2010-07-20 21:12:46 +000094 }
95 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000096 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000097 ELF::SHF_ALLOC,
Matt Flemingf525c2a2010-07-20 21:12:46 +000098 SectionKind::getReadOnly());
99 }
100 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000101 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000102 ELF::SHF_ALLOC |
103 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000104 SectionKind::getThreadData());
105 }
106 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000107 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000108 ELF::SHF_ALLOC |
109 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000110 SectionKind::getThreadBSS());
111 }
112 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000113 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000114 ELF::SHF_ALLOC |
115 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000116 SectionKind::getDataRel());
117 }
118 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000119 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000120 ELF::SHF_ALLOC |
121 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000122 SectionKind::getReadOnlyWithRel());
123 }
124 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000125 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000126 ELF::SHF_ALLOC |
127 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000128 SectionKind::getReadOnlyWithRelLocal());
129 }
130 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000131 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000132 ELF::SHF_ALLOC |
133 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000134 SectionKind::getDataRel());
135 }
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000136 bool ParseDirectivePushSection(StringRef, SMLoc);
137 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman21444ef2010-07-17 04:29:04 +0000138 bool ParseDirectiveSection(StringRef, SMLoc);
139 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000140 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000141 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000142 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000143 bool ParseDirectiveSymver(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000144 bool ParseDirectiveWeakref(StringRef, SMLoc);
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000145 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000146
147private:
148 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000149};
150
151}
152
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000153/// ParseDirectiveSymbolAttribute
154/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
155bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
156 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
157 .Case(".weak", MCSA_Weak)
158 .Case(".local", MCSA_Local)
159 .Case(".hidden", MCSA_Hidden)
160 .Case(".internal", MCSA_Internal)
161 .Case(".protected", MCSA_Protected)
162 .Default(MCSA_Invalid);
163 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
164 if (getLexer().isNot(AsmToken::EndOfStatement)) {
165 for (;;) {
166 StringRef Name;
167
168 if (getParser().ParseIdentifier(Name))
169 return TokError("expected identifier in directive");
170
171 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
172
173 getStreamer().EmitSymbolAttribute(Sym, Attr);
174
175 if (getLexer().is(AsmToken::EndOfStatement))
176 break;
177
178 if (getLexer().isNot(AsmToken::Comma))
179 return TokError("unexpected token in directive");
180 Lex();
181 }
182 }
183
184 Lex();
185 return false;
186}
187
Daniel Dunbar5146a092010-07-12 21:23:32 +0000188bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
189 unsigned Flags, SectionKind Kind) {
190 if (getLexer().isNot(AsmToken::EndOfStatement))
191 return TokError("unexpected token in section switching directive");
192 Lex();
193
194 getStreamer().SwitchSection(getContext().getELFSection(
195 Section, Type, Flags, Kind));
196
197 return false;
198}
199
Eli Friedman21444ef2010-07-17 04:29:04 +0000200bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000201 StringRef Name;
202 if (getParser().ParseIdentifier(Name))
203 return TokError("expected identifier in directive");
204 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
205
206 if (getLexer().isNot(AsmToken::Comma))
207 return TokError("unexpected token in directive");
208 Lex();
209
210 const MCExpr *Expr;
211 if (getParser().ParseExpression(Expr))
212 return true;
213
214 if (getLexer().isNot(AsmToken::EndOfStatement))
215 return TokError("unexpected token in directive");
216
217 getStreamer().EmitELFSize(Sym, Expr);
218 return false;
219}
220
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000221bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
222 // A section name can contain -, so we cannot just use
223 // ParseIdentifier.
224 SMLoc FirstLoc = getLexer().getLoc();
225 unsigned Size = 0;
226
Rafael Espindola184640e2011-01-24 18:02:54 +0000227 if (getLexer().is(AsmToken::String)) {
228 SectionName = getTok().getIdentifier();
229 Lex();
230 return false;
231 }
232
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000233 for (;;) {
234 StringRef Tmp;
235 unsigned CurSize;
236
237 SMLoc PrevLoc = getLexer().getLoc();
238 if (getLexer().is(AsmToken::Minus)) {
239 CurSize = 1;
240 Lex(); // Consume the "-".
Rafael Espindola184640e2011-01-24 18:02:54 +0000241 } else if (getLexer().is(AsmToken::String)) {
242 CurSize = getTok().getIdentifier().size() + 2;
243 Lex();
244 } else if (getLexer().is(AsmToken::Identifier)) {
245 CurSize = getTok().getIdentifier().size();
246 Lex();
247 } else {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000248 break;
Rafael Espindola184640e2011-01-24 18:02:54 +0000249 }
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000250
251 Size += CurSize;
252 SectionName = StringRef(FirstLoc.getPointer(), Size);
253
254 // Make sure the following token is adjacent.
255 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
256 break;
257 }
258 if (Size == 0)
259 return true;
260
261 return false;
262}
263
Rafael Espindola25958732010-11-24 21:57:39 +0000264static SectionKind computeSectionKind(unsigned Flags) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000265 if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola25958732010-11-24 21:57:39 +0000266 return SectionKind::getText();
Rafael Espindola1c130262011-01-23 04:43:11 +0000267 if (Flags & ELF::SHF_TLS)
Rafael Espindola25958732010-11-24 21:57:39 +0000268 return SectionKind::getThreadData();
269 return SectionKind::getDataRel();
270}
271
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000272static int parseSectionFlags(StringRef flagsStr) {
273 int flags = 0;
274
275 for (unsigned i = 0; i < flagsStr.size(); i++) {
276 switch (flagsStr[i]) {
277 case 'a':
Rafael Espindola1c130262011-01-23 04:43:11 +0000278 flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000279 break;
280 case 'x':
Rafael Espindola1c130262011-01-23 04:43:11 +0000281 flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000282 break;
283 case 'w':
Rafael Espindola1c130262011-01-23 04:43:11 +0000284 flags |= ELF::SHF_WRITE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000285 break;
286 case 'M':
Rafael Espindola1c130262011-01-23 04:43:11 +0000287 flags |= ELF::SHF_MERGE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000288 break;
289 case 'S':
Rafael Espindola1c130262011-01-23 04:43:11 +0000290 flags |= ELF::SHF_STRINGS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000291 break;
292 case 'T':
Rafael Espindola1c130262011-01-23 04:43:11 +0000293 flags |= ELF::SHF_TLS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000294 break;
295 case 'c':
Rafael Espindola1c130262011-01-23 04:43:11 +0000296 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000297 break;
298 case 'd':
Rafael Espindola1c130262011-01-23 04:43:11 +0000299 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000300 break;
301 case 'G':
Rafael Espindola1c130262011-01-23 04:43:11 +0000302 flags |= ELF::SHF_GROUP;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000303 break;
304 default:
305 return -1;
306 }
307 }
308
309 return flags;
310}
311
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000312bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
313 getStreamer().PushSection();
314
315 if (ParseDirectiveSection(s, loc)) {
316 getStreamer().PopSection();
317 return true;
318 }
319
320 return false;
321}
322
323bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
324 if (!getStreamer().PopSection())
325 return TokError(".popsection without corresponding .pushsection");
326 return false;
327}
328
Eli Friedman21444ef2010-07-17 04:29:04 +0000329// FIXME: This is a work in progress.
330bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
331 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000332
333 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000334 return TokError("expected identifier in directive");
335
Eli Friedman21444ef2010-07-17 04:29:04 +0000336 StringRef TypeName;
337 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000338 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000339 unsigned Flags = 0;
340
341 // Set the defaults first.
342 if (SectionName == ".fini" || SectionName == ".init" ||
343 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000344 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000345 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000346 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000347
Eli Friedman21444ef2010-07-17 04:29:04 +0000348 if (getLexer().is(AsmToken::Comma)) {
349 Lex();
350
351 if (getLexer().isNot(AsmToken::String))
352 return TokError("expected string in directive");
353
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000354 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000355 Lex();
356
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000357 int extraFlags = parseSectionFlags(FlagsStr);
358 if (extraFlags < 0)
359 return TokError("unknown flag");
360 Flags |= extraFlags;
361
Rafael Espindola1c130262011-01-23 04:43:11 +0000362 bool Mergeable = Flags & ELF::SHF_MERGE;
363 bool Group = Flags & ELF::SHF_GROUP;
Eli Friedman21444ef2010-07-17 04:29:04 +0000364
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000365 if (getLexer().isNot(AsmToken::Comma)) {
366 if (Mergeable)
367 return TokError("Mergeable section must specify the type");
368 if (Group)
369 return TokError("Group section must specify the type");
370 } else {
371 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000372 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
373 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000374
375 Lex();
376 if (getParser().ParseIdentifier(TypeName))
377 return TokError("expected identifier in directive");
378
379 if (Mergeable) {
380 if (getLexer().isNot(AsmToken::Comma))
381 return TokError("expected the entry size");
382 Lex();
383 if (getParser().ParseAbsoluteExpression(Size))
384 return true;
385 if (Size <= 0)
386 return TokError("entry size must be positive");
387 }
388
389 if (Group) {
390 if (getLexer().isNot(AsmToken::Comma))
391 return TokError("expected group name");
392 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000393 if (getParser().ParseIdentifier(GroupName))
394 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000395 if (getLexer().is(AsmToken::Comma)) {
396 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000397 StringRef Linkage;
398 if (getParser().ParseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000399 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000400 if (Linkage != "comdat")
401 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000402 }
403 }
404 }
405 }
406
407 if (getLexer().isNot(AsmToken::EndOfStatement))
408 return TokError("unexpected token in directive");
409
Rafael Espindolac85dca62011-01-23 04:28:49 +0000410 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000411
Eli Friedman21444ef2010-07-17 04:29:04 +0000412 if (!TypeName.empty()) {
413 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000414 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000415 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000416 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000417 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000418 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000419 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000420 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000421 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000422 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000423 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000424 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000425 else if (TypeName == "unwind")
426 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000427 else
428 return TokError("unknown section type");
429 }
430
Rafael Espindola25958732010-11-24 21:57:39 +0000431 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000432 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000433 Flags, Kind, Size,
434 GroupName));
Eli Friedman21444ef2010-07-17 04:29:04 +0000435 return false;
436}
437
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000438bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
439 const MCSection *PreviousSection = getStreamer().getPreviousSection();
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000440 if (PreviousSection == NULL)
441 return TokError(".previous without corresponding .section");
442 getStreamer().SwitchSection(PreviousSection);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000443
444 return false;
445}
446
Michael J. Spencere90ea132010-10-09 03:47:55 +0000447/// ParseDirectiveELFType
448/// ::= .type identifier , @attribute
449bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
450 StringRef Name;
451 if (getParser().ParseIdentifier(Name))
452 return TokError("expected identifier in directive");
453
454 // Handle the identifier as the key symbol.
455 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
456
457 if (getLexer().isNot(AsmToken::Comma))
458 return TokError("unexpected token in '.type' directive");
459 Lex();
460
Rafael Espindolad4a35262010-11-12 15:47:08 +0000461 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
462 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000463 Lex();
464
465 StringRef Type;
466 SMLoc TypeLoc;
467
468 TypeLoc = getLexer().getLoc();
469 if (getParser().ParseIdentifier(Type))
470 return TokError("expected symbol type in directive");
471
472 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
473 .Case("function", MCSA_ELF_TypeFunction)
474 .Case("object", MCSA_ELF_TypeObject)
475 .Case("tls_object", MCSA_ELF_TypeTLS)
476 .Case("common", MCSA_ELF_TypeCommon)
477 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000478 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Roman Divackya0c17a42011-12-12 17:34:04 +0000479 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000480 .Default(MCSA_Invalid);
481
482 if (Attr == MCSA_Invalid)
483 return Error(TypeLoc, "unsupported attribute in '.type' directive");
484
485 if (getLexer().isNot(AsmToken::EndOfStatement))
486 return TokError("unexpected token in '.type' directive");
487
488 Lex();
489
490 getStreamer().EmitSymbolAttribute(Sym, Attr);
491
492 return false;
493}
494
Rafael Espindola61e3b912010-10-26 19:35:47 +0000495/// ParseDirectiveIdent
496/// ::= .ident string
497bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
498 if (getLexer().isNot(AsmToken::String))
499 return TokError("unexpected token in '.ident' directive");
500
501 StringRef Data = getTok().getIdentifier();
502
503 Lex();
504
Rafael Espindola61e3b912010-10-26 19:35:47 +0000505 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000506 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000507 ELF::SHF_MERGE |
508 ELF::SHF_STRINGS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000509 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000510 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000511
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000512 getStreamer().PushSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000513 getStreamer().SwitchSection(Comment);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000514 if (!SeenIdent) {
Rafael Espindola61e3b912010-10-26 19:35:47 +0000515 getStreamer().EmitIntValue(0, 1);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000516 SeenIdent = true;
517 }
Rafael Espindola61e3b912010-10-26 19:35:47 +0000518 getStreamer().EmitBytes(Data, 0);
519 getStreamer().EmitIntValue(0, 1);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000520 getStreamer().PopSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000521 return false;
522}
523
Rafael Espindola88182132010-10-27 15:18:17 +0000524/// ParseDirectiveSymver
525/// ::= .symver foo, bar2@zed
526bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
527 StringRef Name;
528 if (getParser().ParseIdentifier(Name))
529 return TokError("expected identifier in directive");
530
531 if (getLexer().isNot(AsmToken::Comma))
532 return TokError("expected a comma");
533
534 Lex();
535
536 StringRef AliasName;
537 if (getParser().ParseIdentifier(AliasName))
538 return TokError("expected identifier in directive");
539
540 if (AliasName.find('@') == StringRef::npos)
541 return TokError("expected a '@' in the name");
542
543 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
544 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
545 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
546
547 getStreamer().EmitAssignment(Alias, Value);
548 return false;
549}
550
Rafael Espindola484291c2010-11-01 14:28:48 +0000551/// ParseDirectiveWeakref
552/// ::= .weakref foo, bar
553bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
554 // FIXME: Share code with the other alias building directives.
555
556 StringRef AliasName;
557 if (getParser().ParseIdentifier(AliasName))
558 return TokError("expected identifier in directive");
559
560 if (getLexer().isNot(AsmToken::Comma))
561 return TokError("expected a comma");
562
563 Lex();
564
565 StringRef Name;
566 if (getParser().ParseIdentifier(Name))
567 return TokError("expected identifier in directive");
568
569 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
570
571 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
572
573 getStreamer().EmitWeakReference(Alias, Sym);
574 return false;
575}
576
Daniel Dunbar5146a092010-07-12 21:23:32 +0000577namespace llvm {
578
579MCAsmParserExtension *createELFAsmParser() {
580 return new ELFAsmParser;
581}
582
583}