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");
70 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
71 // the best way for us to get access to it?
72 bool ParseSectionDirectiveData(StringRef, SMLoc) {
73 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
74 ELF::SHF_WRITE |ELF::SHF_ALLOC,
75 SectionKind::getDataRel());
77 bool ParseSectionDirectiveText(StringRef, SMLoc) {
78 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
80 ELF::SHF_ALLOC, SectionKind::getText());
82 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
83 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
85 ELF::SHF_ALLOC, SectionKind::getBSS());
87 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
88 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
90 SectionKind::getReadOnly());
92 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
93 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
95 ELF::SHF_TLS | ELF::SHF_WRITE,
96 SectionKind::getThreadData());
98 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
99 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
101 ELF::SHF_TLS | ELF::SHF_WRITE,
102 SectionKind::getThreadBSS());
104 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
105 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
108 SectionKind::getDataRel());
110 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
111 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
114 SectionKind::getReadOnlyWithRel());
116 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
117 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
120 SectionKind::getReadOnlyWithRelLocal());
122 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
123 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
126 SectionKind::getDataRel());
128 bool ParseDirectivePushSection(StringRef, SMLoc);
129 bool ParseDirectivePopSection(StringRef, SMLoc);
130 bool ParseDirectiveSection(StringRef, SMLoc);
131 bool ParseDirectiveSize(StringRef, SMLoc);
132 bool ParseDirectivePrevious(StringRef, SMLoc);
133 bool ParseDirectiveType(StringRef, SMLoc);
134 bool ParseDirectiveIdent(StringRef, SMLoc);
135 bool ParseDirectiveSymver(StringRef, SMLoc);
136 bool ParseDirectiveWeakref(StringRef, SMLoc);
139 bool ParseSectionName(StringRef &SectionName);
144 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
145 unsigned Flags, SectionKind Kind) {
146 if (getLexer().isNot(AsmToken::EndOfStatement))
147 return TokError("unexpected token in section switching directive");
150 getStreamer().SwitchSection(getContext().getELFSection(
151 Section, Type, Flags, Kind));
156 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
158 if (getParser().ParseIdentifier(Name))
159 return TokError("expected identifier in directive");
160 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
162 if (getLexer().isNot(AsmToken::Comma))
163 return TokError("unexpected token in directive");
167 if (getParser().ParseExpression(Expr))
170 if (getLexer().isNot(AsmToken::EndOfStatement))
171 return TokError("unexpected token in directive");
173 getStreamer().EmitELFSize(Sym, Expr);
177 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
178 // A section name can contain -, so we cannot just use
180 SMLoc FirstLoc = getLexer().getLoc();
183 if (getLexer().is(AsmToken::String)) {
184 SectionName = getTok().getIdentifier();
193 SMLoc PrevLoc = getLexer().getLoc();
194 if (getLexer().is(AsmToken::Minus)) {
196 Lex(); // Consume the "-".
197 } else if (getLexer().is(AsmToken::String)) {
198 CurSize = getTok().getIdentifier().size() + 2;
200 } else if (getLexer().is(AsmToken::Identifier)) {
201 CurSize = getTok().getIdentifier().size();
208 SectionName = StringRef(FirstLoc.getPointer(), Size);
210 // Make sure the following token is adjacent.
211 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
220 static SectionKind computeSectionKind(unsigned Flags) {
221 if (Flags & ELF::SHF_EXECINSTR)
222 return SectionKind::getText();
223 if (Flags & ELF::SHF_TLS)
224 return SectionKind::getThreadData();
225 return SectionKind::getDataRel();
228 static int parseSectionFlags(StringRef flagsStr) {
231 for (unsigned i = 0; i < flagsStr.size(); i++) {
232 switch (flagsStr[i]) {
234 flags |= ELF::SHF_ALLOC;
237 flags |= ELF::SHF_EXECINSTR;
240 flags |= ELF::SHF_WRITE;
243 flags |= ELF::SHF_MERGE;
246 flags |= ELF::SHF_STRINGS;
249 flags |= ELF::SHF_TLS;
252 flags |= ELF::XCORE_SHF_CP_SECTION;
255 flags |= ELF::XCORE_SHF_DP_SECTION;
258 flags |= ELF::SHF_GROUP;
268 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
269 getStreamer().PushSection();
271 if (ParseDirectiveSection(s, loc)) {
272 getStreamer().PopSection();
279 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
280 if (!getStreamer().PopSection())
281 return TokError(".popsection without corresponding .pushsection");
285 // FIXME: This is a work in progress.
286 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
287 StringRef SectionName;
289 if (ParseSectionName(SectionName))
290 return TokError("expected identifier in directive");
297 // Set the defaults first.
298 if (SectionName == ".fini" || SectionName == ".init" ||
299 SectionName == ".rodata")
300 Flags |= ELF::SHF_ALLOC;
301 if (SectionName == ".fini" || SectionName == ".init")
302 Flags |= ELF::SHF_EXECINSTR;
304 if (getLexer().is(AsmToken::Comma)) {
307 if (getLexer().isNot(AsmToken::String))
308 return TokError("expected string in directive");
310 StringRef FlagsStr = getTok().getStringContents();
313 int extraFlags = parseSectionFlags(FlagsStr);
315 return TokError("unknown flag");
318 bool Mergeable = Flags & ELF::SHF_MERGE;
319 bool Group = Flags & ELF::SHF_GROUP;
321 if (getLexer().isNot(AsmToken::Comma)) {
323 return TokError("Mergeable section must specify the type");
325 return TokError("Group section must specify the type");
328 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
329 return TokError("expected '@' or '%' before type");
332 if (getParser().ParseIdentifier(TypeName))
333 return TokError("expected identifier in directive");
336 if (getLexer().isNot(AsmToken::Comma))
337 return TokError("expected the entry size");
339 if (getParser().ParseAbsoluteExpression(Size))
342 return TokError("entry size must be positive");
346 if (getLexer().isNot(AsmToken::Comma))
347 return TokError("expected group name");
349 if (getParser().ParseIdentifier(GroupName))
351 if (getLexer().is(AsmToken::Comma)) {
354 if (getParser().ParseIdentifier(Linkage))
356 if (Linkage != "comdat")
357 return TokError("Linkage must be 'comdat'");
363 if (getLexer().isNot(AsmToken::EndOfStatement))
364 return TokError("unexpected token in directive");
366 unsigned Type = ELF::SHT_PROGBITS;
368 if (!TypeName.empty()) {
369 if (TypeName == "init_array")
370 Type = ELF::SHT_INIT_ARRAY;
371 else if (TypeName == "fini_array")
372 Type = ELF::SHT_FINI_ARRAY;
373 else if (TypeName == "preinit_array")
374 Type = ELF::SHT_PREINIT_ARRAY;
375 else if (TypeName == "nobits")
376 Type = ELF::SHT_NOBITS;
377 else if (TypeName == "progbits")
378 Type = ELF::SHT_PROGBITS;
379 else if (TypeName == "note")
380 Type = ELF::SHT_NOTE;
381 else if (TypeName == "unwind")
382 Type = ELF::SHT_X86_64_UNWIND;
384 return TokError("unknown section type");
387 SectionKind Kind = computeSectionKind(Flags);
388 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
394 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
395 const MCSection *PreviousSection = getStreamer().getPreviousSection();
396 if (PreviousSection == NULL)
397 return TokError(".previous without corresponding .section");
398 getStreamer().SwitchSection(PreviousSection);
403 /// ParseDirectiveELFType
404 /// ::= .type identifier , @attribute
405 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
407 if (getParser().ParseIdentifier(Name))
408 return TokError("expected identifier in directive");
410 // Handle the identifier as the key symbol.
411 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
413 if (getLexer().isNot(AsmToken::Comma))
414 return TokError("unexpected token in '.type' directive");
417 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
418 return TokError("expected '@' or '%' before type");
424 TypeLoc = getLexer().getLoc();
425 if (getParser().ParseIdentifier(Type))
426 return TokError("expected symbol type in directive");
428 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
429 .Case("function", MCSA_ELF_TypeFunction)
430 .Case("object", MCSA_ELF_TypeObject)
431 .Case("tls_object", MCSA_ELF_TypeTLS)
432 .Case("common", MCSA_ELF_TypeCommon)
433 .Case("notype", MCSA_ELF_TypeNoType)
434 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
435 .Default(MCSA_Invalid);
437 if (Attr == MCSA_Invalid)
438 return Error(TypeLoc, "unsupported attribute in '.type' directive");
440 if (getLexer().isNot(AsmToken::EndOfStatement))
441 return TokError("unexpected token in '.type' directive");
445 getStreamer().EmitSymbolAttribute(Sym, Attr);
450 /// ParseDirectiveIdent
451 /// ::= .ident string
452 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
453 if (getLexer().isNot(AsmToken::String))
454 return TokError("unexpected token in '.ident' directive");
456 StringRef Data = getTok().getIdentifier();
460 const MCSection *Comment =
461 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
464 SectionKind::getReadOnly(),
467 getStreamer().PushSection();
468 getStreamer().SwitchSection(Comment);
470 getStreamer().EmitIntValue(0, 1);
473 getStreamer().EmitBytes(Data, 0);
474 getStreamer().EmitIntValue(0, 1);
475 getStreamer().PopSection();
479 /// ParseDirectiveSymver
480 /// ::= .symver foo, bar2@zed
481 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
483 if (getParser().ParseIdentifier(Name))
484 return TokError("expected identifier in directive");
486 if (getLexer().isNot(AsmToken::Comma))
487 return TokError("expected a comma");
492 if (getParser().ParseIdentifier(AliasName))
493 return TokError("expected identifier in directive");
495 if (AliasName.find('@') == StringRef::npos)
496 return TokError("expected a '@' in the name");
498 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
499 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
500 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
502 getStreamer().EmitAssignment(Alias, Value);
506 /// ParseDirectiveWeakref
507 /// ::= .weakref foo, bar
508 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
509 // FIXME: Share code with the other alias building directives.
512 if (getParser().ParseIdentifier(AliasName))
513 return TokError("expected identifier in directive");
515 if (getLexer().isNot(AsmToken::Comma))
516 return TokError("expected a comma");
521 if (getParser().ParseIdentifier(Name))
522 return TokError("expected identifier in directive");
524 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
526 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
528 getStreamer().EmitWeakReference(Alias, Sym);
534 MCAsmParserExtension *createELFAsmParser() {
535 return new ELFAsmParser;