Compute the ELF SectionKind from the flags.
[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, SMLoc loc);
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 = nullptr;
197   if (getLexer().isNot(AsmToken::EndOfStatement)) {
198     if (getParser().parseExpression(Subsection))
199       return true;
200   }
201
202   getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
203                               Subsection);
204
205   return false;
206 }
207
208 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
209   StringRef Name;
210   if (getParser().parseIdentifier(Name))
211     return TokError("expected identifier in directive");
212   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
213
214   if (getLexer().isNot(AsmToken::Comma))
215     return TokError("unexpected token in directive");
216   Lex();
217
218   const MCExpr *Expr;
219   if (getParser().parseExpression(Expr))
220     return true;
221
222   if (getLexer().isNot(AsmToken::EndOfStatement))
223     return TokError("unexpected token in directive");
224
225   getStreamer().EmitELFSize(Sym, Expr);
226   return false;
227 }
228
229 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
230   // A section name can contain -, so we cannot just use
231   // parseIdentifier.
232   SMLoc FirstLoc = getLexer().getLoc();
233   unsigned Size = 0;
234
235   if (getLexer().is(AsmToken::String)) {
236     SectionName = getTok().getIdentifier();
237     Lex();
238     return false;
239   }
240
241   for (;;) {
242     unsigned CurSize;
243
244     SMLoc PrevLoc = getLexer().getLoc();
245     if (getLexer().is(AsmToken::Minus)) {
246       CurSize = 1;
247       Lex(); // Consume the "-".
248     } else if (getLexer().is(AsmToken::String)) {
249       CurSize = getTok().getIdentifier().size() + 2;
250       Lex();
251     } else if (getLexer().is(AsmToken::Identifier)) {
252       CurSize = getTok().getIdentifier().size();
253       Lex();
254     } else {
255       break;
256     }
257
258     Size += CurSize;
259     SectionName = StringRef(FirstLoc.getPointer(), Size);
260
261     // Make sure the following token is adjacent.
262     if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
263       break;
264   }
265   if (Size == 0)
266     return true;
267
268   return false;
269 }
270
271 static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
272   unsigned flags = 0;
273
274   for (unsigned i = 0; i < flagsStr.size(); i++) {
275     switch (flagsStr[i]) {
276     case 'a':
277       flags |= ELF::SHF_ALLOC;
278       break;
279     case 'e':
280       flags |= ELF::SHF_EXCLUDE;
281       break;
282     case 'x':
283       flags |= ELF::SHF_EXECINSTR;
284       break;
285     case 'w':
286       flags |= ELF::SHF_WRITE;
287       break;
288     case 'M':
289       flags |= ELF::SHF_MERGE;
290       break;
291     case 'S':
292       flags |= ELF::SHF_STRINGS;
293       break;
294     case 'T':
295       flags |= ELF::SHF_TLS;
296       break;
297     case 'c':
298       flags |= ELF::XCORE_SHF_CP_SECTION;
299       break;
300     case 'd':
301       flags |= ELF::XCORE_SHF_DP_SECTION;
302       break;
303     case 'G':
304       flags |= ELF::SHF_GROUP;
305       break;
306     case '?':
307       *UseLastGroup = true;
308       break;
309     default:
310       return -1U;
311     }
312   }
313
314   return flags;
315 }
316
317 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
318   unsigned flags = 0;
319   while (getLexer().is(AsmToken::Hash)) {
320     Lex(); // Eat the #.
321
322     if (!getLexer().is(AsmToken::Identifier))
323       return -1U;
324
325     StringRef flagId = getTok().getIdentifier();
326     if (flagId == "alloc")
327       flags |= ELF::SHF_ALLOC;
328     else if (flagId == "execinstr")
329       flags |= ELF::SHF_EXECINSTR;
330     else if (flagId == "write")
331       flags |= ELF::SHF_WRITE;
332     else if (flagId == "tls")
333       flags |= ELF::SHF_TLS;
334     else
335       return -1U;
336
337     Lex(); // Eat the flag.
338
339     if (!getLexer().is(AsmToken::Comma))
340         break;
341     Lex(); // Eat the comma.
342   }
343   return flags;
344 }
345
346
347 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
348   getStreamer().PushSection();
349
350   if (ParseSectionArguments(/*IsPush=*/true, loc)) {
351     getStreamer().PopSection();
352     return true;
353   }
354
355   return false;
356 }
357
358 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
359   if (!getStreamer().PopSection())
360     return TokError(".popsection without corresponding .pushsection");
361   return false;
362 }
363
364 // FIXME: This is a work in progress.
365 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
366   return ParseSectionArguments(/*IsPush=*/false, loc);
367 }
368
369 bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
370   StringRef SectionName;
371
372   if (ParseSectionName(SectionName))
373     return TokError("expected identifier in directive");
374
375   StringRef TypeName;
376   int64_t Size = 0;
377   StringRef GroupName;
378   unsigned Flags = 0;
379   const MCExpr *Subsection = nullptr;
380   bool UseLastGroup = false;
381
382   // Set the defaults first.
383   if (SectionName == ".fini" || SectionName == ".init" ||
384       SectionName == ".rodata")
385     Flags |= ELF::SHF_ALLOC;
386   if (SectionName == ".fini" || SectionName == ".init")
387     Flags |= ELF::SHF_EXECINSTR;
388
389   if (getLexer().is(AsmToken::Comma)) {
390     Lex();
391
392     if (IsPush && getLexer().isNot(AsmToken::String)) {
393       if (getParser().parseExpression(Subsection))
394         return true;
395       if (getLexer().isNot(AsmToken::Comma))
396         goto EndStmt;
397       Lex();
398     }
399
400     unsigned extraFlags;
401
402     if (getLexer().isNot(AsmToken::String)) {
403       if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
404           || getLexer().isNot(AsmToken::Hash))
405         return TokError("expected string in directive");
406       extraFlags = parseSunStyleSectionFlags();
407     } else {
408       StringRef FlagsStr = getTok().getStringContents();
409       Lex();
410       extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
411     }
412
413     if (extraFlags == -1U)
414       return TokError("unknown flag");
415     Flags |= extraFlags;
416
417     bool Mergeable = Flags & ELF::SHF_MERGE;
418     bool Group = Flags & ELF::SHF_GROUP;
419     if (Group && UseLastGroup)
420       return TokError("Section cannot specifiy a group name while also acting "
421                       "as a member of the last group");
422
423     if (getLexer().isNot(AsmToken::Comma)) {
424       if (Mergeable)
425         return TokError("Mergeable section must specify the type");
426       if (Group)
427         return TokError("Group section must specify the type");
428     } else {
429       Lex();
430       if (getLexer().is(AsmToken::At) || getLexer().is(AsmToken::Percent) ||
431           getLexer().is(AsmToken::String)) {
432         if (!getLexer().is(AsmToken::String))
433           Lex();
434       } else
435         return TokError("expected '@<type>', '%<type>' or \"<type>\"");
436
437       if (getParser().parseIdentifier(TypeName))
438         return TokError("expected identifier in directive");
439
440       if (Mergeable) {
441         if (getLexer().isNot(AsmToken::Comma))
442           return TokError("expected the entry size");
443         Lex();
444         if (getParser().parseAbsoluteExpression(Size))
445           return true;
446         if (Size <= 0)
447           return TokError("entry size must be positive");
448       }
449
450       if (Group) {
451         if (getLexer().isNot(AsmToken::Comma))
452           return TokError("expected group name");
453         Lex();
454         if (getParser().parseIdentifier(GroupName))
455           return true;
456         if (getLexer().is(AsmToken::Comma)) {
457           Lex();
458           StringRef Linkage;
459           if (getParser().parseIdentifier(Linkage))
460             return true;
461           if (Linkage != "comdat")
462             return TokError("Linkage must be 'comdat'");
463         }
464       }
465     }
466   }
467
468 EndStmt:
469   if (getLexer().isNot(AsmToken::EndOfStatement))
470     return TokError("unexpected token in directive");
471
472   unsigned Type = ELF::SHT_PROGBITS;
473
474   if (TypeName.empty()) {
475     if (SectionName.startswith(".note"))
476       Type = ELF::SHT_NOTE;
477     else if (SectionName == ".init_array")
478       Type = ELF::SHT_INIT_ARRAY;
479     else if (SectionName == ".fini_array")
480       Type = ELF::SHT_FINI_ARRAY;
481     else if (SectionName == ".preinit_array")
482       Type = ELF::SHT_PREINIT_ARRAY;
483   } else {
484     if (TypeName == "init_array")
485       Type = ELF::SHT_INIT_ARRAY;
486     else if (TypeName == "fini_array")
487       Type = ELF::SHT_FINI_ARRAY;
488     else if (TypeName == "preinit_array")
489       Type = ELF::SHT_PREINIT_ARRAY;
490     else if (TypeName == "nobits")
491       Type = ELF::SHT_NOBITS;
492     else if (TypeName == "progbits")
493       Type = ELF::SHT_PROGBITS;
494     else if (TypeName == "note")
495       Type = ELF::SHT_NOTE;
496     else if (TypeName == "unwind")
497       Type = ELF::SHT_X86_64_UNWIND;
498     else
499       return TokError("unknown section type");
500   }
501
502   if (UseLastGroup) {
503     MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
504     if (const MCSectionELF *Section =
505             cast_or_null<MCSectionELF>(CurrentSection.first))
506       if (const MCSymbol *Group = Section->getGroup()) {
507         GroupName = Group->getName();
508         Flags |= ELF::SHF_GROUP;
509       }
510   }
511
512   const MCSection *ELFSection =
513       getContext().getELFSection(SectionName, Type, Flags, Size, GroupName);
514   getStreamer().SwitchSection(ELFSection, Subsection);
515
516   if (getContext().getGenDwarfForAssembly()) {
517     auto &Sections = getContext().getGenDwarfSectionSyms();
518     auto InsertResult = Sections.insert(
519         std::make_pair(ELFSection, std::make_pair(nullptr, nullptr)));
520     if (InsertResult.second) {
521       if (getContext().getDwarfVersion() <= 2)
522         Warning(loc, "DWARF2 only supports one section per compilation unit");
523
524       MCSymbol *SectionStartSymbol = getContext().CreateTempSymbol();
525       getStreamer().EmitLabel(SectionStartSymbol);
526       InsertResult.first->second.first = SectionStartSymbol;
527     }
528   }
529
530   return false;
531 }
532
533 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
534   MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
535   if (PreviousSection.first == nullptr)
536       return TokError(".previous without corresponding .section");
537   getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
538
539   return false;
540 }
541
542 static MCSymbolAttr MCAttrForString(StringRef Type) {
543   return StringSwitch<MCSymbolAttr>(Type)
544           .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
545           .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
546           .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
547           .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
548           .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
549           .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
550                  MCSA_ELF_TypeIndFunction)
551           .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
552           .Default(MCSA_Invalid);
553 }
554
555 /// ParseDirectiveELFType
556 ///  ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
557 ///  ::= .type identifier , #attribute
558 ///  ::= .type identifier , @attribute
559 ///  ::= .type identifier , %attribute
560 ///  ::= .type identifier , "attribute"
561 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
562   StringRef Name;
563   if (getParser().parseIdentifier(Name))
564     return TokError("expected identifier in directive");
565
566   // Handle the identifier as the key symbol.
567   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
568
569   // NOTE the comma is optional in all cases.  It is only documented as being
570   // optional in the first case, however, GAS will silently treat the comma as
571   // optional in all cases.  Furthermore, although the documentation states that
572   // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
573   // accepts both the upper case name as well as the lower case aliases.
574   if (getLexer().is(AsmToken::Comma))
575     Lex();
576
577   if (getLexer().isNot(AsmToken::Identifier) &&
578       getLexer().isNot(AsmToken::Hash) && getLexer().isNot(AsmToken::At) &&
579       getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::String))
580     return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
581                     "'%<type>' or \"<type>\"");
582
583   if (getLexer().isNot(AsmToken::String) &&
584       getLexer().isNot(AsmToken::Identifier))
585     Lex();
586
587   SMLoc TypeLoc = getLexer().getLoc();
588
589   StringRef Type;
590   if (getParser().parseIdentifier(Type))
591     return TokError("expected symbol type in directive");
592
593   MCSymbolAttr Attr = MCAttrForString(Type);
594   if (Attr == MCSA_Invalid)
595     return Error(TypeLoc, "unsupported attribute in '.type' directive");
596
597   if (getLexer().isNot(AsmToken::EndOfStatement))
598     return TokError("unexpected token in '.type' directive");
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 = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
665
666   getStreamer().PushSection();
667   getStreamer().SwitchSection(Note);
668   getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
669   getStreamer().EmitIntValue(0, 4);             // descsz = 0 (no description).
670   getStreamer().EmitIntValue(1, 4);             // type = NT_VERSION.
671   getStreamer().EmitBytes(Data);                // name.
672   getStreamer().EmitIntValue(0, 1);             // terminate the string.
673   getStreamer().EmitValueToAlignment(4);        // ensure 4 byte alignment.
674   getStreamer().PopSection();
675   return false;
676 }
677
678 /// ParseDirectiveWeakref
679 ///  ::= .weakref foo, bar
680 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
681   // FIXME: Share code with the other alias building directives.
682
683   StringRef AliasName;
684   if (getParser().parseIdentifier(AliasName))
685     return TokError("expected identifier in directive");
686
687   if (getLexer().isNot(AsmToken::Comma))
688     return TokError("expected a comma");
689
690   Lex();
691
692   StringRef Name;
693   if (getParser().parseIdentifier(Name))
694     return TokError("expected identifier in directive");
695
696   MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
697
698   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
699
700   getStreamer().EmitWeakReference(Alias, Sym);
701   return false;
702 }
703
704 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
705   const MCExpr *Subsection = nullptr;
706   if (getLexer().isNot(AsmToken::EndOfStatement)) {
707     if (getParser().parseExpression(Subsection))
708      return true;
709   }
710
711   if (getLexer().isNot(AsmToken::EndOfStatement))
712     return TokError("unexpected token in directive");
713
714   getStreamer().SubSection(Subsection);
715   return false;
716 }
717
718 namespace llvm {
719
720 MCAsmParserExtension *createELFAsmParser() {
721   return new ELFAsmParser;
722 }
723
724 }