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::ParseDirectiveSize>(".size");
53 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
54 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
55 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
56 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
57 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
60 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
61 // the best way for us to get access to it?
62 bool ParseSectionDirectiveData(StringRef, SMLoc) {
63 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
64 ELF::SHF_WRITE |ELF::SHF_ALLOC,
65 SectionKind::getDataRel());
67 bool ParseSectionDirectiveText(StringRef, SMLoc) {
68 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
70 ELF::SHF_ALLOC, SectionKind::getText());
72 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
73 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
75 ELF::SHF_ALLOC, SectionKind::getBSS());
77 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
78 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
80 SectionKind::getReadOnly());
82 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
83 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
85 ELF::SHF_TLS | ELF::SHF_WRITE,
86 SectionKind::getThreadData());
88 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
89 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
91 ELF::SHF_TLS | ELF::SHF_WRITE,
92 SectionKind::getThreadBSS());
94 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
95 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
98 SectionKind::getDataRel());
100 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
101 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
104 SectionKind::getReadOnlyWithRel());
106 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
107 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
110 SectionKind::getReadOnlyWithRelLocal());
112 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
113 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
116 SectionKind::getDataRel());
118 bool ParseDirectiveSection(StringRef, SMLoc);
119 bool ParseDirectiveSize(StringRef, SMLoc);
120 bool ParseDirectivePrevious(StringRef, SMLoc);
121 bool ParseDirectiveType(StringRef, SMLoc);
122 bool ParseDirectiveIdent(StringRef, SMLoc);
123 bool ParseDirectiveSymver(StringRef, SMLoc);
124 bool ParseDirectiveWeakref(StringRef, SMLoc);
127 bool ParseSectionName(StringRef &SectionName);
132 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
133 unsigned Flags, SectionKind Kind) {
134 if (getLexer().isNot(AsmToken::EndOfStatement))
135 return TokError("unexpected token in section switching directive");
138 getStreamer().SwitchSection(getContext().getELFSection(
139 Section, Type, Flags, Kind));
144 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
146 if (getParser().ParseIdentifier(Name))
147 return TokError("expected identifier in directive");
148 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
150 if (getLexer().isNot(AsmToken::Comma))
151 return TokError("unexpected token in directive");
155 if (getParser().ParseExpression(Expr))
158 if (getLexer().isNot(AsmToken::EndOfStatement))
159 return TokError("unexpected token in directive");
161 getStreamer().EmitELFSize(Sym, Expr);
165 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
166 // A section name can contain -, so we cannot just use
168 SMLoc FirstLoc = getLexer().getLoc();
171 if (getLexer().is(AsmToken::String)) {
172 SectionName = getTok().getIdentifier();
181 SMLoc PrevLoc = getLexer().getLoc();
182 if (getLexer().is(AsmToken::Minus)) {
184 Lex(); // Consume the "-".
185 } else if (getLexer().is(AsmToken::String)) {
186 CurSize = getTok().getIdentifier().size() + 2;
188 } else if (getLexer().is(AsmToken::Identifier)) {
189 CurSize = getTok().getIdentifier().size();
196 SectionName = StringRef(FirstLoc.getPointer(), Size);
198 // Make sure the following token is adjacent.
199 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
208 static SectionKind computeSectionKind(unsigned Flags) {
209 if (Flags & ELF::SHF_EXECINSTR)
210 return SectionKind::getText();
211 if (Flags & ELF::SHF_TLS)
212 return SectionKind::getThreadData();
213 return SectionKind::getDataRel();
216 static int parseSectionFlags(StringRef flagsStr) {
219 for (unsigned i = 0; i < flagsStr.size(); i++) {
220 switch (flagsStr[i]) {
222 flags |= ELF::SHF_ALLOC;
225 flags |= ELF::SHF_EXECINSTR;
228 flags |= ELF::SHF_WRITE;
231 flags |= ELF::SHF_MERGE;
234 flags |= ELF::SHF_STRINGS;
237 flags |= ELF::SHF_TLS;
240 flags |= ELF::XCORE_SHF_CP_SECTION;
243 flags |= ELF::XCORE_SHF_DP_SECTION;
246 flags |= ELF::SHF_GROUP;
256 // FIXME: This is a work in progress.
257 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
258 StringRef SectionName;
260 if (ParseSectionName(SectionName))
261 return TokError("expected identifier in directive");
268 // Set the defaults first.
269 if (SectionName == ".fini" || SectionName == ".init" ||
270 SectionName == ".rodata")
271 Flags |= ELF::SHF_ALLOC;
272 if (SectionName == ".fini" || SectionName == ".init")
273 Flags |= ELF::SHF_EXECINSTR;
275 if (getLexer().is(AsmToken::Comma)) {
278 if (getLexer().isNot(AsmToken::String))
279 return TokError("expected string in directive");
281 StringRef FlagsStr = getTok().getStringContents();
284 int extraFlags = parseSectionFlags(FlagsStr);
286 return TokError("unknown flag");
289 bool Mergeable = Flags & ELF::SHF_MERGE;
290 bool Group = Flags & ELF::SHF_GROUP;
292 if (getLexer().isNot(AsmToken::Comma)) {
294 return TokError("Mergeable section must specify the type");
296 return TokError("Group section must specify the type");
299 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
300 return TokError("expected '@' or '%' before type");
303 if (getParser().ParseIdentifier(TypeName))
304 return TokError("expected identifier in directive");
307 if (getLexer().isNot(AsmToken::Comma))
308 return TokError("expected the entry size");
310 if (getParser().ParseAbsoluteExpression(Size))
313 return TokError("entry size must be positive");
317 if (getLexer().isNot(AsmToken::Comma))
318 return TokError("expected group name");
320 if (getParser().ParseIdentifier(GroupName))
322 if (getLexer().is(AsmToken::Comma)) {
325 if (getParser().ParseIdentifier(Linkage))
327 if (Linkage != "comdat")
328 return TokError("Linkage must be 'comdat'");
334 if (getLexer().isNot(AsmToken::EndOfStatement))
335 return TokError("unexpected token in directive");
337 unsigned Type = ELF::SHT_PROGBITS;
339 if (!TypeName.empty()) {
340 if (TypeName == "init_array")
341 Type = ELF::SHT_INIT_ARRAY;
342 else if (TypeName == "fini_array")
343 Type = ELF::SHT_FINI_ARRAY;
344 else if (TypeName == "preinit_array")
345 Type = ELF::SHT_PREINIT_ARRAY;
346 else if (TypeName == "nobits")
347 Type = ELF::SHT_NOBITS;
348 else if (TypeName == "progbits")
349 Type = ELF::SHT_PROGBITS;
350 else if (TypeName == "note")
351 Type = ELF::SHT_NOTE;
352 else if (TypeName == "unwind")
353 Type = ELF::SHT_X86_64_UNWIND;
355 return TokError("unknown section type");
358 SectionKind Kind = computeSectionKind(Flags);
359 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
365 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
366 const MCSection *PreviousSection = getStreamer().getPreviousSection();
367 if (PreviousSection != NULL)
368 getStreamer().SwitchSection(PreviousSection);
373 /// ParseDirectiveELFType
374 /// ::= .type identifier , @attribute
375 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
377 if (getParser().ParseIdentifier(Name))
378 return TokError("expected identifier in directive");
380 // Handle the identifier as the key symbol.
381 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
383 if (getLexer().isNot(AsmToken::Comma))
384 return TokError("unexpected token in '.type' directive");
387 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
388 return TokError("expected '@' or '%' before type");
394 TypeLoc = getLexer().getLoc();
395 if (getParser().ParseIdentifier(Type))
396 return TokError("expected symbol type in directive");
398 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
399 .Case("function", MCSA_ELF_TypeFunction)
400 .Case("object", MCSA_ELF_TypeObject)
401 .Case("tls_object", MCSA_ELF_TypeTLS)
402 .Case("common", MCSA_ELF_TypeCommon)
403 .Case("notype", MCSA_ELF_TypeNoType)
404 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
405 .Default(MCSA_Invalid);
407 if (Attr == MCSA_Invalid)
408 return Error(TypeLoc, "unsupported attribute in '.type' directive");
410 if (getLexer().isNot(AsmToken::EndOfStatement))
411 return TokError("unexpected token in '.type' directive");
415 getStreamer().EmitSymbolAttribute(Sym, Attr);
420 /// ParseDirectiveIdent
421 /// ::= .ident string
422 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
423 if (getLexer().isNot(AsmToken::String))
424 return TokError("unexpected token in '.ident' directive");
426 StringRef Data = getTok().getIdentifier();
430 const MCSection *OldSection = getStreamer().getCurrentSection();
431 const MCSection *Comment =
432 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
435 SectionKind::getReadOnly(),
438 static bool First = true;
440 getStreamer().SwitchSection(Comment);
442 getStreamer().EmitIntValue(0, 1);
444 getStreamer().EmitBytes(Data, 0);
445 getStreamer().EmitIntValue(0, 1);
446 getStreamer().SwitchSection(OldSection);
450 /// ParseDirectiveSymver
451 /// ::= .symver foo, bar2@zed
452 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
454 if (getParser().ParseIdentifier(Name))
455 return TokError("expected identifier in directive");
457 if (getLexer().isNot(AsmToken::Comma))
458 return TokError("expected a comma");
463 if (getParser().ParseIdentifier(AliasName))
464 return TokError("expected identifier in directive");
466 if (AliasName.find('@') == StringRef::npos)
467 return TokError("expected a '@' in the name");
469 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
470 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
471 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
473 getStreamer().EmitAssignment(Alias, Value);
477 /// ParseDirectiveWeakref
478 /// ::= .weakref foo, bar
479 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
480 // FIXME: Share code with the other alias building directives.
483 if (getParser().ParseIdentifier(AliasName))
484 return TokError("expected identifier in directive");
486 if (getLexer().isNot(AsmToken::Comma))
487 return TokError("expected a comma");
492 if (getParser().ParseIdentifier(Name))
493 return TokError("expected identifier in directive");
495 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
497 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
499 getStreamer().EmitWeakReference(Alias, Sym);
505 MCAsmParserExtension *createELFAsmParser() {
506 return new ELFAsmParser;