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