Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@@ -0,0 +1,28 @@
if(NOT LLD_BUILT_STANDALONE)
set(tablegen_deps intrinsics_gen)
endif()
add_lld_library(lldCore
DefinedAtom.cpp
Error.cpp
File.cpp
LinkingContext.cpp
Reader.cpp
Resolver.cpp
SymbolTable.cpp
Writer.cpp
ADDITIONAL_HEADER_DIRS
${LLD_INCLUDE_DIR}/lld/Core
LINK_COMPONENTS
BinaryFormat
MC
Support
LINK_LIBS
${LLVM_PTHREAD_LIB}
DEPENDS
${tablegen_deps}
)

View File

@@ -0,0 +1,82 @@
//===- DefinedAtom.cpp ------------------------------------------*- C++ -*-===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/ErrorHandling.h"
#include "lld/Core/DefinedAtom.h"
#include "lld/Core/File.h"
namespace lld {
DefinedAtom::ContentPermissions DefinedAtom::permissions() const {
// By default base permissions on content type.
return permissions(this->contentType());
}
// Utility function for deriving permissions from content type
DefinedAtom::ContentPermissions DefinedAtom::permissions(ContentType type) {
switch (type) {
case typeCode:
case typeResolver:
case typeBranchIsland:
case typeBranchShim:
case typeStub:
case typeStubHelper:
case typeMachHeader:
return permR_X;
case typeConstant:
case typeCString:
case typeUTF16String:
case typeCFI:
case typeLSDA:
case typeLiteral4:
case typeLiteral8:
case typeLiteral16:
case typeDTraceDOF:
case typeCompactUnwindInfo:
case typeProcessedUnwindInfo:
case typeObjCImageInfo:
case typeObjCMethodList:
return permR__;
case typeData:
case typeDataFast:
case typeZeroFill:
case typeZeroFillFast:
case typeObjC1Class:
case typeLazyPointer:
case typeLazyDylibPointer:
case typeNonLazyPointer:
case typeThunkTLV:
return permRW_;
case typeGOT:
case typeConstData:
case typeCFString:
case typeInitializerPtr:
case typeTerminatorPtr:
case typeCStringPtr:
case typeObjCClassPtr:
case typeObjC2CategoryList:
case typeInterposingTuples:
case typeTLVInitialData:
case typeTLVInitialZeroFill:
case typeTLVInitializerPtr:
return permRW_L;
case typeUnknown:
case typeTempLTO:
case typeSectCreate:
case typeDSOHandle:
return permUnknown;
}
llvm_unreachable("unknown content type");
}
} // namespace

View File

@@ -0,0 +1,93 @@
//===- Error.cpp - system_error extensions for lld --------------*- C++ -*-===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Core/Error.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/ErrorHandling.h"
#include <mutex>
#include <string>
#include <vector>
using namespace lld;
namespace {
class _YamlReaderErrorCategory : public std::error_category {
public:
const char* name() const noexcept override {
return "lld.yaml.reader";
}
std::string message(int ev) const override {
switch (static_cast<YamlReaderError>(ev)) {
case YamlReaderError::unknown_keyword:
return "Unknown keyword found in yaml file";
case YamlReaderError::illegal_value:
return "Bad value found in yaml file";
}
llvm_unreachable("An enumerator of YamlReaderError does not have a "
"message defined.");
}
};
} // end anonymous namespace
const std::error_category &lld::YamlReaderCategory() {
static _YamlReaderErrorCategory o;
return o;
}
namespace lld {
/// Temporary class to enable make_dynamic_error_code() until
/// llvm::ErrorOr<> is updated to work with error encapsulations
/// other than error_code.
class dynamic_error_category : public std::error_category {
public:
~dynamic_error_category() override = default;
const char *name() const noexcept override {
return "lld.dynamic_error";
}
std::string message(int ev) const override {
assert(ev >= 0);
assert(ev < (int)_messages.size());
// The value is an index into the string vector.
return _messages[ev];
}
int add(std::string msg) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
// Value zero is always the successs value.
if (_messages.empty())
_messages.push_back("Success");
_messages.push_back(msg);
// Return the index of the string just appended.
return _messages.size() - 1;
}
private:
std::vector<std::string> _messages;
std::recursive_mutex _mutex;
};
static dynamic_error_category categorySingleton;
std::error_code make_dynamic_error_code(StringRef msg) {
return std::error_code(categorySingleton.add(msg), categorySingleton);
}
char GenericError::ID = 0;
GenericError::GenericError(Twine Msg) : Msg(Msg.str()) { }
void GenericError::log(raw_ostream &OS) const {
OS << Msg;
}
} // namespace lld

View File

@@ -0,0 +1,29 @@
//===- Core/File.cpp - A Container of Atoms -------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Core/File.h"
#include <mutex>
namespace lld {
File::~File() = default;
File::AtomVector<DefinedAtom> File::_noDefinedAtoms;
File::AtomVector<UndefinedAtom> File::_noUndefinedAtoms;
File::AtomVector<SharedLibraryAtom> File::_noSharedLibraryAtoms;
File::AtomVector<AbsoluteAtom> File::_noAbsoluteAtoms;
std::error_code File::parse() {
std::lock_guard<std::mutex> lock(_parseMutex);
if (!_lastError.hasValue())
_lastError = doParse();
return _lastError.getValue();
}
} // end namespace lld

View File

@@ -0,0 +1,70 @@
//===- lib/Core/LinkingContext.cpp - Linker Context Object Interface ------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Core/LinkingContext.h"
#include "lld/Core/File.h"
#include "lld/Core/Node.h"
#include "lld/Core/Simple.h"
#include "lld/Core/Writer.h"
#include <algorithm>
namespace lld {
LinkingContext::LinkingContext() = default;
LinkingContext::~LinkingContext() = default;
bool LinkingContext::validate(raw_ostream &diagnostics) {
return validateImpl(diagnostics);
}
llvm::Error LinkingContext::writeFile(const File &linkedFile) const {
return this->writer().writeFile(linkedFile, _outputPath);
}
std::unique_ptr<File> LinkingContext::createEntrySymbolFile() const {
return createEntrySymbolFile("<command line option -e>");
}
std::unique_ptr<File>
LinkingContext::createEntrySymbolFile(StringRef filename) const {
if (entrySymbolName().empty())
return nullptr;
std::unique_ptr<SimpleFile> entryFile(new SimpleFile(filename,
File::kindEntryObject));
entryFile->addAtom(
*(new (_allocator) SimpleUndefinedAtom(*entryFile, entrySymbolName())));
return std::move(entryFile);
}
std::unique_ptr<File> LinkingContext::createUndefinedSymbolFile() const {
return createUndefinedSymbolFile("<command line option -u or --defsym>");
}
std::unique_ptr<File>
LinkingContext::createUndefinedSymbolFile(StringRef filename) const {
if (_initialUndefinedSymbols.empty())
return nullptr;
std::unique_ptr<SimpleFile> undefinedSymFile(
new SimpleFile(filename, File::kindUndefinedSymsObject));
for (StringRef undefSym : _initialUndefinedSymbols)
undefinedSymFile->addAtom(*(new (_allocator) SimpleUndefinedAtom(
*undefinedSymFile, undefSym)));
return std::move(undefinedSymFile);
}
void LinkingContext::createInternalFiles(
std::vector<std::unique_ptr<File>> &result) const {
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));
}
} // end namespace lld

View File

@@ -0,0 +1,114 @@
//===- lib/Core/Reader.cpp ------------------------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Core/Reader.h"
#include "lld/Core/File.h"
#include "lld/Core/Reference.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include <algorithm>
#include <memory>
using llvm::file_magic;
using llvm::identify_magic;
namespace lld {
YamlIOTaggedDocumentHandler::~YamlIOTaggedDocumentHandler() = default;
void Registry::add(std::unique_ptr<Reader> reader) {
_readers.push_back(std::move(reader));
}
void Registry::add(std::unique_ptr<YamlIOTaggedDocumentHandler> handler) {
_yamlHandlers.push_back(std::move(handler));
}
ErrorOr<std::unique_ptr<File>>
Registry::loadFile(std::unique_ptr<MemoryBuffer> mb) const {
// Get file magic.
StringRef content(mb->getBufferStart(), mb->getBufferSize());
file_magic fileType = identify_magic(content);
// Ask each registered reader if it can handle this file type or extension.
for (const std::unique_ptr<Reader> &reader : _readers) {
if (!reader->canParse(fileType, mb->getMemBufferRef()))
continue;
return reader->loadFile(std::move(mb), *this);
}
// No Reader could parse this file.
return make_error_code(llvm::errc::executable_format_error);
}
static const Registry::KindStrings kindStrings[] = {
{Reference::kindLayoutAfter, "layout-after"},
{Reference::kindAssociate, "associate"},
LLD_KIND_STRING_END};
Registry::Registry() {
addKindTable(Reference::KindNamespace::all, Reference::KindArch::all,
kindStrings);
}
bool Registry::handleTaggedDoc(llvm::yaml::IO &io,
const lld::File *&file) const {
for (const std::unique_ptr<YamlIOTaggedDocumentHandler> &h : _yamlHandlers)
if (h->handledDocTag(io, file))
return true;
return false;
}
void Registry::addKindTable(Reference::KindNamespace ns,
Reference::KindArch arch,
const KindStrings array[]) {
KindEntry entry = { ns, arch, array };
_kindEntries.push_back(entry);
}
bool Registry::referenceKindFromString(StringRef inputStr,
Reference::KindNamespace &ns,
Reference::KindArch &arch,
Reference::KindValue &value) const {
for (const KindEntry &entry : _kindEntries) {
for (const KindStrings *pair = entry.array; !pair->name.empty(); ++pair) {
if (!inputStr.equals(pair->name))
continue;
ns = entry.ns;
arch = entry.arch;
value = pair->value;
return true;
}
}
return false;
}
bool Registry::referenceKindToString(Reference::KindNamespace ns,
Reference::KindArch arch,
Reference::KindValue value,
StringRef &str) const {
for (const KindEntry &entry : _kindEntries) {
if (entry.ns != ns)
continue;
if (entry.arch != arch)
continue;
for (const KindStrings *pair = entry.array; !pair->name.empty(); ++pair) {
if (pair->value != value)
continue;
str = pair->name;
return true;
}
}
return false;
}
} // end namespace lld

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,291 @@
//===- Core/SymbolTable.cpp - Main Symbol Table ---------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Core/SymbolTable.h"
#include "lld/Common/LLVM.h"
#include "lld/Core/AbsoluteAtom.h"
#include "lld/Core/Atom.h"
#include "lld/Core/DefinedAtom.h"
#include "lld/Core/File.h"
#include "lld/Core/LinkingContext.h"
#include "lld/Core/Resolver.h"
#include "lld/Core/SharedLibraryAtom.h"
#include "lld/Core/UndefinedAtom.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <vector>
namespace lld {
bool SymbolTable::add(const UndefinedAtom &atom) { return addByName(atom); }
bool SymbolTable::add(const SharedLibraryAtom &atom) { return addByName(atom); }
bool SymbolTable::add(const AbsoluteAtom &atom) { return addByName(atom); }
bool SymbolTable::add(const DefinedAtom &atom) {
if (!atom.name().empty() &&
atom.scope() != DefinedAtom::scopeTranslationUnit) {
// Named atoms cannot be merged by content.
assert(atom.merge() != DefinedAtom::mergeByContent);
// Track named atoms that are not scoped to file (static).
return addByName(atom);
}
if (atom.merge() == DefinedAtom::mergeByContent) {
// Named atoms cannot be merged by content.
assert(atom.name().empty());
// Currently only read-only constants can be merged.
if (atom.permissions() == DefinedAtom::permR__)
return addByContent(atom);
// TODO: support mergeByContent of data atoms by comparing content & fixups.
}
return false;
}
enum NameCollisionResolution {
NCR_First,
NCR_Second,
NCR_DupDef,
NCR_DupUndef,
NCR_DupShLib,
NCR_Error
};
static NameCollisionResolution cases[4][4] = {
//regular absolute undef sharedLib
{
// first is regular
NCR_DupDef, NCR_Error, NCR_First, NCR_First
},
{
// first is absolute
NCR_Error, NCR_Error, NCR_First, NCR_First
},
{
// first is undef
NCR_Second, NCR_Second, NCR_DupUndef, NCR_Second
},
{
// first is sharedLib
NCR_Second, NCR_Second, NCR_First, NCR_DupShLib
}
};
static NameCollisionResolution collide(Atom::Definition first,
Atom::Definition second) {
return cases[first][second];
}
enum MergeResolution {
MCR_First,
MCR_Second,
MCR_Largest,
MCR_SameSize,
MCR_Error
};
static MergeResolution mergeCases[][6] = {
// no tentative weak weakAddress sameNameAndSize largest
{MCR_Error, MCR_First, MCR_First, MCR_First, MCR_SameSize, MCR_Largest}, // no
{MCR_Second, MCR_Largest, MCR_Second, MCR_Second, MCR_SameSize, MCR_Largest}, // tentative
{MCR_Second, MCR_First, MCR_First, MCR_Second, MCR_SameSize, MCR_Largest}, // weak
{MCR_Second, MCR_First, MCR_First, MCR_First, MCR_SameSize, MCR_Largest}, // weakAddress
{MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize, MCR_SameSize}, // sameSize
{MCR_Largest, MCR_Largest, MCR_Largest, MCR_Largest, MCR_SameSize, MCR_Largest}, // largest
};
static MergeResolution mergeSelect(DefinedAtom::Merge first,
DefinedAtom::Merge second) {
assert(first != DefinedAtom::mergeByContent);
assert(second != DefinedAtom::mergeByContent);
return mergeCases[first][second];
}
bool SymbolTable::addByName(const Atom &newAtom) {
StringRef name = newAtom.name();
assert(!name.empty());
const Atom *existing = findByName(name);
if (existing == nullptr) {
// Name is not in symbol table yet, add it associate with this atom.
_nameTable[name] = &newAtom;
return true;
}
// Do nothing if the same object is added more than once.
if (existing == &newAtom)
return false;
// Name is already in symbol table and associated with another atom.
bool useNew = true;
switch (collide(existing->definition(), newAtom.definition())) {
case NCR_First:
useNew = false;
break;
case NCR_Second:
useNew = true;
break;
case NCR_DupDef: {
const auto *existingDef = cast<DefinedAtom>(existing);
const auto *newDef = cast<DefinedAtom>(&newAtom);
switch (mergeSelect(existingDef->merge(), newDef->merge())) {
case MCR_First:
useNew = false;
break;
case MCR_Second:
useNew = true;
break;
case MCR_Largest: {
uint64_t existingSize = existingDef->sectionSize();
uint64_t newSize = newDef->sectionSize();
useNew = (newSize >= existingSize);
break;
}
case MCR_SameSize: {
uint64_t existingSize = existingDef->sectionSize();
uint64_t newSize = newDef->sectionSize();
if (existingSize == newSize) {
useNew = true;
break;
}
llvm::errs() << "Size mismatch: "
<< existing->name() << " (" << existingSize << ") "
<< newAtom.name() << " (" << newSize << ")\n";
LLVM_FALLTHROUGH;
}
case MCR_Error:
llvm::errs() << "Duplicate symbols: "
<< existing->name()
<< ":"
<< existing->file().path()
<< " and "
<< newAtom.name()
<< ":"
<< newAtom.file().path()
<< "\n";
llvm::report_fatal_error("duplicate symbol error");
break;
}
break;
}
case NCR_DupUndef: {
const UndefinedAtom* existingUndef = cast<UndefinedAtom>(existing);
const UndefinedAtom* newUndef = cast<UndefinedAtom>(&newAtom);
bool sameCanBeNull = (existingUndef->canBeNull() == newUndef->canBeNull());
if (sameCanBeNull)
useNew = false;
else
useNew = (newUndef->canBeNull() < existingUndef->canBeNull());
break;
}
case NCR_DupShLib: {
useNew = false;
break;
}
case NCR_Error:
llvm::errs() << "SymbolTable: error while merging " << name << "\n";
llvm::report_fatal_error("duplicate symbol error");
break;
}
if (useNew) {
// Update name table to use new atom.
_nameTable[name] = &newAtom;
// Add existing atom to replacement table.
_replacedAtoms[existing] = &newAtom;
} else {
// New atom is not being used. Add it to replacement table.
_replacedAtoms[&newAtom] = existing;
}
return false;
}
unsigned SymbolTable::AtomMappingInfo::getHashValue(const DefinedAtom *atom) {
auto content = atom->rawContent();
return llvm::hash_combine(atom->size(),
atom->contentType(),
llvm::hash_combine_range(content.begin(),
content.end()));
}
bool SymbolTable::AtomMappingInfo::isEqual(const DefinedAtom * const l,
const DefinedAtom * const r) {
if (l == r)
return true;
if (l == getEmptyKey() || r == getEmptyKey())
return false;
if (l == getTombstoneKey() || r == getTombstoneKey())
return false;
if (l->contentType() != r->contentType())
return false;
if (l->size() != r->size())
return false;
if (l->sectionChoice() != r->sectionChoice())
return false;
if (l->sectionChoice() == DefinedAtom::sectionCustomRequired) {
if (!l->customSectionName().equals(r->customSectionName()))
return false;
}
ArrayRef<uint8_t> lc = l->rawContent();
ArrayRef<uint8_t> rc = r->rawContent();
return memcmp(lc.data(), rc.data(), lc.size()) == 0;
}
bool SymbolTable::addByContent(const DefinedAtom &newAtom) {
AtomContentSet::iterator pos = _contentTable.find(&newAtom);
if (pos == _contentTable.end()) {
_contentTable.insert(&newAtom);
return true;
}
const Atom* existing = *pos;
// New atom is not being used. Add it to replacement table.
_replacedAtoms[&newAtom] = existing;
return false;
}
const Atom *SymbolTable::findByName(StringRef sym) {
NameToAtom::iterator pos = _nameTable.find(sym);
if (pos == _nameTable.end())
return nullptr;
return pos->second;
}
const Atom *SymbolTable::replacement(const Atom *atom) {
// Find the replacement for a given atom. Atoms in _replacedAtoms
// may be chained, so find the last one.
for (;;) {
AtomToAtom::iterator pos = _replacedAtoms.find(atom);
if (pos == _replacedAtoms.end())
return atom;
atom = pos->second;
}
}
bool SymbolTable::isCoalescedAway(const Atom *atom) {
return _replacedAtoms.count(atom) > 0;
}
std::vector<const UndefinedAtom *> SymbolTable::undefines() {
std::vector<const UndefinedAtom *> ret;
for (auto it : _nameTable) {
const Atom *atom = it.second;
assert(atom != nullptr);
if (const auto *undef = dyn_cast<const UndefinedAtom>(atom))
if (_replacedAtoms.count(undef) == 0)
ret.push_back(undef);
}
return ret;
}
} // namespace lld

View File

@@ -0,0 +1,18 @@
//===- lib/Core/Writer.cpp ------------------------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Core/Writer.h"
namespace lld {
Writer::Writer() = default;
Writer::~Writer() = default;
} // end namespace lld