blob: b9fa49e131bd41c15b9950a0648f487edadd8935 [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
Eugene Zelenko1d435522017-02-07 23:02:00 +000010#include "llvm/ADT/StringRef.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000011#include "llvm/ADT/StringSwitch.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000012#include "llvm/BinaryFormat/ELF.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"
Eugene Zelenko1d435522017-02-07 23:02:00 +000015#include "llvm/MC/MCDirectives.h"
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +000016#include "llvm/MC/MCExpr.h"
Daniel Dunbarab058b82010-07-12 21:23:32 +000017#include "llvm/MC/MCParser/MCAsmLexer.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000018#include "llvm/MC/MCParser/MCAsmParser.h"
19#include "llvm/MC/MCParser/MCAsmParserExtension.h"
20#include "llvm/MC/MCSection.h"
Eli Friedman9e36dd02010-07-17 04:29:04 +000021#include "llvm/MC/MCSectionELF.h"
22#include "llvm/MC/MCStreamer.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000023#include "llvm/MC/MCSymbol.h"
Rafael Espindolaa8695762015-06-02 00:25:12 +000024#include "llvm/MC/MCSymbolELF.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000025#include "llvm/MC/SectionKind.h"
26#include "llvm/Support/Casting.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);
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000160 bool parseMetadataSym(MCSymbolELF *&Associated);
Rafael Espindolaa86be222017-01-31 23:26:32 +0000161 bool maybeParseUniqueID(int64_t &UniqueID);
Daniel Dunbarab058b82010-07-12 21:23:32 +0000162};
163
Eugene Zelenko1d435522017-02-07 23:02:00 +0000164} // end anonymous namespace
Daniel Dunbarab058b82010-07-12 21:23:32 +0000165
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000166/// ParseDirectiveSymbolAttribute
167/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
168bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
169 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
170 .Case(".weak", MCSA_Weak)
171 .Case(".local", MCSA_Local)
172 .Case(".hidden", MCSA_Hidden)
173 .Case(".internal", MCSA_Internal)
174 .Case(".protected", MCSA_Protected)
175 .Default(MCSA_Invalid);
176 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
177 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Eugene Zelenko1d435522017-02-07 23:02:00 +0000178 while (true) {
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000179 StringRef Name;
180
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000181 if (getParser().parseIdentifier(Name))
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000182 return TokError("expected identifier in directive");
183
Jim Grosbach6f482002015-05-18 18:43:14 +0000184 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000185
186 getStreamer().EmitSymbolAttribute(Sym, Attr);
187
188 if (getLexer().is(AsmToken::EndOfStatement))
189 break;
190
191 if (getLexer().isNot(AsmToken::Comma))
192 return TokError("unexpected token in directive");
193 Lex();
194 }
195 }
196
197 Lex();
198 return false;
199}
200
Daniel Dunbarab058b82010-07-12 21:23:32 +0000201bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
202 unsigned Flags, SectionKind Kind) {
Craig Topper353eda42014-04-24 06:44:33 +0000203 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000204 if (getLexer().isNot(AsmToken::EndOfStatement)) {
205 if (getParser().parseExpression(Subsection))
206 return true;
207 }
Nirav Dave53a72f42016-07-11 12:42:14 +0000208 Lex();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000209
Rafael Espindolaba31e272015-01-29 17:33:21 +0000210 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000211 Subsection);
Daniel Dunbarab058b82010-07-12 21:23:32 +0000212
213 return false;
214}
215
Eli Friedman9e36dd02010-07-17 04:29:04 +0000216bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedman56178a02010-07-17 03:09:18 +0000217 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000218 if (getParser().parseIdentifier(Name))
Eli Friedman56178a02010-07-17 03:09:18 +0000219 return TokError("expected identifier in directive");
Rafael Espindolaa8695762015-06-02 00:25:12 +0000220 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
Eli Friedman56178a02010-07-17 03:09:18 +0000221
222 if (getLexer().isNot(AsmToken::Comma))
223 return TokError("unexpected token in directive");
224 Lex();
225
226 const MCExpr *Expr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000227 if (getParser().parseExpression(Expr))
Eli Friedman56178a02010-07-17 03:09:18 +0000228 return true;
229
230 if (getLexer().isNot(AsmToken::EndOfStatement))
231 return TokError("unexpected token in directive");
Nirav Davea645433c2016-07-18 15:24:03 +0000232 Lex();
Eli Friedman56178a02010-07-17 03:09:18 +0000233
Rafael Espindolaa8695762015-06-02 00:25:12 +0000234 getStreamer().emitELFSize(Sym, Expr);
Eli Friedman56178a02010-07-17 03:09:18 +0000235 return false;
236}
237
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000238bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
239 // A section name can contain -, so we cannot just use
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000240 // parseIdentifier.
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000241 SMLoc FirstLoc = getLexer().getLoc();
242 unsigned Size = 0;
243
Rafael Espindola689939e2011-01-24 18:02:54 +0000244 if (getLexer().is(AsmToken::String)) {
245 SectionName = getTok().getIdentifier();
246 Lex();
247 return false;
248 }
249
George Rimarb074fbc2017-10-05 08:15:55 +0000250 while (!getParser().hasPendingError()) {
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000251 SMLoc PrevLoc = getLexer().getLoc();
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000252 if (getLexer().is(AsmToken::Comma) ||
253 getLexer().is(AsmToken::EndOfStatement))
254 break;
255
256 unsigned CurSize;
257 if (getLexer().is(AsmToken::String)) {
Rafael Espindola689939e2011-01-24 18:02:54 +0000258 CurSize = getTok().getIdentifier().size() + 2;
259 Lex();
260 } else if (getLexer().is(AsmToken::Identifier)) {
261 CurSize = getTok().getIdentifier().size();
262 Lex();
263 } else {
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000264 CurSize = getTok().getString().size();
265 Lex();
Rafael Espindola689939e2011-01-24 18:02:54 +0000266 }
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000267 Size += CurSize;
268 SectionName = StringRef(FirstLoc.getPointer(), Size);
269
270 // Make sure the following token is adjacent.
271 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
272 break;
273 }
274 if (Size == 0)
275 return true;
276
277 return false;
278}
279
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000280static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
281 unsigned flags = 0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000282
Prakhar Bahugunae640c6f2016-12-15 07:59:15 +0000283 // If a valid numerical value is set for the section flag, use it verbatim
284 if (!flagsStr.getAsInteger(0, flags))
285 return flags;
286
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000287 for (char i : flagsStr) {
288 switch (i) {
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000289 case 'a':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000290 flags |= ELF::SHF_ALLOC;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000291 break;
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000292 case 'e':
293 flags |= ELF::SHF_EXCLUDE;
294 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000295 case 'x':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000296 flags |= ELF::SHF_EXECINSTR;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000297 break;
298 case 'w':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000299 flags |= ELF::SHF_WRITE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000300 break;
Evgeniy Stepanov12de7b22017-04-04 22:35:08 +0000301 case 'o':
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000302 flags |= ELF::SHF_LINK_ORDER;
303 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000304 case 'M':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000305 flags |= ELF::SHF_MERGE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000306 break;
307 case 'S':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000308 flags |= ELF::SHF_STRINGS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000309 break;
310 case 'T':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000311 flags |= ELF::SHF_TLS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000312 break;
313 case 'c':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000314 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000315 break;
316 case 'd':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000317 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000318 break;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000319 case 'y':
320 flags |= ELF::SHF_ARM_PURECODE;
321 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000322 case 'G':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000323 flags |= ELF::SHF_GROUP;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000324 break;
David Majnemera4b521b2013-09-15 19:24:16 +0000325 case '?':
326 *UseLastGroup = true;
327 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000328 default:
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000329 return -1U;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000330 }
331 }
332
333 return flags;
334}
335
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000336unsigned ELFAsmParser::parseSunStyleSectionFlags() {
337 unsigned flags = 0;
338 while (getLexer().is(AsmToken::Hash)) {
339 Lex(); // Eat the #.
340
341 if (!getLexer().is(AsmToken::Identifier))
342 return -1U;
343
344 StringRef flagId = getTok().getIdentifier();
345 if (flagId == "alloc")
346 flags |= ELF::SHF_ALLOC;
347 else if (flagId == "execinstr")
348 flags |= ELF::SHF_EXECINSTR;
349 else if (flagId == "write")
350 flags |= ELF::SHF_WRITE;
351 else if (flagId == "tls")
352 flags |= ELF::SHF_TLS;
353 else
354 return -1U;
355
356 Lex(); // Eat the flag.
357
358 if (!getLexer().is(AsmToken::Comma))
359 break;
360 Lex(); // Eat the comma.
361 }
362 return flags;
363}
364
365
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000366bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
367 getStreamer().PushSection();
368
Oliver Stannard8b273082014-06-19 15:52:37 +0000369 if (ParseSectionArguments(/*IsPush=*/true, loc)) {
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000370 getStreamer().PopSection();
371 return true;
372 }
373
374 return false;
375}
376
377bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
378 if (!getStreamer().PopSection())
379 return TokError(".popsection without corresponding .pushsection");
380 return false;
381}
382
Eli Friedman9e36dd02010-07-17 04:29:04 +0000383// FIXME: This is a work in progress.
Oliver Stannard8b273082014-06-19 15:52:37 +0000384bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
385 return ParseSectionArguments(/*IsPush=*/false, loc);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000386}
387
Rafael Espindolad9953d92017-01-31 23:07:08 +0000388bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
389 MCAsmLexer &L = getLexer();
390 if (L.isNot(AsmToken::Comma))
391 return false;
392 Lex();
393 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
Oliver Stannard8761e9b2017-03-17 11:10:17 +0000394 L.isNot(AsmToken::String)) {
395 if (L.getAllowAtInIdentifier())
396 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
397 else
398 return TokError("expected '%<type>' or \"<type>\"");
399 }
Rafael Espindolad9953d92017-01-31 23:07:08 +0000400 if (!L.is(AsmToken::String))
401 Lex();
Simon Atanasyan29532242017-03-10 08:22:13 +0000402 if (L.is(AsmToken::Integer)) {
403 TypeName = getTok().getString();
404 Lex();
405 } else if (getParser().parseIdentifier(TypeName))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000406 return TokError("expected identifier in directive");
407 return false;
408}
409
Rafael Espindolaa86be222017-01-31 23:26:32 +0000410bool ELFAsmParser::parseMergeSize(int64_t &Size) {
411 if (getLexer().isNot(AsmToken::Comma))
412 return TokError("expected the entry size");
413 Lex();
414 if (getParser().parseAbsoluteExpression(Size))
415 return true;
416 if (Size <= 0)
417 return TokError("entry size must be positive");
418 return false;
419}
420
421bool ELFAsmParser::parseGroup(StringRef &GroupName) {
422 MCAsmLexer &L = getLexer();
423 if (L.isNot(AsmToken::Comma))
424 return TokError("expected group name");
425 Lex();
George Rimar64edcdc2017-12-24 06:13:36 +0000426 if (L.is(AsmToken::Integer)) {
427 GroupName = getTok().getString();
428 Lex();
429 } else if (getParser().parseIdentifier(GroupName)) {
George Rimar18e6a782017-12-25 09:41:00 +0000430 return TokError("invalid group name");
George Rimar64edcdc2017-12-24 06:13:36 +0000431 }
Rafael Espindolaa86be222017-01-31 23:26:32 +0000432 if (L.is(AsmToken::Comma)) {
433 Lex();
434 StringRef Linkage;
435 if (getParser().parseIdentifier(Linkage))
George Rimar18e6a782017-12-25 09:41:00 +0000436 return TokError("invalid linkage");
Rafael Espindolaa86be222017-01-31 23:26:32 +0000437 if (Linkage != "comdat")
438 return TokError("Linkage must be 'comdat'");
439 }
440 return false;
441}
442
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000443bool ELFAsmParser::parseMetadataSym(MCSymbolELF *&Associated) {
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000444 MCAsmLexer &L = getLexer();
445 if (L.isNot(AsmToken::Comma))
446 return TokError("expected metadata symbol");
447 Lex();
448 StringRef Name;
449 if (getParser().parseIdentifier(Name))
George Rimar7672eb82017-12-31 07:41:02 +0000450 return TokError("invalid metadata symbol");
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000451 Associated = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
452 if (!Associated || !Associated->isInSection())
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000453 return TokError("symbol is not in a section: " + Name);
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000454 return false;
455}
456
Rafael Espindolaa86be222017-01-31 23:26:32 +0000457bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
458 MCAsmLexer &L = getLexer();
459 if (L.isNot(AsmToken::Comma))
460 return false;
461 Lex();
462 StringRef UniqueStr;
463 if (getParser().parseIdentifier(UniqueStr))
464 return TokError("expected identifier in directive");
465 if (UniqueStr != "unique")
466 return TokError("expected 'unique'");
467 if (L.isNot(AsmToken::Comma))
468 return TokError("expected commma");
469 Lex();
470 if (getParser().parseAbsoluteExpression(UniqueID))
471 return true;
472 if (UniqueID < 0)
473 return TokError("unique id must be positive");
474 if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
475 return TokError("unique id is too large");
476 return false;
477}
478
Rafael Espindolaccd9f4f2017-03-22 13:35:41 +0000479static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
480 return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back();
481}
482
Oliver Stannard8b273082014-06-19 15:52:37 +0000483bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000484 StringRef SectionName;
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000485
486 if (ParseSectionName(SectionName))
Eli Friedman9e36dd02010-07-17 04:29:04 +0000487 return TokError("expected identifier in directive");
488
Eli Friedman9e36dd02010-07-17 04:29:04 +0000489 StringRef TypeName;
490 int64_t Size = 0;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000491 StringRef GroupName;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000492 unsigned Flags = 0;
Craig Topper353eda42014-04-24 06:44:33 +0000493 const MCExpr *Subsection = nullptr;
David Majnemera4b521b2013-09-15 19:24:16 +0000494 bool UseLastGroup = false;
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000495 MCSymbolELF *Associated = nullptr;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000496 int64_t UniqueID = ~0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000497
498 // Set the defaults first.
Petr Hosek880cfd42017-04-04 23:32:45 +0000499 if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000500 Flags |= ELF::SHF_ALLOC;
Petr Hosek880cfd42017-04-04 23:32:45 +0000501 if (SectionName == ".fini" || SectionName == ".init" ||
502 hasPrefix(SectionName, ".text."))
503 Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
504 if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
505 hasPrefix(SectionName, ".bss.") ||
506 hasPrefix(SectionName, ".init_array.") ||
507 hasPrefix(SectionName, ".fini_array.") ||
508 hasPrefix(SectionName, ".preinit_array."))
509 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE;
510 if (hasPrefix(SectionName, ".tdata.") ||
511 hasPrefix(SectionName, ".tbss."))
512 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000513
Eli Friedman9e36dd02010-07-17 04:29:04 +0000514 if (getLexer().is(AsmToken::Comma)) {
515 Lex();
516
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000517 if (IsPush && getLexer().isNot(AsmToken::String)) {
518 if (getParser().parseExpression(Subsection))
519 return true;
520 if (getLexer().isNot(AsmToken::Comma))
521 goto EndStmt;
522 Lex();
523 }
Eli Friedman9e36dd02010-07-17 04:29:04 +0000524
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000525 unsigned extraFlags;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000526
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000527 if (getLexer().isNot(AsmToken::String)) {
528 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
529 || getLexer().isNot(AsmToken::Hash))
530 return TokError("expected string in directive");
531 extraFlags = parseSunStyleSectionFlags();
532 } else {
533 StringRef FlagsStr = getTok().getStringContents();
534 Lex();
535 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
536 }
537
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000538 if (extraFlags == -1U)
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000539 return TokError("unknown flag");
540 Flags |= extraFlags;
541
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000542 bool Mergeable = Flags & ELF::SHF_MERGE;
543 bool Group = Flags & ELF::SHF_GROUP;
David Majnemera4b521b2013-09-15 19:24:16 +0000544 if (Group && UseLastGroup)
545 return TokError("Section cannot specifiy a group name while also acting "
546 "as a member of the last group");
Eli Friedman9e36dd02010-07-17 04:29:04 +0000547
Rafael Espindolad9953d92017-01-31 23:07:08 +0000548 if (maybeParseSectionType(TypeName))
549 return true;
550
551 MCAsmLexer &L = getLexer();
552 if (TypeName.empty()) {
Rafael Espindola8aefb662010-10-28 21:33:33 +0000553 if (Mergeable)
554 return TokError("Mergeable section must specify the type");
555 if (Group)
556 return TokError("Group section must specify the type");
Rafael Espindolad9953d92017-01-31 23:07:08 +0000557 if (L.isNot(AsmToken::EndOfStatement))
558 return TokError("unexpected token in directive");
559 }
560
Rafael Espindolaa86be222017-01-31 23:26:32 +0000561 if (Mergeable)
562 if (parseMergeSize(Size))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000563 return true;
Rafael Espindolaa86be222017-01-31 23:26:32 +0000564 if (Group)
565 if (parseGroup(GroupName))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000566 return true;
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000567 if (Flags & ELF::SHF_LINK_ORDER)
568 if (parseMetadataSym(Associated))
569 return true;
Rafael Espindolaa86be222017-01-31 23:26:32 +0000570 if (maybeParseUniqueID(UniqueID))
571 return true;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000572 }
573
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000574EndStmt:
Eli Friedman9e36dd02010-07-17 04:29:04 +0000575 if (getLexer().isNot(AsmToken::EndOfStatement))
576 return TokError("unexpected token in directive");
Nirav Davea645433c2016-07-18 15:24:03 +0000577 Lex();
Eli Friedman9e36dd02010-07-17 04:29:04 +0000578
Rafael Espindolaaea49582011-01-23 04:28:49 +0000579 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000580
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000581 if (TypeName.empty()) {
582 if (SectionName.startswith(".note"))
583 Type = ELF::SHT_NOTE;
Rafael Espindolaccd9f4f2017-03-22 13:35:41 +0000584 else if (hasPrefix(SectionName, ".init_array."))
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000585 Type = ELF::SHT_INIT_ARRAY;
Rafael Espindolaf4b9da62017-03-22 13:57:16 +0000586 else if (hasPrefix(SectionName, ".bss."))
587 Type = ELF::SHT_NOBITS;
Rafael Espindola72dc2542017-03-22 14:04:19 +0000588 else if (hasPrefix(SectionName, ".tbss."))
589 Type = ELF::SHT_NOBITS;
Petr Hosek880cfd42017-04-04 23:32:45 +0000590 else if (hasPrefix(SectionName, ".fini_array."))
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000591 Type = ELF::SHT_FINI_ARRAY;
Petr Hosek880cfd42017-04-04 23:32:45 +0000592 else if (hasPrefix(SectionName, ".preinit_array."))
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000593 Type = ELF::SHT_PREINIT_ARRAY;
594 } else {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000595 if (TypeName == "init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000596 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000597 else if (TypeName == "fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000598 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000599 else if (TypeName == "preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000600 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000601 else if (TypeName == "nobits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000602 Type = ELF::SHT_NOBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000603 else if (TypeName == "progbits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000604 Type = ELF::SHT_PROGBITS;
Rafael Espindola9ae2d052010-12-26 21:30:59 +0000605 else if (TypeName == "note")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000606 Type = ELF::SHT_NOTE;
Rafael Espindola4b7b7fb2011-01-23 05:43:40 +0000607 else if (TypeName == "unwind")
608 Type = ELF::SHT_X86_64_UNWIND;
Peter Collingbournef0e26e72017-06-14 18:52:12 +0000609 else if (TypeName == "llvm_odrtab")
610 Type = ELF::SHT_LLVM_ODRTAB;
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000611 else if (TypeName == "llvm_linker_options")
612 Type = ELF::SHT_LLVM_LINKER_OPTIONS;
Simon Atanasyan29532242017-03-10 08:22:13 +0000613 else if (TypeName.getAsInteger(0, Type))
Eli Friedman9e36dd02010-07-17 04:29:04 +0000614 return TokError("unknown section type");
615 }
616
David Majnemera4b521b2013-09-15 19:24:16 +0000617 if (UseLastGroup) {
618 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
619 if (const MCSectionELF *Section =
620 cast_or_null<MCSectionELF>(CurrentSection.first))
621 if (const MCSymbol *Group = Section->getGroup()) {
622 GroupName = Group->getName();
623 Flags |= ELF::SHF_GROUP;
624 }
625 }
626
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000627 MCSection *ELFSection =
628 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
629 UniqueID, Associated);
Oliver Stannard8b273082014-06-19 15:52:37 +0000630 getStreamer().SwitchSection(ELFSection, Subsection);
631
632 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindolae0746792015-05-21 16:52:32 +0000633 bool InsertResult = getContext().addGenDwarfSection(ELFSection);
634 if (InsertResult) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000635 if (getContext().getDwarfVersion() <= 2)
Oliver Stannard14f97d02014-09-22 10:45:16 +0000636 Warning(loc, "DWARF2 only supports one section per compilation unit");
Oliver Stannard8b273082014-06-19 15:52:37 +0000637
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000638 if (!ELFSection->getBeginSymbol()) {
639 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
640 getStreamer().EmitLabel(SectionStartSymbol);
641 ELFSection->setBeginSymbol(SectionStartSymbol);
642 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000643 }
644 }
645
Eli Friedman9e36dd02010-07-17 04:29:04 +0000646 return false;
647}
648
Benjamin Kramere39017c2010-09-02 18:53:37 +0000649bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000650 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
Craig Topper353eda42014-04-24 06:44:33 +0000651 if (PreviousSection.first == nullptr)
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000652 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000653 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Benjamin Kramere39017c2010-09-02 18:53:37 +0000654
655 return false;
656}
657
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000658static MCSymbolAttr MCAttrForString(StringRef Type) {
659 return StringSwitch<MCSymbolAttr>(Type)
660 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
661 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
662 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
663 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
664 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
665 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
666 MCSA_ELF_TypeIndFunction)
667 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
668 .Default(MCSA_Invalid);
669}
670
Michael J. Spencer3d898232010-10-09 03:47:55 +0000671/// ParseDirectiveELFType
David Majnemerf90c3b52013-09-21 05:25:12 +0000672/// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
673/// ::= .type identifier , #attribute
Michael J. Spencer3d898232010-10-09 03:47:55 +0000674/// ::= .type identifier , @attribute
David Majnemerf90c3b52013-09-21 05:25:12 +0000675/// ::= .type identifier , %attribute
676/// ::= .type identifier , "attribute"
Michael J. Spencer3d898232010-10-09 03:47:55 +0000677bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
678 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000679 if (getParser().parseIdentifier(Name))
Michael J. Spencer3d898232010-10-09 03:47:55 +0000680 return TokError("expected identifier in directive");
681
682 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000683 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000684
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000685 // NOTE the comma is optional in all cases. It is only documented as being
686 // optional in the first case, however, GAS will silently treat the comma as
687 // optional in all cases. Furthermore, although the documentation states that
688 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
689 // accepts both the upper case name as well as the lower case aliases.
690 if (getLexer().is(AsmToken::Comma))
691 Lex();
Michael J. Spencer3d898232010-10-09 03:47:55 +0000692
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000693 if (getLexer().isNot(AsmToken::Identifier) &&
Gabor Ballabasaf06a882015-07-01 08:58:49 +0000694 getLexer().isNot(AsmToken::Hash) &&
695 getLexer().isNot(AsmToken::Percent) &&
696 getLexer().isNot(AsmToken::String)) {
697 if (!getLexer().getAllowAtInIdentifier())
698 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
699 "'%<type>' or \"<type>\"");
700 else if (getLexer().isNot(AsmToken::At))
701 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
702 "'%<type>' or \"<type>\"");
703 }
Michael J. Spencer3d898232010-10-09 03:47:55 +0000704
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000705 if (getLexer().isNot(AsmToken::String) &&
706 getLexer().isNot(AsmToken::Identifier))
707 Lex();
708
709 SMLoc TypeLoc = getLexer().getLoc();
710
711 StringRef Type;
712 if (getParser().parseIdentifier(Type))
713 return TokError("expected symbol type in directive");
714
715 MCSymbolAttr Attr = MCAttrForString(Type);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000716 if (Attr == MCSA_Invalid)
717 return Error(TypeLoc, "unsupported attribute in '.type' directive");
718
719 if (getLexer().isNot(AsmToken::EndOfStatement))
720 return TokError("unexpected token in '.type' directive");
Michael J. Spencer3d898232010-10-09 03:47:55 +0000721 Lex();
722
723 getStreamer().EmitSymbolAttribute(Sym, Attr);
724
725 return false;
726}
727
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000728/// ParseDirectiveIdent
729/// ::= .ident string
730bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
731 if (getLexer().isNot(AsmToken::String))
732 return TokError("unexpected token in '.ident' directive");
733
734 StringRef Data = getTok().getIdentifier();
735
736 Lex();
737
Nirav Davea645433c2016-07-18 15:24:03 +0000738 if (getLexer().isNot(AsmToken::EndOfStatement))
739 return TokError("unexpected token in '.ident' directive");
740 Lex();
741
Rafael Espindola5645bad2013-10-16 01:05:45 +0000742 getStreamer().EmitIdent(Data);
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000743 return false;
744}
745
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000746/// ParseDirectiveSymver
747/// ::= .symver foo, bar2@zed
748bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
749 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000750 if (getParser().parseIdentifier(Name))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000751 return TokError("expected identifier in directive");
752
753 if (getLexer().isNot(AsmToken::Comma))
754 return TokError("expected a comma");
755
David Peixottoc0f92a22014-01-15 22:40:02 +0000756 // ARM assembly uses @ for a comment...
757 // except when parsing the second parameter of the .symver directive.
758 // Force the next symbol to allow @ in the identifier, which is
759 // required for this directive and then reset it to its initial state.
760 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
761 getLexer().setAllowAtInIdentifier(true);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000762 Lex();
David Peixottoc0f92a22014-01-15 22:40:02 +0000763 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000764
765 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000766 if (getParser().parseIdentifier(AliasName))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000767 return TokError("expected identifier in directive");
768
769 if (AliasName.find('@') == StringRef::npos)
770 return TokError("expected a '@' in the name");
771
Jim Grosbach6f482002015-05-18 18:43:14 +0000772 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Rafael Espindola47b4d6b2018-03-09 18:42:25 +0000773 getStreamer().emitELFSymverDirective(AliasName, Sym);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000774 return false;
775}
776
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000777/// ParseDirectiveVersion
778/// ::= .version string
779bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
780 if (getLexer().isNot(AsmToken::String))
781 return TokError("unexpected token in '.version' directive");
782
783 StringRef Data = getTok().getIdentifier();
784
785 Lex();
786
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000787 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000788
789 getStreamer().PushSection();
790 getStreamer().SwitchSection(Note);
791 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
792 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
793 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000794 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000795 getStreamer().EmitIntValue(0, 1); // terminate the string.
796 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
797 getStreamer().PopSection();
798 return false;
799}
800
Rafael Espindola16145972010-11-01 14:28:48 +0000801/// ParseDirectiveWeakref
802/// ::= .weakref foo, bar
803bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
804 // FIXME: Share code with the other alias building directives.
805
806 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000807 if (getParser().parseIdentifier(AliasName))
Rafael Espindola16145972010-11-01 14:28:48 +0000808 return TokError("expected identifier in directive");
809
810 if (getLexer().isNot(AsmToken::Comma))
811 return TokError("expected a comma");
812
813 Lex();
814
815 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000816 if (getParser().parseIdentifier(Name))
Rafael Espindola16145972010-11-01 14:28:48 +0000817 return TokError("expected identifier in directive");
818
Jim Grosbach6f482002015-05-18 18:43:14 +0000819 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
Rafael Espindola16145972010-11-01 14:28:48 +0000820
Jim Grosbach6f482002015-05-18 18:43:14 +0000821 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Rafael Espindola16145972010-11-01 14:28:48 +0000822
823 getStreamer().EmitWeakReference(Alias, Sym);
824 return false;
825}
826
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000827bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
Craig Topper353eda42014-04-24 06:44:33 +0000828 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000829 if (getLexer().isNot(AsmToken::EndOfStatement)) {
830 if (getParser().parseExpression(Subsection))
831 return true;
832 }
833
834 if (getLexer().isNot(AsmToken::EndOfStatement))
835 return TokError("unexpected token in directive");
836
Nirav Davea645433c2016-07-18 15:24:03 +0000837 Lex();
838
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000839 getStreamer().SubSection(Subsection);
840 return false;
841}
842
Daniel Dunbarab058b82010-07-12 21:23:32 +0000843namespace llvm {
844
845MCAsmParserExtension *createELFAsmParser() {
846 return new ELFAsmParser;
847}
848
Eugene Zelenko1d435522017-02-07 23:02:00 +0000849} // end namespace llvm