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