diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -407,6 +407,12 @@ bool androidMemtagStack; unsigned threadCount; + + // If an input file equals a key, remap it to the value. + llvm::DenseMap remapInputs; + // If an input file matches a wildcard pattern, remap it to the value. + llvm::SmallVector, 0> + remapInputsWildcards; }; struct ConfigWrapper { Config c; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -1077,6 +1077,33 @@ return arg == "none" || arg == "warning" || arg == "error"; } +// A remap inputs file contains a tab-separated pair: from-glob, to-file. +static void readRemapInputsFile(StringRef filename) { + std::optional buffer = readFile(filename); + if (!buffer) + return; + SmallVector fields; + size_t lineno = 0; + for (StringRef line : args::getLines(*buffer)) { + fields.clear(); + line.split(fields, '\t'); + ++lineno; + if (fields.size() != 2) { + error(filename + ":" + Twine(lineno) + + ": parse error, not from-globto-file"); + return; + } + if (!hasWildcard(fields[0])) + config->remapInputs[fields[0]] = fields[1]; + else if (Expected pat = GlobPattern::create(fields[0])) + config->remapInputsWildcards.emplace_back(std::move(*pat), fields[1]); + else { + error(filename + ":" + Twine(lineno) + ": " + toString(pat.takeError())); + return; + } + } +} + // Initializes Config members by the command line options. static void readConfigs(opt::InputArgList &args) { errorHandler().verbose = args.hasArg(OPT_verbose); @@ -1335,6 +1362,9 @@ config->optEL = true; } + for (opt::Arg *arg : args.filtered(OPT_remap_inputs_file)) + readRemapInputsFile(arg->getValue()); + for (opt::Arg *arg : args.filtered(OPT_shuffle_sections)) { constexpr StringRef errPrefix = "--shuffle-sections=: "; std::pair kv = StringRef(arg->getValue()).split('='); diff --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp --- a/lld/ELF/DriverUtils.cpp +++ b/lld/ELF/DriverUtils.cpp @@ -192,6 +192,7 @@ case OPT_export_dynamic_symbol_list: case OPT_just_symbols: case OPT_library_path: + case OPT_remap_inputs_file: case OPT_retain_symbols_file: case OPT_rpath: case OPT_script: diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -195,6 +195,29 @@ if (!config->chroot.empty() && path.startswith("/")) path = saver().save(config->chroot + path); + bool remapped = false; + auto it = config->remapInputs.find(path); + if (it != config->remapInputs.end()) { + path = it->second; + remapped = true; + } else { + for (const auto &[pat, toFile] : config->remapInputsWildcards) { + if (pat.match(path)) { + path = toFile; + remapped = true; + break; + } + } + } + if (remapped) { + // Use /dev/null to indicate an input file that should be ignored. Change + // the path to NUL on Windows. +#ifdef _WIN32 + if (path == "/dev/null") + path = "NUL"; +#endif + } + log(path); config->dependencyFiles.insert(llvm::CachedHashString(path)); diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td --- a/lld/ELF/Options.td +++ b/lld/ELF/Options.td @@ -359,6 +359,10 @@ "Enable global pointer relaxation", "Disable global pointer relaxation (default)">; +def remap_inputs_file: JJ<"remap-inputs-file=">, + HelpText<"Each line contains from-globto-file. An input file matching is remapped to ">, + MetaVarName<"">; + defm reproduce: EEq<"reproduce", "Write tar file containing inputs and command to reproduce link">; @@ -610,6 +614,7 @@ defm thinlto_cache_policy: EEq<"thinlto-cache-policy", "Pruning policy for the ThinLTO cache">; def thinlto_emit_imports_files: FF<"thinlto-emit-imports-files">; def thinlto_emit_index_files: FF<"thinlto-emit-index-files">; +def thinlto_index: JJ<"thinlto-index=">; def thinlto_index_only: FF<"thinlto-index-only">; def thinlto_index_only_eq: JJ<"thinlto-index-only=">; def thinlto_jobs_eq: JJ<"thinlto-jobs=">, diff --git a/lld/docs/ld.lld.1 b/lld/docs/ld.lld.1 --- a/lld/docs/ld.lld.1 +++ b/lld/docs/ld.lld.1 @@ -497,6 +497,13 @@ Enable global pointer relaxation for RISC-V. .It Fl -relocatable , Fl r Create relocatable object file. +.It Fl -remap-inputs-file Ns = Ns Ar file +Remap input files based on patterns in +.Ar file . +Each line in the remap file is of the format +.Cm from-globto-file +or a comment starting with +.Cm # . .It Fl -reproduce Ns = Ns Ar path Write a tar file to .Ar path, diff --git a/lld/test/ELF/remap-inputs-file.test b/lld/test/ELF/remap-inputs-file.test new file mode 100644 --- /dev/null +++ b/lld/test/ELF/remap-inputs-file.test @@ -0,0 +1,77 @@ +# REQUIRES: x86 +## --remap-inputs-file= remaps an input file to another file. +## It can be specified multiple times. + +# RUN: rm -rf %t && split-file %s %t && cd %t +# RUN: llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o +# RUN: llvm-as b.ll -o b.o +# RUN: llvm-mc -filetype=obj -triple=x86_64 c.s -o c.o && llvm-ar rc c.a c.o +# RUN: llvm-mc -filetype=obj -triple=x86_64 d.s -o d.o && ld.lld -shared -soname=d d.o -o d.so +# RUN: ld.lld --remap-inputs-file=1.map --remap-inputs-file=2.map --reproduce=repro.tar aa.o bb.bc cc.a dd.so empty -o 1 +# RUN: tar tf repro.tar | FileCheck %s --check-prefix=REPRO + +# REPRO: 1.map +# REPRO-NEXT: 2.map +# REPRO-NEXT: a.o +# REPRO-NEXT: b.o +# REPRO-NEXT: c.a +# REPRO-NEXT: d.so + +## A multiple-to-one pattern may easily cause issues. Users should be careful. +# RUN: not ld.lld --remap-inputs-file=3.map aa.o bb.bc -o /dev/null 2>&1 | \ +# RUN: FileCheck %s --check-prefix=DUPLICATE --implicit-check-not=error: +# DUPLICATE: error: duplicate symbol: _start + +# RUN: not ld.lld --remap-inputs-file=err1.map --reproduce=repro.tar aa.o bb.bc -o /dev/null 2>&1 | \ +# RUN: FileCheck %s --check-prefix=ERR1 --implicit-check-not=error: +# ERR1: error: err1.map:2: parse error, not from-globto-file +# ERR1-NEXT: error: cannot open bb.bc: {{.*}} + +# RUN: not ld.lld --remap-inputs-file=err2.map aa.o -o /dev/null 2>&1 | \ +# RUN: FileCheck %s --check-prefix=ERR2 --implicit-check-not=error: +# ERR2: error: err2.map:1: invalid glob pattern: aa.[o +# ERR2-NEXT: error: cannot open aa.o: {{.*}} + +#--- a.s +.globl _start +_start: + call b + call c + call d + +#--- b.ll +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define void @b() { + ret void +} + +#--- c.s +.globl c +c: + +#--- d.s +.globl d +d: + +#--- 1.map +aa.o a.o +b?.[b]c b.o +cc.a c.a + +#--- 2.map +d*.so d.so +## Use /dev/null to indicate an input file which should be ignored. +empty /dev/null + +#--- 3.map +* a.o + +#--- err1.map +aa.o a.o +bb.bc +cc.a + +#--- err2.map +aa.[o a.o