blob: 3bee12f68248177c23381b2047a666a635353cc2 [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
Michael J. Spencer3d898232010-10-09 03:47:55 +000010#include "llvm/ADT/StringSwitch.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000011#include "llvm/ADT/StringRef.h"
Eli Friedman9e36dd02010-07-17 04:29:04 +000012#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbarab058b82010-07-12 21:23:32 +000013#include "llvm/MC/MCContext.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000014#include "llvm/MC/MCDirectives.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"
Eugene Zelenko1d435522017-02-07 23:02:00 +000017#include "llvm/MC/MCParser/MCAsmParser.h"
18#include "llvm/MC/MCParser/MCAsmParserExtension.h"
19#include "llvm/MC/MCSection.h"
Eli Friedman9e36dd02010-07-17 04:29:04 +000020#include "llvm/MC/MCSectionELF.h"
21#include "llvm/MC/MCStreamer.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000022#include "llvm/MC/MCSymbol.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000023#include "llvm/MC/MCSymbolELF.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000024#include "llvm/MC/SectionKind.h"
25#include "llvm/Support/Casting.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000026#include "llvm/Support/ELF.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000027#include "llvm/Support/MathExtras.h"
28#include "llvm/Support/SMLoc.h"
29#include <cassert>
30#include <cstdint>
31#include <utility>
32
Daniel Dunbarab058b82010-07-12 21:23:32 +000033using namespace llvm;
34
35namespace {
36
37class ELFAsmParser : public MCAsmParserExtension {
Eli Bendersky29b9f472013-01-16 00:50:52 +000038 template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Jim Grosbachd2037eb2013-02-20 22:21:35 +000039 void addDirectiveHandler(StringRef Directive) {
Eli Bendersky29b9f472013-01-16 00:50:52 +000040 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
41 this, HandleDirective<ELFAsmParser, HandlerMethod>);
42
Jim Grosbachd2037eb2013-02-20 22:21:35 +000043 getParser().addDirectiveHandler(Directive, Handler);
Daniel Dunbar8897d472010-07-18 22:22:07 +000044 }
45
Rafael Espindola5645bad2013-10-16 01:05:45 +000046 bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
47 SectionKind Kind);
Daniel Dunbarab058b82010-07-12 21:23:32 +000048
49public:
Rafael Espindola5645bad2013-10-16 01:05:45 +000050 ELFAsmParser() { BracketExpressionsSupported = true; }
Daniel Dunbarab058b82010-07-12 21:23:32 +000051
Craig Topper59be68f2014-03-08 07:14:16 +000052 void Initialize(MCAsmParser &Parser) override {
Daniel Dunbarab058b82010-07-12 21:23:32 +000053 // Call the base implementation.
54 this->MCAsmParserExtension::Initialize(Parser);
55
Jim Grosbachd2037eb2013-02-20 22:21:35 +000056 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
57 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
58 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
59 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
60 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
61 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
62 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000063 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000064 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000065 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000066 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000067 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000068 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
69 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000070 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000071 addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
72 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
73 addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
74 addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
75 addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
76 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
77 addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
78 addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
79 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
80 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
81 addDirectiveHandler<
Jim Grosbach38b1ed82011-07-25 17:55:35 +000082 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000083 addDirectiveHandler<
Jim Grosbach38b1ed82011-07-25 17:55:35 +000084 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000085 addDirectiveHandler<
Jim Grosbach38b1ed82011-07-25 17:55:35 +000086 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
Peter Collingbourne2f495b92013-04-17 21:18:16 +000087 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
Daniel Dunbarab058b82010-07-12 21:23:32 +000088 }
89
Rafael Espindolaf667d922010-09-15 21:48:40 +000090 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
91 // the best way for us to get access to it?
Daniel Dunbarab058b82010-07-12 21:23:32 +000092 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +000093 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola449711c2015-11-18 06:02:15 +000094 ELF::SHF_WRITE | ELF::SHF_ALLOC,
95 SectionKind::getData());
Daniel Dunbarab058b82010-07-12 21:23:32 +000096 }
97 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +000098 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000099 ELF::SHF_EXECINSTR |
100 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbarab058b82010-07-12 21:23:32 +0000101 }
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000102 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000103 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000104 ELF::SHF_WRITE |
105 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000106 }
107 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000108 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000109 ELF::SHF_ALLOC,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000110 SectionKind::getReadOnly());
111 }
112 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000113 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000114 ELF::SHF_ALLOC |
115 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000116 SectionKind::getThreadData());
117 }
118 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000119 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000120 ELF::SHF_ALLOC |
121 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000122 SectionKind::getThreadBSS());
123 }
124 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000125 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola449711c2015-11-18 06:02:15 +0000126 ELF::SHF_ALLOC | ELF::SHF_WRITE,
127 SectionKind::getData());
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000128 }
129 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000130 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000131 ELF::SHF_ALLOC |
132 ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000133 SectionKind::getReadOnlyWithRel());
134 }
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000135 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000136 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola449711c2015-11-18 06:02:15 +0000137 ELF::SHF_ALLOC | ELF::SHF_WRITE,
138 SectionKind::getData());
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000139 }
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000140 bool ParseDirectivePushSection(StringRef, SMLoc);
141 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman9e36dd02010-07-17 04:29:04 +0000142 bool ParseDirectiveSection(StringRef, SMLoc);
143 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramere39017c2010-09-02 18:53:37 +0000144 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000145 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000146 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000147 bool ParseDirectiveSymver(StringRef, SMLoc);
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000148 bool ParseDirectiveVersion(StringRef, SMLoc);
Rafael Espindola16145972010-11-01 14:28:48 +0000149 bool ParseDirectiveWeakref(StringRef, SMLoc);
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000150 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000151 bool ParseDirectiveSubsection(StringRef, SMLoc);
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000152
153private:
154 bool ParseSectionName(StringRef &SectionName);
Oliver Stannard8b273082014-06-19 15:52:37 +0000155 bool ParseSectionArguments(bool IsPush, SMLoc loc);
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000156 unsigned parseSunStyleSectionFlags();
Rafael Espindolad9953d92017-01-31 23:07:08 +0000157 bool maybeParseSectionType(StringRef &TypeName);
Rafael Espindolaa86be222017-01-31 23:26:32 +0000158 bool parseMergeSize(int64_t &Size);
159 bool parseGroup(StringRef &GroupName);
160 bool maybeParseUniqueID(int64_t &UniqueID);
Daniel Dunbarab058b82010-07-12 21:23:32 +0000161};
162
Eugene Zelenko1d435522017-02-07 23:02:00 +0000163} // end anonymous namespace
Daniel Dunbarab058b82010-07-12 21:23:32 +0000164
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000165/// ParseDirectiveSymbolAttribute
166/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
167bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
168 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
169 .Case(".weak", MCSA_Weak)
170 .Case(".local", MCSA_Local)
171 .Case(".hidden", MCSA_Hidden)
172 .Case(".internal", MCSA_Internal)
173 .Case(".protected", MCSA_Protected)
174 .Default(MCSA_Invalid);
175 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
176 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Eugene Zelenko1d435522017-02-07 23:02:00 +0000177 while (true) {
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000178 StringRef Name;
179
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000180 if (getParser().parseIdentifier(Name))
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000181 return TokError("expected identifier in directive");
182
Jim Grosbach6f482002015-05-18 18:43:14 +0000183 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000184
185 getStreamer().EmitSymbolAttribute(Sym, Attr);
186
187 if (getLexer().is(AsmToken::EndOfStatement))
188 break;
189
190 if (getLexer().isNot(AsmToken::Comma))
191 return TokError("unexpected token in directive");
192 Lex();
193 }
194 }
195
196 Lex();
197 return false;
198}
199
Daniel Dunbarab058b82010-07-12 21:23:32 +0000200bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
201 unsigned Flags, SectionKind Kind) {
Craig Topper353eda42014-04-24 06:44:33 +0000202 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000203 if (getLexer().isNot(AsmToken::EndOfStatement)) {
204 if (getParser().parseExpression(Subsection))
205 return true;
206 }
Nirav Dave53a72f42016-07-11 12:42:14 +0000207 Lex();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000208
Rafael Espindolaba31e272015-01-29 17:33:21 +0000209 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000210 Subsection);
Daniel Dunbarab058b82010-07-12 21:23:32 +0000211
212 return false;
213}
214
Eli Friedman9e36dd02010-07-17 04:29:04 +0000215bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedman56178a02010-07-17 03:09:18 +0000216 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000217 if (getParser().parseIdentifier(Name))
Eli Friedman56178a02010-07-17 03:09:18 +0000218 return TokError("expected identifier in directive");
Rafael Espindolaa8695762015-06-02 00:25:12 +0000219 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
Eli Friedman56178a02010-07-17 03:09:18 +0000220
221 if (getLexer().isNot(AsmToken::Comma))
222 return TokError("unexpected token in directive");
223 Lex();
224
225 const MCExpr *Expr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000226 if (getParser().parseExpression(Expr))
Eli Friedman56178a02010-07-17 03:09:18 +0000227 return true;
228
229 if (getLexer().isNot(AsmToken::EndOfStatement))
230 return TokError("unexpected token in directive");
Nirav Davea645433c2016-07-18 15:24:03 +0000231 Lex();
Eli Friedman56178a02010-07-17 03:09:18 +0000232
Rafael Espindolaa8695762015-06-02 00:25:12 +0000233 getStreamer().emitELFSize(Sym, Expr);
Eli Friedman56178a02010-07-17 03:09:18 +0000234 return false;
235}
236
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000237bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
238 // A section name can contain -, so we cannot just use
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000239 // parseIdentifier.
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000240 SMLoc FirstLoc = getLexer().getLoc();
241 unsigned Size = 0;
242
Rafael Espindola689939e2011-01-24 18:02:54 +0000243 if (getLexer().is(AsmToken::String)) {
244 SectionName = getTok().getIdentifier();
245 Lex();
246 return false;
247 }
248
Eugene Zelenko1d435522017-02-07 23:02:00 +0000249 while (true) {
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000250 SMLoc PrevLoc = getLexer().getLoc();
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000251 if (getLexer().is(AsmToken::Comma) ||
252 getLexer().is(AsmToken::EndOfStatement))
253 break;
254
255 unsigned CurSize;
256 if (getLexer().is(AsmToken::String)) {
Rafael Espindola689939e2011-01-24 18:02:54 +0000257 CurSize = getTok().getIdentifier().size() + 2;
258 Lex();
259 } else if (getLexer().is(AsmToken::Identifier)) {
260 CurSize = getTok().getIdentifier().size();
261 Lex();
262 } else {
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000263 CurSize = getTok().getString().size();
264 Lex();
Rafael Espindola689939e2011-01-24 18:02:54 +0000265 }
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000266 Size += CurSize;
267 SectionName = StringRef(FirstLoc.getPointer(), Size);
268
269 // Make sure the following token is adjacent.
270 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
271 break;
272 }
273 if (Size == 0)
274 return true;
275
276 return false;
277}
278
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000279static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
280 unsigned flags = 0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000281
Prakhar Bahugunae640c6f2016-12-15 07:59:15 +0000282 // If a valid numerical value is set for the section flag, use it verbatim
283 if (!flagsStr.getAsInteger(0, flags))
284 return flags;
285
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000286 for (char i : flagsStr) {
287 switch (i) {
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000288 case 'a':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000289 flags |= ELF::SHF_ALLOC;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000290 break;
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000291 case 'e':
292 flags |= ELF::SHF_EXCLUDE;
293 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000294 case 'x':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000295 flags |= ELF::SHF_EXECINSTR;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000296 break;
297 case 'w':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000298 flags |= ELF::SHF_WRITE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000299 break;
300 case 'M':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000301 flags |= ELF::SHF_MERGE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000302 break;
303 case 'S':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000304 flags |= ELF::SHF_STRINGS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000305 break;
306 case 'T':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000307 flags |= ELF::SHF_TLS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000308 break;
309 case 'c':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000310 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000311 break;
312 case 'd':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000313 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000314 break;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000315 case 'y':
316 flags |= ELF::SHF_ARM_PURECODE;
317 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000318 case 'G':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000319 flags |= ELF::SHF_GROUP;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000320 break;
David Majnemera4b521b2013-09-15 19:24:16 +0000321 case '?':
322 *UseLastGroup = true;
323 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000324 default:
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000325 return -1U;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000326 }
327 }
328
329 return flags;
330}
331
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000332unsigned ELFAsmParser::parseSunStyleSectionFlags() {
333 unsigned flags = 0;
334 while (getLexer().is(AsmToken::Hash)) {
335 Lex(); // Eat the #.
336
337 if (!getLexer().is(AsmToken::Identifier))
338 return -1U;
339
340 StringRef flagId = getTok().getIdentifier();
341 if (flagId == "alloc")
342 flags |= ELF::SHF_ALLOC;
343 else if (flagId == "execinstr")
344 flags |= ELF::SHF_EXECINSTR;
345 else if (flagId == "write")
346 flags |= ELF::SHF_WRITE;
347 else if (flagId == "tls")
348 flags |= ELF::SHF_TLS;
349 else
350 return -1U;
351
352 Lex(); // Eat the flag.
353
354 if (!getLexer().is(AsmToken::Comma))
355 break;
356 Lex(); // Eat the comma.
357 }
358 return flags;
359}
360
361
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000362bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
363 getStreamer().PushSection();
364
Oliver Stannard8b273082014-06-19 15:52:37 +0000365 if (ParseSectionArguments(/*IsPush=*/true, loc)) {
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000366 getStreamer().PopSection();
367 return true;
368 }
369
370 return false;
371}
372
373bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
374 if (!getStreamer().PopSection())
375 return TokError(".popsection without corresponding .pushsection");
376 return false;
377}
378
Eli Friedman9e36dd02010-07-17 04:29:04 +0000379// FIXME: This is a work in progress.
Oliver Stannard8b273082014-06-19 15:52:37 +0000380bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
381 return ParseSectionArguments(/*IsPush=*/false, loc);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000382}
383
Rafael Espindolad9953d92017-01-31 23:07:08 +0000384bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
385 MCAsmLexer &L = getLexer();
386 if (L.isNot(AsmToken::Comma))
387 return false;
388 Lex();
389 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
390 L.isNot(AsmToken::String))
391 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
392 if (!L.is(AsmToken::String))
393 Lex();
394 if (getParser().parseIdentifier(TypeName))
395 return TokError("expected identifier in directive");
396 return false;
397}
398
Rafael Espindolaa86be222017-01-31 23:26:32 +0000399bool ELFAsmParser::parseMergeSize(int64_t &Size) {
400 if (getLexer().isNot(AsmToken::Comma))
401 return TokError("expected the entry size");
402 Lex();
403 if (getParser().parseAbsoluteExpression(Size))
404 return true;
405 if (Size <= 0)
406 return TokError("entry size must be positive");
407 return false;
408}
409
410bool ELFAsmParser::parseGroup(StringRef &GroupName) {
411 MCAsmLexer &L = getLexer();
412 if (L.isNot(AsmToken::Comma))
413 return TokError("expected group name");
414 Lex();
415 if (getParser().parseIdentifier(GroupName))
416 return true;
417 if (L.is(AsmToken::Comma)) {
418 Lex();
419 StringRef Linkage;
420 if (getParser().parseIdentifier(Linkage))
421 return true;
422 if (Linkage != "comdat")
423 return TokError("Linkage must be 'comdat'");
424 }
425 return false;
426}
427
428bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
429 MCAsmLexer &L = getLexer();
430 if (L.isNot(AsmToken::Comma))
431 return false;
432 Lex();
433 StringRef UniqueStr;
434 if (getParser().parseIdentifier(UniqueStr))
435 return TokError("expected identifier in directive");
436 if (UniqueStr != "unique")
437 return TokError("expected 'unique'");
438 if (L.isNot(AsmToken::Comma))
439 return TokError("expected commma");
440 Lex();
441 if (getParser().parseAbsoluteExpression(UniqueID))
442 return true;
443 if (UniqueID < 0)
444 return TokError("unique id must be positive");
445 if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
446 return TokError("unique id is too large");
447 return false;
448}
449
Oliver Stannard8b273082014-06-19 15:52:37 +0000450bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000451 StringRef SectionName;
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000452
453 if (ParseSectionName(SectionName))
Eli Friedman9e36dd02010-07-17 04:29:04 +0000454 return TokError("expected identifier in directive");
455
Eli Friedman9e36dd02010-07-17 04:29:04 +0000456 StringRef TypeName;
457 int64_t Size = 0;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000458 StringRef GroupName;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000459 unsigned Flags = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000460 const MCExpr *Subsection = nullptr;
David Majnemera4b521b2013-09-15 19:24:16 +0000461 bool UseLastGroup = false;
Rafael Espindola68fa2492015-02-17 20:48:01 +0000462 StringRef UniqueStr;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000463 int64_t UniqueID = ~0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000464
465 // Set the defaults first.
466 if (SectionName == ".fini" || SectionName == ".init" ||
467 SectionName == ".rodata")
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000468 Flags |= ELF::SHF_ALLOC;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000469 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000470 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000471
Eli Friedman9e36dd02010-07-17 04:29:04 +0000472 if (getLexer().is(AsmToken::Comma)) {
473 Lex();
474
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000475 if (IsPush && getLexer().isNot(AsmToken::String)) {
476 if (getParser().parseExpression(Subsection))
477 return true;
478 if (getLexer().isNot(AsmToken::Comma))
479 goto EndStmt;
480 Lex();
481 }
Eli Friedman9e36dd02010-07-17 04:29:04 +0000482
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000483 unsigned extraFlags;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000484
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000485 if (getLexer().isNot(AsmToken::String)) {
486 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
487 || getLexer().isNot(AsmToken::Hash))
488 return TokError("expected string in directive");
489 extraFlags = parseSunStyleSectionFlags();
490 } else {
491 StringRef FlagsStr = getTok().getStringContents();
492 Lex();
493 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
494 }
495
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000496 if (extraFlags == -1U)
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000497 return TokError("unknown flag");
498 Flags |= extraFlags;
499
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000500 bool Mergeable = Flags & ELF::SHF_MERGE;
501 bool Group = Flags & ELF::SHF_GROUP;
David Majnemera4b521b2013-09-15 19:24:16 +0000502 if (Group && UseLastGroup)
503 return TokError("Section cannot specifiy a group name while also acting "
504 "as a member of the last group");
Eli Friedman9e36dd02010-07-17 04:29:04 +0000505
Rafael Espindolad9953d92017-01-31 23:07:08 +0000506 if (maybeParseSectionType(TypeName))
507 return true;
508
509 MCAsmLexer &L = getLexer();
510 if (TypeName.empty()) {
Rafael Espindola8aefb662010-10-28 21:33:33 +0000511 if (Mergeable)
512 return TokError("Mergeable section must specify the type");
513 if (Group)
514 return TokError("Group section must specify the type");
Rafael Espindolad9953d92017-01-31 23:07:08 +0000515 if (L.isNot(AsmToken::EndOfStatement))
516 return TokError("unexpected token in directive");
517 }
518
Rafael Espindolaa86be222017-01-31 23:26:32 +0000519 if (Mergeable)
520 if (parseMergeSize(Size))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000521 return true;
Rafael Espindolaa86be222017-01-31 23:26:32 +0000522 if (Group)
523 if (parseGroup(GroupName))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000524 return true;
Rafael Espindolaa86be222017-01-31 23:26:32 +0000525 if (maybeParseUniqueID(UniqueID))
526 return true;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000527 }
528
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000529EndStmt:
Eli Friedman9e36dd02010-07-17 04:29:04 +0000530 if (getLexer().isNot(AsmToken::EndOfStatement))
531 return TokError("unexpected token in directive");
Nirav Davea645433c2016-07-18 15:24:03 +0000532 Lex();
Eli Friedman9e36dd02010-07-17 04:29:04 +0000533
Rafael Espindolaaea49582011-01-23 04:28:49 +0000534 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000535
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000536 if (TypeName.empty()) {
537 if (SectionName.startswith(".note"))
538 Type = ELF::SHT_NOTE;
539 else if (SectionName == ".init_array")
540 Type = ELF::SHT_INIT_ARRAY;
541 else if (SectionName == ".fini_array")
542 Type = ELF::SHT_FINI_ARRAY;
543 else if (SectionName == ".preinit_array")
544 Type = ELF::SHT_PREINIT_ARRAY;
545 } else {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000546 if (TypeName == "init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000547 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000548 else if (TypeName == "fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000549 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000550 else if (TypeName == "preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000551 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000552 else if (TypeName == "nobits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000553 Type = ELF::SHT_NOBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000554 else if (TypeName == "progbits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000555 Type = ELF::SHT_PROGBITS;
Rafael Espindola9ae2d052010-12-26 21:30:59 +0000556 else if (TypeName == "note")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000557 Type = ELF::SHT_NOTE;
Rafael Espindola4b7b7fb2011-01-23 05:43:40 +0000558 else if (TypeName == "unwind")
559 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000560 else
561 return TokError("unknown section type");
562 }
563
David Majnemera4b521b2013-09-15 19:24:16 +0000564 if (UseLastGroup) {
565 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
566 if (const MCSectionELF *Section =
567 cast_or_null<MCSectionELF>(CurrentSection.first))
568 if (const MCSymbol *Group = Section->getGroup()) {
569 GroupName = Group->getName();
570 Flags |= ELF::SHF_GROUP;
571 }
572 }
573
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000574 MCSection *ELFSection = getContext().getELFSection(SectionName, Type, Flags,
575 Size, GroupName, UniqueID);
Oliver Stannard8b273082014-06-19 15:52:37 +0000576 getStreamer().SwitchSection(ELFSection, Subsection);
577
578 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindolae0746792015-05-21 16:52:32 +0000579 bool InsertResult = getContext().addGenDwarfSection(ELFSection);
580 if (InsertResult) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000581 if (getContext().getDwarfVersion() <= 2)
Oliver Stannard14f97d02014-09-22 10:45:16 +0000582 Warning(loc, "DWARF2 only supports one section per compilation unit");
Oliver Stannard8b273082014-06-19 15:52:37 +0000583
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000584 if (!ELFSection->getBeginSymbol()) {
585 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
586 getStreamer().EmitLabel(SectionStartSymbol);
587 ELFSection->setBeginSymbol(SectionStartSymbol);
588 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000589 }
590 }
591
Eli Friedman9e36dd02010-07-17 04:29:04 +0000592 return false;
593}
594
Benjamin Kramere39017c2010-09-02 18:53:37 +0000595bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000596 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
Craig Topper353eda42014-04-24 06:44:33 +0000597 if (PreviousSection.first == nullptr)
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000598 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000599 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Benjamin Kramere39017c2010-09-02 18:53:37 +0000600
601 return false;
602}
603
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000604static MCSymbolAttr MCAttrForString(StringRef Type) {
605 return StringSwitch<MCSymbolAttr>(Type)
606 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
607 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
608 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
609 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
610 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
611 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
612 MCSA_ELF_TypeIndFunction)
613 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
614 .Default(MCSA_Invalid);
615}
616
Michael J. Spencer3d898232010-10-09 03:47:55 +0000617/// ParseDirectiveELFType
David Majnemerf90c3b52013-09-21 05:25:12 +0000618/// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
619/// ::= .type identifier , #attribute
Michael J. Spencer3d898232010-10-09 03:47:55 +0000620/// ::= .type identifier , @attribute
David Majnemerf90c3b52013-09-21 05:25:12 +0000621/// ::= .type identifier , %attribute
622/// ::= .type identifier , "attribute"
Michael J. Spencer3d898232010-10-09 03:47:55 +0000623bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
624 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000625 if (getParser().parseIdentifier(Name))
Michael J. Spencer3d898232010-10-09 03:47:55 +0000626 return TokError("expected identifier in directive");
627
628 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000629 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000630
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000631 // NOTE the comma is optional in all cases. It is only documented as being
632 // optional in the first case, however, GAS will silently treat the comma as
633 // optional in all cases. Furthermore, although the documentation states that
634 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
635 // accepts both the upper case name as well as the lower case aliases.
636 if (getLexer().is(AsmToken::Comma))
637 Lex();
Michael J. Spencer3d898232010-10-09 03:47:55 +0000638
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000639 if (getLexer().isNot(AsmToken::Identifier) &&
Gabor Ballabasaf06a882015-07-01 08:58:49 +0000640 getLexer().isNot(AsmToken::Hash) &&
641 getLexer().isNot(AsmToken::Percent) &&
642 getLexer().isNot(AsmToken::String)) {
643 if (!getLexer().getAllowAtInIdentifier())
644 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
645 "'%<type>' or \"<type>\"");
646 else if (getLexer().isNot(AsmToken::At))
647 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
648 "'%<type>' or \"<type>\"");
649 }
Michael J. Spencer3d898232010-10-09 03:47:55 +0000650
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000651 if (getLexer().isNot(AsmToken::String) &&
652 getLexer().isNot(AsmToken::Identifier))
653 Lex();
654
655 SMLoc TypeLoc = getLexer().getLoc();
656
657 StringRef Type;
658 if (getParser().parseIdentifier(Type))
659 return TokError("expected symbol type in directive");
660
661 MCSymbolAttr Attr = MCAttrForString(Type);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000662 if (Attr == MCSA_Invalid)
663 return Error(TypeLoc, "unsupported attribute in '.type' directive");
664
665 if (getLexer().isNot(AsmToken::EndOfStatement))
666 return TokError("unexpected token in '.type' directive");
Michael J. Spencer3d898232010-10-09 03:47:55 +0000667 Lex();
668
669 getStreamer().EmitSymbolAttribute(Sym, Attr);
670
671 return false;
672}
673
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000674/// ParseDirectiveIdent
675/// ::= .ident string
676bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
677 if (getLexer().isNot(AsmToken::String))
678 return TokError("unexpected token in '.ident' directive");
679
680 StringRef Data = getTok().getIdentifier();
681
682 Lex();
683
Nirav Davea645433c2016-07-18 15:24:03 +0000684 if (getLexer().isNot(AsmToken::EndOfStatement))
685 return TokError("unexpected token in '.ident' directive");
686 Lex();
687
Rafael Espindola5645bad2013-10-16 01:05:45 +0000688 getStreamer().EmitIdent(Data);
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000689 return false;
690}
691
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000692/// ParseDirectiveSymver
693/// ::= .symver foo, bar2@zed
694bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
695 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000696 if (getParser().parseIdentifier(Name))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000697 return TokError("expected identifier in directive");
698
699 if (getLexer().isNot(AsmToken::Comma))
700 return TokError("expected a comma");
701
David Peixottoc0f92a22014-01-15 22:40:02 +0000702 // ARM assembly uses @ for a comment...
703 // except when parsing the second parameter of the .symver directive.
704 // Force the next symbol to allow @ in the identifier, which is
705 // required for this directive and then reset it to its initial state.
706 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
707 getLexer().setAllowAtInIdentifier(true);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000708 Lex();
David Peixottoc0f92a22014-01-15 22:40:02 +0000709 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000710
711 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000712 if (getParser().parseIdentifier(AliasName))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000713 return TokError("expected identifier in directive");
714
715 if (AliasName.find('@') == StringRef::npos)
716 return TokError("expected a '@' in the name");
717
Jim Grosbach6f482002015-05-18 18:43:14 +0000718 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
719 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000720 const MCExpr *Value = MCSymbolRefExpr::create(Sym, getContext());
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000721
722 getStreamer().EmitAssignment(Alias, Value);
723 return false;
724}
725
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000726/// ParseDirectiveVersion
727/// ::= .version string
728bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
729 if (getLexer().isNot(AsmToken::String))
730 return TokError("unexpected token in '.version' directive");
731
732 StringRef Data = getTok().getIdentifier();
733
734 Lex();
735
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000736 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000737
738 getStreamer().PushSection();
739 getStreamer().SwitchSection(Note);
740 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
741 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
742 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000743 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000744 getStreamer().EmitIntValue(0, 1); // terminate the string.
745 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
746 getStreamer().PopSection();
747 return false;
748}
749
Rafael Espindola16145972010-11-01 14:28:48 +0000750/// ParseDirectiveWeakref
751/// ::= .weakref foo, bar
752bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
753 // FIXME: Share code with the other alias building directives.
754
755 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000756 if (getParser().parseIdentifier(AliasName))
Rafael Espindola16145972010-11-01 14:28:48 +0000757 return TokError("expected identifier in directive");
758
759 if (getLexer().isNot(AsmToken::Comma))
760 return TokError("expected a comma");
761
762 Lex();
763
764 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000765 if (getParser().parseIdentifier(Name))
Rafael Espindola16145972010-11-01 14:28:48 +0000766 return TokError("expected identifier in directive");
767
Jim Grosbach6f482002015-05-18 18:43:14 +0000768 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
Rafael Espindola16145972010-11-01 14:28:48 +0000769
Jim Grosbach6f482002015-05-18 18:43:14 +0000770 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Rafael Espindola16145972010-11-01 14:28:48 +0000771
772 getStreamer().EmitWeakReference(Alias, Sym);
773 return false;
774}
775
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000776bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
Craig Topper353eda42014-04-24 06:44:33 +0000777 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000778 if (getLexer().isNot(AsmToken::EndOfStatement)) {
779 if (getParser().parseExpression(Subsection))
780 return true;
781 }
782
783 if (getLexer().isNot(AsmToken::EndOfStatement))
784 return TokError("unexpected token in directive");
785
Nirav Davea645433c2016-07-18 15:24:03 +0000786 Lex();
787
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000788 getStreamer().SubSection(Subsection);
789 return false;
790}
791
Daniel Dunbarab058b82010-07-12 21:23:32 +0000792namespace llvm {
793
794MCAsmParserExtension *createELFAsmParser() {
795 return new ELFAsmParser;
796}
797
Eugene Zelenko1d435522017-02-07 23:02:00 +0000798} // end namespace llvm