[llvm] Extend llvm objdump fatbin (PR #114834)

Joseph Huber via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 28 17:55:52 PST 2025


================
@@ -96,6 +103,48 @@ Error extractFromObject(const ObjectFile &Obj,
   return Error::success();
 }
 
+// Extract an Offload bundle (usually a Offload Bundle) from a fat_bin
+// section
+Error extractOffloadBundle(MemoryBufferRef Contents, uint64_t SectionOffset,
+                           StringRef fileName,
+                           SmallVectorImpl<OffloadBundleFatBin> &Bundles) {
+
+  uint64_t Offset = 0;
+  int64_t nextbundleStart = 0;
+
+  // There could be multiple offloading bundles stored at this section.
+  while (nextbundleStart >= 0) {
+
+    std::unique_ptr<MemoryBuffer> Buffer =
+        MemoryBuffer::getMemBuffer(Contents.getBuffer().drop_front(Offset), "",
+                                   /*RequiresNullTerminator*/ false);
+
+    // Create the FatBinBindle object. This will also create the Bundle Entry
+    // list info.
+    auto FatBundleOrErr =
+        OffloadBundleFatBin::create(*Buffer, SectionOffset + Offset, fileName);
+    if (!FatBundleOrErr)
+      return FatBundleOrErr.takeError();
+    OffloadBundleFatBin &Bundle = **FatBundleOrErr;
+
+    // add current Bundle to list.
+    Bundles.emplace_back(std::move(**FatBundleOrErr));
+
+    // find the next bundle by searching for the magic string
+    StringRef str = Buffer->getBuffer();
+    nextbundleStart =
+        (int64_t)str.find(StringRef("__CLANG_OFFLOAD_BUNDLE__"), 24);
+
+    if (nextbundleStart >= 0)
+      Offset += nextbundleStart;
+    else {
+      return Error::success();
----------------
jhuber6 wrote:

Why this return? Just let it exit the while loop and return normally.

https://github.com/llvm/llvm-project/pull/114834


More information about the llvm-commits mailing list