RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
mzexe.h
1#ifndef MZEXE_H
2#define MZEXE_H
3
4#include <iomanip>
5#include <set>
6#include <string>
7#include <vector>
8#include "../common.h"
9#include "../linker/linker.h"
10#include "../linker/format.h"
11#include "../linker/module.h"
12#include "../linker/reader.h"
13#include "../linker/section.h"
14#include "../linker/segment.h"
15#include "../linker/writer.h"
16#include "../dumper/dumper.h"
17
18namespace Microsoft
19{
35 {
36 public:
37 /* * * General members * * */
38
49
51 char signature[2] = { 'M', 'Z' }; /* TODO: make parameter */
52
54 uint16_t last_block_size = 0;
56 uint16_t file_size_blocks = 0; /* TODO: consider making file size a parameter */
57
58 uint32_t GetFileSize() const;
59
61 uint16_t relocation_count = 0;
63 uint16_t header_size_paras = 0; /* TODO: make header size a parameter */
65 uint16_t min_extra_paras = 0;
67 uint16_t max_extra_paras = 0;
69 uint16_t ss = 0;
71 uint16_t sp = 0;
73 uint16_t checksum = 0; /* TODO: fill when writing */
75 uint16_t ip = 0;
77 uint16_t cs = 0;
79 uint16_t relocation_offset = 0; /* TODO: make parameter */
80
82 uint16_t overlay_number = 0;
84 uint16_t data_segment = 0;
85
92 {
94 uint16_t segment;
96 uint16_t offset;
97
98 Relocation(uint16_t segment, uint16_t offset)
100 {
101 }
102
103 static Relocation FromLinear(uint32_t address);
104
105 uint32_t GetOffset() const;
106
107 bool operator ==(const Relocation& other) const;
108
109 bool operator <(const Relocation& other) const;
110 };
111
113 std::vector<Relocation> relocations;
114
116 struct PIF
117 {
118 static constexpr uint32_t MAGIC_BEGIN = 0x0013EDC1;
119 static constexpr uint32_t MAGIC_END = 0xEDC10013;
120 static constexpr size_t SIZE = 19;
121
122 /* TODO: requires testing */
123 uint16_t maximum_extra_paragraphs;
124 uint16_t minimum_extra_paragraphs;
125 uint8_t flags;
126 uint8_t lowest_used_interrupt;
127 uint8_t highest_used_interrupt;
128 uint8_t com_port_usage;
129 uint8_t lpt_port_usage;
130 uint8_t screen_usage;
131
132 void SetDefaults();
133
134 void ReadFile(Linker::Reader& rd);
135
136 void WriteFile(Linker::Writer& wr);
137
138 void Dump(Dumper::Dumper& dump, offset_t file_offset);
139 };
140
142 std::unique_ptr<PIF> pif;
143
145 std::shared_ptr<Linker::Writable> image;
146
147 magic_type GetSignature() const;
148
149 void SetSignature(magic_type magic);
150
151 void Clear() override;
152
153 ~MZFormat()
154 {
155 Clear();
156 }
157
158 void SetFileSize(uint32_t size);
159
160 uint32_t GetHeaderSize();
161
162 uint32_t GetPifOffset() const;
163
164 void ReadFile(Linker::Reader& rd) override;
165
166 void WriteFile(Linker::Writer& wr) override;
167
168 void Dump(Dumper::Dumper& dump) override;
169
170 void CalculateValues() override;
171
172 /* * * Writer members * * */
173
190
191 /* filled in automatically */
193 uint16_t extra_paras = 0;
194
196 uint32_t zero_fill = 0;
197
199 uint32_t option_header_align = 0x10;
200
202 uint32_t option_file_align = 1;
203
204 bool FormatSupportsSegmentation() const override;
205
206 bool FormatIs16bit() const override;
207
208 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
209
210 using LinkerManager::SetLinkScript;
211
212 void SetModel(std::string model) override;
213
214 void SetOptions(std::map<std::string, std::string>& options) override;
215
216 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
217
223
224 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
225
229 void Link(Linker::Module& module);
230
231#if 0
235 void LinkLarge(Linker::Module& module);
236#endif
237
238 void ProcessModule(Linker::Module& module) override;
239
240 uint32_t GetDataSize() const;
241
242 void GenerateFile(std::string filename, Linker::Module& module) override;
243
244 std::string GetDefaultExtension(Linker::Module& module, std::string filename) override;
245 };
246
248 {
249 public:
250 std::string stub_file;
251 bool stub_file_valid = true;
252 std::ifstream stub;
253
254 MZSimpleStubWriter(std::string stub_file = "")
255 : stub_file(stub_file)
256 {
257 }
258
259 offset_t stub_size = -1;
260
261 bool OpenAndCheckValidFile();
262
263 offset_t GetStubImageSize();
264
265 void WriteStubImage(std::ostream& out);
266
267 void WriteStubImage(Linker::Writer& wr);
268
270 {
271 if(stub.is_open())
272 {
273 stub.close();
274 }
275 }
276 };
277
279 {
280 public:
281 std::string stub_file;
282 bool stub_file_valid = true;
283 std::ifstream stub;
284
285 MZStubWriter(std::string stub_file = "")
286 : stub_file(stub_file)
287 {
288 }
289
290 uint32_t original_file_size = -1;
291 uint32_t stub_file_size = 0;
292 uint16_t stub_reloc_count = 0;
293 uint32_t original_header_size = 0;
294 uint32_t stub_header_size = 0;
295 uint16_t original_reloc_offset = 0;
296 uint16_t stub_reloc_offset = 0;
297
298 bool OpenAndCheckValidFile();
299
300 offset_t GetStubImageSize();
301
302 void WriteStubImage(std::ostream& out);
303
304 void WriteStubImage(Linker::Writer& wr);
305
307 {
308 if(stub.is_open())
309 {
310 stub.close();
311 }
312 }
313 };
314
315}
316
317#endif /* MZEXE_H */
A class to control the output of a file analysis.
Definition dumper.h:550
A helper class to collect sections into segments.
Definition linker.h:19
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:20
A class that provides a general interface to setting up generation for a format.
Definition format.h:56
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:16
A helper class, encapsulating functionality needed to export binary data.
Definition writer.h:15
MZ .EXE format for MS-DOS.
Definition mzexe.h:35
std::string GetDefaultExtension(Linker::Module &module, std::string filename) override
Appends a default extension to the filename.
Definition mzexe.cc:679
void CreateDefaultSegments()
Create the required segments, if they have not already been allocated. The MZ format uses a single se...
Definition mzexe.cc:459
void WriteFile(Linker::Writer &wr) override
Stores data in memory to file.
Definition mzexe.cc:234
void Dump(Dumper::Dumper &dump) override
Display file contents in a nice manner.
Definition mzexe.cc:269
uint16_t relocation_offset
Offset to first relocation. Updated by CalculateValues.
Definition mzexe.h:79
uint16_t cs
Initial value for the code segment (CS)
Definition mzexe.h:77
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition mzexe.cc:174
uint16_t min_extra_paras
Minimum required extra memory, in paragraphs.
Definition mzexe.h:65
magic_type
Type of magic number, usually "MZ".
Definition mzexe.h:41
@ MAGIC_DL
HP 100LX/200LX System Manager modules (.exm) use the magic number "DL".
Definition mzexe.h:47
@ MAGIC_ZM
According to some sources such as Ralf Brown's interrupt list, some early excutables started with the...
Definition mzexe.h:45
@ MAGIC_MZ
The most common magic number "MZ".
Definition mzexe.h:43
void OnNewSegment(std::shared_ptr< Linker::Segment > segment) override
Callback function when allocating a new segment When the linker script runs, it creates segments cons...
Definition mzexe.cc:439
void Link(Linker::Module &module)
Link application according to script or memory model ()
Definition mzexe.cc:543
uint16_t checksum
Checksum.
Definition mzexe.h:73
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition mzexe.cc:669
uint16_t last_block_size
Size of last 512 byte block, 0 if full. Set by CalculateValues.
Definition mzexe.h:54
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition mzexe.cc:370
uint16_t extra_paras
Required maximum extra paragraphs after bss.
Definition mzexe.h:193
uint32_t option_file_align
User provided alignment value for file align.
Definition mzexe.h:202
uint16_t header_size_paras
Size of MZ header. Updated by CalculateValues.
Definition mzexe.h:63
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition mzexe.cc:318
uint16_t file_size_blocks
Size of MZ image in 512 blocks, rounded up. Set by CalculateValues.
Definition mzexe.h:56
void SetModel(std::string model) override
Sets the way memory is organized, typically modifying a built-in script.
Definition mzexe.cc:398
uint32_t zero_fill
Total size of bss and stack.
Definition mzexe.h:196
std::shared_ptr< Linker::Writable > image
The program image, placed after the MZ header.
Definition mzexe.h:145
uint16_t relocation_count
Number of relocations. Updated by CalculateValues.
Definition mzexe.h:61
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition mzexe.cc:148
uint16_t data_segment
Starting paragraph of program data, only required for .exm files.
Definition mzexe.h:84
memory_model_t
Represents the memory model of the running executable, which is the way in which the segments are set...
Definition mzexe.h:176
@ MODEL_LARGE
Large model, every section is a separate segment.
Definition mzexe.h:186
@ MODEL_SMALL
Small model, separate code and data segments.
Definition mzexe.h:182
@ MODEL_DEFAULT
Default model, same as small.
Definition mzexe.h:178
@ MODEL_TINY
Tiny model, code and data segment are the same.
Definition mzexe.h:180
@ MODEL_COMPACT
Compact model, separate code and multiple data segments.
Definition mzexe.h:184
uint16_t ip
Entry point initial value for IP.
Definition mzexe.h:75
memory_model_t memory_model
Memory model of generated executable.
Definition mzexe.h:189
uint16_t sp
Initial value for the stack (SP)
Definition mzexe.h:71
char signature[2]
The magic number at the start of the executable file, usually "MZ".
Definition mzexe.h:51
std::vector< Relocation > relocations
Address relocation offsets to paragraph fixups.
Definition mzexe.h:113
uint32_t option_header_align
User provided alignment value for header size.
Definition mzexe.h:199
bool FormatIs16bit() const override
Whether the format is 16-bit or not.
Definition mzexe.cc:375
uint16_t ss
Initial value for the stack segment (SS)
Definition mzexe.h:69
uint16_t overlay_number
Overlay number, should be 0 for main programs, not used for .exm files.
Definition mzexe.h:82
uint16_t max_extra_paras
Maximum required extra memory, in paragraphs. Set by CalculateValues using extra_paras.
Definition mzexe.h:67
std::unique_ptr< PIF > pif
Concurrent DOS program information entry, allocated only if present.
Definition mzexe.h:142
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition mzexe.cc:584
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition mzexe.cc:427
Definition mzexe.h:248
Definition mzexe.h:279
Concurrent DOS embedded program information, produced by PIFED.
Definition mzexe.h:117
Represents a relocation entry in the header, as a pair of 16-bit words.
Definition mzexe.h:92
uint16_t segment
Segment of relocation.
Definition mzexe.h:94
uint16_t offset
Offset of relocation within segment.
Definition mzexe.h:96