Chromium Code Reviews| 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 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 376 assert(ST == BSS || ST == ROData); | 376 assert(ST == BSS || ST == ROData); |
| 377 if (ST == ROData) | 377 if (ST == ROData) |
| 378 Section->appendZeros(Str, SymbolSize); | 378 Section->appendZeros(Str, SymbolSize); |
| 379 else | 379 else |
| 380 Section->setSize(Section->getCurrentSize() + SymbolSize); | 380 Section->setSize(Section->getCurrentSize() + SymbolSize); |
| 381 } else { | 381 } else { |
| 382 assert(ST != BSS); | 382 assert(ST != BSS); |
| 383 for (VariableDeclaration::Initializer *Init : Var->getInitializers()) { | 383 for (VariableDeclaration::Initializer *Init : Var->getInitializers()) { |
| 384 switch (Init->getKind()) { | 384 switch (Init->getKind()) { |
| 385 case VariableDeclaration::Initializer::DataInitializerKind: { | 385 case VariableDeclaration::Initializer::DataInitializerKind: { |
| 386 const auto Data = llvm::cast<VariableDeclaration::DataInitializer>( | 386 const auto Data = |
| 387 Init)->getContents(); | 387 llvm::cast<VariableDeclaration::DataInitializer>(Init) |
| 388 ->getContents(); | |
| 388 Section->appendData(Str, llvm::StringRef(Data.data(), Data.size())); | 389 Section->appendData(Str, llvm::StringRef(Data.data(), Data.size())); |
| 389 break; | 390 break; |
| 390 } | 391 } |
| 391 case VariableDeclaration::Initializer::ZeroInitializerKind: | 392 case VariableDeclaration::Initializer::ZeroInitializerKind: |
| 392 Section->appendZeros(Str, Init->getNumBytes()); | 393 Section->appendZeros(Str, Init->getNumBytes()); |
| 393 break; | 394 break; |
| 394 case VariableDeclaration::Initializer::RelocInitializerKind: { | 395 case VariableDeclaration::Initializer::RelocInitializerKind: { |
| 395 const auto Reloc = | 396 const auto Reloc = |
| 396 llvm::cast<VariableDeclaration::RelocInitializer>(Init); | 397 llvm::cast<VariableDeclaration::RelocInitializer>(Init); |
| 397 AssemblerFixup NewFixup; | 398 AssemblerFixup NewFixup; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 480 } | 481 } |
| 481 SizeT Align = typeAlignInBytes(Ty); | 482 SizeT Align = typeAlignInBytes(Ty); |
| 482 size_t EntSize = typeWidthInBytes(Ty); | 483 size_t EntSize = typeWidthInBytes(Ty); |
| 483 char Buf[20]; | 484 char Buf[20]; |
| 484 SizeT WriteAmt = std::min(EntSize, llvm::array_lengthof(Buf)); | 485 SizeT WriteAmt = std::min(EntSize, llvm::array_lengthof(Buf)); |
| 485 assert(WriteAmt == EntSize); | 486 assert(WriteAmt == EntSize); |
| 486 // Assume that writing WriteAmt bytes at a time allows us to avoid aligning | 487 // Assume that writing WriteAmt bytes at a time allows us to avoid aligning |
| 487 // between entries. | 488 // between entries. |
| 488 assert(WriteAmt % Align == 0); | 489 assert(WriteAmt % Align == 0); |
| 489 // Check that we write the full PrimType. | 490 // Check that we write the full PrimType. |
| 490 assert(WriteAmt == sizeof(typename ConstType::PrimType)); | 491 assert(WriteAmt == sizeof(typename ConstType::PrimType) || |
|
Jim Stichnoth
2015/06/19 16:51:02
I think this assert should be removed. The commen
qining
2015/06/19 20:22:25
Done.
| |
| 492 WriteAmt == sizeof(short) || WriteAmt == sizeof(char)); | |
| 491 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_MERGE; | 493 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_MERGE; |
| 492 std::string SecBuffer; | 494 std::string SecBuffer; |
| 493 llvm::raw_string_ostream SecStrBuf(SecBuffer); | 495 llvm::raw_string_ostream SecStrBuf(SecBuffer); |
| 494 SecStrBuf << ".rodata.cst" << WriteAmt; | 496 SecStrBuf << ".rodata.cst" << WriteAmt; |
| 495 ELFDataSection *Section = createSection<ELFDataSection>( | 497 ELFDataSection *Section = createSection<ELFDataSection>( |
| 496 SecStrBuf.str(), SHT_PROGBITS, ShFlags, Align, WriteAmt); | 498 SecStrBuf.str(), SHT_PROGBITS, ShFlags, Align, WriteAmt); |
| 497 RODataSections.push_back(Section); | 499 RODataSections.push_back(Section); |
| 498 SizeT OffsetInSection = 0; | 500 SizeT OffsetInSection = 0; |
| 499 // The symbol table entry doesn't need to know the defined symbol's | 501 // The symbol table entry doesn't need to know the defined symbol's |
| 500 // size since this is in a section with a fixed Entry Size. | 502 // size since this is in a section with a fixed Entry Size. |
| 501 const SizeT SymbolSize = 0; | 503 const SizeT SymbolSize = 0; |
| 502 Section->setFileOffset(alignFileOffset(Align)); | 504 Section->setFileOffset(alignFileOffset(Align)); |
| 503 | 505 |
| 504 // Write the data. | 506 // Write the data. |
| 505 for (Constant *C : Pool) { | 507 for (Constant *C : Pool) { |
| 508 if (!C->shouldBePooled) | |
| 509 continue; | |
| 506 auto Const = llvm::cast<ConstType>(C); | 510 auto Const = llvm::cast<ConstType>(C); |
| 507 std::string SymBuffer; | 511 std::string SymBuffer; |
| 508 llvm::raw_string_ostream SymStrBuf(SymBuffer); | 512 llvm::raw_string_ostream SymStrBuf(SymBuffer); |
| 509 Const->emitPoolLabel(SymStrBuf); | 513 Const->emitPoolLabel(SymStrBuf); |
| 510 std::string &SymName = SymStrBuf.str(); | 514 std::string &SymName = SymStrBuf.str(); |
| 511 SymTab->createDefinedSym(SymName, STT_NOTYPE, STB_LOCAL, Section, | 515 SymTab->createDefinedSym(SymName, STT_NOTYPE, STB_LOCAL, Section, |
| 512 OffsetInSection, SymbolSize); | 516 OffsetInSection, SymbolSize); |
| 513 StrTab->add(SymName); | 517 StrTab->add(SymName); |
| 514 typename ConstType::PrimType Value = Const->getValue(); | 518 typename ConstType::PrimType Value = Const->getValue(); |
| 515 memcpy(Buf, &Value, WriteAmt); | 519 memcpy(Buf, &Value, WriteAmt); |
| 516 Str.writeBytes(llvm::StringRef(Buf, WriteAmt)); | 520 Str.writeBytes(llvm::StringRef(Buf, WriteAmt)); |
| 517 OffsetInSection += WriteAmt; | 521 OffsetInSection += WriteAmt; |
| 518 } | 522 } |
| 519 Section->setSize(OffsetInSection); | 523 Section->setSize(OffsetInSection); |
| 520 } | 524 } |
| 521 | 525 |
| 522 // Instantiate known needed versions of the template, since we are | 526 // Instantiate known needed versions of the template, since we are |
| 523 // defining the function in the .cpp file instead of the .h file. | 527 // defining the function in the .cpp file instead of the .h file. |
| 524 // We may need to instantiate constant pools for integers as well | 528 // We may need to instantiate constant pools for integers as well |
| 525 // if we do constant-pooling of large integers to remove them | 529 // if we do constant-pooling of large integers to remove them |
| 526 // from the instruction stream (fewer bytes controlled by an attacker). | 530 // from the instruction stream (fewer bytes controlled by an attacker). |
| 527 template void ELFObjectWriter::writeConstantPool<ConstantFloat>(Type Ty); | 531 template void ELFObjectWriter::writeConstantPool<ConstantFloat>(Type Ty); |
| 528 | 532 |
| 529 template void ELFObjectWriter::writeConstantPool<ConstantDouble>(Type Ty); | 533 template void ELFObjectWriter::writeConstantPool<ConstantDouble>(Type Ty); |
| 530 | 534 |
| 535 template void ELFObjectWriter::writeConstantPool<ConstantInteger32>(Type Ty); | |
| 536 | |
| 531 void ELFObjectWriter::writeAllRelocationSections() { | 537 void ELFObjectWriter::writeAllRelocationSections() { |
| 532 writeRelocationSections(RelTextSections); | 538 writeRelocationSections(RelTextSections); |
| 533 writeRelocationSections(RelDataSections); | 539 writeRelocationSections(RelDataSections); |
| 534 writeRelocationSections(RelRODataSections); | 540 writeRelocationSections(RelRODataSections); |
| 535 } | 541 } |
| 536 | 542 |
| 537 void ELFObjectWriter::setUndefinedSyms(const ConstantList &UndefSyms) { | 543 void ELFObjectWriter::setUndefinedSyms(const ConstantList &UndefSyms) { |
| 538 for (const Constant *S : UndefSyms) { | 544 for (const Constant *S : UndefSyms) { |
| 539 const auto Sym = llvm::cast<ConstantRelocatable>(S); | 545 const auto Sym = llvm::cast<ConstantRelocatable>(S); |
| 540 const IceString &Name = Sym->getName(); | 546 const IceString &Name = Sym->getName(); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 607 if (ELF64) { | 613 if (ELF64) { |
| 608 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), | 614 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), |
| 609 AllSections.size()); | 615 AllSections.size()); |
| 610 } else { | 616 } else { |
| 611 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), | 617 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), |
| 612 AllSections.size()); | 618 AllSections.size()); |
| 613 } | 619 } |
| 614 } | 620 } |
| 615 | 621 |
| 616 } // end of namespace Ice | 622 } // end of namespace Ice |
| OLD | NEW |