@@ -3887,6 +3887,43 @@ void RewriteInstance::mapCodeSections(BOLTLinker::SectionMapper MapSection) {
38873887
38883888void RewriteInstance::mapAllocatableSections (
38893889 BOLTLinker::SectionMapper MapSection) {
3890+
3891+ if (opts::UseOldText || opts::StrictMode) {
3892+ auto tryRewriteSection = [&](BinarySection &OldSection,
3893+ BinarySection &NewSection) {
3894+ if (OldSection.getSize () < NewSection.getOutputSize ())
3895+ return ;
3896+
3897+ BC->outs () << " BOLT-INFO: rewriting " << OldSection.getName ()
3898+ << " in-place\n " ;
3899+
3900+ NewSection.setOutputAddress (OldSection.getAddress ());
3901+ NewSection.setOutputFileOffset (OldSection.getInputFileOffset ());
3902+ MapSection (NewSection, OldSection.getAddress ());
3903+
3904+ // Pad contents with zeros.
3905+ NewSection.addPadding (OldSection.getSize () - NewSection.getOutputSize ());
3906+
3907+ // Prevent the original section name from appearing in the section header
3908+ // table.
3909+ OldSection.setAnonymous (true );
3910+ };
3911+
3912+ if (EHFrameSection) {
3913+ BinarySection *NewEHFrameSection =
3914+ getSection (getNewSecPrefix () + getEHFrameSectionName ());
3915+ assert (NewEHFrameSection && " New contents expected for .eh_frame" );
3916+ tryRewriteSection (*EHFrameSection, *NewEHFrameSection);
3917+ }
3918+ BinarySection *EHSection = getSection (" .gcc_except_table" );
3919+ BinarySection *NewEHSection =
3920+ getSection (getNewSecPrefix () + " .gcc_except_table" );
3921+ if (EHSection) {
3922+ assert (NewEHSection && " New contents expected for .gcc_except_table" );
3923+ tryRewriteSection (*EHSection, *NewEHSection);
3924+ }
3925+ }
3926+
38903927 // Allocate read-only sections first, then writable sections.
38913928 enum : uint8_t { ST_READONLY, ST_READWRITE };
38923929 for (uint8_t SType = ST_READONLY; SType <= ST_READWRITE; ++SType) {
@@ -4164,7 +4201,6 @@ void RewriteInstance::rewriteNoteSections() {
41644201 // New section size.
41654202 uint64_t Size = 0 ;
41664203 bool DataWritten = false ;
4167- uint8_t *SectionData = nullptr ;
41684204 // Copy over section contents unless it's one of the sections we overwrite.
41694205 if (!willOverwriteSection (SectionName)) {
41704206 Size = Section.sh_size ;
@@ -4196,12 +4232,7 @@ void RewriteInstance::rewriteNoteSections() {
41964232 if (BSec->getAllocAddress ()) {
41974233 assert (!DataWritten && " Writing section twice." );
41984234 (void )DataWritten;
4199- SectionData = BSec->getOutputData ();
4200-
4201- LLVM_DEBUG (dbgs () << " BOLT-DEBUG: " << (Size ? " appending" : " writing" )
4202- << " contents to section " << SectionName << ' \n ' );
4203- OS.write (reinterpret_cast <char *>(SectionData), BSec->getOutputSize ());
4204- Size += BSec->getOutputSize ();
4235+ Size += BSec->write (OS);
42054236 }
42064237
42074238 BSec->setOutputFileOffset (NextAvailableOffset);
@@ -4232,8 +4263,7 @@ void RewriteInstance::rewriteNoteSections() {
42324263 << " of size " << Section.getOutputSize () << " at offset 0x"
42334264 << Twine::utohexstr (Section.getOutputFileOffset ()) << ' \n ' );
42344265
4235- OS.write (Section.getOutputContents ().data (), Section.getOutputSize ());
4236- NextAvailableOffset += Section.getOutputSize ();
4266+ NextAvailableOffset += Section.write (OS);
42374267 }
42384268}
42394269
@@ -4347,6 +4377,10 @@ RewriteInstance::getOutputSections(ELFObjectFile<ELFT> *File,
43474377 BinarySection *BinSec = BC->getSectionForSectionRef (SecRef);
43484378 assert (BinSec && " Matching BinarySection should exist." );
43494379
4380+ // Exclude anonymous sections.
4381+ if (BinSec->isAnonymous ())
4382+ continue ;
4383+
43504384 addSection (Section, *BinSec);
43514385 }
43524386
@@ -5699,8 +5733,8 @@ void RewriteInstance::rewriteFile() {
56995733 << Twine::utohexstr (Section.getAllocAddress ()) << " \n of size "
57005734 << Section.getOutputSize () << " \n at offset "
57015735 << Section.getOutputFileOffset () << ' \n ' ;
5702- OS.pwrite ( reinterpret_cast < const char *>( Section.getOutputData ()),
5703- Section.getOutputSize (), Section. getOutputFileOffset () );
5736+ OS.seek ( Section.getOutputFileOffset ());
5737+ Section.write (OS );
57045738 }
57055739
57065740 for (BinarySection &Section : BC->allocatableSections ())
0 commit comments