blob: 7d8f68f1b0c51b367b8afa10dfe0224d29cdacd6 [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 Espindola0453dd92010-09-27 21:40:27 +000057 return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
Daniel Dunbar5146a092010-07-12 21:23:32 +000058 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
59 SectionKind::getDataRel());
60 }
61 bool ParseSectionDirectiveText(StringRef, SMLoc) {
Rafael Espindola0453dd92010-09-27 21:40:27 +000062 return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
Daniel Dunbar5146a092010-07-12 21:23:32 +000063 MCSectionELF::SHF_EXECINSTR |
64 MCSectionELF::SHF_ALLOC, SectionKind::getText());
65 }
Matt Flemingf525c2a2010-07-20 21:12:46 +000066 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
Rafael Espindola0453dd92010-09-27 21:40:27 +000067 return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
Matt Flemingf525c2a2010-07-20 21:12:46 +000068 MCSectionELF::SHF_WRITE |
69 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
70 }
71 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
72 return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
73 MCSectionELF::SHF_ALLOC,
74 SectionKind::getReadOnly());
75 }
76 bool ParseSectionDirectiveTData(StringRef, SMLoc) {
77 return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
78 MCSectionELF::SHF_ALLOC |
79 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
80 SectionKind::getThreadData());
81 }
82 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
83 return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
84 MCSectionELF::SHF_ALLOC |
85 MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
86 SectionKind::getThreadBSS());
87 }
88 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
89 return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
90 MCSectionELF::SHF_ALLOC |
91 MCSectionELF::SHF_WRITE,
92 SectionKind::getDataRel());
93 }
94 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
95 return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
96 MCSectionELF::SHF_ALLOC |
97 MCSectionELF::SHF_WRITE,
98 SectionKind::getReadOnlyWithRel());
99 }
100 bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
101 return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
102 MCSectionELF::SHF_ALLOC |
103 MCSectionELF::SHF_WRITE,
104 SectionKind::getReadOnlyWithRelLocal());
105 }
106 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
107 return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
108 MCSectionELF::SHF_ALLOC |
109 MCSectionELF::SHF_WRITE,
110 SectionKind::getDataRel());
111 }
Eli Friedman21444ef2010-07-17 04:29:04 +0000112 bool ParseDirectiveSection(StringRef, SMLoc);
113 bool ParseDirectiveSize(StringRef, SMLoc);
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000114 bool ParseDirectivePrevious(StringRef, SMLoc);
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000115
116private:
117 bool ParseSectionName(StringRef &SectionName);
Daniel Dunbar5146a092010-07-12 21:23:32 +0000118};
119
120}
121
122bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
123 unsigned Flags, SectionKind Kind) {
124 if (getLexer().isNot(AsmToken::EndOfStatement))
125 return TokError("unexpected token in section switching directive");
126 Lex();
127
128 getStreamer().SwitchSection(getContext().getELFSection(
129 Section, Type, Flags, Kind));
130
131 return false;
132}
133
Eli Friedman21444ef2010-07-17 04:29:04 +0000134bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
Eli Friedmanf82ccf52010-07-17 03:09:18 +0000135 StringRef Name;
136 if (getParser().ParseIdentifier(Name))
137 return TokError("expected identifier in directive");
138 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
139
140 if (getLexer().isNot(AsmToken::Comma))
141 return TokError("unexpected token in directive");
142 Lex();
143
144 const MCExpr *Expr;
145 if (getParser().ParseExpression(Expr))
146 return true;
147
148 if (getLexer().isNot(AsmToken::EndOfStatement))
149 return TokError("unexpected token in directive");
150
151 getStreamer().EmitELFSize(Sym, Expr);
152 return false;
153}
154
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000155bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
156 // A section name can contain -, so we cannot just use
157 // ParseIdentifier.
158 SMLoc FirstLoc = getLexer().getLoc();
159 unsigned Size = 0;
160
161 for (;;) {
162 StringRef Tmp;
163 unsigned CurSize;
164
165 SMLoc PrevLoc = getLexer().getLoc();
166 if (getLexer().is(AsmToken::Minus)) {
167 CurSize = 1;
168 Lex(); // Consume the "-".
169 } else if (!getParser().ParseIdentifier(Tmp))
170 CurSize = Tmp.size();
171 else
172 break;
173
174 Size += CurSize;
175 SectionName = StringRef(FirstLoc.getPointer(), Size);
176
177 // Make sure the following token is adjacent.
178 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
179 break;
180 }
181 if (Size == 0)
182 return true;
183
184 return false;
185}
186
Eli Friedman21444ef2010-07-17 04:29:04 +0000187// FIXME: This is a work in progress.
188bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
189 StringRef SectionName;
Rafael Espindola34e3d0c2010-09-16 17:05:55 +0000190
191 if (ParseSectionName(SectionName))
Eli Friedman21444ef2010-07-17 04:29:04 +0000192 return TokError("expected identifier in directive");
193
194 std::string FlagsStr;
195 StringRef TypeName;
196 int64_t Size = 0;
197 if (getLexer().is(AsmToken::Comma)) {
198 Lex();
199
200 if (getLexer().isNot(AsmToken::String))
201 return TokError("expected string in directive");
202
203 FlagsStr = getTok().getStringContents();
204 Lex();
205
206 AsmToken::TokenKind TypeStartToken;
207 if (getContext().getAsmInfo().getCommentString()[0] == '@')
208 TypeStartToken = AsmToken::Percent;
209 else
210 TypeStartToken = AsmToken::At;
211
212 if (getLexer().is(AsmToken::Comma)) {
213 Lex();
214 if (getLexer().is(TypeStartToken)) {
215 Lex();
216 if (getParser().ParseIdentifier(TypeName))
217 return TokError("expected identifier in directive");
218
219 if (getLexer().is(AsmToken::Comma)) {
220 Lex();
221
222 if (getParser().ParseAbsoluteExpression(Size))
223 return true;
224
225 if (Size <= 0)
226 return TokError("section size must be positive");
227 }
228 }
229 }
230 }
231
232 if (getLexer().isNot(AsmToken::EndOfStatement))
233 return TokError("unexpected token in directive");
234
235 unsigned Flags = 0;
236 for (unsigned i = 0; i < FlagsStr.size(); i++) {
237 switch (FlagsStr[i]) {
238 case 'a':
239 Flags |= MCSectionELF::SHF_ALLOC;
240 break;
241 case 'x':
242 Flags |= MCSectionELF::SHF_EXECINSTR;
243 break;
244 case 'w':
245 Flags |= MCSectionELF::SHF_WRITE;
246 break;
247 case 'M':
248 Flags |= MCSectionELF::SHF_MERGE;
249 break;
250 case 'S':
251 Flags |= MCSectionELF::SHF_STRINGS;
252 break;
253 case 'T':
254 Flags |= MCSectionELF::SHF_TLS;
255 break;
256 case 'c':
257 Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
258 break;
259 case 'd':
260 Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
261 break;
262 default:
263 return TokError("unknown flag");
264 }
265 }
266
267 unsigned Type = MCSectionELF::SHT_NULL;
268 if (!TypeName.empty()) {
269 if (TypeName == "init_array")
270 Type = MCSectionELF::SHT_INIT_ARRAY;
271 else if (TypeName == "fini_array")
272 Type = MCSectionELF::SHT_FINI_ARRAY;
273 else if (TypeName == "preinit_array")
274 Type = MCSectionELF::SHT_PREINIT_ARRAY;
275 else if (TypeName == "nobits")
276 Type = MCSectionELF::SHT_NOBITS;
277 else if (TypeName == "progbits")
278 Type = MCSectionELF::SHT_PROGBITS;
279 else
280 return TokError("unknown section type");
281 }
282
283 SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
284 ? SectionKind::getText()
285 : SectionKind::getDataRel();
286 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
287 Flags, Kind, false));
288 return false;
289}
290
Benjamin Kramer1674b0b2010-09-02 18:53:37 +0000291bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
292 const MCSection *PreviousSection = getStreamer().getPreviousSection();
293 if (PreviousSection != NULL)
294 getStreamer().SwitchSection(PreviousSection);
295
296 return false;
297}
298
Daniel Dunbar5146a092010-07-12 21:23:32 +0000299namespace llvm {
300
301MCAsmParserExtension *createELFAsmParser() {
302 return new ELFAsmParser;
303}
304
305}