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 // FIXME: This is a work in progress.
197 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
198 StringRef SectionName;
200 if (ParseSectionName(SectionName))
201 return TokError("expected identifier in directive");
207 if (getLexer().is(AsmToken::Comma)) {
210 if (getLexer().isNot(AsmToken::String))
211 return TokError("expected string in directive");
213 FlagsStr = getTok().getStringContents();
216 AsmToken::TokenKind TypeStartToken;
217 if (getContext().getAsmInfo().getCommentString()[0] == '@')
218 TypeStartToken = AsmToken::Percent;
220 TypeStartToken = AsmToken::At;
222 bool Mergeable = FlagsStr.find('M') != StringRef::npos;
223 bool Group = FlagsStr.find('G') != StringRef::npos;
225 if (getLexer().isNot(AsmToken::Comma)) {
227 return TokError("Mergeable section must specify the type");
229 return TokError("Group section must specify the type");
232 if (getLexer().isNot(TypeStartToken))
233 return TokError("expected the type");
236 if (getParser().ParseIdentifier(TypeName))
237 return TokError("expected identifier in directive");
240 if (getLexer().isNot(AsmToken::Comma))
241 return TokError("expected the entry size");
243 if (getParser().ParseAbsoluteExpression(Size))
246 return TokError("entry size must be positive");
250 if (getLexer().isNot(AsmToken::Comma))
251 return TokError("expected group name");
253 if (getParser().ParseIdentifier(GroupName))
255 if (getLexer().is(AsmToken::Comma)) {
258 if (getParser().ParseIdentifier(Linkage))
260 if (Linkage != "comdat")
261 return TokError("Linkage must be 'comdat'");
267 if (getLexer().isNot(AsmToken::EndOfStatement))
268 return TokError("unexpected token in directive");
271 unsigned Type = MCSectionELF::SHT_NULL;
273 // Set the defaults first.
274 if (SectionName == ".fini" || SectionName == ".init" || SectionName == ".rodata") {
275 Type = MCSectionELF::SHT_PROGBITS;
276 Flags |= MCSectionELF::SHF_ALLOC;
278 if (SectionName == ".fini" || SectionName == ".init") {
279 Flags |= MCSectionELF::SHF_EXECINSTR;
282 for (unsigned i = 0; i < FlagsStr.size(); i++) {
283 switch (FlagsStr[i]) {
285 Flags |= MCSectionELF::SHF_ALLOC;
288 Flags |= MCSectionELF::SHF_EXECINSTR;
291 Flags |= MCSectionELF::SHF_WRITE;
294 Flags |= MCSectionELF::SHF_MERGE;
297 Flags |= MCSectionELF::SHF_STRINGS;
300 Flags |= MCSectionELF::SHF_TLS;
303 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
306 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
309 Flags |= MCSectionELF::SHF_GROUP;
312 return TokError("unknown flag");
316 if (!TypeName.empty()) {
317 if (TypeName == "init_array")
318 Type = MCSectionELF::SHT_INIT_ARRAY;
319 else if (TypeName == "fini_array")
320 Type = MCSectionELF::SHT_FINI_ARRAY;
321 else if (TypeName == "preinit_array")
322 Type = MCSectionELF::SHT_PREINIT_ARRAY;
323 else if (TypeName == "nobits")
324 Type = MCSectionELF::SHT_NOBITS;
325 else if (TypeName == "progbits")
326 Type = MCSectionELF::SHT_PROGBITS;
328 return TokError("unknown section type");
331 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
332 ? SectionKind::getText()
333 : SectionKind::getDataRel();
334 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
340 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
341 const MCSection *PreviousSection = getStreamer().getPreviousSection();
342 if (PreviousSection != NULL)
343 getStreamer().SwitchSection(PreviousSection);
348 /// ParseDirectiveELFType
349 /// ::= .type identifier , @attribute
350 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
352 if (getParser().ParseIdentifier(Name))
353 return TokError("expected identifier in directive");
355 // Handle the identifier as the key symbol.
356 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
358 if (getLexer().isNot(AsmToken::Comma))
359 return TokError("unexpected token in '.type' directive");
362 if (getLexer().isNot(AsmToken::At))
363 return TokError("expected '@' before type");
369 TypeLoc = getLexer().getLoc();
370 if (getParser().ParseIdentifier(Type))
371 return TokError("expected symbol type in directive");
373 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
374 .Case("function", MCSA_ELF_TypeFunction)
375 .Case("object", MCSA_ELF_TypeObject)
376 .Case("tls_object", MCSA_ELF_TypeTLS)
377 .Case("common", MCSA_ELF_TypeCommon)
378 .Case("notype", MCSA_ELF_TypeNoType)
379 .Default(MCSA_Invalid);
381 if (Attr == MCSA_Invalid)
382 return Error(TypeLoc, "unsupported attribute in '.type' directive");
384 if (getLexer().isNot(AsmToken::EndOfStatement))
385 return TokError("unexpected token in '.type' directive");
389 getStreamer().EmitSymbolAttribute(Sym, Attr);
394 /// ParseDirectiveIdent
395 /// ::= .ident string
396 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
397 if (getLexer().isNot(AsmToken::String))
398 return TokError("unexpected token in '.ident' directive");
400 StringRef Data = getTok().getIdentifier();
404 const MCSection *OldSection = getStreamer().getCurrentSection();
405 const MCSection *Comment =
406 getContext().getELFSection(".comment", MCSectionELF::SHT_PROGBITS,
407 MCSectionELF::SHF_MERGE |
408 MCSectionELF::SHF_STRINGS,
409 SectionKind::getReadOnly(),
412 static bool First = true;
414 getStreamer().SwitchSection(Comment);
416 getStreamer().EmitIntValue(0, 1);
418 getStreamer().EmitBytes(Data, 0);
419 getStreamer().EmitIntValue(0, 1);
420 getStreamer().SwitchSection(OldSection);
424 /// ParseDirectiveSymver
425 /// ::= .symver foo, bar2@zed
426 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
428 if (getParser().ParseIdentifier(Name))
429 return TokError("expected identifier in directive");
431 if (getLexer().isNot(AsmToken::Comma))
432 return TokError("expected a comma");
437 if (getParser().ParseIdentifier(AliasName))
438 return TokError("expected identifier in directive");
440 if (AliasName.find('@') == StringRef::npos)
441 return TokError("expected a '@' in the name");
443 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
444 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
445 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
447 getStreamer().EmitAssignment(Alias, Value);
451 /// ParseDirectiveWeakref
452 /// ::= .weakref foo, bar
453 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
454 // FIXME: Share code with the other alias building directives.
457 if (getParser().ParseIdentifier(AliasName))
458 return TokError("expected identifier in directive");
460 if (getLexer().isNot(AsmToken::Comma))
461 return TokError("expected a comma");
466 if (getParser().ParseIdentifier(Name))
467 return TokError("expected identifier in directive");
469 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
471 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
473 getStreamer().EmitWeakReference(Alias, Sym);
479 MCAsmParserExtension *createELFAsmParser() {
480 return new ELFAsmParser;