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