1 //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCParser/MCAsmLexer.h"
17 #include "llvm/MC/MCSectionELF.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/Support/ELF.h"
24 class ELFAsmParser : public MCAsmParserExtension {
25 template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
26 void addDirectiveHandler(StringRef Directive) {
27 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
28 this, HandleDirective<ELFAsmParser, HandlerMethod>);
30 getParser().addDirectiveHandler(Directive, Handler);
33 bool ParseSectionSwitch(StringRef Section, unsigned Type,
34 unsigned Flags, SectionKind Kind);
38 ELFAsmParser() : SeenIdent(false) {
39 BracketExpressionsSupported = true;
42 virtual void Initialize(MCAsmParser &Parser) {
43 // Call the base implementation.
44 this->MCAsmParserExtension::Initialize(Parser);
46 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
47 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
48 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
49 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
50 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
51 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
53 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
55 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
57 &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
59 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
60 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
62 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
63 addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
64 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
65 addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
66 addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
67 addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
68 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
69 addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
70 addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
71 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
72 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
74 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
76 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
78 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
79 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
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,
86 ELF::SHF_WRITE |ELF::SHF_ALLOC,
87 SectionKind::getDataRel());
89 bool ParseSectionDirectiveText(StringRef, SMLoc) {
90 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
92 ELF::SHF_ALLOC, SectionKind::getText());
94 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
95 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
97 ELF::SHF_ALLOC, SectionKind::getBSS());
99 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
100 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
102 SectionKind::getReadOnly());
104 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
105 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
107 ELF::SHF_TLS | ELF::SHF_WRITE,
108 SectionKind::getThreadData());
110 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
111 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
113 ELF::SHF_TLS | ELF::SHF_WRITE,
114 SectionKind::getThreadBSS());
116 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
117 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
120 SectionKind::getDataRel());
122 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
123 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
126 SectionKind::getReadOnlyWithRel());
128 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
129 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
132 SectionKind::getReadOnlyWithRelLocal());
134 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
135 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
138 SectionKind::getDataRel());
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);
154 bool ParseSectionName(StringRef &SectionName);
155 bool ParseSectionArguments(bool IsPush);
160 /// ParseDirectiveSymbolAttribute
161 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
162 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
163 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
164 .Case(".weak", MCSA_Weak)
165 .Case(".local", MCSA_Local)
166 .Case(".hidden", MCSA_Hidden)
167 .Case(".internal", MCSA_Internal)
168 .Case(".protected", MCSA_Protected)
169 .Default(MCSA_Invalid);
170 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
171 if (getLexer().isNot(AsmToken::EndOfStatement)) {
175 if (getParser().parseIdentifier(Name))
176 return TokError("expected identifier in directive");
178 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
180 getStreamer().EmitSymbolAttribute(Sym, Attr);
182 if (getLexer().is(AsmToken::EndOfStatement))
185 if (getLexer().isNot(AsmToken::Comma))
186 return TokError("unexpected token in directive");
195 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
196 unsigned Flags, SectionKind Kind) {
197 const MCExpr *Subsection = 0;
198 if (getLexer().isNot(AsmToken::EndOfStatement)) {
199 if (getParser().parseExpression(Subsection))
203 getStreamer().SwitchSection(getContext().getELFSection(
204 Section, Type, Flags, Kind),
210 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
212 if (getParser().parseIdentifier(Name))
213 return TokError("expected identifier in directive");
214 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
216 if (getLexer().isNot(AsmToken::Comma))
217 return TokError("unexpected token in directive");
221 if (getParser().parseExpression(Expr))
224 if (getLexer().isNot(AsmToken::EndOfStatement))
225 return TokError("unexpected token in directive");
227 getStreamer().EmitELFSize(Sym, Expr);
231 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
232 // A section name can contain -, so we cannot just use
234 SMLoc FirstLoc = getLexer().getLoc();
237 if (getLexer().is(AsmToken::String)) {
238 SectionName = getTok().getIdentifier();
246 SMLoc PrevLoc = getLexer().getLoc();
247 if (getLexer().is(AsmToken::Minus)) {
249 Lex(); // Consume the "-".
250 } else if (getLexer().is(AsmToken::String)) {
251 CurSize = getTok().getIdentifier().size() + 2;
253 } else if (getLexer().is(AsmToken::Identifier)) {
254 CurSize = getTok().getIdentifier().size();
261 SectionName = StringRef(FirstLoc.getPointer(), Size);
263 // Make sure the following token is adjacent.
264 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
273 static SectionKind computeSectionKind(unsigned Flags) {
274 if (Flags & ELF::SHF_EXECINSTR)
275 return SectionKind::getText();
276 if (Flags & ELF::SHF_TLS)
277 return SectionKind::getThreadData();
278 return SectionKind::getDataRel();
281 static int parseSectionFlags(StringRef flagsStr) {
284 for (unsigned i = 0; i < flagsStr.size(); i++) {
285 switch (flagsStr[i]) {
287 flags |= ELF::SHF_ALLOC;
290 flags |= ELF::SHF_EXECINSTR;
293 flags |= ELF::SHF_WRITE;
296 flags |= ELF::SHF_MERGE;
299 flags |= ELF::SHF_STRINGS;
302 flags |= ELF::SHF_TLS;
305 flags |= ELF::XCORE_SHF_CP_SECTION;
308 flags |= ELF::XCORE_SHF_DP_SECTION;
311 flags |= ELF::SHF_GROUP;
321 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
322 getStreamer().PushSection();
324 if (ParseSectionArguments(/*IsPush=*/true)) {
325 getStreamer().PopSection();
332 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
333 if (!getStreamer().PopSection())
334 return TokError(".popsection without corresponding .pushsection");
338 // FIXME: This is a work in progress.
339 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
340 return ParseSectionArguments(/*IsPush=*/false);
343 bool ELFAsmParser::ParseSectionArguments(bool IsPush) {
344 StringRef SectionName;
346 if (ParseSectionName(SectionName))
347 return TokError("expected identifier in directive");
353 const MCExpr *Subsection = 0;
355 // Set the defaults first.
356 if (SectionName == ".fini" || SectionName == ".init" ||
357 SectionName == ".rodata")
358 Flags |= ELF::SHF_ALLOC;
359 if (SectionName == ".fini" || SectionName == ".init")
360 Flags |= ELF::SHF_EXECINSTR;
362 if (getLexer().is(AsmToken::Comma)) {
365 if (IsPush && getLexer().isNot(AsmToken::String)) {
366 if (getParser().parseExpression(Subsection))
368 if (getLexer().isNot(AsmToken::Comma))
373 if (getLexer().isNot(AsmToken::String))
374 return TokError("expected string in directive");
376 StringRef FlagsStr = getTok().getStringContents();
379 int extraFlags = parseSectionFlags(FlagsStr);
381 return TokError("unknown flag");
384 bool Mergeable = Flags & ELF::SHF_MERGE;
385 bool Group = Flags & ELF::SHF_GROUP;
387 if (getLexer().isNot(AsmToken::Comma)) {
389 return TokError("Mergeable section must specify the type");
391 return TokError("Group section must specify the type");
394 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
395 return TokError("expected '@' or '%' before type");
398 if (getParser().parseIdentifier(TypeName))
399 return TokError("expected identifier in directive");
402 if (getLexer().isNot(AsmToken::Comma))
403 return TokError("expected the entry size");
405 if (getParser().parseAbsoluteExpression(Size))
408 return TokError("entry size must be positive");
412 if (getLexer().isNot(AsmToken::Comma))
413 return TokError("expected group name");
415 if (getParser().parseIdentifier(GroupName))
417 if (getLexer().is(AsmToken::Comma)) {
420 if (getParser().parseIdentifier(Linkage))
422 if (Linkage != "comdat")
423 return TokError("Linkage must be 'comdat'");
430 if (getLexer().isNot(AsmToken::EndOfStatement))
431 return TokError("unexpected token in directive");
433 unsigned Type = ELF::SHT_PROGBITS;
435 if (TypeName.empty()) {
436 if (SectionName.startswith(".note"))
437 Type = ELF::SHT_NOTE;
438 else if (SectionName == ".init_array")
439 Type = ELF::SHT_INIT_ARRAY;
440 else if (SectionName == ".fini_array")
441 Type = ELF::SHT_FINI_ARRAY;
442 else if (SectionName == ".preinit_array")
443 Type = ELF::SHT_PREINIT_ARRAY;
445 if (TypeName == "init_array")
446 Type = ELF::SHT_INIT_ARRAY;
447 else if (TypeName == "fini_array")
448 Type = ELF::SHT_FINI_ARRAY;
449 else if (TypeName == "preinit_array")
450 Type = ELF::SHT_PREINIT_ARRAY;
451 else if (TypeName == "nobits")
452 Type = ELF::SHT_NOBITS;
453 else if (TypeName == "progbits")
454 Type = ELF::SHT_PROGBITS;
455 else if (TypeName == "note")
456 Type = ELF::SHT_NOTE;
457 else if (TypeName == "unwind")
458 Type = ELF::SHT_X86_64_UNWIND;
460 return TokError("unknown section type");
463 SectionKind Kind = computeSectionKind(Flags);
464 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
471 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
472 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
473 if (PreviousSection.first == NULL)
474 return TokError(".previous without corresponding .section");
475 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
480 /// ParseDirectiveELFType
481 /// ::= .type identifier , @attribute
482 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
484 if (getParser().parseIdentifier(Name))
485 return TokError("expected identifier in directive");
487 // Handle the identifier as the key symbol.
488 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
490 if (getLexer().isNot(AsmToken::Comma))
491 return TokError("unexpected token in '.type' directive");
494 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
495 return TokError("expected '@' or '%' before type");
501 TypeLoc = getLexer().getLoc();
502 if (getParser().parseIdentifier(Type))
503 return TokError("expected symbol type in directive");
505 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
506 .Case("function", MCSA_ELF_TypeFunction)
507 .Case("object", MCSA_ELF_TypeObject)
508 .Case("tls_object", MCSA_ELF_TypeTLS)
509 .Case("common", MCSA_ELF_TypeCommon)
510 .Case("notype", MCSA_ELF_TypeNoType)
511 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
512 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
513 .Default(MCSA_Invalid);
515 if (Attr == MCSA_Invalid)
516 return Error(TypeLoc, "unsupported attribute in '.type' directive");
518 if (getLexer().isNot(AsmToken::EndOfStatement))
519 return TokError("unexpected token in '.type' directive");
523 getStreamer().EmitSymbolAttribute(Sym, Attr);
528 /// ParseDirectiveIdent
529 /// ::= .ident string
530 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
531 if (getLexer().isNot(AsmToken::String))
532 return TokError("unexpected token in '.ident' directive");
534 StringRef Data = getTok().getIdentifier();
538 const MCSection *Comment =
539 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
542 SectionKind::getReadOnly(),
545 getStreamer().PushSection();
546 getStreamer().SwitchSection(Comment);
548 getStreamer().EmitIntValue(0, 1);
551 getStreamer().EmitBytes(Data);
552 getStreamer().EmitIntValue(0, 1);
553 getStreamer().PopSection();
557 /// ParseDirectiveSymver
558 /// ::= .symver foo, bar2@zed
559 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
561 if (getParser().parseIdentifier(Name))
562 return TokError("expected identifier in directive");
564 if (getLexer().isNot(AsmToken::Comma))
565 return TokError("expected a comma");
570 if (getParser().parseIdentifier(AliasName))
571 return TokError("expected identifier in directive");
573 if (AliasName.find('@') == StringRef::npos)
574 return TokError("expected a '@' in the name");
576 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
577 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
578 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
580 getStreamer().EmitAssignment(Alias, Value);
584 /// ParseDirectiveVersion
585 /// ::= .version string
586 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
587 if (getLexer().isNot(AsmToken::String))
588 return TokError("unexpected token in '.version' directive");
590 StringRef Data = getTok().getIdentifier();
594 const MCSection *Note =
595 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
596 SectionKind::getReadOnly());
598 getStreamer().PushSection();
599 getStreamer().SwitchSection(Note);
600 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
601 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
602 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
603 getStreamer().EmitBytes(Data); // name.
604 getStreamer().EmitIntValue(0, 1); // terminate the string.
605 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
606 getStreamer().PopSection();
610 /// ParseDirectiveWeakref
611 /// ::= .weakref foo, bar
612 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
613 // FIXME: Share code with the other alias building directives.
616 if (getParser().parseIdentifier(AliasName))
617 return TokError("expected identifier in directive");
619 if (getLexer().isNot(AsmToken::Comma))
620 return TokError("expected a comma");
625 if (getParser().parseIdentifier(Name))
626 return TokError("expected identifier in directive");
628 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
630 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
632 getStreamer().EmitWeakReference(Alias, Sym);
636 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
637 const MCExpr *Subsection = 0;
638 if (getLexer().isNot(AsmToken::EndOfStatement)) {
639 if (getParser().parseExpression(Subsection))
643 if (getLexer().isNot(AsmToken::EndOfStatement))
644 return TokError("unexpected token in directive");
646 getStreamer().SubSection(Subsection);
652 MCAsmParserExtension *createELFAsmParser() {
653 return new ELFAsmParser;