2013-11-25 06:29:19 +08:00
|
|
|
//===- lib/Driver/GnuLdInputGraph.cpp -------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lld/Driver/GnuLdInputGraph.h"
|
|
|
|
#include "lld/ReaderWriter/LinkerScript.h"
|
|
|
|
|
[ELF] Fix the file look up algorithm used in the linker script GROUP command.
In general the linker scripts's GROUP command works like a pair
of command line options --start-group/--end-group. But there is
a difference in the files look up algorithm.
The --start-group/--end-group commands use a trivial approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) Otherwise, use the path 'as-is'.
The GROUP command implements more compicated approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) If the path does not have '-l' prefix, and sysroot is configured,
and the path starts with the / character, and the script being
processed is located inside the sysroot, search the path under
the sysroot. Otherwise, try to open the path in the current
directory. If it is not found, search through library search
directories.
https://www.sourceware.org/binutils/docs-2.24/ld/File-Commands.html
The patch reviewed by Shankar Easwaran, Rui Ueyama.
llvm-svn: 207769
2014-05-02 00:22:08 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
2013-11-25 06:29:19 +08:00
|
|
|
using namespace lld;
|
|
|
|
|
|
|
|
/// \brief Parse the input file to lld::File.
|
2014-06-12 22:53:47 +08:00
|
|
|
std::error_code ELFFileNode::parse(const LinkingContext &ctx,
|
|
|
|
raw_ostream &diagnostics) {
|
2013-11-25 06:29:19 +08:00
|
|
|
ErrorOr<StringRef> filePath = getPath(ctx);
|
2014-06-12 22:53:47 +08:00
|
|
|
if (std::error_code ec = filePath.getError())
|
2014-01-09 06:00:09 +08:00
|
|
|
return ec;
|
2014-06-12 22:53:47 +08:00
|
|
|
if (std::error_code ec = getBuffer(*filePath))
|
2013-11-25 06:29:19 +08:00
|
|
|
return ec;
|
|
|
|
if (ctx.logInputFiles())
|
|
|
|
diagnostics << *filePath << "\n";
|
|
|
|
|
2014-04-02 11:57:39 +08:00
|
|
|
if (_attributes._isWholeArchive) {
|
[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
|
|
|
std::vector<std::unique_ptr<File>> parsedFiles;
|
2014-06-12 22:53:47 +08:00
|
|
|
if (std::error_code ec = ctx.registry().parseFile(_buffer, parsedFiles))
|
[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 ec;
|
|
|
|
assert(parsedFiles.size() == 1);
|
|
|
|
std::unique_ptr<File> f(parsedFiles[0].release());
|
2014-06-10 21:43:13 +08:00
|
|
|
if (const auto *archive = dyn_cast<ArchiveLibraryFile>(f.get())) {
|
[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
|
|
|
// Have this node own the FileArchive object.
|
|
|
|
_archiveFile.reset(archive);
|
|
|
|
f.release();
|
|
|
|
// Add all members to _files vector
|
|
|
|
return archive->parseAllMembers(_files);
|
2013-11-25 06:29:19 +08:00
|
|
|
}
|
2014-04-08 05:01:11 +08:00
|
|
|
// if --whole-archive is around non-archive, just use it as normal.
|
|
|
|
_files.push_back(std::move(f));
|
2014-06-12 22:53:47 +08:00
|
|
|
return std::error_code();
|
2013-11-25 06:29:19 +08:00
|
|
|
}
|
[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-25 07:12:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Parse the GnuLD Script
|
2014-06-12 22:53:47 +08:00
|
|
|
std::error_code GNULdScript::parse(const LinkingContext &ctx,
|
|
|
|
raw_ostream &diagnostics) {
|
2013-11-25 07:12:36 +08:00
|
|
|
ErrorOr<StringRef> filePath = getPath(ctx);
|
2014-06-12 22:53:47 +08:00
|
|
|
if (std::error_code ec = filePath.getError())
|
2014-01-09 06:00:09 +08:00
|
|
|
return ec;
|
2014-06-12 22:53:47 +08:00
|
|
|
if (std::error_code ec = getBuffer(*filePath))
|
2013-11-25 07:12:36 +08:00
|
|
|
return ec;
|
|
|
|
|
|
|
|
if (ctx.logInputFiles())
|
|
|
|
diagnostics << *filePath << "\n";
|
|
|
|
|
|
|
|
_lexer.reset(new script::Lexer(std::move(_buffer)));
|
|
|
|
_parser.reset(new script::Parser(*_lexer.get()));
|
|
|
|
|
|
|
|
_linkerScript = _parser->parse();
|
|
|
|
|
|
|
|
if (!_linkerScript)
|
|
|
|
return LinkerScriptReaderError::parse_error;
|
|
|
|
|
2014-06-12 22:53:47 +08:00
|
|
|
return std::error_code();
|
2013-11-25 07:12:36 +08:00
|
|
|
}
|
|
|
|
|
[ELF] Fix the file look up algorithm used in the linker script GROUP command.
In general the linker scripts's GROUP command works like a pair
of command line options --start-group/--end-group. But there is
a difference in the files look up algorithm.
The --start-group/--end-group commands use a trivial approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) Otherwise, use the path 'as-is'.
The GROUP command implements more compicated approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) If the path does not have '-l' prefix, and sysroot is configured,
and the path starts with the / character, and the script being
processed is located inside the sysroot, search the path under
the sysroot. Otherwise, try to open the path in the current
directory. If it is not found, search through library search
directories.
https://www.sourceware.org/binutils/docs-2.24/ld/File-Commands.html
The patch reviewed by Shankar Easwaran, Rui Ueyama.
llvm-svn: 207769
2014-05-02 00:22:08 +08:00
|
|
|
static bool isPathUnderSysroot(StringRef sysroot, StringRef path) {
|
2014-06-26 18:48:52 +08:00
|
|
|
if (sysroot.empty())
|
[ELF] Fix the file look up algorithm used in the linker script GROUP command.
In general the linker scripts's GROUP command works like a pair
of command line options --start-group/--end-group. But there is
a difference in the files look up algorithm.
The --start-group/--end-group commands use a trivial approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) Otherwise, use the path 'as-is'.
The GROUP command implements more compicated approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) If the path does not have '-l' prefix, and sysroot is configured,
and the path starts with the / character, and the script being
processed is located inside the sysroot, search the path under
the sysroot. Otherwise, try to open the path in the current
directory. If it is not found, search through library search
directories.
https://www.sourceware.org/binutils/docs-2.24/ld/File-Commands.html
The patch reviewed by Shankar Easwaran, Rui Ueyama.
llvm-svn: 207769
2014-05-02 00:22:08 +08:00
|
|
|
return false;
|
|
|
|
|
2014-06-26 18:48:52 +08:00
|
|
|
while (!path.empty() && !llvm::sys::fs::equivalent(sysroot, path))
|
|
|
|
path = llvm::sys::path::parent_path(path);
|
|
|
|
|
|
|
|
return !path.empty();
|
[ELF] Fix the file look up algorithm used in the linker script GROUP command.
In general the linker scripts's GROUP command works like a pair
of command line options --start-group/--end-group. But there is
a difference in the files look up algorithm.
The --start-group/--end-group commands use a trivial approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) Otherwise, use the path 'as-is'.
The GROUP command implements more compicated approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) If the path does not have '-l' prefix, and sysroot is configured,
and the path starts with the / character, and the script being
processed is located inside the sysroot, search the path under
the sysroot. Otherwise, try to open the path in the current
directory. If it is not found, search through library search
directories.
https://www.sourceware.org/binutils/docs-2.24/ld/File-Commands.html
The patch reviewed by Shankar Easwaran, Rui Ueyama.
llvm-svn: 207769
2014-05-02 00:22:08 +08:00
|
|
|
}
|
|
|
|
|
2013-11-25 07:12:36 +08:00
|
|
|
/// \brief Handle GnuLD script for ELF.
|
2014-06-12 22:53:47 +08:00
|
|
|
std::error_code ELFGNULdScript::parse(const LinkingContext &ctx,
|
|
|
|
raw_ostream &diagnostics) {
|
2014-04-02 11:57:39 +08:00
|
|
|
ELFFileNode::Attributes attributes;
|
2014-06-12 22:53:47 +08:00
|
|
|
if (std::error_code ec = GNULdScript::parse(ctx, diagnostics))
|
2013-11-25 07:12:36 +08:00
|
|
|
return ec;
|
[ELF] Fix the file look up algorithm used in the linker script GROUP command.
In general the linker scripts's GROUP command works like a pair
of command line options --start-group/--end-group. But there is
a difference in the files look up algorithm.
The --start-group/--end-group commands use a trivial approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) Otherwise, use the path 'as-is'.
The GROUP command implements more compicated approach:
a) If the path has '-l' prefix, add 'lib' prefix and '.a'/'.so'
suffix and search the path through library search directories.
b) If the path does not have '-l' prefix, and sysroot is configured,
and the path starts with the / character, and the script being
processed is located inside the sysroot, search the path under
the sysroot. Otherwise, try to open the path in the current
directory. If it is not found, search through library search
directories.
https://www.sourceware.org/binutils/docs-2.24/ld/File-Commands.html
The patch reviewed by Shankar Easwaran, Rui Ueyama.
llvm-svn: 207769
2014-05-02 00:22:08 +08:00
|
|
|
StringRef sysRoot = _elfLinkingContext.getSysroot();
|
|
|
|
if (!sysRoot.empty() && isPathUnderSysroot(sysRoot, *getPath(ctx)))
|
|
|
|
attributes.setSysRooted(true);
|
2014-04-04 04:47:50 +08:00
|
|
|
for (const script::Command *c : _linkerScript->_commands) {
|
2014-04-08 05:13:33 +08:00
|
|
|
auto *group = dyn_cast<script::Group>(c);
|
|
|
|
if (!group)
|
|
|
|
continue;
|
|
|
|
std::unique_ptr<Group> groupStart(new Group());
|
|
|
|
for (const script::Path &path : group->getPaths()) {
|
|
|
|
// TODO : Propagate Set WholeArchive/dashlPrefix
|
|
|
|
attributes.setAsNeeded(path._asNeeded);
|
|
|
|
auto inputNode = new ELFFileNode(
|
|
|
|
_elfLinkingContext, _elfLinkingContext.allocateString(path._path),
|
|
|
|
attributes);
|
|
|
|
std::unique_ptr<InputElement> inputFile(inputNode);
|
2014-05-01 03:03:45 +08:00
|
|
|
groupStart.get()->addFile(std::move(inputFile));
|
2013-11-25 07:12:36 +08:00
|
|
|
}
|
2014-04-08 05:13:33 +08:00
|
|
|
_expandElements.push_back(std::move(groupStart));
|
2013-11-25 07:12:36 +08:00
|
|
|
}
|
2014-06-12 22:53:47 +08:00
|
|
|
return std::error_code();
|
2013-11-25 06:29:19 +08:00
|
|
|
}
|