1 | /* |
---|
2 | * Copyright (c) 2011, Chris Johns <chrisj@rtems.org> |
---|
3 | * |
---|
4 | * Permission to use, copy, modify, and/or distribute this software for any |
---|
5 | * purpose with or without fee is hereby granted, provided that the above |
---|
6 | * copyright notice and this permission notice appear in all copies. |
---|
7 | * |
---|
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
15 | */ |
---|
16 | /** |
---|
17 | * @file |
---|
18 | * |
---|
19 | * @ingroup rtems-ld |
---|
20 | * |
---|
21 | * @brief RTEMS Linker symbols manages the symbols from all the object files. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #if !defined (_RLD_SYMBOLS_H_) |
---|
26 | #define _RLD_SYMBOLS_H_ |
---|
27 | |
---|
28 | #include <iostream> |
---|
29 | #include <list> |
---|
30 | #include <map> |
---|
31 | #include <string> |
---|
32 | |
---|
33 | #include <rld-elf-types.h> |
---|
34 | |
---|
35 | namespace rld |
---|
36 | { |
---|
37 | /** |
---|
38 | * Forward declarations. |
---|
39 | */ |
---|
40 | namespace files |
---|
41 | { |
---|
42 | class object; |
---|
43 | } |
---|
44 | |
---|
45 | namespace symbols |
---|
46 | { |
---|
47 | /** |
---|
48 | * A symbol. |
---|
49 | */ |
---|
50 | class symbol |
---|
51 | { |
---|
52 | public: |
---|
53 | /** |
---|
54 | * Default constructor. No symbol has been defined. |
---|
55 | */ |
---|
56 | symbol (); |
---|
57 | |
---|
58 | /** |
---|
59 | * Construct an exported symbol with a object file. |
---|
60 | */ |
---|
61 | symbol (int index, |
---|
62 | const std::string& name, |
---|
63 | files::object& object, |
---|
64 | const elf::elf_sym& esym); |
---|
65 | |
---|
66 | /** |
---|
67 | * Construct a symbol with no object file and an ELF index. |
---|
68 | */ |
---|
69 | symbol (int index, const std::string& name, const elf::elf_sym& esym); |
---|
70 | |
---|
71 | /** |
---|
72 | * Construct a linker symbol that is internally created. |
---|
73 | */ |
---|
74 | symbol (const std::string& name, |
---|
75 | const elf::elf_addr value = 0); |
---|
76 | |
---|
77 | /** |
---|
78 | * Construct a linker symbol that is internally created. |
---|
79 | */ |
---|
80 | symbol (const char* name, |
---|
81 | elf::elf_addr value = 0); |
---|
82 | |
---|
83 | /** |
---|
84 | * The symbol's index in the symtab section of the ELF file. |
---|
85 | */ |
---|
86 | int index () const; |
---|
87 | |
---|
88 | /** |
---|
89 | * The symbol's name. |
---|
90 | */ |
---|
91 | const std::string& name () const; |
---|
92 | |
---|
93 | /** |
---|
94 | * The symbol's demangled name. |
---|
95 | */ |
---|
96 | const std::string& demangled () const; |
---|
97 | |
---|
98 | /** |
---|
99 | * Is the symbol a C++ name ? |
---|
100 | */ |
---|
101 | bool is_cplusplus () const; |
---|
102 | |
---|
103 | /** |
---|
104 | * Is the symbol binding is local ? |
---|
105 | */ |
---|
106 | bool is_local () const; |
---|
107 | |
---|
108 | /** |
---|
109 | * Is the symbol binding weak ? |
---|
110 | */ |
---|
111 | bool is_weak () const; |
---|
112 | |
---|
113 | /** |
---|
114 | * Is the symbol binding global ? |
---|
115 | */ |
---|
116 | bool is_global () const; |
---|
117 | |
---|
118 | /** |
---|
119 | * The symbol's type. |
---|
120 | */ |
---|
121 | int type () const; |
---|
122 | |
---|
123 | /** |
---|
124 | * The symbol's binding, ie local, weak, or global. |
---|
125 | */ |
---|
126 | int binding () const; |
---|
127 | |
---|
128 | /** |
---|
129 | * The symbol's section index. |
---|
130 | */ |
---|
131 | int section_index () const; |
---|
132 | |
---|
133 | /** |
---|
134 | * The value of the symbol. |
---|
135 | */ |
---|
136 | elf::elf_addr value () const; |
---|
137 | |
---|
138 | /** |
---|
139 | * The data of the symbol. |
---|
140 | */ |
---|
141 | uint32_t info () const; |
---|
142 | |
---|
143 | /** |
---|
144 | * The symbol's object file name. |
---|
145 | */ |
---|
146 | files::object* object () const; |
---|
147 | |
---|
148 | /** |
---|
149 | * Set the symbol's object file name. Used when resolving unresolved |
---|
150 | * symbols. |
---|
151 | */ |
---|
152 | void set_object (files::object& obj); |
---|
153 | |
---|
154 | /** |
---|
155 | * The ELF symbol. |
---|
156 | */ |
---|
157 | const elf::elf_sym& esym () const; |
---|
158 | |
---|
159 | /** |
---|
160 | * Return the number of references. |
---|
161 | */ |
---|
162 | int references () const { |
---|
163 | return references_; |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * Return the number of references. |
---|
168 | */ |
---|
169 | void referenced (); |
---|
170 | |
---|
171 | /** |
---|
172 | * Less than operator for the map container. |
---|
173 | */ |
---|
174 | bool operator< (const symbol& rhs) const; |
---|
175 | |
---|
176 | /** |
---|
177 | * Output to the a stream. |
---|
178 | */ |
---|
179 | void output (std::ostream& out) const; |
---|
180 | |
---|
181 | private: |
---|
182 | |
---|
183 | int index_; //< The symbol's index in the ELF file. |
---|
184 | std::string name_; //< The name of the symbol. |
---|
185 | std::string demangled_; //< If a C++ symbol the demangled name. |
---|
186 | files::object* object_; //< The object file containing the symbol. |
---|
187 | elf::elf_sym esym_; //< The ELF symbol. |
---|
188 | int references_; //< The number of times if it referenced. |
---|
189 | }; |
---|
190 | |
---|
191 | /** |
---|
192 | * Container of symbols. A bucket of symbols. |
---|
193 | */ |
---|
194 | typedef std::list < symbol > bucket; |
---|
195 | |
---|
196 | /** |
---|
197 | * References to symbols. Should always point to symbols held in a bucket. |
---|
198 | */ |
---|
199 | typedef std::list < symbol* > pointers; |
---|
200 | |
---|
201 | /** |
---|
202 | * A symbols table is a map container of symbols. Should always point to |
---|
203 | * symbols held in a bucket. |
---|
204 | */ |
---|
205 | typedef std::map < std::string, symbol* > symtab; |
---|
206 | |
---|
207 | /** |
---|
208 | * A symbols contains a symbol table of global, weak and local symbols. |
---|
209 | */ |
---|
210 | class table |
---|
211 | { |
---|
212 | public: |
---|
213 | /** |
---|
214 | * Construct a table. |
---|
215 | */ |
---|
216 | table (); |
---|
217 | |
---|
218 | /** |
---|
219 | * Destruct a table. |
---|
220 | */ |
---|
221 | ~table (); |
---|
222 | |
---|
223 | /** |
---|
224 | * Add a global symbol. |
---|
225 | */ |
---|
226 | void add_global (symbol& sym); |
---|
227 | |
---|
228 | /** |
---|
229 | * Add a weak symbol. |
---|
230 | */ |
---|
231 | void add_weak (symbol& sym); |
---|
232 | |
---|
233 | /** |
---|
234 | * Add a local symbol. |
---|
235 | */ |
---|
236 | void add_local (symbol& sym); |
---|
237 | |
---|
238 | /** |
---|
239 | * Find a global symbol. |
---|
240 | */ |
---|
241 | symbol* find_global (const std::string& name); |
---|
242 | |
---|
243 | /** |
---|
244 | * Find an weak symbol. |
---|
245 | */ |
---|
246 | symbol* find_weak (const std::string& name); |
---|
247 | |
---|
248 | /** |
---|
249 | * Find an local symbol. |
---|
250 | */ |
---|
251 | symbol* find_local (const std::string& name); |
---|
252 | |
---|
253 | /** |
---|
254 | * Return the size of the symbols loaded. |
---|
255 | */ |
---|
256 | size_t size () const; |
---|
257 | |
---|
258 | /** |
---|
259 | * Return the globals symbol table. |
---|
260 | */ |
---|
261 | const symtab& globals () const; |
---|
262 | |
---|
263 | /** |
---|
264 | * Return the weaks symbol table. |
---|
265 | */ |
---|
266 | const symtab& weaks () const; |
---|
267 | |
---|
268 | /** |
---|
269 | * Return the locals symbol table. |
---|
270 | */ |
---|
271 | const symtab& locals () const; |
---|
272 | |
---|
273 | private: |
---|
274 | |
---|
275 | /** |
---|
276 | * Cannot copy a table. |
---|
277 | */ |
---|
278 | table (const table& orig); |
---|
279 | |
---|
280 | /** |
---|
281 | * A table of global symbols. |
---|
282 | */ |
---|
283 | symtab globals_; |
---|
284 | |
---|
285 | /** |
---|
286 | * A table of weak symbols. |
---|
287 | */ |
---|
288 | symtab weaks_; |
---|
289 | |
---|
290 | /** |
---|
291 | * A table of local symbols. |
---|
292 | */ |
---|
293 | symtab locals_; |
---|
294 | }; |
---|
295 | |
---|
296 | /** |
---|
297 | * Load a table from a buckey. |
---|
298 | */ |
---|
299 | void load (bucket& bucket_, table& table_); |
---|
300 | |
---|
301 | /** |
---|
302 | * Load a table from a buckey. |
---|
303 | */ |
---|
304 | void load (bucket& bucket_, symtab& table_); |
---|
305 | |
---|
306 | /** |
---|
307 | * Given a container of symbols return how many are referenced. |
---|
308 | */ |
---|
309 | size_t referenced (pointers& symbols); |
---|
310 | |
---|
311 | /** |
---|
312 | * Output the symbol table. |
---|
313 | */ |
---|
314 | void output (std::ostream& out, const table& symbols); |
---|
315 | |
---|
316 | /** |
---|
317 | * Output the symbol table. |
---|
318 | */ |
---|
319 | void output (std::ostream& out, const symtab& symbols); |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | * Output stream operator. |
---|
325 | */ |
---|
326 | static inline std::ostream& operator<< (std::ostream& out, |
---|
327 | const rld::symbols::symbol& sym) { |
---|
328 | sym.output (out); |
---|
329 | return out; |
---|
330 | } |
---|
331 | |
---|
332 | #endif |
---|