1 //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/StringRef.h"
10 #include "llvm/ADT/StringSwitch.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCDirectives.h"
15 #include "llvm/MC/MCParser/MCAsmLexer.h"
16 #include "llvm/MC/MCParser/MCAsmParser.h"
17 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
18 #include "llvm/MC/MCSectionELF.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/MC/MCSymbolELF.h"
22 #include "llvm/MC/SectionKind.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/MathExtras.h"
25 #include "llvm/Support/SMLoc.h"
26 #include <cassert>
27 #include <cstdint>
28 #include <utility>
29 
30 using namespace llvm;
31 
32 namespace {
33 
34 class ELFAsmParser : public MCAsmParserExtension {
35   template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
36   void addDirectiveHandler(StringRef Directive) {
37     MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
38         this, HandleDirective<ELFAsmParser, HandlerMethod>);
39 
40     getParser().addDirectiveHandler(Directive, Handler);
41   }
42 
43   bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
44                           SectionKind Kind);
45 
46 public:
47   ELFAsmParser() { BracketExpressionsSupported = true; }
48 
49   void Initialize(MCAsmParser &Parser) override {
50     // Call the base implementation.
51     this->MCAsmParserExtension::Initialize(Parser);
52 
53     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
54     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
55     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
56     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
57     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
58     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
59     addDirectiveHandler<
60       &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
61     addDirectiveHandler<
62       &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
63     addDirectiveHandler<
64       &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
65     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
66     addDirectiveHandler<
67       &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
68     addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
69     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
70     addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
71     addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
72     addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
73     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
74     addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
75     addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
76     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
77     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
78     addDirectiveHandler<
79       &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
80     addDirectiveHandler<
81       &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
82     addDirectiveHandler<
83       &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
84     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
85     addDirectiveHandler<&ELFAsmParser::ParseDirectiveCGProfile>(".cg_profile");
86   }
87 
88   // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
89   // the best way for us to get access to it?
90   bool ParseSectionDirectiveData(StringRef, SMLoc) {
91     return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
92                               ELF::SHF_WRITE | ELF::SHF_ALLOC,
93                               SectionKind::getData());
94   }
95   bool ParseSectionDirectiveText(StringRef, SMLoc) {
96     return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
97                               ELF::SHF_EXECINSTR |
98                               ELF::SHF_ALLOC, SectionKind::getText());
99   }
100   bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
101     return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
102                               ELF::SHF_WRITE |
103                               ELF::SHF_ALLOC, SectionKind::getBSS());
104   }
105   bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
106     return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
107                               ELF::SHF_ALLOC,
108                               SectionKind::getReadOnly());
109   }
110   bool ParseSectionDirectiveTData(StringRef, SMLoc) {
111     return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
112                               ELF::SHF_ALLOC |
113                               ELF::SHF_TLS | ELF::SHF_WRITE,
114                               SectionKind::getThreadData());
115   }
116   bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
117     return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
118                               ELF::SHF_ALLOC |
119                               ELF::SHF_TLS | ELF::SHF_WRITE,
120                               SectionKind::getThreadBSS());
121   }
122   bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
123     return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
124                               ELF::SHF_ALLOC | ELF::SHF_WRITE,
125                               SectionKind::getData());
126   }
127   bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
128     return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
129                               ELF::SHF_ALLOC |
130                               ELF::SHF_WRITE,
131                               SectionKind::getReadOnlyWithRel());
132   }
133   bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
134     return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
135                               ELF::SHF_ALLOC | ELF::SHF_WRITE,
136                               SectionKind::getData());
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   bool ParseDirectiveCGProfile(StringRef, SMLoc);
151 
152 private:
153   bool ParseSectionName(StringRef &SectionName);
154   bool ParseSectionArguments(bool IsPush, SMLoc loc);
155   unsigned parseSunStyleSectionFlags();
156   bool maybeParseSectionType(StringRef &TypeName);
157   bool parseMergeSize(int64_t &Size);
158   bool parseGroup(StringRef &GroupName, bool &IsComdat);
159   bool parseLinkedToSym(MCSymbolELF *&LinkedToSym);
160   bool maybeParseUniqueID(int64_t &UniqueID);
161 };
162 
163 } // end anonymous namespace
164 
165 /// ParseDirectiveSymbolAttribute
166 ///  ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
167 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
168   MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
169     .Case(".weak", MCSA_Weak)
170     .Case(".local", MCSA_Local)
171     .Case(".hidden", MCSA_Hidden)
172     .Case(".internal", MCSA_Internal)
173     .Case(".protected", MCSA_Protected)
174     .Default(MCSA_Invalid);
175   assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
176   if (getLexer().isNot(AsmToken::EndOfStatement)) {
177     while (true) {
178       StringRef Name;
179 
180       if (getParser().parseIdentifier(Name))
181         return TokError("expected identifier in directive");
182 
183       if (getParser().discardLTOSymbol(Name)) {
184         if (getLexer().is(AsmToken::EndOfStatement))
185           break;
186         continue;
187       }
188 
189       MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
190 
191       getStreamer().emitSymbolAttribute(Sym, Attr);
192 
193       if (getLexer().is(AsmToken::EndOfStatement))
194         break;
195 
196       if (getLexer().isNot(AsmToken::Comma))
197         return TokError("unexpected token in directive");
198       Lex();
199     }
200   }
201 
202   Lex();
203   return false;
204 }
205 
206 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
207                                       unsigned Flags, SectionKind Kind) {
208   const MCExpr *Subsection = nullptr;
209   if (getLexer().isNot(AsmToken::EndOfStatement)) {
210     if (getParser().parseExpression(Subsection))
211       return true;
212   }
213   Lex();
214 
215   getStreamer().switchSection(getContext().getELFSection(Section, Type, Flags),
216                               Subsection);
217 
218   return false;
219 }
220 
221 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
222   StringRef Name;
223   if (getParser().parseIdentifier(Name))
224     return TokError("expected identifier in directive");
225   MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
226 
227   if (getLexer().isNot(AsmToken::Comma))
228     return TokError("unexpected token in directive");
229   Lex();
230 
231   const MCExpr *Expr;
232   if (getParser().parseExpression(Expr))
233     return true;
234 
235   if (getLexer().isNot(AsmToken::EndOfStatement))
236     return TokError("unexpected token in directive");
237   Lex();
238 
239   getStreamer().emitELFSize(Sym, Expr);
240   return false;
241 }
242 
243 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
244   // A section name can contain -, so we cannot just use
245   // parseIdentifier.
246   SMLoc FirstLoc = getLexer().getLoc();
247   unsigned Size = 0;
248 
249   if (getLexer().is(AsmToken::String)) {
250     SectionName = getTok().getIdentifier();
251     Lex();
252     return false;
253   }
254 
255   while (!getParser().hasPendingError()) {
256     SMLoc PrevLoc = getLexer().getLoc();
257     if (getLexer().is(AsmToken::Comma) ||
258       getLexer().is(AsmToken::EndOfStatement))
259       break;
260 
261     unsigned CurSize;
262     if (getLexer().is(AsmToken::String)) {
263       CurSize = getTok().getIdentifier().size() + 2;
264       Lex();
265     } else if (getLexer().is(AsmToken::Identifier)) {
266       CurSize = getTok().getIdentifier().size();
267       Lex();
268     } else {
269       CurSize = getTok().getString().size();
270       Lex();
271     }
272     Size += CurSize;
273     SectionName = StringRef(FirstLoc.getPointer(), Size);
274 
275     // Make sure the following token is adjacent.
276     if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
277       break;
278   }
279   if (Size == 0)
280     return true;
281 
282   return false;
283 }
284 
285 static unsigned parseSectionFlags(const Triple &TT, StringRef flagsStr,
286                                   bool *UseLastGroup) {
287   unsigned flags = 0;
288 
289   // If a valid numerical value is set for the section flag, use it verbatim
290   if (!flagsStr.getAsInteger(0, flags))
291     return flags;
292 
293   for (char i : flagsStr) {
294     switch (i) {
295     case 'a':
296       flags |= ELF::SHF_ALLOC;
297       break;
298     case 'e':
299       flags |= ELF::SHF_EXCLUDE;
300       break;
301     case 'x':
302       flags |= ELF::SHF_EXECINSTR;
303       break;
304     case 'w':
305       flags |= ELF::SHF_WRITE;
306       break;
307     case 'o':
308       flags |= ELF::SHF_LINK_ORDER;
309       break;
310     case 'M':
311       flags |= ELF::SHF_MERGE;
312       break;
313     case 'S':
314       flags |= ELF::SHF_STRINGS;
315       break;
316     case 'T':
317       flags |= ELF::SHF_TLS;
318       break;
319     case 'c':
320       flags |= ELF::XCORE_SHF_CP_SECTION;
321       break;
322     case 'd':
323       flags |= ELF::XCORE_SHF_DP_SECTION;
324       break;
325     case 'y':
326       flags |= ELF::SHF_ARM_PURECODE;
327       break;
328     case 's':
329       flags |= ELF::SHF_HEX_GPREL;
330       break;
331     case 'G':
332       flags |= ELF::SHF_GROUP;
333       break;
334     case 'R':
335       if (TT.isOSSolaris())
336         flags |= ELF::SHF_SUNW_NODISCARD;
337       else
338         flags |= ELF::SHF_GNU_RETAIN;
339       break;
340     case '?':
341       *UseLastGroup = true;
342       break;
343     default:
344       return -1U;
345     }
346   }
347 
348   return flags;
349 }
350 
351 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
352   unsigned flags = 0;
353   while (getLexer().is(AsmToken::Hash)) {
354     Lex(); // Eat the #.
355 
356     if (!getLexer().is(AsmToken::Identifier))
357       return -1U;
358 
359     StringRef flagId = getTok().getIdentifier();
360     if (flagId == "alloc")
361       flags |= ELF::SHF_ALLOC;
362     else if (flagId == "execinstr")
363       flags |= ELF::SHF_EXECINSTR;
364     else if (flagId == "write")
365       flags |= ELF::SHF_WRITE;
366     else if (flagId == "tls")
367       flags |= ELF::SHF_TLS;
368     else
369       return -1U;
370 
371     Lex(); // Eat the flag.
372 
373     if (!getLexer().is(AsmToken::Comma))
374         break;
375     Lex(); // Eat the comma.
376   }
377   return flags;
378 }
379 
380 
381 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
382   getStreamer().pushSection();
383 
384   if (ParseSectionArguments(/*IsPush=*/true, loc)) {
385     getStreamer().popSection();
386     return true;
387   }
388 
389   return false;
390 }
391 
392 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
393   if (!getStreamer().popSection())
394     return TokError(".popsection without corresponding .pushsection");
395   return false;
396 }
397 
398 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
399   return ParseSectionArguments(/*IsPush=*/false, loc);
400 }
401 
402 bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
403   MCAsmLexer &L = getLexer();
404   if (L.isNot(AsmToken::Comma))
405     return false;
406   Lex();
407   if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
408       L.isNot(AsmToken::String)) {
409     if (L.getAllowAtInIdentifier())
410       return TokError("expected '@<type>', '%<type>' or \"<type>\"");
411     else
412       return TokError("expected '%<type>' or \"<type>\"");
413   }
414   if (!L.is(AsmToken::String))
415     Lex();
416   if (L.is(AsmToken::Integer)) {
417     TypeName = getTok().getString();
418     Lex();
419   } else if (getParser().parseIdentifier(TypeName))
420     return TokError("expected identifier in directive");
421   return false;
422 }
423 
424 bool ELFAsmParser::parseMergeSize(int64_t &Size) {
425   if (getLexer().isNot(AsmToken::Comma))
426     return TokError("expected the entry size");
427   Lex();
428   if (getParser().parseAbsoluteExpression(Size))
429     return true;
430   if (Size <= 0)
431     return TokError("entry size must be positive");
432   return false;
433 }
434 
435 bool ELFAsmParser::parseGroup(StringRef &GroupName, bool &IsComdat) {
436   MCAsmLexer &L = getLexer();
437   if (L.isNot(AsmToken::Comma))
438     return TokError("expected group name");
439   Lex();
440   if (L.is(AsmToken::Integer)) {
441     GroupName = getTok().getString();
442     Lex();
443   } else if (getParser().parseIdentifier(GroupName)) {
444     return TokError("invalid group name");
445   }
446   if (L.is(AsmToken::Comma)) {
447     Lex();
448     StringRef Linkage;
449     if (getParser().parseIdentifier(Linkage))
450       return TokError("invalid linkage");
451     if (Linkage != "comdat")
452       return TokError("Linkage must be 'comdat'");
453     IsComdat = true;
454   } else {
455     IsComdat = false;
456   }
457   return false;
458 }
459 
460 bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) {
461   MCAsmLexer &L = getLexer();
462   if (L.isNot(AsmToken::Comma))
463     return TokError("expected linked-to symbol");
464   Lex();
465   StringRef Name;
466   SMLoc StartLoc = L.getLoc();
467   if (getParser().parseIdentifier(Name)) {
468     if (getParser().getTok().getString() == "0") {
469       getParser().Lex();
470       LinkedToSym = nullptr;
471       return false;
472     }
473     return TokError("invalid linked-to symbol");
474   }
475   LinkedToSym = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
476   if (!LinkedToSym || !LinkedToSym->isInSection())
477     return Error(StartLoc, "linked-to symbol is not in a section: " + Name);
478   return false;
479 }
480 
481 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
482   MCAsmLexer &L = getLexer();
483   if (L.isNot(AsmToken::Comma))
484     return false;
485   Lex();
486   StringRef UniqueStr;
487   if (getParser().parseIdentifier(UniqueStr))
488     return TokError("expected identifier in directive");
489   if (UniqueStr != "unique")
490     return TokError("expected 'unique'");
491   if (L.isNot(AsmToken::Comma))
492     return TokError("expected commma");
493   Lex();
494   if (getParser().parseAbsoluteExpression(UniqueID))
495     return true;
496   if (UniqueID < 0)
497     return TokError("unique id must be positive");
498   if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
499     return TokError("unique id is too large");
500   return false;
501 }
502 
503 static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
504   return SectionName.consume_front(Prefix) &&
505          (SectionName.empty() || SectionName[0] == '.');
506 }
507 
508 static bool allowSectionTypeMismatch(const Triple &TT, StringRef SectionName,
509                                      unsigned Type) {
510   if (TT.getArch() == Triple::x86_64) {
511     // x86-64 psABI names SHT_X86_64_UNWIND as the canonical type for .eh_frame,
512     // but GNU as emits SHT_PROGBITS .eh_frame for .cfi_* directives. Don't
513     // error for SHT_PROGBITS .eh_frame
514     return SectionName == ".eh_frame" && Type == ELF::SHT_PROGBITS;
515   }
516   if (TT.isMIPS()) {
517     // MIPS .debug_* sections should have SHT_MIPS_DWARF section type to
518     // distinguish among sections contain DWARF and ECOFF debug formats,
519     // but in assembly files these sections have SHT_PROGBITS type.
520     return SectionName.startswith(".debug_") && Type == ELF::SHT_PROGBITS;
521   }
522   return false;
523 }
524 
525 bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
526   StringRef SectionName;
527 
528   if (ParseSectionName(SectionName))
529     return TokError("expected identifier in directive");
530 
531   StringRef TypeName;
532   int64_t Size = 0;
533   StringRef GroupName;
534   bool IsComdat = false;
535   unsigned Flags = 0;
536   unsigned extraFlags = 0;
537   const MCExpr *Subsection = nullptr;
538   bool UseLastGroup = false;
539   MCSymbolELF *LinkedToSym = nullptr;
540   int64_t UniqueID = ~0;
541 
542   // Set the defaults first.
543   if (hasPrefix(SectionName, ".rodata") || SectionName == ".rodata1")
544     Flags |= ELF::SHF_ALLOC;
545   else if (SectionName == ".fini" || SectionName == ".init" ||
546            hasPrefix(SectionName, ".text"))
547     Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR;
548   else if (hasPrefix(SectionName, ".data") || SectionName == ".data1" ||
549            hasPrefix(SectionName, ".bss") ||
550            hasPrefix(SectionName, ".init_array") ||
551            hasPrefix(SectionName, ".fini_array") ||
552            hasPrefix(SectionName, ".preinit_array"))
553     Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE;
554   else if (hasPrefix(SectionName, ".tdata") || hasPrefix(SectionName, ".tbss"))
555     Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS;
556 
557   if (getLexer().is(AsmToken::Comma)) {
558     Lex();
559 
560     if (IsPush && getLexer().isNot(AsmToken::String)) {
561       if (getParser().parseExpression(Subsection))
562         return true;
563       if (getLexer().isNot(AsmToken::Comma))
564         goto EndStmt;
565       Lex();
566     }
567 
568     if (getLexer().isNot(AsmToken::String)) {
569       if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
570           || getLexer().isNot(AsmToken::Hash))
571         return TokError("expected string in directive");
572       extraFlags = parseSunStyleSectionFlags();
573     } else {
574       StringRef FlagsStr = getTok().getStringContents();
575       Lex();
576       extraFlags = parseSectionFlags(getContext().getTargetTriple(), FlagsStr,
577                                      &UseLastGroup);
578     }
579 
580     if (extraFlags == -1U)
581       return TokError("unknown flag");
582     Flags |= extraFlags;
583 
584     bool Mergeable = Flags & ELF::SHF_MERGE;
585     bool Group = Flags & ELF::SHF_GROUP;
586     if (Group && UseLastGroup)
587       return TokError("Section cannot specifiy a group name while also acting "
588                       "as a member of the last group");
589 
590     if (maybeParseSectionType(TypeName))
591       return true;
592 
593     MCAsmLexer &L = getLexer();
594     if (TypeName.empty()) {
595       if (Mergeable)
596         return TokError("Mergeable section must specify the type");
597       if (Group)
598         return TokError("Group section must specify the type");
599       if (L.isNot(AsmToken::EndOfStatement))
600         return TokError("unexpected token in directive");
601     }
602 
603     if (Mergeable)
604       if (parseMergeSize(Size))
605         return true;
606     if (Group)
607       if (parseGroup(GroupName, IsComdat))
608         return true;
609     if (Flags & ELF::SHF_LINK_ORDER)
610       if (parseLinkedToSym(LinkedToSym))
611         return true;
612     if (maybeParseUniqueID(UniqueID))
613       return true;
614   }
615 
616 EndStmt:
617   if (getLexer().isNot(AsmToken::EndOfStatement))
618     return TokError("unexpected token in directive");
619   Lex();
620 
621   unsigned Type = ELF::SHT_PROGBITS;
622 
623   if (TypeName.empty()) {
624     if (SectionName.startswith(".note"))
625       Type = ELF::SHT_NOTE;
626     else if (hasPrefix(SectionName, ".init_array"))
627       Type = ELF::SHT_INIT_ARRAY;
628     else if (hasPrefix(SectionName, ".bss"))
629       Type = ELF::SHT_NOBITS;
630     else if (hasPrefix(SectionName, ".tbss"))
631       Type = ELF::SHT_NOBITS;
632     else if (hasPrefix(SectionName, ".fini_array"))
633       Type = ELF::SHT_FINI_ARRAY;
634     else if (hasPrefix(SectionName, ".preinit_array"))
635       Type = ELF::SHT_PREINIT_ARRAY;
636   } else {
637     if (TypeName == "init_array")
638       Type = ELF::SHT_INIT_ARRAY;
639     else if (TypeName == "fini_array")
640       Type = ELF::SHT_FINI_ARRAY;
641     else if (TypeName == "preinit_array")
642       Type = ELF::SHT_PREINIT_ARRAY;
643     else if (TypeName == "nobits")
644       Type = ELF::SHT_NOBITS;
645     else if (TypeName == "progbits")
646       Type = ELF::SHT_PROGBITS;
647     else if (TypeName == "note")
648       Type = ELF::SHT_NOTE;
649     else if (TypeName == "unwind")
650       Type = ELF::SHT_X86_64_UNWIND;
651     else if (TypeName == "llvm_odrtab")
652       Type = ELF::SHT_LLVM_ODRTAB;
653     else if (TypeName == "llvm_linker_options")
654       Type = ELF::SHT_LLVM_LINKER_OPTIONS;
655     else if (TypeName == "llvm_call_graph_profile")
656       Type = ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
657     else if (TypeName == "llvm_dependent_libraries")
658       Type = ELF::SHT_LLVM_DEPENDENT_LIBRARIES;
659     else if (TypeName == "llvm_sympart")
660       Type = ELF::SHT_LLVM_SYMPART;
661     else if (TypeName == "llvm_bb_addr_map")
662       Type = ELF::SHT_LLVM_BB_ADDR_MAP;
663     else if (TypeName.getAsInteger(0, Type))
664       return TokError("unknown section type");
665   }
666 
667   if (UseLastGroup) {
668     MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
669     if (const MCSectionELF *Section =
670             cast_or_null<MCSectionELF>(CurrentSection.first))
671       if (const MCSymbol *Group = Section->getGroup()) {
672         GroupName = Group->getName();
673         IsComdat = Section->isComdat();
674         Flags |= ELF::SHF_GROUP;
675       }
676   }
677 
678   MCSectionELF *Section =
679       getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
680                                  IsComdat, UniqueID, LinkedToSym);
681   getStreamer().switchSection(Section, Subsection);
682   // Check that flags are used consistently. However, the GNU assembler permits
683   // to leave out in subsequent uses of the same sections; for compatibility,
684   // do likewise.
685   if (!TypeName.empty() && Section->getType() != Type &&
686       !allowSectionTypeMismatch(getContext().getTargetTriple(), SectionName,
687                                 Type))
688     Error(loc, "changed section type for " + SectionName + ", expected: 0x" +
689                    utohexstr(Section->getType()));
690   if ((extraFlags || Size || !TypeName.empty()) && Section->getFlags() != Flags)
691     Error(loc, "changed section flags for " + SectionName + ", expected: 0x" +
692                    utohexstr(Section->getFlags()));
693   if ((extraFlags || Size || !TypeName.empty()) &&
694       Section->getEntrySize() != Size)
695     Error(loc, "changed section entsize for " + SectionName +
696                    ", expected: " + Twine(Section->getEntrySize()));
697 
698   if (getContext().getGenDwarfForAssembly() &&
699       (Section->getFlags() & ELF::SHF_ALLOC) &&
700       (Section->getFlags() & ELF::SHF_EXECINSTR)) {
701     bool InsertResult = getContext().addGenDwarfSection(Section);
702     if (InsertResult) {
703       if (getContext().getDwarfVersion() <= 2)
704         Warning(loc, "DWARF2 only supports one section per compilation unit");
705 
706       if (!Section->getBeginSymbol()) {
707         MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
708         getStreamer().emitLabel(SectionStartSymbol);
709         Section->setBeginSymbol(SectionStartSymbol);
710       }
711     }
712   }
713 
714   return false;
715 }
716 
717 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
718   MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
719   if (PreviousSection.first == nullptr)
720       return TokError(".previous without corresponding .section");
721   getStreamer().switchSection(PreviousSection.first, PreviousSection.second);
722 
723   return false;
724 }
725 
726 static MCSymbolAttr MCAttrForString(StringRef Type) {
727   return StringSwitch<MCSymbolAttr>(Type)
728           .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
729           .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
730           .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
731           .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
732           .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
733           .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
734                  MCSA_ELF_TypeIndFunction)
735           .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
736           .Default(MCSA_Invalid);
737 }
738 
739 /// ParseDirectiveELFType
740 ///  ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
741 ///  ::= .type identifier , #attribute
742 ///  ::= .type identifier , @attribute
743 ///  ::= .type identifier , %attribute
744 ///  ::= .type identifier , "attribute"
745 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
746   StringRef Name;
747   if (getParser().parseIdentifier(Name))
748     return TokError("expected identifier in directive");
749 
750   // Handle the identifier as the key symbol.
751   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
752 
753   // NOTE the comma is optional in all cases.  It is only documented as being
754   // optional in the first case, however, GAS will silently treat the comma as
755   // optional in all cases.  Furthermore, although the documentation states that
756   // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
757   // accepts both the upper case name as well as the lower case aliases.
758   if (getLexer().is(AsmToken::Comma))
759     Lex();
760 
761   if (getLexer().isNot(AsmToken::Identifier) &&
762       getLexer().isNot(AsmToken::Hash) &&
763       getLexer().isNot(AsmToken::Percent) &&
764       getLexer().isNot(AsmToken::String)) {
765     if (!getLexer().getAllowAtInIdentifier())
766       return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
767                       "'%<type>' or \"<type>\"");
768     else if (getLexer().isNot(AsmToken::At))
769       return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
770                       "'%<type>' or \"<type>\"");
771   }
772 
773   if (getLexer().isNot(AsmToken::String) &&
774       getLexer().isNot(AsmToken::Identifier))
775     Lex();
776 
777   SMLoc TypeLoc = getLexer().getLoc();
778 
779   StringRef Type;
780   if (getParser().parseIdentifier(Type))
781     return TokError("expected symbol type in directive");
782 
783   MCSymbolAttr Attr = MCAttrForString(Type);
784   if (Attr == MCSA_Invalid)
785     return Error(TypeLoc, "unsupported attribute in '.type' directive");
786 
787   if (getLexer().isNot(AsmToken::EndOfStatement))
788     return TokError("unexpected token in '.type' directive");
789   Lex();
790 
791   getStreamer().emitSymbolAttribute(Sym, Attr);
792 
793   return false;
794 }
795 
796 /// ParseDirectiveIdent
797 ///  ::= .ident string
798 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
799   if (getLexer().isNot(AsmToken::String))
800     return TokError("unexpected token in '.ident' directive");
801 
802   StringRef Data = getTok().getIdentifier();
803 
804   Lex();
805 
806   if (getLexer().isNot(AsmToken::EndOfStatement))
807     return TokError("unexpected token in '.ident' directive");
808   Lex();
809 
810   getStreamer().emitIdent(Data);
811   return false;
812 }
813 
814 /// ParseDirectiveSymver
815 ///  ::= .symver foo, bar2@zed
816 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
817   StringRef OriginalName, Name, Action;
818   if (getParser().parseIdentifier(OriginalName))
819     return TokError("expected identifier in directive");
820 
821   if (getLexer().isNot(AsmToken::Comma))
822     return TokError("expected a comma");
823 
824   // ARM assembly uses @ for a comment...
825   // except when parsing the second parameter of the .symver directive.
826   // Force the next symbol to allow @ in the identifier, which is
827   // required for this directive and then reset it to its initial state.
828   const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
829   getLexer().setAllowAtInIdentifier(true);
830   Lex();
831   getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
832 
833   if (getParser().parseIdentifier(Name))
834     return TokError("expected identifier in directive");
835 
836   if (!Name.contains('@'))
837     return TokError("expected a '@' in the name");
838   bool KeepOriginalSym = !Name.contains("@@@");
839   if (parseOptionalToken(AsmToken::Comma)) {
840     if (getParser().parseIdentifier(Action) || Action != "remove")
841       return TokError("expected 'remove'");
842     KeepOriginalSym = false;
843   }
844   (void)parseOptionalToken(AsmToken::EndOfStatement);
845 
846   getStreamer().emitELFSymverDirective(
847       getContext().getOrCreateSymbol(OriginalName), Name, KeepOriginalSym);
848   return false;
849 }
850 
851 /// ParseDirectiveVersion
852 ///  ::= .version string
853 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
854   if (getLexer().isNot(AsmToken::String))
855     return TokError("unexpected token in '.version' directive");
856 
857   StringRef Data = getTok().getIdentifier();
858 
859   Lex();
860 
861   MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
862 
863   getStreamer().pushSection();
864   getStreamer().switchSection(Note);
865   getStreamer().emitInt32(Data.size() + 1); // namesz
866   getStreamer().emitInt32(0);               // descsz = 0 (no description).
867   getStreamer().emitInt32(1);               // type = NT_VERSION
868   getStreamer().emitBytes(Data);            // name
869   getStreamer().emitInt8(0);                // NUL
870   getStreamer().emitValueToAlignment(4);
871   getStreamer().popSection();
872   return false;
873 }
874 
875 /// ParseDirectiveWeakref
876 ///  ::= .weakref foo, bar
877 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
878   // FIXME: Share code with the other alias building directives.
879 
880   StringRef AliasName;
881   if (getParser().parseIdentifier(AliasName))
882     return TokError("expected identifier in directive");
883 
884   if (getLexer().isNot(AsmToken::Comma))
885     return TokError("expected a comma");
886 
887   Lex();
888 
889   StringRef Name;
890   if (getParser().parseIdentifier(Name))
891     return TokError("expected identifier in directive");
892 
893   MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
894 
895   MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
896 
897   getStreamer().emitWeakReference(Alias, Sym);
898   return false;
899 }
900 
901 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
902   const MCExpr *Subsection = nullptr;
903   if (getLexer().isNot(AsmToken::EndOfStatement)) {
904     if (getParser().parseExpression(Subsection))
905      return true;
906   }
907 
908   if (getLexer().isNot(AsmToken::EndOfStatement))
909     return TokError("unexpected token in directive");
910 
911   Lex();
912 
913   getStreamer().subSection(Subsection);
914   return false;
915 }
916 
917 bool ELFAsmParser::ParseDirectiveCGProfile(StringRef S, SMLoc Loc) {
918   return MCAsmParserExtension::ParseDirectiveCGProfile(S, Loc);
919 }
920 
921 namespace llvm {
922 
923 MCAsmParserExtension *createELFAsmParser() {
924   return new ELFAsmParser;
925 }
926 
927 } // end namespace llvm
928