blob: 7bf14968c973cc79fd30818f27a021b67830048c [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");
Michael J. Spencerae6eeae2018-06-02 16:33:01 +000088 addDirectiveHandler<&ELFAsmParser::ParseDirectiveCGProfile>(".cg_profile");
Daniel Dunbarab058b82010-07-12 21:23:32 +000089 }
90
Rafael Espindolaf667d922010-09-15 21:48:40 +000091 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
92 // the best way for us to get access to it?
Daniel Dunbarab058b82010-07-12 21:23:32 +000093 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +000094 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola449711c2015-11-18 06:02:15 +000095 ELF::SHF_WRITE | ELF::SHF_ALLOC,
96 SectionKind::getData());
Daniel Dunbarab058b82010-07-12 21:23:32 +000097 }
98 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +000099 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000100 ELF::SHF_EXECINSTR |
101 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbarab058b82010-07-12 21:23:32 +0000102 }
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000103 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000104 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000105 ELF::SHF_WRITE |
106 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000107 }
108 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000109 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000110 ELF::SHF_ALLOC,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000111 SectionKind::getReadOnly());
112 }
113 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000114 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000115 ELF::SHF_ALLOC |
116 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000117 SectionKind::getThreadData());
118 }
119 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000120 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000121 ELF::SHF_ALLOC |
122 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000123 SectionKind::getThreadBSS());
124 }
125 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000126 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola449711c2015-11-18 06:02:15 +0000127 ELF::SHF_ALLOC | ELF::SHF_WRITE,
128 SectionKind::getData());
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000129 }
130 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000131 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000132 ELF::SHF_ALLOC |
133 ELF::SHF_WRITE,
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000134 SectionKind::getReadOnlyWithRel());
135 }
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000136 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000137 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola449711c2015-11-18 06:02:15 +0000138 ELF::SHF_ALLOC | ELF::SHF_WRITE,
139 SectionKind::getData());
Matt Fleminga8f6c1c2010-07-20 21:12:46 +0000140 }
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000141 bool ParseDirectivePushSection(StringRef, SMLoc);
142 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman9e36dd02010-07-17 04:29:04 +0000143 bool ParseDirectiveSection(StringRef, SMLoc);
144 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramere39017c2010-09-02 18:53:37 +0000145 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000146 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000147 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000148 bool ParseDirectiveSymver(StringRef, SMLoc);
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000149 bool ParseDirectiveVersion(StringRef, SMLoc);
Rafael Espindola16145972010-11-01 14:28:48 +0000150 bool ParseDirectiveWeakref(StringRef, SMLoc);
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000151 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000152 bool ParseDirectiveSubsection(StringRef, SMLoc);
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000153 bool ParseDirectiveCGProfile(StringRef, SMLoc);
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000154
155private:
156 bool ParseSectionName(StringRef &SectionName);
Oliver Stannard8b273082014-06-19 15:52:37 +0000157 bool ParseSectionArguments(bool IsPush, SMLoc loc);
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000158 unsigned parseSunStyleSectionFlags();
Rafael Espindolad9953d92017-01-31 23:07:08 +0000159 bool maybeParseSectionType(StringRef &TypeName);
Rafael Espindolaa86be222017-01-31 23:26:32 +0000160 bool parseMergeSize(int64_t &Size);
161 bool parseGroup(StringRef &GroupName);
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000162 bool parseMetadataSym(MCSymbolELF *&Associated);
Rafael Espindolaa86be222017-01-31 23:26:32 +0000163 bool maybeParseUniqueID(int64_t &UniqueID);
Daniel Dunbarab058b82010-07-12 21:23:32 +0000164};
165
Eugene Zelenko1d435522017-02-07 23:02:00 +0000166} // end anonymous namespace
Daniel Dunbarab058b82010-07-12 21:23:32 +0000167
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000168/// ParseDirectiveSymbolAttribute
169/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
170bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
171 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
172 .Case(".weak", MCSA_Weak)
173 .Case(".local", MCSA_Local)
174 .Case(".hidden", MCSA_Hidden)
175 .Case(".internal", MCSA_Internal)
176 .Case(".protected", MCSA_Protected)
177 .Default(MCSA_Invalid);
178 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
179 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Eugene Zelenko1d435522017-02-07 23:02:00 +0000180 while (true) {
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000181 StringRef Name;
182
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000183 if (getParser().parseIdentifier(Name))
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000184 return TokError("expected identifier in directive");
185
Jim Grosbach6f482002015-05-18 18:43:14 +0000186 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Jim Grosbach38b1ed82011-07-25 17:55:35 +0000187
188 getStreamer().EmitSymbolAttribute(Sym, Attr);
189
190 if (getLexer().is(AsmToken::EndOfStatement))
191 break;
192
193 if (getLexer().isNot(AsmToken::Comma))
194 return TokError("unexpected token in directive");
195 Lex();
196 }
197 }
198
199 Lex();
200 return false;
201}
202
Daniel Dunbarab058b82010-07-12 21:23:32 +0000203bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
204 unsigned Flags, SectionKind Kind) {
Craig Topper353eda42014-04-24 06:44:33 +0000205 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000206 if (getLexer().isNot(AsmToken::EndOfStatement)) {
207 if (getParser().parseExpression(Subsection))
208 return true;
209 }
Nirav Dave53a72f42016-07-11 12:42:14 +0000210 Lex();
Daniel Dunbarab058b82010-07-12 21:23:32 +0000211
Rafael Espindolaba31e272015-01-29 17:33:21 +0000212 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000213 Subsection);
Daniel Dunbarab058b82010-07-12 21:23:32 +0000214
215 return false;
216}
217
Eli Friedman9e36dd02010-07-17 04:29:04 +0000218bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedman56178a02010-07-17 03:09:18 +0000219 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000220 if (getParser().parseIdentifier(Name))
Eli Friedman56178a02010-07-17 03:09:18 +0000221 return TokError("expected identifier in directive");
Rafael Espindolaa8695762015-06-02 00:25:12 +0000222 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
Eli Friedman56178a02010-07-17 03:09:18 +0000223
224 if (getLexer().isNot(AsmToken::Comma))
225 return TokError("unexpected token in directive");
226 Lex();
227
228 const MCExpr *Expr;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000229 if (getParser().parseExpression(Expr))
Eli Friedman56178a02010-07-17 03:09:18 +0000230 return true;
231
232 if (getLexer().isNot(AsmToken::EndOfStatement))
233 return TokError("unexpected token in directive");
Nirav Davea645433c2016-07-18 15:24:03 +0000234 Lex();
Eli Friedman56178a02010-07-17 03:09:18 +0000235
Rafael Espindolaa8695762015-06-02 00:25:12 +0000236 getStreamer().emitELFSize(Sym, Expr);
Eli Friedman56178a02010-07-17 03:09:18 +0000237 return false;
238}
239
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000240bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
241 // A section name can contain -, so we cannot just use
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000242 // parseIdentifier.
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000243 SMLoc FirstLoc = getLexer().getLoc();
244 unsigned Size = 0;
245
Rafael Espindola689939e2011-01-24 18:02:54 +0000246 if (getLexer().is(AsmToken::String)) {
247 SectionName = getTok().getIdentifier();
248 Lex();
249 return false;
250 }
251
George Rimarb074fbc2017-10-05 08:15:55 +0000252 while (!getParser().hasPendingError()) {
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000253 SMLoc PrevLoc = getLexer().getLoc();
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000254 if (getLexer().is(AsmToken::Comma) ||
255 getLexer().is(AsmToken::EndOfStatement))
256 break;
Fangrui Songf78650a2018-07-30 19:41:25 +0000257
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000258 unsigned CurSize;
259 if (getLexer().is(AsmToken::String)) {
Rafael Espindola689939e2011-01-24 18:02:54 +0000260 CurSize = getTok().getIdentifier().size() + 2;
261 Lex();
262 } else if (getLexer().is(AsmToken::Identifier)) {
263 CurSize = getTok().getIdentifier().size();
264 Lex();
265 } else {
Marina Yatsina33ef7da2016-03-22 11:23:15 +0000266 CurSize = getTok().getString().size();
267 Lex();
Rafael Espindola689939e2011-01-24 18:02:54 +0000268 }
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000269 Size += CurSize;
270 SectionName = StringRef(FirstLoc.getPointer(), Size);
271
272 // Make sure the following token is adjacent.
273 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
274 break;
275 }
276 if (Size == 0)
277 return true;
278
279 return false;
280}
281
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000282static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
283 unsigned flags = 0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000284
Prakhar Bahugunae640c6f2016-12-15 07:59:15 +0000285 // If a valid numerical value is set for the section flag, use it verbatim
286 if (!flagsStr.getAsInteger(0, flags))
287 return flags;
288
Benjamin Kramer7b4658f2016-06-26 14:49:00 +0000289 for (char i : flagsStr) {
290 switch (i) {
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000291 case 'a':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000292 flags |= ELF::SHF_ALLOC;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000293 break;
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000294 case 'e':
295 flags |= ELF::SHF_EXCLUDE;
296 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000297 case 'x':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000298 flags |= ELF::SHF_EXECINSTR;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000299 break;
300 case 'w':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000301 flags |= ELF::SHF_WRITE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000302 break;
Evgeniy Stepanov12de7b22017-04-04 22:35:08 +0000303 case 'o':
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000304 flags |= ELF::SHF_LINK_ORDER;
305 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000306 case 'M':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000307 flags |= ELF::SHF_MERGE;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000308 break;
309 case 'S':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000310 flags |= ELF::SHF_STRINGS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000311 break;
312 case 'T':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000313 flags |= ELF::SHF_TLS;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000314 break;
315 case 'c':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000316 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000317 break;
318 case 'd':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000319 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000320 break;
Prakhar Bahuguna52a7dd72016-12-15 07:59:08 +0000321 case 'y':
322 flags |= ELF::SHF_ARM_PURECODE;
323 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000324 case 'G':
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000325 flags |= ELF::SHF_GROUP;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000326 break;
David Majnemera4b521b2013-09-15 19:24:16 +0000327 case '?':
328 *UseLastGroup = true;
329 break;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000330 default:
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000331 return -1U;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000332 }
333 }
334
335 return flags;
336}
337
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000338unsigned ELFAsmParser::parseSunStyleSectionFlags() {
339 unsigned flags = 0;
340 while (getLexer().is(AsmToken::Hash)) {
341 Lex(); // Eat the #.
342
343 if (!getLexer().is(AsmToken::Identifier))
344 return -1U;
345
346 StringRef flagId = getTok().getIdentifier();
347 if (flagId == "alloc")
348 flags |= ELF::SHF_ALLOC;
349 else if (flagId == "execinstr")
350 flags |= ELF::SHF_EXECINSTR;
351 else if (flagId == "write")
352 flags |= ELF::SHF_WRITE;
353 else if (flagId == "tls")
354 flags |= ELF::SHF_TLS;
355 else
356 return -1U;
357
358 Lex(); // Eat the flag.
359
360 if (!getLexer().is(AsmToken::Comma))
361 break;
362 Lex(); // Eat the comma.
363 }
364 return flags;
365}
366
367
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000368bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
369 getStreamer().PushSection();
370
Oliver Stannard8b273082014-06-19 15:52:37 +0000371 if (ParseSectionArguments(/*IsPush=*/true, loc)) {
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000372 getStreamer().PopSection();
373 return true;
374 }
375
376 return false;
377}
378
379bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
380 if (!getStreamer().PopSection())
381 return TokError(".popsection without corresponding .pushsection");
382 return false;
383}
384
Oliver Stannard8b273082014-06-19 15:52:37 +0000385bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
386 return ParseSectionArguments(/*IsPush=*/false, loc);
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000387}
388
Rafael Espindolad9953d92017-01-31 23:07:08 +0000389bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
390 MCAsmLexer &L = getLexer();
391 if (L.isNot(AsmToken::Comma))
392 return false;
393 Lex();
394 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
Oliver Stannard8761e9b2017-03-17 11:10:17 +0000395 L.isNot(AsmToken::String)) {
396 if (L.getAllowAtInIdentifier())
397 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
398 else
399 return TokError("expected '%<type>' or \"<type>\"");
400 }
Rafael Espindolad9953d92017-01-31 23:07:08 +0000401 if (!L.is(AsmToken::String))
402 Lex();
Simon Atanasyan29532242017-03-10 08:22:13 +0000403 if (L.is(AsmToken::Integer)) {
404 TypeName = getTok().getString();
405 Lex();
406 } else if (getParser().parseIdentifier(TypeName))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000407 return TokError("expected identifier in directive");
408 return false;
409}
410
Rafael Espindolaa86be222017-01-31 23:26:32 +0000411bool ELFAsmParser::parseMergeSize(int64_t &Size) {
412 if (getLexer().isNot(AsmToken::Comma))
413 return TokError("expected the entry size");
414 Lex();
415 if (getParser().parseAbsoluteExpression(Size))
416 return true;
417 if (Size <= 0)
418 return TokError("entry size must be positive");
419 return false;
420}
421
422bool ELFAsmParser::parseGroup(StringRef &GroupName) {
423 MCAsmLexer &L = getLexer();
424 if (L.isNot(AsmToken::Comma))
425 return TokError("expected group name");
426 Lex();
George Rimar64edcdc2017-12-24 06:13:36 +0000427 if (L.is(AsmToken::Integer)) {
428 GroupName = getTok().getString();
429 Lex();
430 } else if (getParser().parseIdentifier(GroupName)) {
George Rimar18e6a782017-12-25 09:41:00 +0000431 return TokError("invalid group name");
George Rimar64edcdc2017-12-24 06:13:36 +0000432 }
Rafael Espindolaa86be222017-01-31 23:26:32 +0000433 if (L.is(AsmToken::Comma)) {
434 Lex();
435 StringRef Linkage;
436 if (getParser().parseIdentifier(Linkage))
George Rimar18e6a782017-12-25 09:41:00 +0000437 return TokError("invalid linkage");
Rafael Espindolaa86be222017-01-31 23:26:32 +0000438 if (Linkage != "comdat")
439 return TokError("Linkage must be 'comdat'");
440 }
441 return false;
442}
443
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000444bool ELFAsmParser::parseMetadataSym(MCSymbolELF *&Associated) {
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000445 MCAsmLexer &L = getLexer();
446 if (L.isNot(AsmToken::Comma))
447 return TokError("expected metadata symbol");
448 Lex();
449 StringRef Name;
450 if (getParser().parseIdentifier(Name))
George Rimar7672eb82017-12-31 07:41:02 +0000451 return TokError("invalid metadata symbol");
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000452 Associated = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
453 if (!Associated || !Associated->isInSection())
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000454 return TokError("symbol is not in a section: " + Name);
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000455 return false;
456}
457
Rafael Espindolaa86be222017-01-31 23:26:32 +0000458bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
459 MCAsmLexer &L = getLexer();
460 if (L.isNot(AsmToken::Comma))
461 return false;
462 Lex();
463 StringRef UniqueStr;
464 if (getParser().parseIdentifier(UniqueStr))
465 return TokError("expected identifier in directive");
466 if (UniqueStr != "unique")
467 return TokError("expected 'unique'");
468 if (L.isNot(AsmToken::Comma))
469 return TokError("expected commma");
470 Lex();
471 if (getParser().parseAbsoluteExpression(UniqueID))
472 return true;
473 if (UniqueID < 0)
474 return TokError("unique id must be positive");
475 if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
476 return TokError("unique id is too large");
477 return false;
478}
479
Rafael Espindolaccd9f4f2017-03-22 13:35:41 +0000480static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
481 return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back();
482}
483
Eric Christopherb7a52bb2018-06-25 23:53:54 +0000484// Return a set of section flags based on the section name that can then
485// be augmented later, otherwise return 0 if we don't have any reasonable
486// defaults.
487static unsigned defaultSectionFlags(StringRef SectionName) {
488
Eric Christopher53054142018-07-02 00:16:39 +0000489 if (hasPrefix(SectionName, ".rodata.cst"))
490 return ELF::SHF_ALLOC | ELF::SHF_MERGE;
491
Eric Christopherb7a52bb2018-06-25 23:53:54 +0000492 if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
493 return ELF::SHF_ALLOC;
494
495 if (SectionName == ".fini" || SectionName == ".init" ||
496 hasPrefix(SectionName, ".text."))
497 return ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
498
499 if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
500 hasPrefix(SectionName, ".bss.") ||
501 hasPrefix(SectionName, ".init_array.") ||
502 hasPrefix(SectionName, ".fini_array.") ||
503 hasPrefix(SectionName, ".preinit_array."))
504 return ELF::SHF_ALLOC | ELF::SHF_WRITE;
505
506 if (hasPrefix(SectionName, ".tdata.") || hasPrefix(SectionName, ".tbss."))
507 return ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS;
508
509 return 0;
510}
511
Oliver Stannard8b273082014-06-19 15:52:37 +0000512bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000513 StringRef SectionName;
Rafael Espindolaf7f43322010-09-16 17:05:55 +0000514
515 if (ParseSectionName(SectionName))
Eli Friedman9e36dd02010-07-17 04:29:04 +0000516 return TokError("expected identifier in directive");
517
Eli Friedman9e36dd02010-07-17 04:29:04 +0000518 StringRef TypeName;
519 int64_t Size = 0;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000520 StringRef GroupName;
Craig Topper353eda42014-04-24 06:44:33 +0000521 const MCExpr *Subsection = nullptr;
David Majnemera4b521b2013-09-15 19:24:16 +0000522 bool UseLastGroup = false;
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000523 MCSymbolELF *Associated = nullptr;
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000524 int64_t UniqueID = ~0;
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000525
Eric Christopherb7a52bb2018-06-25 23:53:54 +0000526 // Set the default section flags first in case no others are given.
527 unsigned Flags = defaultSectionFlags(SectionName);
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000528
Eli Friedman9e36dd02010-07-17 04:29:04 +0000529 if (getLexer().is(AsmToken::Comma)) {
530 Lex();
531
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000532 if (IsPush && getLexer().isNot(AsmToken::String)) {
533 if (getParser().parseExpression(Subsection))
534 return true;
535 if (getLexer().isNot(AsmToken::Comma))
536 goto EndStmt;
537 Lex();
538 }
Eli Friedman9e36dd02010-07-17 04:29:04 +0000539
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000540 unsigned extraFlags;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000541
Venkatraman Govindarajubf705662014-03-01 06:21:00 +0000542 if (getLexer().isNot(AsmToken::String)) {
543 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
544 || getLexer().isNot(AsmToken::Hash))
545 return TokError("expected string in directive");
546 extraFlags = parseSunStyleSectionFlags();
547 } else {
548 StringRef FlagsStr = getTok().getStringContents();
549 Lex();
550 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
551 }
552
Benjamin Kramerac511ca2013-09-15 19:53:20 +0000553 if (extraFlags == -1U)
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000554 return TokError("unknown flag");
Eric Christopherb7a52bb2018-06-25 23:53:54 +0000555
556 // If we found additional section flags on a known section then give a
557 // warning.
558 if (Flags && Flags != extraFlags)
559 Warning(loc, "setting incorrect section attributes for " + SectionName);
560
Rafael Espindolaf8e127e2010-11-25 15:32:56 +0000561 Flags |= extraFlags;
562
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000563 bool Mergeable = Flags & ELF::SHF_MERGE;
564 bool Group = Flags & ELF::SHF_GROUP;
David Majnemera4b521b2013-09-15 19:24:16 +0000565 if (Group && UseLastGroup)
566 return TokError("Section cannot specifiy a group name while also acting "
567 "as a member of the last group");
Eli Friedman9e36dd02010-07-17 04:29:04 +0000568
Rafael Espindolad9953d92017-01-31 23:07:08 +0000569 if (maybeParseSectionType(TypeName))
570 return true;
571
572 MCAsmLexer &L = getLexer();
573 if (TypeName.empty()) {
Rafael Espindola8aefb662010-10-28 21:33:33 +0000574 if (Mergeable)
575 return TokError("Mergeable section must specify the type");
576 if (Group)
577 return TokError("Group section must specify the type");
Rafael Espindolad9953d92017-01-31 23:07:08 +0000578 if (L.isNot(AsmToken::EndOfStatement))
579 return TokError("unexpected token in directive");
580 }
581
Rafael Espindolaa86be222017-01-31 23:26:32 +0000582 if (Mergeable)
583 if (parseMergeSize(Size))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000584 return true;
Rafael Espindolaa86be222017-01-31 23:26:32 +0000585 if (Group)
586 if (parseGroup(GroupName))
Rafael Espindolad9953d92017-01-31 23:07:08 +0000587 return true;
Rafael Espindoladc1c3012017-02-09 14:59:20 +0000588 if (Flags & ELF::SHF_LINK_ORDER)
589 if (parseMetadataSym(Associated))
590 return true;
Rafael Espindolaa86be222017-01-31 23:26:32 +0000591 if (maybeParseUniqueID(UniqueID))
592 return true;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000593 }
594
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000595EndStmt:
Eli Friedman9e36dd02010-07-17 04:29:04 +0000596 if (getLexer().isNot(AsmToken::EndOfStatement))
597 return TokError("unexpected token in directive");
Nirav Davea645433c2016-07-18 15:24:03 +0000598 Lex();
Eli Friedman9e36dd02010-07-17 04:29:04 +0000599
Rafael Espindolaaea49582011-01-23 04:28:49 +0000600 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000601
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000602 if (TypeName.empty()) {
603 if (SectionName.startswith(".note"))
604 Type = ELF::SHT_NOTE;
Rafael Espindolaccd9f4f2017-03-22 13:35:41 +0000605 else if (hasPrefix(SectionName, ".init_array."))
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000606 Type = ELF::SHT_INIT_ARRAY;
Rafael Espindolaf4b9da62017-03-22 13:57:16 +0000607 else if (hasPrefix(SectionName, ".bss."))
608 Type = ELF::SHT_NOBITS;
Rafael Espindola72dc2542017-03-22 14:04:19 +0000609 else if (hasPrefix(SectionName, ".tbss."))
610 Type = ELF::SHT_NOBITS;
Petr Hosek880cfd42017-04-04 23:32:45 +0000611 else if (hasPrefix(SectionName, ".fini_array."))
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000612 Type = ELF::SHT_FINI_ARRAY;
Petr Hosek880cfd42017-04-04 23:32:45 +0000613 else if (hasPrefix(SectionName, ".preinit_array."))
Joerg Sonnenbergere2bb3142013-02-16 00:32:53 +0000614 Type = ELF::SHT_PREINIT_ARRAY;
615 } else {
Eli Friedman9e36dd02010-07-17 04:29:04 +0000616 if (TypeName == "init_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000617 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000618 else if (TypeName == "fini_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000619 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000620 else if (TypeName == "preinit_array")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000621 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000622 else if (TypeName == "nobits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000623 Type = ELF::SHT_NOBITS;
Eli Friedman9e36dd02010-07-17 04:29:04 +0000624 else if (TypeName == "progbits")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000625 Type = ELF::SHT_PROGBITS;
Rafael Espindola9ae2d052010-12-26 21:30:59 +0000626 else if (TypeName == "note")
Rafael Espindolaaea49582011-01-23 04:28:49 +0000627 Type = ELF::SHT_NOTE;
Rafael Espindola4b7b7fb2011-01-23 05:43:40 +0000628 else if (TypeName == "unwind")
629 Type = ELF::SHT_X86_64_UNWIND;
Peter Collingbournef0e26e72017-06-14 18:52:12 +0000630 else if (TypeName == "llvm_odrtab")
631 Type = ELF::SHT_LLVM_ODRTAB;
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000632 else if (TypeName == "llvm_linker_options")
633 Type = ELF::SHT_LLVM_LINKER_OPTIONS;
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000634 else if (TypeName == "llvm_call_graph_profile")
635 Type = ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
Simon Atanasyan29532242017-03-10 08:22:13 +0000636 else if (TypeName.getAsInteger(0, Type))
Eli Friedman9e36dd02010-07-17 04:29:04 +0000637 return TokError("unknown section type");
638 }
639
David Majnemera4b521b2013-09-15 19:24:16 +0000640 if (UseLastGroup) {
641 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
642 if (const MCSectionELF *Section =
643 cast_or_null<MCSectionELF>(CurrentSection.first))
644 if (const MCSymbol *Group = Section->getGroup()) {
645 GroupName = Group->getName();
646 Flags |= ELF::SHF_GROUP;
647 }
648 }
649
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000650 MCSection *ELFSection =
651 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
652 UniqueID, Associated);
Oliver Stannard8b273082014-06-19 15:52:37 +0000653 getStreamer().SwitchSection(ELFSection, Subsection);
654
655 if (getContext().getGenDwarfForAssembly()) {
Rafael Espindolae0746792015-05-21 16:52:32 +0000656 bool InsertResult = getContext().addGenDwarfSection(ELFSection);
657 if (InsertResult) {
Oliver Stannard8b273082014-06-19 15:52:37 +0000658 if (getContext().getDwarfVersion() <= 2)
Oliver Stannard14f97d02014-09-22 10:45:16 +0000659 Warning(loc, "DWARF2 only supports one section per compilation unit");
Oliver Stannard8b273082014-06-19 15:52:37 +0000660
Rafael Espindola2f9bdd82015-05-27 20:52:32 +0000661 if (!ELFSection->getBeginSymbol()) {
662 MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
663 getStreamer().EmitLabel(SectionStartSymbol);
664 ELFSection->setBeginSymbol(SectionStartSymbol);
665 }
Oliver Stannard8b273082014-06-19 15:52:37 +0000666 }
667 }
668
Eli Friedman9e36dd02010-07-17 04:29:04 +0000669 return false;
670}
671
Benjamin Kramere39017c2010-09-02 18:53:37 +0000672bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000673 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
Craig Topper353eda42014-04-24 06:44:33 +0000674 if (PreviousSection.first == nullptr)
Rafael Espindola58ac6e12011-02-16 01:08:29 +0000675 return TokError(".previous without corresponding .section");
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000676 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
Benjamin Kramere39017c2010-09-02 18:53:37 +0000677
678 return false;
679}
680
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000681static MCSymbolAttr MCAttrForString(StringRef Type) {
682 return StringSwitch<MCSymbolAttr>(Type)
683 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
684 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
685 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
686 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
687 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
688 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
689 MCSA_ELF_TypeIndFunction)
690 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
691 .Default(MCSA_Invalid);
692}
693
Michael J. Spencer3d898232010-10-09 03:47:55 +0000694/// ParseDirectiveELFType
David Majnemerf90c3b52013-09-21 05:25:12 +0000695/// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
696/// ::= .type identifier , #attribute
Michael J. Spencer3d898232010-10-09 03:47:55 +0000697/// ::= .type identifier , @attribute
David Majnemerf90c3b52013-09-21 05:25:12 +0000698/// ::= .type identifier , %attribute
699/// ::= .type identifier , "attribute"
Michael J. Spencer3d898232010-10-09 03:47:55 +0000700bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
701 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000702 if (getParser().parseIdentifier(Name))
Michael J. Spencer3d898232010-10-09 03:47:55 +0000703 return TokError("expected identifier in directive");
704
705 // Handle the identifier as the key symbol.
Jim Grosbach6f482002015-05-18 18:43:14 +0000706 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000707
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000708 // NOTE the comma is optional in all cases. It is only documented as being
709 // optional in the first case, however, GAS will silently treat the comma as
710 // optional in all cases. Furthermore, although the documentation states that
711 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
712 // accepts both the upper case name as well as the lower case aliases.
713 if (getLexer().is(AsmToken::Comma))
714 Lex();
Michael J. Spencer3d898232010-10-09 03:47:55 +0000715
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000716 if (getLexer().isNot(AsmToken::Identifier) &&
Gabor Ballabasaf06a882015-07-01 08:58:49 +0000717 getLexer().isNot(AsmToken::Hash) &&
718 getLexer().isNot(AsmToken::Percent) &&
719 getLexer().isNot(AsmToken::String)) {
720 if (!getLexer().getAllowAtInIdentifier())
721 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
722 "'%<type>' or \"<type>\"");
723 else if (getLexer().isNot(AsmToken::At))
724 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
725 "'%<type>' or \"<type>\"");
726 }
Michael J. Spencer3d898232010-10-09 03:47:55 +0000727
Saleem Abdulrasool13b414a2014-06-08 00:34:34 +0000728 if (getLexer().isNot(AsmToken::String) &&
729 getLexer().isNot(AsmToken::Identifier))
730 Lex();
731
732 SMLoc TypeLoc = getLexer().getLoc();
733
734 StringRef Type;
735 if (getParser().parseIdentifier(Type))
736 return TokError("expected symbol type in directive");
737
738 MCSymbolAttr Attr = MCAttrForString(Type);
Michael J. Spencer3d898232010-10-09 03:47:55 +0000739 if (Attr == MCSA_Invalid)
740 return Error(TypeLoc, "unsupported attribute in '.type' directive");
741
742 if (getLexer().isNot(AsmToken::EndOfStatement))
743 return TokError("unexpected token in '.type' directive");
Michael J. Spencer3d898232010-10-09 03:47:55 +0000744 Lex();
745
746 getStreamer().EmitSymbolAttribute(Sym, Attr);
747
748 return false;
749}
750
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000751/// ParseDirectiveIdent
752/// ::= .ident string
753bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
754 if (getLexer().isNot(AsmToken::String))
755 return TokError("unexpected token in '.ident' directive");
756
757 StringRef Data = getTok().getIdentifier();
758
759 Lex();
760
Nirav Davea645433c2016-07-18 15:24:03 +0000761 if (getLexer().isNot(AsmToken::EndOfStatement))
762 return TokError("unexpected token in '.ident' directive");
763 Lex();
764
Rafael Espindola5645bad2013-10-16 01:05:45 +0000765 getStreamer().EmitIdent(Data);
Rafael Espindolac9fb35e2010-10-26 19:35:47 +0000766 return false;
767}
768
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000769/// ParseDirectiveSymver
770/// ::= .symver foo, bar2@zed
771bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
772 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000773 if (getParser().parseIdentifier(Name))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000774 return TokError("expected identifier in directive");
775
776 if (getLexer().isNot(AsmToken::Comma))
777 return TokError("expected a comma");
778
David Peixottoc0f92a22014-01-15 22:40:02 +0000779 // ARM assembly uses @ for a comment...
780 // except when parsing the second parameter of the .symver directive.
781 // Force the next symbol to allow @ in the identifier, which is
782 // required for this directive and then reset it to its initial state.
783 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
784 getLexer().setAllowAtInIdentifier(true);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000785 Lex();
David Peixottoc0f92a22014-01-15 22:40:02 +0000786 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000787
788 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000789 if (getParser().parseIdentifier(AliasName))
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000790 return TokError("expected identifier in directive");
791
792 if (AliasName.find('@') == StringRef::npos)
793 return TokError("expected a '@' in the name");
794
Jim Grosbach6f482002015-05-18 18:43:14 +0000795 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Rafael Espindola47b4d6b2018-03-09 18:42:25 +0000796 getStreamer().emitELFSymverDirective(AliasName, Sym);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000797 return false;
798}
799
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000800/// ParseDirectiveVersion
801/// ::= .version string
802bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
803 if (getLexer().isNot(AsmToken::String))
804 return TokError("unexpected token in '.version' directive");
805
806 StringRef Data = getTok().getIdentifier();
807
808 Lex();
809
Rafael Espindola0709a7b2015-05-21 19:20:38 +0000810 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000811
812 getStreamer().PushSection();
813 getStreamer().SwitchSection(Note);
814 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
815 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
816 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
Eric Christophere3ab3d02013-01-09 01:57:54 +0000817 getStreamer().EmitBytes(Data); // name.
Benjamin Kramer6bee7f72012-05-12 14:30:47 +0000818 getStreamer().EmitIntValue(0, 1); // terminate the string.
819 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
820 getStreamer().PopSection();
821 return false;
822}
823
Rafael Espindola16145972010-11-01 14:28:48 +0000824/// ParseDirectiveWeakref
825/// ::= .weakref foo, bar
826bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
827 // FIXME: Share code with the other alias building directives.
828
829 StringRef AliasName;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000830 if (getParser().parseIdentifier(AliasName))
Rafael Espindola16145972010-11-01 14:28:48 +0000831 return TokError("expected identifier in directive");
832
833 if (getLexer().isNot(AsmToken::Comma))
834 return TokError("expected a comma");
835
836 Lex();
837
838 StringRef Name;
Jim Grosbachd2037eb2013-02-20 22:21:35 +0000839 if (getParser().parseIdentifier(Name))
Rafael Espindola16145972010-11-01 14:28:48 +0000840 return TokError("expected identifier in directive");
841
Jim Grosbach6f482002015-05-18 18:43:14 +0000842 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
Rafael Espindola16145972010-11-01 14:28:48 +0000843
Jim Grosbach6f482002015-05-18 18:43:14 +0000844 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
Rafael Espindola16145972010-11-01 14:28:48 +0000845
846 getStreamer().EmitWeakReference(Alias, Sym);
847 return false;
848}
849
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000850bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
Craig Topper353eda42014-04-24 06:44:33 +0000851 const MCExpr *Subsection = nullptr;
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000852 if (getLexer().isNot(AsmToken::EndOfStatement)) {
853 if (getParser().parseExpression(Subsection))
854 return true;
855 }
856
857 if (getLexer().isNot(AsmToken::EndOfStatement))
858 return TokError("unexpected token in directive");
859
Nirav Davea645433c2016-07-18 15:24:03 +0000860 Lex();
861
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000862 getStreamer().SubSection(Subsection);
863 return false;
864}
865
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000866/// ParseDirectiveCGProfile
867/// ::= .cg_profile identifier, identifier, <number>
868bool ELFAsmParser::ParseDirectiveCGProfile(StringRef, SMLoc) {
869 StringRef From;
870 SMLoc FromLoc = getLexer().getLoc();
871 if (getParser().parseIdentifier(From))
872 return TokError("expected identifier in directive");
873
874 if (getLexer().isNot(AsmToken::Comma))
875 return TokError("expected a comma");
876 Lex();
877
878 StringRef To;
879 SMLoc ToLoc = getLexer().getLoc();
880 if (getParser().parseIdentifier(To))
881 return TokError("expected identifier in directive");
882
883 if (getLexer().isNot(AsmToken::Comma))
884 return TokError("expected a comma");
885 Lex();
886
887 int64_t Count;
888 if (getParser().parseIntToken(
889 Count, "expected integer count in '.cg_profile' directive"))
890 return true;
891
892 if (getLexer().isNot(AsmToken::EndOfStatement))
893 return TokError("unexpected token in directive");
894
895 MCSymbol *FromSym = getContext().getOrCreateSymbol(From);
896 MCSymbol *ToSym = getContext().getOrCreateSymbol(To);
897
898 getStreamer().emitCGProfileEntry(
899 MCSymbolRefExpr::create(FromSym, MCSymbolRefExpr::VK_None, getContext(),
900 FromLoc),
901 MCSymbolRefExpr::create(ToSym, MCSymbolRefExpr::VK_None, getContext(),
902 ToLoc),
903 Count);
904 return false;
905}
906
Daniel Dunbarab058b82010-07-12 21:23:32 +0000907namespace llvm {
908
909MCAsmParserExtension *createELFAsmParser() {
910 return new ELFAsmParser;
911}
912
Eugene Zelenko1d435522017-02-07 23:02:00 +0000913} // end namespace llvm