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

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 14 06:08:27 PDT 2025


================
@@ -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();
----------------
mstorsjo wrote:

That's not what I meant with offset. The string-or-offset aspect of the name is not an issue here.

What I meant was, we have this:
```
struct coff_symbol {
  union { ... } Name;

  support::ulittle32_t Value;
  SectionNumberType SectionNumber;
```

A symbol in a COFF object points at a section number, and `Value` is an offset into that section. E.g. when you have one large `.text` section and a function is at a specific offset within that section.

For section definition symbols, with `coff_aux_section_definition`, it is of course most common that they have a zero offset and point to the start of the section. (I'm not sure if it makes sense to have a section definition symbol pointing at an offset within the section.) But it would be good to verify this, because if the symbol has got a nonzero `Value` here, it really means that it points at an offset into the section.

Now if the linker doesn't really use the symbol at all, but just inspects the whole contents of the section, it probably doesn't matter either way. But in the current form, where we primarily iterate over symbols and use that to look up a section, it gives an impression that almost any symbol would be ok.

Anyway, if you add a `Value != 0` to the check where you currently check `Sym.Sym.NumberOfAuxSymbols != 1 || Sym.Sym.StorageClass != IMAGE_SYM_CLASS_STATIC`, then it's probably fine.


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


More information about the llvm-commits mailing list