blob: ddf988faa64291774436574841f050fc2c272b75 [file] [log] [blame]
Daniel Dunbar5146a092010-07-12 21:23:32 +00001//===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCParser/MCAsmParserExtension.h"
Eli Friedmandc1ad222010-07-17 06:27:28 +000011#include "llvm/ADT/Twine.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000012#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000013#include "llvm/MC/MCContext.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000014#include "llvm/MC/MCParser/MCAsmLexer.h"
Eli Friedman21444ef2010-07-17 04:29:04 +000015#include "llvm/MC/MCSectionELF.h"
16#include "llvm/MC/MCStreamer.h"
Daniel Dunbarf21e4e92010-07-18 18:31:42 +000017#include "llvm/ADT/Twine.h"
Daniel Dunbar5146a092010-07-12 21:23:32 +000018using namespace llvm;
19
20namespace {
21
22class ELFAsmParser : public MCAsmParserExtension {
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000023 template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
24 void AddDirectiveHandler(StringRef Directive) {
25 getParser().AddDirectiveHandler(this, Directive,
26 HandleDirective<ELFAsmParser, Handler>);
27 }
28
Daniel Dunbar5146a092010-07-12 21:23:32 +000029 bool ParseSectionSwitch(StringRef Section, unsigned Type,
30 unsigned Flags, SectionKind Kind);
31
32public:
33 ELFAsmParser() {}
34
35 virtual void Initialize(MCAsmParser &Parser) {
36 // Call the base implementation.
37 this->MCAsmParserExtension::Initialize(Parser);
38
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000039 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
40 AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
Matt Flemingf525c2a2010-07-20 21:12:46 +000041 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");
Daniel Dunbar1edf6ca2010-07-18 22:22:07 +000049 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
50 AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
Benjamin Kramer1674b0b2010-09-02 18:53:37 +000051 AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
Daniel Dunbar5146a092010-07-12 21:23:32 +000052 }
53
Rafael Espindolad80781b2010-09-15 21:48:40 +000054 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
55 // the best way for us to get access to it?
Daniel Dunbar5146a092010-07-12 21:23:32 +000056 bool ParseSectionDirectiveData(StringRef, SMLoc) {
Rafael Espindolad80781b2010-09-15 21:48:40 +000057 bool ret = ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
Daniel Dunbar5146a092010-07-12 21:23:32 +000058 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
59 SectionKind::getDataRel());
Rafael Espindolad80781b2010-09-15 21:48:40 +000060 getStreamer().EmitCodeAlignment(4, 0);
61 return ret;
Daniel Dunbar5146a092010-07-12 21:23:32 +000062 }
63 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindolad80781b2010-09-15 21:48:40 +000064 bool ret = ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
Daniel Dunbar5146a092010-07-12 21:23:32 +000065 MCSectionELF::SHF_EXECINSTR |
66 MCSectionELF::SHF_ALLOC, SectionKind::getText());
Rafael Espindolad80781b2010-09-15 21:48:40 +000067 getStreamer().EmitCodeAlignment(4, 0);
68 return ret;
Daniel Dunbar5146a092010-07-12 21:23:32 +000069 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000070 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindolad80781b2010-09-15 21:48:40 +000071 bool ret = ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +000072 MCSectionELF::SHF_WRITE |
73 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
Rafael Espindolad80781b2010-09-15 21:48:40 +000074 getStreamer().EmitCodeAlignment(4, 0);
75 return ret;
Matt Flemingf525c2a2010-07-20 21:12:46 +000076 }
77 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
78 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
79 MCSectionELF::SHF_ALLOC,
80 SectionKind::getReadOnly());
81 }
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());
87 }
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());
93 }
94 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
95 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
96 MCSectionELF::SHF_ALLOC |
97 MCSectionELF::SHF_WRITE,
98 SectionKind::getDataRel());
99 }
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());
105 }
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());
111 }
112 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
113 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
114 MCSectionELF::SHF_ALLOC |
115 MCSectionELF::SHF_WRITE,
116 SectionKind::getDataRel());
117 }
Eli Friedman21444ef2010-07-17 04:29:04 +0000118 bool ParseDirectiveSection(StringRef, SMLoc);
119 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000120 bool ParseDirectivePrevious(StringRef, SMLoc);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000121};
122
123}
124
125bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
126 unsigned Flags, SectionKind Kind) {
127 if (getLexer().isNot(AsmToken::EndOfStatement))
128 return TokError("unexpected token in section switching directive");
129 Lex();
130
131 getStreamer().SwitchSection(getContext().getELFSection(
132 Section, Type, Flags, Kind));
133
134 return false;
135}
136
Eli Friedman21444ef2010-07-17 04:29:04 +0000137bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000138 StringRef Name;
139 if (getParser().ParseIdentifier(Name))
140 return TokError("expected identifier in directive");
141 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
142
143 if (getLexer().isNot(AsmToken::Comma))
144 return TokError("unexpected token in directive");
145 Lex();
146
147 const MCExpr *Expr;
148 if (getParser().ParseExpression(Expr))
149 return true;
150
151 if (getLexer().isNot(AsmToken::EndOfStatement))
152 return TokError("unexpected token in directive");
153
154 getStreamer().EmitELFSize(Sym, Expr);
155 return false;
156}
157
Eli Friedman21444ef2010-07-17 04:29:04 +0000158// FIXME: This is a work in progress.
159bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
160 StringRef SectionName;
161 // FIXME: This doesn't parse section names like ".note.GNU-stack" correctly.
162 if (getParser().ParseIdentifier(SectionName))
163 return TokError("expected identifier in directive");
164
165 std::string FlagsStr;
166 StringRef TypeName;
167 int64_t Size = 0;
168 if (getLexer().is(AsmToken::Comma)) {
169 Lex();
170
171 if (getLexer().isNot(AsmToken::String))
172 return TokError("expected string in directive");
173
174 FlagsStr = getTok().getStringContents();
175 Lex();
176
177 AsmToken::TokenKind TypeStartToken;
178 if (getContext().getAsmInfo().getCommentString()[0] == '@')
179 TypeStartToken = AsmToken::Percent;
180 else
181 TypeStartToken = AsmToken::At;
182
183 if (getLexer().is(AsmToken::Comma)) {
184 Lex();
185 if (getLexer().is(TypeStartToken)) {
186 Lex();
187 if (getParser().ParseIdentifier(TypeName))
188 return TokError("expected identifier in directive");
189
190 if (getLexer().is(AsmToken::Comma)) {
191 Lex();
192
193 if (getParser().ParseAbsoluteExpression(Size))
194 return true;
195
196 if (Size <= 0)
197 return TokError("section size must be positive");
198 }
199 }
200 }
201 }
202
203 if (getLexer().isNot(AsmToken::EndOfStatement))
204 return TokError("unexpected token in directive");
205
206 unsigned Flags = 0;
207 for (unsigned i = 0; i < FlagsStr.size(); i++) {
208 switch (FlagsStr[i]) {
209 case 'a':
210 Flags |= MCSectionELF::SHF_ALLOC;
211 break;
212 case 'x':
213 Flags |= MCSectionELF::SHF_EXECINSTR;
214 break;
215 case 'w':
216 Flags |= MCSectionELF::SHF_WRITE;
217 break;
218 case 'M':
219 Flags |= MCSectionELF::SHF_MERGE;
220 break;
221 case 'S':
222 Flags |= MCSectionELF::SHF_STRINGS;
223 break;
224 case 'T':
225 Flags |= MCSectionELF::SHF_TLS;
226 break;
227 case 'c':
228 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
229 break;
230 case 'd':
231 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
232 break;
233 default:
234 return TokError("unknown flag");
235 }
236 }
237
238 unsigned Type = MCSectionELF::SHT_NULL;
239 if (!TypeName.empty()) {
240 if (TypeName == "init_array")
241 Type = MCSectionELF::SHT_INIT_ARRAY;
242 else if (TypeName == "fini_array")
243 Type = MCSectionELF::SHT_FINI_ARRAY;
244 else if (TypeName == "preinit_array")
245 Type = MCSectionELF::SHT_PREINIT_ARRAY;
246 else if (TypeName == "nobits")
247 Type = MCSectionELF::SHT_NOBITS;
248 else if (TypeName == "progbits")
249 Type = MCSectionELF::SHT_PROGBITS;
250 else
251 return TokError("unknown section type");
252 }
253
254 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
255 ? SectionKind::getText()
256 : SectionKind::getDataRel();
257 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
258 Flags, Kind, false));
259 return false;
260}
261
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000262bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
263 const MCSection *PreviousSection = getStreamer().getPreviousSection();
264 if (PreviousSection != NULL)
265 getStreamer().SwitchSection(PreviousSection);
266
267 return false;
268}
269
Daniel Dunbar5146a092010-07-12 21:23:32 +0000270namespace llvm {
271
272MCAsmParserExtension *createELFAsmParser() {
273 return new ELFAsmParser;
274}
275
276}