refactor: add some defs.

This commit is contained in:
2025-03-01 00:41:25 +08:00
parent fc42c2b6eb
commit c51f7336e5
18 changed files with 42 additions and 29 deletions

View File

@@ -6,7 +6,7 @@ namespace di::data_format {
constexpr int BOUND_SYMBOL_LIST_FORMAT_VERSION = 1;
void BoundSymbolList::read(const std::filesystem::path& path) {
void BoundSymbolList::read(const fs::path& path) {
std::ifstream ifs(path);
if (!ifs) {
throw std::runtime_error("Failed to open data path.");
@@ -25,7 +25,7 @@ void BoundSymbolList::read(const std::filesystem::path& path) {
}
}
void BoundSymbolList::write(const std::filesystem::path& path) const {
void BoundSymbolList::write(const fs::path& path) const {
std::ofstream ofs(path);
if (!ofs) {
throw std::runtime_error("Failed to open file!");
@@ -47,7 +47,7 @@ void BoundSymbolList::write(const std::filesystem::path& path) const {
void BoundSymbolList::record(
std::string_view symbol,
uint64_t rva,
rva_t rva,
bool is_function
) {
m_entities.emplace(BoundSymbol{std::string(symbol), rva, is_function});

View File

@@ -9,10 +9,10 @@ class BoundSymbolList : public IOBase {
public:
using for_each_callback_t = std::function<void(BoundSymbol const&)>;
void read(const std::filesystem::path& path) override;
void write(const std::filesystem::path& path) const override;
void read(const fs::path& path) override;
void write(const fs::path& path) const override;
void record(std::string_view symbol, uint64_t rva, bool is_function);
void record(std::string_view symbol, rva_t rva, bool is_function);
constexpr void for_each(const for_each_callback_t& callback) const {
for (const auto& entity : m_entities) callback(entity);

View File

@@ -2,7 +2,7 @@
namespace di::data_format {
void RawText::read(const std::filesystem::path& path) {
void RawText::read(const fs::path& path) {
std::ifstream ifs(path);
auto size = std::filesystem::file_size(path);
@@ -10,7 +10,7 @@ void RawText::read(const std::filesystem::path& path) {
m_data.assign(std::istreambuf_iterator<char>(ifs), {});
}
void RawText::write(const std::filesystem::path& path) const {
void RawText::write(const fs::path& path) const {
std::ofstream ofs(path);
if (!ofs) {
throw std::runtime_error("Failed to open save file.");

View File

@@ -6,8 +6,8 @@ namespace di::data_format {
class RawText : public IOBase {
public:
void read(const std::filesystem::path& path) override;
void write(const std::filesystem::path& path) const override;
void read(const fs::path& path) override;
void write(const fs::path& path) const override;
void record(std::string_view line);

View File

@@ -12,7 +12,7 @@ using namespace llvm::pdb;
namespace di::data_format {
void RawTypeData::read(const std::filesystem::path& path) {
void RawTypeData::read(const fs::path& path) {
std::unique_ptr<IPDBSession> pdb_session;
if (llvm::pdb::loadDataForPDB(
PDB_ReaderType::Native,

View File

@@ -15,8 +15,8 @@ public:
RawTypeData() : m_storaged_IPI(m_allocator), m_storaged_TPI(m_allocator) {}
void read(const std::filesystem::path& path) override;
void write(const std::filesystem::path& path) const override {
void read(const fs::path& path) override;
void write(const fs::path& path) const override {
throw std::runtime_error("Unsupported operation.");
}

View File

@@ -6,7 +6,7 @@ namespace di {
struct BoundSymbol {
std::string m_symbol_name;
uint64_t m_rva;
rva_t m_rva;
bool m_is_function;
bool operator==(const BoundSymbol& other) const {

View File

@@ -2,7 +2,7 @@
namespace di::data_format {
void TypedSymbolList::read(const std::filesystem::path& path) {
void TypedSymbolList::read(const fs::path& path) {
std::ifstream ifs(path);
if (!ifs) {
throw std::runtime_error("Failed to open symlist file.");
@@ -27,7 +27,7 @@ void TypedSymbolList::read(const std::filesystem::path& path) {
}
}
void TypedSymbolList::write(const std::filesystem::path& path) const {
void TypedSymbolList::write(const fs::path& path) const {
std::ofstream ofs(path);
for (const auto& [symbol, decl_type] : m_data) {
ofs << symbol << ", " << decl_type.string() << "\n";

View File

@@ -13,8 +13,8 @@ public:
// this method in this class supports multiple calls (reading multiple
// files)
void read(const std::filesystem::path& path) override;
void write(const std::filesystem::path& path) const override;
void read(const fs::path& path) override;
void write(const fs::path& path) const override;
void record(const std::string& symbol, DeclType type);

View File

@@ -51,3 +51,5 @@ struct hash<std::tuple<TT...>> {
};
} // namespace std
namespace fs = std::filesystem;

View File

@@ -2,7 +2,7 @@
namespace di::object_file {
COFF::COFF(const std::filesystem::path& path) {
COFF::COFF(const fs::path& path) {
using namespace object;
auto obj_or_err = ObjectFile::createObjectFile(path.string());
@@ -38,10 +38,10 @@ codeview::PDB70DebugInfo COFF::get_debug_info() const {
return debug_info->PDB70;
}
size_t COFF::get_section_index(uint64_t offset) const {
size_t COFF::get_section_index(size_t offset) const {
using namespace object;
uint64_t current_index = 0;
size_t current_index{};
for (const SectionRef& sec_ref : get_owning_coff().sections()) {
const coff_section* section = get_owning_coff().getCOFFSection(sec_ref);
if (offset >= section->VirtualAddress

View File

@@ -6,11 +6,11 @@ namespace di::object_file {
class COFF {
public:
explicit COFF(const std::filesystem::path& path);
explicit COFF(const fs::path& path);
codeview::PDB70DebugInfo get_debug_info() const;
size_t get_section_index(uint64_t offset) const;
size_t get_section_index(size_t offset) const;
object::coff_section* get_section_table();
uint32_t get_number_of_sections() const;

View File

@@ -12,7 +12,7 @@ using namespace llvm::pdb;
namespace di::object_file {
PDB::PDB() : m_builder(m_allocator) {
constexpr uint32_t block_size = 4096;
constexpr auto block_size = 4096;
if (m_builder.initialize(block_size)) {
throw std::runtime_error("Failed to initialize pdb file builder.");
}
@@ -24,7 +24,7 @@ PDB::PDB() : m_builder(m_allocator) {
}
}
void PDB::write(const std::filesystem::path& path) {
void PDB::write(const fs::path& path) {
build();
codeview::GUID out_guid;

View File

@@ -30,7 +30,7 @@ public:
m_owning_raw_type_data = std::move(raw_type_data);
}
void write(const std::filesystem::path& path);
void write(const fs::path& path);
private:
void build();
@@ -44,7 +44,7 @@ private:
owning_symbol_data_t m_owning_symbol_data;
owning_type_data_t m_owning_raw_type_data;
uint64_t m_image_base;
addr_t m_image_base;
BumpPtrAllocator m_allocator;
pdb::PDBFileBuilder m_builder;

View File

@@ -27,3 +27,4 @@ using namespace llvm;
// Helper
#include "nonstd.h"
#include "typedef.h"

View File

@@ -76,7 +76,7 @@ int main(int argc, char* argv[]) try {
if (rva) {
bound_symbol_list.record(
symbol.m_name,
reinterpret_cast<uint64_t>(rva),
reinterpret_cast<uintptr_t>(rva),
symbol.m_type.is_function()
);
} else {

View File

@@ -73,7 +73,7 @@ int main(int argc, char* argv[]) try {
auto publics_symbols =
publics_symbol_stream->getSymbolArray().getUnderlyingStream();
for (uint32_t offset : publics_stream->getPublicsTable()) {
for (auto offset : publics_stream->getPublicsTable()) {
auto cv_symbol = readSymbolFromStream(publics_symbols, offset);
auto public_sym32 =
SymbolDeserializer::deserializeAs<PublicSym32>(cv_symbol.get());

10
src/typedef.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
namespace di {
using rva_t = uint32_t;
using addr_t = uint64_t;
using hash_t = uint64_t;
} // namespace di