source: rtems-tools/rtemstoolkit/rld-symbols.h @ 2e97351

4.10
Last change on this file since 2e97351 was 2e97351, checked in by Chris Johns <chrisj@…>, on Apr 3, 2016 at 5:38:14 AM

rtemstoolkit: Add an address table of symbols key by the symbol address.

This is useful if you need to look up a symbol based on its address.

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