Make sure that names like .note.GNU-stack are accepted as valid section names.
[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 private:
123   bool ParseSectionName(StringRef &SectionName);
124 };
125
126 }
127
128 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
129                                       unsigned Flags, SectionKind Kind) {
130   if (getLexer().isNot(AsmToken::EndOfStatement))
131     return TokError("unexpected token in section switching directive");
132   Lex();
133
134   getStreamer().SwitchSection(getContext().getELFSection(
135                                 Section, Type, Flags, Kind));
136
137   return false;
138 }
139
140 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
141   StringRef Name;
142   if (getParser().ParseIdentifier(Name))
143     return TokError("expected identifier in directive");
144   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);;
145
146   if (getLexer().isNot(AsmToken::Comma))
147     return TokError("unexpected token in directive");
148   Lex();
149
150   const MCExpr *Expr;
151   if (getParser().ParseExpression(Expr))
152     return true;
153
154   if (getLexer().isNot(AsmToken::EndOfStatement))
155     return TokError("unexpected token in directive");
156
157   getStreamer().EmitELFSize(Sym, Expr);
158   return false;
159 }
160
161 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
162   // A section name can contain -, so we cannot just use
163   // ParseIdentifier.
164   SMLoc FirstLoc = getLexer().getLoc();
165   unsigned Size = 0;
166
167   for (;;) {
168     StringRef Tmp;
169     unsigned CurSize;
170
171     SMLoc PrevLoc = getLexer().getLoc();
172     if (getLexer().is(AsmToken::Minus)) {
173       CurSize = 1;
174       Lex(); // Consume the "-".
175     } else if (!getParser().ParseIdentifier(Tmp))
176       CurSize = Tmp.size();
177     else
178       break;
179
180     Size += CurSize;
181     SectionName = StringRef(FirstLoc.getPointer(), Size);
182
183     // Make sure the following token is adjacent.
184     if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
185       break;
186   }
187   if (Size == 0)
188     return true;
189
190   return false;
191 }
192
193 // FIXME: This is a work in progress.
194 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
195   StringRef SectionName;
196
197   if (ParseSectionName(SectionName))
198     return TokError("expected identifier in directive");
199
200   std::string FlagsStr;
201   StringRef TypeName;
202   int64_t Size = 0;
203   if (getLexer().is(AsmToken::Comma)) {
204     Lex();
205
206     if (getLexer().isNot(AsmToken::String))
207       return TokError("expected string in directive");
208
209     FlagsStr = getTok().getStringContents();
210     Lex();
211
212     AsmToken::TokenKind TypeStartToken;
213     if (getContext().getAsmInfo().getCommentString()[0] == '@')
214       TypeStartToken = AsmToken::Percent;
215     else
216       TypeStartToken = AsmToken::At;
217
218     if (getLexer().is(AsmToken::Comma)) {
219       Lex();
220       if (getLexer().is(TypeStartToken)) {
221         Lex();
222         if (getParser().ParseIdentifier(TypeName))
223           return TokError("expected identifier in directive");
224
225         if (getLexer().is(AsmToken::Comma)) {
226           Lex();
227
228           if (getParser().ParseAbsoluteExpression(Size))
229             return true;
230
231           if (Size <= 0)
232             return TokError("section size must be positive");
233         }
234       }
235     }
236   }
237
238   if (getLexer().isNot(AsmToken::EndOfStatement))
239     return TokError("unexpected token in directive");
240
241   unsigned Flags = 0;
242   for (unsigned i = 0; i < FlagsStr.size(); i++) {
243     switch (FlagsStr[i]) {
244     case 'a':
245       Flags |= MCSectionELF::SHF_ALLOC;
246       break;
247     case 'x':
248       Flags |= MCSectionELF::SHF_EXECINSTR;
249       break;
250     case 'w':
251       Flags |= MCSectionELF::SHF_WRITE;
252       break;
253     case 'M':
254       Flags |= MCSectionELF::SHF_MERGE;
255       break;
256     case 'S':
257       Flags |= MCSectionELF::SHF_STRINGS;
258       break;
259     case 'T':
260       Flags |= MCSectionELF::SHF_TLS;
261       break;
262     case 'c':
263       Flags |= MCSectionELF::XCORE_SHF_CP_SECTION;
264       break;
265     case 'd':
266       Flags |= MCSectionELF::XCORE_SHF_DP_SECTION;
267       break;
268     default:
269       return TokError("unknown flag");
270     }
271   }
272
273   unsigned Type = MCSectionELF::SHT_NULL;
274   if (!TypeName.empty()) {
275     if (TypeName == "init_array")
276       Type = MCSectionELF::SHT_INIT_ARRAY;
277     else if (TypeName == "fini_array")
278       Type = MCSectionELF::SHT_FINI_ARRAY;
279     else if (TypeName == "preinit_array")
280       Type = MCSectionELF::SHT_PREINIT_ARRAY;
281     else if (TypeName == "nobits")
282       Type = MCSectionELF::SHT_NOBITS;
283     else if (TypeName == "progbits")
284       Type = MCSectionELF::SHT_PROGBITS;
285     else
286       return TokError("unknown section type");
287   }
288
289   SectionKind Kind = (Flags & MCSectionELF::SHF_EXECINSTR)
290                      ? SectionKind::getText()
291                      : SectionKind::getDataRel();
292   getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
293                                                          Flags, Kind, false));
294   return false;
295 }
296
297 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
298   const MCSection *PreviousSection = getStreamer().getPreviousSection();
299   if (PreviousSection != NULL)
300     getStreamer().SwitchSection(PreviousSection);
301
302   return false;
303 }
304
305 namespace llvm {
306
307 MCAsmParserExtension *createELFAsmParser() {
308   return new ELFAsmParser;
309 }
310
311 }