ELF: Parse types in directives like binutils gas
[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/MC/MCSymbol.h"
20 #include "llvm/Support/ELF.h"
21 using namespace llvm;
22
23 namespace {
24
25 class ELFAsmParser : public MCAsmParserExtension {
26   template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)>
27   void addDirectiveHandler(StringRef Directive) {
28     MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair(
29         this, HandleDirective<ELFAsmParser, HandlerMethod>);
30
31     getParser().addDirectiveHandler(Directive, Handler);
32   }
33
34   bool ParseSectionSwitch(StringRef Section, unsigned Type,
35                           unsigned Flags, SectionKind Kind);
36   bool SeenIdent;
37
38 public:
39   ELFAsmParser() : SeenIdent(false) {
40     BracketExpressionsSupported = true;
41   }
42
43   virtual void Initialize(MCAsmParser &Parser) {
44     // Call the base implementation.
45     this->MCAsmParserExtension::Initialize(Parser);
46
47     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data");
48     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text");
49     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss");
50     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata");
51     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata");
52     addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss");
53     addDirectiveHandler<
54       &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel");
55     addDirectiveHandler<
56       &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro");
57     addDirectiveHandler<
58       &ELFAsmParser::ParseSectionDirectiveDataRelRoLocal>(".data.rel.ro.local");
59     addDirectiveHandler<
60       &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
61     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
62     addDirectiveHandler<
63       &ELFAsmParser::ParseDirectivePushSection>(".pushsection");
64     addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection");
65     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
66     addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
67     addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type");
68     addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident");
69     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver");
70     addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version");
71     addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref");
72     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
73     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local");
74     addDirectiveHandler<
75       &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected");
76     addDirectiveHandler<
77       &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal");
78     addDirectiveHandler<
79       &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden");
80     addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection");
81   }
82
83   // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
84   // the best way for us to get access to it?
85   bool ParseSectionDirectiveData(StringRef, SMLoc) {
86     return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
87                               ELF::SHF_WRITE |ELF::SHF_ALLOC,
88                               SectionKind::getDataRel());
89   }
90   bool ParseSectionDirectiveText(StringRef, SMLoc) {
91     return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
92                               ELF::SHF_EXECINSTR |
93                               ELF::SHF_ALLOC, SectionKind::getText());
94   }
95   bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
96     return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
97                               ELF::SHF_WRITE |
98                               ELF::SHF_ALLOC, SectionKind::getBSS());
99   }
100   bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
101     return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
102                               ELF::SHF_ALLOC,
103                               SectionKind::getReadOnly());
104   }
105   bool ParseSectionDirectiveTData(StringRef, SMLoc) {
106     return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS,
107                               ELF::SHF_ALLOC |
108                               ELF::SHF_TLS | ELF::SHF_WRITE,
109                               SectionKind::getThreadData());
110   }
111   bool ParseSectionDirectiveTBSS(StringRef, SMLoc) {
112     return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS,
113                               ELF::SHF_ALLOC |
114                               ELF::SHF_TLS | ELF::SHF_WRITE,
115                               SectionKind::getThreadBSS());
116   }
117   bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
118     return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
119                               ELF::SHF_ALLOC |
120                               ELF::SHF_WRITE,
121                               SectionKind::getDataRel());
122   }
123   bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) {
124     return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS,
125                               ELF::SHF_ALLOC |
126                               ELF::SHF_WRITE,
127                               SectionKind::getReadOnlyWithRel());
128   }
129   bool ParseSectionDirectiveDataRelRoLocal(StringRef, SMLoc) {
130     return ParseSectionSwitch(".data.rel.ro.local", ELF::SHT_PROGBITS,
131                               ELF::SHF_ALLOC |
132                               ELF::SHF_WRITE,
133                               SectionKind::getReadOnlyWithRelLocal());
134   }
135   bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
136     return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
137                               ELF::SHF_ALLOC |
138                               ELF::SHF_WRITE,
139                               SectionKind::getDataRel());
140   }
141   bool ParseDirectivePushSection(StringRef, SMLoc);
142   bool ParseDirectivePopSection(StringRef, SMLoc);
143   bool ParseDirectiveSection(StringRef, SMLoc);
144   bool ParseDirectiveSize(StringRef, SMLoc);
145   bool ParseDirectivePrevious(StringRef, SMLoc);
146   bool ParseDirectiveType(StringRef, SMLoc);
147   bool ParseDirectiveIdent(StringRef, SMLoc);
148   bool ParseDirectiveSymver(StringRef, SMLoc);
149   bool ParseDirectiveVersion(StringRef, SMLoc);
150   bool ParseDirectiveWeakref(StringRef, SMLoc);
151   bool ParseDirectiveSymbolAttribute(StringRef, SMLoc);
152   bool ParseDirectiveSubsection(StringRef, SMLoc);
153
154 private:
155   bool ParseSectionName(StringRef &SectionName);
156   bool ParseSectionArguments(bool IsPush);
157 };
158
159 }
160
161 /// ParseDirectiveSymbolAttribute
162 ///  ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ]
163 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
164   MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
165     .Case(".weak", MCSA_Weak)
166     .Case(".local", MCSA_Local)
167     .Case(".hidden", MCSA_Hidden)
168     .Case(".internal", MCSA_Internal)
169     .Case(".protected", MCSA_Protected)
170     .Default(MCSA_Invalid);
171   assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
172   if (getLexer().isNot(AsmToken::EndOfStatement)) {
173     for (;;) {
174       StringRef Name;
175
176       if (getParser().parseIdentifier(Name))
177         return TokError("expected identifier in directive");
178
179       MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
180
181       getStreamer().EmitSymbolAttribute(Sym, Attr);
182
183       if (getLexer().is(AsmToken::EndOfStatement))
184         break;
185
186       if (getLexer().isNot(AsmToken::Comma))
187         return TokError("unexpected token in directive");
188       Lex();
189     }
190   }
191
192   Lex();
193   return false;
194 }
195
196 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type,
197                                       unsigned Flags, SectionKind Kind) {
198   const MCExpr *Subsection = 0;
199   if (getLexer().isNot(AsmToken::EndOfStatement)) {
200     if (getParser().parseExpression(Subsection))
201       return true;
202   }
203
204   getStreamer().SwitchSection(getContext().getELFSection(
205                                 Section, Type, Flags, Kind),
206                               Subsection);
207
208   return false;
209 }
210
211 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
212   StringRef Name;
213   if (getParser().parseIdentifier(Name))
214     return TokError("expected identifier in directive");
215   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
216
217   if (getLexer().isNot(AsmToken::Comma))
218     return TokError("unexpected token in directive");
219   Lex();
220
221   const MCExpr *Expr;
222   if (getParser().parseExpression(Expr))
223     return true;
224
225   if (getLexer().isNot(AsmToken::EndOfStatement))
226     return TokError("unexpected token in directive");
227
228   getStreamer().EmitELFSize(Sym, Expr);
229   return false;
230 }
231
232 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) {
233   // A section name can contain -, so we cannot just use
234   // parseIdentifier.
235   SMLoc FirstLoc = getLexer().getLoc();
236   unsigned Size = 0;
237
238   if (getLexer().is(AsmToken::String)) {
239     SectionName = getTok().getIdentifier();
240     Lex();
241     return false;
242   }
243
244   for (;;) {
245     unsigned CurSize;
246
247     SMLoc PrevLoc = getLexer().getLoc();
248     if (getLexer().is(AsmToken::Minus)) {
249       CurSize = 1;
250       Lex(); // Consume the "-".
251     } else if (getLexer().is(AsmToken::String)) {
252       CurSize = getTok().getIdentifier().size() + 2;
253       Lex();
254     } else if (getLexer().is(AsmToken::Identifier)) {
255       CurSize = getTok().getIdentifier().size();
256       Lex();
257     } else {
258       break;
259     }
260
261     Size += CurSize;
262     SectionName = StringRef(FirstLoc.getPointer(), Size);
263
264     // Make sure the following token is adjacent.
265     if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
266       break;
267   }
268   if (Size == 0)
269     return true;
270
271   return false;
272 }
273
274 static SectionKind computeSectionKind(unsigned Flags) {
275   if (Flags & ELF::SHF_EXECINSTR)
276     return SectionKind::getText();
277   if (Flags & ELF::SHF_TLS)
278     return SectionKind::getThreadData();
279   return SectionKind::getDataRel();
280 }
281
282 static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
283   unsigned flags = 0;
284
285   for (unsigned i = 0; i < flagsStr.size(); i++) {
286     switch (flagsStr[i]) {
287     case 'a':
288       flags |= ELF::SHF_ALLOC;
289       break;
290     case 'e':
291       flags |= ELF::SHF_EXCLUDE;
292       break;
293     case 'x':
294       flags |= ELF::SHF_EXECINSTR;
295       break;
296     case 'w':
297       flags |= ELF::SHF_WRITE;
298       break;
299     case 'M':
300       flags |= ELF::SHF_MERGE;
301       break;
302     case 'S':
303       flags |= ELF::SHF_STRINGS;
304       break;
305     case 'T':
306       flags |= ELF::SHF_TLS;
307       break;
308     case 'c':
309       flags |= ELF::XCORE_SHF_CP_SECTION;
310       break;
311     case 'd':
312       flags |= ELF::XCORE_SHF_DP_SECTION;
313       break;
314     case 'G':
315       flags |= ELF::SHF_GROUP;
316       break;
317     case '?':
318       *UseLastGroup = true;
319       break;
320     default:
321       return -1U;
322     }
323   }
324
325   return flags;
326 }
327
328 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
329   getStreamer().PushSection();
330
331   if (ParseSectionArguments(/*IsPush=*/true)) {
332     getStreamer().PopSection();
333     return true;
334   }
335
336   return false;
337 }
338
339 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
340   if (!getStreamer().PopSection())
341     return TokError(".popsection without corresponding .pushsection");
342   return false;
343 }
344
345 // FIXME: This is a work in progress.
346 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
347   return ParseSectionArguments(/*IsPush=*/false);
348 }
349
350 bool ELFAsmParser::ParseSectionArguments(bool IsPush) {
351   StringRef SectionName;
352
353   if (ParseSectionName(SectionName))
354     return TokError("expected identifier in directive");
355
356   StringRef TypeName;
357   int64_t Size = 0;
358   StringRef GroupName;
359   unsigned Flags = 0;
360   const MCExpr *Subsection = 0;
361   bool UseLastGroup = false;
362
363   // Set the defaults first.
364   if (SectionName == ".fini" || SectionName == ".init" ||
365       SectionName == ".rodata")
366     Flags |= ELF::SHF_ALLOC;
367   if (SectionName == ".fini" || SectionName == ".init")
368     Flags |= ELF::SHF_EXECINSTR;
369
370   if (getLexer().is(AsmToken::Comma)) {
371     Lex();
372
373     if (IsPush && getLexer().isNot(AsmToken::String)) {
374       if (getParser().parseExpression(Subsection))
375         return true;
376       if (getLexer().isNot(AsmToken::Comma))
377         goto EndStmt;
378       Lex();
379     }
380    
381     if (getLexer().isNot(AsmToken::String))
382       return TokError("expected string in directive");
383
384     StringRef FlagsStr = getTok().getStringContents();
385     Lex();
386
387     unsigned extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
388     if (extraFlags == -1U)
389       return TokError("unknown flag");
390     Flags |= extraFlags;
391
392     bool Mergeable = Flags & ELF::SHF_MERGE;
393     bool Group = Flags & ELF::SHF_GROUP;
394     if (Group && UseLastGroup)
395       return TokError("Section cannot specifiy a group name while also acting "
396                       "as a member of the last group");
397
398     if (getLexer().isNot(AsmToken::Comma)) {
399       if (Mergeable)
400         return TokError("Mergeable section must specify the type");
401       if (Group)
402         return TokError("Group section must specify the type");
403     } else {
404       Lex();
405       if (getLexer().is(AsmToken::At) || getLexer().is(AsmToken::Percent) ||
406           getLexer().is(AsmToken::String)) {
407         if (!getLexer().is(AsmToken::String))
408           Lex();
409       } else
410         return TokError("expected '@<type>', '%<type>' or \"<type>\"");
411
412       if (getParser().parseIdentifier(TypeName))
413         return TokError("expected identifier in directive");
414
415       if (Mergeable) {
416         if (getLexer().isNot(AsmToken::Comma))
417           return TokError("expected the entry size");
418         Lex();
419         if (getParser().parseAbsoluteExpression(Size))
420           return true;
421         if (Size <= 0)
422           return TokError("entry size must be positive");
423       }
424
425       if (Group) {
426         if (getLexer().isNot(AsmToken::Comma))
427           return TokError("expected group name");
428         Lex();
429         if (getParser().parseIdentifier(GroupName))
430           return true;
431         if (getLexer().is(AsmToken::Comma)) {
432           Lex();
433           StringRef Linkage;
434           if (getParser().parseIdentifier(Linkage))
435             return true;
436           if (Linkage != "comdat")
437             return TokError("Linkage must be 'comdat'");
438         }
439       }
440     }
441   }
442
443 EndStmt:
444   if (getLexer().isNot(AsmToken::EndOfStatement))
445     return TokError("unexpected token in directive");
446
447   unsigned Type = ELF::SHT_PROGBITS;
448
449   if (TypeName.empty()) {
450     if (SectionName.startswith(".note"))
451       Type = ELF::SHT_NOTE;
452     else if (SectionName == ".init_array")
453       Type = ELF::SHT_INIT_ARRAY;
454     else if (SectionName == ".fini_array")
455       Type = ELF::SHT_FINI_ARRAY;
456     else if (SectionName == ".preinit_array")
457       Type = ELF::SHT_PREINIT_ARRAY;
458   } else {
459     if (TypeName == "init_array")
460       Type = ELF::SHT_INIT_ARRAY;
461     else if (TypeName == "fini_array")
462       Type = ELF::SHT_FINI_ARRAY;
463     else if (TypeName == "preinit_array")
464       Type = ELF::SHT_PREINIT_ARRAY;
465     else if (TypeName == "nobits")
466       Type = ELF::SHT_NOBITS;
467     else if (TypeName == "progbits")
468       Type = ELF::SHT_PROGBITS;
469     else if (TypeName == "note")
470       Type = ELF::SHT_NOTE;
471     else if (TypeName == "unwind")
472       Type = ELF::SHT_X86_64_UNWIND;
473     else
474       return TokError("unknown section type");
475   }
476
477   if (UseLastGroup) {
478     MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
479     if (const MCSectionELF *Section =
480             cast_or_null<MCSectionELF>(CurrentSection.first))
481       if (const MCSymbol *Group = Section->getGroup()) {
482         GroupName = Group->getName();
483         Flags |= ELF::SHF_GROUP;
484       }
485   }
486
487   SectionKind Kind = computeSectionKind(Flags);
488   getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
489                                                          Flags, Kind, Size,
490                                                          GroupName),
491                               Subsection);
492   return false;
493 }
494
495 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
496   MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
497   if (PreviousSection.first == NULL)
498       return TokError(".previous without corresponding .section");
499   getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
500
501   return false;
502 }
503
504 /// ParseDirectiveELFType
505 ///  ::= .type identifier , STT_<TYPE_IN_UPPER_CASE>
506 ///  ::= .type identifier , #attribute
507 ///  ::= .type identifier , @attribute
508 ///  ::= .type identifier , %attribute
509 ///  ::= .type identifier , "attribute"
510 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
511   StringRef Name;
512   if (getParser().parseIdentifier(Name))
513     return TokError("expected identifier in directive");
514
515   // Handle the identifier as the key symbol.
516   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
517
518   if (getLexer().isNot(AsmToken::Comma))
519     return TokError("unexpected token in '.type' directive");
520   Lex();
521
522   StringRef Type;
523   SMLoc TypeLoc;
524   MCSymbolAttr Attr;
525   if (getLexer().is(AsmToken::Identifier)) {
526     TypeLoc = getLexer().getLoc();
527     if (getParser().parseIdentifier(Type))
528       return TokError("expected symbol type in directive");
529     Attr = StringSwitch<MCSymbolAttr>(Type)
530                .Case("STT_FUNC", MCSA_ELF_TypeFunction)
531                .Case("STT_OBJECT", MCSA_ELF_TypeObject)
532                .Case("STT_TLS", MCSA_ELF_TypeTLS)
533                .Case("STT_COMMON", MCSA_ELF_TypeCommon)
534                .Case("STT_NOTYPE", MCSA_ELF_TypeNoType)
535                .Case("STT_GNU_IFUNC", MCSA_ELF_TypeIndFunction)
536                .Default(MCSA_Invalid);
537   } else if (getLexer().is(AsmToken::Hash) || getLexer().is(AsmToken::At) ||
538              getLexer().is(AsmToken::Percent) ||
539              getLexer().is(AsmToken::String)) {
540     if (!getLexer().is(AsmToken::String))
541       Lex();
542
543     TypeLoc = getLexer().getLoc();
544     if (getParser().parseIdentifier(Type))
545       return TokError("expected symbol type in directive");
546     Attr = StringSwitch<MCSymbolAttr>(Type)
547                .Case("function", MCSA_ELF_TypeFunction)
548                .Case("object", MCSA_ELF_TypeObject)
549                .Case("tls_object", MCSA_ELF_TypeTLS)
550                .Case("common", MCSA_ELF_TypeCommon)
551                .Case("notype", MCSA_ELF_TypeNoType)
552                .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
553                .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
554                .Default(MCSA_Invalid);
555   } else
556     return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', "
557                     "'%<type>' or \"<type>\"");
558
559   if (Attr == MCSA_Invalid)
560     return Error(TypeLoc, "unsupported attribute in '.type' directive");
561
562   if (getLexer().isNot(AsmToken::EndOfStatement))
563     return TokError("unexpected token in '.type' directive");
564
565   Lex();
566
567   getStreamer().EmitSymbolAttribute(Sym, Attr);
568
569   return false;
570 }
571
572 /// ParseDirectiveIdent
573 ///  ::= .ident string
574 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
575   if (getLexer().isNot(AsmToken::String))
576     return TokError("unexpected token in '.ident' directive");
577
578   StringRef Data = getTok().getIdentifier();
579
580   Lex();
581
582   const MCSection *Comment =
583     getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
584                                ELF::SHF_MERGE |
585                                ELF::SHF_STRINGS,
586                                SectionKind::getReadOnly(),
587                                1, "");
588
589   getStreamer().PushSection();
590   getStreamer().SwitchSection(Comment);
591   if (!SeenIdent) {
592     getStreamer().EmitIntValue(0, 1);
593     SeenIdent = true;
594   }
595   getStreamer().EmitBytes(Data);
596   getStreamer().EmitIntValue(0, 1);
597   getStreamer().PopSection();
598   return false;
599 }
600
601 /// ParseDirectiveSymver
602 ///  ::= .symver foo, bar2@zed
603 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
604   StringRef Name;
605   if (getParser().parseIdentifier(Name))
606     return TokError("expected identifier in directive");
607
608   if (getLexer().isNot(AsmToken::Comma))
609     return TokError("expected a comma");
610
611   Lex();
612
613   StringRef AliasName;
614   if (getParser().parseIdentifier(AliasName))
615     return TokError("expected identifier in directive");
616
617   if (AliasName.find('@') == StringRef::npos)
618     return TokError("expected a '@' in the name");
619
620   MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
621   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
622   const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
623
624   getStreamer().EmitAssignment(Alias, Value);
625   return false;
626 }
627
628 /// ParseDirectiveVersion
629 ///  ::= .version string
630 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
631   if (getLexer().isNot(AsmToken::String))
632     return TokError("unexpected token in '.version' directive");
633
634   StringRef Data = getTok().getIdentifier();
635
636   Lex();
637
638   const MCSection *Note =
639     getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
640                                SectionKind::getReadOnly());
641
642   getStreamer().PushSection();
643   getStreamer().SwitchSection(Note);
644   getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
645   getStreamer().EmitIntValue(0, 4);             // descsz = 0 (no description).
646   getStreamer().EmitIntValue(1, 4);             // type = NT_VERSION.
647   getStreamer().EmitBytes(Data);                // name.
648   getStreamer().EmitIntValue(0, 1);             // terminate the string.
649   getStreamer().EmitValueToAlignment(4);        // ensure 4 byte alignment.
650   getStreamer().PopSection();
651   return false;
652 }
653
654 /// ParseDirectiveWeakref
655 ///  ::= .weakref foo, bar
656 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
657   // FIXME: Share code with the other alias building directives.
658
659   StringRef AliasName;
660   if (getParser().parseIdentifier(AliasName))
661     return TokError("expected identifier in directive");
662
663   if (getLexer().isNot(AsmToken::Comma))
664     return TokError("expected a comma");
665
666   Lex();
667
668   StringRef Name;
669   if (getParser().parseIdentifier(Name))
670     return TokError("expected identifier in directive");
671
672   MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
673
674   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
675
676   getStreamer().EmitWeakReference(Alias, Sym);
677   return false;
678 }
679
680 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
681   const MCExpr *Subsection = 0;
682   if (getLexer().isNot(AsmToken::EndOfStatement)) {
683     if (getParser().parseExpression(Subsection))
684      return true;
685   }
686
687   if (getLexer().isNot(AsmToken::EndOfStatement))
688     return TokError("unexpected token in directive");
689
690   getStreamer().SubSection(Subsection);
691   return false;
692 }
693
694 namespace llvm {
695
696 MCAsmParserExtension *createELFAsmParser() {
697   return new ELFAsmParser;
698 }
699
700 }