[C++11] Add 'override' keyword to virtual methods that override their base class.
[oota-llvm.git] / lib / MC / MCParser / ELFAsmParser.cpp
1 //===- 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"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCParser/MCAsmLexer.h"
17 #include "llvm/MC/MCSectionELF.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Support/ELF.h"
21 using namespace llvm;
22
23 namespace {
24
25 class ELFAsmParser : public MCAsmParserExtension {
26   template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
27   void addDirectiveHandler(StringRef Directive) {
28     MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
29         this, HandleDirective<ELFAsmParser, HandlerMethod>);
30
31     getParser().addDirectiveHandler(Directive, Handler);
32   }
33
34   bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
35                           SectionKind Kind);
36
37 public:
38   ELFAsmParser() { BracketExpressionsSupported = true; }
39
40   void Initialize(MCAsmParser &Parser) override {
41     // Call the base implementation.
42     this->MCAsmParserExtension::Initialize(Parser);
43
44     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
45     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
46     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
47     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
48     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
49     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
50     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");
58     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
59     addDirectiveHandler<
60       &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
61     addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
62     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
63     addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
64     addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
65     addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
66     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
67     addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
68     addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
69     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
70     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
71     addDirectiveHandler<
72       &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
73     addDirectiveHandler<
74       &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
75     addDirectiveHandler<
76       &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
77     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
78   }
79
80   // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
81   // the best way for us to get access to it?
82   bool ParseSectionDirectiveData(StringRef, SMLoc) {
83     return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
84                               ELF::SHF_WRITE |ELF::SHF_ALLOC,
85                               SectionKind::getDataRel());
86   }
87   bool ParseSectionDirectiveText(StringRef, SMLoc) {
88     return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
89                               ELF::SHF_EXECINSTR |
90                               ELF::SHF_ALLOC, SectionKind::getText());
91   }
92   bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
93     return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
94                               ELF::SHF_WRITE |
95                               ELF::SHF_ALLOC, SectionKind::getBSS());
96   }
97   bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
98     return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
99                               ELF::SHF_ALLOC,
100                               SectionKind::getReadOnly());
101   }
102   bool ParseSectionDirectiveTData(StringRef, SMLoc) {
103     return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
104                               ELF::SHF_ALLOC |
105                               ELF::SHF_TLS | ELF::SHF_WRITE,
106                               SectionKind::getThreadData());
107   }
108   bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
109     return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
110                               ELF::SHF_ALLOC |
111                               ELF::SHF_TLS | ELF::SHF_WRITE,
112                               SectionKind::getThreadBSS());
113   }
114   bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
115     return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
116                               ELF::SHF_ALLOC |
117                               ELF::SHF_WRITE,
118                               SectionKind::getDataRel());
119   }
120   bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
121     return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
122                               ELF::SHF_ALLOC |
123                               ELF::SHF_WRITE,
124                               SectionKind::getReadOnlyWithRel());
125   }
126   bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
127     return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
128                               ELF::SHF_ALLOC |
129                               ELF::SHF_WRITE,
130                               SectionKind::getReadOnlyWithRelLocal());
131   }
132   bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
133     return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
134                               ELF::SHF_ALLOC |
135                               ELF::SHF_WRITE,
136                               SectionKind::getDataRel());
137   }
138   bool ParseDirectivePushSection(StringRef, SMLoc);
139   bool ParseDirectivePopSection(StringRef, SMLoc);
140   bool ParseDirectiveSection(StringRef, SMLoc);
141   bool ParseDirectiveSize(StringRef, SMLoc);
142   bool ParseDirectivePrevious(StringRef, SMLoc);
143   bool ParseDirectiveType(StringRef, SMLoc);
144   bool ParseDirectiveIdent(StringRef, SMLoc);
145   bool ParseDirectiveSymver(StringRef, SMLoc);
146   bool ParseDirectiveVersion(StringRef, SMLoc);
147   bool ParseDirectiveWeakref(StringRef, SMLoc);
148   bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
149   bool ParseDirectiveSubsection(StringRef, SMLoc);
150
151 private:
152   bool ParseSectionName(StringRef &SectionName);
153   bool ParseSectionArguments(bool IsPush);
154   unsigned parseSunStyleSectionFlags();
155 };
156
157 }
158
159 /// ParseDirectiveSymbolAttribute
160 ///  ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
161 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
162   MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
163     .Case(".weak", MCSA_Weak)
164     .Case(".local", MCSA_Local)
165     .Case(".hidden", MCSA_Hidden)
166     .Case(".internal", MCSA_Internal)
167     .Case(".protected", MCSA_Protected)
168     .Default(MCSA_Invalid);
169   assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
170   if (getLexer().isNot(AsmToken::EndOfStatement)) {
171     for (;;) {
172       StringRef Name;
173
174       if (getParser().parseIdentifier(Name))
175         return TokError("expected identifier in directive");
176
177       MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
178
179       getStreamer().EmitSymbolAttribute(Sym, Attr);
180
181       if (getLexer().is(AsmToken::EndOfStatement))
182         break;
183
184       if (getLexer().isNot(AsmToken::Comma))
185         return TokError("unexpected token in directive");
186       Lex();
187     }
188   }
189
190   Lex();
191   return false;
192 }
193
194 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
195                                       unsigned Flags, SectionKind Kind) {
196   const MCExpr *Subsection = 0;
197   if (getLexer().isNot(AsmToken::EndOfStatement)) {
198     if (getParser().parseExpression(Subsection))
199       return true;
200   }
201
202   getStreamer().SwitchSection(getContext().getELFSection(
203                                 Section, Type, Flags, Kind),
204                               Subsection);
205
206   return false;
207 }
208
209 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
210   StringRef Name;
211   if (getParser().parseIdentifier(Name))
212     return TokError("expected identifier in directive");
213   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
214
215   if (getLexer().isNot(AsmToken::Comma))
216     return TokError("unexpected token in directive");
217   Lex();
218
219   const MCExpr *Expr;
220   if (getParser().parseExpression(Expr))
221     return true;
222
223   if (getLexer().isNot(AsmToken::EndOfStatement))
224     return TokError("unexpected token in directive");
225
226   getStreamer().EmitELFSize(Sym, Expr);
227   return false;
228 }
229
230 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
231   // A section name can contain -, so we cannot just use
232   // parseIdentifier.
233   SMLoc FirstLoc = getLexer().getLoc();
234   unsigned Size = 0;
235
236   if (getLexer().is(AsmToken::String)) {
237     SectionName = getTok().getIdentifier();
238     Lex();
239     return false;
240   }
241
242   for (;;) {
243     unsigned CurSize;
244
245     SMLoc PrevLoc = getLexer().getLoc();
246     if (getLexer().is(AsmToken::Minus)) {
247       CurSize = 1;
248       Lex(); // Consume the "-".
249     } else if (getLexer().is(AsmToken::String)) {
250       CurSize = getTok().getIdentifier().size() + 2;
251       Lex();
252     } else if (getLexer().is(AsmToken::Identifier)) {
253       CurSize = getTok().getIdentifier().size();
254       Lex();
255     } else {
256       break;
257     }
258
259     Size += CurSize;
260     SectionName = StringRef(FirstLoc.getPointer(), Size);
261
262     // Make sure the following token is adjacent.
263     if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
264       break;
265   }
266   if (Size == 0)
267     return true;
268
269   return false;
270 }
271
272 static SectionKind computeSectionKind(unsigned Flags) {
273   if (Flags & ELF::SHF_EXECINSTR)
274     return SectionKind::getText();
275   if (Flags & ELF::SHF_TLS)
276     return SectionKind::getThreadData();
277   return SectionKind::getDataRel();
278 }
279
280 static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
281   unsigned flags = 0;
282
283   for (unsigned i = 0; i < flagsStr.size(); i++) {
284     switch (flagsStr[i]) {
285     case 'a':
286       flags |= ELF::SHF_ALLOC;
287       break;
288     case 'e':
289       flags |= ELF::SHF_EXCLUDE;
290       break;
291     case 'x':
292       flags |= ELF::SHF_EXECINSTR;
293       break;
294     case 'w':
295       flags |= ELF::SHF_WRITE;
296       break;
297     case 'M':
298       flags |= ELF::SHF_MERGE;
299       break;
300     case 'S':
301       flags |= ELF::SHF_STRINGS;
302       break;
303     case 'T':
304       flags |= ELF::SHF_TLS;
305       break;
306     case 'c':
307       flags |= ELF::XCORE_SHF_CP_SECTION;
308       break;
309     case 'd':
310       flags |= ELF::XCORE_SHF_DP_SECTION;
311       break;
312     case 'G':
313       flags |= ELF::SHF_GROUP;
314       break;
315     case '?':
316       *UseLastGroup = true;
317       break;
318     default:
319       return -1U;
320     }
321   }
322
323   return flags;
324 }
325
326 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
327   unsigned flags = 0;
328   while (getLexer().is(AsmToken::Hash)) {
329     Lex(); // Eat the #.
330
331     if (!getLexer().is(AsmToken::Identifier))
332       return -1U;
333
334     StringRef flagId = getTok().getIdentifier();
335     if (flagId == "alloc")
336       flags |= ELF::SHF_ALLOC;
337     else if (flagId == "execinstr")
338       flags |= ELF::SHF_EXECINSTR;
339     else if (flagId == "write")
340       flags |= ELF::SHF_WRITE;
341     else if (flagId == "tls")
342       flags |= ELF::SHF_TLS;
343     else
344       return -1U;
345
346     Lex(); // Eat the flag.
347
348     if (!getLexer().is(AsmToken::Comma))
349         break;
350     Lex(); // Eat the comma.
351   }
352   return flags;
353 }
354
355
356 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
357   getStreamer().PushSection();
358
359   if (ParseSectionArguments(/*IsPush=*/true)) {
360     getStreamer().PopSection();
361     return true;
362   }
363
364   return false;
365 }
366
367 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
368   if (!getStreamer().PopSection())
369     return TokError(".popsection without corresponding .pushsection");
370   return false;
371 }
372
373 // FIXME: This is a work in progress.
374 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
375   return ParseSectionArguments(/*IsPush=*/false);
376 }
377
378 bool ELFAsmParser::ParseSectionArguments(bool IsPush) {
379   StringRef SectionName;
380
381   if (ParseSectionName(SectionName))
382     return TokError("expected identifier in directive");
383
384   StringRef TypeName;
385   int64_t Size = 0;
386   StringRef GroupName;
387   unsigned Flags = 0;
388   const MCExpr *Subsection = 0;
389   bool UseLastGroup = false;
390
391   // Set the defaults first.
392   if (SectionName == ".fini" || SectionName == ".init" ||
393       SectionName == ".rodata")
394     Flags |= ELF::SHF_ALLOC;
395   if (SectionName == ".fini" || SectionName == ".init")
396     Flags |= ELF::SHF_EXECINSTR;
397
398   if (getLexer().is(AsmToken::Comma)) {
399     Lex();
400
401     if (IsPush && getLexer().isNot(AsmToken::String)) {
402       if (getParser().parseExpression(Subsection))
403         return true;
404       if (getLexer().isNot(AsmToken::Comma))
405         goto EndStmt;
406       Lex();
407     }
408
409     unsigned extraFlags;
410
411     if (getLexer().isNot(AsmToken::String)) {
412       if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
413           || getLexer().isNot(AsmToken::Hash))
414         return TokError("expected string in directive");
415       extraFlags = parseSunStyleSectionFlags();
416     } else {
417       StringRef FlagsStr = getTok().getStringContents();
418       Lex();
419       extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
420     }
421
422     if (extraFlags == -1U)
423       return TokError("unknown flag");
424     Flags |= extraFlags;
425
426     bool Mergeable = Flags & ELF::SHF_MERGE;
427     bool Group = Flags & ELF::SHF_GROUP;
428     if (Group && UseLastGroup)
429       return TokError("Section cannot specifiy a group name while also acting "
430                       "as a member of the last group");
431
432     if (getLexer().isNot(AsmToken::Comma)) {
433       if (Mergeable)
434         return TokError("Mergeable section must specify the type");
435       if (Group)
436         return TokError("Group section must specify the type");
437     } else {
438       Lex();
439       if (getLexer().is(AsmToken::At) || getLexer().is(AsmToken::Percent) ||
440           getLexer().is(AsmToken::String)) {
441         if (!getLexer().is(AsmToken::String))
442           Lex();
443       } else
444         return TokError("expected '@<type>', '%<type>' or \"<type>\"");
445
446       if (getParser().parseIdentifier(TypeName))
447         return TokError("expected identifier in directive");
448
449       if (Mergeable) {
450         if (getLexer().isNot(AsmToken::Comma))
451           return TokError("expected the entry size");
452         Lex();
453         if (getParser().parseAbsoluteExpression(Size))
454           return true;
455         if (Size <= 0)
456           return TokError("entry size must be positive");
457       }
458
459       if (Group) {
460         if (getLexer().isNot(AsmToken::Comma))
461           return TokError("expected group name");
462         Lex();
463         if (getParser().parseIdentifier(GroupName))
464           return true;
465         if (getLexer().is(AsmToken::Comma)) {
466           Lex();
467           StringRef Linkage;
468           if (getParser().parseIdentifier(Linkage))
469             return true;
470           if (Linkage != "comdat")
471             return TokError("Linkage must be 'comdat'");
472         }
473       }
474     }
475   }
476
477 EndStmt:
478   if (getLexer().isNot(AsmToken::EndOfStatement))
479     return TokError("unexpected token in directive");
480
481   unsigned Type = ELF::SHT_PROGBITS;
482
483   if (TypeName.empty()) {
484     if (SectionName.startswith(".note"))
485       Type = ELF::SHT_NOTE;
486     else if (SectionName == ".init_array")
487       Type = ELF::SHT_INIT_ARRAY;
488     else if (SectionName == ".fini_array")
489       Type = ELF::SHT_FINI_ARRAY;
490     else if (SectionName == ".preinit_array")
491       Type = ELF::SHT_PREINIT_ARRAY;
492   } else {
493     if (TypeName == "init_array")
494       Type = ELF::SHT_INIT_ARRAY;
495     else if (TypeName == "fini_array")
496       Type = ELF::SHT_FINI_ARRAY;
497     else if (TypeName == "preinit_array")
498       Type = ELF::SHT_PREINIT_ARRAY;
499     else if (TypeName == "nobits")
500       Type = ELF::SHT_NOBITS;
501     else if (TypeName == "progbits")
502       Type = ELF::SHT_PROGBITS;
503     else if (TypeName == "note")
504       Type = ELF::SHT_NOTE;
505     else if (TypeName == "unwind")
506       Type = ELF::SHT_X86_64_UNWIND;
507     else
508       return TokError("unknown section type");
509   }
510
511   if (UseLastGroup) {
512     MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
513     if (const MCSectionELF *Section =
514             cast_or_null<MCSectionELF>(CurrentSection.first))
515       if (const MCSymbol *Group = Section->getGroup()) {
516         GroupName = Group->getName();
517         Flags |= ELF::SHF_GROUP;
518       }
519   }
520
521   SectionKind Kind = computeSectionKind(Flags);
522   getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
523                                                          Flags, Kind, Size,
524                                                          GroupName),
525                               Subsection);
526   return false;
527 }
528
529 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
530   MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
531   if (PreviousSection.first == NULL)
532       return TokError(".previous without corresponding .section");
533   getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
534
535   return false;
536 }
537
538 /// ParseDirectiveELFType
539 ///  ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
540 ///  ::= .type identifier , #attribute
541 ///  ::= .type identifier , @attribute
542 ///  ::= .type identifier , %attribute
543 ///  ::= .type identifier , "attribute"
544 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
545   StringRef Name;
546   if (getParser().parseIdentifier(Name))
547     return TokError("expected identifier in directive");
548
549   // Handle the identifier as the key symbol.
550   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
551
552   if (getLexer().isNot(AsmToken::Comma))
553     return TokError("unexpected token in '.type' directive");
554   Lex();
555
556   StringRef Type;
557   SMLoc TypeLoc;
558   MCSymbolAttr Attr;
559   if (getLexer().is(AsmToken::Identifier)) {
560     TypeLoc = getLexer().getLoc();
561     if (getParser().parseIdentifier(Type))
562       return TokError("expected symbol type in directive");
563     Attr = StringSwitch<MCSymbolAttr>(Type)
564                .Case("STT_FUNC", MCSA_ELF_TypeFunction)
565                .Case("STT_OBJECT", MCSA_ELF_TypeObject)
566                .Case("STT_TLS", MCSA_ELF_TypeTLS)
567                .Case("STT_COMMON", MCSA_ELF_TypeCommon)
568                .Case("STT_NOTYPE", MCSA_ELF_TypeNoType)
569                .Case("STT_GNU_IFUNC", MCSA_ELF_TypeIndFunction)
570                .Default(MCSA_Invalid);
571   } else if (getLexer().is(AsmToken::Hash) || getLexer().is(AsmToken::At) ||
572              getLexer().is(AsmToken::Percent) ||
573              getLexer().is(AsmToken::String)) {
574     if (!getLexer().is(AsmToken::String))
575       Lex();
576
577     TypeLoc = getLexer().getLoc();
578     if (getParser().parseIdentifier(Type))
579       return TokError("expected symbol type in directive");
580     Attr = StringSwitch<MCSymbolAttr>(Type)
581                .Case("function", MCSA_ELF_TypeFunction)
582                .Case("object", MCSA_ELF_TypeObject)
583                .Case("tls_object", MCSA_ELF_TypeTLS)
584                .Case("common", MCSA_ELF_TypeCommon)
585                .Case("notype", MCSA_ELF_TypeNoType)
586                .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
587                .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
588                .Default(MCSA_Invalid);
589   } else
590     return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
591                     "'%<type>' or \"<type>\"");
592
593   if (Attr == MCSA_Invalid)
594     return Error(TypeLoc, "unsupported attribute in '.type' directive");
595
596   if (getLexer().isNot(AsmToken::EndOfStatement))
597     return TokError("unexpected token in '.type' directive");
598
599   Lex();
600
601   getStreamer().EmitSymbolAttribute(Sym, Attr);
602
603   return false;
604 }
605
606 /// ParseDirectiveIdent
607 ///  ::= .ident string
608 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
609   if (getLexer().isNot(AsmToken::String))
610     return TokError("unexpected token in '.ident' directive");
611
612   StringRef Data = getTok().getIdentifier();
613
614   Lex();
615
616   getStreamer().EmitIdent(Data);
617   return false;
618 }
619
620 /// ParseDirectiveSymver
621 ///  ::= .symver foo, bar2@zed
622 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
623   StringRef Name;
624   if (getParser().parseIdentifier(Name))
625     return TokError("expected identifier in directive");
626
627   if (getLexer().isNot(AsmToken::Comma))
628     return TokError("expected a comma");
629
630   // ARM assembly uses @ for a comment...
631   // except when parsing the second parameter of the .symver directive.
632   // Force the next symbol to allow @ in the identifier, which is
633   // required for this directive and then reset it to its initial state.
634   const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
635   getLexer().setAllowAtInIdentifier(true);
636   Lex();
637   getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
638
639   StringRef AliasName;
640   if (getParser().parseIdentifier(AliasName))
641     return TokError("expected identifier in directive");
642
643   if (AliasName.find('@') == StringRef::npos)
644     return TokError("expected a '@' in the name");
645
646   MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
647   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
648   const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
649
650   getStreamer().EmitAssignment(Alias, Value);
651   return false;
652 }
653
654 /// ParseDirectiveVersion
655 ///  ::= .version string
656 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
657   if (getLexer().isNot(AsmToken::String))
658     return TokError("unexpected token in '.version' directive");
659
660   StringRef Data = getTok().getIdentifier();
661
662   Lex();
663
664   const MCSection *Note =
665     getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
666                                SectionKind::getReadOnly());
667
668   getStreamer().PushSection();
669   getStreamer().SwitchSection(Note);
670   getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
671   getStreamer().EmitIntValue(0, 4);             // descsz = 0 (no description).
672   getStreamer().EmitIntValue(1, 4);             // type = NT_VERSION.
673   getStreamer().EmitBytes(Data);                // name.
674   getStreamer().EmitIntValue(0, 1);             // terminate the string.
675   getStreamer().EmitValueToAlignment(4);        // ensure 4 byte alignment.
676   getStreamer().PopSection();
677   return false;
678 }
679
680 /// ParseDirectiveWeakref
681 ///  ::= .weakref foo, bar
682 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
683   // FIXME: Share code with the other alias building directives.
684
685   StringRef AliasName;
686   if (getParser().parseIdentifier(AliasName))
687     return TokError("expected identifier in directive");
688
689   if (getLexer().isNot(AsmToken::Comma))
690     return TokError("expected a comma");
691
692   Lex();
693
694   StringRef Name;
695   if (getParser().parseIdentifier(Name))
696     return TokError("expected identifier in directive");
697
698   MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
699
700   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
701
702   getStreamer().EmitWeakReference(Alias, Sym);
703   return false;
704 }
705
706 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
707   const MCExpr *Subsection = 0;
708   if (getLexer().isNot(AsmToken::EndOfStatement)) {
709     if (getParser().parseExpression(Subsection))
710      return true;
711   }
712
713   if (getLexer().isNot(AsmToken::EndOfStatement))
714     return TokError("unexpected token in directive");
715
716   getStreamer().SubSection(Subsection);
717   return false;
718 }
719
720 namespace llvm {
721
722 MCAsmParserExtension *createELFAsmParser() {
723   return new ELFAsmParser;
724 }
725
726 }