[lld] [lld-macho] Fix category merging category map non-determinism (PR #91159)

Kyungwoo Lee via llvm-commits llvm-commits at lists.llvm.org
Mon May 6 09:17:01 PDT 2024


================
@@ -1072,12 +1080,25 @@ void ObjcCategoryMerger::collectAndValidateCategoriesData() {
           tryGetSymbolAtIsecOffset(catBodyIsec, catLayout.klassOffset);
       assert(classSym && "Category does not have a valid base class");
 
-      InfoInputCategory catInputInfo{catListCisec, catBodyIsec, off};
+      InfoInputCategory catInputInfo{catListCisec, catBodyIsec, off,
+                                     inputCategoryIndex++};
       categoryMap[classSym].push_back(catInputInfo);
 
       collectCategoryWriterInfoFromCategory(catInputInfo);
     }
   }
+
+  // Move categoryMap into categoryGroups and sort by the first category's
+  // inputIndex. This way we can be sure that category merging will be
+  // deterministic across linker runs.
+  categoryGroups.reserve(categoryMap.size());
+  for (auto &mapEntry : categoryMap)
+    categoryGroups.push_back(mapEntry.second);
+
+  std::sort(categoryGroups.begin(), categoryGroups.end(),
----------------
kyulee-com wrote:

```suggestion
llvm::sort(categoryGroups, [](const CategoryGroup &a, const CategoryGroup &b) {
              return a[0].inputIndex < b[0].inputIndex;
            });

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


More information about the llvm-commits mailing list