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
81
85 cpu_type cpu = NONE;
86 ::EndianType endiantype = ::UndefinedEndian;
88 std::vector<SymbolDefinition> symbol_sequence;
89 private:
90 std::vector<std::shared_ptr<Section>> sections;
91 std::map<std::string, std::shared_ptr<Section>> section_names;
93 std::map<std::string, SymbolDefinition> global_symbols; // includes Weak and Common symbols
95 std::map<std::string, SymbolDefinition> local_symbols;
96 std::vector<SymbolName> imported_symbols;
97 std::map<ExportedSymbolName, Location> exported_symbols;
98
99 friend class ModuleCollector;
100
101 private:
102 bool AddSymbol(const SymbolDefinition& symbol);
103 void AppendSymbolDefinition(const SymbolDefinition& symbol);
104 void DeleteSymbolDefinition(const SymbolDefinition& symbol);
105 SymbolDefinition * FetchSymbolDefinition(const SymbolDefinition& symbol);
106 public:
107 bool HasSymbolDefinition(const SymbolDefinition& symbol);
108
109 private:
113 std::vector<Relocation> relocations;
114
121 std::map<Location, size_t> relocation_indexes;
122
123 public:
127 std::vector<Relocation>& GetRelocations();
128
130 bool is_included = false;
131
138 void SetupOptions(char special_char, std::shared_ptr<OutputFormat> output_format, std::shared_ptr<const InputFormat> input_format);
139
140 private:
141 /* GNU assembler can use '$', NASM must use '?' */
142 char special_prefix_char = '$';
143 std::weak_ptr<OutputFormat> output_format;
144 std::weak_ptr<const InputFormat> input_format;
145
146 /* symbols */
147 std::string section_prefix();
148 std::string segment_prefix();
149 std::string segment_of_prefix();
150 std::string segment_at_prefix();
151 std::string segment_at_section_prefix();
152 std::string with_respect_to_segment_prefix();
153 std::string segment_difference_prefix();
154 std::string import_prefix();
155 std::string segment_of_import_prefix();
156 std::string export_prefix();
157 std::string fix_byte_prefix();
158
159 /* sections */
160 std::string resource_prefix();
161
162 bool parse_imported_name(std::string reference_name, SymbolName& symbol);
163 bool parse_exported_name(std::string reference_name, ExportedSymbolName& symbol);
164
165 public:
169 bool AddLocalSymbol(std::string name, Location location);
170 private:
171 bool AddLocalSymbol(const SymbolDefinition& symbol);
172
173 public:
177 bool AddGlobalSymbol(std::string name, Location location);
178 private:
179 bool AddGlobalSymbol(const SymbolDefinition& symbol);
180
181 public:
185 bool AddWeakSymbol(std::string name, Location location);
186 private:
187 bool AddWeakSymbol(const SymbolDefinition& symbol);
188
189 public:
194
199
203 void AddImportedSymbol(SymbolName name);
204
209
213 bool AddUndefinedSymbol(std::string symbol_name);
214 private:
215 bool AddUndefinedSymbol(const SymbolDefinition& symbol);
216
217 public:
221 void AddRelocation(Relocation relocation);
222
226 const std::vector<SymbolName>& GetImportedSymbols() const;
227
231 const std::map<ExportedSymbolName, Location>& GetExportedSymbols() const;
232
236 bool FindLocalSymbol(std::string name, Location& location);
237
241 bool FindGlobalSymbol(std::string name, Location& location);
242
246 void AddSection(std::shared_ptr<Section> section);
247
248 private:
249 void _AddSection(std::shared_ptr<Section> section);
250
251 public:
255 const std::vector<std::shared_ptr<Section>>& Sections() const;
256
260 void DeleteSection(size_t index);
261
265 void RemoveSections();
266
270 std::shared_ptr<Section> FindSection(std::string name);
271
275 std::shared_ptr<Section> FetchSection(std::string name, unsigned default_flags);
276
281
285 void Append(std::shared_ptr<Section> dst, std::shared_ptr<Section> src);
286
290 void Append(Module& other);
291
295 void AllocateSymbols(std::string default_section_name = ".bss", bool force_create_segment = false);
296
300 void AllocateStack(offset_t stack_size, std::string default_section_name = ".stack");
301 };
302}
303
304#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:1040
const std::vector< SymbolName > & GetImportedSymbols() const
Retrieves list of all imported symbols.
Definition module.cc:909
bool AddUndefinedSymbol(std::string symbol_name)
Processes undefined symbol for extended syntax.
Definition module.cc:541
bool AddGlobalSymbol(std::string name, Location location)
Adds and processes exported symbol for extended syntax.
Definition module.cc:271
std::vector< SymbolDefinition > symbol_sequence
Sorted collection of all symbols appearing in the module, duplicates removed.
Definition module.h:88
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:1074
bool is_included
Set to true if module is included in the linking process, relevant for libraries.
Definition module.h:130
const std::vector< std::shared_ptr< Section > > & Sections() const
Retrieves list of all sections.
Definition module.cc:1023
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:1046
bool AddLocalSymbol(std::string name, Location location)
Adds an internal symbol.
Definition module.cc:239
const std::map< ExportedSymbolName, Location > & GetExportedSymbols() const
Retrieves map of all exported symbols and their locations.
Definition module.cc:914
void AddSection(std::shared_ptr< Section > section)
Adds a new section.
Definition module.cc:941
bool FindGlobalSymbol(std::string name, Location &location)
Searches for a global or weak symbol.
Definition module.cc:928
bool AddWeakSymbol(std::string name, Location location)
Adds a weakly bound symbol.
Definition module.cc:359
void RemoveSections()
Removes all sections from internal list, without deleting them.
Definition module.cc:1034
void ResolveLocalRelocations()
Attempts to resolve the local targets of all relocations.
Definition module.cc:1057
void AllocateStack(offset_t stack_size, std::string default_section_name=".stack")
Unless .stack_top is defined or stack_size is zero, makes sure a .stack segment exists and is at leas...
Definition module.cc:1174
void AddImportedSymbol(SymbolName name)
Adds an imported symbol (Microsoft format: library name + symbol name + ordinal/hint)
Definition module.cc:528
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:77
std::vector< Relocation > & GetRelocations()
Returns all the relocations within the module.
Definition module.cc:72
cpu_type
Supported CPU types.
Definition module.h:38
@ MIPS
MIPS.
Definition module.h:71
@ PPC64
IBM PowerPC (64-bit)
Definition module.h:63
@ Z8K
Zilog Z8000.
Definition module.h:59
@ ARM64
AArch64 or ARM64 (64-bit)
Definition module.h:67
@ X86_64
Intel and AMD 64-bit x86.
Definition module.h:47
@ IA64
Intel Itanium.
Definition module.h:79
@ SH
Hitachi SuperH.
Definition module.h:75
@ MOS6502
MOS 6502.
Definition module.h:55
@ I86
Intel 8086 and 80286.
Definition module.h:43
@ W65K
WDC 65C816.
Definition module.h:57
@ PPC
IBM PowerPC (32-bit)
Definition module.h:61
@ M68K
Motorla 68000 and 68020.
Definition module.h:53
@ PDP11
PDP-11.
Definition module.h:69
@ I80
Intel 8080 and Zilog Z80.
Definition module.h:41
@ M6809
Motorla 6809.
Definition module.h:51
@ ALPHA
DEC Alpha.
Definition module.h:77
@ ARM
ARM (32-bit)
Definition module.h:65
@ I386
Intel 80386.
Definition module.h:45
@ M6800
Motorla 6800.
Definition module.h:49
@ SPARC
Sun SPARC.
Definition module.h:73
bool FindLocalSymbol(std::string name, Location &location)
Searches for a local symbol.
Definition module.cc:919
void AddExportedSymbol(ExportedSymbolName name, Location symbol)
Adds an exported symbol (Microsoft format: library name + symbol name + ordinal/hint)
Definition module.cc:536
void DeleteSection(size_t index)
Deletes a specific sections.
Definition module.cc:1028
void AllocateSymbols(std::string default_section_name=".bss", bool force_create_segment=false)
All common symbols are converted to global symbols and assigned addresses within a their section.
Definition module.cc:1153
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:85
bool AddLocalCommonSymbol(SymbolDefinition symbol)
Adds a local common symbol.
Definition module.cc:490
void AddRelocation(Relocation relocation)
Adds and processes relocation for extended syntax.
Definition module.cc:623
bool AddCommonSymbol(SymbolDefinition symbol)
Adds a common symbol.
Definition module.cc:421
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