Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 72a8d84

Browse files
committed
[llvm-strip] Fix -p|--preserve-dates to not truncate output when used in-place.
The restoreDateOnFile() method used to preserve dates uses sys::fs::openFileForWrite(). That method defaults to opening files with CD_CreateAlways, which truncates the output file if it exists. Use CD_OpenExisting instead to open it and *not* truncate it, which also has the side benefit of erroring if the file does not exist (it should always exist, because we just wrote it out). Also, fix the test case to make sure the output is a valid output file, and not empty. The extra test assertions are enough to catch this regression. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@340996 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 04e389a commit 72a8d84

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

test/tools/llvm-objcopy/strip-preserve-time.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# RUN: llvm-strip -p %t.1.o -o %t-preserved.1.o
88
# RUN: ls -lu %t-preserved.1.o | FileCheck %s --check-prefix=CHECK-PRESERVE-ATIME
99
# RUN: ls -l %t-preserved.1.o | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
10+
# Check that the stripped output is in fact a valid object file.
11+
# RUN: llvm-readobj %t-preserved.1.o
1012

1113
# Preserve dates available via objcopy interface as well.
1214
# RUN: yaml2obj %s > %t.2.o
@@ -15,6 +17,7 @@
1517
# RUN: llvm-objcopy -p %t.2.o %t-preserved.2.o
1618
# RUN: ls -lu %t-preserved.2.o | FileCheck %s --check-prefix=CHECK-PRESERVE-ATIME
1719
# RUN: ls -l %t-preserved.2.o | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
20+
# RUN: llvm-readobj %t-preserved.2.o
1821

1922
# Preserve dates when stripping in place.
2023
# RUN: yaml2obj %s > %t.3.o
@@ -23,6 +26,7 @@
2326
# RUN: llvm-strip -p %t.3.o
2427
# RUN: ls -lu %t.3.o | FileCheck %s --check-prefix=CHECK-PRESERVE-ATIME
2528
# RUN: ls -l %t.3.o | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
29+
# RUN: llvm-readobj %t.3.o
2630

2731
# Without -p set, don't preserve dates.
2832
# RUN: yaml2obj %s > %t.4.o
@@ -31,6 +35,7 @@
3135
# RUN: llvm-strip %t.4.o
3236
# RUN: ls -lu %t.4.o | FileCheck %s --check-prefix=CHECK-NO-PRESERVE-ATIME
3337
# RUN: ls -l %t.4.o | FileCheck %s --check-prefix=CHECK-NO-PRESERVE-MTIME
38+
# RUN: llvm-readobj %t.4.o
3439

3540
# Preserve dates in archives.
3641
# RUN: yaml2obj %s > %t.5.o
@@ -41,6 +46,7 @@
4146
# RUN: llvm-strip -p %t.a
4247
# RUN: ls -lu %t.a | FileCheck %s --check-prefix=CHECK-PRESERVE-ATIME
4348
# RUN: ls -l %t.a | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
49+
# RUN: llvm-readobj %t.a
4450

4551
# Preserve dates in split DWO files.
4652
# RUN: cp %p/Inputs/dwarf.dwo %t-input.dwo
@@ -49,8 +55,10 @@
4955
# RUN: llvm-objcopy -p -split-dwo=%t-dwo %t-input.dwo %t-nondwo
5056
# RUN: ls -lu %t-dwo | FileCheck %s --check-prefix=CHECK-PRESERVE-ATIME
5157
# RUN: ls -l %t-dwo | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
58+
# RUN: llvm-readobj %t-dwo
5259
# RUN: ls -lu %t-nondwo | FileCheck %s --check-prefix=CHECK-PRESERVE-ATIME
5360
# RUN: ls -l %t-nondwo | FileCheck %s --check-prefix=CHECK-PRESERVE-MTIME
61+
# RUN: llvm-readobj %t-nondwo
5462

5563
# CHECK-PRESERVE-ATIME: 1995
5664
# CHECK-PRESERVE-MTIME: 1997

tools/llvm-objcopy/llvm-objcopy.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,8 @@ static void restoreDateOnFile(StringRef Filename,
748748
const sys::fs::file_status &Stat) {
749749
int FD;
750750

751-
if (auto EC = sys::fs::openFileForWrite(Filename, FD))
751+
if (auto EC =
752+
sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting))
752753
reportError(Filename, EC);
753754

754755
if (auto EC = sys::fs::setLastAccessAndModificationTime(

0 commit comments

Comments
 (0)