LLVM  5.0.0
ELFAsmParser.cpp
Go to the documentation of this file.
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/ADT/StringRef.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCDirectives.h"
16 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCSection.h"
21 #include "llvm/MC/MCSectionELF.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/MC/MCSymbolELF.h"
25 #include "llvm/MC/SectionKind.h"
26 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/SMLoc.h"
29 #include <cassert>
30 #include <cstdint>
31 #include <utility>
32 
33 using namespace llvm;
34 
35 namespace {
36 
37 class ELFAsmParser : public MCAsmParserExtension {
38  template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
39  void addDirectiveHandler(StringRef Directive) {
40  MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
41  this, HandleDirective<ELFAsmParser, HandlerMethod>);
42 
43  getParser().addDirectiveHandler(Directive, Handler);
44  }
45 
46  bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
48 
49 public:
50  ELFAsmParser() { BracketExpressionsSupported = true; }
51 
52  void Initialize(MCAsmParser &Parser) override {
53  // Call the base implementation.
55 
56  addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
57  addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
58  addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
59  addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
60  addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
61  addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
62  addDirectiveHandler<
63  &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
64  addDirectiveHandler<
65  &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
66  addDirectiveHandler<
67  &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
68  addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
69  addDirectiveHandler<
70  &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
71  addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
72  addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
73  addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
74  addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
75  addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
76  addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
77  addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
78  addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
79  addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
80  addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
81  addDirectiveHandler<
82  &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
83  addDirectiveHandler<
84  &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
85  addDirectiveHandler<
86  &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
87  addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
88  }
89 
90  // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
91  // the best way for us to get access to it?
92  bool ParseSectionDirectiveData(StringRef, SMLoc) {
93  return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
96  }
97  bool ParseSectionDirectiveText(StringRef, SMLoc) {
98  return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
101  }
102  bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
103  return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
106  }
107  bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
108  return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
111  }
112  bool ParseSectionDirectiveTData(StringRef, SMLoc) {
113  return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
117  }
118  bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
119  return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
123  }
124  bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
125  return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
128  }
129  bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
130  return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
134  }
135  bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
136  return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
139  }
140  bool ParseDirectivePushSection(StringRef, SMLoc);
141  bool ParseDirectivePopSection(StringRef, SMLoc);
142  bool ParseDirectiveSection(StringRef, SMLoc);
143  bool ParseDirectiveSize(StringRef, SMLoc);
144  bool ParseDirectivePrevious(StringRef, SMLoc);
145  bool ParseDirectiveType(StringRef, SMLoc);
146  bool ParseDirectiveIdent(StringRef, SMLoc);
147  bool ParseDirectiveSymver(StringRef, SMLoc);
148  bool ParseDirectiveVersion(StringRef, SMLoc);
149  bool ParseDirectiveWeakref(StringRef, SMLoc);
150  bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
151  bool ParseDirectiveSubsection(StringRef, SMLoc);
152 
153 private:
154  bool ParseSectionName(StringRef &SectionName);
155  bool ParseSectionArguments(bool IsPush, SMLoc loc);
156  unsigned parseSunStyleSectionFlags();
157  bool maybeParseSectionType(StringRef &TypeName);
158  bool parseMergeSize(int64_t &Size);
159  bool parseGroup(StringRef &GroupName);
160  bool parseMetadataSym(MCSymbolELF *&Associated);
161  bool maybeParseUniqueID(int64_t &UniqueID);
162 };
163 
164 } // end anonymous namespace
165 
166 /// ParseDirectiveSymbolAttribute
167 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
168 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
169  MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
170  .Case(".weak", MCSA_Weak)
171  .Case(".local", MCSA_Local)
172  .Case(".hidden", MCSA_Hidden)
173  .Case(".internal", MCSA_Internal)
174  .Case(".protected", MCSA_Protected)
176  assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
177  if (getLexer().isNot(AsmToken::EndOfStatement)) {
178  while (true) {
179  StringRef Name;
180 
181  if (getParser().parseIdentifier(Name))
182  return TokError("expected identifier in directive");
183 
184  MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
185 
186  getStreamer().EmitSymbolAttribute(Sym, Attr);
187 
188  if (getLexer().is(AsmToken::EndOfStatement))
189  break;
190 
191  if (getLexer().isNot(AsmToken::Comma))
192  return TokError("unexpected token in directive");
193  Lex();
194  }
195  }
196 
197  Lex();
198  return false;
199 }
200 
201 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
202  unsigned Flags, SectionKind Kind) {
203  const MCExpr *Subsection = nullptr;
204  if (getLexer().isNot(AsmToken::EndOfStatement)) {
205  if (getParser().parseExpression(Subsection))
206  return true;
207  }
208  Lex();
209 
210  getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
211  Subsection);
212 
213  return false;
214 }
215 
216 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
217  StringRef Name;
218  if (getParser().parseIdentifier(Name))
219  return TokError("expected identifier in directive");
220  MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
221 
222  if (getLexer().isNot(AsmToken::Comma))
223  return TokError("unexpected token in directive");
224  Lex();
225 
226  const MCExpr *Expr;
227  if (getParser().parseExpression(Expr))
228  return true;
229 
230  if (getLexer().isNot(AsmToken::EndOfStatement))
231  return TokError("unexpected token in directive");
232  Lex();
233 
234  getStreamer().emitELFSize(Sym, Expr);
235  return false;
236 }
237 
238 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
239  // A section name can contain -, so we cannot just use
240  // parseIdentifier.
241  SMLoc FirstLoc = getLexer().getLoc();
242  unsigned Size = 0;
243 
244  if (getLexer().is(AsmToken::String)) {
245  SectionName = getTok().getIdentifier();
246  Lex();
247  return false;
248  }
249 
250  while (true) {
251  SMLoc PrevLoc = getLexer().getLoc();
252  if (getLexer().is(AsmToken::Comma) ||
253  getLexer().is(AsmToken::EndOfStatement))
254  break;
255 
256  unsigned CurSize;
257  if (getLexer().is(AsmToken::String)) {
258  CurSize = getTok().getIdentifier().size() + 2;
259  Lex();
260  } else if (getLexer().is(AsmToken::Identifier)) {
261  CurSize = getTok().getIdentifier().size();
262  Lex();
263  } else {
264  CurSize = getTok().getString().size();
265  Lex();
266  }
267  Size += CurSize;
268  SectionName = StringRef(FirstLoc.getPointer(), Size);
269 
270  // Make sure the following token is adjacent.
271  if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
272  break;
273  }
274  if (Size == 0)
275  return true;
276 
277  return false;
278 }
279 
280 static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
281  unsigned flags = 0;
282 
283  // If a valid numerical value is set for the section flag, use it verbatim
284  if (!flagsStr.getAsInteger(0, flags))
285  return flags;
286 
287  for (char i : flagsStr) {
288  switch (i) {
289  case 'a':
290  flags |= ELF::SHF_ALLOC;
291  break;
292  case 'e':
293  flags |= ELF::SHF_EXCLUDE;
294  break;
295  case 'x':
296  flags |= ELF::SHF_EXECINSTR;
297  break;
298  case 'w':
299  flags |= ELF::SHF_WRITE;
300  break;
301  case 'o':
302  flags |= ELF::SHF_LINK_ORDER;
303  break;
304  case 'M':
305  flags |= ELF::SHF_MERGE;
306  break;
307  case 'S':
308  flags |= ELF::SHF_STRINGS;
309  break;
310  case 'T':
311  flags |= ELF::SHF_TLS;
312  break;
313  case 'c':
314  flags |= ELF::XCORE_SHF_CP_SECTION;
315  break;
316  case 'd':
317  flags |= ELF::XCORE_SHF_DP_SECTION;
318  break;
319  case 'y':
320  flags |= ELF::SHF_ARM_PURECODE;
321  break;
322  case 'G':
323  flags |= ELF::SHF_GROUP;
324  break;
325  case '?':
326  *UseLastGroup = true;
327  break;
328  default:
329  return -1U;
330  }
331  }
332 
333  return flags;
334 }
335 
336 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
337  unsigned flags = 0;
338  while (getLexer().is(AsmToken::Hash)) {
339  Lex(); // Eat the #.
340 
341  if (!getLexer().is(AsmToken::Identifier))
342  return -1U;
343 
344  StringRef flagId = getTok().getIdentifier();
345  if (flagId == "alloc")
346  flags |= ELF::SHF_ALLOC;
347  else if (flagId == "execinstr")
348  flags |= ELF::SHF_EXECINSTR;
349  else if (flagId == "write")
350  flags |= ELF::SHF_WRITE;
351  else if (flagId == "tls")
352  flags |= ELF::SHF_TLS;
353  else
354  return -1U;
355 
356  Lex(); // Eat the flag.
357 
358  if (!getLexer().is(AsmToken::Comma))
359  break;
360  Lex(); // Eat the comma.
361  }
362  return flags;
363 }
364 
365 
366 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
367  getStreamer().PushSection();
368 
369  if (ParseSectionArguments(/*IsPush=*/true, loc)) {
370  getStreamer().PopSection();
371  return true;
372  }
373 
374  return false;
375 }
376 
377 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
378  if (!getStreamer().PopSection())
379  return TokError(".popsection without corresponding .pushsection");
380  return false;
381 }
382 
383 // FIXME: This is a work in progress.
384 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
385  return ParseSectionArguments(/*IsPush=*/false, loc);
386 }
387 
388 bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
389  MCAsmLexer &L = getLexer();
390  if (L.isNot(AsmToken::Comma))
391  return false;
392  Lex();
393  if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
394  L.isNot(AsmToken::String)) {
395  if (L.getAllowAtInIdentifier())
396  return TokError("expected '@<type>', '%<type>' or \"<type>\"");
397  else
398  return TokError("expected '%<type>' or \"<type>\"");
399  }
400  if (!L.is(AsmToken::String))
401  Lex();
402  if (L.is(AsmToken::Integer)) {
403  TypeName = getTok().getString();
404  Lex();
405  } else if (getParser().parseIdentifier(TypeName))
406  return TokError("expected identifier in directive");
407  return false;
408 }
409 
410 bool ELFAsmParser::parseMergeSize(int64_t &Size) {
411  if (getLexer().isNot(AsmToken::Comma))
412  return TokError("expected the entry size");
413  Lex();
414  if (getParser().parseAbsoluteExpression(Size))
415  return true;
416  if (Size <= 0)
417  return TokError("entry size must be positive");
418  return false;
419 }
420 
421 bool ELFAsmParser::parseGroup(StringRef &GroupName) {
422  MCAsmLexer &L = getLexer();
423  if (L.isNot(AsmToken::Comma))
424  return TokError("expected group name");
425  Lex();
426  if (getParser().parseIdentifier(GroupName))
427  return true;
428  if (L.is(AsmToken::Comma)) {
429  Lex();
430  StringRef Linkage;
431  if (getParser().parseIdentifier(Linkage))
432  return true;
433  if (Linkage != "comdat")
434  return TokError("Linkage must be 'comdat'");
435  }
436  return false;
437 }
438 
439 bool ELFAsmParser::parseMetadataSym(MCSymbolELF *&Associated) {
440  MCAsmLexer &L = getLexer();
441  if (L.isNot(AsmToken::Comma))
442  return TokError("expected metadata symbol");
443  Lex();
444  StringRef Name;
445  if (getParser().parseIdentifier(Name))
446  return true;
447  Associated = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
448  if (!Associated || !Associated->isInSection())
449  return TokError("symbol is not in a section: " + Name);
450  return false;
451 }
452 
453 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
454  MCAsmLexer &L = getLexer();
455  if (L.isNot(AsmToken::Comma))
456  return false;
457  Lex();
458  StringRef UniqueStr;
459  if (getParser().parseIdentifier(UniqueStr))
460  return TokError("expected identifier in directive");
461  if (UniqueStr != "unique")
462  return TokError("expected 'unique'");
463  if (L.isNot(AsmToken::Comma))
464  return TokError("expected commma");
465  Lex();
466  if (getParser().parseAbsoluteExpression(UniqueID))
467  return true;
468  if (UniqueID < 0)
469  return TokError("unique id must be positive");
470  if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
471  return TokError("unique id is too large");
472  return false;
473 }
474 
475 static bool hasPrefix(StringRef SectionName, StringRef Prefix) {
476  return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back();
477 }
478 
479 bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
481 
482  if (ParseSectionName(SectionName))
483  return TokError("expected identifier in directive");
484 
486  int64_t Size = 0;
487  StringRef GroupName;
488  unsigned Flags = 0;
489  const MCExpr *Subsection = nullptr;
490  bool UseLastGroup = false;
491  StringRef UniqueStr;
492  MCSymbolELF *Associated = nullptr;
493  int64_t UniqueID = ~0;
494 
495  // Set the defaults first.
496  if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
497  Flags |= ELF::SHF_ALLOC;
498  if (SectionName == ".fini" || SectionName == ".init" ||
499  hasPrefix(SectionName, ".text."))
501  if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
502  hasPrefix(SectionName, ".bss.") ||
503  hasPrefix(SectionName, ".init_array.") ||
504  hasPrefix(SectionName, ".fini_array.") ||
505  hasPrefix(SectionName, ".preinit_array."))
506  Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE;
507  if (hasPrefix(SectionName, ".tdata.") ||
508  hasPrefix(SectionName, ".tbss."))
510 
511  if (getLexer().is(AsmToken::Comma)) {
512  Lex();
513 
514  if (IsPush && getLexer().isNot(AsmToken::String)) {
515  if (getParser().parseExpression(Subsection))
516  return true;
517  if (getLexer().isNot(AsmToken::Comma))
518  goto EndStmt;
519  Lex();
520  }
521 
522  unsigned extraFlags;
523 
524  if (getLexer().isNot(AsmToken::String)) {
525  if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
526  || getLexer().isNot(AsmToken::Hash))
527  return TokError("expected string in directive");
528  extraFlags = parseSunStyleSectionFlags();
529  } else {
530  StringRef FlagsStr = getTok().getStringContents();
531  Lex();
532  extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
533  }
534 
535  if (extraFlags == -1U)
536  return TokError("unknown flag");
537  Flags |= extraFlags;
538 
539  bool Mergeable = Flags & ELF::SHF_MERGE;
540  bool Group = Flags & ELF::SHF_GROUP;
541  if (Group && UseLastGroup)
542  return TokError("Section cannot specifiy a group name while also acting "
543  "as a member of the last group");
544 
545  if (maybeParseSectionType(TypeName))
546  return true;
547 
548  MCAsmLexer &L = getLexer();
549  if (TypeName.empty()) {
550  if (Mergeable)
551  return TokError("Mergeable section must specify the type");
552  if (Group)
553  return TokError("Group section must specify the type");
555  return TokError("unexpected token in directive");
556  }
557 
558  if (Mergeable)
559  if (parseMergeSize(Size))
560  return true;
561  if (Group)
562  if (parseGroup(GroupName))
563  return true;
564  if (Flags & ELF::SHF_LINK_ORDER)
565  if (parseMetadataSym(Associated))
566  return true;
567  if (maybeParseUniqueID(UniqueID))
568  return true;
569  }
570 
571 EndStmt:
572  if (getLexer().isNot(AsmToken::EndOfStatement))
573  return TokError("unexpected token in directive");
574  Lex();
575 
576  unsigned Type = ELF::SHT_PROGBITS;
577 
578  if (TypeName.empty()) {
579  if (SectionName.startswith(".note"))
580  Type = ELF::SHT_NOTE;
581  else if (hasPrefix(SectionName, ".init_array."))
582  Type = ELF::SHT_INIT_ARRAY;
583  else if (hasPrefix(SectionName, ".bss."))
584  Type = ELF::SHT_NOBITS;
585  else if (hasPrefix(SectionName, ".tbss."))
586  Type = ELF::SHT_NOBITS;
587  else if (hasPrefix(SectionName, ".fini_array."))
588  Type = ELF::SHT_FINI_ARRAY;
589  else if (hasPrefix(SectionName, ".preinit_array."))
590  Type = ELF::SHT_PREINIT_ARRAY;
591  } else {
592  if (TypeName == "init_array")
593  Type = ELF::SHT_INIT_ARRAY;
594  else if (TypeName == "fini_array")
595  Type = ELF::SHT_FINI_ARRAY;
596  else if (TypeName == "preinit_array")
597  Type = ELF::SHT_PREINIT_ARRAY;
598  else if (TypeName == "nobits")
599  Type = ELF::SHT_NOBITS;
600  else if (TypeName == "progbits")
601  Type = ELF::SHT_PROGBITS;
602  else if (TypeName == "note")
603  Type = ELF::SHT_NOTE;
604  else if (TypeName == "unwind")
605  Type = ELF::SHT_X86_64_UNWIND;
606  else if (TypeName == "llvm_odrtab")
607  Type = ELF::SHT_LLVM_ODRTAB;
608  else if (TypeName.getAsInteger(0, Type))
609  return TokError("unknown section type");
610  }
611 
612  if (UseLastGroup) {
613  MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
614  if (const MCSectionELF *Section =
615  cast_or_null<MCSectionELF>(CurrentSection.first))
616  if (const MCSymbol *Group = Section->getGroup()) {
617  GroupName = Group->getName();
618  Flags |= ELF::SHF_GROUP;
619  }
620  }
621 
622  MCSection *ELFSection =
623  getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
624  UniqueID, Associated);
625  getStreamer().SwitchSection(ELFSection, Subsection);
626 
627  if (getContext().getGenDwarfForAssembly()) {
628  bool InsertResult = getContext().addGenDwarfSection(ELFSection);
629  if (InsertResult) {
630  if (getContext().getDwarfVersion() <= 2)
631  Warning(loc, "DWARF2 only supports one section per compilation unit");
632 
633  if (!ELFSection->getBeginSymbol()) {
634  MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
635  getStreamer().EmitLabel(SectionStartSymbol);
636  ELFSection->setBeginSymbol(SectionStartSymbol);
637  }
638  }
639  }
640 
641  return false;
642 }
643 
644 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
645  MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
646  if (PreviousSection.first == nullptr)
647  return TokError(".previous without corresponding .section");
648  getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
649 
650  return false;
651 }
652 
655  .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
656  .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
657  .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
658  .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
659  .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
660  .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
662  .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
664 }
665 
666 /// ParseDirectiveELFType
667 /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
668 /// ::= .type identifier , #attribute
669 /// ::= .type identifier , @attribute
670 /// ::= .type identifier , %attribute
671 /// ::= .type identifier , "attribute"
672 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
673  StringRef Name;
674  if (getParser().parseIdentifier(Name))
675  return TokError("expected identifier in directive");
676 
677  // Handle the identifier as the key symbol.
678  MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
679 
680  // NOTE the comma is optional in all cases. It is only documented as being
681  // optional in the first case, however, GAS will silently treat the comma as
682  // optional in all cases. Furthermore, although the documentation states that
683  // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
684  // accepts both the upper case name as well as the lower case aliases.
685  if (getLexer().is(AsmToken::Comma))
686  Lex();
687 
688  if (getLexer().isNot(AsmToken::Identifier) &&
689  getLexer().isNot(AsmToken::Hash) &&
690  getLexer().isNot(AsmToken::Percent) &&
691  getLexer().isNot(AsmToken::String)) {
692  if (!getLexer().getAllowAtInIdentifier())
693  return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
694  "'%<type>' or \"<type>\"");
695  else if (getLexer().isNot(AsmToken::At))
696  return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
697  "'%<type>' or \"<type>\"");
698  }
699 
700  if (getLexer().isNot(AsmToken::String) &&
701  getLexer().isNot(AsmToken::Identifier))
702  Lex();
703 
704  SMLoc TypeLoc = getLexer().getLoc();
705 
706  StringRef Type;
707  if (getParser().parseIdentifier(Type))
708  return TokError("expected symbol type in directive");
709 
710  MCSymbolAttr Attr = MCAttrForString(Type);
711  if (Attr == MCSA_Invalid)
712  return Error(TypeLoc, "unsupported attribute in '.type' directive");
713 
714  if (getLexer().isNot(AsmToken::EndOfStatement))
715  return TokError("unexpected token in '.type' directive");
716  Lex();
717 
718  getStreamer().EmitSymbolAttribute(Sym, Attr);
719 
720  return false;
721 }
722 
723 /// ParseDirectiveIdent
724 /// ::= .ident string
725 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
726  if (getLexer().isNot(AsmToken::String))
727  return TokError("unexpected token in '.ident' directive");
728 
729  StringRef Data = getTok().getIdentifier();
730 
731  Lex();
732 
733  if (getLexer().isNot(AsmToken::EndOfStatement))
734  return TokError("unexpected token in '.ident' directive");
735  Lex();
736 
737  getStreamer().EmitIdent(Data);
738  return false;
739 }
740 
741 /// ParseDirectiveSymver
742 /// ::= .symver foo, bar2@zed
743 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
744  StringRef Name;
745  if (getParser().parseIdentifier(Name))
746  return TokError("expected identifier in directive");
747 
748  if (getLexer().isNot(AsmToken::Comma))
749  return TokError("expected a comma");
750 
751  // ARM assembly uses @ for a comment...
752  // except when parsing the second parameter of the .symver directive.
753  // Force the next symbol to allow @ in the identifier, which is
754  // required for this directive and then reset it to its initial state.
755  const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
756  getLexer().setAllowAtInIdentifier(true);
757  Lex();
758  getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
759 
760  StringRef AliasName;
761  if (getParser().parseIdentifier(AliasName))
762  return TokError("expected identifier in directive");
763 
764  if (AliasName.find('@') == StringRef::npos)
765  return TokError("expected a '@' in the name");
766 
767  MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
768  MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
769  const MCExpr *Value = MCSymbolRefExpr::create(Sym, getContext());
770 
771  getStreamer().EmitAssignment(Alias, Value);
772  getStreamer().emitELFSymverDirective(Alias, Sym);
773  return false;
774 }
775 
776 /// ParseDirectiveVersion
777 /// ::= .version string
778 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
779  if (getLexer().isNot(AsmToken::String))
780  return TokError("unexpected token in '.version' directive");
781 
782  StringRef Data = getTok().getIdentifier();
783 
784  Lex();
785 
786  MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
787 
788  getStreamer().PushSection();
789  getStreamer().SwitchSection(Note);
790  getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
791  getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
792  getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
793  getStreamer().EmitBytes(Data); // name.
794  getStreamer().EmitIntValue(0, 1); // terminate the string.
795  getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
796  getStreamer().PopSection();
797  return false;
798 }
799 
800 /// ParseDirectiveWeakref
801 /// ::= .weakref foo, bar
802 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
803  // FIXME: Share code with the other alias building directives.
804 
805  StringRef AliasName;
806  if (getParser().parseIdentifier(AliasName))
807  return TokError("expected identifier in directive");
808 
809  if (getLexer().isNot(AsmToken::Comma))
810  return TokError("expected a comma");
811 
812  Lex();
813 
814  StringRef Name;
815  if (getParser().parseIdentifier(Name))
816  return TokError("expected identifier in directive");
817 
818  MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
819 
820  MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
821 
822  getStreamer().EmitWeakReference(Alias, Sym);
823  return false;
824 }
825 
826 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
827  const MCExpr *Subsection = nullptr;
828  if (getLexer().isNot(AsmToken::EndOfStatement)) {
829  if (getParser().parseExpression(Subsection))
830  return true;
831  }
832 
833  if (getLexer().isNot(AsmToken::EndOfStatement))
834  return TokError("unexpected token in directive");
835 
836  Lex();
837 
838  getStreamer().SubSection(Subsection);
839  return false;
840 }
841 
842 namespace llvm {
843 
845  return new ELFAsmParser;
846 }
847 
848 } // end namespace llvm
MachineLoop * L
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:497
constexpr bool isUInt< 32 >(uint64_t x)
Definition: MathExtras.h:341
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
static SectionKind getData()
Definition: SectionKind.h:202
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:305
const char * getPointer() const
Definition: SMLoc.h:35
.type _foo, STT_OBJECT # aka
Definition: MCDirectives.h:25
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
Not a valid directive.
Definition: MCDirectives.h:20
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:68
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup)
static MCSymbolAttr MCAttrForString(StringRef Type)
.type _foo, STT_NOTYPE # aka
Definition: MCDirectives.h:28
MCAsmParserExtension * createELFAsmParser()
static SectionKind getBSS()
Definition: SectionKind.h:198
Generic assembler lexer interface, for use by target specific assembly lexers.
Definition: MCAsmLexer.h:149
static bool hasPrefix(StringRef SectionName, StringRef Prefix)
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(const T &Value) const
Definition: StringSwitch.h:244
.local (ELF)
Definition: MCDirectives.h:35
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(const char(&S)[N], const T &Value)
Definition: StringSwitch.h:74
bool isNot(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition: MCAsmLexer.h:256
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
.type _foo, STT_GNU_IFUNC
Definition: MCDirectives.h:24
.protected (ELF)
Definition: MCDirectives.h:40
static SectionKind getThreadData()
Definition: SectionKind.h:197
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
const char * Name
.hidden (ELF)
Definition: MCDirectives.h:31
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:298
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:23
bool getAllowAtInIdentifier()
Definition: MCAsmLexer.h:261
All sections with the "d" flag are grouped together by the linker to form the data section and the dp...
Definition: ELF.h:768
bool isInSection(bool SetUsed=true) const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
Definition: MCSymbol.h:255
bool is(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition: MCAsmLexer.h:253
static SectionKind getThreadBSS()
Definition: SectionKind.h:196
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
Definition: MCStreamer.h:51
.type _foo, STT_TLS # aka
Definition: MCDirectives.h:26
static SectionKind getReadOnlyWithRel()
Definition: SectionKind.h:203
MCSymbol * getBeginSymbol()
Definition: MCSection.h:106
MCSymbolAttr
Definition: MCDirectives.h:19
.internal (ELF)
Definition: MCDirectives.h:33
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
.type _foo, STT_COMMON # aka
Definition: MCDirectives.h:27
static std::vector< std::string > Flags
Definition: FlagsTest.cpp:8
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Cases(const char(&S0)[N0], const char(&S1)[N1], const T &Value)
Definition: StringSwitch.h:107
static const size_t npos
Definition: StringRef.h:51
.type _foo, STT_FUNC # aka
Definition: MCDirectives.h:23
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:28
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
All sections with the "c" flag are grouped together by the linker to form the constant pool and the c...
Definition: ELF.h:773
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
std::string DirName(const std::string &FileName)
LLVM Value Representation.
Definition: Value.h:73
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
Definition: MCAsmParser.h:72
const char SectionName[]
Definition: AMDGPUPTNote.h:24
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
Definition: StringRef.h:654
Represents a location in source code.
Definition: SMLoc.h:24
static SectionKind getReadOnly()
Definition: SectionKind.h:182
static SectionKind getText()
Definition: SectionKind.h:180
void setBeginSymbol(MCSymbol *Sym)
Definition: MCSection.h:110