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
11namespace SeychellDOS32
12{
16 class AdamFormat : public virtual Linker::Format, public virtual Linker::SegmentManager
17 {
18 public:
20 {
21 FORMAT_UNSPECIFIED,
25 FORMAT_35, /* based on Michael Tippach's and own research */
30 };
31 format_type format;
32
33 enum output_type
34 {
35 OUTPUT_EXE,
36 OUTPUT_DLL
37 };
38 output_type output;
39
40 AdamFormat(format_type format = FORMAT_UNSPECIFIED, output_type output = OUTPUT_EXE)
41 : format(format), output(output)
42 {
43 if(output == OUTPUT_EXE)
44 MakeApplication();
45 else
46 MakeLibrary();
47 }
48
49 enum relocation_type
50 {
51 Selector16,
52 Offset32,
53 };
54
55 std::array<char, 4> signature;
56 std::array<uint8_t, 2> minimum_dos_version;
57 std::array<uint8_t, 2> dlink_version;
58 /* header + program + relocations */
59 uint32_t image_size = 0;
60 /* header */
61 uint32_t header_size = 0;
62 /* program */
63 uint32_t program_size = 0;
64 /* program + zero data */
65 uint32_t memory_size = 0;
66 /* v3.5 only: program + relocations */
67 uint32_t contents_size = 0;
68 /* v3.3 only */
69 uint32_t selector_relocation_count = 0;
70 /* DX64 only */
71 uint32_t offset_relocations_size = 0;
72 uint32_t eip = 0;
73 uint32_t esp = 0;
74 std::vector<uint32_t> selector_relocations;
75 std::vector<uint32_t> offset_relocations;
76 /* v3.3: used internally, v3.5: used to import/export */
77 std::map<uint32_t, relocation_type> relocations_map;
78 uint32_t flags = 0;
79
80 std::shared_ptr<Linker::Contents> image;
81
82 enum
83 {
84 FLAG_COMPRESSED = 0x0001,
85 FLAG_DISPLAY_LOGO = 0x0002,
86 FLAG_4MB_HEAP_LIMIT = 0x0004, // 3.5 only
87 };
88
89 constexpr bool IsV35() const { return (dlink_version[1] > 0x03) || (dlink_version[1] == 0x03 && dlink_version[0] >= 0x50); }
90 constexpr bool IsDLL() const { return signature[0] == 'D'; }
91
92 void MakeApplication();
93 void MakeLibrary();
94
95 /* only for v3.5 */
96 static uint32_t GetRelocationSize(uint32_t displacement, relocation_type type);
97
98 void CalculateValues() override;
99
100 void ReadFile(Linker::Reader& rd) override;
101
103 offset_t WriteFile(Linker::Writer& wr) const override;
104 void Dump(Dumper::Dumper& dump) const override;
105
106 /* * * Writer members * * */
107
109 uint32_t stack_size = 0;
110
112 {
113 public:
114 class OutputEnumeration : public Linker::Enumeration<output_type>
115 {
116 public:
118 : Enumeration(
119 "EXE", OUTPUT_EXE,
120 "DLL", OUTPUT_DLL)
121 {
122 descriptions = {
123 { OUTPUT_EXE, "executable" },
124 { OUTPUT_DLL, "shared library" },
125 };
126 }
127 };
128
129 Linker::Option<std::string> stub{"stub", "Filename for stub that gets prepended to executable"};
130 Linker::Option<Linker::ItemOf<OutputEnumeration>> output{"output", "Binary type"};
131 Linker::Option<offset_t> stack{"stack", "Specify the stack size"};
132
133 AdamOptionCollector()
134 {
135 InitializeFields(stub, output, stack);
136 }
137 };
138
139 bool FormatSupportsSegmentation() const override;
140
141 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
142
143 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
144
145 void SetOptions(std::map<std::string, std::string>& options) override;
146
147 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
148
154
155 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
156
160 void Link(Linker::Module& module);
161
162 void ProcessModule(Linker::Module& module) override;
163
164 void GenerateFile(std::string filename, Linker::Module& module) override;
165
167 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
168 };
169};
170
171namespace BorcaD3X
172{
176 class D3X1Format : public virtual Linker::Format
177 {
178 public:
179 uint32_t header_size = 0;
180 uint32_t binary_size = 0;
181 uint32_t extra_size = 0;
182 uint32_t entry = 0;
183 uint32_t stack_top = 0;
184
185 D3X1Format()
186 : header_size(24)
187 {
188 }
189
190 void ReadFile(Linker::Reader& rd) override;
191
193 offset_t WriteFile(Linker::Writer& wr) const override;
194 void Dump(Dumper::Dumper& dump) const override;
195 };
196};
197
198namespace DX64
199{
203 class LVFormat : public virtual SeychellDOS32::AdamFormat
204 {
205 public:
206 enum format_type
207 {
208 FORMAT_FLAT,
209 FORMAT_LV,
210 };
211
212 explicit LVFormat()
213 {
214 }
215
216 LVFormat(format_type type)
217 : AdamFormat(AdamFormat::FORMAT_LV_FLAT, OUTPUT_EXE)
218 {
219 SetSignature(type);
220 }
221
222 void SetSignature(format_type type);
223
224 void ReadFile(Linker::Reader& rd) override;
225
227 offset_t WriteFile(Linker::Writer& wr) const override;
228 void Dump(Dumper::Dumper& dump) const override;
229
230 /* * * Writer members * * */
231
232 // TODO: check Flat support
233
235 {
236 public:
237 Linker::Option<std::string> stub{"stub", "Filename for stub that gets prepended to executable"};
238 Linker::Option<offset_t> stack{"stack", "Specify the stack size"};
239
241 {
242 InitializeFields(stub, stack);
243 }
244 };
245
246 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
247
248 void SetOptions(std::map<std::string, std::string>& options) override;
249
250 void GenerateFile(std::string filename, Linker::Module& module) override;
251 };
252}
253
254/* TODO: other formats? */
255
256#endif /* DOSEXE_H */
Daniel Borca's D3X executable format.
Definition dosexe.h:177
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition dosexe.cc:656
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition dosexe.cc:669
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition dosexe.cc:682
Definition dosexe.h:235
CandyMan's DX64 "Flat" and "LV" executable formats.
Definition dosexe.h:204
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition dosexe.cc:746
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition dosexe.cc:777
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition dosexe.cc:769
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:764
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition dosexe.cc:710
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition dosexe.cc:726
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
A class to encode a general file format.
Definition format.h:29
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:17
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:20
@ FORMAT_33
DOS32 version 3.3.
Definition dosexe.h:23
@ FORMAT_35
DOS32 version 3.5 beta.
Definition dosexe.h:25
@ FORMAT_DX64
DX64 veresion.
Definition dosexe.h:27
@ FORMAT_LV_FLAT
DX64 version, only used with LVFormat.
Definition dosexe.h:29
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