source: rtems-tools/linkers/rld-symbols.h @ 9b66527

4.104.115
Last change on this file since 9b66527 was 977c3de, checked in by Chris Johns <chrisj@…>, on 11/17/12 at 06:34:33

Refactor the ELF support to allow ELF write suppport.

The refactoring allows better reuse of the ELF support and cleans up
some hacks from the generic file and archive handling improving the
separation of the file handling from the file format, ie ELF. The
handling of ELF object files and ELF object files inside archives
is cleaner.

The refactor cleaned up the symbol handling where the symbols now
reside in the ELF file object and references are take in symbol
pointer containers and symbol table containers.

The main purpose of the refactor is to allow support for creating
and writing ELF files.

Also added an rtems-syms command where special symbol support
can be added.

  • Property mode set to 100644
File size: 4.6 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     * 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 (const std::string&  name,
62              files::object&      object,
63              const elf::elf_sym& esym);
64
65      /**
66       * Construct an unresolved symbol with no object file.
67       */
68      symbol (const std::string& name, const elf::elf_sym& esym);
69
70      /**
71       * Construct a linker symbol that is internally created.
72       */
73      symbol (const std::string&  name,
74              const elf::elf_addr value);
75
76      /**
77       * Construct a linker symbol that is internally created.
78       */
79      symbol (const char*   name,
80              elf::elf_addr value = 0);
81
82      /**
83       * The symbol's name.
84       */
85      const std::string& name () const;
86
87      /**
88       * The symbol's demangled name.
89       */
90      const std::string& demangled () const;
91
92      /**
93       * Is the symbol a C++ name ?
94       */
95      bool is_cplusplus () const;
96
97      /**
98       * The symbol's type.
99       */
100      int type () const;
101
102      /**
103       * The symbol's binding, ie local, weak, or global.
104       */
105      int binding () const;
106
107      /**
108       * The synbol's section index.
109       */
110      int index () const;
111
112      /**
113       * The symbol's object file name.
114       */
115      files::object* object () const;
116
117      /**
118       * Set the symbol's object file name. Used when resolving unresolved
119       * symbols.
120       */
121      void set_object (files::object& obj);
122
123      /**
124       * The ELF symbol.
125       */
126      const elf::elf_sym& esym () const;
127
128      /**
129       * Return the number of references.
130       */
131      int references () const {
132        return references_;
133      }
134
135      /**
136       * Return the number of references.
137       */
138      void referenced ();
139
140      /**
141       * Less than operator for the map container.
142       */
143      bool operator< (const symbol& rhs) const;
144
145      /**
146       * Output to the a stream.
147       */
148      void output (std::ostream& out) const;
149
150    private:
151
152      std::string    name_;       //< The name of the symbol.
153      std::string    demangled_;  //< If a C++ symbol the demangled name.
154      files::object* object_;     //< The object file containing the symbol.
155      elf::elf_sym   esym_;       //< The ELF symbol.
156      int            references_; //< The number of times if it referenced.
157    };
158
159    /**
160     * Container of symbols. A bucket of symbols.
161     */
162    typedef std::list < symbol > bucket;
163
164    /**
165     * References to symbols. Should always point to symbols held in a bucket.
166     */
167    typedef std::list < symbol* > pointers;
168
169    /**
170     * A symbols table is a map container of symbols. Should always point to
171     * symbols held in a bucket.
172     */
173    typedef std::map < std::string, symbol* > table;
174
175    /**
176     * Load a table from a buckey.
177     */
178    void load (bucket& bucket_, table& table_);
179
180    /**
181     * Given a container of symbols return how many are referenced.
182     */
183    size_t referenced (pointers& symbols);
184
185    /**
186     * Output the symbol table.
187     */
188    void output (std::ostream& out, const table& symbols);
189  }
190}
191
192/**
193 * Output stream operator.
194 */
195static inline std::ostream& operator<< (std::ostream&               out,
196                                        const rld::symbols::symbol& sym) {
197  sym.output (out);
198  return out;
199}
200
201#endif
Note: See TracBrowser for help on using the repository browser.