178 lines
7.2 KiB
C++
178 lines
7.2 KiB
C++
#include "reader.hpp"
|
|
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <vector>
|
|
|
|
namespace voidlibc::voidlink {
|
|
|
|
using namespace voidlibc::format;
|
|
|
|
ReadResult read_container(const std::filesystem::path& in_path) {
|
|
std::error_code ec;
|
|
const auto file_size = std::filesystem::file_size(in_path, ec);
|
|
if (ec) {
|
|
return ReadError{"stat failed: " + ec.message()};
|
|
}
|
|
if (file_size < sizeof(FileHeader)) {
|
|
return ReadError{"file too small to be a VoidLibC file"};
|
|
}
|
|
|
|
std::ifstream in(in_path, std::ios::binary);
|
|
if (!in) {
|
|
return ReadError{"could not open " + in_path.string()};
|
|
}
|
|
|
|
Container c{};
|
|
in.read(reinterpret_cast<char*>(&c.header), sizeof(c.header));
|
|
if (!in) {
|
|
return ReadError{"short read on header"};
|
|
}
|
|
|
|
if (c.header.magic != kMagic) {
|
|
return ReadError{"bad magic; not a VoidLibC file"};
|
|
}
|
|
if (c.header.version_major != kVersionMajor) {
|
|
std::ostringstream os;
|
|
os << "unsupported major version " << c.header.version_major;
|
|
return ReadError{os.str()};
|
|
}
|
|
if (c.header.arch != TargetArch::X86_64) {
|
|
return ReadError{"unsupported architecture (MVP is x86_64-only)"};
|
|
}
|
|
if (c.header.file_size != file_size) {
|
|
std::ostringstream os;
|
|
os << "header file_size " << c.header.file_size
|
|
<< " disagrees with on-disk size " << file_size;
|
|
return ReadError{os.str()};
|
|
}
|
|
|
|
c.actual_file_size = file_size;
|
|
c.path = in_path;
|
|
return c;
|
|
}
|
|
|
|
static const char* os_name(TargetOS os) {
|
|
switch (os) {
|
|
case TargetOS::Linux: return "linux";
|
|
case TargetOS::Windows: return "windows";
|
|
case TargetOS::MacOS: return "macos";
|
|
case TargetOS::Unknown: return "unknown";
|
|
}
|
|
return "?";
|
|
}
|
|
[[maybe_unused]] static auto silence_unused_os_name = &os_name;
|
|
|
|
void dump_container(const Container& c) {
|
|
const auto& h = c.header;
|
|
const bool is_exec = flag_set(h.flags, ContainerFlags::Executable);
|
|
const bool is_stat = flag_set(h.flags, ContainerFlags::StaticLibrary);
|
|
std::printf("magic : 0x%08x (VLC\\0)\n", h.magic);
|
|
std::printf("type : %s (%s)\n",
|
|
is_stat ? "static library" : is_exec ? "executable" : "shared library",
|
|
infer_extension(h.flags));
|
|
std::printf("version : %u.%u\n", h.version_major, h.version_minor);
|
|
const bool has_dyn = flag_set(h.flags, ContainerFlags::HasDynamicImports);
|
|
std::printf("linkage : %s\n", has_dyn ? "dynamic" : "static");
|
|
std::printf("flags : 0x%08x%s%s%s%s\n", h.flags,
|
|
flag_set(h.flags, ContainerFlags::HasDebug) ? " HasDebug" : "",
|
|
flag_set(h.flags, ContainerFlags::HasCppExports) ? " HasCppExports" : "",
|
|
has_dyn ? " HasDynamicImports" : "",
|
|
is_stat ? " StaticLibrary" : "");
|
|
std::printf("arch : %s\n",
|
|
h.arch == TargetArch::X86_64 ? "x86_64" : "unknown");
|
|
std::printf("section_count : %u\n", h.section_count);
|
|
std::printf("section_dir_off : %llu\n",
|
|
static_cast<unsigned long long>(h.section_dir_off));
|
|
std::printf("symtab : off=%llu size=%llu\n",
|
|
static_cast<unsigned long long>(h.symtab_off),
|
|
static_cast<unsigned long long>(h.symtab_size));
|
|
std::printf("imports : off=%llu size=%llu\n",
|
|
static_cast<unsigned long long>(h.imports_off),
|
|
static_cast<unsigned long long>(h.imports_size));
|
|
std::printf("exports : off=%llu size=%llu\n",
|
|
static_cast<unsigned long long>(h.exports_off),
|
|
static_cast<unsigned long long>(h.exports_size));
|
|
std::printf("file_size : %llu (on disk: %llu)\n",
|
|
static_cast<unsigned long long>(h.file_size),
|
|
static_cast<unsigned long long>(c.actual_file_size));
|
|
|
|
if (c.path.empty() || h.section_count == 0) return;
|
|
|
|
std::ifstream f(c.path, std::ios::binary);
|
|
if (!f) return;
|
|
|
|
// Per-section details
|
|
f.seekg(static_cast<std::streamoff>(h.section_dir_off));
|
|
std::vector<SectionEntry> sects(h.section_count);
|
|
f.read(reinterpret_cast<char*>(sects.data()),
|
|
static_cast<std::streamsize>(h.section_count * sizeof(SectionEntry)));
|
|
if (!f) return;
|
|
|
|
std::printf("sections:\n");
|
|
for (std::uint32_t i = 0; i < h.section_count; ++i) {
|
|
const auto& s = sects[i];
|
|
const char* osn =
|
|
(s.target_os == TargetOS::Linux) ? "linux" :
|
|
(s.target_os == TargetOS::Windows) ? "windows" :
|
|
(s.target_os == TargetOS::MacOS) ? "macos" : "unknown";
|
|
const char* kind =
|
|
(s.kind == SectionKind::NativeBlob) ? "NativeBlob" : "RawCode ";
|
|
std::printf(" [%u] %-7s %s code=@%llu+%llu",
|
|
i, osn, kind,
|
|
static_cast<unsigned long long>(s.code_off),
|
|
static_cast<unsigned long long>(s.code_size));
|
|
if (s.data_size > 0)
|
|
std::printf(" data=@%llu+%llu",
|
|
static_cast<unsigned long long>(s.data_off),
|
|
static_cast<unsigned long long>(s.data_size));
|
|
if (s.entry_offset > 0)
|
|
std::printf(" entry=+%llu", static_cast<unsigned long long>(s.entry_offset));
|
|
std::printf("\n");
|
|
}
|
|
|
|
// Symbol table
|
|
if (h.symtab_size < sizeof(SymbolTableHeader)) return;
|
|
f.seekg(static_cast<std::streamoff>(h.symtab_off));
|
|
SymbolTableHeader sth{};
|
|
f.read(reinterpret_cast<char*>(&sth), sizeof(sth));
|
|
if (!f || sth.entry_count == 0) return;
|
|
|
|
std::vector<SymbolEntry> syms(sth.entry_count);
|
|
f.read(reinterpret_cast<char*>(syms.data()),
|
|
static_cast<std::streamsize>(sth.entry_count * sizeof(SymbolEntry)));
|
|
std::string pool(sth.name_pool_size, '\0');
|
|
f.read(pool.data(), static_cast<std::streamsize>(sth.name_pool_size));
|
|
if (!f) return;
|
|
|
|
auto name_of = [&](const SymbolEntry& e) -> std::string {
|
|
if (e.name_off + e.name_len > sth.name_pool_size) return "?";
|
|
return pool.substr(e.name_off, e.name_len);
|
|
};
|
|
|
|
std::vector<std::string> exports_list, imports_list;
|
|
for (const auto& sym : syms) {
|
|
if (sym.section_index == kSymUndefined)
|
|
imports_list.push_back(name_of(sym));
|
|
else if (sym.flags & static_cast<std::uint8_t>(SymbolFlags::Exported))
|
|
exports_list.push_back(name_of(sym));
|
|
}
|
|
|
|
auto print_names = [](const char* label, const std::vector<std::string>& names) {
|
|
constexpr std::size_t kMax = 8;
|
|
std::printf(" %-8s (%zu):", label, names.size());
|
|
for (std::size_t i = 0; i < std::min(names.size(), kMax); ++i)
|
|
std::printf(" %s", names[i].c_str());
|
|
if (names.size() > kMax)
|
|
std::printf(" ... +%zu more", names.size() - kMax);
|
|
std::printf("\n");
|
|
};
|
|
|
|
std::printf("symbols (%u total):\n", sth.entry_count);
|
|
if (!exports_list.empty()) print_names("exports", exports_list);
|
|
if (!imports_list.empty()) print_names("imports", imports_list);
|
|
}
|
|
|
|
} // namespace voidlibc::voidlink
|