RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
symbol_name.h
1#ifndef SYMBOL_NAME_H
2#define SYMBOL_NAME_H
3
4#include <iostream>
5#include <optional>
6#include <string>
7#include "../common.h"
8
9namespace Linker
10{
18 {
19 protected:
20 std::optional<std::string> library;
21 /* internal symbols: empty, imported symbols: refers to the library (NE, LE, PE) */
22 std::optional<std::string> name;
23 /* symbols imported by ordinal: empty (NE, LE, PE), other symbols: the name of the symbol */
24 std::optional<uint16_t> hint;
25 /* symbols imported not by ordinal: empty (NE, LE), except PE where it is a hint, other symbols: ordinal */
26
27 public:
29 offset_t addend = 0;
30
34 SymbolName(std::string name)
35 : name(name)
36 {
37 }
38
42 SymbolName(std::string library, std::string name)
43 : library(library), name(name)
44 {
45 }
46
52 SymbolName(std::string library, std::string name, uint16_t hint)
53 : library(library), name(name), hint(hint)
54 {
55 }
56
62 SymbolName(std::string library, uint16_t ordinal)
63 : library(library), hint(ordinal)
64 {
65 }
66
67 struct LibraryMark { };
68 static LibraryMark IsLibrary;
69
73 SymbolName(std::string library, LibraryMark is_library)
74 : library(library)
75 {
76 (void) is_library;
77 }
78
82 bool LoadName(std::string& result) const;
83
87 bool LoadLibraryName(std::string& result) const;
88
92 bool LoadOrdinalOrHint(uint16_t& result) const;
93
97 bool GetLocalName(std::string& result) const;
98
102 bool GetImportedName(std::string& result_library, std::string& result_name) const;
103
107 bool GetImportedName(std::string& result_library, std::string& result_name, uint16_t& result_hint) const;
108
112 bool GetImportedOrdinal(std::string& result_library, uint16_t& result_ordinal) const;
113
117 bool GetImportedLibrary(std::string& result_library) const;
118
122 bool operator ==(const SymbolName& other) const;
123
127 bool operator !=(const SymbolName& other) const;
128
133 };
134
138 std::ostream& operator<<(std::ostream& out, const SymbolName& symbol);
139
144 {
145 protected:
146 bool by_ordinal;
147 std::string name;
148 std::optional<uint16_t> ordinal;
149
150 public:
154 ExportedSymbolName(std::string name)
155 : by_ordinal(false), name(name), ordinal()
156 {
157 }
158
164 ExportedSymbolName(std::string name, uint16_t hint)
165 : by_ordinal(false), name(name), ordinal(hint)
166 {
167 }
168
172 ExportedSymbolName(uint16_t ordinal, std::string internal_name)
173 : by_ordinal(true), name(internal_name), ordinal(ordinal)
174 {
175 }
176
177 bool IsExportedByOrdinal() const;
178
182 bool LoadName(std::string& result) const;
183
187 bool LoadOrdinalOrHint(uint16_t& result) const;
188
192 bool GetExportedByName(std::string& result) const;
193
197 bool GetExportedByName(std::string& result, uint16_t& hint) const;
198
202 bool GetExportedByOrdinal(uint16_t& result) const;
203
207 bool GetExportedByOrdinal(uint16_t& result, std::string& result_name) const;
208
212 bool operator ==(const ExportedSymbolName& other) const;
213
217 bool operator !=(const ExportedSymbolName& other) const;
218
222 bool operator <(const ExportedSymbolName& other) const;
223
227 bool operator >=(const ExportedSymbolName& other) const;
228
232 bool operator >(const ExportedSymbolName& other) const;
233
237 bool operator <=(const ExportedSymbolName& other) const;
238 };
239
243 std::ostream& operator<<(std::ostream& out, const ExportedSymbolName& symbol);
244}
245
246#endif /* SYMBOL_NAME_H */
Represents a symbol to be exported from the module.
Definition symbol_name.h:144
bool operator<(const ExportedSymbolName &other) const
Compares two symbols for ordering.
Definition symbol_name.cc:258
bool operator!=(const ExportedSymbolName &other) const
Compares two symbols for inequality.
Definition symbol_name.cc:253
ExportedSymbolName(std::string name, uint16_t hint)
Creates a symbol exported by name, with a corresponding hint.
Definition symbol_name.h:164
bool operator<=(const ExportedSymbolName &other) const
Compares two symbols for ordering.
Definition symbol_name.cc:276
bool operator==(const ExportedSymbolName &other) const
Compares two symbols for equality.
Definition symbol_name.cc:248
bool operator>(const ExportedSymbolName &other) const
Compares two symbols for ordering.
Definition symbol_name.cc:271
bool GetExportedByName(std::string &result) const
For symbols exported by name, returns the name.
Definition symbol_name.cc:189
bool LoadName(std::string &result) const
Returns the name of the symbol.
Definition symbol_name.cc:170
ExportedSymbolName(std::string name)
Creates a symbol exported by name.
Definition symbol_name.h:154
ExportedSymbolName(uint16_t ordinal, std::string internal_name)
Creates a symbol exported by ordinal, with an associated internal name.
Definition symbol_name.h:172
bool GetExportedByOrdinal(uint16_t &result) const
For symbols exported by ordinal, returns the ordinal.
Definition symbol_name.cc:219
bool LoadOrdinalOrHint(uint16_t &result) const
Returns the hint or ordinal of the symbol.
Definition symbol_name.cc:176
bool operator>=(const ExportedSymbolName &other) const
Compares two symbols for ordering.
Definition symbol_name.cc:266
Represents an (imported or internal) symbol name, which can be more complex than a string.
Definition symbol_name.h:18
bool GetImportedOrdinal(std::string &result_library, uint16_t &result_ordinal) const
For symbols imported by ordinal, returns the library and ordinal.
Definition symbol_name.cc:92
bool operator!=(const SymbolName &other) const
Compares two symbols for inequality.
Definition symbol_name.cc:124
SymbolName(std::string library, uint16_t ordinal)
Creates a symbol imported via ordinal, from a library.
Definition symbol_name.h:62
SymbolName(std::string name)
Creates an internal symbol with a name.
Definition symbol_name.h:34
bool GetLocalName(std::string &result) const
For local symbols, returns the name.
Definition symbol_name.cc:45
SymbolName(std::string library, LibraryMark is_library)
Creates a symbol referencing the base of an imported library.
Definition symbol_name.h:73
bool LoadName(std::string &result) const
Retrieves the name of the symbol, if it has one.
Definition symbol_name.cc:6
SymbolName(std::string library, std::string name)
Creates a symbol imported via name, from a library.
Definition symbol_name.h:42
offset_t addend
Optional value to be added to the symbol location (TODO: not yet fully implemented)
Definition symbol_name.h:29
SymbolName(std::string library, std::string name, uint16_t hint)
Creates a symbol imported via name and a hint, from a library.
Definition symbol_name.h:52
bool LoadOrdinalOrHint(uint16_t &result) const
Retrieves the ordinal of symbols imported by ordinal, or the hint for imported symbols with a hint.
Definition symbol_name.cc:32
static SymbolName GOT
Symbol representing the global offset table.
Definition symbol_name.h:132
bool LoadLibraryName(std::string &result) const
Retrieves the name of the library, if it is imported.
Definition symbol_name.cc:19
bool GetImportedName(std::string &result_library, std::string &result_name) const
For symbols imported by name, returns the library and name.
Definition symbol_name.cc:60
bool GetImportedLibrary(std::string &result_library) const
For symbols that are identified by the library name and the offset, stored in the addend.
Definition symbol_name.cc:106
bool operator==(const SymbolName &other) const
Compares two symbols for equality.
Definition symbol_name.cc:119
Definition symbol_name.h:67