blob: 3134fc3d859743841ae21f114fb0af29e282fc8c [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 (;;) {
244 StringRef Tmp;
245 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
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000282static int parseSectionFlags(StringRef flagsStr) {
283 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;
314 default:
315 return -1;
316 }
317 }
318
319 return flags;
320}
321
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000322bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
323 getStreamer().PushSection();
324
Peter Collingbournedf39be62013-04-17 21:18:16 +0000325 if (ParseSectionArguments(/*IsPush=*/true)) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000326 getStreamer().PopSection();
327 return true;
328 }
329
330 return false;
331}
332
333bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
334 if (!getStreamer().PopSection())
335 return TokError(".popsection without corresponding .pushsection");
336 return false;
337}
338
Eli Friedman21444ef2010-07-17 04:29:04 +0000339// FIXME: This is a work in progress.
340bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000341 return ParseSectionArguments(/*IsPush=*/false);
342}
343
344bool ELFAsmParser::ParseSectionArguments(bool IsPush) {
Eli Friedman21444ef2010-07-17 04:29:04 +0000345 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000346
347 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000348 return TokError("expected identifier in directive");
349
Eli Friedman21444ef2010-07-17 04:29:04 +0000350 StringRef TypeName;
351 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000352 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000353 unsigned Flags = 0;
Peter Collingbournedf39be62013-04-17 21:18:16 +0000354 const MCExpr *Subsection = 0;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000355
356 // Set the defaults first.
357 if (SectionName == ".fini" || SectionName == ".init" ||
358 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000359 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000360 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000361 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000362
Eli Friedman21444ef2010-07-17 04:29:04 +0000363 if (getLexer().is(AsmToken::Comma)) {
364 Lex();
365
Peter Collingbournedf39be62013-04-17 21:18:16 +0000366 if (IsPush && getLexer().isNot(AsmToken::String)) {
367 if (getParser().parseExpression(Subsection))
368 return true;
369 if (getLexer().isNot(AsmToken::Comma))
370 goto EndStmt;
371 Lex();
372 }
373
Eli Friedman21444ef2010-07-17 04:29:04 +0000374 if (getLexer().isNot(AsmToken::String))
375 return TokError("expected string in directive");
376
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000377 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000378 Lex();
379
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000380 int extraFlags = parseSectionFlags(FlagsStr);
381 if (extraFlags < 0)
382 return TokError("unknown flag");
383 Flags |= extraFlags;
384
Rafael Espindola1c130262011-01-23 04:43:11 +0000385 bool Mergeable = Flags & ELF::SHF_MERGE;
386 bool Group = Flags & ELF::SHF_GROUP;
Eli Friedman21444ef2010-07-17 04:29:04 +0000387
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000388 if (getLexer().isNot(AsmToken::Comma)) {
389 if (Mergeable)
390 return TokError("Mergeable section must specify the type");
391 if (Group)
392 return TokError("Group section must specify the type");
393 } else {
394 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000395 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
396 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000397
398 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000399 if (getParser().parseIdentifier(TypeName))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000400 return TokError("expected identifier in directive");
401
402 if (Mergeable) {
403 if (getLexer().isNot(AsmToken::Comma))
404 return TokError("expected the entry size");
405 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000406 if (getParser().parseAbsoluteExpression(Size))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000407 return true;
408 if (Size <= 0)
409 return TokError("entry size must be positive");
410 }
411
412 if (Group) {
413 if (getLexer().isNot(AsmToken::Comma))
414 return TokError("expected group name");
415 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000416 if (getParser().parseIdentifier(GroupName))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000417 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000418 if (getLexer().is(AsmToken::Comma)) {
419 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000420 StringRef Linkage;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000421 if (getParser().parseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000422 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000423 if (Linkage != "comdat")
424 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000425 }
426 }
427 }
428 }
429
Peter Collingbournedf39be62013-04-17 21:18:16 +0000430EndStmt:
Eli Friedman21444ef2010-07-17 04:29:04 +0000431 if (getLexer().isNot(AsmToken::EndOfStatement))
432 return TokError("unexpected token in directive");
433
Rafael Espindolac85dca62011-01-23 04:28:49 +0000434 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000435
Joerg Sonnenberger42edeb12013-02-16 00:32:53 +0000436 if (TypeName.empty()) {
437 if (SectionName.startswith(".note"))
438 Type = ELF::SHT_NOTE;
439 else if (SectionName == ".init_array")
440 Type = ELF::SHT_INIT_ARRAY;
441 else if (SectionName == ".fini_array")
442 Type = ELF::SHT_FINI_ARRAY;
443 else if (SectionName == ".preinit_array")
444 Type = ELF::SHT_PREINIT_ARRAY;
445 } else {
Eli Friedman21444ef2010-07-17 04:29:04 +0000446 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000447 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000448 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000449 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000450 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000451 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000452 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000453 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000454 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000455 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000456 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000457 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000458 else if (TypeName == "unwind")
459 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000460 else
461 return TokError("unknown section type");
462 }
463
Rafael Espindola25958732010-11-24 21:57:39 +0000464 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000465 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000466 Flags, Kind, Size,
Peter Collingbournedf39be62013-04-17 21:18:16 +0000467 GroupName),
468 Subsection);
Eli Friedman21444ef2010-07-17 04:29:04 +0000469 return false;
470}
471
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000472bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000473 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
474 if (PreviousSection.first == NULL)
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000475 return TokError(".previous without corresponding .section");
Peter Collingbournedf39be62013-04-17 21:18:16 +0000476 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000477
478 return false;
479}
480
Michael J. Spencere90ea132010-10-09 03:47:55 +0000481/// ParseDirectiveELFType
482/// ::= .type identifier , @attribute
483bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
484 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000485 if (getParser().parseIdentifier(Name))
Michael J. Spencere90ea132010-10-09 03:47:55 +0000486 return TokError("expected identifier in directive");
487
488 // Handle the identifier as the key symbol.
489 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
490
491 if (getLexer().isNot(AsmToken::Comma))
492 return TokError("unexpected token in '.type' directive");
493 Lex();
494
Rafael Espindolad4a35262010-11-12 15:47:08 +0000495 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
496 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000497 Lex();
498
499 StringRef Type;
500 SMLoc TypeLoc;
501
502 TypeLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000503 if (getParser().parseIdentifier(Type))
Michael J. Spencere90ea132010-10-09 03:47:55 +0000504 return TokError("expected symbol type in directive");
505
506 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
507 .Case("function", MCSA_ELF_TypeFunction)
508 .Case("object", MCSA_ELF_TypeObject)
509 .Case("tls_object", MCSA_ELF_TypeTLS)
510 .Case("common", MCSA_ELF_TypeCommon)
511 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000512 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Roman Divackya0c17a42011-12-12 17:34:04 +0000513 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000514 .Default(MCSA_Invalid);
515
516 if (Attr == MCSA_Invalid)
517 return Error(TypeLoc, "unsupported attribute in '.type' directive");
518
519 if (getLexer().isNot(AsmToken::EndOfStatement))
520 return TokError("unexpected token in '.type' directive");
521
522 Lex();
523
524 getStreamer().EmitSymbolAttribute(Sym, Attr);
525
526 return false;
527}
528
Rafael Espindola61e3b912010-10-26 19:35:47 +0000529/// ParseDirectiveIdent
530/// ::= .ident string
531bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
532 if (getLexer().isNot(AsmToken::String))
533 return TokError("unexpected token in '.ident' directive");
534
535 StringRef Data = getTok().getIdentifier();
536
537 Lex();
538
Rafael Espindola61e3b912010-10-26 19:35:47 +0000539 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000540 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000541 ELF::SHF_MERGE |
542 ELF::SHF_STRINGS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000543 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000544 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000545
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000546 getStreamer().PushSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000547 getStreamer().SwitchSection(Comment);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000548 if (!SeenIdent) {
Rafael Espindola61e3b912010-10-26 19:35:47 +0000549 getStreamer().EmitIntValue(0, 1);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000550 SeenIdent = true;
551 }
Eric Christopher68ca5622013-01-09 01:57:54 +0000552 getStreamer().EmitBytes(Data);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000553 getStreamer().EmitIntValue(0, 1);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000554 getStreamer().PopSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000555 return false;
556}
557
Rafael Espindola88182132010-10-27 15:18:17 +0000558/// ParseDirectiveSymver
559/// ::= .symver foo, bar2@zed
560bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
561 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000562 if (getParser().parseIdentifier(Name))
Rafael Espindola88182132010-10-27 15:18:17 +0000563 return TokError("expected identifier in directive");
564
565 if (getLexer().isNot(AsmToken::Comma))
566 return TokError("expected a comma");
567
568 Lex();
569
570 StringRef AliasName;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000571 if (getParser().parseIdentifier(AliasName))
Rafael Espindola88182132010-10-27 15:18:17 +0000572 return TokError("expected identifier in directive");
573
574 if (AliasName.find('@') == StringRef::npos)
575 return TokError("expected a '@' in the name");
576
577 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
578 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
579 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
580
581 getStreamer().EmitAssignment(Alias, Value);
582 return false;
583}
584
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000585/// ParseDirectiveVersion
586/// ::= .version string
587bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
588 if (getLexer().isNot(AsmToken::String))
589 return TokError("unexpected token in '.version' directive");
590
591 StringRef Data = getTok().getIdentifier();
592
593 Lex();
594
595 const MCSection *Note =
596 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
597 SectionKind::getReadOnly());
598
599 getStreamer().PushSection();
600 getStreamer().SwitchSection(Note);
601 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
602 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
603 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christopher68ca5622013-01-09 01:57:54 +0000604 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000605 getStreamer().EmitIntValue(0, 1); // terminate the string.
606 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
607 getStreamer().PopSection();
608 return false;
609}
610
Rafael Espindola484291c2010-11-01 14:28:48 +0000611/// ParseDirectiveWeakref
612/// ::= .weakref foo, bar
613bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
614 // FIXME: Share code with the other alias building directives.
615
616 StringRef AliasName;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000617 if (getParser().parseIdentifier(AliasName))
Rafael Espindola484291c2010-11-01 14:28:48 +0000618 return TokError("expected identifier in directive");
619
620 if (getLexer().isNot(AsmToken::Comma))
621 return TokError("expected a comma");
622
623 Lex();
624
625 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000626 if (getParser().parseIdentifier(Name))
Rafael Espindola484291c2010-11-01 14:28:48 +0000627 return TokError("expected identifier in directive");
628
629 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
630
631 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
632
633 getStreamer().EmitWeakReference(Alias, Sym);
634 return false;
635}
636
Peter Collingbournedf39be62013-04-17 21:18:16 +0000637bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
638 const MCExpr *Subsection = 0;
639 if (getLexer().isNot(AsmToken::EndOfStatement)) {
640 if (getParser().parseExpression(Subsection))
641 return true;
642 }
643
644 if (getLexer().isNot(AsmToken::EndOfStatement))
645 return TokError("unexpected token in directive");
646
647 getStreamer().SubSection(Subsection);
648 return false;
649}
650
Daniel Dunbar5146a092010-07-12 21:23:32 +0000651namespace llvm {
652
653MCAsmParserExtension *createELFAsmParser() {
654 return new ELFAsmParser;
655}
656
657}