OLD | NEW |
1 //===- subzero/src/IceELFObjectWriter.cpp - ELF object file writer --------===// | 1 //===- subzero/src/IceELFObjectWriter.cpp - ELF object file writer --------===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This file defines the writer for ELF relocatable object files. | 10 // This file defines the writer for ELF relocatable object files. |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 | 483 |
484 template <typename ConstType> void ELFObjectWriter::writeConstantPool(Type Ty) { | 484 template <typename ConstType> void ELFObjectWriter::writeConstantPool(Type Ty) { |
485 ConstantList Pool = Ctx.getConstantPool(Ty); | 485 ConstantList Pool = Ctx.getConstantPool(Ty); |
486 if (Pool.empty()) { | 486 if (Pool.empty()) { |
487 return; | 487 return; |
488 } | 488 } |
489 SizeT Align = typeAlignInBytes(Ty); | 489 SizeT Align = typeAlignInBytes(Ty); |
490 size_t EntSize = typeWidthInBytes(Ty); | 490 size_t EntSize = typeWidthInBytes(Ty); |
491 char Buf[20]; | 491 char Buf[20]; |
492 SizeT WriteAmt = std::min(EntSize, llvm::array_lengthof(Buf)); | 492 SizeT WriteAmt = std::min(EntSize, llvm::array_lengthof(Buf)); |
| 493 // Check that we write the full PrimType. |
493 assert(WriteAmt == EntSize); | 494 assert(WriteAmt == EntSize); |
494 // Assume that writing WriteAmt bytes at a time allows us to avoid aligning | 495 // Assume that writing WriteAmt bytes at a time allows us to avoid aligning |
495 // between entries. | 496 // between entries. |
496 assert(WriteAmt % Align == 0); | 497 assert(WriteAmt % Align == 0); |
497 // Check that we write the full PrimType. | |
498 assert(WriteAmt == sizeof(typename ConstType::PrimType)); | |
499 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_MERGE; | 498 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_MERGE; |
500 std::string SecBuffer; | 499 std::string SecBuffer; |
501 llvm::raw_string_ostream SecStrBuf(SecBuffer); | 500 llvm::raw_string_ostream SecStrBuf(SecBuffer); |
502 SecStrBuf << ".rodata.cst" << WriteAmt; | 501 SecStrBuf << ".rodata.cst" << WriteAmt; |
503 ELFDataSection *Section = createSection<ELFDataSection>( | 502 ELFDataSection *Section = createSection<ELFDataSection>( |
504 SecStrBuf.str(), SHT_PROGBITS, ShFlags, Align, WriteAmt); | 503 SecStrBuf.str(), SHT_PROGBITS, ShFlags, Align, WriteAmt); |
505 RODataSections.push_back(Section); | 504 RODataSections.push_back(Section); |
506 SizeT OffsetInSection = 0; | 505 SizeT OffsetInSection = 0; |
507 // The symbol table entry doesn't need to know the defined symbol's | 506 // The symbol table entry doesn't need to know the defined symbol's |
508 // size since this is in a section with a fixed Entry Size. | 507 // size since this is in a section with a fixed Entry Size. |
509 const SizeT SymbolSize = 0; | 508 const SizeT SymbolSize = 0; |
510 Section->setFileOffset(alignFileOffset(Align)); | 509 Section->setFileOffset(alignFileOffset(Align)); |
511 | 510 |
512 // Write the data. | 511 // Write the data. |
513 for (Constant *C : Pool) { | 512 for (Constant *C : Pool) { |
| 513 if (!C->getShouldBePooled()) |
| 514 continue; |
514 auto Const = llvm::cast<ConstType>(C); | 515 auto Const = llvm::cast<ConstType>(C); |
515 std::string SymBuffer; | 516 std::string SymBuffer; |
516 llvm::raw_string_ostream SymStrBuf(SymBuffer); | 517 llvm::raw_string_ostream SymStrBuf(SymBuffer); |
517 Const->emitPoolLabel(SymStrBuf); | 518 Const->emitPoolLabel(SymStrBuf); |
518 std::string &SymName = SymStrBuf.str(); | 519 std::string &SymName = SymStrBuf.str(); |
519 SymTab->createDefinedSym(SymName, STT_NOTYPE, STB_LOCAL, Section, | 520 SymTab->createDefinedSym(SymName, STT_NOTYPE, STB_LOCAL, Section, |
520 OffsetInSection, SymbolSize); | 521 OffsetInSection, SymbolSize); |
521 StrTab->add(SymName); | 522 StrTab->add(SymName); |
522 typename ConstType::PrimType Value = Const->getValue(); | 523 typename ConstType::PrimType Value = Const->getValue(); |
523 memcpy(Buf, &Value, WriteAmt); | 524 memcpy(Buf, &Value, WriteAmt); |
524 Str.writeBytes(llvm::StringRef(Buf, WriteAmt)); | 525 Str.writeBytes(llvm::StringRef(Buf, WriteAmt)); |
525 OffsetInSection += WriteAmt; | 526 OffsetInSection += WriteAmt; |
526 } | 527 } |
527 Section->setSize(OffsetInSection); | 528 Section->setSize(OffsetInSection); |
528 } | 529 } |
529 | 530 |
530 // Instantiate known needed versions of the template, since we are | 531 // Instantiate known needed versions of the template, since we are |
531 // defining the function in the .cpp file instead of the .h file. | 532 // defining the function in the .cpp file instead of the .h file. |
532 // We may need to instantiate constant pools for integers as well | 533 // We may need to instantiate constant pools for integers as well |
533 // if we do constant-pooling of large integers to remove them | 534 // if we do constant-pooling of large integers to remove them |
534 // from the instruction stream (fewer bytes controlled by an attacker). | 535 // from the instruction stream (fewer bytes controlled by an attacker). |
535 template void ELFObjectWriter::writeConstantPool<ConstantFloat>(Type Ty); | 536 template void ELFObjectWriter::writeConstantPool<ConstantFloat>(Type Ty); |
536 | 537 |
537 template void ELFObjectWriter::writeConstantPool<ConstantDouble>(Type Ty); | 538 template void ELFObjectWriter::writeConstantPool<ConstantDouble>(Type Ty); |
538 | 539 |
| 540 template void ELFObjectWriter::writeConstantPool<ConstantInteger32>(Type Ty); |
| 541 |
539 void ELFObjectWriter::writeAllRelocationSections() { | 542 void ELFObjectWriter::writeAllRelocationSections() { |
540 writeRelocationSections(RelTextSections); | 543 writeRelocationSections(RelTextSections); |
541 writeRelocationSections(RelDataSections); | 544 writeRelocationSections(RelDataSections); |
542 writeRelocationSections(RelRODataSections); | 545 writeRelocationSections(RelRODataSections); |
543 } | 546 } |
544 | 547 |
545 void ELFObjectWriter::setUndefinedSyms(const ConstantList &UndefSyms) { | 548 void ELFObjectWriter::setUndefinedSyms(const ConstantList &UndefSyms) { |
546 for (const Constant *S : UndefSyms) { | 549 for (const Constant *S : UndefSyms) { |
547 const auto Sym = llvm::cast<ConstantRelocatable>(S); | 550 const auto Sym = llvm::cast<ConstantRelocatable>(S); |
548 const IceString &Name = Sym->getName(); | 551 const IceString &Name = Sym->getName(); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 if (ELF64) { | 618 if (ELF64) { |
616 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), | 619 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), |
617 AllSections.size()); | 620 AllSections.size()); |
618 } else { | 621 } else { |
619 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), | 622 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), |
620 AllSections.size()); | 623 AllSections.size()); |
621 } | 624 } |
622 } | 625 } |
623 | 626 |
624 } // end of namespace Ice | 627 } // end of namespace Ice |
OLD | NEW |