]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/MC/MCParser/ELFAsmParser.cpp
Merge ^/head r313896 through r314128.
[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   // If a valid numerical value is set for the section flag, use it verbatim
268   if (!flagsStr.getAsInteger(0, flags))
269     return flags;
270
271   for (char i : flagsStr) {
272     switch (i) {
273     case 'a':
274       flags |= ELF::SHF_ALLOC;
275       break;
276     case 'e':
277       flags |= ELF::SHF_EXCLUDE;
278       break;
279     case 'x':
280       flags |= ELF::SHF_EXECINSTR;
281       break;
282     case 'w':
283       flags |= ELF::SHF_WRITE;
284       break;
285     case 'M':
286       flags |= ELF::SHF_MERGE;
287       break;
288     case 'S':
289       flags |= ELF::SHF_STRINGS;
290       break;
291     case 'T':
292       flags |= ELF::SHF_TLS;
293       break;
294     case 'c':
295       flags |= ELF::XCORE_SHF_CP_SECTION;
296       break;
297     case 'd':
298       flags |= ELF::XCORE_SHF_DP_SECTION;
299       break;
300     case 'y':
301       flags |= ELF::SHF_ARM_PURECODE;
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   StringRef UniqueStr;
382   int64_t UniqueID = ~0;
383
384   // Set the defaults first.
385   if (SectionName == ".fini" || SectionName == ".init" ||
386       SectionName == ".rodata")
387     Flags |= ELF::SHF_ALLOC;
388   if (SectionName == ".fini" || SectionName == ".init")
389     Flags |= ELF::SHF_EXECINSTR;
390
391   if (getLexer().is(AsmToken::Comma)) {
392     Lex();
393
394     if (IsPush && getLexer().isNot(AsmToken::String)) {
395       if (getParser().parseExpression(Subsection))
396         return true;
397       if (getLexer().isNot(AsmToken::Comma))
398         goto EndStmt;
399       Lex();
400     }
401
402     unsigned extraFlags;
403
404     if (getLexer().isNot(AsmToken::String)) {
405       if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
406           || getLexer().isNot(AsmToken::Hash))
407         return TokError("expected string in directive");
408       extraFlags = parseSunStyleSectionFlags();
409     } else {
410       StringRef FlagsStr = getTok().getStringContents();
411       Lex();
412       extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
413     }
414
415     if (extraFlags == -1U)
416       return TokError("unknown flag");
417     Flags |= extraFlags;
418
419     bool Mergeable = Flags & ELF::SHF_MERGE;
420     bool Group = Flags & ELF::SHF_GROUP;
421     if (Group && UseLastGroup)
422       return TokError("Section cannot specifiy a group name while also acting "
423                       "as a member of the last group");
424
425     if (getLexer().isNot(AsmToken::Comma)) {
426       if (Mergeable)
427         return TokError("Mergeable section must specify the type");
428       if (Group)
429         return TokError("Group section must specify the type");
430     } else {
431       Lex();
432       if (getLexer().is(AsmToken::At) || getLexer().is(AsmToken::Percent) ||
433           getLexer().is(AsmToken::String)) {
434         if (!getLexer().is(AsmToken::String))
435           Lex();
436       } else
437         return TokError("expected '@<type>', '%<type>' or \"<type>\"");
438
439       if (getParser().parseIdentifier(TypeName))
440         return TokError("expected identifier in directive");
441
442       if (Mergeable) {
443         if (getLexer().isNot(AsmToken::Comma))
444           return TokError("expected the entry size");
445         Lex();
446         if (getParser().parseAbsoluteExpression(Size))
447           return true;
448         if (Size <= 0)
449           return TokError("entry size must be positive");
450       }
451
452       if (Group) {
453         if (getLexer().isNot(AsmToken::Comma))
454           return TokError("expected group name");
455         Lex();
456         if (getParser().parseIdentifier(GroupName))
457           return true;
458         if (getLexer().is(AsmToken::Comma)) {
459           Lex();
460           StringRef Linkage;
461           if (getParser().parseIdentifier(Linkage))
462             return true;
463           if (Linkage != "comdat")
464             return TokError("Linkage must be 'comdat'");
465         }
466       }
467       if (getLexer().is(AsmToken::Comma)) {
468         Lex();
469         if (getParser().parseIdentifier(UniqueStr))
470           return TokError("expected identifier in directive");
471         if (UniqueStr != "unique")
472           return TokError("expected 'unique'");
473         if (getLexer().isNot(AsmToken::Comma))
474           return TokError("expected commma");
475         Lex();
476         if (getParser().parseAbsoluteExpression(UniqueID))
477           return true;
478         if (UniqueID < 0)
479           return TokError("unique id must be positive");
480         if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
481           return TokError("unique id is too large");
482       }
483     }
484   }
485
486 EndStmt:
487   if (getLexer().isNot(AsmToken::EndOfStatement))
488     return TokError("unexpected token in directive");
489   Lex();
490
491   unsigned Type = ELF::SHT_PROGBITS;
492
493   if (TypeName.empty()) {
494     if (SectionName.startswith(".note"))
495       Type = ELF::SHT_NOTE;
496     else if (SectionName == ".init_array")
497       Type = ELF::SHT_INIT_ARRAY;
498     else if (SectionName == ".fini_array")
499       Type = ELF::SHT_FINI_ARRAY;
500     else if (SectionName == ".preinit_array")
501       Type = ELF::SHT_PREINIT_ARRAY;
502   } else {
503     if (TypeName == "init_array")
504       Type = ELF::SHT_INIT_ARRAY;
505     else if (TypeName == "fini_array")
506       Type = ELF::SHT_FINI_ARRAY;
507     else if (TypeName == "preinit_array")
508       Type = ELF::SHT_PREINIT_ARRAY;
509     else if (TypeName == "nobits")
510       Type = ELF::SHT_NOBITS;
511     else if (TypeName == "progbits")
512       Type = ELF::SHT_PROGBITS;
513     else if (TypeName == "note")
514       Type = ELF::SHT_NOTE;
515     else if (TypeName == "unwind")
516       Type = ELF::SHT_X86_64_UNWIND;
517     else
518       return TokError("unknown section type");
519   }
520
521   if (UseLastGroup) {
522     MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
523     if (const MCSectionELF *Section =
524             cast_or_null<MCSectionELF>(CurrentSection.first))
525       if (const MCSymbol *Group = Section->getGroup()) {
526         GroupName = Group->getName();
527         Flags |= ELF::SHF_GROUP;
528       }
529   }
530
531   MCSection *ELFSection = getContext().getELFSection(SectionName, Type, Flags,
532                                                      Size, GroupName, UniqueID);
533   getStreamer().SwitchSection(ELFSection, Subsection);
534
535   if (getContext().getGenDwarfForAssembly()) {
536     bool InsertResult = getContext().addGenDwarfSection(ELFSection);
537     if (InsertResult) {
538       if (getContext().getDwarfVersion() <= 2)
539         Warning(loc, "DWARF2 only supports one section per compilation unit");
540
541       if (!ELFSection->getBeginSymbol()) {
542         MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
543         getStreamer().EmitLabel(SectionStartSymbol);
544         ELFSection->setBeginSymbol(SectionStartSymbol);
545       }
546     }
547   }
548
549   return false;
550 }
551
552 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
553   MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
554   if (PreviousSection.first == nullptr)
555       return TokError(".previous without corresponding .section");
556   getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
557
558   return false;
559 }
560
561 static MCSymbolAttr MCAttrForString(StringRef Type) {
562   return StringSwitch<MCSymbolAttr>(Type)
563           .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
564           .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
565           .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
566           .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
567           .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
568           .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
569                  MCSA_ELF_TypeIndFunction)
570           .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
571           .Default(MCSA_Invalid);
572 }
573
574 /// ParseDirectiveELFType
575 ///  ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
576 ///  ::= .type identifier , #attribute
577 ///  ::= .type identifier , @attribute
578 ///  ::= .type identifier , %attribute
579 ///  ::= .type identifier , "attribute"
580 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
581   StringRef Name;
582   if (getParser().parseIdentifier(Name))
583     return TokError("expected identifier in directive");
584
585   // Handle the identifier as the key symbol.
586   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
587
588   // NOTE the comma is optional in all cases.  It is only documented as being
589   // optional in the first case, however, GAS will silently treat the comma as
590   // optional in all cases.  Furthermore, although the documentation states that
591   // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
592   // accepts both the upper case name as well as the lower case aliases.
593   if (getLexer().is(AsmToken::Comma))
594     Lex();
595
596   if (getLexer().isNot(AsmToken::Identifier) &&
597       getLexer().isNot(AsmToken::Hash) &&
598       getLexer().isNot(AsmToken::Percent) &&
599       getLexer().isNot(AsmToken::String)) {
600     if (!getLexer().getAllowAtInIdentifier())
601       return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
602                       "'%<type>' or \"<type>\"");
603     else if (getLexer().isNot(AsmToken::At))
604       return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
605                       "'%<type>' or \"<type>\"");
606   }
607
608   if (getLexer().isNot(AsmToken::String) &&
609       getLexer().isNot(AsmToken::Identifier))
610     Lex();
611
612   SMLoc TypeLoc = getLexer().getLoc();
613
614   StringRef Type;
615   if (getParser().parseIdentifier(Type))
616     return TokError("expected symbol type in directive");
617
618   MCSymbolAttr Attr = MCAttrForString(Type);
619   if (Attr == MCSA_Invalid)
620     return Error(TypeLoc, "unsupported attribute in '.type' directive");
621
622   if (getLexer().isNot(AsmToken::EndOfStatement))
623     return TokError("unexpected token in '.type' directive");
624   Lex();
625
626   getStreamer().EmitSymbolAttribute(Sym, Attr);
627
628   return false;
629 }
630
631 /// ParseDirectiveIdent
632 ///  ::= .ident string
633 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
634   if (getLexer().isNot(AsmToken::String))
635     return TokError("unexpected token in '.ident' directive");
636
637   StringRef Data = getTok().getIdentifier();
638
639   Lex();
640
641   if (getLexer().isNot(AsmToken::EndOfStatement))
642     return TokError("unexpected token in '.ident' directive");
643   Lex();
644
645   getStreamer().EmitIdent(Data);
646   return false;
647 }
648
649 /// ParseDirectiveSymver
650 ///  ::= .symver foo, bar2@zed
651 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
652   StringRef Name;
653   if (getParser().parseIdentifier(Name))
654     return TokError("expected identifier in directive");
655
656   if (getLexer().isNot(AsmToken::Comma))
657     return TokError("expected a comma");
658
659   // ARM assembly uses @ for a comment...
660   // except when parsing the second parameter of the .symver directive.
661   // Force the next symbol to allow @ in the identifier, which is
662   // required for this directive and then reset it to its initial state.
663   const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
664   getLexer().setAllowAtInIdentifier(true);
665   Lex();
666   getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
667
668   StringRef AliasName;
669   if (getParser().parseIdentifier(AliasName))
670     return TokError("expected identifier in directive");
671
672   if (AliasName.find('@') == StringRef::npos)
673     return TokError("expected a '@' in the name");
674
675   MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
676   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
677   const MCExpr *Value = MCSymbolRefExpr::create(Sym, getContext());
678
679   getStreamer().EmitAssignment(Alias, Value);
680   return false;
681 }
682
683 /// ParseDirectiveVersion
684 ///  ::= .version string
685 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
686   if (getLexer().isNot(AsmToken::String))
687     return TokError("unexpected token in '.version' directive");
688
689   StringRef Data = getTok().getIdentifier();
690
691   Lex();
692
693   MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
694
695   getStreamer().PushSection();
696   getStreamer().SwitchSection(Note);
697   getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
698   getStreamer().EmitIntValue(0, 4);             // descsz = 0 (no description).
699   getStreamer().EmitIntValue(1, 4);             // type = NT_VERSION.
700   getStreamer().EmitBytes(Data);                // name.
701   getStreamer().EmitIntValue(0, 1);             // terminate the string.
702   getStreamer().EmitValueToAlignment(4);        // ensure 4 byte alignment.
703   getStreamer().PopSection();
704   return false;
705 }
706
707 /// ParseDirectiveWeakref
708 ///  ::= .weakref foo, bar
709 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
710   // FIXME: Share code with the other alias building directives.
711
712   StringRef AliasName;
713   if (getParser().parseIdentifier(AliasName))
714     return TokError("expected identifier in directive");
715
716   if (getLexer().isNot(AsmToken::Comma))
717     return TokError("expected a comma");
718
719   Lex();
720
721   StringRef Name;
722   if (getParser().parseIdentifier(Name))
723     return TokError("expected identifier in directive");
724
725   MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
726
727   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
728
729   getStreamer().EmitWeakReference(Alias, Sym);
730   return false;
731 }
732
733 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
734   const MCExpr *Subsection = nullptr;
735   if (getLexer().isNot(AsmToken::EndOfStatement)) {
736     if (getParser().parseExpression(Subsection))
737      return true;
738   }
739
740   if (getLexer().isNot(AsmToken::EndOfStatement))
741     return TokError("unexpected token in directive");
742
743   Lex();
744
745   getStreamer().SubSection(Subsection);
746   return false;
747 }
748
749 namespace llvm {
750
751 MCAsmParserExtension *createELFAsmParser() {
752   return new ELFAsmParser;
753 }
754
755 }