blob: c7b53ec19d8b58aaba5a11803d171713508599cf [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();
Rafael Espindolad9953d92017-01-31 23:07:08 +0000145 bool maybeParseSectionType(StringRef &TypeName);
Rafael Espindolaa86be222017-01-31 23:26:32 +0000146 bool parseMergeSize(int64_t &Size);
147 bool parseGroup(StringRef &GroupName);
148 bool maybeParseUniqueID(int64_t &UniqueID);
Daniel Dunbarab058b82010-07-12 21:23:32 +0000149};
150
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000151}
Daniel Dunbarab058b82010-07-12 21:23:32 +0000152
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000153/// ParseDirectiveSymbolAttribute
154/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
155bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
156 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
157 .Case(".weak", MCSA_Weak)
158 .Case(".local", MCSA_Local)
159 .Case(".hidden", MCSA_Hidden)
160 .Case(".internal", MCSA_Internal)
161 .Case(".protected", MCSA_Protected)
162 .Default(MCSA_Invalid);
163 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
164 if (getLexer().isNot(AsmToken::EndOfStatement)) {
165 for (;;) {
166 StringRef Name;
167
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000168 if (getParser().parseIdentifier(Name))
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000169 return TokError("expected identifier in directive");
170
Jim Grosbach6f482002015-05-18 18:43:14 +0000171 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000172
173 getStreamer().EmitSymbolAttribute(Sym, Attr);
174
175 if (getLexer().is(AsmToken::EndOfStatement))
176 break;
177
178 if (getLexer().isNot(AsmToken::Comma))
179 return TokError("unexpected token in directive");
180 Lex();
181 }
182 }
183
184 Lex();
185 return false;
186}
187
Daniel Dunbarab058b82010-07-12 21:23:32 +0000188bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
189 unsigned Flags, SectionKind Kind) {
Craig Topper353eda42014-04-24 06:44:33 +0000190 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000191 if (getLexer().isNot(AsmToken::EndOfStatement)) {
192 if (getParser().parseExpression(Subsection))
193 return true;
194 }
Nirav Dave53a72f42016-07-11 12:42:14 +0000195 Lex();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000196
Rafael Espindolaba31e272015-01-29 17:33:21 +0000197 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000198 Subsection);
Daniel Dunbarab058b82010-07-12 21:23:32 +0000199
200 return false;
201}
202
Eli Friedman9e36dd02010-07-17 04:29:04 +0000203bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedman56178a02010-07-17 03:09:18 +0000204 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000205 if (getParser().parseIdentifier(Name))
Eli Friedman56178a02010-07-17 03:09:18 +0000206 return TokError("expected identifier in directive");
Rafael Espindolaa8695762015-06-02 00:25:12 +0000207 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
Eli Friedman56178a02010-07-17 03:09:18 +0000208
209 if (getLexer().isNot(AsmToken::Comma))
210 return TokError("unexpected token in directive");
211 Lex();
212
213 const MCExpr *Expr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000214 if (getParser().parseExpression(Expr))
Eli Friedman56178a02010-07-17 03:09:18 +0000215 return true;
216
217 if (getLexer().isNot(AsmToken::EndOfStatement))
218 return TokError("unexpected token in directive");
Nirav Davea645433c2016-07-18 15:24:03 +0000219 Lex();
Eli Friedman56178a02010-07-17 03:09:18 +0000220
Rafael Espindolaa8695762015-06-02 00:25:12 +0000221 getStreamer().emitELFSize(Sym, Expr);
Eli Friedman56178a02010-07-17 03:09:18 +0000222 return false;
223}
224
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000225bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
226 // A section name can contain -, so we cannot just use
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000227 // parseIdentifier.
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000228 SMLoc FirstLoc = getLexer().getLoc();
229 unsigned Size = 0;
230
Rafael Espindola689939e2011-01-24 18:02:54 +0000231 if (getLexer().is(AsmToken::String)) {
232 SectionName = getTok().getIdentifier();
233 Lex();
234 return false;
235 }
236
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000237 for (;;) {
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000238
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000239 SMLoc PrevLoc = getLexer().getLoc();
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000240 if (getLexer().is(AsmToken::Comma) ||
241 getLexer().is(AsmToken::EndOfStatement))
242 break;
243
244 unsigned CurSize;
245 if (getLexer().is(AsmToken::String)) {
Rafael Espindola689939e2011-01-24 18:02:54 +0000246 CurSize = getTok().getIdentifier().size() + 2;
247 Lex();
248 } else if (getLexer().is(AsmToken::Identifier)) {
249 CurSize = getTok().getIdentifier().size();
250 Lex();
251 } else {
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000252 CurSize = getTok().getString().size();
253 Lex();
Rafael Espindola689939e2011-01-24 18:02:54 +0000254 }
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000255 Size += CurSize;
256 SectionName = StringRef(FirstLoc.getPointer(), Size);
257
258 // Make sure the following token is adjacent.
259 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
260 break;
261 }
262 if (Size == 0)
263 return true;
264
265 return false;
266}
267
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000268static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
269 unsigned flags = 0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000270
Prakhar Bahugunae640c6f2016-12-15 07:59:15 +0000271 // If a valid numerical value is set for the section flag, use it verbatim
272 if (!flagsStr.getAsInteger(0, flags))
273 return flags;
274
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000275 for (char i : flagsStr) {
276 switch (i) {
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000277 case 'a':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000278 flags |= ELF::SHF_ALLOC;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000279 break;
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000280 case 'e':
281 flags |= ELF::SHF_EXCLUDE;
282 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000283 case 'x':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000284 flags |= ELF::SHF_EXECINSTR;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000285 break;
286 case 'w':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000287 flags |= ELF::SHF_WRITE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000288 break;
289 case 'M':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000290 flags |= ELF::SHF_MERGE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000291 break;
292 case 'S':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000293 flags |= ELF::SHF_STRINGS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000294 break;
295 case 'T':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000296 flags |= ELF::SHF_TLS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000297 break;
298 case 'c':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000299 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000300 break;
301 case 'd':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000302 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000303 break;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000304 case 'y':
305 flags |= ELF::SHF_ARM_PURECODE;
306 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000307 case 'G':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000308 flags |= ELF::SHF_GROUP;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000309 break;
David Majnemera4b521b2013-09-15 19:24:16 +0000310 case '?':
311 *UseLastGroup = true;
312 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000313 default:
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000314 return -1U;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000315 }
316 }
317
318 return flags;
319}
320
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000321unsigned ELFAsmParser::parseSunStyleSectionFlags() {
322 unsigned flags = 0;
323 while (getLexer().is(AsmToken::Hash)) {
324 Lex(); // Eat the #.
325
326 if (!getLexer().is(AsmToken::Identifier))
327 return -1U;
328
329 StringRef flagId = getTok().getIdentifier();
330 if (flagId == "alloc")
331 flags |= ELF::SHF_ALLOC;
332 else if (flagId == "execinstr")
333 flags |= ELF::SHF_EXECINSTR;
334 else if (flagId == "write")
335 flags |= ELF::SHF_WRITE;
336 else if (flagId == "tls")
337 flags |= ELF::SHF_TLS;
338 else
339 return -1U;
340
341 Lex(); // Eat the flag.
342
343 if (!getLexer().is(AsmToken::Comma))
344 break;
345 Lex(); // Eat the comma.
346 }
347 return flags;
348}
349
350
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000351bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
352 getStreamer().PushSection();
353
Oliver Stannard8b273082014-06-19 15:52:37 +0000354 if (ParseSectionArguments(/*IsPush=*/true, loc)) {
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000355 getStreamer().PopSection();
356 return true;
357 }
358
359 return false;
360}
361
362bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
363 if (!getStreamer().PopSection())
364 return TokError(".popsection without corresponding .pushsection");
365 return false;
366}
367
Eli Friedman9e36dd02010-07-17 04:29:04 +0000368// FIXME: This is a work in progress.
Oliver Stannard8b273082014-06-19 15:52:37 +0000369bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
370 return ParseSectionArguments(/*IsPush=*/false, loc);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000371}
372
Rafael Espindolad9953d92017-01-31 23:07:08 +0000373bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
374 MCAsmLexer &L = getLexer();
375 if (L.isNot(AsmToken::Comma))
376 return false;
377 Lex();
378 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
379 L.isNot(AsmToken::String))
380 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
381 if (!L.is(AsmToken::String))
382 Lex();
383 if (getParser().parseIdentifier(TypeName))
384 return TokError("expected identifier in directive");
385 return false;
386}
387
Rafael Espindolaa86be222017-01-31 23:26:32 +0000388bool ELFAsmParser::parseMergeSize(int64_t &Size) {
389 if (getLexer().isNot(AsmToken::Comma))
390 return TokError("expected the entry size");
391 Lex();
392 if (getParser().parseAbsoluteExpression(Size))
393 return true;
394 if (Size <= 0)
395 return TokError("entry size must be positive");
396 return false;
397}
398
399bool ELFAsmParser::parseGroup(StringRef &GroupName) {
400 MCAsmLexer &L = getLexer();
401 if (L.isNot(AsmToken::Comma))
402 return TokError("expected group name");
403 Lex();
404 if (getParser().parseIdentifier(GroupName))
405 return true;
406 if (L.is(AsmToken::Comma)) {
407 Lex();
408 StringRef Linkage;
409 if (getParser().parseIdentifier(Linkage))
410 return true;
411 if (Linkage != "comdat")
412 return TokError("Linkage must be 'comdat'");
413 }
414 return false;
415}
416
417bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
418 MCAsmLexer &L = getLexer();
419 if (L.isNot(AsmToken::Comma))
420 return false;
421 Lex();
422 StringRef UniqueStr;
423 if (getParser().parseIdentifier(UniqueStr))
424 return TokError("expected identifier in directive");
425 if (UniqueStr != "unique")
426 return TokError("expected 'unique'");
427 if (L.isNot(AsmToken::Comma))
428 return TokError("expected commma");
429 Lex();
430 if (getParser().parseAbsoluteExpression(UniqueID))
431 return true;
432 if (UniqueID < 0)
433 return TokError("unique id must be positive");
434 if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
435 return TokError("unique id is too large");
436 return false;
437}
438
Oliver Stannard8b273082014-06-19 15:52:37 +0000439bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000440 StringRef SectionName;
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000441
442 if (ParseSectionName(SectionName))
Eli Friedman9e36dd02010-07-17 04:29:04 +0000443 return TokError("expected identifier in directive");
444
Eli Friedman9e36dd02010-07-17 04:29:04 +0000445 StringRef TypeName;
446 int64_t Size = 0;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000447 StringRef GroupName;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000448 unsigned Flags = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000449 const MCExpr *Subsection = nullptr;
David Majnemera4b521b2013-09-15 19:24:16 +0000450 bool UseLastGroup = false;
Rafael Espindola68fa2492015-02-17 20:48:01 +0000451 StringRef UniqueStr;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000452 int64_t UniqueID = ~0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000453
454 // Set the defaults first.
455 if (SectionName == ".fini" || SectionName == ".init" ||
456 SectionName == ".rodata")
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000457 Flags |= ELF::SHF_ALLOC;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000458 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000459 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000460
Eli Friedman9e36dd02010-07-17 04:29:04 +0000461 if (getLexer().is(AsmToken::Comma)) {
462 Lex();
463
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000464 if (IsPush && getLexer().isNot(AsmToken::String)) {
465 if (getParser().parseExpression(Subsection))
466 return true;
467 if (getLexer().isNot(AsmToken::Comma))
468 goto EndStmt;
469 Lex();
470 }
Eli Friedman9e36dd02010-07-17 04:29:04 +0000471
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000472 unsigned extraFlags;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000473
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000474 if (getLexer().isNot(AsmToken::String)) {
475 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
476 || getLexer().isNot(AsmToken::Hash))
477 return TokError("expected string in directive");
478 extraFlags = parseSunStyleSectionFlags();
479 } else {
480 StringRef FlagsStr = getTok().getStringContents();
481 Lex();
482 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
483 }
484
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000485 if (extraFlags == -1U)
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000486 return TokError("unknown flag");
487 Flags |= extraFlags;
488
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000489 bool Mergeable = Flags & ELF::SHF_MERGE;
490 bool Group = Flags & ELF::SHF_GROUP;
David Majnemera4b521b2013-09-15 19:24:16 +0000491 if (Group && UseLastGroup)
492 return TokError("Section cannot specifiy a group name while also acting "
493 "as a member of the last group");
Eli Friedman9e36dd02010-07-17 04:29:04 +0000494
Rafael Espindolad9953d92017-01-31 23:07:08 +0000495 if (maybeParseSectionType(TypeName))
496 return true;
497
498 MCAsmLexer &L = getLexer();
499 if (TypeName.empty()) {
Rafael Espindola8aefb662010-10-28 21:33:33 +0000500 if (Mergeable)
501 return TokError("Mergeable section must specify the type");
502 if (Group)
503 return TokError("Group section must specify the type");
Rafael Espindolad9953d92017-01-31 23:07:08 +0000504 if (L.isNot(AsmToken::EndOfStatement))
505 return TokError("unexpected token in directive");
506 }
507
Rafael Espindolaa86be222017-01-31 23:26:32 +0000508 if (Mergeable)
509 if (parseMergeSize(Size))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000510 return true;
Rafael Espindolaa86be222017-01-31 23:26:32 +0000511 if (Group)
512 if (parseGroup(GroupName))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000513 return true;
Rafael Espindolaa86be222017-01-31 23:26:32 +0000514 if (maybeParseUniqueID(UniqueID))
515 return true;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000516 }
517
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000518EndStmt:
Eli Friedman9e36dd02010-07-17 04:29:04 +0000519 if (getLexer().isNot(AsmToken::EndOfStatement))
520 return TokError("unexpected token in directive");
Nirav Davea645433c2016-07-18 15:24:03 +0000521 Lex();
Eli Friedman9e36dd02010-07-17 04:29:04 +0000522
Rafael Espindolaaea49582011-01-23 04:28:49 +0000523 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000524
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000525 if (TypeName.empty()) {
526 if (SectionName.startswith(".note"))
527 Type = ELF::SHT_NOTE;
528 else if (SectionName == ".init_array")
529 Type = ELF::SHT_INIT_ARRAY;
530 else if (SectionName == ".fini_array")
531 Type = ELF::SHT_FINI_ARRAY;
532 else if (SectionName == ".preinit_array")
533 Type = ELF::SHT_PREINIT_ARRAY;
534 } else {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000535 if (TypeName == "init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000536 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000537 else if (TypeName == "fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000538 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000539 else if (TypeName == "preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000540 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000541 else if (TypeName == "nobits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000542 Type = ELF::SHT_NOBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000543 else if (TypeName == "progbits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000544 Type = ELF::SHT_PROGBITS;
Rafael Espindola9ae2d052010-12-26 21:30:59 +0000545 else if (TypeName == "note")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000546 Type = ELF::SHT_NOTE;
Rafael Espindola4b7b7fb2011-01-23 05:43:40 +0000547 else if (TypeName == "unwind")
548 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000549 else
550 return TokError("unknown section type");
551 }
552
David Majnemera4b521b2013-09-15 19:24:16 +0000553 if (UseLastGroup) {
554 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
555 if (const MCSectionELF *Section =
556 cast_or_null<MCSectionELF>(CurrentSection.first))
557 if (const MCSymbol *Group = Section->getGroup()) {
558 GroupName = Group->getName();
559 Flags |= ELF::SHF_GROUP;
560 }
561 }
562
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000563 MCSection *ELFSection = getContext().getELFSection(SectionName, Type, Flags,
564 Size, GroupName, UniqueID);
Oliver Stannard8b273082014-06-19 15:52:37 +0000565 getStreamer().SwitchSection(ELFSection, Subsection);
566
567 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindolae0746792015-05-21 16:52:32 +0000568 bool InsertResult = getContext().addGenDwarfSection(ELFSection);
569 if (InsertResult) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000570 if (getContext().getDwarfVersion() <= 2)
Oliver Stannard14f97d02014-09-22 10:45:16 +0000571 Warning(loc, "DWARF2 only supports one section per compilation unit");
Oliver Stannard8b273082014-06-19 15:52:37 +0000572
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000573 if (!ELFSection->getBeginSymbol()) {
574 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
575 getStreamer().EmitLabel(SectionStartSymbol);
576 ELFSection->setBeginSymbol(SectionStartSymbol);
577 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000578 }
579 }
580
Eli Friedman9e36dd02010-07-17 04:29:04 +0000581 return false;
582}
583
Benjamin Kramere39017c2010-09-02 18:53:37 +0000584bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000585 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
Craig Topper353eda42014-04-24 06:44:33 +0000586 if (PreviousSection.first == nullptr)
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000587 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000588 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Benjamin Kramere39017c2010-09-02 18:53:37 +0000589
590 return false;
591}
592
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000593static MCSymbolAttr MCAttrForString(StringRef Type) {
594 return StringSwitch<MCSymbolAttr>(Type)
595 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
596 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
597 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
598 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
599 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
600 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
601 MCSA_ELF_TypeIndFunction)
602 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
603 .Default(MCSA_Invalid);
604}
605
Michael J. Spencer3d898232010-10-09 03:47:55 +0000606/// ParseDirectiveELFType
David Majnemerf90c3b52013-09-21 05:25:12 +0000607/// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
608/// ::= .type identifier , #attribute
Michael J. Spencer3d898232010-10-09 03:47:55 +0000609/// ::= .type identifier , @attribute
David Majnemerf90c3b52013-09-21 05:25:12 +0000610/// ::= .type identifier , %attribute
611/// ::= .type identifier , "attribute"
Michael J. Spencer3d898232010-10-09 03:47:55 +0000612bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
613 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000614 if (getParser().parseIdentifier(Name))
Michael J. Spencer3d898232010-10-09 03:47:55 +0000615 return TokError("expected identifier in directive");
616
617 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000618 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000619
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000620 // NOTE the comma is optional in all cases. It is only documented as being
621 // optional in the first case, however, GAS will silently treat the comma as
622 // optional in all cases. Furthermore, although the documentation states that
623 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
624 // accepts both the upper case name as well as the lower case aliases.
625 if (getLexer().is(AsmToken::Comma))
626 Lex();
Michael J. Spencer3d898232010-10-09 03:47:55 +0000627
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000628 if (getLexer().isNot(AsmToken::Identifier) &&
Gabor Ballabasaf06a882015-07-01 08:58:49 +0000629 getLexer().isNot(AsmToken::Hash) &&
630 getLexer().isNot(AsmToken::Percent) &&
631 getLexer().isNot(AsmToken::String)) {
632 if (!getLexer().getAllowAtInIdentifier())
633 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
634 "'%<type>' or \"<type>\"");
635 else if (getLexer().isNot(AsmToken::At))
636 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
637 "'%<type>' or \"<type>\"");
638 }
Michael J. Spencer3d898232010-10-09 03:47:55 +0000639
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000640 if (getLexer().isNot(AsmToken::String) &&
641 getLexer().isNot(AsmToken::Identifier))
642 Lex();
643
644 SMLoc TypeLoc = getLexer().getLoc();
645
646 StringRef Type;
647 if (getParser().parseIdentifier(Type))
648 return TokError("expected symbol type in directive");
649
650 MCSymbolAttr Attr = MCAttrForString(Type);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000651 if (Attr == MCSA_Invalid)
652 return Error(TypeLoc, "unsupported attribute in '.type' directive");
653
654 if (getLexer().isNot(AsmToken::EndOfStatement))
655 return TokError("unexpected token in '.type' directive");
Michael J. Spencer3d898232010-10-09 03:47:55 +0000656 Lex();
657
658 getStreamer().EmitSymbolAttribute(Sym, Attr);
659
660 return false;
661}
662
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000663/// ParseDirectiveIdent
664/// ::= .ident string
665bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
666 if (getLexer().isNot(AsmToken::String))
667 return TokError("unexpected token in '.ident' directive");
668
669 StringRef Data = getTok().getIdentifier();
670
671 Lex();
672
Nirav Davea645433c2016-07-18 15:24:03 +0000673 if (getLexer().isNot(AsmToken::EndOfStatement))
674 return TokError("unexpected token in '.ident' directive");
675 Lex();
676
Rafael Espindola5645bad2013-10-16 01:05:45 +0000677 getStreamer().EmitIdent(Data);
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000678 return false;
679}
680
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000681/// ParseDirectiveSymver
682/// ::= .symver foo, bar2@zed
683bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
684 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000685 if (getParser().parseIdentifier(Name))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000686 return TokError("expected identifier in directive");
687
688 if (getLexer().isNot(AsmToken::Comma))
689 return TokError("expected a comma");
690
David Peixottoc0f92a22014-01-15 22:40:02 +0000691 // ARM assembly uses @ for a comment...
692 // except when parsing the second parameter of the .symver directive.
693 // Force the next symbol to allow @ in the identifier, which is
694 // required for this directive and then reset it to its initial state.
695 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
696 getLexer().setAllowAtInIdentifier(true);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000697 Lex();
David Peixottoc0f92a22014-01-15 22:40:02 +0000698 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000699
700 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000701 if (getParser().parseIdentifier(AliasName))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000702 return TokError("expected identifier in directive");
703
704 if (AliasName.find('@') == StringRef::npos)
705 return TokError("expected a '@' in the name");
706
Jim Grosbach6f482002015-05-18 18:43:14 +0000707 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
708 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Jim Grosbach13760bd2015-05-30 01:25:56 +0000709 const MCExpr *Value = MCSymbolRefExpr::create(Sym, getContext());
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000710
711 getStreamer().EmitAssignment(Alias, Value);
712 return false;
713}
714
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000715/// ParseDirectiveVersion
716/// ::= .version string
717bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
718 if (getLexer().isNot(AsmToken::String))
719 return TokError("unexpected token in '.version' directive");
720
721 StringRef Data = getTok().getIdentifier();
722
723 Lex();
724
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000725 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000726
727 getStreamer().PushSection();
728 getStreamer().SwitchSection(Note);
729 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
730 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
731 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000732 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000733 getStreamer().EmitIntValue(0, 1); // terminate the string.
734 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
735 getStreamer().PopSection();
736 return false;
737}
738
Rafael Espindola16145972010-11-01 14:28:48 +0000739/// ParseDirectiveWeakref
740/// ::= .weakref foo, bar
741bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
742 // FIXME: Share code with the other alias building directives.
743
744 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000745 if (getParser().parseIdentifier(AliasName))
Rafael Espindola16145972010-11-01 14:28:48 +0000746 return TokError("expected identifier in directive");
747
748 if (getLexer().isNot(AsmToken::Comma))
749 return TokError("expected a comma");
750
751 Lex();
752
753 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000754 if (getParser().parseIdentifier(Name))
Rafael Espindola16145972010-11-01 14:28:48 +0000755 return TokError("expected identifier in directive");
756
Jim Grosbach6f482002015-05-18 18:43:14 +0000757 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
Rafael Espindola16145972010-11-01 14:28:48 +0000758
Jim Grosbach6f482002015-05-18 18:43:14 +0000759 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Rafael Espindola16145972010-11-01 14:28:48 +0000760
761 getStreamer().EmitWeakReference(Alias, Sym);
762 return false;
763}
764
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000765bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
Craig Topper353eda42014-04-24 06:44:33 +0000766 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000767 if (getLexer().isNot(AsmToken::EndOfStatement)) {
768 if (getParser().parseExpression(Subsection))
769 return true;
770 }
771
772 if (getLexer().isNot(AsmToken::EndOfStatement))
773 return TokError("unexpected token in directive");
774
Nirav Davea645433c2016-07-18 15:24:03 +0000775 Lex();
776
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000777 getStreamer().SubSection(Subsection);
778 return false;
779}
780
Daniel Dunbarab058b82010-07-12 21:23:32 +0000781namespace llvm {
782
783MCAsmParserExtension *createELFAsmParser() {
784 return new ELFAsmParser;
785}
786
787}