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/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCParser/MCAsmLexer.h"
14 #include "llvm/MC/MCSectionELF.h"
15 #include "llvm/MC/MCStreamer.h"
20 class ELFAsmParser : public MCAsmParserExtension {
21 bool ParseSectionSwitch(StringRef Section, unsigned Type,
22 unsigned Flags, SectionKind Kind);
27 virtual void Initialize(MCAsmParser &Parser) {
28 // Call the base implementation.
29 this->MCAsmParserExtension::Initialize(Parser);
31 Parser.AddDirectiveHandler(this, ".data", MCAsmParser::DirectiveHandler(
32 &ELFAsmParser::ParseSectionDirectiveData));
33 Parser.AddDirectiveHandler(this, ".text", MCAsmParser::DirectiveHandler(
34 &ELFAsmParser::ParseSectionDirectiveText));
35 Parser.AddDirectiveHandler(this, ".section", MCAsmParser::DirectiveHandler(
36 &ELFAsmParser::ParseDirectiveSection));
37 Parser.AddDirectiveHandler(this, ".size", MCAsmParser::DirectiveHandler(
38 &ELFAsmParser::ParseDirectiveSize));
41 bool ParseSectionDirectiveData(StringRef, SMLoc) {
42 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
43 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
44 SectionKind::getDataRel());
46 bool ParseSectionDirectiveText(StringRef, SMLoc) {
47 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
48 MCSectionELF::SHF_EXECINSTR |
49 MCSectionELF::SHF_ALLOC, SectionKind::getText());
51 bool ParseDirectiveSection(StringRef, SMLoc);
52 bool ParseDirectiveSize(StringRef, SMLoc);
57 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
58 unsigned Flags, SectionKind Kind) {
59 if (getLexer().isNot(AsmToken::EndOfStatement))
60 return TokError("unexpected token in section switching directive");
63 getStreamer().SwitchSection(getContext().getELFSection(
64 Section, Type, Flags, Kind));
69 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
71 if (getParser().ParseIdentifier(Name))
72 return TokError("expected identifier in directive");
73 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
75 if (getLexer().isNot(AsmToken::Comma))
76 return TokError("unexpected token in directive");
80 if (getParser().ParseExpression(Expr))
83 if (getLexer().isNot(AsmToken::EndOfStatement))
84 return TokError("unexpected token in directive");
86 getStreamer().EmitELFSize(Sym, Expr);
90 // FIXME: This is a work in progress.
91 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
92 StringRef SectionName;
93 // FIXME: This doesn't parse section names like ".note.GNU-stack" correctly.
94 if (getParser().ParseIdentifier(SectionName))
95 return TokError("expected identifier in directive");
100 if (getLexer().is(AsmToken::Comma)) {
103 if (getLexer().isNot(AsmToken::String))
104 return TokError("expected string in directive");
106 FlagsStr = getTok().getStringContents();
109 AsmToken::TokenKind TypeStartToken;
110 if (getContext().getAsmInfo().getCommentString()[0] == '@')
111 TypeStartToken = AsmToken::Percent;
113 TypeStartToken = AsmToken::At;
115 if (getLexer().is(AsmToken::Comma)) {
117 if (getLexer().is(TypeStartToken)) {
119 if (getParser().ParseIdentifier(TypeName))
120 return TokError("expected identifier in directive");
122 if (getLexer().is(AsmToken::Comma)) {
125 if (getParser().ParseAbsoluteExpression(Size))
129 return TokError("section size must be positive");
135 if (getLexer().isNot(AsmToken::EndOfStatement))
136 return TokError("unexpected token in directive");
139 for (unsigned i = 0; i < FlagsStr.size(); i++) {
140 switch (FlagsStr[i]) {
142 Flags |= MCSectionELF::SHF_ALLOC;
145 Flags |= MCSectionELF::SHF_EXECINSTR;
148 Flags |= MCSectionELF::SHF_WRITE;
151 Flags |= MCSectionELF::SHF_MERGE;
154 Flags |= MCSectionELF::SHF_STRINGS;
157 Flags |= MCSectionELF::SHF_TLS;
160 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
163 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
166 return TokError("unknown flag");
170 unsigned Type = MCSectionELF::SHT_NULL;
171 if (!TypeName.empty()) {
172 if (TypeName == "init_array")
173 Type = MCSectionELF::SHT_INIT_ARRAY;
174 else if (TypeName == "fini_array")
175 Type = MCSectionELF::SHT_FINI_ARRAY;
176 else if (TypeName == "preinit_array")
177 Type = MCSectionELF::SHT_PREINIT_ARRAY;
178 else if (TypeName == "nobits")
179 Type = MCSectionELF::SHT_NOBITS;
180 else if (TypeName == "progbits")
181 Type = MCSectionELF::SHT_PROGBITS;
183 return TokError("unknown section type");
186 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
187 ? SectionKind::getText()
188 : SectionKind::getDataRel();
189 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
190 Flags, Kind, false));
196 MCAsmParserExtension *createELFAsmParser() {
197 return new ELFAsmParser;