2013-10-08 08:43:34 +08:00
|
|
|
//===- lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp -----------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-10-08 08:43:34 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
///
|
|
|
|
/// \file For mach-o object files, this implementation uses YAML I/O to
|
|
|
|
/// provide the convert between YAML and the normalized mach-o (NM).
|
|
|
|
///
|
2014-01-27 11:09:26 +08:00
|
|
|
/// +------------+ +------+
|
|
|
|
/// | normalized | <-> | yaml |
|
|
|
|
/// +------------+ +------+
|
2013-10-08 08:43:34 +08:00
|
|
|
|
|
|
|
#include "MachONormalizedFile.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2013-10-08 08:43:34 +08:00
|
|
|
#include "lld/Core/Error.h"
|
[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
|
|
|
#include "lld/ReaderWriter/YamlContext.h"
|
2013-10-08 08:43:34 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/MachO.h"
|
2013-10-08 08:43:34 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2014-11-19 10:21:53 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2013-10-08 08:43:34 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2014-06-13 01:12:28 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-06-13 01:15:58 +08:00
|
|
|
#include <system_error>
|
2013-10-08 08:43:34 +08:00
|
|
|
|
|
|
|
using llvm::StringRef;
|
2013-11-07 05:36:55 +08:00
|
|
|
using namespace llvm::yaml;
|
|
|
|
using namespace llvm::MachO;
|
|
|
|
using namespace lld::mach_o::normalized;
|
[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
|
|
|
using lld::YamlContext;
|
2013-10-15 05:24:48 +08:00
|
|
|
|
2013-12-28 08:46:57 +08:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(Segment)
|
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(DependentDylib)
|
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(RebaseLocation)
|
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(BindLocation)
|
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(Export)
|
2014-07-25 07:06:56 +08:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(DataInCode)
|
2013-10-15 05:24:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
// for compatibility with gcc-4.7 in C++11 mode, add extra namespace
|
|
|
|
namespace llvm {
|
|
|
|
namespace yaml {
|
|
|
|
|
2013-10-08 08:43:34 +08:00
|
|
|
// A vector of Sections is a sequence.
|
|
|
|
template<>
|
|
|
|
struct SequenceTraits< std::vector<Section> > {
|
|
|
|
static size_t size(IO &io, std::vector<Section> &seq) {
|
|
|
|
return seq.size();
|
|
|
|
}
|
|
|
|
static Section& element(IO &io, std::vector<Section> &seq, size_t index) {
|
|
|
|
if ( index >= seq.size() )
|
|
|
|
seq.resize(index+1);
|
|
|
|
return seq[index];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct SequenceTraits< std::vector<Symbol> > {
|
|
|
|
static size_t size(IO &io, std::vector<Symbol> &seq) {
|
|
|
|
return seq.size();
|
|
|
|
}
|
|
|
|
static Symbol& element(IO &io, std::vector<Symbol> &seq, size_t index) {
|
|
|
|
if ( index >= seq.size() )
|
|
|
|
seq.resize(index+1);
|
|
|
|
return seq[index];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// A vector of Relocations is a sequence.
|
|
|
|
template<>
|
|
|
|
struct SequenceTraits< Relocations > {
|
|
|
|
static size_t size(IO &io, Relocations &seq) {
|
|
|
|
return seq.size();
|
|
|
|
}
|
|
|
|
static Relocation& element(IO &io, Relocations &seq, size_t index) {
|
|
|
|
if ( index >= seq.size() )
|
|
|
|
seq.resize(index+1);
|
|
|
|
return seq[index];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The content for a section is represented as a flow sequence of hex bytes.
|
|
|
|
template<>
|
|
|
|
struct SequenceTraits< ContentBytes > {
|
|
|
|
static size_t size(IO &io, ContentBytes &seq) {
|
|
|
|
return seq.size();
|
|
|
|
}
|
|
|
|
static Hex8& element(IO &io, ContentBytes &seq, size_t index) {
|
|
|
|
if ( index >= seq.size() )
|
|
|
|
seq.resize(index+1);
|
|
|
|
return seq[index];
|
|
|
|
}
|
|
|
|
static const bool flow = true;
|
|
|
|
};
|
|
|
|
|
2014-01-11 09:07:43 +08:00
|
|
|
// The indirect symbols for a section is represented as a flow sequence
|
2013-10-08 08:43:34 +08:00
|
|
|
// of numbers (symbol table indexes).
|
|
|
|
template<>
|
|
|
|
struct SequenceTraits< IndirectSymbols > {
|
|
|
|
static size_t size(IO &io, IndirectSymbols &seq) {
|
|
|
|
return seq.size();
|
|
|
|
}
|
|
|
|
static uint32_t& element(IO &io, IndirectSymbols &seq, size_t index) {
|
|
|
|
if ( index >= seq.size() )
|
|
|
|
seq.resize(index+1);
|
|
|
|
return seq[index];
|
|
|
|
}
|
|
|
|
static const bool flow = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<lld::MachOLinkingContext::Arch> {
|
|
|
|
static void enumeration(IO &io, lld::MachOLinkingContext::Arch &value) {
|
|
|
|
io.enumCase(value, "unknown",lld::MachOLinkingContext::arch_unknown);
|
|
|
|
io.enumCase(value, "ppc", lld::MachOLinkingContext::arch_ppc);
|
|
|
|
io.enumCase(value, "x86", lld::MachOLinkingContext::arch_x86);
|
|
|
|
io.enumCase(value, "x86_64", lld::MachOLinkingContext::arch_x86_64);
|
|
|
|
io.enumCase(value, "armv6", lld::MachOLinkingContext::arch_armv6);
|
|
|
|
io.enumCase(value, "armv7", lld::MachOLinkingContext::arch_armv7);
|
|
|
|
io.enumCase(value, "armv7s", lld::MachOLinkingContext::arch_armv7s);
|
2014-09-10 07:52:59 +08:00
|
|
|
io.enumCase(value, "arm64", lld::MachOLinkingContext::arch_arm64);
|
2013-10-08 08:43:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<lld::MachOLinkingContext::OS> {
|
|
|
|
static void enumeration(IO &io, lld::MachOLinkingContext::OS &value) {
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "unknown",
|
2013-10-08 08:43:34 +08:00
|
|
|
lld::MachOLinkingContext::OS::unknown);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "Mac OS X",
|
2013-10-08 08:43:34 +08:00
|
|
|
lld::MachOLinkingContext::OS::macOSX);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "iOS",
|
2013-10-08 08:43:34 +08:00
|
|
|
lld::MachOLinkingContext::OS::iOS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "iOS Simulator",
|
2013-10-08 08:43:34 +08:00
|
|
|
lld::MachOLinkingContext::OS::iOS_simulator);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<HeaderFileType> {
|
|
|
|
static void enumeration(IO &io, HeaderFileType &value) {
|
2014-01-08 09:38:07 +08:00
|
|
|
io.enumCase(value, "MH_OBJECT", llvm::MachO::MH_OBJECT);
|
|
|
|
io.enumCase(value, "MH_DYLIB", llvm::MachO::MH_DYLIB);
|
|
|
|
io.enumCase(value, "MH_EXECUTE", llvm::MachO::MH_EXECUTE);
|
|
|
|
io.enumCase(value, "MH_BUNDLE", llvm::MachO::MH_BUNDLE);
|
2013-10-08 08:43:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarBitSetTraits<FileFlags> {
|
|
|
|
static void bitset(IO &io, FileFlags &value) {
|
2014-01-27 11:09:26 +08:00
|
|
|
io.bitSetCase(value, "MH_TWOLEVEL",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::MH_TWOLEVEL);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.bitSetCase(value, "MH_SUBSECTIONS_VIA_SYMBOLS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::MH_SUBSECTIONS_VIA_SYMBOLS);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<SectionType> {
|
|
|
|
static void enumeration(IO &io, SectionType &value) {
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_REGULAR",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_REGULAR);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_ZEROFILL",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_ZEROFILL);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_CSTRING_LITERALS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_CSTRING_LITERALS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_4BYTE_LITERALS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_4BYTE_LITERALS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_8BYTE_LITERALS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_8BYTE_LITERALS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_LITERAL_POINTERS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_LITERAL_POINTERS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_NON_LAZY_SYMBOL_POINTERS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_NON_LAZY_SYMBOL_POINTERS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_LAZY_SYMBOL_POINTERS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_LAZY_SYMBOL_POINTERS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_SYMBOL_STUBS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_SYMBOL_STUBS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_MOD_INIT_FUNC_POINTERS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_MOD_INIT_FUNC_POINTERS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_MOD_TERM_FUNC_POINTERS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_MOD_TERM_FUNC_POINTERS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_COALESCED",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_COALESCED);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_GB_ZEROFILL",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_GB_ZEROFILL);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_INTERPOSING",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_INTERPOSING);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_16BYTE_LITERALS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_16BYTE_LITERALS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_DTRACE_DOF",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_DTRACE_DOF);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_LAZY_DYLIB_SYMBOL_POINTERS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_LAZY_DYLIB_SYMBOL_POINTERS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_THREAD_LOCAL_REGULAR",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_THREAD_LOCAL_REGULAR);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_THREAD_LOCAL_ZEROFILL",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_THREAD_LOCAL_ZEROFILL);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_THREAD_LOCAL_VARIABLES",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_THREAD_LOCAL_VARIABLES);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_THREAD_LOCAL_VARIABLE_POINTERS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_THREAD_LOCAL_VARIABLE_POINTERS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "S_THREAD_LOCAL_INIT_FUNCTION_POINTERS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarBitSetTraits<SectionAttr> {
|
|
|
|
static void bitset(IO &io, SectionAttr &value) {
|
2014-01-27 11:09:26 +08:00
|
|
|
io.bitSetCase(value, "S_ATTR_PURE_INSTRUCTIONS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_ATTR_PURE_INSTRUCTIONS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.bitSetCase(value, "S_ATTR_SOME_INSTRUCTIONS",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_ATTR_SOME_INSTRUCTIONS);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.bitSetCase(value, "S_ATTR_NO_DEAD_STRIP",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_ATTR_NO_DEAD_STRIP);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.bitSetCase(value, "S_ATTR_EXT_RELOC",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_ATTR_EXT_RELOC);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.bitSetCase(value, "S_ATTR_LOC_RELOC",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::S_ATTR_LOC_RELOC);
|
2016-07-27 01:17:17 +08:00
|
|
|
io.bitSetCase(value, "S_ATTR_DEBUG",
|
|
|
|
llvm::MachO::S_ATTR_DEBUG);
|
2013-10-08 08:43:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-24 08:36:37 +08:00
|
|
|
/// This is a custom formatter for SectionAlignment. Values are
|
|
|
|
/// the power to raise by, ie, the n in 2^n.
|
|
|
|
template <> struct ScalarTraits<SectionAlignment> {
|
|
|
|
static void output(const SectionAlignment &value, void *ctxt,
|
|
|
|
raw_ostream &out) {
|
|
|
|
out << llvm::format("%d", (uint32_t)value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringRef input(StringRef scalar, void *ctxt,
|
|
|
|
SectionAlignment &value) {
|
|
|
|
uint32_t alignment;
|
|
|
|
if (scalar.getAsInteger(0, alignment)) {
|
|
|
|
return "malformed alignment value";
|
|
|
|
}
|
|
|
|
if (!llvm::isPowerOf2_32(alignment))
|
|
|
|
return "alignment must be a power of 2";
|
|
|
|
value = alignment;
|
|
|
|
return StringRef(); // returning empty string means success
|
|
|
|
}
|
|
|
|
|
2017-12-19 01:49:27 +08:00
|
|
|
static QuotingType mustQuote(StringRef) { return QuotingType::None; }
|
2016-03-24 08:36:37 +08:00
|
|
|
};
|
|
|
|
|
2013-10-08 08:43:34 +08:00
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<NListType> {
|
|
|
|
static void enumeration(IO &io, NListType &value) {
|
|
|
|
io.enumCase(value, "N_UNDF", llvm::MachO::N_UNDF);
|
|
|
|
io.enumCase(value, "N_ABS", llvm::MachO::N_ABS);
|
|
|
|
io.enumCase(value, "N_SECT", llvm::MachO::N_SECT);
|
2014-01-04 09:22:05 +08:00
|
|
|
io.enumCase(value, "N_PBUD", llvm::MachO::N_PBUD);
|
|
|
|
io.enumCase(value, "N_INDR", llvm::MachO::N_INDR);
|
2013-10-08 08:43:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarBitSetTraits<SymbolScope> {
|
|
|
|
static void bitset(IO &io, SymbolScope &value) {
|
|
|
|
io.bitSetCase(value, "N_EXT", llvm::MachO::N_EXT);
|
|
|
|
io.bitSetCase(value, "N_PEXT", llvm::MachO::N_PEXT);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarBitSetTraits<SymbolDesc> {
|
|
|
|
static void bitset(IO &io, SymbolDesc &value) {
|
|
|
|
io.bitSetCase(value, "N_NO_DEAD_STRIP", llvm::MachO::N_NO_DEAD_STRIP);
|
|
|
|
io.bitSetCase(value, "N_WEAK_REF", llvm::MachO::N_WEAK_REF);
|
|
|
|
io.bitSetCase(value, "N_WEAK_DEF", llvm::MachO::N_WEAK_DEF);
|
|
|
|
io.bitSetCase(value, "N_ARM_THUMB_DEF", llvm::MachO::N_ARM_THUMB_DEF);
|
|
|
|
io.bitSetCase(value, "N_SYMBOL_RESOLVER", llvm::MachO::N_SYMBOL_RESOLVER);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MappingTraits<Section> {
|
2014-01-04 07:12:02 +08:00
|
|
|
struct NormalizedContentBytes;
|
2013-10-08 08:43:34 +08:00
|
|
|
static void mapping(IO &io, Section §) {
|
|
|
|
io.mapRequired("segment", sect.segmentName);
|
|
|
|
io.mapRequired("section", sect.sectionName);
|
|
|
|
io.mapRequired("type", sect.type);
|
|
|
|
io.mapOptional("attributes", sect.attributes);
|
2016-03-24 08:36:37 +08:00
|
|
|
io.mapOptional("alignment", sect.alignment, (SectionAlignment)1);
|
2013-10-08 08:43:34 +08:00
|
|
|
io.mapRequired("address", sect.address);
|
2015-12-12 07:25:09 +08:00
|
|
|
if (isZeroFillSection(sect.type)) {
|
2014-05-16 07:03:50 +08:00
|
|
|
// S_ZEROFILL sections use "size:" instead of "content:"
|
|
|
|
uint64_t size = sect.content.size();
|
|
|
|
io.mapOptional("size", size);
|
|
|
|
if (!io.outputting()) {
|
|
|
|
uint8_t *bytes = nullptr;
|
|
|
|
sect.content = makeArrayRef(bytes, size);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MappingNormalization<NormalizedContent, ArrayRef<uint8_t>> content(
|
2014-01-04 07:12:02 +08:00
|
|
|
io, sect.content);
|
2014-05-16 07:03:50 +08:00
|
|
|
io.mapOptional("content", content->_normalizedContent);
|
|
|
|
}
|
2013-10-08 08:43:34 +08:00
|
|
|
io.mapOptional("relocations", sect.relocations);
|
|
|
|
io.mapOptional("indirect-syms", sect.indirectSymbols);
|
|
|
|
}
|
2014-01-04 07:12:02 +08:00
|
|
|
|
|
|
|
struct NormalizedContent {
|
2014-01-11 09:07:43 +08:00
|
|
|
NormalizedContent(IO &io) : _io(io) {}
|
|
|
|
NormalizedContent(IO &io, ArrayRef<uint8_t> content) : _io(io) {
|
|
|
|
// When writing yaml, copy content byte array to Hex8 vector.
|
2014-01-04 07:12:02 +08:00
|
|
|
for (auto &c : content) {
|
2014-01-11 09:07:43 +08:00
|
|
|
_normalizedContent.push_back(c);
|
2014-01-04 07:12:02 +08:00
|
|
|
}
|
|
|
|
}
|
2014-01-11 09:07:43 +08:00
|
|
|
ArrayRef<uint8_t> denormalize(IO &io) {
|
|
|
|
// When reading yaml, allocate byte array owned by NormalizedFile and
|
|
|
|
// copy Hex8 vector to byte array.
|
|
|
|
YamlContext *info = reinterpret_cast<YamlContext *>(io.getContext());
|
|
|
|
assert(info != nullptr);
|
|
|
|
NormalizedFile *file = info->_normalizeMachOFile;
|
|
|
|
assert(file != nullptr);
|
|
|
|
size_t size = _normalizedContent.size();
|
2016-03-24 09:16:06 +08:00
|
|
|
if (!size)
|
2016-03-25 01:39:34 +08:00
|
|
|
return None;
|
2014-01-11 09:07:43 +08:00
|
|
|
uint8_t *bytes = file->ownedAllocations.Allocate<uint8_t>(size);
|
|
|
|
std::copy(_normalizedContent.begin(), _normalizedContent.end(), bytes);
|
|
|
|
return makeArrayRef(bytes, size);
|
2014-01-04 07:12:02 +08:00
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2014-01-11 09:07:43 +08:00
|
|
|
IO &_io;
|
|
|
|
ContentBytes _normalizedContent;
|
2014-01-04 07:12:02 +08:00
|
|
|
};
|
2013-10-08 08:43:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MappingTraits<Relocation> {
|
|
|
|
static void mapping(IO &io, Relocation &reloc) {
|
|
|
|
io.mapRequired("offset", reloc.offset);
|
|
|
|
io.mapOptional("scattered", reloc.scattered, false);
|
|
|
|
io.mapRequired("type", reloc.type);
|
|
|
|
io.mapRequired("length", reloc.length);
|
|
|
|
io.mapRequired("pc-rel", reloc.pcRel);
|
|
|
|
if ( !reloc.scattered )
|
|
|
|
io.mapRequired("extern", reloc.isExtern);
|
|
|
|
if ( reloc.scattered )
|
|
|
|
io.mapRequired("value", reloc.value);
|
|
|
|
if ( !reloc.scattered )
|
|
|
|
io.mapRequired("symbol", reloc.symbol);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<RelocationInfoType> {
|
|
|
|
static void enumeration(IO &io, RelocationInfoType &value) {
|
2013-12-20 15:48:29 +08:00
|
|
|
YamlContext *info = reinterpret_cast<YamlContext *>(io.getContext());
|
[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
|
|
|
assert(info != nullptr);
|
|
|
|
NormalizedFile *file = info->_normalizeMachOFile;
|
2013-10-08 08:43:34 +08:00
|
|
|
assert(file != nullptr);
|
|
|
|
switch (file->arch) {
|
|
|
|
case lld::MachOLinkingContext::arch_x86_64:
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "X86_64_RELOC_UNSIGNED",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::X86_64_RELOC_UNSIGNED);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "X86_64_RELOC_SIGNED",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::X86_64_RELOC_SIGNED);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "X86_64_RELOC_BRANCH",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::X86_64_RELOC_BRANCH);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "X86_64_RELOC_GOT_LOAD",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::X86_64_RELOC_GOT_LOAD);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "X86_64_RELOC_GOT",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::X86_64_RELOC_GOT);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "X86_64_RELOC_SUBTRACTOR",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::X86_64_RELOC_SUBTRACTOR);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "X86_64_RELOC_SIGNED_1",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::X86_64_RELOC_SIGNED_1);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "X86_64_RELOC_SIGNED_2",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::X86_64_RELOC_SIGNED_2);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "X86_64_RELOC_SIGNED_4",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::X86_64_RELOC_SIGNED_4);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "X86_64_RELOC_TLV",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::X86_64_RELOC_TLV);
|
|
|
|
break;
|
|
|
|
case lld::MachOLinkingContext::arch_x86:
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "GENERIC_RELOC_VANILLA",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::GENERIC_RELOC_VANILLA);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "GENERIC_RELOC_PAIR",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::GENERIC_RELOC_PAIR);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "GENERIC_RELOC_SECTDIFF",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::GENERIC_RELOC_SECTDIFF);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "GENERIC_RELOC_LOCAL_SECTDIFF",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::GENERIC_RELOC_LOCAL_SECTDIFF);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "GENERIC_RELOC_TLV",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::GENERIC_RELOC_TLV);
|
|
|
|
break;
|
|
|
|
case lld::MachOLinkingContext::arch_armv6:
|
|
|
|
case lld::MachOLinkingContext::arch_armv7:
|
|
|
|
case lld::MachOLinkingContext::arch_armv7s:
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "ARM_RELOC_VANILLA",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::ARM_RELOC_VANILLA);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "ARM_RELOC_PAIR",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::ARM_RELOC_PAIR);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "ARM_RELOC_SECTDIFF",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::ARM_RELOC_SECTDIFF);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "ARM_RELOC_LOCAL_SECTDIFF",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::ARM_RELOC_LOCAL_SECTDIFF);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "ARM_RELOC_BR24",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::ARM_RELOC_BR24);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "ARM_THUMB_RELOC_BR22",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::ARM_THUMB_RELOC_BR22);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "ARM_RELOC_HALF",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::ARM_RELOC_HALF);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "ARM_RELOC_HALF_SECTDIFF",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::ARM_RELOC_HALF_SECTDIFF);
|
|
|
|
break;
|
2014-09-10 07:52:59 +08:00
|
|
|
case lld::MachOLinkingContext::arch_arm64:
|
|
|
|
io.enumCase(value, "ARM64_RELOC_UNSIGNED",
|
|
|
|
llvm::MachO::ARM64_RELOC_UNSIGNED);
|
|
|
|
io.enumCase(value, "ARM64_RELOC_SUBTRACTOR",
|
|
|
|
llvm::MachO::ARM64_RELOC_SUBTRACTOR);
|
|
|
|
io.enumCase(value, "ARM64_RELOC_BRANCH26",
|
|
|
|
llvm::MachO::ARM64_RELOC_BRANCH26);
|
|
|
|
io.enumCase(value, "ARM64_RELOC_PAGE21",
|
|
|
|
llvm::MachO::ARM64_RELOC_PAGE21);
|
|
|
|
io.enumCase(value, "ARM64_RELOC_PAGEOFF12",
|
|
|
|
llvm::MachO::ARM64_RELOC_PAGEOFF12);
|
|
|
|
io.enumCase(value, "ARM64_RELOC_GOT_LOAD_PAGE21",
|
|
|
|
llvm::MachO::ARM64_RELOC_GOT_LOAD_PAGE21);
|
|
|
|
io.enumCase(value, "ARM64_RELOC_GOT_LOAD_PAGEOFF12",
|
|
|
|
llvm::MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12);
|
|
|
|
io.enumCase(value, "ARM64_RELOC_POINTER_TO_GOT",
|
|
|
|
llvm::MachO::ARM64_RELOC_POINTER_TO_GOT);
|
|
|
|
io.enumCase(value, "ARM64_RELOC_TLVP_LOAD_PAGE21",
|
|
|
|
llvm::MachO::ARM64_RELOC_TLVP_LOAD_PAGE21);
|
|
|
|
io.enumCase(value, "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
|
|
|
|
llvm::MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12);
|
|
|
|
io.enumCase(value, "ARM64_RELOC_ADDEND",
|
|
|
|
llvm::MachO::ARM64_RELOC_ADDEND);
|
|
|
|
break;
|
2013-10-08 08:43:34 +08:00
|
|
|
default:
|
2013-10-08 10:07:19 +08:00
|
|
|
llvm_unreachable("unknown architecture");
|
2013-10-08 08:43:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MappingTraits<Symbol> {
|
|
|
|
static void mapping(IO &io, Symbol& sym) {
|
|
|
|
io.mapRequired("name", sym.name);
|
|
|
|
io.mapRequired("type", sym.type);
|
|
|
|
io.mapOptional("scope", sym.scope, SymbolScope(0));
|
|
|
|
io.mapOptional("sect", sym.sect, (uint8_t)0);
|
2014-05-16 04:59:23 +08:00
|
|
|
if (sym.type == llvm::MachO::N_UNDF) {
|
|
|
|
// In undef symbols, desc field contains alignment/ordinal info
|
|
|
|
// which is better represented as a hex vaule.
|
|
|
|
uint16_t t1 = sym.desc;
|
|
|
|
Hex16 t2 = t1;
|
|
|
|
io.mapOptional("desc", t2, Hex16(0));
|
|
|
|
sym.desc = t2;
|
|
|
|
} else {
|
|
|
|
// In defined symbols, desc fit is a set of option bits.
|
|
|
|
io.mapOptional("desc", sym.desc, SymbolDesc(0));
|
|
|
|
}
|
|
|
|
io.mapRequired("value", sym.value);
|
2013-10-08 08:43:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Custom mapping for VMProtect (e.g. "r-x").
|
|
|
|
template <>
|
|
|
|
struct ScalarTraits<VMProtect> {
|
2013-11-05 09:37:40 +08:00
|
|
|
static void output(const VMProtect &value, void*, raw_ostream &out) {
|
2013-10-08 08:43:34 +08:00
|
|
|
out << ( (value & llvm::MachO::VM_PROT_READ) ? 'r' : '-');
|
|
|
|
out << ( (value & llvm::MachO::VM_PROT_WRITE) ? 'w' : '-');
|
|
|
|
out << ( (value & llvm::MachO::VM_PROT_EXECUTE) ? 'x' : '-');
|
|
|
|
}
|
|
|
|
static StringRef input(StringRef scalar, void*, VMProtect &value) {
|
|
|
|
value = 0;
|
|
|
|
if (scalar.size() != 3)
|
|
|
|
return "segment access protection must be three chars (e.g. \"r-x\")";
|
|
|
|
switch (scalar[0]) {
|
|
|
|
case 'r':
|
|
|
|
value = llvm::MachO::VM_PROT_READ;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return "segment access protection first char must be 'r' or '-'";
|
|
|
|
}
|
|
|
|
switch (scalar[1]) {
|
|
|
|
case 'w':
|
|
|
|
value = value | llvm::MachO::VM_PROT_WRITE;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return "segment access protection second char must be 'w' or '-'";
|
|
|
|
}
|
|
|
|
switch (scalar[2]) {
|
|
|
|
case 'x':
|
|
|
|
value = value | llvm::MachO::VM_PROT_EXECUTE;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return "segment access protection third char must be 'x' or '-'";
|
|
|
|
}
|
|
|
|
// Return the empty string on success,
|
|
|
|
return StringRef();
|
|
|
|
}
|
2017-12-19 01:49:27 +08:00
|
|
|
static QuotingType mustQuote(StringRef) { return QuotingType::None; }
|
2013-10-08 08:43:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MappingTraits<Segment> {
|
|
|
|
static void mapping(IO &io, Segment& seg) {
|
2016-02-06 08:51:16 +08:00
|
|
|
io.mapRequired("name", seg.name);
|
|
|
|
io.mapRequired("address", seg.address);
|
|
|
|
io.mapRequired("size", seg.size);
|
|
|
|
io.mapRequired("init-access", seg.init_access);
|
|
|
|
io.mapRequired("max-access", seg.max_access);
|
2013-10-08 08:43:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<LoadCommandType> {
|
|
|
|
static void enumeration(IO &io, LoadCommandType &value) {
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "LC_LOAD_DYLIB",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::LC_LOAD_DYLIB);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "LC_LOAD_WEAK_DYLIB",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::LC_LOAD_WEAK_DYLIB);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "LC_REEXPORT_DYLIB",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::LC_REEXPORT_DYLIB);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "LC_LOAD_UPWARD_DYLIB",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::LC_LOAD_UPWARD_DYLIB);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "LC_LAZY_LOAD_DYLIB",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::LC_LAZY_LOAD_DYLIB);
|
2016-02-04 10:16:08 +08:00
|
|
|
io.enumCase(value, "LC_VERSION_MIN_MACOSX",
|
|
|
|
llvm::MachO::LC_VERSION_MIN_MACOSX);
|
|
|
|
io.enumCase(value, "LC_VERSION_MIN_IPHONEOS",
|
|
|
|
llvm::MachO::LC_VERSION_MIN_IPHONEOS);
|
|
|
|
io.enumCase(value, "LC_VERSION_MIN_TVOS",
|
|
|
|
llvm::MachO::LC_VERSION_MIN_TVOS);
|
|
|
|
io.enumCase(value, "LC_VERSION_MIN_WATCHOS",
|
|
|
|
llvm::MachO::LC_VERSION_MIN_WATCHOS);
|
2013-10-08 08:43:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MappingTraits<DependentDylib> {
|
|
|
|
static void mapping(IO &io, DependentDylib& dylib) {
|
2014-11-19 10:21:53 +08:00
|
|
|
io.mapRequired("path", dylib.path);
|
|
|
|
io.mapOptional("kind", dylib.kind,
|
|
|
|
llvm::MachO::LC_LOAD_DYLIB);
|
|
|
|
io.mapOptional("compat-version", dylib.compatVersion,
|
|
|
|
PackedVersion(0x10000));
|
|
|
|
io.mapOptional("current-version", dylib.currentVersion,
|
|
|
|
PackedVersion(0x10000));
|
2013-10-08 08:43:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<RebaseType> {
|
|
|
|
static void enumeration(IO &io, RebaseType &value) {
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "REBASE_TYPE_POINTER",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::REBASE_TYPE_POINTER);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "REBASE_TYPE_TEXT_PCREL32",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::REBASE_TYPE_TEXT_PCREL32);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "REBASE_TYPE_TEXT_ABSOLUTE32",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::REBASE_TYPE_TEXT_ABSOLUTE32);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MappingTraits<RebaseLocation> {
|
|
|
|
static void mapping(IO &io, RebaseLocation& rebase) {
|
|
|
|
io.mapRequired("segment-index", rebase.segIndex);
|
|
|
|
io.mapRequired("segment-offset", rebase.segOffset);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.mapOptional("kind", rebase.kind,
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::REBASE_TYPE_POINTER);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-15 05:24:48 +08:00
|
|
|
|
2013-10-08 08:43:34 +08:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<BindType> {
|
|
|
|
static void enumeration(IO &io, BindType &value) {
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "BIND_TYPE_POINTER",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::BIND_TYPE_POINTER);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "BIND_TYPE_TEXT_ABSOLUTE32",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::BIND_TYPE_TEXT_ABSOLUTE32);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "BIND_TYPE_TEXT_PCREL32",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::BIND_TYPE_TEXT_PCREL32);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MappingTraits<BindLocation> {
|
|
|
|
static void mapping(IO &io, BindLocation &bind) {
|
|
|
|
io.mapRequired("segment-index", bind.segIndex);
|
|
|
|
io.mapRequired("segment-offset", bind.segOffset);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.mapOptional("kind", bind.kind,
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::BIND_TYPE_POINTER);
|
|
|
|
io.mapOptional("can-be-null", bind.canBeNull, false);
|
|
|
|
io.mapRequired("ordinal", bind.ordinal);
|
|
|
|
io.mapRequired("symbol-name", bind.symbolName);
|
|
|
|
io.mapOptional("addend", bind.addend, Hex64(0));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<ExportSymbolKind> {
|
|
|
|
static void enumeration(IO &io, ExportSymbolKind &value) {
|
2014-01-27 11:09:26 +08:00
|
|
|
io.enumCase(value, "EXPORT_SYMBOL_FLAGS_KIND_REGULAR",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::EXPORT_SYMBOL_FLAGS_KIND_REGULAR);
|
2014-09-04 03:52:50 +08:00
|
|
|
io.enumCase(value, "EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL);
|
2014-09-04 03:52:50 +08:00
|
|
|
io.enumCase(value, "EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE",
|
|
|
|
llvm::MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE);
|
2013-10-08 08:43:34 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ScalarBitSetTraits<ExportFlags> {
|
|
|
|
static void bitset(IO &io, ExportFlags &value) {
|
2014-01-27 11:09:26 +08:00
|
|
|
io.bitSetCase(value, "EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.bitSetCase(value, "EXPORT_SYMBOL_FLAGS_REEXPORT",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::EXPORT_SYMBOL_FLAGS_REEXPORT);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.bitSetCase(value, "EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER",
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MappingTraits<Export> {
|
|
|
|
static void mapping(IO &io, Export &exp) {
|
|
|
|
io.mapRequired("name", exp.name);
|
2014-09-04 03:52:50 +08:00
|
|
|
io.mapOptional("offset", exp.offset);
|
2014-01-27 11:09:26 +08:00
|
|
|
io.mapOptional("kind", exp.kind,
|
2013-10-08 08:43:34 +08:00
|
|
|
llvm::MachO::EXPORT_SYMBOL_FLAGS_KIND_REGULAR);
|
2014-09-04 03:52:50 +08:00
|
|
|
if (!io.outputting() || exp.flags)
|
|
|
|
io.mapOptional("flags", exp.flags);
|
|
|
|
io.mapOptional("other", exp.otherOffset, Hex32(0));
|
2013-10-08 08:43:34 +08:00
|
|
|
io.mapOptional("other-name", exp.otherName, StringRef());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-25 07:06:56 +08:00
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<DataRegionType> {
|
|
|
|
static void enumeration(IO &io, DataRegionType &value) {
|
|
|
|
io.enumCase(value, "DICE_KIND_DATA",
|
|
|
|
llvm::MachO::DICE_KIND_DATA);
|
|
|
|
io.enumCase(value, "DICE_KIND_JUMP_TABLE8",
|
|
|
|
llvm::MachO::DICE_KIND_JUMP_TABLE8);
|
|
|
|
io.enumCase(value, "DICE_KIND_JUMP_TABLE16",
|
|
|
|
llvm::MachO::DICE_KIND_JUMP_TABLE16);
|
|
|
|
io.enumCase(value, "DICE_KIND_JUMP_TABLE32",
|
|
|
|
llvm::MachO::DICE_KIND_JUMP_TABLE32);
|
|
|
|
io.enumCase(value, "DICE_KIND_ABS_JUMP_TABLE32",
|
|
|
|
llvm::MachO::DICE_KIND_ABS_JUMP_TABLE32);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MappingTraits<DataInCode> {
|
|
|
|
static void mapping(IO &io, DataInCode &entry) {
|
|
|
|
io.mapRequired("offset", entry.offset);
|
|
|
|
io.mapRequired("length", entry.length);
|
|
|
|
io.mapRequired("kind", entry.kind);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-19 10:21:53 +08:00
|
|
|
template <>
|
|
|
|
struct ScalarTraits<PackedVersion> {
|
|
|
|
static void output(const PackedVersion &value, void*, raw_ostream &out) {
|
|
|
|
out << llvm::format("%d.%d", (value >> 16), (value >> 8) & 0xFF);
|
|
|
|
if (value & 0xFF) {
|
|
|
|
out << llvm::format(".%d", (value & 0xFF));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static StringRef input(StringRef scalar, void*, PackedVersion &result) {
|
|
|
|
uint32_t value;
|
|
|
|
if (lld::MachOLinkingContext::parsePackedVersion(scalar, value))
|
|
|
|
return "malformed version number";
|
|
|
|
result = value;
|
|
|
|
// Return the empty string on success,
|
|
|
|
return StringRef();
|
|
|
|
}
|
2017-12-19 01:49:27 +08:00
|
|
|
static QuotingType mustQuote(StringRef) { return QuotingType::None; }
|
2014-11-19 10:21:53 +08:00
|
|
|
};
|
2013-10-08 08:43:34 +08:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MappingTraits<NormalizedFile> {
|
|
|
|
static void mapping(IO &io, NormalizedFile &file) {
|
|
|
|
io.mapRequired("arch", file.arch);
|
|
|
|
io.mapRequired("file-type", file.fileType);
|
|
|
|
io.mapOptional("flags", file.flags);
|
|
|
|
io.mapOptional("dependents", file.dependentDylibs);
|
|
|
|
io.mapOptional("install-name", file.installName, StringRef());
|
2014-11-19 10:21:53 +08:00
|
|
|
io.mapOptional("compat-version", file.compatVersion, PackedVersion(0x10000));
|
|
|
|
io.mapOptional("current-version", file.currentVersion, PackedVersion(0x10000));
|
2013-10-08 08:43:34 +08:00
|
|
|
io.mapOptional("has-UUID", file.hasUUID, true);
|
|
|
|
io.mapOptional("rpaths", file.rpaths);
|
|
|
|
io.mapOptional("entry-point", file.entryAddress, Hex64(0));
|
2015-05-22 08:25:34 +08:00
|
|
|
io.mapOptional("stack-size", file.stackSize, Hex64(0));
|
2013-10-08 08:43:34 +08:00
|
|
|
io.mapOptional("source-version", file.sourceVersion, Hex64(0));
|
|
|
|
io.mapOptional("OS", file.os);
|
2014-11-19 10:21:53 +08:00
|
|
|
io.mapOptional("min-os-version", file.minOSverson, PackedVersion(0));
|
2016-02-04 10:16:08 +08:00
|
|
|
io.mapOptional("min-os-version-kind", file.minOSVersionKind, (LoadCommandType)0);
|
2014-11-19 10:21:53 +08:00
|
|
|
io.mapOptional("sdk-version", file.sdkVersion, PackedVersion(0));
|
2013-10-08 08:43:34 +08:00
|
|
|
io.mapOptional("segments", file.segments);
|
|
|
|
io.mapOptional("sections", file.sections);
|
|
|
|
io.mapOptional("local-symbols", file.localSymbols);
|
|
|
|
io.mapOptional("global-symbols", file.globalSymbols);
|
|
|
|
io.mapOptional("undefined-symbols",file.undefinedSymbols);
|
2014-09-10 07:52:59 +08:00
|
|
|
io.mapOptional("page-size", file.pageSize, Hex32(4096));
|
2013-10-08 08:43:34 +08:00
|
|
|
io.mapOptional("rebasings", file.rebasingInfo);
|
|
|
|
io.mapOptional("bindings", file.bindingInfo);
|
|
|
|
io.mapOptional("weak-bindings", file.weakBindingInfo);
|
|
|
|
io.mapOptional("lazy-bindings", file.lazyBindingInfo);
|
|
|
|
io.mapOptional("exports", file.exportInfo);
|
2014-07-25 07:06:56 +08:00
|
|
|
io.mapOptional("dataInCode", file.dataInCode);
|
2013-10-08 08:43:34 +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
|
|
|
static StringRef validate(IO &io, NormalizedFile &file) {
|
|
|
|
return StringRef();
|
|
|
|
}
|
2013-10-08 08:43:34 +08:00
|
|
|
};
|
|
|
|
|
2013-10-15 05:24:48 +08:00
|
|
|
} // namespace llvm
|
|
|
|
} // namespace yaml
|
|
|
|
|
2013-10-08 08:43:34 +08:00
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace mach_o {
|
2014-01-11 09:07:43 +08:00
|
|
|
|
2014-01-27 11:09:26 +08:00
|
|
|
/// Handles !mach-o tagged yaml documents.
|
2014-01-11 09:07:43 +08:00
|
|
|
bool MachOYamlIOTaggedDocumentHandler::handledDocTag(llvm::yaml::IO &io,
|
|
|
|
const lld::File *&file) const {
|
|
|
|
if (!io.mapTag("!mach-o"))
|
|
|
|
return false;
|
|
|
|
// Step 1: parse yaml into normalized mach-o struct.
|
|
|
|
NormalizedFile nf;
|
|
|
|
YamlContext *info = reinterpret_cast<YamlContext *>(io.getContext());
|
|
|
|
assert(info != nullptr);
|
|
|
|
assert(info->_normalizeMachOFile == nullptr);
|
|
|
|
info->_normalizeMachOFile = &nf;
|
|
|
|
MappingTraits<NormalizedFile>::mapping(io, nf);
|
|
|
|
// Step 2: parse normalized mach-o struct into atoms.
|
2016-03-31 07:43:27 +08:00
|
|
|
auto fileOrError = normalizedToAtoms(nf, info->_path, true);
|
2014-07-01 06:57:33 +08:00
|
|
|
|
2016-04-06 06:06:48 +08:00
|
|
|
// Check that we parsed successfully.
|
|
|
|
if (!fileOrError) {
|
2016-03-31 07:43:27 +08:00
|
|
|
std::string buffer;
|
|
|
|
llvm::raw_string_ostream stream(buffer);
|
|
|
|
handleAllErrors(fileOrError.takeError(),
|
|
|
|
[&](const llvm::ErrorInfoBase &EI) {
|
|
|
|
EI.log(stream);
|
|
|
|
stream << "\n";
|
|
|
|
});
|
|
|
|
io.setError(stream.str());
|
2014-05-23 04:05:43 +08:00
|
|
|
return false;
|
2014-01-11 09:07:43 +08:00
|
|
|
}
|
2016-04-06 06:06:48 +08:00
|
|
|
|
|
|
|
if (nf.arch != _arch) {
|
|
|
|
io.setError(Twine("file is wrong architecture. Expected ("
|
|
|
|
+ MachOLinkingContext::nameFromArch(_arch)
|
|
|
|
+ ") found ("
|
|
|
|
+ MachOLinkingContext::nameFromArch(nf.arch)
|
|
|
|
+ ")"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
info->_normalizeMachOFile = nullptr;
|
2016-04-07 01:18:55 +08:00
|
|
|
file = fileOrError->release();
|
2016-04-06 06:06:48 +08:00
|
|
|
return true;
|
2014-01-11 09:07:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-08 08:43:34 +08:00
|
|
|
namespace normalized {
|
|
|
|
|
|
|
|
/// Parses a yaml encoded mach-o file to produce an in-memory normalized view.
|
2016-03-31 09:13:04 +08:00
|
|
|
llvm::Expected<std::unique_ptr<NormalizedFile>>
|
2013-10-08 08:43:34 +08:00
|
|
|
readYaml(std::unique_ptr<MemoryBuffer> &mb) {
|
|
|
|
// Make empty NormalizedFile.
|
|
|
|
std::unique_ptr<NormalizedFile> f(new NormalizedFile());
|
|
|
|
|
|
|
|
// Create YAML Input parser.
|
[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
|
|
|
YamlContext yamlContext;
|
|
|
|
yamlContext._normalizeMachOFile = f.get();
|
|
|
|
llvm::yaml::Input yin(mb->getBuffer(), &yamlContext);
|
2013-10-08 08:43:34 +08:00
|
|
|
|
|
|
|
// Fill NormalizedFile by parsing yaml.
|
|
|
|
yin >> *f;
|
|
|
|
|
|
|
|
// Return error if there were parsing problems.
|
2016-03-31 09:13:04 +08:00
|
|
|
if (auto ec = yin.error())
|
|
|
|
return llvm::make_error<GenericError>(Twine("YAML parsing error: ")
|
|
|
|
+ ec.message());
|
2013-10-08 08:43:34 +08:00
|
|
|
|
|
|
|
// Hand ownership of instantiated NormalizedFile to caller.
|
|
|
|
return std::move(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Writes a yaml encoded mach-o files from an in-memory normalized view.
|
2014-06-13 04:42:12 +08:00
|
|
|
std::error_code writeYaml(const NormalizedFile &file, raw_ostream &out) {
|
2013-10-08 08:43:34 +08:00
|
|
|
// YAML I/O is not const aware, so need to cast away ;-(
|
|
|
|
NormalizedFile *f = const_cast<NormalizedFile*>(&file);
|
|
|
|
|
|
|
|
// Create yaml Output writer, using yaml options for context.
|
[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
|
|
|
YamlContext yamlContext;
|
|
|
|
yamlContext._normalizeMachOFile = f;
|
|
|
|
llvm::yaml::Output yout(out, &yamlContext);
|
2013-10-08 08:43:34 +08:00
|
|
|
|
|
|
|
// Stream out yaml.
|
|
|
|
yout << *f;
|
|
|
|
|
2014-06-13 04:42:12 +08:00
|
|
|
return std::error_code();
|
2013-10-08 08:43:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace normalized
|
|
|
|
} // namespace mach_o
|
|
|
|
} // namespace lld
|