RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
cpm68k.h
1#ifndef CPM68K_H
2#define CPM68K_H
3
4#include <map>
5#include <set>
6#include <string>
7#include "../common.h"
8#include "../linker/module.h"
9#include "../linker/segment.h"
10#include "../linker/segment_manager.h"
11#include "../linker/writer.h"
12#include "../dumper/dumper.h"
13
14namespace DigitalResearch
15{
26 class CPM68KFormat : public virtual Linker::SegmentManager
27 {
28 public:
29 /* * * General members * * */
30
35 {
47 MAGIC_CRUNCHED, /* Concurrent DOS 68K only */
48 };
49
51 char signature[2] = { 0x60, 0x1A };
52
56 uint32_t code_size = 0;
60 uint32_t data_size = 0;
64 uint32_t bss_size = 0;
68 uint32_t symbol_table_size = 0;
72 uint32_t stack_size = 0;
73
77 uint32_t code_address = 0;
81 uint32_t program_flags = 0; /* TODO: make parameter */
82
90 uint32_t data_address = 0;
94 uint32_t bss_address = 0;
98 offset_t file_size = 0;
99
103 std::shared_ptr<Linker::Image> code = nullptr;
107 std::shared_ptr<Linker::Image> data = nullptr;
108
109 /* filled in automatically */
111 {
115 size_t size;
119 unsigned segment;
120 operator size_t() const;
121 };
125 std::map<uint32_t, Relocation> relocations;
126
127 magic_type GetSignature() const;
128
129 void SetSignature(magic_type magic);
130
161
166
167 void Clear() override;
168
170 : system(system)
171 {
172 }
173
175 : system(system)
176 {
177 SetSignature(magic);
178 }
179
180 void ReadFile(Linker::Reader& rd) override;
181
182 template <typename SizeType>
183 static offset_t CDOS68K_MeasureRelocations(std::map<uint32_t, SizeType> relocations)
184 {
185 /* TODO: test */
186 offset_t count = 0;
187 offset_t last_relocation = 0;
188 for(auto it : relocations)
189 {
190 offset_t difference = it.first - last_relocation;
191 if(difference != 0 && difference <= 0x7C)
192 {
193 count += 1;
194 }
195 else if(difference < 0x100)
196 {
197 count += 2;
198 }
199 else if(difference < 0x10000)
200 {
201 count += 3;
202 }
203 else
204 {
205 count += 5;
206 }
207 }
208 return count;
209 }
210
211 template <typename SizeType>
212 static void CDOS68K_WriteRelocations(Linker::Writer& wr, std::map<uint32_t, SizeType> relocations)
213 {
214 /* TODO: test */
215 offset_t last_relocation = 0;
216 for(auto it : relocations)
217 {
218 offset_t difference = it.first - last_relocation;
219 uint8_t highbit = it.second/*.size*/ == 2 ? 0x80 : 0x00;
220 if(difference != 0 && difference <= 0x7C)
221 {
222 wr.WriteWord(1, highbit | difference);
223 }
224 else if(difference < 0x100)
225 {
226 wr.WriteWord(1, highbit | 0x7D);
227 wr.WriteWord(1, difference);
228 }
229 else if(difference < 0x10000)
230 {
231 wr.WriteWord(1, highbit | 0x7E);
232 wr.WriteWord(2, difference);
233 }
234 else
235 {
236 wr.WriteWord(1, highbit | 0x7F);
237 wr.WriteWord(4, difference);
238 }
239 }
240 }
241
242 offset_t MeasureRelocations() const;
243
244 offset_t ImageSize() const override;
245
247 offset_t WriteFile(Linker::Writer& wr) const override;
248
249 void Dump(Dumper::Dumper& dump) const override;
250
251 void CalculateValues() override;
252
253 /* * * Writer members * * */
254
259
261 std::shared_ptr<Linker::Segment> bss_segment;
263 std::shared_ptr<Linker::Segment> stack_segment;
264
266 std::shared_ptr<Linker::Segment> CodeSegment();
267
269 std::shared_ptr<Linker::Segment> DataSegment();
270
271 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
272
273 void SetOptions(std::map<std::string, std::string>& options) override;
274
275 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
276
277 void CreateDefaultSegments();
278
279 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
280
281 void Link(Linker::Module& module);
282
283 void ProcessModule(Linker::Module& module) override;
284
285 void GenerateFile(std::string filename, Linker::Module& module) override;
286
288 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
289 };
290
291}
292
293#endif /* CPM68K_H */
The native executable format for the Motorola 68000 port of CP/M.
Definition cpm68k.h:27
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition cpm68k.cc:945
offset_t file_size
Size of entire file, not used for generation.
Definition cpm68k.h:98
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 cpm68k.cc:681
uint32_t stack_size
Size of the stack segment. Only used by Concurrent DOS 68K.
Definition cpm68k.h:72
uint32_t code_address
Load address of the code/text segment. Not used by GEMDOS which stores the program flags at this offs...
Definition cpm68k.h:77
std::shared_ptr< Linker::Segment > bss_segment
Segment to collect bss.
Definition cpm68k.h:261
uint32_t bss_size
Size of the uninitialized data (bss) segment. Human68k includes the stack in it.
Definition cpm68k.h:64
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition cpm68k.cc:967
std::shared_ptr< Linker::Segment > CodeSegment()
Return code segment (if it exists)
Definition cpm68k.cc:591
uint32_t program_flags
Program flags, used by GEMDOS.
Definition cpm68k.h:81
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition cpm68k.cc:355
uint32_t bss_address
Load address of the uninitialized data (bss) segment. Only relevant for non-contiguous executables (C...
Definition cpm68k.h:94
std::map< uint32_t, Relocation > relocations
Relocations, not used for Human68k.
Definition cpm68k.h:125
bool option_no_relocation
Makes sure no relocations are placed into the output file.
Definition cpm68k.h:258
std::shared_ptr< Linker::Image > data
Storage for data segment.
Definition cpm68k.h:107
uint16_t relocations_suppressed
Set to a non-0 value when relocations are suppressed. Typically this can be 1, but Human68k specifica...
Definition cpm68k.h:86
magic_type
Represents the magic number at the beginning of the executable file.
Definition cpm68k.h:35
@ MAGIC_CONTIGUOUS
Contiguous executables (magic value 0x601A in big endian) must load the code, data,...
Definition cpm68k.h:39
@ MAGIC_CRUNCHED
Contiguous executables with crunched relocations (magic value 0x601C in big endian),...
Definition cpm68k.h:47
@ MAGIC_NONCONTIGUOUS
Non-contiguous executables (magic value 0x601B in big endian) can load the code, data,...
Definition cpm68k.h:43
std::shared_ptr< Linker::Segment > stack_segment
Segment to collect stack (Concurrent DOS 68K only)
Definition cpm68k.h:263
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition cpm68k.cc:49
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition cpm68k.cc:613
system_type system
The system which will load the executable.
Definition cpm68k.h:165
std::shared_ptr< Linker::Segment > DataSegment()
Return data segment (if it exists)
Definition cpm68k.cc:596
offset_t ImageSize() const override
Retrieves size of stored data.
Definition cpm68k.cc:350
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition cpm68k.cc:558
system_type
Different systems have different relocation formats and expectations as to what segments should be pr...
Definition cpm68k.h:135
@ SYSTEM_GEMDOS
Digital Research GEMDOS, Atari TOS, only contiguous, text load address field replaced by program fiel...
Definition cpm68k.h:151
@ SYSTEM_UNKNOWN
Unknown system: use GEMDOS with no relocations.
Definition cpm68k.h:139
@ SYSTEM_CDOS68K
Digital Research Concurrent DOS 68K, non-contiguous not allowed, but relocations can be in CP/M-68K f...
Definition cpm68k.h:159
@ SYSTEM_GEMDOS_EARLY
Digital Research GEMDOS, only contiguous, relocations always present, header is an unusual 0x1E bytes...
Definition cpm68k.h:147
@ SYSTEM_HUMAN68K
Sharp Corporation & Hudson Soft Human68k .z executable, only contiguous, no relocations or symbol tab...
Definition cpm68k.h:155
@ SYSTEM_CPM68K
Digital Research CP/M-68K, uses CP/M-68K relocations.
Definition cpm68k.h:143
uint32_t code_size
Size of the code/text segment.
Definition cpm68k.h:56
char signature[2]
The magic number at the beginning of the executable file, one of 0x601A (contiguous),...
Definition cpm68k.h:51
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition cpm68k.cc:848
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition cpm68k.cc:459
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition cpm68k.cc:60
std::shared_ptr< Linker::Image > code
Storage for code segment.
Definition cpm68k.h:103
uint32_t symbol_table_size
Size of the symbol table.
Definition cpm68k.h:68
uint32_t data_size
Size of the initialized data segment.
Definition cpm68k.h:60
uint32_t data_address
Load address of the initialized data segment. Only relevant for non-contiguous executables (CP/M-68K)...
Definition cpm68k.h:90
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 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
void WriteWord(size_t bytes, uint64_t value, EndianType endiantype)
Read a word.
Definition writer.cc:66
unsigned segment
Segment value, as required by CP/M-68K, they take the value that is stored in file: 1 for data,...
Definition cpm68k.h:119
size_t size
Size of value to relocate.
Definition cpm68k.h:115