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");
81 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
82 // the best way for us to get access to it?
83 bool ParseSectionDirectiveData(StringRef, SMLoc) {
84 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
85 ELF::SHF_WRITE |ELF::SHF_ALLOC,
86 SectionKind::getDataRel());
88 bool ParseSectionDirectiveText(StringRef, SMLoc) {
89 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
91 ELF::SHF_ALLOC, SectionKind::getText());
93 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
94 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
96 ELF::SHF_ALLOC, SectionKind::getBSS());
98 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
99 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
101 SectionKind::getReadOnly());
103 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
104 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
106 ELF::SHF_TLS | ELF::SHF_WRITE,
107 SectionKind::getThreadData());
109 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
110 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
112 ELF::SHF_TLS | ELF::SHF_WRITE,
113 SectionKind::getThreadBSS());
115 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
116 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
119 SectionKind::getDataRel());
121 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
122 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
125 SectionKind::getReadOnlyWithRel());
127 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
128 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
131 SectionKind::getReadOnlyWithRelLocal());
133 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
134 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
137 SectionKind::getDataRel());
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);
152 bool ParseSectionName(StringRef &SectionName);
157 /// ParseDirectiveSymbolAttribute
158 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
159 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
160 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
161 .Case(".weak", MCSA_Weak)
162 .Case(".local", MCSA_Local)
163 .Case(".hidden", MCSA_Hidden)
164 .Case(".internal", MCSA_Internal)
165 .Case(".protected", MCSA_Protected)
166 .Default(MCSA_Invalid);
167 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
168 if (getLexer().isNot(AsmToken::EndOfStatement)) {
172 if (getParser().ParseIdentifier(Name))
173 return TokError("expected identifier in directive");
175 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
177 getStreamer().EmitSymbolAttribute(Sym, Attr);
179 if (getLexer().is(AsmToken::EndOfStatement))
182 if (getLexer().isNot(AsmToken::Comma))
183 return TokError("unexpected token in directive");
192 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
193 unsigned Flags, SectionKind Kind) {
194 if (getLexer().isNot(AsmToken::EndOfStatement))
195 return TokError("unexpected token in section switching directive");
198 getStreamer().SwitchSection(getContext().getELFSection(
199 Section, Type, Flags, Kind));
204 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
206 if (getParser().ParseIdentifier(Name))
207 return TokError("expected identifier in directive");
208 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
210 if (getLexer().isNot(AsmToken::Comma))
211 return TokError("unexpected token in directive");
215 if (getParser().ParseExpression(Expr))
218 if (getLexer().isNot(AsmToken::EndOfStatement))
219 return TokError("unexpected token in directive");
221 getStreamer().EmitELFSize(Sym, Expr);
225 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
226 // A section name can contain -, so we cannot just use
228 SMLoc FirstLoc = getLexer().getLoc();
231 if (getLexer().is(AsmToken::String)) {
232 SectionName = getTok().getIdentifier();
241 SMLoc PrevLoc = getLexer().getLoc();
242 if (getLexer().is(AsmToken::Minus)) {
244 Lex(); // Consume the "-".
245 } else if (getLexer().is(AsmToken::String)) {
246 CurSize = getTok().getIdentifier().size() + 2;
248 } else if (getLexer().is(AsmToken::Identifier)) {
249 CurSize = getTok().getIdentifier().size();
256 SectionName = StringRef(FirstLoc.getPointer(), Size);
258 // Make sure the following token is adjacent.
259 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
268 static SectionKind computeSectionKind(unsigned Flags) {
269 if (Flags & ELF::SHF_EXECINSTR)
270 return SectionKind::getText();
271 if (Flags & ELF::SHF_TLS)
272 return SectionKind::getThreadData();
273 return SectionKind::getDataRel();
276 static int parseSectionFlags(StringRef flagsStr) {
279 for (unsigned i = 0; i < flagsStr.size(); i++) {
280 switch (flagsStr[i]) {
282 flags |= ELF::SHF_ALLOC;
285 flags |= ELF::SHF_EXECINSTR;
288 flags |= ELF::SHF_WRITE;
291 flags |= ELF::SHF_MERGE;
294 flags |= ELF::SHF_STRINGS;
297 flags |= ELF::SHF_TLS;
300 flags |= ELF::XCORE_SHF_CP_SECTION;
303 flags |= ELF::XCORE_SHF_DP_SECTION;
306 flags |= ELF::SHF_GROUP;
316 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
317 getStreamer().PushSection();
319 if (ParseDirectiveSection(s, loc)) {
320 getStreamer().PopSection();
327 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
328 if (!getStreamer().PopSection())
329 return TokError(".popsection without corresponding .pushsection");
333 // FIXME: This is a work in progress.
334 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
335 StringRef SectionName;
337 if (ParseSectionName(SectionName))
338 return TokError("expected identifier in directive");
345 // Set the defaults first.
346 if (SectionName == ".fini" || SectionName == ".init" ||
347 SectionName == ".rodata")
348 Flags |= ELF::SHF_ALLOC;
349 if (SectionName == ".fini" || SectionName == ".init")
350 Flags |= ELF::SHF_EXECINSTR;
352 if (getLexer().is(AsmToken::Comma)) {
355 if (getLexer().isNot(AsmToken::String))
356 return TokError("expected string in directive");
358 StringRef FlagsStr = getTok().getStringContents();
361 int extraFlags = parseSectionFlags(FlagsStr);
363 return TokError("unknown flag");
366 bool Mergeable = Flags & ELF::SHF_MERGE;
367 bool Group = Flags & ELF::SHF_GROUP;
369 if (getLexer().isNot(AsmToken::Comma)) {
371 return TokError("Mergeable section must specify the type");
373 return TokError("Group section must specify the type");
376 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
377 return TokError("expected '@' or '%' before type");
380 if (getParser().ParseIdentifier(TypeName))
381 return TokError("expected identifier in directive");
384 if (getLexer().isNot(AsmToken::Comma))
385 return TokError("expected the entry size");
387 if (getParser().ParseAbsoluteExpression(Size))
390 return TokError("entry size must be positive");
394 if (getLexer().isNot(AsmToken::Comma))
395 return TokError("expected group name");
397 if (getParser().ParseIdentifier(GroupName))
399 if (getLexer().is(AsmToken::Comma)) {
402 if (getParser().ParseIdentifier(Linkage))
404 if (Linkage != "comdat")
405 return TokError("Linkage must be 'comdat'");
411 if (getLexer().isNot(AsmToken::EndOfStatement))
412 return TokError("unexpected token in directive");
414 unsigned Type = ELF::SHT_PROGBITS;
416 if (!TypeName.empty()) {
417 if (TypeName == "init_array")
418 Type = ELF::SHT_INIT_ARRAY;
419 else if (TypeName == "fini_array")
420 Type = ELF::SHT_FINI_ARRAY;
421 else if (TypeName == "preinit_array")
422 Type = ELF::SHT_PREINIT_ARRAY;
423 else if (TypeName == "nobits")
424 Type = ELF::SHT_NOBITS;
425 else if (TypeName == "progbits")
426 Type = ELF::SHT_PROGBITS;
427 else if (TypeName == "note")
428 Type = ELF::SHT_NOTE;
429 else if (TypeName == "unwind")
430 Type = ELF::SHT_X86_64_UNWIND;
432 return TokError("unknown section type");
435 SectionKind Kind = computeSectionKind(Flags);
436 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
442 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
443 const MCSection *PreviousSection = getStreamer().getPreviousSection();
444 if (PreviousSection == NULL)
445 return TokError(".previous without corresponding .section");
446 getStreamer().SwitchSection(PreviousSection);
451 /// ParseDirectiveELFType
452 /// ::= .type identifier , @attribute
453 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
455 if (getParser().ParseIdentifier(Name))
456 return TokError("expected identifier in directive");
458 // Handle the identifier as the key symbol.
459 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
461 if (getLexer().isNot(AsmToken::Comma))
462 return TokError("unexpected token in '.type' directive");
465 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
466 return TokError("expected '@' or '%' before type");
472 TypeLoc = getLexer().getLoc();
473 if (getParser().ParseIdentifier(Type))
474 return TokError("expected symbol type in directive");
476 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
477 .Case("function", MCSA_ELF_TypeFunction)
478 .Case("object", MCSA_ELF_TypeObject)
479 .Case("tls_object", MCSA_ELF_TypeTLS)
480 .Case("common", MCSA_ELF_TypeCommon)
481 .Case("notype", MCSA_ELF_TypeNoType)
482 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
483 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
484 .Default(MCSA_Invalid);
486 if (Attr == MCSA_Invalid)
487 return Error(TypeLoc, "unsupported attribute in '.type' directive");
489 if (getLexer().isNot(AsmToken::EndOfStatement))
490 return TokError("unexpected token in '.type' directive");
494 getStreamer().EmitSymbolAttribute(Sym, Attr);
499 /// ParseDirectiveIdent
500 /// ::= .ident string
501 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
502 if (getLexer().isNot(AsmToken::String))
503 return TokError("unexpected token in '.ident' directive");
505 StringRef Data = getTok().getIdentifier();
509 const MCSection *Comment =
510 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
513 SectionKind::getReadOnly(),
516 getStreamer().PushSection();
517 getStreamer().SwitchSection(Comment);
519 getStreamer().EmitIntValue(0, 1);
522 getStreamer().EmitBytes(Data);
523 getStreamer().EmitIntValue(0, 1);
524 getStreamer().PopSection();
528 /// ParseDirectiveSymver
529 /// ::= .symver foo, bar2@zed
530 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
532 if (getParser().ParseIdentifier(Name))
533 return TokError("expected identifier in directive");
535 if (getLexer().isNot(AsmToken::Comma))
536 return TokError("expected a comma");
541 if (getParser().ParseIdentifier(AliasName))
542 return TokError("expected identifier in directive");
544 if (AliasName.find('@') == StringRef::npos)
545 return TokError("expected a '@' in the name");
547 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
548 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
549 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
551 getStreamer().EmitAssignment(Alias, Value);
555 /// ParseDirectiveVersion
556 /// ::= .version string
557 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
558 if (getLexer().isNot(AsmToken::String))
559 return TokError("unexpected token in '.version' directive");
561 StringRef Data = getTok().getIdentifier();
565 const MCSection *Note =
566 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
567 SectionKind::getReadOnly());
569 getStreamer().PushSection();
570 getStreamer().SwitchSection(Note);
571 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
572 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
573 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
574 getStreamer().EmitBytes(Data); // name.
575 getStreamer().EmitIntValue(0, 1); // terminate the string.
576 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
577 getStreamer().PopSection();
581 /// ParseDirectiveWeakref
582 /// ::= .weakref foo, bar
583 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
584 // FIXME: Share code with the other alias building directives.
587 if (getParser().ParseIdentifier(AliasName))
588 return TokError("expected identifier in directive");
590 if (getLexer().isNot(AsmToken::Comma))
591 return TokError("expected a comma");
596 if (getParser().ParseIdentifier(Name))
597 return TokError("expected identifier in directive");
599 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
601 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
603 getStreamer().EmitWeakReference(Alias, Sym);
609 MCAsmParserExtension *createELFAsmParser() {
610 return new ELFAsmParser;