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/Twine.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCParser/MCAsmLexer.h"
15 #include "llvm/MC/MCSectionELF.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/ADT/Twine.h"
22 class ELFAsmParser : public MCAsmParserExtension {
23 template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
24 void AddDirectiveHandler(StringRef Directive) {
25 getParser().AddDirectiveHandler(this, Directive,
26 HandleDirective<ELFAsmParser, Handler>);
29 bool ParseSectionSwitch(StringRef Section, unsigned Type,
30 unsigned Flags, SectionKind Kind);
35 virtual void Initialize(MCAsmParser &Parser) {
36 // Call the base implementation.
37 this->MCAsmParserExtension::Initialize(Parser);
39 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
40 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
41 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
42 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
43 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
44 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
45 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
46 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
47 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
48 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
49 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
50 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
51 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
54 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
55 // the best way for us to get access to it?
56 bool ParseSectionDirectiveData(StringRef, SMLoc) {
57 bool ret = ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
58 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
59 SectionKind::getDataRel());
60 getStreamer().EmitCodeAlignment(4, 0);
63 bool ParseSectionDirectiveText(StringRef, SMLoc) {
64 bool ret = ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
65 MCSectionELF::SHF_EXECINSTR |
66 MCSectionELF::SHF_ALLOC, SectionKind::getText());
67 getStreamer().EmitCodeAlignment(4, 0);
70 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
71 bool ret = ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
72 MCSectionELF::SHF_WRITE |
73 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
74 getStreamer().EmitCodeAlignment(4, 0);
77 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
78 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
79 MCSectionELF::SHF_ALLOC,
80 SectionKind::getReadOnly());
82 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
83 return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
84 MCSectionELF::SHF_ALLOC |
85 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
86 SectionKind::getThreadData());
88 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
89 return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
90 MCSectionELF::SHF_ALLOC |
91 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
92 SectionKind::getThreadBSS());
94 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
95 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
96 MCSectionELF::SHF_ALLOC |
97 MCSectionELF::SHF_WRITE,
98 SectionKind::getDataRel());
100 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
101 return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
102 MCSectionELF::SHF_ALLOC |
103 MCSectionELF::SHF_WRITE,
104 SectionKind::getReadOnlyWithRel());
106 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
107 return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
108 MCSectionELF::SHF_ALLOC |
109 MCSectionELF::SHF_WRITE,
110 SectionKind::getReadOnlyWithRelLocal());
112 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
113 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
114 MCSectionELF::SHF_ALLOC |
115 MCSectionELF::SHF_WRITE,
116 SectionKind::getDataRel());
118 bool ParseDirectiveSection(StringRef, SMLoc);
119 bool ParseDirectiveSize(StringRef, SMLoc);
120 bool ParseDirectivePrevious(StringRef, SMLoc);
123 bool ParseSectionName(StringRef &SectionName);
128 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
129 unsigned Flags, SectionKind Kind) {
130 if (getLexer().isNot(AsmToken::EndOfStatement))
131 return TokError("unexpected token in section switching directive");
134 getStreamer().SwitchSection(getContext().getELFSection(
135 Section, Type, Flags, Kind));
140 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
142 if (getParser().ParseIdentifier(Name))
143 return TokError("expected identifier in directive");
144 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
146 if (getLexer().isNot(AsmToken::Comma))
147 return TokError("unexpected token in directive");
151 if (getParser().ParseExpression(Expr))
154 if (getLexer().isNot(AsmToken::EndOfStatement))
155 return TokError("unexpected token in directive");
157 getStreamer().EmitELFSize(Sym, Expr);
161 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
162 // A section name can contain -, so we cannot just use
164 SMLoc FirstLoc = getLexer().getLoc();
171 SMLoc PrevLoc = getLexer().getLoc();
172 if (getLexer().is(AsmToken::Minus)) {
174 Lex(); // Consume the "-".
175 } else if (!getParser().ParseIdentifier(Tmp))
176 CurSize = Tmp.size();
181 SectionName = StringRef(FirstLoc.getPointer(), Size);
183 // Make sure the following token is adjacent.
184 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
193 // FIXME: This is a work in progress.
194 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
195 StringRef SectionName;
197 if (ParseSectionName(SectionName))
198 return TokError("expected identifier in directive");
200 std::string FlagsStr;
203 if (getLexer().is(AsmToken::Comma)) {
206 if (getLexer().isNot(AsmToken::String))
207 return TokError("expected string in directive");
209 FlagsStr = getTok().getStringContents();
212 AsmToken::TokenKind TypeStartToken;
213 if (getContext().getAsmInfo().getCommentString()[0] == '@')
214 TypeStartToken = AsmToken::Percent;
216 TypeStartToken = AsmToken::At;
218 if (getLexer().is(AsmToken::Comma)) {
220 if (getLexer().is(TypeStartToken)) {
222 if (getParser().ParseIdentifier(TypeName))
223 return TokError("expected identifier in directive");
225 if (getLexer().is(AsmToken::Comma)) {
228 if (getParser().ParseAbsoluteExpression(Size))
232 return TokError("section size must be positive");
238 if (getLexer().isNot(AsmToken::EndOfStatement))
239 return TokError("unexpected token in directive");
242 for (unsigned i = 0; i < FlagsStr.size(); i++) {
243 switch (FlagsStr[i]) {
245 Flags |= MCSectionELF::SHF_ALLOC;
248 Flags |= MCSectionELF::SHF_EXECINSTR;
251 Flags |= MCSectionELF::SHF_WRITE;
254 Flags |= MCSectionELF::SHF_MERGE;
257 Flags |= MCSectionELF::SHF_STRINGS;
260 Flags |= MCSectionELF::SHF_TLS;
263 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
266 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
269 return TokError("unknown flag");
273 unsigned Type = MCSectionELF::SHT_NULL;
274 if (!TypeName.empty()) {
275 if (TypeName == "init_array")
276 Type = MCSectionELF::SHT_INIT_ARRAY;
277 else if (TypeName == "fini_array")
278 Type = MCSectionELF::SHT_FINI_ARRAY;
279 else if (TypeName == "preinit_array")
280 Type = MCSectionELF::SHT_PREINIT_ARRAY;
281 else if (TypeName == "nobits")
282 Type = MCSectionELF::SHT_NOBITS;
283 else if (TypeName == "progbits")
284 Type = MCSectionELF::SHT_PROGBITS;
286 return TokError("unknown section type");
289 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
290 ? SectionKind::getText()
291 : SectionKind::getDataRel();
292 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
293 Flags, Kind, false));
297 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
298 const MCSection *PreviousSection = getStreamer().getPreviousSection();
299 if (PreviousSection != NULL)
300 getStreamer().SwitchSection(PreviousSection);
307 MCAsmParserExtension *createELFAsmParser() {
308 return new ELFAsmParser;