ELFAsmParser.cpp revision 263508
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 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< 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}; 155 156} 157 158/// ParseDirectiveSymbolAttribute 159/// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ] 160bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) { 161 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive) 162 .Case(".weak", MCSA_Weak) 163 .Case(".local", MCSA_Local) 164 .Case(".hidden", MCSA_Hidden) 165 .Case(".internal", MCSA_Internal) 166 .Case(".protected", MCSA_Protected) 167 .Default(MCSA_Invalid); 168 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!"); 169 if (getLexer().isNot(AsmToken::EndOfStatement)) { 170 for (;;) { 171 StringRef Name; 172 173 if (getParser().parseIdentifier(Name)) 174 return TokError("expected identifier in directive"); 175 176 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); 177 178 getStreamer().EmitSymbolAttribute(Sym, Attr); 179 180 if (getLexer().is(AsmToken::EndOfStatement)) 181 break; 182 183 if (getLexer().isNot(AsmToken::Comma)) 184 return TokError("unexpected token in directive"); 185 Lex(); 186 } 187 } 188 189 Lex(); 190 return false; 191} 192 193bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type, 194 unsigned Flags, SectionKind Kind) { 195 const MCExpr *Subsection = 0; 196 if (getLexer().isNot(AsmToken::EndOfStatement)) { 197 if (getParser().parseExpression(Subsection)) 198 return true; 199 } 200 201 getStreamer().SwitchSection(getContext().getELFSection( 202 Section, Type, Flags, Kind), 203 Subsection); 204 205 return false; 206} 207 208bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) { 209 StringRef Name; 210 if (getParser().parseIdentifier(Name)) 211 return TokError("expected identifier in directive"); 212 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); 213 214 if (getLexer().isNot(AsmToken::Comma)) 215 return TokError("unexpected token in directive"); 216 Lex(); 217 218 const MCExpr *Expr; 219 if (getParser().parseExpression(Expr)) 220 return true; 221 222 if (getLexer().isNot(AsmToken::EndOfStatement)) 223 return TokError("unexpected token in directive"); 224 225 getStreamer().EmitELFSize(Sym, Expr); 226 return false; 227} 228 229bool ELFAsmParser::ParseSectionName(StringRef &SectionName) { 230 // A section name can contain -, so we cannot just use 231 // parseIdentifier. 232 SMLoc FirstLoc = getLexer().getLoc(); 233 unsigned Size = 0; 234 235 if (getLexer().is(AsmToken::String)) { 236 SectionName = getTok().getIdentifier(); 237 Lex(); 238 return false; 239 } 240 241 for (;;) { 242 unsigned CurSize; 243 244 SMLoc PrevLoc = getLexer().getLoc(); 245 if (getLexer().is(AsmToken::Minus)) { 246 CurSize = 1; 247 Lex(); // Consume the "-". 248 } else if (getLexer().is(AsmToken::String)) { 249 CurSize = getTok().getIdentifier().size() + 2; 250 Lex(); 251 } else if (getLexer().is(AsmToken::Identifier)) { 252 CurSize = getTok().getIdentifier().size(); 253 Lex(); 254 } else { 255 break; 256 } 257 258 Size += CurSize; 259 SectionName = StringRef(FirstLoc.getPointer(), Size); 260 261 // Make sure the following token is adjacent. 262 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer()) 263 break; 264 } 265 if (Size == 0) 266 return true; 267 268 return false; 269} 270 271static SectionKind computeSectionKind(unsigned Flags) { 272 if (Flags & ELF::SHF_EXECINSTR) 273 return SectionKind::getText(); 274 if (Flags & ELF::SHF_TLS) 275 return SectionKind::getThreadData(); 276 return SectionKind::getDataRel(); 277} 278 279static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) { 280 unsigned flags = 0; 281 282 for (unsigned i = 0; i < flagsStr.size(); i++) { 283 switch (flagsStr[i]) { 284 case 'a': 285 flags |= ELF::SHF_ALLOC; 286 break; 287 case 'e': 288 flags |= ELF::SHF_EXCLUDE; 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 -1U; 319 } 320 } 321 322 return flags; 323} 324 325bool 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 336bool 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. 343bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc) { 344 return ParseSectionArguments(/*IsPush=*/false); 345} 346 347bool 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 unsigned extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup); 385 if (extraFlags == -1U) 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().is(AsmToken::At) || getLexer().is(AsmToken::Percent) || 403 getLexer().is(AsmToken::String)) { 404 if (!getLexer().is(AsmToken::String)) 405 Lex(); 406 } else 407 return TokError("expected '@<type>', '%<type>' or \"<type>\""); 408 409 if (getParser().parseIdentifier(TypeName)) 410 return TokError("expected identifier in directive"); 411 412 if (Mergeable) { 413 if (getLexer().isNot(AsmToken::Comma)) 414 return TokError("expected the entry size"); 415 Lex(); 416 if (getParser().parseAbsoluteExpression(Size)) 417 return true; 418 if (Size <= 0) 419 return TokError("entry size must be positive"); 420 } 421 422 if (Group) { 423 if (getLexer().isNot(AsmToken::Comma)) 424 return TokError("expected group name"); 425 Lex(); 426 if (getParser().parseIdentifier(GroupName)) 427 return true; 428 if (getLexer().is(AsmToken::Comma)) { 429 Lex(); 430 StringRef Linkage; 431 if (getParser().parseIdentifier(Linkage)) 432 return true; 433 if (Linkage != "comdat") 434 return TokError("Linkage must be 'comdat'"); 435 } 436 } 437 } 438 } 439 440EndStmt: 441 if (getLexer().isNot(AsmToken::EndOfStatement)) 442 return TokError("unexpected token in directive"); 443 444 unsigned Type = ELF::SHT_PROGBITS; 445 446 if (TypeName.empty()) { 447 if (SectionName.startswith(".note")) 448 Type = ELF::SHT_NOTE; 449 else if (SectionName == ".init_array") 450 Type = ELF::SHT_INIT_ARRAY; 451 else if (SectionName == ".fini_array") 452 Type = ELF::SHT_FINI_ARRAY; 453 else if (SectionName == ".preinit_array") 454 Type = ELF::SHT_PREINIT_ARRAY; 455 } else { 456 if (TypeName == "init_array") 457 Type = ELF::SHT_INIT_ARRAY; 458 else if (TypeName == "fini_array") 459 Type = ELF::SHT_FINI_ARRAY; 460 else if (TypeName == "preinit_array") 461 Type = ELF::SHT_PREINIT_ARRAY; 462 else if (TypeName == "nobits") 463 Type = ELF::SHT_NOBITS; 464 else if (TypeName == "progbits") 465 Type = ELF::SHT_PROGBITS; 466 else if (TypeName == "note") 467 Type = ELF::SHT_NOTE; 468 else if (TypeName == "unwind") 469 Type = ELF::SHT_X86_64_UNWIND; 470 else 471 return TokError("unknown section type"); 472 } 473 474 if (UseLastGroup) { 475 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection(); 476 if (const MCSectionELF *Section = 477 cast_or_null<MCSectionELF>(CurrentSection.first)) 478 if (const MCSymbol *Group = Section->getGroup()) { 479 GroupName = Group->getName(); 480 Flags |= ELF::SHF_GROUP; 481 } 482 } 483 484 SectionKind Kind = computeSectionKind(Flags); 485 getStreamer().SwitchSection(getContext().getELFSection(SectionName, Type, 486 Flags, Kind, Size, 487 GroupName), 488 Subsection); 489 return false; 490} 491 492bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) { 493 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection(); 494 if (PreviousSection.first == NULL) 495 return TokError(".previous without corresponding .section"); 496 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second); 497 498 return false; 499} 500 501/// ParseDirectiveELFType 502/// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE> 503/// ::= .type identifier , #attribute 504/// ::= .type identifier , @attribute 505/// ::= .type identifier , %attribute 506/// ::= .type identifier , "attribute" 507bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) { 508 StringRef Name; 509 if (getParser().parseIdentifier(Name)) 510 return TokError("expected identifier in directive"); 511 512 // Handle the identifier as the key symbol. 513 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); 514 515 if (getLexer().isNot(AsmToken::Comma)) 516 return TokError("unexpected token in '.type' directive"); 517 Lex(); 518 519 StringRef Type; 520 SMLoc TypeLoc; 521 MCSymbolAttr Attr; 522 if (getLexer().is(AsmToken::Identifier)) { 523 TypeLoc = getLexer().getLoc(); 524 if (getParser().parseIdentifier(Type)) 525 return TokError("expected symbol type in directive"); 526 Attr = StringSwitch<MCSymbolAttr>(Type) 527 .Case("STT_FUNC", MCSA_ELF_TypeFunction) 528 .Case("STT_OBJECT", MCSA_ELF_TypeObject) 529 .Case("STT_TLS", MCSA_ELF_TypeTLS) 530 .Case("STT_COMMON", MCSA_ELF_TypeCommon) 531 .Case("STT_NOTYPE", MCSA_ELF_TypeNoType) 532 .Case("STT_GNU_IFUNC", MCSA_ELF_TypeIndFunction) 533 .Default(MCSA_Invalid); 534 } else if (getLexer().is(AsmToken::Hash) || getLexer().is(AsmToken::At) || 535 getLexer().is(AsmToken::Percent) || 536 getLexer().is(AsmToken::String)) { 537 if (!getLexer().is(AsmToken::String)) 538 Lex(); 539 540 TypeLoc = getLexer().getLoc(); 541 if (getParser().parseIdentifier(Type)) 542 return TokError("expected symbol type in directive"); 543 Attr = StringSwitch<MCSymbolAttr>(Type) 544 .Case("function", MCSA_ELF_TypeFunction) 545 .Case("object", MCSA_ELF_TypeObject) 546 .Case("tls_object", MCSA_ELF_TypeTLS) 547 .Case("common", MCSA_ELF_TypeCommon) 548 .Case("notype", MCSA_ELF_TypeNoType) 549 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject) 550 .Case("gnu_indirect_function", MCSA_ELF_TypeIndFunction) 551 .Default(MCSA_Invalid); 552 } else 553 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', " 554 "'%<type>' or \"<type>\""); 555 556 if (Attr == MCSA_Invalid) 557 return Error(TypeLoc, "unsupported attribute in '.type' directive"); 558 559 if (getLexer().isNot(AsmToken::EndOfStatement)) 560 return TokError("unexpected token in '.type' directive"); 561 562 Lex(); 563 564 getStreamer().EmitSymbolAttribute(Sym, Attr); 565 566 return false; 567} 568 569/// ParseDirectiveIdent 570/// ::= .ident string 571bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) { 572 if (getLexer().isNot(AsmToken::String)) 573 return TokError("unexpected token in '.ident' directive"); 574 575 StringRef Data = getTok().getIdentifier(); 576 577 Lex(); 578 579 getStreamer().EmitIdent(Data); 580 return false; 581} 582 583/// ParseDirectiveSymver 584/// ::= .symver foo, bar2@zed 585bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) { 586 StringRef Name; 587 if (getParser().parseIdentifier(Name)) 588 return TokError("expected identifier in directive"); 589 590 if (getLexer().isNot(AsmToken::Comma)) 591 return TokError("expected a comma"); 592 593 Lex(); 594 595 StringRef AliasName; 596 if (getParser().parseIdentifier(AliasName)) 597 return TokError("expected identifier in directive"); 598 599 if (AliasName.find('@') == StringRef::npos) 600 return TokError("expected a '@' in the name"); 601 602 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName); 603 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); 604 const MCExpr *Value = MCSymbolRefExpr::Create(Sym, getContext()); 605 606 getStreamer().EmitAssignment(Alias, Value); 607 return false; 608} 609 610/// ParseDirectiveVersion 611/// ::= .version string 612bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) { 613 if (getLexer().isNot(AsmToken::String)) 614 return TokError("unexpected token in '.version' directive"); 615 616 StringRef Data = getTok().getIdentifier(); 617 618 Lex(); 619 620 const MCSection *Note = 621 getContext().getELFSection(".note", ELF::SHT_NOTE, 0, 622 SectionKind::getReadOnly()); 623 624 getStreamer().PushSection(); 625 getStreamer().SwitchSection(Note); 626 getStreamer().EmitIntValue(Data.size()+1, 4); // namesz. 627 getStreamer().EmitIntValue(0, 4); // descsz = 0 (no description). 628 getStreamer().EmitIntValue(1, 4); // type = NT_VERSION. 629 getStreamer().EmitBytes(Data); // name. 630 getStreamer().EmitIntValue(0, 1); // terminate the string. 631 getStreamer().EmitValueToAlignment(4); // ensure 4 byte alignment. 632 getStreamer().PopSection(); 633 return false; 634} 635 636/// ParseDirectiveWeakref 637/// ::= .weakref foo, bar 638bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) { 639 // FIXME: Share code with the other alias building directives. 640 641 StringRef AliasName; 642 if (getParser().parseIdentifier(AliasName)) 643 return TokError("expected identifier in directive"); 644 645 if (getLexer().isNot(AsmToken::Comma)) 646 return TokError("expected a comma"); 647 648 Lex(); 649 650 StringRef Name; 651 if (getParser().parseIdentifier(Name)) 652 return TokError("expected identifier in directive"); 653 654 MCSymbol *Alias = getContext().GetOrCreateSymbol(AliasName); 655 656 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); 657 658 getStreamer().EmitWeakReference(Alias, Sym); 659 return false; 660} 661 662bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) { 663 const MCExpr *Subsection = 0; 664 if (getLexer().isNot(AsmToken::EndOfStatement)) { 665 if (getParser().parseExpression(Subsection)) 666 return true; 667 } 668 669 if (getLexer().isNot(AsmToken::EndOfStatement)) 670 return TokError("unexpected token in directive"); 671 672 getStreamer().SubSection(Subsection); 673 return false; 674} 675 676namespace llvm { 677 678MCAsmParserExtension *createELFAsmParser() { 679 return new ELFAsmParser; 680} 681 682} 683