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) {}
38 virtual void Initialize(MCAsmParser &Parser) {
39 // Call the base implementation.
40 this->MCAsmParserExtension::Initialize(Parser);
42 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
43 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
44 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
45 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
46 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
48 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
49 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
50 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
51 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
52 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
53 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePushSection>(".pushsection");
54 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
55 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
56 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
57 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
58 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
59 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
60 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
63 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
64 // the best way for us to get access to it?
65 bool ParseSectionDirectiveData(StringRef, SMLoc) {
66 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
67 ELF::SHF_WRITE |ELF::SHF_ALLOC,
68 SectionKind::getDataRel());
70 bool ParseSectionDirectiveText(StringRef, SMLoc) {
71 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
73 ELF::SHF_ALLOC, SectionKind::getText());
75 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
76 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
78 ELF::SHF_ALLOC, SectionKind::getBSS());
80 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
81 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
83 SectionKind::getReadOnly());
85 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
86 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
88 ELF::SHF_TLS | ELF::SHF_WRITE,
89 SectionKind::getThreadData());
91 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
92 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
94 ELF::SHF_TLS | ELF::SHF_WRITE,
95 SectionKind::getThreadBSS());
97 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
98 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
101 SectionKind::getDataRel());
103 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
104 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
107 SectionKind::getReadOnlyWithRel());
109 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
110 return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
113 SectionKind::getReadOnlyWithRelLocal());
115 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
116 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
119 SectionKind::getDataRel());
121 bool ParseDirectivePushSection(StringRef, SMLoc);
122 bool ParseDirectivePopSection(StringRef, SMLoc);
123 bool ParseDirectiveSection(StringRef, SMLoc);
124 bool ParseDirectiveSize(StringRef, SMLoc);
125 bool ParseDirectivePrevious(StringRef, SMLoc);
126 bool ParseDirectiveType(StringRef, SMLoc);
127 bool ParseDirectiveIdent(StringRef, SMLoc);
128 bool ParseDirectiveSymver(StringRef, SMLoc);
129 bool ParseDirectiveWeakref(StringRef, SMLoc);
132 bool ParseSectionName(StringRef &SectionName);
137 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
138 unsigned Flags, SectionKind Kind) {
139 if (getLexer().isNot(AsmToken::EndOfStatement))
140 return TokError("unexpected token in section switching directive");
143 getStreamer().SwitchSection(getContext().getELFSection(
144 Section, Type, Flags, Kind));
149 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
151 if (getParser().ParseIdentifier(Name))
152 return TokError("expected identifier in directive");
153 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
155 if (getLexer().isNot(AsmToken::Comma))
156 return TokError("unexpected token in directive");
160 if (getParser().ParseExpression(Expr))
163 if (getLexer().isNot(AsmToken::EndOfStatement))
164 return TokError("unexpected token in directive");
166 getStreamer().EmitELFSize(Sym, Expr);
170 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
171 // A section name can contain -, so we cannot just use
173 SMLoc FirstLoc = getLexer().getLoc();
176 if (getLexer().is(AsmToken::String)) {
177 SectionName = getTok().getIdentifier();
186 SMLoc PrevLoc = getLexer().getLoc();
187 if (getLexer().is(AsmToken::Minus)) {
189 Lex(); // Consume the "-".
190 } else if (getLexer().is(AsmToken::String)) {
191 CurSize = getTok().getIdentifier().size() + 2;
193 } else if (getLexer().is(AsmToken::Identifier)) {
194 CurSize = getTok().getIdentifier().size();
201 SectionName = StringRef(FirstLoc.getPointer(), Size);
203 // Make sure the following token is adjacent.
204 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
213 static SectionKind computeSectionKind(unsigned Flags) {
214 if (Flags & ELF::SHF_EXECINSTR)
215 return SectionKind::getText();
216 if (Flags & ELF::SHF_TLS)
217 return SectionKind::getThreadData();
218 return SectionKind::getDataRel();
221 static int parseSectionFlags(StringRef flagsStr) {
224 for (unsigned i = 0; i < flagsStr.size(); i++) {
225 switch (flagsStr[i]) {
227 flags |= ELF::SHF_ALLOC;
230 flags |= ELF::SHF_EXECINSTR;
233 flags |= ELF::SHF_WRITE;
236 flags |= ELF::SHF_MERGE;
239 flags |= ELF::SHF_STRINGS;
242 flags |= ELF::SHF_TLS;
245 flags |= ELF::XCORE_SHF_CP_SECTION;
248 flags |= ELF::XCORE_SHF_DP_SECTION;
251 flags |= ELF::SHF_GROUP;
261 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
262 getStreamer().PushSection();
264 if (ParseDirectiveSection(s, loc)) {
265 getStreamer().PopSection();
272 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
273 if (!getStreamer().PopSection())
274 return TokError(".popsection without corresponding .pushsection");
278 // FIXME: This is a work in progress.
279 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
280 StringRef SectionName;
282 if (ParseSectionName(SectionName))
283 return TokError("expected identifier in directive");
290 // Set the defaults first.
291 if (SectionName == ".fini" || SectionName == ".init" ||
292 SectionName == ".rodata")
293 Flags |= ELF::SHF_ALLOC;
294 if (SectionName == ".fini" || SectionName == ".init")
295 Flags |= ELF::SHF_EXECINSTR;
297 if (getLexer().is(AsmToken::Comma)) {
300 if (getLexer().isNot(AsmToken::String))
301 return TokError("expected string in directive");
303 StringRef FlagsStr = getTok().getStringContents();
306 int extraFlags = parseSectionFlags(FlagsStr);
308 return TokError("unknown flag");
311 bool Mergeable = Flags & ELF::SHF_MERGE;
312 bool Group = Flags & ELF::SHF_GROUP;
314 if (getLexer().isNot(AsmToken::Comma)) {
316 return TokError("Mergeable section must specify the type");
318 return TokError("Group section must specify the type");
321 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
322 return TokError("expected '@' or '%' before type");
325 if (getParser().ParseIdentifier(TypeName))
326 return TokError("expected identifier in directive");
329 if (getLexer().isNot(AsmToken::Comma))
330 return TokError("expected the entry size");
332 if (getParser().ParseAbsoluteExpression(Size))
335 return TokError("entry size must be positive");
339 if (getLexer().isNot(AsmToken::Comma))
340 return TokError("expected group name");
342 if (getParser().ParseIdentifier(GroupName))
344 if (getLexer().is(AsmToken::Comma)) {
347 if (getParser().ParseIdentifier(Linkage))
349 if (Linkage != "comdat")
350 return TokError("Linkage must be 'comdat'");
356 if (getLexer().isNot(AsmToken::EndOfStatement))
357 return TokError("unexpected token in directive");
359 unsigned Type = ELF::SHT_PROGBITS;
361 if (!TypeName.empty()) {
362 if (TypeName == "init_array")
363 Type = ELF::SHT_INIT_ARRAY;
364 else if (TypeName == "fini_array")
365 Type = ELF::SHT_FINI_ARRAY;
366 else if (TypeName == "preinit_array")
367 Type = ELF::SHT_PREINIT_ARRAY;
368 else if (TypeName == "nobits")
369 Type = ELF::SHT_NOBITS;
370 else if (TypeName == "progbits")
371 Type = ELF::SHT_PROGBITS;
372 else if (TypeName == "note")
373 Type = ELF::SHT_NOTE;
374 else if (TypeName == "unwind")
375 Type = ELF::SHT_X86_64_UNWIND;
377 return TokError("unknown section type");
380 SectionKind Kind = computeSectionKind(Flags);
381 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
387 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
388 const MCSection *PreviousSection = getStreamer().getPreviousSection();
389 if (PreviousSection == NULL)
390 return TokError(".previous without corresponding .section");
391 getStreamer().SwitchSection(PreviousSection);
396 /// ParseDirectiveELFType
397 /// ::= .type identifier , @attribute
398 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
400 if (getParser().ParseIdentifier(Name))
401 return TokError("expected identifier in directive");
403 // Handle the identifier as the key symbol.
404 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
406 if (getLexer().isNot(AsmToken::Comma))
407 return TokError("unexpected token in '.type' directive");
410 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
411 return TokError("expected '@' or '%' before type");
417 TypeLoc = getLexer().getLoc();
418 if (getParser().ParseIdentifier(Type))
419 return TokError("expected symbol type in directive");
421 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
422 .Case("function", MCSA_ELF_TypeFunction)
423 .Case("object", MCSA_ELF_TypeObject)
424 .Case("tls_object", MCSA_ELF_TypeTLS)
425 .Case("common", MCSA_ELF_TypeCommon)
426 .Case("notype", MCSA_ELF_TypeNoType)
427 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
428 .Default(MCSA_Invalid);
430 if (Attr == MCSA_Invalid)
431 return Error(TypeLoc, "unsupported attribute in '.type' directive");
433 if (getLexer().isNot(AsmToken::EndOfStatement))
434 return TokError("unexpected token in '.type' directive");
438 getStreamer().EmitSymbolAttribute(Sym, Attr);
443 /// ParseDirectiveIdent
444 /// ::= .ident string
445 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
446 if (getLexer().isNot(AsmToken::String))
447 return TokError("unexpected token in '.ident' directive");
449 StringRef Data = getTok().getIdentifier();
453 const MCSection *Comment =
454 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
457 SectionKind::getReadOnly(),
460 getStreamer().PushSection();
461 getStreamer().SwitchSection(Comment);
463 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;