2012-12-08 08:47:36 +08:00
|
|
|
//===- lib/Driver/Driver.cpp - Linker Driver Emulator ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
Convert CoreInputGraph.
This is a part of InputGraph cleanup to represent input files as a flat
list of Files (and some meta-nodes for group etc.)
We cannot achieve that goal in one gigantic patch, so I split the task
into small steps as shown below.
(Recap the progress so far: Currently InputGraph contains a list of
InputElements. Each InputElement contain one File (that used to have
multiple Files, but I eliminated that use case in r223867). Files are
currently instantiated in Driver::link(), but I already made a change
to separate file parsing from object instantiation (r224102), so we
can safely instantiate Files when we need them, instead of wrapping
a file with the wrapper class (FileNode class). InputGraph used to
act like a generator class by interpreting groups by itself, but it's
now just a container of a list of InputElements (r223867).)
1. Instantiate Files in the driver and wrap them with WrapperNode.
WrapperNode is a temporary class that allows us to instantiate Files
in the driver while keep using the current InputGraph data structure.
This patch demonstrates how this step 1 looks like, using Core driver
as an example.
2. Do the same thing for the other drivers.
When step 2 is done, an InputGraph consists of GroupEnd objects or
WrapperNodes each of which contains one File. Other types of
FileNode subclasses are removed.
3. Replace InputGraph with std::vector<std::unique_ptr<InputElement>>.
InputGraph is already just a container of list of InputElements,
so this step removes that needless class.
4. Remove WrapperNode.
We need some code cleanup between each step, because many classes
do a bit odd things (e.g. InputGraph::getGroupSize()). I'll straight
things up as I need to.
llvm-svn: 225313
2015-01-07 07:06:49 +08:00
|
|
|
#include "lld/Core/ArchiveLibraryFile.h"
|
|
|
|
#include "lld/Core/File.h"
|
2013-05-29 02:55:39 +08:00
|
|
|
#include "lld/Core/Instrumentation.h"
|
2014-10-18 13:33:55 +08:00
|
|
|
#include "lld/Core/LLVM.h"
|
2013-05-29 02:37:39 +08:00
|
|
|
#include "lld/Core/Parallel.h"
|
2014-10-18 13:33:55 +08:00
|
|
|
#include "lld/Core/PassManager.h"
|
2015-01-22 06:54:56 +08:00
|
|
|
#include "lld/Core/Reader.h"
|
2013-05-29 02:37:39 +08:00
|
|
|
#include "lld/Core/Resolver.h"
|
2015-01-22 06:54:56 +08:00
|
|
|
#include "lld/Core/Writer.h"
|
2015-01-15 15:05:46 +08:00
|
|
|
#include "lld/Driver/Driver.h"
|
2015-03-02 08:48:06 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-04-05 02:59:24 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2015-02-06 12:15:02 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2013-04-05 02:59:24 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-10-09 07:01:52 +08:00
|
|
|
#include <mutex>
|
|
|
|
|
2013-04-05 02:59:24 +08:00
|
|
|
namespace lld {
|
|
|
|
|
Convert CoreInputGraph.
This is a part of InputGraph cleanup to represent input files as a flat
list of Files (and some meta-nodes for group etc.)
We cannot achieve that goal in one gigantic patch, so I split the task
into small steps as shown below.
(Recap the progress so far: Currently InputGraph contains a list of
InputElements. Each InputElement contain one File (that used to have
multiple Files, but I eliminated that use case in r223867). Files are
currently instantiated in Driver::link(), but I already made a change
to separate file parsing from object instantiation (r224102), so we
can safely instantiate Files when we need them, instead of wrapping
a file with the wrapper class (FileNode class). InputGraph used to
act like a generator class by interpreting groups by itself, but it's
now just a container of a list of InputElements (r223867).)
1. Instantiate Files in the driver and wrap them with WrapperNode.
WrapperNode is a temporary class that allows us to instantiate Files
in the driver while keep using the current InputGraph data structure.
This patch demonstrates how this step 1 looks like, using Core driver
as an example.
2. Do the same thing for the other drivers.
When step 2 is done, an InputGraph consists of GroupEnd objects or
WrapperNodes each of which contains one File. Other types of
FileNode subclasses are removed.
3. Replace InputGraph with std::vector<std::unique_ptr<InputElement>>.
InputGraph is already just a container of list of InputElements,
so this step removes that needless class.
4. Remove WrapperNode.
We need some code cleanup between each step, because many classes
do a bit odd things (e.g. InputGraph::getGroupSize()). I'll straight
things up as I need to.
llvm-svn: 225313
2015-01-07 07:06:49 +08:00
|
|
|
FileVector makeErrorFile(StringRef path, std::error_code ec) {
|
|
|
|
std::vector<std::unique_ptr<File>> result;
|
|
|
|
result.push_back(llvm::make_unique<ErrorFile>(path, ec));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileVector parseMemberFiles(FileVector &files) {
|
|
|
|
std::vector<std::unique_ptr<File>> members;
|
|
|
|
for (std::unique_ptr<File> &file : files) {
|
|
|
|
if (auto *archive = dyn_cast<ArchiveLibraryFile>(file.get())) {
|
|
|
|
if (std::error_code ec = archive->parseAllMembers(members))
|
|
|
|
return makeErrorFile(file->path(), ec);
|
|
|
|
} else {
|
|
|
|
members.push_back(std::move(file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return members;
|
|
|
|
}
|
|
|
|
|
2015-01-15 12:34:31 +08:00
|
|
|
FileVector loadFile(LinkingContext &ctx, StringRef path, bool wholeArchive) {
|
Convert CoreInputGraph.
This is a part of InputGraph cleanup to represent input files as a flat
list of Files (and some meta-nodes for group etc.)
We cannot achieve that goal in one gigantic patch, so I split the task
into small steps as shown below.
(Recap the progress so far: Currently InputGraph contains a list of
InputElements. Each InputElement contain one File (that used to have
multiple Files, but I eliminated that use case in r223867). Files are
currently instantiated in Driver::link(), but I already made a change
to separate file parsing from object instantiation (r224102), so we
can safely instantiate Files when we need them, instead of wrapping
a file with the wrapper class (FileNode class). InputGraph used to
act like a generator class by interpreting groups by itself, but it's
now just a container of a list of InputElements (r223867).)
1. Instantiate Files in the driver and wrap them with WrapperNode.
WrapperNode is a temporary class that allows us to instantiate Files
in the driver while keep using the current InputGraph data structure.
This patch demonstrates how this step 1 looks like, using Core driver
as an example.
2. Do the same thing for the other drivers.
When step 2 is done, an InputGraph consists of GroupEnd objects or
WrapperNodes each of which contains one File. Other types of
FileNode subclasses are removed.
3. Replace InputGraph with std::vector<std::unique_ptr<InputElement>>.
InputGraph is already just a container of list of InputElements,
so this step removes that needless class.
4. Remove WrapperNode.
We need some code cleanup between each step, because many classes
do a bit odd things (e.g. InputGraph::getGroupSize()). I'll straight
things up as I need to.
llvm-svn: 225313
2015-01-07 07:06:49 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> mb
|
|
|
|
= MemoryBuffer::getFileOrSTDIN(path);
|
|
|
|
if (std::error_code ec = mb.getError())
|
|
|
|
return makeErrorFile(path, ec);
|
2015-04-25 02:51:30 +08:00
|
|
|
ErrorOr<std::unique_ptr<File>> fileOrErr =
|
|
|
|
ctx.registry().loadFile(std::move(mb.get()));
|
|
|
|
if (std::error_code ec = fileOrErr.getError())
|
Convert CoreInputGraph.
This is a part of InputGraph cleanup to represent input files as a flat
list of Files (and some meta-nodes for group etc.)
We cannot achieve that goal in one gigantic patch, so I split the task
into small steps as shown below.
(Recap the progress so far: Currently InputGraph contains a list of
InputElements. Each InputElement contain one File (that used to have
multiple Files, but I eliminated that use case in r223867). Files are
currently instantiated in Driver::link(), but I already made a change
to separate file parsing from object instantiation (r224102), so we
can safely instantiate Files when we need them, instead of wrapping
a file with the wrapper class (FileNode class). InputGraph used to
act like a generator class by interpreting groups by itself, but it's
now just a container of a list of InputElements (r223867).)
1. Instantiate Files in the driver and wrap them with WrapperNode.
WrapperNode is a temporary class that allows us to instantiate Files
in the driver while keep using the current InputGraph data structure.
This patch demonstrates how this step 1 looks like, using Core driver
as an example.
2. Do the same thing for the other drivers.
When step 2 is done, an InputGraph consists of GroupEnd objects or
WrapperNodes each of which contains one File. Other types of
FileNode subclasses are removed.
3. Replace InputGraph with std::vector<std::unique_ptr<InputElement>>.
InputGraph is already just a container of list of InputElements,
so this step removes that needless class.
4. Remove WrapperNode.
We need some code cleanup between each step, because many classes
do a bit odd things (e.g. InputGraph::getGroupSize()). I'll straight
things up as I need to.
llvm-svn: 225313
2015-01-07 07:06:49 +08:00
|
|
|
return makeErrorFile(path, ec);
|
2015-04-24 23:51:45 +08:00
|
|
|
std::vector<std::unique_ptr<File>> files;
|
2015-04-25 02:51:30 +08:00
|
|
|
files.push_back(std::move(fileOrErr.get()));
|
Convert CoreInputGraph.
This is a part of InputGraph cleanup to represent input files as a flat
list of Files (and some meta-nodes for group etc.)
We cannot achieve that goal in one gigantic patch, so I split the task
into small steps as shown below.
(Recap the progress so far: Currently InputGraph contains a list of
InputElements. Each InputElement contain one File (that used to have
multiple Files, but I eliminated that use case in r223867). Files are
currently instantiated in Driver::link(), but I already made a change
to separate file parsing from object instantiation (r224102), so we
can safely instantiate Files when we need them, instead of wrapping
a file with the wrapper class (FileNode class). InputGraph used to
act like a generator class by interpreting groups by itself, but it's
now just a container of a list of InputElements (r223867).)
1. Instantiate Files in the driver and wrap them with WrapperNode.
WrapperNode is a temporary class that allows us to instantiate Files
in the driver while keep using the current InputGraph data structure.
This patch demonstrates how this step 1 looks like, using Core driver
as an example.
2. Do the same thing for the other drivers.
When step 2 is done, an InputGraph consists of GroupEnd objects or
WrapperNodes each of which contains one File. Other types of
FileNode subclasses are removed.
3. Replace InputGraph with std::vector<std::unique_ptr<InputElement>>.
InputGraph is already just a container of list of InputElements,
so this step removes that needless class.
4. Remove WrapperNode.
We need some code cleanup between each step, because many classes
do a bit odd things (e.g. InputGraph::getGroupSize()). I'll straight
things up as I need to.
llvm-svn: 225313
2015-01-07 07:06:49 +08:00
|
|
|
if (wholeArchive)
|
|
|
|
return parseMemberFiles(files);
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2013-04-05 02:59:24 +08:00
|
|
|
/// This is where the link is actually performed.
|
2015-04-04 09:37:25 +08:00
|
|
|
bool Driver::link(LinkingContext &ctx, raw_ostream &diagnostics) {
|
2013-04-05 02:59:24 +08:00
|
|
|
// Honor -mllvm
|
2015-04-04 09:37:25 +08:00
|
|
|
if (!ctx.llvmOptions().empty()) {
|
|
|
|
unsigned numArgs = ctx.llvmOptions().size();
|
2013-08-07 06:31:59 +08:00
|
|
|
const char **args = new const char *[numArgs + 2];
|
2013-04-05 02:59:24 +08:00
|
|
|
args[0] = "lld (LLVM option parsing)";
|
|
|
|
for (unsigned i = 0; i != numArgs; ++i)
|
2015-04-04 09:37:25 +08:00
|
|
|
args[i + 1] = ctx.llvmOptions()[i];
|
2013-04-05 02:59:24 +08:00
|
|
|
args[numArgs + 1] = 0;
|
|
|
|
llvm::cl::ParseCommandLineOptions(numArgs + 1, args);
|
|
|
|
}
|
2015-04-04 09:37:25 +08:00
|
|
|
if (ctx.getNodes().empty())
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2015-04-04 09:37:25 +08:00
|
|
|
for (std::unique_ptr<Node> &ie : ctx.getNodes())
|
2015-03-05 08:07:38 +08:00
|
|
|
if (FileNode *node = dyn_cast<FileNode>(ie.get()))
|
2015-04-04 09:37:25 +08:00
|
|
|
ctx.getTaskGroup().spawn([node] { node->getFile()->parse(); });
|
2013-05-29 02:37:39 +08:00
|
|
|
|
2015-01-15 15:56:14 +08:00
|
|
|
std::vector<std::unique_ptr<File>> internalFiles;
|
2015-04-04 09:37:25 +08:00
|
|
|
ctx.createInternalFiles(internalFiles);
|
2014-12-10 08:33:00 +08:00
|
|
|
for (auto i = internalFiles.rbegin(), e = internalFiles.rend(); i != e; ++i) {
|
2015-04-04 09:37:25 +08:00
|
|
|
auto &members = ctx.getNodes();
|
2015-01-15 16:18:14 +08:00
|
|
|
members.insert(members.begin(), llvm::make_unique<FileNode>(std::move(*i)));
|
2014-12-10 08:33:00 +08:00
|
|
|
}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
// Give target a chance to add files.
|
2015-01-15 15:56:14 +08:00
|
|
|
std::vector<std::unique_ptr<File>> implicitFiles;
|
2015-04-04 09:37:25 +08:00
|
|
|
ctx.createImplicitFiles(implicitFiles);
|
2014-12-10 08:33:00 +08:00
|
|
|
for (auto i = implicitFiles.rbegin(), e = implicitFiles.rend(); i != e; ++i) {
|
2015-04-04 09:37:25 +08:00
|
|
|
auto &members = ctx.getNodes();
|
2015-01-15 16:18:14 +08:00
|
|
|
members.insert(members.begin(), llvm::make_unique<FileNode>(std::move(*i)));
|
2014-12-10 08:33:00 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 18:34:43 +08:00
|
|
|
// Give target a chance to postprocess input files.
|
2014-12-10 08:33:00 +08:00
|
|
|
// Mach-O uses this chance to move all object files before library files.
|
2015-03-14 18:34:43 +08:00
|
|
|
// ELF adds specific undefined symbols resolver.
|
2015-04-04 09:37:25 +08:00
|
|
|
ctx.finalizeInputFiles();
|
2013-10-07 10:47:09 +08:00
|
|
|
|
2013-04-05 02:59:24 +08:00
|
|
|
// Do core linking.
|
2013-05-29 02:55:39 +08:00
|
|
|
ScopedTask resolveTask(getDefaultDomain(), "Resolve");
|
2015-04-04 09:37:25 +08:00
|
|
|
Resolver resolver(ctx);
|
2015-04-04 08:10:14 +08:00
|
|
|
if (!resolver.resolve()) {
|
2015-04-04 09:37:25 +08:00
|
|
|
ctx.getTaskGroup().sync();
|
2013-10-11 14:16:35 +08:00
|
|
|
return false;
|
2015-04-04 08:10:14 +08:00
|
|
|
}
|
2015-04-08 04:43:38 +08:00
|
|
|
std::unique_ptr<SimpleFile> merged = resolver.resultFile();
|
2013-05-29 02:55:39 +08:00
|
|
|
resolveTask.end();
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
// Run passes on linked atoms.
|
2013-05-29 02:55:39 +08:00
|
|
|
ScopedTask passTask(getDefaultDomain(), "Passes");
|
2013-04-05 02:59:24 +08:00
|
|
|
PassManager pm;
|
2015-04-04 09:37:25 +08:00
|
|
|
ctx.addPasses(pm);
|
2013-04-05 02:59:24 +08:00
|
|
|
pm.runOnFile(merged);
|
2013-05-29 02:55:39 +08:00
|
|
|
passTask.end();
|
2013-04-05 02:59:24 +08:00
|
|
|
|
|
|
|
// Give linked atoms to Writer to generate output file.
|
2013-05-29 02:55:39 +08:00
|
|
|
ScopedTask writeTask(getDefaultDomain(), "Write");
|
2015-04-04 09:37:25 +08:00
|
|
|
if (std::error_code ec = ctx.writeFile(*merged)) {
|
|
|
|
diagnostics << "Failed to write file '" << ctx.outputPath()
|
2013-07-16 07:55:07 +08:00
|
|
|
<< "': " << ec.message() << "\n";
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
|
|
|
|
2013-09-25 07:26:34 +08:00
|
|
|
return true;
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|