aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorJordan Rupprecht <rupprecht@google.com>2018-08-16 18:29:40 +0000
committerJordan Rupprecht <rupprecht@google.com>2018-08-16 18:29:40 +0000
commit7b455c4fd2fc947f57e62746093b670a152681ea (patch)
treec0b32feabcabf3fdb90ca2dc665ad4aadeeb18c8 /tools
parentd9c9d82716750f150de211c9889cb7678cd07888 (diff)
[llvm-strip] Add support for -p/--preserve-dates
Summary: [llvm-strip] Preserve access/modification timestamps when -p is used. Reviewers: jakehehrlich, jhenderson, alexshap Reviewed By: jhenderson Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D50744 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@339921 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r--tools/llvm-objcopy/ObjcopyOpts.td6
-rw-r--r--tools/llvm-objcopy/StripOpts.td5
-rw-r--r--tools/llvm-objcopy/llvm-objcopy.cpp41
3 files changed, 48 insertions, 4 deletions
diff --git a/tools/llvm-objcopy/ObjcopyOpts.td b/tools/llvm-objcopy/ObjcopyOpts.td
index 5e3ac1f7947..8cdef453b74 100644
--- a/tools/llvm-objcopy/ObjcopyOpts.td
+++ b/tools/llvm-objcopy/ObjcopyOpts.td
@@ -22,6 +22,12 @@ def O : JoinedOrSeparate<["-"], "O">,
defm split_dwo : Eq<"split-dwo">,
MetaVarName<"dwo-file">,
HelpText<"Equivalent to extract-dwo on the input file to <dwo-file>, then strip-dwo on the input file">;
+
+def preserve_dates : Flag<[ "-", "--" ], "preserve-dates">,
+ HelpText<"Preserve access and modification timestamps">;
+
+def p : Flag<[ "-" ], "p">, Alias<preserve_dates>;
+
defm add_gnu_debuglink : Eq<"add-gnu-debuglink">,
MetaVarName<"debug-file">,
HelpText<"Add a .gnu_debuglink for <debug-file>">;
diff --git a/tools/llvm-objcopy/StripOpts.td b/tools/llvm-objcopy/StripOpts.td
index 333b0d288ef..5abc23fb577 100644
--- a/tools/llvm-objcopy/StripOpts.td
+++ b/tools/llvm-objcopy/StripOpts.td
@@ -11,6 +11,11 @@ defm output : Eq<"o">,
MetaVarName<"output">,
HelpText<"Write output to <file>">;
+def preserve_dates : Flag<[ "-", "--" ], "preserve-dates">,
+ HelpText<"Preserve access and modification timestamps">;
+
+def p : Flag<[ "-" ], "p">, Alias<preserve_dates>;
+
def strip_all : Flag<["-", "--"], "strip-all">,
HelpText<"Remove non-allocated sections other than .gnu.warning* sections">;
diff --git a/tools/llvm-objcopy/llvm-objcopy.cpp b/tools/llvm-objcopy/llvm-objcopy.cpp
index 73db661283a..ca6609f9a99 100644
--- a/tools/llvm-objcopy/llvm-objcopy.cpp
+++ b/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -35,6 +35,7 @@
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
@@ -161,6 +162,7 @@ struct CopyConfig {
bool DiscardAll = false;
bool OnlyKeepDebug = false;
bool KeepFileSymbols = false;
+ bool PreserveDates = false;
};
using SectionPred = std::function<bool(const SectionBase &Sec)>;
@@ -675,17 +677,44 @@ static void executeElfObjcopyOnArchive(const CopyConfig &Config,
reportError(Config.OutputFilename, std::move(E));
}
+static void restoreDateOnFile(StringRef Filename,
+ const sys::fs::file_status &Stat) {
+ int FD;
+
+ if (auto EC = sys::fs::openFileForWrite(Filename, FD))
+ reportError(Filename, EC);
+
+ if (auto EC = sys::fs::setLastAccessAndModificationTime(
+ FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime()))
+ reportError(Filename, EC);
+
+ if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
+ reportError(Filename, EC);
+}
+
static void executeElfObjcopy(const CopyConfig &Config) {
+ sys::fs::file_status Stat;
+ if (Config.PreserveDates)
+ if (auto EC = sys::fs::status(Config.InputFilename, Stat))
+ reportError(Config.InputFilename, EC);
+
Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr =
createBinary(Config.InputFilename);
if (!BinaryOrErr)
reportError(Config.InputFilename, BinaryOrErr.takeError());
- if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary()))
- return executeElfObjcopyOnArchive(Config, *Ar);
+ if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary())) {
+ executeElfObjcopyOnArchive(Config, *Ar);
+ } else {
+ FileBuffer FB(Config.OutputFilename);
+ executeElfObjcopyOnBinary(Config, *BinaryOrErr.get().getBinary(), FB);
+ }
- FileBuffer FB(Config.OutputFilename);
- executeElfObjcopyOnBinary(Config, *BinaryOrErr.get().getBinary(), FB);
+ if (Config.PreserveDates) {
+ restoreDateOnFile(Config.OutputFilename, Stat);
+ if (!Config.SplitDWO.empty())
+ restoreDateOnFile(Config.SplitDWO, Stat);
+ }
}
// ParseObjcopyOptions returns the config and sets the input arguments. If a
@@ -780,6 +809,8 @@ static CopyConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol))
Config.SymbolsToKeep.push_back(Arg->getValue());
+ Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates);
+
return Config;
}
@@ -834,6 +865,8 @@ static CopyConfig parseStripOptions(ArrayRef<const char *> ArgsArr) {
for (auto Arg : InputArgs.filtered(STRIP_keep_symbol))
Config.SymbolsToKeep.push_back(Arg->getValue());
+ Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates);
+
return Config;
}