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/segment.h"
9#include "../linker/segment_manager.h"
10#include "../linker/writer.h"
11
12/* TODO: UNIX v1 a.out
13 magic
14 header_size + text_size + data_size
15 symtab_size
16 relocations_size
17 bss_size
18 0
19
20TODO: PDP-11 a.out
21*/
22
23namespace AOut
24{
48 class AOutFormat : public virtual Linker::InputFormat, public virtual Linker::SegmentManager
49 {
50 public:
51 /* * * General members * * */
52 ::EndianType endiantype;
53 unsigned word_size;
54
55 enum magic_type
56 {
57 OMAGIC = 0x0107,
58 NMAGIC = 0x0108,
59 ZMAGIC = 0x010B,
60 QMAGIC = 0x00CC,
61 };
62 magic_type magic = magic_type(0);
63
64 enum cpu_type
65 {
66 UNKNOWN = 0x00,
67 M68010 = 0x01,
68 M68020 = 0x02,
69 SPARC = 0x03,
70 I80386 = 0x64,
71 ARM = 0x67, /* according to BFD */
72 MIPS1 = 0x97,
73 MIPS2 = 0x98,
74 PDP11 = 0xFF, /* not a real magic number */
75 };
76 cpu_type cpu = UNKNOWN;
77
78 ::EndianType GetEndianType() const;
79
80 unsigned GetWordSize() const;
81
82 uint32_t code_size = 0;
83 uint32_t data_size = 0;
84 uint32_t bss_size = 0;
85 uint32_t symbol_table_size = 0;
86 uint32_t entry_address = 0;
87 uint32_t code_relocation_size = 0;
88 uint32_t data_relocation_size = 0;
89 std::map<uint32_t, uint32_t> code_relocations, data_relocations; /* only used by PDOS386 OMAGIC */
90
91 std::shared_ptr<Linker::Image> code, data, bss;
92
93 private:
94 bool AttemptFetchMagic(uint8_t signature[4]);
95
96 bool AttemptReadFile(Linker::Reader& rd, uint8_t signature[4], offset_t image_size);
97
98 public:
99 class Symbol
100 {
101 public:
102 std::string name;
103 uint16_t unknown = 0;
104 uint16_t name_offset = 0;
105 uint16_t type = 0;
106 uint16_t value = 0;
107 };
108
109 std::vector<Symbol> symbols;
110
111 void ReadFile(Linker::Reader& rd) override;
112
113 offset_t ImageSize() const override;
114
116 offset_t WriteFile(Linker::Writer& wr) const override;
117
118 void Dump(Dumper::Dumper& dump) const override;
119
120 /* * * Reader * * */
121
123 void GenerateModule(Linker::Module& module) const override;
124
125 /* * * Writer * * */
126
127 // for old DJGPP executables
129
130 enum system_type
131 {
132 UNIX, /* also Linux */ /* TODO */
133 DJGPP1, /* early DJGPP */
134 PDOS386, /* http://pdos.sourceforge.net/ */
135 };
136 system_type system = system_type(0);
137
138 static std::shared_ptr<AOutFormat> CreateWriter(system_type system, magic_type magic);
139
140 static std::shared_ptr<AOutFormat> CreateWriter(system_type system);
141
148 static magic_type GetDefaultMagic(system_type system);
149
150 void SetOptions(std::map<std::string, std::string>& options) override;
151
152 void OnNewSegment(std::shared_ptr<Linker::Segment> segment) override;
153
154 void CreateDefaultSegments();
155
156 std::unique_ptr<Script::List> GetScript(Linker::Module& module);
157
158 void Link(Linker::Module& module);
159
160 std::shared_ptr<Linker::Segment> GetCodeSegment();
161
162 std::shared_ptr<Linker::Segment> GetDataSegment();
163
164 std::shared_ptr<Linker::Segment> GetBssSegment();
165
166 void ProcessModule(Linker::Module& module) override;
167
168 void CalculateValues() override;
169
170 void GenerateFile(std::string filename, Linker::Module& module) override;
171
173 std::string GetDefaultExtension(Linker::Module& module, std::string filename) const override;
174 std::string GetDefaultExtension(Linker::Module& module) const override;
175 };
176
177}
178
179#endif /* AOUT_H */
Definition aout.h:100
UNIX/Linux a.out binary file format.
Definition aout.h:49
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
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:708
void SetOptions(std::map< std::string, std::string > &options) override
Passes command line parameters as settings over to format object.
Definition aout.cc:607
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition aout.cc:811
std::string GetDefaultExtension(Linker::Module &module, std::string filename) const override
Appends a default extension to the filename.
Definition aout.cc:845
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:614
void GenerateFile(std::string filename, Linker::Module &module) override
The main function that handles processing, calculating and generating the final image.
Definition aout.cc:828
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:166
virtual void GenerateModule(ModuleCollector &linker, std::string file_name, bool is_library=false) const
Loads the information into a module object.
Definition format.cc:180
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 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:248