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 "../dumper/dumper.h"
6#include "../linker/image.h"
7#include "../linker/reader.h"
8#include "../linker/segment_manager.h"
9#include "../linker/writer.h"
10
11/* o65 object format (input only) */
12namespace O65
13{
19 class O65Format : public virtual Linker::InputFormat
20 {
21 public:
23 class Module
24 {
25 public:
28 {
30 static const int TYPE_FILENAME = 0x00;
32 static const int TYPE_SYSTEM = 0x01;
34 static const int TYPE_ASSEMBLER = 0x02;
36 static const int TYPE_AUTHOR = 0x03;
38 static const int TYPE_CREATION = 0x04;
39
41 uint8_t type;
43 std::vector<uint8_t> data;
44
45 header_option(uint8_t type)
46 : type(type), data()
47 {
48 }
49
50 header_option(uint8_t type, std::vector<uint8_t>& data)
51 : type(type), data(data)
52 {
53 }
54 };
55
58 {
60 static const int RELOC_TYPE_MASK = 0xE0;
62 static const int RELOC_TYPE_LOW = 0x20;
64 static const int RELOC_TYPE_HIGH = 0x40;
66 static const int RELOC_TYPE_WORD = 0x80;
68 static const int RELOC_TYPE_SEG = 0xA0;
70 static const int RELOC_TYPE_SEGADDR = 0xC0;
71
73 static const int RELOC_SEGMENT_MASK = 0x1F;
75 static const int RELOC_SEGMENT_UNDEFINED = 0x00;
77 static const int RELOC_SEGMENT_ABSOLUTE = 0x01;
79 static const int RELOC_SEGMENT_TEXT = 0x02;
81 static const int RELOC_SEGMENT_DATA = 0x03;
83 static const int RELOC_SEGMENT_BSS = 0x04;
85 static const int RELOC_SEGMENT_ZERO = 0x05;
86
88 uint8_t type_segment;
90 offset_t symbol_index;
92 offset_t value;
93
96 {
97 }
98
99 relocation(uint8_t type_segment, offset_t value = 0, offset_t symbol_index = 0)
101 {
102 }
103 };
104
106 {
108 std::string name;
110 uint8_t segment_id;
112 offset_t value;
113
114 exported_global(std::string_view name, uint8_t segment_id, offset_t value)
116 {
117 }
118 };
119
121 static const int MODE_ALIGN_MASK = 0x0003;
123 static const int MODE_ALIGN_BYTE = 0x0000;
125 static const int MODE_ALIGN_WORD = 0x0001;
127 static const int MODE_ALIGN_LONG = 0x0002;
129 static const int MODE_ALIGN_PAGE = 0x0003;
130
132 static const int MODE_CPU_MASK = 0x00F0;
133 static const int MODE_CPU_6502 = 0x0000;
134 static const int MODE_CPU_65C02 = 0x0010;
135 static const int MODE_CPU_65SC02 = 0x0020;
136 static const int MODE_CPU_65CE02 = 0x0030;
137 static const int MODE_CPU_NMOS = 0x0040;
138 static const int MODE_CPU_65816 = 0x0050;
139
141 static const int MODE_BSS_ZERO = 0x0200;
143 static const int MODE_CHAIN = 0x0400;
145 static const int MODE_SIMPLE = 0x0800;
147 static const int MODE_OBJ = 0x1000;
149 static const int MODE_SIZE = 0x2000;
151 static const int MODE_PAGE_RELOC = 0x4000;
153 static const int MODE_65816 = 0x8000;
154
156 uint16_t mode_word = 0;
157
159 offset_t code_base = 0;
161 std::shared_ptr<Linker::Image> code_image = nullptr;
163 offset_t data_base = 0;
165 std::shared_ptr<Linker::Image> data_image = nullptr;
167 offset_t bss_base = 0;
169 offset_t bss_size = 0;
171 offset_t zero_base = 0;
173 offset_t zero_size = 0;
175 offset_t stack_size = 0;
176
178 std::vector<header_option> header_options;
180 std::vector<std::string> undefined_references;
181
183 std::map<offset_t, relocation> code_relocations;
185 std::map<offset_t, relocation> data_relocations;
186
188 std::vector<exported_global> exported_globals;
189
190 ~Module()
191 {
192 Clear();
193 }
194
195 void Clear();
196
198 bool IsPageRelocatable() const;
200 bool IsChained() const;
201 private:
203 void SetChained();
204 friend class O65Format;
205 public:
207 int GetWordSize() const;
209 offset_t ReadUnsigned(Linker::Reader& rd) const;
211 void WriteWord(Linker::Writer& wr, offset_t value) const;
212
213 void ReadFile(Linker::Reader& rd);
214 void CalculateValues();
215 void WriteFile(Linker::Writer& wr) const;
216 void GenerateModule(Linker::Module& module) const;
217 };
218
219 protected:
220 std::vector<std::unique_ptr<Module>> modules;
221
222 public:
223 offset_t GetModuleCount();
224 std::unique_ptr<O65Format::Module>& GetModule(offset_t index);
225 std::unique_ptr<O65Format::Module>& AddModule();
226
227 ~O65Format()
228 {
229 Clear();
230 }
231
232 void Clear() override;
233
234 void ReadFile(Linker::Reader& rd) override;
236 void CalculateValues() /*override*/;
237 offset_t WriteFile(Linker::Writer& wr) const override;
238 void Dump(Dumper::Dumper& dump) const override;
240 void GenerateModule(Linker::Module& module) const override;
241 };
242}
243
244#endif /* O65_H */
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.
A class that provides a general interface to loading a module.
Definition format.h:166
virtual void GenerateModule(ModuleCollector &linker, std::string file_name, bool is_library=false) const
Loads the information into a module object.
Definition format.cc:180
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:24
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:24
static const int MODE_OBJ
Set for non-executable object modules.
Definition o65.h:147
static const int MODE_ALIGN_BYTE
File alignment is byte.
Definition o65.h:123
static const int MODE_ALIGN_LONG
File alignment is 4 byte long word.
Definition o65.h:127
std::vector< header_option > header_options
Additional header field entries.
Definition o65.h:178
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:58
static const int MODE_CHAIN
Set if another module follows.
Definition o65.h:143
offset_t zero_base
Base address of zero segment.
Definition o65.h:171
static const int MODE_CPU_MASK
Bits 4 to 7 in mode_word describing CPU instruction set.
Definition o65.h:132
std::shared_ptr< Linker::Image > data_image
Data segment contents.
Definition o65.h:165
std::map< offset_t, relocation > code_relocations
Relocations within code segment.
Definition o65.h:183
static const int MODE_SIMPLE
Set if code, data and bss are contiguous.
Definition o65.h:145
std::shared_ptr< Linker::Image > code_image
Code segment contents.
Definition o65.h:161
uint16_t mode_word
The file mode.
Definition o65.h:156
static const int MODE_65816
Set for 65816 mode.
Definition o65.h:153
static const int MODE_BSS_ZERO
Set if bss should be cleared on load.
Definition o65.h:141
offset_t ReadUnsigned(Linker::Reader &rd) const
Reads a module dependent unsigned word, the size of which is given by GetWordSize()
Definition o65.cc:53
std::vector< std::string > undefined_references
Undefined references appearing in file.
Definition o65.h:180
static const int MODE_SIZE
Set if address/offset fields are 32-bit wide instead of 16-bit.
Definition o65.h:149
offset_t zero_size
Zero segment size.
Definition o65.h:173
int GetWordSize() const
Return the size of address/offset values in the file, in bytes (2 or 4)
Definition o65.cc:48
static const int MODE_PAGE_RELOC
Set if file is page relocatable (256 bytes) instead of byte relocatable.
Definition o65.h:151
std::map< offset_t, relocation > data_relocations
Relocations within data segment.
Definition o65.h:185
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:33
bool IsChained() const
Whether another module follows this one (MODE_CHAIN is set in mode_word.
Definition o65.cc:38
offset_t code_base
Base address of code segment.
Definition o65.h:159
static const int MODE_ALIGN_WORD
File alignment is 2 byte word.
Definition o65.h:125
offset_t bss_size
Bss segment size.
Definition o65.h:169
static const int MODE_ALIGN_PAGE
File alignment is 256 byte page/block.
Definition o65.h:129
offset_t data_base
Base address of data segment.
Definition o65.h:163
offset_t bss_base
Base address of bss segment.
Definition o65.h:167
offset_t stack_size
Stack segment size.
Definition o65.h:175
std::vector< exported_global > exported_globals
Exported global symbols.
Definition o65.h:188
static const int MODE_ALIGN_MASK
Bits 0 to 1 in mode_word describing the file alignment.
Definition o65.h:121
Output format for the 6502 assembler xa.
Definition o65.h:20
void GenerateModule(Linker::Module &module) const override
Loads the information into a module object, a convenience method when there is a single module genera...
Definition o65.cc:423
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition o65.cc:380
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition o65.cc:412
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition o65.cc:402
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition o65.cc:385
offset_t value
Symbol value.
Definition o65.h:112
uint8_t segment_id
Segment identifier.
Definition o65.h:110
std::string name
Name of the exported symbol.
Definition o65.h:108
Additional entries in the header.
Definition o65.h:28
static const int TYPE_SYSTEM
OS type.
Definition o65.h:32
static const int TYPE_AUTHOR
Name of author.
Definition o65.h:36
static const int TYPE_CREATION
Creation date in a human readable format.
Definition o65.h:38
uint8_t type
Type of header entry.
Definition o65.h:41
static const int TYPE_FILENAME
Filename of object file.
Definition o65.h:30
static const int TYPE_ASSEMBLER
Name of assembler that created this file.
Definition o65.h:34
std::vector< uint8_t > data
Header entry contents.
Definition o65.h:43
Relocation entries.
Definition o65.h:58
static const int RELOC_SEGMENT_ABSOLUTE
Reference is a global value.
Definition o65.h:77
static const int RELOC_SEGMENT_BSS
Reference belongs to the bss segment.
Definition o65.h:83
offset_t symbol_index
(optional) For undefined references (RELOC_SEGMENT_UNDEFINED), the index of the external symbol in th...
Definition o65.h:90
static const int RELOC_SEGMENT_UNDEFINED
Reference does not belong to a segment, instead it is an external reference.
Definition o65.h:75
static const int RELOC_SEGMENT_ZERO
Reference belongs to the zero segment (6502/65c816 specific)
Definition o65.h:85
static const int RELOC_SEGMENT_MASK
The bitmask in the relocation type corresponding to the segment number.
Definition o65.h:73
static const int RELOC_SEGMENT_DATA
Reference belongs to the data segment.
Definition o65.h:81
static const int RELOC_TYPE_WORD
16-bit value
Definition o65.h:66
static const int RELOC_TYPE_SEGADDR
24-bit address
Definition o65.h:70
static const int RELOC_TYPE_MASK
The bitmask in the relocation type corresponding to the relocation type.
Definition o65.h:60
offset_t value
(optional) The unrelocated parts of the reference
Definition o65.h:92
static const int RELOC_TYPE_SEG
High 8 bits (bits 16 to 23) of a far address.
Definition o65.h:68
static const int RELOC_TYPE_LOW
Low 8 bits of a value.
Definition o65.h:62
static const int RELOC_SEGMENT_TEXT
Reference belongs to the text segment.
Definition o65.h:79
uint8_t type_segment
The type (bits 4 to 7) and segment (bits 0 to 3) of the relocation.
Definition o65.h:88
static const int RELOC_TYPE_HIGH
High 8 bits (bits 8 to 15) of a value.
Definition o65.h:64