[llvm] [llvm-objcopy][COFF] Update WinCFGuard section contents after stripping (PR #153322)

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 19 04:34:52 PDT 2025


================
@@ -92,6 +94,79 @@ Error COFFWriter::finalizeSymbolContents() {
   return Error::success();
 }
 
+Error COFFWriter::finalizeSymIdxContents() {
+  // CFGuards shouldn't be present in PE
+  if (Obj.IsPE)
+    return Error::success();
+
+  // Currently handle only sections consisting only of .symidx.
+  // TODO: other sections such as .impcall and .hybmp$x require more complex
+  // handling as they have more complex layout.
+  auto IsSymIdxSection = [](StringRef Name) {
+    return Name == ".gljmp$y" || Name == ".giats$y" || Name == ".gfids$y" ||
+           Name == ".gehcont$y";
+  };
+
+  DenseMap<size_t, size_t> SymIdMap;
+  SmallDenseMap<ssize_t, coff_aux_section_definition *, 4> SecIdMap;
+  bool NeedUpdate = false;
+  for (auto &Sym : Obj.getMutableSymbols()) {
+    NeedUpdate |= Sym.OriginalRawIndex == Sym.RawIndex;
----------------
mstorsjo wrote:

Isn't this condition inverted? If `RawIndex != OriginalRawIndex`, we would need an update?

But on the other hand, I'm not sure if it is safe to skip the update on itself. Consider a case where some sections and symbols are removed. All remaining symbols have the exact same raw indices as they had before, so this would conclude that we don't need to do any update. But the sections may reference a symbol which no longer exists, which either would require us to error out, or prune it out from the symbol list.

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


More information about the llvm-commits mailing list