RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
dosexe.h
1#ifndef DOSEXE_H
2#define DOSEXE_H
3
4#include <array>
5#include "../common.h"
6#include "../dumper/dumper.h"
7#include "../linker/segment_manager.h"
8#include "../linker/section.h"
9#include "mzexe.h"
10#include "binary.h"
11
12namespace SeychellDOS32
13{
17 class AdamFormat : public virtual Linker::SegmentManager
18 {
19 public:
21 {
22 FORMAT_UNSPECIFIED,
26 FORMAT_35, /* based on Michael Tippach's and own research */
31 };
32 format_type format;
33
34 enum output_type
35 {
36 OUTPUT_EXE,
37 OUTPUT_DLL
38 };
39 output_type output;
40
41 AdamFormat(format_type format = FORMAT_UNSPECIFIED, output_type output = OUTPUT_EXE)
42 : format(format), output(output)
43 {
44 if(output == OUTPUT_EXE)
45 MakeApplication();
46 else
47 MakeLibrary();
48 }
49
50 enum relocation_type
51 {
52 Selector16,
53 Offset32,
54 };
55
56 std::array<char, 4> signature;
57 std::array<uint8_t, 2> minimum_dos_version;
58 std::array<uint8_t, 2> dlink_version;
59 /* header + program + relocations */
60 uint32_t image_size = 0;
61 /* header */
62 uint32_t header_size = 0;
63 /* program */
64 uint32_t program_size = 0;
65 /* program + zero data */
66 uint32_t memory_size = 0;
67 /* v3.5 only: program + relocations */
68 uint32_t contents_size = 0;
69 /* v3.3 only */
70 uint32_t selector_relocation_count = 0;
71 /* DX64 only */
72 uint32_t offset_relocations_size = 0;
73 uint32_t eip = 0;
74 uint32_t esp = 0;
75 std::vector<uint32_t> selector_relocations;
76 std::vector<uint32_t> offset_relocations;
77 /* v3.3: used internally, v3.5: used to import/export */
78 std::map<uint32_t, relocation_type> relocations_map;
79 uint32_t flags = 0;
80
81 std::shared_ptr<Linker::Contents> image;
82
83 enum
84 {
85 FLAG_COMPRESSED = 0x0001,
86 FLAG_DISPLAY_LOGO = 0x0002,
87 FLAG_4MB_HEAP_LIMIT = 0x0004, // 3.5 only
88 };
89
90 constexpr bool IsV35() const { return (dlink_version[1] > 0x03) || (dlink_version[1] == 0x03 && dlink_version[0] >= 0x50); }
91 constexpr bool IsDLL() const { return signature[0] == 'D'; }
92
93 void MakeApplication();
94 void MakeLibrary();
95
96 /* only for v3.5 */
97 static uint32_t GetRelocationSize(uint32_t displacement, relocation_type type);
98
99 void CalculateValues() override;
100
101 void ReadFile(Linker::Reader& rd) override;
102
104 offset_t WriteFile(Linker::Writer& wr) const override;
105 void Dump(Dumper::Dumper& dump) const override;
106
107 /* * * Writer members * * */
108
110 uint32_t stack_size = 0;
111
113 {
114 public:
115 class OutputEnumeration : public Linker::Enumeration<output_type>
116 {
117 public:
119 : Enumeration(
120 "EXE", OUTPUT_EXE,
121 "DLL", OUTPUT_DLL)
122 {
123 descriptions = {
124 { OUTPUT_EXE, "executable" },
125 { OUTPUT_DLL, "shared library" },
126 };
127 }
128 };
129
130 Linker::Option<std::string> stub{"stub", "Filename for stub that gets prepended to executable"};
131 Linker::Option<Linker::ItemOf<OutputEnumeration>> output{"output", "Binary type"};
132 Linker::Option<offset_t> stack{"stack", "Specify the stack size"};
133
134 AdamOptionCollector()
135 {
136 InitializeFields(stub, output, stack);
137 }
138 };
139
140 bool FormatSupportsSegmentation() const override;
141
142 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
143
144 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
145
146 void SetOptions(std::map<std::string, std::string>& options) override;
147
148 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
149
155
156 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
157
161 void Link(Linker::Module& module);
162
163 void ProcessModule(Linker::Module& module) override;
164
165 void GenerateFile(std::string filename, Linker::Module& module) override;
166
168 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
169 };
170};
171
172namespace DX64
173{
177 class LVFormat : public virtual SeychellDOS32::AdamFormat
178 {
179 public:
180 enum format_type
181 {
182 FORMAT_FLAT,
183 FORMAT_LV,
184 };
185
186 explicit LVFormat()
187 {
188 }
189
190 LVFormat(format_type type)
191 : AdamFormat(AdamFormat::FORMAT_LV_FLAT, OUTPUT_EXE)
192 {
193 SetSignature(type);
194 }
195
196 void SetSignature(format_type type);
197
198 void ReadFile(Linker::Reader& rd) override;
199
201 offset_t WriteFile(Linker::Writer& wr) const override;
202 void Dump(Dumper::Dumper& dump) const override;
203
204 /* * * Writer members * * */
205
206 // TODO: check Flat support
207
209 {
210 public:
211 Linker::Option<std::string> stub{"stub", "Filename for stub that gets prepended to executable"};
212 Linker::Option<offset_t> stack{"stack", "Specify the stack size"};
213
215 {
216 InitializeFields(stub, stack);
217 }
218 };
219
220 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
221
222 void SetOptions(std::map<std::string, std::string>& options) override;
223
224 void GenerateFile(std::string filename, Linker::Module& module) override;
225 };
226}
227
228namespace BorcaD3X
229{
234 {
235 public:
236 uint32_t header_size = 0;
237 uint32_t binary_size = 0;
238 uint32_t extra_size = 0;
239 uint32_t entry = 0;
240 uint32_t stack_top = 0;
241
242 D3X1Format()
243 : Binary::GenericBinaryFormat(0, ".exe"), header_size(32)
244 {
245 }
246
247 void ReadFile(Linker::Reader& rd) override;
248
249 offset_t ImageSize() const override;
250
252 offset_t WriteFile(Linker::Writer& wr) const override;
253 void Dump(Dumper::Dumper& dump) const override;
254
255 void CalculateValues() override;
256
257 /* * * Writer members * * */
258
260 {
261 public:
262 Linker::Option<std::string> stub{"stub", "Filename for stub that gets prepended to executable"};
263
265 {
266 InitializeFields(stub);
267 }
268 };
269
271
272 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
273
274 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
275
276 void SetOptions(std::map<std::string, std::string>& options) override;
277
278 void ProcessModule(Linker::Module& module) override;
279 };
280};
281
282/* TODO: other formats? */
283
284#endif /* DOSEXE_H */
A template for flat binary formats.
Definition binary.h:21
Daniel Borca's D3X executable format.
Definition dosexe.h:234
offset_t ImageSize() const override
Retrieves size of stored data.
Definition dosexe.cc:764
std::shared_ptr< Linker::OptionCollector > GetOptions() override
Returns object containing a sequence of option fields provided with the -S command line flag.
Definition dosexe.cc:830
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition dosexe.cc:807
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition dosexe.cc:750
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition dosexe.cc:769
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition dosexe.cc:789
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition dosexe.cc:835
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition dosexe.cc:842
Definition dosexe.h:209
CandyMan's DX64 "Flat" and "LV" executable formats.
Definition dosexe.h:178
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition dosexe.cc:705
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition dosexe.cc:736
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition dosexe.cc:728
std::shared_ptr< Linker::OptionCollector > GetOptions() override
Returns object containing a sequence of option fields provided with the -S command line flag.
Definition dosexe.cc:723
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition dosexe.cc:669
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition dosexe.cc:685
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:773
A representation of an enumeration with associated string representations for each value.
Definition options.h:15
std::map< value_type, std::string > descriptions
An empty dictionary that explains the value types in detail.
Definition options.h:20
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
Helper class that contains the options interpreted by the format.
Definition options.h:474
Documents and handles command line options.
Definition options.h:306
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:20
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
Definition mzexe.h:274
Adam Seychell's DOS32 "Adam" executable format.
Definition dosexe.h:18
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition dosexe.cc:647
void Link(Linker::Module &module)
Link application according to script or memory model.
Definition dosexe.cc:496
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition dosexe.cc:42
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition dosexe.cc:409
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition dosexe.cc:576
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 dosexe.cc:449
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition dosexe.cc:440
std::shared_ptr< Linker::OptionCollector > GetOptions() override
Returns object containing a sequence of option fields provided with the -S command line flag.
Definition dosexe.cc:435
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition dosexe.cc:331
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition dosexe.cc:505
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition dosexe.cc:232
format_type
Definition dosexe.h:21
@ FORMAT_33
DOS32 version 3.3.
Definition dosexe.h:24
@ FORMAT_35
DOS32 version 3.5 beta.
Definition dosexe.h:26
@ FORMAT_DX64
DX64 veresion.
Definition dosexe.h:28
@ FORMAT_LV_FLAT
DX64 version, only used with LVFormat.
Definition dosexe.h:30
void CreateDefaultSegments()
Create the required segments, if they have not already been allocated. The Adam format uses a single ...
Definition dosexe.cc:467
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition dosexe.cc:105