17
17
#include < algorithm>
18
18
#include < fstream>
19
19
#include < iterator>
20
+ #include < numeric>
20
21
21
22
#include " easylogging++.h"
22
23
@@ -41,7 +42,8 @@ Builder::Builder(Binary* binary) :
41
42
patch_imports_{false },
42
43
build_relocations_{false },
43
44
build_tls_{false },
44
- build_resources_{false }
45
+ build_resources_{false },
46
+ build_overlay_{true }
45
47
{}
46
48
47
49
@@ -69,7 +71,10 @@ Builder& Builder::build_resources(bool flag) {
69
71
return *this ;
70
72
}
71
73
72
-
74
+ Builder& Builder::build_overlay (bool flag) {
75
+ this ->build_overlay_ = flag;
76
+ return *this ;
77
+ }
73
78
74
79
75
80
void Builder::write (const std::string& filename) const {
@@ -85,8 +90,6 @@ void Builder::write(const std::string& filename) const {
85
90
}
86
91
87
92
88
-
89
-
90
93
void Builder::build (void ) {
91
94
92
95
LOG (DEBUG) << " Rebuilding" << std::endl;
@@ -151,6 +154,12 @@ void Builder::build(void) {
151
154
152
155
LOG (DEBUG) << " [+] Rebuilding string table" << std::endl;
153
156
// this->build_string_table();
157
+
158
+ if (this ->binary_ ->overlay ().size () > 0 and this ->build_overlay_ ) {
159
+ this ->build_overlay ();
160
+ }
161
+
162
+
154
163
}
155
164
156
165
const std::vector<uint8_t >& Builder::get_build (void ) {
@@ -402,6 +411,25 @@ void Builder::build_string_table(void) {
402
411
// TODO
403
412
}
404
413
414
+ void Builder::build_overlay (void ) {
415
+ LOG (DEBUG) << " Building overlay" ;
416
+
417
+ const uint64_t last_section_offset = std::accumulate (
418
+ std::begin (this ->binary_ ->sections_ ),
419
+ std::end (this ->binary_ ->sections_ ), 0 ,
420
+ [] (uint64_t offset, const Section* section) {
421
+ return std::max<uint64_t >(section->offset () + section->size (), offset);
422
+ });
423
+
424
+ LOG (DEBUG) << " Overlay offset: 0x" << std::hex << last_section_offset;
425
+ LOG (DEBUG) << " Overlay size: " << std::dec << this ->binary_ ->overlay ().size ();
426
+
427
+ const size_t saved_offset = this ->ios_ .tellp ();
428
+ this ->ios_ .seekp (last_section_offset);
429
+ this ->ios_ .write (this ->binary_ ->overlay ());
430
+ this ->ios_ .seekp (saved_offset);
431
+ }
432
+
405
433
Builder& Builder::operator <<(const DosHeader& dos_header) {
406
434
407
435
pe_dos_header dosHeader;
@@ -519,6 +547,7 @@ std::ostream& operator<<(std::ostream& os, const Builder& b) {
519
547
os << std::setw (20 ) << " Builde relocations:" << b.build_relocations_ << std::endl;
520
548
os << std::setw (20 ) << " Builde TLS:" << b.build_tls_ << std::endl;
521
549
os << std::setw (20 ) << " Builder resources:" << b.build_resources_ << std::endl;
550
+ os << std::setw (20 ) << " Builder overlay:" << b.build_overlay_ << std::endl;
522
551
return os;
523
552
}
524
553
0 commit comments