blob: 1e02f05abc4d10367f51c37e642c18b68cf68b3a [file] [log] [blame]
Daniel Dunbarab058b82010-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. Spencer3d898232010-10-09 03:47:55 +000011#include "llvm/ADT/StringSwitch.h"
Eli Friedman9de59672010-07-17 06:27:28 +000012#include "llvm/ADT/Twine.h"
Eli Friedman9e36dd02010-07-17 04:29:04 +000013#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarab058b82010-07-12 21:23:32 +000014#include "llvm/MC/MCContext.h"
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbarab058b82010-07-12 21:23:32 +000016#include "llvm/MC/MCParser/MCAsmLexer.h"
Eli Friedman9e36dd02010-07-17 04:29:04 +000017#include "llvm/MC/MCSectionELF.h"
18#include "llvm/MC/MCStreamer.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000019#include "llvm/MC/MCSymbolELF.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000020#include "llvm/Support/ELF.h"
Daniel Dunbarab058b82010-07-12 21:23:32 +000021using namespace llvm;
22
23namespace {
24
25class ELFAsmParser : public MCAsmParserExtension {
Eli Bendersky29b9f472013-01-16 00:50:52 +000026 template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Jim Grosbachd2037eb2013-02-20 22:21:35 +000027 void addDirectiveHandler(StringRef Directive) {
Eli Bendersky29b9f472013-01-16 00:50:52 +000028 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
29 this, HandleDirective<ELFAsmParser, HandlerMethod>);
30
Jim Grosbachd2037eb2013-02-20 22:21:35 +000031 getParser().addDirectiveHandler(Directive, Handler);
Daniel Dunbar8897d472010-07-18 22:22:07 +000032 }
33
Rafael Espindola5645bad2013-10-16 01:05:45 +000034 bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
35 SectionKind Kind);
Daniel Dunbarab058b82010-07-12 21:23:32 +000036
37public:
Rafael Espindola5645bad2013-10-16 01:05:45 +000038 ELFAsmParser() { BracketExpressionsSupported = true; }
Daniel Dunbarab058b82010-07-12 21:23:32 +000039
Craig Topper59be68f2014-03-08 07:14:16 +000040 void Initialize(MCAsmParser &Parser) override {
Daniel Dunbarab058b82010-07-12 21:23:32 +000041 // Call the base implementation.
42 this->MCAsmParserExtension::Initialize(Parser);
43
Jim Grosbachd2037eb2013-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 Grosbach1a55fa62011-07-25 17:11:29 +000051 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000052 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000053 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000054 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000055 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000056 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
57 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000058 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000059 addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
60 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
61 addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
62 addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
63 addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
64 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
65 addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
66 addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
67 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
68 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
69 addDirectiveHandler<
Jim Grosbach38b1ed82011-07-25 17:55:35 +000070 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000071 addDirectiveHandler<
Jim Grosbach38b1ed82011-07-25 17:55:35 +000072 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000073 addDirectiveHandler<
Jim Grosbach38b1ed82011-07-25 17:55:35 +000074 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
Peter Collingbourne2f495b92013-04-17 21:18:16 +000075 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
Daniel Dunbarab058b82010-07-12 21:23:32 +000076 }
77
Rafael Espindolaf667d922010-09-15 21:48:40 +000078 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
79 // the best way for us to get access to it?
Daniel Dunbarab058b82010-07-12 21:23:32 +000080 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +000081 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola449711c2015-11-18 06:02:15 +000082 ELF::SHF_WRITE | ELF::SHF_ALLOC,
83 SectionKind::getData());
Daniel Dunbarab058b82010-07-12 21:23:32 +000084 }
85 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +000086 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000087 ELF::SHF_EXECINSTR |
88 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbarab058b82010-07-12 21:23:32 +000089 }
Matt Fleminga8f6c1c2010-07-20 21:12:46 +000090 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +000091 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000092 ELF::SHF_WRITE |
93 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Fleminga8f6c1c2010-07-20 21:12:46 +000094 }
95 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +000096 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000097 ELF::SHF_ALLOC,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +000098 SectionKind::getReadOnly());
99 }
100 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000101 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000102 ELF::SHF_ALLOC |
103 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000104 SectionKind::getThreadData());
105 }
106 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000107 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000108 ELF::SHF_ALLOC |
109 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000110 SectionKind::getThreadBSS());
111 }
112 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000113 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola449711c2015-11-18 06:02:15 +0000114 ELF::SHF_ALLOC | ELF::SHF_WRITE,
115 SectionKind::getData());
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000116 }
117 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000118 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000119 ELF::SHF_ALLOC |
120 ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000121 SectionKind::getReadOnlyWithRel());
122 }
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000123 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000124 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola449711c2015-11-18 06:02:15 +0000125 ELF::SHF_ALLOC | ELF::SHF_WRITE,
126 SectionKind::getData());
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000127 }
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000128 bool ParseDirectivePushSection(StringRef, SMLoc);
129 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman9e36dd02010-07-17 04:29:04 +0000130 bool ParseDirectiveSection(StringRef, SMLoc);
131 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramere39017c2010-09-02 18:53:37 +0000132 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000133 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000134 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000135 bool ParseDirectiveSymver(StringRef, SMLoc);
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000136 bool ParseDirectiveVersion(StringRef, SMLoc);
Rafael Espindola16145972010-11-01 14:28:48 +0000137 bool ParseDirectiveWeakref(StringRef, SMLoc);
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000138 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000139 bool ParseDirectiveSubsection(StringRef, SMLoc);
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000140
141private:
142 bool ParseSectionName(StringRef &SectionName);
Oliver Stannard8b273082014-06-19 15:52:37 +0000143 bool ParseSectionArguments(bool IsPush, SMLoc loc);
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000144 unsigned parseSunStyleSectionFlags();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000145};
146
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000147}
Daniel Dunbarab058b82010-07-12 21:23:32 +0000148
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000149/// ParseDirectiveSymbolAttribute
150/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
151bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
152 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
153 .Case(".weak", MCSA_Weak)
154 .Case(".local", MCSA_Local)
155 .Case(".hidden", MCSA_Hidden)
156 .Case(".internal", MCSA_Internal)
157 .Case(".protected", MCSA_Protected)
158 .Default(MCSA_Invalid);
159 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
160 if (getLexer().isNot(AsmToken::EndOfStatement)) {
161 for (;;) {
162 StringRef Name;
163
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000164 if (getParser().parseIdentifier(Name))
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000165 return TokError("expected identifier in directive");
166
Jim Grosbach6f482002015-05-18 18:43:14 +0000167 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000168
169 getStreamer().EmitSymbolAttribute(Sym, Attr);
170
171 if (getLexer().is(AsmToken::EndOfStatement))
172 break;
173
174 if (getLexer().isNot(AsmToken::Comma))
175 return TokError("unexpected token in directive");
176 Lex();
177 }
178 }
179
180 Lex();
181 return false;
182}
183
Daniel Dunbarab058b82010-07-12 21:23:32 +0000184bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
185 unsigned Flags, SectionKind Kind) {
Craig Topper353eda42014-04-24 06:44:33 +0000186 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000187 if (getLexer().isNot(AsmToken::EndOfStatement)) {
188 if (getParser().parseExpression(Subsection))
189 return true;
190 }
Daniel Dunbarab058b82010-07-12 21:23:32 +0000191
Rafael Espindolaba31e272015-01-29 17:33:21 +0000192 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000193 Subsection);
Daniel Dunbarab058b82010-07-12 21:23:32 +0000194
195 return false;
196}
197
Eli Friedman9e36dd02010-07-17 04:29:04 +0000198bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedman56178a02010-07-17 03:09:18 +0000199 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000200 if (getParser().parseIdentifier(Name))
Eli Friedman56178a02010-07-17 03:09:18 +0000201 return TokError("expected identifier in directive");
Rafael Espindolaa8695762015-06-02 00:25:12 +0000202 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
Eli Friedman56178a02010-07-17 03:09:18 +0000203
204 if (getLexer().isNot(AsmToken::Comma))
205 return TokError("unexpected token in directive");
206 Lex();
207
208 const MCExpr *Expr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000209 if (getParser().parseExpression(Expr))
Eli Friedman56178a02010-07-17 03:09:18 +0000210 return true;
211
212 if (getLexer().isNot(AsmToken::EndOfStatement))
213 return TokError("unexpected token in directive");
214
Rafael Espindolaa8695762015-06-02 00:25:12 +0000215 getStreamer().emitELFSize(Sym, Expr);
Eli Friedman56178a02010-07-17 03:09:18 +0000216 return false;
217}
218
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000219bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
220 // A section name can contain -, so we cannot just use
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000221 // parseIdentifier.
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000222 SMLoc FirstLoc = getLexer().getLoc();
223 unsigned Size = 0;
224
Rafael Espindola689939e2011-01-24 18:02:54 +0000225 if (getLexer().is(AsmToken::String)) {
226 SectionName = getTok().getIdentifier();
227 Lex();
228 return false;
229 }
230
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000231 for (;;) {
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000232
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000233 SMLoc PrevLoc = getLexer().getLoc();
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000234 if (getLexer().is(AsmToken::Comma) ||
235 getLexer().is(AsmToken::EndOfStatement))
236 break;
237
238 unsigned CurSize;
239 if (getLexer().is(AsmToken::String)) {
Rafael Espindola689939e2011-01-24 18:02:54 +0000240 CurSize = getTok().getIdentifier().size() + 2;
241 Lex();
242 } else if (getLexer().is(AsmToken::Identifier)) {
243 CurSize = getTok().getIdentifier().size();
244 Lex();
245 } else {
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000246 CurSize = getTok().getString().size();
247 Lex();
Rafael Espindola689939e2011-01-24 18:02:54 +0000248 }
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000249 Size += CurSize;
250 SectionName = StringRef(FirstLoc.getPointer(), Size);
251
252 // Make sure the following token is adjacent.
253 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
254 break;
255 }
256 if (Size == 0)
257 return true;
258
259 return false;
260}
261
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000262static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
263 unsigned flags = 0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000264
265 for (unsigned i = 0; i < flagsStr.size(); i++) {
266 switch (flagsStr[i]) {
267 case 'a':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000268 flags |= ELF::SHF_ALLOC;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000269 break;
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000270 case 'e':
271 flags |= ELF::SHF_EXCLUDE;
272 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000273 case 'x':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000274 flags |= ELF::SHF_EXECINSTR;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000275 break;
276 case 'w':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000277 flags |= ELF::SHF_WRITE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000278 break;
279 case 'M':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000280 flags |= ELF::SHF_MERGE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000281 break;
282 case 'S':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000283 flags |= ELF::SHF_STRINGS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000284 break;
285 case 'T':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000286 flags |= ELF::SHF_TLS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000287 break;
288 case 'c':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000289 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000290 break;
291 case 'd':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000292 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000293 break;
294 case 'G':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000295 flags |= ELF::SHF_GROUP;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000296 break;
David Majnemera4b521b2013-09-15 19:24:16 +0000297 case '?':
298 *UseLastGroup = true;
299 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000300 default:
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000301 return -1U;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000302 }
303 }
304
305 return flags;
306}
307
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000308unsigned ELFAsmParser::parseSunStyleSectionFlags() {
309 unsigned flags = 0;
310 while (getLexer().is(AsmToken::Hash)) {
311 Lex(); // Eat the #.
312
313 if (!getLexer().is(AsmToken::Identifier))
314 return -1U;
315
316 StringRef flagId = getTok().getIdentifier();
317 if (flagId == "alloc")
318 flags |= ELF::SHF_ALLOC;
319 else if (flagId == "execinstr")
320 flags |= ELF::SHF_EXECINSTR;
321 else if (flagId == "write")
322 flags |= ELF::SHF_WRITE;
323 else if (flagId == "tls")
324 flags |= ELF::SHF_TLS;
325 else
326 return -1U;
327
328 Lex(); // Eat the flag.
329
330 if (!getLexer().is(AsmToken::Comma))
331 break;
332 Lex(); // Eat the comma.
333 }
334 return flags;
335}
336
337
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000338bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
339 getStreamer().PushSection();
340
Oliver Stannard8b273082014-06-19 15:52:37 +0000341 if (ParseSectionArguments(/*IsPush=*/true, loc)) {
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000342 getStreamer().PopSection();
343 return true;
344 }
345
346 return false;
347}
348
349bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
350 if (!getStreamer().PopSection())
351 return TokError(".popsection without corresponding .pushsection");
352 return false;
353}
354
Eli Friedman9e36dd02010-07-17 04:29:04 +0000355// FIXME: This is a work in progress.
Oliver Stannard8b273082014-06-19 15:52:37 +0000356bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
357 return ParseSectionArguments(/*IsPush=*/false, loc);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000358}
359
Oliver Stannard8b273082014-06-19 15:52:37 +0000360bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000361 StringRef SectionName;
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000362
363 if (ParseSectionName(SectionName))
Eli Friedman9e36dd02010-07-17 04:29:04 +0000364 return TokError("expected identifier in directive");
365
Eli Friedman9e36dd02010-07-17 04:29:04 +0000366 StringRef TypeName;
367 int64_t Size = 0;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000368 StringRef GroupName;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000369 unsigned Flags = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000370 const MCExpr *Subsection = nullptr;
David Majnemera4b521b2013-09-15 19:24:16 +0000371 bool UseLastGroup = false;
Rafael Espindola68fa2492015-02-17 20:48:01 +0000372 StringRef UniqueStr;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000373 int64_t UniqueID = ~0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000374
375 // Set the defaults first.
376 if (SectionName == ".fini" || SectionName == ".init" ||
377 SectionName == ".rodata")
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000378 Flags |= ELF::SHF_ALLOC;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000379 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000380 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000381
Eli Friedman9e36dd02010-07-17 04:29:04 +0000382 if (getLexer().is(AsmToken::Comma)) {
383 Lex();
384
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000385 if (IsPush && getLexer().isNot(AsmToken::String)) {
386 if (getParser().parseExpression(Subsection))
387 return true;
388 if (getLexer().isNot(AsmToken::Comma))
389 goto EndStmt;
390 Lex();
391 }
Eli Friedman9e36dd02010-07-17 04:29:04 +0000392
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000393 unsigned extraFlags;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000394
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000395 if (getLexer().isNot(AsmToken::String)) {
396 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
397 || getLexer().isNot(AsmToken::Hash))
398 return TokError("expected string in directive");
399 extraFlags = parseSunStyleSectionFlags();
400 } else {
401 StringRef FlagsStr = getTok().getStringContents();
402 Lex();
403 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
404 }
405
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000406 if (extraFlags == -1U)
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000407 return TokError("unknown flag");
408 Flags |= extraFlags;
409
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000410 bool Mergeable = Flags & ELF::SHF_MERGE;
411 bool Group = Flags & ELF::SHF_GROUP;
David Majnemera4b521b2013-09-15 19:24:16 +0000412 if (Group && UseLastGroup)
413 return TokError("Section cannot specifiy a group name while also acting "
414 "as a member of the last group");
Eli Friedman9e36dd02010-07-17 04:29:04 +0000415
Rafael Espindola8aefb662010-10-28 21:33:33 +0000416 if (getLexer().isNot(AsmToken::Comma)) {
417 if (Mergeable)
418 return TokError("Mergeable section must specify the type");
419 if (Group)
420 return TokError("Group section must specify the type");
421 } else {
422 Lex();
David Majnemerf90c3b52013-09-21 05:25:12 +0000423 if (getLexer().is(AsmToken::At) || getLexer().is(AsmToken::Percent) ||
424 getLexer().is(AsmToken::String)) {
425 if (!getLexer().is(AsmToken::String))
426 Lex();
427 } else
428 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
Rafael Espindola8aefb662010-10-28 21:33:33 +0000429
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000430 if (getParser().parseIdentifier(TypeName))
Rafael Espindola8aefb662010-10-28 21:33:33 +0000431 return TokError("expected identifier in directive");
432
433 if (Mergeable) {
434 if (getLexer().isNot(AsmToken::Comma))
435 return TokError("expected the entry size");
436 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000437 if (getParser().parseAbsoluteExpression(Size))
Rafael Espindola8aefb662010-10-28 21:33:33 +0000438 return true;
439 if (Size <= 0)
440 return TokError("entry size must be positive");
441 }
442
443 if (Group) {
444 if (getLexer().isNot(AsmToken::Comma))
445 return TokError("expected group name");
446 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000447 if (getParser().parseIdentifier(GroupName))
Rafael Espindola8aefb662010-10-28 21:33:33 +0000448 return true;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000449 if (getLexer().is(AsmToken::Comma)) {
450 Lex();
Rafael Espindola8aefb662010-10-28 21:33:33 +0000451 StringRef Linkage;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000452 if (getParser().parseIdentifier(Linkage))
Eli Friedman9e36dd02010-07-17 04:29:04 +0000453 return true;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000454 if (Linkage != "comdat")
455 return TokError("Linkage must be 'comdat'");
Eli Friedman9e36dd02010-07-17 04:29:04 +0000456 }
457 }
Rafael Espindola68fa2492015-02-17 20:48:01 +0000458 if (getLexer().is(AsmToken::Comma)) {
459 Lex();
460 if (getParser().parseIdentifier(UniqueStr))
461 return TokError("expected identifier in directive");
462 if (UniqueStr != "unique")
463 return TokError("expected 'unique'");
Rafael Espindola41e2b5c2015-04-06 16:34:41 +0000464 if (getLexer().isNot(AsmToken::Comma))
465 return TokError("expected commma");
466 Lex();
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000467 if (getParser().parseAbsoluteExpression(UniqueID))
468 return true;
469 if (UniqueID < 0)
470 return TokError("unique id must be positive");
471 if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
472 return TokError("unique id is too large");
Rafael Espindola68fa2492015-02-17 20:48:01 +0000473 }
Eli Friedman9e36dd02010-07-17 04:29:04 +0000474 }
475 }
476
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000477EndStmt:
Eli Friedman9e36dd02010-07-17 04:29:04 +0000478 if (getLexer().isNot(AsmToken::EndOfStatement))
479 return TokError("unexpected token in directive");
480
Rafael Espindolaaea49582011-01-23 04:28:49 +0000481 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000482
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000483 if (TypeName.empty()) {
484 if (SectionName.startswith(".note"))
485 Type = ELF::SHT_NOTE;
486 else if (SectionName == ".init_array")
487 Type = ELF::SHT_INIT_ARRAY;
488 else if (SectionName == ".fini_array")
489 Type = ELF::SHT_FINI_ARRAY;
490 else if (SectionName == ".preinit_array")
491 Type = ELF::SHT_PREINIT_ARRAY;
492 } else {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000493 if (TypeName == "init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000494 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000495 else if (TypeName == "fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000496 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000497 else if (TypeName == "preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000498 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000499 else if (TypeName == "nobits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000500 Type = ELF::SHT_NOBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000501 else if (TypeName == "progbits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000502 Type = ELF::SHT_PROGBITS;
Rafael Espindola9ae2d052010-12-26 21:30:59 +0000503 else if (TypeName == "note")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000504 Type = ELF::SHT_NOTE;
Rafael Espindola4b7b7fb2011-01-23 05:43:40 +0000505 else if (TypeName == "unwind")
506 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000507 else
508 return TokError("unknown section type");
509 }
510
David Majnemera4b521b2013-09-15 19:24:16 +0000511 if (UseLastGroup) {
512 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
513 if (const MCSectionELF *Section =
514 cast_or_null<MCSectionELF>(CurrentSection.first))
515 if (const MCSymbol *Group = Section->getGroup()) {
516 GroupName = Group->getName();
517 Flags |= ELF::SHF_GROUP;
518 }
519 }
520
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000521 MCSection *ELFSection = getContext().getELFSection(SectionName, Type, Flags,
522 Size, GroupName, UniqueID);
Oliver Stannard8b273082014-06-19 15:52:37 +0000523 getStreamer().SwitchSection(ELFSection, Subsection);
524
525 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindolae0746792015-05-21 16:52:32 +0000526 bool InsertResult = getContext().addGenDwarfSection(ELFSection);
527 if (InsertResult) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000528 if (getContext().getDwarfVersion() <= 2)
Oliver Stannard14f97d02014-09-22 10:45:16 +0000529 Warning(loc, "DWARF2 only supports one section per compilation unit");
Oliver Stannard8b273082014-06-19 15:52:37 +0000530
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000531 if (!ELFSection->getBeginSymbol()) {
532 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
533 getStreamer().EmitLabel(SectionStartSymbol);
534 ELFSection->setBeginSymbol(SectionStartSymbol);
535 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000536 }
537 }
538
Eli Friedman9e36dd02010-07-17 04:29:04 +0000539 return false;
540}
541
Benjamin Kramere39017c2010-09-02 18:53:37 +0000542bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000543 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
Craig Topper353eda42014-04-24 06:44:33 +0000544 if (PreviousSection.first == nullptr)
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000545 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000546 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Benjamin Kramere39017c2010-09-02 18:53:37 +0000547
548 return false;
549}
550
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000551static MCSymbolAttr MCAttrForString(StringRef Type) {
552 return StringSwitch<MCSymbolAttr>(Type)
553 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
554 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
555 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
556 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
557 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
558 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
559 MCSA_ELF_TypeIndFunction)
560 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
561 .Default(MCSA_Invalid);
562}
563
Michael J. Spencer3d898232010-10-09 03:47:55 +0000564/// ParseDirectiveELFType
David Majnemerf90c3b52013-09-21 05:25:12 +0000565/// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
566/// ::= .type identifier , #attribute
Michael J. Spencer3d898232010-10-09 03:47:55 +0000567/// ::= .type identifier , @attribute
David Majnemerf90c3b52013-09-21 05:25:12 +0000568/// ::= .type identifier , %attribute
569/// ::= .type identifier , "attribute"
Michael J. Spencer3d898232010-10-09 03:47:55 +0000570bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
571 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000572 if (getParser().parseIdentifier(Name))
Michael J. Spencer3d898232010-10-09 03:47:55 +0000573 return TokError("expected identifier in directive");
574
575 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000576 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000577
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000578 // NOTE the comma is optional in all cases. It is only documented as being
579 // optional in the first case, however, GAS will silently treat the comma as
580 // optional in all cases. Furthermore, although the documentation states that
581 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
582 // accepts both the upper case name as well as the lower case aliases.
583 if (getLexer().is(AsmToken::Comma))
584 Lex();
Michael J. Spencer3d898232010-10-09 03:47:55 +0000585
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000586 if (getLexer().isNot(AsmToken::Identifier) &&
Gabor Ballabasaf06a882015-07-01 08:58:49 +0000587 getLexer().isNot(AsmToken::Hash) &&
588 getLexer().isNot(AsmToken::Percent) &&
589 getLexer().isNot(AsmToken::String)) {
590 if (!getLexer().getAllowAtInIdentifier())
591 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
592 "'%<type>' or \"<type>\"");
593 else if (getLexer().isNot(AsmToken::At))
594 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
595 "'%<type>' or \"<type>\"");
596 }
Michael J. Spencer3d898232010-10-09 03:47:55 +0000597
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000598 if (getLexer().isNot(AsmToken::String) &&
599 getLexer().isNot(AsmToken::Identifier))
600 Lex();
601
602 SMLoc TypeLoc = getLexer().getLoc();
603
604 StringRef Type;
605 if (getParser().parseIdentifier(Type))
606 return TokError("expected symbol type in directive");
607
608 MCSymbolAttr Attr = MCAttrForString(Type);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000609 if (Attr == MCSA_Invalid)
610 return Error(TypeLoc, "unsupported attribute in '.type' directive");
611
612 if (getLexer().isNot(AsmToken::EndOfStatement))
613 return TokError("unexpected token in '.type' directive");
Michael J. Spencer3d898232010-10-09 03:47:55 +0000614 Lex();
615
616 getStreamer().EmitSymbolAttribute(Sym, Attr);
617
618 return false;
619}
620
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000621/// ParseDirectiveIdent
622/// ::= .ident string
623bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
624 if (getLexer().isNot(AsmToken::String))
625 return TokError("unexpected token in '.ident' directive");
626
627 StringRef Data = getTok().getIdentifier();
628
629 Lex();
630
Rafael Espindola5645bad2013-10-16 01:05:45 +0000631 getStreamer().EmitIdent(Data);
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000632 return false;
633}
634
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000635/// ParseDirectiveSymver
636/// ::= .symver foo, bar2@zed
637bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
638 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000639 if (getParser().parseIdentifier(Name))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000640 return TokError("expected identifier in directive");
641
642 if (getLexer().isNot(AsmToken::Comma))
643 return TokError("expected a comma");
644
David Peixottoc0f92a22014-01-15 22:40:02 +0000645 // ARM assembly uses @ for a comment...
646 // except when parsing the second parameter of the .symver directive.
647 // Force the next symbol to allow @ in the identifier, which is
648 // required for this directive and then reset it to its initial state.
649 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
650 getLexer().setAllowAtInIdentifier(true);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000651 Lex();
David Peixottoc0f92a22014-01-15 22:40:02 +0000652 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000653
654 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000655 if (getParser().parseIdentifier(AliasName))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000656 return TokError("expected identifier in directive");
657
658 if (AliasName.find('@') == StringRef::npos)
659 return TokError("expected a '@' in the name");
660
Jim Grosbach6f482002015-05-18 18:43:14 +0000661 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
662 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000663 const MCExpr *Value = MCSymbolRefExpr::create(Sym, getContext());
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000664
665 getStreamer().EmitAssignment(Alias, Value);
666 return false;
667}
668
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000669/// ParseDirectiveVersion
670/// ::= .version string
671bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
672 if (getLexer().isNot(AsmToken::String))
673 return TokError("unexpected token in '.version' directive");
674
675 StringRef Data = getTok().getIdentifier();
676
677 Lex();
678
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000679 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000680
681 getStreamer().PushSection();
682 getStreamer().SwitchSection(Note);
683 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
684 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
685 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000686 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000687 getStreamer().EmitIntValue(0, 1); // terminate the string.
688 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
689 getStreamer().PopSection();
690 return false;
691}
692
Rafael Espindola16145972010-11-01 14:28:48 +0000693/// ParseDirectiveWeakref
694/// ::= .weakref foo, bar
695bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
696 // FIXME: Share code with the other alias building directives.
697
698 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000699 if (getParser().parseIdentifier(AliasName))
Rafael Espindola16145972010-11-01 14:28:48 +0000700 return TokError("expected identifier in directive");
701
702 if (getLexer().isNot(AsmToken::Comma))
703 return TokError("expected a comma");
704
705 Lex();
706
707 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000708 if (getParser().parseIdentifier(Name))
Rafael Espindola16145972010-11-01 14:28:48 +0000709 return TokError("expected identifier in directive");
710
Jim Grosbach6f482002015-05-18 18:43:14 +0000711 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
Rafael Espindola16145972010-11-01 14:28:48 +0000712
Jim Grosbach6f482002015-05-18 18:43:14 +0000713 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Rafael Espindola16145972010-11-01 14:28:48 +0000714
715 getStreamer().EmitWeakReference(Alias, Sym);
716 return false;
717}
718
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000719bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
Craig Topper353eda42014-04-24 06:44:33 +0000720 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000721 if (getLexer().isNot(AsmToken::EndOfStatement)) {
722 if (getParser().parseExpression(Subsection))
723 return true;
724 }
725
726 if (getLexer().isNot(AsmToken::EndOfStatement))
727 return TokError("unexpected token in directive");
728
729 getStreamer().SubSection(Subsection);
730 return false;
731}
732
Daniel Dunbarab058b82010-07-12 21:23:32 +0000733namespace llvm {
734
735MCAsmParserExtension *createELFAsmParser() {
736 return new ELFAsmParser;
737}
738
739}