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;
65 std::vector<SymbolDefinition> symbol_sequence;
66 private:
67 std::vector<std::shared_ptr<Section>> sections;
68 std::map<std::string, std::shared_ptr<Section>> section_names;
70 std::map<std::string, SymbolDefinition> global_symbols; // includes Weak and Common symbols
72 std::map<std::string, SymbolDefinition> local_symbols;
73 std::vector<SymbolName> imported_symbols;
74 std::map<ExportedSymbolName, Location> exported_symbols;
75
76 friend class ModuleCollector;
77
78 private:
79 bool AddSymbol(const SymbolDefinition& symbol);
80 void AppendSymbolDefinition(const SymbolDefinition& symbol);
81 void DeleteSymbolDefinition(const SymbolDefinition& symbol);
82 SymbolDefinition * FetchSymbolDefinition(const SymbolDefinition& symbol);
83 public:
84 bool HasSymbolDefinition(const SymbolDefinition& symbol);
85
89 std::vector<Relocation> relocations;
90
92 bool is_included = false;
93
100 void SetupOptions(char special_char, std::shared_ptr<OutputFormat> output_format, std::shared_ptr<const InputFormat> input_format);
101
102 private:
103 /* GNU assembler can use '$', NASM must use '?' */
104 char special_prefix_char = '$';
105 std::weak_ptr<OutputFormat> output_format;
106 std::weak_ptr<const InputFormat> input_format;
107
108 /* symbols */
109 std::string segment_prefix();
110 std::string segment_of_prefix();
111 std::string segment_at_prefix();
112 std::string with_respect_to_segment_prefix();
113 std::string segment_difference_prefix();
114 std::string import_prefix();
115 std::string segment_of_import_prefix();
116 std::string export_prefix();
117 std::string fix_byte_prefix();
118
119 /* sections */
120 std::string resource_prefix();
121
122 bool parse_imported_name(std::string reference_name, SymbolName& symbol);
123 bool parse_exported_name(std::string reference_name, ExportedSymbolName& symbol);
124
125 public:
129 bool AddLocalSymbol(std::string name, Location location);
130 private:
131 bool AddLocalSymbol(const SymbolDefinition& symbol);
132
133 public:
137 bool AddGlobalSymbol(std::string name, Location location);
138 private:
139 bool AddGlobalSymbol(const SymbolDefinition& symbol);
140
141 public:
145 bool AddWeakSymbol(std::string name, Location location);
146 private:
147 bool AddWeakSymbol(const SymbolDefinition& symbol);
148
149 public:
154
159
163 void AddImportedSymbol(SymbolName name);
164
169
173 bool AddUndefinedSymbol(std::string symbol_name);
174 private:
175 bool AddUndefinedSymbol(const SymbolDefinition& symbol);
176
177 public:
181 void AddRelocation(Relocation relocation);
182
186 const std::vector<SymbolName>& GetImportedSymbols() const;
187
191 const std::map<ExportedSymbolName, Location>& GetExportedSymbols() const;
192
196 bool FindLocalSymbol(std::string name, Location& location);
197
201 bool FindGlobalSymbol(std::string name, Location& location);
202
206 void AddSection(std::shared_ptr<Section> section);
207
208 private:
209 void _AddSection(std::shared_ptr<Section> section);
210
211 public:
215 const std::vector<std::shared_ptr<Section>>& Sections() const;
216
220 void DeleteSection(size_t index);
221
225 void RemoveSections();
226
230 std::shared_ptr<Section> FindSection(std::string name);
231
235 std::shared_ptr<Section> FetchSection(std::string name, unsigned default_flags);
236
241
245 void Append(std::shared_ptr<Section> dst, std::shared_ptr<Section> src);
246
250 void Append(Module& other);
251
255 void AllocateSymbols(std::string default_section_name = ".comm");
256 };
257}
258
259#endif /* MODULE_H */
Represents a symbol to be exported from the module.
Definition symbol_name.h:119
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:19
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:821
const std::vector< SymbolName > & GetImportedSymbols() const
Retrieves list of all imported symbols.
Definition module.cc:724
bool AddUndefinedSymbol(std::string symbol_name)
Processes undefined symbol for extended syntax.
Definition module.cc:504
bool AddGlobalSymbol(std::string name, Location location)
Adds and processes exported symbol for extended syntax.
Definition module.cc:235
std::vector< SymbolDefinition > symbol_sequence
Sorted collection of all symbols appearing in the module, duplicates removed.
Definition module.h:65
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:855
bool is_included
Set to true if module is included in the linking process, relevant for libraries.
Definition module.h:92
const std::vector< std::shared_ptr< Section > > & Sections() const
Retrieves list of all sections.
Definition module.cc:804
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:827
bool AddLocalSymbol(std::string name, Location location)
Adds an internal symbol.
Definition module.cc:203
const std::map< ExportedSymbolName, Location > & GetExportedSymbols() const
Retrieves map of all exported symbols and their locations.
Definition module.cc:729
void AddSection(std::shared_ptr< Section > section)
Adds a new section.
Definition module.cc:756
bool FindGlobalSymbol(std::string name, Location &location)
Searches for a global or weak symbol.
Definition module.cc:743
bool AddWeakSymbol(std::string name, Location location)
Adds a weakly bound symbol.
Definition module.cc:322
void RemoveSections()
Removes all sections from internal list, without deleting them.
Definition module.cc:815
void ResolveLocalRelocations()
Attempts to resolve the local targets of all relocations.
Definition module.cc:838
void AddImportedSymbol(SymbolName name)
Adds an imported symbol (Microsoft format: library name + symbol name + ordinal/hint)
Definition module.cc:491
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:934
cpu_type
Supported CPU types.
Definition module.h:38
bool FindLocalSymbol(std::string name, Location &location)
Searches for a local symbol.
Definition module.cc:734
void AddExportedSymbol(ExportedSymbolName name, Location symbol)
Adds an exported symbol (Microsoft format: library name + symbol name + ordinal/hint)
Definition module.cc:499
void DeleteSection(size_t index)
Deletes a specific sections.
Definition module.cc:809
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:453
std::vector< Relocation > relocations
List of relocations within the module.
Definition module.h:89
void AddRelocation(Relocation relocation)
Adds and processes relocation for extended syntax.
Definition module.cc:580
bool AddCommonSymbol(SymbolDefinition symbol)
Adds a common symbol.
Definition module.cc:384
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