[ec24a37] | 1 | /* |
---|
[977c3de] | 2 | * Copyright (c) 2011, Chris Johns <chrisj@rtems.org> |
---|
[ec24a37] | 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. |
---|
[977c3de] | 7 | * |
---|
[ec24a37] | 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 | */ |
---|
[90d8d43] | 61 | symbol (int index, |
---|
| 62 | const std::string& name, |
---|
[977c3de] | 63 | files::object& object, |
---|
[ec24a37] | 64 | const elf::elf_sym& esym); |
---|
| 65 | |
---|
| 66 | /** |
---|
[90d8d43] | 67 | * Construct a symbol with no object file and an ELF index. |
---|
[ec24a37] | 68 | */ |
---|
[90d8d43] | 69 | symbol (int index, const std::string& name, const elf::elf_sym& esym); |
---|
[977c3de] | 70 | |
---|
[ec24a37] | 71 | /** |
---|
| 72 | * Construct a linker symbol that is internally created. |
---|
| 73 | */ |
---|
| 74 | symbol (const std::string& name, |
---|
[43ec8b0] | 75 | const elf::elf_addr value = 0); |
---|
[977c3de] | 76 | |
---|
[ec24a37] | 77 | /** |
---|
| 78 | * Construct a linker symbol that is internally created. |
---|
| 79 | */ |
---|
[977c3de] | 80 | symbol (const char* name, |
---|
| 81 | elf::elf_addr value = 0); |
---|
| 82 | |
---|
[90d8d43] | 83 | /** |
---|
| 84 | * The symbol's index in the symtab section of the ELF file. |
---|
| 85 | */ |
---|
| 86 | int index () const; |
---|
| 87 | |
---|
[ec24a37] | 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 | |
---|
[977c3de] | 103 | /** |
---|
| 104 | * The symbol's type. |
---|
| 105 | */ |
---|
| 106 | int type () const; |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * The symbol's binding, ie local, weak, or global. |
---|
| 110 | */ |
---|
| 111 | int binding () const; |
---|
| 112 | |
---|
| 113 | /** |
---|
[90d8d43] | 114 | * The symbol's section index. |
---|
[977c3de] | 115 | */ |
---|
[90d8d43] | 116 | int section_index () const; |
---|
[977c3de] | 117 | |
---|
[ea6f8d4] | 118 | /** |
---|
| 119 | * The value of the symbol. |
---|
| 120 | */ |
---|
| 121 | elf::elf_addr value () const; |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * The data of the symbol. |
---|
| 125 | */ |
---|
| 126 | uint32_t info () const; |
---|
| 127 | |
---|
[ec24a37] | 128 | /** |
---|
| 129 | * The symbol's object file name. |
---|
| 130 | */ |
---|
[977c3de] | 131 | files::object* object () const; |
---|
[ec24a37] | 132 | |
---|
| 133 | /** |
---|
| 134 | * Set the symbol's object file name. Used when resolving unresolved |
---|
| 135 | * symbols. |
---|
| 136 | */ |
---|
[977c3de] | 137 | void set_object (files::object& obj); |
---|
[ec24a37] | 138 | |
---|
| 139 | /** |
---|
| 140 | * The ELF symbol. |
---|
| 141 | */ |
---|
[977c3de] | 142 | const elf::elf_sym& esym () const; |
---|
[ec24a37] | 143 | |
---|
| 144 | /** |
---|
| 145 | * Return the number of references. |
---|
| 146 | */ |
---|
| 147 | int references () const { |
---|
| 148 | return references_; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * Return the number of references. |
---|
| 153 | */ |
---|
| 154 | void referenced (); |
---|
| 155 | |
---|
| 156 | /** |
---|
| 157 | * Less than operator for the map container. |
---|
| 158 | */ |
---|
| 159 | bool operator< (const symbol& rhs) const; |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * Output to the a stream. |
---|
| 163 | */ |
---|
| 164 | void output (std::ostream& out) const; |
---|
| 165 | |
---|
| 166 | private: |
---|
[977c3de] | 167 | |
---|
[90d8d43] | 168 | int index_; //< The symbol's index in the ELF file. |
---|
[977c3de] | 169 | std::string name_; //< The name of the symbol. |
---|
| 170 | std::string demangled_; //< If a C++ symbol the demangled name. |
---|
| 171 | files::object* object_; //< The object file containing the symbol. |
---|
| 172 | elf::elf_sym esym_; //< The ELF symbol. |
---|
| 173 | int references_; //< The number of times if it referenced. |
---|
[ec24a37] | 174 | }; |
---|
| 175 | |
---|
| 176 | /** |
---|
[977c3de] | 177 | * Container of symbols. A bucket of symbols. |
---|
| 178 | */ |
---|
| 179 | typedef std::list < symbol > bucket; |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * References to symbols. Should always point to symbols held in a bucket. |
---|
| 183 | */ |
---|
| 184 | typedef std::list < symbol* > pointers; |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * A symbols table is a map container of symbols. Should always point to |
---|
| 188 | * symbols held in a bucket. |
---|
[ec24a37] | 189 | */ |
---|
[544de91] | 190 | typedef std::map < std::string, symbol* > symtab; |
---|
| 191 | |
---|
| 192 | /** |
---|
| 193 | * A symbols contains a symbol table of externals and weak symbols. |
---|
| 194 | */ |
---|
| 195 | class table |
---|
| 196 | { |
---|
| 197 | public: |
---|
| 198 | /** |
---|
| 199 | * Construct a table. |
---|
| 200 | */ |
---|
| 201 | table (); |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * Destruct a table. |
---|
| 205 | */ |
---|
| 206 | ~table (); |
---|
| 207 | |
---|
| 208 | /** |
---|
| 209 | * Add an external symbol. |
---|
| 210 | */ |
---|
| 211 | void add_external (symbol& sym); |
---|
| 212 | |
---|
| 213 | /** |
---|
| 214 | * Add a weak symbol. |
---|
| 215 | */ |
---|
| 216 | void add_weak (symbol& sym); |
---|
| 217 | |
---|
| 218 | /** |
---|
| 219 | * Find an external symbol. |
---|
| 220 | */ |
---|
| 221 | symbol* find_external (const std::string& name); |
---|
| 222 | |
---|
| 223 | /** |
---|
| 224 | * Find an weak symbol. |
---|
| 225 | */ |
---|
| 226 | symbol* find_weak (const std::string& name); |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * Return the size of the symbols loaded. |
---|
| 230 | */ |
---|
| 231 | size_t size () const; |
---|
| 232 | |
---|
| 233 | /** |
---|
| 234 | * Return the externals symbol table. |
---|
| 235 | */ |
---|
| 236 | const symtab& externals () const; |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | * Return the weaks symbol table. |
---|
| 240 | */ |
---|
| 241 | const symtab& weaks () const; |
---|
| 242 | |
---|
| 243 | private: |
---|
| 244 | |
---|
| 245 | /** |
---|
| 246 | * Cannot copy a table. |
---|
| 247 | */ |
---|
| 248 | table (const table& orig); |
---|
| 249 | |
---|
| 250 | /** |
---|
| 251 | * A table of external symbols. |
---|
| 252 | */ |
---|
| 253 | symtab _externals; |
---|
| 254 | |
---|
| 255 | /** |
---|
| 256 | * A table of weak symbols. |
---|
| 257 | */ |
---|
| 258 | symtab _weaks; |
---|
| 259 | }; |
---|
[ec24a37] | 260 | |
---|
| 261 | /** |
---|
[977c3de] | 262 | * Load a table from a buckey. |
---|
[ec24a37] | 263 | */ |
---|
[977c3de] | 264 | void load (bucket& bucket_, table& table_); |
---|
[ec24a37] | 265 | |
---|
[544de91] | 266 | /** |
---|
| 267 | * Load a table from a buckey. |
---|
| 268 | */ |
---|
| 269 | void load (bucket& bucket_, symtab& table_); |
---|
| 270 | |
---|
[ec24a37] | 271 | /** |
---|
[977c3de] | 272 | * Given a container of symbols return how many are referenced. |
---|
[ec24a37] | 273 | */ |
---|
[977c3de] | 274 | size_t referenced (pointers& symbols); |
---|
[ec24a37] | 275 | |
---|
| 276 | /** |
---|
| 277 | * Output the symbol table. |
---|
| 278 | */ |
---|
| 279 | void output (std::ostream& out, const table& symbols); |
---|
[544de91] | 280 | |
---|
| 281 | /** |
---|
| 282 | * Output the symbol table. |
---|
| 283 | */ |
---|
| 284 | void output (std::ostream& out, const symtab& symbols); |
---|
[ec24a37] | 285 | } |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | /** |
---|
| 289 | * Output stream operator. |
---|
| 290 | */ |
---|
[977c3de] | 291 | static inline std::ostream& operator<< (std::ostream& out, |
---|
| 292 | const rld::symbols::symbol& sym) { |
---|
[ec24a37] | 293 | sym.output (out); |
---|
| 294 | return out; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | #endif |
---|