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#include "symbol_definition.h"
12#include "symbol_name.h"
13
14namespace Linker
15{
16 class InputFormat;
17 class OutputFormat;
18 class Section;
19
23 class Module
24 {
25 public:
27 std::string file_name;
28
29 Module(std::string file_name = "")
31 {
32 }
33
38 {
39 NONE,
40 I80, /* also Z80 */
41 I86,
42 I386,
43 X86_64,
44 M6800,
45 M6809,
46 M68K,
47 MOS6502,
48 W65K,
49 Z8K,
50 PPC,
51 PPC64,
52 ARM,
53 ARM64,
54 PDP11,
55 MIPS,
56 SPARC,
57 SH,
58 };
59
63 cpu_type cpu = NONE;
64 ::EndianType endiantype = ::UndefinedEndian;
66 std::vector<SymbolDefinition> symbol_sequence;
67 private:
68 std::vector<std::shared_ptr<Section>> sections;
69 std::map<std::string, std::shared_ptr<Section>> section_names;
71 std::map<std::string, SymbolDefinition> global_symbols; // includes Weak and Common symbols
73 std::map<std::string, SymbolDefinition> local_symbols;
74 std::vector<SymbolName> imported_symbols;
75 std::map<ExportedSymbolName, Location> exported_symbols;
76
77 friend class ModuleCollector;
78
79 private:
80 bool AddSymbol(const SymbolDefinition& symbol);
81 void AppendSymbolDefinition(const SymbolDefinition& symbol);
82 void DeleteSymbolDefinition(const SymbolDefinition& symbol);
83 SymbolDefinition * FetchSymbolDefinition(const SymbolDefinition& symbol);
84 public:
85 bool HasSymbolDefinition(const SymbolDefinition& symbol);
86
90 std::vector<Relocation> relocations;
91
93 bool is_included = false;
94
101 void SetupOptions(char special_char, std::shared_ptr<OutputFormat> output_format, std::shared_ptr<const InputFormat> input_format);
102
103 private:
104 /* GNU assembler can use '$', NASM must use '?' */
105 char special_prefix_char = '$';
106 std::weak_ptr<OutputFormat> output_format;
107 std::weak_ptr<const InputFormat> input_format;
108
109 /* symbols */
110 std::string section_prefix();
111 std::string segment_prefix();
112 std::string segment_of_prefix();
113 std::string segment_at_prefix();
114 std::string segment_at_section_prefix();
115 std::string with_respect_to_segment_prefix();
116 std::string segment_difference_prefix();
117 std::string import_prefix();
118 std::string segment_of_import_prefix();
119 std::string export_prefix();
120 std::string fix_byte_prefix();
121
122 /* sections */
123 std::string resource_prefix();
124
125 bool parse_imported_name(std::string reference_name, SymbolName& symbol);
126 bool parse_exported_name(std::string reference_name, ExportedSymbolName& symbol);
127
128 public:
132 bool AddLocalSymbol(std::string name, Location location);
133 private:
134 bool AddLocalSymbol(const SymbolDefinition& symbol);
135
136 public:
140 bool AddGlobalSymbol(std::string name, Location location);
141 private:
142 bool AddGlobalSymbol(const SymbolDefinition& symbol);
143
144 public:
148 bool AddWeakSymbol(std::string name, Location location);
149 private:
150 bool AddWeakSymbol(const SymbolDefinition& symbol);
151
152 public:
157
162
166 void AddImportedSymbol(SymbolName name);
167
172
176 bool AddUndefinedSymbol(std::string symbol_name);
177 private:
178 bool AddUndefinedSymbol(const SymbolDefinition& symbol);
179
180 public:
184 void AddRelocation(Relocation relocation);
185
189 const std::vector<SymbolName>& GetImportedSymbols() const;
190
194 const std::map<ExportedSymbolName, Location>& GetExportedSymbols() const;
195
199 bool FindLocalSymbol(std::string name, Location& location);
200
204 bool FindGlobalSymbol(std::string name, Location& location);
205
209 void AddSection(std::shared_ptr<Section> section);
210
211 private:
212 void _AddSection(std::shared_ptr<Section> section);
213
214 public:
218 const std::vector<std::shared_ptr<Section>>& Sections() const;
219
223 void DeleteSection(size_t index);
224
228 void RemoveSections();
229
233 std::shared_ptr<Section> FindSection(std::string name);
234
238 std::shared_ptr<Section> FetchSection(std::string name, unsigned default_flags);
239
244
248 void Append(std::shared_ptr<Section> dst, std::shared_ptr<Section> src);
249
253 void Append(Module& other);
254
258 void AllocateSymbols(std::string default_section_name = ".comm");
259 };
260}
261
262#endif /* MODULE_H */
Represents a symbol to be exported from the module.
Definition symbol_name.h:144
Represents a single offset within a section, or an absolute location in memory if the section is null...
Definition location.h:17
Helper class that collects object files and libraries, and includes library objects for required symb...
Definition module_collector.h:25
Encodes an object module file as a collection of sections, symbols and relocations.
Definition module.h:24
std::shared_ptr< Section > FindSection(std::string name)
Searches for a section with a specific name.
Definition module.cc:892
const std::vector< SymbolName > & GetImportedSymbols() const
Retrieves list of all imported symbols.
Definition module.cc:795
bool AddUndefinedSymbol(std::string symbol_name)
Processes undefined symbol for extended syntax.
Definition module.cc:526
bool AddGlobalSymbol(std::string name, Location location)
Adds and processes exported symbol for extended syntax.
Definition module.cc:257
std::vector< SymbolDefinition > symbol_sequence
Sorted collection of all symbols appearing in the module, duplicates removed.
Definition module.h:66
void Append(std::shared_ptr< Section > dst, std::shared_ptr< Section > src)
Appends two sections by shifting all symbols locations and relocation targets in the second one.
Definition module.cc:926
bool is_included
Set to true if module is included in the linking process, relevant for libraries.
Definition module.h:93
const std::vector< std::shared_ptr< Section > > & Sections() const
Retrieves list of all sections.
Definition module.cc:875
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:898
bool AddLocalSymbol(std::string name, Location location)
Adds an internal symbol.
Definition module.cc:225
const std::map< ExportedSymbolName, Location > & GetExportedSymbols() const
Retrieves map of all exported symbols and their locations.
Definition module.cc:800
void AddSection(std::shared_ptr< Section > section)
Adds a new section.
Definition module.cc:827
bool FindGlobalSymbol(std::string name, Location &location)
Searches for a global or weak symbol.
Definition module.cc:814
bool AddWeakSymbol(std::string name, Location location)
Adds a weakly bound symbol.
Definition module.cc:344
void RemoveSections()
Removes all sections from internal list, without deleting them.
Definition module.cc:886
void ResolveLocalRelocations()
Attempts to resolve the local targets of all relocations.
Definition module.cc:909
void AddImportedSymbol(SymbolName name)
Adds an imported symbol (Microsoft format: library name + symbol name + ordinal/hint)
Definition module.cc:513
void SetupOptions(char special_char, std::shared_ptr< OutputFormat > output_format, std::shared_ptr< const InputFormat > input_format)
Initializes the reader for linking purposes.
Definition module.cc:72
void AllocateSymbols(std::string default_section_name=".comm")
All common symbols are converted to global symbols and assigned addresses within a their section (usu...
Definition module.cc:1005
cpu_type
Supported CPU types.
Definition module.h:38
bool FindLocalSymbol(std::string name, Location &location)
Searches for a local symbol.
Definition module.cc:805
void AddExportedSymbol(ExportedSymbolName name, Location symbol)
Adds an exported symbol (Microsoft format: library name + symbol name + ordinal/hint)
Definition module.cc:521
void DeleteSection(size_t index)
Deletes a specific sections.
Definition module.cc:880
std::string file_name
Stores source/destination file name.
Definition module.h:27
cpu_type cpu
Encodes the CPU for the target.
Definition module.h:63
bool AddLocalCommonSymbol(SymbolDefinition symbol)
Adds a local common symbol.
Definition module.cc:475
std::vector< Relocation > relocations
List of relocations within the module.
Definition module.h:90
void AddRelocation(Relocation relocation)
Adds and processes relocation for extended syntax.
Definition module.cc:604
bool AddCommonSymbol(SymbolDefinition symbol)
Adds a common symbol.
Definition module.cc:406
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 a symbol definition.
Definition symbol_definition.h:16
Represents an (imported or internal) symbol name, which can be more complex than a string.
Definition symbol_name.h:18