Bug#9172: Don't use static in file scope, use an attribute on the
[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/MCExpr.h"
16 #include "llvm/MC/MCParser/MCAsmLexer.h"
17 #include "llvm/MC/MCSectionELF.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/Support/ELF.h"
20 using namespace llvm;
21
22 namespace {
23
24 class ELFAsmParser : public MCAsmParserExtension {
25   template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
26   void AddDirectiveHandler(StringRef Directive) {
27     getParser().AddDirectiveHandler(this, Directive,
28                                     HandleDirective<ELFAsmParser, Handler>);
29   }
30
31   bool ParseSectionSwitch(StringRef Section, unsigned Type,
32                           unsigned Flags, SectionKind Kind);
33   bool SeenIdent;
34
35 public:
36   ELFAsmParser() : SeenIdent(false) {}
37
38   virtual void Initialize(MCAsmParser &Parser) {
39     // Call the base implementation.
40     this->MCAsmParserExtension::Initialize(Parser);
41
42     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
43     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
44     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
45     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
46     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
47     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
48     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
49     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
50     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
51     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
52     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
53     AddDirectiveHandler<&ELFAsmParser::ParseDirectivePushSection>(".pushsection");
54     AddDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
55     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
56     AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
57     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
58     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
59     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
60     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
61   }
62
63   // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
64   // the best way for us to get access to it?
65   bool ParseSectionDirectiveData(StringRef, SMLoc) {
66     return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
67                               ELF::SHF_WRITE |ELF::SHF_ALLOC,
68                               SectionKind::getDataRel());
69   }
70   bool ParseSectionDirectiveText(StringRef, SMLoc) {
71     return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
72                               ELF::SHF_EXECINSTR |
73                               ELF::SHF_ALLOC, SectionKind::getText());
74   }
75   bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
76     return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
77                               ELF::SHF_WRITE |
78                               ELF::SHF_ALLOC, SectionKind::getBSS());
79   }
80   bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
81     return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
82                               ELF::SHF_ALLOC,
83                               SectionKind::getReadOnly());
84   }
85   bool ParseSectionDirectiveTData(StringRef, SMLoc) {
86     return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
87                               ELF::SHF_ALLOC |
88                               ELF::SHF_TLS | ELF::SHF_WRITE,
89                               SectionKind::getThreadData());
90   }
91   bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
92     return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
93                               ELF::SHF_ALLOC |
94                               ELF::SHF_TLS | ELF::SHF_WRITE,
95                               SectionKind::getThreadBSS());
96   }
97   bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
98     return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
99                               ELF::SHF_ALLOC |
100                               ELF::SHF_WRITE,
101                               SectionKind::getDataRel());
102   }
103   bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
104     return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
105                               ELF::SHF_ALLOC |
106                               ELF::SHF_WRITE,
107                               SectionKind::getReadOnlyWithRel());
108   }
109   bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
110     return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
111                               ELF::SHF_ALLOC |
112                               ELF::SHF_WRITE,
113                               SectionKind::getReadOnlyWithRelLocal());
114   }
115   bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
116     return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
117                               ELF::SHF_ALLOC |
118                               ELF::SHF_WRITE,
119                               SectionKind::getDataRel());
120   }
121   bool ParseDirectivePushSection(StringRef, SMLoc);
122   bool ParseDirectivePopSection(StringRef, SMLoc);
123   bool ParseDirectiveSection(StringRef, SMLoc);
124   bool ParseDirectiveSize(StringRef, SMLoc);
125   bool ParseDirectivePrevious(StringRef, SMLoc);
126   bool ParseDirectiveType(StringRef, SMLoc);
127   bool ParseDirectiveIdent(StringRef, SMLoc);
128   bool ParseDirectiveSymver(StringRef, SMLoc);
129   bool ParseDirectiveWeakref(StringRef, SMLoc);
130
131 private:
132   bool ParseSectionName(StringRef &SectionName);
133 };
134
135 }
136
137 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
138                                       unsigned Flags, SectionKind Kind) {
139   if (getLexer().isNot(AsmToken::EndOfStatement))
140     return TokError("unexpected token in section switching directive");
141   Lex();
142
143   getStreamer().SwitchSection(getContext().getELFSection(
144                                 Section, Type, Flags, Kind));
145
146   return false;
147 }
148
149 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
150   StringRef Name;
151   if (getParser().ParseIdentifier(Name))
152     return TokError("expected identifier in directive");
153   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
154
155   if (getLexer().isNot(AsmToken::Comma))
156     return TokError("unexpected token in directive");
157   Lex();
158
159   const MCExpr *Expr;
160   if (getParser().ParseExpression(Expr))
161     return true;
162
163   if (getLexer().isNot(AsmToken::EndOfStatement))
164     return TokError("unexpected token in directive");
165
166   getStreamer().EmitELFSize(Sym, Expr);
167   return false;
168 }
169
170 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
171   // A section name can contain -, so we cannot just use
172   // ParseIdentifier.
173   SMLoc FirstLoc = getLexer().getLoc();
174   unsigned Size = 0;
175
176   if (getLexer().is(AsmToken::String)) {
177     SectionName = getTok().getIdentifier();
178     Lex();
179     return false;
180   }
181
182   for (;;) {
183     StringRef Tmp;
184     unsigned CurSize;
185
186     SMLoc PrevLoc = getLexer().getLoc();
187     if (getLexer().is(AsmToken::Minus)) {
188       CurSize = 1;
189       Lex(); // Consume the "-".
190     } else if (getLexer().is(AsmToken::String)) {
191       CurSize = getTok().getIdentifier().size() + 2;
192       Lex();
193     } else if (getLexer().is(AsmToken::Identifier)) {
194       CurSize = getTok().getIdentifier().size();
195       Lex();
196     } else {
197       break;
198     }
199
200     Size += CurSize;
201     SectionName = StringRef(FirstLoc.getPointer(), Size);
202
203     // Make sure the following token is adjacent.
204     if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
205       break;
206   }
207   if (Size == 0)
208     return true;
209
210   return false;
211 }
212
213 static SectionKind computeSectionKind(unsigned Flags) {
214   if (Flags & ELF::SHF_EXECINSTR)
215     return SectionKind::getText();
216   if (Flags & ELF::SHF_TLS)
217     return SectionKind::getThreadData();
218   return SectionKind::getDataRel();
219 }
220
221 static int parseSectionFlags(StringRef flagsStr) {
222   int flags = 0;
223
224   for (unsigned i = 0; i < flagsStr.size(); i++) {
225     switch (flagsStr[i]) {
226     case 'a':
227       flags |= ELF::SHF_ALLOC;
228       break;
229     case 'x':
230       flags |= ELF::SHF_EXECINSTR;
231       break;
232     case 'w':
233       flags |= ELF::SHF_WRITE;
234       break;
235     case 'M':
236       flags |= ELF::SHF_MERGE;
237       break;
238     case 'S':
239       flags |= ELF::SHF_STRINGS;
240       break;
241     case 'T':
242       flags |= ELF::SHF_TLS;
243       break;
244     case 'c':
245       flags |= ELF::XCORE_SHF_CP_SECTION;
246       break;
247     case 'd':
248       flags |= ELF::XCORE_SHF_DP_SECTION;
249       break;
250     case 'G':
251       flags |= ELF::SHF_GROUP;
252       break;
253     default:
254       return -1;
255     }
256   }
257
258   return flags;
259 }
260
261 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
262   getStreamer().PushSection();
263
264   if (ParseDirectiveSection(s, loc)) {
265     getStreamer().PopSection();
266     return true;
267   }
268
269   return false;
270 }
271
272 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
273   if (!getStreamer().PopSection())
274     return TokError(".popsection without corresponding .pushsection");
275   return false;
276 }
277
278 // FIXME: This is a work in progress.
279 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
280   StringRef SectionName;
281
282   if (ParseSectionName(SectionName))
283     return TokError("expected identifier in directive");
284
285   StringRef TypeName;
286   int64_t Size = 0;
287   StringRef GroupName;
288   unsigned Flags = 0;
289
290   // Set the defaults first.
291   if (SectionName == ".fini" || SectionName == ".init" ||
292       SectionName == ".rodata")
293     Flags |= ELF::SHF_ALLOC;
294   if (SectionName == ".fini" || SectionName == ".init")
295     Flags |= ELF::SHF_EXECINSTR;
296
297   if (getLexer().is(AsmToken::Comma)) {
298     Lex();
299
300     if (getLexer().isNot(AsmToken::String))
301       return TokError("expected string in directive");
302
303     StringRef FlagsStr = getTok().getStringContents();
304     Lex();
305
306     int extraFlags = parseSectionFlags(FlagsStr);
307     if (extraFlags < 0)
308       return TokError("unknown flag");
309     Flags |= extraFlags;
310
311     bool Mergeable = Flags & ELF::SHF_MERGE;
312     bool Group = Flags & ELF::SHF_GROUP;
313
314     if (getLexer().isNot(AsmToken::Comma)) {
315       if (Mergeable)
316         return TokError("Mergeable section must specify the type");
317       if (Group)
318         return TokError("Group section must specify the type");
319     } else {
320       Lex();
321       if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
322         return TokError("expected '@' or '%' before type");
323
324       Lex();
325       if (getParser().ParseIdentifier(TypeName))
326         return TokError("expected identifier in directive");
327
328       if (Mergeable) {
329         if (getLexer().isNot(AsmToken::Comma))
330           return TokError("expected the entry size");
331         Lex();
332         if (getParser().ParseAbsoluteExpression(Size))
333           return true;
334         if (Size <= 0)
335           return TokError("entry size must be positive");
336       }
337
338       if (Group) {
339         if (getLexer().isNot(AsmToken::Comma))
340           return TokError("expected group name");
341         Lex();
342         if (getParser().ParseIdentifier(GroupName))
343           return true;
344         if (getLexer().is(AsmToken::Comma)) {
345           Lex();
346           StringRef Linkage;
347           if (getParser().ParseIdentifier(Linkage))
348             return true;
349           if (Linkage != "comdat")
350             return TokError("Linkage must be 'comdat'");
351         }
352       }
353     }
354   }
355
356   if (getLexer().isNot(AsmToken::EndOfStatement))
357     return TokError("unexpected token in directive");
358
359   unsigned Type = ELF::SHT_PROGBITS;
360
361   if (!TypeName.empty()) {
362     if (TypeName == "init_array")
363       Type = ELF::SHT_INIT_ARRAY;
364     else if (TypeName == "fini_array")
365       Type = ELF::SHT_FINI_ARRAY;
366     else if (TypeName == "preinit_array")
367       Type = ELF::SHT_PREINIT_ARRAY;
368     else if (TypeName == "nobits")
369       Type = ELF::SHT_NOBITS;
370     else if (TypeName == "progbits")
371       Type = ELF::SHT_PROGBITS;
372     else if (TypeName == "note")
373       Type = ELF::SHT_NOTE;
374     else if (TypeName == "unwind")
375       Type = ELF::SHT_X86_64_UNWIND;
376     else
377       return TokError("unknown section type");
378   }
379
380   SectionKind Kind = computeSectionKind(Flags);
381   getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
382                                                          Flags, Kind, Size,
383                                                          GroupName));
384   return false;
385 }
386
387 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
388   const MCSection *PreviousSection = getStreamer().getPreviousSection();
389   if (PreviousSection == NULL)
390       return TokError(".previous without corresponding .section");
391   getStreamer().SwitchSection(PreviousSection);
392
393   return false;
394 }
395
396 /// ParseDirectiveELFType
397 ///  ::= .type identifier , @attribute
398 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
399   StringRef Name;
400   if (getParser().ParseIdentifier(Name))
401     return TokError("expected identifier in directive");
402
403   // Handle the identifier as the key symbol.
404   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
405
406   if (getLexer().isNot(AsmToken::Comma))
407     return TokError("unexpected token in '.type' directive");
408   Lex();
409
410   if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
411     return TokError("expected '@' or '%' before type");
412   Lex();
413
414   StringRef Type;
415   SMLoc TypeLoc;
416
417   TypeLoc = getLexer().getLoc();
418   if (getParser().ParseIdentifier(Type))
419     return TokError("expected symbol type in directive");
420
421   MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
422     .Case("function", MCSA_ELF_TypeFunction)
423     .Case("object", MCSA_ELF_TypeObject)
424     .Case("tls_object", MCSA_ELF_TypeTLS)
425     .Case("common", MCSA_ELF_TypeCommon)
426     .Case("notype", MCSA_ELF_TypeNoType)
427     .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
428     .Default(MCSA_Invalid);
429
430   if (Attr == MCSA_Invalid)
431     return Error(TypeLoc, "unsupported attribute in '.type' directive");
432
433   if (getLexer().isNot(AsmToken::EndOfStatement))
434     return TokError("unexpected token in '.type' directive");
435
436   Lex();
437
438   getStreamer().EmitSymbolAttribute(Sym, Attr);
439
440   return false;
441 }
442
443 /// ParseDirectiveIdent
444 ///  ::= .ident string
445 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
446   if (getLexer().isNot(AsmToken::String))
447     return TokError("unexpected token in '.ident' directive");
448
449   StringRef Data = getTok().getIdentifier();
450
451   Lex();
452
453   const MCSection *Comment =
454     getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
455                                ELF::SHF_MERGE |
456                                ELF::SHF_STRINGS,
457                                SectionKind::getReadOnly(),
458                                1, "");
459
460   getStreamer().PushSection();
461   getStreamer().SwitchSection(Comment);
462   if (!SeenIdent) {
463     getStreamer().EmitIntValue(0, 1);
464     SeenIdent = true;
465   }
466   getStreamer().EmitBytes(Data, 0);
467   getStreamer().EmitIntValue(0, 1);
468   getStreamer().PopSection();
469   return false;
470 }
471
472 /// ParseDirectiveSymver
473 ///  ::= .symver foo, bar2@zed
474 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
475   StringRef Name;
476   if (getParser().ParseIdentifier(Name))
477     return TokError("expected identifier in directive");
478
479   if (getLexer().isNot(AsmToken::Comma))
480     return TokError("expected a comma");
481
482   Lex();
483
484   StringRef AliasName;
485   if (getParser().ParseIdentifier(AliasName))
486     return TokError("expected identifier in directive");
487
488   if (AliasName.find('@') == StringRef::npos)
489     return TokError("expected a '@' in the name");
490
491   MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
492   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
493   const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
494
495   getStreamer().EmitAssignment(Alias, Value);
496   return false;
497 }
498
499 /// ParseDirectiveWeakref
500 ///  ::= .weakref foo, bar
501 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
502   // FIXME: Share code with the other alias building directives.
503
504   StringRef AliasName;
505   if (getParser().ParseIdentifier(AliasName))
506     return TokError("expected identifier in directive");
507
508   if (getLexer().isNot(AsmToken::Comma))
509     return TokError("expected a comma");
510
511   Lex();
512
513   StringRef Name;
514   if (getParser().ParseIdentifier(Name))
515     return TokError("expected identifier in directive");
516
517   MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
518
519   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
520
521   getStreamer().EmitWeakReference(Alias, Sym);
522   return false;
523 }
524
525 namespace llvm {
526
527 MCAsmParserExtension *createELFAsmParser() {
528   return new ELFAsmParser;
529 }
530
531 }