Skip to content

Commit fcc75dd

Browse files
1orenz0romainthomas
authored andcommitted
Parse PE debug data directory as a list of debug entries
PE's DATA_DIRECTORY_DEBUG does not define a single debug entry, but a list of struct pe_debug entries (the number of entries is determined by the data directory's size). This commit reflect this layout by returning a vector<> when calling PE.debug()
1 parent 3ac2067 commit fcc75dd

File tree

14 files changed

+77
-36
lines changed

14 files changed

+77
-36
lines changed

api/python/PE/objects/pyBinary.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ void create<Binary>(py::module& m) {
135135
.def_property_readonly("has_signature", &Binary::has_signature,
136136
"``True`` if the binary is signed (" RST_CLASS_REF(lief.PE.Signature) ")")
137137

138+
.def_property_readonly("is_reproducible_build", &Binary::is_reproducible_build,
139+
"``True`` if the binary was compiled with a reproducible build directive (" RST_CLASS_REF(lief.PE.Debug) ")")
140+
138141
.def_property_readonly("functions",
139142
&Binary::functions,
140143
"**All** " RST_CLASS_REF(lief.Function) " found in the binary")
@@ -153,10 +156,9 @@ void create<Binary>(py::module& m) {
153156
"Return the " RST_CLASS_REF(lief.PE.Signature) " object",
154157
py::return_value_policy::reference)
155158

156-
157159
.def_property_readonly("debug",
158-
static_cast<Debug& (Binary::*)(void)>(&Binary::debug),
159-
"Return the " RST_CLASS_REF(lief.PE.Debug) " object",
160+
static_cast<debug_entries_t& (Binary::*)(void)>(&Binary::debug),
161+
"Return the " RST_CLASS_REF(lief.PE.Debug) "",
160162
py::return_value_policy::reference)
161163

162164
.def_property_readonly("load_configuration",

api/python/PE/pyEnums.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,12 @@ void init_enums(py::module& m) {
253253
.value(PY_ENUM(LIEF::PE::DEBUG_TYPES::IMAGE_DEBUG_TYPE_OMAP_TO_SRC))
254254
.value(PY_ENUM(LIEF::PE::DEBUG_TYPES::IMAGE_DEBUG_TYPE_OMAP_FROM_SRC))
255255
.value(PY_ENUM(LIEF::PE::DEBUG_TYPES::IMAGE_DEBUG_TYPE_BORLAND))
256-
.value(PY_ENUM(LIEF::PE::DEBUG_TYPES::IMAGE_DEBUG_TYPE_CLSID));
256+
.value(PY_ENUM(LIEF::PE::DEBUG_TYPES::IMAGE_DEBUG_TYPE_CLSID))
257+
.value(PY_ENUM(LIEF::PE::DEBUG_TYPES::IMAGE_DEBUG_TYPE_VC_FEATURE))
258+
.value(PY_ENUM(LIEF::PE::DEBUG_TYPES::IMAGE_DEBUG_TYPE_POGO))
259+
.value(PY_ENUM(LIEF::PE::DEBUG_TYPES::IMAGE_DEBUG_TYPE_ILTCG))
260+
.value(PY_ENUM(LIEF::PE::DEBUG_TYPES::IMAGE_DEBUG_TYPE_MPX))
261+
.value(PY_ENUM(LIEF::PE::DEBUG_TYPES::IMAGE_DEBUG_TYPE_REPRO));
257262

258263

259264
LIEF::enum_<LIEF::PE::RESOURCE_TYPES>(m, "RESOURCE_TYPES")

examples/cpp/pe_reader.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ int main(int argc, char **argv) {
8989

9090
if (binary->has_debug()) {
9191
std::cout << "== Debug ==" << std::endl;
92-
std::cout << binary->debug() << std::endl;
92+
for (const Debug& debug : binary->debug()) {
93+
std::cout << debug << std::endl;
94+
}
9395
}
9496

9597

include/LIEF/PE/Binary.hpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ class LIEF_API Binary : public LIEF::Binary {
144144
//! @brief Check if the current binary has a load configuration
145145
bool has_configuration(void) const;
146146

147+
//! @brief Check if the current binary has been built has reproducible, replacing timestamps by a compile hash.
148+
//!
149+
//! @see Debug
150+
bool is_reproducible_build(void) const;
151+
147152
//! @brief Return the Signature object if the bianry is signed
148153
const Signature& signature(void) const;
149154

@@ -237,9 +242,9 @@ class LIEF_API Binary : public LIEF::Binary {
237242

238243
bool has(DATA_DIRECTORY index) const;
239244

240-
//! @brief Return the Debug object
241-
Debug& debug(void);
242-
const Debug& debug(void) const;
245+
//! @brief Return the debug_entries_t object
246+
debug_entries_t& debug(void);
247+
const debug_entries_t& debug(void) const;
243248

244249
//! @brief Retrun the LoadConfiguration object
245250
const LoadConfiguration& load_configuration(void) const;
@@ -431,6 +436,7 @@ class LIEF_API Binary : public LIEF::Binary {
431436
bool has_relocations_;
432437
bool has_debug_;
433438
bool has_configuration_;
439+
bool is_reproducible_build_;
434440

435441
Signature signature_;
436442
TLS tls_;
@@ -442,7 +448,7 @@ class LIEF_API Binary : public LIEF::Binary {
442448
ResourceNode* resources_;
443449
imports_t imports_;
444450
Export export_;
445-
Debug debug_;
451+
debug_entries_t debug_;
446452
std::vector<uint8_t> overlay_;
447453
std::vector<uint8_t> dos_stub_;
448454

include/LIEF/PE/Parser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class LIEF_API Parser : public LIEF::Parser {
8585

8686
void parse_export_table(void);
8787
void parse_debug(void);
88-
void parse_debug_code_view(void);
88+
void parse_debug_code_view(Debug& debug_info);
8989

9090
template<typename PE_T>
9191
void parse_tls(void);

include/LIEF/PE/enums.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ enum _LIEF_EN(DEBUG_TYPES) {
266266
_LIEF_EI(IMAGE_DEBUG_TYPE_OMAP_TO_SRC) = 7, ///< The mapping from an RVA in image to an RVA in source image.
267267
_LIEF_EI(IMAGE_DEBUG_TYPE_OMAP_FROM_SRC) = 8, ///< The mapping from an RVA in source image to an RVA in image.
268268
_LIEF_EI(IMAGE_DEBUG_TYPE_BORLAND) = 9, ///< Reserved for Borland.
269+
_LIEF_EI(IMAGE_DEBUG_TYPE_RESERVED10) = 10, ///< Reserved for future use.
269270
_LIEF_EI(IMAGE_DEBUG_TYPE_CLSID) = 11,
270271
_LIEF_EI(IMAGE_DEBUG_TYPE_VC_FEATURE) = 12,
271272
_LIEF_EI(IMAGE_DEBUG_TYPE_POGO) = 13,

include/LIEF/PE/type_traits.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ using export_entries_t = std::vector<ExportEntry>;
6565
using it_export_entries = ref_iterator<export_entries_t&>;
6666
using it_const_export_entries = const_ref_iterator<const export_entries_t&>;
6767

68+
using debug_entries_t = std::vector<Debug>;
69+
using it_debug_entries = ref_iterator<debug_entries_t&>;
70+
using it_const_debug_entries = const_ref_iterator<const debug_entries_t&>;
71+
6872
using symbols_t = std::vector<Symbol>;
6973
using it_symbols = ref_iterator<symbols_t&>;
7074
using it_const_symbols = const_ref_iterator<const symbols_t&>;

include/LIEF/PE/undef.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@
242242
#undef IMAGE_DEBUG_TYPE_OMAP_TO_SRC
243243
#undef IMAGE_DEBUG_TYPE_OMAP_FROM_SRC
244244
#undef IMAGE_DEBUG_TYPE_BORLAND
245+
#undef IMAGE_DEBUG_TYPE_RESERVED10
245246
#undef IMAGE_DEBUG_TYPE_CLSID
246247
#undef IMAGE_DEBUG_TYPE_VC_FEATURE
247248
#undef IMAGE_DEBUG_TYPE_POGO

src/PE/Binary.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Binary::Binary(void) :
9292
has_relocations_{false},
9393
has_debug_{false},
9494
has_configuration_{false},
95+
is_reproducible_build_{false},
9596
tls_{},
9697
sections_{},
9798
data_directories_{},
@@ -358,6 +359,9 @@ bool Binary::has_debug(void) const {
358359
return this->has_debug_;
359360
}
360361

362+
bool Binary::is_reproducible_build(void) const {
363+
return this->is_reproducible_build_;
364+
}
361365

362366
bool Binary::has_configuration(void) const {
363367
return this->has_configuration_ and this->load_configuration_ != nullptr;
@@ -978,12 +982,12 @@ it_const_data_directories Binary::data_directories(void) const {
978982
}
979983

980984

981-
Debug& Binary::debug(void) {
982-
return const_cast<Debug&>(static_cast<const Binary*>(this)->debug());
985+
debug_entries_t& Binary::debug(void) {
986+
return const_cast<debug_entries_t&>(static_cast<const Binary*>(this)->debug());
983987
}
984988

985989

986-
const Debug& Binary::debug(void) const {
990+
const debug_entries_t& Binary::debug(void) const {
987991
return this->debug_;
988992
}
989993

@@ -1410,7 +1414,9 @@ std::ostream& Binary::print(std::ostream& os) const {
14101414
if (this->has_debug()) {
14111415
os << "Debug" << std::endl;
14121416
os << "=====" << std::endl;
1413-
os << this->debug() << std::endl;
1417+
for (const Debug& debug : this->debug()) {
1418+
os << debug << std::endl;
1419+
}
14141420
os << std::endl;
14151421
}
14161422

src/PE/EnumToString.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ const char* to_string(RELOCATIONS_BASE_TYPES e) {
385385

386386

387387
const char* to_string(DEBUG_TYPES e) {
388-
CONST_MAP(DEBUG_TYPES, const char*, 16) enumStrings {
388+
CONST_MAP(DEBUG_TYPES, const char*, 17) enumStrings {
389389
{ DEBUG_TYPES::IMAGE_DEBUG_TYPE_UNKNOWN, "UNKNOWN" },
390390
{ DEBUG_TYPES::IMAGE_DEBUG_TYPE_COFF, "COFF" },
391391
{ DEBUG_TYPES::IMAGE_DEBUG_TYPE_CODEVIEW, "CODEVIEW" },
@@ -396,6 +396,7 @@ const char* to_string(DEBUG_TYPES e) {
396396
{ DEBUG_TYPES::IMAGE_DEBUG_TYPE_OMAP_TO_SRC, "SRC" },
397397
{ DEBUG_TYPES::IMAGE_DEBUG_TYPE_OMAP_FROM_SRC, "SRC" },
398398
{ DEBUG_TYPES::IMAGE_DEBUG_TYPE_BORLAND, "BORLAND" },
399+
{ DEBUG_TYPES::IMAGE_DEBUG_TYPE_RESERVED10, "RESERVED" },
399400
{ DEBUG_TYPES::IMAGE_DEBUG_TYPE_CLSID, "CLSID" },
400401
{ DEBUG_TYPES::IMAGE_DEBUG_TYPE_VC_FEATURE, "VC_FEATURE" },
401402
{ DEBUG_TYPES::IMAGE_DEBUG_TYPE_POGO, "POGO" },

0 commit comments

Comments
 (0)