refactor: use PEP-8 style instead of CamelCase.
This commit is contained in:
@@ -2,68 +2,69 @@
|
||||
|
||||
namespace makepdb::binary {
|
||||
|
||||
COFF::COFF(std::string_view Path) {
|
||||
COFF::COFF(std::string_view path) {
|
||||
using namespace object;
|
||||
|
||||
auto ObjOrErr = ObjectFile::createObjectFile(Path);
|
||||
if (!ObjOrErr) {
|
||||
auto obj_or_err = ObjectFile::createObjectFile(path);
|
||||
if (!obj_or_err) {
|
||||
throw std::runtime_error("Failed to create object file.");
|
||||
}
|
||||
|
||||
if (!isa<COFFObjectFile>(ObjOrErr->getBinary())) {
|
||||
if (!isa<COFFObjectFile>(obj_or_err->getBinary())) {
|
||||
throw std::runtime_error("Is not a valid PE file.");
|
||||
}
|
||||
|
||||
auto Bin = ObjOrErr->takeBinary();
|
||||
auto bin = obj_or_err->takeBinary();
|
||||
|
||||
OwningBinary = object::OwningBinary(
|
||||
static_unique_ptr_cast<COFFObjectFile>(std::move(Bin.first)),
|
||||
std::move(Bin.second)
|
||||
m_owning_binary = object::OwningBinary(
|
||||
static_unique_ptr_cast<COFFObjectFile>(std::move(bin.first)),
|
||||
std::move(bin.second)
|
||||
);
|
||||
}
|
||||
|
||||
codeview::PDB70DebugInfo COFF::DebugInfo() const {
|
||||
const codeview::DebugInfo* DebugInfo;
|
||||
StringRef PDBFileName;
|
||||
codeview::PDB70DebugInfo COFF::get_debug_info() const {
|
||||
const codeview::DebugInfo* debug_info;
|
||||
StringRef pdb_file_name;
|
||||
|
||||
if (OwningCOFF().getDebugPDBInfo(DebugInfo, PDBFileName) || !DebugInfo) {
|
||||
if (get_owning_coff().getDebugPDBInfo(debug_info, pdb_file_name)
|
||||
|| !debug_info) {
|
||||
throw std::runtime_error("Failed to get pdb info from coff file.");
|
||||
}
|
||||
|
||||
if (DebugInfo->Signature.CVSignature != OMF::Signature::PDB70) {
|
||||
if (debug_info->Signature.CVSignature != OMF::Signature::PDB70) {
|
||||
throw std::runtime_error("Unsupported PDB format.");
|
||||
}
|
||||
|
||||
return DebugInfo->PDB70;
|
||||
return debug_info->PDB70;
|
||||
}
|
||||
|
||||
size_t COFF::SectionIndex(uint64_t Offset) const {
|
||||
size_t COFF::get_section_index(uint64_t offset) const {
|
||||
using namespace object;
|
||||
|
||||
uint64_t CurrentIndex = 0;
|
||||
for (const SectionRef& Sec : OwningCOFF().sections()) {
|
||||
const coff_section* Section = OwningCOFF().getCOFFSection(Sec);
|
||||
if (Offset >= Section->VirtualAddress
|
||||
&& Offset < Section->VirtualAddress + Section->VirtualSize) {
|
||||
return CurrentIndex;
|
||||
uint64_t current_index = 0;
|
||||
for (const SectionRef& sec_ref : get_owning_coff().sections()) {
|
||||
const coff_section* section = get_owning_coff().getCOFFSection(sec_ref);
|
||||
if (offset >= section->VirtualAddress
|
||||
&& offset < section->VirtualAddress + section->VirtualSize) {
|
||||
return current_index;
|
||||
}
|
||||
CurrentIndex++;
|
||||
current_index++;
|
||||
}
|
||||
throw std::runtime_error("Offset is not in any section.");
|
||||
}
|
||||
|
||||
object::coff_section* COFF::SectionTable() {
|
||||
object::coff_section* COFF::get_section_table() {
|
||||
return reinterpret_cast<object::coff_section*>(
|
||||
OwningCOFF().section_begin()->getRawDataRefImpl().p
|
||||
get_owning_coff().section_begin()->getRawDataRefImpl().p
|
||||
);
|
||||
}
|
||||
|
||||
uint32_t COFF::NumberOfSections() const {
|
||||
return OwningCOFF().getNumberOfSections();
|
||||
uint32_t COFF::get_number_of_sections() const {
|
||||
return get_owning_coff().getNumberOfSections();
|
||||
}
|
||||
|
||||
object::COFFObjectFile const& COFF::OwningCOFF() const {
|
||||
return *OwningBinary.getBinary();
|
||||
object::COFFObjectFile const& COFF::get_owning_coff() const {
|
||||
return *m_owning_binary.getBinary();
|
||||
}
|
||||
|
||||
} // namespace makepdb::binary
|
||||
|
||||
@@ -6,18 +6,18 @@ namespace makepdb::binary {
|
||||
|
||||
class COFF {
|
||||
public:
|
||||
explicit COFF(std::string_view Path);
|
||||
explicit COFF(std::string_view path);
|
||||
|
||||
codeview::PDB70DebugInfo DebugInfo() const;
|
||||
codeview::PDB70DebugInfo get_debug_info() const;
|
||||
|
||||
size_t SectionIndex(uint64_t Offset) const;
|
||||
object::coff_section* SectionTable();
|
||||
uint32_t NumberOfSections() const;
|
||||
size_t get_section_index(uint64_t offset) const;
|
||||
object::coff_section* get_section_table();
|
||||
uint32_t get_number_of_sections() const;
|
||||
|
||||
object::COFFObjectFile const& OwningCOFF() const;
|
||||
object::COFFObjectFile const& get_owning_coff() const;
|
||||
|
||||
private:
|
||||
object::OwningBinary<object::COFFObjectFile> OwningBinary;
|
||||
object::OwningBinary<object::COFFObjectFile> m_owning_binary;
|
||||
};
|
||||
|
||||
} // namespace makepdb::binary
|
||||
|
||||
@@ -11,109 +11,114 @@ using namespace llvm::pdb;
|
||||
|
||||
namespace makepdb::binary {
|
||||
|
||||
PDB::PDB(COFF&& COFF, SymbolData&& SymbolData)
|
||||
: OwningCOFF(std::move(COFF)),
|
||||
OwningSymbolData(std::move(SymbolData)),
|
||||
Builder(Allocator) {
|
||||
constexpr uint32_t BlockSize = 4096;
|
||||
if (Builder.initialize(BlockSize)) {
|
||||
PDB::PDB(COFF&& coff, SymbolData&& symbol_data)
|
||||
: m_owning_coff(std::move(coff)),
|
||||
m_owning_symbol_data(std::move(symbol_data)),
|
||||
m_builder(m_allocator) {
|
||||
constexpr uint32_t block_size = 4096;
|
||||
if (m_builder.initialize(block_size)) {
|
||||
throw std::runtime_error("Failed to initialize pdb file builder.");
|
||||
}
|
||||
|
||||
for (uint32_t I = 0; I < pdb::kSpecialStreamCount; ++I) {
|
||||
if (!Builder.getMsfBuilder().addStream(0)) {
|
||||
if (!m_builder.getMsfBuilder().addStream(0)) {
|
||||
throw std::runtime_error("Failed to add initial stream.");
|
||||
}
|
||||
}
|
||||
|
||||
ImageBase = OwningCOFF.OwningCOFF().getImageBase();
|
||||
m_image_base = m_owning_coff.get_owning_coff().getImageBase();
|
||||
}
|
||||
|
||||
void PDB::WriteTo(std::string_view Path) {
|
||||
Build();
|
||||
void PDB::write(std::string_view path) {
|
||||
build();
|
||||
|
||||
auto Guid = Builder.getInfoBuilder().getGuid();
|
||||
if (Builder.commit(Path, &Guid)) {
|
||||
auto guid = m_builder.getInfoBuilder().getGuid();
|
||||
if (m_builder.commit(path, &guid)) {
|
||||
throw std::runtime_error("Failed to create pdb!");
|
||||
}
|
||||
}
|
||||
|
||||
void PDB::Build() {
|
||||
BuildInfo();
|
||||
BuildDBI();
|
||||
BuildTPI();
|
||||
BuildGSI();
|
||||
void PDB::build() {
|
||||
build_Info();
|
||||
build_DBI();
|
||||
build_TPI();
|
||||
build_GSI();
|
||||
}
|
||||
|
||||
void PDB::BuildInfo() {
|
||||
auto PDBInfo = OwningCOFF.DebugInfo();
|
||||
auto& InfoBuilder = Builder.getInfoBuilder();
|
||||
void PDB::build_Info() {
|
||||
auto pdb_info = m_owning_coff.get_debug_info();
|
||||
auto& info_builder = m_builder.getInfoBuilder();
|
||||
|
||||
InfoBuilder.setVersion(PdbRaw_ImplVer::PdbImplVC70);
|
||||
InfoBuilder.setAge(PDBInfo.Age);
|
||||
InfoBuilder.setGuid(*reinterpret_cast<codeview::GUID*>(PDBInfo.Signature));
|
||||
InfoBuilder.addFeature(PdbRaw_FeatureSig::VC140);
|
||||
info_builder.setVersion(PdbRaw_ImplVer::PdbImplVC70);
|
||||
info_builder.setAge(pdb_info.Age);
|
||||
info_builder.setGuid(*reinterpret_cast<codeview::GUID*>(pdb_info.Signature)
|
||||
);
|
||||
info_builder.addFeature(PdbRaw_FeatureSig::VC140);
|
||||
}
|
||||
|
||||
void PDB::BuildDBI() {
|
||||
auto PDBInfo = OwningCOFF.DebugInfo();
|
||||
auto& DbiBuilder = Builder.getDbiBuilder();
|
||||
void PDB::build_DBI() {
|
||||
auto pdb_info = m_owning_coff.get_debug_info();
|
||||
auto& DBI = m_builder.getDbiBuilder();
|
||||
|
||||
DbiBuilder.setVersionHeader(PdbRaw_DbiVer::PdbDbiV70);
|
||||
DbiBuilder.setAge(PDBInfo.Age);
|
||||
DbiBuilder.setMachineType(PDB_Machine::Amd64);
|
||||
DbiBuilder.setFlags(DbiFlags::FlagStrippedMask);
|
||||
DbiBuilder.setBuildNumber(14, 11); // LLVM is compatible with LINK 14.11
|
||||
DBI.setVersionHeader(PdbRaw_DbiVer::PdbDbiV70);
|
||||
DBI.setAge(pdb_info.Age);
|
||||
DBI.setMachineType(PDB_Machine::Amd64);
|
||||
DBI.setFlags(DbiFlags::FlagStrippedMask);
|
||||
DBI.setBuildNumber(14, 11); // LLVM is compatible with LINK 14.11
|
||||
|
||||
// Add sections.
|
||||
auto SectionTable = OwningCOFF.SectionTable();
|
||||
auto NumberOfSections = OwningCOFF.NumberOfSections();
|
||||
auto section_table = m_owning_coff.get_section_table();
|
||||
auto number_of_sections = m_owning_coff.get_number_of_sections();
|
||||
|
||||
auto SectionDataRef = ArrayRef<uint8_t>(
|
||||
(uint8_t*)SectionTable,
|
||||
OwningCOFF.NumberOfSections() * sizeof(object::coff_section)
|
||||
auto section_data_ref = ArrayRef<uint8_t>(
|
||||
(uint8_t*)section_table,
|
||||
m_owning_coff.get_number_of_sections() * sizeof(object::coff_section)
|
||||
);
|
||||
|
||||
auto SectionTableRef = ArrayRef<object::coff_section>(
|
||||
(const object::coff_section*)SectionDataRef.data(),
|
||||
NumberOfSections
|
||||
auto section_table_ref = ArrayRef<object::coff_section>(
|
||||
(const object::coff_section*)section_data_ref.data(),
|
||||
number_of_sections
|
||||
);
|
||||
|
||||
DbiBuilder.createSectionMap(SectionTableRef);
|
||||
DBI.createSectionMap(section_table_ref);
|
||||
|
||||
// Add COFF section header stream.
|
||||
if (DbiBuilder.addDbgStream(DbgHeaderType::SectionHdr, SectionDataRef)) {
|
||||
if (DBI.addDbgStream(DbgHeaderType::SectionHdr, section_data_ref)) {
|
||||
throw std::runtime_error("Failed to add dbg stream.");
|
||||
}
|
||||
}
|
||||
|
||||
void PDB::BuildTPI() {
|
||||
Builder.getTpiBuilder().setVersionHeader(PdbRaw_TpiVer::PdbTpiV80);
|
||||
Builder.getIpiBuilder().setVersionHeader(PdbRaw_TpiVer::PdbTpiV80);
|
||||
void PDB::build_TPI() {
|
||||
m_builder.getTpiBuilder().setVersionHeader(PdbRaw_TpiVer::PdbTpiV80);
|
||||
m_builder.getIpiBuilder().setVersionHeader(PdbRaw_TpiVer::PdbTpiV80);
|
||||
}
|
||||
|
||||
void PDB::BuildGSI() {
|
||||
std::vector<BulkPublic> PublicsIn;
|
||||
OwningSymbolData.forEach([&PublicsIn, this](const SymbolDataEntity& E) {
|
||||
BulkPublic Symbol;
|
||||
void PDB::build_GSI() {
|
||||
std::vector<BulkPublic> publics;
|
||||
m_owning_symbol_data.for_each([&publics,
|
||||
this](const SymbolDataEntity& entity) {
|
||||
BulkPublic symbol;
|
||||
|
||||
auto SectionIndex = OwningCOFF.SectionIndex(E.RVA - ImageBase);
|
||||
auto SectionOrErr =
|
||||
OwningCOFF.OwningCOFF().getSection(SectionIndex + 1);
|
||||
if (!SectionOrErr) {
|
||||
auto section_index =
|
||||
m_owning_coff.get_section_index(entity.rva - m_image_base);
|
||||
auto section_or_err =
|
||||
m_owning_coff.get_owning_coff().getSection(section_index + 1);
|
||||
if (!section_or_err) {
|
||||
throw std::runtime_error("Invalid section.");
|
||||
}
|
||||
|
||||
Symbol.Name = strdup(E.SymbolName.c_str());
|
||||
Symbol.NameLen = E.SymbolName.size();
|
||||
Symbol.Segment = SectionIndex + 1;
|
||||
Symbol.Offset = E.RVA - ImageBase - SectionOrErr.get()->VirtualAddress;
|
||||
if (E.IsFunction) Symbol.setFlags(codeview::PublicSymFlags::Function);
|
||||
symbol.Name = strdup(entity.symbol_name.c_str());
|
||||
symbol.NameLen = entity.symbol_name.size();
|
||||
symbol.Segment = section_index + 1;
|
||||
symbol.Offset =
|
||||
entity.rva - m_image_base - section_or_err.get()->VirtualAddress;
|
||||
if (entity.is_function)
|
||||
symbol.setFlags(codeview::PublicSymFlags::Function);
|
||||
|
||||
PublicsIn.emplace_back(Symbol);
|
||||
publics.emplace_back(symbol);
|
||||
});
|
||||
|
||||
Builder.getGsiBuilder().addPublicSymbols(std::move(PublicsIn));
|
||||
m_builder.getGsiBuilder().addPublicSymbols(std::move(publics));
|
||||
}
|
||||
|
||||
} // namespace makepdb::binary
|
||||
|
||||
@@ -10,25 +10,25 @@ namespace makepdb::binary {
|
||||
|
||||
class PDB {
|
||||
public:
|
||||
explicit PDB(COFF&& COFF, SymbolData&& SymbolData);
|
||||
explicit PDB(COFF&& coff, SymbolData&& symbol_data);
|
||||
|
||||
void WriteTo(std::string_view Path);
|
||||
void write(std::string_view path);
|
||||
|
||||
private:
|
||||
void Build();
|
||||
void build();
|
||||
|
||||
inline void BuildInfo();
|
||||
inline void BuildDBI();
|
||||
inline void BuildTPI();
|
||||
inline void BuildGSI();
|
||||
inline void build_Info();
|
||||
inline void build_DBI();
|
||||
inline void build_TPI();
|
||||
inline void build_GSI();
|
||||
|
||||
COFF OwningCOFF;
|
||||
SymbolData OwningSymbolData;
|
||||
COFF m_owning_coff;
|
||||
SymbolData m_owning_symbol_data;
|
||||
|
||||
uint64_t ImageBase;
|
||||
uint64_t m_image_base;
|
||||
|
||||
BumpPtrAllocator Allocator;
|
||||
pdb::PDBFileBuilder Builder;
|
||||
BumpPtrAllocator m_allocator;
|
||||
pdb::PDBFileBuilder m_builder;
|
||||
};
|
||||
|
||||
} // namespace makepdb::binary
|
||||
|
||||
@@ -47,7 +47,7 @@ int main(int argc, char* argv[]) try {
|
||||
|
||||
binary::PDB pdb(std::move(server_program), std::move(symbol_data));
|
||||
|
||||
pdb.WriteTo(args.output_path);
|
||||
pdb.write(args.output_path);
|
||||
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
|
||||
@@ -4,26 +4,29 @@
|
||||
|
||||
namespace makepdb {
|
||||
|
||||
SymbolData::SymbolData(std::string_view Path) {
|
||||
std::ifstream IFS(Path.data());
|
||||
if (!IFS) {
|
||||
SymbolData::SymbolData(std::string_view path) {
|
||||
std::ifstream ifs(path.data());
|
||||
if (!ifs) {
|
||||
throw std::runtime_error("Failed to open data path.");
|
||||
}
|
||||
|
||||
auto Data = nlohmann::json::parse(IFS);
|
||||
if (Data["version"] != 1) {
|
||||
auto data = nlohmann::json::parse(ifs);
|
||||
if (data["version"] != 1) {
|
||||
throw std::runtime_error("Unsupported data version.");
|
||||
}
|
||||
|
||||
for (const auto& E : Data["data"]) {
|
||||
Entities.emplace(
|
||||
SymbolDataEntity{E["symbol"], E["rva"], E["is_function"]}
|
||||
);
|
||||
for (const auto& entity : data["data"]) {
|
||||
m_entities.emplace(SymbolDataEntity{
|
||||
entity["symbol"],
|
||||
entity["rva"],
|
||||
entity["is_function"]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void SymbolData::forEach(const std::function<void(SymbolDataEntity)> Callback) {
|
||||
for (const auto& E : Entities) Callback(E);
|
||||
void SymbolData::for_each(const std::function<void(SymbolDataEntity)> callback
|
||||
) {
|
||||
for (const auto& entity : m_entities) callback(entity);
|
||||
}
|
||||
|
||||
} // namespace makepdb
|
||||
|
||||
@@ -3,20 +3,20 @@
|
||||
namespace makepdb {
|
||||
|
||||
struct SymbolDataEntity {
|
||||
std::string SymbolName;
|
||||
uint64_t RVA;
|
||||
bool IsFunction;
|
||||
std::string symbol_name;
|
||||
uint64_t rva;
|
||||
bool is_function;
|
||||
|
||||
bool operator==(const SymbolDataEntity& other) const {
|
||||
return SymbolName == other.SymbolName && RVA == other.RVA
|
||||
&& IsFunction == other.IsFunction;
|
||||
return symbol_name == other.symbol_name && rva == other.rva
|
||||
&& is_function == other.is_function;
|
||||
}
|
||||
|
||||
struct H {
|
||||
size_t operator()(const SymbolDataEntity E) const {
|
||||
size_t h1 = std::hash<std::string>{}(E.SymbolName);
|
||||
size_t h2 = std::hash<uint64_t>{}(E.RVA);
|
||||
size_t h3 = std::hash<bool>{}(E.IsFunction);
|
||||
struct Hash {
|
||||
size_t operator()(const SymbolDataEntity entity) const {
|
||||
size_t h1 = std::hash<std::string>{}(entity.symbol_name);
|
||||
size_t h2 = std::hash<uint64_t>{}(entity.rva);
|
||||
size_t h3 = std::hash<bool>{}(entity.is_function);
|
||||
return h1 ^ (h2 << 1) ^ (h3 << 2);
|
||||
}
|
||||
};
|
||||
@@ -24,12 +24,12 @@ struct SymbolDataEntity {
|
||||
|
||||
class SymbolData {
|
||||
public:
|
||||
explicit SymbolData(std::string_view Path);
|
||||
explicit SymbolData(std::string_view path);
|
||||
|
||||
void forEach(const std::function<void(SymbolDataEntity)> Callback);
|
||||
void for_each(const std::function<void(SymbolDataEntity)> callback);
|
||||
|
||||
private:
|
||||
std::unordered_set<SymbolDataEntity, SymbolDataEntity::H> Entities;
|
||||
std::unordered_set<SymbolDataEntity, SymbolDataEntity::Hash> m_entities;
|
||||
};
|
||||
|
||||
} // namespace makepdb
|
||||
|
||||
Reference in New Issue
Block a user