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,3 @@
add_subdirectory(Core)
add_subdirectory(Driver)
add_subdirectory(ReaderWriter)

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

View File

@@ -0,0 +1,22 @@
set(LLVM_TARGET_DEFINITIONS DarwinLdOptions.td)
tablegen(LLVM DarwinLdOptions.inc -gen-opt-parser-defs)
add_public_tablegen_target(DriverOptionsTableGen)
add_lld_library(lldDriver
DarwinLdDriver.cpp
ADDITIONAL_HEADER_DIRS
${LLD_INCLUDE_DIR}/lld/Driver
LINK_COMPONENTS
Option
Support
LINK_LIBS
lldCore
lldMachO
lldReaderWriter
lldYAML
)
add_dependencies(lldDriver DriverOptionsTableGen)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,242 @@
include "llvm/Option/OptParser.td"
// output kinds
def grp_kind : OptionGroup<"outs">, HelpText<"OUTPUT KIND">;
def relocatable : Flag<["-"], "r">,
HelpText<"Create relocatable object file">, Group<grp_kind>;
def static : Flag<["-"], "static">,
HelpText<"Create static executable">, Group<grp_kind>;
def dynamic : Flag<["-"], "dynamic">,
HelpText<"Create dynamic executable (default)">,Group<grp_kind>;
def dylib : Flag<["-"], "dylib">,
HelpText<"Create dynamic library">, Group<grp_kind>;
def bundle : Flag<["-"], "bundle">,
HelpText<"Create dynamic bundle">, Group<grp_kind>;
def execute : Flag<["-"], "execute">,
HelpText<"Create main executable (default)">, Group<grp_kind>;
def preload : Flag<["-"], "preload">,
HelpText<"Create binary for use with embedded systems">, Group<grp_kind>;
// optimizations
def grp_opts : OptionGroup<"opts">, HelpText<"OPTIMIZATIONS">;
def dead_strip : Flag<["-"], "dead_strip">,
HelpText<"Remove unreference code and data">, Group<grp_opts>;
def macosx_version_min : Separate<["-"], "macosx_version_min">,
MetaVarName<"<version>">,
HelpText<"Minimum Mac OS X version">, Group<grp_opts>;
def ios_version_min : Separate<["-"], "ios_version_min">,
MetaVarName<"<version>">,
HelpText<"Minimum iOS version">, Group<grp_opts>;
def iphoneos_version_min : Separate<["-"], "iphoneos_version_min">,
Alias<ios_version_min>;
def ios_simulator_version_min : Separate<["-"], "ios_simulator_version_min">,
MetaVarName<"<version>">,
HelpText<"Minimum iOS simulator version">, Group<grp_opts>;
def sdk_version : Separate<["-"], "sdk_version">,
MetaVarName<"<version>">,
HelpText<"SDK version">, Group<grp_opts>;
def source_version : Separate<["-"], "source_version">,
MetaVarName<"<version>">,
HelpText<"Source version">, Group<grp_opts>;
def version_load_command : Flag<["-"], "version_load_command">,
HelpText<"Force generation of a version load command">, Group<grp_opts>;
def no_version_load_command : Flag<["-"], "no_version_load_command">,
HelpText<"Disable generation of a version load command">, Group<grp_opts>;
def function_starts : Flag<["-"], "function_starts">,
HelpText<"Force generation of a function starts load command">,
Group<grp_opts>;
def no_function_starts : Flag<["-"], "no_function_starts">,
HelpText<"Disable generation of a function starts load command">,
Group<grp_opts>;
def data_in_code_info : Flag<["-"], "data_in_code_info">,
HelpText<"Force generation of a data in code load command">,
Group<grp_opts>;
def no_data_in_code_info : Flag<["-"], "no_data_in_code_info">,
HelpText<"Disable generation of a data in code load command">,
Group<grp_opts>;
def mllvm : Separate<["-"], "mllvm">,
MetaVarName<"<option>">,
HelpText<"Options to pass to LLVM during LTO">, Group<grp_opts>;
def exported_symbols_list : Separate<["-"], "exported_symbols_list">,
MetaVarName<"<file-path>">,
HelpText<"Restricts which symbols will be exported">, Group<grp_opts>;
def exported_symbol : Separate<["-"], "exported_symbol">,
MetaVarName<"<symbol>">,
HelpText<"Restricts which symbols will be exported">, Group<grp_opts>;
def unexported_symbols_list : Separate<["-"], "unexported_symbols_list">,
MetaVarName<"<file-path>">,
HelpText<"Lists symbols that should not be exported">, Group<grp_opts>;
def unexported_symbol : Separate<["-"], "unexported_symbol">,
MetaVarName<"<symbol>">,
HelpText<"A symbol which should not be exported">, Group<grp_opts>;
def keep_private_externs : Flag<["-"], "keep_private_externs">,
HelpText<"Private extern (hidden) symbols should not be transformed "
"into local symbols">, Group<grp_opts>;
def order_file : Separate<["-"], "order_file">,
MetaVarName<"<file-path>">,
HelpText<"re-order and move specified symbols to start of their section">,
Group<grp_opts>;
def flat_namespace : Flag<["-"], "flat_namespace">,
HelpText<"Resolves symbols in any (transitively) linked dynamic libraries. "
"Source libraries are not recorded: dyld will re-search all "
"images at runtime and use the first definition found.">,
Group<grp_opts>;
def twolevel_namespace : Flag<["-"], "twolevel_namespace">,
HelpText<"Resolves symbols in listed libraries only. Source libraries are "
"recorded in the symbol table.">,
Group<grp_opts>;
def undefined : Separate<["-"], "undefined">,
MetaVarName<"<undefined>">,
HelpText<"Determines how undefined symbols are handled.">,
Group<grp_opts>;
def no_objc_category_merging : Flag<["-"], "no_objc_category_merging">,
HelpText<"Disables the optimisation which merges Objective-C categories "
"on a class in to the class itself.">,
Group<grp_opts>;
// main executable options
def grp_main : OptionGroup<"opts">, HelpText<"MAIN EXECUTABLE OPTIONS">;
def entry : Separate<["-"], "e">,
MetaVarName<"<entry-name>">,
HelpText<"entry symbol name">,Group<grp_main>;
def pie : Flag<["-"], "pie">,
HelpText<"Create Position Independent Executable (for ASLR)">,
Group<grp_main>;
def no_pie : Flag<["-"], "no_pie">,
HelpText<"Do not create Position Independent Executable">,
Group<grp_main>;
def stack_size : Separate<["-"], "stack_size">,
HelpText<"Specifies the maximum stack size for the main thread in a program. "
"Must be a page-size multiple. (default=8Mb)">,
Group<grp_main>;
def export_dynamic : Flag<["-"], "export_dynamic">,
HelpText<"Preserves all global symbols in main executables during LTO">,
Group<grp_main>;
// dylib executable options
def grp_dylib : OptionGroup<"opts">, HelpText<"DYLIB EXECUTABLE OPTIONS">;
def install_name : Separate<["-"], "install_name">,
MetaVarName<"<path>">,
HelpText<"The dylib's install name">, Group<grp_dylib>;
def mark_dead_strippable_dylib : Flag<["-"], "mark_dead_strippable_dylib">,
HelpText<"Marks the dylib as having no side effects during initialization">,
Group<grp_dylib>;
def compatibility_version : Separate<["-"], "compatibility_version">,
MetaVarName<"<version>">,
HelpText<"The dylib's compatibility version">, Group<grp_dylib>;
def current_version : Separate<["-"], "current_version">,
MetaVarName<"<version>">,
HelpText<"The dylib's current version">, Group<grp_dylib>;
// dylib executable options - compatibility aliases
def dylib_install_name : Separate<["-"], "dylib_install_name">,
Alias<install_name>;
def dylib_compatibility_version : Separate<["-"], "dylib_compatibility_version">,
MetaVarName<"<version>">, Alias<compatibility_version>;
def dylib_current_version : Separate<["-"], "dylib_current_version">,
MetaVarName<"<version>">, Alias<current_version>;
// bundle executable options
def grp_bundle : OptionGroup<"opts">, HelpText<"BUNDLE EXECUTABLE OPTIONS">;
def bundle_loader : Separate<["-"], "bundle_loader">,
MetaVarName<"<path>">,
HelpText<"The executable that will be loading this Mach-O bundle">,
Group<grp_bundle>;
// library options
def grp_libs : OptionGroup<"libs">, HelpText<"LIBRARY OPTIONS">;
def L : JoinedOrSeparate<["-"], "L">,
MetaVarName<"<dir>">,
HelpText<"Add directory to library search path">, Group<grp_libs>;
def F : JoinedOrSeparate<["-"], "F">,
MetaVarName<"<dir>">,
HelpText<"Add directory to framework search path">, Group<grp_libs>;
def Z : Flag<["-"], "Z">,
HelpText<"Do not search standard directories for libraries or frameworks">;
def all_load : Flag<["-"], "all_load">,
HelpText<"Forces all members of all static libraries to be loaded">,
Group<grp_libs>;
def force_load : Separate<["-"], "force_load">,
MetaVarName<"<library-path>">,
HelpText<"Forces all members of specified static libraries to be loaded">,
Group<grp_libs>;
def syslibroot : Separate<["-"], "syslibroot">, MetaVarName<"<dir>">,
HelpText<"Add path to SDK to all absolute library search paths">,
Group<grp_libs>;
// Input options
def l : Joined<["-"], "l">,
MetaVarName<"<libname>">,
HelpText<"Base name of library searched for in -L directories">;
def upward_l : Joined<["-"], "upward-l">,
MetaVarName<"<libname>">,
HelpText<"Base name of upward library searched for in -L directories">;
def framework : Separate<["-"], "framework">,
MetaVarName<"<name>">,
HelpText<"Base name of framework searched for in -F directories">;
def upward_framework : Separate<["-"], "upward_framework">,
MetaVarName<"<name>">,
HelpText<"Base name of upward framework searched for in -F directories">;
def upward_library : Separate<["-"], "upward_library">,
MetaVarName<"<path>">,
HelpText<"path to upward dylib to link with">;
def filelist : Separate<["-"], "filelist">,
MetaVarName<"<path>">,
HelpText<"file containing paths to input files">;
// test case options
def print_atoms : Flag<["-"], "print_atoms">,
HelpText<"Emit output as yaml atoms">;
def test_file_usage : Flag<["-"], "test_file_usage">,
HelpText<"Only files specified by -file_exists are considered to exist. "
"Print which files would be used">;
def path_exists : Separate<["-"], "path_exists">,
MetaVarName<"<path>">,
HelpText<"Used with -test_file_usage to declare a path">;
// general options
def output : Separate<["-"], "o">,
MetaVarName<"<path>">,
HelpText<"Output file path">;
def arch : Separate<["-"], "arch">,
MetaVarName<"<arch-name>">,
HelpText<"Architecture to link">;
def sectalign : MultiArg<["-"], "sectalign", 3>,
MetaVarName<"<segname> <sectname> <alignment>">,
HelpText<"Alignment for segment/section">;
def sectcreate : MultiArg<["-"], "sectcreate", 3>,
MetaVarName<"<segname> <sectname> <file>">,
HelpText<"Create section <segname>/<sectname> from contents of <file>">;
def image_base : Separate<["-"], "image_base">;
def seg1addr : Separate<["-"], "seg1addr">, Alias<image_base>;
def demangle : Flag<["-"], "demangle">,
HelpText<"Demangles symbol names in errors and warnings">;
def dependency_info : Separate<["-"], "dependency_info">,
MetaVarName<"<file>">,
HelpText<"Write binary list of files used during link">;
def S : Flag<["-"], "S">,
HelpText<"Remove debug information (STABS or DWARF) from the output file">;
def rpath : Separate<["-"], "rpath">,
MetaVarName<"<path>">,
HelpText<"Add path to the runpath search path list for image being created">;
def t : Flag<["-"], "t">,
HelpText<"Print the names of the input files as ld processes them">;
def v : Flag<["-"], "v">,
HelpText<"Print linker information">;
// Obsolete options
def grp_obsolete : OptionGroup<"obsolete">, HelpText<"OBSOLETE OPTIONS">;
def single_module : Flag<["-"], "single_module">,
HelpText<"Default for dylibs">, Group<grp_obsolete>;
def multi_module : Flag<["-"], "multi_module">,
HelpText<"Unsupported way to build dylibs">, Group<grp_obsolete>;
def objc_gc_compaction : Flag<["-"], "objc_gc_compaction">,
HelpText<"Unsupported ObjC GC option">, Group<grp_obsolete>;
def objc_gc : Flag<["-"], "objc_gc">,
HelpText<"Unsupported ObjC GC option">, Group<grp_obsolete>;
def objc_gc_only : Flag<["-"], "objc_gc_only">,
HelpText<"Unsupported ObjC GC option">, Group<grp_obsolete>;

View File

@@ -0,0 +1,20 @@
add_subdirectory(MachO)
add_subdirectory(YAML)
if (MSVC)
add_definitions(-wd4062) # Suppress 'warning C4062: Enumerator has no associated handler in a switch statement.'
endif()
add_lld_library(lldReaderWriter
FileArchive.cpp
ADDITIONAL_HEADER_DIRS
${LLD_INCLUDE_DIR}/lld/ReaderWriter
LINK_COMPONENTS
Object
Support
LINK_LIBS
lldCore
)

View File

@@ -0,0 +1,228 @@
//===- lib/ReaderWriter/FileArchive.cpp -----------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Common/LLVM.h"
#include "lld/Core/ArchiveLibraryFile.h"
#include "lld/Core/File.h"
#include "lld/Core/Reader.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/Object/Archive.h"
#include "llvm/Object/Error.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>
#include <set>
#include <string>
#include <system_error>
#include <unordered_map>
#include <utility>
#include <vector>
using llvm::object::Archive;
using llvm::file_magic;
using llvm::identify_magic;
namespace lld {
namespace {
/// \brief The FileArchive class represents an Archive Library file
class FileArchive : public lld::ArchiveLibraryFile {
public:
FileArchive(std::unique_ptr<MemoryBuffer> mb, const Registry &reg,
StringRef path, bool logLoading)
: ArchiveLibraryFile(path), _mb(std::shared_ptr<MemoryBuffer>(mb.release())),
_registry(reg), _logLoading(logLoading) {}
/// \brief Check if any member of the archive contains an Atom with the
/// specified name and return the File object for that member, or nullptr.
File *find(StringRef name) override {
auto member = _symbolMemberMap.find(name);
if (member == _symbolMemberMap.end())
return nullptr;
Archive::Child c = member->second;
// Don't return a member already returned
Expected<StringRef> buf = c.getBuffer();
if (!buf) {
// TODO: Actually report errors helpfully.
consumeError(buf.takeError());
return nullptr;
}
const char *memberStart = buf->data();
if (_membersInstantiated.count(memberStart))
return nullptr;
_membersInstantiated.insert(memberStart);
std::unique_ptr<File> result;
if (instantiateMember(c, result))
return nullptr;
File *file = result.get();
_filesReturned.push_back(std::move(result));
// Give up the file pointer. It was stored and will be destroyed with destruction of FileArchive
return file;
}
/// \brief parse each member
std::error_code
parseAllMembers(std::vector<std::unique_ptr<File>> &result) override {
if (std::error_code ec = parse())
return ec;
llvm::Error err = llvm::Error::success();
for (auto mf = _archive->child_begin(err), me = _archive->child_end();
mf != me; ++mf) {
std::unique_ptr<File> file;
if (std::error_code ec = instantiateMember(*mf, file)) {
// err is Success (or we wouldn't be in the loop body) but we can't
// return without testing or consuming it.
consumeError(std::move(err));
return ec;
}
result.push_back(std::move(file));
}
if (err)
return errorToErrorCode(std::move(err));
return std::error_code();
}
const AtomRange<DefinedAtom> defined() const override {
return _noDefinedAtoms;
}
const AtomRange<UndefinedAtom> undefined() const override {
return _noUndefinedAtoms;
}
const AtomRange<SharedLibraryAtom> sharedLibrary() const override {
return _noSharedLibraryAtoms;
}
const AtomRange<AbsoluteAtom> absolute() const override {
return _noAbsoluteAtoms;
}
void clearAtoms() override {
_noDefinedAtoms.clear();
_noUndefinedAtoms.clear();
_noSharedLibraryAtoms.clear();
_noAbsoluteAtoms.clear();
}
protected:
std::error_code doParse() override {
// Make Archive object which will be owned by FileArchive object.
llvm::Error Err = llvm::Error::success();
_archive.reset(new Archive(_mb->getMemBufferRef(), Err));
if (Err)
return errorToErrorCode(std::move(Err));
std::error_code ec;
if ((ec = buildTableOfContents()))
return ec;
return std::error_code();
}
private:
std::error_code instantiateMember(Archive::Child member,
std::unique_ptr<File> &result) const {
Expected<llvm::MemoryBufferRef> mbOrErr = member.getMemoryBufferRef();
if (!mbOrErr)
return errorToErrorCode(mbOrErr.takeError());
llvm::MemoryBufferRef mb = mbOrErr.get();
std::string memberPath = (_archive->getFileName() + "("
+ mb.getBufferIdentifier() + ")").str();
if (_logLoading)
llvm::errs() << memberPath << "\n";
std::unique_ptr<MemoryBuffer> memberMB(MemoryBuffer::getMemBuffer(
mb.getBuffer(), mb.getBufferIdentifier(), false));
ErrorOr<std::unique_ptr<File>> fileOrErr =
_registry.loadFile(std::move(memberMB));
if (std::error_code ec = fileOrErr.getError())
return ec;
result = std::move(fileOrErr.get());
if (std::error_code ec = result->parse())
return ec;
result->setArchivePath(_archive->getFileName());
// The memory buffer is co-owned by the archive file and the children,
// so that the bufffer is deallocated when all the members are destructed.
result->setSharedMemoryBuffer(_mb);
return std::error_code();
}
std::error_code buildTableOfContents() {
DEBUG_WITH_TYPE("FileArchive", llvm::dbgs()
<< "Table of contents for archive '"
<< _archive->getFileName() << "':\n");
for (const Archive::Symbol &sym : _archive->symbols()) {
StringRef name = sym.getName();
Expected<Archive::Child> memberOrErr = sym.getMember();
if (!memberOrErr)
return errorToErrorCode(memberOrErr.takeError());
Archive::Child member = memberOrErr.get();
DEBUG_WITH_TYPE("FileArchive",
llvm::dbgs()
<< llvm::format("0x%08llX ",
member.getBuffer()->data())
<< "'" << name << "'\n");
_symbolMemberMap.insert(std::make_pair(name, member));
}
return std::error_code();
}
typedef std::unordered_map<StringRef, Archive::Child> MemberMap;
typedef std::set<const char *> InstantiatedSet;
std::shared_ptr<MemoryBuffer> _mb;
const Registry &_registry;
std::unique_ptr<Archive> _archive;
MemberMap _symbolMemberMap;
InstantiatedSet _membersInstantiated;
bool _logLoading;
std::vector<std::unique_ptr<MemoryBuffer>> _memberBuffers;
std::vector<std::unique_ptr<File>> _filesReturned;
};
class ArchiveReader : public Reader {
public:
ArchiveReader(bool logLoading) : _logLoading(logLoading) {}
bool canParse(file_magic magic, MemoryBufferRef) const override {
return magic == file_magic::archive;
}
ErrorOr<std::unique_ptr<File>> loadFile(std::unique_ptr<MemoryBuffer> mb,
const Registry &reg) const override {
StringRef path = mb->getBufferIdentifier();
std::unique_ptr<File> ret =
llvm::make_unique<FileArchive>(std::move(mb), reg, path, _logLoading);
return std::move(ret);
}
private:
bool _logLoading;
};
} // anonymous namespace
void Registry::addSupportArchives(bool logLoading) {
add(std::unique_ptr<Reader>(new ArchiveReader(logLoading)));
}
} // namespace lld

View File

@@ -0,0 +1,172 @@
//===- lib/FileFormat/MachO/ArchHandler.cpp -------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "ArchHandler.h"
#include "Atoms.h"
#include "MachONormalizedFileBinaryUtils.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Support/ErrorHandling.h"
using namespace llvm::MachO;
using namespace lld::mach_o::normalized;
namespace lld {
namespace mach_o {
ArchHandler::ArchHandler() {
}
ArchHandler::~ArchHandler() {
}
std::unique_ptr<mach_o::ArchHandler> ArchHandler::create(
MachOLinkingContext::Arch arch) {
switch (arch) {
case MachOLinkingContext::arch_x86_64:
return create_x86_64();
case MachOLinkingContext::arch_x86:
return create_x86();
case MachOLinkingContext::arch_armv6:
case MachOLinkingContext::arch_armv7:
case MachOLinkingContext::arch_armv7s:
return create_arm();
case MachOLinkingContext::arch_arm64:
return create_arm64();
default:
llvm_unreachable("Unknown arch");
}
}
bool ArchHandler::isLazyPointer(const Reference &ref) {
// A lazy bind entry is needed for a lazy pointer.
const StubInfo &info = stubInfo();
if (ref.kindNamespace() != Reference::KindNamespace::mach_o)
return false;
if (ref.kindArch() != info.lazyPointerReferenceToFinal.arch)
return false;
return (ref.kindValue() == info.lazyPointerReferenceToFinal.kind);
}
ArchHandler::RelocPattern ArchHandler::relocPattern(const Relocation &reloc) {
assert((reloc.type & 0xFFF0) == 0);
uint16_t result = reloc.type;
if (reloc.scattered)
result |= rScattered;
if (reloc.pcRel)
result |= rPcRel;
if (reloc.isExtern)
result |= rExtern;
switch(reloc.length) {
case 0:
break;
case 1:
result |= rLength2;
break;
case 2:
result |= rLength4;
break;
case 3:
result |= rLength8;
break;
default:
llvm_unreachable("bad r_length");
}
return result;
}
normalized::Relocation
ArchHandler::relocFromPattern(ArchHandler::RelocPattern pattern) {
normalized::Relocation result;
result.offset = 0;
result.scattered = (pattern & rScattered);
result.type = (RelocationInfoType)(pattern & 0xF);
result.pcRel = (pattern & rPcRel);
result.isExtern = (pattern & rExtern);
result.value = 0;
result.symbol = 0;
switch (pattern & 0x300) {
case rLength1:
result.length = 0;
break;
case rLength2:
result.length = 1;
break;
case rLength4:
result.length = 2;
break;
case rLength8:
result.length = 3;
break;
}
return result;
}
void ArchHandler::appendReloc(normalized::Relocations &relocs, uint32_t offset,
uint32_t symbol, uint32_t value,
RelocPattern pattern) {
normalized::Relocation reloc = relocFromPattern(pattern);
reloc.offset = offset;
reloc.symbol = symbol;
reloc.value = value;
relocs.push_back(reloc);
}
int16_t ArchHandler::readS16(const uint8_t *addr, bool isBig) {
return read16(addr, isBig);
}
int32_t ArchHandler::readS32(const uint8_t *addr, bool isBig) {
return read32(addr, isBig);
}
uint32_t ArchHandler::readU32(const uint8_t *addr, bool isBig) {
return read32(addr, isBig);
}
int64_t ArchHandler::readS64(const uint8_t *addr, bool isBig) {
return read64(addr, isBig);
}
bool ArchHandler::isDwarfCIE(bool isBig, const DefinedAtom *atom) {
assert(atom->contentType() == DefinedAtom::typeCFI);
if (atom->rawContent().size() < sizeof(uint32_t))
return false;
uint32_t size = read32(atom->rawContent().data(), isBig);
uint32_t idOffset = sizeof(uint32_t);
if (size == 0xffffffffU)
idOffset += sizeof(uint64_t);
return read32(atom->rawContent().data() + idOffset, isBig) == 0;
}
const Atom *ArchHandler::fdeTargetFunction(const DefinedAtom *fde) {
for (auto ref : *fde) {
if (ref->kindNamespace() == Reference::KindNamespace::mach_o &&
ref->kindValue() == unwindRefToFunctionKind()) {
assert(ref->kindArch() == kindArch() && "unexpected Reference arch");
return ref->target();
}
}
return nullptr;
}
} // namespace mach_o
} // namespace lld

View File

@@ -0,0 +1,323 @@
//===- lib/FileFormat/MachO/ArchHandler.h ---------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLD_READER_WRITER_MACHO_ARCH_HANDLER_H
#define LLD_READER_WRITER_MACHO_ARCH_HANDLER_H
#include "Atoms.h"
#include "File.h"
#include "MachONormalizedFile.h"
#include "lld/Common/LLVM.h"
#include "lld/Core/Error.h"
#include "lld/Core/Reference.h"
#include "lld/Core/Simple.h"
#include "lld/ReaderWriter/MachOLinkingContext.h"
#include "llvm/ADT/Triple.h"
namespace lld {
namespace mach_o {
///
/// The ArchHandler class handles all architecture specific aspects of
/// mach-o linking.
///
class ArchHandler {
public:
virtual ~ArchHandler();
/// There is no public interface to subclasses of ArchHandler, so this
/// is the only way to instantiate an ArchHandler.
static std::unique_ptr<ArchHandler> create(MachOLinkingContext::Arch arch);
/// Get (arch specific) kind strings used by Registry.
virtual const Registry::KindStrings *kindStrings() = 0;
/// Convert mach-o Arch to Reference::KindArch.
virtual Reference::KindArch kindArch() = 0;
/// Used by StubPass to update References to shared library functions
/// to be references to a stub.
virtual bool isCallSite(const Reference &) = 0;
/// Used by GOTPass to locate GOT References
virtual bool isGOTAccess(const Reference &, bool &canBypassGOT) {
return false;
}
/// Used by TLVPass to locate TLV References.
virtual bool isTLVAccess(const Reference &) const { return false; }
/// Used by the TLVPass to update TLV References.
virtual void updateReferenceToTLV(const Reference *) {}
/// Used by ShimPass to insert shims in branches that switch mode.
virtual bool isNonCallBranch(const Reference &) = 0;
/// Used by GOTPass to update GOT References
virtual void updateReferenceToGOT(const Reference *, bool targetIsNowGOT) {}
/// Does this architecture make use of __unwind_info sections for exception
/// handling? If so, it will need a separate pass to create them.
virtual bool needsCompactUnwind() = 0;
/// Returns the kind of reference to use to synthesize a 32-bit image-offset
/// value, used in the __unwind_info section.
virtual Reference::KindValue imageOffsetKind() = 0;
/// Returns the kind of reference to use to synthesize a 32-bit image-offset
/// indirect value. Used for personality functions in the __unwind_info
/// section.
virtual Reference::KindValue imageOffsetKindIndirect() = 0;
/// Architecture specific compact unwind type that signals __eh_frame should
/// actually be used.
virtual uint32_t dwarfCompactUnwindType() = 0;
/// Reference from an __eh_frame CIE atom to its personality function it's
/// describing. Usually pointer-sized and PC-relative, but differs in whether
/// it needs to be in relocatable objects.
virtual Reference::KindValue unwindRefToPersonalityFunctionKind() = 0;
/// Reference from an __eh_frame FDE to the CIE it's based on.
virtual Reference::KindValue unwindRefToCIEKind() = 0;
/// Reference from an __eh_frame FDE atom to the function it's
/// describing. Usually pointer-sized and PC-relative, but differs in whether
/// it needs to be in relocatable objects.
virtual Reference::KindValue unwindRefToFunctionKind() = 0;
/// Reference from an __unwind_info entry of dwarfCompactUnwindType to the
/// required __eh_frame entry. On current architectures, the low 24 bits
/// represent the offset of the function's FDE entry from the start of
/// __eh_frame.
virtual Reference::KindValue unwindRefToEhFrameKind() = 0;
/// Returns a pointer sized reference kind. On 64-bit targets this will
/// likely be something like pointer64, and pointer32 on 32-bit targets.
virtual Reference::KindValue pointerKind() = 0;
virtual const Atom *fdeTargetFunction(const DefinedAtom *fde);
/// Used by normalizedFromAtoms() to know where to generated rebasing and
/// binding info in final executables.
virtual bool isPointer(const Reference &) = 0;
/// Used by normalizedFromAtoms() to know where to generated lazy binding
/// info in final executables.
virtual bool isLazyPointer(const Reference &);
/// Reference from an __stub_helper entry to the required offset of the
/// lazy bind commands.
virtual Reference::KindValue lazyImmediateLocationKind() = 0;
/// Returns true if the specified relocation is paired to the next relocation.
virtual bool isPairedReloc(const normalized::Relocation &) = 0;
/// Prototype for a helper function. Given a sectionIndex and address,
/// finds the atom and offset with that atom of that address.
typedef std::function<llvm::Error (uint32_t sectionIndex, uint64_t addr,
const lld::Atom **, Reference::Addend *)>
FindAtomBySectionAndAddress;
/// Prototype for a helper function. Given a symbolIndex, finds the atom
/// representing that symbol.
typedef std::function<llvm::Error (uint32_t symbolIndex,
const lld::Atom **)> FindAtomBySymbolIndex;
/// Analyzes a relocation from a .o file and returns the info
/// (kind, target, addend) needed to instantiate a Reference.
/// Two helper functions are passed as parameters to find the target atom
/// given a symbol index or address.
virtual llvm::Error
getReferenceInfo(const normalized::Relocation &reloc,
const DefinedAtom *inAtom,
uint32_t offsetInAtom,
uint64_t fixupAddress, bool isBigEndian,
FindAtomBySectionAndAddress atomFromAddress,
FindAtomBySymbolIndex atomFromSymbolIndex,
Reference::KindValue *kind,
const lld::Atom **target,
Reference::Addend *addend) = 0;
/// Analyzes a pair of relocations from a .o file and returns the info
/// (kind, target, addend) needed to instantiate a Reference.
/// Two helper functions are passed as parameters to find the target atom
/// given a symbol index or address.
virtual llvm::Error
getPairReferenceInfo(const normalized::Relocation &reloc1,
const normalized::Relocation &reloc2,
const DefinedAtom *inAtom,
uint32_t offsetInAtom,
uint64_t fixupAddress, bool isBig, bool scatterable,
FindAtomBySectionAndAddress atomFromAddress,
FindAtomBySymbolIndex atomFromSymbolIndex,
Reference::KindValue *kind,
const lld::Atom **target,
Reference::Addend *addend) = 0;
/// Prototype for a helper function. Given an atom, finds the symbol table
/// index for it in the output file.
typedef std::function<uint32_t (const Atom &atom)> FindSymbolIndexForAtom;
/// Prototype for a helper function. Given an atom, finds the index
/// of the section that will contain the atom.
typedef std::function<uint32_t (const Atom &atom)> FindSectionIndexForAtom;
/// Prototype for a helper function. Given an atom, finds the address
/// assigned to it in the output file.
typedef std::function<uint64_t (const Atom &atom)> FindAddressForAtom;
/// Some architectures require local symbols on anonymous atoms.
virtual bool needsLocalSymbolInRelocatableFile(const DefinedAtom *atom) {
return false;
}
/// Copy raw content then apply all fixup References on an Atom.
virtual void generateAtomContent(const DefinedAtom &atom, bool relocatable,
FindAddressForAtom findAddress,
FindAddressForAtom findSectionAddress,
uint64_t imageBaseAddress,
llvm::MutableArrayRef<uint8_t> atomContentBuffer) = 0;
/// Used in -r mode to convert a Reference to a mach-o relocation.
virtual void appendSectionRelocations(const DefinedAtom &atom,
uint64_t atomSectionOffset,
const Reference &ref,
FindSymbolIndexForAtom,
FindSectionIndexForAtom,
FindAddressForAtom,
normalized::Relocations&) = 0;
/// Add arch-specific References.
virtual void addAdditionalReferences(MachODefinedAtom &atom) { }
// Add Reference for data-in-code marker.
virtual void addDataInCodeReference(MachODefinedAtom &atom, uint32_t atomOff,
uint16_t length, uint16_t kind) { }
/// Returns true if the specificed Reference value marks the start or end
/// of a data-in-code range in an atom.
virtual bool isDataInCodeTransition(Reference::KindValue refKind) {
return false;
}
/// Returns the Reference value for a Reference that marks that start of
/// a data-in-code range.
virtual Reference::KindValue dataInCodeTransitionStart(
const MachODefinedAtom &atom) {
return 0;
}
/// Returns the Reference value for a Reference that marks that end of
/// a data-in-code range.
virtual Reference::KindValue dataInCodeTransitionEnd(
const MachODefinedAtom &atom) {
return 0;
}
/// Only relevant for 32-bit arm archs.
virtual bool isThumbFunction(const DefinedAtom &atom) { return false; }
/// Only relevant for 32-bit arm archs.
virtual const DefinedAtom *createShim(MachOFile &file, bool thumbToArm,
const DefinedAtom &) {
llvm_unreachable("shims only support on arm");
}
/// Does a given unwind-cfi atom represent a CIE (as opposed to an FDE).
static bool isDwarfCIE(bool isBig, const DefinedAtom *atom);
struct ReferenceInfo {
Reference::KindArch arch;
uint16_t kind;
uint32_t offset;
int32_t addend;
};
struct OptionalRefInfo {
bool used;
uint16_t kind;
uint32_t offset;
int32_t addend;
};
/// Table of architecture specific information for creating stubs.
struct StubInfo {
const char* binderSymbolName;
ReferenceInfo lazyPointerReferenceToHelper;
ReferenceInfo lazyPointerReferenceToFinal;
ReferenceInfo nonLazyPointerReferenceToBinder;
uint8_t codeAlignment;
uint32_t stubSize;
uint8_t stubBytes[16];
ReferenceInfo stubReferenceToLP;
OptionalRefInfo optStubReferenceToLP;
uint32_t stubHelperSize;
uint8_t stubHelperBytes[16];
ReferenceInfo stubHelperReferenceToImm;
ReferenceInfo stubHelperReferenceToHelperCommon;
DefinedAtom::ContentType stubHelperImageCacheContentType;
uint32_t stubHelperCommonSize;
uint8_t stubHelperCommonAlignment;
uint8_t stubHelperCommonBytes[36];
ReferenceInfo stubHelperCommonReferenceToCache;
OptionalRefInfo optStubHelperCommonReferenceToCache;
ReferenceInfo stubHelperCommonReferenceToBinder;
OptionalRefInfo optStubHelperCommonReferenceToBinder;
};
virtual const StubInfo &stubInfo() = 0;
protected:
ArchHandler();
static std::unique_ptr<mach_o::ArchHandler> create_x86_64();
static std::unique_ptr<mach_o::ArchHandler> create_x86();
static std::unique_ptr<mach_o::ArchHandler> create_arm();
static std::unique_ptr<mach_o::ArchHandler> create_arm64();
// Handy way to pack mach-o r_type and other bit fields into one 16-bit value.
typedef uint16_t RelocPattern;
enum {
rScattered = 0x8000,
rPcRel = 0x4000,
rExtern = 0x2000,
rLength1 = 0x0000,
rLength2 = 0x0100,
rLength4 = 0x0200,
rLength8 = 0x0300,
rLenArmLo = rLength1,
rLenArmHi = rLength2,
rLenThmbLo = rLength4,
rLenThmbHi = rLength8
};
/// Extract RelocPattern from normalized mach-o relocation.
static RelocPattern relocPattern(const normalized::Relocation &reloc);
/// Create normalized Relocation initialized from pattern.
static normalized::Relocation relocFromPattern(RelocPattern pattern);
/// One liner to add a relocation.
static void appendReloc(normalized::Relocations &relocs, uint32_t offset,
uint32_t symbol, uint32_t value,
RelocPattern pattern);
static int16_t readS16(const uint8_t *addr, bool isBig);
static int32_t readS32(const uint8_t *addr, bool isBig);
static uint32_t readU32(const uint8_t *addr, bool isBig);
static int64_t readS64(const uint8_t *addr, bool isBig);
};
} // namespace mach_o
} // namespace lld
#endif // LLD_READER_WRITER_MACHO_ARCH_HANDLER_H

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More