[llvm] [bolt] keep only function with prologue (PR #87520)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 7 00:54:20 PDT 2024


https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/87520

>From 69c54558e724d9a1c830febfd4de4639ea40d492 Mon Sep 17 00:00:00 2001
From: Bing1 Yu <bing1.yu at intel.com>
Date: Thu, 4 Apr 2024 00:43:56 +0800
Subject: [PATCH] [bolt] keep only function with prologue

---
 bolt/include/bolt/Rewrite/RewriteInstance.h |  2 ++
 bolt/lib/Rewrite/RewriteInstance.cpp        | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/bolt/include/bolt/Rewrite/RewriteInstance.h b/bolt/include/bolt/Rewrite/RewriteInstance.h
index 97ab65cd5a4a1f..9af81ee9caf5cc 100644
--- a/bolt/include/bolt/Rewrite/RewriteInstance.h
+++ b/bolt/include/bolt/Rewrite/RewriteInstance.h
@@ -98,6 +98,8 @@ class RewriteInstance {
   /// from meta data in the file.
   void discoverFileObjects();
 
+  void keepPrologueFunction();
+
   /// 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 cde195c1739074..f4404acc004b79 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -722,6 +722,7 @@ Error RewriteInstance::run() {
     return E;
   adjustCommandLineOptions();
   discoverFileObjects();
+  keepPrologueFunction();
 
   if (opts::Instrument && !BC->IsStaticExecutable)
     if (Error E = discoverRtFiniAddress())
@@ -784,6 +785,24 @@ Error RewriteInstance::run() {
   return Error::success();
 }
 
+void RewriteInstance::keepPrologueFunction() {
+ BC->outs() << "keepPrologueFunction!"<<"\n";
+  for(auto &BFI : BC->getBinaryFunctions()) {
+    BinaryFunction &BF = BFI.second;
+    ErrorOr<ArrayRef<uint8_t>> ErrorOrFunctionData = BF.getData();
+    assert(ErrorOrFunctionData && "function data is not available");
+    ArrayRef<uint8_t> IData = *ErrorOrFunctionData;
+      BC->outs()  << "Potential Function Entry Point: 0x" << Twine::utohexstr(IData[0]) << "\n";
+      // Check for common function prologue patterns
+      if ((char)IData[0] == '\x55' && (char)IData[1] == '\x48' && (char)IData[2] == '\x89' && (char)IData[3] == '\xe5') {
+          BC->outs()  << "Potential Function Entry Point: 0x" << Twine::utohexstr(BF.getAddress()) << "\n";
+      } else {
+        BF.setIgnored();
+      }
+
+  }
+}
+
 void RewriteInstance::discoverFileObjects() {
   NamedRegionTimer T("discoverFileObjects", "discover file objects",
                      TimerGroupName, TimerGroupDesc, opts::TimeRewrite);



More information about the llvm-commits mailing list