RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
cpm8k.h
1#ifndef CPM8K_H
2#define CPM8K_H
3
4#include <map>
5#include <set>
6#include <string>
7#include "../common.h"
8#include "../dumper/dumper.h"
9#include "../linker/module.h"
10#include "../linker/segment.h"
11#include "../linker/segment_manager.h"
12#include "../linker/writer.h"
13
14namespace DigitalResearch
15{
19 class CPM8KFormat : public virtual Linker::SegmentManager
20 {
21 public:
22 /* * * General members * * */
23
27 class Segment
28 {
29 public:
33 uint8_t number = 0xFF;
38 {
39 BSS = 1,
40 STACK,
41 CODE,
42 RODATA,
43 DATA,
44 MIXED,
45 MIXED_PROTECTABLE,
46 };
54 uint16_t length = 0;
58 std::shared_ptr<Linker::Contents> image = nullptr;
59
60 void Clear();
61
62 bool IsPresent() const;
63 };
64
66 {
70 uint8_t segment;
86 relocation_type type;
90 uint16_t offset;
94 uint16_t target;
95
96 constexpr bool IsExternal() const
97 {
98 return type == EXT_OFFSET || type == EXT_SHORT_SEGMENTED || type == EXT_LONG_SEGMENTED;
99 }
100
101 constexpr size_t GetRelocationSize() const
102 {
103 switch(type)
104 {
105 case SEG_OFFSET:
107 case EXT_OFFSET:
109 return 2;
112 return 4;
113 default:
114 return 0;
115 }
116 }
117
118 static Relocation ReadFile(Linker::Reader& rd);
119 void WriteFile(Linker::Writer& wr) const;
120 };
121
122 struct Symbol
123 {
124 uint8_t segment_number;
125 static constexpr uint8_t ABSOLUTE = 0xFF;
126 enum symbol_type
127 {
128 LOCAL = 1,
129 EXTERNAL = 2,
130 GLOBAL = 3,
131 SEGMENT = 4,
132 };
133 symbol_type type;
134 uint16_t value;
135 std::string name;
136
137 static Symbol ReadFile(Linker::Reader& rd);
138 void WriteFile(Linker::Writer& wr) const;
139 };
140
141 enum magic_type
142 {
143 MAGIC_SEGMENTED_OBJECT = 0xEE00,
144 MAGIC_SEGMENTED = 0xEE01,
145 MAGIC_NONSHARED_OBJECT = 0xEE02,
146 MAGIC_NONSHARED = 0xEE03,
147 MAGIC_SHARED_OBJECT = 0xEE06,
148 MAGIC_SHARED = 0xEE07,
149 MAGIC_SPLIT_OBJECT = 0xEE0A,
150 MAGIC_SPLIT = 0xEE0B,
151 };
152
154 char signature[2];
156 uint16_t segment_count = 0;
158 uint32_t total_size = 0;
160 uint32_t relocation_size = 0;
162 uint32_t symbol_table_size = 0;
163
164 std::vector<Segment> segments;
165 std::vector<Relocation> relocations;
166 std::vector<Symbol> symbols;
167
168 magic_type GetSignature() const;
169
170 void SetSignature(magic_type magic);
171
172 void Clear() override;
173
174 CPM8KFormat(magic_type magic = MAGIC_NONSHARED)
175 {
176 SetSignature(magic);
177 }
178
179 void ReadFile(Linker::Reader& rd) override;
180
181 offset_t ImageSize() const override;
182
184 offset_t WriteFile(Linker::Writer& wr) const override;
185
186 void Dump(Dumper::Dumper& dump) const override;
187
188 void CalculateValues() override;
189
190 /* * * Writer members * * */
191
193 std::shared_ptr<Linker::Segment> bss_segment;
194
195 bool FormatSupportsSegmentation() const override;
196
197 std::vector<std::shared_ptr<Linker::Segment>>& Segments();
198
199 unsigned GetSegmentNumber(std::shared_ptr<Linker::Segment> segment);
200
201 void SetOptions(std::map<std::string, std::string>& options) override;
202
203 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
204
205 bool IsCombined() const;
206
207 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
208
209 void Link(Linker::Module& module);
210
211 void ProcessModule(Linker::Module& module) override;
212
213 void GenerateFile(std::string filename, Linker::Module& module) override;
214
216 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
217 };
218}
219
220#endif /* CPM8K_H */
Represents a segment within the module.
Definition cpm8k.h:28
segment_type
The type of a segment.
Definition cpm8k.h:38
std::shared_ptr< Linker::Contents > image
Storage for segment.
Definition cpm8k.h:58
segment_type type
The type of the segment.
Definition cpm8k.h:50
uint8_t number
Each segment has an associated number. For 0xFF, the linker can assign a value. For segmented executa...
Definition cpm8k.h:33
uint16_t length
Length of segment in bytes.
Definition cpm8k.h:54
CP/M-8000 .z8k file format.
Definition cpm8k.h:20
char signature[2]
The magic number at the beginning of the executable file.
Definition cpm8k.h:154
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition cpm8k.cc:355
uint32_t total_size
Total number of bytes in all the segments combined.
Definition cpm8k.h:158
uint16_t segment_count
Number of segments in the segment_array.
Definition cpm8k.h:156
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition cpm8k.cc:655
uint32_t relocation_size
Total size of relocations.
Definition cpm8k.h:160
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition cpm8k.cc:199
uint32_t symbol_table_size
Total size of symbols.
Definition cpm8k.h:162
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition cpm8k.cc:101
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition cpm8k.cc:91
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition cpm8k.cc:160
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition cpm8k.cc:645
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition cpm8k.cc:337
std::shared_ptr< Linker::Segment > bss_segment
Segment to collect bss.
Definition cpm8k.h:193
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition cpm8k.cc:376
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition cpm8k.cc:587
offset_t ImageSize() const override
Retrieves size of stored data.
Definition cpm8k.cc:142
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:773
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:20
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
@ SEG_OFFSET
A 16-bit offset to segment.
Definition cpm8k.h:74
@ SEG_LONG_SEGMENTED
A 32-bit segmented address of segment.
Definition cpm8k.h:78
@ SEG_SHORT_SEGMENTED
A 16-bit segmented address of segment.
Definition cpm8k.h:76
@ EXT_LONG_SEGMENTED
A 32-bit segmented address of external item.
Definition cpm8k.h:84
@ EXT_SHORT_SEGMENTED
A 16-bit segmented address of external item.
Definition cpm8k.h:82
@ EXT_OFFSET
A 16-bit offset to external item.
Definition cpm8k.h:80
uint8_t segment
The source segment of the relocation.
Definition cpm8k.h:70
uint16_t offset
Source offset of relocation.
Definition cpm8k.h:90
uint16_t target
The segment or symbol number that the relocation references.
Definition cpm8k.h:94