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
89
93 cpu_type cpu = NONE;
94 ::EndianType endiantype = ::UndefinedEndian;
96 std::vector<SymbolDefinition> symbol_sequence;
97 private:
98 std::vector<std::shared_ptr<Section>> sections;
99 std::map<std::string, std::shared_ptr<Section>> section_names;
101 std::map<std::string, SymbolDefinition> global_symbols; // includes Weak and Common symbols
103 std::map<std::string, SymbolDefinition> local_symbols;
104 std::vector<SymbolName> imported_symbols;
105 std::map<ExportedSymbolName, Location> exported_symbols;
106
107 friend class ModuleCollector;
108
109 private:
110 bool AddSymbol(const SymbolDefinition& symbol);
111 void AppendSymbolDefinition(const SymbolDefinition& symbol);
112 void DeleteSymbolDefinition(const SymbolDefinition& symbol);
113 SymbolDefinition * FetchSymbolDefinition(const SymbolDefinition& symbol);
114 public:
115 bool HasSymbolDefinition(const SymbolDefinition& symbol);
116
117 private:
121 std::vector<Relocation> relocations;
122
129 std::map<Location, size_t> relocation_indexes;
130
131 public:
135 std::vector<Relocation>& GetRelocations();
136
138 bool is_included = false;
139
146 void SetupOptions(char special_char, std::shared_ptr<OutputFormat> output_format, std::shared_ptr<const InputFormat> input_format);
147
148 private:
153 char special_prefix_char = '$';
154 std::weak_ptr<OutputFormat> output_format;
155 std::weak_ptr<const InputFormat> input_format;
156
158 std::string section_prefix();
160 std::string segment_prefix();
162 std::string segment_of_prefix();
164 std::string segment_at_prefix();
166 std::string segment_at_section_prefix();
168 std::string with_respect_to_segment_prefix();
170 std::string segment_difference_prefix();
172 std::string entry_prefix();
174 std::string far_entry_prefix();
176 std::string define_entry_prefix();
178 std::string transition_vector_prefix();
180 std::string import_prefix();
182 std::string segment_of_import_prefix();
184 std::string export_prefix();
186 std::string fix_byte_prefix();
187
188 /* sections */
190 std::string resource_prefix();
191
192 bool parse_imported_name(std::string reference_name, SymbolName& symbol);
193 bool parse_exported_name(std::string reference_name, ExportedSymbolName& symbol);
194
195 public:
199 bool AddLocalSymbol(std::string name, Location location);
200 private:
201 bool AddLocalSymbol(const SymbolDefinition& symbol);
202
203 public:
207 bool AddGlobalSymbol(std::string name, Location location);
208 private:
209 bool AddGlobalSymbol(const SymbolDefinition& symbol);
210
211 public:
215 bool AddWeakSymbol(std::string name, Location location);
216 private:
217 bool AddWeakSymbol(const SymbolDefinition& symbol);
218
219 public:
224
229
233 void AddImportedSymbol(SymbolName name);
234
239
243 bool AddUndefinedSymbol(std::string symbol_name);
244 private:
245 bool AddUndefinedSymbol(const SymbolDefinition& symbol);
246
247 public:
251 void AddRelocation(Relocation relocation);
252
256 const std::vector<SymbolName>& GetImportedSymbols() const;
257
261 const std::map<ExportedSymbolName, Location>& GetExportedSymbols() const;
262
266 bool FindLocalSymbol(std::string name, Location& location);
267
271 bool FindGlobalSymbol(std::string name, Location& location);
272
276 void AddSection(std::shared_ptr<Section> section);
277
278 private:
279 void _AddSection(std::shared_ptr<Section> section);
280
281 public:
285 const std::vector<std::shared_ptr<Section>>& Sections() const;
286
290 void DeleteSection(size_t index);
291
295 void RemoveSections();
296
300 std::shared_ptr<Section> FindSection(std::string name);
301
305 std::shared_ptr<Section> FetchSection(std::string name, unsigned default_flags);
306
311
315 void Append(std::shared_ptr<Section> dst, std::shared_ptr<Section> src);
316
320 void Append(Module& other);
321
325 void AllocateSymbols(std::string default_section_name = ".bss", bool force_create_segment = false);
326
330 void AllocateStack(offset_t stack_size, std::string default_section_name = ".stack");
331 };
332}
333
334#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:1029
const std::vector< SymbolName > & GetImportedSymbols() const
Retrieves list of all imported symbols.
Definition module.cc:898
bool AddUndefinedSymbol(std::string symbol_name)
Processes undefined symbol for extended syntax.
Definition module.cc:576
bool AddGlobalSymbol(std::string name, Location location)
Adds and processes exported symbol for extended syntax.
Definition module.cc:299
std::vector< SymbolDefinition > symbol_sequence
Sorted collection of all symbols appearing in the module, duplicates removed.
Definition module.h:96
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:1063
bool is_included
Set to true if module is included in the linking process, relevant for libraries.
Definition module.h:138
const std::vector< std::shared_ptr< Section > > & Sections() const
Retrieves list of all sections.
Definition module.cc:1012
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:1035
bool AddLocalSymbol(std::string name, Location location)
Adds an internal symbol.
Definition module.cc:267
const std::map< ExportedSymbolName, Location > & GetExportedSymbols() const
Retrieves map of all exported symbols and their locations.
Definition module.cc:903
void AddSection(std::shared_ptr< Section > section)
Adds a new section.
Definition module.cc:930
bool FindGlobalSymbol(std::string name, Location &location)
Searches for a global or weak symbol.
Definition module.cc:917
bool AddWeakSymbol(std::string name, Location location)
Adds a weakly bound symbol.
Definition module.cc:394
void RemoveSections()
Removes all sections from internal list, without deleting them.
Definition module.cc:1023
void ResolveLocalRelocations()
Attempts to resolve the local targets of all relocations.
Definition module.cc:1046
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:1163
void AddImportedSymbol(SymbolName name)
Adds an imported symbol (Microsoft format: library name + symbol name + ordinal/hint)
Definition module.cc:563
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:81
@ 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
@ SYS390_64
IBM z/Architecture.
Definition module.h:79
@ IA64
Intel Itanium.
Definition module.h:87
@ SH
Hitachi SuperH.
Definition module.h:85
@ 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:73
@ WE32K
Bellmac 32/Western Electric 32000/32100/32200.
Definition module.h:75
@ ARM
ARM (32-bit)
Definition module.h:65
@ I386
Intel 80386.
Definition module.h:45
@ M6800
Motorla 6800.
Definition module.h:49
@ SYS360
IBM System/360, System/370, System/390.
Definition module.h:77
@ VAX
VAX.
Definition module.h:71
@ SPARC
Sun SPARC.
Definition module.h:83
bool FindLocalSymbol(std::string name, Location &location)
Searches for a local symbol.
Definition module.cc:908
void AddExportedSymbol(ExportedSymbolName name, Location symbol)
Adds an exported symbol (Microsoft format: library name + symbol name + ordinal/hint)
Definition module.cc:571
void DeleteSection(size_t index)
Deletes a specific sections.
Definition module.cc:1017
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:1142
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:93
bool AddLocalCommonSymbol(SymbolDefinition symbol)
Adds a local common symbol.
Definition module.cc:525
void AddRelocation(Relocation relocation)
Adds and processes relocation for extended syntax.
Definition module.cc:679
bool AddCommonSymbol(SymbolDefinition symbol)
Adds a common symbol.
Definition module.cc:456
A representation of a value within some binary data that has to be fixed up once the exact position o...
Definition relocation.h:28
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