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/segment_manager.h"
7#include "mzexe.h"
8
9namespace PharLap
10{
14 class MPFormat : public virtual Linker::SegmentManager
15 {
16 public:
17 void ReadFile(Linker::Reader& rd) override;
18
19 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
20
21 bool has_relocations;
22
23 std::shared_ptr<Linker::Segment> image;
24
25 static const uint32_t REL32 = 0x80000000;
27 {
28 struct
29 {
30 uint32_t offset : 31, rel32 : 1;
31 };
32 uint32_t value;
33
34 Relocation(uint32_t offset, unsigned rel32)
35 : offset(offset), rel32(rel32)
36 {
37 }
38
39 /*Relocation(uint32_t offset)
40 : value(offset)
41 {
42 }*/
43
44 bool operator ==(const Relocation& other) const;
45
46 bool operator <(const Relocation& other) const;
47 };
48
49 offset_t image_size = 0;
50 std::set<Relocation> relocations;
51 offset_t header_size = 0;
52 offset_t bss_pages = 0;
53 offset_t extra_pages = 0;
54 uint32_t esp = 0;
55 uint32_t eip = 0;
56 offset_t relocation_offset = 0;
57 mutable Microsoft::MZStubWriter stub;
58
59 MPFormat(bool has_relocations = false)
60 : has_relocations(has_relocations)
61 {
62 }
63
64 void SetOptions(std::map<std::string, std::string>& options) override;
65
66 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
67
68 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
69
70 void Link(Linker::Module& module);
71
72 void ProcessModule(Linker::Module& module) override;
73
74 void CalculateValues() override;
75
77 offset_t WriteFile(Linker::Writer& wr) const override;
78 void Dump(Dumper::Dumper& dump) const override;
79
81 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
82 };
83
87 class P3Format : public virtual Linker::SegmentManager
88 {
89 public:
90 void ReadFile(Linker::Reader& rd) override;
91
92 bool FormatSupportsSegmentation() const override;
93
94#if 0
95 bool FormatSupportsStackSection() const override;
96#endif
97
98 const bool is_multisegmented;
99 bool is_32bit;
100
101 uint16_t header_size = 0;
102 uint32_t file_size = 0;
103
104 uint32_t runtime_parameters_offset = 0;
105 uint32_t runtime_parameters_size = 0;
106 uint32_t relocation_table_offset = 0;
107 uint32_t relocation_table_size = 0;
108 uint32_t segment_information_table_offset = 0;
109 uint32_t segment_information_table_size = 0;
110 uint16_t segment_information_table_entry_size = 0;
111 uint32_t load_image_offset = 0;
112 uint32_t load_image_size = 0;
113 uint32_t symbol_table_offset = 0;
114 uint32_t symbol_table_size = 0;
115 uint32_t gdt_address = 0;
116 uint32_t gdt_size = 0;
117 uint32_t ldt_address = 0;
118 uint32_t ldt_size = 0;
119 uint32_t idt_address = 0;
120 uint32_t idt_size = 0;
121 uint32_t tss_address = 0;
122 uint32_t tss_size = 0;
123 uint32_t minimum_extra = 0;
124 uint32_t maximum_extra = 0;
125 uint32_t base_load_offset = 0;
126 uint32_t esp = 0;
127 uint16_t ss = 0;
128 uint32_t eip = 0;
129 uint16_t cs = 0;
130 uint16_t ldtr = 0;
131 uint16_t tr = 0;
132 uint16_t flags = 0;
133 uint32_t memory_requirements = 0;
134 uint32_t stack_size = 0;
135
136 mutable Microsoft::MZStubWriter stub;
137
138 P3Format(bool is_multisegmented, bool is_32bit = true)
139 : is_multisegmented(is_multisegmented), is_32bit(is_32bit)
140 {
141 }
142
144 {
145 public:
146 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;
147 uint32_t realmode_area_end = 0;
148 uint16_t call_buffer_size_kb = 0, flags = 0, ring = 0;
149
150 void CalculateValues();
151
152 void WriteFile(Linker::Writer& wr) const;
153 };
154
155 RunTimeParameterBlock runtime_parameters;
156
157 void SetOptions(std::map<std::string, std::string>& options) override;
158
160 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
161
163 offset_t WriteFile(Linker::Writer& wr) const override;
164 void Dump(Dumper::Dumper& dump) const override;
165
166 class Flat;
167 class MultiSegmented;
168 };
169
171 {
172 public:
173 Flat(bool is_32bit = true)
174 : P3Format(false, is_32bit)
175 {
176 }
177
178 std::shared_ptr<Linker::Segment> image;
179
180 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
181
182 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
183
184 void Link(Linker::Module& module);
185
186 void ProcessModule(Linker::Module& module) override;
187
188 void CalculateValues() override;
189
191 offset_t WriteFile(Linker::Writer& wr) const override;
192 void Dump(Dumper::Dumper& dump) const override;
193 };
194
196 {
197 public:
199 {
200 public:
201 uint32_t address = 0;
202 virtual ~AbstractSegment();
203 virtual uint32_t GetStoredSize() const = 0;
204 virtual uint32_t GetLoadedSize() const = 0;
205 virtual void WriteFile(Linker::Writer& wr) const = 0;
206 };
207
209 {
210 public:
211 std::weak_ptr<AbstractSegment> image;
212
213 enum
214 {
215 TSS16 = 0x00008100,
216 LDT = 0x00008200,
217 TSS32 = 0x00008900,
218 Code16 = 0x00009A00,
219 Code32 = 0x00409A00,
220 Data16 = 0x00009200,
221 Data32 = 0x00409200,
222
223 DESC_G = 0x00800000,
224 };
225
226 uint32_t limit = 0;
227 uint32_t base = 0;
228 uint32_t access;
229
230 Descriptor(uint32_t access, std::weak_ptr<AbstractSegment> image = std::weak_ptr<AbstractSegment>())
231 : image(image), access(access)
232 {
233 }
234
235 void CalculateValues();
236
237 void WriteEntry(Linker::Writer& wr) const;
238 };
239
241 {
242 public:
243 std::vector<Descriptor> descriptors;
244
245 uint32_t GetStoredSize() const override;
246
247 uint32_t GetLoadedSize() const override;
248
249 void WriteFile(Linker::Writer& wr) const override;
250
251 void CalculateValues();
252 };
253
254 std::shared_ptr<DescriptorTable> gdt;
255 std::shared_ptr<DescriptorTable> idt;
256 std::shared_ptr<DescriptorTable> ldt;
257
259 {
260 public:
261 bool is_32bit;
262
263 uint32_t esp0 = 0, esp1 = 0, esp2 = 0;
264 uint32_t cr3 = 0, eip = 0, eflags = 0, eax = 0, ecx = 0, edx = 0, ebx = 0, esp = 0, ebp = 0, esi = 0, edi = 0;
265 uint16_t ss0 = 0, ss1 = 0, ss2 = 0, es = 0, cs = 0, ss = 0, ds = 0, fs = 0, gs = 0, ldtr = 0, iopb = 0;
266 uint16_t link = 0;
267
268 TaskStateSegment(bool is_32bit = true)
269 : is_32bit(is_32bit)
270 {
271 }
272
273 uint32_t GetStoredSize() const override;
274
275 uint32_t GetLoadedSize() const override;
276
277 void WriteFile(Linker::Writer& wr) const override;
278 };
279
280 std::shared_ptr<TaskStateSegment> tss;
281
283 {
284 public:
285 /* Segment members */
286 std::shared_ptr<Linker::Segment> segment;
287
288 uint32_t access;
289 uint16_t selector;
290 uint16_t flags = 0;
291 uint32_t base_offset = 0; /* TODO??? */
292
293 Segment(std::shared_ptr<Linker::Segment> segment, uint32_t access, uint16_t selector)
294 : segment(segment), access(access), selector(selector)
295 {
296 }
297
298 uint32_t GetStoredSize() const override;
299
300 uint32_t GetLoadedSize() const override;
301
302 void WriteSITEntry(Linker::Writer& wr) const;
303
304 void WriteFile(Linker::Writer& wr) const override;
305 };
306
307 MultiSegmented(bool is_32bit = true)
308 : P3Format(true, is_32bit)
309 {
310 }
311
313 {
314 public:
315 std::shared_ptr<Segment> segment;
316 uint32_t offset;
317
318 Relocation(std::shared_ptr<Segment> segment, uint32_t offset)
319 : segment(segment), offset(offset)
320 {
321 }
322
323 bool operator ==(const Relocation& other) const;
324
325 bool operator <(const Relocation& other) const;
326
327 void WriteFile(Linker::Writer& wr) const;
328 };
329
330 std::vector<std::shared_ptr<AbstractSegment>> segments;
331 std::map<std::shared_ptr<Linker::Segment>, std::shared_ptr<Segment>> segment_associations;
332 std::set<Relocation> relocations;
333 std::shared_ptr<Segment> code;
334 std::shared_ptr<Segment> data;
335
336 void OnNewSegment(std::shared_ptr<Linker::Segment> linker_segment) override;
337
338 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
339
340 void Link(Linker::Module& module);
341
342 void ProcessModule(Linker::Module& module) override;
343
344 void CalculateValues() override;
345
347 offset_t WriteFile(Linker::Writer& wr) const override;
348 void Dump(Dumper::Dumper& dump) const override;
349 };
350
355 {
356 public:
357 std::unique_ptr<P3Format> contents;
358
359 void ReadFile(Linker::Reader& rd) override;
360 bool FormatSupportsSegmentation() const override;
361
362 void SetOptions(std::map<std::string, std::string>& options) override;
363
365 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
366
367 void ProcessModule(Linker::Module& module) override;
368
369 void CalculateValues() override;
370
372 offset_t WriteFile(Linker::Writer& wr) const override;
373 void Dump(Dumper::Dumper& dump) const override;
374 };
375}
376
377#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
A class that provides a general interface to setting up generation for a format.
Definition format.h:61
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:279
Phar Lap "MP" .exp and "MQ" .rex file.
Definition pharlap.h:15
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition pharlap.cc:191
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:145
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:42
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition pharlap.cc:202
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition pharlap.cc:37
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition pharlap.cc:155
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition pharlap.cc:87
Container for Phar Lap "P2"/"P3" .exp files, used when reading in a P2/P3 file.
Definition pharlap.h:355
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition pharlap.cc:1057
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition pharlap.cc:1045
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition pharlap.cc:1022
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition pharlap.cc:1037
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition pharlap.cc:994
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition pharlap.cc:1027
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition pharlap.cc:1032
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition pharlap.cc:1017
Definition pharlap.h:171
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition pharlap.cc:501
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition pharlap.cc:514
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:343
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition pharlap.cc:457
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition pharlap.cc:388
Definition pharlap.h:196
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition pharlap.cc:988
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition pharlap.cc:779
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition pharlap.cc:957
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition pharlap.cc:879
Phar Lap "P2"/"P3" .exp file.
Definition pharlap.h:88
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition pharlap.cc:271
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition pharlap.cc:220
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition pharlap.cc:266
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition pharlap.cc:332
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition pharlap.cc:225
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition pharlap.cc:283
Definition pharlap.h:27