[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 | { |
---|
[6c94148] | 47 | /** |
---|
| 48 | * C++ demangler. |
---|
| 49 | */ |
---|
| 50 | bool is_cplusplus (const std::string& name); |
---|
[78bbe4c] | 51 | bool demangle_name (const std::string& name, std::string& demangled); |
---|
[6c94148] | 52 | |
---|
[2e97351] | 53 | /** |
---|
| 54 | * Use a local type for the address. |
---|
| 55 | */ |
---|
| 56 | typedef elf::elf_addr address; |
---|
| 57 | |
---|
[ec24a37] | 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 | */ |
---|
[90d8d43] | 72 | symbol (int index, |
---|
| 73 | const std::string& name, |
---|
[977c3de] | 74 | files::object& object, |
---|
[ec24a37] | 75 | const elf::elf_sym& esym); |
---|
| 76 | |
---|
| 77 | /** |
---|
[90d8d43] | 78 | * Construct a symbol with no object file and an ELF index. |
---|
[ec24a37] | 79 | */ |
---|
[90d8d43] | 80 | symbol (int index, const std::string& name, const elf::elf_sym& esym); |
---|
[977c3de] | 81 | |
---|
[ec24a37] | 82 | /** |
---|
| 83 | * Construct a linker symbol that is internally created. |
---|
| 84 | */ |
---|
| 85 | symbol (const std::string& name, |
---|
[43ec8b0] | 86 | const elf::elf_addr value = 0); |
---|
[977c3de] | 87 | |
---|
[ec24a37] | 88 | /** |
---|
| 89 | * Construct a linker symbol that is internally created. |
---|
| 90 | */ |
---|
[977c3de] | 91 | symbol (const char* name, |
---|
| 92 | elf::elf_addr value = 0); |
---|
| 93 | |
---|
[90d8d43] | 94 | /** |
---|
| 95 | * The symbol's index in the symtab section of the ELF file. |
---|
| 96 | */ |
---|
| 97 | int index () const; |
---|
| 98 | |
---|
[ec24a37] | 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 | |
---|
[fdb1fe6] | 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 | |
---|
[977c3de] | 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 | /** |
---|
[90d8d43] | 140 | * The symbol's section index. |
---|
[977c3de] | 141 | */ |
---|
[90d8d43] | 142 | int section_index () const; |
---|
[977c3de] | 143 | |
---|
[ea6f8d4] | 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 | |
---|
[ec24a37] | 154 | /** |
---|
| 155 | * The symbol's object file name. |
---|
| 156 | */ |
---|
[977c3de] | 157 | files::object* object () const; |
---|
[ec24a37] | 158 | |
---|
| 159 | /** |
---|
| 160 | * Set the symbol's object file name. Used when resolving unresolved |
---|
| 161 | * symbols. |
---|
| 162 | */ |
---|
[977c3de] | 163 | void set_object (files::object& obj); |
---|
[ec24a37] | 164 | |
---|
| 165 | /** |
---|
| 166 | * The ELF symbol. |
---|
| 167 | */ |
---|
[977c3de] | 168 | const elf::elf_sym& esym () const; |
---|
[ec24a37] | 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: |
---|
[977c3de] | 193 | |
---|
[90d8d43] | 194 | int index_; //< The symbol's index in the ELF file. |
---|
[977c3de] | 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. |
---|
[ec24a37] | 200 | }; |
---|
| 201 | |
---|
| 202 | /** |
---|
[977c3de] | 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. |
---|
[ec24a37] | 215 | */ |
---|
[544de91] | 216 | typedef std::map < std::string, symbol* > symtab; |
---|
| 217 | |
---|
[2e97351] | 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 | |
---|
[544de91] | 224 | /** |
---|
[fdb1fe6] | 225 | * A symbols contains a symbol table of global, weak and local symbols. |
---|
[544de91] | 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 | /** |
---|
[fdb1fe6] | 241 | * Add a global symbol. |
---|
[544de91] | 242 | */ |
---|
[fdb1fe6] | 243 | void add_global (symbol& sym); |
---|
[544de91] | 244 | |
---|
| 245 | /** |
---|
| 246 | * Add a weak symbol. |
---|
| 247 | */ |
---|
| 248 | void add_weak (symbol& sym); |
---|
| 249 | |
---|
| 250 | /** |
---|
[fdb1fe6] | 251 | * Add a local symbol. |
---|
[544de91] | 252 | */ |
---|
[fdb1fe6] | 253 | void add_local (symbol& sym); |
---|
| 254 | |
---|
| 255 | /** |
---|
| 256 | * Find a global symbol. |
---|
| 257 | */ |
---|
| 258 | symbol* find_global (const std::string& name); |
---|
[544de91] | 259 | |
---|
| 260 | /** |
---|
| 261 | * Find an weak symbol. |
---|
| 262 | */ |
---|
| 263 | symbol* find_weak (const std::string& name); |
---|
| 264 | |
---|
[fdb1fe6] | 265 | /** |
---|
| 266 | * Find an local symbol. |
---|
| 267 | */ |
---|
| 268 | symbol* find_local (const std::string& name); |
---|
| 269 | |
---|
[544de91] | 270 | /** |
---|
| 271 | * Return the size of the symbols loaded. |
---|
| 272 | */ |
---|
| 273 | size_t size () const; |
---|
| 274 | |
---|
| 275 | /** |
---|
[fdb1fe6] | 276 | * Return the globals symbol table. |
---|
[544de91] | 277 | */ |
---|
[fdb1fe6] | 278 | const symtab& globals () const; |
---|
[544de91] | 279 | |
---|
| 280 | /** |
---|
| 281 | * Return the weaks symbol table. |
---|
| 282 | */ |
---|
| 283 | const symtab& weaks () const; |
---|
| 284 | |
---|
[fdb1fe6] | 285 | /** |
---|
| 286 | * Return the locals symbol table. |
---|
| 287 | */ |
---|
| 288 | const symtab& locals () const; |
---|
| 289 | |
---|
[2e97351] | 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 | |
---|
[544de91] | 305 | private: |
---|
| 306 | |
---|
| 307 | /** |
---|
| 308 | * Cannot copy a table. |
---|
| 309 | */ |
---|
| 310 | table (const table& orig); |
---|
| 311 | |
---|
| 312 | /** |
---|
[fdb1fe6] | 313 | * A table of global symbols. |
---|
[544de91] | 314 | */ |
---|
[fdb1fe6] | 315 | symtab globals_; |
---|
[544de91] | 316 | |
---|
| 317 | /** |
---|
| 318 | * A table of weak symbols. |
---|
| 319 | */ |
---|
[fdb1fe6] | 320 | symtab weaks_; |
---|
| 321 | |
---|
| 322 | /** |
---|
| 323 | * A table of local symbols. |
---|
| 324 | */ |
---|
| 325 | symtab locals_; |
---|
[544de91] | 326 | }; |
---|
[ec24a37] | 327 | |
---|
| 328 | /** |
---|
[977c3de] | 329 | * Load a table from a buckey. |
---|
[ec24a37] | 330 | */ |
---|
[977c3de] | 331 | void load (bucket& bucket_, table& table_); |
---|
[ec24a37] | 332 | |
---|
[544de91] | 333 | /** |
---|
| 334 | * Load a table from a buckey. |
---|
| 335 | */ |
---|
| 336 | void load (bucket& bucket_, symtab& table_); |
---|
| 337 | |
---|
[ec24a37] | 338 | /** |
---|
[977c3de] | 339 | * Given a container of symbols return how many are referenced. |
---|
[ec24a37] | 340 | */ |
---|
[977c3de] | 341 | size_t referenced (pointers& symbols); |
---|
[ec24a37] | 342 | |
---|
| 343 | /** |
---|
| 344 | * Output the symbol table. |
---|
| 345 | */ |
---|
| 346 | void output (std::ostream& out, const table& symbols); |
---|
[544de91] | 347 | |
---|
| 348 | /** |
---|
| 349 | * Output the symbol table. |
---|
| 350 | */ |
---|
| 351 | void output (std::ostream& out, const symtab& symbols); |
---|
[ec24a37] | 352 | } |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | /** |
---|
| 356 | * Output stream operator. |
---|
| 357 | */ |
---|
[977c3de] | 358 | static inline std::ostream& operator<< (std::ostream& out, |
---|
| 359 | const rld::symbols::symbol& sym) { |
---|
[ec24a37] | 360 | sym.output (out); |
---|
| 361 | return out; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | #endif |
---|