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

Evgenii Kudriashov via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 20 09:09:28 PDT 2025


================
@@ -93,59 +94,75 @@ Error COFFWriter::finalizeSymbolContents() {
   return Error::success();
 }
 
-Error COFFWriter::finalizeCFGuardContents() {
+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 (Symbol &Sym : Obj.getMutableSymbols()) {
+  for (auto &Sym : Obj.getMutableSymbols()) {
     NeedUpdate |= Sym.OriginalRawIndex == Sym.RawIndex;
     SymIdMap[Sym.OriginalRawIndex] = Sym.RawIndex;
+
+    // We collect only definition symbols of the sections to update checksum
+    if (Sym.Sym.NumberOfAuxSymbols == 1 &&
+        Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC && Sym.Sym.Value == 0 &&
+        IsSymIdxSection(Sym.Name))
+      SecIdMap[Sym.TargetSectionId] =
+          reinterpret_cast<coff_aux_section_definition *>(
+              Sym.AuxData[0].Opaque);
   }
 
   if (!NeedUpdate)
     return Error::success();
 
-  for (auto &Sym : Obj.getMutableSymbols()) {
-    if (Sym.Name != ".gljmp$y" && Sym.Name != ".giats$y" &&
-        Sym.Name != ".gfids$y")
+  for (auto &Sec : Obj.getMutableSections()) {
+    if (!IsSymIdxSection(Sec.Name))
       continue;
 
-    auto Sec = find_if(Obj.getMutableSections(),
-                       [&Sym](Section &S) { return S.Name == Sym.Name; });
-
-    if (Sec == Obj.getMutableSections().end() ||
-        Sec->UniqueId != Sym.TargetSectionId)
-      return createStringError(object_error::invalid_symbol_index,
-                               "symbol '%s' is missing its section",
-                               Sym.Name.str().c_str());
-
-    if (Sym.Sym.NumberOfAuxSymbols != 1 ||
-        Sym.Sym.StorageClass != IMAGE_SYM_CLASS_STATIC)
-      return createStringError(object_error::invalid_symbol_index,
-                               "symbol '%s' has unexpected section format",
-                               Sym.Name.str().c_str());
-
-    ArrayRef<uint8_t> RawIds = Sec->getContents();
+    ArrayRef<uint8_t> RawIds = Sec.getContents();
     // Nothing to do and also CheckSum will be -1 instead of 0 if we recalculate
     // it on empty input.
     if (RawIds.size() == 0)
-      return Error::success();
+      continue;
+
+    if (!SecIdMap.contains(Sec.UniqueId))
+      return createStringError(object_error::invalid_symbol_index,
+                               "section '%s' does not have the corresponding "
+                               "symbol or the symbol has unexpected format",
+                               Sec.Name.str().c_str());
 
     // Create updated content
-    ArrayRef<uint32_t> Ids(reinterpret_cast<const uint32_t *>(RawIds.data()),
-                           RawIds.size() / 4);
-    std::vector<uint32_t> NewIds;
-    for (auto Id : Ids)
-      NewIds.push_back(SymIdMap[Id]);
+    ArrayRef<support::ulittle32_t> Ids(
+        reinterpret_cast<const support::ulittle32_t *>(RawIds.data()),
+        RawIds.size() / 4);
+    std::vector<support::ulittle32_t> NewIds;
+    for (auto Id : Ids) {
+      if (!SymIdMap.contains(Id))
----------------
e-kud wrote:

Moved to the iterators.

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


More information about the llvm-commits mailing list