blob: a3bd1d7dfb487e6004d250da61b1e0139644946f [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);
33
34public:
35 ELFAsmParser() {}
36
37 virtual void Initialize(MCAsmParser &Parser) {
38 // Call the base implementation.
39 this->MCAsmParserExtension::Initialize(Parser);
40
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000041 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
42 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
Matt Flemingf525c2a2010-07-20 21:12:46 +000043 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
44 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
45 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
46 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
48 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
49 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
50 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000051 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
52 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000053 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
Michael J. Spencere90ea132010-10-09 03:47:55 +000054 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
Rafael Espindola61e3b912010-10-26 19:35:47 +000055 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
Rafael Espindola88182132010-10-27 15:18:17 +000056 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
Rafael Espindola484291c2010-11-01 14:28:48 +000057 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
Daniel Dunbar5146a092010-07-12 21:23:32 +000058 }
59
Rafael Espindolad80781b2010-09-15 21:48:40 +000060 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
61 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000062 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000063 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
Daniel Dunbar5146a092010-07-12 21:23:32 +000064 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
65 SectionKind::getDataRel());
66 }
67 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000068 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
Daniel Dunbar5146a092010-07-12 21:23:32 +000069 MCSectionELF::SHF_EXECINSTR |
70 MCSectionELF::SHF_ALLOC, SectionKind::getText());
71 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000072 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000073 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +000074 MCSectionELF::SHF_WRITE |
75 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
76 }
77 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000078 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +000079 MCSectionELF::SHF_ALLOC,
80 SectionKind::getReadOnly());
81 }
82 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000083 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +000084 MCSectionELF::SHF_ALLOC |
85 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
86 SectionKind::getThreadData());
87 }
88 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000089 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +000090 MCSectionELF::SHF_ALLOC |
91 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
92 SectionKind::getThreadBSS());
93 }
94 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +000095 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +000096 MCSectionELF::SHF_ALLOC |
97 MCSectionELF::SHF_WRITE,
98 SectionKind::getDataRel());
99 }
100 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000101 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000102 MCSectionELF::SHF_ALLOC |
103 MCSectionELF::SHF_WRITE,
104 SectionKind::getReadOnlyWithRel());
105 }
106 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000107 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000108 MCSectionELF::SHF_ALLOC |
109 MCSectionELF::SHF_WRITE,
110 SectionKind::getReadOnlyWithRelLocal());
111 }
112 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000113 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +0000114 MCSectionELF::SHF_ALLOC |
115 MCSectionELF::SHF_WRITE,
116 SectionKind::getDataRel());
117 }
Eli Friedman21444ef2010-07-17 04:29:04 +0000118 bool ParseDirectiveSection(StringRef, SMLoc);
119 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000120 bool ParseDirectivePrevious(StringRef, SMLoc);
Michael J. Spencere90ea132010-10-09 03:47:55 +0000121 bool ParseDirectiveType(StringRef, SMLoc);
Rafael Espindola61e3b912010-10-26 19:35:47 +0000122 bool ParseDirectiveIdent(StringRef, SMLoc);
Rafael Espindola88182132010-10-27 15:18:17 +0000123 bool ParseDirectiveSymver(StringRef, SMLoc);
Rafael Espindola484291c2010-11-01 14:28:48 +0000124 bool ParseDirectiveWeakref(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000125
126private:
127 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000128};
129
130}
131
132bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
133 unsigned Flags, SectionKind Kind) {
134 if (getLexer().isNot(AsmToken::EndOfStatement))
135 return TokError("unexpected token in section switching directive");
136 Lex();
137
138 getStreamer().SwitchSection(getContext().getELFSection(
139 Section, Type, Flags, Kind));
140
141 return false;
142}
143
Eli Friedman21444ef2010-07-17 04:29:04 +0000144bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000145 StringRef Name;
146 if (getParser().ParseIdentifier(Name))
147 return TokError("expected identifier in directive");
148 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
149
150 if (getLexer().isNot(AsmToken::Comma))
151 return TokError("unexpected token in directive");
152 Lex();
153
154 const MCExpr *Expr;
155 if (getParser().ParseExpression(Expr))
156 return true;
157
158 if (getLexer().isNot(AsmToken::EndOfStatement))
159 return TokError("unexpected token in directive");
160
161 getStreamer().EmitELFSize(Sym, Expr);
162 return false;
163}
164
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000165bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
166 // A section name can contain -, so we cannot just use
167 // ParseIdentifier.
168 SMLoc FirstLoc = getLexer().getLoc();
169 unsigned Size = 0;
170
171 for (;;) {
172 StringRef Tmp;
173 unsigned CurSize;
174
175 SMLoc PrevLoc = getLexer().getLoc();
176 if (getLexer().is(AsmToken::Minus)) {
177 CurSize = 1;
178 Lex(); // Consume the "-".
179 } else if (!getParser().ParseIdentifier(Tmp))
180 CurSize = Tmp.size();
181 else
182 break;
183
184 Size += CurSize;
185 SectionName = StringRef(FirstLoc.getPointer(), Size);
186
187 // Make sure the following token is adjacent.
188 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
189 break;
190 }
191 if (Size == 0)
192 return true;
193
194 return false;
195}
196
Rafael Espindola25958732010-11-24 21:57:39 +0000197static SectionKind computeSectionKind(unsigned Flags) {
198 if (Flags & MCSectionELF::SHF_EXECINSTR)
199 return SectionKind::getText();
200 if (Flags & MCSectionELF::SHF_TLS)
201 return SectionKind::getThreadData();
202 return SectionKind::getDataRel();
203}
204
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000205static int parseSectionFlags(StringRef flagsStr) {
206 int flags = 0;
207
208 for (unsigned i = 0; i < flagsStr.size(); i++) {
209 switch (flagsStr[i]) {
210 case 'a':
211 flags |= MCSectionELF::SHF_ALLOC;
212 break;
213 case 'x':
214 flags |= MCSectionELF::SHF_EXECINSTR;
215 break;
216 case 'w':
217 flags |= MCSectionELF::SHF_WRITE;
218 break;
219 case 'M':
220 flags |= MCSectionELF::SHF_MERGE;
221 break;
222 case 'S':
223 flags |= MCSectionELF::SHF_STRINGS;
224 break;
225 case 'T':
226 flags |= MCSectionELF::SHF_TLS;
227 break;
228 case 'c':
229 flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
230 break;
231 case 'd':
232 flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
233 break;
234 case 'G':
235 flags |= MCSectionELF::SHF_GROUP;
236 break;
237 default:
238 return -1;
239 }
240 }
241
242 return flags;
243}
244
Eli Friedman21444ef2010-07-17 04:29:04 +0000245// FIXME: This is a work in progress.
246bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
247 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000248
249 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000250 return TokError("expected identifier in directive");
251
Eli Friedman21444ef2010-07-17 04:29:04 +0000252 StringRef TypeName;
253 int64_t Size = 0;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000254 StringRef GroupName;
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000255 unsigned Flags = 0;
256
257 // Set the defaults first.
258 if (SectionName == ".fini" || SectionName == ".init" ||
259 SectionName == ".rodata")
260 Flags |= MCSectionELF::SHF_ALLOC;
261 if (SectionName == ".fini" || SectionName == ".init")
262 Flags |= MCSectionELF::SHF_EXECINSTR;
263
Eli Friedman21444ef2010-07-17 04:29:04 +0000264 if (getLexer().is(AsmToken::Comma)) {
265 Lex();
266
267 if (getLexer().isNot(AsmToken::String))
268 return TokError("expected string in directive");
269
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000270 StringRef FlagsStr = getTok().getStringContents();
Eli Friedman21444ef2010-07-17 04:29:04 +0000271 Lex();
272
Rafael Espindola6b8e4352010-11-25 15:32:56 +0000273 int extraFlags = parseSectionFlags(FlagsStr);
274 if (extraFlags < 0)
275 return TokError("unknown flag");
276 Flags |= extraFlags;
277
278 bool Mergeable = Flags & MCSectionELF::SHF_MERGE;
279 bool Group = Flags & MCSectionELF::SHF_GROUP;
Eli Friedman21444ef2010-07-17 04:29:04 +0000280
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000281 if (getLexer().isNot(AsmToken::Comma)) {
282 if (Mergeable)
283 return TokError("Mergeable section must specify the type");
284 if (Group)
285 return TokError("Group section must specify the type");
286 } else {
287 Lex();
Rafael Espindolad4a35262010-11-12 15:47:08 +0000288 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
289 return TokError("expected '@' or '%' before type");
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000290
291 Lex();
292 if (getParser().ParseIdentifier(TypeName))
293 return TokError("expected identifier in directive");
294
295 if (Mergeable) {
296 if (getLexer().isNot(AsmToken::Comma))
297 return TokError("expected the entry size");
298 Lex();
299 if (getParser().ParseAbsoluteExpression(Size))
300 return true;
301 if (Size <= 0)
302 return TokError("entry size must be positive");
303 }
304
305 if (Group) {
306 if (getLexer().isNot(AsmToken::Comma))
307 return TokError("expected group name");
308 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000309 if (getParser().ParseIdentifier(GroupName))
310 return true;
Eli Friedman21444ef2010-07-17 04:29:04 +0000311 if (getLexer().is(AsmToken::Comma)) {
312 Lex();
Rafael Espindolaf4b0f3e2010-10-28 21:33:33 +0000313 StringRef Linkage;
314 if (getParser().ParseIdentifier(Linkage))
Eli Friedman21444ef2010-07-17 04:29:04 +0000315 return true;
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000316 if (Linkage != "comdat")
317 return TokError("Linkage must be 'comdat'");
Eli Friedman21444ef2010-07-17 04:29:04 +0000318 }
319 }
320 }
321 }
322
323 if (getLexer().isNot(AsmToken::EndOfStatement))
324 return TokError("unexpected token in directive");
325
Rafael Espindolac85dca62011-01-23 04:28:49 +0000326 unsigned Type = ELF::SHT_PROGBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000327
Eli Friedman21444ef2010-07-17 04:29:04 +0000328 if (!TypeName.empty()) {
329 if (TypeName == "init_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000330 Type = ELF::SHT_INIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000331 else if (TypeName == "fini_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000332 Type = ELF::SHT_FINI_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000333 else if (TypeName == "preinit_array")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000334 Type = ELF::SHT_PREINIT_ARRAY;
Eli Friedman21444ef2010-07-17 04:29:04 +0000335 else if (TypeName == "nobits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000336 Type = ELF::SHT_NOBITS;
Eli Friedman21444ef2010-07-17 04:29:04 +0000337 else if (TypeName == "progbits")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000338 Type = ELF::SHT_PROGBITS;
Rafael Espindola98976612010-12-26 21:30:59 +0000339 else if (TypeName == "note")
Rafael Espindolac85dca62011-01-23 04:28:49 +0000340 Type = ELF::SHT_NOTE;
Eli Friedman21444ef2010-07-17 04:29:04 +0000341 else
342 return TokError("unknown section type");
343 }
344
Rafael Espindola25958732010-11-24 21:57:39 +0000345 SectionKind Kind = computeSectionKind(Flags);
Eli Friedman21444ef2010-07-17 04:29:04 +0000346 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000347 Flags, Kind, Size,
348 GroupName));
Eli Friedman21444ef2010-07-17 04:29:04 +0000349 return false;
350}
351
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000352bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
353 const MCSection *PreviousSection = getStreamer().getPreviousSection();
354 if (PreviousSection != NULL)
355 getStreamer().SwitchSection(PreviousSection);
356
357 return false;
358}
359
Michael J. Spencere90ea132010-10-09 03:47:55 +0000360/// ParseDirectiveELFType
361/// ::= .type identifier , @attribute
362bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
363 StringRef Name;
364 if (getParser().ParseIdentifier(Name))
365 return TokError("expected identifier in directive");
366
367 // Handle the identifier as the key symbol.
368 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
369
370 if (getLexer().isNot(AsmToken::Comma))
371 return TokError("unexpected token in '.type' directive");
372 Lex();
373
Rafael Espindolad4a35262010-11-12 15:47:08 +0000374 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
375 return TokError("expected '@' or '%' before type");
Michael J. Spencere90ea132010-10-09 03:47:55 +0000376 Lex();
377
378 StringRef Type;
379 SMLoc TypeLoc;
380
381 TypeLoc = getLexer().getLoc();
382 if (getParser().ParseIdentifier(Type))
383 return TokError("expected symbol type in directive");
384
385 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
386 .Case("function", MCSA_ELF_TypeFunction)
387 .Case("object", MCSA_ELF_TypeObject)
388 .Case("tls_object", MCSA_ELF_TypeTLS)
389 .Case("common", MCSA_ELF_TypeCommon)
390 .Case("notype", MCSA_ELF_TypeNoType)
Rafael Espindolae13a0ff2010-11-13 04:51:02 +0000391 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
Michael J. Spencere90ea132010-10-09 03:47:55 +0000392 .Default(MCSA_Invalid);
393
394 if (Attr == MCSA_Invalid)
395 return Error(TypeLoc, "unsupported attribute in '.type' directive");
396
397 if (getLexer().isNot(AsmToken::EndOfStatement))
398 return TokError("unexpected token in '.type' directive");
399
400 Lex();
401
402 getStreamer().EmitSymbolAttribute(Sym, Attr);
403
404 return false;
405}
406
Rafael Espindola61e3b912010-10-26 19:35:47 +0000407/// ParseDirectiveIdent
408/// ::= .ident string
409bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
410 if (getLexer().isNot(AsmToken::String))
411 return TokError("unexpected token in '.ident' directive");
412
413 StringRef Data = getTok().getIdentifier();
414
415 Lex();
416
417 const MCSection *OldSection = getStreamer().getCurrentSection();
418 const MCSection *Comment =
Rafael Espindolac85dca62011-01-23 04:28:49 +0000419 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
Rafael Espindola61e3b912010-10-26 19:35:47 +0000420 MCSectionELF::SHF_MERGE |
421 MCSectionELF::SHF_STRINGS,
422 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000423 1, "");
Rafael Espindola61e3b912010-10-26 19:35:47 +0000424
425 static bool First = true;
426
427 getStreamer().SwitchSection(Comment);
428 if (First)
429 getStreamer().EmitIntValue(0, 1);
430 First = false;
431 getStreamer().EmitBytes(Data, 0);
432 getStreamer().EmitIntValue(0, 1);
433 getStreamer().SwitchSection(OldSection);
434 return false;
435}
436
Rafael Espindola88182132010-10-27 15:18:17 +0000437/// ParseDirectiveSymver
438/// ::= .symver foo, bar2@zed
439bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
440 StringRef Name;
441 if (getParser().ParseIdentifier(Name))
442 return TokError("expected identifier in directive");
443
444 if (getLexer().isNot(AsmToken::Comma))
445 return TokError("expected a comma");
446
447 Lex();
448
449 StringRef AliasName;
450 if (getParser().ParseIdentifier(AliasName))
451 return TokError("expected identifier in directive");
452
453 if (AliasName.find('@') == StringRef::npos)
454 return TokError("expected a '@' in the name");
455
456 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
457 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
458 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
459
460 getStreamer().EmitAssignment(Alias, Value);
461 return false;
462}
463
Rafael Espindola484291c2010-11-01 14:28:48 +0000464/// ParseDirectiveWeakref
465/// ::= .weakref foo, bar
466bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
467 // FIXME: Share code with the other alias building directives.
468
469 StringRef AliasName;
470 if (getParser().ParseIdentifier(AliasName))
471 return TokError("expected identifier in directive");
472
473 if (getLexer().isNot(AsmToken::Comma))
474 return TokError("expected a comma");
475
476 Lex();
477
478 StringRef Name;
479 if (getParser().ParseIdentifier(Name))
480 return TokError("expected identifier in directive");
481
482 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
483
484 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
485
486 getStreamer().EmitWeakReference(Alias, Sym);
487 return false;
488}
489
Daniel Dunbar5146a092010-07-12 21:23:32 +0000490namespace llvm {
491
492MCAsmParserExtension *createELFAsmParser() {
493 return new ELFAsmParser;
494}
495
496}