Add a InitSections method to the streamer interface.
[oota-llvm.git] / lib / MC / MCParser / ELFAsmParser.cpp
1 //===- 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"
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"
18 using namespace llvm;
19
20 namespace {
21
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>);
27   }
28
29   bool ParseSectionSwitch(StringRef Section, unsigned Type,
30                           unsigned Flags, SectionKind Kind);
31
32 public:
33   ELFAsmParser() {}
34
35   virtual void Initialize(MCAsmParser &Parser) {
36     // Call the base implementation.
37     this->MCAsmParserExtension::Initialize(Parser);
38
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");
52   }
53
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);
61     return ret;
62   }
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);
68     return ret;
69   }
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);
75     return ret;
76   }
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   }
118   bool ParseDirectiveSection(StringRef, SMLoc);
119   bool ParseDirectiveSize(StringRef, SMLoc);
120   bool ParseDirectivePrevious(StringRef, SMLoc);
121 };
122
123 }
124
125 bool 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
137 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
138   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
158 // FIXME: This is a work in progress.
159 bool 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
262 bool 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
270 namespace llvm {
271
272 MCAsmParserExtension *createELFAsmParser() {
273   return new ELFAsmParser;
274 }
275
276 }