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 
192   getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
193                               Subsection);
194 
195   return false;
196 }
197 
198 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
199   StringRef Name;
200   if (getParser().parseIdentifier(Name))
201     return TokError("expected identifier in directive");
202   MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
203 
204   if (getLexer().isNot(AsmToken::Comma))
205     return TokError("unexpected token in directive");
206   Lex();
207 
208   const MCExpr *Expr;
209   if (getParser().parseExpression(Expr))
210     return true;
211 
212   if (getLexer().isNot(AsmToken::EndOfStatement))
213     return TokError("unexpected token in directive");
214 
215   getStreamer().emitELFSize(Sym, Expr);
216   return false;
217 }
218 
219 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
220   // A section name can contain -, so we cannot just use
221   // parseIdentifier.
222   SMLoc FirstLoc = getLexer().getLoc();
223   unsigned Size = 0;
224 
225   if (getLexer().is(AsmToken::String)) {
226     SectionName = getTok().getIdentifier();
227     Lex();
228     return false;
229   }
230 
231   for (;;) {
232 
233     SMLoc PrevLoc = getLexer().getLoc();
234     if (getLexer().is(AsmToken::Comma) ||
235       getLexer().is(AsmToken::EndOfStatement))
236       break;
237 
238     unsigned CurSize;
239     if (getLexer().is(AsmToken::String)) {
240       CurSize = getTok().getIdentifier().size() + 2;
241       Lex();
242     } else if (getLexer().is(AsmToken::Identifier)) {
243       CurSize = getTok().getIdentifier().size();
244       Lex();
245     } else {
246       CurSize = getTok().getString().size();
247       Lex();
248     }
249     Size += CurSize;
250     SectionName = StringRef(FirstLoc.getPointer(), Size);
251 
252     // Make sure the following token is adjacent.
253     if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
254       break;
255   }
256   if (Size == 0)
257     return true;
258 
259   return false;
260 }
261 
262 static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
263   unsigned flags = 0;
264 
265   for (char i : flagsStr) {
266     switch (i) {
267     case 'a':
268       flags |= ELF::SHF_ALLOC;
269       break;
270     case 'e':
271       flags |= ELF::SHF_EXCLUDE;
272       break;
273     case 'x':
274       flags |= ELF::SHF_EXECINSTR;
275       break;
276     case 'w':
277       flags |= ELF::SHF_WRITE;
278       break;
279     case 'M':
280       flags |= ELF::SHF_MERGE;
281       break;
282     case 'S':
283       flags |= ELF::SHF_STRINGS;
284       break;
285     case 'T':
286       flags |= ELF::SHF_TLS;
287       break;
288     case 'c':
289       flags |= ELF::XCORE_SHF_CP_SECTION;
290       break;
291     case 'd':
292       flags |= ELF::XCORE_SHF_DP_SECTION;
293       break;
294     case 'G':
295       flags |= ELF::SHF_GROUP;
296       break;
297     case '?':
298       *UseLastGroup = true;
299       break;
300     default:
301       return -1U;
302     }
303   }
304 
305   return flags;
306 }
307 
308 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
309   unsigned flags = 0;
310   while (getLexer().is(AsmToken::Hash)) {
311     Lex(); // Eat the #.
312 
313     if (!getLexer().is(AsmToken::Identifier))
314       return -1U;
315 
316     StringRef flagId = getTok().getIdentifier();
317     if (flagId == "alloc")
318       flags |= ELF::SHF_ALLOC;
319     else if (flagId == "execinstr")
320       flags |= ELF::SHF_EXECINSTR;
321     else if (flagId == "write")
322       flags |= ELF::SHF_WRITE;
323     else if (flagId == "tls")
324       flags |= ELF::SHF_TLS;
325     else
326       return -1U;
327 
328     Lex(); // Eat the flag.
329 
330     if (!getLexer().is(AsmToken::Comma))
331         break;
332     Lex(); // Eat the comma.
333   }
334   return flags;
335 }
336 
337 
338 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
339   getStreamer().PushSection();
340 
341   if (ParseSectionArguments(/*IsPush=*/true, loc)) {
342     getStreamer().PopSection();
343     return true;
344   }
345 
346   return false;
347 }
348 
349 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
350   if (!getStreamer().PopSection())
351     return TokError(".popsection without corresponding .pushsection");
352   return false;
353 }
354 
355 // FIXME: This is a work in progress.
356 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
357   return ParseSectionArguments(/*IsPush=*/false, loc);
358 }
359 
360 bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
361   StringRef SectionName;
362 
363   if (ParseSectionName(SectionName))
364     return TokError("expected identifier in directive");
365 
366   StringRef TypeName;
367   int64_t Size = 0;
368   StringRef GroupName;
369   unsigned Flags = 0;
370   const MCExpr *Subsection = nullptr;
371   bool UseLastGroup = false;
372   StringRef UniqueStr;
373   int64_t UniqueID = ~0;
374 
375   // Set the defaults first.
376   if (SectionName == ".fini" || SectionName == ".init" ||
377       SectionName == ".rodata")
378     Flags |= ELF::SHF_ALLOC;
379   if (SectionName == ".fini" || SectionName == ".init")
380     Flags |= ELF::SHF_EXECINSTR;
381 
382   if (getLexer().is(AsmToken::Comma)) {
383     Lex();
384 
385     if (IsPush && getLexer().isNot(AsmToken::String)) {
386       if (getParser().parseExpression(Subsection))
387         return true;
388       if (getLexer().isNot(AsmToken::Comma))
389         goto EndStmt;
390       Lex();
391     }
392 
393     unsigned extraFlags;
394 
395     if (getLexer().isNot(AsmToken::String)) {
396       if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
397           || getLexer().isNot(AsmToken::Hash))
398         return TokError("expected string in directive");
399       extraFlags = parseSunStyleSectionFlags();
400     } else {
401       StringRef FlagsStr = getTok().getStringContents();
402       Lex();
403       extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
404     }
405 
406     if (extraFlags == -1U)
407       return TokError("unknown flag");
408     Flags |= extraFlags;
409 
410     bool Mergeable = Flags & ELF::SHF_MERGE;
411     bool Group = Flags & ELF::SHF_GROUP;
412     if (Group && UseLastGroup)
413       return TokError("Section cannot specifiy a group name while also acting "
414                       "as a member of the last group");
415 
416     if (getLexer().isNot(AsmToken::Comma)) {
417       if (Mergeable)
418         return TokError("Mergeable section must specify the type");
419       if (Group)
420         return TokError("Group section must specify the type");
421     } else {
422       Lex();
423       if (getLexer().is(AsmToken::At) || getLexer().is(AsmToken::Percent) ||
424           getLexer().is(AsmToken::String)) {
425         if (!getLexer().is(AsmToken::String))
426           Lex();
427       } else
428         return TokError("expected '@<type>', '%<type>' or \"<type>\"");
429 
430       if (getParser().parseIdentifier(TypeName))
431         return TokError("expected identifier in directive");
432 
433       if (Mergeable) {
434         if (getLexer().isNot(AsmToken::Comma))
435           return TokError("expected the entry size");
436         Lex();
437         if (getParser().parseAbsoluteExpression(Size))
438           return true;
439         if (Size <= 0)
440           return TokError("entry size must be positive");
441       }
442 
443       if (Group) {
444         if (getLexer().isNot(AsmToken::Comma))
445           return TokError("expected group name");
446         Lex();
447         if (getParser().parseIdentifier(GroupName))
448           return true;
449         if (getLexer().is(AsmToken::Comma)) {
450           Lex();
451           StringRef Linkage;
452           if (getParser().parseIdentifier(Linkage))
453             return true;
454           if (Linkage != "comdat")
455             return TokError("Linkage must be 'comdat'");
456         }
457       }
458       if (getLexer().is(AsmToken::Comma)) {
459         Lex();
460         if (getParser().parseIdentifier(UniqueStr))
461           return TokError("expected identifier in directive");
462         if (UniqueStr != "unique")
463           return TokError("expected 'unique'");
464         if (getLexer().isNot(AsmToken::Comma))
465           return TokError("expected commma");
466         Lex();
467         if (getParser().parseAbsoluteExpression(UniqueID))
468           return true;
469         if (UniqueID < 0)
470           return TokError("unique id must be positive");
471         if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
472           return TokError("unique id is too large");
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   MCSection *ELFSection = getContext().getELFSection(SectionName, Type, Flags,
522                                                      Size, GroupName, UniqueID);
523   getStreamer().SwitchSection(ELFSection, Subsection);
524 
525   if (getContext().getGenDwarfForAssembly()) {
526     bool InsertResult = getContext().addGenDwarfSection(ELFSection);
527     if (InsertResult) {
528       if (getContext().getDwarfVersion() <= 2)
529         Warning(loc, "DWARF2 only supports one section per compilation unit");
530 
531       if (!ELFSection->getBeginSymbol()) {
532         MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
533         getStreamer().EmitLabel(SectionStartSymbol);
534         ELFSection->setBeginSymbol(SectionStartSymbol);
535       }
536     }
537   }
538 
539   return false;
540 }
541 
542 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
543   MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
544   if (PreviousSection.first == nullptr)
545       return TokError(".previous without corresponding .section");
546   getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
547 
548   return false;
549 }
550 
551 static MCSymbolAttr MCAttrForString(StringRef Type) {
552   return StringSwitch<MCSymbolAttr>(Type)
553           .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
554           .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
555           .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
556           .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
557           .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
558           .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
559                  MCSA_ELF_TypeIndFunction)
560           .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
561           .Default(MCSA_Invalid);
562 }
563 
564 /// ParseDirectiveELFType
565 ///  ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
566 ///  ::= .type identifier , #attribute
567 ///  ::= .type identifier , @attribute
568 ///  ::= .type identifier , %attribute
569 ///  ::= .type identifier , "attribute"
570 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
571   StringRef Name;
572   if (getParser().parseIdentifier(Name))
573     return TokError("expected identifier in directive");
574 
575   // Handle the identifier as the key symbol.
576   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
577 
578   // NOTE the comma is optional in all cases.  It is only documented as being
579   // optional in the first case, however, GAS will silently treat the comma as
580   // optional in all cases.  Furthermore, although the documentation states that
581   // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
582   // accepts both the upper case name as well as the lower case aliases.
583   if (getLexer().is(AsmToken::Comma))
584     Lex();
585 
586   if (getLexer().isNot(AsmToken::Identifier) &&
587       getLexer().isNot(AsmToken::Hash) &&
588       getLexer().isNot(AsmToken::Percent) &&
589       getLexer().isNot(AsmToken::String)) {
590     if (!getLexer().getAllowAtInIdentifier())
591       return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
592                       "'%<type>' or \"<type>\"");
593     else if (getLexer().isNot(AsmToken::At))
594       return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
595                       "'%<type>' or \"<type>\"");
596   }
597 
598   if (getLexer().isNot(AsmToken::String) &&
599       getLexer().isNot(AsmToken::Identifier))
600     Lex();
601 
602   SMLoc TypeLoc = getLexer().getLoc();
603 
604   StringRef Type;
605   if (getParser().parseIdentifier(Type))
606     return TokError("expected symbol type in directive");
607 
608   MCSymbolAttr Attr = MCAttrForString(Type);
609   if (Attr == MCSA_Invalid)
610     return Error(TypeLoc, "unsupported attribute in '.type' directive");
611 
612   if (getLexer().isNot(AsmToken::EndOfStatement))
613     return TokError("unexpected token in '.type' directive");
614   Lex();
615 
616   getStreamer().EmitSymbolAttribute(Sym, Attr);
617 
618   return false;
619 }
620 
621 /// ParseDirectiveIdent
622 ///  ::= .ident string
623 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
624   if (getLexer().isNot(AsmToken::String))
625     return TokError("unexpected token in '.ident' directive");
626 
627   StringRef Data = getTok().getIdentifier();
628 
629   Lex();
630 
631   getStreamer().EmitIdent(Data);
632   return false;
633 }
634 
635 /// ParseDirectiveSymver
636 ///  ::= .symver foo, bar2@zed
637 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
638   StringRef Name;
639   if (getParser().parseIdentifier(Name))
640     return TokError("expected identifier in directive");
641 
642   if (getLexer().isNot(AsmToken::Comma))
643     return TokError("expected a comma");
644 
645   // ARM assembly uses @ for a comment...
646   // except when parsing the second parameter of the .symver directive.
647   // Force the next symbol to allow @ in the identifier, which is
648   // required for this directive and then reset it to its initial state.
649   const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
650   getLexer().setAllowAtInIdentifier(true);
651   Lex();
652   getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
653 
654   StringRef AliasName;
655   if (getParser().parseIdentifier(AliasName))
656     return TokError("expected identifier in directive");
657 
658   if (AliasName.find('@') == StringRef::npos)
659     return TokError("expected a '@' in the name");
660 
661   MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
662   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
663   const MCExpr *Value = MCSymbolRefExpr::create(Sym, getContext());
664 
665   getStreamer().EmitAssignment(Alias, Value);
666   return false;
667 }
668 
669 /// ParseDirectiveVersion
670 ///  ::= .version string
671 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
672   if (getLexer().isNot(AsmToken::String))
673     return TokError("unexpected token in '.version' directive");
674 
675   StringRef Data = getTok().getIdentifier();
676 
677   Lex();
678 
679   MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
680 
681   getStreamer().PushSection();
682   getStreamer().SwitchSection(Note);
683   getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
684   getStreamer().EmitIntValue(0, 4);             // descsz = 0 (no description).
685   getStreamer().EmitIntValue(1, 4);             // type = NT_VERSION.
686   getStreamer().EmitBytes(Data);                // name.
687   getStreamer().EmitIntValue(0, 1);             // terminate the string.
688   getStreamer().EmitValueToAlignment(4);        // ensure 4 byte alignment.
689   getStreamer().PopSection();
690   return false;
691 }
692 
693 /// ParseDirectiveWeakref
694 ///  ::= .weakref foo, bar
695 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
696   // FIXME: Share code with the other alias building directives.
697 
698   StringRef AliasName;
699   if (getParser().parseIdentifier(AliasName))
700     return TokError("expected identifier in directive");
701 
702   if (getLexer().isNot(AsmToken::Comma))
703     return TokError("expected a comma");
704 
705   Lex();
706 
707   StringRef Name;
708   if (getParser().parseIdentifier(Name))
709     return TokError("expected identifier in directive");
710 
711   MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
712 
713   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
714 
715   getStreamer().EmitWeakReference(Alias, Sym);
716   return false;
717 }
718 
719 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
720   const MCExpr *Subsection = nullptr;
721   if (getLexer().isNot(AsmToken::EndOfStatement)) {
722     if (getParser().parseExpression(Subsection))
723      return true;
724   }
725 
726   if (getLexer().isNot(AsmToken::EndOfStatement))
727     return TokError("unexpected token in directive");
728 
729   getStreamer().SubSection(Subsection);
730   return false;
731 }
732 
733 namespace llvm {
734 
735 MCAsmParserExtension *createELFAsmParser() {
736   return new ELFAsmParser;
737 }
738 
739 }
740