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"
23 class ELFAsmParser : public MCAsmParserExtension {
24 template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
25 void AddDirectiveHandler(StringRef Directive) {
26 getParser().AddDirectiveHandler(this, Directive,
27 HandleDirective<ELFAsmParser, Handler>);
30 bool ParseSectionSwitch(StringRef Section, unsigned Type,
31 unsigned Flags, SectionKind Kind);
36 virtual void Initialize(MCAsmParser &Parser) {
37 // Call the base implementation.
38 this->MCAsmParserExtension::Initialize(Parser);
40 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
41 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
42 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
43 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
44 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
45 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
46 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
48 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
49 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
50 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
51 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
52 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
53 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
54 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
55 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
56 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
59 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
60 // the best way for us to get access to it?
61 bool ParseSectionDirectiveData(StringRef, SMLoc) {
62 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
63 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
64 SectionKind::getDataRel());
66 bool ParseSectionDirectiveText(StringRef, SMLoc) {
67 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
68 MCSectionELF::SHF_EXECINSTR |
69 MCSectionELF::SHF_ALLOC, SectionKind::getText());
71 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
72 return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
73 MCSectionELF::SHF_WRITE |
74 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
76 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
77 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
78 MCSectionELF::SHF_ALLOC,
79 SectionKind::getReadOnly());
81 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
82 return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
83 MCSectionELF::SHF_ALLOC |
84 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
85 SectionKind::getThreadData());
87 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
88 return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
89 MCSectionELF::SHF_ALLOC |
90 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
91 SectionKind::getThreadBSS());
93 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
94 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
95 MCSectionELF::SHF_ALLOC |
96 MCSectionELF::SHF_WRITE,
97 SectionKind::getDataRel());
99 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
100 return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
101 MCSectionELF::SHF_ALLOC |
102 MCSectionELF::SHF_WRITE,
103 SectionKind::getReadOnlyWithRel());
105 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
106 return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
107 MCSectionELF::SHF_ALLOC |
108 MCSectionELF::SHF_WRITE,
109 SectionKind::getReadOnlyWithRelLocal());
111 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
112 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
113 MCSectionELF::SHF_ALLOC |
114 MCSectionELF::SHF_WRITE,
115 SectionKind::getDataRel());
117 bool ParseDirectiveSection(StringRef, SMLoc);
118 bool ParseDirectiveSize(StringRef, SMLoc);
119 bool ParseDirectivePrevious(StringRef, SMLoc);
120 bool ParseDirectiveType(StringRef, SMLoc);
121 bool ParseDirectiveIdent(StringRef, SMLoc);
122 bool ParseDirectiveSymver(StringRef, SMLoc);
123 bool ParseDirectiveWeakref(StringRef, SMLoc);
126 bool ParseSectionName(StringRef &SectionName);
131 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
132 unsigned Flags, SectionKind Kind) {
133 if (getLexer().isNot(AsmToken::EndOfStatement))
134 return TokError("unexpected token in section switching directive");
137 getStreamer().SwitchSection(getContext().getELFSection(
138 Section, Type, Flags, Kind));
143 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
145 if (getParser().ParseIdentifier(Name))
146 return TokError("expected identifier in directive");
147 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
149 if (getLexer().isNot(AsmToken::Comma))
150 return TokError("unexpected token in directive");
154 if (getParser().ParseExpression(Expr))
157 if (getLexer().isNot(AsmToken::EndOfStatement))
158 return TokError("unexpected token in directive");
160 getStreamer().EmitELFSize(Sym, Expr);
164 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
165 // A section name can contain -, so we cannot just use
167 SMLoc FirstLoc = getLexer().getLoc();
174 SMLoc PrevLoc = getLexer().getLoc();
175 if (getLexer().is(AsmToken::Minus)) {
177 Lex(); // Consume the "-".
178 } else if (!getParser().ParseIdentifier(Tmp))
179 CurSize = Tmp.size();
184 SectionName = StringRef(FirstLoc.getPointer(), Size);
186 // Make sure the following token is adjacent.
187 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
196 static SectionKind computeSectionKind(unsigned Flags) {
197 if (Flags & MCSectionELF::SHF_EXECINSTR)
198 return SectionKind::getText();
199 if (Flags & MCSectionELF::SHF_TLS)
200 return SectionKind::getThreadData();
201 return SectionKind::getDataRel();
204 static int parseSectionFlags(StringRef flagsStr) {
207 for (unsigned i = 0; i < flagsStr.size(); i++) {
208 switch (flagsStr[i]) {
210 flags |= MCSectionELF::SHF_ALLOC;
213 flags |= MCSectionELF::SHF_EXECINSTR;
216 flags |= MCSectionELF::SHF_WRITE;
219 flags |= MCSectionELF::SHF_MERGE;
222 flags |= MCSectionELF::SHF_STRINGS;
225 flags |= MCSectionELF::SHF_TLS;
228 flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
231 flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
234 flags |= MCSectionELF::SHF_GROUP;
244 // FIXME: This is a work in progress.
245 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
246 StringRef SectionName;
248 if (ParseSectionName(SectionName))
249 return TokError("expected identifier in directive");
256 // Set the defaults first.
257 if (SectionName == ".fini" || SectionName == ".init" ||
258 SectionName == ".rodata")
259 Flags |= MCSectionELF::SHF_ALLOC;
260 if (SectionName == ".fini" || SectionName == ".init")
261 Flags |= MCSectionELF::SHF_EXECINSTR;
263 if (getLexer().is(AsmToken::Comma)) {
266 if (getLexer().isNot(AsmToken::String))
267 return TokError("expected string in directive");
269 StringRef FlagsStr = getTok().getStringContents();
272 int extraFlags = parseSectionFlags(FlagsStr);
274 return TokError("unknown flag");
277 bool Mergeable = Flags & MCSectionELF::SHF_MERGE;
278 bool Group = Flags & MCSectionELF::SHF_GROUP;
280 if (getLexer().isNot(AsmToken::Comma)) {
282 return TokError("Mergeable section must specify the type");
284 return TokError("Group section must specify the type");
287 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
288 return TokError("expected '@' or '%' before type");
291 if (getParser().ParseIdentifier(TypeName))
292 return TokError("expected identifier in directive");
295 if (getLexer().isNot(AsmToken::Comma))
296 return TokError("expected the entry size");
298 if (getParser().ParseAbsoluteExpression(Size))
301 return TokError("entry size must be positive");
305 if (getLexer().isNot(AsmToken::Comma))
306 return TokError("expected group name");
308 if (getParser().ParseIdentifier(GroupName))
310 if (getLexer().is(AsmToken::Comma)) {
313 if (getParser().ParseIdentifier(Linkage))
315 if (Linkage != "comdat")
316 return TokError("Linkage must be 'comdat'");
322 if (getLexer().isNot(AsmToken::EndOfStatement))
323 return TokError("unexpected token in directive");
325 unsigned Type = MCSectionELF::SHT_PROGBITS;
327 if (!TypeName.empty()) {
328 if (TypeName == "init_array")
329 Type = MCSectionELF::SHT_INIT_ARRAY;
330 else if (TypeName == "fini_array")
331 Type = MCSectionELF::SHT_FINI_ARRAY;
332 else if (TypeName == "preinit_array")
333 Type = MCSectionELF::SHT_PREINIT_ARRAY;
334 else if (TypeName == "nobits")
335 Type = MCSectionELF::SHT_NOBITS;
336 else if (TypeName == "progbits")
337 Type = MCSectionELF::SHT_PROGBITS;
339 return TokError("unknown section type");
342 SectionKind Kind = computeSectionKind(Flags);
343 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
349 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
350 const MCSection *PreviousSection = getStreamer().getPreviousSection();
351 if (PreviousSection != NULL)
352 getStreamer().SwitchSection(PreviousSection);
357 /// ParseDirectiveELFType
358 /// ::= .type identifier , @attribute
359 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
361 if (getParser().ParseIdentifier(Name))
362 return TokError("expected identifier in directive");
364 // Handle the identifier as the key symbol.
365 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
367 if (getLexer().isNot(AsmToken::Comma))
368 return TokError("unexpected token in '.type' directive");
371 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
372 return TokError("expected '@' or '%' before type");
378 TypeLoc = getLexer().getLoc();
379 if (getParser().ParseIdentifier(Type))
380 return TokError("expected symbol type in directive");
382 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
383 .Case("function", MCSA_ELF_TypeFunction)
384 .Case("object", MCSA_ELF_TypeObject)
385 .Case("tls_object", MCSA_ELF_TypeTLS)
386 .Case("common", MCSA_ELF_TypeCommon)
387 .Case("notype", MCSA_ELF_TypeNoType)
388 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
389 .Default(MCSA_Invalid);
391 if (Attr == MCSA_Invalid)
392 return Error(TypeLoc, "unsupported attribute in '.type' directive");
394 if (getLexer().isNot(AsmToken::EndOfStatement))
395 return TokError("unexpected token in '.type' directive");
399 getStreamer().EmitSymbolAttribute(Sym, Attr);
404 /// ParseDirectiveIdent
405 /// ::= .ident string
406 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
407 if (getLexer().isNot(AsmToken::String))
408 return TokError("unexpected token in '.ident' directive");
410 StringRef Data = getTok().getIdentifier();
414 const MCSection *OldSection = getStreamer().getCurrentSection();
415 const MCSection *Comment =
416 getContext().getELFSection(".comment", MCSectionELF::SHT_PROGBITS,
417 MCSectionELF::SHF_MERGE |
418 MCSectionELF::SHF_STRINGS,
419 SectionKind::getReadOnly(),
422 static bool First = true;
424 getStreamer().SwitchSection(Comment);
426 getStreamer().EmitIntValue(0, 1);
428 getStreamer().EmitBytes(Data, 0);
429 getStreamer().EmitIntValue(0, 1);
430 getStreamer().SwitchSection(OldSection);
434 /// ParseDirectiveSymver
435 /// ::= .symver foo, bar2@zed
436 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
438 if (getParser().ParseIdentifier(Name))
439 return TokError("expected identifier in directive");
441 if (getLexer().isNot(AsmToken::Comma))
442 return TokError("expected a comma");
447 if (getParser().ParseIdentifier(AliasName))
448 return TokError("expected identifier in directive");
450 if (AliasName.find('@') == StringRef::npos)
451 return TokError("expected a '@' in the name");
453 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
454 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
455 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
457 getStreamer().EmitAssignment(Alias, Value);
461 /// ParseDirectiveWeakref
462 /// ::= .weakref foo, bar
463 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
464 // FIXME: Share code with the other alias building directives.
467 if (getParser().ParseIdentifier(AliasName))
468 return TokError("expected identifier in directive");
470 if (getLexer().isNot(AsmToken::Comma))
471 return TokError("expected a comma");
476 if (getParser().ParseIdentifier(Name))
477 return TokError("expected identifier in directive");
479 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
481 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
483 getStreamer().EmitWeakReference(Alias, Sym);
489 MCAsmParserExtension *createELFAsmParser() {
490 return new ELFAsmParser;