LLVM 22.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
11#include "llvm/ADT/StringRef.h"
14#include "llvm/MC/MCAsmInfo.h"
15#include "llvm/MC/MCContext.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/SMLoc.h"
26#include <cassert>
27#include <cstdint>
28#include <utility>
29
30using namespace llvm;
31
32namespace {
33
34class 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
41 }
42
43 bool parseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
44 SectionKind Kind);
45
46public:
47 ELFAsmParser() { BracketExpressionsSupported = true; }
48
49 void Initialize(MCAsmParser &Parser) override {
50 // Call the base implementation.
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<&ELFAsmParser::parseDirectiveSection>(".section");
60 addDirectiveHandler<
61 &ELFAsmParser::parseDirectivePushSection>(".pushsection");
62 addDirectiveHandler<&ELFAsmParser::parseDirectivePopSection>(".popsection");
63 addDirectiveHandler<&ELFAsmParser::parseDirectiveSize>(".size");
64 addDirectiveHandler<&ELFAsmParser::parseDirectivePrevious>(".previous");
65 addDirectiveHandler<&ELFAsmParser::parseDirectiveType>(".type");
66 addDirectiveHandler<&ELFAsmParser::parseDirectiveIdent>(".ident");
67 addDirectiveHandler<&ELFAsmParser::parseDirectiveSymver>(".symver");
68 addDirectiveHandler<&ELFAsmParser::parseDirectiveVersion>(".version");
69 addDirectiveHandler<&ELFAsmParser::parseDirectiveWeakref>(".weakref");
70 addDirectiveHandler<&ELFAsmParser::parseDirectiveSymbolAttribute>(".weak");
71 addDirectiveHandler<&ELFAsmParser::parseDirectiveSymbolAttribute>(".local");
72 addDirectiveHandler<
73 &ELFAsmParser::parseDirectiveSymbolAttribute>(".protected");
74 addDirectiveHandler<
75 &ELFAsmParser::parseDirectiveSymbolAttribute>(".internal");
76 addDirectiveHandler<
77 &ELFAsmParser::parseDirectiveSymbolAttribute>(".hidden");
78 addDirectiveHandler<&ELFAsmParser::parseDirectiveSubsection>(".subsection");
79 addDirectiveHandler<&ELFAsmParser::parseDirectiveCGProfile>(".cg_profile");
80 }
81
82 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
83 // the best way for us to get access to it?
84 bool parseSectionDirectiveData(StringRef, SMLoc) {
85 return parseSectionSwitch(".data", ELF::SHT_PROGBITS,
88 }
89 bool parseSectionDirectiveText(StringRef, SMLoc) {
90 return parseSectionSwitch(".text", ELF::SHT_PROGBITS,
93 }
94 bool parseSectionDirectiveBSS(StringRef, SMLoc) {
95 return parseSectionSwitch(".bss", ELF::SHT_NOBITS,
98 }
99 bool parseSectionDirectiveRoData(StringRef, SMLoc) {
100 return parseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
103 }
104 bool parseSectionDirectiveTData(StringRef, SMLoc) {
105 return parseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
109 }
110 bool parseSectionDirectiveTBSS(StringRef, SMLoc) {
111 return parseSectionSwitch(".tbss", ELF::SHT_NOBITS,
115 }
116 bool parseDirectivePushSection(StringRef, SMLoc);
117 bool parseDirectivePopSection(StringRef, SMLoc);
118 bool parseDirectiveSection(StringRef, SMLoc);
119 bool parseDirectiveSize(StringRef, SMLoc);
120 bool parseDirectivePrevious(StringRef, SMLoc);
121 bool parseDirectiveType(StringRef, SMLoc);
122 bool parseDirectiveIdent(StringRef, SMLoc);
123 bool parseDirectiveSymver(StringRef, SMLoc);
124 bool parseDirectiveVersion(StringRef, SMLoc);
125 bool parseDirectiveWeakref(StringRef, SMLoc);
126 bool parseDirectiveSymbolAttribute(StringRef, SMLoc);
127 bool parseDirectiveSubsection(StringRef, SMLoc);
129
130private:
131 bool parseSectionName(StringRef &SectionName);
132 bool parseSectionArguments(bool IsPush, SMLoc loc);
133 unsigned parseSunStyleSectionFlags();
134 bool maybeParseSectionType(StringRef &TypeName);
135 bool parseMergeSize(int64_t &Size);
136 bool parseGroup(StringRef &GroupName, bool &IsComdat);
137 bool parseLinkedToSym(MCSymbolELF *&LinkedToSym);
138};
139
140} // end anonymous namespace
141
142/// parseDirectiveSymbolAttribute
143/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
144bool ELFAsmParser::parseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
146 .Case(".weak", MCSA_Weak)
147 .Case(".local", MCSA_Local)
148 .Case(".hidden", MCSA_Hidden)
149 .Case(".internal", MCSA_Internal)
150 .Case(".protected", MCSA_Protected)
152 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
153 if (getLexer().isNot(AsmToken::EndOfStatement)) {
154 while (true) {
156
157 if (getParser().parseIdentifier(Name))
158 return TokError("expected identifier");
159
160 if (getParser().discardLTOSymbol(Name)) {
161 if (getLexer().is(AsmToken::EndOfStatement))
162 break;
163 continue;
164 }
165
166 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
167
168 getStreamer().emitSymbolAttribute(Sym, Attr);
169
170 if (getLexer().is(AsmToken::EndOfStatement))
171 break;
172
173 if (getLexer().isNot(AsmToken::Comma))
174 return TokError("expected comma");
175 Lex();
176 }
177 }
178
179 Lex();
180 return false;
181}
182
183bool ELFAsmParser::parseSectionSwitch(StringRef Section, unsigned Type,
184 unsigned Flags, SectionKind Kind) {
185 const MCExpr *Subsection = nullptr;
186 if (getLexer().isNot(AsmToken::EndOfStatement)) {
187 if (getParser().parseExpression(Subsection))
188 return true;
189 }
190 Lex();
191
192 getStreamer().switchSection(getContext().getELFSection(Section, Type, Flags),
193 Subsection);
194
195 return false;
196}
197
198bool ELFAsmParser::parseDirectiveSize(StringRef, SMLoc) {
200 if (getParser().parseIdentifier(Name))
201 return TokError("expected identifier");
202 auto *Sym = static_cast<MCSymbolELF *>(getContext().getOrCreateSymbol(Name));
203
204 if (getLexer().isNot(AsmToken::Comma))
205 return TokError("expected comma");
206 Lex();
207
208 const MCExpr *Expr;
209 if (getParser().parseExpression(Expr))
210 return true;
211
212 if (getLexer().isNot(AsmToken::EndOfStatement))
213 return TokError("unexpected token");
214 Lex();
215
216 getStreamer().emitELFSize(Sym, Expr);
217 return false;
218}
219
220bool ELFAsmParser::parseSectionName(StringRef &SectionName) {
221 // A section name can contain -, so we cannot just use
222 // parseIdentifier.
223 SMLoc FirstLoc = getLexer().getLoc();
224 unsigned Size = 0;
225
226 if (getLexer().is(AsmToken::String)) {
227 SectionName = getTok().getIdentifier();
228 Lex();
229 return false;
230 }
231
232 while (!getParser().hasPendingError()) {
233 SMLoc PrevLoc = getLexer().getLoc();
234 if (getLexer().is(AsmToken::Comma) ||
235 getLexer().is(AsmToken::EndOfStatement))
236 break;
237
238 unsigned CurSize;
239 if (getLexer().is(AsmToken::String)) {
240 CurSize = getTok().getIdentifier().size() + 2;
241 Lex();
242 } else if (getLexer().is(AsmToken::Identifier)) {
243 CurSize = getTok().getIdentifier().size();
244 Lex();
245 } else {
246 CurSize = getTok().getString().size();
247 Lex();
248 }
249 Size += CurSize;
250 SectionName = StringRef(FirstLoc.getPointer(), Size);
251
252 // Make sure the following token is adjacent.
253 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
254 break;
255 }
256 if (Size == 0)
257 return true;
258
259 return false;
260}
261
262static unsigned parseSectionFlags(const Triple &TT, StringRef flagsStr,
263 bool *UseLastGroup) {
264 unsigned flags = 0;
265
266 // If a valid numerical value is set for the section flag, use it verbatim
267 if (!flagsStr.getAsInteger(0, flags))
268 return flags;
269
270 for (char i : flagsStr) {
271 switch (i) {
272 case 'a':
273 flags |= ELF::SHF_ALLOC;
274 break;
275 case 'e':
276 flags |= ELF::SHF_EXCLUDE;
277 break;
278 case 'x':
279 flags |= ELF::SHF_EXECINSTR;
280 break;
281 case 'w':
282 flags |= ELF::SHF_WRITE;
283 break;
284 case 'o':
285 flags |= ELF::SHF_LINK_ORDER;
286 break;
287 case 'M':
288 flags |= ELF::SHF_MERGE;
289 break;
290 case 'S':
291 flags |= ELF::SHF_STRINGS;
292 break;
293 case 'T':
294 flags |= ELF::SHF_TLS;
295 break;
296 case 'c':
297 if (TT.getArch() != Triple::xcore)
298 return -1U;
300 break;
301 case 'd':
302 if (TT.getArch() != Triple::xcore)
303 return -1U;
305 break;
306 case 'y':
307 if (TT.isARM() || TT.isThumb())
308 flags |= ELF::SHF_ARM_PURECODE;
309 else if (TT.isAArch64())
311 else
312 return -1U;
313 break;
314 case 's':
315 if (TT.getArch() != Triple::hexagon)
316 return -1U;
317 flags |= ELF::SHF_HEX_GPREL;
318 break;
319 case 'G':
320 flags |= ELF::SHF_GROUP;
321 break;
322 case 'l':
323 if (TT.getArch() != Triple::x86_64)
324 return -1U;
325 flags |= ELF::SHF_X86_64_LARGE;
326 break;
327 case 'R':
328 if (TT.isOSSolaris())
330 else
331 flags |= ELF::SHF_GNU_RETAIN;
332 break;
333 case '?':
334 *UseLastGroup = true;
335 break;
336 default:
337 return -1U;
338 }
339 }
340
341 return flags;
342}
343
344unsigned ELFAsmParser::parseSunStyleSectionFlags() {
345 unsigned flags = 0;
346 while (getLexer().is(AsmToken::Hash)) {
347 Lex(); // Eat the #.
348
349 if (!getLexer().is(AsmToken::Identifier))
350 return -1U;
351
352 StringRef flagId = getTok().getIdentifier();
353 if (flagId == "alloc")
354 flags |= ELF::SHF_ALLOC;
355 else if (flagId == "execinstr")
356 flags |= ELF::SHF_EXECINSTR;
357 else if (flagId == "write")
358 flags |= ELF::SHF_WRITE;
359 else if (flagId == "tls")
360 flags |= ELF::SHF_TLS;
361 else
362 return -1U;
363
364 Lex(); // Eat the flag.
365
366 if (!getLexer().is(AsmToken::Comma))
367 break;
368 Lex(); // Eat the comma.
369 }
370 return flags;
371}
372
373
374bool ELFAsmParser::parseDirectivePushSection(StringRef s, SMLoc loc) {
375 getStreamer().pushSection();
376
377 if (parseSectionArguments(/*IsPush=*/true, loc)) {
378 getStreamer().popSection();
379 return true;
380 }
381
382 return false;
383}
384
385bool ELFAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
386 if (!getStreamer().popSection())
387 return TokError(".popsection without corresponding .pushsection");
388 return false;
389}
390
391bool ELFAsmParser::parseDirectiveSection(StringRef, SMLoc loc) {
392 return parseSectionArguments(/*IsPush=*/false, loc);
393}
394
395bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
396 AsmLexer &L = getLexer();
397 if (L.isNot(AsmToken::Comma))
398 return false;
399 Lex();
400 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
401 L.isNot(AsmToken::String)) {
402 if (getContext().getAsmInfo()->getCommentString().starts_with('@'))
403 return TokError("expected '%<type>' or \"<type>\"");
404 else
405 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
406 }
407 if (!L.is(AsmToken::String))
408 Lex();
409 if (L.is(AsmToken::Integer)) {
410 TypeName = getTok().getString();
411 Lex();
412 } else if (getParser().parseIdentifier(TypeName))
413 return TokError("expected identifier");
414 return false;
415}
416
417bool ELFAsmParser::parseMergeSize(int64_t &Size) {
418 if (getLexer().isNot(AsmToken::Comma))
419 return TokError("expected the entry size");
420 Lex();
421 if (getParser().parseAbsoluteExpression(Size))
422 return true;
423 if (Size <= 0)
424 return TokError("entry size must be positive");
425 return false;
426}
427
428bool ELFAsmParser::parseGroup(StringRef &GroupName, bool &IsComdat) {
429 AsmLexer &L = getLexer();
430 if (L.isNot(AsmToken::Comma))
431 return TokError("expected group name");
432 Lex();
433 if (L.is(AsmToken::Integer)) {
434 GroupName = getTok().getString();
435 Lex();
436 } else if (getParser().parseIdentifier(GroupName)) {
437 return TokError("invalid group name");
438 }
439 if (L.is(AsmToken::Comma)) {
440 Lex();
442 if (getParser().parseIdentifier(Linkage))
443 return TokError("invalid linkage");
444 if (Linkage != "comdat")
445 return TokError("Linkage must be 'comdat'");
446 IsComdat = true;
447 } else {
448 IsComdat = false;
449 }
450 return false;
451}
452
453bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) {
454 AsmLexer &L = getLexer();
455 if (L.isNot(AsmToken::Comma))
456 return TokError("expected linked-to symbol");
457 Lex();
459 SMLoc StartLoc = L.getLoc();
460 if (getParser().parseIdentifier(Name)) {
461 if (getParser().getTok().getString() == "0") {
462 getParser().Lex();
463 LinkedToSym = nullptr;
464 return false;
465 }
466 return TokError("invalid linked-to symbol");
467 }
468 LinkedToSym = static_cast<MCSymbolELF *>(getContext().lookupSymbol(Name));
469 if (!LinkedToSym || !LinkedToSym->isInSection())
470 return Error(StartLoc, "linked-to symbol is not in a section: " + Name);
471 return false;
472}
473
475 return SectionName.consume_front(Prefix) &&
476 (SectionName.empty() || SectionName[0] == '.');
477}
478
480 unsigned Type) {
481 if (TT.getArch() == Triple::x86_64) {
482 // x86-64 psABI names SHT_X86_64_UNWIND as the canonical type for .eh_frame,
483 // but GNU as emits SHT_PROGBITS .eh_frame for .cfi_* directives. Don't
484 // error for SHT_PROGBITS .eh_frame
485 return SectionName == ".eh_frame" && Type == ELF::SHT_PROGBITS;
486 }
487 if (TT.isMIPS()) {
488 // MIPS .debug_* sections should have SHT_MIPS_DWARF section type to
489 // distinguish among sections contain DWARF and ECOFF debug formats,
490 // but in assembly files these sections have SHT_PROGBITS type.
491 return SectionName.starts_with(".debug_") && Type == ELF::SHT_PROGBITS;
492 }
493 return false;
494}
495
496bool ELFAsmParser::parseSectionArguments(bool IsPush, SMLoc loc) {
498
499 if (parseSectionName(SectionName))
500 return TokError("expected identifier");
501
503 int64_t Size = 0;
504 StringRef GroupName;
505 bool IsComdat = false;
506 unsigned Flags = 0;
507 unsigned extraFlags = 0;
508 const MCExpr *Subsection = nullptr;
509 bool UseLastGroup = false;
510 MCSymbolELF *LinkedToSym = nullptr;
511 int64_t UniqueID = ~0;
512
513 // Set the defaults first.
514 if (hasPrefix(SectionName, ".rodata") || SectionName == ".rodata1")
516 else if (SectionName == ".fini" || SectionName == ".init" ||
517 hasPrefix(SectionName, ".text"))
519 else if (hasPrefix(SectionName, ".data") || SectionName == ".data1" ||
520 hasPrefix(SectionName, ".bss") ||
521 hasPrefix(SectionName, ".init_array") ||
522 hasPrefix(SectionName, ".fini_array") ||
523 hasPrefix(SectionName, ".preinit_array"))
525 else if (hasPrefix(SectionName, ".tdata") || hasPrefix(SectionName, ".tbss"))
527
528 if (getLexer().is(AsmToken::Comma)) {
529 Lex();
530
531 if (IsPush && getLexer().isNot(AsmToken::String)) {
532 if (getParser().parseExpression(Subsection))
533 return true;
534 if (getLexer().isNot(AsmToken::Comma))
535 goto EndStmt;
536 Lex();
537 }
538
539 if (getLexer().isNot(AsmToken::String)) {
540 if (getLexer().isNot(AsmToken::Hash))
541 return TokError("expected string");
542 extraFlags = parseSunStyleSectionFlags();
543 } else {
544 StringRef FlagsStr = getTok().getStringContents();
545 Lex();
546 extraFlags = parseSectionFlags(getContext().getTargetTriple(), FlagsStr,
547 &UseLastGroup);
548 }
549
550 if (extraFlags == -1U)
551 return TokError("unknown flag");
552 Flags |= extraFlags;
553
554 bool Mergeable = Flags & ELF::SHF_MERGE;
555 bool Group = Flags & ELF::SHF_GROUP;
556 if (Group && UseLastGroup)
557 return TokError("Section cannot specifiy a group name while also acting "
558 "as a member of the last group");
559
560 if (maybeParseSectionType(TypeName))
561 return true;
562
563 AsmLexer &L = getLexer();
564 if (TypeName.empty()) {
565 if (Mergeable)
566 return TokError("Mergeable section must specify the type");
567 if (Group)
568 return TokError("Group section must specify the type");
569 if (L.isNot(AsmToken::EndOfStatement))
570 return TokError("expected end of directive");
571 }
572
573 if (Mergeable || TypeName == "llvm_cfi_jump_table")
574 if (parseMergeSize(Size))
575 return true;
576 if (Flags & ELF::SHF_LINK_ORDER)
577 if (parseLinkedToSym(LinkedToSym))
578 return true;
579 if (Group)
580 if (parseGroup(GroupName, IsComdat))
581 return true;
582 if (maybeParseUniqueID(UniqueID))
583 return true;
584 }
585
586EndStmt:
587 if (getLexer().isNot(AsmToken::EndOfStatement))
588 return TokError("expected end of directive");
589 Lex();
590
591 unsigned Type = ELF::SHT_PROGBITS;
592
593 if (TypeName.empty()) {
594 if (SectionName.starts_with(".note"))
596 else if (hasPrefix(SectionName, ".init_array"))
598 else if (hasPrefix(SectionName, ".bss"))
600 else if (hasPrefix(SectionName, ".tbss"))
602 else if (hasPrefix(SectionName, ".fini_array"))
604 else if (hasPrefix(SectionName, ".preinit_array"))
606 } else {
607 if (TypeName == "init_array")
609 else if (TypeName == "fini_array")
611 else if (TypeName == "preinit_array")
613 else if (TypeName == "nobits")
615 else if (TypeName == "progbits")
617 else if (TypeName == "note")
619 else if (TypeName == "unwind")
621 else if (TypeName == "llvm_odrtab")
623 else if (TypeName == "llvm_linker_options")
625 else if (TypeName == "llvm_call_graph_profile")
627 else if (TypeName == "llvm_dependent_libraries")
629 else if (TypeName == "llvm_sympart")
631 else if (TypeName == "llvm_bb_addr_map")
633 else if (TypeName == "llvm_offloading")
635 else if (TypeName == "llvm_lto")
637 else if (TypeName == "llvm_jt_sizes")
639 else if (TypeName == "llvm_cfi_jump_table")
641 else if (TypeName.getAsInteger(0, Type))
642 return TokError("unknown section type");
643 }
644
645 if (UseLastGroup) {
646 if (auto *Section = static_cast<const MCSectionELF *>(
647 getStreamer().getCurrentSectionOnly()))
648 if (const MCSymbol *Group = Section->getGroup()) {
649 GroupName = Group->getName();
650 IsComdat = Section->isComdat();
652 }
653 }
654
656 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
657 IsComdat, UniqueID, LinkedToSym);
658 getStreamer().switchSection(Section, Subsection);
659 // Check that flags are used consistently. However, the GNU assembler permits
660 // to leave out in subsequent uses of the same sections; for compatibility,
661 // do likewise.
662 if (!TypeName.empty() && Section->getType() != Type &&
663 !allowSectionTypeMismatch(getContext().getTargetTriple(), SectionName,
664 Type))
665 Error(loc, "changed section type for " + SectionName + ", expected: 0x" +
666 utohexstr(Section->getType()));
667 if ((extraFlags || Size || !TypeName.empty()) && Section->getFlags() != Flags)
668 Error(loc, "changed section flags for " + SectionName + ", expected: 0x" +
669 utohexstr(Section->getFlags()));
670 if ((extraFlags || Size || !TypeName.empty()) &&
671 Section->getEntrySize() != Size)
672 Error(loc, "changed section entsize for " + SectionName +
673 ", expected: " + Twine(Section->getEntrySize()));
674
675 if (getContext().getGenDwarfForAssembly() &&
676 (Section->getFlags() & ELF::SHF_ALLOC) &&
677 (Section->getFlags() & ELF::SHF_EXECINSTR)) {
678 bool InsertResult = getContext().addGenDwarfSection(Section);
679 if (InsertResult && getContext().getDwarfVersion() <= 2)
680 Warning(loc, "DWARF2 only supports one section per compilation unit");
681 }
682
683 return false;
684}
685
686bool ELFAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
687 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
688 if (PreviousSection.first == nullptr)
689 return TokError(".previous without corresponding .section");
690 getStreamer().switchSection(PreviousSection.first, PreviousSection.second);
691
692 return false;
693}
694
697 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
698 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
699 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
700 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
701 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
702 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
704 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
706}
707
708/// parseDirectiveELFType
709/// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
710/// ::= .type identifier , #attribute
711/// ::= .type identifier , @attribute
712/// ::= .type identifier , %attribute
713/// ::= .type identifier , "attribute"
714bool ELFAsmParser::parseDirectiveType(StringRef, SMLoc) {
716 if (getParser().parseIdentifier(Name))
717 return TokError("expected identifier");
718
719 // Handle the identifier as the key symbol.
720 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
721
722 bool AllowAt = getLexer().getAllowAtInIdentifier();
723 if (!AllowAt &&
724 !getContext().getAsmInfo()->getCommentString().starts_with("@"))
725 getLexer().setAllowAtInIdentifier(true);
726 auto _ =
727 make_scope_exit([&]() { getLexer().setAllowAtInIdentifier(AllowAt); });
728
729 // NOTE the comma is optional in all cases. It is only documented as being
730 // optional in the first case, however, GAS will silently treat the comma as
731 // optional in all cases. Furthermore, although the documentation states that
732 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
733 // accepts both the upper case name as well as the lower case aliases.
734 if (getLexer().is(AsmToken::Comma))
735 Lex();
736
737 if (getLexer().isNot(AsmToken::Identifier) &&
738 getLexer().isNot(AsmToken::Hash) &&
739 getLexer().isNot(AsmToken::Percent) &&
740 getLexer().isNot(AsmToken::String)) {
741 if (!getLexer().getAllowAtInIdentifier())
742 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
743 "'%<type>' or \"<type>\"");
744 else if (getLexer().isNot(AsmToken::At))
745 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
746 "'%<type>' or \"<type>\"");
747 }
748
749 if (getLexer().isNot(AsmToken::String) &&
750 getLexer().isNot(AsmToken::Identifier))
751 Lex();
752
753 SMLoc TypeLoc = getLexer().getLoc();
754
756 if (getParser().parseIdentifier(Type))
757 return TokError("expected symbol type");
758
760 if (Attr == MCSA_Invalid)
761 return Error(TypeLoc, "unsupported attribute");
762
763 if (getLexer().isNot(AsmToken::EndOfStatement))
764 return TokError("expected end of directive");
765 Lex();
766
767 getStreamer().emitSymbolAttribute(Sym, Attr);
768
769 return false;
770}
771
772/// parseDirectiveIdent
773/// ::= .ident string
774bool ELFAsmParser::parseDirectiveIdent(StringRef, SMLoc) {
775 if (getLexer().isNot(AsmToken::String))
776 return TokError("expected string");
777
778 StringRef Data = getTok().getIdentifier();
779
780 Lex();
781
782 if (getLexer().isNot(AsmToken::EndOfStatement))
783 return TokError("expected end of directive");
784 Lex();
785
786 getStreamer().emitIdent(Data);
787 return false;
788}
789
790/// parseDirectiveSymver
791/// ::= .symver foo, bar2@zed
792bool ELFAsmParser::parseDirectiveSymver(StringRef, SMLoc) {
793 StringRef OriginalName, Name, Action;
794 if (getParser().parseIdentifier(OriginalName))
795 return TokError("expected identifier");
796
797 if (getLexer().isNot(AsmToken::Comma))
798 return TokError("expected a comma");
799
800 // ARM assembly uses @ for a comment...
801 // except when parsing the second parameter of the .symver directive.
802 // Force the next symbol to allow @ in the identifier, which is
803 // required for this directive and then reset it to its initial state.
804 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
805 getLexer().setAllowAtInIdentifier(true);
806 Lex();
807 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
808
809 if (getParser().parseIdentifier(Name))
810 return TokError("expected identifier");
811
812 if (!Name.contains('@'))
813 return TokError("expected a '@' in the name");
814 bool KeepOriginalSym = !Name.contains("@@@");
815 if (parseOptionalToken(AsmToken::Comma)) {
816 if (getParser().parseIdentifier(Action) || Action != "remove")
817 return TokError("expected 'remove'");
818 KeepOriginalSym = false;
819 }
820 (void)parseOptionalToken(AsmToken::EndOfStatement);
821
822 getStreamer().emitELFSymverDirective(
823 getContext().getOrCreateSymbol(OriginalName), Name, KeepOriginalSym);
824 return false;
825}
826
827/// parseDirectiveVersion
828/// ::= .version string
829bool ELFAsmParser::parseDirectiveVersion(StringRef, SMLoc) {
830 if (getLexer().isNot(AsmToken::String))
831 return TokError("expected string");
832
833 StringRef Data = getTok().getIdentifier();
834
835 Lex();
836
837 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
838
839 getStreamer().pushSection();
840 getStreamer().switchSection(Note);
841 getStreamer().emitInt32(Data.size() + 1); // namesz
842 getStreamer().emitInt32(0); // descsz = 0 (no description).
843 getStreamer().emitInt32(1); // type = NT_VERSION
844 getStreamer().emitBytes(Data); // name
845 getStreamer().emitInt8(0); // NUL
846 getStreamer().emitValueToAlignment(Align(4));
847 getStreamer().popSection();
848 return false;
849}
850
851/// parseDirectiveWeakref
852/// ::= .weakref foo, bar
853bool ELFAsmParser::parseDirectiveWeakref(StringRef, SMLoc) {
854 // FIXME: Share code with the other alias building directives.
855
856 StringRef AliasName;
857 if (getParser().parseIdentifier(AliasName))
858 return TokError("expected identifier");
859
860 if (getLexer().isNot(AsmToken::Comma))
861 return TokError("expected a comma");
862
863 Lex();
864
866 if (getParser().parseIdentifier(Name))
867 return TokError("expected identifier");
868
869 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
870
871 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
872
873 getStreamer().emitWeakReference(Alias, Sym);
874 return false;
875}
876
877bool ELFAsmParser::parseDirectiveSubsection(StringRef, SMLoc) {
878 const MCExpr *Subsection = MCConstantExpr::create(0, getContext());
879 if (getLexer().isNot(AsmToken::EndOfStatement)) {
880 if (getParser().parseExpression(Subsection))
881 return true;
882 }
883
884 if (getLexer().isNot(AsmToken::EndOfStatement))
885 return TokError("expected end of directive");
886
887 Lex();
888
889 return getStreamer().switchSection(getStreamer().getCurrentSectionOnly(),
890 Subsection);
891}
892
893bool ELFAsmParser::parseDirectiveCGProfile(StringRef S, SMLoc Loc) {
895}
896
897namespace llvm {
898
900 return new ELFAsmParser;
901}
902
903} // end namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isNot(const MachineRegisterInfo &MRI, const MachineInstr &MI)
static bool hasPrefix(StringRef SectionName, StringRef Prefix)
static bool allowSectionTypeMismatch(const Triple &TT, StringRef SectionName, unsigned Type)
static unsigned parseSectionFlags(const Triple &TT, StringRef flagsStr, bool *UseLastGroup)
static MCSymbolAttr MCAttrForString(StringRef Type)
std::string Name
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define _
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file contains some functions that are useful when dealing with strings.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
DEMANGLE_NAMESPACE_BEGIN bool starts_with(std::string_view self, char C) noexcept
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
bool parseDirectiveCGProfile(StringRef, SMLoc)
parseDirectiveCGProfile ::= .cg_profile identifier, identifier, <number>
Generic assembler parser interface, for use by target specific assembly parsers.
Definition: MCAsmParser.h:124
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
Definition: MCAsmParser.h:128
virtual void addDirectiveHandler(StringRef Directive, ExtensionDirectiveHandler Handler)=0
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:212
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:27
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:496
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
Definition: MCSymbol.h:237
Represents a location in source code.
Definition: SMLoc.h:23
constexpr const char * getPointer() const
Definition: SMLoc.h:34
SectionKind - This is a simple POD value that classifies the properties of a section.
Definition: SectionKind.h:22
static SectionKind getThreadData()
Definition: SectionKind.h:207
static SectionKind getText()
Definition: SectionKind.h:190
static SectionKind getData()
Definition: SectionKind.h:213
static SectionKind getBSS()
Definition: SectionKind.h:209
static SectionKind getThreadBSS()
Definition: SectionKind.h:206
static SectionKind getReadOnly()
Definition: SectionKind.h:192
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:480
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:68
R Default(T Value)
Definition: StringSwitch.h:177
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:87
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
@ SHT_LLVM_JT_SIZES
Definition: ELF.h:1181
@ SHT_LLVM_DEPENDENT_LIBRARIES
Definition: ELF.h:1171
@ SHT_PROGBITS
Definition: ELF.h:1140
@ SHT_LLVM_LINKER_OPTIONS
Definition: ELF.h:1168
@ SHT_LLVM_CALL_GRAPH_PROFILE
Definition: ELF.h:1177
@ SHT_NOBITS
Definition: ELF.h:1147
@ SHT_LLVM_ODRTAB
Definition: ELF.h:1167
@ SHT_LLVM_OFFLOADING
Definition: ELF.h:1179
@ SHT_LLVM_LTO
Definition: ELF.h:1180
@ SHT_PREINIT_ARRAY
Definition: ELF.h:1153
@ SHT_LLVM_BB_ADDR_MAP
Definition: ELF.h:1178
@ SHT_LLVM_CFI_JUMP_TABLE
Definition: ELF.h:1182
@ SHT_INIT_ARRAY
Definition: ELF.h:1151
@ SHT_NOTE
Definition: ELF.h:1146
@ SHT_X86_64_UNWIND
Definition: ELF.h:1214
@ SHT_LLVM_SYMPART
Definition: ELF.h:1173
@ SHT_FINI_ARRAY
Definition: ELF.h:1152
@ 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:1289
@ SHF_MERGE
Definition: ELF.h:1246
@ SHF_STRINGS
Definition: ELF.h:1249
@ SHF_AARCH64_PURECODE
Definition: ELF.h:1338
@ 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:1294
@ SHF_EXCLUDE
Definition: ELF.h:1274
@ SHF_ALLOC
Definition: ELF.h:1240
@ SHF_LINK_ORDER
Definition: ELF.h:1255
@ SHF_HEX_GPREL
Definition: ELF.h:1307
@ SHF_GROUP
Definition: ELF.h:1262
@ SHF_SUNW_NODISCARD
Definition: ELF.h:1281
@ SHF_X86_64_LARGE
Definition: ELF.h:1303
@ SHF_GNU_RETAIN
Definition: ELF.h:1271
@ SHF_WRITE
Definition: ELF.h:1237
@ SHF_TLS
Definition: ELF.h:1265
@ SHF_ARM_PURECODE
Definition: ELF.h:1335
@ SHF_EXECINSTR
Definition: ELF.h:1243
LLVM_ABI int getDwarfVersion()
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
std::pair< MCSection *, uint32_t > MCSectionSubPair
Definition: MCStreamer.h:66
MCAsmParserExtension * createELFAsmParser()
MCSymbolAttr
Definition: MCDirectives.h:18
@ MCSA_Local
.local (ELF)
Definition: MCDirectives.h:38
@ MCSA_Protected
.protected (ELF)
Definition: MCDirectives.h:43
@ MCSA_Internal
.internal (ELF)
Definition: MCDirectives.h:36
@ MCSA_ELF_TypeIndFunction
.type _foo, STT_GNU_IFUNC
Definition: MCDirectives.h:24
@ MCSA_ELF_TypeNoType
.type _foo, STT_NOTYPE # aka @notype
Definition: MCDirectives.h:28
@ MCSA_Weak
.weak
Definition: MCDirectives.h:45
@ MCSA_ELF_TypeTLS
.type _foo, STT_TLS # aka @tls_object
Definition: MCDirectives.h:26
@ MCSA_ELF_TypeCommon
.type _foo, STT_COMMON # aka @common
Definition: MCDirectives.h:27
@ MCSA_ELF_TypeObject
.type _foo, STT_OBJECT # aka @object
Definition: MCDirectives.h:25
@ MCSA_ELF_TypeGnuUniqueObject
Definition: MCDirectives.h:29
@ MCSA_ELF_TypeFunction
.type _foo, STT_FUNC # aka @function
Definition: MCDirectives.h:23
@ MCSA_Hidden
.hidden (ELF)
Definition: MCDirectives.h:33
@ MCSA_Invalid
Not a valid directive.
Definition: MCDirectives.h:19
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39