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