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 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
63
64 void CreateDefaultSegments();
65
66 virtual std::unique_ptr<Script::List> GetScript(Linker::Module& module);
67
71 virtual bool ProcessRelocation(Linker::Module& module, Linker::Relocation& rel, Linker::Resolution resolution);
72
73 void Link(Linker::Module& module);
74
75 void ProcessModule(Linker::Module& module) override;
76
77 void CalculateValues() override;
78
79 void GenerateFile(std::string filename, Linker::Module& module) override;
80
82 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
83 };
84
98 {
99 public:
100 /* * * General members * * */
101
103 std::unique_ptr<Microsoft::MZFormat::PIF> pif = nullptr;
104
105 void Clear() override;
106
107 BinaryFormat(uint64_t default_base_address = 0, std::string default_extension = "")
108 : GenericBinaryFormat(default_base_address, default_extension)
109 {
110 }
111
112 BinaryFormat(std::string default_extension)
113 : GenericBinaryFormat(default_extension)
114 {
115 }
116
117 ~BinaryFormat()
118 {
119 Clear();
120 }
121
122 void ReadFile(Linker::Reader& rd) override;
123
124 offset_t ImageSize() const override;
125
127 offset_t WriteFile(Linker::Writer& wr) const override;
128
129 void Dump(Dumper::Dumper& dump) const override;
130
131 /* * * Writer members * * */
132 bool FormatSupportsSegmentation() const override;
133
134 bool FormatIs16bit() const override;
135
136 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
137
154
155 void SetModel(std::string model) override;
156
157 std::unique_ptr<Script::List> GetScript(Linker::Module& module) override;
158
159 void ProcessModule(Linker::Module& module) override;
160 };
161
162}
163
164#endif /* BINARY_H */
A flat binary format, with no header, loaded directly into memory.
Definition binary.h:98
bool FormatIs16bit() const override
Whether the format is 16-bit or not.
Definition binary.cc:273
offset_t ImageSize() const override
Retrieves size of stored data.
Definition binary.cc:236
memory_model_t memory_model
Memory model of generated executable, must be MODEL_DEFAULT for all non-x86 platforms.
Definition binary.h:153
void SetModel(std::string model) override
Sets the way memory is organized, typically modifying a built-in script.
Definition binary.cc:291
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition binary.cc:241
memory_model_t
(x86 only) Represents the memory model of the running executable, which is the way in which the segme...
Definition binary.h:140
@ MODEL_COMPACT
Compact model, separate code and multiple data segments.
Definition binary.h:148
@ MODEL_LARGE
Large model, every section is a separate segment.
Definition binary.h:150
@ MODEL_SMALL
Small model, separate code and data segments.
Definition binary.h:146
@ MODEL_DEFAULT
Default model, for x86, same as tiny, for other platforms the only possible option.
Definition binary.h:142
@ MODEL_TINY
Tiny model, code and data segment are the same.
Definition binary.h:144
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition binary.cc:416
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition binary.cc:267
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition binary.cc:198
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition binary.cc:250
std::unique_ptr< Microsoft::MZFormat::PIF > pif
Concurrent DOS program information entry, allocated only if present.
Definition binary.h:103
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition binary.cc:205
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:34
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition binary.cc:40
offset_t ImageSize() const override
Retrieves size of stored data.
Definition binary.cc:29
virtual bool ProcessRelocation(Linker::Module &module, Linker::Relocation &rel, Linker::Resolution resolution)
Callback function to process relocations.
Definition binary.cc:89
std::shared_ptr< Linker::Image > 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:121
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition binary.cc:184
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition binary.cc:18
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition binary.cc:144
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:140
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:52
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:12
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