RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
aout.h
1#ifndef AOUT_H
2#define AOUT_H
3
4#include "mzexe.h"
5#include "../common.h"
6#include "../dumper/dumper.h"
7#include "../linker/module.h"
8#include "../linker/options.h"
9#include "../linker/segment.h"
10#include "../linker/segment_manager.h"
11#include "../linker/writer.h"
12
13/* TODO: UNIX v1 a.out
14 magic
15 header_size + text_size + data_size
16 symtab_size
17 relocations_size
18 bss_size
19 0
20
21TODO: PDP-11 a.out
22*/
23
24namespace AOut
25{
49 class AOutFormat : public virtual Linker::InputFormat, public virtual Linker::SegmentManager
50 {
51 public:
52 /* * * General members * * */
53 ::EndianType endiantype;
54 unsigned word_size;
55
56 enum magic_type
57 {
58 OMAGIC = 0x0107,
59 NMAGIC = 0x0108,
60 ZMAGIC = 0x010B,
61 QMAGIC = 0x00CC,
62 };
63 magic_type magic = magic_type(0);
64
65 enum cpu_type
66 {
67 UNKNOWN = 0x00,
68 M68010 = 0x01,
69 M68020 = 0x02,
70 SPARC = 0x03,
71 I80386 = 0x64,
72 ARM = 0x67, /* according to BFD */
73 MIPS1 = 0x97,
74 MIPS2 = 0x98,
75 PDP11 = 0xFF, /* not a real magic number */
76 };
77 cpu_type cpu = UNKNOWN;
78
79 ::EndianType GetEndianType() const;
80
81 unsigned GetWordSize() const;
82
83 uint32_t code_size = 0;
84 uint32_t data_size = 0;
85 uint32_t bss_size = 0;
86 uint32_t symbol_table_size = 0;
87 uint32_t entry_address = 0;
88 uint32_t code_relocation_size = 0;
89 uint32_t data_relocation_size = 0;
90 std::map<uint32_t, uint32_t> code_relocations, data_relocations; /* only used by PDOS386 OMAGIC */
91
92 std::shared_ptr<Linker::Image> code, data, bss;
93
94 private:
95 bool AttemptFetchMagic(uint8_t signature[4]);
96
97 bool AttemptReadFile(Linker::Reader& rd, uint8_t signature[4], offset_t image_size);
98
99 public:
100 class Symbol
101 {
102 public:
103 std::string name;
104 uint16_t unknown = 0;
105 uint16_t name_offset = 0;
106 uint16_t type = 0;
107 uint16_t value = 0;
108 };
109
110 std::vector<Symbol> symbols;
111
112 void ReadFile(Linker::Reader& rd) override;
113
114 offset_t ImageSize() const override;
115
117 offset_t WriteFile(Linker::Writer& wr) const override;
118
119 void Dump(Dumper::Dumper& dump) const override;
120
121 /* * * Reader members * * */
122
124 void GenerateModule(Linker::Module& module) const override;
125
126 /* * * Writer members * * */
127
129 {
130 public:
131 Linker::Option<std::string> stub{"stub", "Filename for stub that gets prepended to executable"};
132
134 {
135 InitializeFields(stub);
136 }
137 };
138
139 // for old DJGPP executables
141
142 enum system_type
143 {
144 UNIX, /* also Linux */ /* TODO */
145 DJGPP1, /* early DJGPP */
146 PDOS386, /* http://pdos.sourceforge.net/ */
147 };
148 system_type system = system_type(0);
149
150 static std::shared_ptr<AOutFormat> CreateWriter(system_type system, magic_type magic);
151
152 static std::shared_ptr<AOutFormat> CreateWriter(system_type system);
153
160 static magic_type GetDefaultMagic(system_type system);
161
162 static std::vector<Linker::OptionDescription<void> *> ParameterNames;
163 std::vector<Linker::OptionDescription<void> *> GetLinkerScriptParameterNames() override;
164
165 std::shared_ptr<Linker::OptionCollector> GetOptions() override;
166
167 void SetOptions(std::map<std::string, std::string>& options) override;
168
169 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
170
171 void CreateDefaultSegments();
172
173 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
174
175 void Link(Linker::Module& module);
176
177 std::shared_ptr<Linker::Segment> GetCodeSegment();
178
179 std::shared_ptr<Linker::Segment> GetDataSegment();
180
181 std::shared_ptr<Linker::Segment> GetBssSegment();
182
183 void ProcessModule(Linker::Module& module) override;
184
185 void CalculateValues() override;
186
187 void GenerateFile(std::string filename, Linker::Module& module) override;
188
190 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
191 std::string GetDefaultExtension(Linker::Module& module) const override;
192 };
193
194}
195
196#endif /* AOUT_H */
Definition aout.h:101
UNIX/Linux a.out binary file format.
Definition aout.h:50
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition aout.cc:352
void GenerateModule(Linker::Module &module) const override
Loads the information into a module object, a convenience method when there is a single module genera...
Definition aout.cc:402
std::vector< Linker::OptionDescription< void > * > GetLinkerScriptParameterNames() override
Returns a list of the parameters used in the linker scripts, used for documentation.
Definition aout.cc:618
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition aout.cc:389
static magic_type GetDefaultMagic(system_type system)
Default magic number associated with the system.
Definition aout.cc:595
void ProcessModule(Linker::Module &module) override
Processes the module object and initializes format fields.
Definition aout.cc:731
std::shared_ptr< Linker::OptionCollector > GetOptions() override
Returns object containing a sequence of option fields provided with the -S command line flag.
Definition aout.cc:623
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition aout.cc:628
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition aout.cc:834
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition aout.cc:868
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition aout.cc:147
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 aout.cc:637
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition aout.cc:851
offset_t ImageSize() const override
Retrieves size of stored data.
Definition aout.cc:347
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.
A class that provides a general interface to loading a module.
Definition format.h:193
virtual void GenerateModule(ModuleCollector &linker, std::string file_name, bool is_library=false) const
Loads the information into a module object.
Definition format.cc:215
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
Definition mzexe.h:269