[llvm] [AIX] Sort relocations in XCOFF object writer. (PR #180807)

via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 10 10:48:04 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-powerpc

Author: Sean Fertile (mandlebug)

<details>
<summary>Changes</summary>



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


2 Files Affected:

- (modified) llvm/lib/MC/XCOFFObjectWriter.cpp (+8-3) 
- (added) llvm/test/CodeGen/PowerPC/aix-reloc-sorting.ll (+54) 


``````````diff
diff --git a/llvm/lib/MC/XCOFFObjectWriter.cpp b/llvm/lib/MC/XCOFFObjectWriter.cpp
index d466009348c47..d9f47d6f4ba25 100644
--- a/llvm/lib/MC/XCOFFObjectWriter.cpp
+++ b/llvm/lib/MC/XCOFFObjectWriter.cpp
@@ -1123,16 +1123,21 @@ void XCOFFWriter::writeRelocation(XCOFFRelocation Reloc,
 }
 
 void XCOFFWriter::writeRelocations() {
-  for (const auto *Section : Sections) {
+  for (auto *Section : Sections) {
     if (Section->Index == SectionEntry::UninitializedIndex)
       // Nothing to write for this Section.
       continue;
 
-    for (const auto *Group : Section->Groups) {
+    for (auto *Group : Section->Groups) {
       if (Group->empty())
         continue;
 
-      for (const auto &Csect : *Group) {
+      for (XCOFFSection &Csect : *Group) {
+        llvm::stable_sort(Csect.Relocations, [](const XCOFFRelocation &A,
+                                                const XCOFFRelocation &B) {
+          return A.FixupOffsetInCsect < B.FixupOffsetInCsect;
+        });
+
         for (const auto Reloc : Csect.Relocations)
           writeRelocation(Reloc, Csect);
       }
diff --git a/llvm/test/CodeGen/PowerPC/aix-reloc-sorting.ll b/llvm/test/CodeGen/PowerPC/aix-reloc-sorting.ll
new file mode 100644
index 0000000000000..7aa51a23e1d90
--- /dev/null
+++ b/llvm/test/CodeGen/PowerPC/aix-reloc-sorting.ll
@@ -0,0 +1,54 @@
+; RUN: llc -verify-machineinstrs -mtriple powerpc-ibm-aix-xcoff \
+; RUN:   -data-sections=false -filetype=obj -o %t.o < %s
+; RUN: llvm-objdump -D -r --symbol-description %t.o | FileCheck %s
+
+; RUN: llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff \
+; RUN:   -data-sections=false -filetype=obj -o %t.o < %s
+; RUN: llvm-objdump -D -r --symbol-description %t.o | FileCheck %s
+
+ at a = global i32 1
+ at b = dso_local constant i32 2
+ at p = dso_local global ptr @a, !implicit.ref !0
+ at c = global i32 3
+
+define dso_local void @foo(i32 noundef signext %i) {
+entry:
+  tail call void @extern_func1(i32 noundef signext %i)
+  ret void
+}
+
+declare void @extern_func1(i32 noundef signext)
+
+define dso_local signext i32 @bar() {
+entry:
+  %0 = load i32, ptr @a, align 4
+  %call = tail call signext i32 @extern_func2(i32 noundef signext %0)
+  ret i32 %call
+}
+
+declare signext i32 @extern_func2(i32 noundef signext)
+
+define dso_local signext i32 @baz() !implicit.ref !0 {
+entry:
+  %0 = load i32, ptr @c, align 4
+  ret i32 %0
+}
+
+!0 = !{ptr @b}
+
+; CHECK: Disassembly of section .text:
+; CHECK-EMPTY:
+; CHECK-NEXT: 00000000 (idx: {{[0-9]+}}) .foo:
+; CHECK:      00000000:  R_REF	(idx: {{[0-9]+}}) b
+; CHECK:      0000000c:  R_RBR	(idx: {{[0-9]+}}) .extern_func1[PR]
+; CHECK:      0000004a:  R_TOC	(idx: {{[0-9]+}}) a[TC]
+; CHECK:      00000054:  R_RBR	(idx: {{[0-9]+}}) .extern_func2[PR]
+; CHECK:      00000092:  R_TOC   (idx: {{[0-9]+}}) c[TC]
+
+; CHECK: Disassembly of section .data:
+; CHECK-EMPTY:
+; CHECK-NEXT: [[DATA:[0-9a-f]+]] (idx: {{[0-9]+}}) a:
+; CHECK:      [[DATA]]:  R_REF	(idx: {{[0-9]+}}) b
+; CHECK:      [[DATA_P:[0-9a-f]+]] (idx: {{[0-9]+}}) p:
+; CHECK:      [[DATA_P]]:  R_POS	(idx: {{[0-9]+}}) a
+; CHECK:      {{[0-9a-f]+}} (idx: {{[0-9]+}}) c:

``````````

</details>


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


More information about the llvm-commits mailing list