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::*Handler)(StringRef, SMLoc)>
26 void AddDirectiveHandler(StringRef Directive) {
27 getParser().AddDirectiveHandler(this, Directive,
28 HandleDirective<ELFAsmParser, Handler>);
31 bool ParseSectionSwitch(StringRef Section, unsigned Type,
32 unsigned Flags, SectionKind Kind);
36 ELFAsmParser() : SeenIdent(false) {
37 BracketExpressionsSupported = true;
40 virtual void Initialize(MCAsmParser &Parser) {
41 // Call the base implementation.
42 this->MCAsmParserExtension::Initialize(Parser);
44 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
45 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
46 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
48 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
49 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
51 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
53 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
55 &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
57 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
58 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
60 &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
61 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
62 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
63 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
64 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
65 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
66 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
67 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
68 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
69 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
71 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
73 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
75 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
78 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
79 // the best way for us to get access to it?
80 bool ParseSectionDirectiveData(StringRef, SMLoc) {
81 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
82 ELF::SHF_WRITE |ELF::SHF_ALLOC,
83 SectionKind::getDataRel());
85 bool ParseSectionDirectiveText(StringRef, SMLoc) {
86 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
88 ELF::SHF_ALLOC, SectionKind::getText());
90 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
91 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
93 ELF::SHF_ALLOC, SectionKind::getBSS());
95 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
96 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
98 SectionKind::getReadOnly());
100 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
101 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
103 ELF::SHF_TLS | ELF::SHF_WRITE,
104 SectionKind::getThreadData());
106 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
107 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
109 ELF::SHF_TLS | ELF::SHF_WRITE,
110 SectionKind::getThreadBSS());
112 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
113 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
116 SectionKind::getDataRel());
118 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
119 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
122 SectionKind::getReadOnlyWithRel());
124 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
125 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
128 SectionKind::getReadOnlyWithRelLocal());
130 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
131 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
134 SectionKind::getDataRel());
136 bool ParseDirectivePushSection(StringRef, SMLoc);
137 bool ParseDirectivePopSection(StringRef, SMLoc);
138 bool ParseDirectiveSection(StringRef, SMLoc);
139 bool ParseDirectiveSize(StringRef, SMLoc);
140 bool ParseDirectivePrevious(StringRef, SMLoc);
141 bool ParseDirectiveType(StringRef, SMLoc);
142 bool ParseDirectiveIdent(StringRef, SMLoc);
143 bool ParseDirectiveSymver(StringRef, SMLoc);
144 bool ParseDirectiveWeakref(StringRef, SMLoc);
145 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
148 bool ParseSectionName(StringRef &SectionName);
153 /// ParseDirectiveSymbolAttribute
154 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
155 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
156 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
157 .Case(".weak", MCSA_Weak)
158 .Case(".local", MCSA_Local)
159 .Case(".hidden", MCSA_Hidden)
160 .Case(".internal", MCSA_Internal)
161 .Case(".protected", MCSA_Protected)
162 .Default(MCSA_Invalid);
163 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
164 if (getLexer().isNot(AsmToken::EndOfStatement)) {
168 if (getParser().ParseIdentifier(Name))
169 return TokError("expected identifier in directive");
171 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
173 getStreamer().EmitSymbolAttribute(Sym, Attr);
175 if (getLexer().is(AsmToken::EndOfStatement))
178 if (getLexer().isNot(AsmToken::Comma))
179 return TokError("unexpected token in directive");
188 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
189 unsigned Flags, SectionKind Kind) {
190 if (getLexer().isNot(AsmToken::EndOfStatement))
191 return TokError("unexpected token in section switching directive");
194 getStreamer().SwitchSection(getContext().getELFSection(
195 Section, Type, Flags, Kind));
200 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
202 if (getParser().ParseIdentifier(Name))
203 return TokError("expected identifier in directive");
204 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
206 if (getLexer().isNot(AsmToken::Comma))
207 return TokError("unexpected token in directive");
211 if (getParser().ParseExpression(Expr))
214 if (getLexer().isNot(AsmToken::EndOfStatement))
215 return TokError("unexpected token in directive");
217 getStreamer().EmitELFSize(Sym, Expr);
221 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
222 // A section name can contain -, so we cannot just use
224 SMLoc FirstLoc = getLexer().getLoc();
227 if (getLexer().is(AsmToken::String)) {
228 SectionName = getTok().getIdentifier();
237 SMLoc PrevLoc = getLexer().getLoc();
238 if (getLexer().is(AsmToken::Minus)) {
240 Lex(); // Consume the "-".
241 } else if (getLexer().is(AsmToken::String)) {
242 CurSize = getTok().getIdentifier().size() + 2;
244 } else if (getLexer().is(AsmToken::Identifier)) {
245 CurSize = getTok().getIdentifier().size();
252 SectionName = StringRef(FirstLoc.getPointer(), Size);
254 // Make sure the following token is adjacent.
255 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
264 static SectionKind computeSectionKind(unsigned Flags) {
265 if (Flags & ELF::SHF_EXECINSTR)
266 return SectionKind::getText();
267 if (Flags & ELF::SHF_TLS)
268 return SectionKind::getThreadData();
269 return SectionKind::getDataRel();
272 static int parseSectionFlags(StringRef flagsStr) {
275 for (unsigned i = 0; i < flagsStr.size(); i++) {
276 switch (flagsStr[i]) {
278 flags |= ELF::SHF_ALLOC;
281 flags |= ELF::SHF_EXECINSTR;
284 flags |= ELF::SHF_WRITE;
287 flags |= ELF::SHF_MERGE;
290 flags |= ELF::SHF_STRINGS;
293 flags |= ELF::SHF_TLS;
296 flags |= ELF::XCORE_SHF_CP_SECTION;
299 flags |= ELF::XCORE_SHF_DP_SECTION;
302 flags |= ELF::SHF_GROUP;
312 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
313 getStreamer().PushSection();
315 if (ParseDirectiveSection(s, loc)) {
316 getStreamer().PopSection();
323 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
324 if (!getStreamer().PopSection())
325 return TokError(".popsection without corresponding .pushsection");
329 // FIXME: This is a work in progress.
330 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
331 StringRef SectionName;
333 if (ParseSectionName(SectionName))
334 return TokError("expected identifier in directive");
341 // Set the defaults first.
342 if (SectionName == ".fini" || SectionName == ".init" ||
343 SectionName == ".rodata")
344 Flags |= ELF::SHF_ALLOC;
345 if (SectionName == ".fini" || SectionName == ".init")
346 Flags |= ELF::SHF_EXECINSTR;
348 if (getLexer().is(AsmToken::Comma)) {
351 if (getLexer().isNot(AsmToken::String))
352 return TokError("expected string in directive");
354 StringRef FlagsStr = getTok().getStringContents();
357 int extraFlags = parseSectionFlags(FlagsStr);
359 return TokError("unknown flag");
362 bool Mergeable = Flags & ELF::SHF_MERGE;
363 bool Group = Flags & ELF::SHF_GROUP;
365 if (getLexer().isNot(AsmToken::Comma)) {
367 return TokError("Mergeable section must specify the type");
369 return TokError("Group section must specify the type");
372 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
373 return TokError("expected '@' or '%' before type");
376 if (getParser().ParseIdentifier(TypeName))
377 return TokError("expected identifier in directive");
380 if (getLexer().isNot(AsmToken::Comma))
381 return TokError("expected the entry size");
383 if (getParser().ParseAbsoluteExpression(Size))
386 return TokError("entry size must be positive");
390 if (getLexer().isNot(AsmToken::Comma))
391 return TokError("expected group name");
393 if (getParser().ParseIdentifier(GroupName))
395 if (getLexer().is(AsmToken::Comma)) {
398 if (getParser().ParseIdentifier(Linkage))
400 if (Linkage != "comdat")
401 return TokError("Linkage must be 'comdat'");
407 if (getLexer().isNot(AsmToken::EndOfStatement))
408 return TokError("unexpected token in directive");
410 unsigned Type = ELF::SHT_PROGBITS;
412 if (!TypeName.empty()) {
413 if (TypeName == "init_array")
414 Type = ELF::SHT_INIT_ARRAY;
415 else if (TypeName == "fini_array")
416 Type = ELF::SHT_FINI_ARRAY;
417 else if (TypeName == "preinit_array")
418 Type = ELF::SHT_PREINIT_ARRAY;
419 else if (TypeName == "nobits")
420 Type = ELF::SHT_NOBITS;
421 else if (TypeName == "progbits")
422 Type = ELF::SHT_PROGBITS;
423 else if (TypeName == "note")
424 Type = ELF::SHT_NOTE;
425 else if (TypeName == "unwind")
426 Type = ELF::SHT_X86_64_UNWIND;
428 return TokError("unknown section type");
431 SectionKind Kind = computeSectionKind(Flags);
432 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
438 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
439 const MCSection *PreviousSection = getStreamer().getPreviousSection();
440 if (PreviousSection == NULL)
441 return TokError(".previous without corresponding .section");
442 getStreamer().SwitchSection(PreviousSection);
447 /// ParseDirectiveELFType
448 /// ::= .type identifier , @attribute
449 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
451 if (getParser().ParseIdentifier(Name))
452 return TokError("expected identifier in directive");
454 // Handle the identifier as the key symbol.
455 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
457 if (getLexer().isNot(AsmToken::Comma))
458 return TokError("unexpected token in '.type' directive");
461 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
462 return TokError("expected '@' or '%' before type");
468 TypeLoc = getLexer().getLoc();
469 if (getParser().ParseIdentifier(Type))
470 return TokError("expected symbol type in directive");
472 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
473 .Case("function", MCSA_ELF_TypeFunction)
474 .Case("object", MCSA_ELF_TypeObject)
475 .Case("tls_object", MCSA_ELF_TypeTLS)
476 .Case("common", MCSA_ELF_TypeCommon)
477 .Case("notype", MCSA_ELF_TypeNoType)
478 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
479 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
480 .Default(MCSA_Invalid);
482 if (Attr == MCSA_Invalid)
483 return Error(TypeLoc, "unsupported attribute in '.type' directive");
485 if (getLexer().isNot(AsmToken::EndOfStatement))
486 return TokError("unexpected token in '.type' directive");
490 getStreamer().EmitSymbolAttribute(Sym, Attr);
495 /// ParseDirectiveIdent
496 /// ::= .ident string
497 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
498 if (getLexer().isNot(AsmToken::String))
499 return TokError("unexpected token in '.ident' directive");
501 StringRef Data = getTok().getIdentifier();
505 const MCSection *Comment =
506 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
509 SectionKind::getReadOnly(),
512 getStreamer().PushSection();
513 getStreamer().SwitchSection(Comment);
515 getStreamer().EmitIntValue(0, 1);
518 getStreamer().EmitBytes(Data, 0);
519 getStreamer().EmitIntValue(0, 1);
520 getStreamer().PopSection();
524 /// ParseDirectiveSymver
525 /// ::= .symver foo, bar2@zed
526 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
528 if (getParser().ParseIdentifier(Name))
529 return TokError("expected identifier in directive");
531 if (getLexer().isNot(AsmToken::Comma))
532 return TokError("expected a comma");
537 if (getParser().ParseIdentifier(AliasName))
538 return TokError("expected identifier in directive");
540 if (AliasName.find('@') == StringRef::npos)
541 return TokError("expected a '@' in the name");
543 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
544 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
545 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
547 getStreamer().EmitAssignment(Alias, Value);
551 /// ParseDirectiveWeakref
552 /// ::= .weakref foo, bar
553 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
554 // FIXME: Share code with the other alias building directives.
557 if (getParser().ParseIdentifier(AliasName))
558 return TokError("expected identifier in directive");
560 if (getLexer().isNot(AsmToken::Comma))
561 return TokError("expected a comma");
566 if (getParser().ParseIdentifier(Name))
567 return TokError("expected identifier in directive");
569 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
571 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
573 getStreamer().EmitWeakReference(Alias, Sym);
579 MCAsmParserExtension *createELFAsmParser() {
580 return new ELFAsmParser;