RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
8bitexe.h
1#ifndef _8BITEXE_H
2#define _8BITEXE_H
3
4#include <algorithm>
5#include "binary.h"
6#include "../common.h"
7#include "../dumper/dumper.h"
8#include "../linker/module.h"
9#include "../linker/reader.h"
10#include "../linker/segment.h"
11#include "../linker/writer.h"
12
13/* TODO: this is all pretty much preliminary */
14/* TODO: combine into binary.h? */
15
16namespace Binary
17{
22 {
23 public:
24 /* TODO: untested */
25
26 /* TODO: enable setting the base address as a parameter */
27 /* TODO: SYS files are pure binary loaded at 0x2000 */
28
29 AppleFormat(uint64_t default_base_address = 0x0803, std::string default_extension = ".bin")
30 : GenericBinaryFormat(default_base_address, default_extension)
31 {
32 }
33
34 void ReadFile(Linker::Reader& rd) override;
35
37 offset_t WriteFile(Linker::Writer& wr) const override;
38 void Dump(Dumper::Dumper& dump) const override;
39 };
40
45 {
46 public:
47 /* exe, obj, com are also used */
48 AtariFormat(uint64_t default_base_address = 0, std::string default_extension = ".xex")
49 : GenericBinaryFormat(default_base_address, default_extension)
50 {
51 }
52
56 struct Segment
57 {
58 public:
62 enum segment_type : uint16_t
63 {
65 SIGNATURE_LOW = 0xFFFA,
67 SDX_FIXED = 0xFFFA,
69 SDX_SYMREQ = 0xFFFB,
71 SDX_SYMDEF = 0xFFFC,
73 SDX_FIXUPS = 0xFFFD,
75 SDX_RAMALLOC = 0xFFFE,
77 SDX_POSIND = 0xFFFE,
79 ATARI_SEGMENT = 0xFFFF,
80 };
92 uint16_t address = 0;
96 uint8_t block_number = 0;
97 enum control_byte_type : uint8_t
98 {
109 };
117 uint16_t size = 0;
121 char symbol_name[8] = { };
125 std::shared_ptr<Linker::Image> image;
129 std::set<uint16_t> relocations; // TODO: multiple blocks?
130
131 Segment(bool header_type_optional = true)
133 {
134 }
135
136 Segment(uint16_t header_type)
138 {
139 }
140
144 offset_t GetSize() const;
145
149 void ReadFile(Linker::Reader& rd);
150
154 void WriteFile(Linker::Writer& wr) const;
155
160
164 void WriteRelocations(Linker::Writer& wr) const;
165 };
166
170 std::vector<std::unique_ptr<Segment>> segments;
171
173 static const uint16_t LOADER_ADDRESS = 0x02E2;
174
179 static const uint16_t ENTRY_ADDRESS = 0x02E0;
180
182 bool HasEntryPoint() const;
184 void AddEntryPoint(uint16_t entry);
185
186 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
187
188 void ProcessModule(Linker::Module& module) override;
189
190 void ReadFile(Linker::Reader& rd) override;
191
193 offset_t WriteFile(Linker::Writer& wr) const override;
194 void Dump(Dumper::Dumper& dump) const override;
195 };
196
201 {
202 public:
203 /* TODO */
204
206 static const uint16_t BASIC_START = 0x0801;
207
208 enum
209 {
210 BASIC_SYS = 0x9E, /* BASIC token */
211 };
212
213 std::shared_ptr<Linker::Segment> loader; /* loader routine in BASIC */
214
215 void Clear() override;
216
218 {
219 Clear();
220 }
221
222 void SetupDefaultLoader();
223
224 void ProcessModule(Linker::Module& module) override;
225
227 offset_t WriteFile(Linker::Writer& wr) const override;
228 void Dump(Dumper::Dumper& dump) const override;
229
231 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
232 };
233
234 class PRLFormat;
235
240 {
241 public:
243 uint8_t preinit_code[10] = { 0xC9 }; /* z80 return instruction */
245 bool loader_active = true;
248 {
250 std::string rsx_file_name;
252 std::string name;
254 uint16_t offset = 0;
256 uint16_t length = 0;
258 bool nonbanked_only = false;
260 std::shared_ptr<PRLFormat> module;
261
263 void OpenAndPrepare();
264 };
266 std::vector<rsx_record> rsx_table;
267
268 void Clear() override;
269
270 CPM3Format()
271 : GenericBinaryFormat(0x0100, ".com")
272 {
273 }
274
276 {
277 Clear();
278 }
279
280 void SetOptions(std::map<std::string, std::string>& options) override;
281
282 void ReadFile(Linker::Reader& rd) override;
283
285 offset_t WriteFile(Linker::Writer& wr) const override;
286 void Dump(Dumper::Dumper& dump) const override;
287
288 void CalculateValues() override;
289 };
290
295 {
296 public:
297 /* TODO: enable setting the base address, default should be ??? */
298
299 struct Segment
300 {
301 public:
302 uint16_t address;
303 uint16_t size; /* it is supposed to be at most 255, but we can store larger segments by cutting them into pieces */
304 std::shared_ptr<Linker::Image> image;
305
306 void WriteFile(Linker::Writer& wr) const;
307 };
308
309 std::vector<std::unique_ptr<Segment>> segments;
310
311 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
312
314 offset_t WriteFile(Linker::Writer& wr) const override;
315 void Dump(Dumper::Dumper& dump) const override;
316
318 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
319 };
320
325 {
326 public:
328 uint16_t zero_fill = 0;
330 uint16_t load_address = 0;
332 uint16_t csbase = 0;
333
336
338 std::set<uint16_t> relocations;
339
340 PRLFormat(uint64_t default_base_address = 0, std::string default_extension = ".prl")
341 : GenericBinaryFormat(default_base_address, default_extension)
342 {
343 }
344
345 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
346
347 bool ProcessRelocation(Linker::Module& module, Linker::Relocation& rel, Linker::Resolution resolution) override;
348
349 void ReadFile(Linker::Reader& rd) override;
350
352 offset_t WriteFile(Linker::Writer& wr) const override;
353
355 void ReadWithoutHeader(Linker::Reader& rd, uint16_t image_size);
356
358 void WriteWithoutHeader(Linker::Writer& wr) const;
359
360 void Dump(Dumper::Dumper& dump) const override;
361 };
362
367 {
368 public:
369 /* TODO */
370 bool uzi180_header;
371 uint16_t entry;
372
373 void ProcessModule(Linker::Module& module) override;
374
376 offset_t WriteFile(Linker::Writer& wr) const override;
377 void Dump(Dumper::Dumper& dump) const override;
378
380 std::string GetDefaultExtension(Linker::Module& module) const override;
381 };
382
387 {
388 public:
389 std::shared_ptr<Linker::Image> code, data;
390
391 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
392
393 /* TODO: apparently both .code and .data are loaded at 0x0100 */
394
396 offset_t WriteFile(Linker::Writer& wr) const override;
397 void Dump(Dumper::Dumper& dump) const override;
398
400 std::string GetDefaultExtension(Linker::Module& module) const override;
401 };
402}
403
404#endif /* _8BITEXE_H */
BIN file for Apple ][.
Definition 8bitexe.h:22
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:24
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition 8bitexe.cc:12
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:33
EXE file for Atari 400/800.
Definition 8bitexe.h:45
static const uint16_t LOADER_ADDRESS
Address which contains a loader between to execute between loading segments.
Definition 8bitexe.h:173
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition 8bitexe.cc:211
void AddEntryPoint(uint16_t entry)
Attaches a new segment that contains the entry point.
Definition 8bitexe.cc:61
std::vector< std::unique_ptr< Segment > > segments
Sequence of segments.
Definition 8bitexe.h:170
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:230
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition 8bitexe.cc:195
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:240
static const uint16_t ENTRY_ADDRESS
Address which contains the actual entry address after loading.
Definition 8bitexe.h:179
bool HasEntryPoint() const
An entry point is present if the memory address at EntryAddress has been filled by a segment.
Definition 8bitexe.cc:51
CP/M Plus .com file format.
Definition 8bitexe.h:240
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition 8bitexe.cc:462
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition 8bitexe.cc:388
uint8_t preinit_code[10]
Pre-initialization code to be executed before fully loading program.
Definition 8bitexe.h:243
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:423
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:451
bool loader_active
Whether loader should be active, even if no RSXs are attached.
Definition 8bitexe.h:245
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition 8bitexe.cc:352
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition 8bitexe.cc:347
std::vector< rsx_record > rsx_table
The attached RSX records.
Definition 8bitexe.h:266
PRG file for Commodore PET/VIC-20/64.
Definition 8bitexe.h:201
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition 8bitexe.cc:253
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition 8bitexe.cc:280
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:286
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:295
static const uint16_t BASIC_START
Address at which the BASIC program should start.
Definition 8bitexe.h:206
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition 8bitexe.cc:306
FLEX .cmd file format.
Definition 8bitexe.h:295
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition 8bitexe.cc:518
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:497
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:507
A template for flat binary formats.
Definition binary.h:21
MP/M .prl file format.
Definition 8bitexe.h:325
std::set< uint16_t > relocations
Offsets to bytes referencing pages that must be relocated.
Definition 8bitexe.h:338
void WriteWithoutHeader(Linker::Writer &wr) const
Write without header, only needed for RSX files stored inside a CP/M 3 .COM file.
Definition 8bitexe.cc:618
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition 8bitexe.cc:554
bool suppress_relocations
Do not include relocations, only used for .OVL files.
Definition 8bitexe.h:335
uint16_t load_address
Address to be loaded at, only used for .OVL files.
Definition 8bitexe.h:330
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 8bitexe.cc:528
bool ProcessRelocation(Linker::Module &module, Linker::Relocation &rel, Linker::Resolution resolution) override
Callback function to process relocations.
Definition 8bitexe.cc:538
void ReadWithoutHeader(Linker::Reader &rd, uint16_t image_size)
Read without header, only needed for RSX files stored inside a CP/M 3 .COM file.
Definition 8bitexe.cc:569
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:641
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:602
uint16_t zero_fill
Additional memory to allocate, similar to .bss.
Definition 8bitexe.h:328
uint16_t csbase
BIOS link.
Definition 8bitexe.h:332
UZI280 file format.
Definition 8bitexe.h:387
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 8bitexe.cc:711
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:743
std::string GetDefaultExtension(Linker::Module &module) const override
Provides a default filename for the output file.
Definition 8bitexe.cc:754
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:729
UZI/UZI280 file formats.
Definition 8bitexe.h:367
std::string GetDefaultExtension(Linker::Module &module) const override
Provides a default filename for the output file.
Definition 8bitexe.cc:704
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:680
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:693
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition 8bitexe.cc:674
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
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 representation of a value within some binary data that has to be fixed up once the exact position o...
Definition relocation.h:27
Representing a resolved relocation.
Definition resolution.h:17
A helper class, encapsulating functionality needed to export binary data.
Definition writer.h:15
Represents a loadable segment in the binary.
Definition 8bitexe.h:57
uint16_t address
Address at which segment must be loaded.
Definition 8bitexe.h:92
offset_t GetSize() const
Retrieves the number of bytes in the segment body.
Definition 8bitexe.cc:46
uint8_t block_number
Only used for SDX_RAMALLOC/SDX_POSIND, SDX_SYMREQ.
Definition 8bitexe.h:96
char symbol_name[8]
Only used for SDX_SYMREQ, SDX_SYMDEF.
Definition 8bitexe.h:121
std::set< uint16_t > relocations
Relocations, only used for SDX_SYMREQ and SDX_FIXUPS.
Definition 8bitexe.h:129
void ReadRelocations(Linker::Reader &rd)
Read relocations.
Definition 8bitexe.cc:174
uint16_t size
Only used for SDX_RAMALLOC/SDX_POSIND.
Definition 8bitexe.h:117
bool header_type_optional
Set if placing header type is optional, also set when signature is absent in file when reading.
Definition 8bitexe.h:88
std::shared_ptr< Linker::Image > image
The binary data in the segment.
Definition 8bitexe.h:125
control_byte_type control_byte
Only used for SDX_RAMALLOC/SDX_POSIND.
Definition 8bitexe.h:113
control_byte_type
Definition 8bitexe.h:98
@ CB_RAMALLOC
SDX_RAMALLOC instead of SDX_POSIND.
Definition 8bitexe.h:108
@ CB_CONVRAM
allocate in conventional RAM
Definition 8bitexe.h:100
@ CB_PROGEXTAREA
(SDX 4.47+) allocate in program extended area
Definition 8bitexe.h:104
@ CB_SYSEXTAREA
allocate in system extended area
Definition 8bitexe.h:102
@ CB_PAGEALIGNED
(SDX 4.43+) page aligned
Definition 8bitexe.h:106
void WriteRelocations(Linker::Writer &wr) const
Writes relocations.
Definition 8bitexe.cc:179
void WriteFile(Linker::Writer &wr) const
Writes the segment into a file.
Definition 8bitexe.cc:123
void ReadFile(Linker::Reader &rd)
Reads a segment from a file into this object.
Definition 8bitexe.cc:71
segment_type
Types of segments, represented by their signature values.
Definition 8bitexe.h:63
@ ATARI_SEGMENT
Atari segment type.
Definition 8bitexe.h:79
@ SDX_FIXUPS
SpartaDOS X fixup information (not implemented)
Definition 8bitexe.h:73
@ SDX_SYMREQ
SpartaDOS X required symbols (not implemented)
Definition 8bitexe.h:69
@ SDX_FIXED
SpartaDOS X fixed-address segment (not implemented)
Definition 8bitexe.h:67
@ SDX_RAMALLOC
SpartaDOS X RAM allocation block (not implemented)
Definition 8bitexe.h:75
@ SDX_POSIND
SpartaDOS X position independent (not implemented)
Definition 8bitexe.h:77
@ SIGNATURE_LOW
Lowest currently defined signature to check on reading.
Definition 8bitexe.h:65
@ SDX_SYMDEF
SpartaDOS X symbol definitions (not implemented)
Definition 8bitexe.h:71
segment_type header_type
Header type, Atari DOS uses only 0xFFFF, signature only obligatory for the first segment.
Definition 8bitexe.h:84
A single RSX record.
Definition 8bitexe.h:248
std::string name
Name of RSX file, as stored inside RSX.
Definition 8bitexe.h:252
uint16_t offset
Offset to RSX block.
Definition 8bitexe.h:254
std::string rsx_file_name
Name of RSX file to load, only used for writing.
Definition 8bitexe.h:250
uint16_t length
Length of RSX module, only used for reading.
Definition 8bitexe.h:256
bool nonbanked_only
Whether RSX is only loaded on non-banked systems.
Definition 8bitexe.h:258
std::shared_ptr< PRLFormat > void OpenAndPrepare()
The actual RSX data, stored in PRLFormat (on disk, without the header)
Definition 8bitexe.cc:313
Definition 8bitexe.h:300