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 | * C++ demangler. |
---|
49 | */ |
---|
50 | bool is_cplusplus (const std::string& name); |
---|
51 | bool demangle_name (const std::string& name, std::string& demangled); |
---|
52 | |
---|
53 | /** |
---|
54 | * Use a local type for the address. |
---|
55 | */ |
---|
56 | typedef elf::elf_addr address; |
---|
57 | |
---|
58 | /** |
---|
59 | * A symbol. |
---|
60 | */ |
---|
61 | class symbol |
---|
62 | { |
---|
63 | public: |
---|
64 | /** |
---|
65 | * Default constructor. No symbol has been defined. |
---|
66 | */ |
---|
67 | symbol (); |
---|
68 | |
---|
69 | /** |
---|
70 | * Construct an exported symbol with a object file. |
---|
71 | */ |
---|
72 | symbol (int index, |
---|
73 | const std::string& name, |
---|
74 | files::object& object, |
---|
75 | const elf::elf_sym& esym); |
---|
76 | |
---|
77 | /** |
---|
78 | * Construct a symbol with no object file and an ELF index. |
---|
79 | */ |
---|
80 | symbol (int index, const std::string& name, const elf::elf_sym& esym); |
---|
81 | |
---|
82 | /** |
---|
83 | * Construct a linker symbol that is internally created. |
---|
84 | */ |
---|
85 | symbol (const std::string& name, |
---|
86 | const elf::elf_addr value = 0); |
---|
87 | |
---|
88 | /** |
---|
89 | * Construct a linker symbol that is internally created. |
---|
90 | */ |
---|
91 | symbol (const char* name, |
---|
92 | elf::elf_addr value = 0); |
---|
93 | |
---|
94 | /** |
---|
95 | * The symbol's index in the symtab section of the ELF file. |
---|
96 | */ |
---|
97 | int index () const; |
---|
98 | |
---|
99 | /** |
---|
100 | * The symbol's name. |
---|
101 | */ |
---|
102 | const std::string& name () const; |
---|
103 | |
---|
104 | /** |
---|
105 | * The symbol's demangled name. |
---|
106 | */ |
---|
107 | const std::string& demangled () const; |
---|
108 | |
---|
109 | /** |
---|
110 | * Is the symbol a C++ name ? |
---|
111 | */ |
---|
112 | bool is_cplusplus () const; |
---|
113 | |
---|
114 | /** |
---|
115 | * Is the symbol binding is local ? |
---|
116 | */ |
---|
117 | bool is_local () const; |
---|
118 | |
---|
119 | /** |
---|
120 | * Is the symbol binding weak ? |
---|
121 | */ |
---|
122 | bool is_weak () const; |
---|
123 | |
---|
124 | /** |
---|
125 | * Is the symbol binding global ? |
---|
126 | */ |
---|
127 | bool is_global () const; |
---|
128 | |
---|
129 | /** |
---|
130 | * The symbol's type. |
---|
131 | */ |
---|
132 | int type () const; |
---|
133 | |
---|
134 | /** |
---|
135 | * The symbol's binding, ie local, weak, or global. |
---|
136 | */ |
---|
137 | int binding () const; |
---|
138 | |
---|
139 | /** |
---|
140 | * The symbol's section index. |
---|
141 | */ |
---|
142 | int section_index () const; |
---|
143 | |
---|
144 | /** |
---|
145 | * The value of the symbol. |
---|
146 | */ |
---|
147 | elf::elf_addr value () const; |
---|
148 | |
---|
149 | /** |
---|
150 | * The data of the symbol. |
---|
151 | */ |
---|
152 | uint32_t info () const; |
---|
153 | |
---|
154 | /** |
---|
155 | * The symbol's object file name. |
---|
156 | */ |
---|
157 | files::object* object () const; |
---|
158 | |
---|
159 | /** |
---|
160 | * Set the symbol's object file name. Used when resolving unresolved |
---|
161 | * symbols. |
---|
162 | */ |
---|
163 | void set_object (files::object& obj); |
---|
164 | |
---|
165 | /** |
---|
166 | * The ELF symbol. |
---|
167 | */ |
---|
168 | const elf::elf_sym& esym () const; |
---|
169 | |
---|
170 | /** |
---|
171 | * Return the number of references. |
---|
172 | */ |
---|
173 | int references () const { |
---|
174 | return references_; |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Return the number of references. |
---|
179 | */ |
---|
180 | void referenced (); |
---|
181 | |
---|
182 | /** |
---|
183 | * Less than operator for the map container. |
---|
184 | */ |
---|
185 | bool operator< (const symbol& rhs) const; |
---|
186 | |
---|
187 | /** |
---|
188 | * Output to the a stream. |
---|
189 | */ |
---|
190 | void output (std::ostream& out) const; |
---|
191 | |
---|
192 | private: |
---|
193 | |
---|
194 | int index_; //< The symbol's index in the ELF file. |
---|
195 | std::string name_; //< The name of the symbol. |
---|
196 | std::string demangled_; //< If a C++ symbol the demangled name. |
---|
197 | files::object* object_; //< The object file containing the symbol. |
---|
198 | elf::elf_sym esym_; //< The ELF symbol. |
---|
199 | int references_; //< The number of times if it referenced. |
---|
200 | }; |
---|
201 | |
---|
202 | /** |
---|
203 | * Container of symbols. A bucket of symbols. |
---|
204 | */ |
---|
205 | typedef std::list < symbol > bucket; |
---|
206 | |
---|
207 | /** |
---|
208 | * References to symbols. Should always point to symbols held in a bucket. |
---|
209 | */ |
---|
210 | typedef std::list < symbol* > pointers; |
---|
211 | |
---|
212 | /** |
---|
213 | * A symbols table is a map container of symbols. Should always point to |
---|
214 | * symbols held in a bucket. |
---|
215 | */ |
---|
216 | typedef std::map < std::string, symbol* > symtab; |
---|
217 | |
---|
218 | /** |
---|
219 | * An address table is a map container of symbols based on address. Should |
---|
220 | * always point to symbols held in a bucket. |
---|
221 | */ |
---|
222 | typedef std::map < address, symbol* > addrtab; |
---|
223 | |
---|
224 | /** |
---|
225 | * A symbols contains a symbol table of global, weak and local symbols. |
---|
226 | */ |
---|
227 | class table |
---|
228 | { |
---|
229 | public: |
---|
230 | /** |
---|
231 | * Construct a table. |
---|
232 | */ |
---|
233 | table (); |
---|
234 | |
---|
235 | /** |
---|
236 | * Destruct a table. |
---|
237 | */ |
---|
238 | ~table (); |
---|
239 | |
---|
240 | /** |
---|
241 | * Add a global symbol. |
---|
242 | */ |
---|
243 | void add_global (symbol& sym); |
---|
244 | |
---|
245 | /** |
---|
246 | * Add a weak symbol. |
---|
247 | */ |
---|
248 | void add_weak (symbol& sym); |
---|
249 | |
---|
250 | /** |
---|
251 | * Add a local symbol. |
---|
252 | */ |
---|
253 | void add_local (symbol& sym); |
---|
254 | |
---|
255 | /** |
---|
256 | * Find a global symbol. |
---|
257 | */ |
---|
258 | symbol* find_global (const std::string& name); |
---|
259 | |
---|
260 | /** |
---|
261 | * Find an weak symbol. |
---|
262 | */ |
---|
263 | symbol* find_weak (const std::string& name); |
---|
264 | |
---|
265 | /** |
---|
266 | * Find an local symbol. |
---|
267 | */ |
---|
268 | symbol* find_local (const std::string& name); |
---|
269 | |
---|
270 | /** |
---|
271 | * Return the size of the symbols loaded. |
---|
272 | */ |
---|
273 | size_t size () const; |
---|
274 | |
---|
275 | /** |
---|
276 | * Return the globals symbol table. |
---|
277 | */ |
---|
278 | const symtab& globals () const; |
---|
279 | |
---|
280 | /** |
---|
281 | * Return the weaks symbol table. |
---|
282 | */ |
---|
283 | const symtab& weaks () const; |
---|
284 | |
---|
285 | /** |
---|
286 | * Return the locals symbol table. |
---|
287 | */ |
---|
288 | const symtab& locals () const; |
---|
289 | |
---|
290 | /** |
---|
291 | * Return the globals symbol table keyed by address. |
---|
292 | */ |
---|
293 | void globals (addrtab& addresses); |
---|
294 | |
---|
295 | /** |
---|
296 | * Return the weak symbol table keyed by address. |
---|
297 | */ |
---|
298 | void weaks (addrtab& addresses); |
---|
299 | |
---|
300 | /** |
---|
301 | * Return the local symbol table keyed by address. |
---|
302 | */ |
---|
303 | void locals (addrtab& addresses); |
---|
304 | |
---|
305 | private: |
---|
306 | |
---|
307 | /** |
---|
308 | * Cannot copy a table. |
---|
309 | */ |
---|
310 | table (const table& orig); |
---|
311 | |
---|
312 | /** |
---|
313 | * A table of global symbols. |
---|
314 | */ |
---|
315 | symtab globals_; |
---|
316 | |
---|
317 | /** |
---|
318 | * A table of weak symbols. |
---|
319 | */ |
---|
320 | symtab weaks_; |
---|
321 | |
---|
322 | /** |
---|
323 | * A table of local symbols. |
---|
324 | */ |
---|
325 | symtab locals_; |
---|
326 | }; |
---|
327 | |
---|
328 | /** |
---|
329 | * Load a table from a buckey. |
---|
330 | */ |
---|
331 | void load (bucket& bucket_, table& table_); |
---|
332 | |
---|
333 | /** |
---|
334 | * Load a table from a buckey. |
---|
335 | */ |
---|
336 | void load (bucket& bucket_, symtab& table_); |
---|
337 | |
---|
338 | /** |
---|
339 | * Given a container of symbols return how many are referenced. |
---|
340 | */ |
---|
341 | size_t referenced (pointers& symbols); |
---|
342 | |
---|
343 | /** |
---|
344 | * Output the symbol table. |
---|
345 | */ |
---|
346 | void output (std::ostream& out, const table& symbols); |
---|
347 | |
---|
348 | /** |
---|
349 | * Output the symbol table. |
---|
350 | */ |
---|
351 | void output (std::ostream& out, const symtab& symbols); |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | /** |
---|
356 | * Output stream operator. |
---|
357 | */ |
---|
358 | static inline std::ostream& operator<< (std::ostream& out, |
---|
359 | const rld::symbols::symbol& sym) { |
---|
360 | sym.output (out); |
---|
361 | return out; |
---|
362 | } |
---|
363 | |
---|
364 | #endif |
---|