RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
minix.h
1#ifndef MINIX_H
2#define MINIX_H
3
4#include "../common.h"
5#include "../dumper/dumper.h"
6#include "../linker/module.h"
7#include "../linker/segment.h"
8#include "../linker/segment_manager.h"
9#include "../linker/writer.h"
10
11namespace MINIX
12{
18 class MINIXFormat : public virtual Linker::SegmentManager
19 {
20 public:
21 /* TODO: incorporate relocations and far code segment from around ELKS 0.8.0 */
22
23 void ReadFile(Linker::Reader& rd) override;
24
25 bool FormatIs16bit() const override;
26
27 enum format_type
28 {
29 UnmappedZeroPage = 0x01,
30 PageAligned = 0x02,
31 NewStyleSymbolTable = 0x04,
32 FormatCombined = 0x10,
33 FormatSeparate = 0x20,
34 PureText = 0x40,
35 TextOverlay = 0x80,
36 };
37 format_type format = format_type(0);
38
39 enum cpu_type
40 {
41 /* TODO: make support for 68K? */
42 I86 = 0x04,
43 M68K = 0x0B,
44 NS16K = 0x0C,
45 I386 = 0x10,
46 SPARC = 0x17,
47 };
48 cpu_type cpu = cpu_type(0);
49
50 uint8_t header_size = 0x20;
51 uint16_t format_version = 0;
52
53 static ::EndianType GetEndianType(cpu_type cpu);
54
55 ::EndianType GetEndianType() const;
56
57 static constexpr size_t PAGE_SIZE = 0x1000;
58
59 explicit MINIXFormat() = default;
60
61 MINIXFormat(format_type format, int version = -1)
62 : format(format), format_version(version)
63 {
64 }
65
66 MINIXFormat(format_type format, cpu_type cpu, int version = -1)
67 : format(format), cpu(cpu), format_version(version)
68 {
69 }
70
71 uint32_t bss_size = 0;
72 uint32_t total_memory = 0; /* TODO: parametrize */
73 uint16_t heap_size = 0, stack_size = 0;
74 uint32_t code_relocation_base = 0;
75 uint32_t data_relocation_base = 0;
76
77 bool enable_relocations = false;
78 bool enable_symbols = false;
79
80 struct Symbol
81 {
82 static constexpr uint8_t N_SECT = 0x07; // mask
83
84 static constexpr uint8_t N_UNDF = 0x00;
85 static constexpr uint8_t N_ABS = 0x01;
86 static constexpr uint8_t N_TEXT = 0x02;
87 static constexpr uint8_t N_DATA = 0x03;
88 static constexpr uint8_t N_BSS = 0x04;
89 static constexpr uint8_t N_COMM = 0x05;
90
91 static constexpr uint8_t N_CLASS = 0xF8; // mask
92
93 static constexpr uint8_t S_NULL = 0x00;
94 static constexpr uint8_t S_EXT = 0x10; // external
95 static constexpr uint8_t S_STAT = 0x18; // static
96
97 std::string name;
98 int32_t value;
99 uint8_t sclass;
100 uint8_t numaux; // not used by MINIX/ELKS
101 uint16_t type; // not used by MINIX/ELKS
102
103 static Symbol Read(Linker::Reader& rd);
104 void Write(Linker::Writer& wr) const;
105 void Dump(Dumper::Dumper& dump, unsigned index, offset_t relocations_offset) const;
106 };
107 std::vector<Symbol> symbols;
108
110 {
111 static constexpr uint16_t S_ABS = uint16_t(-1);
112 static constexpr uint16_t S_TEXT = uint16_t(-2);
113 static constexpr uint16_t S_DATA = uint16_t(-3);
114 static constexpr uint16_t S_BSS = uint16_t(-4);
115 /* ELKS extension */
116 static constexpr uint16_t S_FTEXT = uint16_t(-5);
117
118 static constexpr uint16_t R_ABBS = 0;
119 static constexpr uint16_t R_RELLBYTE = 2;
120 static constexpr uint16_t R_PCRBYTE = 3;
121 static constexpr uint16_t R_RELWORD = 4;
122 static constexpr uint16_t R_PCRWORD = 5;
123 static constexpr uint16_t R_RELLONG = 6;
124 static constexpr uint16_t R_PCRLONG = 7;
125 static constexpr uint16_t R_REL3BYTE = 8;
126 static constexpr uint16_t R_KBRANCHE = 9;
127 /* ELKS extension */
128 static constexpr uint16_t R_SEGWORD = 80;
129
130 uint32_t address = 0;
131 uint16_t symbol = 0;
132 uint16_t type = 0;
133 std::string symbol_name;
134
135 static Relocation Read(Linker::Reader& rd);
136 void FetchSymbolName(std::vector<Symbol>& symbols);
137 void Write(Linker::Writer& wr) const;
138 void Dump(Dumper::Dumper& dump, unsigned index, offset_t relocations_offset) const;
139 size_t GetSize() const;
140 };
141 std::vector<Relocation> code_relocations, data_relocations, far_code_relocations;
142
143 /* generated */
144 std::shared_ptr<Linker::Image> code, data, far_code;
145 std::shared_ptr<Linker::Segment> bss, heap, stack;
146 uint32_t entry_address = 0;
147
148 void SetOptions(std::map<std::string, std::string>& options) override;
149
150 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
151
152 void CreateDefaultSegments();
153
154 void SetLinkScript(std::string script_file, std::map<std::string, std::string>& options) override;
155 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
156
157 void Link(Linker::Module& module);
158
159 void ProcessModule(Linker::Module& module) override;
160
161 void CalculateValues() override;
162
163 offset_t ImageSize() const override;
165 offset_t WriteFile(Linker::Writer& wr) const override;
166 void Dump(Dumper::Dumper& dump) const override;
167
168 void GenerateFile(std::string filename, Linker::Module& module) override;
169
171 std::string GetDefaultExtension(Linker::Module& module) const override;
172 };
173
174}
175
176#endif /* MINIX_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.
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
MINIX/ELKS a.out file format.
Definition minix.h:19
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition minix.cc:287
offset_t ImageSize() const override
Retrieves size of stored data.
Definition minix.cc:729
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition minix.cc:144
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition minix.cc:482
bool FormatIs16bit() const override
Whether the format is 16-bit or not.
Definition minix.cc:259
void SetLinkScript(std::string script_file, std::map< std::string, std::string > &options) override
Selects a script file to use for linking.
Definition minix.cc:385
std::string GetDefaultExtension(Linker::Module &module) const override
Provides a default filename for the output file.
Definition minix.cc:980
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 minix.cc:341
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition minix.cc:725
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition minix.cc:754
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition minix.cc:833
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition minix.cc:963
Definition minix.h:110
Definition minix.h:81