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::ParseDirectiveVersion>(".version");
68 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
69 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
70 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
72 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
74 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
76 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
79 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
80 // the best way for us to get access to it?
81 bool ParseSectionDirectiveData(StringRef, SMLoc) {
82 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
83 ELF::SHF_WRITE |ELF::SHF_ALLOC,
84 SectionKind::getDataRel());
86 bool ParseSectionDirectiveText(StringRef, SMLoc) {
87 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
89 ELF::SHF_ALLOC, SectionKind::getText());
91 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
92 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
94 ELF::SHF_ALLOC, SectionKind::getBSS());
96 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
97 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
99 SectionKind::getReadOnly());
101 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
102 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
104 ELF::SHF_TLS | ELF::SHF_WRITE,
105 SectionKind::getThreadData());
107 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
108 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
110 ELF::SHF_TLS | ELF::SHF_WRITE,
111 SectionKind::getThreadBSS());
113 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
114 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
117 SectionKind::getDataRel());
119 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
120 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
123 SectionKind::getReadOnlyWithRel());
125 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
126 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
129 SectionKind::getReadOnlyWithRelLocal());
131 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
132 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
135 SectionKind::getDataRel());
137 bool ParseDirectivePushSection(StringRef, SMLoc);
138 bool ParseDirectivePopSection(StringRef, SMLoc);
139 bool ParseDirectiveSection(StringRef, SMLoc);
140 bool ParseDirectiveSize(StringRef, SMLoc);
141 bool ParseDirectivePrevious(StringRef, SMLoc);
142 bool ParseDirectiveType(StringRef, SMLoc);
143 bool ParseDirectiveIdent(StringRef, SMLoc);
144 bool ParseDirectiveSymver(StringRef, SMLoc);
145 bool ParseDirectiveVersion(StringRef, SMLoc);
146 bool ParseDirectiveWeakref(StringRef, SMLoc);
147 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
150 bool ParseSectionName(StringRef &SectionName);
155 /// ParseDirectiveSymbolAttribute
156 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
157 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
158 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
159 .Case(".weak", MCSA_Weak)
160 .Case(".local", MCSA_Local)
161 .Case(".hidden", MCSA_Hidden)
162 .Case(".internal", MCSA_Internal)
163 .Case(".protected", MCSA_Protected)
164 .Default(MCSA_Invalid);
165 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
166 if (getLexer().isNot(AsmToken::EndOfStatement)) {
170 if (getParser().ParseIdentifier(Name))
171 return TokError("expected identifier in directive");
173 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
175 getStreamer().EmitSymbolAttribute(Sym, Attr);
177 if (getLexer().is(AsmToken::EndOfStatement))
180 if (getLexer().isNot(AsmToken::Comma))
181 return TokError("unexpected token in directive");
190 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
191 unsigned Flags, SectionKind Kind) {
192 if (getLexer().isNot(AsmToken::EndOfStatement))
193 return TokError("unexpected token in section switching directive");
196 getStreamer().SwitchSection(getContext().getELFSection(
197 Section, Type, Flags, Kind));
202 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
204 if (getParser().ParseIdentifier(Name))
205 return TokError("expected identifier in directive");
206 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
208 if (getLexer().isNot(AsmToken::Comma))
209 return TokError("unexpected token in directive");
213 if (getParser().ParseExpression(Expr))
216 if (getLexer().isNot(AsmToken::EndOfStatement))
217 return TokError("unexpected token in directive");
219 getStreamer().EmitELFSize(Sym, Expr);
223 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
224 // A section name can contain -, so we cannot just use
226 SMLoc FirstLoc = getLexer().getLoc();
229 if (getLexer().is(AsmToken::String)) {
230 SectionName = getTok().getIdentifier();
239 SMLoc PrevLoc = getLexer().getLoc();
240 if (getLexer().is(AsmToken::Minus)) {
242 Lex(); // Consume the "-".
243 } else if (getLexer().is(AsmToken::String)) {
244 CurSize = getTok().getIdentifier().size() + 2;
246 } else if (getLexer().is(AsmToken::Identifier)) {
247 CurSize = getTok().getIdentifier().size();
254 SectionName = StringRef(FirstLoc.getPointer(), Size);
256 // Make sure the following token is adjacent.
257 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
266 static SectionKind computeSectionKind(unsigned Flags) {
267 if (Flags & ELF::SHF_EXECINSTR)
268 return SectionKind::getText();
269 if (Flags & ELF::SHF_TLS)
270 return SectionKind::getThreadData();
271 return SectionKind::getDataRel();
274 static int parseSectionFlags(StringRef flagsStr) {
277 for (unsigned i = 0; i < flagsStr.size(); i++) {
278 switch (flagsStr[i]) {
280 flags |= ELF::SHF_ALLOC;
283 flags |= ELF::SHF_EXECINSTR;
286 flags |= ELF::SHF_WRITE;
289 flags |= ELF::SHF_MERGE;
292 flags |= ELF::SHF_STRINGS;
295 flags |= ELF::SHF_TLS;
298 flags |= ELF::XCORE_SHF_CP_SECTION;
301 flags |= ELF::XCORE_SHF_DP_SECTION;
304 flags |= ELF::SHF_GROUP;
314 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
315 getStreamer().PushSection();
317 if (ParseDirectiveSection(s, loc)) {
318 getStreamer().PopSection();
325 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
326 if (!getStreamer().PopSection())
327 return TokError(".popsection without corresponding .pushsection");
331 // FIXME: This is a work in progress.
332 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
333 StringRef SectionName;
335 if (ParseSectionName(SectionName))
336 return TokError("expected identifier in directive");
343 // Set the defaults first.
344 if (SectionName == ".fini" || SectionName == ".init" ||
345 SectionName == ".rodata")
346 Flags |= ELF::SHF_ALLOC;
347 if (SectionName == ".fini" || SectionName == ".init")
348 Flags |= ELF::SHF_EXECINSTR;
350 if (getLexer().is(AsmToken::Comma)) {
353 if (getLexer().isNot(AsmToken::String))
354 return TokError("expected string in directive");
356 StringRef FlagsStr = getTok().getStringContents();
359 int extraFlags = parseSectionFlags(FlagsStr);
361 return TokError("unknown flag");
364 bool Mergeable = Flags & ELF::SHF_MERGE;
365 bool Group = Flags & ELF::SHF_GROUP;
367 if (getLexer().isNot(AsmToken::Comma)) {
369 return TokError("Mergeable section must specify the type");
371 return TokError("Group section must specify the type");
374 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
375 return TokError("expected '@' or '%' before type");
378 if (getParser().ParseIdentifier(TypeName))
379 return TokError("expected identifier in directive");
382 if (getLexer().isNot(AsmToken::Comma))
383 return TokError("expected the entry size");
385 if (getParser().ParseAbsoluteExpression(Size))
388 return TokError("entry size must be positive");
392 if (getLexer().isNot(AsmToken::Comma))
393 return TokError("expected group name");
395 if (getParser().ParseIdentifier(GroupName))
397 if (getLexer().is(AsmToken::Comma)) {
400 if (getParser().ParseIdentifier(Linkage))
402 if (Linkage != "comdat")
403 return TokError("Linkage must be 'comdat'");
409 if (getLexer().isNot(AsmToken::EndOfStatement))
410 return TokError("unexpected token in directive");
412 unsigned Type = ELF::SHT_PROGBITS;
414 if (!TypeName.empty()) {
415 if (TypeName == "init_array")
416 Type = ELF::SHT_INIT_ARRAY;
417 else if (TypeName == "fini_array")
418 Type = ELF::SHT_FINI_ARRAY;
419 else if (TypeName == "preinit_array")
420 Type = ELF::SHT_PREINIT_ARRAY;
421 else if (TypeName == "nobits")
422 Type = ELF::SHT_NOBITS;
423 else if (TypeName == "progbits")
424 Type = ELF::SHT_PROGBITS;
425 else if (TypeName == "note")
426 Type = ELF::SHT_NOTE;
427 else if (TypeName == "unwind")
428 Type = ELF::SHT_X86_64_UNWIND;
430 return TokError("unknown section type");
433 SectionKind Kind = computeSectionKind(Flags);
434 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
440 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
441 const MCSection *PreviousSection = getStreamer().getPreviousSection();
442 if (PreviousSection == NULL)
443 return TokError(".previous without corresponding .section");
444 getStreamer().SwitchSection(PreviousSection);
449 /// ParseDirectiveELFType
450 /// ::= .type identifier , @attribute
451 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
453 if (getParser().ParseIdentifier(Name))
454 return TokError("expected identifier in directive");
456 // Handle the identifier as the key symbol.
457 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
459 if (getLexer().isNot(AsmToken::Comma))
460 return TokError("unexpected token in '.type' directive");
463 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
464 return TokError("expected '@' or '%' before type");
470 TypeLoc = getLexer().getLoc();
471 if (getParser().ParseIdentifier(Type))
472 return TokError("expected symbol type in directive");
474 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
475 .Case("function", MCSA_ELF_TypeFunction)
476 .Case("object", MCSA_ELF_TypeObject)
477 .Case("tls_object", MCSA_ELF_TypeTLS)
478 .Case("common", MCSA_ELF_TypeCommon)
479 .Case("notype", MCSA_ELF_TypeNoType)
480 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
481 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
482 .Default(MCSA_Invalid);
484 if (Attr == MCSA_Invalid)
485 return Error(TypeLoc, "unsupported attribute in '.type' directive");
487 if (getLexer().isNot(AsmToken::EndOfStatement))
488 return TokError("unexpected token in '.type' directive");
492 getStreamer().EmitSymbolAttribute(Sym, Attr);
497 /// ParseDirectiveIdent
498 /// ::= .ident string
499 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
500 if (getLexer().isNot(AsmToken::String))
501 return TokError("unexpected token in '.ident' directive");
503 StringRef Data = getTok().getIdentifier();
507 const MCSection *Comment =
508 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
511 SectionKind::getReadOnly(),
514 getStreamer().PushSection();
515 getStreamer().SwitchSection(Comment);
517 getStreamer().EmitIntValue(0, 1);
520 getStreamer().EmitBytes(Data, 0);
521 getStreamer().EmitIntValue(0, 1);
522 getStreamer().PopSection();
526 /// ParseDirectiveSymver
527 /// ::= .symver foo, bar2@zed
528 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
530 if (getParser().ParseIdentifier(Name))
531 return TokError("expected identifier in directive");
533 if (getLexer().isNot(AsmToken::Comma))
534 return TokError("expected a comma");
539 if (getParser().ParseIdentifier(AliasName))
540 return TokError("expected identifier in directive");
542 if (AliasName.find('@') == StringRef::npos)
543 return TokError("expected a '@' in the name");
545 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
546 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
547 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
549 getStreamer().EmitAssignment(Alias, Value);
553 /// ParseDirectiveVersion
554 /// ::= .version string
555 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
556 if (getLexer().isNot(AsmToken::String))
557 return TokError("unexpected token in '.version' directive");
559 StringRef Data = getTok().getIdentifier();
563 const MCSection *Note =
564 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
565 SectionKind::getReadOnly());
567 getStreamer().PushSection();
568 getStreamer().SwitchSection(Note);
569 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
570 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
571 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
572 getStreamer().EmitBytes(Data, 0); // name.
573 getStreamer().EmitIntValue(0, 1); // terminate the string.
574 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
575 getStreamer().PopSection();
579 /// ParseDirectiveWeakref
580 /// ::= .weakref foo, bar
581 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
582 // FIXME: Share code with the other alias building directives.
585 if (getParser().ParseIdentifier(AliasName))
586 return TokError("expected identifier in directive");
588 if (getLexer().isNot(AsmToken::Comma))
589 return TokError("expected a comma");
594 if (getParser().ParseIdentifier(Name))
595 return TokError("expected identifier in directive");
597 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
599 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
601 getStreamer().EmitWeakReference(Alias, Sym);
607 MCAsmParserExtension *createELFAsmParser() {
608 return new ELFAsmParser;