@@ -3887,6 +3887,43 @@ void RewriteInstance::mapCodeSections(BOLTLinker::SectionMapper MapSection) {
3887
3887
3888
3888
void RewriteInstance::mapAllocatableSections (
3889
3889
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
+
3890
3927
// Allocate read-only sections first, then writable sections.
3891
3928
enum : uint8_t { ST_READONLY, ST_READWRITE };
3892
3929
for (uint8_t SType = ST_READONLY; SType <= ST_READWRITE; ++SType) {
@@ -4164,7 +4201,6 @@ void RewriteInstance::rewriteNoteSections() {
4164
4201
// New section size.
4165
4202
uint64_t Size = 0 ;
4166
4203
bool DataWritten = false ;
4167
- uint8_t *SectionData = nullptr ;
4168
4204
// Copy over section contents unless it's one of the sections we overwrite.
4169
4205
if (!willOverwriteSection (SectionName)) {
4170
4206
Size = Section.sh_size ;
@@ -4196,12 +4232,7 @@ void RewriteInstance::rewriteNoteSections() {
4196
4232
if (BSec->getAllocAddress ()) {
4197
4233
assert (!DataWritten && " Writing section twice." );
4198
4234
(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);
4205
4236
}
4206
4237
4207
4238
BSec->setOutputFileOffset (NextAvailableOffset);
@@ -4232,8 +4263,7 @@ void RewriteInstance::rewriteNoteSections() {
4232
4263
<< " of size " << Section.getOutputSize () << " at offset 0x"
4233
4264
<< Twine::utohexstr (Section.getOutputFileOffset ()) << ' \n ' );
4234
4265
4235
- OS.write (Section.getOutputContents ().data (), Section.getOutputSize ());
4236
- NextAvailableOffset += Section.getOutputSize ();
4266
+ NextAvailableOffset += Section.write (OS);
4237
4267
}
4238
4268
}
4239
4269
@@ -4347,6 +4377,10 @@ RewriteInstance::getOutputSections(ELFObjectFile<ELFT> *File,
4347
4377
BinarySection *BinSec = BC->getSectionForSectionRef (SecRef);
4348
4378
assert (BinSec && " Matching BinarySection should exist." );
4349
4379
4380
+ // Exclude anonymous sections.
4381
+ if (BinSec->isAnonymous ())
4382
+ continue ;
4383
+
4350
4384
addSection (Section, *BinSec);
4351
4385
}
4352
4386
@@ -5699,8 +5733,8 @@ void RewriteInstance::rewriteFile() {
5699
5733
<< Twine::utohexstr (Section.getAllocAddress ()) << " \n of size "
5700
5734
<< Section.getOutputSize () << " \n at offset "
5701
5735
<< 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 );
5704
5738
}
5705
5739
5706
5740
for (BinarySection &Section : BC->allocatableSections ())
0 commit comments