RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
binary.h
1#ifndef BINARY_H
2#define BINARY_H
3
4#include "../common.h"
5#include "../linker/module.h"
6#include "../linker/segment.h"
7#include "../linker/segment_manager.h"
8#include "../linker/writer.h"
9#include "mzexe.h"
10
11/* TODO:
12 refactor, use an Image for writing, introduce base_address and zero_fill fields? maybe only for specific subclasses (such as PRL)
13*/
14
15namespace Binary
16{
21 {
22 public:
23 /* * * General members * * */
24
26 std::shared_ptr<Linker::Image> image = nullptr;
27
28 void Clear() override;
29
30 GenericBinaryFormat(uint64_t default_base_address = 0, std::string default_extension = "")
31 : position_independent(false), base_address(default_base_address), extension(default_extension)
32 {
33 }
34
35 GenericBinaryFormat(std::string default_extension)
36 : position_independent(true), extension(default_extension)
37 {
38 }
39
40 ~GenericBinaryFormat()
41 {
42 Clear();
43 }
44
45 void ReadFile(Linker::Reader& rd) override;
46
47 offset_t ImageSize() const override;
48
50 offset_t WriteFile(Linker::Writer& wr) const override;
51
52 void Dump(Dumper::Dumper& dump) const override;
53
54 /* * * Writer members * * */
58 uint64_t base_address = 0;
60 std::string extension;
61
62 static std::vector<Linker::OptionDescription<void> *> ParameterNames;
63 std::vector<Linker::OptionDescription<void> *> GetLinkerScriptParameterNames() override;
64
65 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
66
67 void CreateDefaultSegments();
68
69 virtual std::unique_ptr<Script::List> GetScript(Linker::Module& module);
70
74 virtual bool ProcessRelocation(Linker::Module& module, Linker::Relocation& rel, Linker::Resolution resolution);
75
76 void Link(Linker::Module& module);
77
78 void ProcessModule(Linker::Module& module) override;
79
80 void CalculateValues() override;
81
82 void GenerateFile(std::string filename, Linker::Module& module) override;
83
85 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
86 };
87
101 {
102 public:
103 /* * * General members * * */
104
106 std::unique_ptr<Microsoft::MZFormat::PIF> pif = nullptr;
107
108 void Clear() override;
109
110 BinaryFormat(uint64_t default_base_address = 0, std::string default_extension = "")
111 : GenericBinaryFormat(default_base_address, default_extension)
112 {
113 }
114
115 BinaryFormat(std::string default_extension)
116 : GenericBinaryFormat(default_extension)
117 {
118 }
119
120 ~BinaryFormat()
121 {
122 Clear();
123 }
124
125 void ReadFile(Linker::Reader& rd) override;
126
127 offset_t ImageSize() const override;
128
130 offset_t WriteFile(Linker::Writer& wr) const override;
131
132 void Dump(Dumper::Dumper& dump) const override;
133
134 /* * * Writer members * * */
135 bool FormatSupportsSegmentation() const override;
136
137 bool FormatIs16bit() const override;
138
139 bool FormatIsProtectedMode() const override;
140
141 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
142
159
160 static std::vector<Linker::OptionDescription<void>> MemoryModelNames;
161 std::vector<Linker::OptionDescription<void>> GetMemoryModelNames() override;
162 static std::vector<Linker::OptionDescription<void> *> ParameterNames;
163 std::vector<Linker::OptionDescription<void> *> GetLinkerScriptParameterNames() override;
164 //std::vector<Linker::OptionDescription<void>> GetSpecialSymbolNames() override;
165
166 void SetModel(std::string model) override;
167
168 std::unique_ptr<Script::List> GetScript(Linker::Module& module) override;
169
170 void ProcessModule(Linker::Module& module) override;
171 };
172
173}
174
175#endif /* BINARY_H */
A flat binary format, with no header, loaded directly into memory.
Definition binary.h:101
bool FormatIs16bit() const override
Whether the format is 16-bit or not.
Definition binary.cc:289
offset_t ImageSize() const override
Retrieves size of stored data.
Definition binary.cc:252
memory_model_t memory_model
Memory model of generated executable, must be MODEL_DEFAULT for all non-x86 platforms.
Definition binary.h:158
void SetModel(std::string model) override
Sets the way memory is organized, typically modifying a built-in script.
Definition binary.cc:343
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition binary.cc:257
std::vector< Linker::OptionDescription< void > * > GetLinkerScriptParameterNames() override
Returns a list of the parameters used in the linker scripts, used for documentation.
Definition binary.cc:334
memory_model_t
(x86 only) Represents the memory model of the running executable, which is the way in which the segme...
Definition binary.h:145
@ MODEL_COMPACT
Compact model, separate code and multiple data segments.
Definition binary.h:153
@ MODEL_LARGE
Large model, every section is a separate segment.
Definition binary.h:155
@ MODEL_SMALL
Small model, separate code and data segments.
Definition binary.h:151
@ MODEL_DEFAULT
Default model, for x86, same as tiny, for other platforms the only possible option.
Definition binary.h:147
@ MODEL_TINY
Tiny model, code and data segment are the same.
Definition binary.h:149
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition binary.cc:468
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition binary.cc:283
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition binary.cc:214
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition binary.cc:266
bool FormatIsProtectedMode() const override
Whether the format is in protected mode or not (x86 only)
Definition binary.cc:295
std::vector< Linker::OptionDescription< void > > GetMemoryModelNames() override
Returns a list of the supported memory models, used for documentation.
Definition binary.cc:322
std::unique_ptr< Microsoft::MZFormat::PIF > pif
Concurrent DOS program information entry, allocated only if present.
Definition binary.h:106
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition binary.cc:221
A template for flat binary formats.
Definition binary.h:21
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition binary.cc:35
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition binary.cc:41
offset_t ImageSize() const override
Retrieves size of stored data.
Definition binary.cc:30
virtual bool ProcessRelocation(Linker::Module &module, Linker::Relocation &rel, Linker::Resolution resolution)
Callback function to process relocations.
Definition binary.cc:105
std::shared_ptr< Linker::Image > image
The actual stored image.
Definition binary.h:26
std::vector< Linker::OptionDescription< void > * > GetLinkerScriptParameterNames() override
Returns a list of the parameters used in the linker scripts, used for documentation.
Definition binary.cc:63
uint64_t base_address
Address at which image is stored, it can be format specific or provided as a parameter.
Definition binary.h:58
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition binary.cc:137
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition binary.cc:200
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition binary.cc:19
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition binary.cc:160
bool position_independent
Set when the generated code must not reference absolute references.
Definition binary.h:56
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition binary.cc:156
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 binary.cc:68
std::string extension
Default filename extension for executables (such as .com for MS-DOS, .r for Human68k)
Definition binary.h:60
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition binary.cc:13
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 representation of a value within some binary data that has to be fixed up once the exact position o...
Definition relocation.h:27
Representing a resolved relocation.
Definition resolution.h:17
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