[llvm] [llvm-objcopy][COFF] Update .symidx values after stripping (PR #153322)

Evgenii Kudriashov via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 29 08:25:13 PDT 2025


https://github.com/e-kud updated https://github.com/llvm/llvm-project/pull/153322

>From b302bd9732d5bc62d9dd59bead180a5c30973a1c Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Tue, 12 Aug 2025 17:38:14 -0700
Subject: [PATCH 01/17] [objcopy][COFF] Update WinCFGuard section contents
 after stripping

After deleting debug sections symbol indexes are shifted but WinCFGuard
sections encode these indices into section data that is completely
ignored. Update symbol indices as well.
---
 llvm/lib/ObjCopy/COFF/COFFObject.cpp          |   3 +
 llvm/lib/ObjCopy/COFF/COFFObject.h            |   1 +
 llvm/lib/ObjCopy/COFF/COFFWriter.cpp          |  60 +++++
 llvm/lib/ObjCopy/COFF/COFFWriter.h            |   1 +
 .../COFF/strip-update-winguards.test          | 237 ++++++++++++++++++
 5 files changed, 302 insertions(+)
 create mode 100644 llvm/test/tools/llvm-objcopy/COFF/strip-update-winguards.test

diff --git a/llvm/lib/ObjCopy/COFF/COFFObject.cpp b/llvm/lib/ObjCopy/COFF/COFFObject.cpp
index 5fa13391c908f..fcb1bbfe91332 100644
--- a/llvm/lib/ObjCopy/COFF/COFFObject.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFObject.cpp
@@ -16,8 +16,11 @@ namespace coff {
 using namespace object;
 
 void Object::addSymbols(ArrayRef<Symbol> NewSymbols) {
+  size_t RawIndex = 0;
   for (Symbol S : NewSymbols) {
     S.UniqueId = NextSymbolUniqueId++;
+    S.OriginalRawIndex = RawIndex;
+    RawIndex += 1 + S.Sym.NumberOfAuxSymbols;
     Symbols.emplace_back(S);
   }
   updateSymbols();
diff --git a/llvm/lib/ObjCopy/COFF/COFFObject.h b/llvm/lib/ObjCopy/COFF/COFFObject.h
index cdd1f17fc6055..9f88d45962513 100644
--- a/llvm/lib/ObjCopy/COFF/COFFObject.h
+++ b/llvm/lib/ObjCopy/COFF/COFFObject.h
@@ -89,6 +89,7 @@ struct Symbol {
   std::optional<size_t> WeakTargetSymbolId;
   size_t UniqueId;
   size_t RawIndex;
+  size_t OriginalRawIndex;
   bool Referenced;
 };
 
diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
index 350c4aec572c9..4e3aa8201f0dc 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
@@ -12,6 +12,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/BinaryFormat/COFF.h"
 #include "llvm/Object/COFF.h"
+#include "llvm/Support/CRC.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/ErrorHandling.h"
 #include <cstddef>
@@ -92,6 +93,63 @@ Error COFFWriter::finalizeSymbolContents() {
   return Error::success();
 }
 
+Error COFFWriter::finalizeCFGuardContents() {
+  DenseMap<size_t, size_t> SymIdMap;
+  bool NeedUpdate = false;
+  for (Symbol &Sym : Obj.getMutableSymbols()) {
+    NeedUpdate |= Sym.OriginalRawIndex == Sym.RawIndex;
+    SymIdMap[Sym.OriginalRawIndex] = Sym.RawIndex;
+  }
+
+  if (!NeedUpdate)
+    return Error::success();
+
+  for (auto &Sym : Obj.getMutableSymbols()) {
+    if (Sym.Name != ".gljmp$y" && Sym.Name != ".giats$y" &&
+        Sym.Name != ".gfids$y")
+      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();
+    // 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();
+
+    // 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<uint8_t> NewRawIds(reinterpret_cast<uint8_t *>(NewIds.data()),
+                                RawIds.size());
+    // Update check sum
+    JamCRC JC(/*Init=*/0);
+    JC.update(NewRawIds);
+    coff_aux_section_definition *SD =
+        reinterpret_cast<coff_aux_section_definition *>(Sym.AuxData[0].Opaque);
+    SD->CheckSum = JC.getCRC();
+    // Set new content
+    Sec->setOwnedContents(NewRawIds);
+  }
+  return Error::success();
+}
+
 void COFFWriter::layoutSections() {
   for (auto &S : Obj.getMutableSections()) {
     if (S.Header.SizeOfRawData > 0)
@@ -183,6 +241,8 @@ Error COFFWriter::finalize(bool IsBigObj) {
     return E;
   if (Error E = finalizeSymbolContents())
     return E;
+  if (Error E = finalizeCFGuardContents())
+    return E;
 
   size_t SizeOfHeaders = 0;
   FileAlignment = 1;
diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.h b/llvm/lib/ObjCopy/COFF/COFFWriter.h
index b7dca69e9a81a..557dbe4c01b9c 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.h
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.h
@@ -34,6 +34,7 @@ class COFFWriter {
   template <class SymbolTy> std::pair<size_t, size_t> finalizeSymbolTable();
   Error finalizeRelocTargets();
   Error finalizeSymbolContents();
+  Error finalizeCFGuardContents();
   void layoutSections();
   Expected<size_t> finalizeStringTable();
 
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-update-winguards.test b/llvm/test/tools/llvm-objcopy/COFF/strip-update-winguards.test
new file mode 100644
index 0000000000000..4dc1a821dbd6a
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-update-winguards.test
@@ -0,0 +1,237 @@
+# RUN: yaml2obj %s -o %t.in.o
+
+# RUN: llvm-readobj -r -s  -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.in.o | FileCheck %s --check-prefix=ORIG
+# RUN: llvm-objcopy --strip-debug %t.in.o %t.out.o
+# RUN: llvm-readobj -r -s  -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.out.o | FileCheck %s --check-prefix=STRIP
+
+# ORIG:      Relocations [
+# ORIG-NEXT:   Section (1) .text {
+# ORIG-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (14)
+# ORIG-NEXT:     0xA IMAGE_REL_AMD64_REL32 bar (15)
+# ORIG-NEXT:     0x11 IMAGE_REL_AMD64_REL32 baz (16)
+# ORIG-NEXT:     0x18 IMAGE_REL_AMD64_REL32 foobar (17)
+# ORIG-NEXT:   }
+# ORIG-NEXT: ]
+# ORIG:      Symbols [
+# ORIG:          Name: .gfids$y
+# ORIG:          Section: .gfids$y
+# ORIG:          AuxSymbolCount: 1
+# ORIG:          AuxSectionDef {
+# ORIG:            Checksum: 0x459345AD
+# ORIG:          }
+# ORIG:          Name: .giats$y
+# ORIG:          Section: .giats$y
+# ORIG:          AuxSymbolCount: 1
+# ORIG:          AuxSectionDef {
+# ORIG:            Checksum: 0x31852256
+# ORIG:          }
+# ORIG:          Name: .gljmp$y
+# ORIG:          Section: .gljmp$y
+# ORIG:          AuxSymbolCount: 1
+# ORIG:          AuxSectionDef {
+# ORIG:            Checksum: 0xC608680B
+# ORIG:          }
+# ORIG:      ]
+# ORIG:      Hex dump of section '.gfids$y':
+# ORIG-NEXT: 0x00000000 0e000000 10000000                   ........
+# ORIG:      Hex dump of section '.giats$y':
+# ORIG-NEXT: 0x00000000 0f000000 11000000                   ........
+# ORIG:      Hex dump of section '.gljmp$y':
+# ORIG-NEXT: 0x00000000 0e000000 0f000000 10000000 11000000 ................
+
+# STRIP:      Relocations [
+# STRIP-NEXT:   Section (1) .text {
+# STRIP-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (12)
+# STRIP-NEXT:     0xA IMAGE_REL_AMD64_REL32 bar (13)
+# STRIP-NEXT:     0x11 IMAGE_REL_AMD64_REL32 baz (14)
+# STRIP-NEXT:     0x18 IMAGE_REL_AMD64_REL32 foobar (15)
+# STRIP-NEXT:   }
+# STRIP-NEXT: ]
+# STRIP:      Symbols [
+# STRIP:          Name: .gfids$y
+# STRIP:          Section: .gfids$y
+# STRIP:          AuxSymbolCount: 1
+# STRIP:          AuxSectionDef {
+# STRIP:            Checksum: 0xB770627C
+# STRIP:          }
+# STRIP:          Name: .giats$y
+# STRIP:          Section: .giats$y
+# STRIP:          AuxSymbolCount: 1
+# STRIP:          AuxSectionDef {
+# STRIP:            Checksum: 0xC3660587
+# STRIP:          }
+# STRIP:          Name: .gljmp$y
+# STRIP:          Section: .gljmp$y
+# STRIP:          AuxSymbolCount: 1
+# STRIP:          AuxSectionDef {
+# STRIP:            Checksum: 0x7464D042
+# STRIP:          }
+# STRIP:      ]
+# STRIP:      Hex dump of section '.gfids$y':
+# STRIP-NEXT: 0x00000000 0c000000 0e000000                   ........
+# STRIP:      Hex dump of section '.giats$y':
+# STRIP-NEXT: 0x00000000 0d000000 0f000000                   ........
+# STRIP:      Hex dump of section '.gljmp$y':
+# STRIP-NEXT: 0x00000000 0c000000 0d000000 0e000000 0f000000 ................
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
+    SizeOfRawData:   28
+    Relocations:
+      - VirtualAddress:  3
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_REL32
+      - VirtualAddress:  10
+        SymbolName:      bar
+        Type:            IMAGE_REL_AMD64_REL32
+      - VirtualAddress:  17
+        SymbolName:      baz
+        Type:            IMAGE_REL_AMD64_REL32
+      - VirtualAddress:  24
+        SymbolName:      foobar
+        Type:            IMAGE_REL_AMD64_REL32
+  - Name:            .data
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       4
+    SectionData:     ''
+  - Name:            .bss
+    Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       4
+    SectionData:     ''
+  - Name:            '.debug$S'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     04000000F100000044656275672073656374696F6E20746F20626520737472697070656400
+    SizeOfRawData:   37
+  - Name:            '.gfids$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '0E00000010000000'
+    SizeOfRawData:   8
+  - Name:            '.giats$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     0F00000011000000
+    SizeOfRawData:   8
+  - Name:            '.gljmp$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     0E0000000F0000001000000011000000
+    SizeOfRawData:   16
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          28
+      NumberOfRelocations: 4
+      NumberOfLinenumbers: 0
+      CheckSum:        3583480811
+      Number:          1
+  - Name:            .data
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          2
+  - Name:            .bss
+    Value:           0
+    SectionNumber:   3
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          3
+  - Name:            '.debug$S'
+    Value:           0
+    SectionNumber:   4
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          37
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        2941632545
+      Number:          4
+  - Name:            '.gfids$y'
+    Value:           0
+    SectionNumber:   5
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          8
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        1167279533
+      Number:          5
+  - Name:            '.giats$y'
+    Value:           0
+    SectionNumber:   6
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          8
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        830808662
+      Number:          6
+  - Name:            '.gljmp$y'
+    Value:           0
+    SectionNumber:   7
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          16
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        3322439691
+      Number:          7
+  - Name:            foo
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            bar
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            baz
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            foobar
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...

>From 42350aab8066c231e612f0a54d91c2f563e2d225 Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Wed, 13 Aug 2025 15:02:02 -0700
Subject: [PATCH 02/17] ulittle32_t, skip for PE and continue instead of return

---
 llvm/lib/ObjCopy/COFF/COFFWriter.cpp | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
index 4e3aa8201f0dc..e5f47ad1f2d5b 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
@@ -14,6 +14,7 @@
 #include "llvm/Object/COFF.h"
 #include "llvm/Support/CRC.h"
 #include "llvm/Support/Errc.h"
+#include "llvm/Support/Endian.h"
 #include "llvm/Support/ErrorHandling.h"
 #include <cstddef>
 #include <cstdint>
@@ -94,9 +95,13 @@ Error COFFWriter::finalizeSymbolContents() {
 }
 
 Error COFFWriter::finalizeCFGuardContents() {
+  // CFGuards shouldn't be present in PE
+  if (Obj.IsPE)
+    return Error::success();
+
   DenseMap<size_t, size_t> SymIdMap;
   bool NeedUpdate = false;
-  for (Symbol &Sym : Obj.getMutableSymbols()) {
+  for (const auto &Sym : Obj.getSymbols()) {
     NeedUpdate |= Sym.OriginalRawIndex == Sym.RawIndex;
     SymIdMap[Sym.OriginalRawIndex] = Sym.RawIndex;
   }
@@ -128,14 +133,14 @@ Error COFFWriter::finalizeCFGuardContents() {
     // 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;
 
     // Create updated content
-    ArrayRef<uint32_t> Ids(reinterpret_cast<const uint32_t *>(RawIds.data()),
-                           RawIds.size() / 4);
-    std::vector<uint32_t> NewIds;
+    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)
-      NewIds.push_back(SymIdMap[Id]);
+      NewIds.push_back(support::ulittle32_t(SymIdMap[Id]));
     ArrayRef<uint8_t> NewRawIds(reinterpret_cast<uint8_t *>(NewIds.data()),
                                 RawIds.size());
     // Update check sum

>From 8acb7f457653801739c8d85ec0a43e92df3c3f1c Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Wed, 13 Aug 2025 15:21:44 -0700
Subject: [PATCH 03/17] Format: forgotten ammend

---
 llvm/lib/ObjCopy/COFF/COFFWriter.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
index e5f47ad1f2d5b..ea803cba561e0 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
@@ -13,8 +13,8 @@
 #include "llvm/BinaryFormat/COFF.h"
 #include "llvm/Object/COFF.h"
 #include "llvm/Support/CRC.h"
-#include "llvm/Support/Errc.h"
 #include "llvm/Support/Endian.h"
+#include "llvm/Support/Errc.h"
 #include "llvm/Support/ErrorHandling.h"
 #include <cstddef>
 #include <cstdint>
@@ -136,8 +136,9 @@ Error COFFWriter::finalizeCFGuardContents() {
       continue;
 
     // Create updated content
-    ArrayRef<support::ulittle32_t> Ids(reinterpret_cast<const support::ulittle32_t *>(RawIds.data()),
-                              RawIds.size() / 4);
+    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)
       NewIds.push_back(support::ulittle32_t(SymIdMap[Id]));
@@ -150,7 +151,7 @@ Error COFFWriter::finalizeCFGuardContents() {
         reinterpret_cast<coff_aux_section_definition *>(Sym.AuxData[0].Opaque);
     SD->CheckSum = JC.getCRC();
     // Set new content
-    Sec->setOwnedContents(NewRawIds);
+    Sec->setOwnedContents(NewRawIds.vec());
   }
   return Error::success();
 }

>From 2264108578abbfa5470f347ab8ce613ebf1a4c5d Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Fri, 15 Aug 2025 15:18:58 -0700
Subject: [PATCH 04/17] Iterate in more natural way

---
 llvm/lib/ObjCopy/COFF/COFFWriter.cpp | 52 +++++++++++++++-------------
 1 file changed, 27 insertions(+), 25 deletions(-)

diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
index ea803cba561e0..892eb9116cee1 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
@@ -99,59 +99,61 @@ Error COFFWriter::finalizeCFGuardContents() {
   if (Obj.IsPE)
     return Error::success();
 
+  auto IsSymIdxSection = [](StringRef Name) {
+    return Name == ".gljmp$y" || Name == ".giats$y" || Name == ".gfids$y";
+  };
+
   DenseMap<size_t, size_t> SymIdMap;
+  SmallDenseMap<ssize_t, coff_aux_section_definition *, 4> SecIdMap;
   bool NeedUpdate = false;
-  for (const auto &Sym : Obj.getSymbols()) {
+  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)
       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<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)
+    for (auto Id : Ids) {
       NewIds.push_back(support::ulittle32_t(SymIdMap[Id]));
+    }
     ArrayRef<uint8_t> NewRawIds(reinterpret_cast<uint8_t *>(NewIds.data()),
                                 RawIds.size());
     // Update check sum
     JamCRC JC(/*Init=*/0);
     JC.update(NewRawIds);
-    coff_aux_section_definition *SD =
-        reinterpret_cast<coff_aux_section_definition *>(Sym.AuxData[0].Opaque);
-    SD->CheckSum = JC.getCRC();
+    SecIdMap[Sec.UniqueId]->CheckSum = JC.getCRC();
     // Set new content
-    Sec->setOwnedContents(NewRawIds.vec());
+    Sec.setOwnedContents(NewRawIds.vec());
   }
   return Error::success();
 }

>From 874c9cc896bc592c352f43649a46c78f3a6ab4fc Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Fri, 15 Aug 2025 16:13:32 -0700
Subject: [PATCH 05/17] Handle illegal symidx

---
 llvm/lib/ObjCopy/COFF/COFFWriter.cpp          |   5 +
 .../COFF/strip-wrong-winguards.test           | 140 ++++++++++++++++++
 2 files changed, 145 insertions(+)
 create mode 100644 llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test

diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
index 892eb9116cee1..844d22c6a082b 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
@@ -144,6 +144,11 @@ Error COFFWriter::finalizeCFGuardContents() {
         RawIds.size() / 4);
     std::vector<support::ulittle32_t> NewIds;
     for (auto Id : Ids) {
+      if (!SymIdMap.contains(Id))
+        return createStringError(object_error::invalid_symbol_index,
+                                 "section '%s' contains a .symidx (%d) that is "
+                                 "incorrect or was stripped",
+                                 Sec.Name.str().c_str(), Id.value());
       NewIds.push_back(support::ulittle32_t(SymIdMap[Id]));
     }
     ArrayRef<uint8_t> NewRawIds(reinterpret_cast<uint8_t *>(NewIds.data()),
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test b/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test
new file mode 100644
index 0000000000000..d58cfb4bf8c41
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test
@@ -0,0 +1,140 @@
+# RUN: yaml2obj %s -o %t.in.o
+# RUN: llvm-readobj -r -s  -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.in.o | FileCheck %s --check-prefix=ORIG
+# RUN: not llvm-objcopy --strip-debug %t.in.o %t.out.o 2>&1 | FileCheck %s --check-prefix=ERROR
+
+# ORIG:      Relocations [
+# ORIG-NEXT:   Section (1) .text {
+# ORIG-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (10)
+# ORIG-NEXT:   }
+# ORIG-NEXT: ]
+# ORIG:      Symbols [
+# ORIG:          Name: .gfids$y
+# ORIG:          Section: .gfids$y
+# ORIG:          AuxSymbolCount: 1
+# ORIG:          AuxSectionDef {
+# ORIG:            Checksum: 0x459345AD
+# ORIG:          }
+# ORIG:          Name: .giats$y
+# ORIG:          Section: .giats$y
+# ORIG:          AuxSymbolCount: 1
+# ORIG:          AuxSectionDef {
+# ORIG:            Checksum: 0x0
+# ORIG:          }
+# ORIG:          Name: .gljmp$y
+# ORIG:          Section: .gljmp$y
+# ORIG:          AuxSymbolCount: 1
+# ORIG:          AuxSectionDef {
+# ORIG:            Checksum: 0x0
+# ORIG:          }
+# ORIG:      ]
+# ORIG:      Hex dump of section '.gfids$y':
+# ORIG-NEXT: 0x00000000 0a000000 10000000                   ........
+# ORIG:      Hex dump of section '.giats$y':
+# ORIG-EMPTY-NEXT:
+# ORIG:      Hex dump of section '.gljmp$y':
+# ORIG-EMPTY-NEXT:
+
+# ERROR: section '.gfids$y' contains a .symidx (16) that is incorrect or was stripped
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
+    SizeOfRawData:   28
+    Relocations:
+      - VirtualAddress:  3
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_REL32
+  - Name:            '.debug$S'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     04000000F100000044656275672073656374696F6E20746F20626520737472697070656400
+    SizeOfRawData:   37
+  - Name:            '.gfids$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '0A00000010000000'
+    SizeOfRawData:   8
+  - Name:            '.giats$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     ''
+    SizeOfRawData:   0
+  - Name:            '.gljmp$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     ''
+    SizeOfRawData:   0
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          28
+      NumberOfRelocations: 4
+      NumberOfLinenumbers: 0
+      CheckSum:        3583480811
+      Number:          1
+  - Name:            '.debug$S'
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          37
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        2941632545
+      Number:          4
+  - Name:            '.gfids$y'
+    Value:           0
+    SectionNumber:   3
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          8
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        1167279533
+      Number:          5
+  - Name:            '.giats$y'
+    Value:           0
+    SectionNumber:   4
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          6
+  - Name:            '.gljmp$y'
+    Value:           0
+    SectionNumber:   5
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          7
+  - Name:            foo
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...

>From 4e80fb8b5880bc983698114ddb4dad992ea5fc77 Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Fri, 15 Aug 2025 17:43:24 -0700
Subject: [PATCH 06/17] Support .gehcont as well

---
 llvm/lib/ObjCopy/COFF/COFFWriter.cpp          |  10 +-
 llvm/lib/ObjCopy/COFF/COFFWriter.h            |   2 +-
 .../COFF/strip-update-ehcont.test             | 388 ++++++++++++++++++
 3 files changed, 396 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test

diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
index 844d22c6a082b..3be5ac2f2d305 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
@@ -94,13 +94,17 @@ 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";
+    return Name == ".gljmp$y" || Name == ".giats$y" || Name == ".gfids$y" ||
+           Name == ".gehcont$y";
   };
 
   DenseMap<size_t, size_t> SymIdMap;
@@ -254,7 +258,7 @@ Error COFFWriter::finalize(bool IsBigObj) {
     return E;
   if (Error E = finalizeSymbolContents())
     return E;
-  if (Error E = finalizeCFGuardContents())
+  if (Error E = finalizeSymIdxContents())
     return E;
 
   size_t SizeOfHeaders = 0;
diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.h b/llvm/lib/ObjCopy/COFF/COFFWriter.h
index 557dbe4c01b9c..66d7f01c87f18 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.h
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.h
@@ -34,7 +34,7 @@ class COFFWriter {
   template <class SymbolTy> std::pair<size_t, size_t> finalizeSymbolTable();
   Error finalizeRelocTargets();
   Error finalizeSymbolContents();
-  Error finalizeCFGuardContents();
+  Error finalizeSymIdxContents();
   void layoutSections();
   Expected<size_t> finalizeStringTable();
 
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test b/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test
new file mode 100644
index 0000000000000..8f78b6f0eb90f
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test
@@ -0,0 +1,388 @@
+# RUN: yaml2obj %s -o %t.in.o
+
+# RUN: llvm-readobj -r -s  -x '.gehcont$y' %t.in.o | FileCheck %s --check-prefix=ORIG
+# RUN: llvm-objcopy --strip-debug %t.in.o %t.out.o
+# RUN: llvm-readobj -r -s  -x '.gehcont$y' %t.out.o | FileCheck %s --check-prefix=STRIP
+
+# ORIG:      Symbols [
+# ORIG:          Name: .text
+# ORIG:          Name: .data
+# ORIG:          Name: .bss
+# ORIG:          Name: .text
+# ORIG:          Name: ?foo@@YAXXZ
+# ORIG:          Name: .data
+# ORIG:          Name: ??_R0H at 8
+# ORIG:          Name: .debug$S
+# ORIG:          Name: .gehcont$y
+# ORIG:          Name: .pdata
+# ORIG:          Name: @feat.00
+# ORIG:          Name: ?foo2@@YAXXZ
+# ORIG:          Name: $ehgcr_0_1
+# ORIG:      Hex dump of section '.gehcont$y':
+# ORIG-NEXT: 0x00000000 18000000 18000000 18000000          ............
+
+# .debug$S is going to be stripped and $ehgcr_0_1 index is decreased by 2
+
+# STRIP:      Symbols [
+# STRIP:          Name: .text
+# STRIP:          Name: .data
+# STRIP:          Name: .bss
+# STRIP:          Name: .text
+# STRIP:          Name: ?foo@@YAXXZ
+# STRIP:          Name: .data
+# STRIP:          Name: ??_R0H at 8
+# STRIP:          Name: .drectve
+# STRIP:          Name: .gehcont$y
+# STRIP:          Name: .pdata
+# STRIP:          Name: @feat.00
+# STRIP:          Name: ?foo2@@YAXXZ
+# STRIP:          Name: $ehgcr_0_1
+# STRIP:      Hex dump of section '.gehcont$y':
+# STRIP-NEXT: 0x00000000 16000000 16000000 16000000          ............
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     ''
+  - Name:            .data
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       4
+    SectionData:     ''
+  - Name:            .bss
+    Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       4
+    SectionData:     ''
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       16
+    SectionData:     554883EC30488D6C243048C745F0FEFFFFFFE800000000904883C4305DC366904889542410554883EC20488D6A30488D05E2FFFFFF4883C4205DC3
+    SizeOfRawData:   59
+    Relocations:
+      - VirtualAddress:  19
+        SymbolName:      '?foo2@@YAXXZ'
+        Type:            IMAGE_REL_AMD64_REL32
+  - Name:            .data
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       16
+    SectionData:     '000000000000000000000000000000002E48000000000000'
+    SizeOfRawData:   24
+    Relocations:
+      - VirtualAddress:  0
+        SymbolName:      '??_7type_info@@6B@'
+        Type:            IMAGE_REL_AMD64_ADDR64
+  - Name:            .drectve
+    Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ]
+    Alignment:       1
+    SectionData:     202F44454641554C544C49423A6C6962636D742E6C6962202F44454641554C544C49423A6C69626972636D742E6C6962202F44454641554C544C49423A73766D6C5F646973706D742E6C6962202F44454641554C544C49423A6C69626D6D742E6C6962202F44454641554C544C49423A6F6C646E616D65732E6C6962
+    SizeOfRawData:   124
+  - Name:            '.debug$S'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     04000000F1000000380000000A00011100000000000000002A003C1101000000D000EA070000000000000852000000000000536F6D6520646562756720696E666F000000
+    Subsections:
+      - !Symbols
+        Records:
+          - Kind:            S_OBJNAME
+            ObjNameSym:
+              Signature:       0
+              ObjectName:      ''
+          - Kind:            S_COMPILE3
+            Compile3Sym:
+              Flags:           [  ]
+              Machine:         X64
+              FrontendMajor:   2026
+              FrontendMinor:   0
+              FrontendBuild:   0
+              FrontendQFE:     0
+              BackendMajor:    21000
+              BackendMinor:    0
+              BackendBuild:    0
+              BackendQFE:      0
+              Version:         Some debug info
+    SizeOfRawData:   68
+  - Name:            '.gehcont$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '180000001800000018000000'
+    SizeOfRawData:   12
+  - Name:            .xdata
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     190A03350A030552015000000000000000000000190E02000A320650000000000000000022059319020000000000000001000000000000000400000000000000200000000000000001000000FFFFFFFF00000000FFFFFFFF00000000000000000000000001000000010000000000000000000000000000002C000000000000003800000000000000FFFFFFFF130000000000000018000000FFFFFFFF0000000001000000
+    SizeOfRawData:   164
+    Relocations:
+      - VirtualAddress:  12
+        SymbolName:      __CxxFrameHandler3
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  16
+        SymbolName:      '$cppxdata$?foo@@YAXXZ'
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  28
+        SymbolName:      __CxxFrameHandler3
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  32
+        SymbolName:      '$cppxdata$?foo@@YAXXZ'
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  44
+        SymbolName:      '$stateUnwindMap$?foo@@YAXXZ'
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  52
+        SymbolName:      '$tryMap$?foo@@YAXXZ'
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  60
+        SymbolName:      '$ip2state$?foo@@YAXXZ'
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  108
+        SymbolName:      '$handlerMap$0$?foo@@YAXXZ'
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  116
+        SymbolName:      '??_R0H at 8'
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  124
+        SymbolName:      '?catch$2@?0??foo@@YAXXZ at 4HA'
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  132
+        SymbolTableIndex: 6
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  140
+        SymbolTableIndex: 6
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  148
+        SymbolTableIndex: 6
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  156
+        SymbolName:      '?catch$2@?0??foo@@YAXXZ at 4HA'
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+  - Name:            .pdata
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     000000001E00000000000000200000003B00000014000000
+    SizeOfRawData:   24
+    Relocations:
+      - VirtualAddress:  0
+        SymbolTableIndex: 6
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  4
+        SymbolTableIndex: 6
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  8
+        SymbolName:      .xdata
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  12
+        SymbolTableIndex: 6
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  16
+        SymbolTableIndex: 6
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  20
+        SymbolName:      .xdata
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          1
+  - Name:            .data
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          2
+  - Name:            .bss
+    Value:           0
+    SectionNumber:   3
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          3
+  - Name:            .text
+    Value:           0
+    SectionNumber:   4
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          59
+      NumberOfRelocations: 1
+      NumberOfLinenumbers: 0
+      CheckSum:        517419950
+      Number:          4
+      Selection:       IMAGE_COMDAT_SELECT_NODUPLICATES
+  - Name:            '?foo@@YAXXZ'
+    Value:           0
+    SectionNumber:   4
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            .xdata
+    Value:           0
+    SectionNumber:   9
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          164
+      NumberOfRelocations: 14
+      NumberOfLinenumbers: 0
+      CheckSum:        135891540
+      Number:          4
+      Selection:       IMAGE_COMDAT_SELECT_ASSOCIATIVE
+  - Name:            .data
+    Value:           0
+    SectionNumber:   5
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          24
+      NumberOfRelocations: 1
+      NumberOfLinenumbers: 0
+      CheckSum:        2602060666
+      Number:          5
+      Selection:       IMAGE_COMDAT_SELECT_ANY
+  - Name:            '??_R0H at 8'
+    Value:           0
+    SectionNumber:   5
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            .drectve
+    Value:           0
+    SectionNumber:   6
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          124
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        1122646683
+      Number:          6
+  - Name:            '.debug$S'
+    Value:           0
+    SectionNumber:   7
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          68
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        82254810
+      Number:          7
+  - Name:            '.gehcont$y'
+    Value:           0
+    SectionNumber:   8
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          12
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        820498156
+      Number:          8
+  - Name:            .pdata
+    Value:           0
+    SectionNumber:   10
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          24
+      NumberOfRelocations: 6
+      NumberOfLinenumbers: 0
+      CheckSum:        3872633945
+      Number:          4
+      Selection:       IMAGE_COMDAT_SELECT_ASSOCIATIVE
+  - Name:            '@feat.00'
+    Value:           16384
+    SectionNumber:   -1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            '?foo2@@YAXXZ'
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            '$ehgcr_0_1'
+    Value:           23
+    SectionNumber:   4
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            __CxxFrameHandler3
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            '$cppxdata$?foo@@YAXXZ'
+    Value:           36
+    SectionNumber:   9
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            '?catch$2@?0??foo@@YAXXZ at 4HA'
+    Value:           32
+    SectionNumber:   4
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            '$stateUnwindMap$?foo@@YAXXZ'
+    Value:           76
+    SectionNumber:   9
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            '$tryMap$?foo@@YAXXZ'
+    Value:           92
+    SectionNumber:   9
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            '$ip2state$?foo@@YAXXZ'
+    Value:           132
+    SectionNumber:   9
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            '$handlerMap$0$?foo@@YAXXZ'
+    Value:           112
+    SectionNumber:   9
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            '??_7type_info@@6B@'
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...

>From 1a8f13cc5d79ced81190cb3117b2d13189dbb15c Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Sun, 17 Aug 2025 13:30:12 -0700
Subject: [PATCH 07/17] Reduce some of the tests

---
 .../COFF/strip-update-ehcont.test             | 203 +-----------------
 .../COFF/strip-wrong-winguards.test           |  58 +----
 2 files changed, 12 insertions(+), 249 deletions(-)

diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test b/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test
index 8f78b6f0eb90f..32382c671b22e 100644
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test
@@ -11,15 +11,13 @@
 # ORIG:          Name: .text
 # ORIG:          Name: ?foo@@YAXXZ
 # ORIG:          Name: .data
-# ORIG:          Name: ??_R0H at 8
+# ORIG:          Name: .drectve
 # ORIG:          Name: .debug$S
 # ORIG:          Name: .gehcont$y
-# ORIG:          Name: .pdata
-# ORIG:          Name: @feat.00
 # ORIG:          Name: ?foo2@@YAXXZ
 # ORIG:          Name: $ehgcr_0_1
 # ORIG:      Hex dump of section '.gehcont$y':
-# ORIG-NEXT: 0x00000000 18000000 18000000 18000000          ............
+# ORIG-NEXT: 0x00000000 12000000 12000000 12000000          ............
 
 # .debug$S is going to be stripped and $ehgcr_0_1 index is decreased by 2
 
@@ -30,15 +28,12 @@
 # STRIP:          Name: .text
 # STRIP:          Name: ?foo@@YAXXZ
 # STRIP:          Name: .data
-# STRIP:          Name: ??_R0H at 8
 # STRIP:          Name: .drectve
 # STRIP:          Name: .gehcont$y
-# STRIP:          Name: .pdata
-# STRIP:          Name: @feat.00
 # STRIP:          Name: ?foo2@@YAXXZ
 # STRIP:          Name: $ehgcr_0_1
 # STRIP:      Hex dump of section '.gehcont$y':
-# STRIP-NEXT: 0x00000000 16000000 16000000 16000000          ............
+# STRIP-NEXT: 0x00000000 10000000 10000000 10000000          ............
 
 --- !COFF
 header:
@@ -71,10 +66,6 @@ sections:
     Alignment:       16
     SectionData:     '000000000000000000000000000000002E48000000000000'
     SizeOfRawData:   24
-    Relocations:
-      - VirtualAddress:  0
-        SymbolName:      '??_7type_info@@6B@'
-        Type:            IMAGE_REL_AMD64_ADDR64
   - Name:            .drectve
     Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ]
     Alignment:       1
@@ -83,105 +74,13 @@ sections:
   - Name:            '.debug$S'
     Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
     Alignment:       4
-    SectionData:     04000000F1000000380000000A00011100000000000000002A003C1101000000D000EA070000000000000852000000000000536F6D6520646562756720696E666F000000
-    Subsections:
-      - !Symbols
-        Records:
-          - Kind:            S_OBJNAME
-            ObjNameSym:
-              Signature:       0
-              ObjectName:      ''
-          - Kind:            S_COMPILE3
-            Compile3Sym:
-              Flags:           [  ]
-              Machine:         X64
-              FrontendMajor:   2026
-              FrontendMinor:   0
-              FrontendBuild:   0
-              FrontendQFE:     0
-              BackendMajor:    21000
-              BackendMinor:    0
-              BackendBuild:    0
-              BackendQFE:      0
-              Version:         Some debug info
-    SizeOfRawData:   68
+    SectionData:     04000000F100000044656275672073656374696F6E20746F20626520737472697070656400
+    SizeOfRawData:   37
   - Name:            '.gehcont$y'
     Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
     Alignment:       4
-    SectionData:     '180000001800000018000000'
+    SectionData:     '120000001200000012000000'
     SizeOfRawData:   12
-  - Name:            .xdata
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     190A03350A030552015000000000000000000000190E02000A320650000000000000000022059319020000000000000001000000000000000400000000000000200000000000000001000000FFFFFFFF00000000FFFFFFFF00000000000000000000000001000000010000000000000000000000000000002C000000000000003800000000000000FFFFFFFF130000000000000018000000FFFFFFFF0000000001000000
-    SizeOfRawData:   164
-    Relocations:
-      - VirtualAddress:  12
-        SymbolName:      __CxxFrameHandler3
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  16
-        SymbolName:      '$cppxdata$?foo@@YAXXZ'
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  28
-        SymbolName:      __CxxFrameHandler3
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  32
-        SymbolName:      '$cppxdata$?foo@@YAXXZ'
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  44
-        SymbolName:      '$stateUnwindMap$?foo@@YAXXZ'
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  52
-        SymbolName:      '$tryMap$?foo@@YAXXZ'
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  60
-        SymbolName:      '$ip2state$?foo@@YAXXZ'
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  108
-        SymbolName:      '$handlerMap$0$?foo@@YAXXZ'
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  116
-        SymbolName:      '??_R0H at 8'
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  124
-        SymbolName:      '?catch$2@?0??foo@@YAXXZ at 4HA'
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  132
-        SymbolTableIndex: 6
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  140
-        SymbolTableIndex: 6
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  148
-        SymbolTableIndex: 6
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  156
-        SymbolName:      '?catch$2@?0??foo@@YAXXZ at 4HA'
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-  - Name:            .pdata
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     000000001E00000000000000200000003B00000014000000
-    SizeOfRawData:   24
-    Relocations:
-      - VirtualAddress:  0
-        SymbolTableIndex: 6
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  4
-        SymbolTableIndex: 6
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  8
-        SymbolName:      .xdata
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  12
-        SymbolTableIndex: 6
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  16
-        SymbolTableIndex: 6
-        Type:            IMAGE_REL_AMD64_ADDR32NB
-      - VirtualAddress:  20
-        SymbolName:      .xdata
-        Type:            IMAGE_REL_AMD64_ADDR32NB
 symbols:
   - Name:            .text
     Value:           0
@@ -238,19 +137,6 @@ symbols:
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
     StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            .xdata
-    Value:           0
-    SectionNumber:   9
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          164
-      NumberOfRelocations: 14
-      NumberOfLinenumbers: 0
-      CheckSum:        135891540
-      Number:          4
-      Selection:       IMAGE_COMDAT_SELECT_ASSOCIATIVE
   - Name:            .data
     Value:           0
     SectionNumber:   5
@@ -264,12 +150,6 @@ symbols:
       CheckSum:        2602060666
       Number:          5
       Selection:       IMAGE_COMDAT_SELECT_ANY
-  - Name:            '??_R0H at 8'
-    Value:           0
-    SectionNumber:   5
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
   - Name:            .drectve
     Value:           0
     SectionNumber:   6
@@ -289,10 +169,10 @@ symbols:
     ComplexType:     IMAGE_SYM_DTYPE_NULL
     StorageClass:    IMAGE_SYM_CLASS_STATIC
     SectionDefinition:
-      Length:          68
+      Length:          37
       NumberOfRelocations: 0
       NumberOfLinenumbers: 0
-      CheckSum:        82254810
+      CheckSum:        820498156
       Number:          7
   - Name:            '.gehcont$y'
     Value:           0
@@ -306,25 +186,6 @@ symbols:
       NumberOfLinenumbers: 0
       CheckSum:        820498156
       Number:          8
-  - Name:            .pdata
-    Value:           0
-    SectionNumber:   10
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          24
-      NumberOfRelocations: 6
-      NumberOfLinenumbers: 0
-      CheckSum:        3872633945
-      Number:          4
-      Selection:       IMAGE_COMDAT_SELECT_ASSOCIATIVE
-  - Name:            '@feat.00'
-    Value:           16384
-    SectionNumber:   -1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
   - Name:            '?foo2@@YAXXZ'
     Value:           0
     SectionNumber:   0
@@ -337,52 +198,4 @@ symbols:
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_NULL
     StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            __CxxFrameHandler3
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            '$cppxdata$?foo@@YAXXZ'
-    Value:           36
-    SectionNumber:   9
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            '?catch$2@?0??foo@@YAXXZ at 4HA'
-    Value:           32
-    SectionNumber:   4
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            '$stateUnwindMap$?foo@@YAXXZ'
-    Value:           76
-    SectionNumber:   9
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            '$tryMap$?foo@@YAXXZ'
-    Value:           92
-    SectionNumber:   9
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            '$ip2state$?foo@@YAXXZ'
-    Value:           132
-    SectionNumber:   9
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            '$handlerMap$0$?foo@@YAXXZ'
-    Value:           112
-    SectionNumber:   9
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-  - Name:            '??_7type_info@@6B@'
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
 ...
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test b/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test
index d58cfb4bf8c41..4e99c1f59a7a3 100644
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test
@@ -1,10 +1,10 @@
 # RUN: yaml2obj %s -o %t.in.o
-# RUN: llvm-readobj -r -s  -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.in.o | FileCheck %s --check-prefix=ORIG
+# RUN: llvm-readobj -r -s  -x '.gfids$y' %t.in.o | FileCheck %s --check-prefix=ORIG
 # RUN: not llvm-objcopy --strip-debug %t.in.o %t.out.o 2>&1 | FileCheck %s --check-prefix=ERROR
 
 # ORIG:      Relocations [
 # ORIG-NEXT:   Section (1) .text {
-# ORIG-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (10)
+# ORIG-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (6)
 # ORIG-NEXT:   }
 # ORIG-NEXT: ]
 # ORIG:      Symbols [
@@ -14,25 +14,9 @@
 # ORIG:          AuxSectionDef {
 # ORIG:            Checksum: 0x459345AD
 # ORIG:          }
-# ORIG:          Name: .giats$y
-# ORIG:          Section: .giats$y
-# ORIG:          AuxSymbolCount: 1
-# ORIG:          AuxSectionDef {
-# ORIG:            Checksum: 0x0
-# ORIG:          }
-# ORIG:          Name: .gljmp$y
-# ORIG:          Section: .gljmp$y
-# ORIG:          AuxSymbolCount: 1
-# ORIG:          AuxSectionDef {
-# ORIG:            Checksum: 0x0
-# ORIG:          }
 # ORIG:      ]
 # ORIG:      Hex dump of section '.gfids$y':
-# ORIG-NEXT: 0x00000000 0a000000 10000000                   ........
-# ORIG:      Hex dump of section '.giats$y':
-# ORIG-EMPTY-NEXT:
-# ORIG:      Hex dump of section '.gljmp$y':
-# ORIG-EMPTY-NEXT:
+# ORIG-NEXT: 0x00000000 06000000 10000000                   ........
 
 # ERROR: section '.gfids$y' contains a .symidx (16) that is incorrect or was stripped
 
@@ -58,18 +42,8 @@ sections:
   - Name:            '.gfids$y'
     Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
     Alignment:       4
-    SectionData:     '0A00000010000000'
+    SectionData:     '0600000010000000'
     SizeOfRawData:   8
-  - Name:            '.giats$y'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     ''
-    SizeOfRawData:   0
-  - Name:            '.gljmp$y'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     ''
-    SizeOfRawData:   0
 symbols:
   - Name:            .text
     Value:           0
@@ -107,30 +81,6 @@ symbols:
       NumberOfLinenumbers: 0
       CheckSum:        1167279533
       Number:          5
-  - Name:            '.giats$y'
-    Value:           0
-    SectionNumber:   4
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          0
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        0
-      Number:          6
-  - Name:            '.gljmp$y'
-    Value:           0
-    SectionNumber:   5
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          0
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        0
-      Number:          7
   - Name:            foo
     Value:           0
     SectionNumber:   0

>From 5f21fe556b5121ea1b7694effa02f3a05d45ff37 Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Wed, 20 Aug 2025 08:40:02 -0700
Subject: [PATCH 08/17] More test coverage for incorrect symbols

---
 .../COFF/strip-invalid-symidx-section-1.test  | 75 +++++++++++++++
 .../COFF/strip-invalid-symidx-section-2.test  | 95 +++++++++++++++++++
 .../COFF/strip-invalid-symidx-section-3.test  | 86 +++++++++++++++++
 3 files changed, 256 insertions(+)
 create mode 100644 llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-1.test
 create mode 100644 llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-2.test
 create mode 100644 llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-3.test

diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-1.test b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-1.test
new file mode 100644
index 0000000000000..4ae6724b9efae
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-1.test
@@ -0,0 +1,75 @@
+# Bail out if a section consisting of symidx has unexpected format for patching
+# its content and updating a check sum. Basically, the expected format is a
+# definitive symbol with a zero offset that is linked with section.
+# In the case, the symbol .gfids$y is not present at all.
+
+# RUN: yaml2obj %s -o %t.in.o
+# RUN: llvm-readobj -r -s -x '.gfids$y' %t.in.o | FileCheck %s --check-prefix=ORIG
+# RUN: not llvm-objcopy --strip-debug %t.in.o %t.out.o 2>&1 | FileCheck %s --check-prefix=ERROR
+
+# ORIG:      Relocations [
+# ORIG-NEXT:   Section (1) .text {
+# ORIG-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (4)
+# ORIG-NEXT:   }
+# ORIG-NEXT: ]
+# ORIG:      Hex dump of section '.gfids$y':
+# ORIG-NEXT: 0x00000000 04000000
+
+# ERROR: section '.gfids$y' does not have the corresponding symbol or the symbol has unexpected format
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
+    SizeOfRawData:   28
+    Relocations:
+      - VirtualAddress:  3
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_REL32
+  - Name:            '.debug$S'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     04000000F100000044656275672073656374696F6E20746F20626520737472697070656400
+    SizeOfRawData:   37
+  - Name:            '.gfids$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '04000000'
+    SizeOfRawData:   8
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          28
+      NumberOfRelocations: 4
+      NumberOfLinenumbers: 0
+      CheckSum:        3583480811
+      Number:          1
+  - Name:            '.debug$S'
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          37
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        2941632545
+      Number:          4
+  - Name:            foo
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-2.test b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-2.test
new file mode 100644
index 0000000000000..cede7a5143fca
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-2.test
@@ -0,0 +1,95 @@
+# Bail out if a section consisting of symidx has unexpected format for patching
+# its content and updating a check sum. Basically, the expected format is a
+# definitive symbol with a zero offset that is linked with section.
+# In the case, the symbol .giats$y has a non-zero offset.
+
+# RUN: yaml2obj %s -o %t.in.o
+# RUN: llvm-readobj -r -s -x '.giats$y' %t.in.o | FileCheck %s --check-prefix=ORIG
+# RUN: not llvm-objcopy --strip-debug %t.in.o %t.out.o 2>&1 | FileCheck %s --check-prefix=ERROR
+
+# ORIG:      Relocations [
+# ORIG-NEXT:   Section (1) .text {
+# ORIG-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (6)
+# ORIG-NEXT:   }
+# ORIG-NEXT: ]
+# ORIG:      Symbols [
+# ORIG:          Name: .giats$y
+# ORIG:          Section: .giats$y
+# ORIG:          AuxSymbolCount: 1
+# ORIG:          AuxSectionDef {
+# ORIG:            Checksum: 0x459345AD
+# ORIG:          }
+# ORIG:      ]
+# ORIG:      Hex dump of section '.giats$y':
+# ORIG-NEXT: 0x00000000 06000000 10000000                   ........
+
+# ERROR: section '.giats$y' does not have the corresponding symbol or the symbol has unexpected format
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
+    SizeOfRawData:   28
+    Relocations:
+      - VirtualAddress:  3
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_REL32
+  - Name:            '.debug$S'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     04000000F100000044656275672073656374696F6E20746F20626520737472697070656400
+    SizeOfRawData:   37
+  - Name:            '.giats$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '0600000010000000'
+    SizeOfRawData:   8
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          28
+      NumberOfRelocations: 4
+      NumberOfLinenumbers: 0
+      CheckSum:        3583480811
+      Number:          1
+  - Name:            '.debug$S'
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          37
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        2941632545
+      Number:          4
+  - Name:            '.giats$y'
+    Value:           42
+    SectionNumber:   3
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          8
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        1167279533
+      Number:          5
+  - Name:            foo
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-3.test b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-3.test
new file mode 100644
index 0000000000000..60bbc154024e8
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-3.test
@@ -0,0 +1,86 @@
+# Bail out if a section consisting of symidx has unexpected format for patching
+# its content and updating a check sum. Basically, the expected format is a
+# definitive symbol with a zero offset that is linked with section.
+# In the case, the symbol .gljmp$y has a non-static storage class.
+
+# RUN: yaml2obj %s -o %t.in.o
+# RUN: llvm-readobj -r -s -x '.gljmp$y' %t.in.o | FileCheck %s --check-prefix=ORIG
+# RUN: not llvm-objcopy --strip-debug %t.in.o %t.out.o 2>&1 | FileCheck %s --check-prefix=ERROR
+
+# ORIG:      Relocations [
+# ORIG-NEXT:   Section (1) .text {
+# ORIG-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (5)
+# ORIG-NEXT:   }
+# ORIG-NEXT: ]
+# ORIG:      Symbols [
+# ORIG:          Name: .gljmp$y
+# ORIG:          Section: .gljmp$y
+# ORIG:          AuxSymbolCount: 0
+# ORIG:      ]
+# ORIG:      Hex dump of section '.gljmp$y':
+# ORIG-NEXT: 0x00000000 06000000 10000000                   ........
+
+# ERROR: section '.gljmp$y' does not have the corresponding symbol or the symbol has unexpected format
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
+    SizeOfRawData:   28
+    Relocations:
+      - VirtualAddress:  3
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_REL32
+  - Name:            '.debug$S'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     04000000F100000044656275672073656374696F6E20746F20626520737472697070656400
+    SizeOfRawData:   37
+  - Name:            '.gljmp$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '0600000010000000'
+    SizeOfRawData:   8
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          28
+      NumberOfRelocations: 4
+      NumberOfLinenumbers: 0
+      CheckSum:        3583480811
+      Number:          1
+  - Name:            '.debug$S'
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          37
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        2941632545
+      Number:          4
+  - Name:            '.gljmp$y'
+    Value:           0
+    SectionNumber:   3
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            foo
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...

>From cbb583211b5f4fea2bb6d7038a758b3a555992b9 Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Wed, 20 Aug 2025 08:41:13 -0700
Subject: [PATCH 09/17] Add description to the tests

---
 .../tools/llvm-objcopy/COFF/strip-update-ehcont.test   | 10 +++++++---
 .../llvm-objcopy/COFF/strip-update-winguards.test      |  8 ++++++--
 .../tools/llvm-objcopy/COFF/strip-wrong-winguards.test |  6 +++++-
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test b/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test
index 32382c671b22e..8fa19ee30ee57 100644
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test
@@ -1,8 +1,12 @@
+# .gehcont$y section consists of .symidx directives. The test checks that
+# indices in the section are updated after stripping as the symbol table could
+# be changed during stripping.
+
 # RUN: yaml2obj %s -o %t.in.o
 
-# RUN: llvm-readobj -r -s  -x '.gehcont$y' %t.in.o | FileCheck %s --check-prefix=ORIG
+# RUN: llvm-readobj -r -s -x '.gehcont$y' %t.in.o | FileCheck %s --check-prefix=ORIG
 # RUN: llvm-objcopy --strip-debug %t.in.o %t.out.o
-# RUN: llvm-readobj -r -s  -x '.gehcont$y' %t.out.o | FileCheck %s --check-prefix=STRIP
+# RUN: llvm-readobj -r -s -x '.gehcont$y' %t.out.o | FileCheck %s --check-prefix=STRIP
 
 # ORIG:      Symbols [
 # ORIG:          Name: .text
@@ -19,7 +23,7 @@
 # ORIG:      Hex dump of section '.gehcont$y':
 # ORIG-NEXT: 0x00000000 12000000 12000000 12000000          ............
 
-# .debug$S is going to be stripped and $ehgcr_0_1 index is decreased by 2
+# .debug$S is going to be stripped and index of $ehgcr_0_1 is decreased by 2
 
 # STRIP:      Symbols [
 # STRIP:          Name: .text
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-update-winguards.test b/llvm/test/tools/llvm-objcopy/COFF/strip-update-winguards.test
index 4dc1a821dbd6a..563e606104b12 100644
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-update-winguards.test
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-update-winguards.test
@@ -1,8 +1,12 @@
+# .gfids$y, .giats$y and .gljmp$y cf-guard sections consist of .symidx
+# directives. The test checks that indices in the section are updated after
+# stripping as the symbol table could be changed during stripping.
+
 # RUN: yaml2obj %s -o %t.in.o
 
-# RUN: llvm-readobj -r -s  -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.in.o | FileCheck %s --check-prefix=ORIG
+# RUN: llvm-readobj -r -s -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.in.o | FileCheck %s --check-prefix=ORIG
 # RUN: llvm-objcopy --strip-debug %t.in.o %t.out.o
-# RUN: llvm-readobj -r -s  -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.out.o | FileCheck %s --check-prefix=STRIP
+# RUN: llvm-readobj -r -s -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.out.o | FileCheck %s --check-prefix=STRIP
 
 # ORIG:      Relocations [
 # ORIG-NEXT:   Section (1) .text {
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test b/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test
index 4e99c1f59a7a3..32bfa7480c72c 100644
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test
@@ -1,5 +1,9 @@
+# Bail out if .gfids$y contains symbol index that is not present in the symbol
+# table.  Generally it should be the same for every section consisting of
+# .symidx directives, e.g .giats$y, .gljmp$y and .gehcont$y.
+
 # RUN: yaml2obj %s -o %t.in.o
-# RUN: llvm-readobj -r -s  -x '.gfids$y' %t.in.o | FileCheck %s --check-prefix=ORIG
+# RUN: llvm-readobj -r -s -x '.gfids$y' %t.in.o | FileCheck %s --check-prefix=ORIG
 # RUN: not llvm-objcopy --strip-debug %t.in.o %t.out.o 2>&1 | FileCheck %s --check-prefix=ERROR
 
 # ORIG:      Relocations [

>From 7b2e3b103dd2addb6c2fa9e857c54154aca3226a Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Wed, 20 Aug 2025 08:43:02 -0700
Subject: [PATCH 10/17] NeedUpdate should be set true if a symbol changed
 RawIndex

---
 llvm/lib/ObjCopy/COFF/COFFWriter.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
index 3be5ac2f2d305..b251593410641 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
@@ -110,8 +110,8 @@ Error COFFWriter::finalizeSymIdxContents() {
   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;
+  for (Symbol &Sym : Obj.getMutableSymbols()) {
+    NeedUpdate |= Sym.OriginalRawIndex != Sym.RawIndex;
     SymIdMap[Sym.OriginalRawIndex] = Sym.RawIndex;
 
     // We collect only definition symbols of the sections to update checksum

>From 48013989da3fb033ef8468571e7f85f32b62ef50 Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Wed, 20 Aug 2025 08:44:22 -0700
Subject: [PATCH 11/17] Use iterators instead of double lookup

---
 llvm/lib/ObjCopy/COFF/COFFWriter.cpp | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
index b251593410641..e93bb3a5fc7f5 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
@@ -126,7 +126,7 @@ Error COFFWriter::finalizeSymIdxContents() {
   if (!NeedUpdate)
     return Error::success();
 
-  for (auto &Sec : Obj.getMutableSections()) {
+  for (Section &Sec : Obj.getMutableSections()) {
     if (!IsSymIdxSection(Sec.Name))
       continue;
 
@@ -136,7 +136,8 @@ Error COFFWriter::finalizeSymIdxContents() {
     if (RawIds.size() == 0)
       continue;
 
-    if (!SecIdMap.contains(Sec.UniqueId))
+    auto SecDefIt = SecIdMap.find(Sec.UniqueId);
+    if (SecDefIt == SecIdMap.end())
       return createStringError(object_error::invalid_symbol_index,
                                "section '%s' does not have the corresponding "
                                "symbol or the symbol has unexpected format",
@@ -147,21 +148,22 @@ Error COFFWriter::finalizeSymIdxContents() {
         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))
+    for (support::ulittle32_t Id : Ids) {
+      auto SymIdIt = SymIdMap.find(Id);
+      if (SymIdIt == SymIdMap.end())
         return createStringError(object_error::invalid_symbol_index,
                                  "section '%s' contains a .symidx (%d) that is "
                                  "incorrect or was stripped",
                                  Sec.Name.str().c_str(), Id.value());
-      NewIds.push_back(support::ulittle32_t(SymIdMap[Id]));
+      NewIds.push_back(support::ulittle32_t(SymIdIt->getSecond()));
     }
     ArrayRef<uint8_t> NewRawIds(reinterpret_cast<uint8_t *>(NewIds.data()),
                                 RawIds.size());
     // Update check sum
     JamCRC JC(/*Init=*/0);
     JC.update(NewRawIds);
-    SecIdMap[Sec.UniqueId]->CheckSum = JC.getCRC();
-    // Set new content
+    SecDefIt->getSecond()->CheckSum = JC.getCRC();
+    // Set new content.
     Sec.setOwnedContents(NewRawIds.vec());
   }
   return Error::success();

>From 2838c933d200cacad4501149f43e046875884955 Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Wed, 20 Aug 2025 08:45:42 -0700
Subject: [PATCH 12/17] Fix comments and order of checks for a symbol

---
 llvm/lib/ObjCopy/COFF/COFFWriter.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
index e93bb3a5fc7f5..1f122aaf433d3 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
@@ -95,7 +95,7 @@ Error COFFWriter::finalizeSymbolContents() {
 }
 
 Error COFFWriter::finalizeSymIdxContents() {
-  // CFGuards shouldn't be present in PE
+  // CFGuards shouldn't be present in PE.
   if (Obj.IsPE)
     return Error::success();
 
@@ -114,9 +114,9 @@ Error COFFWriter::finalizeSymIdxContents() {
     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 &&
+    // We collect only definition symbols of the sections to update checksum.
+    if (Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC &&
+        Sym.Sym.NumberOfAuxSymbols == 1 && Sym.Sym.Value == 0 &&
         IsSymIdxSection(Sym.Name))
       SecIdMap[Sym.TargetSectionId] =
           reinterpret_cast<coff_aux_section_definition *>(
@@ -143,7 +143,7 @@ Error COFFWriter::finalizeSymIdxContents() {
                                "symbol or the symbol has unexpected format",
                                Sec.Name.str().c_str());
 
-    // Create updated content
+    // Create updated content.
     ArrayRef<support::ulittle32_t> Ids(
         reinterpret_cast<const support::ulittle32_t *>(RawIds.data()),
         RawIds.size() / 4);
@@ -159,7 +159,7 @@ Error COFFWriter::finalizeSymIdxContents() {
     }
     ArrayRef<uint8_t> NewRawIds(reinterpret_cast<uint8_t *>(NewIds.data()),
                                 RawIds.size());
-    // Update check sum
+    // Update check sum.
     JamCRC JC(/*Init=*/0);
     JC.update(NewRawIds);
     SecDefIt->getSecond()->CheckSum = JC.getCRC();

>From 73186e3dd6ced6be04498b93f7f1fd3854fdac53 Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Wed, 20 Aug 2025 08:56:38 -0700
Subject: [PATCH 13/17] Make OriginalRawIndex a class member

---
 llvm/lib/ObjCopy/COFF/COFFObject.cpp | 5 ++---
 llvm/lib/ObjCopy/COFF/COFFObject.h   | 1 +
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/ObjCopy/COFF/COFFObject.cpp b/llvm/lib/ObjCopy/COFF/COFFObject.cpp
index fcb1bbfe91332..91cf7e32a7396 100644
--- a/llvm/lib/ObjCopy/COFF/COFFObject.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFObject.cpp
@@ -16,11 +16,10 @@ namespace coff {
 using namespace object;
 
 void Object::addSymbols(ArrayRef<Symbol> NewSymbols) {
-  size_t RawIndex = 0;
   for (Symbol S : NewSymbols) {
     S.UniqueId = NextSymbolUniqueId++;
-    S.OriginalRawIndex = RawIndex;
-    RawIndex += 1 + S.Sym.NumberOfAuxSymbols;
+    S.OriginalRawIndex = NextSymbolOriginalIndex;
+    NextSymbolOriginalIndex += 1 + S.Sym.NumberOfAuxSymbols;
     Symbols.emplace_back(S);
   }
   updateSymbols();
diff --git a/llvm/lib/ObjCopy/COFF/COFFObject.h b/llvm/lib/ObjCopy/COFF/COFFObject.h
index 9f88d45962513..6b70add1bb1b7 100644
--- a/llvm/lib/ObjCopy/COFF/COFFObject.h
+++ b/llvm/lib/ObjCopy/COFF/COFFObject.h
@@ -141,6 +141,7 @@ struct Object {
   DenseMap<size_t, Symbol *> SymbolMap;
 
   size_t NextSymbolUniqueId = 0;
+  size_t NextSymbolOriginalIndex = 0;
 
   std::vector<Section> Sections;
   DenseMap<ssize_t, Section *> SectionMap;

>From ee600094743399b4fe2020f5296ea6cbd3b288fa Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Thu, 21 Aug 2025 07:07:50 -0700
Subject: [PATCH 14/17] Same symbols indices doesn't mean sections are good

---
 llvm/lib/ObjCopy/COFF/COFFWriter.cpp | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
index 1f122aaf433d3..c94588f91a147 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
@@ -109,9 +109,7 @@ Error COFFWriter::finalizeSymIdxContents() {
 
   DenseMap<size_t, size_t> SymIdMap;
   SmallDenseMap<ssize_t, coff_aux_section_definition *, 4> SecIdMap;
-  bool NeedUpdate = false;
   for (Symbol &Sym : Obj.getMutableSymbols()) {
-    NeedUpdate |= Sym.OriginalRawIndex != Sym.RawIndex;
     SymIdMap[Sym.OriginalRawIndex] = Sym.RawIndex;
 
     // We collect only definition symbols of the sections to update checksum.
@@ -123,9 +121,6 @@ Error COFFWriter::finalizeSymIdxContents() {
               Sym.AuxData[0].Opaque);
   }
 
-  if (!NeedUpdate)
-    return Error::success();
-
   for (Section &Sec : Obj.getMutableSections()) {
     if (!IsSymIdxSection(Sec.Name))
       continue;

>From 4c0f90f760ab6890b551b3a43a054eefcfd5e8b5 Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Fri, 22 Aug 2025 10:48:15 -0700
Subject: [PATCH 15/17] Update tests and comments

---
 llvm/lib/ObjCopy/COFF/COFFWriter.cpp          |   6 +-
 .../COFF/strip-invalid-symidx-section-1.test  |  75 ------
 .../COFF/strip-invalid-symidx-section-2.test  |  95 -------
 .../COFF/strip-invalid-symidx-section-3.test  |  86 -------
 .../COFF/strip-invalid-symidx-section.test    | 225 ++++++++++++++++
 ....test => strip-update-symidx-section.test} | 127 ++++++---
 .../COFF/strip-update-winguards.test          | 241 ------------------
 .../COFF/strip-wrong-winguards.test           |  94 -------
 8 files changed, 321 insertions(+), 628 deletions(-)
 delete mode 100644 llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-1.test
 delete mode 100644 llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-2.test
 delete mode 100644 llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-3.test
 create mode 100644 llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section.test
 rename llvm/test/tools/llvm-objcopy/COFF/{strip-update-ehcont.test => strip-update-symidx-section.test} (64%)
 delete mode 100644 llvm/test/tools/llvm-objcopy/COFF/strip-update-winguards.test
 delete mode 100644 llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test

diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
index c94588f91a147..5b5407d5172f7 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
@@ -112,7 +112,7 @@ Error COFFWriter::finalizeSymIdxContents() {
   for (Symbol &Sym : Obj.getMutableSymbols()) {
     SymIdMap[Sym.OriginalRawIndex] = Sym.RawIndex;
 
-    // We collect only definition symbols of the sections to update checksum.
+    // We collect only definition symbols of the sections to update the checksums.
     if (Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC &&
         Sym.Sym.NumberOfAuxSymbols == 1 && Sym.Sym.Value == 0 &&
         IsSymIdxSection(Sym.Name))
@@ -126,7 +126,7 @@ Error COFFWriter::finalizeSymIdxContents() {
       continue;
 
     ArrayRef<uint8_t> RawIds = Sec.getContents();
-    // Nothing to do and also CheckSum will be -1 instead of 0 if we recalculate
+    // Nothing to do and also the checksum will be -1 instead of 0 if we recalculate
     // it on empty input.
     if (RawIds.size() == 0)
       continue;
@@ -154,7 +154,7 @@ Error COFFWriter::finalizeSymIdxContents() {
     }
     ArrayRef<uint8_t> NewRawIds(reinterpret_cast<uint8_t *>(NewIds.data()),
                                 RawIds.size());
-    // Update check sum.
+    // Update the checksum.
     JamCRC JC(/*Init=*/0);
     JC.update(NewRawIds);
     SecDefIt->getSecond()->CheckSum = JC.getCRC();
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-1.test b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-1.test
deleted file mode 100644
index 4ae6724b9efae..0000000000000
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-1.test
+++ /dev/null
@@ -1,75 +0,0 @@
-# Bail out if a section consisting of symidx has unexpected format for patching
-# its content and updating a check sum. Basically, the expected format is a
-# definitive symbol with a zero offset that is linked with section.
-# In the case, the symbol .gfids$y is not present at all.
-
-# RUN: yaml2obj %s -o %t.in.o
-# RUN: llvm-readobj -r -s -x '.gfids$y' %t.in.o | FileCheck %s --check-prefix=ORIG
-# RUN: not llvm-objcopy --strip-debug %t.in.o %t.out.o 2>&1 | FileCheck %s --check-prefix=ERROR
-
-# ORIG:      Relocations [
-# ORIG-NEXT:   Section (1) .text {
-# ORIG-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (4)
-# ORIG-NEXT:   }
-# ORIG-NEXT: ]
-# ORIG:      Hex dump of section '.gfids$y':
-# ORIG-NEXT: 0x00000000 04000000
-
-# ERROR: section '.gfids$y' does not have the corresponding symbol or the symbol has unexpected format
-
---- !COFF
-header:
-  Machine:         IMAGE_FILE_MACHINE_AMD64
-  Characteristics: [  ]
-sections:
-  - Name:            .text
-    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
-    SizeOfRawData:   28
-    Relocations:
-      - VirtualAddress:  3
-        SymbolName:      foo
-        Type:            IMAGE_REL_AMD64_REL32
-  - Name:            '.debug$S'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     04000000F100000044656275672073656374696F6E20746F20626520737472697070656400
-    SizeOfRawData:   37
-  - Name:            '.gfids$y'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     '04000000'
-    SizeOfRawData:   8
-symbols:
-  - Name:            .text
-    Value:           0
-    SectionNumber:   1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          28
-      NumberOfRelocations: 4
-      NumberOfLinenumbers: 0
-      CheckSum:        3583480811
-      Number:          1
-  - Name:            '.debug$S'
-    Value:           0
-    SectionNumber:   2
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          37
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        2941632545
-      Number:          4
-  - Name:            foo
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-...
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-2.test b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-2.test
deleted file mode 100644
index cede7a5143fca..0000000000000
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-2.test
+++ /dev/null
@@ -1,95 +0,0 @@
-# Bail out if a section consisting of symidx has unexpected format for patching
-# its content and updating a check sum. Basically, the expected format is a
-# definitive symbol with a zero offset that is linked with section.
-# In the case, the symbol .giats$y has a non-zero offset.
-
-# RUN: yaml2obj %s -o %t.in.o
-# RUN: llvm-readobj -r -s -x '.giats$y' %t.in.o | FileCheck %s --check-prefix=ORIG
-# RUN: not llvm-objcopy --strip-debug %t.in.o %t.out.o 2>&1 | FileCheck %s --check-prefix=ERROR
-
-# ORIG:      Relocations [
-# ORIG-NEXT:   Section (1) .text {
-# ORIG-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (6)
-# ORIG-NEXT:   }
-# ORIG-NEXT: ]
-# ORIG:      Symbols [
-# ORIG:          Name: .giats$y
-# ORIG:          Section: .giats$y
-# ORIG:          AuxSymbolCount: 1
-# ORIG:          AuxSectionDef {
-# ORIG:            Checksum: 0x459345AD
-# ORIG:          }
-# ORIG:      ]
-# ORIG:      Hex dump of section '.giats$y':
-# ORIG-NEXT: 0x00000000 06000000 10000000                   ........
-
-# ERROR: section '.giats$y' does not have the corresponding symbol or the symbol has unexpected format
-
---- !COFF
-header:
-  Machine:         IMAGE_FILE_MACHINE_AMD64
-  Characteristics: [  ]
-sections:
-  - Name:            .text
-    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
-    SizeOfRawData:   28
-    Relocations:
-      - VirtualAddress:  3
-        SymbolName:      foo
-        Type:            IMAGE_REL_AMD64_REL32
-  - Name:            '.debug$S'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     04000000F100000044656275672073656374696F6E20746F20626520737472697070656400
-    SizeOfRawData:   37
-  - Name:            '.giats$y'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     '0600000010000000'
-    SizeOfRawData:   8
-symbols:
-  - Name:            .text
-    Value:           0
-    SectionNumber:   1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          28
-      NumberOfRelocations: 4
-      NumberOfLinenumbers: 0
-      CheckSum:        3583480811
-      Number:          1
-  - Name:            '.debug$S'
-    Value:           0
-    SectionNumber:   2
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          37
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        2941632545
-      Number:          4
-  - Name:            '.giats$y'
-    Value:           42
-    SectionNumber:   3
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          8
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        1167279533
-      Number:          5
-  - Name:            foo
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-...
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-3.test b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-3.test
deleted file mode 100644
index 60bbc154024e8..0000000000000
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section-3.test
+++ /dev/null
@@ -1,86 +0,0 @@
-# Bail out if a section consisting of symidx has unexpected format for patching
-# its content and updating a check sum. Basically, the expected format is a
-# definitive symbol with a zero offset that is linked with section.
-# In the case, the symbol .gljmp$y has a non-static storage class.
-
-# RUN: yaml2obj %s -o %t.in.o
-# RUN: llvm-readobj -r -s -x '.gljmp$y' %t.in.o | FileCheck %s --check-prefix=ORIG
-# RUN: not llvm-objcopy --strip-debug %t.in.o %t.out.o 2>&1 | FileCheck %s --check-prefix=ERROR
-
-# ORIG:      Relocations [
-# ORIG-NEXT:   Section (1) .text {
-# ORIG-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (5)
-# ORIG-NEXT:   }
-# ORIG-NEXT: ]
-# ORIG:      Symbols [
-# ORIG:          Name: .gljmp$y
-# ORIG:          Section: .gljmp$y
-# ORIG:          AuxSymbolCount: 0
-# ORIG:      ]
-# ORIG:      Hex dump of section '.gljmp$y':
-# ORIG-NEXT: 0x00000000 06000000 10000000                   ........
-
-# ERROR: section '.gljmp$y' does not have the corresponding symbol or the symbol has unexpected format
-
---- !COFF
-header:
-  Machine:         IMAGE_FILE_MACHINE_AMD64
-  Characteristics: [  ]
-sections:
-  - Name:            .text
-    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
-    SizeOfRawData:   28
-    Relocations:
-      - VirtualAddress:  3
-        SymbolName:      foo
-        Type:            IMAGE_REL_AMD64_REL32
-  - Name:            '.debug$S'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     04000000F100000044656275672073656374696F6E20746F20626520737472697070656400
-    SizeOfRawData:   37
-  - Name:            '.gljmp$y'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     '0600000010000000'
-    SizeOfRawData:   8
-symbols:
-  - Name:            .text
-    Value:           0
-    SectionNumber:   1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          28
-      NumberOfRelocations: 4
-      NumberOfLinenumbers: 0
-      CheckSum:        3583480811
-      Number:          1
-  - Name:            '.debug$S'
-    Value:           0
-    SectionNumber:   2
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          37
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        2941632545
-      Number:          4
-  - Name:            '.gljmp$y'
-    Value:           0
-    SectionNumber:   3
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            foo
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-...
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section.test b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section.test
new file mode 100644
index 0000000000000..a491d71068385
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section.test
@@ -0,0 +1,225 @@
+## Bail out if a section consisting of symidx is invalid.
+## It includes both: an unexpected format for patching section data and
+## updating the checksum, as well as data validity.
+## Essentially, the expected format is a definitive symbol with a zero offset
+## linked to the section. Valid data consists of a sequence of symbol indices
+## that remain in the symbol table even after stripping.
+
+## In this case, the symbol .gfids$y is not present at all.
+
+# RUN: yaml2obj %s --docnum=1 -o %t1.in.o
+# RUN: not llvm-objcopy --strip-debug %t1.in.o %t1.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-NOSYM
+
+# ERROR-NOSYM: section '.gfids$y' does not have the corresponding symbol or the symbol has unexpected format
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
+    SizeOfRawData:   28
+    Relocations:
+      - VirtualAddress:  3
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_REL32
+  - Name:            '.gfids$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '04000000'
+    SizeOfRawData:   8
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          28
+      NumberOfRelocations: 4
+      NumberOfLinenumbers: 0
+      CheckSum:        3583480811
+      Number:          1
+  - Name:            foo
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
+
+## In this case, the symbol .giats$y has a non-zero offset.
+
+# RUN: yaml2obj %s --docnum=2 -o %t2.in.o
+# RUN: not llvm-objcopy --strip-debug %t2.in.o %t2.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-OFFSET
+
+# ERROR-OFFSET: section '.giats$y' does not have the corresponding symbol or the symbol has unexpected format
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
+    SizeOfRawData:   28
+    Relocations:
+      - VirtualAddress:  3
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_REL32
+  - Name:            '.giats$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '0600000010000000'
+    SizeOfRawData:   8
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          28
+      NumberOfRelocations: 4
+      NumberOfLinenumbers: 0
+      CheckSum:        3583480811
+      Number:          1
+  - Name:            '.giats$y'
+    Value:           42
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          8
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        1167279533
+      Number:          5
+  - Name:            foo
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
+
+## In this case, the symbol .gljmp$y has a non-static storage class.
+
+# RUN: yaml2obj %s --docnum=3 -o %t3.in.o
+# RUN: not llvm-objcopy --strip-debug %t3.in.o %t3.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-EXTERNAL
+
+# ERROR-EXTERNAL: section '.gljmp$y' does not have the corresponding symbol or the symbol has unexpected format
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
+    SizeOfRawData:   28
+    Relocations:
+      - VirtualAddress:  3
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_REL32
+  - Name:            '.gljmp$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '0600000010000000'
+    SizeOfRawData:   8
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          28
+      NumberOfRelocations: 4
+      NumberOfLinenumbers: 0
+      CheckSum:        3583480811
+      Number:          1
+  - Name:            '.gljmp$y'
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            foo
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
+
+## In this case, .gfids$y contains a symbol index that is not present in the
+## symbol table. Generally the behavior should be the same for every section consisting
+## of .symidx directives, e.g .giats$y, .gljmp$y and .gehcont$y.
+
+# RUN: yaml2obj %s --docnum=4 -o %t4.in.o
+# RUN: not llvm-objcopy --strip-debug %t4.in.o %t4.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-SYMIDX
+
+# ERROR-SYMIDX: section '.gfids$y' contains a .symidx (16) that is incorrect or was stripped
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
+    SizeOfRawData:   28
+    Relocations:
+      - VirtualAddress:  3
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_REL32
+  - Name:            '.gfids$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '0400000010000000'
+    SizeOfRawData:   8
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          28
+      NumberOfRelocations: 4
+      NumberOfLinenumbers: 0
+      CheckSum:        3583480811
+      Number:          1
+  - Name:            '.gfids$y'
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          8
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        1167279533
+      Number:          5
+  - Name:            foo
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test b/llvm/test/tools/llvm-objcopy/COFF/strip-update-symidx-section.test
similarity index 64%
rename from llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test
rename to llvm/test/tools/llvm-objcopy/COFF/strip-update-symidx-section.test
index 8fa19ee30ee57..4ad2e51084f1a 100644
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-update-ehcont.test
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-update-symidx-section.test
@@ -1,43 +1,24 @@
-# .gehcont$y section consists of .symidx directives. The test checks that
-# indices in the section are updated after stripping as the symbol table could
-# be changed during stripping.
+## Check sections consisting only of .symidx directives. The test checks that
+## indices in the sections are updated after stripping as the symbol table could
+## be changed during stripping.
 
 # RUN: yaml2obj %s -o %t.in.o
 
-# RUN: llvm-readobj -r -s -x '.gehcont$y' %t.in.o | FileCheck %s --check-prefix=ORIG
 # RUN: llvm-objcopy --strip-debug %t.in.o %t.out.o
-# RUN: llvm-readobj -r -s -x '.gehcont$y' %t.out.o | FileCheck %s --check-prefix=STRIP
+# RUN: llvm-readobj -x '.gehcont$y' -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.out.o | FileCheck %s
 
-# ORIG:      Symbols [
-# ORIG:          Name: .text
-# ORIG:          Name: .data
-# ORIG:          Name: .bss
-# ORIG:          Name: .text
-# ORIG:          Name: ?foo@@YAXXZ
-# ORIG:          Name: .data
-# ORIG:          Name: .drectve
-# ORIG:          Name: .debug$S
-# ORIG:          Name: .gehcont$y
-# ORIG:          Name: ?foo2@@YAXXZ
-# ORIG:          Name: $ehgcr_0_1
-# ORIG:      Hex dump of section '.gehcont$y':
-# ORIG-NEXT: 0x00000000 12000000 12000000 12000000          ............
+# CHECK:      Hex dump of section '.gehcont$y':
+# CHECK-NEXT: 0x00000000 10000000 10000000 10000000
 
-# .debug$S is going to be stripped and index of $ehgcr_0_1 is decreased by 2
+# CHECK:      Hex dump of section '.gfids$y':
+# CHECK-NEXT: 0x00000000 17000000 19000000
+
+# CHECK:      Hex dump of section '.giats$y':
+# CHECK-NEXT: 0x00000000 18000000
+
+# CHECK:      Hex dump of section '.gljmp$y':
+# CHECK-NEXT: 0x00000000 17000000 18000000 19000000
 
-# STRIP:      Symbols [
-# STRIP:          Name: .text
-# STRIP:          Name: .data
-# STRIP:          Name: .bss
-# STRIP:          Name: .text
-# STRIP:          Name: ?foo@@YAXXZ
-# STRIP:          Name: .data
-# STRIP:          Name: .drectve
-# STRIP:          Name: .gehcont$y
-# STRIP:          Name: ?foo2@@YAXXZ
-# STRIP:          Name: $ehgcr_0_1
-# STRIP:      Hex dump of section '.gehcont$y':
-# STRIP-NEXT: 0x00000000 10000000 10000000 10000000          ............
 
 --- !COFF
 header:
@@ -62,7 +43,16 @@ sections:
     SectionData:     554883EC30488D6C243048C745F0FEFFFFFFE800000000904883C4305DC366904889542410554883EC20488D6A30488D05E2FFFFFF4883C4205DC3
     SizeOfRawData:   59
     Relocations:
-      - VirtualAddress:  19
+      - VirtualAddress:  3
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_REL32
+      - VirtualAddress:  10
+        SymbolName:      bar
+        Type:            IMAGE_REL_AMD64_REL32
+      - VirtualAddress:  17
+        SymbolName:      baz
+        Type:            IMAGE_REL_AMD64_REL32
+      - VirtualAddress:  24
         SymbolName:      '?foo2@@YAXXZ'
         Type:            IMAGE_REL_AMD64_REL32
   - Name:            .data
@@ -85,6 +75,21 @@ sections:
     Alignment:       4
     SectionData:     '120000001200000012000000'
     SizeOfRawData:   12
+  - Name:            '.gfids$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '190000001B000000'
+    SizeOfRawData:   8
+  - Name:            '.giats$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '1A000000'
+    SizeOfRawData:   4
+  - Name:            '.gljmp$y'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '190000001A0000001B000000'
+    SizeOfRawData:   12
 symbols:
   - Name:            .text
     Value:           0
@@ -202,4 +207,58 @@ symbols:
     SimpleType:      IMAGE_SYM_TYPE_NULL
     ComplexType:     IMAGE_SYM_DTYPE_NULL
     StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            '.gfids$y'
+    Value:           0
+    SectionNumber:   9
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          8
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        1167279533
+      Number:          9
+  - Name:            '.giats$y'
+    Value:           0
+    SectionNumber:   10
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          8
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        830808662
+      Number:          10
+  - Name:            '.gljmp$y'
+    Value:           0
+    SectionNumber:   11
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          16
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        3322439691
+      Number:          11
+  - Name:            foo
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            bar
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            baz
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
 ...
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-update-winguards.test b/llvm/test/tools/llvm-objcopy/COFF/strip-update-winguards.test
deleted file mode 100644
index 563e606104b12..0000000000000
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-update-winguards.test
+++ /dev/null
@@ -1,241 +0,0 @@
-# .gfids$y, .giats$y and .gljmp$y cf-guard sections consist of .symidx
-# directives. The test checks that indices in the section are updated after
-# stripping as the symbol table could be changed during stripping.
-
-# RUN: yaml2obj %s -o %t.in.o
-
-# RUN: llvm-readobj -r -s -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.in.o | FileCheck %s --check-prefix=ORIG
-# RUN: llvm-objcopy --strip-debug %t.in.o %t.out.o
-# RUN: llvm-readobj -r -s -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.out.o | FileCheck %s --check-prefix=STRIP
-
-# ORIG:      Relocations [
-# ORIG-NEXT:   Section (1) .text {
-# ORIG-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (14)
-# ORIG-NEXT:     0xA IMAGE_REL_AMD64_REL32 bar (15)
-# ORIG-NEXT:     0x11 IMAGE_REL_AMD64_REL32 baz (16)
-# ORIG-NEXT:     0x18 IMAGE_REL_AMD64_REL32 foobar (17)
-# ORIG-NEXT:   }
-# ORIG-NEXT: ]
-# ORIG:      Symbols [
-# ORIG:          Name: .gfids$y
-# ORIG:          Section: .gfids$y
-# ORIG:          AuxSymbolCount: 1
-# ORIG:          AuxSectionDef {
-# ORIG:            Checksum: 0x459345AD
-# ORIG:          }
-# ORIG:          Name: .giats$y
-# ORIG:          Section: .giats$y
-# ORIG:          AuxSymbolCount: 1
-# ORIG:          AuxSectionDef {
-# ORIG:            Checksum: 0x31852256
-# ORIG:          }
-# ORIG:          Name: .gljmp$y
-# ORIG:          Section: .gljmp$y
-# ORIG:          AuxSymbolCount: 1
-# ORIG:          AuxSectionDef {
-# ORIG:            Checksum: 0xC608680B
-# ORIG:          }
-# ORIG:      ]
-# ORIG:      Hex dump of section '.gfids$y':
-# ORIG-NEXT: 0x00000000 0e000000 10000000                   ........
-# ORIG:      Hex dump of section '.giats$y':
-# ORIG-NEXT: 0x00000000 0f000000 11000000                   ........
-# ORIG:      Hex dump of section '.gljmp$y':
-# ORIG-NEXT: 0x00000000 0e000000 0f000000 10000000 11000000 ................
-
-# STRIP:      Relocations [
-# STRIP-NEXT:   Section (1) .text {
-# STRIP-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (12)
-# STRIP-NEXT:     0xA IMAGE_REL_AMD64_REL32 bar (13)
-# STRIP-NEXT:     0x11 IMAGE_REL_AMD64_REL32 baz (14)
-# STRIP-NEXT:     0x18 IMAGE_REL_AMD64_REL32 foobar (15)
-# STRIP-NEXT:   }
-# STRIP-NEXT: ]
-# STRIP:      Symbols [
-# STRIP:          Name: .gfids$y
-# STRIP:          Section: .gfids$y
-# STRIP:          AuxSymbolCount: 1
-# STRIP:          AuxSectionDef {
-# STRIP:            Checksum: 0xB770627C
-# STRIP:          }
-# STRIP:          Name: .giats$y
-# STRIP:          Section: .giats$y
-# STRIP:          AuxSymbolCount: 1
-# STRIP:          AuxSectionDef {
-# STRIP:            Checksum: 0xC3660587
-# STRIP:          }
-# STRIP:          Name: .gljmp$y
-# STRIP:          Section: .gljmp$y
-# STRIP:          AuxSymbolCount: 1
-# STRIP:          AuxSectionDef {
-# STRIP:            Checksum: 0x7464D042
-# STRIP:          }
-# STRIP:      ]
-# STRIP:      Hex dump of section '.gfids$y':
-# STRIP-NEXT: 0x00000000 0c000000 0e000000                   ........
-# STRIP:      Hex dump of section '.giats$y':
-# STRIP-NEXT: 0x00000000 0d000000 0f000000                   ........
-# STRIP:      Hex dump of section '.gljmp$y':
-# STRIP-NEXT: 0x00000000 0c000000 0d000000 0e000000 0f000000 ................
-
---- !COFF
-header:
-  Machine:         IMAGE_FILE_MACHINE_AMD64
-  Characteristics: [  ]
-sections:
-  - Name:            .text
-    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
-    SizeOfRawData:   28
-    Relocations:
-      - VirtualAddress:  3
-        SymbolName:      foo
-        Type:            IMAGE_REL_AMD64_REL32
-      - VirtualAddress:  10
-        SymbolName:      bar
-        Type:            IMAGE_REL_AMD64_REL32
-      - VirtualAddress:  17
-        SymbolName:      baz
-        Type:            IMAGE_REL_AMD64_REL32
-      - VirtualAddress:  24
-        SymbolName:      foobar
-        Type:            IMAGE_REL_AMD64_REL32
-  - Name:            .data
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
-    Alignment:       4
-    SectionData:     ''
-  - Name:            .bss
-    Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
-    Alignment:       4
-    SectionData:     ''
-  - Name:            '.debug$S'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     04000000F100000044656275672073656374696F6E20746F20626520737472697070656400
-    SizeOfRawData:   37
-  - Name:            '.gfids$y'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     '0E00000010000000'
-    SizeOfRawData:   8
-  - Name:            '.giats$y'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     0F00000011000000
-    SizeOfRawData:   8
-  - Name:            '.gljmp$y'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     0E0000000F0000001000000011000000
-    SizeOfRawData:   16
-symbols:
-  - Name:            .text
-    Value:           0
-    SectionNumber:   1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          28
-      NumberOfRelocations: 4
-      NumberOfLinenumbers: 0
-      CheckSum:        3583480811
-      Number:          1
-  - Name:            .data
-    Value:           0
-    SectionNumber:   2
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          0
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        0
-      Number:          2
-  - Name:            .bss
-    Value:           0
-    SectionNumber:   3
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          0
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        0
-      Number:          3
-  - Name:            '.debug$S'
-    Value:           0
-    SectionNumber:   4
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          37
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        2941632545
-      Number:          4
-  - Name:            '.gfids$y'
-    Value:           0
-    SectionNumber:   5
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          8
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        1167279533
-      Number:          5
-  - Name:            '.giats$y'
-    Value:           0
-    SectionNumber:   6
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          8
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        830808662
-      Number:          6
-  - Name:            '.gljmp$y'
-    Value:           0
-    SectionNumber:   7
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          16
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        3322439691
-      Number:          7
-  - Name:            foo
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            bar
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            baz
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-  - Name:            foobar
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-...
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test b/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test
deleted file mode 100644
index 32bfa7480c72c..0000000000000
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-wrong-winguards.test
+++ /dev/null
@@ -1,94 +0,0 @@
-# Bail out if .gfids$y contains symbol index that is not present in the symbol
-# table.  Generally it should be the same for every section consisting of
-# .symidx directives, e.g .giats$y, .gljmp$y and .gehcont$y.
-
-# RUN: yaml2obj %s -o %t.in.o
-# RUN: llvm-readobj -r -s -x '.gfids$y' %t.in.o | FileCheck %s --check-prefix=ORIG
-# RUN: not llvm-objcopy --strip-debug %t.in.o %t.out.o 2>&1 | FileCheck %s --check-prefix=ERROR
-
-# ORIG:      Relocations [
-# ORIG-NEXT:   Section (1) .text {
-# ORIG-NEXT:     0x3 IMAGE_REL_AMD64_REL32 foo (6)
-# ORIG-NEXT:   }
-# ORIG-NEXT: ]
-# ORIG:      Symbols [
-# ORIG:          Name: .gfids$y
-# ORIG:          Section: .gfids$y
-# ORIG:          AuxSymbolCount: 1
-# ORIG:          AuxSectionDef {
-# ORIG:            Checksum: 0x459345AD
-# ORIG:          }
-# ORIG:      ]
-# ORIG:      Hex dump of section '.gfids$y':
-# ORIG-NEXT: 0x00000000 06000000 10000000                   ........
-
-# ERROR: section '.gfids$y' contains a .symidx (16) that is incorrect or was stripped
-
---- !COFF
-header:
-  Machine:         IMAGE_FILE_MACHINE_AMD64
-  Characteristics: [  ]
-sections:
-  - Name:            .text
-    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     488D0500000000488D0D00000000488D0500000000488D0500000000
-    SizeOfRawData:   28
-    Relocations:
-      - VirtualAddress:  3
-        SymbolName:      foo
-        Type:            IMAGE_REL_AMD64_REL32
-  - Name:            '.debug$S'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     04000000F100000044656275672073656374696F6E20746F20626520737472697070656400
-    SizeOfRawData:   37
-  - Name:            '.gfids$y'
-    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
-    Alignment:       4
-    SectionData:     '0600000010000000'
-    SizeOfRawData:   8
-symbols:
-  - Name:            .text
-    Value:           0
-    SectionNumber:   1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          28
-      NumberOfRelocations: 4
-      NumberOfLinenumbers: 0
-      CheckSum:        3583480811
-      Number:          1
-  - Name:            '.debug$S'
-    Value:           0
-    SectionNumber:   2
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          37
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        2941632545
-      Number:          4
-  - Name:            '.gfids$y'
-    Value:           0
-    SectionNumber:   3
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_STATIC
-    SectionDefinition:
-      Length:          8
-      NumberOfRelocations: 0
-      NumberOfLinenumbers: 0
-      CheckSum:        1167279533
-      Number:          5
-  - Name:            foo
-    Value:           0
-    SectionNumber:   0
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_NULL
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
-...

>From 71a638c4b89e4d514411a22d9045aaccb7731247 Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Fri, 22 Aug 2025 11:03:04 -0700
Subject: [PATCH 16/17] Format

---
 llvm/lib/ObjCopy/COFF/COFFWriter.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
index 5b5407d5172f7..fed67d67f13a7 100644
--- a/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
+++ b/llvm/lib/ObjCopy/COFF/COFFWriter.cpp
@@ -112,7 +112,8 @@ Error COFFWriter::finalizeSymIdxContents() {
   for (Symbol &Sym : Obj.getMutableSymbols()) {
     SymIdMap[Sym.OriginalRawIndex] = Sym.RawIndex;
 
-    // We collect only definition symbols of the sections to update the checksums.
+    // We collect only definition symbols of the sections to update the
+    // checksums.
     if (Sym.Sym.StorageClass == IMAGE_SYM_CLASS_STATIC &&
         Sym.Sym.NumberOfAuxSymbols == 1 && Sym.Sym.Value == 0 &&
         IsSymIdxSection(Sym.Name))
@@ -126,8 +127,8 @@ Error COFFWriter::finalizeSymIdxContents() {
       continue;
 
     ArrayRef<uint8_t> RawIds = Sec.getContents();
-    // Nothing to do and also the checksum will be -1 instead of 0 if we recalculate
-    // it on empty input.
+    // Nothing to do and also the checksum will be -1 instead of 0 if we
+    // recalculate it on empty input.
     if (RawIds.size() == 0)
       continue;
 

>From 975184fef0f270500f5837ee20bd1a92442cfc71 Mon Sep 17 00:00:00 2001
From: Evgenii Kudriashov <evgenii.kudriashov at intel.com>
Date: Fri, 29 Aug 2025 08:07:31 -0700
Subject: [PATCH 17/17] Check file name in errors and return lost checksum
 checks

---
 .../COFF/strip-invalid-symidx-section.test    | 16 ++++----
 .../COFF/strip-update-symidx-section.test     | 39 ++++++++++++++++---
 2 files changed, 42 insertions(+), 13 deletions(-)

diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section.test b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section.test
index a491d71068385..eb74f24c998a0 100644
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section.test
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-invalid-symidx-section.test
@@ -8,9 +8,9 @@
 ## In this case, the symbol .gfids$y is not present at all.
 
 # RUN: yaml2obj %s --docnum=1 -o %t1.in.o
-# RUN: not llvm-objcopy --strip-debug %t1.in.o %t1.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-NOSYM
+# RUN: not llvm-objcopy --strip-debug %t1.in.o %t1.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-NOSYM -DFILE=%t1.out.o
 
-# ERROR-NOSYM: section '.gfids$y' does not have the corresponding symbol or the symbol has unexpected format
+# ERROR-NOSYM: error: '[[FILE]]': section '.gfids$y' does not have the corresponding symbol or the symbol has unexpected format
 
 --- !COFF
 header:
@@ -55,9 +55,9 @@ symbols:
 ## In this case, the symbol .giats$y has a non-zero offset.
 
 # RUN: yaml2obj %s --docnum=2 -o %t2.in.o
-# RUN: not llvm-objcopy --strip-debug %t2.in.o %t2.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-OFFSET
+# RUN: not llvm-objcopy --strip-debug %t2.in.o %t2.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-OFFSET -DFILE=%t2.out.o
 
-# ERROR-OFFSET: section '.giats$y' does not have the corresponding symbol or the symbol has unexpected format
+# ERROR-OFFSET: error: '[[FILE]]': section '.giats$y' does not have the corresponding symbol or the symbol has unexpected format
 
 --- !COFF
 header:
@@ -114,9 +114,9 @@ symbols:
 ## In this case, the symbol .gljmp$y has a non-static storage class.
 
 # RUN: yaml2obj %s --docnum=3 -o %t3.in.o
-# RUN: not llvm-objcopy --strip-debug %t3.in.o %t3.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-EXTERNAL
+# RUN: not llvm-objcopy --strip-debug %t3.in.o %t3.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-EXTERNAL -DFILE=%t3.out.o
 
-# ERROR-EXTERNAL: section '.gljmp$y' does not have the corresponding symbol or the symbol has unexpected format
+# ERROR-EXTERNAL: error: '[[FILE]]': section '.gljmp$y' does not have the corresponding symbol or the symbol has unexpected format
 
 --- !COFF
 header:
@@ -169,9 +169,9 @@ symbols:
 ## of .symidx directives, e.g .giats$y, .gljmp$y and .gehcont$y.
 
 # RUN: yaml2obj %s --docnum=4 -o %t4.in.o
-# RUN: not llvm-objcopy --strip-debug %t4.in.o %t4.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-SYMIDX
+# RUN: not llvm-objcopy --strip-debug %t4.in.o %t4.out.o 2>&1 | FileCheck %s --check-prefix=ERROR-SYMIDX  -DFILE=%t4.out.o
 
-# ERROR-SYMIDX: section '.gfids$y' contains a .symidx (16) that is incorrect or was stripped
+# ERROR-SYMIDX: error: '[[FILE]]': section '.gfids$y' contains a .symidx (16) that is incorrect or was stripped
 --- !COFF
 header:
   Machine:         IMAGE_FILE_MACHINE_AMD64
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-update-symidx-section.test b/llvm/test/tools/llvm-objcopy/COFF/strip-update-symidx-section.test
index 4ad2e51084f1a..8fbef3be463cf 100644
--- a/llvm/test/tools/llvm-objcopy/COFF/strip-update-symidx-section.test
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-update-symidx-section.test
@@ -5,7 +5,36 @@
 # RUN: yaml2obj %s -o %t.in.o
 
 # RUN: llvm-objcopy --strip-debug %t.in.o %t.out.o
-# RUN: llvm-readobj -x '.gehcont$y' -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.out.o | FileCheck %s
+# RUN: llvm-readobj -s -x '.gehcont$y' -x '.gfids$y' -x '.giats$y' -x '.gljmp$y' %t.out.o | FileCheck %s
+
+# CHECK: Symbols [
+# CHECK:     Name: .text
+# CHECK:     Name: .data
+# CHECK:     Name: .bss
+# CHECK:     Name: .text
+# CHECK:     Name: ?foo@@YAXXZ
+# CHECK:     Name: .data
+# CHECK:     Name: .drectve
+# CHECK:     Name: .gehcont$y
+# CHECK:     AuxSectionDef {
+# CHECK:       Checksum: 0x20BA8B48
+# CHECK:     }
+# CHECK:     Name: ?foo2@@YAXXZ
+# CHECK:     Name: $ehgcr_0_1
+# CHECK:     Name: .gfids$y
+# CHECK:     AuxSectionDef {
+# CHECK:       Checksum: 0x5B6F2027
+# CHECK:     }
+# CHECK:     Name: .giats$y
+# CHECK:     AuxSectionDef {
+# CHECK:       Checksum: 0x95AD7F70
+# CHECK:     }
+# CHECK:     Name: .gljmp$y
+# CHECK:     AuxSectionDef {
+# CHECK:       Checksum: 0xFACF36F8
+# CHECK:     }
+# CHECK:     Name: foo
+# CHECK: ]
 
 # CHECK:      Hex dump of section '.gehcont$y':
 # CHECK-NEXT: 0x00000000 10000000 10000000 10000000
@@ -193,7 +222,7 @@ symbols:
       Length:          12
       NumberOfRelocations: 0
       NumberOfLinenumbers: 0
-      CheckSum:        820498156
+      CheckSum:        0x30E7CEEC
       Number:          8
   - Name:            '?foo2@@YAXXZ'
     Value:           0
@@ -217,7 +246,7 @@ symbols:
       Length:          8
       NumberOfRelocations: 0
       NumberOfLinenumbers: 0
-      CheckSum:        1167279533
+      CheckSum:        0x459345AD
       Number:          9
   - Name:            '.giats$y'
     Value:           0
@@ -229,7 +258,7 @@ symbols:
       Length:          8
       NumberOfRelocations: 0
       NumberOfLinenumbers: 0
-      CheckSum:        830808662
+      CheckSum:        0x31852256
       Number:          10
   - Name:            '.gljmp$y'
     Value:           0
@@ -241,7 +270,7 @@ symbols:
       Length:          16
       NumberOfRelocations: 0
       NumberOfLinenumbers: 0
-      CheckSum:        3322439691
+      CheckSum:        0xC608680B
       Number:          11
   - Name:            foo
     Value:           0



More information about the llvm-commits mailing list