2015-07-25 05:03:07 +08:00
|
|
|
//===- DriverUtils.cpp ----------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-07-25 05:03:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains utility functions for the driver. Because there
|
|
|
|
// are so many small functions, we created this separate file to make
|
|
|
|
// Driver.cpp less cluttered.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Driver.h"
|
2022-01-21 03:53:18 +08:00
|
|
|
#include "lld/Common/CommonLinkerContext.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/Reproduce.h"
|
|
|
|
#include "lld/Common/Version.h"
|
2016-11-20 03:23:56 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-07-07 05:24:34 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2016-05-05 07:12:55 +08:00
|
|
|
#include "llvm/Option/Option.h"
|
2015-09-25 23:37:33 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2015-10-11 11:28:39 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2020-03-12 06:39:28 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2015-10-11 11:28:39 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-12-20 10:17:24 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2020-11-03 22:41:09 +08:00
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2016-04-27 04:41:32 +08:00
|
|
|
using namespace llvm::sys;
|
2018-02-07 02:12:41 +08:00
|
|
|
using namespace llvm::opt;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
// Create OptTable
|
|
|
|
|
|
|
|
// Create prefix string literals used in Options.td
|
|
|
|
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef PREFIX
|
|
|
|
|
|
|
|
// Create table mapping all options defined in Options.td
|
2016-04-03 03:15:26 +08:00
|
|
|
static const opt::OptTable::Info optInfo[] = {
|
2017-06-21 03:17:58 +08:00
|
|
|
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
|
|
|
|
{X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \
|
|
|
|
X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12},
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2016-04-03 03:15:26 +08:00
|
|
|
ELFOptTable::ELFOptTable() : OptTable(optInfo) {}
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2021-10-26 03:52:06 +08:00
|
|
|
// Set color diagnostics according to --color-diagnostics={auto,always,never}
|
|
|
|
// or --no-color-diagnostics flags.
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
static void handleColorDiagnostics(opt::InputArgList &args) {
|
2016-12-22 16:20:28 +08:00
|
|
|
auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
|
|
|
|
OPT_no_color_diagnostics);
|
2016-12-20 10:17:24 +08:00
|
|
|
if (!arg)
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
return;
|
2018-05-11 02:19:02 +08:00
|
|
|
if (arg->getOption().getID() == OPT_color_diagnostics) {
|
2019-11-20 23:08:18 +08:00
|
|
|
lld::errs().enable_colors(true);
|
2018-05-11 02:19:02 +08:00
|
|
|
} else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
|
2019-11-20 23:08:18 +08:00
|
|
|
lld::errs().enable_colors(false);
|
2018-05-11 02:19:02 +08:00
|
|
|
} else {
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
StringRef s = arg->getValue();
|
|
|
|
if (s == "always")
|
2019-11-20 23:08:18 +08:00
|
|
|
lld::errs().enable_colors(true);
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
else if (s == "never")
|
2019-11-20 23:08:18 +08:00
|
|
|
lld::errs().enable_colors(false);
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
else if (s != "auto")
|
2018-05-11 02:19:02 +08:00
|
|
|
error("unknown option: --color-diagnostics=" + s);
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
}
|
2016-12-20 10:17:24 +08:00
|
|
|
}
|
|
|
|
|
2016-07-07 05:24:34 +08:00
|
|
|
static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) {
|
|
|
|
if (auto *arg = args.getLastArg(OPT_rsp_quoting)) {
|
|
|
|
StringRef s = arg->getValue();
|
|
|
|
if (s != "windows" && s != "posix")
|
|
|
|
error("invalid response file quoting: " + s);
|
|
|
|
if (s == "windows")
|
|
|
|
return cl::TokenizeWindowsCommandLine;
|
|
|
|
return cl::TokenizeGNUCommandLine;
|
|
|
|
}
|
2020-03-05 03:29:45 +08:00
|
|
|
if (Triple(sys::getProcessTriple()).isOSWindows())
|
2016-07-07 05:24:34 +08:00
|
|
|
return cl::TokenizeWindowsCommandLine;
|
|
|
|
return cl::TokenizeGNUCommandLine;
|
|
|
|
}
|
|
|
|
|
2018-05-22 10:53:11 +08:00
|
|
|
// Gold LTO plugin takes a `--plugin-opt foo=bar` option as an alias for
|
|
|
|
// `--plugin-opt=foo=bar`. We want to handle `--plugin-opt=foo=` as an
|
|
|
|
// option name and `bar` as a value. Unfortunately, OptParser cannot
|
|
|
|
// handle an option with a space in it.
|
|
|
|
//
|
|
|
|
// In this function, we concatenate command line arguments so that
|
|
|
|
// `--plugin-opt <foo>` is converted to `--plugin-opt=<foo>`. This is a
|
|
|
|
// bit hacky, but looks like it is still better than handling --plugin-opt
|
|
|
|
// options by hand.
|
|
|
|
static void concatLTOPluginOptions(SmallVectorImpl<const char *> &args) {
|
|
|
|
SmallVector<const char *, 256> v;
|
|
|
|
for (size_t i = 0, e = args.size(); i != e; ++i) {
|
|
|
|
StringRef s = args[i];
|
|
|
|
if ((s == "-plugin-opt" || s == "--plugin-opt") && i + 1 != e) {
|
2022-01-21 03:53:18 +08:00
|
|
|
v.push_back(saver().save(s + "=" + args[i + 1]).data());
|
2018-05-22 10:53:11 +08:00
|
|
|
++i;
|
|
|
|
} else {
|
|
|
|
v.push_back(args[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args = std::move(v);
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
// Parses a given list of options.
|
2016-03-16 02:20:50 +08:00
|
|
|
opt::InputArgList ELFOptTable::parse(ArrayRef<const char *> argv) {
|
2015-07-25 05:03:07 +08:00
|
|
|
// Make InputArgList from string vectors.
|
|
|
|
unsigned missingIndex;
|
|
|
|
unsigned missingCount;
|
2016-07-07 05:24:34 +08:00
|
|
|
SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
|
|
|
|
|
|
|
|
// We need to get the quoting style for response files before parsing all
|
|
|
|
// options so we parse here before and ignore all the options but
|
|
|
|
// --rsp-quoting.
|
|
|
|
opt::InputArgList args = this->ParseArgs(vec, missingIndex, missingCount);
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2016-12-20 10:17:24 +08:00
|
|
|
// Expand response files (arguments in the form of @<filename>)
|
|
|
|
// and then parse the argument again.
|
2022-01-21 03:53:18 +08:00
|
|
|
cl::ExpandResponseFiles(saver(), getQuotingStyle(args), vec);
|
2018-05-22 10:53:11 +08:00
|
|
|
concatLTOPluginOptions(vec);
|
2016-07-07 05:24:34 +08:00
|
|
|
args = this->ParseArgs(vec, missingIndex, missingCount);
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
handleColorDiagnostics(args);
|
2015-07-25 05:03:07 +08:00
|
|
|
if (missingCount)
|
2016-11-20 02:49:38 +08:00
|
|
|
error(Twine(args.getArgString(missingIndex)) + ": missing argument");
|
[Coding style change] Rename variables so that they start with a lowercase letter
This patch is mechanically generated by clang-llvm-rename tool that I wrote
using Clang Refactoring Engine just for creating this patch. You can see the
source code of the tool at https://reviews.llvm.org/D64123. There's no manual
post-processing; you can generate the same patch by re-running the tool against
lld's code base.
Here is the main discussion thread to change the LLVM coding style:
https://lists.llvm.org/pipermail/llvm-dev/2019-February/130083.html
In the discussion thread, I proposed we use lld as a testbed for variable
naming scheme change, and this patch does that.
I chose to rename variables so that they are in camelCase, just because that
is a minimal change to make variables to start with a lowercase letter.
Note to downstream patch maintainers: if you are maintaining a downstream lld
repo, just rebasing ahead of this commit would cause massive merge conflicts
because this patch essentially changes every line in the lld subdirectory. But
there's a remedy.
clang-llvm-rename tool is a batch tool, so you can rename variables in your
downstream repo with the tool. Given that, here is how to rebase your repo to
a commit after the mass renaming:
1. rebase to the commit just before the mass variable renaming,
2. apply the tool to your downstream repo to mass-rename variables locally, and
3. rebase again to the head.
Most changes made by the tool should be identical for a downstream repo and
for the head, so at the step 3, almost all changes should be merged and
disappear. I'd expect that there would be some lines that you need to merge by
hand, but that shouldn't be too many.
Differential Revision: https://reviews.llvm.org/D64121
llvm-svn: 365595
2019-07-10 13:00:37 +08:00
|
|
|
|
2020-11-23 23:51:56 +08:00
|
|
|
for (opt::Arg *arg : args.filtered(OPT_UNKNOWN)) {
|
2019-05-07 21:48:30 +08:00
|
|
|
std::string nearest;
|
|
|
|
if (findNearest(arg->getAsString(args), nearest) > 1)
|
2019-07-05 20:31:32 +08:00
|
|
|
error("unknown argument '" + arg->getAsString(args) + "'");
|
2019-05-07 21:48:30 +08:00
|
|
|
else
|
2019-07-05 20:31:32 +08:00
|
|
|
error("unknown argument '" + arg->getAsString(args) +
|
|
|
|
"', did you mean '" + nearest + "'");
|
2019-05-07 21:48:30 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
return args;
|
|
|
|
}
|
2015-10-11 11:28:39 +08:00
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::printHelp() {
|
2021-06-25 05:47:03 +08:00
|
|
|
ELFOptTable().printHelp(
|
Make it possible to redirect not only errs() but also outs()
This change is for those who use lld as a library. Context:
https://reviews.llvm.org/D70287
This patch adds a new parmeter to lld::*::link() so that we can pass
an raw_ostream object representing stdout. Previously, lld::*::link()
took only an stderr object.
Justification for making stdoutOS and stderrOS mandatory: I wanted to
make link() functions to take stdout and stderr in that order.
However, if we change the function signature from
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stderrOS = llvm::errs());
to
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stdoutOS = llvm::outs(),
raw_ostream &stderrOS = llvm::errs());
, then the meaning of existing code that passes stderrOS silently
changes (stderrOS would be interpreted as stdoutOS). So, I chose to
make existing code not to compile, so that developers can fix their
code.
Differential Revision: https://reviews.llvm.org/D70292
2019-11-15 13:06:57 +08:00
|
|
|
lld::outs(), (config->progName + " [options] file...").str().c_str(),
|
|
|
|
"lld", false /*ShowHidden*/, true /*ShowAllAliases*/);
|
|
|
|
lld::outs() << "\n";
|
2017-03-23 09:00:22 +08:00
|
|
|
|
2021-10-26 03:52:06 +08:00
|
|
|
// Scripts generated by Libtool versions up to 2021-10 expect /: supported
|
|
|
|
// targets:.* elf/ in a message for the --help option. If it doesn't match,
|
|
|
|
// the scripts assume that the linker doesn't support very basic features
|
|
|
|
// such as shared libraries. Therefore, we need to print out at least "elf".
|
Make it possible to redirect not only errs() but also outs()
This change is for those who use lld as a library. Context:
https://reviews.llvm.org/D70287
This patch adds a new parmeter to lld::*::link() so that we can pass
an raw_ostream object representing stdout. Previously, lld::*::link()
took only an stderr object.
Justification for making stdoutOS and stderrOS mandatory: I wanted to
make link() functions to take stdout and stderr in that order.
However, if we change the function signature from
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stderrOS = llvm::errs());
to
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stdoutOS = llvm::outs(),
raw_ostream &stderrOS = llvm::errs());
, then the meaning of existing code that passes stderrOS silently
changes (stderrOS would be interpreted as stdoutOS). So, I chose to
make existing code not to compile, so that developers can fix their
code.
Differential Revision: https://reviews.llvm.org/D70292
2019-11-15 13:06:57 +08:00
|
|
|
lld::outs() << config->progName << ": supported targets: elf\n";
|
2016-02-28 11:18:09 +08:00
|
|
|
}
|
|
|
|
|
2019-02-23 07:59:43 +08:00
|
|
|
static std::string rewritePath(StringRef s) {
|
|
|
|
if (fs::exists(s))
|
|
|
|
return relativeToRoot(s);
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(s);
|
2019-02-23 07:59:43 +08:00
|
|
|
}
|
|
|
|
|
2016-05-16 01:10:23 +08:00
|
|
|
// Reconstructs command line arguments so that so that you can re-run
|
|
|
|
// the same command with the same inputs. This is for --reproduce.
|
2020-05-15 13:18:58 +08:00
|
|
|
std::string elf::createResponseFile(const opt::InputArgList &args) {
|
2016-05-04 01:30:44 +08:00
|
|
|
SmallString<0> data;
|
|
|
|
raw_svector_ostream os(data);
|
2017-07-21 02:17:55 +08:00
|
|
|
os << "--chroot .\n";
|
2016-05-16 01:10:23 +08:00
|
|
|
|
|
|
|
// Copy the command line to the output while rewriting paths.
|
2016-05-01 05:40:04 +08:00
|
|
|
for (auto *arg : args) {
|
Make joined instances of JoinedOrSeparate flags point to the unaliased args, like all other arg types do
This fixes an 8-year-old regression. r105763 made it so that aliases
always refer to the unaliased option – but it missed the "joined" branch
of JoinedOrSeparate flags. (r162231 then made the Args classes
non-virtual, and r169344 moved them from clang to llvm.)
Back then, there was no JoinedOrSeparate flag that was an alias, so it
wasn't observable. Now /U in CLCompatOptions is a JoinedOrSeparate alias
in clang, and warn_slash_u_filename incorrectly used the aliased arg id
(using the unaliased one isn't really a regression since that warning
checks if the undefined macro contains slash or backslash and only then
emits the warning – and no valid use will pass "-Ufoo/bar" or similar).
Also, lld has many JoinedOrSeparate aliases, and due to this bug it had
to explicitly call `getUnaliasedOption()` in a bunch of places, even
though that shouldn't be necessary by design. After this fix in Option,
these calls really don't have an effect any more, so remove them.
No intended behavior change.
(I accidentally fixed this bug while working on PR29106 but then
wondered why the warn_slash_u_filename broke. When I figured it out, I
thought it would make sense to land this in a separate commit.)
Differential Revision: https://reviews.llvm.org/D64156
llvm-svn: 365186
2019-07-05 19:45:24 +08:00
|
|
|
switch (arg->getOption().getID()) {
|
2016-05-01 05:40:04 +08:00
|
|
|
case OPT_reproduce:
|
|
|
|
break;
|
2016-05-01 06:46:47 +08:00
|
|
|
case OPT_INPUT:
|
|
|
|
os << quote(rewritePath(arg->getValue())) << "\n";
|
|
|
|
break;
|
2017-02-02 02:04:45 +08:00
|
|
|
case OPT_o:
|
|
|
|
// If -o path contains directories, "lld @response.txt" will likely
|
|
|
|
// fail because the archive we are creating doesn't contain empty
|
|
|
|
// directories for the output path (-o doesn't create directories).
|
|
|
|
// Strip directories to prevent the issue.
|
2020-11-29 11:38:27 +08:00
|
|
|
os << "-o " << quote(path::filename(arg->getValue())) << "\n";
|
2017-02-02 02:04:45 +08:00
|
|
|
break;
|
2021-02-06 09:56:45 +08:00
|
|
|
case OPT_lto_sample_profile:
|
|
|
|
os << arg->getSpelling() << quote(rewritePath(arg->getValue())) << "\n";
|
|
|
|
break;
|
2020-12-01 08:54:04 +08:00
|
|
|
case OPT_call_graph_ordering_file:
|
2016-05-01 06:46:47 +08:00
|
|
|
case OPT_dynamic_list:
|
2020-12-01 08:54:04 +08:00
|
|
|
case OPT_just_symbols:
|
2017-07-22 00:27:26 +08:00
|
|
|
case OPT_library_path:
|
2020-12-01 08:54:04 +08:00
|
|
|
case OPT_retain_symbols_file:
|
2016-05-01 06:46:47 +08:00
|
|
|
case OPT_rpath:
|
2016-05-01 05:40:04 +08:00
|
|
|
case OPT_script:
|
2017-09-06 18:10:49 +08:00
|
|
|
case OPT_symbol_ordering_file:
|
2017-11-20 23:49:48 +08:00
|
|
|
case OPT_sysroot:
|
2016-05-01 06:46:47 +08:00
|
|
|
case OPT_version_script:
|
2016-07-26 10:00:42 +08:00
|
|
|
os << arg->getSpelling() << " " << quote(rewritePath(arg->getValue()))
|
|
|
|
<< "\n";
|
2016-05-01 05:40:04 +08:00
|
|
|
break;
|
|
|
|
default:
|
2017-12-06 00:50:46 +08:00
|
|
|
os << toString(*arg) << "\n";
|
2016-05-01 05:40:04 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(data.str());
|
2016-04-27 04:41:32 +08:00
|
|
|
}
|
|
|
|
|
2016-11-20 03:23:56 +08:00
|
|
|
// Find a file by concatenating given paths. If a resulting path
|
|
|
|
// starts with "=", the character is replaced with a --sysroot value.
|
|
|
|
static Optional<std::string> findFile(StringRef path1, const Twine &path2) {
|
|
|
|
SmallString<128> s;
|
|
|
|
if (path1.startswith("="))
|
|
|
|
path::append(s, config->sysroot, path1.substr(1), path2);
|
|
|
|
else
|
|
|
|
path::append(s, path1, path2);
|
|
|
|
|
|
|
|
if (fs::exists(s))
|
2020-01-30 13:30:21 +08:00
|
|
|
return std::string(s);
|
2016-11-20 03:23:56 +08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
Optional<std::string> elf::findFromSearchPaths(StringRef path) {
|
2016-11-20 03:23:56 +08:00
|
|
|
for (StringRef dir : config->searchPaths)
|
|
|
|
if (Optional<std::string> s = findFile(dir, path))
|
2016-11-20 03:23:58 +08:00
|
|
|
return s;
|
|
|
|
return None;
|
2015-10-11 11:28:42 +08:00
|
|
|
}
|
|
|
|
|
[ELF] Implement Dependent Libraries Feature
This patch implements a limited form of autolinking primarily designed to allow
either the --dependent-library compiler option, or "comment lib" pragmas (
https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=vs-2017) in
C/C++ e.g. #pragma comment(lib, "foo"), to cause an ELF linker to automatically
add the specified library to the link when processing the input file generated
by the compiler.
Currently this extension is unique to LLVM and LLD. However, care has been taken
to design this feature so that it could be supported by other ELF linkers.
The design goals were to provide:
- A simple linking model for developers to reason about.
- The ability to to override autolinking from the linker command line.
- Source code compatibility, where possible, with "comment lib" pragmas in other
environments (MSVC in particular).
Dependent library support is implemented differently for ELF platforms than on
the other platforms. Primarily this difference is that on ELF we pass the
dependent library specifiers directly to the linker without manipulating them.
This is in contrast to other platforms where they are mapped to a specific
linker option by the compiler. This difference is a result of the greater
variety of ELF linkers and the fact that ELF linkers tend to handle libraries in
a more complicated fashion than on other platforms. This forces us to defer
handling the specifiers to the linker.
In order to achieve a level of source code compatibility with other platforms
we have restricted this feature to work with libraries that meet the following
"reasonable" requirements:
1. There are no competing defined symbols in a given set of libraries, or
if they exist, the program owner doesn't care which is linked to their
program.
2. There may be circular dependencies between libraries.
The binary representation is a mergeable string section (SHF_MERGE,
SHF_STRINGS), called .deplibs, with custom type SHT_LLVM_DEPENDENT_LIBRARIES
(0x6fff4c04). The compiler forms this section by concatenating the arguments of
the "comment lib" pragmas and --dependent-library options in the order they are
encountered. Partial (-r, -Ur) links are handled by concatenating .deplibs
sections with the normal mergeable string section rules. As an example, #pragma
comment(lib, "foo") would result in:
.section ".deplibs","MS",@llvm_dependent_libraries,1
.asciz "foo"
For LTO, equivalent information to the contents of a the .deplibs section can be
retrieved by the LLD for bitcode input files.
LLD processes the dependent library specifiers in the following way:
1. Dependent libraries which are found from the specifiers in .deplibs sections
of relocatable object files are added when the linker decides to include that
file (which could itself be in a library) in the link. Dependent libraries
behave as if they were appended to the command line after all other options. As
a consequence the set of dependent libraries are searched last to resolve
symbols.
2. It is an error if a file cannot be found for a given specifier.
3. Any command line options in effect at the end of the command line parsing apply
to the dependent libraries, e.g. --whole-archive.
4. The linker tries to add a library or relocatable object file from each of the
strings in a .deplibs section by; first, handling the string as if it was
specified on the command line; second, by looking for the string in each of the
library search paths in turn; third, by looking for a lib<string>.a or
lib<string>.so (depending on the current mode of the linker) in each of the
library search paths.
5. A new command line option --no-dependent-libraries tells LLD to ignore the
dependent libraries.
Rationale for the above points:
1. Adding the dependent libraries last makes the process simple to understand
from a developers perspective. All linkers are able to implement this scheme.
2. Error-ing for libraries that are not found seems like better behavior than
failing the link during symbol resolution.
3. It seems useful for the user to be able to apply command line options which
will affect all of the dependent libraries. There is a potential problem of
surprise for developers, who might not realize that these options would apply
to these "invisible" input files; however, despite the potential for surprise,
this is easy for developers to reason about and gives developers the control
that they may require.
4. This algorithm takes into account all of the different ways that ELF linkers
find input files. The different search methods are tried by the linker in most
obvious to least obvious order.
5. I considered adding finer grained control over which dependent libraries were
ignored (e.g. MSVC has /nodefaultlib:<library>); however, I concluded that this
is not necessary: if finer control is required developers can fall back to using
the command line directly.
RFC thread: http://lists.llvm.org/pipermail/llvm-dev/2019-March/131004.html.
Differential Revision: https://reviews.llvm.org/D60274
llvm-svn: 360984
2019-05-17 11:44:15 +08:00
|
|
|
// This is for -l<basename>. We'll look for lib<basename>.so or lib<basename>.a from
|
2016-11-20 03:26:52 +08:00
|
|
|
// search paths.
|
2020-05-15 13:18:58 +08:00
|
|
|
Optional<std::string> elf::searchLibraryBaseName(StringRef name) {
|
2016-06-27 15:26:28 +08:00
|
|
|
for (StringRef dir : config->searchPaths) {
|
2016-11-20 03:23:56 +08:00
|
|
|
if (!config->isStatic)
|
|
|
|
if (Optional<std::string> s = findFile(dir, "lib" + name + ".so"))
|
2016-11-20 03:23:58 +08:00
|
|
|
return s;
|
2016-11-20 03:23:56 +08:00
|
|
|
if (Optional<std::string> s = findFile(dir, "lib" + name + ".a"))
|
2016-11-20 03:23:58 +08:00
|
|
|
return s;
|
2015-10-11 11:28:39 +08:00
|
|
|
}
|
2016-11-20 03:23:58 +08:00
|
|
|
return None;
|
2015-10-11 11:28:39 +08:00
|
|
|
}
|
2017-11-20 23:43:20 +08:00
|
|
|
|
[ELF] Implement Dependent Libraries Feature
This patch implements a limited form of autolinking primarily designed to allow
either the --dependent-library compiler option, or "comment lib" pragmas (
https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=vs-2017) in
C/C++ e.g. #pragma comment(lib, "foo"), to cause an ELF linker to automatically
add the specified library to the link when processing the input file generated
by the compiler.
Currently this extension is unique to LLVM and LLD. However, care has been taken
to design this feature so that it could be supported by other ELF linkers.
The design goals were to provide:
- A simple linking model for developers to reason about.
- The ability to to override autolinking from the linker command line.
- Source code compatibility, where possible, with "comment lib" pragmas in other
environments (MSVC in particular).
Dependent library support is implemented differently for ELF platforms than on
the other platforms. Primarily this difference is that on ELF we pass the
dependent library specifiers directly to the linker without manipulating them.
This is in contrast to other platforms where they are mapped to a specific
linker option by the compiler. This difference is a result of the greater
variety of ELF linkers and the fact that ELF linkers tend to handle libraries in
a more complicated fashion than on other platforms. This forces us to defer
handling the specifiers to the linker.
In order to achieve a level of source code compatibility with other platforms
we have restricted this feature to work with libraries that meet the following
"reasonable" requirements:
1. There are no competing defined symbols in a given set of libraries, or
if they exist, the program owner doesn't care which is linked to their
program.
2. There may be circular dependencies between libraries.
The binary representation is a mergeable string section (SHF_MERGE,
SHF_STRINGS), called .deplibs, with custom type SHT_LLVM_DEPENDENT_LIBRARIES
(0x6fff4c04). The compiler forms this section by concatenating the arguments of
the "comment lib" pragmas and --dependent-library options in the order they are
encountered. Partial (-r, -Ur) links are handled by concatenating .deplibs
sections with the normal mergeable string section rules. As an example, #pragma
comment(lib, "foo") would result in:
.section ".deplibs","MS",@llvm_dependent_libraries,1
.asciz "foo"
For LTO, equivalent information to the contents of a the .deplibs section can be
retrieved by the LLD for bitcode input files.
LLD processes the dependent library specifiers in the following way:
1. Dependent libraries which are found from the specifiers in .deplibs sections
of relocatable object files are added when the linker decides to include that
file (which could itself be in a library) in the link. Dependent libraries
behave as if they were appended to the command line after all other options. As
a consequence the set of dependent libraries are searched last to resolve
symbols.
2. It is an error if a file cannot be found for a given specifier.
3. Any command line options in effect at the end of the command line parsing apply
to the dependent libraries, e.g. --whole-archive.
4. The linker tries to add a library or relocatable object file from each of the
strings in a .deplibs section by; first, handling the string as if it was
specified on the command line; second, by looking for the string in each of the
library search paths in turn; third, by looking for a lib<string>.a or
lib<string>.so (depending on the current mode of the linker) in each of the
library search paths.
5. A new command line option --no-dependent-libraries tells LLD to ignore the
dependent libraries.
Rationale for the above points:
1. Adding the dependent libraries last makes the process simple to understand
from a developers perspective. All linkers are able to implement this scheme.
2. Error-ing for libraries that are not found seems like better behavior than
failing the link during symbol resolution.
3. It seems useful for the user to be able to apply command line options which
will affect all of the dependent libraries. There is a potential problem of
surprise for developers, who might not realize that these options would apply
to these "invisible" input files; however, despite the potential for surprise,
this is easy for developers to reason about and gives developers the control
that they may require.
4. This algorithm takes into account all of the different ways that ELF linkers
find input files. The different search methods are tried by the linker in most
obvious to least obvious order.
5. I considered adding finer grained control over which dependent libraries were
ignored (e.g. MSVC has /nodefaultlib:<library>); however, I concluded that this
is not necessary: if finer control is required developers can fall back to using
the command line directly.
RFC thread: http://lists.llvm.org/pipermail/llvm-dev/2019-March/131004.html.
Differential Revision: https://reviews.llvm.org/D60274
llvm-svn: 360984
2019-05-17 11:44:15 +08:00
|
|
|
// This is for -l<namespec>.
|
2020-05-15 13:18:58 +08:00
|
|
|
Optional<std::string> elf::searchLibrary(StringRef name) {
|
2020-11-03 22:41:09 +08:00
|
|
|
llvm::TimeTraceScope timeScope("Locate library", name);
|
2019-10-07 16:31:18 +08:00
|
|
|
if (name.startswith(":"))
|
|
|
|
return findFromSearchPaths(name.substr(1));
|
|
|
|
return searchLibraryBaseName(name);
|
[ELF] Implement Dependent Libraries Feature
This patch implements a limited form of autolinking primarily designed to allow
either the --dependent-library compiler option, or "comment lib" pragmas (
https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=vs-2017) in
C/C++ e.g. #pragma comment(lib, "foo"), to cause an ELF linker to automatically
add the specified library to the link when processing the input file generated
by the compiler.
Currently this extension is unique to LLVM and LLD. However, care has been taken
to design this feature so that it could be supported by other ELF linkers.
The design goals were to provide:
- A simple linking model for developers to reason about.
- The ability to to override autolinking from the linker command line.
- Source code compatibility, where possible, with "comment lib" pragmas in other
environments (MSVC in particular).
Dependent library support is implemented differently for ELF platforms than on
the other platforms. Primarily this difference is that on ELF we pass the
dependent library specifiers directly to the linker without manipulating them.
This is in contrast to other platforms where they are mapped to a specific
linker option by the compiler. This difference is a result of the greater
variety of ELF linkers and the fact that ELF linkers tend to handle libraries in
a more complicated fashion than on other platforms. This forces us to defer
handling the specifiers to the linker.
In order to achieve a level of source code compatibility with other platforms
we have restricted this feature to work with libraries that meet the following
"reasonable" requirements:
1. There are no competing defined symbols in a given set of libraries, or
if they exist, the program owner doesn't care which is linked to their
program.
2. There may be circular dependencies between libraries.
The binary representation is a mergeable string section (SHF_MERGE,
SHF_STRINGS), called .deplibs, with custom type SHT_LLVM_DEPENDENT_LIBRARIES
(0x6fff4c04). The compiler forms this section by concatenating the arguments of
the "comment lib" pragmas and --dependent-library options in the order they are
encountered. Partial (-r, -Ur) links are handled by concatenating .deplibs
sections with the normal mergeable string section rules. As an example, #pragma
comment(lib, "foo") would result in:
.section ".deplibs","MS",@llvm_dependent_libraries,1
.asciz "foo"
For LTO, equivalent information to the contents of a the .deplibs section can be
retrieved by the LLD for bitcode input files.
LLD processes the dependent library specifiers in the following way:
1. Dependent libraries which are found from the specifiers in .deplibs sections
of relocatable object files are added when the linker decides to include that
file (which could itself be in a library) in the link. Dependent libraries
behave as if they were appended to the command line after all other options. As
a consequence the set of dependent libraries are searched last to resolve
symbols.
2. It is an error if a file cannot be found for a given specifier.
3. Any command line options in effect at the end of the command line parsing apply
to the dependent libraries, e.g. --whole-archive.
4. The linker tries to add a library or relocatable object file from each of the
strings in a .deplibs section by; first, handling the string as if it was
specified on the command line; second, by looking for the string in each of the
library search paths in turn; third, by looking for a lib<string>.a or
lib<string>.so (depending on the current mode of the linker) in each of the
library search paths.
5. A new command line option --no-dependent-libraries tells LLD to ignore the
dependent libraries.
Rationale for the above points:
1. Adding the dependent libraries last makes the process simple to understand
from a developers perspective. All linkers are able to implement this scheme.
2. Error-ing for libraries that are not found seems like better behavior than
failing the link during symbol resolution.
3. It seems useful for the user to be able to apply command line options which
will affect all of the dependent libraries. There is a potential problem of
surprise for developers, who might not realize that these options would apply
to these "invisible" input files; however, despite the potential for surprise,
this is easy for developers to reason about and gives developers the control
that they may require.
4. This algorithm takes into account all of the different ways that ELF linkers
find input files. The different search methods are tried by the linker in most
obvious to least obvious order.
5. I considered adding finer grained control over which dependent libraries were
ignored (e.g. MSVC has /nodefaultlib:<library>); however, I concluded that this
is not necessary: if finer control is required developers can fall back to using
the command line directly.
RFC thread: http://lists.llvm.org/pipermail/llvm-dev/2019-March/131004.html.
Differential Revision: https://reviews.llvm.org/D60274
llvm-svn: 360984
2019-05-17 11:44:15 +08:00
|
|
|
}
|
|
|
|
|
2018-07-26 05:53:18 +08:00
|
|
|
// If a linker/version script doesn't exist in the current directory, we also
|
|
|
|
// look for the script in the '-L' search paths. This matches the behaviour of
|
|
|
|
// '-T', --version-script=, and linker script INPUT() command in ld.bfd.
|
2020-05-15 13:18:58 +08:00
|
|
|
Optional<std::string> elf::searchScript(StringRef name) {
|
2017-11-20 23:43:20 +08:00
|
|
|
if (fs::exists(name))
|
|
|
|
return name.str();
|
|
|
|
return findFromSearchPaths(name);
|
|
|
|
}
|