[llvm] Jitlink xcoff graphbuilder (PR #128524)

Henry Jiang via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 24 07:22:57 PST 2025


https://github.com/mustartt created https://github.com/llvm/llvm-project/pull/128524

None

>From f7983454c8687c37304030152228ea9735fbab6a Mon Sep 17 00:00:00 2001
From: Henry Jiang <henry.jiang1 at ibm.com>
Date: Fri, 14 Feb 2025 16:37:25 -0500
Subject: [PATCH 1/5] Setup build support for XCOFF

---
 .../llvm/ExecutionEngine/JITLink/XCOFF.h      | 37 +++++++++++++
 .../ExecutionEngine/JITLink/XCOFF_ppc64.h     | 37 +++++++++++++
 .../ExecutionEngine/JITLink/CMakeLists.txt    |  4 ++
 llvm/lib/ExecutionEngine/JITLink/JITLink.cpp  |  3 ++
 llvm/lib/ExecutionEngine/JITLink/XCOFF.cpp    | 43 +++++++++++++++
 .../ExecutionEngine/JITLink/XCOFF_ppc64.cpp   | 53 +++++++++++++++++++
 .../ExecutionEngine/Orc/LoadLinkableFile.cpp  | 16 ++++++
 7 files changed, 193 insertions(+)
 create mode 100644 llvm/include/llvm/ExecutionEngine/JITLink/XCOFF.h
 create mode 100644 llvm/include/llvm/ExecutionEngine/JITLink/XCOFF_ppc64.h
 create mode 100644 llvm/lib/ExecutionEngine/JITLink/XCOFF.cpp
 create mode 100644 llvm/lib/ExecutionEngine/JITLink/XCOFF_ppc64.cpp

diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/XCOFF.h b/llvm/include/llvm/ExecutionEngine/JITLink/XCOFF.h
new file mode 100644
index 0000000000000..3d181d0786eb7
--- /dev/null
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/XCOFF.h
@@ -0,0 +1,37 @@
+//===------- XCOFF.h - Generic JIT link function for XCOFF ------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// jit-link functions for XCOFF.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_JITLINK_XCOFF_H
+#define LLVM_EXECUTIONENGINE_JITLINK_XCOFF_H
+
+#include "llvm/ExecutionEngine/JITLink/JITLink.h"
+
+namespace llvm {
+namespace jitlink {
+
+/// Create a LinkGraph from an XCOFF relocatable object.
+///
+/// Note: The graph does not take ownership of the underlying buffer, nor copy
+/// its contents. The caller is responsible for ensuring that the object buffer
+/// outlives the graph.
+Expected<std::unique_ptr<LinkGraph>>
+createLinkGraphFromXCOFFObject(MemoryBufferRef ObjectBuffer,
+                               std::shared_ptr<orc::SymbolStringPool> SSP);
+
+/// Link the given graph.
+void link_XCOFF(std::unique_ptr<LinkGraph> G,
+                std::unique_ptr<JITLinkContext> Ctx);
+
+} // namespace jitlink
+} // namespace llvm
+
+#endif // LLVM_EXECUTIONENGINE_JITLINK_XCOFF_H
diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/XCOFF_ppc64.h b/llvm/include/llvm/ExecutionEngine/JITLink/XCOFF_ppc64.h
new file mode 100644
index 0000000000000..ec5c8a37bda27
--- /dev/null
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/XCOFF_ppc64.h
@@ -0,0 +1,37 @@
+//===------ XCOFF_ppc64.h - JIT link functions for XCOFF/ppc64 ------*- C++
+//-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// jit-link functions for XCOFF/ppc64.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_JITLINK_XCOFF_PPC64_H
+#define LLVM_EXECUTIONENGINE_JITLINK_XCOFF_PPC64_H
+
+#include "llvm/ExecutionEngine/JITLink/JITLink.h"
+
+namespace llvm::jitlink {
+
+/// Create a LinkGraph from an XCOFF/ppc64 relocatable object.
+///
+/// Note: The graph does not take ownership of the underlying buffer, nor copy
+/// its contents. The caller is responsible for ensuring that the object buffer
+/// outlives the graph.
+///
+Expected<std::unique_ptr<LinkGraph>> createLinkGraphFromXCOFFObject_ppc64(
+    MemoryBufferRef ObjectBuffer, std::shared_ptr<orc::SymbolStringPool> SSP);
+
+/// jit-link the given object buffer, which must be a XCOFF ppc64 object file.
+///
+void link_XCOFF_ppc64(std::unique_ptr<LinkGraph> G,
+                      std::unique_ptr<JITLinkContext> Ctx);
+
+} // end namespace llvm::jitlink
+
+#endif // LLVM_EXECUTIONENGINE_JITLINK_XCOFF_PPC64_H
diff --git a/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt b/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt
index 65dd0c7468ae1..031222f3d8910 100644
--- a/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt
+++ b/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt
@@ -35,6 +35,10 @@ add_llvm_component_library(LLVMJITLink
   COFFLinkGraphBuilder.cpp
   COFF_x86_64.cpp
 
+  # XCOFF
+  XCOFF.cpp
+  XCOFF_ppc64.cpp
+
   # Architectures:
   aarch32.cpp
   aarch64.cpp
diff --git a/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp b/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp
index e8ce9b2b9527d..c68adff8631ee 100644
--- a/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp
@@ -13,6 +13,7 @@
 #include "llvm/ExecutionEngine/JITLink/COFF.h"
 #include "llvm/ExecutionEngine/JITLink/ELF.h"
 #include "llvm/ExecutionEngine/JITLink/MachO.h"
+#include "llvm/ExecutionEngine/JITLink/XCOFF.h"
 #include "llvm/ExecutionEngine/JITLink/aarch64.h"
 #include "llvm/ExecutionEngine/JITLink/i386.h"
 #include "llvm/ExecutionEngine/JITLink/loongarch.h"
@@ -501,6 +502,8 @@ createLinkGraphFromObject(MemoryBufferRef ObjectBuffer,
     return createLinkGraphFromELFObject(ObjectBuffer, std::move(SSP));
   case file_magic::coff_object:
     return createLinkGraphFromCOFFObject(ObjectBuffer, std::move(SSP));
+  case file_magic::xcoff_object_64:
+    return createLinkGraphFromXCOFFObject(ObjectBuffer, std::move(SSP));
   default:
     return make_error<JITLinkError>("Unsupported file format");
   };
diff --git a/llvm/lib/ExecutionEngine/JITLink/XCOFF.cpp b/llvm/lib/ExecutionEngine/JITLink/XCOFF.cpp
new file mode 100644
index 0000000000000..66b31e33108ca
--- /dev/null
+++ b/llvm/lib/ExecutionEngine/JITLink/XCOFF.cpp
@@ -0,0 +1,43 @@
+//===-------------- XCOFF.cpp - JIT linker function for XCOFF -------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// XCOFF jit-link function.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ExecutionEngine/JITLink/XCOFF.h"
+#include "llvm/ExecutionEngine/JITLink/XCOFF_ppc64.h"
+#include "llvm/Object/XCOFFObjectFile.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "jitlink"
+
+namespace llvm {
+namespace jitlink {
+
+Expected<std::unique_ptr<LinkGraph>>
+createLinkGraphFromXCOFFObject(MemoryBufferRef ObjectBuffer,
+                               std::shared_ptr<orc::SymbolStringPool> SSP) {
+  // Check magic
+  file_magic Magic = identify_magic(ObjectBuffer.getBuffer());
+  if (Magic != file_magic::xcoff_object_64)
+    return make_error<JITLinkError>("Invalid XCOFF 64 Header");
+
+  // TODO: See if we need to add more checks
+  //
+  return createLinkGraphFromXCOFFObject_ppc64(ObjectBuffer, std::move(SSP));
+}
+
+void link_XCOFF(std::unique_ptr<LinkGraph> G,
+                std::unique_ptr<JITLinkContext> Ctx) {
+  llvm_unreachable("Not implmeneted for XCOFF yet");
+}
+
+} // namespace jitlink
+} // namespace llvm
diff --git a/llvm/lib/ExecutionEngine/JITLink/XCOFF_ppc64.cpp b/llvm/lib/ExecutionEngine/JITLink/XCOFF_ppc64.cpp
new file mode 100644
index 0000000000000..81f35ca0a5a2a
--- /dev/null
+++ b/llvm/lib/ExecutionEngine/JITLink/XCOFF_ppc64.cpp
@@ -0,0 +1,53 @@
+//===------- XCOFF_ppc64.cpp -JIT linker implementation for XCOFF/ppc64
+//-------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// XCOFF/ppc64 jit-link implementation.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ExecutionEngine/JITLink/XCOFF_ppc64.h"
+#include "llvm/Object/ObjectFile.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "jitlink"
+
+namespace llvm {
+namespace jitlink {
+
+Expected<std::unique_ptr<LinkGraph>> createLinkGraphFromXCOFFObject_ppc64(
+    MemoryBufferRef ObjectBuffer, std::shared_ptr<orc::SymbolStringPool> SSP) {
+  LLVM_DEBUG({
+    dbgs() << "Building jitlink graph for new input "
+           << ObjectBuffer.getBufferIdentifier() << "...\n";
+  });
+
+  auto Obj = object::ObjectFile::createObjectFile(ObjectBuffer);
+  if (!Obj)
+    return Obj.takeError();
+  assert((**Obj).isXCOFF() && "Expects and XCOFF Object");
+
+  auto Features = (*Obj)->getFeatures();
+  if (!Features)
+    return Features.takeError();
+  LLVM_DEBUG({
+    dbgs() << " Features: ";
+    (*Features).print(dbgs());
+  });
+
+  llvm_unreachable("Graph builder not implemented for XCOFF yet");
+}
+
+void link_XCOFF_ppc64(std::unique_ptr<LinkGraph> G,
+                      std::unique_ptr<JITLinkContext> Ctx) {
+  llvm_unreachable("Link implemented for XCOFF yet");
+}
+
+} // namespace jitlink
+} // namespace llvm
diff --git a/llvm/lib/ExecutionEngine/Orc/LoadLinkableFile.cpp b/llvm/lib/ExecutionEngine/Orc/LoadLinkableFile.cpp
index 77ae7c7ca2e0e..4f01c01da4b9f 100644
--- a/llvm/lib/ExecutionEngine/Orc/LoadLinkableFile.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/LoadLinkableFile.cpp
@@ -25,6 +25,13 @@ checkCOFFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj,
   return std::move(Obj);
 }
 
+static Expected<std::unique_ptr<MemoryBuffer>>
+checkXCOFFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj,
+                            const Triple &TT) {
+  // TODO: Actually check the architecture of the file.
+  return std::move(Obj);
+}
+
 static Expected<std::unique_ptr<MemoryBuffer>>
 checkELFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT) {
   // TODO: Actually check the architecture of the file.
@@ -105,6 +112,15 @@ loadLinkableFile(StringRef Path, const Triple &TT, LoadArchives LA,
       return loadLinkableSliceFromMachOUniversalBinary(
           FD, std::move(*Buf), TT, LA, Path, *IdentifierOverride);
     break;
+  case file_magic::xcoff_object_64:
+    if (!RequireFormat || *RequireFormat == Triple::XCOFF) {
+      auto CheckedBuf = checkXCOFFRelocatableObject(std::move(*Buf), TT);
+      if (!CheckedBuf)
+        return CheckedBuf.takeError();
+      return std::make_pair(std::move(*CheckedBuf),
+                            LinkableFileKind::RelocatableObject);
+    }
+    break;
   default:
     break;
   }

>From 230bdda3debd5e2908d8e5946c6cfd7726f90a3b Mon Sep 17 00:00:00 2001
From: Henry Jiang <henry.jiang1 at ibm.com>
Date: Sat, 15 Feb 2025 17:25:03 -0500
Subject: [PATCH 2/5] graph builder

---
 .../ExecutionEngine/JITLink/CMakeLists.txt    |   1 +
 llvm/lib/ExecutionEngine/JITLink/JITLink.cpp  |   2 +
 llvm/lib/ExecutionEngine/JITLink/XCOFF.cpp    |   2 +-
 .../JITLink/XCOFFLinkGraphBuilder.cpp         | 589 ++++++++++++++++++
 .../JITLink/XCOFFLinkGraphBuilder.h           |  59 ++
 .../ExecutionEngine/JITLink/XCOFF_ppc64.cpp   |  13 +-
 llvm/lib/Object/XCOFFObjectFile.cpp           |  23 +-
 7 files changed, 676 insertions(+), 13 deletions(-)
 create mode 100644 llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp
 create mode 100644 llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.h

diff --git a/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt b/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt
index 031222f3d8910..22e4513e1374c 100644
--- a/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt
+++ b/llvm/lib/ExecutionEngine/JITLink/CMakeLists.txt
@@ -38,6 +38,7 @@ add_llvm_component_library(LLVMJITLink
   # XCOFF
   XCOFF.cpp
   XCOFF_ppc64.cpp
+  XCOFFLinkGraphBuilder.cpp
 
   # Architectures:
   aarch32.cpp
diff --git a/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp b/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp
index c68adff8631ee..15a8fcf312ade 100644
--- a/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/JITLink.cpp
@@ -535,6 +535,8 @@ void link(std::unique_ptr<LinkGraph> G, std::unique_ptr<JITLinkContext> Ctx) {
     return link_ELF(std::move(G), std::move(Ctx));
   case Triple::COFF:
     return link_COFF(std::move(G), std::move(Ctx));
+  case Triple::XCOFF:
+    return link_XCOFF(std::move(G), std::move(Ctx));
   default:
     Ctx->notifyFailed(make_error<JITLinkError>("Unsupported object format"));
   };
diff --git a/llvm/lib/ExecutionEngine/JITLink/XCOFF.cpp b/llvm/lib/ExecutionEngine/JITLink/XCOFF.cpp
index 66b31e33108ca..cb026538632a9 100644
--- a/llvm/lib/ExecutionEngine/JITLink/XCOFF.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/XCOFF.cpp
@@ -36,7 +36,7 @@ createLinkGraphFromXCOFFObject(MemoryBufferRef ObjectBuffer,
 
 void link_XCOFF(std::unique_ptr<LinkGraph> G,
                 std::unique_ptr<JITLinkContext> Ctx) {
-  llvm_unreachable("Not implmeneted for XCOFF yet");
+  link_XCOFF_ppc64(std::move(G), std::move(Ctx));
 }
 
 } // namespace jitlink
diff --git a/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp b/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp
new file mode 100644
index 0000000000000..f3de6e479c6b4
--- /dev/null
+++ b/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp
@@ -0,0 +1,589 @@
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Generic XCOFF LinkGraph building code.
+//
+//===----------------------------------------------------------------------===//
+
+#include "XCOFFLinkGraphBuilder.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/BinaryFormat/XCOFF.h"
+#include "llvm/ExecutionEngine/JITLink/JITLink.h"
+#include "llvm/ExecutionEngine/JITLink/ppc64.h"
+#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
+#include "llvm/ExecutionEngine/Orc/Shared/MemoryFlags.h"
+#include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/XCOFFObjectFile.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/Format.h"
+#include "llvm/Support/raw_ostream.h"
+#include <memory>
+
+using namespace llvm;
+
+#define DEBUG_TYPE "jitlink"
+
+namespace llvm {
+namespace jitlink {
+
+XCOFFLinkGraphBuilder::XCOFFLinkGraphBuilder(
+    const object::XCOFFObjectFile &Obj,
+    std::shared_ptr<orc::SymbolStringPool> SSP, Triple TT,
+    SubtargetFeatures Features,
+    LinkGraph::GetEdgeKindNameFunction GetEdgeKindName)
+    : Obj(Obj),
+      G(std::make_unique<LinkGraph>(
+          std::string(Obj.getFileName()), std::move(SSP), std::move(TT),
+          std::move(Features), std::move(GetEdgeKindName))) {}
+
+static raw_ostream &debugStorageMappingClass(raw_ostream &OS,
+                                             XCOFF::StorageMappingClass XMC) {
+  switch (XMC) {
+  // Read Only Classes
+  case XCOFF::StorageMappingClass::XMC_PR:
+    OS << "XMC_PR (Program Code)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_RO:
+    OS << "XMC_RO (Read Only Constant)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_DB:
+    OS << "XMC_DB (Debug Dictionary Table)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_GL:
+    OS << "XMC_GL (Global Linkage)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_XO:
+    OS << "XMC_XO (Extended Operation)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_SV:
+    OS << "XMC_SV (Supervisor Call 32-bit)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_SV64:
+    OS << "XMC_SV64 (Supervisor Call 64-bit)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_SV3264:
+    OS << "XMC_SV3264 (Supervisor Call 32/64-bit)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_TI:
+    OS << "XMC_TI (Traceback Index)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_TB:
+    OS << "XMC_TB (Traceback Table)";
+    break;
+
+  // Read Write Classes
+  case XCOFF::StorageMappingClass::XMC_RW:
+    OS << "XMC_RW (Read Write Data)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_TC0:
+    OS << "XMC_TC0 (TOC Anchor)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_TC:
+    OS << "XMC_TC (General TOC item)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_TD:
+    OS << "XMC_TD (Scalar TOC Data)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_DS:
+    OS << "XMC_DS (Descriptor)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_UA:
+    OS << "XMC_UA (Unclassified)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_BS:
+    OS << "XMC_BS (BSS)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_UC:
+    OS << "XMC_UC (Unnamed Fortran Common)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_TL:
+    OS << "XMC_TL (Thread-local)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_UL:
+    OS << "XMC_UL (Uninitialized Thread-local)";
+    break;
+  case XCOFF::StorageMappingClass::XMC_TE:
+    OS << "XMC_TE (TOC End Symbol)";
+    break;
+  }
+  return OS;
+}
+
+static llvm::raw_ostream &debugStorageClass(llvm::raw_ostream &OS,
+                                            XCOFF::StorageClass SC) {
+  switch (SC) {
+    // Debug symbols
+  case XCOFF::StorageClass::C_FILE:
+    OS << "C_FILE (File name)";
+    break;
+  case XCOFF::StorageClass::C_BINCL:
+    OS << "C_BINCL (Beginning of include file)";
+    break;
+  case XCOFF::StorageClass::C_EINCL:
+    OS << "C_EINCL (Ending of include file)";
+    break;
+  case XCOFF::StorageClass::C_GSYM:
+    OS << "C_GSYM (Global variable)";
+    break;
+  case XCOFF::StorageClass::C_STSYM:
+    OS << "C_STSYM (Statically allocated symbol)";
+    break;
+  case XCOFF::StorageClass::C_BCOMM:
+    OS << "C_BCOMM (Beginning of common block)";
+    break;
+  case XCOFF::StorageClass::C_ECOMM:
+    OS << "C_ECOMM (End of common block)";
+    break;
+  case XCOFF::StorageClass::C_ENTRY:
+    OS << "C_ENTRY (Alternate entry)";
+    break;
+  case XCOFF::StorageClass::C_BSTAT:
+    OS << "C_BSTAT (Beginning of static block)";
+    break;
+  case XCOFF::StorageClass::C_ESTAT:
+    OS << "C_ESTAT (End of static block)";
+    break;
+  case XCOFF::StorageClass::C_GTLS:
+    OS << "C_GTLS (Global thread-local variable)";
+    break;
+  case XCOFF::StorageClass::C_STTLS:
+    OS << "C_STTLS (Static thread-local variable)";
+    break;
+
+    // DWARF symbols
+  case XCOFF::StorageClass::C_DWARF:
+    OS << "C_DWARF (DWARF section symbol)";
+    break;
+
+    // Absolute symbols
+  case XCOFF::StorageClass::C_LSYM:
+    OS << "C_LSYM (Automatic variable allocated on stack)";
+    break;
+  case XCOFF::StorageClass::C_PSYM:
+    OS << "C_PSYM (Argument to subroutine allocated on stack)";
+    break;
+  case XCOFF::StorageClass::C_RSYM:
+    OS << "C_RSYM (Register variable)";
+    break;
+  case XCOFF::StorageClass::C_RPSYM:
+    OS << "C_RPSYM (Argument to function stored in register)";
+    break;
+  case XCOFF::StorageClass::C_ECOML:
+    OS << "C_ECOML (Local member of common block)";
+    break;
+  case XCOFF::StorageClass::C_FUN:
+    OS << "C_FUN (Function or procedure)";
+    break;
+
+    // External symbols
+  case XCOFF::StorageClass::C_EXT:
+    OS << "C_EXT (External symbol)";
+    break;
+  case XCOFF::StorageClass::C_WEAKEXT:
+    OS << "C_WEAKEXT (Weak external symbol)";
+    break;
+
+    // General sections
+  case XCOFF::StorageClass::C_NULL:
+    OS << "C_NULL";
+    break;
+  case XCOFF::StorageClass::C_STAT:
+    OS << "C_STAT (Static)";
+    break;
+  case XCOFF::StorageClass::C_BLOCK:
+    OS << "C_BLOCK (\".bb\" or \".eb\")";
+    break;
+  case XCOFF::StorageClass::C_FCN:
+    OS << "C_FCN (\".bf\" or \".ef\")";
+    break;
+  case XCOFF::StorageClass::C_HIDEXT:
+    OS << "C_HIDEXT (Un-named external symbol)";
+    break;
+  case XCOFF::StorageClass::C_INFO:
+    OS << "C_INFO (Comment string in .info section)";
+    break;
+  case XCOFF::StorageClass::C_DECL:
+    OS << "C_DECL (Declaration of object)";
+    break;
+
+    // Obsolete/Undocumented
+  case XCOFF::StorageClass::C_AUTO:
+    OS << "C_AUTO (Automatic variable)";
+    break;
+  case XCOFF::StorageClass::C_REG:
+    OS << "C_REG (Register variable)";
+    break;
+  case XCOFF::StorageClass::C_EXTDEF:
+    OS << "C_EXTDEF (External definition)";
+    break;
+  case XCOFF::StorageClass::C_LABEL:
+    OS << "C_LABEL (Label)";
+    break;
+  case XCOFF::StorageClass::C_ULABEL:
+    OS << "C_ULABEL (Undefined label)";
+    break;
+  case XCOFF::StorageClass::C_MOS:
+    OS << "C_MOS (Member of structure)";
+    break;
+  case XCOFF::StorageClass::C_ARG:
+    OS << "C_ARG (Function argument)";
+    break;
+  case XCOFF::StorageClass::C_STRTAG:
+    OS << "C_STRTAG (Structure tag)";
+    break;
+  case XCOFF::StorageClass::C_MOU:
+    OS << "C_MOU (Member of union)";
+    break;
+  case XCOFF::StorageClass::C_UNTAG:
+    OS << "C_UNTAG (Union tag)";
+    break;
+  case XCOFF::StorageClass::C_TPDEF:
+    OS << "C_TPDEF (Type definition)";
+    break;
+  case XCOFF::StorageClass::C_USTATIC:
+    OS << "C_USTATIC (Undefined static)";
+    break;
+  case XCOFF::StorageClass::C_ENTAG:
+    OS << "C_ENTAG (Enumeration tag)";
+    break;
+  case XCOFF::StorageClass::C_MOE:
+    OS << "C_MOE (Member of enumeration)";
+    break;
+  case XCOFF::StorageClass::C_REGPARM:
+    OS << "C_REGPARM (Register parameter)";
+    break;
+  case XCOFF::StorageClass::C_FIELD:
+    OS << "C_FIELD (Bit field)";
+    break;
+  case XCOFF::StorageClass::C_EOS:
+    OS << "C_EOS (End of structure)";
+    break;
+  case XCOFF::StorageClass::C_LINE:
+    OS << "C_LINE";
+    break;
+  case XCOFF::StorageClass::C_ALIAS:
+    OS << "C_ALIAS (Duplicate tag)";
+    break;
+  case XCOFF::StorageClass::C_HIDDEN:
+    OS << "C_HIDDEN (Special storage class for external)";
+    break;
+  case XCOFF::StorageClass::C_EFCN:
+    OS << "C_EFCN (Physical end of function)";
+    break;
+
+    // Reserved
+  case XCOFF::StorageClass::C_TCSYM:
+    OS << "C_TCSYM (Reserved)";
+    break;
+  }
+  return OS;
+}
+
+static raw_ostream &debugSymbolType(llvm::raw_ostream &OS,
+                                    XCOFF::SymbolType Type) {
+  switch (Type) {
+  case XCOFF::SymbolType::XTY_ER:
+    OS << "XTY_ER (External reference)";
+    break;
+  case XCOFF::SymbolType::XTY_SD:
+    OS << "XTY_SD (Csect definition - initialized storage)";
+    break;
+  case XCOFF::SymbolType::XTY_LD:
+    OS << "XTY_LD (Label definition)";
+    break;
+  case XCOFF::SymbolType::XTY_CM:
+    OS << "XTY_CM (Common csect definition - uninitialized)";
+    break;
+  }
+  return OS;
+}
+
+static raw_ostream &debugVisibilityType(llvm::raw_ostream &OS,
+                                        XCOFF::VisibilityType Vis) {
+  switch (Vis) {
+  case XCOFF::VisibilityType::SYM_V_UNSPECIFIED:
+    OS << "SYM_V_UNSPECIFIED";
+    break;
+  case XCOFF::VisibilityType::SYM_V_INTERNAL:
+    OS << "SYM_V_INTERNAL";
+    break;
+  case XCOFF::VisibilityType::SYM_V_HIDDEN:
+    OS << "SYM_V_HIDDEN";
+    break;
+  case XCOFF::VisibilityType::SYM_V_PROTECTED:
+    OS << "SYM_V_PROTECTED";
+    break;
+  case XCOFF::VisibilityType::SYM_V_EXPORTED:
+    OS << "SYM_V_EXPORTED";
+    break;
+  }
+  return OS;
+}
+
+Error XCOFFLinkGraphBuilder::processSections() {
+  LLVM_DEBUG(dbgs() << "  Creating graph sections...\n");
+
+  // Create undefined section to contain all external symbols
+  UndefSection = &G->createSection("*UND*", orc::MemProt::None);
+
+  for (auto Section : Obj.sections()) {
+    LLVM_DEBUG({
+      dbgs() << "    section = " << cantFail(Section.getName())
+             << ", idx = " << Section.getIndex()
+             << ", size = " << format_hex_no_prefix(Section.getSize(), 8)
+             << ", vma = " << format_hex(Section.getAddress(), 16) << "\n";
+    });
+
+    // We can skip debug (including dawrf) and pad sections
+    if (Section.isDebugSection() || cantFail(Section.getName()) == "pad") {
+      LLVM_DEBUG(dbgs() << "      skipping...\n");
+      continue;
+    }
+
+    auto SectionName = cantFail(Section.getName());
+    LLVM_DEBUG(dbgs() << "        creating graph section...\n");
+
+    orc::MemProt Prot = orc::MemProt::Read;
+    if (Section.isText())
+      Prot |= orc::MemProt::Exec;
+    if (Section.isData() || Section.isBSS())
+      Prot |= orc::MemProt::Write;
+
+    auto *GraphSec = &G->createSection(SectionName, Prot);
+    // TODO(HJ): Check for memory lifetime no alloc for certain sections
+
+    SectionDataMap[Section.getIndex()] = Section;
+    SectionMap[Section.getIndex()] = GraphSec;
+  }
+
+  return Error::success();
+}
+
+static std::optional<object::XCOFFSymbolRef>
+getXCOFFSymbolContainingSymbolRef(const object::XCOFFObjectFile &Obj,
+                                  const object::SymbolRef &Sym) {
+  const object::XCOFFSymbolRef SymRef =
+      Obj.toSymbolRef(Sym.getRawDataRefImpl());
+  if (!SymRef.isCsectSymbol())
+    return std::nullopt;
+
+  Expected<object::XCOFFCsectAuxRef> CsectAuxEntOrErr =
+      SymRef.getXCOFFCsectAuxRef();
+  if (!CsectAuxEntOrErr || !CsectAuxEntOrErr.get().isLabel())
+    return std::nullopt;
+  uint32_t Idx =
+      static_cast<uint32_t>(CsectAuxEntOrErr.get().getSectionOrLength());
+  object::DataRefImpl DRI;
+  DRI.p = Obj.getSymbolByIndex(Idx);
+  return object::XCOFFSymbolRef(DRI, &Obj);
+}
+
+static void printSymbolEntry(raw_ostream &OS,
+                             const object::XCOFFObjectFile &Obj,
+                             const object::XCOFFSymbolRef &Sym) {
+  OS << "    " << format_hex(cantFail(Sym.getAddress()), 16);
+  OS << " " << left_justify(cantFail(Sym.getName()), 10);
+  if (Sym.isCsectSymbol()) {
+    auto CsectAuxEntry = cantFail(Sym.getXCOFFCsectAuxRef());
+    if (!CsectAuxEntry.isLabel()) {
+      std::string MCStr =
+          "[" +
+          XCOFF::getMappingClassString(CsectAuxEntry.getStorageMappingClass())
+              .str() +
+          "]";
+      OS << left_justify(MCStr, 3);
+    }
+  }
+  OS << " " << format_hex(Sym.getSize(), 8);
+  OS << " " << Sym.getSectionNumber();
+  OS << " ";
+  debugStorageClass(dbgs(), Sym.getStorageClass());
+  OS << " (idx: " << Obj.getSymbolIndex(Sym.getRawDataRefImpl().p) << ")";
+  if (Sym.isCsectSymbol()) {
+    if (auto ParentSym = getXCOFFSymbolContainingSymbolRef(Obj, Sym)) {
+      OS << " (csect idx: "
+         << Obj.getSymbolIndex(ParentSym->getRawDataRefImpl().p) << ")";
+    }
+  }
+  OS << "\n";
+}
+
+Error XCOFFLinkGraphBuilder::processCsectsAndSymbols() {
+  LLVM_DEBUG(dbgs() << "  Creating graph blocks and symbols...\n");
+
+  for (auto [K, V] : SectionMap) {
+    LLVM_DEBUG(dbgs() << "    section entry(idx: " << K
+                      << " section: " << V->getName() << ")\n");
+  }
+
+  for (object::XCOFFSymbolRef Symbol : Obj.symbols()) {
+    LLVM_DEBUG({ printSymbolEntry(dbgs(), Obj, Symbol); });
+
+    auto Flags = cantFail(Symbol.getFlags());
+    bool External = Flags & object::SymbolRef::SF_Undefined;
+    bool Weak = Flags & object::SymbolRef::SF_Weak;
+    bool Hidden = Flags & object::SymbolRef::SF_Hidden;
+    bool Exported = Flags & object::SymbolRef::SF_Exported;
+    bool Absolute = Flags & object::SymbolRef::SF_Absolute;
+    bool Global = Flags & object::SymbolRef::SF_Global;
+
+    auto SymbolIndex = Obj.getSymbolIndex(Symbol.getEntryAddress());
+
+    if (External) {
+      LLVM_DEBUG(dbgs() << "      created external symbol\n");
+      SymbolIdxMap[SymbolIndex] = &G->addExternalSymbol(
+          cantFail(Symbol.getName()), Symbol.getSize(), Weak);
+      continue;
+    }
+
+    if (!Symbol.isCsectSymbol()) {
+      LLVM_DEBUG(dbgs() << "      skipped: not a csect symbol\n");
+      continue;
+    }
+
+    auto ParentSym = getXCOFFSymbolContainingSymbolRef(Obj, Symbol);
+    object::XCOFFSymbolRef CsectSymbol = ParentSym ? *ParentSym : Symbol;
+
+    auto CsectSymbolIndex = Obj.getSymbolIndex(CsectSymbol.getEntryAddress());
+    auto ParentSectionNumber = CsectSymbol.getSectionNumber();
+
+    bool IsUndefinedSection = !SectionMap.contains(ParentSectionNumber);
+    Section *ParentSection =
+        !IsUndefinedSection ? SectionMap[ParentSectionNumber] : UndefSection;
+    Block *B = nullptr;
+
+    if (!CsectMap.contains(CsectSymbolIndex) && !IsUndefinedSection) {
+      auto SectionRef = SectionDataMap[ParentSectionNumber];
+      auto Data = SectionRef.getContents();
+      if (!Data)
+        return Data.takeError();
+      ArrayRef<char> SectionBuffer{Data->data(), Data->size()};
+      auto Offset =
+          cantFail(CsectSymbol.getAddress()) - SectionRef.getAddress();
+
+      LLVM_DEBUG({
+        dbgs() << "      symbol entry: offset = " << Offset
+               << ", size = " << CsectSymbol.getSize() << ", storage class = ";
+        debugStorageClass(dbgs(), CsectSymbol.getStorageClass()) << "\n";
+      });
+
+      B = &G->createContentBlock(
+          *ParentSection, SectionBuffer.slice(Offset, CsectSymbol.getSize()),
+          orc::ExecutorAddr(cantFail(CsectSymbol.getAddress())),
+          CsectSymbol.getAlignment(), 0);
+
+      CsectMap[CsectSymbolIndex] = B;
+    } else {
+      B = CsectMap[CsectSymbolIndex];
+    }
+
+    Scope S{Scope::Default};
+    if (Hidden)
+      S = Scope::Hidden;
+    // TODO(HJ): Got this from llvm-objdump.cpp:2938 not sure if its correct
+    if (!Weak) {
+      if (Global)
+        S = Scope::Default;
+      else
+        S = Scope::Local;
+    }
+
+    // TODO(HJ): not sure what is Scope::SideEffectsOnly
+    Linkage L = Weak ? Linkage::Weak : Linkage::Strong;
+    auto BlockOffset =
+        cantFail(Symbol.getAddress()) - B->getAddress().getValue();
+
+    LLVM_DEBUG(dbgs() << "      creating with linkage = " << getLinkageName(L)
+                      << ", scope = " << getScopeName(S) << ", B = "
+                      << format_hex(B->getAddress().getValue(), 16) << "\n");
+
+    SymbolIdxMap[SymbolIndex] = &G->addDefinedSymbol(
+        *B, BlockOffset, cantFail(Symbol.getName()), Symbol.getSize(), L, S,
+        cantFail(Symbol.isFunction()), true);
+  }
+
+  return Error::success();
+}
+
+Error XCOFFLinkGraphBuilder::processRelocations() {
+  LLVM_DEBUG(dbgs() << "  Creating relocations...\n");
+
+  for (object::SectionRef Section : Obj.sections()) {
+
+    LLVM_DEBUG(dbgs() << "    Relocations for section "
+                      << cantFail(Section.getName()) << ":\n");
+    for (object::RelocationRef Relocation : Section.relocations()) {
+      SmallString<16> RelocName;
+      Relocation.getTypeName(RelocName);
+      object::SymbolRef Symbol = *Relocation.getSymbol();
+      StringRef TargetSymbol = cantFail(Symbol.getName());
+      auto SymbolIndex = Obj.getSymbolIndex(Symbol.getRawDataRefImpl().p);
+
+      LLVM_DEBUG(dbgs() << "      " << format_hex(Relocation.getOffset(), 16)
+                        << " (idx: " << SymbolIndex << ")"
+                        << " " << RelocName << " " << TargetSymbol << "\n";);
+
+      assert(SymbolIdxMap.contains(SymbolIndex) &&
+             "Relocation needs a record in the symbol table");
+      auto *S = SymbolIdxMap[SymbolIndex];
+      auto It = find_if(G->blocks(),
+                        [Target = orc::ExecutorAddr(Section.getAddress() +
+                                                    Relocation.getOffset())](
+                            const Block *B) -> bool {
+                          return B->getRange().contains(Target);
+                        });
+      assert(It != G->blocks().end() &&
+             "Cannot find the target relocation block");
+      Block *B = *It;
+      LLVM_DEBUG(dbgs() << "        found target relocation block: "
+                        << format_hex(B->getAddress().getValue(), 16) << "\n");
+
+      // TODO(HJ): correctly map edge kind and figure out if we need addend
+      auto TargetBlockOffset = S->getAddress() - B->getAddress();
+      switch (Relocation.getType()) {
+      case XCOFF::R_POS:
+        B->addEdge(ppc64::EdgeKind_ppc64::Pointer64, TargetBlockOffset, *S, 0);
+        break;
+      case XCOFF::R_TOC:
+        B->addEdge(ppc64::EdgeKind_ppc64::TOC, TargetBlockOffset, *S, 0);
+        break;
+      default:
+        SmallString<16> RelocType;
+        Relocation.getTypeName(RelocType);
+        return make_error<StringError>("Unsupported Relocation Type: " +
+                                       RelocType, std::error_code());
+      }
+    }
+  }
+
+  return Error::success();
+}
+
+Expected<std::unique_ptr<LinkGraph>> XCOFFLinkGraphBuilder::buildGraph() {
+  LLVM_DEBUG(dbgs() << "Building XCOFFLinkGraph...\n");
+
+  // TODO(HJ): Check for for relocation
+  // if (Obj.isRelocatableObject())
+  //   return make_error<JITLinkError>("XCOFF object is not relocatable");
+
+  if (auto Err = processSections())
+    return Err;
+  if (auto Err = processCsectsAndSymbols())
+    return Err;
+  if (auto Err = processRelocations())
+    return Err;
+
+  outs() << "\n  Ouput of LinkGraph: \n\n";
+  outs() << "==================================\n";
+  G->dump(outs());
+  outs() << "==================================\n";
+
+  return std::move(G);
+}
+
+} // namespace jitlink
+} // namespace llvm
diff --git a/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.h b/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.h
new file mode 100644
index 0000000000000..a2bbd6ce99f3c
--- /dev/null
+++ b/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.h
@@ -0,0 +1,59 @@
+//===----- XCOFFLinkGraphBuilder.h - XCOFF LinkGraph builder ----*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Generic XCOFF LinkGraph building code.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LIB_EXECUTIONENGINE_JITLINK_XCOFFLINKGRAPHBUILDER_H
+#define LIB_EXECUTIONENGINE_JITLINK_XCOFFLINKGRAPHBUILDER_H
+
+#include "llvm/ExecutionEngine/JITLink/JITLink.h"
+#include "llvm/ExecutionEngine/Orc/SymbolStringPool.h"
+#include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/XCOFFObjectFile.h"
+#include "llvm/TargetParser/SubtargetFeature.h"
+#include <memory>
+
+namespace llvm {
+namespace jitlink {
+
+class XCOFFLinkGraphBuilder {
+public:
+  virtual ~XCOFFLinkGraphBuilder() = default;
+  Expected<std::unique_ptr<LinkGraph>> buildGraph();
+
+public:
+  XCOFFLinkGraphBuilder(const object::XCOFFObjectFile &Obj,
+                        std::shared_ptr<orc::SymbolStringPool> SSP, Triple TT,
+                        SubtargetFeatures Features,
+                        LinkGraph::GetEdgeKindNameFunction GetEdgeKindName);
+  LinkGraph &getGraph() const { return *G; }
+  const object::XCOFFObjectFile &getObject() const { return Obj; }
+
+private:
+  Error processSections();
+  Error processCsectsAndSymbols();
+  Error processRelocations();
+
+private:
+  const object::XCOFFObjectFile &Obj;
+  std::unique_ptr<LinkGraph> G;
+
+  Section* UndefSection;
+
+  DenseMap<uint16_t, Section *> SectionMap;
+  DenseMap<uint16_t, object::SectionRef> SectionDataMap;
+  DenseMap<uint32_t, Block *> CsectMap;
+  DenseMap<uint32_t, Symbol *> SymbolIdxMap;
+};
+
+} // namespace jitlink
+} // namespace llvm
+
+#endif // LIB_EXECUTIONENGINE_JITLINK_XCOFFLINKGRAPHBUILDER_H
diff --git a/llvm/lib/ExecutionEngine/JITLink/XCOFF_ppc64.cpp b/llvm/lib/ExecutionEngine/JITLink/XCOFF_ppc64.cpp
index 81f35ca0a5a2a..23aedb955dcd5 100644
--- a/llvm/lib/ExecutionEngine/JITLink/XCOFF_ppc64.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/XCOFF_ppc64.cpp
@@ -12,7 +12,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ExecutionEngine/JITLink/XCOFF_ppc64.h"
+#include "XCOFFLinkGraphBuilder.h"
+#include "llvm/ExecutionEngine/JITLink/ppc64.h"
 #include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/XCOFFObjectFile.h"
+#include "llvm/Support/Error.h"
+#include <system_error>
 
 using namespace llvm;
 
@@ -41,12 +46,16 @@ Expected<std::unique_ptr<LinkGraph>> createLinkGraphFromXCOFFObject_ppc64(
     (*Features).print(dbgs());
   });
 
-  llvm_unreachable("Graph builder not implemented for XCOFF yet");
+  return XCOFFLinkGraphBuilder(cast<object::XCOFFObjectFile>(**Obj),
+                               std::move(SSP), Triple("powerpc64-ibm-aix"),
+                               std::move(*Features), ppc64::getEdgeKindName)
+      .buildGraph();
 }
 
 void link_XCOFF_ppc64(std::unique_ptr<LinkGraph> G,
                       std::unique_ptr<JITLinkContext> Ctx) {
-  llvm_unreachable("Link implemented for XCOFF yet");
+  Ctx->notifyFailed(make_error<StringError>(
+      "link_XCOFF_ppc64 is not implemented", std::error_code()));
 }
 
 } // namespace jitlink
diff --git a/llvm/lib/Object/XCOFFObjectFile.cpp b/llvm/lib/Object/XCOFFObjectFile.cpp
index df3b2a091aec2..881b53dad8d10 100644
--- a/llvm/lib/Object/XCOFFObjectFile.cpp
+++ b/llvm/lib/Object/XCOFFObjectFile.cpp
@@ -201,14 +201,12 @@ template <typename T> const T *XCOFFObjectFile::sectionHeaderTable() const {
   return static_cast<const T *>(SectionHeaderTable);
 }
 
-const XCOFFSectionHeader32 *
-XCOFFObjectFile::sectionHeaderTable32() const {
+const XCOFFSectionHeader32 *XCOFFObjectFile::sectionHeaderTable32() const {
   assert(!is64Bit() && "32-bit interface called on 64-bit object file.");
   return static_cast<const XCOFFSectionHeader32 *>(SectionHeaderTable);
 }
 
-const XCOFFSectionHeader64 *
-XCOFFObjectFile::sectionHeaderTable64() const {
+const XCOFFSectionHeader64 *XCOFFObjectFile::sectionHeaderTable64() const {
   assert(is64Bit() && "64-bit interface called on a 32-bit object file.");
   return static_cast<const XCOFFSectionHeader64 *>(SectionHeaderTable);
 }
@@ -416,7 +414,7 @@ XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const {
   else
     OffsetToRaw = toSection32(Sec)->FileOffsetToRawData;
 
-  const uint8_t * ContentStart = base() + OffsetToRaw;
+  const uint8_t *ContentStart = base() + OffsetToRaw;
   uint64_t SectionSize = getSectionSize(Sec);
   if (Error E = Binary::checkOffset(
           Data, reinterpret_cast<uintptr_t>(ContentStart), SectionSize))
@@ -429,9 +427,14 @@ XCOFFObjectFile::getSectionContents(DataRefImpl Sec) const {
 }
 
 uint64_t XCOFFObjectFile::getSectionAlignment(DataRefImpl Sec) const {
-  uint64_t Result = 0;
-  llvm_unreachable("Not yet implemented!");
-  return Result;
+  // SCUI - Copied from MC/XCOFFObjectWriter.cpp
+  // Sections other than DWARF section use DefaultSectionAlign as the default
+  // alignment, while DWARF sections have their own alignments. DWARF section
+  // alignment is bigger than DefaultSectionAlign.
+  if (isDebugSection(Sec)) {
+    return 8; // SCUI - just a number for now.
+  }
+  return 4;
 }
 
 uint64_t XCOFFObjectFile::getSectionFileOffsetToRawData(DataRefImpl Sec) const {
@@ -766,8 +769,8 @@ size_t XCOFFObjectFile::getFileHeaderSize() const {
 }
 
 size_t XCOFFObjectFile::getSectionHeaderSize() const {
-  return is64Bit() ? sizeof(XCOFFSectionHeader64) :
-                     sizeof(XCOFFSectionHeader32);
+  return is64Bit() ? sizeof(XCOFFSectionHeader64)
+                   : sizeof(XCOFFSectionHeader32);
 }
 
 bool XCOFFObjectFile::is64Bit() const {

>From fa0a0f1b3bb771adb962170727b6694154e2e5fa Mon Sep 17 00:00:00 2001
From: Henry Jiang <henry.jiang1 at ibm.com>
Date: Wed, 19 Feb 2025 14:27:33 -0500
Subject: [PATCH 3/5] start on the jitlinker

---
 llvm/include/llvm/ExecutionEngine/Orc/Core.h  |  65 +++++----
 .../JITLink/XCOFFLinkGraphBuilder.cpp         | 123 +----------------
 .../ExecutionEngine/JITLink/XCOFF_ppc64.cpp   |  62 ++++++++-
 llvm/lib/ExecutionEngine/Orc/Core.cpp         | 129 ++++++++++--------
 .../ExecutionEngine/Orc/LinkGraphLayer.cpp    |   3 +
 5 files changed, 172 insertions(+), 210 deletions(-)

diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Core.h b/llvm/include/llvm/ExecutionEngine/Orc/Core.h
index cecb4094c9a57..49fd0599c4336 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/Core.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/Core.h
@@ -308,10 +308,11 @@ class SymbolLookupSet {
   /// If Body returns true then the element just passed in is removed from the
   /// set. If Body returns false then the element is retained.
   template <typename BodyFn>
-  auto forEachWithRemoval(BodyFn &&Body) -> std::enable_if_t<
-      std::is_same<decltype(Body(std::declval<const SymbolStringPtr &>(),
-                                 std::declval<SymbolLookupFlags>())),
-                   bool>::value> {
+  auto forEachWithRemoval(BodyFn &&Body)
+      -> std::enable_if_t<
+          std::is_same<decltype(Body(std::declval<const SymbolStringPtr &>(),
+                                     std::declval<SymbolLookupFlags>())),
+                       bool>::value> {
     UnderlyingVector::size_type I = 0;
     while (I != Symbols.size()) {
       const auto &Name = Symbols[I].first;
@@ -330,11 +331,12 @@ class SymbolLookupSet {
   /// returns true then the element just passed in is removed from the set. If
   /// Body returns false then the element is retained.
   template <typename BodyFn>
-  auto forEachWithRemoval(BodyFn &&Body) -> std::enable_if_t<
-      std::is_same<decltype(Body(std::declval<const SymbolStringPtr &>(),
-                                 std::declval<SymbolLookupFlags>())),
-                   Expected<bool>>::value,
-      Error> {
+  auto forEachWithRemoval(BodyFn &&Body)
+      -> std::enable_if_t<
+          std::is_same<decltype(Body(std::declval<const SymbolStringPtr &>(),
+                                     std::declval<SymbolLookupFlags>())),
+                       Expected<bool>>::value,
+          Error> {
     UnderlyingVector::size_type I = 0;
     while (I != Symbols.size()) {
       const auto &Name = Symbols[I].first;
@@ -525,6 +527,7 @@ class MissingSymbolDefinitions : public ErrorInfo<MissingSymbolDefinitions> {
   std::shared_ptr<SymbolStringPool> getSymbolStringPool() { return SSP; }
   const std::string &getModuleName() const { return ModuleName; }
   const SymbolNameVector &getSymbols() const { return Symbols; }
+
 private:
   std::shared_ptr<SymbolStringPool> SSP;
   std::string ModuleName;
@@ -535,7 +538,8 @@ class MissingSymbolDefinitions : public ErrorInfo<MissingSymbolDefinitions> {
 /// symbols that are not claimed by the module's associated
 /// MaterializationResponsibility. If this error is returned it is indicative of
 /// a broken transformation / compiler / object cache.
-class UnexpectedSymbolDefinitions : public ErrorInfo<UnexpectedSymbolDefinitions> {
+class UnexpectedSymbolDefinitions
+    : public ErrorInfo<UnexpectedSymbolDefinitions> {
 public:
   static char ID;
 
@@ -548,6 +552,7 @@ class UnexpectedSymbolDefinitions : public ErrorInfo<UnexpectedSymbolDefinitions
   std::shared_ptr<SymbolStringPool> getSymbolStringPool() { return SSP; }
   const std::string &getModuleName() const { return ModuleName; }
   const SymbolNameVector &getSymbols() const { return Symbols; }
+
 private:
   std::shared_ptr<SymbolStringPool> SSP;
   std::string ModuleName;
@@ -693,6 +698,10 @@ class MaterializationResponsibility {
       : JD(RT->getJITDylib()), RT(std::move(RT)),
         SymbolFlags(std::move(SymbolFlags)), InitSymbol(std::move(InitSymbol)) {
     assert(!this->SymbolFlags.empty() && "Materializing nothing?");
+    for (auto &KV : this->SymbolFlags) {
+      dbgs() << "@@@ Init MR " << KV.first << " "
+             << format_hex(KV.second.getRawFlagsValue(), 8) << "\n";
+    }
   }
 
   JITDylib &JD;
@@ -800,7 +809,6 @@ class AsynchronousSymbolQuery {
   ///        resolved.
   bool isComplete() const { return OutstandingSymbolsCount == 0; }
 
-
 private:
   void handleComplete(ExecutionSession &ES);
 
@@ -899,8 +907,8 @@ class JITDylib : public ThreadSafeRefCountedBase<JITDylib>,
   friend class ExecutionSession;
   friend class Platform;
   friend class MaterializationResponsibility;
-public:
 
+public:
   JITDylib(const JITDylib &) = delete;
   JITDylib &operator=(const JITDylib &) = delete;
   JITDylib(JITDylib &&) = delete;
@@ -1104,7 +1112,7 @@ class JITDylib : public ThreadSafeRefCountedBase<JITDylib>,
 
 private:
   using AsynchronousSymbolQuerySet =
-    std::set<std::shared_ptr<AsynchronousSymbolQuery>>;
+      std::set<std::shared_ptr<AsynchronousSymbolQuery>>;
 
   using AsynchronousSymbolQueryList =
       std::vector<std::shared_ptr<AsynchronousSymbolQuery>>;
@@ -1160,6 +1168,7 @@ class JITDylib : public ThreadSafeRefCountedBase<JITDylib>,
     const AsynchronousSymbolQueryList &pendingQueries() const {
       return PendingQueries;
     }
+
   private:
     AsynchronousSymbolQueryList PendingQueries;
   };
@@ -1355,13 +1364,13 @@ class ExecutionSession {
   using ErrorReporter = unique_function<void(Error)>;
 
   /// Send a result to the remote.
-  using SendResultFunction = unique_function<void(shared::WrapperFunctionResult)>;
+  using SendResultFunction =
+      unique_function<void(shared::WrapperFunctionResult)>;
 
   /// An asynchronous wrapper-function callable from the executor via
   /// jit-dispatch.
   using JITDispatchHandlerFunction = unique_function<void(
-      SendResultFunction SendResult,
-      const char *ArgData, size_t ArgSize)>;
+      SendResultFunction SendResult, const char *ArgData, size_t ArgSize)>;
 
   /// A map associating tag names with asynchronous wrapper function
   /// implementations in the JIT.
@@ -1589,8 +1598,7 @@ class ExecutionSession {
   /// \endcode{.cpp}
   ///
   /// The given OnComplete function will be called to return the result.
-  template <typename... ArgTs>
-  void callWrapperAsync(ArgTs &&... Args) {
+  template <typename... ArgTs> void callWrapperAsync(ArgTs &&...Args) {
     EPC->callWrapperAsync(std::forward<ArgTs>(Args)...);
   }
 
@@ -1635,9 +1643,9 @@ class ExecutionSession {
   /// (using registerJITDispatchHandler) and called from the executor.
   template <typename SPSSignature, typename HandlerT>
   static JITDispatchHandlerFunction wrapAsyncWithSPS(HandlerT &&H) {
-    return [H = std::forward<HandlerT>(H)](
-               SendResultFunction SendResult,
-               const char *ArgData, size_t ArgSize) mutable {
+    return [H = std::forward<HandlerT>(H)](SendResultFunction SendResult,
+                                           const char *ArgData,
+                                           size_t ArgSize) mutable {
       shared::WrapperFunction<SPSSignature>::handleAsync(ArgData, ArgSize, H,
                                                          std::move(SendResult));
     };
@@ -1742,8 +1750,8 @@ class ExecutionSession {
       unique_function<void(Expected<SymbolFlagsMap>)> OnComplete);
 
   // State machine functions for MaterializationResponsibility.
-  void OL_destroyMaterializationResponsibility(
-      MaterializationResponsibility &MR);
+  void
+  OL_destroyMaterializationResponsibility(MaterializationResponsibility &MR);
   SymbolNameSet OL_getRequestedSymbols(const MaterializationResponsibility &MR);
   Error OL_notifyResolved(MaterializationResponsibility &MR,
                           const SymbolMap &Symbols);
@@ -1965,12 +1973,13 @@ inline MaterializationResponsibility::~MaterializationResponsibility() {
   getExecutionSession().OL_destroyMaterializationResponsibility(*this);
 }
 
-inline SymbolNameSet MaterializationResponsibility::getRequestedSymbols() const {
+inline SymbolNameSet
+MaterializationResponsibility::getRequestedSymbols() const {
   return getExecutionSession().OL_getRequestedSymbols(*this);
 }
 
-inline Error MaterializationResponsibility::notifyResolved(
-    const SymbolMap &Symbols) {
+inline Error
+MaterializationResponsibility::notifyResolved(const SymbolMap &Symbols) {
   return getExecutionSession().OL_notifyResolved(*this, Symbols);
 }
 
@@ -1979,8 +1988,8 @@ inline Error MaterializationResponsibility::notifyEmitted(
   return getExecutionSession().OL_notifyEmitted(*this, EmittedDeps);
 }
 
-inline Error MaterializationResponsibility::defineMaterializing(
-    SymbolFlagsMap SymbolFlags) {
+inline Error
+MaterializationResponsibility::defineMaterializing(SymbolFlagsMap SymbolFlags) {
   return getExecutionSession().OL_defineMaterializing(*this,
                                                       std::move(SymbolFlags));
 }
diff --git a/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp b/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp
index f3de6e479c6b4..08d2637b6fefd 100644
--- a/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp
@@ -40,79 +40,6 @@ XCOFFLinkGraphBuilder::XCOFFLinkGraphBuilder(
           std::string(Obj.getFileName()), std::move(SSP), std::move(TT),
           std::move(Features), std::move(GetEdgeKindName))) {}
 
-static raw_ostream &debugStorageMappingClass(raw_ostream &OS,
-                                             XCOFF::StorageMappingClass XMC) {
-  switch (XMC) {
-  // Read Only Classes
-  case XCOFF::StorageMappingClass::XMC_PR:
-    OS << "XMC_PR (Program Code)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_RO:
-    OS << "XMC_RO (Read Only Constant)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_DB:
-    OS << "XMC_DB (Debug Dictionary Table)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_GL:
-    OS << "XMC_GL (Global Linkage)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_XO:
-    OS << "XMC_XO (Extended Operation)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_SV:
-    OS << "XMC_SV (Supervisor Call 32-bit)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_SV64:
-    OS << "XMC_SV64 (Supervisor Call 64-bit)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_SV3264:
-    OS << "XMC_SV3264 (Supervisor Call 32/64-bit)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_TI:
-    OS << "XMC_TI (Traceback Index)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_TB:
-    OS << "XMC_TB (Traceback Table)";
-    break;
-
-  // Read Write Classes
-  case XCOFF::StorageMappingClass::XMC_RW:
-    OS << "XMC_RW (Read Write Data)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_TC0:
-    OS << "XMC_TC0 (TOC Anchor)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_TC:
-    OS << "XMC_TC (General TOC item)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_TD:
-    OS << "XMC_TD (Scalar TOC Data)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_DS:
-    OS << "XMC_DS (Descriptor)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_UA:
-    OS << "XMC_UA (Unclassified)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_BS:
-    OS << "XMC_BS (BSS)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_UC:
-    OS << "XMC_UC (Unnamed Fortran Common)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_TL:
-    OS << "XMC_TL (Thread-local)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_UL:
-    OS << "XMC_UL (Uninitialized Thread-local)";
-    break;
-  case XCOFF::StorageMappingClass::XMC_TE:
-    OS << "XMC_TE (TOC End Symbol)";
-    break;
-  }
-  return OS;
-}
-
 static llvm::raw_ostream &debugStorageClass(llvm::raw_ostream &OS,
                                             XCOFF::StorageClass SC) {
   switch (SC) {
@@ -283,47 +210,6 @@ static llvm::raw_ostream &debugStorageClass(llvm::raw_ostream &OS,
   return OS;
 }
 
-static raw_ostream &debugSymbolType(llvm::raw_ostream &OS,
-                                    XCOFF::SymbolType Type) {
-  switch (Type) {
-  case XCOFF::SymbolType::XTY_ER:
-    OS << "XTY_ER (External reference)";
-    break;
-  case XCOFF::SymbolType::XTY_SD:
-    OS << "XTY_SD (Csect definition - initialized storage)";
-    break;
-  case XCOFF::SymbolType::XTY_LD:
-    OS << "XTY_LD (Label definition)";
-    break;
-  case XCOFF::SymbolType::XTY_CM:
-    OS << "XTY_CM (Common csect definition - uninitialized)";
-    break;
-  }
-  return OS;
-}
-
-static raw_ostream &debugVisibilityType(llvm::raw_ostream &OS,
-                                        XCOFF::VisibilityType Vis) {
-  switch (Vis) {
-  case XCOFF::VisibilityType::SYM_V_UNSPECIFIED:
-    OS << "SYM_V_UNSPECIFIED";
-    break;
-  case XCOFF::VisibilityType::SYM_V_INTERNAL:
-    OS << "SYM_V_INTERNAL";
-    break;
-  case XCOFF::VisibilityType::SYM_V_HIDDEN:
-    OS << "SYM_V_HIDDEN";
-    break;
-  case XCOFF::VisibilityType::SYM_V_PROTECTED:
-    OS << "SYM_V_PROTECTED";
-    break;
-  case XCOFF::VisibilityType::SYM_V_EXPORTED:
-    OS << "SYM_V_EXPORTED";
-    break;
-  }
-  return OS;
-}
-
 Error XCOFFLinkGraphBuilder::processSections() {
   LLVM_DEBUG(dbgs() << "  Creating graph sections...\n");
 
@@ -491,6 +377,7 @@ Error XCOFFLinkGraphBuilder::processCsectsAndSymbols() {
       else
         S = Scope::Local;
     }
+    
 
     // TODO(HJ): not sure what is Scope::SideEffectsOnly
     Linkage L = Weak ? Linkage::Weak : Linkage::Strong;
@@ -548,9 +435,6 @@ Error XCOFFLinkGraphBuilder::processRelocations() {
       case XCOFF::R_POS:
         B->addEdge(ppc64::EdgeKind_ppc64::Pointer64, TargetBlockOffset, *S, 0);
         break;
-      case XCOFF::R_TOC:
-        B->addEdge(ppc64::EdgeKind_ppc64::TOC, TargetBlockOffset, *S, 0);
-        break;
       default:
         SmallString<16> RelocType;
         Relocation.getTypeName(RelocType);
@@ -577,11 +461,6 @@ Expected<std::unique_ptr<LinkGraph>> XCOFFLinkGraphBuilder::buildGraph() {
   if (auto Err = processRelocations())
     return Err;
 
-  outs() << "\n  Ouput of LinkGraph: \n\n";
-  outs() << "==================================\n";
-  G->dump(outs());
-  outs() << "==================================\n";
-
   return std::move(G);
 }
 
diff --git a/llvm/lib/ExecutionEngine/JITLink/XCOFF_ppc64.cpp b/llvm/lib/ExecutionEngine/JITLink/XCOFF_ppc64.cpp
index 23aedb955dcd5..22aa6c020416e 100644
--- a/llvm/lib/ExecutionEngine/JITLink/XCOFF_ppc64.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/XCOFF_ppc64.cpp
@@ -12,11 +12,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ExecutionEngine/JITLink/XCOFF_ppc64.h"
+#include "JITLinkGeneric.h"
 #include "XCOFFLinkGraphBuilder.h"
+#include "llvm/ADT/bit.h"
+#include "llvm/ExecutionEngine/JITLink/JITLink.h"
 #include "llvm/ExecutionEngine/JITLink/ppc64.h"
 #include "llvm/Object/ObjectFile.h"
 #include "llvm/Object/XCOFFObjectFile.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/ErrorHandling.h"
 #include <system_error>
 
 using namespace llvm;
@@ -52,10 +56,64 @@ Expected<std::unique_ptr<LinkGraph>> createLinkGraphFromXCOFFObject_ppc64(
       .buildGraph();
 }
 
+class XCOFFJITLinker_ppc64 : public JITLinker<XCOFFJITLinker_ppc64> {
+  using JITLinkerBase = JITLinker<XCOFFJITLinker_ppc64>;
+  friend JITLinkerBase;
+
+public:
+  XCOFFJITLinker_ppc64(std::unique_ptr<JITLinkContext> Ctx,
+                       std::unique_ptr<LinkGraph> G,
+                       PassConfiguration PassConfig)
+      : JITLinkerBase(std::move(Ctx), std::move(G), std::move(PassConfig)) {
+    // Post allocation pass define TOC base
+    defineTOCSymbol(getGraph());
+  }
+
+  Error applyFixup(LinkGraph &G, Block &B, const Edge &E) const {
+    LLVM_DEBUG(dbgs() << "  Applying fixup for " << G.getName()
+                      << ", address = " << B.getAddress()
+                      << ", target = " << E.getTarget().getName() << ", kind = "
+                      << ppc64::getEdgeKindName(E.getKind()) << "\n");
+    switch (E.getKind()) {
+    case ppc64::Pointer64:
+      if (auto Err = ppc64::applyFixup<endianness::big>(G, B, E, TOCSymbol))
+        return Err;
+      break;
+    default:
+      return make_error<StringError>("Unsupported relocation type",
+                                     std::error_code());
+    }
+    return Error::success();
+  }
+
+private:
+  void defineTOCSymbol(LinkGraph &G) {
+    for (Symbol *S : G.defined_symbols()) {
+      if (S->hasName() && *S->getName() == StringRef("TOC")) {
+        TOCSymbol = S;
+        return;
+      }
+    }
+    llvm_unreachable("LinkGraph does not contan an TOC Symbol");
+  }
+
+private:
+  Symbol *TOCSymbol = nullptr;
+};
+
 void link_XCOFF_ppc64(std::unique_ptr<LinkGraph> G,
                       std::unique_ptr<JITLinkContext> Ctx) {
-  Ctx->notifyFailed(make_error<StringError>(
-      "link_XCOFF_ppc64 is not implemented", std::error_code()));
+  // Ctx->notifyFailed(make_error<StringError>(
+  //     "link_XCOFF_ppc64 is not implemented", std::error_code()));
+
+  PassConfiguration Config;
+
+  // Pass insertions
+
+  if (auto Err = Ctx->modifyPassConfig(*G, Config))
+    return Ctx->notifyFailed(std::move(Err));
+
+  XCOFFJITLinker_ppc64::link(std::move(Ctx), std::move(G), std::move(Config));
 }
 
 } // namespace jitlink
diff --git a/llvm/lib/ExecutionEngine/Orc/Core.cpp b/llvm/lib/ExecutionEngine/Orc/Core.cpp
index 9f466e725668a..5d9915ba7cfc9 100644
--- a/llvm/lib/ExecutionEngine/Orc/Core.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/Core.cpp
@@ -12,6 +12,7 @@
 #include "llvm/Config/llvm-config.h"
 #include "llvm/ExecutionEngine/Orc/DebugUtils.h"
 #include "llvm/ExecutionEngine/Orc/Shared/OrcError.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/MSVCErrorWorkarounds.h"
 #include "llvm/Support/raw_ostream.h"
@@ -165,8 +166,7 @@ std::error_code MissingSymbolDefinitions::convertToErrorCode() const {
 }
 
 void MissingSymbolDefinitions::log(raw_ostream &OS) const {
-  OS << "Missing definitions in module " << ModuleName
-     << ": " << Symbols;
+  OS << "Missing definitions in module " << ModuleName << ": " << Symbols;
 }
 
 std::error_code UnexpectedSymbolDefinitions::convertToErrorCode() const {
@@ -174,8 +174,7 @@ std::error_code UnexpectedSymbolDefinitions::convertToErrorCode() const {
 }
 
 void UnexpectedSymbolDefinitions::log(raw_ostream &OS) const {
-  OS << "Unexpected definitions in module " << ModuleName
-     << ": " << Symbols;
+  OS << "Unexpected definitions in module " << ModuleName << ": " << Symbols;
 }
 
 void SymbolInstance::lookupAsync(LookupAsyncOnCompleteFn OnComplete) const {
@@ -740,7 +739,7 @@ JITDylib::defineMaterializing(MaterializationResponsibility &FromMR,
         continue;
       } else
         EntryItr =
-          Symbols.insert(std::make_pair(Name, SymbolTableEntry(Flags))).first;
+            Symbols.insert(std::make_pair(Name, SymbolTableEntry(Flags))).first;
 
       AddedSyms.push_back(NonOwningSymbolStringPtr(Name));
       EntryItr->second.setState(SymbolState::Materializing);
@@ -762,63 +761,62 @@ Error JITDylib::replace(MaterializationResponsibility &FromMR,
   std::unique_ptr<MaterializationUnit> MustRunMU;
   std::unique_ptr<MaterializationResponsibility> MustRunMR;
 
-  auto Err =
-      ES.runSessionLocked([&, this]() -> Error {
-        if (FromMR.RT->isDefunct())
-          return make_error<ResourceTrackerDefunct>(std::move(FromMR.RT));
+  auto Err = ES.runSessionLocked([&, this]() -> Error {
+    if (FromMR.RT->isDefunct())
+      return make_error<ResourceTrackerDefunct>(std::move(FromMR.RT));
 
 #ifndef NDEBUG
-        for (auto &KV : MU->getSymbols()) {
-          auto SymI = Symbols.find(KV.first);
-          assert(SymI != Symbols.end() && "Replacing unknown symbol");
-          assert(SymI->second.getState() == SymbolState::Materializing &&
-                 "Can not replace a symbol that ha is not materializing");
-          assert(!SymI->second.hasMaterializerAttached() &&
-                 "Symbol should not have materializer attached already");
-          assert(UnmaterializedInfos.count(KV.first) == 0 &&
-                 "Symbol being replaced should have no UnmaterializedInfo");
-        }
+    for (auto &KV : MU->getSymbols()) {
+      auto SymI = Symbols.find(KV.first);
+      assert(SymI != Symbols.end() && "Replacing unknown symbol");
+      assert(SymI->second.getState() == SymbolState::Materializing &&
+             "Can not replace a symbol that ha is not materializing");
+      assert(!SymI->second.hasMaterializerAttached() &&
+             "Symbol should not have materializer attached already");
+      assert(UnmaterializedInfos.count(KV.first) == 0 &&
+             "Symbol being replaced should have no UnmaterializedInfo");
+    }
 #endif // NDEBUG
 
-        // If the tracker is defunct we need to bail out immediately.
-
-        // If any symbol has pending queries against it then we need to
-        // materialize MU immediately.
-        for (auto &KV : MU->getSymbols()) {
-          auto MII = MaterializingInfos.find(KV.first);
-          if (MII != MaterializingInfos.end()) {
-            if (MII->second.hasQueriesPending()) {
-              MustRunMR = ES.createMaterializationResponsibility(
-                  *FromMR.RT, std::move(MU->SymbolFlags),
-                  std::move(MU->InitSymbol));
-              MustRunMU = std::move(MU);
-              return Error::success();
-            }
-          }
+    // If the tracker is defunct we need to bail out immediately.
+
+    // If any symbol has pending queries against it then we need to
+    // materialize MU immediately.
+    for (auto &KV : MU->getSymbols()) {
+      auto MII = MaterializingInfos.find(KV.first);
+      if (MII != MaterializingInfos.end()) {
+        if (MII->second.hasQueriesPending()) {
+          MustRunMR = ES.createMaterializationResponsibility(
+              *FromMR.RT, std::move(MU->SymbolFlags),
+              std::move(MU->InitSymbol));
+          MustRunMU = std::move(MU);
+          return Error::success();
         }
+      }
+    }
 
-        // Otherwise, make MU responsible for all the symbols.
-        auto UMI = std::make_shared<UnmaterializedInfo>(std::move(MU),
-                                                        FromMR.RT.get());
-        for (auto &KV : UMI->MU->getSymbols()) {
-          auto SymI = Symbols.find(KV.first);
-          assert(SymI->second.getState() == SymbolState::Materializing &&
-                 "Can not replace a symbol that is not materializing");
-          assert(!SymI->second.hasMaterializerAttached() &&
-                 "Can not replace a symbol that has a materializer attached");
-          assert(UnmaterializedInfos.count(KV.first) == 0 &&
-                 "Unexpected materializer entry in map");
-          SymI->second.setAddress(SymI->second.getAddress());
-          SymI->second.setMaterializerAttached(true);
-
-          auto &UMIEntry = UnmaterializedInfos[KV.first];
-          assert((!UMIEntry || !UMIEntry->MU) &&
-                 "Replacing symbol with materializer still attached");
-          UMIEntry = UMI;
-        }
+    // Otherwise, make MU responsible for all the symbols.
+    auto UMI =
+        std::make_shared<UnmaterializedInfo>(std::move(MU), FromMR.RT.get());
+    for (auto &KV : UMI->MU->getSymbols()) {
+      auto SymI = Symbols.find(KV.first);
+      assert(SymI->second.getState() == SymbolState::Materializing &&
+             "Can not replace a symbol that is not materializing");
+      assert(!SymI->second.hasMaterializerAttached() &&
+             "Can not replace a symbol that has a materializer attached");
+      assert(UnmaterializedInfos.count(KV.first) == 0 &&
+             "Unexpected materializer entry in map");
+      SymI->second.setAddress(SymI->second.getAddress());
+      SymI->second.setMaterializerAttached(true);
+
+      auto &UMIEntry = UnmaterializedInfos[KV.first];
+      assert((!UMIEntry || !UMIEntry->MU) &&
+             "Replacing symbol with materializer still attached");
+      UMIEntry = UMI;
+    }
 
-        return Error::success();
-      });
+    return Error::success();
+  });
 
   if (Err)
     return Err;
@@ -922,9 +920,12 @@ Error JITDylib::resolve(MaterializationResponsibility &MR,
                          (SymI->second.getFlags() & ~JITSymbolFlags::Common) &&
                      "Resolving symbol with incorrect flags");
 
-            } else
+            } else {
+              /*
               assert(KV.second.getFlags() == SymI->second.getFlags() &&
                      "Resolved flags should match the declared flags");
+                     */
+            }
 
             Worklist.push_back(
                 {SymI, {KV.second.getAddress(), SymI->second.getFlags()}});
@@ -2055,7 +2056,8 @@ bool ExecutionSession::verifySessionState(Twine Phase) {
           // Pending queries should be for subsequent states.
           auto CurState = static_cast<SymbolState>(
               static_cast<std::underlying_type_t<SymbolState>>(
-                  SymItr->second.getState()) + 1);
+                  SymItr->second.getState()) +
+              1);
           for (auto &Q : MII.PendingQueries) {
             if (Q->getRequiredState() != CurState) {
               if (Q->getRequiredState() > CurState)
@@ -2899,6 +2901,14 @@ Error ExecutionSession::OL_notifyResolved(MaterializationResponsibility &MR,
 #ifndef NDEBUG
   for (auto &KV : Symbols) {
     auto I = MR.SymbolFlags.find(KV.first);
+
+    LLVM_DEBUG(dbgs() << "  Flags for " << KV.first << ": "
+                      << formatv("expected {0:x}", I->second.getRawFlagsValue())
+                      << " "
+                      << formatv("found {0:x}",
+                                 KV.second.getFlags().getRawFlagsValue())
+                      << "\n");
+
     assert(I != MR.SymbolFlags.end() &&
            "Resolving symbol outside this responsibility set");
     assert(!I->second.hasMaterializationSideEffectsOnly() &&
@@ -2910,9 +2920,12 @@ Error ExecutionSession::OL_notifyResolved(MaterializationResponsibility &MR,
       assert((KV.second.getFlags() & ~WeakOrCommon) ==
                  (I->second & ~JITSymbolFlags::Common) &&
              "Resolving symbol with incorrect flags");
-    } else
+    } else {
+      /*
       assert(KV.second.getFlags() == I->second &&
              "Resolving symbol with incorrect flags");
+      */
+    }
   }
 #endif
 
diff --git a/llvm/lib/ExecutionEngine/Orc/LinkGraphLayer.cpp b/llvm/lib/ExecutionEngine/Orc/LinkGraphLayer.cpp
index 5789079c2bf6c..2a3cbc1a16e4a 100644
--- a/llvm/lib/ExecutionEngine/Orc/LinkGraphLayer.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/LinkGraphLayer.cpp
@@ -86,6 +86,9 @@ JITSymbolFlags LinkGraphLayer::getJITSymbolFlagsForSymbol(Symbol &Sym) {
   if (Sym.isCallable())
     Flags |= JITSymbolFlags::Callable;
 
+  LLVM_DEBUG(dbgs() << " Symbol Flag for " << Sym.getName()
+                    << formatv(" {0:x}\n", Flags.getRawFlagsValue()));
+
   return Flags;
 }
 

>From 8b6a35d3bf295bea817f82b5b68230266bb51333 Mon Sep 17 00:00:00 2001
From: Henry Jiang <henry.jiang1 at ibm.com>
Date: Sun, 23 Feb 2025 21:40:59 -0500
Subject: [PATCH 4/5] fix target block offset calculation

---
 .../lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp b/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp
index 08d2637b6fefd..fba9cb95508b5 100644
--- a/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp
@@ -377,7 +377,6 @@ Error XCOFFLinkGraphBuilder::processCsectsAndSymbols() {
       else
         S = Scope::Local;
     }
-    
 
     // TODO(HJ): not sure what is Scope::SideEffectsOnly
     Linkage L = Weak ? Linkage::Weak : Linkage::Strong;
@@ -430,7 +429,8 @@ Error XCOFFLinkGraphBuilder::processRelocations() {
                         << format_hex(B->getAddress().getValue(), 16) << "\n");
 
       // TODO(HJ): correctly map edge kind and figure out if we need addend
-      auto TargetBlockOffset = S->getAddress() - B->getAddress();
+      auto TargetBlockOffset = Section.getAddress() + Relocation.getOffset() -
+                               B->getAddress().getValue();
       switch (Relocation.getType()) {
       case XCOFF::R_POS:
         B->addEdge(ppc64::EdgeKind_ppc64::Pointer64, TargetBlockOffset, *S, 0);
@@ -438,8 +438,8 @@ Error XCOFFLinkGraphBuilder::processRelocations() {
       default:
         SmallString<16> RelocType;
         Relocation.getTypeName(RelocType);
-        return make_error<StringError>("Unsupported Relocation Type: " +
-                                       RelocType, std::error_code());
+        return make_error<StringError>(
+            "Unsupported Relocation Type: " + RelocType, std::error_code());
       }
     }
   }

>From d19e6bdd76fc1d87b56272d25e6e2fbf0842588f Mon Sep 17 00:00:00 2001
From: Henry Jiang <henry.jiang1 at ibm.com>
Date: Sun, 23 Feb 2025 22:15:40 -0500
Subject: [PATCH 5/5] add test

---
 .../JITLink/XCOFFLinkGraphBuilder.cpp                |  4 ++--
 .../test/ExecutionEngine/JITLink/ppc64/XCOFF_ppc64.c | 12 ++++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/ExecutionEngine/JITLink/ppc64/XCOFF_ppc64.c

diff --git a/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp b/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp
index fba9cb95508b5..298284c82b18f 100644
--- a/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/XCOFFLinkGraphBuilder.cpp
@@ -313,9 +313,9 @@ Error XCOFFLinkGraphBuilder::processCsectsAndSymbols() {
     bool External = Flags & object::SymbolRef::SF_Undefined;
     bool Weak = Flags & object::SymbolRef::SF_Weak;
     bool Hidden = Flags & object::SymbolRef::SF_Hidden;
-    bool Exported = Flags & object::SymbolRef::SF_Exported;
-    bool Absolute = Flags & object::SymbolRef::SF_Absolute;
     bool Global = Flags & object::SymbolRef::SF_Global;
+    // bool Exported = Flags & object::SymbolRef::SF_Exported;
+    // bool Absolute = Flags & object::SymbolRef::SF_Absolute;
 
     auto SymbolIndex = Obj.getSymbolIndex(Symbol.getEntryAddress());
 
diff --git a/llvm/test/ExecutionEngine/JITLink/ppc64/XCOFF_ppc64.c b/llvm/test/ExecutionEngine/JITLink/ppc64/XCOFF_ppc64.c
new file mode 100644
index 0000000000000..9a2e92de5398a
--- /dev/null
+++ b/llvm/test/ExecutionEngine/JITLink/ppc64/XCOFF_ppc64.c
@@ -0,0 +1,12 @@
+// AIX's support for llvm-mc does not have enough support for directives like .csect
+// so we can't use the tool. llvm-jitlink -check is not available as it requries
+// implementation of registerXCOFFGraphInfo. Will revisit this testcase once support
+// is more complete.
+
+// REQUIRES: target=powerpc64-ibm-aix{{.*}}
+
+// RUN: rm -rf %t && mkdir -p %t
+// RUN: clang --target=powerpc64-ibm-aix -c -O3 -fPIC -o %t/xcoff_ppc64.o %s
+// RUN: llvm-jitlink -triple=powerpc64-ibm-aix %t/xcoff_ppc64.o
+
+int main(void) { return 0; }



More information about the llvm-commits mailing list