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/linker.h"
6#include "../linker/module.h"
7#include "../linker/segment.h"
8#include "../linker/writer.h"
9#include "mzexe.h"
10
11/* TODO:
12 refactor, use a Writable 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::Writable> 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 void WriteFile(Linker::Writer& wr) override;
48
49 void Dump(Dumper::Dumper& dump) override;
50
51 /* * * Writer members * * */
55 uint64_t base_address = 0;
57 std::string extension;
58
59 using LinkerManager::SetLinkScript;
60
61 void SetOptions(std::map<std::string, std::string>& options) override;
62
63 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
64
65 void CreateDefaultSegments();
66
67 virtual std::unique_ptr<Script::List> GetScript(Linker::Module& module);
68
72 virtual bool ProcessRelocation(Linker::Module& module, Linker::Relocation& rel, Linker::Resolution resolution);
73
74 void Link(Linker::Module& module);
75
76 void ProcessModule(Linker::Module& module) override;
77
78 void CalculateValues() override;
79
80 void GenerateFile(std::string filename, Linker::Module& module) override;
81
82 std::string GetDefaultExtension(Linker::Module& module, std::string filename) 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 void WriteFile(Linker::Writer& wr) override;
125
126 void Dump(Dumper::Dumper& dump) override;
127
128 /* * * Writer members * * */
129 bool FormatSupportsSegmentation() const override;
130
131 bool FormatIs16bit() const override;
132
133 unsigned FormatAdditionalSectionFlags(std::string section_name) const override;
134
151
152 using LinkerManager::SetLinkScript;
153
154 void SetModel(std::string model) override;
155
156 std::unique_ptr<Script::List> GetScript(Linker::Module& module) override;
157
158 void ProcessModule(Linker::Module& module) override;
159 };
160
161}
162
163#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:266
memory_model_t memory_model
Memory model of generated executable, must be MODEL_DEFAULT for all non-x86 platforms.
Definition binary.h:150
void SetModel(std::string model) override
Sets the way memory is organized, typically modifying a built-in script.
Definition binary.cc:284
void Dump(Dumper::Dumper &dump) override
Display file contents in a nice manner.
Definition binary.cc:243
void WriteFile(Linker::Writer &wr) override
Stores data in memory to file.
Definition binary.cc:235
memory_model_t
(x86 only) Represents the memory model of the running executable, which is the way in which the segme...
Definition binary.h:137
@ MODEL_COMPACT
Compact model, separate code and multiple data segments.
Definition binary.h:145
@ MODEL_LARGE
Large model, every section is a separate segment.
Definition binary.h:147
@ MODEL_SMALL
Small model, separate code and data segments.
Definition binary.h:143
@ MODEL_DEFAULT
Default model, for x86, same as tiny, for other platforms the only possible option.
Definition binary.h:139
@ MODEL_TINY
Tiny model, code and data segment are the same.
Definition binary.h:141
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition binary.cc:409
bool FormatSupportsSegmentation() const override
Whether the format supports multiple segments.
Definition binary.cc:260
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition binary.cc:197
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:204
A template for flat binary formats.
Definition binary.h:21
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition binary.cc:45
std::string GetDefaultExtension(Linker::Module &module, std::string filename) override
Appends a default extension to the filename.
Definition binary.cc:183
virtual bool ProcessRelocation(Linker::Module &module, Linker::Relocation &rel, Linker::Resolution resolution)
Callback function to process relocations.
Definition binary.cc:88
void Dump(Dumper::Dumper &dump) override
Display file contents in a nice manner.
Definition binary.cc:33
uint64_t base_address
Address at which image is stored, it can be format specific or provided as a parameter.
Definition binary.h:55
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition binary.cc:120
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition binary.cc:16
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition binary.cc:143
void WriteFile(Linker::Writer &wr) override
Stores data in memory to file.
Definition binary.cc:27
bool position_independent
Set when the generated code must not reference absolute references.
Definition binary.h:53
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition binary.cc:139
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:51
std::string extension
Default filename extension for executables (such as .com for MS-DOS, .r for Human68k)
Definition binary.h:57
std::shared_ptr< Linker::Writable > image
The actual stored image.
Definition binary.h:26
void Clear() override
Resets all fields to their default values, deallocate memory.
Definition binary.cc:10
A class to control the output of a file analysis.
Definition dumper.h:550
A helper class to collect sections into segments.
Definition linker.h:19
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:20
A class that provides a general interface to setting up generation for a format.
Definition format.h:56
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, encapsulating functionality needed to export binary data.
Definition writer.h:15