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/options.h"
10#include "../linker/reader.h"
11#include "../linker/segment.h"
12#include "../linker/writer.h"
13
14/* TODO: this is all pretty much preliminary */
15/* TODO: combine into binary.h? */
16
17namespace Binary
18{
23 {
24 public:
25 /* TODO: untested */
26
27 /* TODO: enable setting the base address as a parameter */
28 /* TODO: SYS files are pure binary loaded at 0x2000 */
29
30 AppleFormat(uint64_t default_base_address = 0x0803, std::string default_extension = ".bin")
31 : GenericBinaryFormat(default_base_address, default_extension)
32 {
33 }
34
35 void ReadFile(Linker::Reader& rd) override;
36
38 offset_t WriteFile(Linker::Writer& wr) const override;
39 void Dump(Dumper::Dumper& dump) const override;
40 };
41
46 {
47 public:
48 /* exe, obj, com are also used */
49 AtariFormat(uint64_t default_base_address = 0, std::string default_extension = ".xex")
50 : GenericBinaryFormat(default_base_address, default_extension)
51 {
52 }
53
57 struct Segment
58 {
59 public:
63 enum segment_type : uint16_t
64 {
66 SIGNATURE_LOW = 0xFFFA,
68 SDX_FIXED = 0xFFFA,
70 SDX_SYMREQ = 0xFFFB,
72 SDX_SYMDEF = 0xFFFC,
74 SDX_FIXUPS = 0xFFFD,
76 SDX_RAMALLOC = 0xFFFE,
78 SDX_POSIND = 0xFFFE,
80 ATARI_SEGMENT = 0xFFFF,
81 };
93 uint16_t address = 0;
97 uint8_t block_number = 0;
98 enum control_byte_type : uint8_t
99 {
110 };
118 uint16_t size = 0;
122 char symbol_name[8] = { };
126 std::shared_ptr<Linker::Image> image;
130 std::set<uint16_t> relocations; // TODO: multiple blocks?
131
132 Segment(bool header_type_optional = true)
134 {
135 }
136
137 Segment(uint16_t header_type)
139 {
140 }
141
145 offset_t GetSize() const;
146
150 void ReadFile(Linker::Reader& rd);
151
155 void WriteFile(Linker::Writer& wr) const;
156
161
165 void WriteRelocations(Linker::Writer& wr) const;
166 };
167
171 std::vector<std::unique_ptr<Segment>> segments;
172
174 static const uint16_t LOADER_ADDRESS = 0x02E2;
175
180 static const uint16_t ENTRY_ADDRESS = 0x02E0;
181
183 bool HasEntryPoint() const;
185 void AddEntryPoint(uint16_t entry);
186
187 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
188
189 void ProcessModule(Linker::Module& module) override;
190
191 void ReadFile(Linker::Reader& rd) override;
192
194 offset_t WriteFile(Linker::Writer& wr) const override;
195 void Dump(Dumper::Dumper& dump) const override;
196 };
197
202 {
203 public:
204 /* TODO */
205
207 static const uint16_t BASIC_START = 0x0801;
208
209 enum
210 {
211 BASIC_SYS = 0x9E, /* BASIC token */
212 };
213
214 std::shared_ptr<Linker::Segment> loader; /* loader routine in BASIC */
215
216 void Clear() override;
217
219 {
220 Clear();
221 }
222
223 void SetupDefaultLoader();
224
225 void ProcessModule(Linker::Module& module) override;
226
228 offset_t WriteFile(Linker::Writer& wr) const override;
229 void Dump(Dumper::Dumper& dump) const override;
230
232 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
233 };
234
235 class PRLFormat;
236
241 {
242 public:
244 {
245 public:
246 Linker::Option<std::optional<std::vector<std::string>>> rsx_file_names{"rsx", "List of filenames to append as Resident System Extensions"};
247
249 {
250 InitializeFields(rsx_file_names);
251 }
252 };
253
255 uint8_t preinit_code[10] = { 0xC9 }; /* z80 return instruction */
257 bool loader_active = true;
260 {
262 std::string rsx_file_name;
264 std::string name;
266 uint16_t offset = 0;
268 uint16_t length = 0;
270 bool nonbanked_only = false;
272 std::shared_ptr<PRLFormat> module;
273
275 void OpenAndPrepare();
276 };
278 std::vector<rsx_record> rsx_table;
279
280 void Clear() override;
281
282 CPM3Format()
283 : GenericBinaryFormat(0x0100, ".com")
284 {
285 }
286
288 {
289 Clear();
290 }
291
292 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
293
294 void SetOptions(std::map<std::string, std::string>& options) override;
295
296 void ReadFile(Linker::Reader& rd) override;
297
299 offset_t WriteFile(Linker::Writer& wr) const override;
300 void Dump(Dumper::Dumper& dump) const override;
301
302 void CalculateValues() override;
303 };
304
309 {
310 public:
311 /* TODO: enable setting the base address, default should be ??? */
312
313 struct Segment
314 {
315 public:
316 uint16_t address;
317 uint16_t size; /* it is supposed to be at most 255, but we can store larger segments by cutting them into pieces */
318 std::shared_ptr<Linker::Image> image;
319
320 void WriteFile(Linker::Writer& wr) const;
321 };
322
323 std::vector<std::unique_ptr<Segment>> segments;
324
325 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
326
328 offset_t WriteFile(Linker::Writer& wr) const override;
329 void Dump(Dumper::Dumper& dump) const override;
330
332 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
333 };
334
339 {
340 public:
342 {
343 public:
344 Linker::Option<bool> banked{"banked", "Generated .SPR file for banked CP/M 3"};
345
347 {
348 InitializeFields(banked);
349 }
350 };
351
353 uint16_t zero_fill = 0;
355 uint16_t load_address = 0; // TODO: implement
357 uint16_t cslen = 0; // TODO: implement
358
361
363 bool option_banked_bios = false; // TODO: make flag, implement behavior
364
366 std::set<uint16_t> relocations;
367
395
396 static uint16_t GetDefaultBaseAddress(application_type application);
397 static std::string GetDefaultApplicationExtension(application_type application);
398
400 :
401 GenericBinaryFormat(GetDefaultBaseAddress(application), GetDefaultApplicationExtension(application)),
404 {
405 }
406
407 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
408 void SetOptions(std::map<std::string, std::string>& options) override;
409
410 std::unique_ptr<Script::List> GetScript(Linker::Module& module) override;
411
412 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
413
414 bool ProcessRelocation(Linker::Module& module, Linker::Relocation& rel, Linker::Resolution resolution) override;
415
416 void ProcessModule(Linker::Module& module) override;
417
418 void ReadFile(Linker::Reader& rd) override;
419
421 offset_t WriteFile(Linker::Writer& wr) const override;
422
424 void ReadWithoutHeader(Linker::Reader& rd, uint16_t image_size);
425
427 void WriteWithoutHeader(Linker::Writer& wr) const;
428
429 void Dump(Dumper::Dumper& dump) const override;
430 };
431
436 {
437 public:
438 /* TODO */
439 bool uzi180_header;
440 uint16_t entry;
441
442 void ProcessModule(Linker::Module& module) override;
443
445 offset_t WriteFile(Linker::Writer& wr) const override;
446 void Dump(Dumper::Dumper& dump) const override;
447
449 std::string GetDefaultExtension(Linker::Module& module) const override;
450 };
451
456 {
457 public:
458 std::shared_ptr<Linker::Image> code, data;
459
460 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
461
462 /* TODO: apparently both .code and .data are loaded at 0x0100 */
463
465 offset_t WriteFile(Linker::Writer& wr) const override;
466 void Dump(Dumper::Dumper& dump) const override;
467
469 std::string GetDefaultExtension(Linker::Module& module) const override;
470 };
471}
472
473#endif /* _8BITEXE_H */
BIN file for Apple ][.
Definition 8bitexe.h:23
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:46
static const uint16_t LOADER_ADDRESS
Address which contains a loader between to execute between loading segments.
Definition 8bitexe.h:174
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:171
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:180
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:241
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition 8bitexe.cc:464
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition 8bitexe.cc:390
uint8_t preinit_code[10]
Pre-initialization code to be executed before fully loading program.
Definition 8bitexe.h:255
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:425
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:453
bool loader_active
Whether loader should be active, even if no RSXs are attached.
Definition 8bitexe.h:257
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition 8bitexe.cc:357
std::shared_ptr< Linker::OptionCollector > GetOptions() override
Returns object containing a sequence of option fields provided with the -S command line flag.
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:278
PRG file for Commodore PET/VIC-20/64.
Definition 8bitexe.h:202
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:207
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:309
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition 8bitexe.cc:520
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:499
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:509
A template for flat binary formats.
Definition binary.h:21
MP/M .prl file format.
Definition 8bitexe.h:339
std::set< uint16_t > relocations
Offsets to bytes referencing pages that must be relocated.
Definition 8bitexe.h:366
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:716
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition 8bitexe.cc:652
bool suppress_relocations
Do not include relocations, only used for .OVL files.
Definition 8bitexe.h:360
uint16_t load_address
Address to be loaded at, only used for .OVL files.
Definition 8bitexe.h:355
uint16_t cslen
Code segment length, only used for .SPR files in banked systems.
Definition 8bitexe.h:357
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:609
bool ProcessRelocation(Linker::Module &module, Linker::Relocation &rel, Linker::Resolution resolution) override
Callback function to process relocations.
Definition 8bitexe.cc:619
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:667
std::shared_ptr< Linker::OptionCollector > GetOptions() override
Returns object containing a sequence of option fields provided with the -S command line flag.
Definition 8bitexe.cc:527
application_type
The format of the generated binary.
Definition 8bitexe.h:375
@ APPL_OVL
CP/M-80 .ovl overlay (unsupported)
Definition 8bitexe.h:391
@ APPL_SPR
MP/M-80 .spr system module file (unsupported)
Definition 8bitexe.h:389
@ APPL_RSX
CP/M-80 Plus .rsx resident system extension file (unsupported)
Definition 8bitexe.h:381
@ APPL_RSP
MP/M-80 .rsp resident system process file (unsupported)
Definition 8bitexe.h:385
@ APPL_RSM
CP/M-80 2 .rsm resident system extension file (unsupported)
Definition 8bitexe.h:383
@ APPL_BRS
MP/M-80 .brs banked resident system process file (unsupported)
Definition 8bitexe.h:387
@ APPL_UNKNOWN
Unspecified.
Definition 8bitexe.h:377
@ APPL_PRL
MP/M-80 .prl executable file.
Definition 8bitexe.h:379
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:739
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition 8bitexe.cc:635
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:700
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition 8bitexe.cc:532
uint16_t zero_fill
Additional memory to allocate, similar to .bss.
Definition 8bitexe.h:353
bool option_banked_bios
On a banked BIOS, align the data segment of the .SPR on a page boundary and store the length of the c...
Definition 8bitexe.h:363
application_type application
Target application type.
Definition 8bitexe.h:394
UZI280 file format.
Definition 8bitexe.h:456
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:812
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:844
std::string GetDefaultExtension(Linker::Module &module) const override
Provides a default filename for the output file.
Definition 8bitexe.cc:855
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:830
UZI/UZI280 file formats.
Definition 8bitexe.h:436
std::string GetDefaultExtension(Linker::Module &module) const override
Provides a default filename for the output file.
Definition 8bitexe.cc:805
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition 8bitexe.cc:781
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition 8bitexe.cc:794
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition 8bitexe.cc:775
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 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:58
uint16_t address
Address at which segment must be loaded.
Definition 8bitexe.h:93
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:97
char symbol_name[8]
Only used for SDX_SYMREQ, SDX_SYMDEF.
Definition 8bitexe.h:122
std::set< uint16_t > relocations
Relocations, only used for SDX_SYMREQ and SDX_FIXUPS.
Definition 8bitexe.h:130
void ReadRelocations(Linker::Reader &rd)
Read relocations.
Definition 8bitexe.cc:174
uint16_t size
Only used for SDX_RAMALLOC/SDX_POSIND.
Definition 8bitexe.h:118
bool header_type_optional
Set if placing header type is optional, also set when signature is absent in file when reading.
Definition 8bitexe.h:89
std::shared_ptr< Linker::Image > image
The binary data in the segment.
Definition 8bitexe.h:126
control_byte_type control_byte
Only used for SDX_RAMALLOC/SDX_POSIND.
Definition 8bitexe.h:114
control_byte_type
Definition 8bitexe.h:99
@ CB_RAMALLOC
SDX_RAMALLOC instead of SDX_POSIND.
Definition 8bitexe.h:109
@ CB_CONVRAM
allocate in conventional RAM
Definition 8bitexe.h:101
@ CB_PROGEXTAREA
(SDX 4.47+) allocate in program extended area
Definition 8bitexe.h:105
@ CB_SYSEXTAREA
allocate in system extended area
Definition 8bitexe.h:103
@ CB_PAGEALIGNED
(SDX 4.43+) page aligned
Definition 8bitexe.h:107
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:64
@ ATARI_SEGMENT
Atari segment type.
Definition 8bitexe.h:80
@ SDX_FIXUPS
SpartaDOS X fixup information (not implemented)
Definition 8bitexe.h:74
@ SDX_SYMREQ
SpartaDOS X required symbols (not implemented)
Definition 8bitexe.h:70
@ SDX_FIXED
SpartaDOS X fixed-address segment (not implemented)
Definition 8bitexe.h:68
@ SDX_RAMALLOC
SpartaDOS X RAM allocation block (not implemented)
Definition 8bitexe.h:76
@ SDX_POSIND
SpartaDOS X position independent (not implemented)
Definition 8bitexe.h:78
@ SIGNATURE_LOW
Lowest currently defined signature to check on reading.
Definition 8bitexe.h:66
@ SDX_SYMDEF
SpartaDOS X symbol definitions (not implemented)
Definition 8bitexe.h:72
segment_type header_type
Header type, Atari DOS uses only 0xFFFF, signature only obligatory for the first segment.
Definition 8bitexe.h:85
A single RSX record.
Definition 8bitexe.h:260
std::string name
Name of RSX file, as stored inside RSX.
Definition 8bitexe.h:264
uint16_t offset
Offset to RSX block.
Definition 8bitexe.h:266
std::string rsx_file_name
Name of RSX file to load, only used for writing.
Definition 8bitexe.h:262
uint16_t length
Length of RSX module, only used for reading.
Definition 8bitexe.h:268
bool nonbanked_only
Whether RSX is only loaded on non-banked systems.
Definition 8bitexe.h:270
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:314