77 lines
3.2 KiB
C++
77 lines
3.2 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <string>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
namespace voidlibc::voidlink::macho {
|
|
|
|
// Mach-O magic (64-bit little-endian, x86_64 or ARM64)
|
|
inline constexpr std::uint32_t kMagic64 = 0xFEEDFACFu;
|
|
inline constexpr std::uint32_t kCpuX86_64 = 0x01000007u;
|
|
inline constexpr std::uint32_t kMhObject = 0x1u;
|
|
|
|
// Relocation types for x86_64
|
|
inline constexpr std::uint32_t kRelocUnsigned = 0; // abs64 / abs32
|
|
inline constexpr std::uint32_t kRelocSigned = 1; // PC32 (inline addend is signed, includes -4)
|
|
inline constexpr std::uint32_t kRelocBranch = 2; // PC32 (call/jmp)
|
|
inline constexpr std::uint32_t kRelocGotLoad = 3; // GOTPCREL (movq via GOT)
|
|
inline constexpr std::uint32_t kRelocGot = 4; // GOT reference
|
|
inline constexpr std::uint32_t kRelocSubtractor = 5; // pair: SUBTRACTOR followed by UNSIGNED
|
|
inline constexpr std::uint32_t kRelocSigned1 = 6; // PC32 with extra -1 skew
|
|
inline constexpr std::uint32_t kRelocSigned2 = 7; // PC32 with extra -2 skew
|
|
inline constexpr std::uint32_t kRelocSigned4 = 8; // PC32 with extra -4 skew
|
|
inline constexpr std::uint32_t kRelocTls = 9; // thread-local
|
|
|
|
// nlist_64 type byte
|
|
inline constexpr std::uint8_t kNTypeStab = 0xE0;
|
|
inline constexpr std::uint8_t kNTypeMask = 0x0E;
|
|
inline constexpr std::uint8_t kNTypeUndef = 0x00;
|
|
inline constexpr std::uint8_t kNTypeSect = 0x0E;
|
|
inline constexpr std::uint8_t kNExternal = 0x01;
|
|
|
|
struct ParsedSymbol {
|
|
std::string name;
|
|
std::uint64_t value = 0; // section-relative offset (N_SECT) or 0 (N_UNDF)
|
|
std::uint32_t section_index = 0; // 1-based Mach-O section ordinal; 0 = undefined
|
|
std::uint8_t bind = 0; // 0=local, 1=global, 2=weak
|
|
std::uint8_t type = 0; // 0=notype, 2=func
|
|
bool defined = false;
|
|
};
|
|
|
|
struct ParsedRelocation {
|
|
std::uint64_t offset = 0;
|
|
std::uint32_t sym_index = 0; // index into ParsedObject::symbols (extern) or section ordinal (local)
|
|
std::uint32_t type = 0; // kReloc* constant
|
|
std::int64_t addend = 0; // inline addend (read from the target bytes)
|
|
bool is_extern = false;
|
|
bool is_pcrel = false;
|
|
std::uint32_t length = 0; // 0=byte, 1=short, 2=long, 3=quad
|
|
};
|
|
|
|
struct ParsedSection {
|
|
std::string segname; // e.g. "__TEXT"
|
|
std::string sectname; // e.g. "__text"
|
|
std::vector<std::uint8_t> bytes;
|
|
std::uint64_t mem_size = 0;
|
|
std::uint64_t addr = 0; // section vmaddr in the .o file (used for layout delta)
|
|
std::uint32_t align_exp = 0; // log2 of section alignment
|
|
std::uint32_t flags = 0;
|
|
std::vector<ParsedRelocation> relocs;
|
|
};
|
|
|
|
struct ParsedObject {
|
|
std::vector<ParsedSection> sections; // indexed by 1-based section ordinal - 1
|
|
std::vector<ParsedSymbol> symbols;
|
|
std::size_t text_index = static_cast<std::size_t>(-1);
|
|
};
|
|
|
|
struct ParseError { std::string message; };
|
|
using ParseResult = std::variant<ParsedObject, ParseError>;
|
|
|
|
ParseResult parse_macho_object(const std::filesystem::path& path);
|
|
|
|
} // namespace voidlibc::voidlink::macho
|