blob: 8807975e85457d9a3cf90cde140a7272c101d9f9 [file] [log] [blame]
Daniel Dunbar5146a092010-07-12 21:23:32 +00001//===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCParser/MCAsmParserExtension.h"
Michael J. Spencere90ea132010-10-09 03:47:55 +000011#include "llvm/ADT/StringSwitch.h"
Eli Friedmandc1ad222010-07-17 06:27:28 +000012#include "llvm/ADT/Twine.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000013#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000014#include "llvm/MC/MCContext.h"
Rafael Espindola88182132010-10-27 15:18:17 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000016#include "llvm/MC/MCParser/MCAsmLexer.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000017#include "llvm/MC/MCSectionELF.h"
18#include "llvm/MC/MCStreamer.h"
David Majnemerbcd9b3b2013-09-15 19:24:16 +000019#include "llvm/MC/MCSymbol.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000020#include "llvm/Support/ELF.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000021using namespace llvm;
22
23namespace {
24
25class ELFAsmParser : public MCAsmParserExtension {
Eli Bendersky171192f2013-01-16 00:50:52 +000026 template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000027 void addDirectiveHandler(StringRef Directive) {
Eli Bendersky171192f2013-01-16 00:50:52 +000028 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
29 this, HandleDirective<ELFAsmParser, HandlerMethod>);
30
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000031 getParser().addDirectiveHandler(Directive, Handler);
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000032 }
33
Rafael Espindolac7ce3e42013-10-16 01:05:45 +000034 bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
35 SectionKind Kind);
Daniel Dunbar5146a092010-07-12 21:23:32 +000036
37public:
Rafael Espindolac7ce3e42013-10-16 01:05:45 +000038 ELFAsmParser() { BracketExpressionsSupported = true; }
Daniel Dunbar5146a092010-07-12 21:23:32 +000039
40 virtual void Initialize(MCAsmParser &Parser) {
41 // Call the base implementation.
42 this->MCAsmParserExtension::Initialize(Parser);
43
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000044 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
45 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
46 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
47 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
48 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
49 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
50 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000051 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000052 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000053 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000054 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000055 &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000056 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000057 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000058 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
59 addDirectiveHandler<
Jim Grosbachcc866d52011-07-25 17:11:29 +000060 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000061 addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
62 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
63 addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
64 addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
65 addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
66 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
67 addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
68 addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
69 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
70 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
71 addDirectiveHandler<
Jim Grosbachf2a35fb2011-07-25 17:55:35 +000072 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000073 addDirectiveHandler<
Jim Grosbachf2a35fb2011-07-25 17:55:35 +000074 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +000075 addDirectiveHandler<
Jim Grosbachf2a35fb2011-07-25 17:55:35 +000076 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
Peter Collingbournedf39be62013-04-17 21:18:16 +000077 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
Daniel Dunbar5146a092010-07-12 21:23:32 +000078 }
79
Rafael Espindolad80781b2010-09-15 21:48:40 +000080 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
81 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000082 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000083 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000084 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Daniel Dunbar5146a092010-07-12 21:23:32 +000085 SectionKind::getDataRel());
86 }
87 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000088 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000089 ELF::SHF_EXECINSTR |
90 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbar5146a092010-07-12 21:23:32 +000091 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000092 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000093 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000094 ELF::SHF_WRITE |
95 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Flemingf525c2a2010-07-20 21:12:46 +000096 }
97 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000098 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000099 ELF::SHF_ALLOC,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000100 SectionKind::getReadOnly());
101 }
102 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000103 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000104 ELF::SHF_ALLOC |
105 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000106 SectionKind::getThreadData());
107 }
108 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000109 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000110 ELF::SHF_ALLOC |
111 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000112 SectionKind::getThreadBSS());
113 }
114 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000115 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000116 ELF::SHF_ALLOC |
117 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000118 SectionKind::getDataRel());
119 }
120 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000121 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000122 ELF::SHF_ALLOC |
123 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000124 SectionKind::getReadOnlyWithRel());
125 }
126 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000127 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000128 ELF::SHF_ALLOC |
129 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000130 SectionKind::getReadOnlyWithRelLocal());
131 }
132 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000133 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000134 ELF::SHF_ALLOC |
135 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000136 SectionKind::getDataRel());
137 }
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000138 bool ParseDirectivePushSection(StringRef, SMLoc);
139 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman21444ef2010-07-17 04:29:04 +0000140 bool ParseDirectiveSection(StringRef, SMLoc);
141 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000142 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000143 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000144 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000145 bool ParseDirectiveSymver(StringRef, SMLoc);
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000146 bool ParseDirectiveVersion(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000147 bool ParseDirectiveWeakref(StringRef, SMLoc);
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000148 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
Peter Collingbournedf39be62013-04-17 21:18:16 +0000149 bool ParseDirectiveSubsection(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000150
151private:
152 bool ParseSectionName(StringRef &SectionName);
Peter Collingbournedf39be62013-04-17 21:18:16 +0000153 bool ParseSectionArguments(bool IsPush);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000154};
155
156}
157
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000158/// ParseDirectiveSymbolAttribute
159/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
160bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
161 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
162 .Case(".weak", MCSA_Weak)
163 .Case(".local", MCSA_Local)
164 .Case(".hidden", MCSA_Hidden)
165 .Case(".internal", MCSA_Internal)
166 .Case(".protected", MCSA_Protected)
167 .Default(MCSA_Invalid);
168 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
169 if (getLexer().isNot(AsmToken::EndOfStatement)) {
170 for (;;) {
171 StringRef Name;
172
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000173 if (getParser().parseIdentifier(Name))
Jim Grosbachf2a35fb2011-07-25 17:55:35 +0000174 return TokError("expected identifier in directive");
175
176 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
177
178 getStreamer().EmitSymbolAttribute(Sym, Attr);
179
180 if (getLexer().is(AsmToken::EndOfStatement))
181 break;
182
183 if (getLexer().isNot(AsmToken::Comma))
184 return TokError("unexpected token in directive");
185 Lex();
186 }
187 }
188
189 Lex();
190 return false;
191}
192
Daniel Dunbar5146a092010-07-12 21:23:32 +0000193bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
194 unsigned Flags, SectionKind Kind) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000195 const MCExpr *Subsection = 0;
196 if (getLexer().isNot(AsmToken::EndOfStatement)) {
197 if (getParser().parseExpression(Subsection))
198 return true;
199 }
Daniel Dunbar5146a092010-07-12 21:23:32 +0000200
201 getStreamer().SwitchSection(getContext().getELFSection(
Peter Collingbournedf39be62013-04-17 21:18:16 +0000202 Section, Type, Flags, Kind),
203 Subsection);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000204
205 return false;
206}
207
Eli Friedman21444ef2010-07-17 04:29:04 +0000208bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000209 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000210 if (getParser().parseIdentifier(Name))
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000211 return TokError("expected identifier in directive");
Dmitri Gribenko2de05722012-09-10 21:26:47 +0000212 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000213
214 if (getLexer().isNot(AsmToken::Comma))
215 return TokError("unexpected token in directive");
216 Lex();
217
218 const MCExpr *Expr;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000219 if (getParser().parseExpression(Expr))
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000220 return true;
221
222 if (getLexer().isNot(AsmToken::EndOfStatement))
223 return TokError("unexpected token in directive");
224
225 getStreamer().EmitELFSize(Sym, Expr);
226 return false;
227}
228
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000229bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
230 // A section name can contain -, so we cannot just use
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000231 // parseIdentifier.
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000232 SMLoc FirstLoc = getLexer().getLoc();
233 unsigned Size = 0;
234
Rafael Espindola184640e2011-01-24 18:02:54 +0000235 if (getLexer().is(AsmToken::String)) {
236 SectionName = getTok().getIdentifier();
237 Lex();
238 return false;
239 }
240
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000241 for (;;) {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000242 unsigned CurSize;
243
244 SMLoc PrevLoc = getLexer().getLoc();
245 if (getLexer().is(AsmToken::Minus)) {
246 CurSize = 1;
247 Lex(); // Consume the "-".
Rafael Espindola184640e2011-01-24 18:02:54 +0000248 } else if (getLexer().is(AsmToken::String)) {
249 CurSize = getTok().getIdentifier().size() + 2;
250 Lex();
251 } else if (getLexer().is(AsmToken::Identifier)) {
252 CurSize = getTok().getIdentifier().size();
253 Lex();
254 } else {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000255 break;
Rafael Espindola184640e2011-01-24 18:02:54 +0000256 }
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000257
258 Size += CurSize;
259 SectionName = StringRef(FirstLoc.getPointer(), Size);
260
261 // Make sure the following token is adjacent.
262 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
263 break;
264 }
265 if (Size == 0)
266 return true;
267
268 return false;
269}
270
Rafael Espindola25958732010-11-24 21:57:39 +0000271static SectionKind computeSectionKind(unsigned Flags) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000272 if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola25958732010-11-24 21:57:39 +0000273 return SectionKind::getText();
Rafael Espindola1c130262011-01-23 04:43:11 +0000274 if (Flags & ELF::SHF_TLS)
Rafael Espindola25958732010-11-24 21:57:39 +0000275 return SectionKind::getThreadData();
276 return SectionKind::getDataRel();
277}
278
Benjamin Kramer766f2532013-09-15 19:53:20 +0000279static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
280 unsigned flags = 0;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000281
282 for (unsigned i = 0; i < flagsStr.size(); i++) {
283 switch (flagsStr[i]) {
284 case 'a':
Rafael Espindola1c130262011-01-23 04:43:11 +0000285 flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000286 break;
Benjamin Kramer766f2532013-09-15 19:53:20 +0000287 case 'e':
288 flags |= ELF::SHF_EXCLUDE;
289 break;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000290 case 'x':
Rafael Espindola1c130262011-01-23 04:43:11 +0000291 flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000292 break;
293 case 'w':
Rafael Espindola1c130262011-01-23 04:43:11 +0000294 flags |= ELF::SHF_WRITE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000295 break;
296 case 'M':
Rafael Espindola1c130262011-01-23 04:43:11 +0000297 flags |= ELF::SHF_MERGE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000298 break;
299 case 'S':
Rafael Espindola1c130262011-01-23 04:43:11 +0000300 flags |= ELF::SHF_STRINGS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000301 break;
302 case 'T':
Rafael Espindola1c130262011-01-23 04:43:11 +0000303 flags |= ELF::SHF_TLS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000304 break;
305 case 'c':
Rafael Espindola1c130262011-01-23 04:43:11 +0000306 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000307 break;
308 case 'd':
Rafael Espindola1c130262011-01-23 04:43:11 +0000309 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000310 break;
311 case 'G':
Rafael Espindola1c130262011-01-23 04:43:11 +0000312 flags |= ELF::SHF_GROUP;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000313 break;
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000314 case '?':
315 *UseLastGroup = true;
316 break;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000317 default:
Benjamin Kramer766f2532013-09-15 19:53:20 +0000318 return -1U;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000319 }
320 }
321
322 return flags;
323}
324
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000325bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
326 getStreamer().PushSection();
327
Peter Collingbournedf39be62013-04-17 21:18:16 +0000328 if (ParseSectionArguments(/*IsPush=*/true)) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000329 getStreamer().PopSection();
330 return true;
331 }
332
333 return false;
334}
335
336bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
337 if (!getStreamer().PopSection())
338 return TokError(".popsection without corresponding .pushsection");
339 return false;
340}
341
Eli Friedman21444ef2010-07-17 04:29:04 +0000342// FIXME: This is a work in progress.
343bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000344 return ParseSectionArguments(/*IsPush=*/false);
345}
346
347bool ELFAsmParser::ParseSectionArguments(bool IsPush) {
Eli Friedman21444ef2010-07-17 04:29:04 +0000348 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000349
350 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000351 return TokError("expected identifier in directive");
352
Eli Friedman21444ef2010-07-17 04:29:04 +0000353 StringRef TypeName;
354 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000355 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000356 unsigned Flags = 0;
Peter Collingbournedf39be62013-04-17 21:18:16 +0000357 const MCExpr *Subsection = 0;
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000358 bool UseLastGroup = false;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000359
360 // Set the defaults first.
361 if (SectionName == ".fini" || SectionName == ".init" ||
362 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000363 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000364 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000365 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000366
Eli Friedman21444ef2010-07-17 04:29:04 +0000367 if (getLexer().is(AsmToken::Comma)) {
368 Lex();
369
Peter Collingbournedf39be62013-04-17 21:18:16 +0000370 if (IsPush && getLexer().isNot(AsmToken::String)) {
371 if (getParser().parseExpression(Subsection))
372 return true;
373 if (getLexer().isNot(AsmToken::Comma))
374 goto EndStmt;
375 Lex();
376 }
377
Eli Friedman21444ef2010-07-17 04:29:04 +0000378 if (getLexer().isNot(AsmToken::String))
379 return TokError("expected string in directive");
380
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000381 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000382 Lex();
383
Benjamin Kramer766f2532013-09-15 19:53:20 +0000384 unsigned extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
385 if (extraFlags == -1U)
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000386 return TokError("unknown flag");
387 Flags |= extraFlags;
388
Rafael Espindola1c130262011-01-23 04:43:11 +0000389 bool Mergeable = Flags & ELF::SHF_MERGE;
390 bool Group = Flags & ELF::SHF_GROUP;
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000391 if (Group && UseLastGroup)
392 return TokError("Section cannot specifiy a group name while also acting "
393 "as a member of the last group");
Eli Friedman21444ef2010-07-17 04:29:04 +0000394
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000395 if (getLexer().isNot(AsmToken::Comma)) {
396 if (Mergeable)
397 return TokError("Mergeable section must specify the type");
398 if (Group)
399 return TokError("Group section must specify the type");
400 } else {
401 Lex();
David Majnemer60c81c92013-09-21 05:25:12 +0000402 if (getLexer().is(AsmToken::At) || getLexer().is(AsmToken::Percent) ||
403 getLexer().is(AsmToken::String)) {
404 if (!getLexer().is(AsmToken::String))
405 Lex();
406 } else
407 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000408
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000409 if (getParser().parseIdentifier(TypeName))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000410 return TokError("expected identifier in directive");
411
412 if (Mergeable) {
413 if (getLexer().isNot(AsmToken::Comma))
414 return TokError("expected the entry size");
415 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000416 if (getParser().parseAbsoluteExpression(Size))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000417 return true;
418 if (Size <= 0)
419 return TokError("entry size must be positive");
420 }
421
422 if (Group) {
423 if (getLexer().isNot(AsmToken::Comma))
424 return TokError("expected group name");
425 Lex();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000426 if (getParser().parseIdentifier(GroupName))
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000427 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000428 if (getLexer().is(AsmToken::Comma)) {
429 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000430 StringRef Linkage;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000431 if (getParser().parseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000432 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000433 if (Linkage != "comdat")
434 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000435 }
436 }
437 }
438 }
439
Peter Collingbournedf39be62013-04-17 21:18:16 +0000440EndStmt:
Eli Friedman21444ef2010-07-17 04:29:04 +0000441 if (getLexer().isNot(AsmToken::EndOfStatement))
442 return TokError("unexpected token in directive");
443
Rafael Espindolac85dca62011-01-23 04:28:49 +0000444 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000445
Joerg Sonnenberger42edeb12013-02-16 00:32:53 +0000446 if (TypeName.empty()) {
447 if (SectionName.startswith(".note"))
448 Type = ELF::SHT_NOTE;
449 else if (SectionName == ".init_array")
450 Type = ELF::SHT_INIT_ARRAY;
451 else if (SectionName == ".fini_array")
452 Type = ELF::SHT_FINI_ARRAY;
453 else if (SectionName == ".preinit_array")
454 Type = ELF::SHT_PREINIT_ARRAY;
455 } else {
Eli Friedman21444ef2010-07-17 04:29:04 +0000456 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000457 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000458 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000459 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000460 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000461 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000462 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000463 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000464 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000465 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000466 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000467 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000468 else if (TypeName == "unwind")
469 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000470 else
471 return TokError("unknown section type");
472 }
473
David Majnemerbcd9b3b2013-09-15 19:24:16 +0000474 if (UseLastGroup) {
475 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
476 if (const MCSectionELF *Section =
477 cast_or_null<MCSectionELF>(CurrentSection.first))
478 if (const MCSymbol *Group = Section->getGroup()) {
479 GroupName = Group->getName();
480 Flags |= ELF::SHF_GROUP;
481 }
482 }
483
Rafael Espindola25958732010-11-24 21:57:39 +0000484 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000485 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000486 Flags, Kind, Size,
Peter Collingbournedf39be62013-04-17 21:18:16 +0000487 GroupName),
488 Subsection);
Eli Friedman21444ef2010-07-17 04:29:04 +0000489 return false;
490}
491
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000492bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbournedf39be62013-04-17 21:18:16 +0000493 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
494 if (PreviousSection.first == NULL)
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000495 return TokError(".previous without corresponding .section");
Peter Collingbournedf39be62013-04-17 21:18:16 +0000496 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000497
498 return false;
499}
500
Michael J. Spencere90ea132010-10-09 03:47:55 +0000501/// ParseDirectiveELFType
David Majnemer60c81c92013-09-21 05:25:12 +0000502/// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
503/// ::= .type identifier , #attribute
Michael J. Spencere90ea132010-10-09 03:47:55 +0000504/// ::= .type identifier , @attribute
David Majnemer60c81c92013-09-21 05:25:12 +0000505/// ::= .type identifier , %attribute
506/// ::= .type identifier , "attribute"
Michael J. Spencere90ea132010-10-09 03:47:55 +0000507bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
508 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000509 if (getParser().parseIdentifier(Name))
Michael J. Spencere90ea132010-10-09 03:47:55 +0000510 return TokError("expected identifier in directive");
511
512 // Handle the identifier as the key symbol.
513 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
514
515 if (getLexer().isNot(AsmToken::Comma))
516 return TokError("unexpected token in '.type' directive");
517 Lex();
518
Michael J. Spencere90ea132010-10-09 03:47:55 +0000519 StringRef Type;
520 SMLoc TypeLoc;
David Majnemer60c81c92013-09-21 05:25:12 +0000521 MCSymbolAttr Attr;
522 if (getLexer().is(AsmToken::Identifier)) {
523 TypeLoc = getLexer().getLoc();
524 if (getParser().parseIdentifier(Type))
525 return TokError("expected symbol type in directive");
526 Attr = StringSwitch<MCSymbolAttr>(Type)
527 .Case("STT_FUNC", MCSA_ELF_TypeFunction)
528 .Case("STT_OBJECT", MCSA_ELF_TypeObject)
529 .Case("STT_TLS", MCSA_ELF_TypeTLS)
530 .Case("STT_COMMON", MCSA_ELF_TypeCommon)
531 .Case("STT_NOTYPE", MCSA_ELF_TypeNoType)
532 .Case("STT_GNU_IFUNC", MCSA_ELF_TypeIndFunction)
533 .Default(MCSA_Invalid);
534 } else if (getLexer().is(AsmToken::Hash) || getLexer().is(AsmToken::At) ||
535 getLexer().is(AsmToken::Percent) ||
536 getLexer().is(AsmToken::String)) {
537 if (!getLexer().is(AsmToken::String))
538 Lex();
Michael J. Spencere90ea132010-10-09 03:47:55 +0000539
David Majnemer60c81c92013-09-21 05:25:12 +0000540 TypeLoc = getLexer().getLoc();
541 if (getParser().parseIdentifier(Type))
542 return TokError("expected symbol type in directive");
543 Attr = StringSwitch<MCSymbolAttr>(Type)
544 .Case("function", MCSA_ELF_TypeFunction)
545 .Case("object", MCSA_ELF_TypeObject)
546 .Case("tls_object", MCSA_ELF_TypeTLS)
547 .Case("common", MCSA_ELF_TypeCommon)
548 .Case("notype", MCSA_ELF_TypeNoType)
549 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
550 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
551 .Default(MCSA_Invalid);
552 } else
553 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
554 "'%<type>' or \"<type>\"");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000555
556 if (Attr == MCSA_Invalid)
557 return Error(TypeLoc, "unsupported attribute in '.type' directive");
558
559 if (getLexer().isNot(AsmToken::EndOfStatement))
560 return TokError("unexpected token in '.type' directive");
561
562 Lex();
563
564 getStreamer().EmitSymbolAttribute(Sym, Attr);
565
566 return false;
567}
568
Rafael Espindola61e3b912010-10-26 19:35:47 +0000569/// ParseDirectiveIdent
570/// ::= .ident string
571bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
572 if (getLexer().isNot(AsmToken::String))
573 return TokError("unexpected token in '.ident' directive");
574
575 StringRef Data = getTok().getIdentifier();
576
577 Lex();
578
Rafael Espindolac7ce3e42013-10-16 01:05:45 +0000579 getStreamer().EmitIdent(Data);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000580 return false;
581}
582
Rafael Espindola88182132010-10-27 15:18:17 +0000583/// ParseDirectiveSymver
584/// ::= .symver foo, bar2@zed
585bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
586 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000587 if (getParser().parseIdentifier(Name))
Rafael Espindola88182132010-10-27 15:18:17 +0000588 return TokError("expected identifier in directive");
589
590 if (getLexer().isNot(AsmToken::Comma))
591 return TokError("expected a comma");
592
593 Lex();
594
595 StringRef AliasName;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000596 if (getParser().parseIdentifier(AliasName))
Rafael Espindola88182132010-10-27 15:18:17 +0000597 return TokError("expected identifier in directive");
598
599 if (AliasName.find('@') == StringRef::npos)
600 return TokError("expected a '@' in the name");
601
602 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
603 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
604 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
605
606 getStreamer().EmitAssignment(Alias, Value);
607 return false;
608}
609
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000610/// ParseDirectiveVersion
611/// ::= .version string
612bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
613 if (getLexer().isNot(AsmToken::String))
614 return TokError("unexpected token in '.version' directive");
615
616 StringRef Data = getTok().getIdentifier();
617
618 Lex();
619
620 const MCSection *Note =
621 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
622 SectionKind::getReadOnly());
623
624 getStreamer().PushSection();
625 getStreamer().SwitchSection(Note);
626 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
627 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
628 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christopher68ca5622013-01-09 01:57:54 +0000629 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6a80f9d2012-05-12 14:30:47 +0000630 getStreamer().EmitIntValue(0, 1); // terminate the string.
631 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
632 getStreamer().PopSection();
633 return false;
634}
635
Rafael Espindola484291c2010-11-01 14:28:48 +0000636/// ParseDirectiveWeakref
637/// ::= .weakref foo, bar
638bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
639 // FIXME: Share code with the other alias building directives.
640
641 StringRef AliasName;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000642 if (getParser().parseIdentifier(AliasName))
Rafael Espindola484291c2010-11-01 14:28:48 +0000643 return TokError("expected identifier in directive");
644
645 if (getLexer().isNot(AsmToken::Comma))
646 return TokError("expected a comma");
647
648 Lex();
649
650 StringRef Name;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000651 if (getParser().parseIdentifier(Name))
Rafael Espindola484291c2010-11-01 14:28:48 +0000652 return TokError("expected identifier in directive");
653
654 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
655
656 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
657
658 getStreamer().EmitWeakReference(Alias, Sym);
659 return false;
660}
661
Peter Collingbournedf39be62013-04-17 21:18:16 +0000662bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
663 const MCExpr *Subsection = 0;
664 if (getLexer().isNot(AsmToken::EndOfStatement)) {
665 if (getParser().parseExpression(Subsection))
666 return true;
667 }
668
669 if (getLexer().isNot(AsmToken::EndOfStatement))
670 return TokError("unexpected token in directive");
671
672 getStreamer().SubSection(Subsection);
673 return false;
674}
675
Daniel Dunbar5146a092010-07-12 21:23:32 +0000676namespace llvm {
677
678MCAsmParserExtension *createELFAsmParser() {
679 return new ELFAsmParser;
680}
681
682}