blob: 7b8a2d0bc5bae7193ce590a5d4a0c43b2a0c3081 [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
Benjamin Kramer766f2532013-09-15 19:53:20 +0000282static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
283 unsigned flags = 0;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000284
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;
Benjamin Kramer766f2532013-09-15 19:53:20 +0000290 case 'e':
291 flags |= ELF::SHF_EXCLUDE;
292 break;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000293 case 'x':
Rafael Espindola1c130262011-01-23 04:43:11 +0000294 flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000295 break;
296 case 'w':
Rafael Espindola1c130262011-01-23 04:43:11 +0000297 flags |= ELF::SHF_WRITE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000298 break;
299 case 'M':
Rafael Espindola1c130262011-01-23 04:43:11 +0000300 flags |= ELF::SHF_MERGE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000301 break;
302 case 'S':
Rafael Espindola1c130262011-01-23 04:43:11 +0000303 flags |= ELF::SHF_STRINGS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000304 break;
305 case 'T':
Rafael Espindola1c130262011-01-23 04:43:11 +0000306 flags |= ELF::SHF_TLS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000307 break;
308 case 'c':
Rafael Espindola1c130262011-01-23 04:43:11 +0000309 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000310 break;
311 case 'd':
Rafael Espindola1c130262011-01-23 04:43:11 +0000312 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000313 break;
314 case 'G':
Rafael Espindola1c130262011-01-23 04:43:11 +0000315 flags |= ELF::SHF_GROUP;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000316 break;
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000317 case '?':
318 *UseLastGroup = true;
319 break;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000320 default:
Benjamin Kramer766f2532013-09-15 19:53:20 +0000321 return -1U;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000322 }
323 }
324
325 return flags;
326}
327
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000328bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
329 getStreamer().PushSection();
330
Peter Collingbournedf39be62013-04-17 21:18:16 +0000331 if (ParseSectionArguments(/*IsPush=*/true)) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000332 getStreamer().PopSection();
333 return true;
334 }
335
336 return false;
337}
338
339bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
340 if (!getStreamer().PopSection())
341 return TokError(".popsection without corresponding .pushsection");
342 return false;
343}
344
Eli Friedman21444ef2010-07-17 04:29:04 +0000345// FIXME: This is a work in progress.
346bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000347 return ParseSectionArguments(/*IsPush=*/false);
348}
349
350bool ELFAsmParser::ParseSectionArguments(bool IsPush) {
Eli Friedman21444ef2010-07-17 04:29:04 +0000351 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000352
353 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000354 return TokError("expected identifier in directive");
355
Eli Friedman21444ef2010-07-17 04:29:04 +0000356 StringRef TypeName;
357 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000358 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000359 unsigned Flags = 0;
Peter Collingbournedf39be62013-04-17 21:18:16 +0000360 const MCExpr *Subsection = 0;
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000361 bool UseLastGroup = false;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000362
363 // Set the defaults first.
364 if (SectionName == ".fini" || SectionName == ".init" ||
365 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000366 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000367 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000368 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000369
Eli Friedman21444ef2010-07-17 04:29:04 +0000370 if (getLexer().is(AsmToken::Comma)) {
371 Lex();
372
Peter Collingbournedf39be62013-04-17 21:18:16 +0000373 if (IsPush && getLexer().isNot(AsmToken::String)) {
374 if (getParser().parseExpression(Subsection))
375 return true;
376 if (getLexer().isNot(AsmToken::Comma))
377 goto EndStmt;
378 Lex();
379 }
380
Eli Friedman21444ef2010-07-17 04:29:04 +0000381 if (getLexer().isNot(AsmToken::String))
382 return TokError("expected string in directive");
383
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000384 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000385 Lex();
386
Benjamin Kramer766f2532013-09-15 19:53:20 +0000387 unsigned extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
388 if (extraFlags == -1U)
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000389 return TokError("unknown flag");
390 Flags |= extraFlags;
391
Rafael Espindola1c130262011-01-23 04:43:11 +0000392 bool Mergeable = Flags & ELF::SHF_MERGE;
393 bool Group = Flags & ELF::SHF_GROUP;
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000394 if (Group && UseLastGroup)
395 return TokError("Section cannot specifiy a group name while also acting "
396 "as a member of the last group");
Eli Friedman21444ef2010-07-17 04:29:04 +0000397
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000398 if (getLexer().isNot(AsmToken::Comma)) {
399 if (Mergeable)
400 return TokError("Mergeable section must specify the type");
401 if (Group)
402 return TokError("Group section must specify the type");
403 } else {
404 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000405 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
406 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000407
408 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000409 if (getParser().parseIdentifier(TypeName))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000410 return TokError("expected identifier in directive");
411
412 if (Mergeable) {
413 if (getLexer().isNot(AsmToken::Comma))
414 return TokError("expected the entry size");
415 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000416 if (getParser().parseAbsoluteExpression(Size))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000417 return true;
418 if (Size <= 0)
419 return TokError("entry size must be positive");
420 }
421
422 if (Group) {
423 if (getLexer().isNot(AsmToken::Comma))
424 return TokError("expected group name");
425 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000426 if (getParser().parseIdentifier(GroupName))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000427 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000428 if (getLexer().is(AsmToken::Comma)) {
429 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000430 StringRef Linkage;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000431 if (getParser().parseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000432 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000433 if (Linkage != "comdat")
434 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000435 }
436 }
437 }
438 }
439
Peter Collingbournedf39be62013-04-17 21:18:16 +0000440EndStmt:
Eli Friedman21444ef2010-07-17 04:29:04 +0000441 if (getLexer().isNot(AsmToken::EndOfStatement))
442 return TokError("unexpected token in directive");
443
Rafael Espindolac85dca62011-01-23 04:28:49 +0000444 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000445
Joerg Sonnenberger42edeb12013-02-16 00:32:53 +0000446 if (TypeName.empty()) {
447 if (SectionName.startswith(".note"))
448 Type = ELF::SHT_NOTE;
449 else if (SectionName == ".init_array")
450 Type = ELF::SHT_INIT_ARRAY;
451 else if (SectionName == ".fini_array")
452 Type = ELF::SHT_FINI_ARRAY;
453 else if (SectionName == ".preinit_array")
454 Type = ELF::SHT_PREINIT_ARRAY;
455 } else {
Eli Friedman21444ef2010-07-17 04:29:04 +0000456 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000457 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000458 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000459 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000460 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000461 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000462 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000463 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000464 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000465 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000466 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000467 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000468 else if (TypeName == "unwind")
469 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000470 else
471 return TokError("unknown section type");
472 }
473
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000474 if (UseLastGroup) {
475 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
476 if (const MCSectionELF *Section =
477 cast_or_null<MCSectionELF>(CurrentSection.first))
478 if (const MCSymbol *Group = Section->getGroup()) {
479 GroupName = Group->getName();
480 Flags |= ELF::SHF_GROUP;
481 }
482 }
483
Rafael Espindola25958732010-11-24 21:57:39 +0000484 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000485 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000486 Flags, Kind, Size,
Peter Collingbournedf39be62013-04-17 21:18:16 +0000487 GroupName),
488 Subsection);
Eli Friedman21444ef2010-07-17 04:29:04 +0000489 return false;
490}
491
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000492bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000493 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
494 if (PreviousSection.first == NULL)
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000495 return TokError(".previous without corresponding .section");
Peter Collingbournedf39be62013-04-17 21:18:16 +0000496 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000497
498 return false;
499}
500
Michael J. Spencere90ea132010-10-09 03:47:55 +0000501/// ParseDirectiveELFType
502/// ::= .type identifier , @attribute
503bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
504 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000505 if (getParser().parseIdentifier(Name))
Michael J. Spencere90ea132010-10-09 03:47:55 +0000506 return TokError("expected identifier in directive");
507
508 // Handle the identifier as the key symbol.
509 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
510
511 if (getLexer().isNot(AsmToken::Comma))
512 return TokError("unexpected token in '.type' directive");
513 Lex();
514
Rafael Espindolad4a35262010-11-12 15:47:08 +0000515 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
516 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000517 Lex();
518
519 StringRef Type;
520 SMLoc TypeLoc;
521
522 TypeLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000523 if (getParser().parseIdentifier(Type))
Michael J. Spencere90ea132010-10-09 03:47:55 +0000524 return TokError("expected symbol type in directive");
525
526 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
527 .Case("function", MCSA_ELF_TypeFunction)
528 .Case("object", MCSA_ELF_TypeObject)
529 .Case("tls_object", MCSA_ELF_TypeTLS)
530 .Case("common", MCSA_ELF_TypeCommon)
531 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000532 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Roman Divackya0c17a42011-12-12 17:34:04 +0000533 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000534 .Default(MCSA_Invalid);
535
536 if (Attr == MCSA_Invalid)
537 return Error(TypeLoc, "unsupported attribute in '.type' directive");
538
539 if (getLexer().isNot(AsmToken::EndOfStatement))
540 return TokError("unexpected token in '.type' directive");
541
542 Lex();
543
544 getStreamer().EmitSymbolAttribute(Sym, Attr);
545
546 return false;
547}
548
Rafael Espindola61e3b912010-10-26 19:35:47 +0000549/// ParseDirectiveIdent
550/// ::= .ident string
551bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
552 if (getLexer().isNot(AsmToken::String))
553 return TokError("unexpected token in '.ident' directive");
554
555 StringRef Data = getTok().getIdentifier();
556
557 Lex();
558
Rafael Espindola61e3b912010-10-26 19:35:47 +0000559 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000560 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000561 ELF::SHF_MERGE |
562 ELF::SHF_STRINGS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000563 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000564 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000565
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000566 getStreamer().PushSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000567 getStreamer().SwitchSection(Comment);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000568 if (!SeenIdent) {
Rafael Espindola61e3b912010-10-26 19:35:47 +0000569 getStreamer().EmitIntValue(0, 1);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000570 SeenIdent = true;
571 }
Eric Christopher68ca5622013-01-09 01:57:54 +0000572 getStreamer().EmitBytes(Data);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000573 getStreamer().EmitIntValue(0, 1);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000574 getStreamer().PopSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000575 return false;
576}
577
Rafael Espindola88182132010-10-27 15:18:17 +0000578/// ParseDirectiveSymver
579/// ::= .symver foo, bar2@zed
580bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
581 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000582 if (getParser().parseIdentifier(Name))
Rafael Espindola88182132010-10-27 15:18:17 +0000583 return TokError("expected identifier in directive");
584
585 if (getLexer().isNot(AsmToken::Comma))
586 return TokError("expected a comma");
587
588 Lex();
589
590 StringRef AliasName;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000591 if (getParser().parseIdentifier(AliasName))
Rafael Espindola88182132010-10-27 15:18:17 +0000592 return TokError("expected identifier in directive");
593
594 if (AliasName.find('@') == StringRef::npos)
595 return TokError("expected a '@' in the name");
596
597 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
598 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
599 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
600
601 getStreamer().EmitAssignment(Alias, Value);
602 return false;
603}
604
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000605/// ParseDirectiveVersion
606/// ::= .version string
607bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
608 if (getLexer().isNot(AsmToken::String))
609 return TokError("unexpected token in '.version' directive");
610
611 StringRef Data = getTok().getIdentifier();
612
613 Lex();
614
615 const MCSection *Note =
616 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
617 SectionKind::getReadOnly());
618
619 getStreamer().PushSection();
620 getStreamer().SwitchSection(Note);
621 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
622 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
623 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christopher68ca5622013-01-09 01:57:54 +0000624 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000625 getStreamer().EmitIntValue(0, 1); // terminate the string.
626 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
627 getStreamer().PopSection();
628 return false;
629}
630
Rafael Espindola484291c2010-11-01 14:28:48 +0000631/// ParseDirectiveWeakref
632/// ::= .weakref foo, bar
633bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
634 // FIXME: Share code with the other alias building directives.
635
636 StringRef AliasName;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000637 if (getParser().parseIdentifier(AliasName))
Rafael Espindola484291c2010-11-01 14:28:48 +0000638 return TokError("expected identifier in directive");
639
640 if (getLexer().isNot(AsmToken::Comma))
641 return TokError("expected a comma");
642
643 Lex();
644
645 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000646 if (getParser().parseIdentifier(Name))
Rafael Espindola484291c2010-11-01 14:28:48 +0000647 return TokError("expected identifier in directive");
648
649 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
650
651 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
652
653 getStreamer().EmitWeakReference(Alias, Sym);
654 return false;
655}
656
Peter Collingbournedf39be62013-04-17 21:18:16 +0000657bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
658 const MCExpr *Subsection = 0;
659 if (getLexer().isNot(AsmToken::EndOfStatement)) {
660 if (getParser().parseExpression(Subsection))
661 return true;
662 }
663
664 if (getLexer().isNot(AsmToken::EndOfStatement))
665 return TokError("unexpected token in directive");
666
667 getStreamer().SubSection(Subsection);
668 return false;
669}
670
Daniel Dunbar5146a092010-07-12 21:23:32 +0000671namespace llvm {
672
673MCAsmParserExtension *createELFAsmParser() {
674 return new ELFAsmParser;
675}
676
677}