LLVM  13.0.0git
ELFAsmParser.cpp
Go to the documentation of this file.
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/MCExpr.h"
19 #include "llvm/MC/MCSection.h"
20 #include "llvm/MC/MCSectionELF.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/MC/MCSymbolELF.h"
24 #include "llvm/MC/SectionKind.h"
25 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/SMLoc.h"
28 #include <cassert>
29 #include <cstdint>
30 #include <utility>
31 
32 using namespace llvm;
33 
34 namespace {
35 
36 class ELFAsmParser : public MCAsmParserExtension {
37  template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
38  void addDirectiveHandler(StringRef Directive) {
39  MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
40  this, HandleDirective<ELFAsmParser, HandlerMethod>);
41 
42  getParser().addDirectiveHandler(Directive, Handler);
43  }
44 
45  bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
47 
48 public:
49  ELFAsmParser() { BracketExpressionsSupported = true; }
50 
51  void Initialize(MCAsmParser &Parser) override {
52  // Call the base implementation.
54 
55  addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
56  addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
57  addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
58  addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
59  addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
60  addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
61  addDirectiveHandler<
62  &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
63  addDirectiveHandler<
64  &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
65  addDirectiveHandler<
66  &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
67  addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
68  addDirectiveHandler<
69  &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
70  addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
71  addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
72  addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
73  addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
74  addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
75  addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
76  addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
77  addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
78  addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
79  addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
80  addDirectiveHandler<
81  &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
82  addDirectiveHandler<
83  &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
84  addDirectiveHandler<
85  &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
86  addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
87  addDirectiveHandler<&ELFAsmParser::ParseDirectiveCGProfile>(".cg_profile");
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  bool ParseDirectiveCGProfile(StringRef, SMLoc);
153 
154 private:
155  bool ParseSectionName(StringRef &SectionName);
156  bool ParseSectionArguments(bool IsPush, SMLoc loc);
157  unsigned parseSunStyleSectionFlags();
158  bool maybeParseSectionType(StringRef &TypeName);
159  bool parseMergeSize(int64_t &Size);
160  bool parseGroup(StringRef &GroupName, bool &IsComdat);
161  bool parseLinkedToSym(MCSymbolELF *&LinkedToSym);
162  bool maybeParseUniqueID(int64_t &UniqueID);
163 };
164 
165 } // end anonymous namespace
166 
167 /// ParseDirectiveSymbolAttribute
168 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
169 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
171  .Case(".weak", MCSA_Weak)
172  .Case(".local", MCSA_Local)
173  .Case(".hidden", MCSA_Hidden)
174  .Case(".internal", MCSA_Internal)
175  .Case(".protected", MCSA_Protected)
177  assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
178  if (getLexer().isNot(AsmToken::EndOfStatement)) {
179  while (true) {
180  StringRef Name;
181 
182  if (getParser().parseIdentifier(Name))
183  return TokError("expected identifier in directive");
184 
185  if (getParser().discardLTOSymbol(Name)) {
186  if (getLexer().is(AsmToken::EndOfStatement))
187  break;
188  continue;
189  }
190 
191  MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
192 
193  getStreamer().emitSymbolAttribute(Sym, Attr);
194 
195  if (getLexer().is(AsmToken::EndOfStatement))
196  break;
197 
198  if (getLexer().isNot(AsmToken::Comma))
199  return TokError("unexpected token in directive");
200  Lex();
201  }
202  }
203 
204  Lex();
205  return false;
206 }
207 
208 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
209  unsigned Flags, SectionKind Kind) {
210  const MCExpr *Subsection = nullptr;
211  if (getLexer().isNot(AsmToken::EndOfStatement)) {
212  if (getParser().parseExpression(Subsection))
213  return true;
214  }
215  Lex();
216 
217  getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags),
218  Subsection);
219 
220  return false;
221 }
222 
223 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
224  StringRef Name;
225  if (getParser().parseIdentifier(Name))
226  return TokError("expected identifier in directive");
227  MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
228 
229  if (getLexer().isNot(AsmToken::Comma))
230  return TokError("unexpected token in directive");
231  Lex();
232 
233  const MCExpr *Expr;
234  if (getParser().parseExpression(Expr))
235  return true;
236 
237  if (getLexer().isNot(AsmToken::EndOfStatement))
238  return TokError("unexpected token in directive");
239  Lex();
240 
241  getStreamer().emitELFSize(Sym, Expr);
242  return false;
243 }
244 
245 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
246  // A section name can contain -, so we cannot just use
247  // parseIdentifier.
248  SMLoc FirstLoc = getLexer().getLoc();
249  unsigned Size = 0;
250 
251  if (getLexer().is(AsmToken::String)) {
252  SectionName = getTok().getIdentifier();
253  Lex();
254  return false;
255  }
256 
257  while (!getParser().hasPendingError()) {
258  SMLoc PrevLoc = getLexer().getLoc();
259  if (getLexer().is(AsmToken::Comma) ||
260  getLexer().is(AsmToken::EndOfStatement))
261  break;
262 
263  unsigned CurSize;
264  if (getLexer().is(AsmToken::String)) {
265  CurSize = getTok().getIdentifier().size() + 2;
266  Lex();
267  } else if (getLexer().is(AsmToken::Identifier)) {
268  CurSize = getTok().getIdentifier().size();
269  Lex();
270  } else {
271  CurSize = getTok().getString().size();
272  Lex();
273  }
274  Size += CurSize;
275  SectionName = StringRef(FirstLoc.getPointer(), Size);
276 
277  // Make sure the following token is adjacent.
278  if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
279  break;
280  }
281  if (Size == 0)
282  return true;
283 
284  return false;
285 }
286 
287 static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
288  unsigned flags = 0;
289 
290  // If a valid numerical value is set for the section flag, use it verbatim
291  if (!flagsStr.getAsInteger(0, flags))
292  return flags;
293 
294  for (char i : flagsStr) {
295  switch (i) {
296  case 'a':
297  flags |= ELF::SHF_ALLOC;
298  break;
299  case 'e':
300  flags |= ELF::SHF_EXCLUDE;
301  break;
302  case 'x':
303  flags |= ELF::SHF_EXECINSTR;
304  break;
305  case 'w':
306  flags |= ELF::SHF_WRITE;
307  break;
308  case 'o':
309  flags |= ELF::SHF_LINK_ORDER;
310  break;
311  case 'M':
312  flags |= ELF::SHF_MERGE;
313  break;
314  case 'S':
315  flags |= ELF::SHF_STRINGS;
316  break;
317  case 'T':
318  flags |= ELF::SHF_TLS;
319  break;
320  case 'c':
321  flags |= ELF::XCORE_SHF_CP_SECTION;
322  break;
323  case 'd':
324  flags |= ELF::XCORE_SHF_DP_SECTION;
325  break;
326  case 'y':
327  flags |= ELF::SHF_ARM_PURECODE;
328  break;
329  case 's':
330  flags |= ELF::SHF_HEX_GPREL;
331  break;
332  case 'G':
333  flags |= ELF::SHF_GROUP;
334  break;
335  case 'R':
336  flags |= ELF::SHF_GNU_RETAIN;
337  break;
338  case '?':
339  *UseLastGroup = true;
340  break;
341  default:
342  return -1U;
343  }
344  }
345 
346  return flags;
347 }
348 
349 unsigned ELFAsmParser::parseSunStyleSectionFlags() {
350  unsigned flags = 0;
351  while (getLexer().is(AsmToken::Hash)) {
352  Lex(); // Eat the #.
353 
354  if (!getLexer().is(AsmToken::Identifier))
355  return -1U;
356 
357  StringRef flagId = getTok().getIdentifier();
358  if (flagId == "alloc")
359  flags |= ELF::SHF_ALLOC;
360  else if (flagId == "execinstr")
361  flags |= ELF::SHF_EXECINSTR;
362  else if (flagId == "write")
363  flags |= ELF::SHF_WRITE;
364  else if (flagId == "tls")
365  flags |= ELF::SHF_TLS;
366  else
367  return -1U;
368 
369  Lex(); // Eat the flag.
370 
371  if (!getLexer().is(AsmToken::Comma))
372  break;
373  Lex(); // Eat the comma.
374  }
375  return flags;
376 }
377 
378 
379 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
380  getStreamer().PushSection();
381 
382  if (ParseSectionArguments(/*IsPush=*/true, loc)) {
383  getStreamer().PopSection();
384  return true;
385  }
386 
387  return false;
388 }
389 
390 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
391  if (!getStreamer().PopSection())
392  return TokError(".popsection without corresponding .pushsection");
393  return false;
394 }
395 
396 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) {
397  return ParseSectionArguments(/*IsPush=*/false, loc);
398 }
399 
400 bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
401  MCAsmLexer &L = getLexer();
402  if (L.isNot(AsmToken::Comma))
403  return false;
404  Lex();
405  if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
406  L.isNot(AsmToken::String)) {
407  if (L.getAllowAtInIdentifier())
408  return TokError("expected '@<type>', '%<type>' or \"<type>\"");
409  else
410  return TokError("expected '%<type>' or \"<type>\"");
411  }
412  if (!L.is(AsmToken::String))
413  Lex();
414  if (L.is(AsmToken::Integer)) {
415  TypeName = getTok().getString();
416  Lex();
417  } else if (getParser().parseIdentifier(TypeName))
418  return TokError("expected identifier in directive");
419  return false;
420 }
421 
422 bool ELFAsmParser::parseMergeSize(int64_t &Size) {
423  if (getLexer().isNot(AsmToken::Comma))
424  return TokError("expected the entry size");
425  Lex();
426  if (getParser().parseAbsoluteExpression(Size))
427  return true;
428  if (Size <= 0)
429  return TokError("entry size must be positive");
430  return false;
431 }
432 
433 bool ELFAsmParser::parseGroup(StringRef &GroupName, bool &IsComdat) {
434  MCAsmLexer &L = getLexer();
435  if (L.isNot(AsmToken::Comma))
436  return TokError("expected group name");
437  Lex();
438  if (L.is(AsmToken::Integer)) {
439  GroupName = getTok().getString();
440  Lex();
441  } else if (getParser().parseIdentifier(GroupName)) {
442  return TokError("invalid group name");
443  }
444  if (L.is(AsmToken::Comma)) {
445  Lex();
447  if (getParser().parseIdentifier(Linkage))
448  return TokError("invalid linkage");
449  if (Linkage != "comdat")
450  return TokError("Linkage must be 'comdat'");
451  IsComdat = true;
452  } else {
453  IsComdat = false;
454  }
455  return false;
456 }
457 
458 bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) {
459  MCAsmLexer &L = getLexer();
460  if (L.isNot(AsmToken::Comma))
461  return TokError("expected linked-to symbol");
462  Lex();
463  StringRef Name;
464  SMLoc StartLoc = L.getLoc();
465  if (getParser().parseIdentifier(Name)) {
466  if (getParser().getTok().getString() == "0") {
467  getParser().Lex();
468  LinkedToSym = nullptr;
469  return false;
470  }
471  return TokError("invalid linked-to symbol");
472  }
473  LinkedToSym = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
474  if (!LinkedToSym || !LinkedToSym->isInSection())
475  return Error(StartLoc, "linked-to symbol is not in a section: " + Name);
476  return false;
477 }
478 
479 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
480  MCAsmLexer &L = getLexer();
481  if (L.isNot(AsmToken::Comma))
482  return false;
483  Lex();
484  StringRef UniqueStr;
485  if (getParser().parseIdentifier(UniqueStr))
486  return TokError("expected identifier in directive");
487  if (UniqueStr != "unique")
488  return TokError("expected 'unique'");
489  if (L.isNot(AsmToken::Comma))
490  return TokError("expected commma");
491  Lex();
492  if (getParser().parseAbsoluteExpression(UniqueID))
493  return true;
494  if (UniqueID < 0)
495  return TokError("unique id must be positive");
496  if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
497  return TokError("unique id is too large");
498  return false;
499 }
500 
502  return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back();
503 }
504 
505 bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
507 
508  if (ParseSectionName(SectionName))
509  return TokError("expected identifier in directive");
510 
512  int64_t Size = 0;
513  StringRef GroupName;
514  bool IsComdat = false;
515  unsigned Flags = 0;
516  unsigned extraFlags = 0;
517  const MCExpr *Subsection = nullptr;
518  bool UseLastGroup = false;
519  MCSymbolELF *LinkedToSym = nullptr;
520  int64_t UniqueID = ~0;
521 
522  // Set the defaults first.
523  if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1")
524  Flags |= ELF::SHF_ALLOC;
525  else if (SectionName == ".fini" || SectionName == ".init" ||
526  hasPrefix(SectionName, ".text."))
528  else if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" ||
529  hasPrefix(SectionName, ".bss.") ||
530  hasPrefix(SectionName, ".init_array.") ||
531  hasPrefix(SectionName, ".fini_array.") ||
532  hasPrefix(SectionName, ".preinit_array."))
533  Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE;
534  else if (hasPrefix(SectionName, ".tdata.") ||
535  hasPrefix(SectionName, ".tbss."))
537 
538  if (getLexer().is(AsmToken::Comma)) {
539  Lex();
540 
541  if (IsPush && getLexer().isNot(AsmToken::String)) {
542  if (getParser().parseExpression(Subsection))
543  return true;
544  if (getLexer().isNot(AsmToken::Comma))
545  goto EndStmt;
546  Lex();
547  }
548 
549  if (getLexer().isNot(AsmToken::String)) {
550  if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax()
551  || getLexer().isNot(AsmToken::Hash))
552  return TokError("expected string in directive");
553  extraFlags = parseSunStyleSectionFlags();
554  } else {
555  StringRef FlagsStr = getTok().getStringContents();
556  Lex();
557  extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
558  }
559 
560  if (extraFlags == -1U)
561  return TokError("unknown flag");
562  Flags |= extraFlags;
563 
564  bool Mergeable = Flags & ELF::SHF_MERGE;
565  bool Group = Flags & ELF::SHF_GROUP;
566  if (Group && UseLastGroup)
567  return TokError("Section cannot specifiy a group name while also acting "
568  "as a member of the last group");
569 
570  if (maybeParseSectionType(TypeName))
571  return true;
572 
573  MCAsmLexer &L = getLexer();
574  if (TypeName.empty()) {
575  if (Mergeable)
576  return TokError("Mergeable section must specify the type");
577  if (Group)
578  return TokError("Group section must specify the type");
580  return TokError("unexpected token in directive");
581  }
582 
583  if (Mergeable)
584  if (parseMergeSize(Size))
585  return true;
586  if (Group)
587  if (parseGroup(GroupName, IsComdat))
588  return true;
589  if (Flags & ELF::SHF_LINK_ORDER)
590  if (parseLinkedToSym(LinkedToSym))
591  return true;
592  if (maybeParseUniqueID(UniqueID))
593  return true;
594  }
595 
596 EndStmt:
597  if (getLexer().isNot(AsmToken::EndOfStatement))
598  return TokError("unexpected token in directive");
599  Lex();
600 
601  unsigned Type = ELF::SHT_PROGBITS;
602 
603  if (TypeName.empty()) {
604  if (SectionName.startswith(".note"))
606  else if (hasPrefix(SectionName, ".init_array."))
608  else if (hasPrefix(SectionName, ".bss."))
610  else if (hasPrefix(SectionName, ".tbss."))
612  else if (hasPrefix(SectionName, ".fini_array."))
614  else if (hasPrefix(SectionName, ".preinit_array."))
616  } else {
617  if (TypeName == "init_array")
619  else if (TypeName == "fini_array")
621  else if (TypeName == "preinit_array")
623  else if (TypeName == "nobits")
625  else if (TypeName == "progbits")
627  else if (TypeName == "note")
629  else if (TypeName == "unwind")
631  else if (TypeName == "llvm_odrtab")
633  else if (TypeName == "llvm_linker_options")
635  else if (TypeName == "llvm_call_graph_profile")
637  else if (TypeName == "llvm_dependent_libraries")
639  else if (TypeName == "llvm_sympart")
641  else if (TypeName == "llvm_bb_addr_map")
643  else if (TypeName.getAsInteger(0, Type))
644  return TokError("unknown section type");
645  }
646 
647  if (UseLastGroup) {
648  MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
649  if (const MCSectionELF *Section =
650  cast_or_null<MCSectionELF>(CurrentSection.first))
651  if (const MCSymbol *Group = Section->getGroup()) {
652  GroupName = Group->getName();
653  IsComdat = Section->isComdat();
654  Flags |= ELF::SHF_GROUP;
655  }
656  }
657 
659  getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
660  IsComdat, UniqueID, LinkedToSym);
661  getStreamer().SwitchSection(Section, Subsection);
662  // x86-64 psABI names SHT_X86_64_UNWIND as the canonical type for .eh_frame,
663  // but GNU as emits SHT_PROGBITS .eh_frame for .cfi_* directives. Don't error
664  // for SHT_PROGBITS .eh_frame
665  if (Section->getType() != Type &&
666  !(SectionName == ".eh_frame" && Type == ELF::SHT_PROGBITS))
667  Error(loc, "changed section type for " + SectionName + ", expected: 0x" +
668  utohexstr(Section->getType()));
669  // Check that flags are used consistently. However, the GNU assembler permits
670  // to leave out in subsequent uses of the same sections; for compatibility,
671  // do likewise.
672  if ((extraFlags || Size || !TypeName.empty()) && Section->getFlags() != Flags)
673  Error(loc, "changed section flags for " + SectionName + ", expected: 0x" +
674  utohexstr(Section->getFlags()));
675  if ((extraFlags || Size || !TypeName.empty()) &&
676  Section->getEntrySize() != Size)
677  Error(loc, "changed section entsize for " + SectionName +
678  ", expected: " + Twine(Section->getEntrySize()));
679 
680  if (getContext().getGenDwarfForAssembly() &&
681  (Section->getFlags() & ELF::SHF_ALLOC) &&
682  (Section->getFlags() & ELF::SHF_EXECINSTR)) {
683  bool InsertResult = getContext().addGenDwarfSection(Section);
684  if (InsertResult) {
685  if (getContext().getDwarfVersion() <= 2)
686  Warning(loc, "DWARF2 only supports one section per compilation unit");
687 
688  if (!Section->getBeginSymbol()) {
689  MCSymbol *SectionStartSymbol = getContext().createTempSymbol();
690  getStreamer().emitLabel(SectionStartSymbol);
691  Section->setBeginSymbol(SectionStartSymbol);
692  }
693  }
694  }
695 
696  return false;
697 }
698 
699 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
700  MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
701  if (PreviousSection.first == nullptr)
702  return TokError(".previous without corresponding .section");
703  getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
704 
705  return false;
706 }
707 
710  .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
711  .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
712  .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
713  .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
714  .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
715  .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
717  .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
719 }
720 
721 /// ParseDirectiveELFType
722 /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
723 /// ::= .type identifier , #attribute
724 /// ::= .type identifier , @attribute
725 /// ::= .type identifier , %attribute
726 /// ::= .type identifier , "attribute"
727 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
728  StringRef Name;
729  if (getParser().parseIdentifier(Name))
730  return TokError("expected identifier in directive");
731 
732  // Handle the identifier as the key symbol.
733  MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
734 
735  // NOTE the comma is optional in all cases. It is only documented as being
736  // optional in the first case, however, GAS will silently treat the comma as
737  // optional in all cases. Furthermore, although the documentation states that
738  // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
739  // accepts both the upper case name as well as the lower case aliases.
740  if (getLexer().is(AsmToken::Comma))
741  Lex();
742 
743  if (getLexer().isNot(AsmToken::Identifier) &&
744  getLexer().isNot(AsmToken::Hash) &&
745  getLexer().isNot(AsmToken::Percent) &&
746  getLexer().isNot(AsmToken::String)) {
747  if (!getLexer().getAllowAtInIdentifier())
748  return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
749  "'%<type>' or \"<type>\"");
750  else if (getLexer().isNot(AsmToken::At))
751  return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
752  "'%<type>' or \"<type>\"");
753  }
754 
755  if (getLexer().isNot(AsmToken::String) &&
756  getLexer().isNot(AsmToken::Identifier))
757  Lex();
758 
759  SMLoc TypeLoc = getLexer().getLoc();
760 
761  StringRef Type;
762  if (getParser().parseIdentifier(Type))
763  return TokError("expected symbol type in directive");
764 
766  if (Attr == MCSA_Invalid)
767  return Error(TypeLoc, "unsupported attribute in '.type' directive");
768 
769  if (getLexer().isNot(AsmToken::EndOfStatement))
770  return TokError("unexpected token in '.type' directive");
771  Lex();
772 
773  getStreamer().emitSymbolAttribute(Sym, Attr);
774 
775  return false;
776 }
777 
778 /// ParseDirectiveIdent
779 /// ::= .ident string
780 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
781  if (getLexer().isNot(AsmToken::String))
782  return TokError("unexpected token in '.ident' directive");
783 
784  StringRef Data = getTok().getIdentifier();
785 
786  Lex();
787 
788  if (getLexer().isNot(AsmToken::EndOfStatement))
789  return TokError("unexpected token in '.ident' directive");
790  Lex();
791 
792  getStreamer().emitIdent(Data);
793  return false;
794 }
795 
796 /// ParseDirectiveSymver
797 /// ::= .symver foo, bar2@zed
798 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
799  StringRef OriginalName, Name, Action;
800  if (getParser().parseIdentifier(OriginalName))
801  return TokError("expected identifier in directive");
802 
803  if (getLexer().isNot(AsmToken::Comma))
804  return TokError("expected a comma");
805 
806  // ARM assembly uses @ for a comment...
807  // except when parsing the second parameter of the .symver directive.
808  // Force the next symbol to allow @ in the identifier, which is
809  // required for this directive and then reset it to its initial state.
810  const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
811  getLexer().setAllowAtInIdentifier(true);
812  Lex();
813  getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
814 
815  if (getParser().parseIdentifier(Name))
816  return TokError("expected identifier in directive");
817 
818  if (Name.find('@') == StringRef::npos)
819  return TokError("expected a '@' in the name");
820  bool KeepOriginalSym = !Name.contains("@@@");
821  if (parseOptionalToken(AsmToken::Comma)) {
822  if (getParser().parseIdentifier(Action) || Action != "remove")
823  return TokError("expected 'remove'");
824  KeepOriginalSym = false;
825  }
826  (void)parseOptionalToken(AsmToken::EndOfStatement);
827 
828  getStreamer().emitELFSymverDirective(
829  getContext().getOrCreateSymbol(OriginalName), Name, KeepOriginalSym);
830  return false;
831 }
832 
833 /// ParseDirectiveVersion
834 /// ::= .version string
835 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
836  if (getLexer().isNot(AsmToken::String))
837  return TokError("unexpected token in '.version' directive");
838 
839  StringRef Data = getTok().getIdentifier();
840 
841  Lex();
842 
843  MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
844 
845  getStreamer().PushSection();
846  getStreamer().SwitchSection(Note);
847  getStreamer().emitInt32(Data.size() + 1); // namesz
848  getStreamer().emitInt32(0); // descsz = 0 (no description).
849  getStreamer().emitInt32(1); // type = NT_VERSION
850  getStreamer().emitBytes(Data); // name
851  getStreamer().emitInt8(0); // NUL
852  getStreamer().emitValueToAlignment(4);
853  getStreamer().PopSection();
854  return false;
855 }
856 
857 /// ParseDirectiveWeakref
858 /// ::= .weakref foo, bar
859 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
860  // FIXME: Share code with the other alias building directives.
861 
862  StringRef AliasName;
863  if (getParser().parseIdentifier(AliasName))
864  return TokError("expected identifier in directive");
865 
866  if (getLexer().isNot(AsmToken::Comma))
867  return TokError("expected a comma");
868 
869  Lex();
870 
871  StringRef Name;
872  if (getParser().parseIdentifier(Name))
873  return TokError("expected identifier in directive");
874 
875  MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
876 
877  MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
878 
879  getStreamer().emitWeakReference(Alias, Sym);
880  return false;
881 }
882 
883 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
884  const MCExpr *Subsection = nullptr;
885  if (getLexer().isNot(AsmToken::EndOfStatement)) {
886  if (getParser().parseExpression(Subsection))
887  return true;
888  }
889 
890  if (getLexer().isNot(AsmToken::EndOfStatement))
891  return TokError("unexpected token in directive");
892 
893  Lex();
894 
895  getStreamer().SubSection(Subsection);
896  return false;
897 }
898 
899 bool ELFAsmParser::ParseDirectiveCGProfile(StringRef S, SMLoc Loc) {
901 }
902 
903 namespace llvm {
904 
906  return new ELFAsmParser;
907 }
908 
909 } // end namespace llvm
llvm::ELF::SHF_GNU_RETAIN
@ SHF_GNU_RETAIN
Definition: ELF.h:1013
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
i
i
Definition: README.txt:29
llvm::MCAsmParser
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:123
is
should just be implemented with a CLZ instruction Since there are other e that share this it would be best to implement this in a target independent as zero is the default value for the binary encoder e add r0 add r5 Register operands should be distinct That is
Definition: README.txt:725
llvm::SectionKind::getReadOnlyWithRel
static SectionKind getReadOnlyWithRel()
Definition: SectionKind.h:207
MathExtras.h
llvm
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::MCAsmLexer
Generic assembler lexer interface, for use by target specific assembly lexers.
Definition: MCAsmLexer.h:39
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:164
llvm::MCAsmParserExtension::ParseDirectiveCGProfile
bool ParseDirectiveCGProfile(StringRef, SMLoc)
ParseDirectiveCGProfile ::= .cg_profile identifier, identifier, <number>
Definition: MCAsmParserExtension.cpp:25
Note
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles Note
Definition: README.txt:239
StringRef.h
llvm::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
llvm::MCSA_ELF_TypeObject
@ MCSA_ELF_TypeObject
.type _foo, STT_OBJECT # aka @object
Definition: MCDirectives.h:25
llvm::AsmToken::EndOfStatement
@ EndOfStatement
Definition: MCAsmMacro.h:42
MCDirectives.h
llvm::MCSA_Invalid
@ MCSA_Invalid
Not a valid directive.
Definition: MCDirectives.h:19
llvm::ELF::SHF_HEX_GPREL
@ SHF_HEX_GPREL
Definition: ELF.h:1046
llvm::StringRef::npos
static constexpr size_t npos
Definition: StringRef.h:60
MCSectionELF.h
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
llvm::AsmToken::Integer
@ Integer
Definition: MCAsmMacro.h:32
llvm::ELF::SHT_PREINIT_ARRAY
@ SHT_PREINIT_ARRAY
Definition: ELF.h:917
llvm::MCSA_ELF_TypeNoType
@ MCSA_ELF_TypeNoType
.type _foo, STT_NOTYPE # aka @notype
Definition: MCDirectives.h:28
llvm::MCSectionELF
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:28
llvm::MCAsmLexer::is
bool is(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition: MCAsmLexer.h:143
llvm::ELF::SHF_WRITE
@ SHF_WRITE
Definition: ELF.h:979
MCAsmParser.h
llvm::ELF::SHT_NOBITS
@ SHT_NOBITS
Definition: ELF.h:911
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::HighlightColor::Warning
@ Warning
llvm::MCSymbolAttr
MCSymbolAttr
Definition: MCDirectives.h:18
llvm::MCSA_Local
@ MCSA_Local
.local (ELF)
Definition: MCDirectives.h:37
llvm::ELF::SHT_X86_64_UNWIND
@ SHT_X86_64_UNWIND
Definition: ELF.h:960
llvm::MCSymbolELF
Definition: MCSymbolELF.h:14
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:78
llvm::SectionKind::getReadOnly
static SectionKind getReadOnly()
Definition: SectionKind.h:185
llvm::ELF::SHF_EXCLUDE
@ SHF_EXCLUDE
Definition: ELF.h:1016
llvm::ELF::SHF_LINK_ORDER
@ SHF_LINK_ORDER
Definition: ELF.h:997
llvm::ELF::SHT_LLVM_SYMPART
@ SHT_LLVM_SYMPART
Definition: ELF.h:935
llvm::SectionKind::getThreadBSS
static SectionKind getThreadBSS()
Definition: SectionKind.h:199
ELF.h
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
MCSymbolELF.h
llvm::MCSA_Protected
@ MCSA_Protected
.protected (ELF)
Definition: MCDirectives.h:42
MCContext.h
MCSymbol.h
llvm::SectionKind::getBSS
static SectionKind getBSS()
Definition: SectionKind.h:202
llvm::MCAsmParser::ExtensionDirectiveHandler
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
Definition: MCAsmParser.h:127
llvm::MCSA_ELF_TypeIndFunction
@ MCSA_ELF_TypeIndFunction
.type _foo, STT_GNU_IFUNC
Definition: MCDirectives.h:24
SMLoc.h
llvm::AMDGPU::ElfNote::SectionName
const char SectionName[]
Definition: AMDGPUPTNote.h:24
llvm::MCSA_Hidden
@ MCSA_Hidden
.hidden (ELF)
Definition: MCDirectives.h:33
llvm::MCSA_ELF_TypeGnuUniqueObject
@ MCSA_ELF_TypeGnuUniqueObject
Definition: MCDirectives.h:29
llvm::SectionKind::getText
static SectionKind getText()
Definition: SectionKind.h:183
isNot
static bool isNot(const MachineRegisterInfo &MRI, const MachineInstr &MI)
Definition: AMDGPULegalizerInfo.cpp:2694
llvm::AsmToken::Percent
@ Percent
Definition: MCAsmMacro.h:52
llvm::ELF::SHT_LLVM_LINKER_OPTIONS
@ SHT_LLVM_LINKER_OPTIONS
Definition: ELF.h:929
llvm::StringRef::getAsInteger
std::enable_if_t< std::numeric_limits< T >::is_signed, bool > getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:508
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
parseSectionFlags
static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup)
Definition: ELFAsmParser.cpp:287
llvm::createELFAsmParser
MCAsmParserExtension * createELFAsmParser()
Definition: ELFAsmParser.cpp:905
llvm::ELF::SHT_NOTE
@ SHT_NOTE
Definition: ELF.h:910
llvm::ELF::SHT_PROGBITS
@ SHT_PROGBITS
Definition: ELF.h:904
MCAsmLexer.h
llvm::MCAsmLexer::getLoc
SMLoc getLoc() const
Get the current source location.
Definition: MCAsmLexer.cpp:23
llvm::AsmToken::At
@ At
Definition: MCAsmMacro.h:54
s
multiplies can be turned into SHL s
Definition: README.txt:370
llvm::MCAsmParserExtension::Initialize
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
Definition: MCAsmParserExtension.cpp:19
llvm::ELF::SHF_ALLOC
@ SHF_ALLOC
Definition: ELF.h:982
llvm::ELF::SHT_FINI_ARRAY
@ SHT_FINI_ARRAY
Definition: ELF.h:916
llvm::isUInt< 32 >
constexpr bool isUInt< 32 >(uint64_t x)
Definition: MathExtras.h:411
llvm::mc::getDwarfVersion
int getDwarfVersion()
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::SectionKind::getThreadData
static SectionKind getThreadData()
Definition: SectionKind.h:200
MCSection.h
llvm::AsmToken::Comma
@ Comma
Definition: MCAsmMacro.h:49
MCAsmInfo.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::ELF::SHT_LLVM_ODRTAB
@ SHT_LLVM_ODRTAB
Definition: ELF.h:928
llvm::MCAsmParserExtension
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
Definition: MCAsmParserExtension.h:25
hasPrefix
static bool hasPrefix(StringRef SectionName, StringRef Prefix)
Definition: ELFAsmParser.cpp:501
llvm::ELF::XCORE_SHF_DP_SECTION
@ XCORE_SHF_DP_SECTION
All sections with the "d" flag are grouped together by the linker to form the data section and the dp...
Definition: ELF.h:1028
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::MCSA_ELF_TypeTLS
@ MCSA_ELF_TypeTLS
.type _foo, STT_TLS # aka @tls_object
Definition: MCDirectives.h:26
llvm::StringSwitch::Cases
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:88
llvm::MCSA_Internal
@ MCSA_Internal
.internal (ELF)
Definition: MCDirectives.h:35
llvm::ELF::SHF_GROUP
@ SHF_GROUP
Definition: ELF.h:1004
llvm::MCSA_ELF_TypeCommon
@ MCSA_ELF_TypeCommon
.type _foo, STT_COMMON # aka @common
Definition: MCDirectives.h:27
llvm::SectionKind::getData
static SectionKind getData()
Definition: SectionKind.h:206
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
MCAttrForString
static MCSymbolAttr MCAttrForString(StringRef Type)
Definition: ELFAsmParser.cpp:708
llvm::SectionKind
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::AsmToken::Identifier
@ Identifier
Definition: MCAsmMacro.h:28
llvm::ELF::SHT_LLVM_DEPENDENT_LIBRARIES
@ SHT_LLVM_DEPENDENT_LIBRARIES
Definition: ELF.h:933
MCAsmParserExtension.h
llvm::AMDGPU::HSAMD::Kernel::Arg::Key::TypeName
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
Definition: AMDGPUMetadata.h:170
llvm::SMLoc::getPointer
const char * getPointer() const
Definition: SMLoc.h:34
llvm::SectionName
Definition: DWARFSection.h:21
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::MCAsmLexer::isNot
bool isNot(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition: MCAsmLexer.h:146
llvm::ELF::SHF_ARM_PURECODE
@ SHF_ARM_PURECODE
Definition: ELF.h:1074
Casting.h
llvm::ELF::SHF_EXECINSTR
@ SHF_EXECINSTR
Definition: ELF.h:985
StringSwitch.h
SectionKind.h
llvm::ELF::SHT_LLVM_BB_ADDR_MAP
@ SHT_LLVM_BB_ADDR_MAP
Definition: ELF.h:938
llvm::MCSA_ELF_TypeFunction
@ MCSA_ELF_TypeFunction
.type _foo, STT_FUNC # aka @function
Definition: MCDirectives.h:23
llvm::MCSA_Weak
@ MCSA_Weak
.weak
Definition: MCDirectives.h:44
llvm::ELF::SHF_MERGE
@ SHF_MERGE
Definition: ELF.h:988
llvm::ELF::SHF_TLS
@ SHF_TLS
Definition: ELF.h:1007
MCStreamer.h
llvm::MCSymbol::isInSection
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
Definition: MCSymbol.h:246
llvm::MCSectionSubPair
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
Definition: MCStreamer.h:64
llvm::ELF::XCORE_SHF_CP_SECTION
@ XCORE_SHF_CP_SECTION
All sections with the "c" flag are grouped together by the linker to form the constant pool and the c...
Definition: ELF.h:1033
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
llvm::AsmToken::String
@ String
Definition: MCAsmMacro.h:29
llvm::AsmToken::Hash
@ Hash
Definition: MCAsmMacro.h:52
MCExpr.h
llvm::Directive
Definition: DirectiveEmitter.h:100
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::ELF::SHT_INIT_ARRAY
@ SHT_INIT_ARRAY
Definition: ELF.h:915
llvm::MCAsmLexer::getAllowAtInIdentifier
bool getAllowAtInIdentifier()
Definition: MCAsmLexer.h:151
llvm::ELF::SHF_STRINGS
@ SHF_STRINGS
Definition: ELF.h:991
llvm::ELF::SHT_LLVM_CALL_GRAPH_PROFILE
@ SHT_LLVM_CALL_GRAPH_PROFILE
Definition: ELF.h:930