2013-11-22 07:54:13 +08:00
|
|
|
//===- lib/Driver/WinLinkInputGraph.cpp -----------------------------------===//
|
2013-11-21 07:51:41 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lld/Driver/WinLinkInputGraph.h"
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
|
2014-04-01 14:11:09 +08:00
|
|
|
bool isCOFFLibraryFileExtension(StringRef path) {
|
|
|
|
return path.endswith_lower(".lib") || path.endswith_lower(".imp");
|
|
|
|
}
|
|
|
|
|
2013-11-21 07:51:41 +08:00
|
|
|
/// \brief Parse the input file to lld::File.
|
2014-06-12 22:53:47 +08:00
|
|
|
std::error_code PECOFFFileNode::parse(const LinkingContext &ctx,
|
|
|
|
raw_ostream &diagnostics) {
|
2014-04-01 14:18:19 +08:00
|
|
|
if (_parsed)
|
2014-06-12 22:53:47 +08:00
|
|
|
return std::error_code();
|
2014-04-01 14:18:19 +08:00
|
|
|
_parsed = true;
|
2013-11-21 07:51:41 +08:00
|
|
|
ErrorOr<StringRef> filePath = getPath(ctx);
|
2014-06-12 22:53:47 +08:00
|
|
|
if (std::error_code ec = filePath.getError()) {
|
2013-12-03 08:57:19 +08:00
|
|
|
diagnostics << "File not found: " << _path << "\n";
|
2014-01-09 06:00:09 +08:00
|
|
|
return ec;
|
2013-12-03 08:57:19 +08:00
|
|
|
}
|
2013-11-21 07:51:41 +08:00
|
|
|
|
2014-06-12 22:53:47 +08:00
|
|
|
if (std::error_code ec = getBuffer(*filePath)) {
|
2013-12-03 08:57:19 +08:00
|
|
|
diagnostics << "Cannot open file: " << *filePath << "\n";
|
2013-11-21 07:51:41 +08:00
|
|
|
return ec;
|
2013-12-03 08:57:19 +08:00
|
|
|
}
|
2013-11-21 07:51:41 +08:00
|
|
|
|
|
|
|
if (ctx.logInputFiles())
|
|
|
|
diagnostics << *filePath << "\n";
|
|
|
|
|
[lld] Introduce registry and Reference kind tuple
The main changes are in:
include/lld/Core/Reference.h
include/lld/ReaderWriter/Reader.h
Everything else is details to support the main change.
1) Registration based Readers
Previously, lld had a tangled interdependency with all the Readers. It would
have been impossible to make a streamlined linker (say for a JIT) which
just supported one file format and one architecture (no yaml, no archives, etc).
The old model also required a LinkingContext to read an object file, which
would have made .o inspection tools awkward.
The new model is that there is a global Registry object. You programmatically
register the Readers you want with the registry object. Whenever you need to
read/parse a file, you ask the registry to do it, and the registry tries each
registered reader.
For ease of use with the existing lld code base, there is one Registry
object inside the LinkingContext object.
2) Changing kind value to be a tuple
Beside Readers, the registry also keeps track of the mapping for Reference
Kind values to and from strings. Along with that, this patch also fixes
an ambiguity with the previous Reference::Kind values. The problem was that
we wanted to reuse existing relocation type values as Reference::Kind values.
But then how can the YAML write know how to convert a value to a string? The
fix is to change the 32-bit Reference::Kind into a tuple with an 8-bit namespace
(e.g. ELF, COFFF, etc), an 8-bit architecture (e.g. x86_64, PowerPC, etc), and
a 16-bit value. This tuple system allows conversion to and from strings with
no ambiguities.
llvm-svn: 197727
2013-12-20 05:58:00 +08:00
|
|
|
return ctx.registry().parseFile(_buffer, _files);
|
2013-11-21 07:51:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<File &> PECOFFFileNode::getNextFile() {
|
|
|
|
if (_nextFileIndex == _files.size())
|
|
|
|
return make_error_code(InputGraphError::no_more_files);
|
|
|
|
return *_files[_nextFileIndex++];
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<StringRef> PECOFFFileNode::getPath(const LinkingContext &) const {
|
2014-04-01 14:11:09 +08:00
|
|
|
if (isCOFFLibraryFileExtension(_path))
|
2013-11-21 07:51:41 +08:00
|
|
|
return _ctx.searchLibraryFile(_path);
|
|
|
|
if (llvm::sys::path::extension(_path).empty())
|
2013-11-21 08:17:31 +08:00
|
|
|
return _ctx.allocate(_path.str() + ".obj");
|
2013-11-21 07:51:41 +08:00
|
|
|
return _path;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<StringRef> PECOFFLibraryNode::getPath(const LinkingContext &) const {
|
2014-04-01 14:11:09 +08:00
|
|
|
if (isCOFFLibraryFileExtension(_path))
|
2013-11-21 07:54:52 +08:00
|
|
|
return _ctx.searchLibraryFile(_path);
|
2013-11-21 08:17:31 +08:00
|
|
|
return _ctx.searchLibraryFile(_ctx.allocate(_path.str() + ".lib"));
|
2013-11-21 07:51:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end anonymous namespace
|