blob: 67b0e5df24aa1d3c4d7d92de8939eb782aac4bd8 [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"
David Majnemerbcd9b3b2013-09-15 19:24:16 +000019#include "llvm/MC/MCSymbol.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000020#include "llvm/Support/ELF.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000021using namespace llvm;
22
23namespace {
24
25class ELFAsmParser : public MCAsmParserExtension {
Eli Bendersky171192f2013-01-16 00:50:52 +000026 template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000027 void addDirectiveHandler(StringRef Directive) {
Eli Bendersky171192f2013-01-16 00:50:52 +000028 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
29 this, HandleDirective<ELFAsmParser, HandlerMethod>);
30
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000031 getParser().addDirectiveHandler(Directive, Handler);
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000032 }
33
Daniel Dunbar5146a092010-07-12 21:23:32 +000034 bool ParseSectionSwitch(StringRef Section, unsigned Type,
35 unsigned Flags, SectionKind Kind);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +000036 bool SeenIdent;
Daniel Dunbar5146a092010-07-12 21:23:32 +000037
38public:
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +000039 ELFAsmParser() : SeenIdent(false) {
40 BracketExpressionsSupported = true;
41 }
Daniel Dunbar5146a092010-07-12 21:23:32 +000042
43 virtual void Initialize(MCAsmParser &Parser) {
44 // Call the base implementation.
45 this->MCAsmParserExtension::Initialize(Parser);
46
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000047 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
48 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
49 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
50 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
51 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
52 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
53 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000054 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000055 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000056 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000057 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000058 &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000059 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000060 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000061 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
62 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000063 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000064 addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
65 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
66 addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
67 addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
68 addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
69 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
70 addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
71 addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
72 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
73 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
74 addDirectiveHandler<
Jim Grosbachf2a35fb2011-07-25 17:55:35 +000075 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000076 addDirectiveHandler<
Jim Grosbachf2a35fb2011-07-25 17:55:35 +000077 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000078 addDirectiveHandler<
Jim Grosbachf2a35fb2011-07-25 17:55:35 +000079 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
Peter Collingbournedf39be62013-04-17 21:18:16 +000080 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
Daniel Dunbar5146a092010-07-12 21:23:32 +000081 }
82
Rafael Espindolad80781b2010-09-15 21:48:40 +000083 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
84 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000085 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000086 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000087 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Daniel Dunbar5146a092010-07-12 21:23:32 +000088 SectionKind::getDataRel());
89 }
90 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000091 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000092 ELF::SHF_EXECINSTR |
93 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbar5146a092010-07-12 21:23:32 +000094 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000095 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000096 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000097 ELF::SHF_WRITE |
98 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Flemingf525c2a2010-07-20 21:12:46 +000099 }
100 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000101 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000102 ELF::SHF_ALLOC,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000103 SectionKind::getReadOnly());
104 }
105 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000106 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000107 ELF::SHF_ALLOC |
108 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000109 SectionKind::getThreadData());
110 }
111 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000112 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000113 ELF::SHF_ALLOC |
114 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000115 SectionKind::getThreadBSS());
116 }
117 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000118 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000119 ELF::SHF_ALLOC |
120 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000121 SectionKind::getDataRel());
122 }
123 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000124 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000125 ELF::SHF_ALLOC |
126 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000127 SectionKind::getReadOnlyWithRel());
128 }
129 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000130 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000131 ELF::SHF_ALLOC |
132 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000133 SectionKind::getReadOnlyWithRelLocal());
134 }
135 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000136 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000137 ELF::SHF_ALLOC |
138 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000139 SectionKind::getDataRel());
140 }
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000141 bool ParseDirectivePushSection(StringRef, SMLoc);
142 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman21444ef2010-07-17 04:29:04 +0000143 bool ParseDirectiveSection(StringRef, SMLoc);
144 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000145 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000146 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000147 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000148 bool ParseDirectiveSymver(StringRef, SMLoc);
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000149 bool ParseDirectiveVersion(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000150 bool ParseDirectiveWeakref(StringRef, SMLoc);
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000151 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
Peter Collingbournedf39be62013-04-17 21:18:16 +0000152 bool ParseDirectiveSubsection(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000153
154private:
155 bool ParseSectionName(StringRef &SectionName);
Peter Collingbournedf39be62013-04-17 21:18:16 +0000156 bool ParseSectionArguments(bool IsPush);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000157};
158
159}
160
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000161/// ParseDirectiveSymbolAttribute
162/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
163bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
164 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
165 .Case(".weak", MCSA_Weak)
166 .Case(".local", MCSA_Local)
167 .Case(".hidden", MCSA_Hidden)
168 .Case(".internal", MCSA_Internal)
169 .Case(".protected", MCSA_Protected)
170 .Default(MCSA_Invalid);
171 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
172 if (getLexer().isNot(AsmToken::EndOfStatement)) {
173 for (;;) {
174 StringRef Name;
175
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000176 if (getParser().parseIdentifier(Name))
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000177 return TokError("expected identifier in directive");
178
179 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
180
181 getStreamer().EmitSymbolAttribute(Sym, Attr);
182
183 if (getLexer().is(AsmToken::EndOfStatement))
184 break;
185
186 if (getLexer().isNot(AsmToken::Comma))
187 return TokError("unexpected token in directive");
188 Lex();
189 }
190 }
191
192 Lex();
193 return false;
194}
195
Daniel Dunbar5146a092010-07-12 21:23:32 +0000196bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
197 unsigned Flags, SectionKind Kind) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000198 const MCExpr *Subsection = 0;
199 if (getLexer().isNot(AsmToken::EndOfStatement)) {
200 if (getParser().parseExpression(Subsection))
201 return true;
202 }
Daniel Dunbar5146a092010-07-12 21:23:32 +0000203
204 getStreamer().SwitchSection(getContext().getELFSection(
Peter Collingbournedf39be62013-04-17 21:18:16 +0000205 Section, Type, Flags, Kind),
206 Subsection);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000207
208 return false;
209}
210
Eli Friedman21444ef2010-07-17 04:29:04 +0000211bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000212 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000213 if (getParser().parseIdentifier(Name))
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000214 return TokError("expected identifier in directive");
Dmitri Gribenko2de05722012-09-10 21:26:47 +0000215 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000216
217 if (getLexer().isNot(AsmToken::Comma))
218 return TokError("unexpected token in directive");
219 Lex();
220
221 const MCExpr *Expr;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000222 if (getParser().parseExpression(Expr))
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000223 return true;
224
225 if (getLexer().isNot(AsmToken::EndOfStatement))
226 return TokError("unexpected token in directive");
227
228 getStreamer().EmitELFSize(Sym, Expr);
229 return false;
230}
231
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000232bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
233 // A section name can contain -, so we cannot just use
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000234 // parseIdentifier.
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000235 SMLoc FirstLoc = getLexer().getLoc();
236 unsigned Size = 0;
237
Rafael Espindola184640e2011-01-24 18:02:54 +0000238 if (getLexer().is(AsmToken::String)) {
239 SectionName = getTok().getIdentifier();
240 Lex();
241 return false;
242 }
243
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000244 for (;;) {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000245 unsigned CurSize;
246
247 SMLoc PrevLoc = getLexer().getLoc();
248 if (getLexer().is(AsmToken::Minus)) {
249 CurSize = 1;
250 Lex(); // Consume the "-".
Rafael Espindola184640e2011-01-24 18:02:54 +0000251 } else if (getLexer().is(AsmToken::String)) {
252 CurSize = getTok().getIdentifier().size() + 2;
253 Lex();
254 } else if (getLexer().is(AsmToken::Identifier)) {
255 CurSize = getTok().getIdentifier().size();
256 Lex();
257 } else {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000258 break;
Rafael Espindola184640e2011-01-24 18:02:54 +0000259 }
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000260
261 Size += CurSize;
262 SectionName = StringRef(FirstLoc.getPointer(), Size);
263
264 // Make sure the following token is adjacent.
265 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
266 break;
267 }
268 if (Size == 0)
269 return true;
270
271 return false;
272}
273
Rafael Espindola25958732010-11-24 21:57:39 +0000274static SectionKind computeSectionKind(unsigned Flags) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000275 if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola25958732010-11-24 21:57:39 +0000276 return SectionKind::getText();
Rafael Espindola1c130262011-01-23 04:43:11 +0000277 if (Flags & ELF::SHF_TLS)
Rafael Espindola25958732010-11-24 21:57:39 +0000278 return SectionKind::getThreadData();
279 return SectionKind::getDataRel();
280}
281
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000282static int parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000283 int flags = 0;
284
285 for (unsigned i = 0; i < flagsStr.size(); i++) {
286 switch (flagsStr[i]) {
287 case 'a':
Rafael Espindola1c130262011-01-23 04:43:11 +0000288 flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000289 break;
290 case 'x':
Rafael Espindola1c130262011-01-23 04:43:11 +0000291 flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000292 break;
293 case 'w':
Rafael Espindola1c130262011-01-23 04:43:11 +0000294 flags |= ELF::SHF_WRITE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000295 break;
296 case 'M':
Rafael Espindola1c130262011-01-23 04:43:11 +0000297 flags |= ELF::SHF_MERGE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000298 break;
299 case 'S':
Rafael Espindola1c130262011-01-23 04:43:11 +0000300 flags |= ELF::SHF_STRINGS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000301 break;
302 case 'T':
Rafael Espindola1c130262011-01-23 04:43:11 +0000303 flags |= ELF::SHF_TLS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000304 break;
305 case 'c':
Rafael Espindola1c130262011-01-23 04:43:11 +0000306 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000307 break;
308 case 'd':
Rafael Espindola1c130262011-01-23 04:43:11 +0000309 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000310 break;
311 case 'G':
Rafael Espindola1c130262011-01-23 04:43:11 +0000312 flags |= ELF::SHF_GROUP;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000313 break;
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000314 case '?':
315 *UseLastGroup = true;
316 break;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000317 default:
318 return -1;
319 }
320 }
321
322 return flags;
323}
324
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000325bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
326 getStreamer().PushSection();
327
Peter Collingbournedf39be62013-04-17 21:18:16 +0000328 if (ParseSectionArguments(/*IsPush=*/true)) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000329 getStreamer().PopSection();
330 return true;
331 }
332
333 return false;
334}
335
336bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
337 if (!getStreamer().PopSection())
338 return TokError(".popsection without corresponding .pushsection");
339 return false;
340}
341
Eli Friedman21444ef2010-07-17 04:29:04 +0000342// FIXME: This is a work in progress.
343bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000344 return ParseSectionArguments(/*IsPush=*/false);
345}
346
347bool ELFAsmParser::ParseSectionArguments(bool IsPush) {
Eli Friedman21444ef2010-07-17 04:29:04 +0000348 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000349
350 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000351 return TokError("expected identifier in directive");
352
Eli Friedman21444ef2010-07-17 04:29:04 +0000353 StringRef TypeName;
354 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000355 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000356 unsigned Flags = 0;
Peter Collingbournedf39be62013-04-17 21:18:16 +0000357 const MCExpr *Subsection = 0;
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000358 bool UseLastGroup = false;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000359
360 // Set the defaults first.
361 if (SectionName == ".fini" || SectionName == ".init" ||
362 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000363 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000364 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000365 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000366
Eli Friedman21444ef2010-07-17 04:29:04 +0000367 if (getLexer().is(AsmToken::Comma)) {
368 Lex();
369
Peter Collingbournedf39be62013-04-17 21:18:16 +0000370 if (IsPush && getLexer().isNot(AsmToken::String)) {
371 if (getParser().parseExpression(Subsection))
372 return true;
373 if (getLexer().isNot(AsmToken::Comma))
374 goto EndStmt;
375 Lex();
376 }
377
Eli Friedman21444ef2010-07-17 04:29:04 +0000378 if (getLexer().isNot(AsmToken::String))
379 return TokError("expected string in directive");
380
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000381 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000382 Lex();
383
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000384 int extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000385 if (extraFlags < 0)
386 return TokError("unknown flag");
387 Flags |= extraFlags;
388
Rafael Espindola1c130262011-01-23 04:43:11 +0000389 bool Mergeable = Flags & ELF::SHF_MERGE;
390 bool Group = Flags & ELF::SHF_GROUP;
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000391 if (Group && UseLastGroup)
392 return TokError("Section cannot specifiy a group name while also acting "
393 "as a member of the last group");
Eli Friedman21444ef2010-07-17 04:29:04 +0000394
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000395 if (getLexer().isNot(AsmToken::Comma)) {
396 if (Mergeable)
397 return TokError("Mergeable section must specify the type");
398 if (Group)
399 return TokError("Group section must specify the type");
400 } else {
401 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000402 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
403 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000404
405 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000406 if (getParser().parseIdentifier(TypeName))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000407 return TokError("expected identifier in directive");
408
409 if (Mergeable) {
410 if (getLexer().isNot(AsmToken::Comma))
411 return TokError("expected the entry size");
412 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000413 if (getParser().parseAbsoluteExpression(Size))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000414 return true;
415 if (Size <= 0)
416 return TokError("entry size must be positive");
417 }
418
419 if (Group) {
420 if (getLexer().isNot(AsmToken::Comma))
421 return TokError("expected group name");
422 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000423 if (getParser().parseIdentifier(GroupName))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000424 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000425 if (getLexer().is(AsmToken::Comma)) {
426 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000427 StringRef Linkage;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000428 if (getParser().parseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000429 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000430 if (Linkage != "comdat")
431 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000432 }
433 }
434 }
435 }
436
Peter Collingbournedf39be62013-04-17 21:18:16 +0000437EndStmt:
Eli Friedman21444ef2010-07-17 04:29:04 +0000438 if (getLexer().isNot(AsmToken::EndOfStatement))
439 return TokError("unexpected token in directive");
440
Rafael Espindolac85dca62011-01-23 04:28:49 +0000441 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000442
Joerg Sonnenberger42edeb12013-02-16 00:32:53 +0000443 if (TypeName.empty()) {
444 if (SectionName.startswith(".note"))
445 Type = ELF::SHT_NOTE;
446 else if (SectionName == ".init_array")
447 Type = ELF::SHT_INIT_ARRAY;
448 else if (SectionName == ".fini_array")
449 Type = ELF::SHT_FINI_ARRAY;
450 else if (SectionName == ".preinit_array")
451 Type = ELF::SHT_PREINIT_ARRAY;
452 } else {
Eli Friedman21444ef2010-07-17 04:29:04 +0000453 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000454 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000455 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000456 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000457 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000458 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000459 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000460 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000461 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000462 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000463 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000464 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000465 else if (TypeName == "unwind")
466 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000467 else
468 return TokError("unknown section type");
469 }
470
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000471 if (UseLastGroup) {
472 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
473 if (const MCSectionELF *Section =
474 cast_or_null<MCSectionELF>(CurrentSection.first))
475 if (const MCSymbol *Group = Section->getGroup()) {
476 GroupName = Group->getName();
477 Flags |= ELF::SHF_GROUP;
478 }
479 }
480
Rafael Espindola25958732010-11-24 21:57:39 +0000481 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000482 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000483 Flags, Kind, Size,
Peter Collingbournedf39be62013-04-17 21:18:16 +0000484 GroupName),
485 Subsection);
Eli Friedman21444ef2010-07-17 04:29:04 +0000486 return false;
487}
488
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000489bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000490 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
491 if (PreviousSection.first == NULL)
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000492 return TokError(".previous without corresponding .section");
Peter Collingbournedf39be62013-04-17 21:18:16 +0000493 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000494
495 return false;
496}
497
Michael J. Spencere90ea132010-10-09 03:47:55 +0000498/// ParseDirectiveELFType
499/// ::= .type identifier , @attribute
500bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
501 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000502 if (getParser().parseIdentifier(Name))
Michael J. Spencere90ea132010-10-09 03:47:55 +0000503 return TokError("expected identifier in directive");
504
505 // Handle the identifier as the key symbol.
506 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
507
508 if (getLexer().isNot(AsmToken::Comma))
509 return TokError("unexpected token in '.type' directive");
510 Lex();
511
Rafael Espindolad4a35262010-11-12 15:47:08 +0000512 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
513 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000514 Lex();
515
516 StringRef Type;
517 SMLoc TypeLoc;
518
519 TypeLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000520 if (getParser().parseIdentifier(Type))
Michael J. Spencere90ea132010-10-09 03:47:55 +0000521 return TokError("expected symbol type in directive");
522
523 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
524 .Case("function", MCSA_ELF_TypeFunction)
525 .Case("object", MCSA_ELF_TypeObject)
526 .Case("tls_object", MCSA_ELF_TypeTLS)
527 .Case("common", MCSA_ELF_TypeCommon)
528 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000529 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Roman Divackya0c17a42011-12-12 17:34:04 +0000530 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000531 .Default(MCSA_Invalid);
532
533 if (Attr == MCSA_Invalid)
534 return Error(TypeLoc, "unsupported attribute in '.type' directive");
535
536 if (getLexer().isNot(AsmToken::EndOfStatement))
537 return TokError("unexpected token in '.type' directive");
538
539 Lex();
540
541 getStreamer().EmitSymbolAttribute(Sym, Attr);
542
543 return false;
544}
545
Rafael Espindola61e3b912010-10-26 19:35:47 +0000546/// ParseDirectiveIdent
547/// ::= .ident string
548bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
549 if (getLexer().isNot(AsmToken::String))
550 return TokError("unexpected token in '.ident' directive");
551
552 StringRef Data = getTok().getIdentifier();
553
554 Lex();
555
Rafael Espindola61e3b912010-10-26 19:35:47 +0000556 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000557 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000558 ELF::SHF_MERGE |
559 ELF::SHF_STRINGS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000560 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000561 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000562
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000563 getStreamer().PushSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000564 getStreamer().SwitchSection(Comment);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000565 if (!SeenIdent) {
Rafael Espindola61e3b912010-10-26 19:35:47 +0000566 getStreamer().EmitIntValue(0, 1);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000567 SeenIdent = true;
568 }
Eric Christopher68ca5622013-01-09 01:57:54 +0000569 getStreamer().EmitBytes(Data);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000570 getStreamer().EmitIntValue(0, 1);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000571 getStreamer().PopSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000572 return false;
573}
574
Rafael Espindola88182132010-10-27 15:18:17 +0000575/// ParseDirectiveSymver
576/// ::= .symver foo, bar2@zed
577bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
578 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000579 if (getParser().parseIdentifier(Name))
Rafael Espindola88182132010-10-27 15:18:17 +0000580 return TokError("expected identifier in directive");
581
582 if (getLexer().isNot(AsmToken::Comma))
583 return TokError("expected a comma");
584
585 Lex();
586
587 StringRef AliasName;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000588 if (getParser().parseIdentifier(AliasName))
Rafael Espindola88182132010-10-27 15:18:17 +0000589 return TokError("expected identifier in directive");
590
591 if (AliasName.find('@') == StringRef::npos)
592 return TokError("expected a '@' in the name");
593
594 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
595 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
596 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
597
598 getStreamer().EmitAssignment(Alias, Value);
599 return false;
600}
601
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000602/// ParseDirectiveVersion
603/// ::= .version string
604bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
605 if (getLexer().isNot(AsmToken::String))
606 return TokError("unexpected token in '.version' directive");
607
608 StringRef Data = getTok().getIdentifier();
609
610 Lex();
611
612 const MCSection *Note =
613 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
614 SectionKind::getReadOnly());
615
616 getStreamer().PushSection();
617 getStreamer().SwitchSection(Note);
618 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
619 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
620 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christopher68ca5622013-01-09 01:57:54 +0000621 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000622 getStreamer().EmitIntValue(0, 1); // terminate the string.
623 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
624 getStreamer().PopSection();
625 return false;
626}
627
Rafael Espindola484291c2010-11-01 14:28:48 +0000628/// ParseDirectiveWeakref
629/// ::= .weakref foo, bar
630bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
631 // FIXME: Share code with the other alias building directives.
632
633 StringRef AliasName;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000634 if (getParser().parseIdentifier(AliasName))
Rafael Espindola484291c2010-11-01 14:28:48 +0000635 return TokError("expected identifier in directive");
636
637 if (getLexer().isNot(AsmToken::Comma))
638 return TokError("expected a comma");
639
640 Lex();
641
642 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000643 if (getParser().parseIdentifier(Name))
Rafael Espindola484291c2010-11-01 14:28:48 +0000644 return TokError("expected identifier in directive");
645
646 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
647
648 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
649
650 getStreamer().EmitWeakReference(Alias, Sym);
651 return false;
652}
653
Peter Collingbournedf39be62013-04-17 21:18:16 +0000654bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
655 const MCExpr *Subsection = 0;
656 if (getLexer().isNot(AsmToken::EndOfStatement)) {
657 if (getParser().parseExpression(Subsection))
658 return true;
659 }
660
661 if (getLexer().isNot(AsmToken::EndOfStatement))
662 return TokError("unexpected token in directive");
663
664 getStreamer().SubSection(Subsection);
665 return false;
666}
667
Daniel Dunbar5146a092010-07-12 21:23:32 +0000668namespace llvm {
669
670MCAsmParserExtension *createELFAsmParser() {
671 return new ELFAsmParser;
672}
673
674}