RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
bwexp.h
1#ifndef BWEXP_H
2#define BWEXP_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 DOS16M
11{
15 class BWFormat : 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 bool FormatSupportsSegmentation() const override;
32
33 bool FormatIs16bit() const override;
34
35 bool FormatIsProtectedMode() const override;
36
37 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
38
40 {
41 public:
42 enum access_type
43 {
44 TYPE_DATA = 0x92,
45 TYPE_CODE = 0x9A,
46 };
47 access_type access;
48
49 enum flag_type
50 {
51 FLAG_EMPTY = 0x2000,
52 FLAG_TRANSPARENT = 0x8000,
53 };
54 flag_type flags;
55
56 uint32_t address = 0;
57 uint32_t total_length;
58
59 AbstractSegment(unsigned access = TYPE_DATA, unsigned flags = 0, uint32_t total_length = 0)
60 : access(access_type(access)), flags(flag_type(flags)), total_length(total_length)
61 {
62 }
63
64 virtual ~AbstractSegment();
65
69 uint32_t GetTotalSize();
70
74 virtual void SetTotalSize(uint32_t new_value);
75
79 virtual uint32_t GetSize(const BWFormat& bw) const = 0;
83 virtual void WriteContent(Linker::Writer& wr, const BWFormat& bw) const = 0;
84
88 void Prepare(BWFormat& bw);
89
93 void WriteHeader(Linker::Writer& wr, const BWFormat& bw) const;
94 };
95
96 class Segment : public AbstractSegment
97 {
98 public:
99 std::shared_ptr<Linker::Segment> image;
100
101 Segment(std::shared_ptr<Linker::Segment> segment, unsigned access = TYPE_DATA, unsigned flags = 0)
102 : AbstractSegment(access, flags, segment->TotalSize()), image(segment)
103 {
104 }
105
106 void SetTotalSize(uint32_t new_value) override;
107
108 uint32_t GetSize(const BWFormat& bw) const override;
109
110 void WriteContent(Linker::Writer& wr, const BWFormat& bw) const override;
111 };
112
114 {
115 public:
116 DummySegment(uint32_t total_length, unsigned access = TYPE_DATA, unsigned flags = 0)
117 : AbstractSegment(access, flags, total_length)
118 {
119 }
120
121 void SetTotalSize(uint32_t new_value) override;
122
123 uint32_t GetSize(const BWFormat& bw) const override;
124
125 void WriteContent(Linker::Writer& wr, const BWFormat& bw) const override;
126 };
127
129 {
130 public:
131 uint16_t index;
132
133 RelocationSegment(uint16_t index)
134 : AbstractSegment(TYPE_DATA), index(index)
135 {
136 }
137
141 void SetTotalSize(uint32_t new_value) override;
142
143 uint32_t GetSize(const BWFormat& bw) const override;
144
145 void WriteContent(Linker::Writer& wr, const BWFormat& bw) const override;
146 };
147
152 {
153 RelocationsNone,
154 RelocationsType1,
155 RelocationsType2,
156 };
157 relocations_type option_relocations = RelocationsType2;
158 bool option_force_relocations = true;
159
160 enum exp_flag_type
161 {
162 EXP_FLAG_RELOCATABLE = 0x0001,
163 };
164 exp_flag_type exp_flags = exp_flag_type(0);
165
166 enum option_type
167 {
168 OPTION_RELOCATIONS = 0x1000,
169 };
170 option_type options = option_type(0);
171
172 std::map<uint16_t, std::set<uint16_t> > relocations;
173
174 offset_t MeasureRelocations() const;
175
176 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
177
178 void SetOptions(std::map<std::string, std::string>& options) override;
179
180 offset_t file_size = 0;
181 offset_t min_extra = 0;
182 offset_t max_extra = 0;
183 uint16_t ss = 0, sp = 0, cs = 0, ip = 0, relocsel = 0;
184 uint16_t runtime_gdt_length = 0xFFFF;
185 uint16_t version = 0;
186 uint32_t next_header_offset = 0;
187 uint32_t debug_info_offset = 0; /* TODO: ??? */
188 uint16_t first_selector = 0x0080; /* TODO: make dynamic */
189 uint32_t private_xm = 0; /* TODO: make parameter */
190 uint16_t ext_reserve = 0; /* TODO: ??? */
191 uint16_t transparent_stack = 0; /* TODO: ??? */
192 uint32_t program_size = 0; /* TODO: ??? */
193 uint8_t default_memory_strategy = 0; /* TODO: ??? */
194 uint16_t transfer_buffer_size = 0; /* TODO: ??? */
195 std::string exp_name; /* TODO: ??? */
196
197 mutable Microsoft::MZStubWriter stub;
198
199 std::vector<std::unique_ptr<AbstractSegment>> segments;
200 std::map<std::shared_ptr<Linker::Segment>, size_t> segment_indices;
201 int default_data = 0;
202
203 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
204
205 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
206
207 void Link(Linker::Module& module);
208
209 size_t GetDefaultDataIndex();
210
211 void ProcessModule(Linker::Module& module) override;
212
213 void CalculateValues() override;
214
215 offset_t ImageSize() const override;
216
218 offset_t WriteFile(Linker::Writer& wr) const override;
219
220 void Dump(Dumper::Dumper& dump) const override;
221
223 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
224 };
225}
226
227#endif /* BWEXP_H */
virtual void WriteContent(Linker::Writer &wr, const BWFormat &bw) const =0
Produces the binary contents of the segment.
virtual uint32_t GetSize(const BWFormat &bw) const =0
Retrieves size of segment. Some subclasses might calculate this dynamically.
uint32_t GetTotalSize()
Retrieves the total size of the segment, including the bss.
Definition bwexp.cc:44
virtual void SetTotalSize(uint32_t new_value)
Modifies the total size of the segment, including the bss. Note that this is not guaranteed to work f...
Definition bwexp.cc:49
void Prepare(BWFormat &bw)
Sets up any values before it can be written to file.
Definition bwexp.cc:61
void WriteHeader(Linker::Writer &wr, const BWFormat &bw) const
Produces the GDT entry for the header.
Definition bwexp.cc:70
Definition bwexp.h:114
uint32_t GetSize(const BWFormat &bw) const override
Retrieves size of segment. Some subclasses might calculate this dynamically.
Definition bwexp.cc:103
void SetTotalSize(uint32_t new_value) override
Modifies the total size of the segment, including the bss. Note that this is not guaranteed to work f...
Definition bwexp.cc:98
void WriteContent(Linker::Writer &wr, const BWFormat &bw) const override
Produces the binary contents of the segment.
Definition bwexp.cc:108
uint32_t GetSize(const BWFormat &bw) const override
Retrieves size of segment. Some subclasses might calculate this dynamically.
Definition bwexp.cc:117
void SetTotalSize(uint32_t new_value) override
Invalid call, the size of a relocation segment is calculated dynamically and cannot be changed via a ...
Definition bwexp.cc:112
void WriteContent(Linker::Writer &wr, const BWFormat &bw) const override
Produces the binary contents of the segment.
Definition bwexp.cc:138
Definition bwexp.h:97
uint32_t GetSize(const BWFormat &bw) const override
Retrieves size of segment. Some subclasses might calculate this dynamically.
Definition bwexp.cc:85
void SetTotalSize(uint32_t new_value) override
Modifies the total size of the segment, including the bss. Note that this is not guaranteed to work f...
Definition bwexp.cc:79
void WriteContent(Linker::Writer &wr, const BWFormat &bw) const override
Produces the binary contents of the segment.
Definition bwexp.cc:90
Rational Systems DOS/16M "BW" .exp file.
Definition bwexp.h:16
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition bwexp.cc:505
offset_t ImageSize() const override
Retrieves size of stored data.
Definition bwexp.cc:434
std::shared_ptr< Linker::OptionCollector > GetOptions() override
Returns object containing a sequence of option fields provided with the -S command line flag.
Definition bwexp.cc:223
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition bwexp.cc:389
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition bwexp.cc:228
relocations_type
BW .exp files support two versions of relocations.
Definition bwexp.h:152
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition bwexp.cc:494
bool FormatIsProtectedMode() const override
Whether the format is in protected mode or not (x86 only)
Definition bwexp.cc:23
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition bwexp.cc:13
bool FormatIs16bit() const override
Whether the format is 16-bit or not.
Definition bwexp.cc:18
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition bwexp.cc:439
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition bwexp.cc:8
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition bwexp.cc:308
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
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