blob: 972e2465d6f822e549b01d3f0f05c649be6aeedb [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");
Jim Grosbachcc866d52011-07-25 17:11:29 +000050 AddDirectiveHandler<
51 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
52 AddDirectiveHandler<
53 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
54 AddDirectiveHandler<
55 &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
56 AddDirectiveHandler<
57 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000058 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
Jim Grosbachcc866d52011-07-25 17:11:29 +000059 AddDirectiveHandler<
60 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
Rafael Espindola7768a9d2011-02-16 01:08:29 +000061 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000062 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000063 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
Michael J. Spencere90ea132010-10-09 03:47:55 +000064 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
Rafael Espindola61e3b912010-10-26 19:35:47 +000065 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
Rafael Espindola88182132010-10-27 15:18:17 +000066 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
Rafael Espindola484291c2010-11-01 14:28:48 +000067 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
Daniel Dunbar5146a092010-07-12 21:23:32 +000068 }
69
Rafael Espindolad80781b2010-09-15 21:48:40 +000070 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
71 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000072 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000073 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000074 ELF::SHF_WRITE |ELF::SHF_ALLOC,
Daniel Dunbar5146a092010-07-12 21:23:32 +000075 SectionKind::getDataRel());
76 }
77 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000078 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000079 ELF::SHF_EXECINSTR |
80 ELF::SHF_ALLOC, SectionKind::getText());
Daniel Dunbar5146a092010-07-12 21:23:32 +000081 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000082 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000083 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000084 ELF::SHF_WRITE |
85 ELF::SHF_ALLOC, SectionKind::getBSS());
Matt Flemingf525c2a2010-07-20 21:12:46 +000086 }
87 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000088 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000089 ELF::SHF_ALLOC,
Matt Flemingf525c2a2010-07-20 21:12:46 +000090 SectionKind::getReadOnly());
91 }
92 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000093 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +000094 ELF::SHF_ALLOC |
95 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +000096 SectionKind::getThreadData());
97 }
98 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000099 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000100 ELF::SHF_ALLOC |
101 ELF::SHF_TLS | ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000102 SectionKind::getThreadBSS());
103 }
104 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000105 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000106 ELF::SHF_ALLOC |
107 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000108 SectionKind::getDataRel());
109 }
110 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000111 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000112 ELF::SHF_ALLOC |
113 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000114 SectionKind::getReadOnlyWithRel());
115 }
116 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000117 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000118 ELF::SHF_ALLOC |
119 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000120 SectionKind::getReadOnlyWithRelLocal());
121 }
122 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000123 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000124 ELF::SHF_ALLOC |
125 ELF::SHF_WRITE,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000126 SectionKind::getDataRel());
127 }
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000128 bool ParseDirectivePushSection(StringRef, SMLoc);
129 bool ParseDirectivePopSection(StringRef, SMLoc);
Eli Friedman21444ef2010-07-17 04:29:04 +0000130 bool ParseDirectiveSection(StringRef, SMLoc);
131 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000132 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000133 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000134 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000135 bool ParseDirectiveSymver(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000136 bool ParseDirectiveWeakref(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000137
138private:
139 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000140};
141
142}
143
144bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
145 unsigned Flags, SectionKind Kind) {
146 if (getLexer().isNot(AsmToken::EndOfStatement))
147 return TokError("unexpected token in section switching directive");
148 Lex();
149
150 getStreamer().SwitchSection(getContext().getELFSection(
151 Section, Type, Flags, Kind));
152
153 return false;
154}
155
Eli Friedman21444ef2010-07-17 04:29:04 +0000156bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000157 StringRef Name;
158 if (getParser().ParseIdentifier(Name))
159 return TokError("expected identifier in directive");
160 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
161
162 if (getLexer().isNot(AsmToken::Comma))
163 return TokError("unexpected token in directive");
164 Lex();
165
166 const MCExpr *Expr;
167 if (getParser().ParseExpression(Expr))
168 return true;
169
170 if (getLexer().isNot(AsmToken::EndOfStatement))
171 return TokError("unexpected token in directive");
172
173 getStreamer().EmitELFSize(Sym, Expr);
174 return false;
175}
176
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000177bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
178 // A section name can contain -, so we cannot just use
179 // ParseIdentifier.
180 SMLoc FirstLoc = getLexer().getLoc();
181 unsigned Size = 0;
182
Rafael Espindola184640e2011-01-24 18:02:54 +0000183 if (getLexer().is(AsmToken::String)) {
184 SectionName = getTok().getIdentifier();
185 Lex();
186 return false;
187 }
188
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000189 for (;;) {
190 StringRef Tmp;
191 unsigned CurSize;
192
193 SMLoc PrevLoc = getLexer().getLoc();
194 if (getLexer().is(AsmToken::Minus)) {
195 CurSize = 1;
196 Lex(); // Consume the "-".
Rafael Espindola184640e2011-01-24 18:02:54 +0000197 } else if (getLexer().is(AsmToken::String)) {
198 CurSize = getTok().getIdentifier().size() + 2;
199 Lex();
200 } else if (getLexer().is(AsmToken::Identifier)) {
201 CurSize = getTok().getIdentifier().size();
202 Lex();
203 } else {
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000204 break;
Rafael Espindola184640e2011-01-24 18:02:54 +0000205 }
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000206
207 Size += CurSize;
208 SectionName = StringRef(FirstLoc.getPointer(), Size);
209
210 // Make sure the following token is adjacent.
211 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
212 break;
213 }
214 if (Size == 0)
215 return true;
216
217 return false;
218}
219
Rafael Espindola25958732010-11-24 21:57:39 +0000220static SectionKind computeSectionKind(unsigned Flags) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000221 if (Flags & ELF::SHF_EXECINSTR)
Rafael Espindola25958732010-11-24 21:57:39 +0000222 return SectionKind::getText();
Rafael Espindola1c130262011-01-23 04:43:11 +0000223 if (Flags & ELF::SHF_TLS)
Rafael Espindola25958732010-11-24 21:57:39 +0000224 return SectionKind::getThreadData();
225 return SectionKind::getDataRel();
226}
227
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000228static int parseSectionFlags(StringRef flagsStr) {
229 int flags = 0;
230
231 for (unsigned i = 0; i < flagsStr.size(); i++) {
232 switch (flagsStr[i]) {
233 case 'a':
Rafael Espindola1c130262011-01-23 04:43:11 +0000234 flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000235 break;
236 case 'x':
Rafael Espindola1c130262011-01-23 04:43:11 +0000237 flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000238 break;
239 case 'w':
Rafael Espindola1c130262011-01-23 04:43:11 +0000240 flags |= ELF::SHF_WRITE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000241 break;
242 case 'M':
Rafael Espindola1c130262011-01-23 04:43:11 +0000243 flags |= ELF::SHF_MERGE;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000244 break;
245 case 'S':
Rafael Espindola1c130262011-01-23 04:43:11 +0000246 flags |= ELF::SHF_STRINGS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000247 break;
248 case 'T':
Rafael Espindola1c130262011-01-23 04:43:11 +0000249 flags |= ELF::SHF_TLS;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000250 break;
251 case 'c':
Rafael Espindola1c130262011-01-23 04:43:11 +0000252 flags |= ELF::XCORE_SHF_CP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000253 break;
254 case 'd':
Rafael Espindola1c130262011-01-23 04:43:11 +0000255 flags |= ELF::XCORE_SHF_DP_SECTION;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000256 break;
257 case 'G':
Rafael Espindola1c130262011-01-23 04:43:11 +0000258 flags |= ELF::SHF_GROUP;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000259 break;
260 default:
261 return -1;
262 }
263 }
264
265 return flags;
266}
267
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000268bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
269 getStreamer().PushSection();
270
271 if (ParseDirectiveSection(s, loc)) {
272 getStreamer().PopSection();
273 return true;
274 }
275
276 return false;
277}
278
279bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
280 if (!getStreamer().PopSection())
281 return TokError(".popsection without corresponding .pushsection");
282 return false;
283}
284
Eli Friedman21444ef2010-07-17 04:29:04 +0000285// FIXME: This is a work in progress.
286bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
287 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000288
289 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000290 return TokError("expected identifier in directive");
291
Eli Friedman21444ef2010-07-17 04:29:04 +0000292 StringRef TypeName;
293 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000294 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000295 unsigned Flags = 0;
296
297 // Set the defaults first.
298 if (SectionName == ".fini" || SectionName == ".init" ||
299 SectionName == ".rodata")
Rafael Espindola1c130262011-01-23 04:43:11 +0000300 Flags |= ELF::SHF_ALLOC;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000301 if (SectionName == ".fini" || SectionName == ".init")
Rafael Espindola1c130262011-01-23 04:43:11 +0000302 Flags |= ELF::SHF_EXECINSTR;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000303
Eli Friedman21444ef2010-07-17 04:29:04 +0000304 if (getLexer().is(AsmToken::Comma)) {
305 Lex();
306
307 if (getLexer().isNot(AsmToken::String))
308 return TokError("expected string in directive");
309
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000310 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000311 Lex();
312
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000313 int extraFlags = parseSectionFlags(FlagsStr);
314 if (extraFlags < 0)
315 return TokError("unknown flag");
316 Flags |= extraFlags;
317
Rafael Espindola1c130262011-01-23 04:43:11 +0000318 bool Mergeable = Flags & ELF::SHF_MERGE;
319 bool Group = Flags & ELF::SHF_GROUP;
Eli Friedman21444ef2010-07-17 04:29:04 +0000320
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000321 if (getLexer().isNot(AsmToken::Comma)) {
322 if (Mergeable)
323 return TokError("Mergeable section must specify the type");
324 if (Group)
325 return TokError("Group section must specify the type");
326 } else {
327 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000328 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
329 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000330
331 Lex();
332 if (getParser().ParseIdentifier(TypeName))
333 return TokError("expected identifier in directive");
334
335 if (Mergeable) {
336 if (getLexer().isNot(AsmToken::Comma))
337 return TokError("expected the entry size");
338 Lex();
339 if (getParser().ParseAbsoluteExpression(Size))
340 return true;
341 if (Size <= 0)
342 return TokError("entry size must be positive");
343 }
344
345 if (Group) {
346 if (getLexer().isNot(AsmToken::Comma))
347 return TokError("expected group name");
348 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000349 if (getParser().ParseIdentifier(GroupName))
350 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000351 if (getLexer().is(AsmToken::Comma)) {
352 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000353 StringRef Linkage;
354 if (getParser().ParseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000355 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000356 if (Linkage != "comdat")
357 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000358 }
359 }
360 }
361 }
362
363 if (getLexer().isNot(AsmToken::EndOfStatement))
364 return TokError("unexpected token in directive");
365
Rafael Espindolac85dca62011-01-23 04:28:49 +0000366 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000367
Eli Friedman21444ef2010-07-17 04:29:04 +0000368 if (!TypeName.empty()) {
369 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000370 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000371 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000372 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000373 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000374 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000375 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000376 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000377 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000378 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000379 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000380 Type = ELF::SHT_NOTE;
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +0000381 else if (TypeName == "unwind")
382 Type = ELF::SHT_X86_64_UNWIND;
Eli Friedman21444ef2010-07-17 04:29:04 +0000383 else
384 return TokError("unknown section type");
385 }
386
Rafael Espindola25958732010-11-24 21:57:39 +0000387 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000388 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000389 Flags, Kind, Size,
390 GroupName));
Eli Friedman21444ef2010-07-17 04:29:04 +0000391 return false;
392}
393
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000394bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
395 const MCSection *PreviousSection = getStreamer().getPreviousSection();
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000396 if (PreviousSection == NULL)
397 return TokError(".previous without corresponding .section");
398 getStreamer().SwitchSection(PreviousSection);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000399
400 return false;
401}
402
Michael J. Spencere90ea132010-10-09 03:47:55 +0000403/// ParseDirectiveELFType
404/// ::= .type identifier , @attribute
405bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
406 StringRef Name;
407 if (getParser().ParseIdentifier(Name))
408 return TokError("expected identifier in directive");
409
410 // Handle the identifier as the key symbol.
411 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
412
413 if (getLexer().isNot(AsmToken::Comma))
414 return TokError("unexpected token in '.type' directive");
415 Lex();
416
Rafael Espindolad4a35262010-11-12 15:47:08 +0000417 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
418 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000419 Lex();
420
421 StringRef Type;
422 SMLoc TypeLoc;
423
424 TypeLoc = getLexer().getLoc();
425 if (getParser().ParseIdentifier(Type))
426 return TokError("expected symbol type in directive");
427
428 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
429 .Case("function", MCSA_ELF_TypeFunction)
430 .Case("object", MCSA_ELF_TypeObject)
431 .Case("tls_object", MCSA_ELF_TypeTLS)
432 .Case("common", MCSA_ELF_TypeCommon)
433 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000434 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000435 .Default(MCSA_Invalid);
436
437 if (Attr == MCSA_Invalid)
438 return Error(TypeLoc, "unsupported attribute in '.type' directive");
439
440 if (getLexer().isNot(AsmToken::EndOfStatement))
441 return TokError("unexpected token in '.type' directive");
442
443 Lex();
444
445 getStreamer().EmitSymbolAttribute(Sym, Attr);
446
447 return false;
448}
449
Rafael Espindola61e3b912010-10-26 19:35:47 +0000450/// ParseDirectiveIdent
451/// ::= .ident string
452bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
453 if (getLexer().isNot(AsmToken::String))
454 return TokError("unexpected token in '.ident' directive");
455
456 StringRef Data = getTok().getIdentifier();
457
458 Lex();
459
Rafael Espindola61e3b912010-10-26 19:35:47 +0000460 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000461 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola1c130262011-01-23 04:43:11 +0000462 ELF::SHF_MERGE |
463 ELF::SHF_STRINGS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000464 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000465 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000466
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000467 getStreamer().PushSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000468 getStreamer().SwitchSection(Comment);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000469 if (!SeenIdent) {
Rafael Espindola61e3b912010-10-26 19:35:47 +0000470 getStreamer().EmitIntValue(0, 1);
Joerg Sonnenbergera04663e2011-02-22 16:53:11 +0000471 SeenIdent = true;
472 }
Rafael Espindola61e3b912010-10-26 19:35:47 +0000473 getStreamer().EmitBytes(Data, 0);
474 getStreamer().EmitIntValue(0, 1);
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000475 getStreamer().PopSection();
Rafael Espindola61e3b912010-10-26 19:35:47 +0000476 return false;
477}
478
Rafael Espindola88182132010-10-27 15:18:17 +0000479/// ParseDirectiveSymver
480/// ::= .symver foo, bar2@zed
481bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
482 StringRef Name;
483 if (getParser().ParseIdentifier(Name))
484 return TokError("expected identifier in directive");
485
486 if (getLexer().isNot(AsmToken::Comma))
487 return TokError("expected a comma");
488
489 Lex();
490
491 StringRef AliasName;
492 if (getParser().ParseIdentifier(AliasName))
493 return TokError("expected identifier in directive");
494
495 if (AliasName.find('@') == StringRef::npos)
496 return TokError("expected a '@' in the name");
497
498 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
499 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
500 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
501
502 getStreamer().EmitAssignment(Alias, Value);
503 return false;
504}
505
Rafael Espindola484291c2010-11-01 14:28:48 +0000506/// ParseDirectiveWeakref
507/// ::= .weakref foo, bar
508bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
509 // FIXME: Share code with the other alias building directives.
510
511 StringRef AliasName;
512 if (getParser().ParseIdentifier(AliasName))
513 return TokError("expected identifier in directive");
514
515 if (getLexer().isNot(AsmToken::Comma))
516 return TokError("expected a comma");
517
518 Lex();
519
520 StringRef Name;
521 if (getParser().ParseIdentifier(Name))
522 return TokError("expected identifier in directive");
523
524 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
525
526 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
527
528 getStreamer().EmitWeakReference(Alias, Sym);
529 return false;
530}
531
Daniel Dunbar5146a092010-07-12 21:23:32 +0000532namespace llvm {
533
534MCAsmParserExtension *createELFAsmParser() {
535 return new ELFAsmParser;
536}
537
538}