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 MCSymbol *Sym = getContext().getOrCreateSymbol(Name); 186 187 getStreamer().emitSymbolAttribute(Sym, Attr); 188 189 if (getLexer().is(AsmToken::EndOfStatement)) 190 break; 191 192 if (getLexer().isNot(AsmToken::Comma)) 193 return TokError("unexpected token in directive"); 194 Lex(); 195 } 196 } 197 198 Lex(); 199 return false; 200 } 201 202 bool ELFAsmParser::ParseSectionSwitch(StringRef Section, unsigned Type, 203 unsigned Flags, SectionKind Kind) { 204 const MCExpr *Subsection = nullptr; 205 if (getLexer().isNot(AsmToken::EndOfStatement)) { 206 if (getParser().parseExpression(Subsection)) 207 return true; 208 } 209 Lex(); 210 211 getStreamer().SwitchSection(getContext().getELFSection(Section, Type, Flags), 212 Subsection); 213 214 return false; 215 } 216 217 bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) { 218 StringRef Name; 219 if (getParser().parseIdentifier(Name)) 220 return TokError("expected identifier in directive"); 221 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name)); 222 223 if (getLexer().isNot(AsmToken::Comma)) 224 return TokError("unexpected token in directive"); 225 Lex(); 226 227 const MCExpr *Expr; 228 if (getParser().parseExpression(Expr)) 229 return true; 230 231 if (getLexer().isNot(AsmToken::EndOfStatement)) 232 return TokError("unexpected token in directive"); 233 Lex(); 234 235 getStreamer().emitELFSize(Sym, Expr); 236 return false; 237 } 238 239 bool ELFAsmParser::ParseSectionName(StringRef &SectionName) { 240 // A section name can contain -, so we cannot just use 241 // parseIdentifier. 242 SMLoc FirstLoc = getLexer().getLoc(); 243 unsigned Size = 0; 244 245 if (getLexer().is(AsmToken::String)) { 246 SectionName = getTok().getIdentifier(); 247 Lex(); 248 return false; 249 } 250 251 while (!getParser().hasPendingError()) { 252 SMLoc PrevLoc = getLexer().getLoc(); 253 if (getLexer().is(AsmToken::Comma) || 254 getLexer().is(AsmToken::EndOfStatement)) 255 break; 256 257 unsigned CurSize; 258 if (getLexer().is(AsmToken::String)) { 259 CurSize = getTok().getIdentifier().size() + 2; 260 Lex(); 261 } else if (getLexer().is(AsmToken::Identifier)) { 262 CurSize = getTok().getIdentifier().size(); 263 Lex(); 264 } else { 265 CurSize = getTok().getString().size(); 266 Lex(); 267 } 268 Size += CurSize; 269 SectionName = StringRef(FirstLoc.getPointer(), Size); 270 271 // Make sure the following token is adjacent. 272 if (PrevLoc.getPointer() + CurSize != getTok().getLoc().getPointer()) 273 break; 274 } 275 if (Size == 0) 276 return true; 277 278 return false; 279 } 280 281 static unsigned parseSectionFlags(StringRef flagsStr, bool *UseLastGroup) { 282 unsigned flags = 0; 283 284 // If a valid numerical value is set for the section flag, use it verbatim 285 if (!flagsStr.getAsInteger(0, flags)) 286 return flags; 287 288 for (char i : flagsStr) { 289 switch (i) { 290 case 'a': 291 flags |= ELF::SHF_ALLOC; 292 break; 293 case 'e': 294 flags |= ELF::SHF_EXCLUDE; 295 break; 296 case 'x': 297 flags |= ELF::SHF_EXECINSTR; 298 break; 299 case 'w': 300 flags |= ELF::SHF_WRITE; 301 break; 302 case 'o': 303 flags |= ELF::SHF_LINK_ORDER; 304 break; 305 case 'M': 306 flags |= ELF::SHF_MERGE; 307 break; 308 case 'S': 309 flags |= ELF::SHF_STRINGS; 310 break; 311 case 'T': 312 flags |= ELF::SHF_TLS; 313 break; 314 case 'c': 315 flags |= ELF::XCORE_SHF_CP_SECTION; 316 break; 317 case 'd': 318 flags |= ELF::XCORE_SHF_DP_SECTION; 319 break; 320 case 'y': 321 flags |= ELF::SHF_ARM_PURECODE; 322 break; 323 case 's': 324 flags |= ELF::SHF_HEX_GPREL; 325 break; 326 case 'G': 327 flags |= ELF::SHF_GROUP; 328 break; 329 case 'R': 330 flags |= ELF::SHF_GNU_RETAIN; 331 break; 332 case '?': 333 *UseLastGroup = true; 334 break; 335 default: 336 return -1U; 337 } 338 } 339 340 return flags; 341 } 342 343 unsigned ELFAsmParser::parseSunStyleSectionFlags() { 344 unsigned flags = 0; 345 while (getLexer().is(AsmToken::Hash)) { 346 Lex(); // Eat the #. 347 348 if (!getLexer().is(AsmToken::Identifier)) 349 return -1U; 350 351 StringRef flagId = getTok().getIdentifier(); 352 if (flagId == "alloc") 353 flags |= ELF::SHF_ALLOC; 354 else if (flagId == "execinstr") 355 flags |= ELF::SHF_EXECINSTR; 356 else if (flagId == "write") 357 flags |= ELF::SHF_WRITE; 358 else if (flagId == "tls") 359 flags |= ELF::SHF_TLS; 360 else 361 return -1U; 362 363 Lex(); // Eat the flag. 364 365 if (!getLexer().is(AsmToken::Comma)) 366 break; 367 Lex(); // Eat the comma. 368 } 369 return flags; 370 } 371 372 373 bool ELFAsmParser::ParseDirectivePushSection(StringRef s, SMLoc loc) { 374 getStreamer().PushSection(); 375 376 if (ParseSectionArguments(/*IsPush=*/true, loc)) { 377 getStreamer().PopSection(); 378 return true; 379 } 380 381 return false; 382 } 383 384 bool ELFAsmParser::ParseDirectivePopSection(StringRef, SMLoc) { 385 if (!getStreamer().PopSection()) 386 return TokError(".popsection without corresponding .pushsection"); 387 return false; 388 } 389 390 bool ELFAsmParser::ParseDirectiveSection(StringRef, SMLoc loc) { 391 return ParseSectionArguments(/*IsPush=*/false, loc); 392 } 393 394 bool ELFAsmParser::maybeParseSectionType(StringRef &TypeName) { 395 MCAsmLexer &L = getLexer(); 396 if (L.isNot(AsmToken::Comma)) 397 return false; 398 Lex(); 399 if (L.isNot(AsmToken::At) && L.isNot(AsmToken::Percent) && 400 L.isNot(AsmToken::String)) { 401 if (L.getAllowAtInIdentifier()) 402 return TokError("expected '@<type>', '%<type>' or \"<type>\""); 403 else 404 return TokError("expected '%<type>' or \"<type>\""); 405 } 406 if (!L.is(AsmToken::String)) 407 Lex(); 408 if (L.is(AsmToken::Integer)) { 409 TypeName = getTok().getString(); 410 Lex(); 411 } else if (getParser().parseIdentifier(TypeName)) 412 return TokError("expected identifier in directive"); 413 return false; 414 } 415 416 bool ELFAsmParser::parseMergeSize(int64_t &Size) { 417 if (getLexer().isNot(AsmToken::Comma)) 418 return TokError("expected the entry size"); 419 Lex(); 420 if (getParser().parseAbsoluteExpression(Size)) 421 return true; 422 if (Size <= 0) 423 return TokError("entry size must be positive"); 424 return false; 425 } 426 427 bool ELFAsmParser::parseGroup(StringRef &GroupName, bool &IsComdat) { 428 MCAsmLexer &L = getLexer(); 429 if (L.isNot(AsmToken::Comma)) 430 return TokError("expected group name"); 431 Lex(); 432 if (L.is(AsmToken::Integer)) { 433 GroupName = getTok().getString(); 434 Lex(); 435 } else if (getParser().parseIdentifier(GroupName)) { 436 return TokError("invalid group name"); 437 } 438 if (L.is(AsmToken::Comma)) { 439 Lex(); 440 StringRef Linkage; 441 if (getParser().parseIdentifier(Linkage)) 442 return TokError("invalid linkage"); 443 if (Linkage != "comdat") 444 return TokError("Linkage must be 'comdat'"); 445 IsComdat = true; 446 } else { 447 IsComdat = false; 448 } 449 return false; 450 } 451 452 bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) { 453 MCAsmLexer &L = getLexer(); 454 if (L.isNot(AsmToken::Comma)) 455 return TokError("expected linked-to symbol"); 456 Lex(); 457 StringRef Name; 458 SMLoc StartLoc = L.getLoc(); 459 if (getParser().parseIdentifier(Name)) { 460 if (getParser().getTok().getString() == "0") { 461 getParser().Lex(); 462 LinkedToSym = nullptr; 463 return false; 464 } 465 return TokError("invalid linked-to symbol"); 466 } 467 LinkedToSym = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name)); 468 if (!LinkedToSym || !LinkedToSym->isInSection()) 469 return Error(StartLoc, "linked-to symbol is not in a section: " + Name); 470 return false; 471 } 472 473 bool ELFAsmParser::maybeParseUniqueID(int64_t &UniqueID) { 474 MCAsmLexer &L = getLexer(); 475 if (L.isNot(AsmToken::Comma)) 476 return false; 477 Lex(); 478 StringRef UniqueStr; 479 if (getParser().parseIdentifier(UniqueStr)) 480 return TokError("expected identifier in directive"); 481 if (UniqueStr != "unique") 482 return TokError("expected 'unique'"); 483 if (L.isNot(AsmToken::Comma)) 484 return TokError("expected commma"); 485 Lex(); 486 if (getParser().parseAbsoluteExpression(UniqueID)) 487 return true; 488 if (UniqueID < 0) 489 return TokError("unique id must be positive"); 490 if (!isUInt<32>(UniqueID) || UniqueID == ~0U) 491 return TokError("unique id is too large"); 492 return false; 493 } 494 495 static bool hasPrefix(StringRef SectionName, StringRef Prefix) { 496 return SectionName.startswith(Prefix) || SectionName == Prefix.drop_back(); 497 } 498 499 bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) { 500 StringRef SectionName; 501 502 if (ParseSectionName(SectionName)) 503 return TokError("expected identifier in directive"); 504 505 StringRef TypeName; 506 int64_t Size = 0; 507 StringRef GroupName; 508 bool IsComdat = false; 509 unsigned Flags = 0; 510 unsigned extraFlags = 0; 511 const MCExpr *Subsection = nullptr; 512 bool UseLastGroup = false; 513 MCSymbolELF *LinkedToSym = nullptr; 514 int64_t UniqueID = ~0; 515 516 // Set the defaults first. 517 if (hasPrefix(SectionName, ".rodata.") || SectionName == ".rodata1") 518 Flags |= ELF::SHF_ALLOC; 519 else if (SectionName == ".fini" || SectionName == ".init" || 520 hasPrefix(SectionName, ".text.")) 521 Flags |= ELF::SHF_ALLOC | ELF::SHF_EXECINSTR; 522 else if (hasPrefix(SectionName, ".data.") || SectionName == ".data1" || 523 hasPrefix(SectionName, ".bss.") || 524 hasPrefix(SectionName, ".init_array.") || 525 hasPrefix(SectionName, ".fini_array.") || 526 hasPrefix(SectionName, ".preinit_array.")) 527 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE; 528 else if (hasPrefix(SectionName, ".tdata.") || 529 hasPrefix(SectionName, ".tbss.")) 530 Flags |= ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::SHF_TLS; 531 532 if (getLexer().is(AsmToken::Comma)) { 533 Lex(); 534 535 if (IsPush && getLexer().isNot(AsmToken::String)) { 536 if (getParser().parseExpression(Subsection)) 537 return true; 538 if (getLexer().isNot(AsmToken::Comma)) 539 goto EndStmt; 540 Lex(); 541 } 542 543 if (getLexer().isNot(AsmToken::String)) { 544 if (!getContext().getAsmInfo()->usesSunStyleELFSectionSwitchSyntax() 545 || getLexer().isNot(AsmToken::Hash)) 546 return TokError("expected string in directive"); 547 extraFlags = parseSunStyleSectionFlags(); 548 } else { 549 StringRef FlagsStr = getTok().getStringContents(); 550 Lex(); 551 extraFlags = parseSectionFlags(FlagsStr, &UseLastGroup); 552 } 553 554 if (extraFlags == -1U) 555 return TokError("unknown flag"); 556 Flags |= extraFlags; 557 558 bool Mergeable = Flags & ELF::SHF_MERGE; 559 bool Group = Flags & ELF::SHF_GROUP; 560 if (Group && UseLastGroup) 561 return TokError("Section cannot specifiy a group name while also acting " 562 "as a member of the last group"); 563 564 if (maybeParseSectionType(TypeName)) 565 return true; 566 567 MCAsmLexer &L = getLexer(); 568 if (TypeName.empty()) { 569 if (Mergeable) 570 return TokError("Mergeable section must specify the type"); 571 if (Group) 572 return TokError("Group section must specify the type"); 573 if (L.isNot(AsmToken::EndOfStatement)) 574 return TokError("unexpected token in directive"); 575 } 576 577 if (Mergeable) 578 if (parseMergeSize(Size)) 579 return true; 580 if (Group) 581 if (parseGroup(GroupName, IsComdat)) 582 return true; 583 if (Flags & ELF::SHF_LINK_ORDER) 584 if (parseLinkedToSym(LinkedToSym)) 585 return true; 586 if (maybeParseUniqueID(UniqueID)) 587 return true; 588 } 589 590 EndStmt: 591 if (getLexer().isNot(AsmToken::EndOfStatement)) 592 return TokError("unexpected token in directive"); 593 Lex(); 594 595 unsigned Type = ELF::SHT_PROGBITS; 596 597 if (TypeName.empty()) { 598 if (SectionName.startswith(".note")) 599 Type = ELF::SHT_NOTE; 600 else if (hasPrefix(SectionName, ".init_array.")) 601 Type = ELF::SHT_INIT_ARRAY; 602 else if (hasPrefix(SectionName, ".bss.")) 603 Type = ELF::SHT_NOBITS; 604 else if (hasPrefix(SectionName, ".tbss.")) 605 Type = ELF::SHT_NOBITS; 606 else if (hasPrefix(SectionName, ".fini_array.")) 607 Type = ELF::SHT_FINI_ARRAY; 608 else if (hasPrefix(SectionName, ".preinit_array.")) 609 Type = ELF::SHT_PREINIT_ARRAY; 610 } else { 611 if (TypeName == "init_array") 612 Type = ELF::SHT_INIT_ARRAY; 613 else if (TypeName == "fini_array") 614 Type = ELF::SHT_FINI_ARRAY; 615 else if (TypeName == "preinit_array") 616 Type = ELF::SHT_PREINIT_ARRAY; 617 else if (TypeName == "nobits") 618 Type = ELF::SHT_NOBITS; 619 else if (TypeName == "progbits") 620 Type = ELF::SHT_PROGBITS; 621 else if (TypeName == "note") 622 Type = ELF::SHT_NOTE; 623 else if (TypeName == "unwind") 624 Type = ELF::SHT_X86_64_UNWIND; 625 else if (TypeName == "llvm_odrtab") 626 Type = ELF::SHT_LLVM_ODRTAB; 627 else if (TypeName == "llvm_linker_options") 628 Type = ELF::SHT_LLVM_LINKER_OPTIONS; 629 else if (TypeName == "llvm_call_graph_profile") 630 Type = ELF::SHT_LLVM_CALL_GRAPH_PROFILE; 631 else if (TypeName == "llvm_dependent_libraries") 632 Type = ELF::SHT_LLVM_DEPENDENT_LIBRARIES; 633 else if (TypeName == "llvm_sympart") 634 Type = ELF::SHT_LLVM_SYMPART; 635 else if (TypeName == "llvm_bb_addr_map") 636 Type = ELF::SHT_LLVM_BB_ADDR_MAP; 637 else if (TypeName.getAsInteger(0, Type)) 638 return TokError("unknown section type"); 639 } 640 641 if (UseLastGroup) { 642 MCSectionSubPair CurrentSection = getStreamer().getCurrentSection(); 643 if (const MCSectionELF *Section = 644 cast_or_null<MCSectionELF>(CurrentSection.first)) 645 if (const MCSymbol *Group = Section->getGroup()) { 646 GroupName = Group->getName(); 647 IsComdat = Section->isComdat(); 648 Flags |= ELF::SHF_GROUP; 649 } 650 } 651 652 MCSectionELF *Section = 653 getContext().getELFSection(SectionName, Type, Flags, Size, GroupName, 654 IsComdat, UniqueID, LinkedToSym); 655 getStreamer().SwitchSection(Section, Subsection); 656 // x86-64 psABI names SHT_X86_64_UNWIND as the canonical type for .eh_frame, 657 // but GNU as emits SHT_PROGBITS .eh_frame for .cfi_* directives. Don't error 658 // for SHT_PROGBITS .eh_frame 659 if (Section->getType() != Type && 660 !(SectionName == ".eh_frame" && Type == ELF::SHT_PROGBITS)) 661 Error(loc, "changed section type for " + SectionName + ", expected: 0x" + 662 utohexstr(Section->getType())); 663 // Check that flags are used consistently. However, the GNU assembler permits 664 // to leave out in subsequent uses of the same sections; for compatibility, 665 // do likewise. 666 if ((extraFlags || Size || !TypeName.empty()) && Section->getFlags() != Flags) 667 Error(loc, "changed section flags for " + SectionName + ", expected: 0x" + 668 utohexstr(Section->getFlags())); 669 if ((extraFlags || Size || !TypeName.empty()) && 670 Section->getEntrySize() != Size) 671 Error(loc, "changed section entsize for " + SectionName + 672 ", expected: " + Twine(Section->getEntrySize())); 673 674 if (getContext().getGenDwarfForAssembly() && 675 (Section->getFlags() & ELF::SHF_ALLOC) && 676 (Section->getFlags() & ELF::SHF_EXECINSTR)) { 677 bool InsertResult = getContext().addGenDwarfSection(Section); 678 if (InsertResult) { 679 if (getContext().getDwarfVersion() <= 2) 680 Warning(loc, "DWARF2 only supports one section per compilation unit"); 681 682 if (!Section->getBeginSymbol()) { 683 MCSymbol *SectionStartSymbol = getContext().createTempSymbol(); 684 getStreamer().emitLabel(SectionStartSymbol); 685 Section->setBeginSymbol(SectionStartSymbol); 686 } 687 } 688 } 689 690 return false; 691 } 692 693 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) { 694 MCSectionSubPair PreviousSection = getStreamer().getPreviousSection(); 695 if (PreviousSection.first == nullptr) 696 return TokError(".previous without corresponding .section"); 697 getStreamer().SwitchSection(PreviousSection.first, PreviousSection.second); 698 699 return false; 700 } 701 702 static MCSymbolAttr MCAttrForString(StringRef Type) { 703 return StringSwitch<MCSymbolAttr>(Type) 704 .Cases("STT_FUNC", "function", MCSA_ELF_TypeFunction) 705 .Cases("STT_OBJECT", "object", MCSA_ELF_TypeObject) 706 .Cases("STT_TLS", "tls_object", MCSA_ELF_TypeTLS) 707 .Cases("STT_COMMON", "common", MCSA_ELF_TypeCommon) 708 .Cases("STT_NOTYPE", "notype", MCSA_ELF_TypeNoType) 709 .Cases("STT_GNU_IFUNC", "gnu_indirect_function", 710 MCSA_ELF_TypeIndFunction) 711 .Case("gnu_unique_object", MCSA_ELF_TypeGnuUniqueObject) 712 .Default(MCSA_Invalid); 713 } 714 715 /// ParseDirectiveELFType 716 /// ::= .type identifier , STT_<TYPE_IN_UPPER_CASE> 717 /// ::= .type identifier , #attribute 718 /// ::= .type identifier , @attribute 719 /// ::= .type identifier , %attribute 720 /// ::= .type identifier , "attribute" 721 bool ELFAsmParser::ParseDirectiveType(StringRef, SMLoc) { 722 StringRef Name; 723 if (getParser().parseIdentifier(Name)) 724 return TokError("expected identifier in directive"); 725 726 // Handle the identifier as the key symbol. 727 MCSymbol *Sym = getContext().getOrCreateSymbol(Name); 728 729 // NOTE the comma is optional in all cases. It is only documented as being 730 // optional in the first case, however, GAS will silently treat the comma as 731 // optional in all cases. Furthermore, although the documentation states that 732 // the first form only accepts STT_<TYPE_IN_UPPER_CASE>, in reality, GAS 733 // accepts both the upper case name as well as the lower case aliases. 734 if (getLexer().is(AsmToken::Comma)) 735 Lex(); 736 737 if (getLexer().isNot(AsmToken::Identifier) && 738 getLexer().isNot(AsmToken::Hash) && 739 getLexer().isNot(AsmToken::Percent) && 740 getLexer().isNot(AsmToken::String)) { 741 if (!getLexer().getAllowAtInIdentifier()) 742 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', " 743 "'%<type>' or \"<type>\""); 744 else if (getLexer().isNot(AsmToken::At)) 745 return TokError("expected STT_<TYPE_IN_UPPER_CASE>, '#<type>', '@<type>', " 746 "'%<type>' or \"<type>\""); 747 } 748 749 if (getLexer().isNot(AsmToken::String) && 750 getLexer().isNot(AsmToken::Identifier)) 751 Lex(); 752 753 SMLoc TypeLoc = getLexer().getLoc(); 754 755 StringRef Type; 756 if (getParser().parseIdentifier(Type)) 757 return TokError("expected symbol type in directive"); 758 759 MCSymbolAttr Attr = MCAttrForString(Type); 760 if (Attr == MCSA_Invalid) 761 return Error(TypeLoc, "unsupported attribute in '.type' directive"); 762 763 if (getLexer().isNot(AsmToken::EndOfStatement)) 764 return TokError("unexpected token in '.type' directive"); 765 Lex(); 766 767 getStreamer().emitSymbolAttribute(Sym, Attr); 768 769 return false; 770 } 771 772 /// ParseDirectiveIdent 773 /// ::= .ident string 774 bool ELFAsmParser::ParseDirectiveIdent(StringRef, SMLoc) { 775 if (getLexer().isNot(AsmToken::String)) 776 return TokError("unexpected token in '.ident' directive"); 777 778 StringRef Data = getTok().getIdentifier(); 779 780 Lex(); 781 782 if (getLexer().isNot(AsmToken::EndOfStatement)) 783 return TokError("unexpected token in '.ident' directive"); 784 Lex(); 785 786 getStreamer().emitIdent(Data); 787 return false; 788 } 789 790 /// ParseDirectiveSymver 791 /// ::= .symver foo, bar2@zed 792 bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) { 793 StringRef OriginalName, Name, Action; 794 if (getParser().parseIdentifier(OriginalName)) 795 return TokError("expected identifier in directive"); 796 797 if (getLexer().isNot(AsmToken::Comma)) 798 return TokError("expected a comma"); 799 800 // ARM assembly uses @ for a comment... 801 // except when parsing the second parameter of the .symver directive. 802 // Force the next symbol to allow @ in the identifier, which is 803 // required for this directive and then reset it to its initial state. 804 const bool AllowAtInIdentifier = getLexer().getAllowAtInIdentifier(); 805 getLexer().setAllowAtInIdentifier(true); 806 Lex(); 807 getLexer().setAllowAtInIdentifier(AllowAtInIdentifier); 808 809 if (getParser().parseIdentifier(Name)) 810 return TokError("expected identifier in directive"); 811 812 if (Name.find('@') == StringRef::npos) 813 return TokError("expected a '@' in the name"); 814 bool KeepOriginalSym = !Name.contains("@@@"); 815 if (parseOptionalToken(AsmToken::Comma)) { 816 if (getParser().parseIdentifier(Action) || Action != "remove") 817 return TokError("expected 'remove'"); 818 KeepOriginalSym = false; 819 } 820 (void)parseOptionalToken(AsmToken::EndOfStatement); 821 822 getStreamer().emitELFSymverDirective( 823 getContext().getOrCreateSymbol(OriginalName), Name, KeepOriginalSym); 824 return false; 825 } 826 827 /// ParseDirectiveVersion 828 /// ::= .version string 829 bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) { 830 if (getLexer().isNot(AsmToken::String)) 831 return TokError("unexpected token in '.version' directive"); 832 833 StringRef Data = getTok().getIdentifier(); 834 835 Lex(); 836 837 MCSection *Note = getContext().getELFSection(".note", ELF::SHT_NOTE, 0); 838 839 getStreamer().PushSection(); 840 getStreamer().SwitchSection(Note); 841 getStreamer().emitInt32(Data.size() + 1); // namesz 842 getStreamer().emitInt32(0); // descsz = 0 (no description). 843 getStreamer().emitInt32(1); // type = NT_VERSION 844 getStreamer().emitBytes(Data); // name 845 getStreamer().emitInt8(0); // NUL 846 getStreamer().emitValueToAlignment(4); 847 getStreamer().PopSection(); 848 return false; 849 } 850 851 /// ParseDirectiveWeakref 852 /// ::= .weakref foo, bar 853 bool ELFAsmParser::ParseDirectiveWeakref(StringRef, SMLoc) { 854 // FIXME: Share code with the other alias building directives. 855 856 StringRef AliasName; 857 if (getParser().parseIdentifier(AliasName)) 858 return TokError("expected identifier in directive"); 859 860 if (getLexer().isNot(AsmToken::Comma)) 861 return TokError("expected a comma"); 862 863 Lex(); 864 865 StringRef Name; 866 if (getParser().parseIdentifier(Name)) 867 return TokError("expected identifier in directive"); 868 869 MCSymbol *Alias = getContext().getOrCreateSymbol(AliasName); 870 871 MCSymbol *Sym = getContext().getOrCreateSymbol(Name); 872 873 getStreamer().emitWeakReference(Alias, Sym); 874 return false; 875 } 876 877 bool ELFAsmParser::ParseDirectiveSubsection(StringRef, SMLoc) { 878 const MCExpr *Subsection = nullptr; 879 if (getLexer().isNot(AsmToken::EndOfStatement)) { 880 if (getParser().parseExpression(Subsection)) 881 return true; 882 } 883 884 if (getLexer().isNot(AsmToken::EndOfStatement)) 885 return TokError("unexpected token in directive"); 886 887 Lex(); 888 889 getStreamer().SubSection(Subsection); 890 return false; 891 } 892 893 bool ELFAsmParser::ParseDirectiveCGProfile(StringRef S, SMLoc Loc) { 894 return MCAsmParserExtension::ParseDirectiveCGProfile(S, Loc); 895 } 896 897 namespace llvm { 898 899 MCAsmParserExtension *createELFAsmParser() { 900 return new ELFAsmParser; 901 } 902 903 } // end namespace llvm 904