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::Contents> 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
83 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
84 };
85
99 {
100 public:
101 /* * * General members * * */
102
104 std::unique_ptr<Microsoft::MZFormat::PIF> pif = nullptr;
105
106 void Clear() override;
107
108 BinaryFormat(uint64_t default_base_address = 0, std::string default_extension = "")
109 : GenericBinaryFormat(default_base_address, default_extension)
110 {
111 }
112
113 BinaryFormat(std::string default_extension)
114 : GenericBinaryFormat(default_extension)
115 {
116 }
117
118 ~BinaryFormat()
119 {
120 Clear();
121 }
122
123 void ReadFile(Linker::Reader& rd) override;
124
125 offset_t ImageSize() const override;
126
128 offset_t WriteFile(Linker::Writer& wr) const override;
129
130 void Dump(Dumper::Dumper& dump) const override;
131
132 /* * * Writer members * * */
133 bool FormatSupportsSegmentation() const override;
134
135 bool FormatIs16bit() const override;
136
137 bool FormatIsProtectedMode() const override;
138
139 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
140
157
158 static std::vector<Linker::OptionDescription<void>> MemoryModelNames;
159 std::vector<Linker::OptionDescription<void>> GetMemoryModelNames() override;
160 static std::vector<Linker::OptionDescription<void> *> ParameterNames;
161 std::vector<Linker::OptionDescription<void> *> GetLinkerScriptParameterNames() override;
162 //std::vector<Linker::OptionDescription<void>> GetSpecialSymbolNames() override;
163
164 void SetModel(std::string model) override;
165
166 std::unique_ptr<Script::List> GetScript(Linker::Module& module) override;
167
168 void ProcessModule(Linker::Module& module) override;
169 };
170
171}
172
173#endif /* BINARY_H */
A flat binary format, with no header, loaded directly into memory.
Definition binary.h:99
bool FormatIs16bit() const override
Whether the format is 16-bit or not.
Definition binary.cc:280
offset_t ImageSize() const override
Retrieves size of stored data.
Definition binary.cc:243
memory_model_t memory_model
Memory model of generated executable, must be MODEL_DEFAULT for all non-x86 platforms.
Definition binary.h:156
void SetModel(std::string model) override
Sets the way memory is organized, typically modifying a built-in script.
Definition binary.cc:334
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition binary.cc:248
std::vector< Linker::OptionDescription< void > * > GetLinkerScriptParameterNames() override
Returns a list of the parameters used in the linker scripts, used for documentation.
Definition binary.cc:325
memory_model_t
(x86 only) Represents the memory model of the running executable, which is the way in which the segme...
Definition binary.h:143
@ MODEL_COMPACT
Compact model, separate code and multiple data segments.
Definition binary.h:151
@ MODEL_LARGE
Large model, every section is a separate segment.
Definition binary.h:153
@ MODEL_SMALL
Small model, separate code and data segments.
Definition binary.h:149
@ MODEL_DEFAULT
Default model, for x86, same as tiny, for other platforms the only possible option.
Definition binary.h:145
@ MODEL_TINY
Tiny model, code and data segment are the same.
Definition binary.h:147
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition binary.cc:459
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition binary.cc:274
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition binary.cc:207
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition binary.cc:257
bool FormatIsProtectedMode() const override
Whether the format is in protected mode or not (x86 only)
Definition binary.cc:286
std::vector< Linker::OptionDescription< void > > GetMemoryModelNames() override
Returns a list of the supported memory models, used for documentation.
Definition binary.cc:313
std::unique_ptr< Microsoft::MZFormat::PIF > pif
Concurrent DOS program information entry, allocated only if present.
Definition binary.h:104
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition binary.cc:214
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:33
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition binary.cc:39
offset_t ImageSize() const override
Retrieves size of stored data.
Definition binary.cc:28
virtual bool ProcessRelocation(Linker::Module &module, Linker::Relocation &rel, Linker::Resolution resolution)
Callback function to process relocations.
Definition binary.cc:103
std::vector< Linker::OptionDescription< void > * > GetLinkerScriptParameterNames() override
Returns a list of the parameters used in the linker scripts, used for documentation.
Definition binary.cc:61
std::shared_ptr< Linker::Contents > image
The actual stored image.
Definition binary.h:26
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:135
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition binary.cc:193
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition binary.cc:19
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:189
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:66
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:828
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 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