2016-05-12 18:04:16 +02:00
|
|
|
//===- MachOYAML.h - Mach-O YAMLIO implementation ---------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01: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
|
2016-05-12 18:04:16 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-01 17:54:18 +02:00
|
|
|
/// This file declares classes for handling the YAML representation
|
2016-05-12 18:04:16 +02:00
|
|
|
/// of Mach-O.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_OBJECTYAML_MACHOYAML_H
|
|
|
|
#define LLVM_OBJECTYAML_MACHOYAML_H
|
|
|
|
|
2017-07-01 03:35:55 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-06-07 05:48:56 +02:00
|
|
|
#include "llvm/BinaryFormat/MachO.h"
|
2016-12-07 22:26:32 +01:00
|
|
|
#include "llvm/ObjectYAML/DWARFYAML.h"
|
2019-08-20 10:49:07 +02:00
|
|
|
#include "llvm/ObjectYAML/YAML.h"
|
2017-07-01 03:35:55 +02:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-05-12 18:04:16 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace MachOYAML {
|
|
|
|
|
2020-04-22 20:50:55 +02:00
|
|
|
struct Relocation {
|
|
|
|
// Offset in the section to what is being relocated.
|
|
|
|
llvm::yaml::Hex32 address;
|
|
|
|
// Symbol index if r_extern == 1 else section index.
|
|
|
|
uint32_t symbolnum;
|
|
|
|
bool is_pcrel;
|
|
|
|
// Real length = 2 ^ length.
|
|
|
|
uint8_t length;
|
|
|
|
bool is_extern;
|
|
|
|
uint8_t type;
|
|
|
|
bool is_scattered;
|
|
|
|
int32_t value;
|
|
|
|
};
|
|
|
|
|
2016-05-18 18:17:23 +02:00
|
|
|
struct Section {
|
|
|
|
char sectname[16];
|
|
|
|
char segname[16];
|
|
|
|
llvm::yaml::Hex64 addr;
|
|
|
|
uint64_t size;
|
|
|
|
llvm::yaml::Hex32 offset;
|
|
|
|
uint32_t align;
|
|
|
|
llvm::yaml::Hex32 reloff;
|
|
|
|
uint32_t nreloc;
|
|
|
|
llvm::yaml::Hex32 flags;
|
|
|
|
llvm::yaml::Hex32 reserved1;
|
|
|
|
llvm::yaml::Hex32 reserved2;
|
|
|
|
llvm::yaml::Hex32 reserved3;
|
2019-08-20 10:49:07 +02:00
|
|
|
Optional<llvm::yaml::BinaryRef> content;
|
2020-04-22 20:50:55 +02:00
|
|
|
std::vector<Relocation> relocations;
|
2016-05-18 18:17:23 +02:00
|
|
|
};
|
|
|
|
|
2016-05-12 18:04:16 +02:00
|
|
|
struct FileHeader {
|
2016-05-12 19:44:43 +02:00
|
|
|
llvm::yaml::Hex32 magic;
|
2016-05-12 18:04:16 +02:00
|
|
|
llvm::yaml::Hex32 cputype;
|
|
|
|
llvm::yaml::Hex32 cpusubtype;
|
|
|
|
llvm::yaml::Hex32 filetype;
|
|
|
|
uint32_t ncmds;
|
2016-05-12 19:44:43 +02:00
|
|
|
uint32_t sizeofcmds;
|
2016-05-12 18:04:16 +02:00
|
|
|
llvm::yaml::Hex32 flags;
|
2016-05-12 20:21:09 +02:00
|
|
|
llvm::yaml::Hex32 reserved;
|
2016-05-12 18:04:16 +02:00
|
|
|
};
|
|
|
|
|
2016-05-13 19:41:41 +02:00
|
|
|
struct LoadCommand {
|
|
|
|
virtual ~LoadCommand();
|
2017-07-01 03:35:55 +02:00
|
|
|
|
2016-05-17 21:44:06 +02:00
|
|
|
llvm::MachO::macho_load_command Data;
|
2016-05-18 18:17:23 +02:00
|
|
|
std::vector<Section> Sections;
|
2017-01-23 21:07:55 +01:00
|
|
|
std::vector<MachO::build_tool_version> Tools;
|
2016-05-19 22:54:43 +02:00
|
|
|
std::vector<llvm::yaml::Hex8> PayloadBytes;
|
2021-07-26 18:04:51 +02:00
|
|
|
std::string Content;
|
2016-05-19 22:54:43 +02:00
|
|
|
uint64_t ZeroPadBytes;
|
2016-05-13 19:41:41 +02:00
|
|
|
};
|
|
|
|
|
2016-06-03 00:54:06 +02:00
|
|
|
struct NListEntry {
|
|
|
|
uint32_t n_strx;
|
2016-08-04 23:07:39 +02:00
|
|
|
llvm::yaml::Hex8 n_type;
|
2016-06-03 00:54:06 +02:00
|
|
|
uint8_t n_sect;
|
|
|
|
uint16_t n_desc;
|
|
|
|
uint64_t n_value;
|
|
|
|
};
|
2017-07-01 03:35:55 +02:00
|
|
|
|
2016-05-25 19:09:07 +02:00
|
|
|
struct RebaseOpcode {
|
|
|
|
MachO::RebaseOpcode Opcode;
|
|
|
|
uint8_t Imm;
|
|
|
|
std::vector<yaml::Hex64> ExtraData;
|
|
|
|
};
|
|
|
|
|
2016-05-26 22:06:14 +02:00
|
|
|
struct BindOpcode {
|
|
|
|
MachO::BindOpcode Opcode;
|
|
|
|
uint8_t Imm;
|
|
|
|
std::vector<yaml::Hex64> ULEBExtraData;
|
|
|
|
std::vector<int64_t> SLEBExtraData;
|
|
|
|
StringRef Symbol;
|
|
|
|
};
|
|
|
|
|
2016-05-31 19:26:36 +02:00
|
|
|
struct ExportEntry {
|
2017-07-01 03:35:55 +02:00
|
|
|
uint64_t TerminalSize = 0;
|
|
|
|
uint64_t NodeOffset = 0;
|
2016-05-31 19:26:36 +02:00
|
|
|
std::string Name;
|
2017-07-01 03:35:55 +02:00
|
|
|
llvm::yaml::Hex64 Flags = 0;
|
|
|
|
llvm::yaml::Hex64 Address = 0;
|
|
|
|
llvm::yaml::Hex64 Other = 0;
|
2016-05-31 19:26:36 +02:00
|
|
|
std::string ImportName;
|
|
|
|
std::vector<MachOYAML::ExportEntry> Children;
|
|
|
|
};
|
|
|
|
|
2016-05-25 19:09:07 +02:00
|
|
|
struct LinkEditData {
|
|
|
|
std::vector<MachOYAML::RebaseOpcode> RebaseOpcodes;
|
2016-05-26 22:06:14 +02:00
|
|
|
std::vector<MachOYAML::BindOpcode> BindOpcodes;
|
2016-05-26 22:50:05 +02:00
|
|
|
std::vector<MachOYAML::BindOpcode> WeakBindOpcodes;
|
2016-05-26 23:29:39 +02:00
|
|
|
std::vector<MachOYAML::BindOpcode> LazyBindOpcodes;
|
2016-05-31 19:26:36 +02:00
|
|
|
MachOYAML::ExportEntry ExportTrie;
|
2016-06-03 00:54:06 +02:00
|
|
|
std::vector<NListEntry> NameList;
|
|
|
|
std::vector<StringRef> StringTable;
|
2016-08-17 23:46:04 +02:00
|
|
|
|
|
|
|
bool isEmpty() const;
|
2016-05-25 19:09:07 +02:00
|
|
|
};
|
|
|
|
|
2016-05-12 18:04:16 +02:00
|
|
|
struct Object {
|
2016-12-22 22:58:03 +01:00
|
|
|
bool IsLittleEndian;
|
2016-05-12 18:04:16 +02:00
|
|
|
FileHeader Header;
|
2016-05-17 21:44:06 +02:00
|
|
|
std::vector<LoadCommand> LoadCommands;
|
2016-05-18 18:17:23 +02:00
|
|
|
std::vector<Section> Sections;
|
2016-05-25 19:09:07 +02:00
|
|
|
LinkEditData LinkEdit;
|
2016-12-08 18:46:57 +01:00
|
|
|
DWARFYAML::Data DWARF;
|
2016-05-12 18:04:16 +02:00
|
|
|
};
|
|
|
|
|
2016-06-24 22:42:28 +02:00
|
|
|
struct FatHeader {
|
|
|
|
llvm::yaml::Hex32 magic;
|
|
|
|
uint32_t nfat_arch;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FatArch {
|
|
|
|
llvm::yaml::Hex32 cputype;
|
|
|
|
llvm::yaml::Hex32 cpusubtype;
|
|
|
|
llvm::yaml::Hex64 offset;
|
|
|
|
uint64_t size;
|
|
|
|
uint32_t align;
|
|
|
|
llvm::yaml::Hex32 reserved;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UniversalBinary {
|
|
|
|
FatHeader Header;
|
|
|
|
std::vector<FatArch> FatArchs;
|
|
|
|
std::vector<Object> Slices;
|
|
|
|
};
|
|
|
|
|
2017-07-01 03:35:55 +02:00
|
|
|
} // end namespace MachOYAML
|
|
|
|
} // end namespace llvm
|
2016-05-12 18:04:16 +02:00
|
|
|
|
2016-05-17 21:44:06 +02:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::LoadCommand)
|
2020-04-22 20:50:55 +02:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Relocation)
|
2016-05-18 18:17:23 +02:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Section)
|
2016-05-25 19:09:07 +02:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::RebaseOpcode)
|
2016-05-26 22:06:14 +02:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::BindOpcode)
|
2016-05-31 19:26:36 +02:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::ExportEntry)
|
2016-06-03 00:54:06 +02:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::NListEntry)
|
2016-06-24 22:42:28 +02:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::Object)
|
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::FatArch)
|
2017-01-23 21:07:55 +01:00
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachO::build_tool_version)
|
2016-05-13 19:41:41 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
2017-07-01 03:35:55 +02:00
|
|
|
|
|
|
|
class raw_ostream;
|
|
|
|
|
2016-05-12 18:04:16 +02:00
|
|
|
namespace yaml {
|
|
|
|
|
|
|
|
template <> struct MappingTraits<MachOYAML::FileHeader> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::FileHeader &FileHeader);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<MachOYAML::Object> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::Object &Object);
|
|
|
|
};
|
|
|
|
|
2016-06-24 22:42:28 +02:00
|
|
|
template <> struct MappingTraits<MachOYAML::FatHeader> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::FatHeader &FatHeader);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<MachOYAML::FatArch> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::FatArch &FatArch);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<MachOYAML::UniversalBinary> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::UniversalBinary &UniversalBinary);
|
|
|
|
};
|
|
|
|
|
2016-05-17 21:44:06 +02:00
|
|
|
template <> struct MappingTraits<MachOYAML::LoadCommand> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::LoadCommand &LoadCommand);
|
2016-05-13 19:41:41 +02:00
|
|
|
};
|
|
|
|
|
2016-05-25 19:09:07 +02:00
|
|
|
template <> struct MappingTraits<MachOYAML::LinkEditData> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::LinkEditData &LinkEditData);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<MachOYAML::RebaseOpcode> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::RebaseOpcode &RebaseOpcode);
|
|
|
|
};
|
|
|
|
|
2016-05-26 22:06:14 +02:00
|
|
|
template <> struct MappingTraits<MachOYAML::BindOpcode> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::BindOpcode &BindOpcode);
|
|
|
|
};
|
|
|
|
|
2016-05-31 19:26:36 +02:00
|
|
|
template <> struct MappingTraits<MachOYAML::ExportEntry> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::ExportEntry &ExportEntry);
|
|
|
|
};
|
|
|
|
|
2020-04-22 20:50:55 +02:00
|
|
|
template <> struct MappingTraits<MachOYAML::Relocation> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::Relocation &R);
|
|
|
|
};
|
|
|
|
|
2016-05-18 18:17:23 +02:00
|
|
|
template <> struct MappingTraits<MachOYAML::Section> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::Section &Section);
|
2020-10-20 15:01:01 +02:00
|
|
|
static std::string validate(IO &io, MachOYAML::Section &Section);
|
2016-05-18 18:17:23 +02:00
|
|
|
};
|
|
|
|
|
2016-06-03 00:54:06 +02:00
|
|
|
template <> struct MappingTraits<MachOYAML::NListEntry> {
|
|
|
|
static void mapping(IO &IO, MachOYAML::NListEntry &NListEntry);
|
|
|
|
};
|
|
|
|
|
2017-01-23 21:07:55 +01:00
|
|
|
template <> struct MappingTraits<MachO::build_tool_version> {
|
|
|
|
static void mapping(IO &IO, MachO::build_tool_version &tool);
|
|
|
|
};
|
|
|
|
|
2016-05-17 21:44:06 +02:00
|
|
|
#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
|
2016-05-14 00:12:41 +02:00
|
|
|
io.enumCase(value, #LCName, MachO::LCName);
|
2016-05-13 19:41:41 +02:00
|
|
|
|
|
|
|
template <> struct ScalarEnumerationTraits<MachO::LoadCommandType> {
|
|
|
|
static void enumeration(IO &io, MachO::LoadCommandType &value) {
|
2017-06-07 05:48:56 +02:00
|
|
|
#include "llvm/BinaryFormat/MachO.def"
|
2016-05-26 22:06:14 +02:00
|
|
|
io.enumFallback<Hex32>(value);
|
2016-05-13 19:41:41 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-26 22:06:14 +02:00
|
|
|
#define ENUM_CASE(Enum) io.enumCase(value, #Enum, MachO::Enum);
|
2016-05-25 19:09:07 +02:00
|
|
|
|
|
|
|
template <> struct ScalarEnumerationTraits<MachO::RebaseOpcode> {
|
|
|
|
static void enumeration(IO &io, MachO::RebaseOpcode &value) {
|
2016-05-26 22:06:14 +02:00
|
|
|
ENUM_CASE(REBASE_OPCODE_DONE)
|
|
|
|
ENUM_CASE(REBASE_OPCODE_SET_TYPE_IMM)
|
|
|
|
ENUM_CASE(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB)
|
|
|
|
ENUM_CASE(REBASE_OPCODE_ADD_ADDR_ULEB)
|
|
|
|
ENUM_CASE(REBASE_OPCODE_ADD_ADDR_IMM_SCALED)
|
|
|
|
ENUM_CASE(REBASE_OPCODE_DO_REBASE_IMM_TIMES)
|
|
|
|
ENUM_CASE(REBASE_OPCODE_DO_REBASE_ULEB_TIMES)
|
|
|
|
ENUM_CASE(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB)
|
|
|
|
ENUM_CASE(REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB)
|
|
|
|
io.enumFallback<Hex8>(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct ScalarEnumerationTraits<MachO::BindOpcode> {
|
|
|
|
static void enumeration(IO &io, MachO::BindOpcode &value) {
|
|
|
|
ENUM_CASE(BIND_OPCODE_DONE)
|
|
|
|
ENUM_CASE(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM)
|
|
|
|
ENUM_CASE(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB)
|
|
|
|
ENUM_CASE(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM)
|
|
|
|
ENUM_CASE(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
|
|
|
|
ENUM_CASE(BIND_OPCODE_SET_TYPE_IMM)
|
|
|
|
ENUM_CASE(BIND_OPCODE_SET_ADDEND_SLEB)
|
|
|
|
ENUM_CASE(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB)
|
|
|
|
ENUM_CASE(BIND_OPCODE_ADD_ADDR_ULEB)
|
|
|
|
ENUM_CASE(BIND_OPCODE_DO_BIND)
|
|
|
|
ENUM_CASE(BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB)
|
|
|
|
ENUM_CASE(BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED)
|
|
|
|
ENUM_CASE(BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB)
|
|
|
|
io.enumFallback<Hex8>(value);
|
2016-05-25 19:09:07 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-17 21:44:06 +02:00
|
|
|
// This trait is used for 16-byte chars in Mach structures used for strings
|
2017-07-01 03:35:55 +02:00
|
|
|
using char_16 = char[16];
|
2016-05-17 21:44:06 +02:00
|
|
|
|
|
|
|
template <> struct ScalarTraits<char_16> {
|
2017-07-01 03:35:55 +02:00
|
|
|
static void output(const char_16 &Val, void *, raw_ostream &Out);
|
2016-05-17 21:44:06 +02:00
|
|
|
static StringRef input(StringRef Scalar, void *, char_16 &Val);
|
2017-12-18 18:38:03 +01:00
|
|
|
static QuotingType mustQuote(StringRef S);
|
2016-05-17 21:44:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// This trait is used for UUIDs. It reads and writes them matching otool's
|
|
|
|
// formatting style.
|
2017-09-13 20:22:59 +02:00
|
|
|
using uuid_t = raw_ostream::uuid_t;
|
2016-05-17 21:44:06 +02:00
|
|
|
|
|
|
|
template <> struct ScalarTraits<uuid_t> {
|
2017-07-01 03:35:55 +02:00
|
|
|
static void output(const uuid_t &Val, void *, raw_ostream &Out);
|
2016-05-17 21:44:06 +02:00
|
|
|
static StringRef input(StringRef Scalar, void *, uuid_t &Val);
|
2017-12-18 18:38:03 +01:00
|
|
|
static QuotingType mustQuote(StringRef S);
|
2016-05-17 21:44:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Load Command struct mapping traits
|
|
|
|
|
|
|
|
#define LOAD_COMMAND_STRUCT(LCStruct) \
|
|
|
|
template <> struct MappingTraits<MachO::LCStruct> { \
|
|
|
|
static void mapping(IO &IO, MachO::LCStruct &LoadCommand); \
|
|
|
|
};
|
|
|
|
|
2017-06-07 05:48:56 +02:00
|
|
|
#include "llvm/BinaryFormat/MachO.def"
|
2016-05-17 21:44:06 +02:00
|
|
|
|
|
|
|
// Extra structures used by load commands
|
|
|
|
template <> struct MappingTraits<MachO::dylib> {
|
|
|
|
static void mapping(IO &IO, MachO::dylib &LoadCommand);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<MachO::fvmlib> {
|
|
|
|
static void mapping(IO &IO, MachO::fvmlib &LoadCommand);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<MachO::section> {
|
|
|
|
static void mapping(IO &IO, MachO::section &LoadCommand);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<MachO::section_64> {
|
|
|
|
static void mapping(IO &IO, MachO::section_64 &LoadCommand);
|
|
|
|
};
|
2016-05-13 19:41:41 +02:00
|
|
|
|
2017-07-01 03:35:55 +02:00
|
|
|
} // end namespace yaml
|
2016-05-12 18:04:16 +02:00
|
|
|
|
2017-07-01 03:35:55 +02:00
|
|
|
} // end namespace llvm
|
2016-05-12 18:04:16 +02:00
|
|
|
|
2017-07-01 03:35:55 +02:00
|
|
|
#endif // LLVM_OBJECTYAML_MACHOYAML_H
|