blob: e3020040e57846022ad52f2c0e8a3e5e36ee7f7b [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"
David Majnemera4b521b2013-09-15 19:24:16 +000019#include "llvm/MC/MCSymbol.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::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000056 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000057 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000058 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
59 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000060 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
Jim Grosbachd2037eb2013-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 Grosbach38b1ed82011-07-25 17:55:35 +000072 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000073 addDirectiveHandler<
Jim Grosbach38b1ed82011-07-25 17:55:35 +000074 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000075 addDirectiveHandler<
Jim Grosbach38b1ed82011-07-25 17:55:35 +000076 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
Peter Collingbourne2f495b92013-04-17 21:18:16 +000077 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
Daniel Dunbarab058b82010-07-12 21:23:32 +000078 }
79
Rafael Espindolaf667d922010-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 Dunbarab058b82010-07-12 21:23:32 +000082 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +000083 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000084 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Daniel Dunbarab058b82010-07-12 21:23:32 +000085 SectionKind::getDataRel());
86 }
87 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +000088 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000089 ELF::SHF_EXECINSTR |
90 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbarab058b82010-07-12 21:23:32 +000091 }
Matt Fleminga8f6c1c2010-07-20 21:12:46 +000092 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +000093 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000094 ELF::SHF_WRITE |
95 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Fleminga8f6c1c2010-07-20 21:12:46 +000096 }
97 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +000098 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000099 ELF::SHF_ALLOC,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000100 SectionKind::getReadOnly());
101 }
102 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000103 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000104 ELF::SHF_ALLOC |
105 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000106 SectionKind::getThreadData());
107 }
108 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000109 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000110 ELF::SHF_ALLOC |
111 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000112 SectionKind::getThreadBSS());
113 }
114 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000115 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000116 ELF::SHF_ALLOC |
117 ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000118 SectionKind::getDataRel());
119 }
120 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000121 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000122 ELF::SHF_ALLOC |
123 ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000124 SectionKind::getReadOnlyWithRel());
125 }
126 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000127 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000128 ELF::SHF_ALLOC |
129 ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000130 SectionKind::getReadOnlyWithRelLocal());
131 }
132 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000133 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000134 ELF::SHF_ALLOC |
135 ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000136 SectionKind::getDataRel());
137 }
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000138 bool ParseDirectivePushSection(StringRef, SMLoc);
139 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman9e36dd02010-07-17 04:29:04 +0000140 bool ParseDirectiveSection(StringRef, SMLoc);
141 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramere39017c2010-09-02 18:53:37 +0000142 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000143 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000144 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000145 bool ParseDirectiveSymver(StringRef, SMLoc);
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000146 bool ParseDirectiveVersion(StringRef, SMLoc);
Rafael Espindola16145972010-11-01 14:28:48 +0000147 bool ParseDirectiveWeakref(StringRef, SMLoc);
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000148 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000149 bool ParseDirectiveSubsection(StringRef, SMLoc);
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000150
151private:
152 bool ParseSectionName(StringRef &SectionName);
Oliver Stannard8b273082014-06-19 15:52:37 +0000153 bool ParseSectionArguments(bool IsPush, SMLoc loc);
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000154 unsigned parseSunStyleSectionFlags();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000155};
156
157}
158
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000159/// ParseDirectiveSymbolAttribute
160/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
161bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
162 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
163 .Case(".weak", MCSA_Weak)
164 .Case(".local", MCSA_Local)
165 .Case(".hidden", MCSA_Hidden)
166 .Case(".internal", MCSA_Internal)
167 .Case(".protected", MCSA_Protected)
168 .Default(MCSA_Invalid);
169 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
170 if (getLexer().isNot(AsmToken::EndOfStatement)) {
171 for (;;) {
172 StringRef Name;
173
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000174 if (getParser().parseIdentifier(Name))
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000175 return TokError("expected identifier in directive");
176
177 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
178
179 getStreamer().EmitSymbolAttribute(Sym, Attr);
180
181 if (getLexer().is(AsmToken::EndOfStatement))
182 break;
183
184 if (getLexer().isNot(AsmToken::Comma))
185 return TokError("unexpected token in directive");
186 Lex();
187 }
188 }
189
190 Lex();
191 return false;
192}
193
Daniel Dunbarab058b82010-07-12 21:23:32 +0000194bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
195 unsigned Flags, SectionKind Kind) {
Craig Topper353eda42014-04-24 06:44:33 +0000196 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000197 if (getLexer().isNot(AsmToken::EndOfStatement)) {
198 if (getParser().parseExpression(Subsection))
199 return true;
200 }
Daniel Dunbarab058b82010-07-12 21:23:32 +0000201
202 getStreamer().SwitchSection(getContext().getELFSection(
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000203 Section, Type, Flags, Kind),
204 Subsection);
Daniel Dunbarab058b82010-07-12 21:23:32 +0000205
206 return false;
207}
208
Eli Friedman9e36dd02010-07-17 04:29:04 +0000209bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedman56178a02010-07-17 03:09:18 +0000210 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000211 if (getParser().parseIdentifier(Name))
Eli Friedman56178a02010-07-17 03:09:18 +0000212 return TokError("expected identifier in directive");
Dmitri Gribenkoca1e27b2012-09-10 21:26:47 +0000213 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
Eli Friedman56178a02010-07-17 03:09:18 +0000214
215 if (getLexer().isNot(AsmToken::Comma))
216 return TokError("unexpected token in directive");
217 Lex();
218
219 const MCExpr *Expr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000220 if (getParser().parseExpression(Expr))
Eli Friedman56178a02010-07-17 03:09:18 +0000221 return true;
222
223 if (getLexer().isNot(AsmToken::EndOfStatement))
224 return TokError("unexpected token in directive");
225
226 getStreamer().EmitELFSize(Sym, Expr);
227 return false;
228}
229
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000230bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
231 // A section name can contain -, so we cannot just use
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000232 // parseIdentifier.
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000233 SMLoc FirstLoc = getLexer().getLoc();
234 unsigned Size = 0;
235
Rafael Espindola689939e2011-01-24 18:02:54 +0000236 if (getLexer().is(AsmToken::String)) {
237 SectionName = getTok().getIdentifier();
238 Lex();
239 return false;
240 }
241
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000242 for (;;) {
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000243 unsigned CurSize;
244
245 SMLoc PrevLoc = getLexer().getLoc();
246 if (getLexer().is(AsmToken::Minus)) {
247 CurSize = 1;
248 Lex(); // Consume the "-".
Rafael Espindola689939e2011-01-24 18:02:54 +0000249 } else if (getLexer().is(AsmToken::String)) {
250 CurSize = getTok().getIdentifier().size() + 2;
251 Lex();
252 } else if (getLexer().is(AsmToken::Identifier)) {
253 CurSize = getTok().getIdentifier().size();
254 Lex();
255 } else {
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000256 break;
Rafael Espindola689939e2011-01-24 18:02:54 +0000257 }
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000258
259 Size += CurSize;
260 SectionName = StringRef(FirstLoc.getPointer(), Size);
261
262 // Make sure the following token is adjacent.
263 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
264 break;
265 }
266 if (Size == 0)
267 return true;
268
269 return false;
270}
271
Rafael Espindolac44c26b2014-03-28 19:14:08 +0000272static SectionKind computeSectionKind(unsigned Flags, unsigned ElemSize) {
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000273 if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000274 return SectionKind::getText();
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000275 if (Flags & ELF::SHF_TLS)
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000276 return SectionKind::getThreadData();
Rafael Espindolac44c26b2014-03-28 19:14:08 +0000277 if (Flags & ELF::SHF_MERGE) {
278 if (Flags & ELF::SHF_STRINGS) {
279 switch (ElemSize) {
280 default:
281 break;
282 case 1:
283 return SectionKind::getMergeable1ByteCString();
284 case 2:
285 return SectionKind::getMergeable2ByteCString();
286 case 4:
287 return SectionKind::getMergeable4ByteCString();
288 }
289 } else {
290 switch (ElemSize) {
291 default:
292 break;
293 case 4:
294 return SectionKind::getMergeableConst4();
295 case 8:
296 return SectionKind::getMergeableConst8();
297 case 16:
298 return SectionKind::getMergeableConst16();
299 }
300 }
301 }
302
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000303 return SectionKind::getDataRel();
304}
305
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000306static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
307 unsigned flags = 0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000308
309 for (unsigned i = 0; i < flagsStr.size(); i++) {
310 switch (flagsStr[i]) {
311 case 'a':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000312 flags |= ELF::SHF_ALLOC;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000313 break;
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000314 case 'e':
315 flags |= ELF::SHF_EXCLUDE;
316 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000317 case 'x':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000318 flags |= ELF::SHF_EXECINSTR;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000319 break;
320 case 'w':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000321 flags |= ELF::SHF_WRITE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000322 break;
323 case 'M':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000324 flags |= ELF::SHF_MERGE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000325 break;
326 case 'S':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000327 flags |= ELF::SHF_STRINGS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000328 break;
329 case 'T':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000330 flags |= ELF::SHF_TLS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000331 break;
332 case 'c':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000333 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000334 break;
335 case 'd':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000336 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000337 break;
338 case 'G':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000339 flags |= ELF::SHF_GROUP;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000340 break;
David Majnemera4b521b2013-09-15 19:24:16 +0000341 case '?':
342 *UseLastGroup = true;
343 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000344 default:
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000345 return -1U;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000346 }
347 }
348
349 return flags;
350}
351
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000352unsigned ELFAsmParser::parseSunStyleSectionFlags() {
353 unsigned flags = 0;
354 while (getLexer().is(AsmToken::Hash)) {
355 Lex(); // Eat the #.
356
357 if (!getLexer().is(AsmToken::Identifier))
358 return -1U;
359
360 StringRef flagId = getTok().getIdentifier();
361 if (flagId == "alloc")
362 flags |= ELF::SHF_ALLOC;
363 else if (flagId == "execinstr")
364 flags |= ELF::SHF_EXECINSTR;
365 else if (flagId == "write")
366 flags |= ELF::SHF_WRITE;
367 else if (flagId == "tls")
368 flags |= ELF::SHF_TLS;
369 else
370 return -1U;
371
372 Lex(); // Eat the flag.
373
374 if (!getLexer().is(AsmToken::Comma))
375 break;
376 Lex(); // Eat the comma.
377 }
378 return flags;
379}
380
381
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000382bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
383 getStreamer().PushSection();
384
Oliver Stannard8b273082014-06-19 15:52:37 +0000385 if (ParseSectionArguments(/*IsPush=*/true, loc)) {
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000386 getStreamer().PopSection();
387 return true;
388 }
389
390 return false;
391}
392
393bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
394 if (!getStreamer().PopSection())
395 return TokError(".popsection without corresponding .pushsection");
396 return false;
397}
398
Eli Friedman9e36dd02010-07-17 04:29:04 +0000399// FIXME: This is a work in progress.
Oliver Stannard8b273082014-06-19 15:52:37 +0000400bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
401 return ParseSectionArguments(/*IsPush=*/false, loc);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000402}
403
Oliver Stannard8b273082014-06-19 15:52:37 +0000404bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000405 StringRef SectionName;
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000406
407 if (ParseSectionName(SectionName))
Eli Friedman9e36dd02010-07-17 04:29:04 +0000408 return TokError("expected identifier in directive");
409
Eli Friedman9e36dd02010-07-17 04:29:04 +0000410 StringRef TypeName;
411 int64_t Size = 0;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000412 StringRef GroupName;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000413 unsigned Flags = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000414 const MCExpr *Subsection = nullptr;
David Majnemera4b521b2013-09-15 19:24:16 +0000415 bool UseLastGroup = false;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000416
417 // Set the defaults first.
418 if (SectionName == ".fini" || SectionName == ".init" ||
419 SectionName == ".rodata")
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000420 Flags |= ELF::SHF_ALLOC;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000421 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000422 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000423
Eli Friedman9e36dd02010-07-17 04:29:04 +0000424 if (getLexer().is(AsmToken::Comma)) {
425 Lex();
426
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000427 if (IsPush && getLexer().isNot(AsmToken::String)) {
428 if (getParser().parseExpression(Subsection))
429 return true;
430 if (getLexer().isNot(AsmToken::Comma))
431 goto EndStmt;
432 Lex();
433 }
Eli Friedman9e36dd02010-07-17 04:29:04 +0000434
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000435 unsigned extraFlags;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000436
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000437 if (getLexer().isNot(AsmToken::String)) {
438 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
439 || getLexer().isNot(AsmToken::Hash))
440 return TokError("expected string in directive");
441 extraFlags = parseSunStyleSectionFlags();
442 } else {
443 StringRef FlagsStr = getTok().getStringContents();
444 Lex();
445 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
446 }
447
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000448 if (extraFlags == -1U)
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000449 return TokError("unknown flag");
450 Flags |= extraFlags;
451
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000452 bool Mergeable = Flags & ELF::SHF_MERGE;
453 bool Group = Flags & ELF::SHF_GROUP;
David Majnemera4b521b2013-09-15 19:24:16 +0000454 if (Group && UseLastGroup)
455 return TokError("Section cannot specifiy a group name while also acting "
456 "as a member of the last group");
Eli Friedman9e36dd02010-07-17 04:29:04 +0000457
Rafael Espindola8aefb662010-10-28 21:33:33 +0000458 if (getLexer().isNot(AsmToken::Comma)) {
459 if (Mergeable)
460 return TokError("Mergeable section must specify the type");
461 if (Group)
462 return TokError("Group section must specify the type");
463 } else {
464 Lex();
David Majnemerf90c3b52013-09-21 05:25:12 +0000465 if (getLexer().is(AsmToken::At) || getLexer().is(AsmToken::Percent) ||
466 getLexer().is(AsmToken::String)) {
467 if (!getLexer().is(AsmToken::String))
468 Lex();
469 } else
470 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
Rafael Espindola8aefb662010-10-28 21:33:33 +0000471
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000472 if (getParser().parseIdentifier(TypeName))
Rafael Espindola8aefb662010-10-28 21:33:33 +0000473 return TokError("expected identifier in directive");
474
475 if (Mergeable) {
476 if (getLexer().isNot(AsmToken::Comma))
477 return TokError("expected the entry size");
478 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000479 if (getParser().parseAbsoluteExpression(Size))
Rafael Espindola8aefb662010-10-28 21:33:33 +0000480 return true;
481 if (Size <= 0)
482 return TokError("entry size must be positive");
483 }
484
485 if (Group) {
486 if (getLexer().isNot(AsmToken::Comma))
487 return TokError("expected group name");
488 Lex();
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000489 if (getParser().parseIdentifier(GroupName))
Rafael Espindola8aefb662010-10-28 21:33:33 +0000490 return true;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000491 if (getLexer().is(AsmToken::Comma)) {
492 Lex();
Rafael Espindola8aefb662010-10-28 21:33:33 +0000493 StringRef Linkage;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000494 if (getParser().parseIdentifier(Linkage))
Eli Friedman9e36dd02010-07-17 04:29:04 +0000495 return true;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000496 if (Linkage != "comdat")
497 return TokError("Linkage must be 'comdat'");
Eli Friedman9e36dd02010-07-17 04:29:04 +0000498 }
499 }
500 }
501 }
502
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000503EndStmt:
Eli Friedman9e36dd02010-07-17 04:29:04 +0000504 if (getLexer().isNot(AsmToken::EndOfStatement))
505 return TokError("unexpected token in directive");
506
Rafael Espindolaaea49582011-01-23 04:28:49 +0000507 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000508
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000509 if (TypeName.empty()) {
510 if (SectionName.startswith(".note"))
511 Type = ELF::SHT_NOTE;
512 else if (SectionName == ".init_array")
513 Type = ELF::SHT_INIT_ARRAY;
514 else if (SectionName == ".fini_array")
515 Type = ELF::SHT_FINI_ARRAY;
516 else if (SectionName == ".preinit_array")
517 Type = ELF::SHT_PREINIT_ARRAY;
518 } else {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000519 if (TypeName == "init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000520 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000521 else if (TypeName == "fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000522 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000523 else if (TypeName == "preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000524 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000525 else if (TypeName == "nobits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000526 Type = ELF::SHT_NOBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000527 else if (TypeName == "progbits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000528 Type = ELF::SHT_PROGBITS;
Rafael Espindola9ae2d052010-12-26 21:30:59 +0000529 else if (TypeName == "note")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000530 Type = ELF::SHT_NOTE;
Rafael Espindola4b7b7fb2011-01-23 05:43:40 +0000531 else if (TypeName == "unwind")
532 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000533 else
534 return TokError("unknown section type");
535 }
536
David Majnemera4b521b2013-09-15 19:24:16 +0000537 if (UseLastGroup) {
538 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
539 if (const MCSectionELF *Section =
540 cast_or_null<MCSectionELF>(CurrentSection.first))
541 if (const MCSymbol *Group = Section->getGroup()) {
542 GroupName = Group->getName();
543 Flags |= ELF::SHF_GROUP;
544 }
545 }
546
Rafael Espindolac44c26b2014-03-28 19:14:08 +0000547 SectionKind Kind = computeSectionKind(Flags, Size);
Oliver Stannard8b273082014-06-19 15:52:37 +0000548 const MCSection *ELFSection = getContext().getELFSection(
549 SectionName, Type, Flags, Kind, Size, GroupName);
550 getStreamer().SwitchSection(ELFSection, Subsection);
551
552 if (getContext().getGenDwarfForAssembly()) {
553 auto &Sections = getContext().getGenDwarfSectionSyms();
554 auto InsertResult = Sections.insert(
555 std::make_pair(ELFSection, std::make_pair(nullptr, nullptr)));
556 if (InsertResult.second) {
557 if (getContext().getDwarfVersion() <= 2)
Oliver Stannard14f97d02014-09-22 10:45:16 +0000558 Warning(loc, "DWARF2 only supports one section per compilation unit");
Oliver Stannard8b273082014-06-19 15:52:37 +0000559
560 MCSymbol *SectionStartSymbol = getContext().CreateTempSymbol();
561 getStreamer().EmitLabel(SectionStartSymbol);
562 InsertResult.first->second.first = SectionStartSymbol;
563 }
564 }
565
Eli Friedman9e36dd02010-07-17 04:29:04 +0000566 return false;
567}
568
Benjamin Kramere39017c2010-09-02 18:53:37 +0000569bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000570 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
Craig Topper353eda42014-04-24 06:44:33 +0000571 if (PreviousSection.first == nullptr)
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000572 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000573 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Benjamin Kramere39017c2010-09-02 18:53:37 +0000574
575 return false;
576}
577
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000578static MCSymbolAttr MCAttrForString(StringRef Type) {
579 return StringSwitch<MCSymbolAttr>(Type)
580 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
581 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
582 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
583 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
584 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
585 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
586 MCSA_ELF_TypeIndFunction)
587 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
588 .Default(MCSA_Invalid);
589}
590
Michael J. Spencer3d898232010-10-09 03:47:55 +0000591/// ParseDirectiveELFType
David Majnemerf90c3b52013-09-21 05:25:12 +0000592/// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
593/// ::= .type identifier , #attribute
Michael J. Spencer3d898232010-10-09 03:47:55 +0000594/// ::= .type identifier , @attribute
David Majnemerf90c3b52013-09-21 05:25:12 +0000595/// ::= .type identifier , %attribute
596/// ::= .type identifier , "attribute"
Michael J. Spencer3d898232010-10-09 03:47:55 +0000597bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
598 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000599 if (getParser().parseIdentifier(Name))
Michael J. Spencer3d898232010-10-09 03:47:55 +0000600 return TokError("expected identifier in directive");
601
602 // Handle the identifier as the key symbol.
603 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
604
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000605 // NOTE the comma is optional in all cases. It is only documented as being
606 // optional in the first case, however, GAS will silently treat the comma as
607 // optional in all cases. Furthermore, although the documentation states that
608 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
609 // accepts both the upper case name as well as the lower case aliases.
610 if (getLexer().is(AsmToken::Comma))
611 Lex();
Michael J. Spencer3d898232010-10-09 03:47:55 +0000612
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000613 if (getLexer().isNot(AsmToken::Identifier) &&
614 getLexer().isNot(AsmToken::Hash) && getLexer().isNot(AsmToken::At) &&
615 getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::String))
David Majnemerf90c3b52013-09-21 05:25:12 +0000616 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
617 "'%<type>' or \"<type>\"");
Michael J. Spencer3d898232010-10-09 03:47:55 +0000618
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000619 if (getLexer().isNot(AsmToken::String) &&
620 getLexer().isNot(AsmToken::Identifier))
621 Lex();
622
623 SMLoc TypeLoc = getLexer().getLoc();
624
625 StringRef Type;
626 if (getParser().parseIdentifier(Type))
627 return TokError("expected symbol type in directive");
628
629 MCSymbolAttr Attr = MCAttrForString(Type);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000630 if (Attr == MCSA_Invalid)
631 return Error(TypeLoc, "unsupported attribute in '.type' directive");
632
633 if (getLexer().isNot(AsmToken::EndOfStatement))
634 return TokError("unexpected token in '.type' directive");
Michael J. Spencer3d898232010-10-09 03:47:55 +0000635 Lex();
636
637 getStreamer().EmitSymbolAttribute(Sym, Attr);
638
639 return false;
640}
641
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000642/// ParseDirectiveIdent
643/// ::= .ident string
644bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
645 if (getLexer().isNot(AsmToken::String))
646 return TokError("unexpected token in '.ident' directive");
647
648 StringRef Data = getTok().getIdentifier();
649
650 Lex();
651
Rafael Espindola5645bad2013-10-16 01:05:45 +0000652 getStreamer().EmitIdent(Data);
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000653 return false;
654}
655
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000656/// ParseDirectiveSymver
657/// ::= .symver foo, bar2@zed
658bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
659 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000660 if (getParser().parseIdentifier(Name))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000661 return TokError("expected identifier in directive");
662
663 if (getLexer().isNot(AsmToken::Comma))
664 return TokError("expected a comma");
665
David Peixottoc0f92a22014-01-15 22:40:02 +0000666 // ARM assembly uses @ for a comment...
667 // except when parsing the second parameter of the .symver directive.
668 // Force the next symbol to allow @ in the identifier, which is
669 // required for this directive and then reset it to its initial state.
670 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
671 getLexer().setAllowAtInIdentifier(true);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000672 Lex();
David Peixottoc0f92a22014-01-15 22:40:02 +0000673 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000674
675 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000676 if (getParser().parseIdentifier(AliasName))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000677 return TokError("expected identifier in directive");
678
679 if (AliasName.find('@') == StringRef::npos)
680 return TokError("expected a '@' in the name");
681
682 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
683 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
684 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
685
686 getStreamer().EmitAssignment(Alias, Value);
687 return false;
688}
689
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000690/// ParseDirectiveVersion
691/// ::= .version string
692bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
693 if (getLexer().isNot(AsmToken::String))
694 return TokError("unexpected token in '.version' directive");
695
696 StringRef Data = getTok().getIdentifier();
697
698 Lex();
699
700 const MCSection *Note =
701 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
702 SectionKind::getReadOnly());
703
704 getStreamer().PushSection();
705 getStreamer().SwitchSection(Note);
706 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
707 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
708 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000709 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000710 getStreamer().EmitIntValue(0, 1); // terminate the string.
711 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
712 getStreamer().PopSection();
713 return false;
714}
715
Rafael Espindola16145972010-11-01 14:28:48 +0000716/// ParseDirectiveWeakref
717/// ::= .weakref foo, bar
718bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
719 // FIXME: Share code with the other alias building directives.
720
721 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000722 if (getParser().parseIdentifier(AliasName))
Rafael Espindola16145972010-11-01 14:28:48 +0000723 return TokError("expected identifier in directive");
724
725 if (getLexer().isNot(AsmToken::Comma))
726 return TokError("expected a comma");
727
728 Lex();
729
730 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000731 if (getParser().parseIdentifier(Name))
Rafael Espindola16145972010-11-01 14:28:48 +0000732 return TokError("expected identifier in directive");
733
734 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
735
736 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
737
738 getStreamer().EmitWeakReference(Alias, Sym);
739 return false;
740}
741
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000742bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
Craig Topper353eda42014-04-24 06:44:33 +0000743 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000744 if (getLexer().isNot(AsmToken::EndOfStatement)) {
745 if (getParser().parseExpression(Subsection))
746 return true;
747 }
748
749 if (getLexer().isNot(AsmToken::EndOfStatement))
750 return TokError("unexpected token in directive");
751
752 getStreamer().SubSection(Subsection);
753 return false;
754}
755
Daniel Dunbarab058b82010-07-12 21:23:32 +0000756namespace llvm {
757
758MCAsmParserExtension *createELFAsmParser() {
759 return new ELFAsmParser;
760}
761
762}