Use %ull here.
[llvm/stm8.git] / lib / MC / MCParser / ELFAsmParser.cpp
blobdcf689a6f0e7fa0a443e1f4f1085f4529476a101
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 //===----------------------------------------------------------------------===//
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;
22 namespace {
24 class ELFAsmParser : public MCAsmParserExtension {
25 template<bool (ELFAsmParser::*Handler)(StringRef, SMLoc)>
26 void AddDirectiveHandler(StringRef Directive) {
27 getParser().AddDirectiveHandler(this, Directive,
28 HandleDirective<ELFAsmParser, Handler>);
31 bool ParseSectionSwitch(StringRef Section, unsigned Type,
32 unsigned Flags, SectionKind Kind);
33 bool SeenIdent;
35 public:
36 ELFAsmParser() : SeenIdent(false) {
37 BracketExpressionsSupported = true;
40 virtual void Initialize(MCAsmParser &Parser) {
41 // Call the base implementation.
42 this->MCAsmParserExtension::Initialize(Parser);
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");
65 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is
66 // the best way for us to get access to it?
67 bool ParseSectionDirectiveData(StringRef, SMLoc) {
68 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS,
69 ELF::SHF_WRITE |ELF::SHF_ALLOC,
70 SectionKind::getDataRel());
72 bool ParseSectionDirectiveText(StringRef, SMLoc) {
73 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS,
74 ELF::SHF_EXECINSTR |
75 ELF::SHF_ALLOC, SectionKind::getText());
77 bool ParseSectionDirectiveBSS(StringRef, SMLoc) {
78 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS,
79 ELF::SHF_WRITE |
80 ELF::SHF_ALLOC, SectionKind::getBSS());
82 bool ParseSectionDirectiveRoData(StringRef, SMLoc) {
83 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS,
84 ELF::SHF_ALLOC,
85 SectionKind::getReadOnly());
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());
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());
99 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) {
100 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS,
101 ELF::SHF_ALLOC |
102 ELF::SHF_WRITE,
103 SectionKind::getDataRel());
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());
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());
117 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) {
118 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS,
119 ELF::SHF_ALLOC |
120 ELF::SHF_WRITE,
121 SectionKind::getDataRel());
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);
133 private:
134 bool ParseSectionName(StringRef &SectionName);
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();
145 getStreamer().SwitchSection(getContext().getELFSection(
146 Section, Type, Flags, Kind));
148 return false;
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);;
157 if (getLexer().isNot(AsmToken::Comma))
158 return TokError("unexpected token in directive");
159 Lex();
161 const MCExpr *Expr;
162 if (getParser().ParseExpression(Expr))
163 return true;
165 if (getLexer().isNot(AsmToken::EndOfStatement))
166 return TokError("unexpected token in directive");
168 getStreamer().EmitELFSize(Sym, Expr);
169 return false;
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;
178 if (getLexer().is(AsmToken::String)) {
179 SectionName = getTok().getIdentifier();
180 Lex();
181 return false;
184 for (;;) {
185 StringRef Tmp;
186 unsigned CurSize;
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;
202 Size += CurSize;
203 SectionName = StringRef(FirstLoc.getPointer(), Size);
205 // Make sure the following token is adjacent.
206 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer())
207 break;
209 if (Size == 0)
210 return true;
212 return false;
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();
223 static int parseSectionFlags(StringRef flagsStr) {
224 int flags = 0;
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;
260 return flags;
263 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) {
264 getStreamer().PushSection();
266 if (ParseDirectiveSection(s, loc)) {
267 getStreamer().PopSection();
268 return true;
271 return false;
274 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) {
275 if (!getStreamer().PopSection())
276 return TokError(".popsection without corresponding .pushsection");
277 return false;
280 // FIXME: This is a work in progress.
281 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) {
282 StringRef SectionName;
284 if (ParseSectionName(SectionName))
285 return TokError("expected identifier in directive");
287 StringRef TypeName;
288 int64_t Size = 0;
289 StringRef GroupName;
290 unsigned Flags = 0;
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;
299 if (getLexer().is(AsmToken::Comma)) {
300 Lex();
302 if (getLexer().isNot(AsmToken::String))
303 return TokError("expected string in directive");
305 StringRef FlagsStr = getTok().getStringContents();
306 Lex();
308 int extraFlags = parseSectionFlags(FlagsStr);
309 if (extraFlags < 0)
310 return TokError("unknown flag");
311 Flags |= extraFlags;
313 bool Mergeable = Flags & ELF::SHF_MERGE;
314 bool Group = Flags & ELF::SHF_GROUP;
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");
326 Lex();
327 if (getParser().ParseIdentifier(TypeName))
328 return TokError("expected identifier in directive");
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");
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'");
358 if (getLexer().isNot(AsmToken::EndOfStatement))
359 return TokError("unexpected token in directive");
361 unsigned Type = ELF::SHT_PROGBITS;
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");
382 SectionKind Kind = computeSectionKind(Flags);
383 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type,
384 Flags, Kind, Size,
385 GroupName));
386 return false;
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);
395 return false;
398 /// ParseDirectiveELFType
399 /// ::= .type identifier , @attribute
400 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) {
401 StringRef Name;
402 if (getParser().ParseIdentifier(Name))
403 return TokError("expected identifier in directive");
405 // Handle the identifier as the key symbol.
406 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
408 if (getLexer().isNot(AsmToken::Comma))
409 return TokError("unexpected token in '.type' directive");
410 Lex();
412 if (getLexer().isNot(AsmToken::Percent) && getLexer().isNot(AsmToken::At))
413 return TokError("expected '@' or '%' before type");
414 Lex();
416 StringRef Type;
417 SMLoc TypeLoc;
419 TypeLoc = getLexer().getLoc();
420 if (getParser().ParseIdentifier(Type))
421 return TokError("expected symbol type in directive");
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);
432 if (Attr == MCSA_Invalid)
433 return Error(TypeLoc, "unsupported attribute in '.type' directive");
435 if (getLexer().isNot(AsmToken::EndOfStatement))
436 return TokError("unexpected token in '.type' directive");
438 Lex();
440 getStreamer().EmitSymbolAttribute(Sym, Attr);
442 return false;
445 /// ParseDirectiveIdent
446 /// ::= .ident string
447 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) {
448 if (getLexer().isNot(AsmToken::String))
449 return TokError("unexpected token in '.ident' directive");
451 StringRef Data = getTok().getIdentifier();
453 Lex();
455 const MCSection *Comment =
456 getContext().getELFSection(".comment", ELF::SHT_PROGBITS,
457 ELF::SHF_MERGE |
458 ELF::SHF_STRINGS,
459 SectionKind::getReadOnly(),
460 1, "");
462 getStreamer().PushSection();
463 getStreamer().SwitchSection(Comment);
464 if (!SeenIdent) {
465 getStreamer().EmitIntValue(0, 1);
466 SeenIdent = true;
468 getStreamer().EmitBytes(Data, 0);
469 getStreamer().EmitIntValue(0, 1);
470 getStreamer().PopSection();
471 return false;
474 /// ParseDirectiveSymver
475 /// ::= .symver foo, bar2@zed
476 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
477 StringRef Name;
478 if (getParser().ParseIdentifier(Name))
479 return TokError("expected identifier in directive");
481 if (getLexer().isNot(AsmToken::Comma))
482 return TokError("expected a comma");
484 Lex();
486 StringRef AliasName;
487 if (getParser().ParseIdentifier(AliasName))
488 return TokError("expected identifier in directive");
490 if (AliasName.find('@') == StringRef::npos)
491 return TokError("expected a '@' in the name");
493 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
494 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
495 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext());
497 getStreamer().EmitAssignment(Alias, Value);
498 return false;
501 /// ParseDirectiveWeakref
502 /// ::= .weakref foo, bar
503 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) {
504 // FIXME: Share code with the other alias building directives.
506 StringRef AliasName;
507 if (getParser().ParseIdentifier(AliasName))
508 return TokError("expected identifier in directive");
510 if (getLexer().isNot(AsmToken::Comma))
511 return TokError("expected a comma");
513 Lex();
515 StringRef Name;
516 if (getParser().ParseIdentifier(Name))
517 return TokError("expected identifier in directive");
519 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName);
521 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
523 getStreamer().EmitWeakReference(Alias, Sym);
524 return false;
527 namespace llvm {
529 MCAsmParserExtension *createELFAsmParser() {
530 return new ELFAsmParser;