RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
pharlap.h
1#ifndef PHARLAP_H
2#define PHARLAP_H
3
4#include "../common.h"
5#include "../dumper/dumper.h"
6#include "../linker/options.h"
7#include "../linker/segment_manager.h"
8#include "mzexe.h"
9
10namespace PharLap
11{
15 class MPFormat : public virtual Linker::SegmentManager
16 {
17 public:
19 {
20 public:
21 Linker::Option<std::string> stub{"stub", "Filename for stub that gets prepended to executable"};
22
24 {
25 InitializeFields(stub);
26 }
27 };
28
29 void ReadFile(Linker::Reader& rd) override;
30
31 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
32
33 bool has_relocations;
34
35 std::shared_ptr<Linker::Segment> image;
36
37 static const uint32_t REL32 = 0x80000000;
39 {
40 struct
41 {
42 uint32_t offset : 31, rel32 : 1;
43 };
44 uint32_t value;
45
46 Relocation(uint32_t offset, unsigned rel32)
47 : offset(offset), rel32(rel32)
48 {
49 }
50
51 /*Relocation(uint32_t offset)
52 : value(offset)
53 {
54 }*/
55
56 bool operator ==(const Relocation& other) const;
57
58 bool operator <(const Relocation& other) const;
59 };
60
61 offset_t image_size = 0;
62 std::set<Relocation> relocations;
63 offset_t header_size = 0;
64 offset_t bss_pages = 0;
65 offset_t extra_pages = 0;
66 uint32_t esp = 0;
67 uint32_t eip = 0;
68 offset_t relocation_offset = 0;
69 mutable Microsoft::MZStubWriter stub;
70
71 MPFormat(bool has_relocations = false)
72 : has_relocations(has_relocations)
73 {
74 }
75
76 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
77
78 void SetOptions(std::map<std::string, std::string>& options) override;
79
80 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
81
82 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
83
84 void Link(Linker::Module& module);
85
86 void ProcessModule(Linker::Module& module) override;
87
88 void CalculateValues() override;
89
91 offset_t WriteFile(Linker::Writer& wr) const override;
92 void Dump(Dumper::Dumper& dump) const override;
93
95 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
96 };
97
101 class P3Format : public virtual Linker::SegmentManager
102 {
103 public:
105 {
106 public:
107 Linker::Option<std::string> stub{"stub", "Filename for stub that gets prepended to executable"};
108
110 {
111 InitializeFields(stub);
112 }
113 };
114
115 void ReadFile(Linker::Reader& rd) override;
116
117 bool FormatSupportsSegmentation() const override;
118
119 bool FormatIs16bit() const override;
120
121 bool FormatIsProtectedMode() const override;
122
123#if 0
124 bool FormatSupportsStackSection() const override;
125#endif
126
127 const bool is_multisegmented;
128 bool is_32bit;
129
130 uint16_t header_size = 0;
131 uint32_t file_size = 0;
132
133 uint32_t runtime_parameters_offset = 0;
134 uint32_t runtime_parameters_size = 0;
135 uint32_t relocation_table_offset = 0;
136 uint32_t relocation_table_size = 0;
137 uint32_t segment_information_table_offset = 0;
138 uint32_t segment_information_table_size = 0;
139 uint16_t segment_information_table_entry_size = 0;
140 uint32_t load_image_offset = 0;
141 uint32_t load_image_size = 0;
142 uint32_t symbol_table_offset = 0;
143 uint32_t symbol_table_size = 0;
144 uint32_t gdt_address = 0;
145 uint32_t gdt_size = 0;
146 uint32_t ldt_address = 0;
147 uint32_t ldt_size = 0;
148 uint32_t idt_address = 0;
149 uint32_t idt_size = 0;
150 uint32_t tss_address = 0;
151 uint32_t tss_size = 0;
152 uint32_t minimum_extra = 0;
153 uint32_t maximum_extra = 0;
154 uint32_t base_load_offset = 0;
155 uint32_t esp = 0;
156 uint16_t ss = 0;
157 uint32_t eip = 0;
158 uint16_t cs = 0;
159 uint16_t ldtr = 0;
160 uint16_t tr = 0;
161 uint16_t flags = 0;
162 uint32_t memory_requirements = 0;
163 uint32_t stack_size = 0;
164
165 mutable Microsoft::MZStubWriter stub;
166
167 P3Format(bool is_multisegmented, bool is_32bit = true)
168 : is_multisegmented(is_multisegmented), is_32bit(is_32bit)
169 {
170 }
171
173 {
174 public:
175 uint16_t min_realmode_param = 0, max_realmode_param = 0, min_int_buffer_size_kb = 0, max_int_buffer_size_kb = 0, int_stack_count = 0, int_stack_size_kb = 0;
176 uint32_t realmode_area_end = 0;
177 uint16_t call_buffer_size_kb = 0, flags = 0, ring = 0;
178
179 void CalculateValues();
180
181 void WriteFile(Linker::Writer& wr) const;
182 };
183
184 RunTimeParameterBlock runtime_parameters;
185
186 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
187
188 void SetOptions(std::map<std::string, std::string>& options) override;
189
191 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
192
194 offset_t WriteFile(Linker::Writer& wr) const override;
195 void Dump(Dumper::Dumper& dump) const override;
196
197 class Flat;
198 class MultiSegmented;
199 };
200
202 {
203 public:
204 Flat(bool is_32bit = true)
205 : P3Format(false, is_32bit)
206 {
207 }
208
209 std::shared_ptr<Linker::Segment> image;
210
211 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
212
213 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
214
215 void Link(Linker::Module& module);
216
217 void ProcessModule(Linker::Module& module) override;
218
219 void CalculateValues() override;
220
222 offset_t WriteFile(Linker::Writer& wr) const override;
223 void Dump(Dumper::Dumper& dump) const override;
224 };
225
227 {
228 public:
230 {
231 public:
232 uint32_t address = 0;
233 virtual ~AbstractSegment();
234 virtual uint32_t GetStoredSize() const = 0;
235 virtual uint32_t GetLoadedSize() const = 0;
236 virtual void WriteFile(Linker::Writer& wr) const = 0;
237 };
238
240 {
241 public:
242 std::weak_ptr<AbstractSegment> image;
243
244 enum
245 {
246 TSS16 = 0x00008100,
247 LDT = 0x00008200,
248 TSS32 = 0x00008900,
249 Code16 = 0x00009A00,
250 Code32 = 0x00409A00,
251 Data16 = 0x00009200,
252 Data32 = 0x00409200,
253
254 DESC_G = 0x00800000,
255 };
256
257 uint32_t limit = 0;
258 uint32_t base = 0;
259 uint32_t access;
260
261 Descriptor(uint32_t access, std::weak_ptr<AbstractSegment> image = std::weak_ptr<AbstractSegment>())
262 : image(image), access(access)
263 {
264 }
265
266 void CalculateValues();
267
268 void WriteEntry(Linker::Writer& wr) const;
269 };
270
272 {
273 public:
274 std::vector<Descriptor> descriptors;
275
276 uint32_t GetStoredSize() const override;
277
278 uint32_t GetLoadedSize() const override;
279
280 void WriteFile(Linker::Writer& wr) const override;
281
282 void CalculateValues();
283 };
284
285 std::shared_ptr<DescriptorTable> gdt;
286 std::shared_ptr<DescriptorTable> idt;
287 std::shared_ptr<DescriptorTable> ldt;
288
290 {
291 public:
292 bool is_32bit;
293
294 uint32_t esp0 = 0, esp1 = 0, esp2 = 0;
295 uint32_t cr3 = 0, eip = 0, eflags = 0, eax = 0, ecx = 0, edx = 0, ebx = 0, esp = 0, ebp = 0, esi = 0, edi = 0;
296 uint16_t ss0 = 0, ss1 = 0, ss2 = 0, es = 0, cs = 0, ss = 0, ds = 0, fs = 0, gs = 0, ldtr = 0, iopb = 0;
297 uint16_t link = 0;
298
299 TaskStateSegment(bool is_32bit = true)
300 : is_32bit(is_32bit)
301 {
302 }
303
304 uint32_t GetStoredSize() const override;
305
306 uint32_t GetLoadedSize() const override;
307
308 void WriteFile(Linker::Writer& wr) const override;
309 };
310
311 std::shared_ptr<TaskStateSegment> tss;
312
314 {
315 public:
316 /* Segment members */
317 std::shared_ptr<Linker::Segment> segment;
318
319 uint32_t access;
320 uint16_t selector;
321 uint16_t flags = 0;
322 uint32_t base_offset = 0; /* TODO??? */
323
324 Segment(std::shared_ptr<Linker::Segment> segment, uint32_t access, uint16_t selector)
325 : segment(segment), access(access), selector(selector)
326 {
327 }
328
329 uint32_t GetStoredSize() const override;
330
331 uint32_t GetLoadedSize() const override;
332
333 void WriteSITEntry(Linker::Writer& wr) const;
334
335 void WriteFile(Linker::Writer& wr) const override;
336 };
337
338 MultiSegmented(bool is_32bit = true)
339 : P3Format(true, is_32bit)
340 {
341 }
342
344 {
345 public:
346 std::shared_ptr<Segment> segment;
347 uint32_t offset;
348
349 Relocation(std::shared_ptr<Segment> segment, uint32_t offset)
350 : segment(segment), offset(offset)
351 {
352 }
353
354 bool operator ==(const Relocation& other) const;
355
356 bool operator <(const Relocation& other) const;
357
358 void WriteFile(Linker::Writer& wr) const;
359 };
360
361 std::vector<std::shared_ptr<AbstractSegment>> segments;
362 std::map<std::shared_ptr<Linker::Segment>, std::shared_ptr<Segment>> segment_associations;
363 std::set<Relocation> relocations;
364 std::shared_ptr<Segment> code;
365 std::shared_ptr<Segment> data;
366
367 void OnNewSegment(std::shared_ptr<Linker::Segment> linker_segment) override;
368
369 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
370
371 void Link(Linker::Module& module);
372
373 void ProcessModule(Linker::Module& module) override;
374
375 void CalculateValues() override;
376
378 offset_t WriteFile(Linker::Writer& wr) const override;
379 void Dump(Dumper::Dumper& dump) const override;
380 };
381
386 {
387 public:
388 std::unique_ptr<P3Format> contents;
389
390 void ReadFile(Linker::Reader& rd) override;
391
392 bool FormatSupportsSegmentation() const override;
393
394 bool FormatIs16bit() const override;
395
396 bool FormatIsProtectedMode() const override;
397
398 void SetOptions(std::map<std::string, std::string>& options) override;
399
401 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
402
403 void ProcessModule(Linker::Module& module) override;
404
405 void CalculateValues() override;
406
408 offset_t WriteFile(Linker::Writer& wr) const override;
409 void Dump(Dumper::Dumper& dump) const override;
410 };
411}
412
413#endif /* PHARLAP_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
Helper class that contains the options interpreted by the format.
Definition options.h:308
Documents and handles command line options.
Definition options.h:196
A class that provides a general interface to setting up generation for a format.
Definition format.h:64
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
Definition mzexe.h:300
Phar Lap "MP" .exp and "MQ" .rex file.
Definition pharlap.h:16
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition pharlap.cc:204
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition pharlap.cc:10
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition pharlap.cc:158
std::shared_ptr< Linker::OptionCollector > GetOptions() override
Returns object containing a sequence of option fields provided with the -S command line flag.
Definition pharlap.cc:37
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 pharlap.cc:49
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition pharlap.cc:215
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition pharlap.cc:42
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition pharlap.cc:168
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition pharlap.cc:94
Container for Phar Lap "P2"/"P3" .exp files, used when reading in a P2/P3 file.
Definition pharlap.h:386
bool FormatIs16bit() const override
Whether the format is 16-bit or not.
Definition pharlap.cc:1064
bool FormatIsProtectedMode() const override
Whether the format is in protected mode or not (x86 only)
Definition pharlap.cc:1069
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition pharlap.cc:1109
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition pharlap.cc:1097
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition pharlap.cc:1074
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition pharlap.cc:1089
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition pharlap.cc:1036
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition pharlap.cc:1079
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition pharlap.cc:1084
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition pharlap.cc:1059
Definition pharlap.h:202
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition pharlap.cc:537
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition pharlap.cc:550
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 pharlap.cc:373
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition pharlap.cc:493
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition pharlap.cc:418
Definition pharlap.h:227
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition pharlap.cc:1030
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition pharlap.cc:815
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition pharlap.cc:999
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition pharlap.cc:921
Definition pharlap.h:105
Phar Lap "P2"/"P3" .exp file.
Definition pharlap.h:102
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition pharlap.cc:301
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition pharlap.cc:233
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition pharlap.cc:294
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition pharlap.cc:362
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition pharlap.cc:238
bool FormatIs16bit() const override
Whether the format is 16-bit or not.
Definition pharlap.cc:243
std::shared_ptr< Linker::OptionCollector > GetOptions() override
Returns object containing a sequence of option fields provided with the -S command line flag.
Definition pharlap.cc:289
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition pharlap.cc:313
bool FormatIsProtectedMode() const override
Whether the format is in protected mode or not (x86 only)
Definition pharlap.cc:248
Definition pharlap.h:39