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"
|
2013-05-29 02:37:39 +08:00
|
|
|
#include "lld/Core/Resolver.h"
|
2015-01-15 15:05:46 +08:00
|
|
|
#include "lld/Driver/Driver.h"
|
2013-10-29 13:12:14 +08:00
|
|
|
#include "lld/Passes/RoundTripNativePass.h"
|
|
|
|
#include "lld/Passes/RoundTripYAMLPass.h"
|
2014-10-18 13:33:55 +08:00
|
|
|
#include "lld/ReaderWriter/Reader.h"
|
|
|
|
#include "lld/ReaderWriter/Writer.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"
|
|
|
|
#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);
|
|
|
|
std::vector<std::unique_ptr<File>> files;
|
2015-01-15 12:34:31 +08:00
|
|
|
if (std::error_code ec = ctx.registry().loadFile(std::move(mb.get()), files))
|
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);
|
|
|
|
if (wholeArchive)
|
|
|
|
return parseMemberFiles(files);
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2013-04-05 02:59:24 +08:00
|
|
|
/// This is where the link is actually performed.
|
2013-10-08 23:43:48 +08:00
|
|
|
bool Driver::link(LinkingContext &context, raw_ostream &diagnostics) {
|
2013-04-05 02:59:24 +08:00
|
|
|
// Honor -mllvm
|
2013-08-07 06:31:59 +08:00
|
|
|
if (!context.llvmOptions().empty()) {
|
|
|
|
unsigned numArgs = context.llvmOptions().size();
|
|
|
|
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)
|
2013-08-07 06:31:59 +08:00
|
|
|
args[i + 1] = context.llvmOptions()[i];
|
2013-04-05 02:59:24 +08:00
|
|
|
args[numArgs + 1] = 0;
|
|
|
|
llvm::cl::ParseCommandLineOptions(numArgs + 1, args);
|
|
|
|
}
|
2015-01-15 16:46:36 +08:00
|
|
|
if (context.getNodes().empty())
|
2013-09-25 07:26:34 +08:00
|
|
|
return false;
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2015-01-16 23:54:13 +08:00
|
|
|
for (std::unique_ptr<Node> &ie : context.getNodes())
|
|
|
|
if (FileNode *node = dyn_cast<FileNode>(ie.get()))
|
|
|
|
context.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;
|
2013-10-07 10:47:09 +08:00
|
|
|
context.createInternalFiles(internalFiles);
|
2014-12-10 08:33:00 +08:00
|
|
|
for (auto i = internalFiles.rbegin(), e = internalFiles.rend(); i != e; ++i) {
|
2015-01-15 16:46:36 +08:00
|
|
|
auto &members = context.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;
|
2013-10-07 10:47:09 +08:00
|
|
|
context.createImplicitFiles(implicitFiles);
|
2014-12-10 08:33:00 +08:00
|
|
|
for (auto i = implicitFiles.rbegin(), e = implicitFiles.rend(); i != e; ++i) {
|
2015-01-15 16:46:36 +08:00
|
|
|
auto &members = context.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
|
|
|
}
|
|
|
|
|
|
|
|
// Give target a chance to sort the input files.
|
|
|
|
// Mach-O uses this chance to move all object files before library files.
|
|
|
|
context.maybeSortInputFiles();
|
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");
|
2013-10-07 10:47:09 +08:00
|
|
|
Resolver resolver(context);
|
2013-10-11 14:16:35 +08:00
|
|
|
if (!resolver.resolve())
|
|
|
|
return false;
|
2013-10-29 13:12:14 +08:00
|
|
|
std::unique_ptr<MutableFile> 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;
|
2013-08-07 06:31:59 +08:00
|
|
|
context.addPasses(pm);
|
2013-10-29 13:12:14 +08:00
|
|
|
|
2013-11-02 05:05:42 +08:00
|
|
|
#ifndef NDEBUG
|
2014-12-01 09:04:11 +08:00
|
|
|
if (context.runRoundTripPass()) {
|
2014-03-20 10:49:33 +08:00
|
|
|
pm.add(std::unique_ptr<Pass>(new RoundTripYAMLPass(context)));
|
|
|
|
pm.add(std::unique_ptr<Pass>(new RoundTripNativePass(context)));
|
|
|
|
}
|
2013-10-29 13:12:14 +08:00
|
|
|
#endif
|
|
|
|
|
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");
|
2014-06-12 22:53:47 +08:00
|
|
|
if (std::error_code ec = context.writeFile(*merged)) {
|
2013-08-07 06:31:59 +08:00
|
|
|
diagnostics << "Failed to write file '" << context.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
|