blob: 072befadd5f333595dc39502c5319cce32e2594e [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);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000121
122private:
123 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000124};
125
126}
127
128bool 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");
132 Lex();
133
134 getStreamer().SwitchSection(getContext().getELFSection(
135 Section, Type, Flags, Kind));
136
137 return false;
138}
139
Eli Friedman21444ef2010-07-17 04:29:04 +0000140bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000141 StringRef Name;
142 if (getParser().ParseIdentifier(Name))
143 return TokError("expected identifier in directive");
144 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
145
146 if (getLexer().isNot(AsmToken::Comma))
147 return TokError("unexpected token in directive");
148 Lex();
149
150 const MCExpr *Expr;
151 if (getParser().ParseExpression(Expr))
152 return true;
153
154 if (getLexer().isNot(AsmToken::EndOfStatement))
155 return TokError("unexpected token in directive");
156
157 getStreamer().EmitELFSize(Sym, Expr);
158 return false;
159}
160
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000161bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
162 // A section name can contain -, so we cannot just use
163 // ParseIdentifier.
164 SMLoc FirstLoc = getLexer().getLoc();
165 unsigned Size = 0;
166
167 for (;;) {
168 StringRef Tmp;
169 unsigned CurSize;
170
171 SMLoc PrevLoc = getLexer().getLoc();
172 if (getLexer().is(AsmToken::Minus)) {
173 CurSize = 1;
174 Lex(); // Consume the "-".
175 } else if (!getParser().ParseIdentifier(Tmp))
176 CurSize = Tmp.size();
177 else
178 break;
179
180 Size += CurSize;
181 SectionName = StringRef(FirstLoc.getPointer(), Size);
182
183 // Make sure the following token is adjacent.
184 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
185 break;
186 }
187 if (Size == 0)
188 return true;
189
190 return false;
191}
192
Eli Friedman21444ef2010-07-17 04:29:04 +0000193// FIXME: This is a work in progress.
194bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
195 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000196
197 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000198 return TokError("expected identifier in directive");
199
200 std::string FlagsStr;
201 StringRef TypeName;
202 int64_t Size = 0;
203 if (getLexer().is(AsmToken::Comma)) {
204 Lex();
205
206 if (getLexer().isNot(AsmToken::String))
207 return TokError("expected string in directive");
208
209 FlagsStr = getTok().getStringContents();
210 Lex();
211
212 AsmToken::TokenKind TypeStartToken;
213 if (getContext().getAsmInfo().getCommentString()[0] == '@')
214 TypeStartToken = AsmToken::Percent;
215 else
216 TypeStartToken = AsmToken::At;
217
218 if (getLexer().is(AsmToken::Comma)) {
219 Lex();
220 if (getLexer().is(TypeStartToken)) {
221 Lex();
222 if (getParser().ParseIdentifier(TypeName))
223 return TokError("expected identifier in directive");
224
225 if (getLexer().is(AsmToken::Comma)) {
226 Lex();
227
228 if (getParser().ParseAbsoluteExpression(Size))
229 return true;
230
231 if (Size <= 0)
232 return TokError("section size must be positive");
233 }
234 }
235 }
236 }
237
238 if (getLexer().isNot(AsmToken::EndOfStatement))
239 return TokError("unexpected token in directive");
240
241 unsigned Flags = 0;
242 for (unsigned i = 0; i < FlagsStr.size(); i++) {
243 switch (FlagsStr[i]) {
244 case 'a':
245 Flags |= MCSectionELF::SHF_ALLOC;
246 break;
247 case 'x':
248 Flags |= MCSectionELF::SHF_EXECINSTR;
249 break;
250 case 'w':
251 Flags |= MCSectionELF::SHF_WRITE;
252 break;
253 case 'M':
254 Flags |= MCSectionELF::SHF_MERGE;
255 break;
256 case 'S':
257 Flags |= MCSectionELF::SHF_STRINGS;
258 break;
259 case 'T':
260 Flags |= MCSectionELF::SHF_TLS;
261 break;
262 case 'c':
263 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
264 break;
265 case 'd':
266 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
267 break;
268 default:
269 return TokError("unknown flag");
270 }
271 }
272
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;
285 else
286 return TokError("unknown section type");
287 }
288
289 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
290 ? SectionKind::getText()
291 : SectionKind::getDataRel();
292 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
293 Flags, Kind, false));
294 return false;
295}
296
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000297bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
298 const MCSection *PreviousSection = getStreamer().getPreviousSection();
299 if (PreviousSection != NULL)
300 getStreamer().SwitchSection(PreviousSection);
301
302 return false;
303}
304
Daniel Dunbar5146a092010-07-12 21:23:32 +0000305namespace llvm {
306
307MCAsmParserExtension *createELFAsmParser() {
308 return new ELFAsmParser;
309}
310
311}