Skip to content

Commit eb98089

Browse files
authored
[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.
1 parent 234c414 commit eb98089

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

llvm/lib/ObjCopy/ConfigManager.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ Expected<const COFFConfig &> ConfigManager::getCOFFConfig() const {
2828
!Common.SymbolsToLocalize.empty() || !Common.SymbolsToWeaken.empty() ||
2929
!Common.SymbolsToKeepGlobal.empty() || !Common.SectionsToRename.empty() ||
3030
!Common.SetSectionAlignment.empty() || !Common.SetSectionType.empty() ||
31-
Common.ExtractDWO || Common.PreserveDates || Common.StripDWO ||
32-
Common.StripNonAlloc || Common.StripSections || Common.Weaken ||
33-
Common.DecompressDebugSections ||
31+
Common.ExtractDWO || Common.StripDWO || Common.StripNonAlloc ||
32+
Common.StripSections || Common.Weaken || Common.DecompressDebugSections ||
3433
Common.DiscardMode == DiscardType::Locals ||
3534
!Common.SymbolsToAdd.empty() || Common.GapFill != 0 ||
3635
Common.PadTo != 0 || Common.ChangeSectionLMAValAll != 0 ||
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Note: ls -l prints the modified timestamp.
2+
3+
# RUN: yaml2obj %s -o %t.exe
4+
5+
## Preserve dates when stripping to an output file.
6+
# RUN: cp %t.exe %t.1.exe
7+
# RUN: touch -m -t 199705050555.55 %t.1.exe
8+
# RUN: llvm-strip -p %t.1.exe -o %t-preserved.1.exe
9+
# RUN: ls -l %t-preserved.1.exe | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
10+
## Check that the stripped output is in fact a valid object file and the timestamp in the PE header is preserved.
11+
# RUN: llvm-readobj --headers %t-preserved.1.exe | FileCheck %s
12+
13+
## Preserve dates available via objcopy interface as well.
14+
# RUN: cp %t.exe %t.2.exe
15+
# RUN: touch -m -t 199705050555.55 %t.2.exe
16+
# RUN: llvm-objcopy -p %t.2.exe %t-preserved.2.exe
17+
# RUN: ls -l %t-preserved.2.exe | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
18+
# RUN: llvm-readobj --headers %t-preserved.2.exe | FileCheck %s
19+
20+
## Preserve dates when stripping in place.
21+
# RUN: cp %t.exe %t.3.exe
22+
# RUN: touch -m -t 199705050555.55 %t.3.exe
23+
# RUN: llvm-strip -p %t.3.exe
24+
# RUN: ls -l %t.3.exe | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
25+
# RUN: llvm-readobj --headers %t.3.exe | FileCheck %s
26+
27+
## Without -p set, don't preserve dates.
28+
# RUN: cp %t.exe %t.4.exe
29+
# RUN: touch -m -t 199705050555.55 %t.4.exe
30+
# RUN: llvm-strip %t.4.exe
31+
# RUN: ls -l %t.4.exe | FileCheck %s --check-prefix=CHECK-NO-PRESERVE-MTIME
32+
# RUN: llvm-readobj --headers %t.4.exe | FileCheck %s
33+
34+
## Always preserve the timestamp in the PE header.
35+
# CHECK: TimeDateStamp: 1970-01-01 00:00:00 (0x0)
36+
37+
# CHECK-PRESERVE-MTIME: {{[[:space:]]1997}}
38+
# CHECK-NO-PRESERVE-MTIME-NOT: {{[[:space:]]1997}}
39+
40+
!COFF
41+
OptionalHeader:
42+
Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI
43+
header:
44+
Machine: IMAGE_FILE_MACHINE_AMD64
45+
sections:
46+
- Name: .text
47+
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
48+
VirtualSize: 20
49+
symbols: []
50+
...

0 commit comments

Comments
 (0)