[llvm] eb98089 - [llvm-objcopy] Allow -p on COFF targets (#171237)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 12 05:48:08 PST 2025
Author: Jacek Caban
Date: 2025-12-12T14:48:04+01:00
New Revision: eb98089a26b8265be2cc3f78b488b98bc9e19db9
URL: https://github.com/llvm/llvm-project/commit/eb98089a26b8265be2cc3f78b488b98bc9e19db9
DIFF: https://github.com/llvm/llvm-project/commit/eb98089a26b8265be2cc3f78b488b98bc9e19db9.diff
LOG: [llvm-objcopy] Allow -p on COFF targets (#171237)
Binutils allows `-p`, which prevents it from modifying the timestamp.
`llvm-objcopy` already preserves the timestamp in the COFF header, so
the only missing piece is allowing the option in the config manager.
Added:
llvm/test/tools/llvm-objcopy/COFF/strip-preserve-mtime.test
Modified:
llvm/lib/ObjCopy/ConfigManager.cpp
Removed:
################################################################################
diff --git a/llvm/lib/ObjCopy/ConfigManager.cpp b/llvm/lib/ObjCopy/ConfigManager.cpp
index 6b7b4f116f583..7424865a2e5e2 100644
--- a/llvm/lib/ObjCopy/ConfigManager.cpp
+++ b/llvm/lib/ObjCopy/ConfigManager.cpp
@@ -28,9 +28,8 @@ Expected<const COFFConfig &> ConfigManager::getCOFFConfig() const {
!Common.SymbolsToLocalize.empty() || !Common.SymbolsToWeaken.empty() ||
!Common.SymbolsToKeepGlobal.empty() || !Common.SectionsToRename.empty() ||
!Common.SetSectionAlignment.empty() || !Common.SetSectionType.empty() ||
- Common.ExtractDWO || Common.PreserveDates || Common.StripDWO ||
- Common.StripNonAlloc || Common.StripSections || Common.Weaken ||
- Common.DecompressDebugSections ||
+ Common.ExtractDWO || Common.StripDWO || Common.StripNonAlloc ||
+ Common.StripSections || Common.Weaken || Common.DecompressDebugSections ||
Common.DiscardMode == DiscardType::Locals ||
!Common.SymbolsToAdd.empty() || Common.GapFill != 0 ||
Common.PadTo != 0 || Common.ChangeSectionLMAValAll != 0 ||
diff --git a/llvm/test/tools/llvm-objcopy/COFF/strip-preserve-mtime.test b/llvm/test/tools/llvm-objcopy/COFF/strip-preserve-mtime.test
new file mode 100644
index 0000000000000..91be6b40edc15
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/COFF/strip-preserve-mtime.test
@@ -0,0 +1,50 @@
+## Note: ls -l prints the modified timestamp.
+
+# RUN: yaml2obj %s -o %t.exe
+
+## Preserve dates when stripping to an output file.
+# RUN: cp %t.exe %t.1.exe
+# RUN: touch -m -t 199705050555.55 %t.1.exe
+# RUN: llvm-strip -p %t.1.exe -o %t-preserved.1.exe
+# RUN: ls -l %t-preserved.1.exe | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
+## Check that the stripped output is in fact a valid object file and the timestamp in the PE header is preserved.
+# RUN: llvm-readobj --headers %t-preserved.1.exe | FileCheck %s
+
+## Preserve dates available via objcopy interface as well.
+# RUN: cp %t.exe %t.2.exe
+# RUN: touch -m -t 199705050555.55 %t.2.exe
+# RUN: llvm-objcopy -p %t.2.exe %t-preserved.2.exe
+# RUN: ls -l %t-preserved.2.exe | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
+# RUN: llvm-readobj --headers %t-preserved.2.exe | FileCheck %s
+
+## Preserve dates when stripping in place.
+# RUN: cp %t.exe %t.3.exe
+# RUN: touch -m -t 199705050555.55 %t.3.exe
+# RUN: llvm-strip -p %t.3.exe
+# RUN: ls -l %t.3.exe | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
+# RUN: llvm-readobj --headers %t.3.exe | FileCheck %s
+
+## Without -p set, don't preserve dates.
+# RUN: cp %t.exe %t.4.exe
+# RUN: touch -m -t 199705050555.55 %t.4.exe
+# RUN: llvm-strip %t.4.exe
+# RUN: ls -l %t.4.exe | FileCheck %s --check-prefix=CHECK-NO-PRESERVE-MTIME
+# RUN: llvm-readobj --headers %t.4.exe | FileCheck %s
+
+## Always preserve the timestamp in the PE header.
+# CHECK: TimeDateStamp: 1970-01-01 00:00:00 (0x0)
+
+# CHECK-PRESERVE-MTIME: {{[[:space:]]1997}}
+# CHECK-NO-PRESERVE-MTIME-NOT: {{[[:space:]]1997}}
+
+!COFF
+OptionalHeader:
+ Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI
+header:
+ Machine: IMAGE_FILE_MACHINE_AMD64
+sections:
+ - Name: .text
+ Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+ VirtualSize: 20
+symbols: []
+...
More information about the llvm-commits
mailing list