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();
175 SMLoc PrevLoc = getLexer().getLoc();
176 if (getLexer().is(AsmToken::Minus)) {
178 Lex(); // Consume the "-".
179 } else if (!getParser().ParseIdentifier(Tmp))
180 CurSize = Tmp.size();
185 SectionName = StringRef(FirstLoc.getPointer(), Size);
187 // Make sure the following token is adjacent.
188 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
197 static SectionKind computeSectionKind(unsigned Flags) {
198 if (Flags & ELF::SHF_EXECINSTR)
199 return SectionKind::getText();
200 if (Flags & ELF::SHF_TLS)
201 return SectionKind::getThreadData();
202 return SectionKind::getDataRel();
205 static int parseSectionFlags(StringRef flagsStr) {
208 for (unsigned i = 0; i < flagsStr.size(); i++) {
209 switch (flagsStr[i]) {
211 flags |= ELF::SHF_ALLOC;
214 flags |= ELF::SHF_EXECINSTR;
217 flags |= ELF::SHF_WRITE;
220 flags |= ELF::SHF_MERGE;
223 flags |= ELF::SHF_STRINGS;
226 flags |= ELF::SHF_TLS;
229 flags |= ELF::XCORE_SHF_CP_SECTION;
232 flags |= ELF::XCORE_SHF_DP_SECTION;
235 flags |= ELF::SHF_GROUP;
245 // FIXME: This is a work in progress.
246 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
247 StringRef SectionName;
249 if (ParseSectionName(SectionName))
250 return TokError("expected identifier in directive");
257 // Set the defaults first.
258 if (SectionName == ".fini" || SectionName == ".init" ||
259 SectionName == ".rodata")
260 Flags |= ELF::SHF_ALLOC;
261 if (SectionName == ".fini" || SectionName == ".init")
262 Flags |= ELF::SHF_EXECINSTR;
264 if (getLexer().is(AsmToken::Comma)) {
267 if (getLexer().isNot(AsmToken::String))
268 return TokError("expected string in directive");
270 StringRef FlagsStr = getTok().getStringContents();
273 int extraFlags = parseSectionFlags(FlagsStr);
275 return TokError("unknown flag");
278 bool Mergeable = Flags & ELF::SHF_MERGE;
279 bool Group = Flags & ELF::SHF_GROUP;
281 if (getLexer().isNot(AsmToken::Comma)) {
283 return TokError("Mergeable section must specify the type");
285 return TokError("Group section must specify the type");
288 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
289 return TokError("expected '@' or '%' before type");
292 if (getParser().ParseIdentifier(TypeName))
293 return TokError("expected identifier in directive");
296 if (getLexer().isNot(AsmToken::Comma))
297 return TokError("expected the entry size");
299 if (getParser().ParseAbsoluteExpression(Size))
302 return TokError("entry size must be positive");
306 if (getLexer().isNot(AsmToken::Comma))
307 return TokError("expected group name");
309 if (getParser().ParseIdentifier(GroupName))
311 if (getLexer().is(AsmToken::Comma)) {
314 if (getParser().ParseIdentifier(Linkage))
316 if (Linkage != "comdat")
317 return TokError("Linkage must be 'comdat'");
323 if (getLexer().isNot(AsmToken::EndOfStatement))
324 return TokError("unexpected token in directive");
326 unsigned Type = ELF::SHT_PROGBITS;
328 if (!TypeName.empty()) {
329 if (TypeName == "init_array")
330 Type = ELF::SHT_INIT_ARRAY;
331 else if (TypeName == "fini_array")
332 Type = ELF::SHT_FINI_ARRAY;
333 else if (TypeName == "preinit_array")
334 Type = ELF::SHT_PREINIT_ARRAY;
335 else if (TypeName == "nobits")
336 Type = ELF::SHT_NOBITS;
337 else if (TypeName == "progbits")
338 Type = ELF::SHT_PROGBITS;
339 else if (TypeName == "note")
340 Type = ELF::SHT_NOTE;
342 return TokError("unknown section type");
345 SectionKind Kind = computeSectionKind(Flags);
346 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
352 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
353 const MCSection *PreviousSection = getStreamer().getPreviousSection();
354 if (PreviousSection != NULL)
355 getStreamer().SwitchSection(PreviousSection);
360 /// ParseDirectiveELFType
361 /// ::= .type identifier , @attribute
362 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
364 if (getParser().ParseIdentifier(Name))
365 return TokError("expected identifier in directive");
367 // Handle the identifier as the key symbol.
368 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
370 if (getLexer().isNot(AsmToken::Comma))
371 return TokError("unexpected token in '.type' directive");
374 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
375 return TokError("expected '@' or '%' before type");
381 TypeLoc = getLexer().getLoc();
382 if (getParser().ParseIdentifier(Type))
383 return TokError("expected symbol type in directive");
385 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
386 .Case("function", MCSA_ELF_TypeFunction)
387 .Case("object", MCSA_ELF_TypeObject)
388 .Case("tls_object", MCSA_ELF_TypeTLS)
389 .Case("common", MCSA_ELF_TypeCommon)
390 .Case("notype", MCSA_ELF_TypeNoType)
391 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
392 .Default(MCSA_Invalid);
394 if (Attr == MCSA_Invalid)
395 return Error(TypeLoc, "unsupported attribute in '.type' directive");
397 if (getLexer().isNot(AsmToken::EndOfStatement))
398 return TokError("unexpected token in '.type' directive");
402 getStreamer().EmitSymbolAttribute(Sym, Attr);
407 /// ParseDirectiveIdent
408 /// ::= .ident string
409 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
410 if (getLexer().isNot(AsmToken::String))
411 return TokError("unexpected token in '.ident' directive");
413 StringRef Data = getTok().getIdentifier();
417 const MCSection *OldSection = getStreamer().getCurrentSection();
418 const MCSection *Comment =
419 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
422 SectionKind::getReadOnly(),
425 static bool First = true;
427 getStreamer().SwitchSection(Comment);
429 getStreamer().EmitIntValue(0, 1);
431 getStreamer().EmitBytes(Data, 0);
432 getStreamer().EmitIntValue(0, 1);
433 getStreamer().SwitchSection(OldSection);
437 /// ParseDirectiveSymver
438 /// ::= .symver foo, bar2@zed
439 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
441 if (getParser().ParseIdentifier(Name))
442 return TokError("expected identifier in directive");
444 if (getLexer().isNot(AsmToken::Comma))
445 return TokError("expected a comma");
450 if (getParser().ParseIdentifier(AliasName))
451 return TokError("expected identifier in directive");
453 if (AliasName.find('@') == StringRef::npos)
454 return TokError("expected a '@' in the name");
456 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
457 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
458 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
460 getStreamer().EmitAssignment(Alias, Value);
464 /// ParseDirectiveWeakref
465 /// ::= .weakref foo, bar
466 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
467 // FIXME: Share code with the other alias building directives.
470 if (getParser().ParseIdentifier(AliasName))
471 return TokError("expected identifier in directive");
473 if (getLexer().isNot(AsmToken::Comma))
474 return TokError("expected a comma");
479 if (getParser().ParseIdentifier(Name))
480 return TokError("expected identifier in directive");
482 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
484 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
486 getStreamer().EmitWeakReference(Alias, Sym);
492 MCAsmParserExtension *createELFAsmParser() {
493 return new ELFAsmParser;