@@ -3918,15 +3918,40 @@ void RewriteInstance::mapCodeSections(BOLTLinker::SectionMapper MapSection) {
39183918 return Address;
39193919 };
39203920
3921+ // Try to allocate sections before the \p Address and return an address for
3922+ // the allocation of the first section, or 0 if [0, Address) range is not
3923+ // big enough to fit all sections.
3924+ auto allocateBefore = [&](uint64_t Address) -> uint64_t {
3925+ for (BinarySection *Section : llvm::reverse (CodeSections)) {
3926+ if (Section->getOutputSize () > Address)
3927+ return 0 ;
3928+ Address -= Section->getOutputSize ();
3929+ Address = alignDown (Address, Section->getAlignment ());
3930+ Section->setOutputAddress (Address);
3931+ }
3932+ return Address;
3933+ };
3934+
39213935 // Check if we can fit code in the original .text
39223936 bool AllocationDone = false ;
39233937 if (opts::UseOldText) {
3924- const uint64_t CodeSize =
3925- allocateAt (BC->OldTextSectionAddress ) - BC->OldTextSectionAddress ;
3938+ uint64_t StartAddress;
3939+ uint64_t EndAddress;
3940+ if (opts::HotFunctionsAtEnd) {
3941+ EndAddress = BC->OldTextSectionAddress + BC->OldTextSectionSize ;
3942+ StartAddress = allocateBefore (EndAddress);
3943+ } else {
3944+ StartAddress = BC->OldTextSectionAddress ;
3945+ EndAddress = allocateAt (BC->OldTextSectionAddress );
3946+ }
39263947
3948+ const uint64_t CodeSize = EndAddress - StartAddress;
39273949 if (CodeSize <= BC->OldTextSectionSize ) {
39283950 BC->outs () << " BOLT-INFO: using original .text for new code with 0x"
3929- << Twine::utohexstr (opts::AlignText) << " alignment\n " ;
3951+ << Twine::utohexstr (opts::AlignText) << " alignment" ;
3952+ if (StartAddress != BC->OldTextSectionAddress )
3953+ BC->outs () << " at 0x" << Twine::utohexstr (StartAddress);
3954+ BC->outs () << ' \n ' ;
39303955 AllocationDone = true ;
39313956 } else {
39323957 BC->errs ()
0 commit comments