blob: dcf689a6f0e7fa0a443e1f4f1085f4529476a101 [file] [log] [blame]
Daniel Dunbar5146a092010-07-12 21:23:32 +00001//===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCParser/MCAsmParserExtension.h"
Michael J. Spencere90ea132010-10-09 03:47:55 +000011#include "llvm/ADT/StringSwitch.h"
Eli Friedmandc1ad222010-07-17 06:27:28 +000012#include "llvm/ADT/Twine.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000013#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000014#include "llvm/MC/MCContext.h"
Rafael Espindola88182132010-10-27 15:18:17 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000016#include "llvm/MC/MCParser/MCAsmLexer.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000017#include "llvm/MC/MCSectionELF.h"
18#include "llvm/MC/MCStreamer.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000019#include "llvm/Support/ELF.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000020using namespace llvm;
21
22namespace {
23
24class ELFAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000025 template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
26 void AddDirectiveHandler(StringRef Directive) {
27 getParser().AddDirectiveHandler(this, Directive,
28 HandleDirective<ELFAsmParser, Handler>);
29 }
30
Daniel Dunbar5146a092010-07-12 21:23:32 +000031 bool ParseSectionSwitch(StringRef Section, unsigned Type,
32 unsigned Flags, SectionKind Kind);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +000033 bool SeenIdent;
Daniel Dunbar5146a092010-07-12 21:23:32 +000034
35public:
Joerg Sonnenberger93c65e62011-02-24 21:59:22 +000036 ELFAsmParser() : SeenIdent(false) {
37 BracketExpressionsSupported = true;
38 }
Daniel Dunbar5146a092010-07-12 21:23:32 +000039
40 virtual void Initialize(MCAsmParser &Parser) {
41 // Call the base implementation.
42 this->MCAsmParserExtension::Initialize(Parser);
43
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000044 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
45 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
Matt Flemingf525c2a2010-07-20 21:12:46 +000046 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
48 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
49 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
50 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
51 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
52 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
53 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000054 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
Rafael Espindola7768a9d2011-02-16 01:08:29 +000055 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePushSection>(".pushsection");
56 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000057 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000058 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
Michael J. Spencere90ea132010-10-09 03:47:55 +000059 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
Rafael Espindola61e3b912010-10-26 19:35:47 +000060 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
Rafael Espindola88182132010-10-27 15:18:17 +000061 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
Rafael Espindola484291c2010-11-01 14:28:48 +000062 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
Daniel Dunbar5146a092010-07-12 21:23:32 +000063 }
64
Rafael Espindolad80781b2010-09-15 21:48:40 +000065 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
66 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000067 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000068 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000069 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Daniel Dunbar5146a092010-07-12 21:23:32 +000070 SectionKind::getDataRel());
71 }
72 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000073 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000074 ELF::SHF_EXECINSTR |
75 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbar5146a092010-07-12 21:23:32 +000076 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000077 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000078 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000079 ELF::SHF_WRITE |
80 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Flemingf525c2a2010-07-20 21:12:46 +000081 }
82 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000083 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000084 ELF::SHF_ALLOC,
Matt Flemingf525c2a2010-07-20 21:12:46 +000085 SectionKind::getReadOnly());
86 }
87 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000088 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000089 ELF::SHF_ALLOC |
90 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +000091 SectionKind::getThreadData());
92 }
93 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000094 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000095 ELF::SHF_ALLOC |
96 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +000097 SectionKind::getThreadBSS());
98 }
99 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000100 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000101 ELF::SHF_ALLOC |
102 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000103 SectionKind::getDataRel());
104 }
105 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000106 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000107 ELF::SHF_ALLOC |
108 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000109 SectionKind::getReadOnlyWithRel());
110 }
111 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000112 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000113 ELF::SHF_ALLOC |
114 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000115 SectionKind::getReadOnlyWithRelLocal());
116 }
117 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000118 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000119 ELF::SHF_ALLOC |
120 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000121 SectionKind::getDataRel());
122 }
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000123 bool ParseDirectivePushSection(StringRef, SMLoc);
124 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman21444ef2010-07-17 04:29:04 +0000125 bool ParseDirectiveSection(StringRef, SMLoc);
126 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000127 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000128 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000129 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000130 bool ParseDirectiveSymver(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000131 bool ParseDirectiveWeakref(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000132
133private:
134 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000135};
136
137}
138
139bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
140 unsigned Flags, SectionKind Kind) {
141 if (getLexer().isNot(AsmToken::EndOfStatement))
142 return TokError("unexpected token in section switching directive");
143 Lex();
144
145 getStreamer().SwitchSection(getContext().getELFSection(
146 Section, Type, Flags, Kind));
147
148 return false;
149}
150
Eli Friedman21444ef2010-07-17 04:29:04 +0000151bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000152 StringRef Name;
153 if (getParser().ParseIdentifier(Name))
154 return TokError("expected identifier in directive");
155 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
156
157 if (getLexer().isNot(AsmToken::Comma))
158 return TokError("unexpected token in directive");
159 Lex();
160
161 const MCExpr *Expr;
162 if (getParser().ParseExpression(Expr))
163 return true;
164
165 if (getLexer().isNot(AsmToken::EndOfStatement))
166 return TokError("unexpected token in directive");
167
168 getStreamer().EmitELFSize(Sym, Expr);
169 return false;
170}
171
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000172bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
173 // A section name can contain -, so we cannot just use
174 // ParseIdentifier.
175 SMLoc FirstLoc = getLexer().getLoc();
176 unsigned Size = 0;
177
Rafael Espindola184640e2011-01-24 18:02:54 +0000178 if (getLexer().is(AsmToken::String)) {
179 SectionName = getTok().getIdentifier();
180 Lex();
181 return false;
182 }
183
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000184 for (;;) {
185 StringRef Tmp;
186 unsigned CurSize;
187
188 SMLoc PrevLoc = getLexer().getLoc();
189 if (getLexer().is(AsmToken::Minus)) {
190 CurSize = 1;
191 Lex(); // Consume the "-".
Rafael Espindola184640e2011-01-24 18:02:54 +0000192 } else if (getLexer().is(AsmToken::String)) {
193 CurSize = getTok().getIdentifier().size() + 2;
194 Lex();
195 } else if (getLexer().is(AsmToken::Identifier)) {
196 CurSize = getTok().getIdentifier().size();
197 Lex();
198 } else {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000199 break;
Rafael Espindola184640e2011-01-24 18:02:54 +0000200 }
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000201
202 Size += CurSize;
203 SectionName = StringRef(FirstLoc.getPointer(), Size);
204
205 // Make sure the following token is adjacent.
206 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
207 break;
208 }
209 if (Size == 0)
210 return true;
211
212 return false;
213}
214
Rafael Espindola25958732010-11-24 21:57:39 +0000215static SectionKind computeSectionKind(unsigned Flags) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000216 if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola25958732010-11-24 21:57:39 +0000217 return SectionKind::getText();
Rafael Espindola1c130262011-01-23 04:43:11 +0000218 if (Flags & ELF::SHF_TLS)
Rafael Espindola25958732010-11-24 21:57:39 +0000219 return SectionKind::getThreadData();
220 return SectionKind::getDataRel();
221}
222
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000223static int parseSectionFlags(StringRef flagsStr) {
224 int flags = 0;
225
226 for (unsigned i = 0; i < flagsStr.size(); i++) {
227 switch (flagsStr[i]) {
228 case 'a':
Rafael Espindola1c130262011-01-23 04:43:11 +0000229 flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000230 break;
231 case 'x':
Rafael Espindola1c130262011-01-23 04:43:11 +0000232 flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000233 break;
234 case 'w':
Rafael Espindola1c130262011-01-23 04:43:11 +0000235 flags |= ELF::SHF_WRITE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000236 break;
237 case 'M':
Rafael Espindola1c130262011-01-23 04:43:11 +0000238 flags |= ELF::SHF_MERGE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000239 break;
240 case 'S':
Rafael Espindola1c130262011-01-23 04:43:11 +0000241 flags |= ELF::SHF_STRINGS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000242 break;
243 case 'T':
Rafael Espindola1c130262011-01-23 04:43:11 +0000244 flags |= ELF::SHF_TLS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000245 break;
246 case 'c':
Rafael Espindola1c130262011-01-23 04:43:11 +0000247 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000248 break;
249 case 'd':
Rafael Espindola1c130262011-01-23 04:43:11 +0000250 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000251 break;
252 case 'G':
Rafael Espindola1c130262011-01-23 04:43:11 +0000253 flags |= ELF::SHF_GROUP;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000254 break;
255 default:
256 return -1;
257 }
258 }
259
260 return flags;
261}
262
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000263bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
264 getStreamer().PushSection();
265
266 if (ParseDirectiveSection(s, loc)) {
267 getStreamer().PopSection();
268 return true;
269 }
270
271 return false;
272}
273
274bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
275 if (!getStreamer().PopSection())
276 return TokError(".popsection without corresponding .pushsection");
277 return false;
278}
279
Eli Friedman21444ef2010-07-17 04:29:04 +0000280// FIXME: This is a work in progress.
281bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
282 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000283
284 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000285 return TokError("expected identifier in directive");
286
Eli Friedman21444ef2010-07-17 04:29:04 +0000287 StringRef TypeName;
288 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000289 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000290 unsigned Flags = 0;
291
292 // Set the defaults first.
293 if (SectionName == ".fini" || SectionName == ".init" ||
294 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000295 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000296 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000297 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000298
Eli Friedman21444ef2010-07-17 04:29:04 +0000299 if (getLexer().is(AsmToken::Comma)) {
300 Lex();
301
302 if (getLexer().isNot(AsmToken::String))
303 return TokError("expected string in directive");
304
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000305 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000306 Lex();
307
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000308 int extraFlags = parseSectionFlags(FlagsStr);
309 if (extraFlags < 0)
310 return TokError("unknown flag");
311 Flags |= extraFlags;
312
Rafael Espindola1c130262011-01-23 04:43:11 +0000313 bool Mergeable = Flags & ELF::SHF_MERGE;
314 bool Group = Flags & ELF::SHF_GROUP;
Eli Friedman21444ef2010-07-17 04:29:04 +0000315
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000316 if (getLexer().isNot(AsmToken::Comma)) {
317 if (Mergeable)
318 return TokError("Mergeable section must specify the type");
319 if (Group)
320 return TokError("Group section must specify the type");
321 } else {
322 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000323 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
324 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000325
326 Lex();
327 if (getParser().ParseIdentifier(TypeName))
328 return TokError("expected identifier in directive");
329
330 if (Mergeable) {
331 if (getLexer().isNot(AsmToken::Comma))
332 return TokError("expected the entry size");
333 Lex();
334 if (getParser().ParseAbsoluteExpression(Size))
335 return true;
336 if (Size <= 0)
337 return TokError("entry size must be positive");
338 }
339
340 if (Group) {
341 if (getLexer().isNot(AsmToken::Comma))
342 return TokError("expected group name");
343 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000344 if (getParser().ParseIdentifier(GroupName))
345 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000346 if (getLexer().is(AsmToken::Comma)) {
347 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000348 StringRef Linkage;
349 if (getParser().ParseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000350 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000351 if (Linkage != "comdat")
352 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000353 }
354 }
355 }
356 }
357
358 if (getLexer().isNot(AsmToken::EndOfStatement))
359 return TokError("unexpected token in directive");
360
Rafael Espindolac85dca62011-01-23 04:28:49 +0000361 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000362
Eli Friedman21444ef2010-07-17 04:29:04 +0000363 if (!TypeName.empty()) {
364 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000365 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000366 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000367 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000368 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000369 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000370 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000371 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000372 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000373 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000374 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000375 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000376 else if (TypeName == "unwind")
377 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000378 else
379 return TokError("unknown section type");
380 }
381
Rafael Espindola25958732010-11-24 21:57:39 +0000382 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000383 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000384 Flags, Kind, Size,
385 GroupName));
Eli Friedman21444ef2010-07-17 04:29:04 +0000386 return false;
387}
388
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000389bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
390 const MCSection *PreviousSection = getStreamer().getPreviousSection();
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000391 if (PreviousSection == NULL)
392 return TokError(".previous without corresponding .section");
393 getStreamer().SwitchSection(PreviousSection);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000394
395 return false;
396}
397
Michael J. Spencere90ea132010-10-09 03:47:55 +0000398/// ParseDirectiveELFType
399/// ::= .type identifier , @attribute
400bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
401 StringRef Name;
402 if (getParser().ParseIdentifier(Name))
403 return TokError("expected identifier in directive");
404
405 // Handle the identifier as the key symbol.
406 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
407
408 if (getLexer().isNot(AsmToken::Comma))
409 return TokError("unexpected token in '.type' directive");
410 Lex();
411
Rafael Espindolad4a35262010-11-12 15:47:08 +0000412 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
413 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000414 Lex();
415
416 StringRef Type;
417 SMLoc TypeLoc;
418
419 TypeLoc = getLexer().getLoc();
420 if (getParser().ParseIdentifier(Type))
421 return TokError("expected symbol type in directive");
422
423 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
424 .Case("function", MCSA_ELF_TypeFunction)
425 .Case("object", MCSA_ELF_TypeObject)
426 .Case("tls_object", MCSA_ELF_TypeTLS)
427 .Case("common", MCSA_ELF_TypeCommon)
428 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000429 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000430 .Default(MCSA_Invalid);
431
432 if (Attr == MCSA_Invalid)
433 return Error(TypeLoc, "unsupported attribute in '.type' directive");
434
435 if (getLexer().isNot(AsmToken::EndOfStatement))
436 return TokError("unexpected token in '.type' directive");
437
438 Lex();
439
440 getStreamer().EmitSymbolAttribute(Sym, Attr);
441
442 return false;
443}
444
Rafael Espindola61e3b912010-10-26 19:35:47 +0000445/// ParseDirectiveIdent
446/// ::= .ident string
447bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
448 if (getLexer().isNot(AsmToken::String))
449 return TokError("unexpected token in '.ident' directive");
450
451 StringRef Data = getTok().getIdentifier();
452
453 Lex();
454
Rafael Espindola61e3b912010-10-26 19:35:47 +0000455 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000456 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000457 ELF::SHF_MERGE |
458 ELF::SHF_STRINGS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000459 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000460 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000461
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000462 getStreamer().PushSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000463 getStreamer().SwitchSection(Comment);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000464 if (!SeenIdent) {
Rafael Espindola61e3b912010-10-26 19:35:47 +0000465 getStreamer().EmitIntValue(0, 1);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000466 SeenIdent = true;
467 }
Rafael Espindola61e3b912010-10-26 19:35:47 +0000468 getStreamer().EmitBytes(Data, 0);
469 getStreamer().EmitIntValue(0, 1);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000470 getStreamer().PopSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000471 return false;
472}
473
Rafael Espindola88182132010-10-27 15:18:17 +0000474/// ParseDirectiveSymver
475/// ::= .symver foo, bar2@zed
476bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
477 StringRef Name;
478 if (getParser().ParseIdentifier(Name))
479 return TokError("expected identifier in directive");
480
481 if (getLexer().isNot(AsmToken::Comma))
482 return TokError("expected a comma");
483
484 Lex();
485
486 StringRef AliasName;
487 if (getParser().ParseIdentifier(AliasName))
488 return TokError("expected identifier in directive");
489
490 if (AliasName.find('@') == StringRef::npos)
491 return TokError("expected a '@' in the name");
492
493 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
494 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
495 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
496
497 getStreamer().EmitAssignment(Alias, Value);
498 return false;
499}
500
Rafael Espindola484291c2010-11-01 14:28:48 +0000501/// ParseDirectiveWeakref
502/// ::= .weakref foo, bar
503bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
504 // FIXME: Share code with the other alias building directives.
505
506 StringRef AliasName;
507 if (getParser().ParseIdentifier(AliasName))
508 return TokError("expected identifier in directive");
509
510 if (getLexer().isNot(AsmToken::Comma))
511 return TokError("expected a comma");
512
513 Lex();
514
515 StringRef Name;
516 if (getParser().ParseIdentifier(Name))
517 return TokError("expected identifier in directive");
518
519 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
520
521 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
522
523 getStreamer().EmitWeakReference(Alias, Sym);
524 return false;
525}
526
Daniel Dunbar5146a092010-07-12 21:23:32 +0000527namespace llvm {
528
529MCAsmParserExtension *createELFAsmParser() {
530 return new ELFAsmParser;
531}
532
533}