diff --git a/bolt/include/bolt/Rewrite/RewriteInstance.h b/bolt/include/bolt/Rewrite/RewriteInstance.h index 41a92e7ba01e..64113bd02601 100644 --- a/bolt/include/bolt/Rewrite/RewriteInstance.h +++ b/bolt/include/bolt/Rewrite/RewriteInstance.h @@ -97,6 +97,10 @@ private: /// from meta data in the file. void discoverFileObjects(); + /// Check if the input binary has a space reserved for BOLT and use it for new + /// section allocations if found. + void discoverBOLTReserved(); + /// Check whether we should use DT_FINI or DT_FINI_ARRAY for instrumentation. /// DT_FINI is preferred; DT_FINI_ARRAY is only used when no DT_FINI entry was /// found. diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp index 62759b7222a7..85b39176754b 100644 --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -1347,6 +1347,35 @@ void RewriteInstance::discoverFileObjects() { registerFragments(); FileSymbols.clear(); + + discoverBOLTReserved(); +} + +void RewriteInstance::discoverBOLTReserved() { + BinaryData *StartBD = BC->getBinaryDataByName(getBOLTReservedStart()); + BinaryData *EndBD = BC->getBinaryDataByName(getBOLTReservedEnd()); + if (!StartBD != !EndBD) { + BC->errs() << "BOLT-ERROR: one of the symbols is missing from the binary: " + << getBOLTReservedStart() << ", " << getBOLTReservedEnd() + << '\n'; + exit(1); + } + + if (!StartBD) + return; + + if (StartBD->getAddress() >= EndBD->getAddress()) { + BC->errs() << "BOLT-ERROR: invalid reserved space boundaries\n"; + exit(1); + } + BC->BOLTReserved = AddressRange(StartBD->getAddress(), EndBD->getAddress()); + BC->outs() << "BOLT-INFO: using reserved space for allocating new sections\n"; + + PHDRTableOffset = 0; + PHDRTableAddress = 0; + NewTextSegmentAddress = 0; + NewTextSegmentOffset = 0; + NextAvailableAddress = BC->BOLTReserved.start(); } Error RewriteInstance::discoverRtFiniAddress() { @@ -3617,32 +3646,6 @@ void RewriteInstance::updateMetadata() { void RewriteInstance::mapFileSections(BOLTLinker::SectionMapper MapSection) { BC->deregisterUnusedSections(); - // Check if the input has a space reserved for BOLT. - BinaryData *StartBD = BC->getBinaryDataByName(getBOLTReservedStart()); - BinaryData *EndBD = BC->getBinaryDataByName(getBOLTReservedEnd()); - if (!StartBD != !EndBD) { - BC->errs() << "BOLT-ERROR: one of the symbols is missing from the binary: " - << getBOLTReservedStart() << ", " << getBOLTReservedEnd() - << '\n'; - exit(1); - } - - if (StartBD) { - if (StartBD->getAddress() >= EndBD->getAddress()) { - BC->errs() << "BOLT-ERROR: invalid reserved space boundaries\n"; - exit(1); - } - BC->BOLTReserved = AddressRange(StartBD->getAddress(), EndBD->getAddress()); - BC->outs() - << "BOLT-INFO: using reserved space for allocating new sections\n"; - - PHDRTableOffset = 0; - PHDRTableAddress = 0; - NewTextSegmentAddress = 0; - NewTextSegmentOffset = 0; - NextAvailableAddress = BC->BOLTReserved.start(); - } - // If no new .eh_frame was written, remove relocated original .eh_frame. BinarySection *RelocatedEHFrameSection = getSection(".relocated" + getEHFrameSectionName());