[BOLT] LongJmp: Fix hot text section alignment
authorVladislav Khmelevsky <och95@yandex.ru>
Tue, 15 Mar 2022 19:17:51 +0000 (22:17 +0300)
committerVladislav Khmelevsky <och95@yandex.ru>
Wed, 16 Mar 2022 12:57:46 +0000 (15:57 +0300)
The BinaryEmitter uses opts::AlignText value to align the hot text
section. Also check that the opts::AlignText is at least
equal opts::AlignFunctions for the same reason, as described in D121392.

Vladislav Khmelevsky,
Advanced Software Technology Lab, Huawei

Differential Revision: https://reviews.llvm.org/D121728

bolt/lib/Passes/LongJmp.cpp
bolt/lib/Rewrite/RewriteInstance.cpp

index d576bf1..fbbe054 100644 (file)
@@ -18,6 +18,7 @@ using namespace llvm;
 
 namespace opts {
 extern cl::OptionCategory BoltOptCategory;
+extern llvm::cl::opt<unsigned> AlignText;
 extern cl::opt<unsigned> AlignFunctions;
 extern cl::opt<bool> UseOldText;
 extern cl::opt<bool> HotFunctionsAtEnd;
@@ -342,7 +343,7 @@ uint64_t LongJmpPass::tentativeLayoutRelocMode(
           tentativeLayoutRelocColdPart(BC, SortedFunctions, DotAddress);
       ColdLayoutDone = true;
       if (opts::HotFunctionsAtEnd)
-        DotAddress = alignTo(DotAddress, BC.PageAlign);
+        DotAddress = alignTo(DotAddress, opts::AlignText);
     }
 
     DotAddress = alignTo(DotAddress, BinaryFunction::MinAlign);
@@ -390,11 +391,11 @@ void LongJmpPass::tentativeLayout(
   // Initial padding
   if (opts::UseOldText && EstimatedTextSize <= BC.OldTextSectionSize) {
     DotAddress = BC.OldTextSectionAddress;
-    uint64_t Pad = offsetToAlignment(DotAddress, llvm::Align(BC.PageAlign));
+    uint64_t Pad = offsetToAlignment(DotAddress, llvm::Align(opts::AlignText));
     if (Pad + EstimatedTextSize <= BC.OldTextSectionSize)
       DotAddress += Pad;
   } else {
-    DotAddress = alignTo(BC.LayoutStartAddress, BC.PageAlign);
+    DotAddress = alignTo(BC.LayoutStartAddress, opts::AlignText);
   }
 
   tentativeLayoutRelocMode(BC, SortedFunctions, DotAddress);
index b814862..8c8c55b 100644 (file)
@@ -1743,6 +1743,9 @@ void RewriteInstance::adjustCommandLineOptions() {
   if (!opts::AlignText.getNumOccurrences())
     opts::AlignText = BC->PageAlign;
 
+  if (opts::AlignText < opts::AlignFunctions)
+    opts::AlignText = (unsigned)opts::AlignFunctions;
+
   if (BC->isX86() && opts::Lite.getNumOccurrences() == 0 && !opts::StrictMode &&
       !opts::UseOldText)
     opts::Lite = true;