blob: 9e5b54a6881303587ca132a344fd8ef90f5448fc [file] [log] [blame]
Daniel Dunbarab058b82010-07-12 21:23:32 +00001//===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Dunbarab058b82010-07-12 21:23:32 +00006//
7//===----------------------------------------------------------------------===//
8
Eugene Zelenko1d435522017-02-07 23:02:00 +00009#include "llvm/ADT/StringRef.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/ADT/StringSwitch.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000011#include "llvm/BinaryFormat/ELF.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"
Eugene Zelenko1d435522017-02-07 23:02:00 +000026#include "llvm/Support/MathExtras.h"
27#include "llvm/Support/SMLoc.h"
28#include <cassert>
29#include <cstdint>
30#include <utility>
31
Daniel Dunbarab058b82010-07-12 21:23:32 +000032using namespace llvm;
33
34namespace {
35
36class ELFAsmParser : public MCAsmParserExtension {
Eli Bendersky29b9f472013-01-16 00:50:52 +000037 template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
Jim Grosbachd2037eb2013-02-20 22:21:35 +000038 void addDirectiveHandler(StringRef Directive) {
Eli Bendersky29b9f472013-01-16 00:50:52 +000039 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
40 this, HandleDirective<ELFAsmParser, HandlerMethod>);
41
Jim Grosbachd2037eb2013-02-20 22:21:35 +000042 getParser().addDirectiveHandler(Directive, Handler);
Daniel Dunbar8897d472010-07-18 22:22:07 +000043 }
44
Rafael Espindola5645bad2013-10-16 01:05:45 +000045 bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
46 SectionKind Kind);
Daniel Dunbarab058b82010-07-12 21:23:32 +000047
48public:
Rafael Espindola5645bad2013-10-16 01:05:45 +000049 ELFAsmParser() { BracketExpressionsSupported = true; }
Daniel Dunbarab058b82010-07-12 21:23:32 +000050
Craig Topper59be68f2014-03-08 07:14:16 +000051 void Initialize(MCAsmParser &Parser) override {
Daniel Dunbarab058b82010-07-12 21:23:32 +000052 // Call the base implementation.
53 this->MCAsmParserExtension::Initialize(Parser);
54
Jim Grosbachd2037eb2013-02-20 22:21:35 +000055 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
56 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
57 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
58 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
59 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
60 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
61 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000062 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000063 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000064 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000065 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000066 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000067 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
68 addDirectiveHandler<
Jim Grosbach1a55fa62011-07-25 17:11:29 +000069 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000070 addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
71 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
72 addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
73 addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
74 addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
75 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
76 addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
77 addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
78 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
79 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
80 addDirectiveHandler<
Jim Grosbach38b1ed82011-07-25 17:55:35 +000081 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000082 addDirectiveHandler<
Jim Grosbach38b1ed82011-07-25 17:55:35 +000083 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
Jim Grosbachd2037eb2013-02-20 22:21:35 +000084 addDirectiveHandler<
Jim Grosbach38b1ed82011-07-25 17:55:35 +000085 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
Peter Collingbourne2f495b92013-04-17 21:18:16 +000086 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
Michael J. Spencerae6eeae2018-06-02 16:33:01 +000087 addDirectiveHandler<&ELFAsmParser::ParseDirectiveCGProfile>(".cg_profile");
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);
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000152 bool ParseDirectiveCGProfile(StringRef, SMLoc);
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000153
154private:
155 bool ParseSectionName(StringRef &SectionName);
Oliver Stannard8b273082014-06-19 15:52:37 +0000156 bool ParseSectionArguments(bool IsPush, SMLoc loc);
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000157 unsigned parseSunStyleSectionFlags();
Rafael Espindolad9953d92017-01-31 23:07:08 +0000158 bool maybeParseSectionType(StringRef &TypeName);
Rafael Espindolaa86be222017-01-31 23:26:32 +0000159 bool parseMergeSize(int64_t &Size);
160 bool parseGroup(StringRef &GroupName);
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000161 bool parseMetadataSym(MCSymbolELF *&Associated);
Rafael Espindolaa86be222017-01-31 23:26:32 +0000162 bool maybeParseUniqueID(int64_t &UniqueID);
Daniel Dunbarab058b82010-07-12 21:23:32 +0000163};
164
Eugene Zelenko1d435522017-02-07 23:02:00 +0000165} // end anonymous namespace
Daniel Dunbarab058b82010-07-12 21:23:32 +0000166
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000167/// ParseDirectiveSymbolAttribute
168/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
169bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
170 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
171 .Case(".weak", MCSA_Weak)
172 .Case(".local", MCSA_Local)
173 .Case(".hidden", MCSA_Hidden)
174 .Case(".internal", MCSA_Internal)
175 .Case(".protected", MCSA_Protected)
176 .Default(MCSA_Invalid);
177 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
178 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Eugene Zelenko1d435522017-02-07 23:02:00 +0000179 while (true) {
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000180 StringRef Name;
181
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000182 if (getParser().parseIdentifier(Name))
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000183 return TokError("expected identifier in directive");
184
Jim Grosbach6f482002015-05-18 18:43:14 +0000185 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000186
187 getStreamer().EmitSymbolAttribute(Sym, Attr);
188
189 if (getLexer().is(AsmToken::EndOfStatement))
190 break;
191
192 if (getLexer().isNot(AsmToken::Comma))
193 return TokError("unexpected token in directive");
194 Lex();
195 }
196 }
197
198 Lex();
199 return false;
200}
201
Daniel Dunbarab058b82010-07-12 21:23:32 +0000202bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
203 unsigned Flags, SectionKind Kind) {
Craig Topper353eda42014-04-24 06:44:33 +0000204 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000205 if (getLexer().isNot(AsmToken::EndOfStatement)) {
206 if (getParser().parseExpression(Subsection))
207 return true;
208 }
Nirav Dave53a72f42016-07-11 12:42:14 +0000209 Lex();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000210
Rafael Espindolaba31e272015-01-29 17:33:21 +0000211 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000212 Subsection);
Daniel Dunbarab058b82010-07-12 21:23:32 +0000213
214 return false;
215}
216
Eli Friedman9e36dd02010-07-17 04:29:04 +0000217bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedman56178a02010-07-17 03:09:18 +0000218 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000219 if (getParser().parseIdentifier(Name))
Eli Friedman56178a02010-07-17 03:09:18 +0000220 return TokError("expected identifier in directive");
Rafael Espindolaa8695762015-06-02 00:25:12 +0000221 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
Eli Friedman56178a02010-07-17 03:09:18 +0000222
223 if (getLexer().isNot(AsmToken::Comma))
224 return TokError("unexpected token in directive");
225 Lex();
226
227 const MCExpr *Expr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000228 if (getParser().parseExpression(Expr))
Eli Friedman56178a02010-07-17 03:09:18 +0000229 return true;
230
231 if (getLexer().isNot(AsmToken::EndOfStatement))
232 return TokError("unexpected token in directive");
Nirav Davea645433c2016-07-18 15:24:03 +0000233 Lex();
Eli Friedman56178a02010-07-17 03:09:18 +0000234
Rafael Espindolaa8695762015-06-02 00:25:12 +0000235 getStreamer().emitELFSize(Sym, Expr);
Eli Friedman56178a02010-07-17 03:09:18 +0000236 return false;
237}
238
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000239bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
240 // A section name can contain -, so we cannot just use
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000241 // parseIdentifier.
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000242 SMLoc FirstLoc = getLexer().getLoc();
243 unsigned Size = 0;
244
Rafael Espindola689939e2011-01-24 18:02:54 +0000245 if (getLexer().is(AsmToken::String)) {
246 SectionName = getTok().getIdentifier();
247 Lex();
248 return false;
249 }
250
George Rimarb074fbc2017-10-05 08:15:55 +0000251 while (!getParser().hasPendingError()) {
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000252 SMLoc PrevLoc = getLexer().getLoc();
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000253 if (getLexer().is(AsmToken::Comma) ||
254 getLexer().is(AsmToken::EndOfStatement))
255 break;
Fangrui Songf78650a2018-07-30 19:41:25 +0000256
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000257 unsigned CurSize;
258 if (getLexer().is(AsmToken::String)) {
Rafael Espindola689939e2011-01-24 18:02:54 +0000259 CurSize = getTok().getIdentifier().size() + 2;
260 Lex();
261 } else if (getLexer().is(AsmToken::Identifier)) {
262 CurSize = getTok().getIdentifier().size();
263 Lex();
264 } else {
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000265 CurSize = getTok().getString().size();
266 Lex();
Rafael Espindola689939e2011-01-24 18:02:54 +0000267 }
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000268 Size += CurSize;
269 SectionName = StringRef(FirstLoc.getPointer(), Size);
270
271 // Make sure the following token is adjacent.
272 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
273 break;
274 }
275 if (Size == 0)
276 return true;
277
278 return false;
279}
280
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000281static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
282 unsigned flags = 0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000283
Prakhar Bahugunae640c6f2016-12-15 07:59:15 +0000284 // If a valid numerical value is set for the section flag, use it verbatim
285 if (!flagsStr.getAsInteger(0, flags))
286 return flags;
287
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000288 for (char i : flagsStr) {
289 switch (i) {
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000290 case 'a':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000291 flags |= ELF::SHF_ALLOC;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000292 break;
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000293 case 'e':
294 flags |= ELF::SHF_EXCLUDE;
295 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000296 case 'x':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000297 flags |= ELF::SHF_EXECINSTR;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000298 break;
299 case 'w':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000300 flags |= ELF::SHF_WRITE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000301 break;
Evgeniy Stepanov12de7b22017-04-04 22:35:08 +0000302 case 'o':
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000303 flags |= ELF::SHF_LINK_ORDER;
304 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000305 case 'M':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000306 flags |= ELF::SHF_MERGE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000307 break;
308 case 'S':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000309 flags |= ELF::SHF_STRINGS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000310 break;
311 case 'T':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000312 flags |= ELF::SHF_TLS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000313 break;
314 case 'c':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000315 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000316 break;
317 case 'd':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000318 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000319 break;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000320 case 'y':
321 flags |= ELF::SHF_ARM_PURECODE;
322 break;
Krzysztof Parzyszekf740fd62018-11-09 14:17:27 +0000323 case 's':
324 flags |= ELF::SHF_HEX_GPREL;
325 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000326 case 'G':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000327 flags |= ELF::SHF_GROUP;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000328 break;
David Majnemera4b521b2013-09-15 19:24:16 +0000329 case '?':
330 *UseLastGroup = true;
331 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000332 default:
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000333 return -1U;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000334 }
335 }
336
337 return flags;
338}
339
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000340unsigned ELFAsmParser::parseSunStyleSectionFlags() {
341 unsigned flags = 0;
342 while (getLexer().is(AsmToken::Hash)) {
343 Lex(); // Eat the #.
344
345 if (!getLexer().is(AsmToken::Identifier))
346 return -1U;
347
348 StringRef flagId = getTok().getIdentifier();
349 if (flagId == "alloc")
350 flags |= ELF::SHF_ALLOC;
351 else if (flagId == "execinstr")
352 flags |= ELF::SHF_EXECINSTR;
353 else if (flagId == "write")
354 flags |= ELF::SHF_WRITE;
355 else if (flagId == "tls")
356 flags |= ELF::SHF_TLS;
357 else
358 return -1U;
359
360 Lex(); // Eat the flag.
361
362 if (!getLexer().is(AsmToken::Comma))
363 break;
364 Lex(); // Eat the comma.
365 }
366 return flags;
367}
368
369
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000370bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
371 getStreamer().PushSection();
372
Oliver Stannard8b273082014-06-19 15:52:37 +0000373 if (ParseSectionArguments(/*IsPush=*/true, loc)) {
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000374 getStreamer().PopSection();
375 return true;
376 }
377
378 return false;
379}
380
381bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
382 if (!getStreamer().PopSection())
383 return TokError(".popsection without corresponding .pushsection");
384 return false;
385}
386
Oliver Stannard8b273082014-06-19 15:52:37 +0000387bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
388 return ParseSectionArguments(/*IsPush=*/false, loc);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000389}
390
Rafael Espindolad9953d92017-01-31 23:07:08 +0000391bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
392 MCAsmLexer &L = getLexer();
393 if (L.isNot(AsmToken::Comma))
394 return false;
395 Lex();
396 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
Oliver Stannard8761e9b2017-03-17 11:10:17 +0000397 L.isNot(AsmToken::String)) {
398 if (L.getAllowAtInIdentifier())
399 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
400 else
401 return TokError("expected '%<type>' or \"<type>\"");
402 }
Rafael Espindolad9953d92017-01-31 23:07:08 +0000403 if (!L.is(AsmToken::String))
404 Lex();
Simon Atanasyan29532242017-03-10 08:22:13 +0000405 if (L.is(AsmToken::Integer)) {
406 TypeName = getTok().getString();
407 Lex();
408 } else if (getParser().parseIdentifier(TypeName))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000409 return TokError("expected identifier in directive");
410 return false;
411}
412
Rafael Espindolaa86be222017-01-31 23:26:32 +0000413bool ELFAsmParser::parseMergeSize(int64_t &Size) {
414 if (getLexer().isNot(AsmToken::Comma))
415 return TokError("expected the entry size");
416 Lex();
417 if (getParser().parseAbsoluteExpression(Size))
418 return true;
419 if (Size <= 0)
420 return TokError("entry size must be positive");
421 return false;
422}
423
424bool ELFAsmParser::parseGroup(StringRef &GroupName) {
425 MCAsmLexer &L = getLexer();
426 if (L.isNot(AsmToken::Comma))
427 return TokError("expected group name");
428 Lex();
George Rimar64edcdc2017-12-24 06:13:36 +0000429 if (L.is(AsmToken::Integer)) {
430 GroupName = getTok().getString();
431 Lex();
432 } else if (getParser().parseIdentifier(GroupName)) {
George Rimar18e6a782017-12-25 09:41:00 +0000433 return TokError("invalid group name");
George Rimar64edcdc2017-12-24 06:13:36 +0000434 }
Rafael Espindolaa86be222017-01-31 23:26:32 +0000435 if (L.is(AsmToken::Comma)) {
436 Lex();
437 StringRef Linkage;
438 if (getParser().parseIdentifier(Linkage))
George Rimar18e6a782017-12-25 09:41:00 +0000439 return TokError("invalid linkage");
Rafael Espindolaa86be222017-01-31 23:26:32 +0000440 if (Linkage != "comdat")
441 return TokError("Linkage must be 'comdat'");
442 }
443 return false;
444}
445
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000446bool ELFAsmParser::parseMetadataSym(MCSymbolELF *&Associated) {
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000447 MCAsmLexer &L = getLexer();
448 if (L.isNot(AsmToken::Comma))
449 return TokError("expected metadata symbol");
450 Lex();
451 StringRef Name;
452 if (getParser().parseIdentifier(Name))
George Rimar7672eb82017-12-31 07:41:02 +0000453 return TokError("invalid metadata symbol");
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000454 Associated = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
455 if (!Associated || !Associated->isInSection())
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000456 return TokError("symbol is not in a section: " + Name);
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000457 return false;
458}
459
Rafael Espindolaa86be222017-01-31 23:26:32 +0000460bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
461 MCAsmLexer &L = getLexer();
462 if (L.isNot(AsmToken::Comma))
463 return false;
464 Lex();
465 StringRef UniqueStr;
466 if (getParser().parseIdentifier(UniqueStr))
467 return TokError("expected identifier in directive");
468 if (UniqueStr != "unique")
469 return TokError("expected 'unique'");
470 if (L.isNot(AsmToken::Comma))
471 return TokError("expected commma");
472 Lex();
473 if (getParser().parseAbsoluteExpression(UniqueID))
474 return true;
475 if (UniqueID < 0)
476 return TokError("unique id must be positive");
477 if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
478 return TokError("unique id is too large");
479 return false;
480}
481
Rafael Espindolaccd9f4f2017-03-22 13:35:41 +0000482static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
483 return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back();
484}
485
Oliver Stannard8b273082014-06-19 15:52:37 +0000486bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000487 StringRef SectionName;
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000488
489 if (ParseSectionName(SectionName))
Eli Friedman9e36dd02010-07-17 04:29:04 +0000490 return TokError("expected identifier in directive");
491
Eli Friedman9e36dd02010-07-17 04:29:04 +0000492 StringRef TypeName;
493 int64_t Size = 0;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000494 StringRef GroupName;
Eric Christopher9855a5a2018-08-05 14:23:37 +0000495 unsigned Flags = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000496 const MCExpr *Subsection = nullptr;
David Majnemera4b521b2013-09-15 19:24:16 +0000497 bool UseLastGroup = false;
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000498 MCSymbolELF *Associated = nullptr;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000499 int64_t UniqueID = ~0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000500
Eric Christopher9855a5a2018-08-05 14:23:37 +0000501 // Set the defaults first.
502 if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
503 Flags |= ELF::SHF_ALLOC;
504 else if (SectionName == ".fini" || SectionName == ".init" ||
505 hasPrefix(SectionName, ".text."))
506 Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
507 else if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
508 hasPrefix(SectionName, ".bss.") ||
509 hasPrefix(SectionName, ".init_array.") ||
510 hasPrefix(SectionName, ".fini_array.") ||
511 hasPrefix(SectionName, ".preinit_array."))
512 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE;
513 else if (hasPrefix(SectionName, ".tdata.") ||
514 hasPrefix(SectionName, ".tbss."))
515 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000516
Eli Friedman9e36dd02010-07-17 04:29:04 +0000517 if (getLexer().is(AsmToken::Comma)) {
518 Lex();
519
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000520 if (IsPush && getLexer().isNot(AsmToken::String)) {
521 if (getParser().parseExpression(Subsection))
522 return true;
523 if (getLexer().isNot(AsmToken::Comma))
524 goto EndStmt;
525 Lex();
526 }
Eli Friedman9e36dd02010-07-17 04:29:04 +0000527
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000528 unsigned extraFlags;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000529
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000530 if (getLexer().isNot(AsmToken::String)) {
531 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
532 || getLexer().isNot(AsmToken::Hash))
533 return TokError("expected string in directive");
534 extraFlags = parseSunStyleSectionFlags();
535 } else {
536 StringRef FlagsStr = getTok().getStringContents();
537 Lex();
538 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
539 }
540
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000541 if (extraFlags == -1U)
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000542 return TokError("unknown flag");
543 Flags |= extraFlags;
544
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000545 bool Mergeable = Flags & ELF::SHF_MERGE;
546 bool Group = Flags & ELF::SHF_GROUP;
David Majnemera4b521b2013-09-15 19:24:16 +0000547 if (Group && UseLastGroup)
548 return TokError("Section cannot specifiy a group name while also acting "
549 "as a member of the last group");
Eli Friedman9e36dd02010-07-17 04:29:04 +0000550
Rafael Espindolad9953d92017-01-31 23:07:08 +0000551 if (maybeParseSectionType(TypeName))
552 return true;
553
554 MCAsmLexer &L = getLexer();
555 if (TypeName.empty()) {
Rafael Espindola8aefb662010-10-28 21:33:33 +0000556 if (Mergeable)
557 return TokError("Mergeable section must specify the type");
558 if (Group)
559 return TokError("Group section must specify the type");
Rafael Espindolad9953d92017-01-31 23:07:08 +0000560 if (L.isNot(AsmToken::EndOfStatement))
561 return TokError("unexpected token in directive");
562 }
563
Rafael Espindolaa86be222017-01-31 23:26:32 +0000564 if (Mergeable)
565 if (parseMergeSize(Size))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000566 return true;
Rafael Espindolaa86be222017-01-31 23:26:32 +0000567 if (Group)
568 if (parseGroup(GroupName))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000569 return true;
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000570 if (Flags & ELF::SHF_LINK_ORDER)
571 if (parseMetadataSym(Associated))
572 return true;
Rafael Espindolaa86be222017-01-31 23:26:32 +0000573 if (maybeParseUniqueID(UniqueID))
574 return true;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000575 }
576
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000577EndStmt:
Eli Friedman9e36dd02010-07-17 04:29:04 +0000578 if (getLexer().isNot(AsmToken::EndOfStatement))
579 return TokError("unexpected token in directive");
Nirav Davea645433c2016-07-18 15:24:03 +0000580 Lex();
Eli Friedman9e36dd02010-07-17 04:29:04 +0000581
Rafael Espindolaaea49582011-01-23 04:28:49 +0000582 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000583
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000584 if (TypeName.empty()) {
585 if (SectionName.startswith(".note"))
586 Type = ELF::SHT_NOTE;
Rafael Espindolaccd9f4f2017-03-22 13:35:41 +0000587 else if (hasPrefix(SectionName, ".init_array."))
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000588 Type = ELF::SHT_INIT_ARRAY;
Rafael Espindolaf4b9da62017-03-22 13:57:16 +0000589 else if (hasPrefix(SectionName, ".bss."))
590 Type = ELF::SHT_NOBITS;
Rafael Espindola72dc2542017-03-22 14:04:19 +0000591 else if (hasPrefix(SectionName, ".tbss."))
592 Type = ELF::SHT_NOBITS;
Petr Hosek880cfd42017-04-04 23:32:45 +0000593 else if (hasPrefix(SectionName, ".fini_array."))
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000594 Type = ELF::SHT_FINI_ARRAY;
Petr Hosek880cfd42017-04-04 23:32:45 +0000595 else if (hasPrefix(SectionName, ".preinit_array."))
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000596 Type = ELF::SHT_PREINIT_ARRAY;
597 } else {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000598 if (TypeName == "init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000599 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000600 else if (TypeName == "fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000601 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000602 else if (TypeName == "preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000603 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000604 else if (TypeName == "nobits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000605 Type = ELF::SHT_NOBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000606 else if (TypeName == "progbits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000607 Type = ELF::SHT_PROGBITS;
Rafael Espindola9ae2d052010-12-26 21:30:59 +0000608 else if (TypeName == "note")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000609 Type = ELF::SHT_NOTE;
Rafael Espindola4b7b7fb2011-01-23 05:43:40 +0000610 else if (TypeName == "unwind")
611 Type = ELF::SHT_X86_64_UNWIND;
Peter Collingbournef0e26e72017-06-14 18:52:12 +0000612 else if (TypeName == "llvm_odrtab")
613 Type = ELF::SHT_LLVM_ODRTAB;
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000614 else if (TypeName == "llvm_linker_options")
615 Type = ELF::SHT_LLVM_LINKER_OPTIONS;
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000616 else if (TypeName == "llvm_call_graph_profile")
617 Type = ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
Simon Atanasyan29532242017-03-10 08:22:13 +0000618 else if (TypeName.getAsInteger(0, Type))
Eli Friedman9e36dd02010-07-17 04:29:04 +0000619 return TokError("unknown section type");
620 }
621
David Majnemera4b521b2013-09-15 19:24:16 +0000622 if (UseLastGroup) {
623 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
624 if (const MCSectionELF *Section =
625 cast_or_null<MCSectionELF>(CurrentSection.first))
626 if (const MCSymbol *Group = Section->getGroup()) {
627 GroupName = Group->getName();
628 Flags |= ELF::SHF_GROUP;
629 }
630 }
631
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000632 MCSection *ELFSection =
633 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
634 UniqueID, Associated);
Oliver Stannard8b273082014-06-19 15:52:37 +0000635 getStreamer().SwitchSection(ELFSection, Subsection);
636
637 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindolae0746792015-05-21 16:52:32 +0000638 bool InsertResult = getContext().addGenDwarfSection(ELFSection);
639 if (InsertResult) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000640 if (getContext().getDwarfVersion() <= 2)
Oliver Stannard14f97d02014-09-22 10:45:16 +0000641 Warning(loc, "DWARF2 only supports one section per compilation unit");
Oliver Stannard8b273082014-06-19 15:52:37 +0000642
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000643 if (!ELFSection->getBeginSymbol()) {
644 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
645 getStreamer().EmitLabel(SectionStartSymbol);
646 ELFSection->setBeginSymbol(SectionStartSymbol);
647 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000648 }
649 }
650
Eli Friedman9e36dd02010-07-17 04:29:04 +0000651 return false;
652}
653
Benjamin Kramere39017c2010-09-02 18:53:37 +0000654bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000655 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
Craig Topper353eda42014-04-24 06:44:33 +0000656 if (PreviousSection.first == nullptr)
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000657 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000658 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Benjamin Kramere39017c2010-09-02 18:53:37 +0000659
660 return false;
661}
662
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000663static MCSymbolAttr MCAttrForString(StringRef Type) {
664 return StringSwitch<MCSymbolAttr>(Type)
665 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
666 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
667 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
668 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
669 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
670 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
671 MCSA_ELF_TypeIndFunction)
672 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
673 .Default(MCSA_Invalid);
674}
675
Michael J. Spencer3d898232010-10-09 03:47:55 +0000676/// ParseDirectiveELFType
David Majnemerf90c3b52013-09-21 05:25:12 +0000677/// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
678/// ::= .type identifier , #attribute
Michael J. Spencer3d898232010-10-09 03:47:55 +0000679/// ::= .type identifier , @attribute
David Majnemerf90c3b52013-09-21 05:25:12 +0000680/// ::= .type identifier , %attribute
681/// ::= .type identifier , "attribute"
Michael J. Spencer3d898232010-10-09 03:47:55 +0000682bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
683 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000684 if (getParser().parseIdentifier(Name))
Michael J. Spencer3d898232010-10-09 03:47:55 +0000685 return TokError("expected identifier in directive");
686
687 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000688 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000689
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000690 // NOTE the comma is optional in all cases. It is only documented as being
691 // optional in the first case, however, GAS will silently treat the comma as
692 // optional in all cases. Furthermore, although the documentation states that
693 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
694 // accepts both the upper case name as well as the lower case aliases.
695 if (getLexer().is(AsmToken::Comma))
696 Lex();
Michael J. Spencer3d898232010-10-09 03:47:55 +0000697
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000698 if (getLexer().isNot(AsmToken::Identifier) &&
Gabor Ballabasaf06a882015-07-01 08:58:49 +0000699 getLexer().isNot(AsmToken::Hash) &&
700 getLexer().isNot(AsmToken::Percent) &&
701 getLexer().isNot(AsmToken::String)) {
702 if (!getLexer().getAllowAtInIdentifier())
703 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
704 "'%<type>' or \"<type>\"");
705 else if (getLexer().isNot(AsmToken::At))
706 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
707 "'%<type>' or \"<type>\"");
708 }
Michael J. Spencer3d898232010-10-09 03:47:55 +0000709
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000710 if (getLexer().isNot(AsmToken::String) &&
711 getLexer().isNot(AsmToken::Identifier))
712 Lex();
713
714 SMLoc TypeLoc = getLexer().getLoc();
715
716 StringRef Type;
717 if (getParser().parseIdentifier(Type))
718 return TokError("expected symbol type in directive");
719
720 MCSymbolAttr Attr = MCAttrForString(Type);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000721 if (Attr == MCSA_Invalid)
722 return Error(TypeLoc, "unsupported attribute in '.type' directive");
723
724 if (getLexer().isNot(AsmToken::EndOfStatement))
725 return TokError("unexpected token in '.type' directive");
Michael J. Spencer3d898232010-10-09 03:47:55 +0000726 Lex();
727
728 getStreamer().EmitSymbolAttribute(Sym, Attr);
729
730 return false;
731}
732
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000733/// ParseDirectiveIdent
734/// ::= .ident string
735bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
736 if (getLexer().isNot(AsmToken::String))
737 return TokError("unexpected token in '.ident' directive");
738
739 StringRef Data = getTok().getIdentifier();
740
741 Lex();
742
Nirav Davea645433c2016-07-18 15:24:03 +0000743 if (getLexer().isNot(AsmToken::EndOfStatement))
744 return TokError("unexpected token in '.ident' directive");
745 Lex();
746
Rafael Espindola5645bad2013-10-16 01:05:45 +0000747 getStreamer().EmitIdent(Data);
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000748 return false;
749}
750
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000751/// ParseDirectiveSymver
752/// ::= .symver foo, bar2@zed
753bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
754 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000755 if (getParser().parseIdentifier(Name))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000756 return TokError("expected identifier in directive");
757
758 if (getLexer().isNot(AsmToken::Comma))
759 return TokError("expected a comma");
760
David Peixottoc0f92a22014-01-15 22:40:02 +0000761 // ARM assembly uses @ for a comment...
762 // except when parsing the second parameter of the .symver directive.
763 // Force the next symbol to allow @ in the identifier, which is
764 // required for this directive and then reset it to its initial state.
765 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
766 getLexer().setAllowAtInIdentifier(true);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000767 Lex();
David Peixottoc0f92a22014-01-15 22:40:02 +0000768 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000769
770 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000771 if (getParser().parseIdentifier(AliasName))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000772 return TokError("expected identifier in directive");
773
774 if (AliasName.find('@') == StringRef::npos)
775 return TokError("expected a '@' in the name");
776
Jim Grosbach6f482002015-05-18 18:43:14 +0000777 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Rafael Espindola47b4d6b2018-03-09 18:42:25 +0000778 getStreamer().emitELFSymverDirective(AliasName, Sym);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000779 return false;
780}
781
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000782/// ParseDirectiveVersion
783/// ::= .version string
784bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
785 if (getLexer().isNot(AsmToken::String))
786 return TokError("unexpected token in '.version' directive");
787
788 StringRef Data = getTok().getIdentifier();
789
790 Lex();
791
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000792 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000793
794 getStreamer().PushSection();
795 getStreamer().SwitchSection(Note);
796 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
797 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
798 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000799 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000800 getStreamer().EmitIntValue(0, 1); // terminate the string.
801 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
802 getStreamer().PopSection();
803 return false;
804}
805
Rafael Espindola16145972010-11-01 14:28:48 +0000806/// ParseDirectiveWeakref
807/// ::= .weakref foo, bar
808bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
809 // FIXME: Share code with the other alias building directives.
810
811 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000812 if (getParser().parseIdentifier(AliasName))
Rafael Espindola16145972010-11-01 14:28:48 +0000813 return TokError("expected identifier in directive");
814
815 if (getLexer().isNot(AsmToken::Comma))
816 return TokError("expected a comma");
817
818 Lex();
819
820 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000821 if (getParser().parseIdentifier(Name))
Rafael Espindola16145972010-11-01 14:28:48 +0000822 return TokError("expected identifier in directive");
823
Jim Grosbach6f482002015-05-18 18:43:14 +0000824 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
Rafael Espindola16145972010-11-01 14:28:48 +0000825
Jim Grosbach6f482002015-05-18 18:43:14 +0000826 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Rafael Espindola16145972010-11-01 14:28:48 +0000827
828 getStreamer().EmitWeakReference(Alias, Sym);
829 return false;
830}
831
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000832bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
Craig Topper353eda42014-04-24 06:44:33 +0000833 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000834 if (getLexer().isNot(AsmToken::EndOfStatement)) {
835 if (getParser().parseExpression(Subsection))
836 return true;
837 }
838
839 if (getLexer().isNot(AsmToken::EndOfStatement))
840 return TokError("unexpected token in directive");
841
Nirav Davea645433c2016-07-18 15:24:03 +0000842 Lex();
843
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000844 getStreamer().SubSection(Subsection);
845 return false;
846}
847
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000848/// ParseDirectiveCGProfile
849/// ::= .cg_profile identifier, identifier, <number>
850bool ELFAsmParser::ParseDirectiveCGProfile(StringRef, SMLoc) {
851 StringRef From;
852 SMLoc FromLoc = getLexer().getLoc();
853 if (getParser().parseIdentifier(From))
854 return TokError("expected identifier in directive");
855
856 if (getLexer().isNot(AsmToken::Comma))
857 return TokError("expected a comma");
858 Lex();
859
860 StringRef To;
861 SMLoc ToLoc = getLexer().getLoc();
862 if (getParser().parseIdentifier(To))
863 return TokError("expected identifier in directive");
864
865 if (getLexer().isNot(AsmToken::Comma))
866 return TokError("expected a comma");
867 Lex();
868
869 int64_t Count;
870 if (getParser().parseIntToken(
871 Count, "expected integer count in '.cg_profile' directive"))
872 return true;
873
874 if (getLexer().isNot(AsmToken::EndOfStatement))
875 return TokError("unexpected token in directive");
876
877 MCSymbol *FromSym = getContext().getOrCreateSymbol(From);
878 MCSymbol *ToSym = getContext().getOrCreateSymbol(To);
879
880 getStreamer().emitCGProfileEntry(
881 MCSymbolRefExpr::create(FromSym, MCSymbolRefExpr::VK_None, getContext(),
882 FromLoc),
883 MCSymbolRefExpr::create(ToSym, MCSymbolRefExpr::VK_None, getContext(),
884 ToLoc),
885 Count);
886 return false;
887}
888
Daniel Dunbarab058b82010-07-12 21:23:32 +0000889namespace llvm {
890
891MCAsmParserExtension *createELFAsmParser() {
892 return new ELFAsmParser;
893}
894
Eugene Zelenko1d435522017-02-07 23:02:00 +0000895} // end namespace llvm