[llvm] 8e4e1c1 - MIPS: Stable sort relocations

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 21 23:05:20 PDT 2025


Author: Fangrui Song
Date: 2025-07-21T23:05:16-07:00
New Revision: 8e4e1c104d88e193d5977e0136509f8c76dde43e

URL: https://github.com/llvm/llvm-project/commit/8e4e1c104d88e193d5977e0136509f8c76dde43e
DIFF: https://github.com/llvm/llvm-project/commit/8e4e1c104d88e193d5977e0136509f8c76dde43e.diff

LOG: MIPS: Stable sort relocations

There might be more than one relocations at an offset with composed
relocations or .reloc directive. llvm::sort output is unstable, and if
EXPENSIVE_CHECKS, also undeterministic, causing
MC/Mips/reloc-directive.s to fail.

Added: 
    

Modified: 
    llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
index ad8f5f0a09745..7abe9c9606749 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
@@ -385,11 +385,12 @@ void MipsELFObjectWriter::sortRelocs(std::vector<ELFRelocationEntry> &Relocs) {
   if (hasRelocationAddend())
     return;
 
-  // Sort relocations by the address they are applied to.
-  llvm::sort(Relocs,
-             [](const ELFRelocationEntry &A, const ELFRelocationEntry &B) {
-               return A.Offset < B.Offset;
-             });
+  // Sort relocations by r_offset. There might be more than one at an offset
+  // with composed relocations or .reloc directives.
+  llvm::stable_sort(
+      Relocs, [](const ELFRelocationEntry &A, const ELFRelocationEntry &B) {
+        return A.Offset < B.Offset;
+      });
 
   // Place relocations in a list for reorder convenience. Hi16 contains the
   // iterators of high-part relocations.


        


More information about the llvm-commits mailing list