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 (SectionName.startswith(".note"))
418 Type = ELF::SHT_NOTE;
419 else if (SectionName == ".init_array")
420 Type = ELF::SHT_INIT_ARRAY;
421 else if (SectionName == ".fini_array")
422 Type = ELF::SHT_FINI_ARRAY;
423 else if (SectionName == ".preinit_array")
424 Type = ELF::SHT_PREINIT_ARRAY;
426 if (TypeName == "init_array")
427 Type = ELF::SHT_INIT_ARRAY;
428 else if (TypeName == "fini_array")
429 Type = ELF::SHT_FINI_ARRAY;
430 else if (TypeName == "preinit_array")
431 Type = ELF::SHT_PREINIT_ARRAY;
432 else if (TypeName == "nobits")
433 Type = ELF::SHT_NOBITS;
434 else if (TypeName == "progbits")
435 Type = ELF::SHT_PROGBITS;
436 else if (TypeName == "note")
437 Type = ELF::SHT_NOTE;
438 else if (TypeName == "unwind")
439 Type = ELF::SHT_X86_64_UNWIND;
441 return TokError("unknown section type");
444 SectionKind Kind = computeSectionKind(Flags);
445 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
451 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
452 const MCSection *PreviousSection = getStreamer().getPreviousSection();
453 if (PreviousSection == NULL)
454 return TokError(".previous without corresponding .section");
455 getStreamer().SwitchSection(PreviousSection);
460 /// ParseDirectiveELFType
461 /// ::= .type identifier , @attribute
462 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
464 if (getParser().ParseIdentifier(Name))
465 return TokError("expected identifier in directive");
467 // Handle the identifier as the key symbol.
468 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
470 if (getLexer().isNot(AsmToken::Comma))
471 return TokError("unexpected token in '.type' directive");
474 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
475 return TokError("expected '@' or '%' before type");
481 TypeLoc = getLexer().getLoc();
482 if (getParser().ParseIdentifier(Type))
483 return TokError("expected symbol type in directive");
485 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
486 .Case("function", MCSA_ELF_TypeFunction)
487 .Case("object", MCSA_ELF_TypeObject)
488 .Case("tls_object", MCSA_ELF_TypeTLS)
489 .Case("common", MCSA_ELF_TypeCommon)
490 .Case("notype", MCSA_ELF_TypeNoType)
491 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
492 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
493 .Default(MCSA_Invalid);
495 if (Attr == MCSA_Invalid)
496 return Error(TypeLoc, "unsupported attribute in '.type' directive");
498 if (getLexer().isNot(AsmToken::EndOfStatement))
499 return TokError("unexpected token in '.type' directive");
503 getStreamer().EmitSymbolAttribute(Sym, Attr);
508 /// ParseDirectiveIdent
509 /// ::= .ident string
510 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
511 if (getLexer().isNot(AsmToken::String))
512 return TokError("unexpected token in '.ident' directive");
514 StringRef Data = getTok().getIdentifier();
518 const MCSection *Comment =
519 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
522 SectionKind::getReadOnly(),
525 getStreamer().PushSection();
526 getStreamer().SwitchSection(Comment);
528 getStreamer().EmitIntValue(0, 1);
531 getStreamer().EmitBytes(Data);
532 getStreamer().EmitIntValue(0, 1);
533 getStreamer().PopSection();
537 /// ParseDirectiveSymver
538 /// ::= .symver foo, bar2@zed
539 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
541 if (getParser().ParseIdentifier(Name))
542 return TokError("expected identifier in directive");
544 if (getLexer().isNot(AsmToken::Comma))
545 return TokError("expected a comma");
550 if (getParser().ParseIdentifier(AliasName))
551 return TokError("expected identifier in directive");
553 if (AliasName.find('@') == StringRef::npos)
554 return TokError("expected a '@' in the name");
556 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
557 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
558 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
560 getStreamer().EmitAssignment(Alias, Value);
564 /// ParseDirectiveVersion
565 /// ::= .version string
566 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
567 if (getLexer().isNot(AsmToken::String))
568 return TokError("unexpected token in '.version' directive");
570 StringRef Data = getTok().getIdentifier();
574 const MCSection *Note =
575 getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
576 SectionKind::getReadOnly());
578 getStreamer().PushSection();
579 getStreamer().SwitchSection(Note);
580 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
581 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description).
582 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION.
583 getStreamer().EmitBytes(Data); // name.
584 getStreamer().EmitIntValue(0, 1); // terminate the string.
585 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment.
586 getStreamer().PopSection();
590 /// ParseDirectiveWeakref
591 /// ::= .weakref foo, bar
592 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
593 // FIXME: Share code with the other alias building directives.
596 if (getParser().ParseIdentifier(AliasName))
597 return TokError("expected identifier in directive");
599 if (getLexer().isNot(AsmToken::Comma))
600 return TokError("expected a comma");
605 if (getParser().ParseIdentifier(Name))
606 return TokError("expected identifier in directive");
608 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
610 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
612 getStreamer().EmitWeakReference(Alias, Sym);
618 MCAsmParserExtension *createELFAsmParser() {
619 return new ELFAsmParser;