blob: 964f915679bbe2314574273905b4823b3817780d [file] [log] [blame]
Daniel Dunbar5146a092010-07-12 21:23:32 +00001//===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCParser/MCAsmParserExtension.h"
Michael J. Spencere90ea132010-10-09 03:47:55 +000011#include "llvm/ADT/StringSwitch.h"
Eli Friedmandc1ad222010-07-17 06:27:28 +000012#include "llvm/ADT/Twine.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000013#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000014#include "llvm/MC/MCContext.h"
Rafael Espindola88182132010-10-27 15:18:17 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000016#include "llvm/MC/MCParser/MCAsmLexer.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000017#include "llvm/MC/MCSectionELF.h"
18#include "llvm/MC/MCStreamer.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000019#include "llvm/Support/ELF.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000020using namespace llvm;
21
22namespace {
23
24class ELFAsmParser : public MCAsmParserExtension {
Eli Bendersky171192f2013-01-16 00:50:52 +000025 template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000026 void addDirectiveHandler(StringRef Directive) {
Eli Bendersky171192f2013-01-16 00:50:52 +000027 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
28 this, HandleDirective<ELFAsmParser, HandlerMethod>);
29
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000030 getParser().addDirectiveHandler(Directive, Handler);
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000031 }
32
Daniel Dunbar5146a092010-07-12 21:23:32 +000033 bool ParseSectionSwitch(StringRef Section, unsigned Type,
34 unsigned Flags, SectionKind Kind);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +000035 bool SeenIdent;
Daniel Dunbar5146a092010-07-12 21:23:32 +000036
37public:
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +000038 ELFAsmParser() : SeenIdent(false) {
39 BracketExpressionsSupported = true;
40 }
Daniel Dunbar5146a092010-07-12 21:23:32 +000041
42 virtual void Initialize(MCAsmParser &Parser) {
43 // Call the base implementation.
44 this->MCAsmParserExtension::Initialize(Parser);
45
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000046 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
47 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
48 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
49 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
50 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
51 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
52 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000053 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000054 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000055 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000056 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000057 &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000058 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000059 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000060 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
61 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000062 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000063 addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
64 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
65 addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
66 addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
67 addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
68 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
69 addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
70 addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
71 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
72 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
73 addDirectiveHandler<
Jim Grosbachf2a35fb2011-07-25 17:55:35 +000074 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000075 addDirectiveHandler<
Jim Grosbachf2a35fb2011-07-25 17:55:35 +000076 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000077 addDirectiveHandler<
Jim Grosbachf2a35fb2011-07-25 17:55:35 +000078 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
Peter Collingbournedf39be62013-04-17 21:18:16 +000079 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
Daniel Dunbar5146a092010-07-12 21:23:32 +000080 }
81
Rafael Espindolad80781b2010-09-15 21:48:40 +000082 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
83 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000084 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000085 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000086 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Daniel Dunbar5146a092010-07-12 21:23:32 +000087 SectionKind::getDataRel());
88 }
89 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000090 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000091 ELF::SHF_EXECINSTR |
92 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbar5146a092010-07-12 21:23:32 +000093 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000094 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000095 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000096 ELF::SHF_WRITE |
97 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Flemingf525c2a2010-07-20 21:12:46 +000098 }
99 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000100 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000101 ELF::SHF_ALLOC,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000102 SectionKind::getReadOnly());
103 }
104 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000105 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000106 ELF::SHF_ALLOC |
107 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000108 SectionKind::getThreadData());
109 }
110 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000111 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000112 ELF::SHF_ALLOC |
113 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000114 SectionKind::getThreadBSS());
115 }
116 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000117 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000118 ELF::SHF_ALLOC |
119 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000120 SectionKind::getDataRel());
121 }
122 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000123 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000124 ELF::SHF_ALLOC |
125 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000126 SectionKind::getReadOnlyWithRel());
127 }
128 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000129 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000130 ELF::SHF_ALLOC |
131 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000132 SectionKind::getReadOnlyWithRelLocal());
133 }
134 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000135 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000136 ELF::SHF_ALLOC |
137 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000138 SectionKind::getDataRel());
139 }
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000140 bool ParseDirectivePushSection(StringRef, SMLoc);
141 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman21444ef2010-07-17 04:29:04 +0000142 bool ParseDirectiveSection(StringRef, SMLoc);
143 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000144 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000145 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000146 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000147 bool ParseDirectiveSymver(StringRef, SMLoc);
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000148 bool ParseDirectiveVersion(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000149 bool ParseDirectiveWeakref(StringRef, SMLoc);
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000150 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
Peter Collingbournedf39be62013-04-17 21:18:16 +0000151 bool ParseDirectiveSubsection(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000152
153private:
154 bool ParseSectionName(StringRef &SectionName);
Peter Collingbournedf39be62013-04-17 21:18:16 +0000155 bool ParseSectionArguments(bool IsPush);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000156};
157
158}
159
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000160/// ParseDirectiveSymbolAttribute
161/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
162bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
163 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
164 .Case(".weak", MCSA_Weak)
165 .Case(".local", MCSA_Local)
166 .Case(".hidden", MCSA_Hidden)
167 .Case(".internal", MCSA_Internal)
168 .Case(".protected", MCSA_Protected)
169 .Default(MCSA_Invalid);
170 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
171 if (getLexer().isNot(AsmToken::EndOfStatement)) {
172 for (;;) {
173 StringRef Name;
174
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000175 if (getParser().parseIdentifier(Name))
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000176 return TokError("expected identifier in directive");
177
178 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
179
180 getStreamer().EmitSymbolAttribute(Sym, Attr);
181
182 if (getLexer().is(AsmToken::EndOfStatement))
183 break;
184
185 if (getLexer().isNot(AsmToken::Comma))
186 return TokError("unexpected token in directive");
187 Lex();
188 }
189 }
190
191 Lex();
192 return false;
193}
194
Daniel Dunbar5146a092010-07-12 21:23:32 +0000195bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
196 unsigned Flags, SectionKind Kind) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000197 const MCExpr *Subsection = 0;
198 if (getLexer().isNot(AsmToken::EndOfStatement)) {
199 if (getParser().parseExpression(Subsection))
200 return true;
201 }
Daniel Dunbar5146a092010-07-12 21:23:32 +0000202
203 getStreamer().SwitchSection(getContext().getELFSection(
Peter Collingbournedf39be62013-04-17 21:18:16 +0000204 Section, Type, Flags, Kind),
205 Subsection);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000206
207 return false;
208}
209
Eli Friedman21444ef2010-07-17 04:29:04 +0000210bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000211 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000212 if (getParser().parseIdentifier(Name))
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000213 return TokError("expected identifier in directive");
Dmitri Gribenko2de05722012-09-10 21:26:47 +0000214 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000215
216 if (getLexer().isNot(AsmToken::Comma))
217 return TokError("unexpected token in directive");
218 Lex();
219
220 const MCExpr *Expr;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000221 if (getParser().parseExpression(Expr))
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000222 return true;
223
224 if (getLexer().isNot(AsmToken::EndOfStatement))
225 return TokError("unexpected token in directive");
226
227 getStreamer().EmitELFSize(Sym, Expr);
228 return false;
229}
230
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000231bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
232 // A section name can contain -, so we cannot just use
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000233 // parseIdentifier.
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000234 SMLoc FirstLoc = getLexer().getLoc();
235 unsigned Size = 0;
236
Rafael Espindola184640e2011-01-24 18:02:54 +0000237 if (getLexer().is(AsmToken::String)) {
238 SectionName = getTok().getIdentifier();
239 Lex();
240 return false;
241 }
242
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000243 for (;;) {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000244 unsigned CurSize;
245
246 SMLoc PrevLoc = getLexer().getLoc();
247 if (getLexer().is(AsmToken::Minus)) {
248 CurSize = 1;
249 Lex(); // Consume the "-".
Rafael Espindola184640e2011-01-24 18:02:54 +0000250 } else if (getLexer().is(AsmToken::String)) {
251 CurSize = getTok().getIdentifier().size() + 2;
252 Lex();
253 } else if (getLexer().is(AsmToken::Identifier)) {
254 CurSize = getTok().getIdentifier().size();
255 Lex();
256 } else {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000257 break;
Rafael Espindola184640e2011-01-24 18:02:54 +0000258 }
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000259
260 Size += CurSize;
261 SectionName = StringRef(FirstLoc.getPointer(), Size);
262
263 // Make sure the following token is adjacent.
264 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
265 break;
266 }
267 if (Size == 0)
268 return true;
269
270 return false;
271}
272
Rafael Espindola25958732010-11-24 21:57:39 +0000273static SectionKind computeSectionKind(unsigned Flags) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000274 if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola25958732010-11-24 21:57:39 +0000275 return SectionKind::getText();
Rafael Espindola1c130262011-01-23 04:43:11 +0000276 if (Flags & ELF::SHF_TLS)
Rafael Espindola25958732010-11-24 21:57:39 +0000277 return SectionKind::getThreadData();
278 return SectionKind::getDataRel();
279}
280
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000281static int parseSectionFlags(StringRef flagsStr) {
282 int flags = 0;
283
284 for (unsigned i = 0; i < flagsStr.size(); i++) {
285 switch (flagsStr[i]) {
286 case 'a':
Rafael Espindola1c130262011-01-23 04:43:11 +0000287 flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000288 break;
289 case 'x':
Rafael Espindola1c130262011-01-23 04:43:11 +0000290 flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000291 break;
292 case 'w':
Rafael Espindola1c130262011-01-23 04:43:11 +0000293 flags |= ELF::SHF_WRITE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000294 break;
295 case 'M':
Rafael Espindola1c130262011-01-23 04:43:11 +0000296 flags |= ELF::SHF_MERGE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000297 break;
298 case 'S':
Rafael Espindola1c130262011-01-23 04:43:11 +0000299 flags |= ELF::SHF_STRINGS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000300 break;
301 case 'T':
Rafael Espindola1c130262011-01-23 04:43:11 +0000302 flags |= ELF::SHF_TLS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000303 break;
304 case 'c':
Rafael Espindola1c130262011-01-23 04:43:11 +0000305 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000306 break;
307 case 'd':
Rafael Espindola1c130262011-01-23 04:43:11 +0000308 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000309 break;
310 case 'G':
Rafael Espindola1c130262011-01-23 04:43:11 +0000311 flags |= ELF::SHF_GROUP;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000312 break;
313 default:
314 return -1;
315 }
316 }
317
318 return flags;
319}
320
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000321bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
322 getStreamer().PushSection();
323
Peter Collingbournedf39be62013-04-17 21:18:16 +0000324 if (ParseSectionArguments(/*IsPush=*/true)) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000325 getStreamer().PopSection();
326 return true;
327 }
328
329 return false;
330}
331
332bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
333 if (!getStreamer().PopSection())
334 return TokError(".popsection without corresponding .pushsection");
335 return false;
336}
337
Eli Friedman21444ef2010-07-17 04:29:04 +0000338// FIXME: This is a work in progress.
339bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000340 return ParseSectionArguments(/*IsPush=*/false);
341}
342
343bool ELFAsmParser::ParseSectionArguments(bool IsPush) {
Eli Friedman21444ef2010-07-17 04:29:04 +0000344 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000345
346 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000347 return TokError("expected identifier in directive");
348
Eli Friedman21444ef2010-07-17 04:29:04 +0000349 StringRef TypeName;
350 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000351 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000352 unsigned Flags = 0;
Peter Collingbournedf39be62013-04-17 21:18:16 +0000353 const MCExpr *Subsection = 0;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000354
355 // Set the defaults first.
356 if (SectionName == ".fini" || SectionName == ".init" ||
357 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000358 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000359 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000360 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000361
Eli Friedman21444ef2010-07-17 04:29:04 +0000362 if (getLexer().is(AsmToken::Comma)) {
363 Lex();
364
Peter Collingbournedf39be62013-04-17 21:18:16 +0000365 if (IsPush && getLexer().isNot(AsmToken::String)) {
366 if (getParser().parseExpression(Subsection))
367 return true;
368 if (getLexer().isNot(AsmToken::Comma))
369 goto EndStmt;
370 Lex();
371 }
372
Eli Friedman21444ef2010-07-17 04:29:04 +0000373 if (getLexer().isNot(AsmToken::String))
374 return TokError("expected string in directive");
375
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000376 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000377 Lex();
378
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000379 int extraFlags = parseSectionFlags(FlagsStr);
380 if (extraFlags < 0)
381 return TokError("unknown flag");
382 Flags |= extraFlags;
383
Rafael Espindola1c130262011-01-23 04:43:11 +0000384 bool Mergeable = Flags & ELF::SHF_MERGE;
385 bool Group = Flags & ELF::SHF_GROUP;
Eli Friedman21444ef2010-07-17 04:29:04 +0000386
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000387 if (getLexer().isNot(AsmToken::Comma)) {
388 if (Mergeable)
389 return TokError("Mergeable section must specify the type");
390 if (Group)
391 return TokError("Group section must specify the type");
392 } else {
393 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000394 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
395 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000396
397 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000398 if (getParser().parseIdentifier(TypeName))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000399 return TokError("expected identifier in directive");
400
401 if (Mergeable) {
402 if (getLexer().isNot(AsmToken::Comma))
403 return TokError("expected the entry size");
404 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000405 if (getParser().parseAbsoluteExpression(Size))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000406 return true;
407 if (Size <= 0)
408 return TokError("entry size must be positive");
409 }
410
411 if (Group) {
412 if (getLexer().isNot(AsmToken::Comma))
413 return TokError("expected group name");
414 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000415 if (getParser().parseIdentifier(GroupName))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000416 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000417 if (getLexer().is(AsmToken::Comma)) {
418 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000419 StringRef Linkage;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000420 if (getParser().parseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000421 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000422 if (Linkage != "comdat")
423 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000424 }
425 }
426 }
427 }
428
Peter Collingbournedf39be62013-04-17 21:18:16 +0000429EndStmt:
Eli Friedman21444ef2010-07-17 04:29:04 +0000430 if (getLexer().isNot(AsmToken::EndOfStatement))
431 return TokError("unexpected token in directive");
432
Rafael Espindolac85dca62011-01-23 04:28:49 +0000433 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000434
Joerg Sonnenberger42edeb12013-02-16 00:32:53 +0000435 if (TypeName.empty()) {
436 if (SectionName.startswith(".note"))
437 Type = ELF::SHT_NOTE;
438 else if (SectionName == ".init_array")
439 Type = ELF::SHT_INIT_ARRAY;
440 else if (SectionName == ".fini_array")
441 Type = ELF::SHT_FINI_ARRAY;
442 else if (SectionName == ".preinit_array")
443 Type = ELF::SHT_PREINIT_ARRAY;
444 } else {
Eli Friedman21444ef2010-07-17 04:29:04 +0000445 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000446 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000447 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000448 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000449 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000450 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000451 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000452 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000453 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000454 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000455 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000456 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000457 else if (TypeName == "unwind")
458 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000459 else
460 return TokError("unknown section type");
461 }
462
Rafael Espindola25958732010-11-24 21:57:39 +0000463 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000464 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000465 Flags, Kind, Size,
Peter Collingbournedf39be62013-04-17 21:18:16 +0000466 GroupName),
467 Subsection);
Eli Friedman21444ef2010-07-17 04:29:04 +0000468 return false;
469}
470
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000471bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000472 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
473 if (PreviousSection.first == NULL)
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000474 return TokError(".previous without corresponding .section");
Peter Collingbournedf39be62013-04-17 21:18:16 +0000475 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000476
477 return false;
478}
479
Michael J. Spencere90ea132010-10-09 03:47:55 +0000480/// ParseDirectiveELFType
481/// ::= .type identifier , @attribute
482bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
483 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000484 if (getParser().parseIdentifier(Name))
Michael J. Spencere90ea132010-10-09 03:47:55 +0000485 return TokError("expected identifier in directive");
486
487 // Handle the identifier as the key symbol.
488 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
489
490 if (getLexer().isNot(AsmToken::Comma))
491 return TokError("unexpected token in '.type' directive");
492 Lex();
493
Rafael Espindolad4a35262010-11-12 15:47:08 +0000494 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
495 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000496 Lex();
497
498 StringRef Type;
499 SMLoc TypeLoc;
500
501 TypeLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000502 if (getParser().parseIdentifier(Type))
Michael J. Spencere90ea132010-10-09 03:47:55 +0000503 return TokError("expected symbol type in directive");
504
505 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
506 .Case("function", MCSA_ELF_TypeFunction)
507 .Case("object", MCSA_ELF_TypeObject)
508 .Case("tls_object", MCSA_ELF_TypeTLS)
509 .Case("common", MCSA_ELF_TypeCommon)
510 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000511 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Roman Divackya0c17a42011-12-12 17:34:04 +0000512 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000513 .Default(MCSA_Invalid);
514
515 if (Attr == MCSA_Invalid)
516 return Error(TypeLoc, "unsupported attribute in '.type' directive");
517
518 if (getLexer().isNot(AsmToken::EndOfStatement))
519 return TokError("unexpected token in '.type' directive");
520
521 Lex();
522
523 getStreamer().EmitSymbolAttribute(Sym, Attr);
524
525 return false;
526}
527
Rafael Espindola61e3b912010-10-26 19:35:47 +0000528/// ParseDirectiveIdent
529/// ::= .ident string
530bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
531 if (getLexer().isNot(AsmToken::String))
532 return TokError("unexpected token in '.ident' directive");
533
534 StringRef Data = getTok().getIdentifier();
535
536 Lex();
537
Rafael Espindola61e3b912010-10-26 19:35:47 +0000538 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000539 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000540 ELF::SHF_MERGE |
541 ELF::SHF_STRINGS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000542 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000543 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000544
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000545 getStreamer().PushSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000546 getStreamer().SwitchSection(Comment);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000547 if (!SeenIdent) {
Rafael Espindola61e3b912010-10-26 19:35:47 +0000548 getStreamer().EmitIntValue(0, 1);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000549 SeenIdent = true;
550 }
Eric Christopher68ca5622013-01-09 01:57:54 +0000551 getStreamer().EmitBytes(Data);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000552 getStreamer().EmitIntValue(0, 1);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000553 getStreamer().PopSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000554 return false;
555}
556
Rafael Espindola88182132010-10-27 15:18:17 +0000557/// ParseDirectiveSymver
558/// ::= .symver foo, bar2@zed
559bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
560 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000561 if (getParser().parseIdentifier(Name))
Rafael Espindola88182132010-10-27 15:18:17 +0000562 return TokError("expected identifier in directive");
563
564 if (getLexer().isNot(AsmToken::Comma))
565 return TokError("expected a comma");
566
567 Lex();
568
569 StringRef AliasName;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000570 if (getParser().parseIdentifier(AliasName))
Rafael Espindola88182132010-10-27 15:18:17 +0000571 return TokError("expected identifier in directive");
572
573 if (AliasName.find('@') == StringRef::npos)
574 return TokError("expected a '@' in the name");
575
576 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
577 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
578 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
579
580 getStreamer().EmitAssignment(Alias, Value);
581 return false;
582}
583
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000584/// ParseDirectiveVersion
585/// ::= .version string
586bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
587 if (getLexer().isNot(AsmToken::String))
588 return TokError("unexpected token in '.version' directive");
589
590 StringRef Data = getTok().getIdentifier();
591
592 Lex();
593
594 const MCSection *Note =
595 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
596 SectionKind::getReadOnly());
597
598 getStreamer().PushSection();
599 getStreamer().SwitchSection(Note);
600 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
601 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
602 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christopher68ca5622013-01-09 01:57:54 +0000603 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000604 getStreamer().EmitIntValue(0, 1); // terminate the string.
605 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
606 getStreamer().PopSection();
607 return false;
608}
609
Rafael Espindola484291c2010-11-01 14:28:48 +0000610/// ParseDirectiveWeakref
611/// ::= .weakref foo, bar
612bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
613 // FIXME: Share code with the other alias building directives.
614
615 StringRef AliasName;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000616 if (getParser().parseIdentifier(AliasName))
Rafael Espindola484291c2010-11-01 14:28:48 +0000617 return TokError("expected identifier in directive");
618
619 if (getLexer().isNot(AsmToken::Comma))
620 return TokError("expected a comma");
621
622 Lex();
623
624 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000625 if (getParser().parseIdentifier(Name))
Rafael Espindola484291c2010-11-01 14:28:48 +0000626 return TokError("expected identifier in directive");
627
628 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
629
630 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
631
632 getStreamer().EmitWeakReference(Alias, Sym);
633 return false;
634}
635
Peter Collingbournedf39be62013-04-17 21:18:16 +0000636bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
637 const MCExpr *Subsection = 0;
638 if (getLexer().isNot(AsmToken::EndOfStatement)) {
639 if (getParser().parseExpression(Subsection))
640 return true;
641 }
642
643 if (getLexer().isNot(AsmToken::EndOfStatement))
644 return TokError("unexpected token in directive");
645
646 getStreamer().SubSection(Subsection);
647 return false;
648}
649
Daniel Dunbar5146a092010-07-12 21:23:32 +0000650namespace llvm {
651
652MCAsmParserExtension *createELFAsmParser() {
653 return new ELFAsmParser;
654}
655
656}