1 //===- ELFAsmParser.cpp - ELF Assembly Parser -----------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/StringRef.h" 10 #include "llvm/ADT/StringSwitch.h" 11 #include "llvm/BinaryFormat/ELF.h" 12 #include "llvm/MC/MCAsmInfo.h" 13 #include "llvm/MC/MCContext.h" 14 #include "llvm/MC/MCDirectives.h" 15 #include "llvm/MC/MCExpr.h" 16 #include "llvm/MC/MCParser/MCAsmLexer.h" 17 #include "llvm/MC/MCParser/MCAsmParser.h" 18 #include "llvm/MC/MCParser/MCAsmParserExtension.h" 19 #include "llvm/MC/MCSection.h" 20 #include "llvm/MC/MCSectionELF.h" 21 #include "llvm/MC/MCStreamer.h" 22 #include "llvm/MC/MCSymbol.h" 23 #include "llvm/MC/MCSymbolELF.h" 24 #include "llvm/MC/SectionKind.h" 25 #include "llvm/Support/Casting.h" 26 #include "llvm/Support/MathExtras.h" 27 #include "llvm/Support/SMLoc.h" 28 #include <cassert> 29 #include <cstdint> 30 #include <utility> 31 32 using namespace llvm; 33 34 namespace { 35 36 class ELFAsmParser : public MCAsmParserExtension { 37 template<bool (ELFAsmParser::*HandlerMethod)(StringRef, SMLoc)> 38 void addDirectiveHandler(StringRef Directive) { 39 MCAsmParser::ExtensionDirectiveHandler Handler = std::make_pair( 40 this, HandleDirective<ELFAsmParser, HandlerMethod>); 41 42 getParser().addDirectiveHandler(Directive, Handler); 43 } 44 45 bool ParseSectionSwitch(StringRef Section, unsigned Type, unsigned Flags, 46 SectionKind Kind); 47 48 public: 49 ELFAsmParser() { BracketExpressionsSupported = true; } 50 51 void Initialize(MCAsmParser &Parser) override { 52 // Call the base implementation. 53 this->MCAsmParserExtension::Initialize(Parser); 54 55 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveData>(".data"); 56 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveText>(".text"); 57 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveBSS>(".bss"); 58 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveRoData>(".rodata"); 59 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTData>(".tdata"); 60 addDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveTBSS>(".tbss"); 61 addDirectiveHandler< 62 &ELFAsmParser::ParseSectionDirectiveDataRel>(".data.rel"); 63 addDirectiveHandler< 64 &ELFAsmParser::ParseSectionDirectiveDataRelRo>(".data.rel.ro"); 65 addDirectiveHandler< 66 &ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame"); 67 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section"); 68 addDirectiveHandler< 69 &ELFAsmParser::ParseDirectivePushSection>(".pushsection"); 70 addDirectiveHandler<&ELFAsmParser::ParseDirectivePopSection>(".popsection"); 71 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size"); 72 addDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous"); 73 addDirectiveHandler<&ELFAsmParser::ParseDirectiveType>(".type"); 74 addDirectiveHandler<&ELFAsmParser::ParseDirectiveIdent>(".ident"); 75 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymver>(".symver"); 76 addDirectiveHandler<&ELFAsmParser::ParseDirectiveVersion>(".version"); 77 addDirectiveHandler<&ELFAsmParser::ParseDirectiveWeakref>(".weakref"); 78 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".weak"); 79 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSymbolAttribute>(".local"); 80 addDirectiveHandler< 81 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".protected"); 82 addDirectiveHandler< 83 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".internal"); 84 addDirectiveHandler< 85 &ELFAsmParser::ParseDirectiveSymbolAttribute>(".hidden"); 86 addDirectiveHandler<&ELFAsmParser::ParseDirectiveSubsection>(".subsection"); 87 addDirectiveHandler<&ELFAsmParser::ParseDirectiveCGProfile>(".cg_profile"); 88 } 89 90 // FIXME: Part of this logic is duplicated in the MCELFStreamer. What is 91 // the best way for us to get access to it? 92 bool ParseSectionDirectiveData(StringRef, SMLoc) { 93 return ParseSectionSwitch(".data", ELF::SHT_PROGBITS, 94 ELF::SHF_WRITE | ELF::SHF_ALLOC, 95 SectionKind::getData()); 96 } 97 bool ParseSectionDirectiveText(StringRef, SMLoc) { 98 return ParseSectionSwitch(".text", ELF::SHT_PROGBITS, 99 ELF::SHF_EXECINSTR | 100 ELF::SHF_ALLOC, SectionKind::getText()); 101 } 102 bool ParseSectionDirectiveBSS(StringRef, SMLoc) { 103 return ParseSectionSwitch(".bss", ELF::SHT_NOBITS, 104 ELF::SHF_WRITE | 105 ELF::SHF_ALLOC, SectionKind::getBSS()); 106 } 107 bool ParseSectionDirectiveRoData(StringRef, SMLoc) { 108 return ParseSectionSwitch(".rodata", ELF::SHT_PROGBITS, 109 ELF::SHF_ALLOC, 110 SectionKind::getReadOnly()); 111 } 112 bool ParseSectionDirectiveTData(StringRef, SMLoc) { 113 return ParseSectionSwitch(".tdata", ELF::SHT_PROGBITS, 114 ELF::SHF_ALLOC | 115 ELF::SHF_TLS | ELF::SHF_WRITE, 116 SectionKind::getThreadData()); 117 } 118 bool ParseSectionDirectiveTBSS(StringRef, SMLoc) { 119 return ParseSectionSwitch(".tbss", ELF::SHT_NOBITS, 120 ELF::SHF_ALLOC | 121 ELF::SHF_TLS | ELF::SHF_WRITE, 122 SectionKind::getThreadBSS()); 123 } 124 bool ParseSectionDirectiveDataRel(StringRef, SMLoc) { 125 return ParseSectionSwitch(".data.rel", ELF::SHT_PROGBITS, 126 ELF::SHF_ALLOC | ELF::SHF_WRITE, 127 SectionKind::getData()); 128 } 129 bool ParseSectionDirectiveDataRelRo(StringRef, SMLoc) { 130 return ParseSectionSwitch(".data.rel.ro", ELF::SHT_PROGBITS, 131 ELF::SHF_ALLOC | 132 ELF::SHF_WRITE, 133 SectionKind::getReadOnlyWithRel()); 134 } 135 bool ParseSectionDirectiveEhFrame(StringRef, SMLoc) { 136 return ParseSectionSwitch(".eh_frame", ELF::SHT_PROGBITS, 137 ELF::SHF_ALLOC | ELF::SHF_WRITE, 138 SectionKind::getData()); 139 } 140 bool ParseDirectivePushSection(StringRef, SMLoc); 141 bool ParseDirectivePopSection(StringRef, SMLoc); 142 bool ParseDirectiveSection(StringRef, SMLoc); 143 bool ParseDirectiveSize(StringRef, SMLoc); 144 bool ParseDirectivePrevious(StringRef, SMLoc); 145 bool ParseDirectiveType(StringRef, SMLoc); 146 bool ParseDirectiveIdent(StringRef, SMLoc); 147 bool ParseDirectiveSymver(StringRef, SMLoc); 148 bool ParseDirectiveVersion(StringRef, SMLoc); 149 bool ParseDirectiveWeakref(StringRef, SMLoc); 150 bool ParseDirectiveSymbolAttribute(StringRef, SMLoc); 151 bool ParseDirectiveSubsection(StringRef, SMLoc); 152 bool ParseDirectiveCGProfile(StringRef, SMLoc); 153 154 private: 155 bool ParseSectionName(StringRef &SectionName); 156 bool ParseSectionArguments(bool IsPush, SMLoc loc); 157 unsigned parseSunStyleSectionFlags(); 158 bool maybeParseSectionType(StringRef &TypeName); 159 bool parseMergeSize(int64_t &Size); 160 bool parseGroup(StringRef &GroupName, bool &IsComdat); 161 bool parseLinkedToSym(MCSymbolELF *&LinkedToSym); 162 bool maybeParseUniqueID(int64_t &UniqueID); 163 }; 164 165 } // end anonymous namespace 166 167 /// ParseDirectiveSymbolAttribute 168 /// ::= { ".local", ".weak", ... } [ identifier ( , identifier )* ] 169 bool ELFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) { 170 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive) 171 .Case(".weak", MCSA_Weak) 172 .Case(".local", MCSA_Local) 173 .Case(".hidden", MCSA_Hidden) 174 .Case(".internal", MCSA_Internal) 175 .Case(".protected", MCSA_Protected) 176 .Default(MCSA_Invalid); 177 assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!"); 178 if (getLexer().isNot(AsmToken::EndOfStatement)) { 179 while (true) { 180 StringRef Name; 181 182 if (getParser().parseIdentifier(Name)) 183 return TokError("expected identifier in directive"); 184 185 if (getParser().discardLTOSymbol(Name)) { 186 if (getLexer().is(AsmToken::EndOfStatement)) 187 break; 188 continue; 189 } 190 191 MCSymbol *Sym = getContext().getOrCreateSymbol(Name); 192 193 getStreamer().emitSymbolAttribute(Sym, Attr); 194 195 if (getLexer().is(AsmToken::EndOfStatement)) 196 break; 197 198 if (getLexer().isNot(AsmToken::Comma)) 199 return TokError("unexpected token in directive"); 200 Lex(); 201 } 202 } 203 204 Lex(); 205 return false; 206 } 207 208 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type, 209 unsigned Flags, SectionKind Kind) { 210 const MCExpr *Subsection = nullptr; 211 if (getLexer().isNot(AsmToken::EndOfStatement)) { 212 if (getParser().parseExpression(Subsection)) 213 return true; 214 } 215 Lex(); 216 217 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags), 218 Subsection); 219 220 return false; 221 } 222 223 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) { 224 StringRef Name; 225 if (getParser().parseIdentifier(Name)) 226 return TokError("expected identifier in directive"); 227 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name)); 228 229 if (getLexer().isNot(AsmToken::Comma)) 230 return TokError("unexpected token in directive"); 231 Lex(); 232 233 const MCExpr *Expr; 234 if (getParser().parseExpression(Expr)) 235 return true; 236 237 if (getLexer().isNot(AsmToken::EndOfStatement)) 238 return TokError("unexpected token in directive"); 239 Lex(); 240 241 getStreamer().emitELFSize(Sym, Expr); 242 return false; 243 } 244 245 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) { 246 // A section name can contain -, so we cannot just use 247 // parseIdentifier. 248 SMLoc FirstLoc = getLexer().getLoc(); 249 unsigned Size = 0; 250 251 if (getLexer().is(AsmToken::String)) { 252 SectionName = getTok().getIdentifier(); 253 Lex(); 254 return false; 255 } 256 257 while (!getParser().hasPendingError()) { 258 SMLoc PrevLoc = getLexer().getLoc(); 259 if (getLexer().is(AsmToken::Comma) || 260 getLexer().is(AsmToken::EndOfStatement)) 261 break; 262 263 unsigned CurSize; 264 if (getLexer().is(AsmToken::String)) { 265 CurSize = getTok().getIdentifier().size() + 2; 266 Lex(); 267 } else if (getLexer().is(AsmToken::Identifier)) { 268 CurSize = getTok().getIdentifier().size(); 269 Lex(); 270 } else { 271 CurSize = getTok().getString().size(); 272 Lex(); 273 } 274 Size += CurSize; 275 SectionName = StringRef(FirstLoc.getPointer(), Size); 276 277 // Make sure the following token is adjacent. 278 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer()) 279 break; 280 } 281 if (Size == 0) 282 return true; 283 284 return false; 285 } 286 287 static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) { 288 unsigned flags = 0; 289 290 // If a valid numerical value is set for the section flag, use it verbatim 291 if (!flagsStr.getAsInteger(0, flags)) 292 return flags; 293 294 for (char i : flagsStr) { 295 switch (i) { 296 case 'a': 297 flags |= ELF::SHF_ALLOC; 298 break; 299 case 'e': 300 flags |= ELF::SHF_EXCLUDE; 301 break; 302 case 'x': 303 flags |= ELF::SHF_EXECINSTR; 304 break; 305 case 'w': 306 flags |= ELF::SHF_WRITE; 307 break; 308 case 'o': 309 flags |= ELF::SHF_LINK_ORDER; 310 break; 311 case 'M': 312 flags |= ELF::SHF_MERGE; 313 break; 314 case 'S': 315 flags |= ELF::SHF_STRINGS; 316 break; 317 case 'T': 318 flags |= ELF::SHF_TLS; 319 break; 320 case 'c': 321 flags |= ELF::XCORE_SHF_CP_SECTION; 322 break; 323 case 'd': 324 flags |= ELF::XCORE_SHF_DP_SECTION; 325 break; 326 case 'y': 327 flags |= ELF::SHF_ARM_PURECODE; 328 break; 329 case 's': 330 flags |= ELF::SHF_HEX_GPREL; 331 break; 332 case 'G': 333 flags |= ELF::SHF_GROUP; 334 break; 335 case 'R': 336 flags |= ELF::SHF_GNU_RETAIN; 337 break; 338 case '?': 339 *UseLastGroup = true; 340 break; 341 default: 342 return -1U; 343 } 344 } 345 346 return flags; 347 } 348 349 unsigned ELFAsmParser::parseSunStyleSectionFlags() { 350 unsigned flags = 0; 351 while (getLexer().is(AsmToken::Hash)) { 352 Lex(); // Eat the #. 353 354 if (!getLexer().is(AsmToken::Identifier)) 355 return -1U; 356 357 StringRef flagId = getTok().getIdentifier(); 358 if (flagId == "alloc") 359 flags |= ELF::SHF_ALLOC; 360 else if (flagId == "execinstr") 361 flags |= ELF::SHF_EXECINSTR; 362 else if (flagId == "write") 363 flags |= ELF::SHF_WRITE; 364 else if (flagId == "tls") 365 flags |= ELF::SHF_TLS; 366 else 367 return -1U; 368 369 Lex(); // Eat the flag. 370 371 if (!getLexer().is(AsmToken::Comma)) 372 break; 373 Lex(); // Eat the comma. 374 } 375 return flags; 376 } 377 378 379 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) { 380 getStreamer().PushSection(); 381 382 if (ParseSectionArguments(/*IsPush=*/true, loc)) { 383 getStreamer().PopSection(); 384 return true; 385 } 386 387 return false; 388 } 389 390 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) { 391 if (!getStreamer().PopSection()) 392 return TokError(".popsection without corresponding .pushsection"); 393 return false; 394 } 395 396 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) { 397 return ParseSectionArguments(/*IsPush=*/false, loc); 398 } 399 400 bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) { 401 MCAsmLexer &L = getLexer(); 402 if (L.isNot(AsmToken::Comma)) 403 return false; 404 Lex(); 405 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) && 406 L.isNot(AsmToken::String)) { 407 if (L.getAllowAtInIdentifier()) 408 return TokError("expected '@<type>', '%<type>' or \"<type>\""); 409 else 410 return TokError("expected '%<type>' or \"<type>\""); 411 } 412 if (!L.is(AsmToken::String)) 413 Lex(); 414 if (L.is(AsmToken::Integer)) { 415 TypeName = getTok().getString(); 416 Lex(); 417 } else if (getParser().parseIdentifier(TypeName)) 418 return TokError("expected identifier in directive"); 419 return false; 420 } 421 422 bool ELFAsmParser::parseMergeSize(int64_t &Size) { 423 if (getLexer().isNot(AsmToken::Comma)) 424 return TokError("expected the entry size"); 425 Lex(); 426 if (getParser().parseAbsoluteExpression(Size)) 427 return true; 428 if (Size <= 0) 429 return TokError("entry size must be positive"); 430 return false; 431 } 432 433 bool ELFAsmParser::parseGroup(StringRef &GroupName, bool &IsComdat) { 434 MCAsmLexer &L = getLexer(); 435 if (L.isNot(AsmToken::Comma)) 436 return TokError("expected group name"); 437 Lex(); 438 if (L.is(AsmToken::Integer)) { 439 GroupName = getTok().getString(); 440 Lex(); 441 } else if (getParser().parseIdentifier(GroupName)) { 442 return TokError("invalid group name"); 443 } 444 if (L.is(AsmToken::Comma)) { 445 Lex(); 446 StringRef Linkage; 447 if (getParser().parseIdentifier(Linkage)) 448 return TokError("invalid linkage"); 449 if (Linkage != "comdat") 450 return TokError("Linkage must be 'comdat'"); 451 IsComdat = true; 452 } else { 453 IsComdat = false; 454 } 455 return false; 456 } 457 458 bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) { 459 MCAsmLexer &L = getLexer(); 460 if (L.isNot(AsmToken::Comma)) 461 return TokError("expected linked-to symbol"); 462 Lex(); 463 StringRef Name; 464 SMLoc StartLoc = L.getLoc(); 465 if (getParser().parseIdentifier(Name)) { 466 if (getParser().getTok().getString() == "0") { 467 getParser().Lex(); 468 LinkedToSym = nullptr; 469 return false; 470 } 471 return TokError("invalid linked-to symbol"); 472 } 473 LinkedToSym = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name)); 474 if (!LinkedToSym || !LinkedToSym->isInSection()) 475 return Error(StartLoc, "linked-to symbol is not in a section: " + Name); 476 return false; 477 } 478 479 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) { 480 MCAsmLexer &L = getLexer(); 481 if (L.isNot(AsmToken::Comma)) 482 return false; 483 Lex(); 484 StringRef UniqueStr; 485 if (getParser().parseIdentifier(UniqueStr)) 486 return TokError("expected identifier in directive"); 487 if (UniqueStr != "unique") 488 return TokError("expected 'unique'"); 489 if (L.isNot(AsmToken::Comma)) 490 return TokError("expected commma"); 491 Lex(); 492 if (getParser().parseAbsoluteExpression(UniqueID)) 493 return true; 494 if (UniqueID < 0) 495 return TokError("unique id must be positive"); 496 if (!isUInt<32>(UniqueID) || UniqueID == ~0U) 497 return TokError("unique id is too large"); 498 return false; 499 } 500 501 static bool hasPrefix(StringRef SectionName, StringRef Prefix) { 502 return SectionName.consume_front(Prefix) && 503 (SectionName.empty() || SectionName[0] == '.'); 504 } 505 506 static bool allowSectionTypeMismatch(const Triple &TT, StringRef SectionName, 507 unsigned Type) { 508 if (TT.getArch() == Triple::x86_64) { 509 // x86-64 psABI names SHT_X86_64_UNWIND as the canonical type for .eh_frame, 510 // but GNU as emits SHT_PROGBITS .eh_frame for .cfi_* directives. Don't 511 // error for SHT_PROGBITS .eh_frame 512 return SectionName == ".eh_frame" && Type == ELF::SHT_PROGBITS; 513 } 514 if (TT.isMIPS()) { 515 // MIPS .debug_* sections should have SHT_MIPS_DWARF section type to 516 // distinguish among sections contain DWARF and ECOFF debug formats, 517 // but in assembly files these sections have SHT_PROGBITS type. 518 return SectionName.startswith(".debug_") && Type == ELF::SHT_PROGBITS; 519 } 520 return false; 521 } 522 523 bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) { 524 StringRef SectionName; 525 526 if (ParseSectionName(SectionName)) 527 return TokError("expected identifier in directive"); 528 529 StringRef TypeName; 530 int64_t Size = 0; 531 StringRef GroupName; 532 bool IsComdat = false; 533 unsigned Flags = 0; 534 unsigned extraFlags = 0; 535 const MCExpr *Subsection = nullptr; 536 bool UseLastGroup = false; 537 MCSymbolELF *LinkedToSym = nullptr; 538 int64_t UniqueID = ~0; 539 540 // Set the defaults first. 541 if (hasPrefix(SectionName, ".rodata") || SectionName == ".rodata1") 542 Flags |= ELF::SHF_ALLOC; 543 else if (SectionName == ".fini" || SectionName == ".init" || 544 hasPrefix(SectionName, ".text")) 545 Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR; 546 else if (hasPrefix(SectionName, ".data") || SectionName == ".data1" || 547 hasPrefix(SectionName, ".bss") || 548 hasPrefix(SectionName, ".init_array") || 549 hasPrefix(SectionName, ".fini_array") || 550 hasPrefix(SectionName, ".preinit_array")) 551 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE; 552 else if (hasPrefix(SectionName, ".tdata") || hasPrefix(SectionName, ".tbss")) 553 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS; 554 555 if (getLexer().is(AsmToken::Comma)) { 556 Lex(); 557 558 if (IsPush && getLexer().isNot(AsmToken::String)) { 559 if (getParser().parseExpression(Subsection)) 560 return true; 561 if (getLexer().isNot(AsmToken::Comma)) 562 goto EndStmt; 563 Lex(); 564 } 565 566 if (getLexer().isNot(AsmToken::String)) { 567 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax() 568 || getLexer().isNot(AsmToken::Hash)) 569 return TokError("expected string in directive"); 570 extraFlags = parseSunStyleSectionFlags(); 571 } else { 572 StringRef FlagsStr = getTok().getStringContents(); 573 Lex(); 574 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup); 575 } 576 577 if (extraFlags == -1U) 578 return TokError("unknown flag"); 579 Flags |= extraFlags; 580 581 bool Mergeable = Flags & ELF::SHF_MERGE; 582 bool Group = Flags & ELF::SHF_GROUP; 583 if (Group && UseLastGroup) 584 return TokError("Section cannot specifiy a group name while also acting " 585 "as a member of the last group"); 586 587 if (maybeParseSectionType(TypeName)) 588 return true; 589 590 MCAsmLexer &L = getLexer(); 591 if (TypeName.empty()) { 592 if (Mergeable) 593 return TokError("Mergeable section must specify the type"); 594 if (Group) 595 return TokError("Group section must specify the type"); 596 if (L.isNot(AsmToken::EndOfStatement)) 597 return TokError("unexpected token in directive"); 598 } 599 600 if (Mergeable) 601 if (parseMergeSize(Size)) 602 return true; 603 if (Group) 604 if (parseGroup(GroupName, IsComdat)) 605 return true; 606 if (Flags & ELF::SHF_LINK_ORDER) 607 if (parseLinkedToSym(LinkedToSym)) 608 return true; 609 if (maybeParseUniqueID(UniqueID)) 610 return true; 611 } 612 613 EndStmt: 614 if (getLexer().isNot(AsmToken::EndOfStatement)) 615 return TokError("unexpected token in directive"); 616 Lex(); 617 618 unsigned Type = ELF::SHT_PROGBITS; 619 620 if (TypeName.empty()) { 621 if (SectionName.startswith(".note")) 622 Type = ELF::SHT_NOTE; 623 else if (hasPrefix(SectionName, ".init_array")) 624 Type = ELF::SHT_INIT_ARRAY; 625 else if (hasPrefix(SectionName, ".bss")) 626 Type = ELF::SHT_NOBITS; 627 else if (hasPrefix(SectionName, ".tbss")) 628 Type = ELF::SHT_NOBITS; 629 else if (hasPrefix(SectionName, ".fini_array")) 630 Type = ELF::SHT_FINI_ARRAY; 631 else if (hasPrefix(SectionName, ".preinit_array")) 632 Type = ELF::SHT_PREINIT_ARRAY; 633 } else { 634 if (TypeName == "init_array") 635 Type = ELF::SHT_INIT_ARRAY; 636 else if (TypeName == "fini_array") 637 Type = ELF::SHT_FINI_ARRAY; 638 else if (TypeName == "preinit_array") 639 Type = ELF::SHT_PREINIT_ARRAY; 640 else if (TypeName == "nobits") 641 Type = ELF::SHT_NOBITS; 642 else if (TypeName == "progbits") 643 Type = ELF::SHT_PROGBITS; 644 else if (TypeName == "note") 645 Type = ELF::SHT_NOTE; 646 else if (TypeName == "unwind") 647 Type = ELF::SHT_X86_64_UNWIND; 648 else if (TypeName == "llvm_odrtab") 649 Type = ELF::SHT_LLVM_ODRTAB; 650 else if (TypeName == "llvm_linker_options") 651 Type = ELF::SHT_LLVM_LINKER_OPTIONS; 652 else if (TypeName == "llvm_call_graph_profile") 653 Type = ELF::SHT_LLVM_CALL_GRAPH_PROFILE; 654 else if (TypeName == "llvm_dependent_libraries") 655 Type = ELF::SHT_LLVM_DEPENDENT_LIBRARIES; 656 else if (TypeName == "llvm_sympart") 657 Type = ELF::SHT_LLVM_SYMPART; 658 else if (TypeName == "llvm_bb_addr_map") 659 Type = ELF::SHT_LLVM_BB_ADDR_MAP; 660 else if (TypeName.getAsInteger(0, Type)) 661 return TokError("unknown section type"); 662 } 663 664 if (UseLastGroup) { 665 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection(); 666 if (const MCSectionELF *Section = 667 cast_or_null<MCSectionELF>(CurrentSection.first)) 668 if (const MCSymbol *Group = Section->getGroup()) { 669 GroupName = Group->getName(); 670 IsComdat = Section->isComdat(); 671 Flags |= ELF::SHF_GROUP; 672 } 673 } 674 675 MCSectionELF *Section = 676 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName, 677 IsComdat, UniqueID, LinkedToSym); 678 getStreamer().SwitchSection(Section, Subsection); 679 // Check that flags are used consistently. However, the GNU assembler permits 680 // to leave out in subsequent uses of the same sections; for compatibility, 681 // do likewise. 682 if (!TypeName.empty() && Section->getType() != Type && 683 !allowSectionTypeMismatch(getContext().getTargetTriple(), SectionName, 684 Type)) 685 Error(loc, "changed section type for " + SectionName + ", expected: 0x" + 686 utohexstr(Section->getType())); 687 if ((extraFlags || Size || !TypeName.empty()) && Section->getFlags() != Flags) 688 Error(loc, "changed section flags for " + SectionName + ", expected: 0x" + 689 utohexstr(Section->getFlags())); 690 if ((extraFlags || Size || !TypeName.empty()) && 691 Section->getEntrySize() != Size) 692 Error(loc, "changed section entsize for " + SectionName + 693 ", expected: " + Twine(Section->getEntrySize())); 694 695 if (getContext().getGenDwarfForAssembly() && 696 (Section->getFlags() & ELF::SHF_ALLOC) && 697 (Section->getFlags() & ELF::SHF_EXECINSTR)) { 698 bool InsertResult = getContext().addGenDwarfSection(Section); 699 if (InsertResult) { 700 if (getContext().getDwarfVersion() <= 2) 701 Warning(loc, "DWARF2 only supports one section per compilation unit"); 702 703 if (!Section->getBeginSymbol()) { 704 MCSymbol *SectionStartSymbol = getContext().createTempSymbol(); 705 getStreamer().emitLabel(SectionStartSymbol); 706 Section->setBeginSymbol(SectionStartSymbol); 707 } 708 } 709 } 710 711 return false; 712 } 713 714 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) { 715 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection(); 716 if (PreviousSection.first == nullptr) 717 return TokError(".previous without corresponding .section"); 718 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second); 719 720 return false; 721 } 722 723 static MCSymbolAttr MCAttrForString(StringRef Type) { 724 return StringSwitch<MCSymbolAttr>(Type) 725 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction) 726 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject) 727 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS) 728 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon) 729 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType) 730 .Cases("STT_GNU_IFUNC", "gnu_indirect_function", 731 MCSA_ELF_TypeIndFunction) 732 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject) 733 .Default(MCSA_Invalid); 734 } 735 736 /// ParseDirectiveELFType 737 /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE> 738 /// ::= .type identifier , #attribute 739 /// ::= .type identifier , @attribute 740 /// ::= .type identifier , %attribute 741 /// ::= .type identifier , "attribute" 742 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) { 743 StringRef Name; 744 if (getParser().parseIdentifier(Name)) 745 return TokError("expected identifier in directive"); 746 747 // Handle the identifier as the key symbol. 748 MCSymbol *Sym = getContext().getOrCreateSymbol(Name); 749 750 // NOTE the comma is optional in all cases. It is only documented as being 751 // optional in the first case, however, GAS will silently treat the comma as 752 // optional in all cases. Furthermore, although the documentation states that 753 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS 754 // accepts both the upper case name as well as the lower case aliases. 755 if (getLexer().is(AsmToken::Comma)) 756 Lex(); 757 758 if (getLexer().isNot(AsmToken::Identifier) && 759 getLexer().isNot(AsmToken::Hash) && 760 getLexer().isNot(AsmToken::Percent) && 761 getLexer().isNot(AsmToken::String)) { 762 if (!getLexer().getAllowAtInIdentifier()) 763 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', " 764 "'%<type>' or \"<type>\""); 765 else if (getLexer().isNot(AsmToken::At)) 766 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', " 767 "'%<type>' or \"<type>\""); 768 } 769 770 if (getLexer().isNot(AsmToken::String) && 771 getLexer().isNot(AsmToken::Identifier)) 772 Lex(); 773 774 SMLoc TypeLoc = getLexer().getLoc(); 775 776 StringRef Type; 777 if (getParser().parseIdentifier(Type)) 778 return TokError("expected symbol type in directive"); 779 780 MCSymbolAttr Attr = MCAttrForString(Type); 781 if (Attr == MCSA_Invalid) 782 return Error(TypeLoc, "unsupported attribute in '.type' directive"); 783 784 if (getLexer().isNot(AsmToken::EndOfStatement)) 785 return TokError("unexpected token in '.type' directive"); 786 Lex(); 787 788 getStreamer().emitSymbolAttribute(Sym, Attr); 789 790 return false; 791 } 792 793 /// ParseDirectiveIdent 794 /// ::= .ident string 795 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) { 796 if (getLexer().isNot(AsmToken::String)) 797 return TokError("unexpected token in '.ident' directive"); 798 799 StringRef Data = getTok().getIdentifier(); 800 801 Lex(); 802 803 if (getLexer().isNot(AsmToken::EndOfStatement)) 804 return TokError("unexpected token in '.ident' directive"); 805 Lex(); 806 807 getStreamer().emitIdent(Data); 808 return false; 809 } 810 811 /// ParseDirectiveSymver 812 /// ::= .symver foo, bar2@zed 813 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) { 814 StringRef OriginalName, Name, Action; 815 if (getParser().parseIdentifier(OriginalName)) 816 return TokError("expected identifier in directive"); 817 818 if (getLexer().isNot(AsmToken::Comma)) 819 return TokError("expected a comma"); 820 821 // ARM assembly uses @ for a comment... 822 // except when parsing the second parameter of the .symver directive. 823 // Force the next symbol to allow @ in the identifier, which is 824 // required for this directive and then reset it to its initial state. 825 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier(); 826 getLexer().setAllowAtInIdentifier(true); 827 Lex(); 828 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier); 829 830 if (getParser().parseIdentifier(Name)) 831 return TokError("expected identifier in directive"); 832 833 if (!Name.contains('@')) 834 return TokError("expected a '@' in the name"); 835 bool KeepOriginalSym = !Name.contains("@@@"); 836 if (parseOptionalToken(AsmToken::Comma)) { 837 if (getParser().parseIdentifier(Action) || Action != "remove") 838 return TokError("expected 'remove'"); 839 KeepOriginalSym = false; 840 } 841 (void)parseOptionalToken(AsmToken::EndOfStatement); 842 843 getStreamer().emitELFSymverDirective( 844 getContext().getOrCreateSymbol(OriginalName), Name, KeepOriginalSym); 845 return false; 846 } 847 848 /// ParseDirectiveVersion 849 /// ::= .version string 850 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) { 851 if (getLexer().isNot(AsmToken::String)) 852 return TokError("unexpected token in '.version' directive"); 853 854 StringRef Data = getTok().getIdentifier(); 855 856 Lex(); 857 858 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0); 859 860 getStreamer().PushSection(); 861 getStreamer().SwitchSection(Note); 862 getStreamer().emitInt32(Data.size() + 1); // namesz 863 getStreamer().emitInt32(0); // descsz = 0 (no description). 864 getStreamer().emitInt32(1); // type = NT_VERSION 865 getStreamer().emitBytes(Data); // name 866 getStreamer().emitInt8(0); // NUL 867 getStreamer().emitValueToAlignment(4); 868 getStreamer().PopSection(); 869 return false; 870 } 871 872 /// ParseDirectiveWeakref 873 /// ::= .weakref foo, bar 874 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) { 875 // FIXME: Share code with the other alias building directives. 876 877 StringRef AliasName; 878 if (getParser().parseIdentifier(AliasName)) 879 return TokError("expected identifier in directive"); 880 881 if (getLexer().isNot(AsmToken::Comma)) 882 return TokError("expected a comma"); 883 884 Lex(); 885 886 StringRef Name; 887 if (getParser().parseIdentifier(Name)) 888 return TokError("expected identifier in directive"); 889 890 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName); 891 892 MCSymbol *Sym = getContext().getOrCreateSymbol(Name); 893 894 getStreamer().emitWeakReference(Alias, Sym); 895 return false; 896 } 897 898 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) { 899 const MCExpr *Subsection = nullptr; 900 if (getLexer().isNot(AsmToken::EndOfStatement)) { 901 if (getParser().parseExpression(Subsection)) 902 return true; 903 } 904 905 if (getLexer().isNot(AsmToken::EndOfStatement)) 906 return TokError("unexpected token in directive"); 907 908 Lex(); 909 910 getStreamer().SubSection(Subsection); 911 return false; 912 } 913 914 bool ELFAsmParser::ParseDirectiveCGProfile(StringRef S, SMLoc Loc) { 915 return MCAsmParserExtension::ParseDirectiveCGProfile(S, Loc); 916 } 917 918 namespace llvm { 919 920 MCAsmParserExtension *createELFAsmParser() { 921 return new ELFAsmParser; 922 } 923 924 } // end namespace llvm 925