Index: test/tools/llvm-objcopy/deterministic-archive.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/deterministic-archive.test @@ -0,0 +1,65 @@ +# RUN: yaml2obj %s > %t.o + +# Create an archive, specifying U so that timestamps/etc. are preserved. +# We only test timestamps as a proxy for full deterministic writing; i.e. we +# assume UID/GIDs are preserved if timestamps are preserved. +# RUN: touch -t 199505050555.55 %t.o +# RUN: rm -f %t.a +# RUN: llvm-ar crsU %t.a %t.o + +# Test short flags. +# RUN: llvm-objcopy -D %t.a %t.2D.a +# RUN: env TZ=GMT llvm-ar tv %t.2D.a | FileCheck %s --check-prefix=CHECK-DETERMINISTIC +# RUN: llvm-objcopy -U %t.a %t.2U.a +# RUN: env TZ=GMT llvm-ar tv %t.2U.a | FileCheck %s --check-prefix=CHECK-NONDETERMINISTIC + +# RUN: llvm-strip -D %t.a -o %t.3D.a +# RUN: env TZ=GMT llvm-ar tv %t.3D.a | FileCheck %s --check-prefix=CHECK-DETERMINISTIC +# RUN: llvm-strip -U %t.a -o %t.3U.a +# RUN: env TZ=GMT llvm-ar tv %t.3U.a | FileCheck %s --check-prefix=CHECK-NONDETERMINISTIC + +# Test long flags. +# RUN: llvm-objcopy --enable-deterministic-archives %t.a %t.4D.a +# RUN: env TZ=GMT llvm-ar tv %t.4D.a | FileCheck %s --check-prefix=CHECK-DETERMINISTIC +# RUN: llvm-objcopy --disable-deterministic-archives %t.a %t.4U.a +# RUN: env TZ=GMT llvm-ar tv %t.4U.a | FileCheck %s --check-prefix=CHECK-NONDETERMINISTIC + +# RUN: llvm-strip --enable-deterministic-archives %t.a -o %t.5D.a +# RUN: env TZ=GMT llvm-ar tv %t.5D.a | FileCheck %s --check-prefix=CHECK-DETERMINISTIC +# RUN: llvm-strip --disable-deterministic-archives %t.a -o %t.5U.a +# RUN: env TZ=GMT llvm-ar tv %t.5U.a | FileCheck %s --check-prefix=CHECK-NONDETERMINISTIC + +# If unspecified, verify that deterministic is the default. +# RUN: llvm-objcopy %t.a %t.6.a +# RUN: env TZ=GMT llvm-ar tv %t.6.a | FileCheck %s --check-prefix=CHECK-DETERMINISTIC +# RUN: llvm-strip %t.a -o %t.7.a +# RUN: env TZ=GMT llvm-ar tv %t.7.a | FileCheck %s --check-prefix=CHECK-DETERMINISTIC + +# If both are specified, last one wins. +# RUN: llvm-objcopy -U -D %t.a %t.8.a +# RUN: env TZ=GMT llvm-ar tv %t.8.a | FileCheck %s --check-prefix=CHECK-DETERMINISTIC +# RUN: llvm-objcopy -D -U %t.a %t.9.a +# RUN: env TZ=GMT llvm-ar tv %t.9.a | FileCheck %s --check-prefix=CHECK-NONDETERMINISTIC +# RUN: llvm-objcopy -D -U -D -U --enable-deterministic-archives %t.a %t.10.a +# RUN: env TZ=GMT llvm-ar tv %t.10.a | FileCheck %s --check-prefix=CHECK-DETERMINISTIC + +# RUN: llvm-strip -U -D %t.a -o %t.11.a +# RUN: env TZ=GMT llvm-ar tv %t.11.a | FileCheck %s --check-prefix=CHECK-DETERMINISTIC +# RUN: llvm-strip -D -U %t.a -o %t.12.a +# RUN: env TZ=GMT llvm-ar tv %t.12.a | FileCheck %s --check-prefix=CHECK-NONDETERMINISTIC +# RUN: llvm-strip -D -U -D -U --enable-deterministic-archives %t.a -o %t.13.a +# RUN: env TZ=GMT llvm-ar tv %t.13.a | FileCheck %s --check-prefix=CHECK-DETERMINISTIC + +# CHECK-DETERMINISTIC: {{[[:space:]]1970[[:space:]]}} +# CHECK-NONDETERMINISTIC: {{[[:space:]]1995[[:space:]]}} + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] Index: tools/llvm-objcopy/CopyConfig.h =================================================================== --- tools/llvm-objcopy/CopyConfig.h +++ tools/llvm-objcopy/CopyConfig.h @@ -72,6 +72,7 @@ StringMap SymbolsToRename; // Boolean options + bool DeterministicArchives = true; bool DiscardAll = false; bool ExtractDWO = false; bool KeepFileSymbols = false; Index: tools/llvm-objcopy/CopyConfig.cpp =================================================================== --- tools/llvm-objcopy/CopyConfig.cpp +++ tools/llvm-objcopy/CopyConfig.cpp @@ -343,6 +343,10 @@ for (auto Arg : InputArgs.filtered(OBJCOPY_keep_symbol)) Config.SymbolsToKeep.push_back(Arg->getValue()); + Config.DeterministicArchives = InputArgs.hasFlag( + OBJCOPY_enable_deterministic_archives, + OBJCOPY_disable_deterministic_archives, /*default=*/true); + Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates); if (Config.DecompressDebugSections && @@ -411,6 +415,10 @@ for (auto Arg : InputArgs.filtered(STRIP_keep_symbol)) Config.SymbolsToKeep.push_back(Arg->getValue()); + Config.DeterministicArchives = + InputArgs.hasFlag(STRIP_enable_deterministic_archives, + STRIP_disable_deterministic_archives, /*default=*/true); + Config.PreserveDates = InputArgs.hasArg(STRIP_preserve_dates); DriverConfig DC; Index: tools/llvm-objcopy/ObjcopyOpts.td =================================================================== --- tools/llvm-objcopy/ObjcopyOpts.td +++ tools/llvm-objcopy/ObjcopyOpts.td @@ -35,6 +35,22 @@ MetaVarName<"dwo-file">, HelpText<"Equivalent to extract-dwo on the input file to , then strip-dwo on the input file">; +def enable_deterministic_archives + : Flag<["-", "--"], "enable-deterministic-archives">, + HelpText<"Enable deterministic mode when copying archives (use zero for " + "UIDs, GIDs, and timestamps).">; +def D : Flag<["-"], "D">, + Alias, + HelpText<"Alias for --enable-deterministic-archives">; + +def disable_deterministic_archives + : Flag<["-", "--"], "disable-deterministic-archives">, + HelpText<"Disable deterministic mode when copying archives (use real " + "values for UIDs, GIDs, and timestamps).">; +def U : Flag<["-"], "U">, + Alias, + HelpText<"Alias for --disable-deterministic-archives">; + def preserve_dates : Flag<[ "-", "--" ], "preserve-dates">, HelpText<"Preserve access and modification timestamps">; Index: tools/llvm-objcopy/StripOpts.td =================================================================== --- tools/llvm-objcopy/StripOpts.td +++ tools/llvm-objcopy/StripOpts.td @@ -7,6 +7,22 @@ def help : Flag<["-", "--"], "help">; +def enable_deterministic_archives + : Flag<["-", "--"], "enable-deterministic-archives">, + HelpText<"Enable deterministic mode when stripping archives (use zero " + "for UIDs, GIDs, and timestamps).">; +def D : Flag<["-"], "D">, + Alias, + HelpText<"Alias for --enable-deterministic-archives">; + +def disable_deterministic_archives + : Flag<["-", "--"], "disable-deterministic-archives">, + HelpText<"Disable deterministic mode when stripping archives (use real " + "values for UIDs, GIDs, and timestamps).">; +def U : Flag<["-"], "U">, + Alias, + HelpText<"Alias for --disable-deterministic-archives">; + defm output : Eq<"o">, MetaVarName<"output">, HelpText<"Write output to ">; Index: tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- tools/llvm-objcopy/llvm-objcopy.cpp +++ tools/llvm-objcopy/llvm-objcopy.cpp @@ -147,7 +147,7 @@ executeObjcopyOnBinary(Config, *Bin, MB); Expected Member = - NewArchiveMember::getOldMember(Child, true); + NewArchiveMember::getOldMember(Child, Config.DeterministicArchives); if (!Member) reportError(Ar.getFileName(), Member.takeError()); Member->Buf = MB.releaseMemoryBuffer(); @@ -157,9 +157,9 @@ if (Err) reportError(Config.InputFilename, std::move(Err)); - if (Error E = - deepWriteArchive(Config.OutputFilename, NewArchiveMembers, - Ar.hasSymbolTable(), Ar.kind(), true, Ar.isThin())) + if (Error E = deepWriteArchive(Config.OutputFilename, NewArchiveMembers, + Ar.hasSymbolTable(), Ar.kind(), + Config.DeterministicArchives, Ar.isThin())) reportError(Config.OutputFilename, std::move(E)); }