LLVM 20.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
10#include "llvm/ADT/StringRef.h"
13#include "llvm/MC/MCAsmInfo.h"
14#include "llvm/MC/MCContext.h"
20#include "llvm/MC/MCStreamer.h"
21#include "llvm/MC/MCSymbol.h"
22#include "llvm/MC/MCSymbolELF.h"
23#include "llvm/MC/SectionKind.h"
26#include "llvm/Support/SMLoc.h"
27#include <cassert>
28#include <cstdint>
29#include <utility>
30
31using namespace llvm;
32
33namespace {
34
35class ELFAsmParser : public MCAsmParserExtension {
36 template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
37 void addDirectiveHandler(StringRef Directive) {
38 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
39 this, HandleDirective<ELFAsmParser, HandlerMethod>);
40
42 }
43
44 bool parseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags,
45 SectionKind Kind);
46
47public:
48 ELFAsmParser() { BracketExpressionsSupported = true; }
49
50 void Initialize(MCAsmParser &Parser) override {
51 // Call the base implementation.
53
54 addDirectiveHandler<&ELFAsmParser::parseSectionDirectiveData>(".data");
55 addDirectiveHandler<&ELFAsmParser::parseSectionDirectiveText>(".text");
56 addDirectiveHandler<&ELFAsmParser::parseSectionDirectiveBSS>(".bss");
57 addDirectiveHandler<&ELFAsmParser::parseSectionDirectiveRoData>(".rodata");
58 addDirectiveHandler<&ELFAsmParser::parseSectionDirectiveTData>(".tdata");
59 addDirectiveHandler<&ELFAsmParser::parseSectionDirectiveTBSS>(".tbss");
60 addDirectiveHandler<
61 &ELFAsmParser::parseSectionDirectiveDataRel>(".data.rel");
62 addDirectiveHandler<
63 &ELFAsmParser::parseSectionDirectiveDataRelRo>(".data.rel.ro");
64 addDirectiveHandler<
65 &ELFAsmParser::parseSectionDirectiveEhFrame>(".eh_frame");
66 addDirectiveHandler<&ELFAsmParser::parseDirectiveSection>(".section");
67 addDirectiveHandler<
68 &ELFAsmParser::parseDirectivePushSection>(".pushsection");
69 addDirectiveHandler<&ELFAsmParser::parseDirectivePopSection>(".popsection");
70 addDirectiveHandler<&ELFAsmParser::parseDirectiveSize>(".size");
71 addDirectiveHandler<&ELFAsmParser::parseDirectivePrevious>(".previous");
72 addDirectiveHandler<&ELFAsmParser::parseDirectiveType>(".type");
73 addDirectiveHandler<&ELFAsmParser::parseDirectiveIdent>(".ident");
74 addDirectiveHandler<&ELFAsmParser::parseDirectiveSymver>(".symver");
75 addDirectiveHandler<&ELFAsmParser::parseDirectiveVersion>(".version");
76 addDirectiveHandler<&ELFAsmParser::parseDirectiveWeakref>(".weakref");
77 addDirectiveHandler<&ELFAsmParser::parseDirectiveSymbolAttribute>(".weak");
78 addDirectiveHandler<&ELFAsmParser::parseDirectiveSymbolAttribute>(".local");
79 addDirectiveHandler<
80 &ELFAsmParser::parseDirectiveSymbolAttribute>(".protected");
81 addDirectiveHandler<
82 &ELFAsmParser::parseDirectiveSymbolAttribute>(".internal");
83 addDirectiveHandler<
84 &ELFAsmParser::parseDirectiveSymbolAttribute>(".hidden");
85 addDirectiveHandler<&ELFAsmParser::parseDirectiveSubsection>(".subsection");
86 addDirectiveHandler<&ELFAsmParser::parseDirectiveCGProfile>(".cg_profile");
87 }
88
89 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
90 // the best way for us to get access to it?
91 bool parseSectionDirectiveData(StringRef, SMLoc) {
92 return parseSectionSwitch(".data", ELF::SHT_PROGBITS,
95 }
96 bool parseSectionDirectiveText(StringRef, SMLoc) {
97 return parseSectionSwitch(".text", ELF::SHT_PROGBITS,
100 }
101 bool parseSectionDirectiveBSS(StringRef, SMLoc) {
102 return parseSectionSwitch(".bss", ELF::SHT_NOBITS,
105 }
106 bool parseSectionDirectiveRoData(StringRef, SMLoc) {
107 return parseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
110 }
111 bool parseSectionDirectiveTData(StringRef, SMLoc) {
112 return parseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
116 }
117 bool parseSectionDirectiveTBSS(StringRef, SMLoc) {
118 return parseSectionSwitch(".tbss", ELF::SHT_NOBITS,
122 }
123 bool parseSectionDirectiveDataRel(StringRef, SMLoc) {
124 return parseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
127 }
128 bool parseSectionDirectiveDataRelRo(StringRef, SMLoc) {
129 return parseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
133 }
134 bool parseSectionDirectiveEhFrame(StringRef, SMLoc) {
135 return parseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
138 }
139 bool parseDirectivePushSection(StringRef, SMLoc);
140 bool parseDirectivePopSection(StringRef, SMLoc);
141 bool parseDirectiveSection(StringRef, SMLoc);
142 bool parseDirectiveSize(StringRef, SMLoc);
143 bool parseDirectivePrevious(StringRef, SMLoc);
144 bool parseDirectiveType(StringRef, SMLoc);
145 bool parseDirectiveIdent(StringRef, SMLoc);
146 bool parseDirectiveSymver(StringRef, SMLoc);
147 bool parseDirectiveVersion(StringRef, SMLoc);
148 bool parseDirectiveWeakref(StringRef, SMLoc);
149 bool parseDirectiveSymbolAttribute(StringRef, SMLoc);
150 bool parseDirectiveSubsection(StringRef, SMLoc);
152
153private:
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, bool &IsComdat);
160 bool parseLinkedToSym(MCSymbolELF *&LinkedToSym);
161 bool maybeParseUniqueID(int64_t &UniqueID);
162};
163
164} // end anonymous namespace
165
166/// parseDirectiveSymbolAttribute
167/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
168bool ELFAsmParser::parseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
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) {
180
181 if (getParser().parseIdentifier(Name))
182 return TokError("expected identifier");
183
184 if (getParser().discardLTOSymbol(Name)) {
185 if (getLexer().is(AsmToken::EndOfStatement))
186 break;
187 continue;
188 }
189
190 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
191
192 getStreamer().emitSymbolAttribute(Sym, Attr);
193
194 if (getLexer().is(AsmToken::EndOfStatement))
195 break;
196
197 if (getLexer().isNot(AsmToken::Comma))
198 return TokError("expected comma");
199 Lex();
200 }
201 }
202
203 Lex();
204 return false;
205}
206
207bool ELFAsmParser::parseSectionSwitch(StringRef Section, unsigned Type,
208 unsigned Flags, SectionKind Kind) {
209 const MCExpr *Subsection = nullptr;
210 if (getLexer().isNot(AsmToken::EndOfStatement)) {
211 if (getParser().parseExpression(Subsection))
212 return true;
213 }
214 Lex();
215
216 getStreamer().switchSection(getContext().getELFSection(Section, Type, Flags),
217 Subsection);
218
219 return false;
220}
221
222bool ELFAsmParser::parseDirectiveSize(StringRef, SMLoc) {
224 if (getParser().parseIdentifier(Name))
225 return TokError("expected identifier");
226 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
227
228 if (getLexer().isNot(AsmToken::Comma))
229 return TokError("expected comma");
230 Lex();
231
232 const MCExpr *Expr;
233 if (getParser().parseExpression(Expr))
234 return true;
235
236 if (getLexer().isNot(AsmToken::EndOfStatement))
237 return TokError("unexpected token");
238 Lex();
239
240 getStreamer().emitELFSize(Sym, Expr);
241 return false;
242}
243
244bool ELFAsmParser::parseSectionName(StringRef &SectionName) {
245 // A section name can contain -, so we cannot just use
246 // parseIdentifier.
247 SMLoc FirstLoc = getLexer().getLoc();
248 unsigned Size = 0;
249
250 if (getLexer().is(AsmToken::String)) {
251 SectionName = getTok().getIdentifier();
252 Lex();
253 return false;
254 }
255
256 while (!getParser().hasPendingError()) {
257 SMLoc PrevLoc = getLexer().getLoc();
258 if (getLexer().is(AsmToken::Comma) ||
259 getLexer().is(AsmToken::EndOfStatement))
260 break;
261
262 unsigned CurSize;
263 if (getLexer().is(AsmToken::String)) {
264 CurSize = getTok().getIdentifier().size() + 2;
265 Lex();
266 } else if (getLexer().is(AsmToken::Identifier)) {
267 CurSize = getTok().getIdentifier().size();
268 Lex();
269 } else {
270 CurSize = getTok().getString().size();
271 Lex();
272 }
273 Size += CurSize;
274 SectionName = StringRef(FirstLoc.getPointer(), Size);
275
276 // Make sure the following token is adjacent.
277 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
278 break;
279 }
280 if (Size == 0)
281 return true;
282
283 return false;
284}
285
286static unsigned parseSectionFlags(const Triple &TT, StringRef flagsStr,
287 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 if (TT.getArch() != Triple::xcore)
322 return -1U;
324 break;
325 case 'd':
326 if (TT.getArch() != Triple::xcore)
327 return -1U;
329 break;
330 case 'y':
331 if (!(TT.isARM() || TT.isThumb()))
332 return -1U;
333 flags |= ELF::SHF_ARM_PURECODE;
334 break;
335 case 's':
336 if (TT.getArch() != Triple::hexagon)
337 return -1U;
338 flags |= ELF::SHF_HEX_GPREL;
339 break;
340 case 'G':
341 flags |= ELF::SHF_GROUP;
342 break;
343 case 'l':
344 if (TT.getArch() != Triple::x86_64)
345 return -1U;
346 flags |= ELF::SHF_X86_64_LARGE;
347 break;
348 case 'R':
349 if (TT.isOSSolaris())
351 else
352 flags |= ELF::SHF_GNU_RETAIN;
353 break;
354 case '?':
355 *UseLastGroup = true;
356 break;
357 default:
358 return -1U;
359 }
360 }
361
362 return flags;
363}
364
365unsigned ELFAsmParser::parseSunStyleSectionFlags() {
366 unsigned flags = 0;
367 while (getLexer().is(AsmToken::Hash)) {
368 Lex(); // Eat the #.
369
370 if (!getLexer().is(AsmToken::Identifier))
371 return -1U;
372
373 StringRef flagId = getTok().getIdentifier();
374 if (flagId == "alloc")
375 flags |= ELF::SHF_ALLOC;
376 else if (flagId == "execinstr")
377 flags |= ELF::SHF_EXECINSTR;
378 else if (flagId == "write")
379 flags |= ELF::SHF_WRITE;
380 else if (flagId == "tls")
381 flags |= ELF::SHF_TLS;
382 else
383 return -1U;
384
385 Lex(); // Eat the flag.
386
387 if (!getLexer().is(AsmToken::Comma))
388 break;
389 Lex(); // Eat the comma.
390 }
391 return flags;
392}
393
394
395bool ELFAsmParser::parseDirectivePushSection(StringRef s, SMLoc loc) {
396 getStreamer().pushSection();
397
398 if (parseSectionArguments(/*IsPush=*/true, loc)) {
399 getStreamer().popSection();
400 return true;
401 }
402
403 return false;
404}
405
406bool ELFAsmParser::parseDirectivePopSection(StringRef, SMLoc) {
407 if (!getStreamer().popSection())
408 return TokError(".popsection without corresponding .pushsection");
409 return false;
410}
411
412bool ELFAsmParser::parseDirectiveSection(StringRef, SMLoc loc) {
413 return parseSectionArguments(/*IsPush=*/false, loc);
414}
415
416bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) {
417 MCAsmLexer &L = getLexer();
418 if (L.isNot(AsmToken::Comma))
419 return false;
420 Lex();
421 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) &&
422 L.isNot(AsmToken::String)) {
423 if (L.getAllowAtInIdentifier())
424 return TokError("expected '@<type>', '%<type>' or \"<type>\"");
425 else
426 return TokError("expected '%<type>' or \"<type>\"");
427 }
428 if (!L.is(AsmToken::String))
429 Lex();
430 if (L.is(AsmToken::Integer)) {
431 TypeName = getTok().getString();
432 Lex();
433 } else if (getParser().parseIdentifier(TypeName))
434 return TokError("expected identifier");
435 return false;
436}
437
438bool ELFAsmParser::parseMergeSize(int64_t &Size) {
439 if (getLexer().isNot(AsmToken::Comma))
440 return TokError("expected the entry size");
441 Lex();
442 if (getParser().parseAbsoluteExpression(Size))
443 return true;
444 if (Size <= 0)
445 return TokError("entry size must be positive");
446 return false;
447}
448
449bool ELFAsmParser::parseGroup(StringRef &GroupName, bool &IsComdat) {
450 MCAsmLexer &L = getLexer();
451 if (L.isNot(AsmToken::Comma))
452 return TokError("expected group name");
453 Lex();
454 if (L.is(AsmToken::Integer)) {
455 GroupName = getTok().getString();
456 Lex();
457 } else if (getParser().parseIdentifier(GroupName)) {
458 return TokError("invalid group name");
459 }
460 if (L.is(AsmToken::Comma)) {
461 Lex();
463 if (getParser().parseIdentifier(Linkage))
464 return TokError("invalid linkage");
465 if (Linkage != "comdat")
466 return TokError("Linkage must be 'comdat'");
467 IsComdat = true;
468 } else {
469 IsComdat = false;
470 }
471 return false;
472}
473
474bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) {
475 MCAsmLexer &L = getLexer();
476 if (L.isNot(AsmToken::Comma))
477 return TokError("expected linked-to symbol");
478 Lex();
480 SMLoc StartLoc = L.getLoc();
481 if (getParser().parseIdentifier(Name)) {
482 if (getParser().getTok().getString() == "0") {
483 getParser().Lex();
484 LinkedToSym = nullptr;
485 return false;
486 }
487 return TokError("invalid linked-to symbol");
488 }
489 LinkedToSym = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
490 if (!LinkedToSym || !LinkedToSym->isInSection())
491 return Error(StartLoc, "linked-to symbol is not in a section: " + Name);
492 return false;
493}
494
495bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) {
496 MCAsmLexer &L = getLexer();
497 if (L.isNot(AsmToken::Comma))
498 return false;
499 Lex();
500 StringRef UniqueStr;
501 if (getParser().parseIdentifier(UniqueStr))
502 return TokError("expected identifier");
503 if (UniqueStr != "unique")
504 return TokError("expected 'unique'");
505 if (L.isNot(AsmToken::Comma))
506 return TokError("expected commma");
507 Lex();
508 if (getParser().parseAbsoluteExpression(UniqueID))
509 return true;
510 if (UniqueID < 0)
511 return TokError("unique id must be positive");
512 if (!isUInt<32>(UniqueID) || UniqueID == ~0U)
513 return TokError("unique id is too large");
514 return false;
515}
516
518 return SectionName.consume_front(Prefix) &&
519 (SectionName.empty() || SectionName[0] == '.');
520}
521
523 unsigned Type) {
524 if (TT.getArch() == Triple::x86_64) {
525 // x86-64 psABI names SHT_X86_64_UNWIND as the canonical type for .eh_frame,
526 // but GNU as emits SHT_PROGBITS .eh_frame for .cfi_* directives. Don't
527 // error for SHT_PROGBITS .eh_frame
528 return SectionName == ".eh_frame" && Type == ELF::SHT_PROGBITS;
529 }
530 if (TT.isMIPS()) {
531 // MIPS .debug_* sections should have SHT_MIPS_DWARF section type to
532 // distinguish among sections contain DWARF and ECOFF debug formats,
533 // but in assembly files these sections have SHT_PROGBITS type.
534 return SectionName.starts_with(".debug_") && Type == ELF::SHT_PROGBITS;
535 }
536 return false;
537}
538
539bool ELFAsmParser::parseSectionArguments(bool IsPush, SMLoc loc) {
541
542 if (parseSectionName(SectionName))
543 return TokError("expected identifier");
544
546 int64_t Size = 0;
547 StringRef GroupName;
548 bool IsComdat = false;
549 unsigned Flags = 0;
550 unsigned extraFlags = 0;
551 const MCExpr *Subsection = nullptr;
552 bool UseLastGroup = false;
553 MCSymbolELF *LinkedToSym = nullptr;
554 int64_t UniqueID = ~0;
555
556 // Set the defaults first.
557 if (hasPrefix(SectionName, ".rodata") || SectionName == ".rodata1")
559 else if (SectionName == ".fini" || SectionName == ".init" ||
560 hasPrefix(SectionName, ".text"))
562 else if (hasPrefix(SectionName, ".data") || SectionName == ".data1" ||
563 hasPrefix(SectionName, ".bss") ||
564 hasPrefix(SectionName, ".init_array") ||
565 hasPrefix(SectionName, ".fini_array") ||
566 hasPrefix(SectionName, ".preinit_array"))
568 else if (hasPrefix(SectionName, ".tdata") || hasPrefix(SectionName, ".tbss"))
570
571 if (getLexer().is(AsmToken::Comma)) {
572 Lex();
573
574 if (IsPush && getLexer().isNot(AsmToken::String)) {
575 if (getParser().parseExpression(Subsection))
576 return true;
577 if (getLexer().isNot(AsmToken::Comma))
578 goto EndStmt;
579 Lex();
580 }
581
582 if (getLexer().isNot(AsmToken::String)) {
583 if (getLexer().isNot(AsmToken::Hash))
584 return TokError("expected string");
585 extraFlags = parseSunStyleSectionFlags();
586 } else {
587 StringRef FlagsStr = getTok().getStringContents();
588 Lex();
589 extraFlags = parseSectionFlags(getContext().getTargetTriple(), FlagsStr,
590 &UseLastGroup);
591 }
592
593 if (extraFlags == -1U)
594 return TokError("unknown flag");
595 Flags |= extraFlags;
596
597 bool Mergeable = Flags & ELF::SHF_MERGE;
598 bool Group = Flags & ELF::SHF_GROUP;
599 if (Group && UseLastGroup)
600 return TokError("Section cannot specifiy a group name while also acting "
601 "as a member of the last group");
602
603 if (maybeParseSectionType(TypeName))
604 return true;
605
606 MCAsmLexer &L = getLexer();
607 if (TypeName.empty()) {
608 if (Mergeable)
609 return TokError("Mergeable section must specify the type");
610 if (Group)
611 return TokError("Group section must specify the type");
612 if (L.isNot(AsmToken::EndOfStatement))
613 return TokError("expected end of directive");
614 }
615
616 if (Mergeable)
617 if (parseMergeSize(Size))
618 return true;
619 if (Flags & ELF::SHF_LINK_ORDER)
620 if (parseLinkedToSym(LinkedToSym))
621 return true;
622 if (Group)
623 if (parseGroup(GroupName, IsComdat))
624 return true;
625 if (maybeParseUniqueID(UniqueID))
626 return true;
627 }
628
629EndStmt:
630 if (getLexer().isNot(AsmToken::EndOfStatement))
631 return TokError("expected end of directive");
632 Lex();
633
634 unsigned Type = ELF::SHT_PROGBITS;
635
636 if (TypeName.empty()) {
637 if (SectionName.starts_with(".note"))
639 else if (hasPrefix(SectionName, ".init_array"))
641 else if (hasPrefix(SectionName, ".bss"))
643 else if (hasPrefix(SectionName, ".tbss"))
645 else if (hasPrefix(SectionName, ".fini_array"))
647 else if (hasPrefix(SectionName, ".preinit_array"))
649 } else {
650 if (TypeName == "init_array")
652 else if (TypeName == "fini_array")
654 else if (TypeName == "preinit_array")
656 else if (TypeName == "nobits")
658 else if (TypeName == "progbits")
660 else if (TypeName == "note")
662 else if (TypeName == "unwind")
664 else if (TypeName == "llvm_odrtab")
666 else if (TypeName == "llvm_linker_options")
668 else if (TypeName == "llvm_call_graph_profile")
670 else if (TypeName == "llvm_dependent_libraries")
672 else if (TypeName == "llvm_sympart")
674 else if (TypeName == "llvm_bb_addr_map")
676 else if (TypeName == "llvm_offloading")
678 else if (TypeName == "llvm_lto")
680 else if (TypeName == "llvm_jt_sizes")
682 else if (TypeName.getAsInteger(0, Type))
683 return TokError("unknown section type");
684 }
685
686 if (UseLastGroup) {
687 if (const MCSectionELF *Section =
688 cast_or_null<MCSectionELF>(getStreamer().getCurrentSectionOnly()))
689 if (const MCSymbol *Group = Section->getGroup()) {
690 GroupName = Group->getName();
691 IsComdat = Section->isComdat();
693 }
694 }
695
697 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName,
698 IsComdat, UniqueID, LinkedToSym);
699 getStreamer().switchSection(Section, Subsection);
700 // Check that flags are used consistently. However, the GNU assembler permits
701 // to leave out in subsequent uses of the same sections; for compatibility,
702 // do likewise.
703 if (!TypeName.empty() && Section->getType() != Type &&
704 !allowSectionTypeMismatch(getContext().getTargetTriple(), SectionName,
705 Type))
706 Error(loc, "changed section type for " + SectionName + ", expected: 0x" +
707 utohexstr(Section->getType()));
708 if ((extraFlags || Size || !TypeName.empty()) && Section->getFlags() != Flags)
709 Error(loc, "changed section flags for " + SectionName + ", expected: 0x" +
710 utohexstr(Section->getFlags()));
711 if ((extraFlags || Size || !TypeName.empty()) &&
712 Section->getEntrySize() != Size)
713 Error(loc, "changed section entsize for " + SectionName +
714 ", expected: " + Twine(Section->getEntrySize()));
715
716 if (getContext().getGenDwarfForAssembly() &&
717 (Section->getFlags() & ELF::SHF_ALLOC) &&
718 (Section->getFlags() & ELF::SHF_EXECINSTR)) {
719 bool InsertResult = getContext().addGenDwarfSection(Section);
720 if (InsertResult && getContext().getDwarfVersion() <= 2)
721 Warning(loc, "DWARF2 only supports one section per compilation unit");
722 }
723
724 return false;
725}
726
727bool ELFAsmParser::parseDirectivePrevious(StringRef DirName, SMLoc) {
728 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
729 if (PreviousSection.first == nullptr)
730 return TokError(".previous without corresponding .section");
731 getStreamer().switchSection(PreviousSection.first, PreviousSection.second);
732
733 return false;
734}
735
738 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction)
739 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject)
740 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS)
741 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon)
742 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType)
743 .Cases("STT_GNU_IFUNC", "gnu_indirect_function",
745 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
747}
748
749/// parseDirectiveELFType
750/// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
751/// ::= .type identifier , #attribute
752/// ::= .type identifier , @attribute
753/// ::= .type identifier , %attribute
754/// ::= .type identifier , "attribute"
755bool ELFAsmParser::parseDirectiveType(StringRef, SMLoc) {
757 if (getParser().parseIdentifier(Name))
758 return TokError("expected identifier");
759
760 // Handle the identifier as the key symbol.
761 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
762
763 // NOTE the comma is optional in all cases. It is only documented as being
764 // optional in the first case, however, GAS will silently treat the comma as
765 // optional in all cases. Furthermore, although the documentation states that
766 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS
767 // accepts both the upper case name as well as the lower case aliases.
768 if (getLexer().is(AsmToken::Comma))
769 Lex();
770
771 if (getLexer().isNot(AsmToken::Identifier) &&
772 getLexer().isNot(AsmToken::Hash) &&
773 getLexer().isNot(AsmToken::Percent) &&
774 getLexer().isNot(AsmToken::String)) {
775 if (!getLexer().getAllowAtInIdentifier())
776 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', "
777 "'%<type>' or \"<type>\"");
778 else if (getLexer().isNot(AsmToken::At))
779 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
780 "'%<type>' or \"<type>\"");
781 }
782
783 if (getLexer().isNot(AsmToken::String) &&
784 getLexer().isNot(AsmToken::Identifier))
785 Lex();
786
787 SMLoc TypeLoc = getLexer().getLoc();
788
790 if (getParser().parseIdentifier(Type))
791 return TokError("expected symbol type");
792
794 if (Attr == MCSA_Invalid)
795 return Error(TypeLoc, "unsupported attribute");
796
797 if (getLexer().isNot(AsmToken::EndOfStatement))
798 return TokError("expected end of directive");
799 Lex();
800
801 getStreamer().emitSymbolAttribute(Sym, Attr);
802
803 return false;
804}
805
806/// parseDirectiveIdent
807/// ::= .ident string
808bool ELFAsmParser::parseDirectiveIdent(StringRef, SMLoc) {
809 if (getLexer().isNot(AsmToken::String))
810 return TokError("expected string");
811
812 StringRef Data = getTok().getIdentifier();
813
814 Lex();
815
816 if (getLexer().isNot(AsmToken::EndOfStatement))
817 return TokError("expected end of directive");
818 Lex();
819
820 getStreamer().emitIdent(Data);
821 return false;
822}
823
824/// parseDirectiveSymver
825/// ::= .symver foo, bar2@zed
826bool ELFAsmParser::parseDirectiveSymver(StringRef, SMLoc) {
827 StringRef OriginalName, Name, Action;
828 if (getParser().parseIdentifier(OriginalName))
829 return TokError("expected identifier");
830
831 if (getLexer().isNot(AsmToken::Comma))
832 return TokError("expected a comma");
833
834 // ARM assembly uses @ for a comment...
835 // except when parsing the second parameter of the .symver directive.
836 // Force the next symbol to allow @ in the identifier, which is
837 // required for this directive and then reset it to its initial state.
838 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier();
839 getLexer().setAllowAtInIdentifier(true);
840 Lex();
841 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier);
842
843 if (getParser().parseIdentifier(Name))
844 return TokError("expected identifier");
845
846 if (!Name.contains('@'))
847 return TokError("expected a '@' in the name");
848 bool KeepOriginalSym = !Name.contains("@@@");
849 if (parseOptionalToken(AsmToken::Comma)) {
850 if (getParser().parseIdentifier(Action) || Action != "remove")
851 return TokError("expected 'remove'");
852 KeepOriginalSym = false;
853 }
854 (void)parseOptionalToken(AsmToken::EndOfStatement);
855
856 getStreamer().emitELFSymverDirective(
857 getContext().getOrCreateSymbol(OriginalName), Name, KeepOriginalSym);
858 return false;
859}
860
861/// parseDirectiveVersion
862/// ::= .version string
863bool ELFAsmParser::parseDirectiveVersion(StringRef, SMLoc) {
864 if (getLexer().isNot(AsmToken::String))
865 return TokError("expected string");
866
867 StringRef Data = getTok().getIdentifier();
868
869 Lex();
870
871 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
872
873 getStreamer().pushSection();
874 getStreamer().switchSection(Note);
875 getStreamer().emitInt32(Data.size() + 1); // namesz
876 getStreamer().emitInt32(0); // descsz = 0 (no description).
877 getStreamer().emitInt32(1); // type = NT_VERSION
878 getStreamer().emitBytes(Data); // name
879 getStreamer().emitInt8(0); // NUL
880 getStreamer().emitValueToAlignment(Align(4));
881 getStreamer().popSection();
882 return false;
883}
884
885/// parseDirectiveWeakref
886/// ::= .weakref foo, bar
887bool ELFAsmParser::parseDirectiveWeakref(StringRef, SMLoc) {
888 // FIXME: Share code with the other alias building directives.
889
890 StringRef AliasName;
891 if (getParser().parseIdentifier(AliasName))
892 return TokError("expected identifier");
893
894 if (getLexer().isNot(AsmToken::Comma))
895 return TokError("expected a comma");
896
897 Lex();
898
900 if (getParser().parseIdentifier(Name))
901 return TokError("expected identifier");
902
903 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName);
904
905 MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
906
907 getStreamer().emitWeakReference(Alias, Sym);
908 return false;
909}
910
911bool ELFAsmParser::parseDirectiveSubsection(StringRef, SMLoc) {
912 const MCExpr *Subsection = MCConstantExpr::create(0, getContext());
913 if (getLexer().isNot(AsmToken::EndOfStatement)) {
914 if (getParser().parseExpression(Subsection))
915 return true;
916 }
917
918 if (getLexer().isNot(AsmToken::EndOfStatement))
919 return TokError("expected end of directive");
920
921 Lex();
922
923 return getStreamer().switchSection(getStreamer().getCurrentSectionOnly(),
924 Subsection);
925}
926
927bool ELFAsmParser::parseDirectiveCGProfile(StringRef S, SMLoc Loc) {
929}
930
931namespace llvm {
932
934 return new ELFAsmParser;
935}
936
937} // end namespace llvm
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
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Generic assembler lexer interface, for use by target specific assembly lexers.
Definition: MCAsmLexer.h:37
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:123
std::pair< MCAsmParserExtension *, DirectiveHandler > ExtensionDirectiveHandler
Definition: MCAsmParser.h:127
virtual void addDirectiveHandler(StringRef Directive, ExtensionDirectiveHandler Handler)=0
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:222
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:36
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
Definition: MCSymbol.h:254
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 getReadOnlyWithRel()
Definition: SectionKind.h:214
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:51
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:470
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
StringSwitch & Cases(StringLiteral S0, StringLiteral S1, T Value)
Definition: StringSwitch.h:90
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
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:1133
@ SHT_LLVM_DEPENDENT_LIBRARIES
Definition: ELF.h:1121
@ SHT_PROGBITS
Definition: ELF.h:1090
@ SHT_LLVM_LINKER_OPTIONS
Definition: ELF.h:1118
@ SHT_LLVM_CALL_GRAPH_PROFILE
Definition: ELF.h:1129
@ SHT_NOBITS
Definition: ELF.h:1097
@ SHT_LLVM_ODRTAB
Definition: ELF.h:1117
@ SHT_LLVM_OFFLOADING
Definition: ELF.h:1131
@ SHT_LLVM_LTO
Definition: ELF.h:1132
@ SHT_PREINIT_ARRAY
Definition: ELF.h:1103
@ SHT_LLVM_BB_ADDR_MAP
Definition: ELF.h:1130
@ SHT_INIT_ARRAY
Definition: ELF.h:1101
@ SHT_NOTE
Definition: ELF.h:1096
@ SHT_X86_64_UNWIND
Definition: ELF.h:1162
@ SHT_LLVM_SYMPART
Definition: ELF.h:1123
@ SHT_FINI_ARRAY
Definition: ELF.h:1102
@ 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:1237
@ SHF_MERGE
Definition: ELF.h:1194
@ SHF_STRINGS
Definition: ELF.h:1197
@ 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:1242
@ SHF_EXCLUDE
Definition: ELF.h:1222
@ SHF_ALLOC
Definition: ELF.h:1188
@ SHF_LINK_ORDER
Definition: ELF.h:1203
@ SHF_HEX_GPREL
Definition: ELF.h:1255
@ SHF_GROUP
Definition: ELF.h:1210
@ SHF_SUNW_NODISCARD
Definition: ELF.h:1229
@ SHF_X86_64_LARGE
Definition: ELF.h:1251
@ SHF_GNU_RETAIN
Definition: ELF.h:1219
@ SHF_WRITE
Definition: ELF.h:1185
@ SHF_TLS
Definition: ELF.h:1213
@ SHF_ARM_PURECODE
Definition: ELF.h:1283
@ SHF_EXECINSTR
Definition: ELF.h:1191
int getDwarfVersion()
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::pair< MCSection *, uint32_t > MCSectionSubPair
Definition: MCStreamer.h:67
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