Add support for leb128 of absolute expressions.
[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   bool ParseSectionDirectiveData(StringRef, SMLoc) {
55     return ParseSectionSwitch(".data", MCSectionELF::SHT_PROGBITS,
56                               MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
57                               SectionKind::getDataRel());
58   }
59   bool ParseSectionDirectiveText(StringRef, SMLoc) {
60     return ParseSectionSwitch(".text", MCSectionELF::SHT_PROGBITS,
61                               MCSectionELF::SHF_EXECINSTR |
62                               MCSectionELF::SHF_ALLOC, SectionKind::getText());
63   }
64   bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
65     return ParseSectionSwitch(".bss", MCSectionELF::SHT_NOBITS,
66                               MCSectionELF::SHF_WRITE |
67                               MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
68   }
69   bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
70     return ParseSectionSwitch(".rodata", MCSectionELF::SHT_PROGBITS,
71                               MCSectionELF::SHF_ALLOC,
72                               SectionKind::getReadOnly());
73   }
74   bool ParseSectionDirectiveTData(StringRef, SMLoc) {
75     return ParseSectionSwitch(".tdata", MCSectionELF::SHT_PROGBITS,
76                               MCSectionELF::SHF_ALLOC |
77                               MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
78                               SectionKind::getThreadData());
79   }
80   bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
81     return ParseSectionSwitch(".tbss", MCSectionELF::SHT_NOBITS,
82                               MCSectionELF::SHF_ALLOC |
83                               MCSectionELF::SHF_TLS | MCSectionELF::SHF_WRITE,
84                               SectionKind::getThreadBSS());
85   }
86   bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
87     return ParseSectionSwitch(".data.rel", MCSectionELF::SHT_PROGBITS,
88                               MCSectionELF::SHF_ALLOC |
89                               MCSectionELF::SHF_WRITE,
90                               SectionKind::getDataRel());
91   }
92   bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
93     return ParseSectionSwitch(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
94                               MCSectionELF::SHF_ALLOC |
95                               MCSectionELF::SHF_WRITE,
96                               SectionKind::getReadOnlyWithRel());
97   }
98   bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
99     return ParseSectionSwitch(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
100                               MCSectionELF::SHF_ALLOC |
101                               MCSectionELF::SHF_WRITE,
102                               SectionKind::getReadOnlyWithRelLocal());
103   }
104   bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
105     return ParseSectionSwitch(".eh_frame", MCSectionELF::SHT_PROGBITS,
106                               MCSectionELF::SHF_ALLOC |
107                               MCSectionELF::SHF_WRITE,
108                               SectionKind::getDataRel());
109   }
110   bool ParseDirectiveSection(StringRef, SMLoc);
111   bool ParseDirectiveSize(StringRef, SMLoc);
112   bool ParseDirectivePrevious(StringRef, SMLoc);
113 };
114
115 }
116
117 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
118                                       unsigned Flags, SectionKind Kind) {
119   if (getLexer().isNot(AsmToken::EndOfStatement))
120     return TokError("unexpected token in section switching directive");
121   Lex();
122
123   getStreamer().SwitchSection(getContext().getELFSection(
124                                 Section, Type, Flags, Kind));
125
126   return false;
127 }
128
129 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
130   StringRef Name;
131   if (getParser().ParseIdentifier(Name))
132     return TokError("expected identifier in directive");
133   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
134
135   if (getLexer().isNot(AsmToken::Comma))
136     return TokError("unexpected token in directive");
137   Lex();
138
139   const MCExpr *Expr;
140   if (getParser().ParseExpression(Expr))
141     return true;
142
143   if (getLexer().isNot(AsmToken::EndOfStatement))
144     return TokError("unexpected token in directive");
145
146   getStreamer().EmitELFSize(Sym, Expr);
147   return false;
148 }
149
150 // FIXME: This is a work in progress.
151 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
152   StringRef SectionName;
153   // FIXME: This doesn't parse section names like ".note.GNU-stack" correctly.
154   if (getParser().ParseIdentifier(SectionName))
155     return TokError("expected identifier in directive");
156
157   std::string FlagsStr;
158   StringRef TypeName;
159   int64_t Size = 0;
160   if (getLexer().is(AsmToken::Comma)) {
161     Lex();
162
163     if (getLexer().isNot(AsmToken::String))
164       return TokError("expected string in directive");
165
166     FlagsStr = getTok().getStringContents();
167     Lex();
168
169     AsmToken::TokenKind TypeStartToken;
170     if (getContext().getAsmInfo().getCommentString()[0] == '@')
171       TypeStartToken = AsmToken::Percent;
172     else
173       TypeStartToken = AsmToken::At;
174
175     if (getLexer().is(AsmToken::Comma)) {
176       Lex();
177       if (getLexer().is(TypeStartToken)) {
178         Lex();
179         if (getParser().ParseIdentifier(TypeName))
180           return TokError("expected identifier in directive");
181
182         if (getLexer().is(AsmToken::Comma)) {
183           Lex();
184
185           if (getParser().ParseAbsoluteExpression(Size))
186             return true;
187
188           if (Size <= 0)
189             return TokError("section size must be positive");
190         }
191       }
192     }
193   }
194
195   if (getLexer().isNot(AsmToken::EndOfStatement))
196     return TokError("unexpected token in directive");
197
198   unsigned Flags = 0;
199   for (unsigned i = 0; i < FlagsStr.size(); i++) {
200     switch (FlagsStr[i]) {
201     case 'a':
202       Flags |= MCSectionELF::SHF_ALLOC;
203       break;
204     case 'x':
205       Flags |= MCSectionELF::SHF_EXECINSTR;
206       break;
207     case 'w':
208       Flags |= MCSectionELF::SHF_WRITE;
209       break;
210     case 'M':
211       Flags |= MCSectionELF::SHF_MERGE;
212       break;
213     case 'S':
214       Flags |= MCSectionELF::SHF_STRINGS;
215       break;
216     case 'T':
217       Flags |= MCSectionELF::SHF_TLS;
218       break;
219     case 'c':
220       Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
221       break;
222     case 'd':
223       Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
224       break;
225     default:
226       return TokError("unknown flag");
227     }
228   }
229
230   unsigned Type = MCSectionELF::SHT_NULL;
231   if (!TypeName.empty()) {
232     if (TypeName == "init_array")
233       Type = MCSectionELF::SHT_INIT_ARRAY;
234     else if (TypeName == "fini_array")
235       Type = MCSectionELF::SHT_FINI_ARRAY;
236     else if (TypeName == "preinit_array")
237       Type = MCSectionELF::SHT_PREINIT_ARRAY;
238     else if (TypeName == "nobits")
239       Type = MCSectionELF::SHT_NOBITS;
240     else if (TypeName == "progbits")
241       Type = MCSectionELF::SHT_PROGBITS;
242     else
243       return TokError("unknown section type");
244   }
245
246   SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
247                      ? SectionKind::getText()
248                      : SectionKind::getDataRel();
249   getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
250                                                          Flags, Kind, false));
251   return false;
252 }
253
254 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
255   const MCSection *PreviousSection = getStreamer().getPreviousSection();
256   if (PreviousSection != NULL)
257     getStreamer().SwitchSection(PreviousSection);
258
259   return false;
260 }
261
262 namespace llvm {
263
264 MCAsmParserExtension *createELFAsmParser() {
265   return new ELFAsmParser;
266 }
267
268 }