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 "../linker/linker.h"
6#include "mzexe.h"
7
8namespace PharLap
9{
14 {
15 public:
16 void ReadFile(Linker::Reader& rd) override;
17
18 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
19
20 bool has_relocations;
21 std::string stub_file;
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
58 MPFormat(bool has_relocations = false)
59 : has_relocations(has_relocations)
60 {
61 }
62
63 using LinkerManager::SetLinkScript;
64
65 void SetOptions(std::map<std::string, std::string>& options) override;
66
67 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
68
69 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
70
71 void Link(Linker::Module& module);
72
73 void ProcessModule(Linker::Module& module) override;
74
75 void CalculateValues() override;
76
77 void WriteFile(Linker::Writer& wr) override;
78
79 std::string GetDefaultExtension(Linker::Module& module, std::string filename) override;
80 };
81
86 {
87 public:
88 void ReadFile(Linker::Reader& rd) override;
89
90 bool FormatSupportsSegmentation() const override;
91
92#if 0
93 bool FormatSupportsStackSection() const override;
94#endif
95
96 /*std::string stub_file;*/
97 const bool is_multisegmented;
98 bool is_32bit;
99
100 uint16_t header_size = 0;
101 uint32_t file_size = 0;
102
103 uint32_t runtime_parameters_offset = 0;
104 uint32_t runtime_parameters_size = 0;
105 uint32_t relocation_table_offset = 0;
106 uint32_t relocation_table_size = 0;
107 uint32_t segment_information_table_offset = 0;
108 uint32_t segment_information_table_size = 0;
109 uint16_t segment_information_table_entry_size = 0;
110 uint32_t load_image_offset = 0;
111 uint32_t load_image_size = 0;
112 uint32_t symbol_table_offset = 0;
113 uint32_t symbol_table_size = 0;
114 uint32_t gdt_address = 0;
115 uint32_t gdt_size = 0;
116 uint32_t ldt_address = 0;
117 uint32_t ldt_size = 0;
118 uint32_t idt_address = 0;
119 uint32_t idt_size = 0;
120 uint32_t tss_address = 0;
121 uint32_t tss_size = 0;
122 uint32_t minimum_extra = 0;
123 uint32_t maximum_extra = 0;
124 uint32_t base_load_offset = 0;
125 uint32_t esp = 0;
126 uint16_t ss = 0;
127 uint32_t eip = 0;
128 uint16_t cs = 0;
129 uint16_t ldtr = 0;
130 uint16_t tr = 0;
131 uint16_t flags = 0;
132 uint32_t memory_requirements = 0;
133 uint32_t stack_size = 0;
134
135 P3Format(bool is_multisegmented, bool is_32bit = true)
136 : is_multisegmented(is_multisegmented), is_32bit(is_32bit)
137 {
138 }
139
141 {
142 public:
143 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;
144 uint32_t realmode_area_end = 0;
145 uint16_t call_buffer_size_kb = 0, flags = 0, ring = 0;
146
147 void CalculateValues();
148
149 void WriteFile(Linker::Writer& wr);
150 };
151
152 RunTimeParameterBlock runtime_parameters;
153
154 using LinkerManager::SetLinkScript;
155
156 void SetOptions(std::map<std::string, std::string>& options) override;
157
158 std::string GetDefaultExtension(Linker::Module& module, std::string filename) override;
159
160 void WriteFile(Linker::Writer& wr) override;
161
162 class Flat;
163 class MultiSegmented;
164 };
165
167 {
168 public:
169 Flat(bool is_32bit = true)
170 : P3Format(false, is_32bit)
171 {
172 }
173
174 std::shared_ptr<Linker::Segment> image;
175
176 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
177
178 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
179
180 void Link(Linker::Module& module);
181
182 void ProcessModule(Linker::Module& module) override;
183
184 void CalculateValues() override;
185
186 void WriteFile(Linker::Writer& wr) override;
187 };
188
190 {
191 public:
193 {
194 public:
195 uint32_t address = 0;
196 virtual ~AbstractSegment();
197 virtual uint32_t GetStoredSize() = 0;
198 virtual uint32_t GetLoadedSize() = 0;
199 virtual void WriteFile(Linker::Writer& wr) = 0;
200 };
201
203 {
204 public:
205 std::weak_ptr<AbstractSegment> image;
206
207 enum
208 {
209 TSS16 = 0x00008100,
210 LDT = 0x00008200,
211 TSS32 = 0x00008900,
212 Code16 = 0x00009A00,
213 Code32 = 0x00409A00,
214 Data16 = 0x00009200,
215 Data32 = 0x00409200,
216
217 DESC_G = 0x00800000,
218 };
219
220 uint32_t limit = 0;
221 uint32_t base = 0;
222 uint32_t access;
223
224 Descriptor(uint32_t access, std::weak_ptr<AbstractSegment> image = std::weak_ptr<AbstractSegment>())
225 : image(image), access(access)
226 {
227 }
228
229 void CalculateValues();
230
231 void WriteEntry(Linker::Writer& wr);
232 };
233
235 {
236 public:
237 std::vector<Descriptor> descriptors;
238
239 uint32_t GetStoredSize() override;
240
241 uint32_t GetLoadedSize() override;
242
243 void WriteFile(Linker::Writer& wr) override;
244
245 void CalculateValues();
246 };
247
248 std::shared_ptr<DescriptorTable> gdt;
249 std::shared_ptr<DescriptorTable> idt;
250 std::shared_ptr<DescriptorTable> ldt;
251
253 {
254 public:
255 bool is_32bit;
256
257 uint32_t esp0 = 0, esp1 = 0, esp2 = 0;
258 uint32_t cr3 = 0, eip = 0, eflags = 0, eax = 0, ecx = 0, edx = 0, ebx = 0, esp = 0, ebp = 0, esi = 0, edi = 0;
259 uint16_t ss0 = 0, ss1 = 0, ss2 = 0, es = 0, cs = 0, ss = 0, ds = 0, fs = 0, gs = 0, ldtr = 0, iopb = 0;
260 uint16_t link = 0;
261
262 TaskStateSegment(bool is_32bit = true)
263 : is_32bit(is_32bit)
264 {
265 }
266
267 uint32_t GetStoredSize() override;
268
269 uint32_t GetLoadedSize() override;
270
271 void WriteFile(Linker::Writer& wr) override;
272 };
273
274 std::shared_ptr<TaskStateSegment> tss;
275
277 {
278 public:
279 /* Segment members */
280 std::shared_ptr<Linker::Segment> segment;
281
282 uint32_t access;
283 uint16_t selector;
284 uint16_t flags = 0;
285 uint32_t base_offset = 0; /* TODO??? */
286
287 Segment(std::shared_ptr<Linker::Segment> segment, uint32_t access, uint16_t selector)
288 : segment(segment), access(access), selector(selector)
289 {
290 }
291
292 uint32_t GetStoredSize() override;
293
294 uint32_t GetLoadedSize() override;
295
296 void WriteSITEntry(Linker::Writer& wr);
297
298 void WriteFile(Linker::Writer& wr) override;
299 };
300
301 MultiSegmented(bool is_32bit = true)
302 : P3Format(true, is_32bit)
303 {
304 }
305
307 {
308 public:
309 std::shared_ptr<Segment> segment;
310 uint32_t offset;
311
312 Relocation(std::shared_ptr<Segment> segment, uint32_t offset)
313 : segment(segment), offset(offset)
314 {
315 }
316
317 bool operator ==(const Relocation& other) const;
318
319 bool operator <(const Relocation& other) const;
320
321 void WriteFile(Linker::Writer& wr) const;
322 };
323
324 std::vector<std::shared_ptr<AbstractSegment>> segments;
325 std::map<std::shared_ptr<Linker::Segment>, std::shared_ptr<Segment>> segment_associations;
326 std::set<Relocation> relocations;
327 std::shared_ptr<Segment> code;
328 std::shared_ptr<Segment> data;
329
330 void OnNewSegment(std::shared_ptr<Linker::Segment> linker_segment) override;
331
332 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
333
334 void Link(Linker::Module& module);
335
336 void ProcessModule(Linker::Module& module) override;
337
338 void CalculateValues() override;
339
340 void WriteFile(Linker::Writer& wr) override;
341 };
342}
343
344#endif /* PHARLAP_H */
A helper class to collect sections into segments.
Definition linker.h:19
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:20
A class that provides a general interface to setting up generation for a format.
Definition format.h:56
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:16
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:14
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition pharlap.cc:8
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition pharlap.cc:143
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:40
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition pharlap.cc:35
void WriteFile(Linker::Writer &wr) override
Stores data in memory to file.
Definition pharlap.cc:153
std::string GetDefaultExtension(Linker::Module &module, std::string filename) override
Appends a default extension to the filename.
Definition pharlap.cc:187
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition pharlap.cc:85
Definition pharlap.h:167
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:315
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition pharlap.cc:429
void WriteFile(Linker::Writer &wr) override
Stores data in memory to file.
Definition pharlap.cc:473
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition pharlap.cc:360
Definition pharlap.h:190
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition pharlap.cc:743
void WriteFile(Linker::Writer &wr) override
Stores data in memory to file.
Definition pharlap.cc:921
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition pharlap.cc:843
Phar Lap "P2"/"P3" .exp file.
Definition pharlap.h:86
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition pharlap.cc:205
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition pharlap.cc:251
void WriteFile(Linker::Writer &wr) override
Stores data in memory to file.
Definition pharlap.cc:268
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition pharlap.cc:210
std::string GetDefaultExtension(Linker::Module &module, std::string filename) override
Appends a default extension to the filename.
Definition pharlap.cc:256
Definition pharlap.h:27