RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
module.h
1#ifndef MODULE_H
2#define MODULE_H
3
4#include <map>
5#include <memory>
6#include <vector>
7#include <string>
8#include "../common.h"
9#include "location.h"
10#include "relocation.h"
11
12namespace Linker
13{
14 class Section;
15
19 class Module
20 {
21 public:
26 {
27 NONE,
28 I80, /* also Z80 */
29 I86,
30 I386,
31 X86_64,
32 M6800,
33 M6809,
34 M68K,
35 MOS6502,
36 W65K,
37 Z8K,
38 PPC,
39 PPC64,
40 ARM,
41 ARM64,
42 PDP11,
43 MIPS,
44 SPARC,
45 SH,
46 };
47
51 cpu_type cpu = NONE;
52 private:
53 std::vector<std::shared_ptr<Section>> sections;
54 std::map<std::string, std::shared_ptr<Section>> section_names;
55 std::map<std::string, Location> symbols;
56 std::map<std::string, Location> local_symbols;
57 std::map<std::string, CommonSymbol> unallocated_symbols;
58 std::vector<SymbolName> imported_symbols;
59 std::map<ExportedSymbol, Location> exported_symbols;
60 public:
64 std::vector<Relocation> relocations;
65
72 void SetupOptions(char special_char, std::shared_ptr<OutputFormat> output_format, std::shared_ptr<InputFormat> input_format);
73
74 private:
75 /* GNU assembler can use '$', NASM must use '?' */
76 char special_prefix_char = '$';
77 std::weak_ptr<Linker::OutputFormat> output_format;
78 std::weak_ptr<Linker::InputFormat> input_format;
79
80 /* symbols */
81 std::string segment_prefix();
82 std::string segment_of_prefix();
83 std::string segment_at_prefix();
84 std::string with_respect_to_segment_prefix();
85 std::string segment_difference_prefix();
86 std::string import_prefix();
87 std::string segment_of_import_prefix();
88 std::string export_prefix();
89 std::string fix_byte_prefix();
90
91 /* sections */
92 std::string resource_prefix();
93
94 bool parse_imported_name(std::string reference_name, Linker::SymbolName& symbol);
95 bool parse_exported_name(std::string reference_name, Linker::ExportedSymbol& symbol);
96
97 public:
101 void AddLocalSymbol(std::string name, Location location);
102 private:
103 void _AddLocalSymbol(std::string name, Location location);
104
105 public:
109 void AddGlobalSymbol(std::string name, Location location);
110 private:
111 void _AddGlobalSymbol(std::string name, Location location);
112
113 public:
117 void AddCommonSymbol(std::string name, CommonSymbol symbol);
118
122 void AddImportedSymbol(SymbolName name);
123
127 void AddExportedSymbol(ExportedSymbol name, Location symbol);
128
132 void AddUndefinedSymbol(std::string symbol_name);
133
137 void AddRelocation(Relocation relocation);
138
142 const std::vector<SymbolName>& GetImportedSymbols() const;
143
147 const std::map<ExportedSymbol, Location>& GetExportedSymbols() const;
148
152 bool FindLocalSymbol(std::string name, Location& location);
153
157 bool FindGlobalSymbol(std::string name, Location& location);
158
162 void AddSection(std::shared_ptr<Section> section);
163
164 private:
165 void _AddSection(std::shared_ptr<Section> section);
166
167 public:
171 const std::vector<std::shared_ptr<Section>>& Sections() const;
172
176 void DeleteSection(size_t index);
177
181 void RemoveSections();
182
186 std::shared_ptr<Section> FindSection(std::string name);
187
191 std::shared_ptr<Section> FetchSection(std::string name, unsigned default_flags);
192
196 void ResolveRelocations();
197
201 void Append(std::shared_ptr<Section> dst, std::shared_ptr<Section> src);
202
206 void Append(Module& other);
207
211 void AllocateSymbols(std::shared_ptr<Section> section);
212
216 void AllocateSymbols();
217 };
218}
219
220#endif /* MODULE_H */
Represents a currently unallocated variable that should be allocated in the final stages of the linki...
Definition symbol.h:224
Represents a symbol to be exported from the module.
Definition symbol.h:119
Represents a single offset within a section, or an absolute location in memory if the section is null...
Definition location.h:17
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:20
std::shared_ptr< Section > FindSection(std::string name)
Searches for a section with a specific name.
Definition module.cc:491
const std::vector< SymbolName > & GetImportedSymbols() const
Retrieves list of all imported symbols.
Definition module.cc:398
void SetupOptions(char special_char, std::shared_ptr< OutputFormat > output_format, std::shared_ptr< InputFormat > input_format)
Initializes the reader for linking purposes.
Definition module.cc:7
void Append(std::shared_ptr< Section > dst, std::shared_ptr< Section > src)
Appends two of its sections.
Definition module.cc:525
const std::vector< std::shared_ptr< Section > > & Sections() const
Retrieves list of all sections.
Definition module.cc:474
void AddExportedSymbol(ExportedSymbol name, Location symbol)
Adds an exported symbol.
Definition module.cc:207
std::shared_ptr< Section > FetchSection(std::string name, unsigned default_flags)
Searches or creates a section with a specific name, with the assigned flags.
Definition module.cc:497
void AddCommonSymbol(std::string name, CommonSymbol symbol)
Adds a common symbol.
Definition module.cc:194
void AddSection(std::shared_ptr< Section > section)
Adds a new section.
Definition module.cc:426
bool FindGlobalSymbol(std::string name, Location &location)
Searches for a global symbol.
Definition module.cc:417
void RemoveSections()
Removes all sections from internal list, without deleting them.
Definition module.cc:485
void AddImportedSymbol(SymbolName name)
Adds an imported symbol.
Definition module.cc:199
cpu_type
Supported CPU types.
Definition module.h:26
bool FindLocalSymbol(std::string name, Location &location)
Searches for a local symbol.
Definition module.cc:408
void ResolveRelocations()
Attempts to resolve the targets of all relocations.
Definition module.cc:508
void AddUndefinedSymbol(std::string symbol_name)
Processes undefined symbol for extended syntax.
Definition module.cc:212
void DeleteSection(size_t index)
Deletes a specific sections.
Definition module.cc:479
void AddLocalSymbol(std::string name, Location location)
Adds an internal symbol.
Definition module.cc:138
cpu_type cpu
Encodes the CPU for the target.
Definition module.h:51
const std::map< ExportedSymbol, Location > & GetExportedSymbols() const
Retrieves map of all exported symbols and their locations.
Definition module.cc:403
std::vector< Relocation > relocations
List of relocations within the module.
Definition module.h:64
void AddGlobalSymbol(std::string name, Location location)
Adds and processes exported symbol for extended syntax.
Definition module.cc:162
void AddRelocation(Relocation relocation)
Adds and processes relocation for extended syntax.
Definition module.cc:254
void AllocateSymbols()
All common symbols are converted to global symbols and assigned addresses within a "....
Definition module.cc:630
A representation of a value within some binary data that has to be fixed up once the exact position o...
Definition relocation.h:27
Represents an (imported or internal) symbol name, which can be more complex than a string.
Definition symbol.h:18