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