blob: 4c45e087445d74a5712bcc3f61b2f8e2aa96aaca [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");
Daniel Dunbar5146a092010-07-12 21:23:32 +000079 }
80
Rafael Espindolad80781b2010-09-15 21:48:40 +000081 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
82 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000083 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000084 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000085 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Daniel Dunbar5146a092010-07-12 21:23:32 +000086 SectionKind::getDataRel());
87 }
88 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000089 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000090 ELF::SHF_EXECINSTR |
91 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbar5146a092010-07-12 21:23:32 +000092 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000093 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000094 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000095 ELF::SHF_WRITE |
96 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Flemingf525c2a2010-07-20 21:12:46 +000097 }
98 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000099 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000100 ELF::SHF_ALLOC,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000101 SectionKind::getReadOnly());
102 }
103 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000104 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000105 ELF::SHF_ALLOC |
106 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000107 SectionKind::getThreadData());
108 }
109 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000110 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000111 ELF::SHF_ALLOC |
112 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000113 SectionKind::getThreadBSS());
114 }
115 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000116 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000117 ELF::SHF_ALLOC |
118 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000119 SectionKind::getDataRel());
120 }
121 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000122 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000123 ELF::SHF_ALLOC |
124 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000125 SectionKind::getReadOnlyWithRel());
126 }
127 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000128 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000129 ELF::SHF_ALLOC |
130 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000131 SectionKind::getReadOnlyWithRelLocal());
132 }
133 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000134 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000135 ELF::SHF_ALLOC |
136 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000137 SectionKind::getDataRel());
138 }
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000139 bool ParseDirectivePushSection(StringRef, SMLoc);
140 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman21444ef2010-07-17 04:29:04 +0000141 bool ParseDirectiveSection(StringRef, SMLoc);
142 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000143 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000144 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000145 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000146 bool ParseDirectiveSymver(StringRef, SMLoc);
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000147 bool ParseDirectiveVersion(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000148 bool ParseDirectiveWeakref(StringRef, SMLoc);
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000149 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000150
151private:
152 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000153};
154
155}
156
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000157/// ParseDirectiveSymbolAttribute
158/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
159bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
160 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
161 .Case(".weak", MCSA_Weak)
162 .Case(".local", MCSA_Local)
163 .Case(".hidden", MCSA_Hidden)
164 .Case(".internal", MCSA_Internal)
165 .Case(".protected", MCSA_Protected)
166 .Default(MCSA_Invalid);
167 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
168 if (getLexer().isNot(AsmToken::EndOfStatement)) {
169 for (;;) {
170 StringRef Name;
171
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000172 if (getParser().parseIdentifier(Name))
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000173 return TokError("expected identifier in directive");
174
175 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
176
177 getStreamer().EmitSymbolAttribute(Sym, Attr);
178
179 if (getLexer().is(AsmToken::EndOfStatement))
180 break;
181
182 if (getLexer().isNot(AsmToken::Comma))
183 return TokError("unexpected token in directive");
184 Lex();
185 }
186 }
187
188 Lex();
189 return false;
190}
191
Daniel Dunbar5146a092010-07-12 21:23:32 +0000192bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
193 unsigned Flags, SectionKind Kind) {
194 if (getLexer().isNot(AsmToken::EndOfStatement))
195 return TokError("unexpected token in section switching directive");
196 Lex();
197
198 getStreamer().SwitchSection(getContext().getELFSection(
199 Section, Type, Flags, Kind));
200
201 return false;
202}
203
Eli Friedman21444ef2010-07-17 04:29:04 +0000204bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000205 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000206 if (getParser().parseIdentifier(Name))
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000207 return TokError("expected identifier in directive");
Dmitri Gribenko2de05722012-09-10 21:26:47 +0000208 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000209
210 if (getLexer().isNot(AsmToken::Comma))
211 return TokError("unexpected token in directive");
212 Lex();
213
214 const MCExpr *Expr;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000215 if (getParser().parseExpression(Expr))
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000216 return true;
217
218 if (getLexer().isNot(AsmToken::EndOfStatement))
219 return TokError("unexpected token in directive");
220
221 getStreamer().EmitELFSize(Sym, Expr);
222 return false;
223}
224
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000225bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
226 // A section name can contain -, so we cannot just use
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000227 // parseIdentifier.
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000228 SMLoc FirstLoc = getLexer().getLoc();
229 unsigned Size = 0;
230
Rafael Espindola184640e2011-01-24 18:02:54 +0000231 if (getLexer().is(AsmToken::String)) {
232 SectionName = getTok().getIdentifier();
233 Lex();
234 return false;
235 }
236
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000237 for (;;) {
238 StringRef Tmp;
239 unsigned CurSize;
240
241 SMLoc PrevLoc = getLexer().getLoc();
242 if (getLexer().is(AsmToken::Minus)) {
243 CurSize = 1;
244 Lex(); // Consume the "-".
Rafael Espindola184640e2011-01-24 18:02:54 +0000245 } else if (getLexer().is(AsmToken::String)) {
246 CurSize = getTok().getIdentifier().size() + 2;
247 Lex();
248 } else if (getLexer().is(AsmToken::Identifier)) {
249 CurSize = getTok().getIdentifier().size();
250 Lex();
251 } else {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000252 break;
Rafael Espindola184640e2011-01-24 18:02:54 +0000253 }
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000254
255 Size += CurSize;
256 SectionName = StringRef(FirstLoc.getPointer(), Size);
257
258 // Make sure the following token is adjacent.
259 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
260 break;
261 }
262 if (Size == 0)
263 return true;
264
265 return false;
266}
267
Rafael Espindola25958732010-11-24 21:57:39 +0000268static SectionKind computeSectionKind(unsigned Flags) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000269 if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola25958732010-11-24 21:57:39 +0000270 return SectionKind::getText();
Rafael Espindola1c130262011-01-23 04:43:11 +0000271 if (Flags & ELF::SHF_TLS)
Rafael Espindola25958732010-11-24 21:57:39 +0000272 return SectionKind::getThreadData();
273 return SectionKind::getDataRel();
274}
275
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000276static int parseSectionFlags(StringRef flagsStr) {
277 int flags = 0;
278
279 for (unsigned i = 0; i < flagsStr.size(); i++) {
280 switch (flagsStr[i]) {
281 case 'a':
Rafael Espindola1c130262011-01-23 04:43:11 +0000282 flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000283 break;
284 case 'x':
Rafael Espindola1c130262011-01-23 04:43:11 +0000285 flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000286 break;
287 case 'w':
Rafael Espindola1c130262011-01-23 04:43:11 +0000288 flags |= ELF::SHF_WRITE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000289 break;
290 case 'M':
Rafael Espindola1c130262011-01-23 04:43:11 +0000291 flags |= ELF::SHF_MERGE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000292 break;
293 case 'S':
Rafael Espindola1c130262011-01-23 04:43:11 +0000294 flags |= ELF::SHF_STRINGS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000295 break;
296 case 'T':
Rafael Espindola1c130262011-01-23 04:43:11 +0000297 flags |= ELF::SHF_TLS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000298 break;
299 case 'c':
Rafael Espindola1c130262011-01-23 04:43:11 +0000300 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000301 break;
302 case 'd':
Rafael Espindola1c130262011-01-23 04:43:11 +0000303 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000304 break;
305 case 'G':
Rafael Espindola1c130262011-01-23 04:43:11 +0000306 flags |= ELF::SHF_GROUP;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000307 break;
308 default:
309 return -1;
310 }
311 }
312
313 return flags;
314}
315
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000316bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
317 getStreamer().PushSection();
318
319 if (ParseDirectiveSection(s, loc)) {
320 getStreamer().PopSection();
321 return true;
322 }
323
324 return false;
325}
326
327bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
328 if (!getStreamer().PopSection())
329 return TokError(".popsection without corresponding .pushsection");
330 return false;
331}
332
Eli Friedman21444ef2010-07-17 04:29:04 +0000333// FIXME: This is a work in progress.
334bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
335 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000336
337 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000338 return TokError("expected identifier in directive");
339
Eli Friedman21444ef2010-07-17 04:29:04 +0000340 StringRef TypeName;
341 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000342 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000343 unsigned Flags = 0;
344
345 // Set the defaults first.
346 if (SectionName == ".fini" || SectionName == ".init" ||
347 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000348 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000349 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000350 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000351
Eli Friedman21444ef2010-07-17 04:29:04 +0000352 if (getLexer().is(AsmToken::Comma)) {
353 Lex();
354
355 if (getLexer().isNot(AsmToken::String))
356 return TokError("expected string in directive");
357
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000358 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000359 Lex();
360
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000361 int extraFlags = parseSectionFlags(FlagsStr);
362 if (extraFlags < 0)
363 return TokError("unknown flag");
364 Flags |= extraFlags;
365
Rafael Espindola1c130262011-01-23 04:43:11 +0000366 bool Mergeable = Flags & ELF::SHF_MERGE;
367 bool Group = Flags & ELF::SHF_GROUP;
Eli Friedman21444ef2010-07-17 04:29:04 +0000368
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000369 if (getLexer().isNot(AsmToken::Comma)) {
370 if (Mergeable)
371 return TokError("Mergeable section must specify the type");
372 if (Group)
373 return TokError("Group section must specify the type");
374 } else {
375 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000376 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
377 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000378
379 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000380 if (getParser().parseIdentifier(TypeName))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000381 return TokError("expected identifier in directive");
382
383 if (Mergeable) {
384 if (getLexer().isNot(AsmToken::Comma))
385 return TokError("expected the entry size");
386 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000387 if (getParser().parseAbsoluteExpression(Size))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000388 return true;
389 if (Size <= 0)
390 return TokError("entry size must be positive");
391 }
392
393 if (Group) {
394 if (getLexer().isNot(AsmToken::Comma))
395 return TokError("expected group name");
396 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000397 if (getParser().parseIdentifier(GroupName))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000398 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000399 if (getLexer().is(AsmToken::Comma)) {
400 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000401 StringRef Linkage;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000402 if (getParser().parseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000403 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000404 if (Linkage != "comdat")
405 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000406 }
407 }
408 }
409 }
410
411 if (getLexer().isNot(AsmToken::EndOfStatement))
412 return TokError("unexpected token in directive");
413
Rafael Espindolac85dca62011-01-23 04:28:49 +0000414 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000415
Joerg Sonnenberger42edeb12013-02-16 00:32:53 +0000416 if (TypeName.empty()) {
417 if (SectionName.startswith(".note"))
418 Type = ELF::SHT_NOTE;
419 else if (SectionName == ".init_array")
420 Type = ELF::SHT_INIT_ARRAY;
421 else if (SectionName == ".fini_array")
422 Type = ELF::SHT_FINI_ARRAY;
423 else if (SectionName == ".preinit_array")
424 Type = ELF::SHT_PREINIT_ARRAY;
425 } else {
Eli Friedman21444ef2010-07-17 04:29:04 +0000426 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000427 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000428 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000429 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000430 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000431 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000432 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000433 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000434 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000435 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000436 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000437 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000438 else if (TypeName == "unwind")
439 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000440 else
441 return TokError("unknown section type");
442 }
443
Rafael Espindola25958732010-11-24 21:57:39 +0000444 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000445 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000446 Flags, Kind, Size,
447 GroupName));
Eli Friedman21444ef2010-07-17 04:29:04 +0000448 return false;
449}
450
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000451bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
452 const MCSection *PreviousSection = getStreamer().getPreviousSection();
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000453 if (PreviousSection == NULL)
454 return TokError(".previous without corresponding .section");
455 getStreamer().SwitchSection(PreviousSection);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000456
457 return false;
458}
459
Michael J. Spencere90ea132010-10-09 03:47:55 +0000460/// ParseDirectiveELFType
461/// ::= .type identifier , @attribute
462bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
463 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000464 if (getParser().parseIdentifier(Name))
Michael J. Spencere90ea132010-10-09 03:47:55 +0000465 return TokError("expected identifier in directive");
466
467 // Handle the identifier as the key symbol.
468 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
469
470 if (getLexer().isNot(AsmToken::Comma))
471 return TokError("unexpected token in '.type' directive");
472 Lex();
473
Rafael Espindolad4a35262010-11-12 15:47:08 +0000474 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
475 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000476 Lex();
477
478 StringRef Type;
479 SMLoc TypeLoc;
480
481 TypeLoc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000482 if (getParser().parseIdentifier(Type))
Michael J. Spencere90ea132010-10-09 03:47:55 +0000483 return TokError("expected symbol type in directive");
484
485 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
486 .Case("function", MCSA_ELF_TypeFunction)
487 .Case("object", MCSA_ELF_TypeObject)
488 .Case("tls_object", MCSA_ELF_TypeTLS)
489 .Case("common", MCSA_ELF_TypeCommon)
490 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000491 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Roman Divackya0c17a42011-12-12 17:34:04 +0000492 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000493 .Default(MCSA_Invalid);
494
495 if (Attr == MCSA_Invalid)
496 return Error(TypeLoc, "unsupported attribute in '.type' directive");
497
498 if (getLexer().isNot(AsmToken::EndOfStatement))
499 return TokError("unexpected token in '.type' directive");
500
501 Lex();
502
503 getStreamer().EmitSymbolAttribute(Sym, Attr);
504
505 return false;
506}
507
Rafael Espindola61e3b912010-10-26 19:35:47 +0000508/// ParseDirectiveIdent
509/// ::= .ident string
510bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
511 if (getLexer().isNot(AsmToken::String))
512 return TokError("unexpected token in '.ident' directive");
513
514 StringRef Data = getTok().getIdentifier();
515
516 Lex();
517
Rafael Espindola61e3b912010-10-26 19:35:47 +0000518 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000519 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000520 ELF::SHF_MERGE |
521 ELF::SHF_STRINGS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000522 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000523 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000524
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000525 getStreamer().PushSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000526 getStreamer().SwitchSection(Comment);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000527 if (!SeenIdent) {
Rafael Espindola61e3b912010-10-26 19:35:47 +0000528 getStreamer().EmitIntValue(0, 1);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000529 SeenIdent = true;
530 }
Eric Christopher68ca5622013-01-09 01:57:54 +0000531 getStreamer().EmitBytes(Data);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000532 getStreamer().EmitIntValue(0, 1);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000533 getStreamer().PopSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000534 return false;
535}
536
Rafael Espindola88182132010-10-27 15:18:17 +0000537/// ParseDirectiveSymver
538/// ::= .symver foo, bar2@zed
539bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
540 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000541 if (getParser().parseIdentifier(Name))
Rafael Espindola88182132010-10-27 15:18:17 +0000542 return TokError("expected identifier in directive");
543
544 if (getLexer().isNot(AsmToken::Comma))
545 return TokError("expected a comma");
546
547 Lex();
548
549 StringRef AliasName;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000550 if (getParser().parseIdentifier(AliasName))
Rafael Espindola88182132010-10-27 15:18:17 +0000551 return TokError("expected identifier in directive");
552
553 if (AliasName.find('@') == StringRef::npos)
554 return TokError("expected a '@' in the name");
555
556 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
557 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
558 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
559
560 getStreamer().EmitAssignment(Alias, Value);
561 return false;
562}
563
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000564/// ParseDirectiveVersion
565/// ::= .version string
566bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
567 if (getLexer().isNot(AsmToken::String))
568 return TokError("unexpected token in '.version' directive");
569
570 StringRef Data = getTok().getIdentifier();
571
572 Lex();
573
574 const MCSection *Note =
575 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
576 SectionKind::getReadOnly());
577
578 getStreamer().PushSection();
579 getStreamer().SwitchSection(Note);
580 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
581 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
582 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christopher68ca5622013-01-09 01:57:54 +0000583 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000584 getStreamer().EmitIntValue(0, 1); // terminate the string.
585 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
586 getStreamer().PopSection();
587 return false;
588}
589
Rafael Espindola484291c2010-11-01 14:28:48 +0000590/// ParseDirectiveWeakref
591/// ::= .weakref foo, bar
592bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
593 // FIXME: Share code with the other alias building directives.
594
595 StringRef AliasName;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000596 if (getParser().parseIdentifier(AliasName))
Rafael Espindola484291c2010-11-01 14:28:48 +0000597 return TokError("expected identifier in directive");
598
599 if (getLexer().isNot(AsmToken::Comma))
600 return TokError("expected a comma");
601
602 Lex();
603
604 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000605 if (getParser().parseIdentifier(Name))
Rafael Espindola484291c2010-11-01 14:28:48 +0000606 return TokError("expected identifier in directive");
607
608 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
609
610 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
611
612 getStreamer().EmitWeakReference(Alias, Sym);
613 return false;
614}
615
Daniel Dunbar5146a092010-07-12 21:23:32 +0000616namespace llvm {
617
618MCAsmParserExtension *createELFAsmParser() {
619 return new ELFAsmParser;
620}
621
622}