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");
58 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
59 // the best way for us to get access to it?
60 bool ParseSectionDirectiveData(StringRef, SMLoc) {
61 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
62 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
63 SectionKind::getDataRel());
65 bool ParseSectionDirectiveText(StringRef, SMLoc) {
66 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
67 MCSectionELF::SHF_EXECINSTR |
68 MCSectionELF::SHF_ALLOC, SectionKind::getText());
70 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
71 return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
72 MCSectionELF::SHF_WRITE |
73 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
75 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
76 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
77 MCSectionELF::SHF_ALLOC,
78 SectionKind::getReadOnly());
80 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
81 return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
82 MCSectionELF::SHF_ALLOC |
83 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
84 SectionKind::getThreadData());
86 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
87 return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
88 MCSectionELF::SHF_ALLOC |
89 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
90 SectionKind::getThreadBSS());
92 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
93 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
94 MCSectionELF::SHF_ALLOC |
95 MCSectionELF::SHF_WRITE,
96 SectionKind::getDataRel());
98 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
99 return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
100 MCSectionELF::SHF_ALLOC |
101 MCSectionELF::SHF_WRITE,
102 SectionKind::getReadOnlyWithRel());
104 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
105 return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
106 MCSectionELF::SHF_ALLOC |
107 MCSectionELF::SHF_WRITE,
108 SectionKind::getReadOnlyWithRelLocal());
110 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
111 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
112 MCSectionELF::SHF_ALLOC |
113 MCSectionELF::SHF_WRITE,
114 SectionKind::getDataRel());
116 bool ParseDirectiveSection(StringRef, SMLoc);
117 bool ParseDirectiveSize(StringRef, SMLoc);
118 bool ParseDirectivePrevious(StringRef, SMLoc);
119 bool ParseDirectiveType(StringRef, SMLoc);
120 bool ParseDirectiveIdent(StringRef, SMLoc);
121 bool ParseDirectiveSymver(StringRef, SMLoc);
124 bool ParseSectionName(StringRef &SectionName);
129 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
130 unsigned Flags, SectionKind Kind) {
131 if (getLexer().isNot(AsmToken::EndOfStatement))
132 return TokError("unexpected token in section switching directive");
135 getStreamer().SwitchSection(getContext().getELFSection(
136 Section, Type, Flags, Kind));
141 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
143 if (getParser().ParseIdentifier(Name))
144 return TokError("expected identifier in directive");
145 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
147 if (getLexer().isNot(AsmToken::Comma))
148 return TokError("unexpected token in directive");
152 if (getParser().ParseExpression(Expr))
155 if (getLexer().isNot(AsmToken::EndOfStatement))
156 return TokError("unexpected token in directive");
158 getStreamer().EmitELFSize(Sym, Expr);
162 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
163 // A section name can contain -, so we cannot just use
165 SMLoc FirstLoc = getLexer().getLoc();
172 SMLoc PrevLoc = getLexer().getLoc();
173 if (getLexer().is(AsmToken::Minus)) {
175 Lex(); // Consume the "-".
176 } else if (!getParser().ParseIdentifier(Tmp))
177 CurSize = Tmp.size();
182 SectionName = StringRef(FirstLoc.getPointer(), Size);
184 // Make sure the following token is adjacent.
185 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
194 // FIXME: This is a work in progress.
195 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
196 StringRef SectionName;
198 if (ParseSectionName(SectionName))
199 return TokError("expected identifier in directive");
204 if (getLexer().is(AsmToken::Comma)) {
207 if (getLexer().isNot(AsmToken::String))
208 return TokError("expected string in directive");
210 FlagsStr = getTok().getStringContents();
213 AsmToken::TokenKind TypeStartToken;
214 if (getContext().getAsmInfo().getCommentString()[0] == '@')
215 TypeStartToken = AsmToken::Percent;
217 TypeStartToken = AsmToken::At;
219 bool Mergeable = FlagsStr.find('M') != StringRef::npos;
220 bool Group = FlagsStr.find('G') != StringRef::npos;
222 if (getLexer().isNot(AsmToken::Comma)) {
224 return TokError("Mergeable section must specify the type");
226 return TokError("Group section must specify the type");
229 if (getLexer().isNot(TypeStartToken))
230 return TokError("expected the type");
233 if (getParser().ParseIdentifier(TypeName))
234 return TokError("expected identifier in directive");
237 if (getLexer().isNot(AsmToken::Comma))
238 return TokError("expected the entry size");
240 if (getParser().ParseAbsoluteExpression(Size))
243 return TokError("entry size must be positive");
247 if (getLexer().isNot(AsmToken::Comma))
248 return TokError("expected group name");
251 if (getParser().ParseIdentifier(GroupName))
253 if (getLexer().is(AsmToken::Comma)) {
256 if (getParser().ParseIdentifier(Linkage))
258 if (Linkage != "comdat" && Linkage != ".gnu.linkonce")
259 return TokError("Linkage must be 'comdat' or '.gnu.linkonce'");
265 if (getLexer().isNot(AsmToken::EndOfStatement))
266 return TokError("unexpected token in directive");
269 unsigned Type = MCSectionELF::SHT_NULL;
271 // Set the defaults first.
272 if (SectionName == ".fini" || SectionName == ".init") {
273 Type = MCSectionELF::SHT_PROGBITS;
274 Flags |= MCSectionELF::SHF_ALLOC;
275 Flags |= MCSectionELF::SHF_EXECINSTR;
278 for (unsigned i = 0; i < FlagsStr.size(); i++) {
279 switch (FlagsStr[i]) {
281 Flags |= MCSectionELF::SHF_ALLOC;
284 Flags |= MCSectionELF::SHF_EXECINSTR;
287 Flags |= MCSectionELF::SHF_WRITE;
290 Flags |= MCSectionELF::SHF_MERGE;
293 Flags |= MCSectionELF::SHF_STRINGS;
296 Flags |= MCSectionELF::SHF_TLS;
299 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
302 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
307 return TokError("unknown flag");
311 if (!TypeName.empty()) {
312 if (TypeName == "init_array")
313 Type = MCSectionELF::SHT_INIT_ARRAY;
314 else if (TypeName == "fini_array")
315 Type = MCSectionELF::SHT_FINI_ARRAY;
316 else if (TypeName == "preinit_array")
317 Type = MCSectionELF::SHT_PREINIT_ARRAY;
318 else if (TypeName == "nobits")
319 Type = MCSectionELF::SHT_NOBITS;
320 else if (TypeName == "progbits")
321 Type = MCSectionELF::SHT_PROGBITS;
323 return TokError("unknown section type");
326 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
327 ? SectionKind::getText()
328 : SectionKind::getDataRel();
329 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
335 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
336 const MCSection *PreviousSection = getStreamer().getPreviousSection();
337 if (PreviousSection != NULL)
338 getStreamer().SwitchSection(PreviousSection);
343 /// ParseDirectiveELFType
344 /// ::= .type identifier , @attribute
345 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
347 if (getParser().ParseIdentifier(Name))
348 return TokError("expected identifier in directive");
350 // Handle the identifier as the key symbol.
351 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
353 if (getLexer().isNot(AsmToken::Comma))
354 return TokError("unexpected token in '.type' directive");
357 if (getLexer().isNot(AsmToken::At))
358 return TokError("expected '@' before type");
364 TypeLoc = getLexer().getLoc();
365 if (getParser().ParseIdentifier(Type))
366 return TokError("expected symbol type in directive");
368 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
369 .Case("function", MCSA_ELF_TypeFunction)
370 .Case("object", MCSA_ELF_TypeObject)
371 .Case("tls_object", MCSA_ELF_TypeTLS)
372 .Case("common", MCSA_ELF_TypeCommon)
373 .Case("notype", MCSA_ELF_TypeNoType)
374 .Default(MCSA_Invalid);
376 if (Attr == MCSA_Invalid)
377 return Error(TypeLoc, "unsupported attribute in '.type' directive");
379 if (getLexer().isNot(AsmToken::EndOfStatement))
380 return TokError("unexpected token in '.type' directive");
384 getStreamer().EmitSymbolAttribute(Sym, Attr);
389 /// ParseDirectiveIdent
390 /// ::= .ident string
391 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
392 if (getLexer().isNot(AsmToken::String))
393 return TokError("unexpected token in '.ident' directive");
395 StringRef Data = getTok().getIdentifier();
399 const MCSection *OldSection = getStreamer().getCurrentSection();
400 const MCSection *Comment =
401 getContext().getELFSection(".comment", MCSectionELF::SHT_PROGBITS,
402 MCSectionELF::SHF_MERGE |
403 MCSectionELF::SHF_STRINGS,
404 SectionKind::getReadOnly(),
407 static bool First = true;
409 getStreamer().SwitchSection(Comment);
411 getStreamer().EmitIntValue(0, 1);
413 getStreamer().EmitBytes(Data, 0);
414 getStreamer().EmitIntValue(0, 1);
415 getStreamer().SwitchSection(OldSection);
419 /// ParseDirectiveSymver
420 /// ::= .symver foo, bar2@zed
421 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
423 if (getParser().ParseIdentifier(Name))
424 return TokError("expected identifier in directive");
426 if (getLexer().isNot(AsmToken::Comma))
427 return TokError("expected a comma");
432 if (getParser().ParseIdentifier(AliasName))
433 return TokError("expected identifier in directive");
435 if (AliasName.find('@') == StringRef::npos)
436 return TokError("expected a '@' in the name");
438 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
439 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
440 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
442 getStreamer().EmitAssignment(Alias, Value);
448 MCAsmParserExtension *createELFAsmParser() {
449 return new ELFAsmParser;