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