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