2013-08-07 06:31:59 +08:00
|
|
|
//===- lib/Core/LinkingContext.cpp - Linker Context Object Interface ------===//
|
2013-01-22 10:15:30 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-06-12 05:47:51 +08:00
|
|
|
#include "lld/Core/Alias.h"
|
2013-08-07 06:31:59 +08:00
|
|
|
#include "lld/Core/LinkingContext.h"
|
2013-10-07 10:47:09 +08:00
|
|
|
#include "lld/Core/Resolver.h"
|
2014-06-12 05:47:51 +08:00
|
|
|
#include "lld/Core/Simple.h"
|
2014-05-23 05:37:56 +08:00
|
|
|
#include "lld/ReaderWriter/Writer.h"
|
2013-01-22 10:15:30 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2014-12-01 09:04:11 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2013-01-22 10:15:30 +08:00
|
|
|
|
|
|
|
namespace lld {
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2014-12-01 09:04:11 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
LinkingContext::LinkingContext()
|
|
|
|
: _deadStrip(false), _allowDuplicates(false),
|
|
|
|
_globalsAreDeadStripRoots(false),
|
|
|
|
_searchArchivesToOverrideTentativeDefinitions(false),
|
|
|
|
_searchSharedLibrariesToOverrideTentativeDefinitions(false),
|
|
|
|
_warnIfCoalesableAtomsHaveDifferentCanBeNull(false),
|
|
|
|
_warnIfCoalesableAtomsHaveDifferentLoadName(false),
|
|
|
|
_printRemainingUndefines(true), _allowRemainingUndefines(false),
|
|
|
|
_logInputFiles(false), _allowShlibUndefines(false),
|
|
|
|
_runRoundTripPasses(false), _outputFileType(OutputFileType::Default),
|
|
|
|
_nextOrdinal(0) {
|
|
|
|
llvm::Optional<std::string> env =
|
|
|
|
llvm::sys::Process::GetEnv("LLD_RUN_ROUNDTRIP_TEST");
|
|
|
|
if (env.hasValue() && !env.getValue().empty())
|
2014-12-02 04:28:54 +08:00
|
|
|
_runRoundTripPasses = true;
|
2014-12-01 09:04:11 +08:00
|
|
|
}
|
|
|
|
#else
|
2013-08-07 06:31:59 +08:00
|
|
|
LinkingContext::LinkingContext()
|
2014-03-29 00:26:38 +08:00
|
|
|
: _deadStrip(false), _allowDuplicates(false),
|
|
|
|
_globalsAreDeadStripRoots(false),
|
2013-04-11 10:56:30 +08:00
|
|
|
_searchArchivesToOverrideTentativeDefinitions(false),
|
|
|
|
_searchSharedLibrariesToOverrideTentativeDefinitions(false),
|
|
|
|
_warnIfCoalesableAtomsHaveDifferentCanBeNull(false),
|
|
|
|
_warnIfCoalesableAtomsHaveDifferentLoadName(false),
|
2013-10-07 10:47:09 +08:00
|
|
|
_printRemainingUndefines(true), _allowRemainingUndefines(false),
|
|
|
|
_logInputFiles(false), _allowShlibUndefines(false),
|
2014-04-02 13:03:40 +08:00
|
|
|
_outputFileType(OutputFileType::Default), _nextOrdinal(0) {}
|
2014-12-01 09:04:11 +08:00
|
|
|
#endif
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
LinkingContext::~LinkingContext() {}
|
2013-01-22 10:15:30 +08:00
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
bool LinkingContext::validate(raw_ostream &diagnostics) {
|
2013-06-11 20:36:05 +08:00
|
|
|
return validateImpl(diagnostics);
|
|
|
|
}
|
|
|
|
|
2014-06-12 22:53:47 +08:00
|
|
|
std::error_code LinkingContext::writeFile(const File &linkedFile) const {
|
2013-08-07 06:31:59 +08:00
|
|
|
return this->writer().writeFile(linkedFile, _outputPath);
|
2013-01-22 10:15:30 +08:00
|
|
|
}
|
|
|
|
|
2013-10-07 10:47:09 +08:00
|
|
|
bool LinkingContext::createImplicitFiles(
|
2014-10-02 04:24:30 +08:00
|
|
|
std::vector<std::unique_ptr<File> > &result) {
|
2013-10-07 10:47:09 +08:00
|
|
|
return this->writer().createImplicitFiles(result);
|
2013-01-22 10:15:30 +08:00
|
|
|
}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-10-07 10:47:09 +08:00
|
|
|
std::unique_ptr<File> LinkingContext::createEntrySymbolFile() const {
|
2014-03-29 03:02:06 +08:00
|
|
|
return createEntrySymbolFile("<command line option -e>");
|
2013-12-26 14:35:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<File>
|
|
|
|
LinkingContext::createEntrySymbolFile(StringRef filename) const {
|
2013-08-31 13:27:38 +08:00
|
|
|
if (entrySymbolName().empty())
|
|
|
|
return nullptr;
|
2013-12-26 14:35:35 +08:00
|
|
|
std::unique_ptr<SimpleFile> entryFile(new SimpleFile(filename));
|
2013-08-31 13:27:38 +08:00
|
|
|
entryFile->addAtom(
|
|
|
|
*(new (_allocator) SimpleUndefinedAtom(*entryFile, entrySymbolName())));
|
|
|
|
return std::move(entryFile);
|
|
|
|
}
|
|
|
|
|
2013-10-07 10:47:09 +08:00
|
|
|
std::unique_ptr<File> LinkingContext::createUndefinedSymbolFile() const {
|
2014-03-29 03:02:06 +08:00
|
|
|
return createUndefinedSymbolFile("<command line option -u or --defsym>");
|
2013-12-26 14:35:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<File>
|
|
|
|
LinkingContext::createUndefinedSymbolFile(StringRef filename) const {
|
2013-08-31 13:27:38 +08:00
|
|
|
if (_initialUndefinedSymbols.empty())
|
|
|
|
return nullptr;
|
2013-12-26 14:35:35 +08:00
|
|
|
std::unique_ptr<SimpleFile> undefinedSymFile(new SimpleFile(filename));
|
2014-05-03 05:48:01 +08:00
|
|
|
for (StringRef undefSym : _initialUndefinedSymbols)
|
2013-08-31 13:27:38 +08:00
|
|
|
undefinedSymFile->addAtom(*(new (_allocator) SimpleUndefinedAtom(
|
2014-05-03 05:48:01 +08:00
|
|
|
*undefinedSymFile, undefSym)));
|
2013-08-31 13:27:38 +08:00
|
|
|
return std::move(undefinedSymFile);
|
|
|
|
}
|
|
|
|
|
2014-05-23 05:37:56 +08:00
|
|
|
std::unique_ptr<File> LinkingContext::createAliasSymbolFile() const {
|
|
|
|
if (getAliases().empty())
|
|
|
|
return nullptr;
|
|
|
|
std::unique_ptr<SimpleFile> file(new SimpleFile("<alias>"));
|
|
|
|
for (const auto &i : getAliases()) {
|
|
|
|
StringRef from = i.first;
|
|
|
|
StringRef to = i.second;
|
|
|
|
SimpleDefinedAtom *fromAtom = new (_allocator) AliasAtom(*file, from);
|
|
|
|
UndefinedAtom *toAtom = new (_allocator) SimpleUndefinedAtom(*file, to);
|
|
|
|
fromAtom->addReference(Reference::KindNamespace::all,
|
|
|
|
Reference::KindArch::all, Reference::kindLayoutAfter,
|
|
|
|
0, toAtom, 0);
|
|
|
|
file->addAtom(*fromAtom);
|
|
|
|
file->addAtom(*toAtom);
|
|
|
|
}
|
|
|
|
return std::move(file);
|
|
|
|
}
|
|
|
|
|
2014-01-01 03:15:42 +08:00
|
|
|
void LinkingContext::createInternalFiles(
|
2013-10-07 10:47:09 +08:00
|
|
|
std::vector<std::unique_ptr<File> > &result) const {
|
2014-05-22 08:36:36 +08:00
|
|
|
if (std::unique_ptr<File> file = createEntrySymbolFile())
|
|
|
|
result.push_back(std::move(file));
|
|
|
|
if (std::unique_ptr<File> file = createUndefinedSymbolFile())
|
|
|
|
result.push_back(std::move(file));
|
2014-05-23 05:37:56 +08:00
|
|
|
if (std::unique_ptr<File> file = createAliasSymbolFile())
|
|
|
|
result.push_back(std::move(file));
|
2013-10-07 10:47:09 +08:00
|
|
|
}
|
|
|
|
|
2013-10-29 13:12:14 +08:00
|
|
|
void LinkingContext::addPasses(PassManager &pm) {}
|
2013-04-05 02:59:24 +08:00
|
|
|
|
2013-01-22 10:15:30 +08:00
|
|
|
} // end namespace lld
|