RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
xpexp.h
1#ifndef XPEXP_H
2#define XPEXP_H
3
4#include "../common.h"
5#include "../dumper/dumper.h"
6#include "../linker/options.h"
7#include "../linker/reader.h"
8#include "../linker/segment_manager.h"
9#include "../linker/writer.h"
10#include "mzexe.h"
11
12/* TODO: unimplemented */
13
14namespace Ergo
15{
19 class XPFormat : public virtual Linker::SegmentManager
20 {
21 public:
22 /* * * General members * * */
23
24 struct Segment
25 {
26 public:
27 uint32_t base = 0;
28 uint32_t limit = 0;
29 enum
30 {
31 ACCESS_DATA = 0xF2, // writable ring 3 data
32 ACCESS_CODE = 0xFA, // readable ring 3 code
33
34 ACCESS_A = 0x01,
35 ACCESS_W = 0x02, // data only
36 ACCESS_R = 0x02, // code only
37 ACCESS_E = 0x04, // data only
38 ACCESS_C = 0x04, // code only
39
40 ACCESS_X = 0x08,
41 ACCESS_S = 0x10,
42
43 ACCESS_CPL0 = 0x00,
44 ACCESS_CPL1 = 0x20,
45 ACCESS_CPL2 = 0x40,
46 ACCESS_CPL3 = 0x60,
47
48 ACCESS_P = 0x80,
49
50 ACCESS_TYPE_EMPTY = 0x00,
51 ACCESS_TYPE_TSS16_A = 0x01,
52 ACCESS_TYPE_LDT = 0x02,
53 ACCESS_TYPE_TSS16_B = 0x03,
54 ACCESS_TYPE_CALLGATE16 = 0x04,
55 ACCESS_TYPE_TASKGATE = 0x05,
56 ACCESS_TYPE_INTGATE16 = 0x06,
57 ACCESS_TYPE_TRAPGATE16 = 0x07,
58
59 ACCESS_TYPE_TSS32_A = 0x09,
60
61 ACCESS_TYPE_TSS32_B = 0x0B,
62 ACCESS_TYPE_CALLGATE32 = 0x0C,
63
64 ACCESS_TYPE_INTGATE32 = 0x0E,
65 ACCESS_TYPE_TRAPGATE32 = 0x0F,
66 };
67 uint8_t access = 0;
68 enum
69 {
70 FLAG_ALIAS = 0x10,
71 FLAG_WINDOW = 0x20,
72 FLAG_32BIT = 0x40,
73 FLAG_PAGES = 0x80, // instead of bytes, for limit size
74 };
75 uint8_t flags = 0;
76
77 Segment()
78 {
79 }
80
81 Segment(uint32_t base, uint32_t limit, uint8_t access, uint8_t flags)
82 : base(base), limit(limit), access(access), flags(flags)
83 {
84 }
85
86 static Segment ReadFile(Linker::Reader& rd);
87 void WriteFile(Linker::Writer& wr) const;
88 void Dump(Dumper::Dumper& dump, const XPFormat& xp, unsigned index) const;
89 };
90
91 uint32_t ldt_offset = 0;
92 uint32_t image_offset = 0;
93 uint32_t relocation_offset = 0; // TODO: unsure
94 uint32_t relocation_count = 0; // TODO: replace with std::vector<Relocation> once the relocation format is known
95 uint32_t minimum_extent = 0;
96 uint32_t maximum_extent = 0;
97 uint32_t unknown_field = 0;
98 uint32_t gs = 0, fs = 0, ds = 0, ss = 0, cs = 0, es = 0, edi = 0, esi = 0, ebp = 0, esp = 0, ebx = 0, edx = 0, ecx = 0, eax = 0, eflags = 0, eip = 0;
99 std::vector<Segment> ldt;
100 std::shared_ptr<Linker::Image> image;
101
102 void Clear() override;
103 void CalculateValues() override;
104 void ReadFile(Linker::Reader& rd) override;
106 offset_t WriteFile(Linker::Writer& wr) const override;
107 offset_t ImageSize() const override;
108 void Dump(Dumper::Dumper& dump) const override;
109
110 /* * * Writer members * * */
112 {
113 public:
114 Linker::Option<std::string> stub{"stub", "Filename for stub that gets prepended to executable"};
115 Linker::Option<bool> dual_selector{"dualsel", "Always generate pairs of code/data selectors"};
116 Linker::Option<bool> no_intermediate_selector{"nointer", "Do not generate selectors for segments inside groups"};
117
119 {
120 InitializeFields(stub, dual_selector, no_intermediate_selector);
121 }
122 };
123
124 mutable Microsoft::MZStubWriter stub;
125
126 enum Wordsize
127 {
128 Unknown = 0,
129 Word = 2,
130 DWord = 4,
131 };
132 Wordsize wordsize = Unknown;
133
144 bool option_no_intermediate_selectors = false; // TODO: groups are not yet implemented
145
158
159 bool FormatSupportsSegmentation() const override;
160
161 bool FormatIs16bit() const override;
162
163 bool FormatIsProtectedMode() const override;
164 static std::vector<Linker::OptionDescription<void>> MemoryModelNames;
165
166 std::vector<Linker::OptionDescription<void>> GetMemoryModelNames() override;
167
168 void SetModel(std::string model) override;
169
170 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
171
172 void SetOptions(std::map<std::string, std::string>& options) override;
173
174 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
175
181
182 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
183
187 void Link(Linker::Module& module);
188
189 void ProcessModule(Linker::Module& module) override;
190
191 std::shared_ptr<Linker::Segment> GetImageSegment();
192
193 class Group
194 {
195 public:
196 size_t first_section;
197 size_t section_count;
198
199 offset_t GetStartAddress(XPFormat * format) const;
200 offset_t GetLength(XPFormat * format) const;
201 };
202 std::vector<Group> section_groups;
203
205 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
206 };
207}
208
209#endif /* XPEXP_H */
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:586
Definition xpexp.h:194
Definition xpexp.h:112
Ergo OS/286 and OS/386 "XP" .exp file (Ergo was formerly A.I. Architects, then Eclipse)
Definition xpexp.h:20
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition xpexp.cc:30
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition xpexp.cc:283
void SetModel(std::string model) override
Sets the way memory is organized, typically modifying a built-in script.
Definition xpexp.cc:335
bool FormatIs16bit() const override
Whether the format is 16-bit or not.
Definition xpexp.cc:288
bool FormatIsProtectedMode() const override
Whether the format is in protected mode or not (x86 only)
Definition xpexp.cc:293
bool option_create_selector_pairs
Generate code/data selector pairs for all segments.
Definition xpexp.h:140
memory_model_t memory_model
Memory model of generated executable.
Definition xpexp.h:157
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition xpexp.cc:14
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition xpexp.cc:298
std::vector< Linker::OptionDescription< void > > GetMemoryModelNames() override
Returns a list of the supported memory models, used for documentation.
Definition xpexp.cc:330
offset_t ImageSize() const override
Retrieves size of stored data.
Definition xpexp.cc:146
void CreateDefaultSegments()
Create the required segments, if they have not already been allocated. The XP format uses a single se...
Definition xpexp.cc:373
memory_model_t
Represents the memory model of the running executable, which is the way in which the segments are set...
Definition xpexp.h:148
@ MODEL_SMALL
Small model, separate code and data segments.
Definition xpexp.h:152
@ MODEL_COMPACT
Compact model, separate code and multiple data segments.
Definition xpexp.h:154
@ MODEL_DEFAULT
Default model, same as small.
Definition xpexp.h:150
bool option_no_intermediate_selectors
No selectors are generated for segments in groups.
Definition xpexp.h:144
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition xpexp.cc:447
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition xpexp.cc:153
void Link(Linker::Module &module)
Link application according to script or memory model.
Definition xpexp.cc:438
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition xpexp.cc:48
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition xpexp.cc:315
std::shared_ptr< Linker::OptionCollector > GetOptions() override
Returns object containing a sequence of option fields provided with the -S command line flag.
Definition xpexp.cc:310
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition xpexp.cc:97
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
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
Definition xpexp.h:25