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);
37 virtual void Initialize(MCAsmParser &Parser) {
38 // Call the base implementation.
39 this->MCAsmParserExtension::Initialize(Parser);
41 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
42 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
43 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
44 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
45 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
46 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
48 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
49 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
50 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
51 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
52 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePushSection>(".pushsection");
53 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
54 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
55 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
56 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
57 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
58 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
59 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
62 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
63 // the best way for us to get access to it?
64 bool ParseSectionDirectiveData(StringRef, SMLoc) {
65 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
66 ELF::SHF_WRITE |ELF::SHF_ALLOC,
67 SectionKind::getDataRel());
69 bool ParseSectionDirectiveText(StringRef, SMLoc) {
70 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
72 ELF::SHF_ALLOC, SectionKind::getText());
74 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
75 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
77 ELF::SHF_ALLOC, SectionKind::getBSS());
79 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
80 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
82 SectionKind::getReadOnly());
84 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
85 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
87 ELF::SHF_TLS | ELF::SHF_WRITE,
88 SectionKind::getThreadData());
90 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
91 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
93 ELF::SHF_TLS | ELF::SHF_WRITE,
94 SectionKind::getThreadBSS());
96 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
97 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
100 SectionKind::getDataRel());
102 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
103 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
106 SectionKind::getReadOnlyWithRel());
108 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
109 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
112 SectionKind::getReadOnlyWithRelLocal());
114 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
115 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
118 SectionKind::getDataRel());
120 bool ParseDirectivePushSection(StringRef, SMLoc);
121 bool ParseDirectivePopSection(StringRef, SMLoc);
122 bool ParseDirectiveSection(StringRef, SMLoc);
123 bool ParseDirectiveSize(StringRef, SMLoc);
124 bool ParseDirectivePrevious(StringRef, SMLoc);
125 bool ParseDirectiveType(StringRef, SMLoc);
126 bool ParseDirectiveIdent(StringRef, SMLoc);
127 bool ParseDirectiveSymver(StringRef, SMLoc);
128 bool ParseDirectiveWeakref(StringRef, SMLoc);
131 bool ParseSectionName(StringRef &SectionName);
136 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
137 unsigned Flags, SectionKind Kind) {
138 if (getLexer().isNot(AsmToken::EndOfStatement))
139 return TokError("unexpected token in section switching directive");
142 getStreamer().SwitchSection(getContext().getELFSection(
143 Section, Type, Flags, Kind));
148 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
150 if (getParser().ParseIdentifier(Name))
151 return TokError("expected identifier in directive");
152 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
154 if (getLexer().isNot(AsmToken::Comma))
155 return TokError("unexpected token in directive");
159 if (getParser().ParseExpression(Expr))
162 if (getLexer().isNot(AsmToken::EndOfStatement))
163 return TokError("unexpected token in directive");
165 getStreamer().EmitELFSize(Sym, Expr);
169 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
170 // A section name can contain -, so we cannot just use
172 SMLoc FirstLoc = getLexer().getLoc();
175 if (getLexer().is(AsmToken::String)) {
176 SectionName = getTok().getIdentifier();
185 SMLoc PrevLoc = getLexer().getLoc();
186 if (getLexer().is(AsmToken::Minus)) {
188 Lex(); // Consume the "-".
189 } else if (getLexer().is(AsmToken::String)) {
190 CurSize = getTok().getIdentifier().size() + 2;
192 } else if (getLexer().is(AsmToken::Identifier)) {
193 CurSize = getTok().getIdentifier().size();
200 SectionName = StringRef(FirstLoc.getPointer(), Size);
202 // Make sure the following token is adjacent.
203 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
212 static SectionKind computeSectionKind(unsigned Flags) {
213 if (Flags & ELF::SHF_EXECINSTR)
214 return SectionKind::getText();
215 if (Flags & ELF::SHF_TLS)
216 return SectionKind::getThreadData();
217 return SectionKind::getDataRel();
220 static int parseSectionFlags(StringRef flagsStr) {
223 for (unsigned i = 0; i < flagsStr.size(); i++) {
224 switch (flagsStr[i]) {
226 flags |= ELF::SHF_ALLOC;
229 flags |= ELF::SHF_EXECINSTR;
232 flags |= ELF::SHF_WRITE;
235 flags |= ELF::SHF_MERGE;
238 flags |= ELF::SHF_STRINGS;
241 flags |= ELF::SHF_TLS;
244 flags |= ELF::XCORE_SHF_CP_SECTION;
247 flags |= ELF::XCORE_SHF_DP_SECTION;
250 flags |= ELF::SHF_GROUP;
260 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
261 getStreamer().PushSection();
263 if (ParseDirectiveSection(s, loc)) {
264 getStreamer().PopSection();
271 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
272 if (!getStreamer().PopSection())
273 return TokError(".popsection without corresponding .pushsection");
277 // FIXME: This is a work in progress.
278 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
279 StringRef SectionName;
281 if (ParseSectionName(SectionName))
282 return TokError("expected identifier in directive");
289 // Set the defaults first.
290 if (SectionName == ".fini" || SectionName == ".init" ||
291 SectionName == ".rodata")
292 Flags |= ELF::SHF_ALLOC;
293 if (SectionName == ".fini" || SectionName == ".init")
294 Flags |= ELF::SHF_EXECINSTR;
296 if (getLexer().is(AsmToken::Comma)) {
299 if (getLexer().isNot(AsmToken::String))
300 return TokError("expected string in directive");
302 StringRef FlagsStr = getTok().getStringContents();
305 int extraFlags = parseSectionFlags(FlagsStr);
307 return TokError("unknown flag");
310 bool Mergeable = Flags & ELF::SHF_MERGE;
311 bool Group = Flags & ELF::SHF_GROUP;
313 if (getLexer().isNot(AsmToken::Comma)) {
315 return TokError("Mergeable section must specify the type");
317 return TokError("Group section must specify the type");
320 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
321 return TokError("expected '@' or '%' before type");
324 if (getParser().ParseIdentifier(TypeName))
325 return TokError("expected identifier in directive");
328 if (getLexer().isNot(AsmToken::Comma))
329 return TokError("expected the entry size");
331 if (getParser().ParseAbsoluteExpression(Size))
334 return TokError("entry size must be positive");
338 if (getLexer().isNot(AsmToken::Comma))
339 return TokError("expected group name");
341 if (getParser().ParseIdentifier(GroupName))
343 if (getLexer().is(AsmToken::Comma)) {
346 if (getParser().ParseIdentifier(Linkage))
348 if (Linkage != "comdat")
349 return TokError("Linkage must be 'comdat'");
355 if (getLexer().isNot(AsmToken::EndOfStatement))
356 return TokError("unexpected token in directive");
358 unsigned Type = ELF::SHT_PROGBITS;
360 if (!TypeName.empty()) {
361 if (TypeName == "init_array")
362 Type = ELF::SHT_INIT_ARRAY;
363 else if (TypeName == "fini_array")
364 Type = ELF::SHT_FINI_ARRAY;
365 else if (TypeName == "preinit_array")
366 Type = ELF::SHT_PREINIT_ARRAY;
367 else if (TypeName == "nobits")
368 Type = ELF::SHT_NOBITS;
369 else if (TypeName == "progbits")
370 Type = ELF::SHT_PROGBITS;
371 else if (TypeName == "note")
372 Type = ELF::SHT_NOTE;
373 else if (TypeName == "unwind")
374 Type = ELF::SHT_X86_64_UNWIND;
376 return TokError("unknown section type");
379 SectionKind Kind = computeSectionKind(Flags);
380 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
386 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
387 const MCSection *PreviousSection = getStreamer().getPreviousSection();
388 if (PreviousSection == NULL)
389 return TokError(".previous without corresponding .section");
390 getStreamer().SwitchSection(PreviousSection);
395 /// ParseDirectiveELFType
396 /// ::= .type identifier , @attribute
397 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
399 if (getParser().ParseIdentifier(Name))
400 return TokError("expected identifier in directive");
402 // Handle the identifier as the key symbol.
403 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
405 if (getLexer().isNot(AsmToken::Comma))
406 return TokError("unexpected token in '.type' directive");
409 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
410 return TokError("expected '@' or '%' before type");
416 TypeLoc = getLexer().getLoc();
417 if (getParser().ParseIdentifier(Type))
418 return TokError("expected symbol type in directive");
420 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
421 .Case("function", MCSA_ELF_TypeFunction)
422 .Case("object", MCSA_ELF_TypeObject)
423 .Case("tls_object", MCSA_ELF_TypeTLS)
424 .Case("common", MCSA_ELF_TypeCommon)
425 .Case("notype", MCSA_ELF_TypeNoType)
426 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
427 .Default(MCSA_Invalid);
429 if (Attr == MCSA_Invalid)
430 return Error(TypeLoc, "unsupported attribute in '.type' directive");
432 if (getLexer().isNot(AsmToken::EndOfStatement))
433 return TokError("unexpected token in '.type' directive");
437 getStreamer().EmitSymbolAttribute(Sym, Attr);
442 /// ParseDirectiveIdent
443 /// ::= .ident string
444 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
445 if (getLexer().isNot(AsmToken::String))
446 return TokError("unexpected token in '.ident' directive");
448 StringRef Data = getTok().getIdentifier();
452 const MCSection *Comment =
453 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
456 SectionKind::getReadOnly(),
459 static bool First = true;
461 getStreamer().PushSection();
462 getStreamer().SwitchSection(Comment);
464 getStreamer().EmitIntValue(0, 1);
466 getStreamer().EmitBytes(Data, 0);
467 getStreamer().EmitIntValue(0, 1);
468 getStreamer().PopSection();
472 /// ParseDirectiveSymver
473 /// ::= .symver foo, bar2@zed
474 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
476 if (getParser().ParseIdentifier(Name))
477 return TokError("expected identifier in directive");
479 if (getLexer().isNot(AsmToken::Comma))
480 return TokError("expected a comma");
485 if (getParser().ParseIdentifier(AliasName))
486 return TokError("expected identifier in directive");
488 if (AliasName.find('@') == StringRef::npos)
489 return TokError("expected a '@' in the name");
491 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
492 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
493 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
495 getStreamer().EmitAssignment(Alias, Value);
499 /// ParseDirectiveWeakref
500 /// ::= .weakref foo, bar
501 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
502 // FIXME: Share code with the other alias building directives.
505 if (getParser().ParseIdentifier(AliasName))
506 return TokError("expected identifier in directive");
508 if (getLexer().isNot(AsmToken::Comma))
509 return TokError("expected a comma");
514 if (getParser().ParseIdentifier(Name))
515 return TokError("expected identifier in directive");
517 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
519 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
521 getStreamer().EmitWeakReference(Alias, Sym);
527 MCAsmParserExtension *createELFAsmParser() {
528 return new ELFAsmParser;