1 //===- LDFileFormat.h -----------------------------------------------------===// 2 // 3 // The MCLinker Project 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 #ifndef MCLD_LD_ELFFILEFORMAT_H_ 10 #define MCLD_LD_ELFFILEFORMAT_H_ 11 #include "mcld/LD/LDFileFormat.h" 12 #include "mcld/LD/LDSection.h" 13 14 namespace mcld { 15 16 class ObjectBuilder; 17 18 /** \class ELFFileFormat 19 * \brief ELFFileFormat describes the common file formats in ELF. 20 * LDFileFormats control the formats of the output file. 21 * 22 * @ref "Object Files," Ch. 4, in System V Application Binary Interface, 23 * Fourth Edition. 24 * 25 * @ref "Object Format," Ch. 10, in ISO/IEC 23360 Part 1:2010(E), Linux 26 * Standard Base Core Specification 4.1. 27 */ 28 class ELFFileFormat : public LDFileFormat { 29 private: 30 /// initObjectFormat - initialize sections that are dependent on object 31 /// formats. (executable, shared objects or relocatable objects). 32 virtual void initObjectFormat(ObjectBuilder& pBuilder, 33 unsigned int pBitClass) = 0; 34 35 public: 36 ELFFileFormat(); 37 38 void initStdSections(ObjectBuilder& pBuilder, unsigned int pBitClass); 39 40 // ----- capacity ----- // 41 /// @ref Special Sections, Ch. 4.17, System V ABI, 4th edition. 42 bool hasNULLSection() const { 43 return (f_pNULLSection != NULL) && (f_pNULLSection->size() != 0); 44 } 45 46 bool hasGOT() const { return (f_pGOT != NULL) && (f_pGOT->size() != 0); } 47 48 bool hasPLT() const { return (f_pPLT != NULL) && (f_pPLT->size() != 0); } 49 50 bool hasRelDyn() const { 51 return (f_pRelDyn != NULL) && (f_pRelDyn->size() != 0); 52 } 53 54 bool hasRelPlt() const { 55 return (f_pRelPlt != NULL) && (f_pRelPlt->size() != 0); 56 } 57 58 bool hasRelaDyn() const { 59 return (f_pRelaDyn != NULL) && (f_pRelaDyn->size() != 0); 60 } 61 62 bool hasRelaPlt() const { 63 return (f_pRelaPlt != NULL) && (f_pRelaPlt->size() != 0); 64 } 65 66 /// @ref 10.3.1.1, ISO/IEC 23360, Part 1:2010(E), p. 21. 67 bool hasComment() const { 68 return (f_pComment != NULL) && (f_pComment->size() != 0); 69 } 70 71 bool hasData1() const { 72 return (f_pData1 != NULL) && (f_pData1->size() != 0); 73 } 74 75 bool hasDebug() const { 76 return (f_pDebug != NULL) && (f_pDebug->size() != 0); 77 } 78 79 bool hasDynamic() const { 80 return (f_pDynamic != NULL) && (f_pDynamic->size() != 0); 81 } 82 83 bool hasDynStrTab() const { 84 return (f_pDynStrTab != NULL) && (f_pDynStrTab->size() != 0); 85 } 86 87 bool hasDynSymTab() const { 88 return (f_pDynSymTab != NULL) && (f_pDynSymTab->size() != 0); 89 } 90 91 bool hasFini() const { return (f_pFini != NULL) && (f_pFini->size() != 0); } 92 93 bool hasFiniArray() const { 94 return (f_pFiniArray != NULL) && (f_pFiniArray->size() != 0); 95 } 96 97 bool hasHashTab() const { 98 return (f_pHashTab != NULL) && (f_pHashTab->size() != 0); 99 } 100 101 bool hasInit() const { return (f_pInit != NULL) && (f_pInit->size() != 0); } 102 103 bool hasInitArray() const { 104 return (f_pInitArray != NULL) && (f_pInitArray->size() != 0); 105 } 106 107 bool hasInterp() const { 108 return (f_pInterp != NULL) && (f_pInterp->size() != 0); 109 } 110 111 bool hasLine() const { return (f_pLine != NULL) && (f_pLine->size() != 0); } 112 113 bool hasNote() const { return (f_pNote != NULL) && (f_pNote->size() != 0); } 114 115 bool hasPreInitArray() const { 116 return (f_pPreInitArray != NULL) && (f_pPreInitArray->size() != 0); 117 } 118 119 bool hasROData1() const { 120 return (f_pROData1 != NULL) && (f_pROData1->size() != 0); 121 } 122 123 bool hasShStrTab() const { 124 return (f_pShStrTab != NULL) && (f_pShStrTab->size() != 0); 125 } 126 127 bool hasStrTab() const { 128 return (f_pStrTab != NULL) && (f_pStrTab->size() != 0); 129 } 130 131 bool hasSymTab() const { 132 return (f_pSymTab != NULL) && (f_pSymTab->size() != 0); 133 } 134 135 bool hasTBSS() const { return (f_pTBSS != NULL) && (f_pTBSS->size() != 0); } 136 137 bool hasTData() const { 138 return (f_pTData != NULL) && (f_pTData->size() != 0); 139 } 140 141 /// @ref 10.3.1.2, ISO/IEC 23360, Part 1:2010(E), p. 24. 142 bool hasCtors() const { 143 return (f_pCtors != NULL) && (f_pCtors->size() != 0); 144 } 145 146 bool hasDataRelRo() const { 147 return (f_pDataRelRo != NULL) && (f_pDataRelRo->size() != 0); 148 } 149 150 bool hasDtors() const { 151 return (f_pDtors != NULL) && (f_pDtors->size() != 0); 152 } 153 154 bool hasEhFrame() const { 155 return (f_pEhFrame != NULL) && (f_pEhFrame->size() != 0); 156 } 157 158 bool hasEhFrameHdr() const { 159 return (f_pEhFrameHdr != NULL) && (f_pEhFrameHdr->size() != 0); 160 } 161 162 bool hasGCCExceptTable() const { 163 return (f_pGCCExceptTable != NULL) && (f_pGCCExceptTable->size() != 0); 164 } 165 166 bool hasGNUVersion() const { 167 return (f_pGNUVersion != NULL) && (f_pGNUVersion->size() != 0); 168 } 169 170 bool hasGNUVersionD() const { 171 return (f_pGNUVersionD != NULL) && (f_pGNUVersionD->size() != 0); 172 } 173 174 bool hasGNUVersionR() const { 175 return (f_pGNUVersionR != NULL) && (f_pGNUVersionR->size() != 0); 176 } 177 178 bool hasGOTPLT() const { 179 return (f_pGOTPLT != NULL) && (f_pGOTPLT->size() != 0); 180 } 181 182 bool hasJCR() const { return (f_pJCR != NULL) && (f_pJCR->size() != 0); } 183 184 bool hasNoteABITag() const { 185 return (f_pNoteABITag != NULL) && (f_pNoteABITag->size() != 0); 186 } 187 188 bool hasStab() const { return (f_pStab != NULL) && (f_pStab->size() != 0); } 189 190 bool hasStabStr() const { 191 return (f_pStabStr != NULL) && (f_pStabStr->size() != 0); 192 } 193 194 bool hasStack() const { 195 return (f_pStack != NULL) && (f_pStack->size() != 0); 196 } 197 198 bool hasStackNote() const { return (f_pStackNote != NULL); } 199 200 bool hasDataRelRoLocal() const { 201 return (f_pDataRelRoLocal != NULL) && (f_pDataRelRoLocal->size() != 0); 202 } 203 204 bool hasGNUHashTab() const { 205 return (f_pGNUHashTab != NULL) && (f_pGNUHashTab->size() != 0); 206 } 207 208 // ----- access functions ----- // 209 /// @ref Special Sections, Ch. 4.17, System V ABI, 4th edition. 210 LDSection& getNULLSection() { 211 assert(f_pNULLSection != NULL); 212 return *f_pNULLSection; 213 } 214 215 const LDSection& getNULLSection() const { 216 assert(f_pNULLSection != NULL); 217 return *f_pNULLSection; 218 } 219 220 LDSection& getGOT() { 221 assert(f_pGOT != NULL); 222 return *f_pGOT; 223 } 224 225 const LDSection& getGOT() const { 226 assert(f_pGOT != NULL); 227 return *f_pGOT; 228 } 229 230 LDSection& getPLT() { 231 assert(f_pPLT != NULL); 232 return *f_pPLT; 233 } 234 235 const LDSection& getPLT() const { 236 assert(f_pPLT != NULL); 237 return *f_pPLT; 238 } 239 240 LDSection& getRelDyn() { 241 assert(f_pRelDyn != NULL); 242 return *f_pRelDyn; 243 } 244 245 const LDSection& getRelDyn() const { 246 assert(f_pRelDyn != NULL); 247 return *f_pRelDyn; 248 } 249 250 LDSection& getRelPlt() { 251 assert(f_pRelPlt != NULL); 252 return *f_pRelPlt; 253 } 254 255 const LDSection& getRelPlt() const { 256 assert(f_pRelPlt != NULL); 257 return *f_pRelPlt; 258 } 259 260 LDSection& getRelaDyn() { 261 assert(f_pRelaDyn != NULL); 262 return *f_pRelaDyn; 263 } 264 265 const LDSection& getRelaDyn() const { 266 assert(f_pRelaDyn != NULL); 267 return *f_pRelaDyn; 268 } 269 270 LDSection& getRelaPlt() { 271 assert(f_pRelaPlt != NULL); 272 return *f_pRelaPlt; 273 } 274 275 const LDSection& getRelaPlt() const { 276 assert(f_pRelaPlt != NULL); 277 return *f_pRelaPlt; 278 } 279 280 LDSection& getComment() { 281 assert(f_pComment != NULL); 282 return *f_pComment; 283 } 284 285 /// @ref 10.3.1.1, ISO/IEC 23360, Part 1:2010(E), p. 21. 286 const LDSection& getComment() const { 287 assert(f_pComment != NULL); 288 return *f_pComment; 289 } 290 291 LDSection& getData1() { 292 assert(f_pData1 != NULL); 293 return *f_pData1; 294 } 295 296 const LDSection& getData1() const { 297 assert(f_pData1 != NULL); 298 return *f_pData1; 299 } 300 301 LDSection& getDebug() { 302 assert(f_pDebug != NULL); 303 return *f_pDebug; 304 } 305 306 const LDSection& getDebug() const { 307 assert(f_pDebug != NULL); 308 return *f_pDebug; 309 } 310 311 LDSection& getDynamic() { 312 assert(f_pDynamic != NULL); 313 return *f_pDynamic; 314 } 315 316 const LDSection& getDynamic() const { 317 assert(f_pDynamic != NULL); 318 return *f_pDynamic; 319 } 320 321 LDSection& getDynStrTab() { 322 assert(f_pDynStrTab != NULL); 323 return *f_pDynStrTab; 324 } 325 326 const LDSection& getDynStrTab() const { 327 assert(f_pDynStrTab != NULL); 328 return *f_pDynStrTab; 329 } 330 331 LDSection& getDynSymTab() { 332 assert(f_pDynSymTab != NULL); 333 return *f_pDynSymTab; 334 } 335 336 const LDSection& getDynSymTab() const { 337 assert(f_pDynSymTab != NULL); 338 return *f_pDynSymTab; 339 } 340 341 LDSection& getFini() { 342 assert(f_pFini != NULL); 343 return *f_pFini; 344 } 345 346 const LDSection& getFini() const { 347 assert(f_pFini != NULL); 348 return *f_pFini; 349 } 350 351 LDSection& getFiniArray() { 352 assert(f_pFiniArray != NULL); 353 return *f_pFiniArray; 354 } 355 356 const LDSection& getFiniArray() const { 357 assert(f_pFiniArray != NULL); 358 return *f_pFiniArray; 359 } 360 361 LDSection& getHashTab() { 362 assert(f_pHashTab != NULL); 363 return *f_pHashTab; 364 } 365 366 const LDSection& getHashTab() const { 367 assert(f_pHashTab != NULL); 368 return *f_pHashTab; 369 } 370 371 LDSection& getInit() { 372 assert(f_pInit != NULL); 373 return *f_pInit; 374 } 375 376 const LDSection& getInit() const { 377 assert(f_pInit != NULL); 378 return *f_pInit; 379 } 380 381 LDSection& getInitArray() { 382 assert(f_pInitArray != NULL); 383 return *f_pInitArray; 384 } 385 386 const LDSection& getInitArray() const { 387 assert(f_pInitArray != NULL); 388 return *f_pInitArray; 389 } 390 391 LDSection& getInterp() { 392 assert(f_pInterp != NULL); 393 return *f_pInterp; 394 } 395 396 const LDSection& getInterp() const { 397 assert(f_pInterp != NULL); 398 return *f_pInterp; 399 } 400 401 LDSection& getLine() { 402 assert(f_pLine != NULL); 403 return *f_pLine; 404 } 405 406 const LDSection& getLine() const { 407 assert(f_pLine != NULL); 408 return *f_pLine; 409 } 410 411 LDSection& getNote() { 412 assert(f_pNote != NULL); 413 return *f_pNote; 414 } 415 416 const LDSection& getNote() const { 417 assert(f_pNote != NULL); 418 return *f_pNote; 419 } 420 421 LDSection& getPreInitArray() { 422 assert(f_pPreInitArray != NULL); 423 return *f_pPreInitArray; 424 } 425 426 const LDSection& getPreInitArray() const { 427 assert(f_pPreInitArray != NULL); 428 return *f_pPreInitArray; 429 } 430 431 LDSection& getROData1() { 432 assert(f_pROData1 != NULL); 433 return *f_pROData1; 434 } 435 436 const LDSection& getROData1() const { 437 assert(f_pROData1 != NULL); 438 return *f_pROData1; 439 } 440 441 LDSection& getShStrTab() { 442 assert(f_pShStrTab != NULL); 443 return *f_pShStrTab; 444 } 445 446 const LDSection& getShStrTab() const { 447 assert(f_pShStrTab != NULL); 448 return *f_pShStrTab; 449 } 450 451 LDSection& getStrTab() { 452 assert(f_pStrTab != NULL); 453 return *f_pStrTab; 454 } 455 456 const LDSection& getStrTab() const { 457 assert(f_pStrTab != NULL); 458 return *f_pStrTab; 459 } 460 461 LDSection& getSymTab() { 462 assert(f_pSymTab != NULL); 463 return *f_pSymTab; 464 } 465 466 const LDSection& getSymTab() const { 467 assert(f_pSymTab != NULL); 468 return *f_pSymTab; 469 } 470 471 LDSection& getTBSS() { 472 assert(f_pTBSS != NULL); 473 return *f_pTBSS; 474 } 475 476 const LDSection& getTBSS() const { 477 assert(f_pTBSS != NULL); 478 return *f_pTBSS; 479 } 480 481 LDSection& getTData() { 482 assert(f_pTData != NULL); 483 return *f_pTData; 484 } 485 486 const LDSection& getTData() const { 487 assert(f_pTData != NULL); 488 return *f_pTData; 489 } 490 491 /// @ref 10.3.1.2, ISO/IEC 23360, Part 1:2010(E), p. 24. 492 LDSection& getCtors() { 493 assert(f_pCtors != NULL); 494 return *f_pCtors; 495 } 496 497 const LDSection& getCtors() const { 498 assert(f_pCtors != NULL); 499 return *f_pCtors; 500 } 501 502 LDSection& getDataRelRo() { 503 assert(f_pDataRelRo != NULL); 504 return *f_pDataRelRo; 505 } 506 507 const LDSection& getDataRelRo() const { 508 assert(f_pDataRelRo != NULL); 509 return *f_pDataRelRo; 510 } 511 512 LDSection& getDtors() { 513 assert(f_pDtors != NULL); 514 return *f_pDtors; 515 } 516 517 const LDSection& getDtors() const { 518 assert(f_pDtors != NULL); 519 return *f_pDtors; 520 } 521 522 LDSection& getEhFrame() { 523 assert(f_pEhFrame != NULL); 524 return *f_pEhFrame; 525 } 526 527 const LDSection& getEhFrame() const { 528 assert(f_pEhFrame != NULL); 529 return *f_pEhFrame; 530 } 531 532 LDSection& getEhFrameHdr() { 533 assert(f_pEhFrameHdr != NULL); 534 return *f_pEhFrameHdr; 535 } 536 537 const LDSection& getEhFrameHdr() const { 538 assert(f_pEhFrameHdr != NULL); 539 return *f_pEhFrameHdr; 540 } 541 542 LDSection& getGCCExceptTable() { 543 assert(f_pGCCExceptTable != NULL); 544 return *f_pGCCExceptTable; 545 } 546 547 const LDSection& getGCCExceptTable() const { 548 assert(f_pGCCExceptTable != NULL); 549 return *f_pGCCExceptTable; 550 } 551 552 LDSection& getGNUVersion() { 553 assert(f_pGNUVersion != NULL); 554 return *f_pGNUVersion; 555 } 556 557 const LDSection& getGNUVersion() const { 558 assert(f_pGNUVersion != NULL); 559 return *f_pGNUVersion; 560 } 561 562 LDSection& getGNUVersionD() { 563 assert(f_pGNUVersionD != NULL); 564 return *f_pGNUVersionD; 565 } 566 567 const LDSection& getGNUVersionD() const { 568 assert(f_pGNUVersionD != NULL); 569 return *f_pGNUVersionD; 570 } 571 572 LDSection& getGNUVersionR() { 573 assert(f_pGNUVersionR != NULL); 574 return *f_pGNUVersionR; 575 } 576 577 const LDSection& getGNUVersionR() const { 578 assert(f_pGNUVersionR != NULL); 579 return *f_pGNUVersionR; 580 } 581 582 LDSection& getGOTPLT() { 583 assert(f_pGOTPLT != NULL); 584 return *f_pGOTPLT; 585 } 586 587 const LDSection& getGOTPLT() const { 588 assert(f_pGOTPLT != NULL); 589 return *f_pGOTPLT; 590 } 591 592 LDSection& getJCR() { 593 assert(f_pJCR != NULL); 594 return *f_pJCR; 595 } 596 597 const LDSection& getJCR() const { 598 assert(f_pJCR != NULL); 599 return *f_pJCR; 600 } 601 602 LDSection& getNoteABITag() { 603 assert(f_pNoteABITag != NULL); 604 return *f_pNoteABITag; 605 } 606 607 const LDSection& getNoteABITag() const { 608 assert(f_pNoteABITag != NULL); 609 return *f_pNoteABITag; 610 } 611 612 LDSection& getStab() { 613 assert(f_pStab != NULL); 614 return *f_pStab; 615 } 616 617 const LDSection& getStab() const { 618 assert(f_pStab != NULL); 619 return *f_pStab; 620 } 621 622 LDSection& getStabStr() { 623 assert(f_pStabStr != NULL); 624 return *f_pStabStr; 625 } 626 627 const LDSection& getStabStr() const { 628 assert(f_pStabStr != NULL); 629 return *f_pStabStr; 630 } 631 632 LDSection& getStack() { 633 assert(f_pStack != NULL); 634 return *f_pStack; 635 } 636 637 const LDSection& getStack() const { 638 assert(f_pStack != NULL); 639 return *f_pStack; 640 } 641 642 LDSection& getStackNote() { 643 assert(f_pStackNote != NULL); 644 return *f_pStackNote; 645 } 646 647 const LDSection& getStackNote() const { 648 assert(f_pStackNote != NULL); 649 return *f_pStackNote; 650 } 651 652 LDSection& getDataRelRoLocal() { 653 assert(f_pDataRelRoLocal != NULL); 654 return *f_pDataRelRoLocal; 655 } 656 657 const LDSection& getDataRelRoLocal() const { 658 assert(f_pDataRelRoLocal != NULL); 659 return *f_pDataRelRoLocal; 660 } 661 662 LDSection& getGNUHashTab() { 663 assert(f_pGNUHashTab != NULL); 664 return *f_pGNUHashTab; 665 } 666 667 const LDSection& getGNUHashTab() const { 668 assert(f_pGNUHashTab != NULL); 669 return *f_pGNUHashTab; 670 } 671 672 protected: 673 // variable name : ELF 674 /// @ref Special Sections, Ch. 4.17, System V ABI, 4th edition. 675 LDSection* f_pNULLSection; 676 LDSection* f_pGOT; // .got 677 LDSection* f_pPLT; // .plt 678 LDSection* f_pRelDyn; // .rel.dyn 679 LDSection* f_pRelPlt; // .rel.plt 680 LDSection* f_pRelaDyn; // .rela.dyn 681 LDSection* f_pRelaPlt; // .rela.plt 682 683 /// @ref 10.3.1.1, ISO/IEC 23360, Part 1:2010(E), p. 21. 684 LDSection* f_pComment; // .comment 685 LDSection* f_pData1; // .data1 686 LDSection* f_pDebug; // .debug 687 LDSection* f_pDynamic; // .dynamic 688 LDSection* f_pDynStrTab; // .dynstr 689 LDSection* f_pDynSymTab; // .dynsym 690 LDSection* f_pFini; // .fini 691 LDSection* f_pFiniArray; // .fini_array 692 LDSection* f_pHashTab; // .hash 693 LDSection* f_pInit; // .init 694 LDSection* f_pInitArray; // .init_array 695 LDSection* f_pInterp; // .interp 696 LDSection* f_pLine; // .line 697 LDSection* f_pNote; // .note 698 LDSection* f_pPreInitArray; // .preinit_array 699 LDSection* f_pROData1; // .rodata1 700 LDSection* f_pShStrTab; // .shstrtab 701 LDSection* f_pStrTab; // .strtab 702 LDSection* f_pSymTab; // .symtab 703 LDSection* f_pTBSS; // .tbss 704 LDSection* f_pTData; // .tdata 705 706 /// @ref 10.3.1.2, ISO/IEC 23360, Part 1:2010(E), p. 24. 707 LDSection* f_pCtors; // .ctors 708 LDSection* f_pDataRelRo; // .data.rel.ro 709 LDSection* f_pDtors; // .dtors 710 LDSection* f_pEhFrame; // .eh_frame 711 LDSection* f_pEhFrameHdr; // .eh_frame_hdr 712 LDSection* f_pGCCExceptTable; // .gcc_except_table 713 LDSection* f_pGNUVersion; // .gnu.version 714 LDSection* f_pGNUVersionD; // .gnu.version_d 715 LDSection* f_pGNUVersionR; // .gnu.version_r 716 LDSection* f_pGOTPLT; // .got.plt 717 LDSection* f_pJCR; // .jcr 718 LDSection* f_pNoteABITag; // .note.ABI-tag 719 LDSection* f_pStab; // .stab 720 LDSection* f_pStabStr; // .stabstr 721 722 /// practical 723 LDSection* f_pStack; // .stack 724 LDSection* f_pStackNote; // .note.GNU-stack 725 LDSection* f_pDataRelRoLocal; // .data.rel.ro.local 726 LDSection* f_pGNUHashTab; // .gnu.hash 727 }; 728 729 } // namespace mcld 730 731 #endif // MCLD_LD_ELFFILEFORMAT_H_ 732