MC: Add support for '?' flags in .section directives
[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 int parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) {
283   int 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 'x':
291       flags |= ELF::SHF_EXECINSTR;
292       break;
293     case 'w':
294       flags |= ELF::SHF_WRITE;
295       break;
296     case 'M':
297       flags |= ELF::SHF_MERGE;
298       break;
299     case 'S':
300       flags |= ELF::SHF_STRINGS;
301       break;
302     case 'T':
303       flags |= ELF::SHF_TLS;
304       break;
305     case 'c':
306       flags |= ELF::XCORE_SHF_CP_SECTION;
307       break;
308     case 'd':
309       flags |= ELF::XCORE_SHF_DP_SECTION;
310       break;
311     case 'G':
312       flags |= ELF::SHF_GROUP;
313       break;
314     case '?':
315       *UseLastGroup = true;
316       break;
317     default:
318       return -1;
319     }
320   }
321
322   return flags;
323 }
324
325 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
326   getStreamer().PushSection();
327
328   if (ParseSectionArguments(/*IsPush=*/true)) {
329     getStreamer().PopSection();
330     return true;
331   }
332
333   return false;
334 }
335
336 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
337   if (!getStreamer().PopSection())
338     return TokError(".popsection without corresponding .pushsection");
339   return false;
340 }
341
342 // FIXME: This is a work in progress.
343 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
344   return ParseSectionArguments(/*IsPush=*/false);
345 }
346
347 bool ELFAsmParser::ParseSectionArguments(bool IsPush) {
348   StringRef SectionName;
349
350   if (ParseSectionName(SectionName))
351     return TokError("expected identifier in directive");
352
353   StringRef TypeName;
354   int64_t Size = 0;
355   StringRef GroupName;
356   unsigned Flags = 0;
357   const MCExpr *Subsection = 0;
358   bool UseLastGroup = false;
359
360   // Set the defaults first.
361   if (SectionName == ".fini" || SectionName == ".init" ||
362       SectionName == ".rodata")
363     Flags |= ELF::SHF_ALLOC;
364   if (SectionName == ".fini" || SectionName == ".init")
365     Flags |= ELF::SHF_EXECINSTR;
366
367   if (getLexer().is(AsmToken::Comma)) {
368     Lex();
369
370     if (IsPush && getLexer().isNot(AsmToken::String)) {
371       if (getParser().parseExpression(Subsection))
372         return true;
373       if (getLexer().isNot(AsmToken::Comma))
374         goto EndStmt;
375       Lex();
376     }
377    
378     if (getLexer().isNot(AsmToken::String))
379       return TokError("expected string in directive");
380
381     StringRef FlagsStr = getTok().getStringContents();
382     Lex();
383
384     int extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup);
385     if (extraFlags < 0)
386       return TokError("unknown flag");
387     Flags |= extraFlags;
388
389     bool Mergeable = Flags & ELF::SHF_MERGE;
390     bool Group = Flags & ELF::SHF_GROUP;
391     if (Group && UseLastGroup)
392       return TokError("Section cannot specifiy a group name while also acting "
393                       "as a member of the last group");
394
395     if (getLexer().isNot(AsmToken::Comma)) {
396       if (Mergeable)
397         return TokError("Mergeable section must specify the type");
398       if (Group)
399         return TokError("Group section must specify the type");
400     } else {
401       Lex();
402       if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
403         return TokError("expected '@' or '%' before type");
404
405       Lex();
406       if (getParser().parseIdentifier(TypeName))
407         return TokError("expected identifier in directive");
408
409       if (Mergeable) {
410         if (getLexer().isNot(AsmToken::Comma))
411           return TokError("expected the entry size");
412         Lex();
413         if (getParser().parseAbsoluteExpression(Size))
414           return true;
415         if (Size <= 0)
416           return TokError("entry size must be positive");
417       }
418
419       if (Group) {
420         if (getLexer().isNot(AsmToken::Comma))
421           return TokError("expected group name");
422         Lex();
423         if (getParser().parseIdentifier(GroupName))
424           return true;
425         if (getLexer().is(AsmToken::Comma)) {
426           Lex();
427           StringRef Linkage;
428           if (getParser().parseIdentifier(Linkage))
429             return true;
430           if (Linkage != "comdat")
431             return TokError("Linkage must be 'comdat'");
432         }
433       }
434     }
435   }
436
437 EndStmt:
438   if (getLexer().isNot(AsmToken::EndOfStatement))
439     return TokError("unexpected token in directive");
440
441   unsigned Type = ELF::SHT_PROGBITS;
442
443   if (TypeName.empty()) {
444     if (SectionName.startswith(".note"))
445       Type = ELF::SHT_NOTE;
446     else if (SectionName == ".init_array")
447       Type = ELF::SHT_INIT_ARRAY;
448     else if (SectionName == ".fini_array")
449       Type = ELF::SHT_FINI_ARRAY;
450     else if (SectionName == ".preinit_array")
451       Type = ELF::SHT_PREINIT_ARRAY;
452   } else {
453     if (TypeName == "init_array")
454       Type = ELF::SHT_INIT_ARRAY;
455     else if (TypeName == "fini_array")
456       Type = ELF::SHT_FINI_ARRAY;
457     else if (TypeName == "preinit_array")
458       Type = ELF::SHT_PREINIT_ARRAY;
459     else if (TypeName == "nobits")
460       Type = ELF::SHT_NOBITS;
461     else if (TypeName == "progbits")
462       Type = ELF::SHT_PROGBITS;
463     else if (TypeName == "note")
464       Type = ELF::SHT_NOTE;
465     else if (TypeName == "unwind")
466       Type = ELF::SHT_X86_64_UNWIND;
467     else
468       return TokError("unknown section type");
469   }
470
471   if (UseLastGroup) {
472     MCSectionSubPair CurrentSection = getStreamer().getCurrentSection();
473     if (const MCSectionELF *Section =
474             cast_or_null<MCSectionELF>(CurrentSection.first))
475       if (const MCSymbol *Group = Section->getGroup()) {
476         GroupName = Group->getName();
477         Flags |= ELF::SHF_GROUP;
478       }
479   }
480
481   SectionKind Kind = computeSectionKind(Flags);
482   getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
483                                                          Flags, Kind, Size,
484                                                          GroupName),
485                               Subsection);
486   return false;
487 }
488
489 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
490   MCSectionSubPair PreviousSection = getStreamer().getPreviousSection();
491   if (PreviousSection.first == NULL)
492       return TokError(".previous without corresponding .section");
493   getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second);
494
495   return false;
496 }
497
498 /// ParseDirectiveELFType
499 ///  ::= .type identifier , @attribute
500 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
501   StringRef Name;
502   if (getParser().parseIdentifier(Name))
503     return TokError("expected identifier in directive");
504
505   // Handle the identifier as the key symbol.
506   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
507
508   if (getLexer().isNot(AsmToken::Comma))
509     return TokError("unexpected token in '.type' directive");
510   Lex();
511
512   if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
513     return TokError("expected '@' or '%' before type");
514   Lex();
515
516   StringRef Type;
517   SMLoc TypeLoc;
518
519   TypeLoc = getLexer().getLoc();
520   if (getParser().parseIdentifier(Type))
521     return TokError("expected symbol type in directive");
522
523   MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
524     .Case("function", MCSA_ELF_TypeFunction)
525     .Case("object", MCSA_ELF_TypeObject)
526     .Case("tls_object", MCSA_ELF_TypeTLS)
527     .Case("common", MCSA_ELF_TypeCommon)
528     .Case("notype", MCSA_ELF_TypeNoType)
529     .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject)
530     .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction)
531     .Default(MCSA_Invalid);
532
533   if (Attr == MCSA_Invalid)
534     return Error(TypeLoc, "unsupported attribute in '.type' directive");
535
536   if (getLexer().isNot(AsmToken::EndOfStatement))
537     return TokError("unexpected token in '.type' directive");
538
539   Lex();
540
541   getStreamer().EmitSymbolAttribute(Sym, Attr);
542
543   return false;
544 }
545
546 /// ParseDirectiveIdent
547 ///  ::= .ident string
548 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
549   if (getLexer().isNot(AsmToken::String))
550     return TokError("unexpected token in '.ident' directive");
551
552   StringRef Data = getTok().getIdentifier();
553
554   Lex();
555
556   const MCSection *Comment =
557     getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
558                                ELF::SHF_MERGE |
559                                ELF::SHF_STRINGS,
560                                SectionKind::getReadOnly(),
561                                1, "");
562
563   getStreamer().PushSection();
564   getStreamer().SwitchSection(Comment);
565   if (!SeenIdent) {
566     getStreamer().EmitIntValue(0, 1);
567     SeenIdent = true;
568   }
569   getStreamer().EmitBytes(Data);
570   getStreamer().EmitIntValue(0, 1);
571   getStreamer().PopSection();
572   return false;
573 }
574
575 /// ParseDirectiveSymver
576 ///  ::= .symver foo, bar2@zed
577 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
578   StringRef Name;
579   if (getParser().parseIdentifier(Name))
580     return TokError("expected identifier in directive");
581
582   if (getLexer().isNot(AsmToken::Comma))
583     return TokError("expected a comma");
584
585   Lex();
586
587   StringRef AliasName;
588   if (getParser().parseIdentifier(AliasName))
589     return TokError("expected identifier in directive");
590
591   if (AliasName.find('@') == StringRef::npos)
592     return TokError("expected a '@' in the name");
593
594   MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
595   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
596   const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
597
598   getStreamer().EmitAssignment(Alias, Value);
599   return false;
600 }
601
602 /// ParseDirectiveVersion
603 ///  ::= .version string
604 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
605   if (getLexer().isNot(AsmToken::String))
606     return TokError("unexpected token in '.version' directive");
607
608   StringRef Data = getTok().getIdentifier();
609
610   Lex();
611
612   const MCSection *Note =
613     getContext().getELFSection(".note", ELF::SHT_NOTE, 0,
614                                SectionKind::getReadOnly());
615
616   getStreamer().PushSection();
617   getStreamer().SwitchSection(Note);
618   getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
619   getStreamer().EmitIntValue(0, 4);             // descsz = 0 (no description).
620   getStreamer().EmitIntValue(1, 4);             // type = NT_VERSION.
621   getStreamer().EmitBytes(Data);                // name.
622   getStreamer().EmitIntValue(0, 1);             // terminate the string.
623   getStreamer().EmitValueToAlignment(4);        // ensure 4 byte alignment.
624   getStreamer().PopSection();
625   return false;
626 }
627
628 /// ParseDirectiveWeakref
629 ///  ::= .weakref foo, bar
630 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
631   // FIXME: Share code with the other alias building directives.
632
633   StringRef AliasName;
634   if (getParser().parseIdentifier(AliasName))
635     return TokError("expected identifier in directive");
636
637   if (getLexer().isNot(AsmToken::Comma))
638     return TokError("expected a comma");
639
640   Lex();
641
642   StringRef Name;
643   if (getParser().parseIdentifier(Name))
644     return TokError("expected identifier in directive");
645
646   MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
647
648   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
649
650   getStreamer().EmitWeakReference(Alias, Sym);
651   return false;
652 }
653
654 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) {
655   const MCExpr *Subsection = 0;
656   if (getLexer().isNot(AsmToken::EndOfStatement)) {
657     if (getParser().parseExpression(Subsection))
658      return true;
659   }
660
661   if (getLexer().isNot(AsmToken::EndOfStatement))
662     return TokError("unexpected token in directive");
663
664   getStreamer().SubSection(Subsection);
665   return false;
666 }
667
668 namespace llvm {
669
670 MCAsmParserExtension *createELFAsmParser() {
671   return new ELFAsmParser;
672 }
673
674 }