RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
o65.h
1#ifndef O65_H
2#define O65_H
3
4#include "../common.h"
5#include "../linker/linker.h"
6#include "../linker/reader.h"
7#include "../linker/writable.h"
8#include "../linker/writer.h"
9
10/* o65 object format (input only) */
11namespace O65
12{
19 {
20 public:
22 class Module
23 {
24 public:
27 {
29 static const int TYPE_FILENAME = 0x00;
31 static const int TYPE_SYSTEM = 0x01;
33 static const int TYPE_ASSEMBLER = 0x02;
35 static const int TYPE_AUTHOR = 0x03;
37 static const int TYPE_CREATION = 0x04;
38
40 uint8_t type;
42 std::vector<uint8_t> data;
43
44 header_option(uint8_t type)
45 : type(type), data()
46 {
47 }
48
49 header_option(uint8_t type, std::vector<uint8_t>& data)
50 : type(type), data(data)
51 {
52 }
53 };
54
57 {
59 static const int RELOC_TYPE_MASK = 0xE0;
61 static const int RELOC_TYPE_LOW = 0x20;
63 static const int RELOC_TYPE_HIGH = 0x40;
65 static const int RELOC_TYPE_WORD = 0x80;
67 static const int RELOC_TYPE_SEG = 0xA0;
69 static const int RELOC_TYPE_SEGADDR = 0xC0;
70
72 static const int RELOC_SEGMENT_MASK = 0x1F;
74 static const int RELOC_SEGMENT_UNDEFINED = 0x00;
76 static const int RELOC_SEGMENT_ABSOLUTE = 0x01;
78 static const int RELOC_SEGMENT_TEXT = 0x02;
80 static const int RELOC_SEGMENT_DATA = 0x03;
82 static const int RELOC_SEGMENT_BSS = 0x04;
84 static const int RELOC_SEGMENT_ZERO = 0x05;
85
87 uint8_t type_segment;
89 offset_t symbol_index;
91 offset_t value;
92
95 {
96 }
97
98 relocation(uint8_t type_segment, offset_t value = 0, offset_t symbol_index = 0)
100 {
101 }
102 };
103
105 {
107 std::string name;
109 uint8_t segment_id;
111 offset_t value;
112
113 exported_global(std::string_view name, uint8_t segment_id, offset_t value)
115 {
116 }
117 };
118
120 static const int MODE_ALIGN_MASK = 0x0003;
122 static const int MODE_ALIGN_BYTE = 0x0000;
124 static const int MODE_ALIGN_WORD = 0x0001;
126 static const int MODE_ALIGN_LONG = 0x0002;
128 static const int MODE_ALIGN_PAGE = 0x0003;
129
131 static const int MODE_CPU_MASK = 0x00F0;
132 static const int MODE_CPU_6502 = 0x0000;
133 static const int MODE_CPU_65C02 = 0x0010;
134 static const int MODE_CPU_65SC02 = 0x0020;
135 static const int MODE_CPU_65CE02 = 0x0030;
136 static const int MODE_CPU_NMOS = 0x0040;
137 static const int MODE_CPU_65816 = 0x0050;
138
140 static const int MODE_BSS_ZERO = 0x0200;
142 static const int MODE_CHAIN = 0x0400;
144 static const int MODE_SIMPLE = 0x0800;
146 static const int MODE_OBJ = 0x1000;
148 static const int MODE_SIZE = 0x2000;
150 static const int MODE_PAGE_RELOC = 0x4000;
152 static const int MODE_65816 = 0x8000;
153
155 uint16_t mode_word = 0;
156
158 offset_t code_base = 0;
160 std::shared_ptr<Linker::Writable> code_image = nullptr;
162 offset_t data_base = 0;
164 std::shared_ptr<Linker::Writable> data_image = nullptr;
166 offset_t bss_base = 0;
168 offset_t bss_size = 0;
170 offset_t zero_base = 0;
172 offset_t zero_size = 0;
174 offset_t stack_size = 0;
175
177 std::vector<header_option> header_options;
179 std::vector<std::string> undefined_references;
180
182 std::map<offset_t, relocation> code_relocations;
184 std::map<offset_t, relocation> data_relocations;
185
187 std::vector<exported_global> exported_globals;
188
189 ~Module()
190 {
191 Clear();
192 }
193
194 void Clear();
195
197 bool IsPageRelocatable() const;
199 bool IsChained() const;
200 private:
202 void SetChained();
203 friend class O65Format;
204 public:
206 int GetWordSize() const;
208 offset_t ReadUnsigned(Linker::Reader& rd) const;
210 void WriteWord(Linker::Writer& wr, offset_t value) const;
211
212 void ReadFile(Linker::Reader& rd);
213 void WriteFile(Linker::Writer& wr);
214 void GenerateModule(Linker::Module& module, Linker::Reader& rd) const;
215 };
216
217 protected:
218 std::vector<std::unique_ptr<Module>> modules;
219
220 public:
221 offset_t GetModuleCount();
222 std::unique_ptr<O65Format::Module>& GetModule(offset_t index);
223 std::unique_ptr<O65Format::Module>& AddModule();
224
225 ~O65Format()
226 {
227 Clear();
228 }
229
230 void Clear() override;
231
232 void ReadFile(Linker::Reader& rd) override;
233 void WriteFile(Linker::Writer& wr) override;
234 void ProduceModule(Linker::Module& module, Linker::Reader& rd) override;
235 };
236}
237
238#endif /* O65_H */
A class that provides a general interface to loading a module.
Definition format.h:161
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 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
A object file typically contains a single module.
Definition o65.h:23
static const int MODE_OBJ
Set for non-executable object modules.
Definition o65.h:146
static const int MODE_ALIGN_BYTE
File alignment is byte.
Definition o65.h:122
static const int MODE_ALIGN_LONG
File alignment is 4 byte long word.
Definition o65.h:126
std::vector< header_option > header_options
Additional header field entries.
Definition o65.h:177
void WriteWord(Linker::Writer &wr, offset_t value) const
Writes a module dependent word, the size of which is given by GetWordSize()
Definition o65.cc:56
static const int MODE_CHAIN
Set if another module follows.
Definition o65.h:142
offset_t zero_base
Base address of zero segment.
Definition o65.h:170
static const int MODE_CPU_MASK
Bits 4 to 7 in mode_word describing CPU instruction set.
Definition o65.h:131
std::map< offset_t, relocation > code_relocations
Relocations within code segment.
Definition o65.h:182
std::shared_ptr< Linker::Writable > code_image
Code segment contents.
Definition o65.h:160
static const int MODE_SIMPLE
Set if code, data and bss are contiguous.
Definition o65.h:144
uint16_t mode_word
The file mode.
Definition o65.h:155
static const int MODE_65816
Set for 65816 mode.
Definition o65.h:152
static const int MODE_BSS_ZERO
Set if bss should be cleared on load.
Definition o65.h:140
offset_t ReadUnsigned(Linker::Reader &rd) const
Reads a module dependent unsigned word, the size of which is given by GetWordSize()
Definition o65.cc:51
std::vector< std::string > undefined_references
Undefined references appearing in file.
Definition o65.h:179
static const int MODE_SIZE
Set if address/offset fields are 32-bit wide instead of 16-bit.
Definition o65.h:148
offset_t zero_size
Zero segment size.
Definition o65.h:172
int GetWordSize() const
Return the size of address/offset values in the file, in bytes (2 or 4)
Definition o65.cc:46
static const int MODE_PAGE_RELOC
Set if file is page relocatable (256 bytes) instead of byte relocatable.
Definition o65.h:150
std::map< offset_t, relocation > data_relocations
Relocations within data segment.
Definition o65.h:184
bool IsPageRelocatable() const
Whether file can only be relocated according to a 256 byte page (MODE_PAGE_RELOC is set in mode_word)
Definition o65.cc:31
bool IsChained() const
Whether another module follows this one (MODE_CHAIN is set in mode_word.
Definition o65.cc:36
offset_t code_base
Base address of code segment.
Definition o65.h:158
static const int MODE_ALIGN_WORD
File alignment is 2 byte word.
Definition o65.h:124
std::shared_ptr< Linker::Writable > data_image
Data segment contents.
Definition o65.h:164
offset_t bss_size
Bss segment size.
Definition o65.h:168
static const int MODE_ALIGN_PAGE
File alignment is 256 byte page/block.
Definition o65.h:128
offset_t data_base
Base address of data segment.
Definition o65.h:162
offset_t bss_base
Base address of bss segment.
Definition o65.h:166
offset_t stack_size
Stack segment size.
Definition o65.h:174
std::vector< exported_global > exported_globals
Exported global symbols.
Definition o65.h:187
static const int MODE_ALIGN_MASK
Bits 0 to 1 in mode_word describing the file alignment.
Definition o65.h:120
Output format for the 6502 assembler xa.
Definition o65.h:19
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition o65.cc:374
void WriteFile(Linker::Writer &wr) override
Stores data in memory to file.
Definition o65.cc:388
void ProduceModule(Linker::Module &module, Linker::Reader &rd) override
Reads a file and loads the information into a module object.
Definition o65.cc:396
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition o65.cc:379
offset_t value
Symbol value.
Definition o65.h:111
uint8_t segment_id
Segment identifier.
Definition o65.h:109
std::string name
Name of the exported symbol.
Definition o65.h:107
Additional entries in the header.
Definition o65.h:27
static const int TYPE_SYSTEM
OS type.
Definition o65.h:31
static const int TYPE_AUTHOR
Name of author.
Definition o65.h:35
static const int TYPE_CREATION
Creation date in a human readable format.
Definition o65.h:37
uint8_t type
Type of header entry.
Definition o65.h:40
static const int TYPE_FILENAME
Filename of object file.
Definition o65.h:29
static const int TYPE_ASSEMBLER
Name of assembler that created this file.
Definition o65.h:33
std::vector< uint8_t > data
Header entry contents.
Definition o65.h:42
Relocation entries.
Definition o65.h:57
static const int RELOC_SEGMENT_ABSOLUTE
Reference is a global value.
Definition o65.h:76
static const int RELOC_SEGMENT_BSS
Reference belongs to the bss segment.
Definition o65.h:82
offset_t symbol_index
(optional) For undefined references (RELOC_SEGMENT_UNDEFINED), the index of the external symbol in th...
Definition o65.h:89
static const int RELOC_SEGMENT_UNDEFINED
Reference does not belong to a segment, instead it is an external reference.
Definition o65.h:74
static const int RELOC_SEGMENT_ZERO
Reference belongs to the zero segment (6502/65c816 specific)
Definition o65.h:84
static const int RELOC_SEGMENT_MASK
The bitmask in the relocation type corresponding to the segment number.
Definition o65.h:72
static const int RELOC_SEGMENT_DATA
Reference belongs to the data segment.
Definition o65.h:80
static const int RELOC_TYPE_WORD
16-bit value
Definition o65.h:65
static const int RELOC_TYPE_SEGADDR
24-bit address
Definition o65.h:69
static const int RELOC_TYPE_MASK
The bitmask in the relocation type corresponding to the relocation type.
Definition o65.h:59
offset_t value
(optional) The unrelocated parts of the reference
Definition o65.h:91
static const int RELOC_TYPE_SEG
High 8 bits (bits 16 to 23) of a far address.
Definition o65.h:67
static const int RELOC_TYPE_LOW
Low 8 bits of a value.
Definition o65.h:61
static const int RELOC_SEGMENT_TEXT
Reference belongs to the text segment.
Definition o65.h:78
uint8_t type_segment
The type (bits 4 to 7) and segment (bits 0 to 3) of the relocation.
Definition o65.h:87
static const int RELOC_TYPE_HIGH
High 8 bits (bits 8 to 15) of a value.
Definition o65.h:63