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/format.h"
10#include "../linker/module.h"
11#include "../linker/reader.h"
12#include "../linker/section.h"
13#include "../linker/segment.h"
14#include "../linker/segment_manager.h"
15#include "../linker/writer.h"
16#include "../dumper/dumper.h"
17
18namespace Microsoft
19{
34 class MZFormat : public virtual Linker::SegmentManager
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) const;
137
138 void Dump(Dumper::Dumper& dump, offset_t file_offset) const;
139 };
140
142 std::unique_ptr<PIF> pif;
143
145 std::shared_ptr<Linker::Image> 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() const;
161
162 uint32_t GetPifOffset() const;
163
164 void ReadFile(Linker::Reader& rd) override;
165
167 offset_t WriteFile(Linker::Writer& wr) const override;
168
169 void Dump(Dumper::Dumper& dump) const override;
170
171 void CalculateValues() override;
172
173 /* * * Writer members * * */
174
191
192 /* filled in automatically */
194 uint16_t extra_paras = 0;
195
197 uint32_t zero_fill = 0;
198
200 uint32_t option_header_align = 0x10;
201
203 uint32_t option_file_align = 1;
204
205 bool FormatSupportsSegmentation() const override;
206
207 bool FormatIs16bit() const override;
208
209 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
210
211 void SetModel(std::string model) override;
212
213 void SetOptions(std::map<std::string, std::string>& options) override;
214
215 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
216
222
223 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
224
228 void Link(Linker::Module& module);
229
230#if 0
234 void LinkLarge(Linker::Module& module);
235#endif
236
237 void ProcessModule(Linker::Module& module) override;
238
239 uint32_t GetDataSize() const;
240
241 void GenerateFile(std::string filename, Linker::Module& module) override;
242
244 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
245 };
246
248 {
249 public:
250 std::string filename;
251 bool valid = true;
252 std::ifstream stream;
253
254 MZSimpleStubWriter(std::string filename = "")
255 : filename(filename)
256 {
257 }
258
259 offset_t 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(stream.is_open())
272 {
273 stream.close();
274 }
275 }
276 };
277
279 {
280 public:
281 std::string filename;
282 bool valid = true;
283 std::ifstream stream;
284
285 MZStubWriter(std::string filename = "")
286 : filename(filename)
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(stream.is_open())
309 {
310 stream.close();
311 }
312 }
313 };
314
315}
316
317#endif /* MZEXE_H */
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:586
offset_t WriteFile(Writer &wr) const override=0
Stores data in memory to file.
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:24
virtual std::string GetDefaultExtension(Module &module, std::string filename) const
Appends a default extension to the filename.
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:16
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
MZ .EXE format for MS-DOS.
Definition mzexe.h:35
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition mzexe.cc:236
void CreateDefaultSegments()
Create the required segments, if they have not already been allocated. The MZ format uses a single se...
Definition mzexe.cc:464
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:176
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:444
void Link(Linker::Module &module)
Link application according to script or memory model ()
Definition mzexe.cc:548
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:672
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:375
uint16_t extra_paras
Required maximum extra paragraphs after bss.
Definition mzexe.h:194
uint32_t option_file_align
User provided alignment value for file align.
Definition mzexe.h:203
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:323
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:403
uint32_t zero_fill
Total size of bss and stack.
Definition mzexe.h:197
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:150
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition mzexe.cc:273
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:177
@ MODEL_LARGE
Large model, every section is a separate segment.
Definition mzexe.h:187
@ MODEL_SMALL
Small model, separate code and data segments.
Definition mzexe.h:183
@ MODEL_DEFAULT
Default model, same as small.
Definition mzexe.h:179
@ MODEL_TINY
Tiny model, code and data segment are the same.
Definition mzexe.h:181
@ MODEL_COMPACT
Compact model, separate code and multiple data segments.
Definition mzexe.h:185
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:190
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:200
bool FormatIs16bit() const override
Whether the format is 16-bit or not.
Definition mzexe.cc:380
uint16_t ss
Initial value for the stack segment (SS)
Definition mzexe.h:69
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition mzexe.cc:682
std::shared_ptr< Linker::Image > image
The program image, placed after the MZ header.
Definition mzexe.h:145
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:589
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition mzexe.cc:432
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