10#include "../linker/format.h"
11#include "../linker/module.h"
12#include "../linker/options.h"
13#include "../linker/reader.h"
14#include "../linker/section.h"
15#include "../linker/segment.h"
16#include "../linker/segment_manager.h"
17#include "../linker/writer.h"
18#include "../dumper/dumper.h"
105 static Relocation FromLinear(uint32_t address);
107 uint32_t GetOffset()
const;
109 bool operator ==(
const Relocation& other)
const;
111 bool operator <(
const Relocation& other)
const;
120 static constexpr uint32_t MAGIC_BEGIN = 0x0013EDC1;
121 static constexpr uint32_t MAGIC_END = 0xEDC10013;
122 static constexpr size_t SIZE = 19;
125 uint16_t maximum_extra_paragraphs;
126 uint16_t minimum_extra_paragraphs;
128 uint8_t lowest_used_interrupt;
129 uint8_t highest_used_interrupt;
130 uint8_t com_port_usage;
131 uint8_t lpt_port_usage;
132 uint8_t screen_usage;
147 std::shared_ptr<Linker::Contents>
image;
153 void Clear()
override;
160 void SetFileSize(uint32_t size);
162 uint32_t GetHeaderSize()
const;
164 uint32_t GetPifOffset()
const;
186 InitializeFields(header_align, file_align, stack);
229 unsigned FormatAdditionalSectionFlags(std::string section_name)
const override;
231 static std::vector<Linker::OptionDescription<void>> MemoryModelNames;
235 void SetModel(std::string model)
override;
237 std::shared_ptr<Linker::OptionCollector>
GetOptions()
override;
239 void SetOptions(std::map<std::string, std::string>& options)
override;
241 void OnNewSegment(std::shared_ptr<Linker::Segment> segment)
override;
265 uint32_t GetDataSize()
const;
276 std::string filename;
278 std::ifstream stream;
287 bool OpenAndCheckValidFile();
289 offset_t GetStubImageSize();
291 void WriteStubImage(std::ostream& out);
307 std::string filename;
309 std::ifstream stream;
316 uint32_t original_file_size = -1;
317 uint32_t stub_file_size = 0;
318 uint16_t stub_reloc_count = 0;
319 uint32_t original_header_size = 0;
320 uint32_t stub_header_size = 0;
321 uint16_t original_reloc_offset = 0;
322 uint16_t stub_reloc_offset = 0;
324 bool OpenAndCheckValidFile();
326 offset_t GetStubImageSize();
328 void WriteStubImage(std::ostream& out);
341 template <
typename T,
size_t N,
typename Predicate>
342 offset_t FindActualSignature(
Linker::Reader& rd, std::array<T, N>& signature, Predicate predicate,
bool search_win386_offset =
false)
344 offset_t file_offset = rd.
Tell();
346 if(file_offset == 0 && !predicate(signature))
352 uint32_t mz_image_end = rd.
ReadUnsigned(2, ::LittleEndian);
353 mz_image_end = (uint32_t(rd.
ReadUnsigned(2, ::LittleEndian)) << 9) - (-mz_image_end & 0x1FF);
355 uint32_t win386_header_offset = 0;
356 uint32_t ne_header_offset = 0;
359 if(search_win386_offset)
362 win386_header_offset = rd.
ReadUnsigned(4, ::LittleEndian);
369 if(ne_header_offset != 0 && ne_header_offset + N < file_end)
371 rd.
Seek(ne_header_offset);
375 if(predicate(signature))
377 file_offset = rd.
Tell() - N;
381 if(mz_image_end != 0 && mz_image_end != ne_header_offset && mz_image_end + N < file_end)
383 rd.
Seek(mz_image_end);
387 if(predicate(signature))
389 file_offset = rd.
Tell() - N;
393 if(win386_header_offset != 0 && win386_header_offset != ne_header_offset && win386_header_offset != mz_image_end && win386_header_offset + N < file_end)
395 rd.
Seek(win386_header_offset);
399 if(predicate(signature))
401 file_offset = rd.
Tell() - N;
414 template <
typename T,
size_t N>
415 offset_t FindActualSignature(
Linker::Reader& rd, std::array<T, N>& signature,
const char * expected_signature,
bool search_win386_offset =
false)
417 return FindActualSignature(
420 [expected_signature](std::array<T, N>& signature) {
return memcmp(signature.data(), expected_signature, N) == 0; },
421 search_win386_offset);
424 template <
typename T,
size_t N>
425 offset_t FindActualSignature(
Linker::Reader& rd, std::array<T, N>& signature,
const char * expected_signature1,
const char * expected_signature2,
bool search_win386_offset =
false)
427 return FindActualSignature(
430 [expected_signature1, expected_signature2](std::array<T, N>& signature) {
return memcmp(signature.data(), expected_signature1, N) == 0 || memcmp(signature.data(), expected_signature2, N) == 0; },
431 search_win386_offset);
434 template <
typename T,
size_t N>
435 offset_t FindActualSignature(
Linker::Reader& rd, std::array<T, N>& signature,
const char * expected_signature1,
const char * expected_signature2,
const char * expected_signature3)
437 return FindActualSignature(
440 [expected_signature1, expected_signature2, expected_signature3](std::array<T, N>& signature) {
return memcmp(signature.data(), expected_signature1, N) == 0 || memcmp(signature.data(), expected_signature2, N) == 0 || memcmp(signature.data(), expected_signature3, N) == 0; },
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:773
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:24
Helper class that contains the options interpreted by the format.
Definition options.h:474
Documents and handles command line options.
Definition options.h:306
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:20
void Seek(offset_t offset)
Jump to a specific location in the input stream.
Definition reader.cc:181
offset_t GetImageEnd()
Returns the last location that can be read.
Definition reader.cc:267
void ReadData(size_t count, void *data)
Read in a sequence of bytes.
Definition reader.cc:30
offset_t Tell()
Retrieve the current location.
Definition reader.cc:238
uint64_t ReadUnsigned(size_t bytes, EndianType endiantype)
Read an unsigned word.
Definition reader.cc:157
A helper class to collect sections into segments.
Definition segment_manager.h:32
A helper class, encapsulating functionality needed to export binary data.
Definition writer.h:15