Skip to content

Conversation

@maksfb
Copy link
Contributor

@maksfb maksfb commented Dec 10, 2025

Assign output sections for injected functions explicitly, and don't reassign in AssignSections pass.

This change is a prerequisite for further PRs where veneer functions are created as injected functions and their code section depends on their placement.

Assign output sections for injected functions explicitly, and don't
reassign in AssignSections pass.

This change is a prerequisite for further PRs where veneer functions are
created as injected functions and their code section depends on their
placement.
@llvmbot
Copy link
Member

llvmbot commented Dec 10, 2025

@llvm/pr-subscribers-bolt

Author: Maksim Panchenko (maksfb)

Changes

Assign output sections for injected functions explicitly, and don't reassign in AssignSections pass.

This change is a prerequisite for further PRs where veneer functions are created as injected functions and their code section depends on their placement.


Full diff: https://github.com/llvm/llvm-project/pull/171579.diff

3 Files Affected:

  • (modified) bolt/include/bolt/Core/BinaryFunction.h (+4-3)
  • (modified) bolt/lib/Core/BinaryContext.cpp (+4)
  • (modified) bolt/lib/Passes/BinaryPasses.cpp (-7)
diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index 5b147b23eccf4..4493d0c5f7872 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -735,11 +735,12 @@ class BinaryFunction {
     Symbols.push_back(BC.Ctx->getOrCreateSymbol(Name));
   }
 
-  /// This constructor is used to create an injected function
+  /// This constructor is used to create an injected function, i.e. a function
+  /// that didn't originate in the input file.
   BinaryFunction(const std::string &Name, BinaryContext &BC, bool IsSimple)
       : Address(0), Size(0), BC(BC), IsSimple(IsSimple),
-        CodeSectionName(buildCodeSectionName(Name, BC)),
-        ColdCodeSectionName(buildColdCodeSectionName(Name, BC)),
+        CodeSectionName(BC.getInjectedCodeSectionName()),
+        ColdCodeSectionName(BC.getInjectedColdCodeSectionName()),
         FunctionNumber(++Count) {
     Symbols.push_back(BC.Ctx->getOrCreateSymbol(Name));
     IsInjected = true;
diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index b06eee2f415a7..a980ef7b99c3f 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -2598,6 +2598,10 @@ BinaryContext::createInstructionPatch(uint64_t Address,
   PBF->addBasicBlock()->addInstructions(Instructions);
   PBF->setIsPatch(true);
 
+  // Patch functions have to be emitted each into their unique section.
+  PBF->setCodeSectionName(
+      BinaryFunction::buildCodeSectionName(PBF->getOneName(), *this));
+
   // Don't create symbol table entry if the name wasn't specified.
   if (Name.str().empty())
     PBF->setAnonymous(true);
diff --git a/bolt/lib/Passes/BinaryPasses.cpp b/bolt/lib/Passes/BinaryPasses.cpp
index 1d187de11c35e..144cbc573ed00 100644
--- a/bolt/lib/Passes/BinaryPasses.cpp
+++ b/bolt/lib/Passes/BinaryPasses.cpp
@@ -1276,13 +1276,6 @@ Error SimplifyRODataLoads::runOnFunctions(BinaryContext &BC) {
 }
 
 Error AssignSections::runOnFunctions(BinaryContext &BC) {
-  for (BinaryFunction *Function : BC.getInjectedBinaryFunctions()) {
-    if (!Function->isPatch()) {
-      Function->setCodeSectionName(BC.getInjectedCodeSectionName());
-      Function->setColdCodeSectionName(BC.getInjectedColdCodeSectionName());
-    }
-  }
-
   // In non-relocation mode functions have pre-assigned section names.
   if (!BC.HasRelocations)
     return Error::success();

Copy link
Contributor

@yozhu yozhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@maksfb maksfb merged commit 6470d1b into llvm:main Dec 10, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants