Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -48,6 +48,7 @@ llvm::StringRef SoName; llvm::StringRef Sysroot; std::string RPath; + std::string Reproduce; std::vector DynamicList; std::vector SearchPaths; std::vector Undefined; Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -19,6 +19,7 @@ #include "Writer.h" #include "lld/Driver/Driver.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/Support/Path.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" #include @@ -97,12 +98,33 @@ return V; } +static void dumpFile(StringRef SrcPath) { + std::error_code EC; + + SmallString<128> DirName; + llvm::sys::path::append(DirName, Config->Reproduce, llvm::sys::path::parent_path(SrcPath)); + EC = llvm::sys::fs::create_directories(DirName); + if (EC) { + error("--reproduce: can't create directory " + DirName + ": " + EC.message()); + return; + } + + SmallString<128> DestPathName; + llvm::sys::path::append(DestPathName, Config->Reproduce, SrcPath); + EC = llvm::sys::fs::copy_file(SrcPath, DestPathName); + if (EC) + error("--reproduce: can't copy file " + SrcPath + ": " + EC.message()); +} + // Opens and parses a file. Path has to be resolved already. // Newly created memory buffers are owned by this driver. void LinkerDriver::addFile(StringRef Path) { using namespace llvm::sys::fs; if (Config->Verbose) llvm::outs() << Path << "\n"; + if (!Config->Reproduce.empty()) + dumpFile(Path); + Optional Buffer = readFile(Path); if (!Buffer.hasValue()) return; @@ -223,6 +245,21 @@ return false; } +static void dumpLinkerInvocation(ArrayRef Args) { + std::error_code EC = llvm::sys::fs::create_directories(Config->Reproduce); + if (EC) { + error("--reproduce: can't create directory."); + return; + } + + raw_fd_ostream OS(Config->Reproduce + "/invocation.txt", EC, + sys::fs::OpenFlags::F_None); + check(EC); + for (const char *Arg: Args) + OS << Arg << " "; + OS << "\n"; +} + void LinkerDriver::main(ArrayRef ArgsArr) { ELFOptTable Parser; opt::InputArgList Args = Parser.parse(ArgsArr.slice(1)); @@ -237,6 +274,10 @@ initLLVM(Args); readConfigs(Args); + + if (!Config->Reproduce.empty()) + dumpLinkerInvocation(ArgsArr); + createFiles(Args); checkOptions(Args); if (HasError) @@ -378,6 +419,12 @@ if (Optional Buffer = readFile(getString(Args, OPT_version_script))) parseVersionScript(*Buffer); + + if (auto *Arg = Args.getLastArg(OPT_reproduce)) { + Config->Reproduce = Arg->getValue(); + if (llvm::sys::fs::exists(Config->Reproduce)) + error("--reproduce: directory " + Config->Reproduce + " already exists."); + } } void LinkerDriver::createFiles(opt::InputArgList &Args) { Index: ELF/Options.td =================================================================== --- ELF/Options.td +++ ELF/Options.td @@ -120,6 +120,9 @@ def print_gc_sections: Flag<["--"], "print-gc-sections">, HelpText<"List removed unused sections">; +def reproduce : Separate<["--"], "reproduce">, + HelpText<"Dump linker invocation and input files for debugging">; + def rpath : Separate<["-"], "rpath">, HelpText<"Add a DT_RUNPATH to the output">;