Add support for .ident.
[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/StringSwitch.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCParser/MCAsmLexer.h"
16 #include "llvm/MC/MCSectionELF.h"
17 #include "llvm/MC/MCStreamer.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     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
53     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
54   }
55
56   // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
57   // the best way for us to get access to it?
58   bool ParseSectionDirectiveData(StringRef, SMLoc) {
59     return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
60                               MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
61                               SectionKind::getDataRel());
62   }
63   bool ParseSectionDirectiveText(StringRef, SMLoc) {
64     return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
65                               MCSectionELF::SHF_EXECINSTR |
66                               MCSectionELF::SHF_ALLOC, SectionKind::getText());
67   }
68   bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
69     return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
70                               MCSectionELF::SHF_WRITE |
71                               MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
72   }
73   bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
74     return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
75                               MCSectionELF::SHF_ALLOC,
76                               SectionKind::getReadOnly());
77   }
78   bool ParseSectionDirectiveTData(StringRef, SMLoc) {
79     return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
80                               MCSectionELF::SHF_ALLOC |
81                               MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
82                               SectionKind::getThreadData());
83   }
84   bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
85     return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
86                               MCSectionELF::SHF_ALLOC |
87                               MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
88                               SectionKind::getThreadBSS());
89   }
90   bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
91     return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
92                               MCSectionELF::SHF_ALLOC |
93                               MCSectionELF::SHF_WRITE,
94                               SectionKind::getDataRel());
95   }
96   bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
97     return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
98                               MCSectionELF::SHF_ALLOC |
99                               MCSectionELF::SHF_WRITE,
100                               SectionKind::getReadOnlyWithRel());
101   }
102   bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
103     return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
104                               MCSectionELF::SHF_ALLOC |
105                               MCSectionELF::SHF_WRITE,
106                               SectionKind::getReadOnlyWithRelLocal());
107   }
108   bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
109     return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
110                               MCSectionELF::SHF_ALLOC |
111                               MCSectionELF::SHF_WRITE,
112                               SectionKind::getDataRel());
113   }
114   bool ParseDirectiveSection(StringRef, SMLoc);
115   bool ParseDirectiveSize(StringRef, SMLoc);
116   bool ParseDirectivePrevious(StringRef, SMLoc);
117   bool ParseDirectiveType(StringRef, SMLoc);
118   bool ParseDirectiveIdent(StringRef, SMLoc);
119
120 private:
121   bool ParseSectionName(StringRef &SectionName);
122 };
123
124 }
125
126 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
127                                       unsigned Flags, SectionKind Kind) {
128   if (getLexer().isNot(AsmToken::EndOfStatement))
129     return TokError("unexpected token in section switching directive");
130   Lex();
131
132   getStreamer().SwitchSection(getContext().getELFSection(
133                                 Section, Type, Flags, Kind));
134
135   return false;
136 }
137
138 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
139   StringRef Name;
140   if (getParser().ParseIdentifier(Name))
141     return TokError("expected identifier in directive");
142   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
143
144   if (getLexer().isNot(AsmToken::Comma))
145     return TokError("unexpected token in directive");
146   Lex();
147
148   const MCExpr *Expr;
149   if (getParser().ParseExpression(Expr))
150     return true;
151
152   if (getLexer().isNot(AsmToken::EndOfStatement))
153     return TokError("unexpected token in directive");
154
155   getStreamer().EmitELFSize(Sym, Expr);
156   return false;
157 }
158
159 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
160   // A section name can contain -, so we cannot just use
161   // ParseIdentifier.
162   SMLoc FirstLoc = getLexer().getLoc();
163   unsigned Size = 0;
164
165   for (;;) {
166     StringRef Tmp;
167     unsigned CurSize;
168
169     SMLoc PrevLoc = getLexer().getLoc();
170     if (getLexer().is(AsmToken::Minus)) {
171       CurSize = 1;
172       Lex(); // Consume the "-".
173     } else if (!getParser().ParseIdentifier(Tmp))
174       CurSize = Tmp.size();
175     else
176       break;
177
178     Size += CurSize;
179     SectionName = StringRef(FirstLoc.getPointer(), Size);
180
181     // Make sure the following token is adjacent.
182     if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
183       break;
184   }
185   if (Size == 0)
186     return true;
187
188   return false;
189 }
190
191 // FIXME: This is a work in progress.
192 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
193   StringRef SectionName;
194
195   if (ParseSectionName(SectionName))
196     return TokError("expected identifier in directive");
197
198   std::string FlagsStr;
199   StringRef TypeName;
200   int64_t Size = 0;
201   if (getLexer().is(AsmToken::Comma)) {
202     Lex();
203
204     if (getLexer().isNot(AsmToken::String))
205       return TokError("expected string in directive");
206
207     FlagsStr = getTok().getStringContents();
208     Lex();
209
210     AsmToken::TokenKind TypeStartToken;
211     if (getContext().getAsmInfo().getCommentString()[0] == '@')
212       TypeStartToken = AsmToken::Percent;
213     else
214       TypeStartToken = AsmToken::At;
215
216     if (getLexer().is(AsmToken::Comma)) {
217       Lex();
218       if (getLexer().is(TypeStartToken)) {
219         Lex();
220         if (getParser().ParseIdentifier(TypeName))
221           return TokError("expected identifier in directive");
222
223         if (getLexer().is(AsmToken::Comma)) {
224           Lex();
225
226           if (getParser().ParseAbsoluteExpression(Size))
227             return true;
228
229           if (Size <= 0)
230             return TokError("section size must be positive");
231         }
232       }
233     }
234   }
235
236   if (getLexer().isNot(AsmToken::EndOfStatement))
237     return TokError("unexpected token in directive");
238
239   unsigned Flags = 0;
240   for (unsigned i = 0; i < FlagsStr.size(); i++) {
241     switch (FlagsStr[i]) {
242     case 'a':
243       Flags |= MCSectionELF::SHF_ALLOC;
244       break;
245     case 'x':
246       Flags |= MCSectionELF::SHF_EXECINSTR;
247       break;
248     case 'w':
249       Flags |= MCSectionELF::SHF_WRITE;
250       break;
251     case 'M':
252       Flags |= MCSectionELF::SHF_MERGE;
253       break;
254     case 'S':
255       Flags |= MCSectionELF::SHF_STRINGS;
256       break;
257     case 'T':
258       Flags |= MCSectionELF::SHF_TLS;
259       break;
260     case 'c':
261       Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
262       break;
263     case 'd':
264       Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
265       break;
266     default:
267       return TokError("unknown flag");
268     }
269   }
270
271   unsigned Type = MCSectionELF::SHT_NULL;
272   if (!TypeName.empty()) {
273     if (TypeName == "init_array")
274       Type = MCSectionELF::SHT_INIT_ARRAY;
275     else if (TypeName == "fini_array")
276       Type = MCSectionELF::SHT_FINI_ARRAY;
277     else if (TypeName == "preinit_array")
278       Type = MCSectionELF::SHT_PREINIT_ARRAY;
279     else if (TypeName == "nobits")
280       Type = MCSectionELF::SHT_NOBITS;
281     else if (TypeName == "progbits")
282       Type = MCSectionELF::SHT_PROGBITS;
283     else
284       return TokError("unknown section type");
285   }
286
287   SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
288                      ? SectionKind::getText()
289                      : SectionKind::getDataRel();
290   getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
291                                                          Flags, Kind, false,
292                                                          Size));
293   return false;
294 }
295
296 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
297   const MCSection *PreviousSection = getStreamer().getPreviousSection();
298   if (PreviousSection != NULL)
299     getStreamer().SwitchSection(PreviousSection);
300
301   return false;
302 }
303
304 /// ParseDirectiveELFType
305 ///  ::= .type identifier , @attribute
306 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
307   StringRef Name;
308   if (getParser().ParseIdentifier(Name))
309     return TokError("expected identifier in directive");
310
311   // Handle the identifier as the key symbol.
312   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
313
314   if (getLexer().isNot(AsmToken::Comma))
315     return TokError("unexpected token in '.type' directive");
316   Lex();
317
318   if (getLexer().isNot(AsmToken::At))
319     return TokError("expected '@' before type");
320   Lex();
321
322   StringRef Type;
323   SMLoc TypeLoc;
324
325   TypeLoc = getLexer().getLoc();
326   if (getParser().ParseIdentifier(Type))
327     return TokError("expected symbol type in directive");
328
329   MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
330     .Case("function", MCSA_ELF_TypeFunction)
331     .Case("object", MCSA_ELF_TypeObject)
332     .Case("tls_object", MCSA_ELF_TypeTLS)
333     .Case("common", MCSA_ELF_TypeCommon)
334     .Case("notype", MCSA_ELF_TypeNoType)
335     .Default(MCSA_Invalid);
336
337   if (Attr == MCSA_Invalid)
338     return Error(TypeLoc, "unsupported attribute in '.type' directive");
339
340   if (getLexer().isNot(AsmToken::EndOfStatement))
341     return TokError("unexpected token in '.type' directive");
342
343   Lex();
344
345   getStreamer().EmitSymbolAttribute(Sym, Attr);
346
347   return false;
348 }
349
350 /// ParseDirectiveIdent
351 ///  ::= .ident string
352 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
353   if (getLexer().isNot(AsmToken::String))
354     return TokError("unexpected token in '.ident' directive");
355
356   StringRef Data = getTok().getIdentifier();
357
358   Lex();
359
360   const MCSection *OldSection = getStreamer().getCurrentSection();
361   const MCSection *Comment =
362     getContext().getELFSection(".comment", MCSectionELF::SHT_PROGBITS,
363                                MCSectionELF::SHF_MERGE |
364                                MCSectionELF::SHF_STRINGS,
365                                SectionKind::getReadOnly(),
366                                false, 1);
367
368   static bool First = true;
369
370   getStreamer().SwitchSection(Comment);
371   if (First)
372     getStreamer().EmitIntValue(0, 1);
373   First = false;
374   getStreamer().EmitBytes(Data, 0);
375   getStreamer().EmitIntValue(0, 1);
376   getStreamer().SwitchSection(OldSection);
377   return false;
378 }
379
380 namespace llvm {
381
382 MCAsmParserExtension *createELFAsmParser() {
383   return new ELFAsmParser;
384 }
385
386 }