[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 | { |
---|
[2e97351] | 47 | /** |
---|
| 48 | * Use a local type for the address. |
---|
| 49 | */ |
---|
| 50 | typedef elf::elf_addr address; |
---|
| 51 | |
---|
[ec24a37] | 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 | */ |
---|
[90d8d43] | 66 | symbol (int index, |
---|
| 67 | const std::string& name, |
---|
[977c3de] | 68 | files::object& object, |
---|
[ec24a37] | 69 | const elf::elf_sym& esym); |
---|
| 70 | |
---|
| 71 | /** |
---|
[90d8d43] | 72 | * Construct a symbol with no object file and an ELF index. |
---|
[ec24a37] | 73 | */ |
---|
[90d8d43] | 74 | symbol (int index, const std::string& name, const elf::elf_sym& esym); |
---|
[977c3de] | 75 | |
---|
[ec24a37] | 76 | /** |
---|
| 77 | * Construct a linker symbol that is internally created. |
---|
| 78 | */ |
---|
| 79 | symbol (const std::string& name, |
---|
[43ec8b0] | 80 | const elf::elf_addr value = 0); |
---|
[977c3de] | 81 | |
---|
[ec24a37] | 82 | /** |
---|
| 83 | * Construct a linker symbol that is internally created. |
---|
| 84 | */ |
---|
[977c3de] | 85 | symbol (const char* name, |
---|
| 86 | elf::elf_addr value = 0); |
---|
| 87 | |
---|
[90d8d43] | 88 | /** |
---|
| 89 | * The symbol's index in the symtab section of the ELF file. |
---|
| 90 | */ |
---|
| 91 | int index () const; |
---|
| 92 | |
---|
[ec24a37] | 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 | |
---|
[fdb1fe6] | 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 | |
---|
[977c3de] | 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 | /** |
---|
[90d8d43] | 134 | * The symbol's section index. |
---|
[977c3de] | 135 | */ |
---|
[90d8d43] | 136 | int section_index () const; |
---|
[977c3de] | 137 | |
---|
[ea6f8d4] | 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 | |
---|
[ec24a37] | 148 | /** |
---|
| 149 | * The symbol's object file name. |
---|
| 150 | */ |
---|
[977c3de] | 151 | files::object* object () const; |
---|
[ec24a37] | 152 | |
---|
| 153 | /** |
---|
| 154 | * Set the symbol's object file name. Used when resolving unresolved |
---|
| 155 | * symbols. |
---|
| 156 | */ |
---|
[977c3de] | 157 | void set_object (files::object& obj); |
---|
[ec24a37] | 158 | |
---|
| 159 | /** |
---|
| 160 | * The ELF symbol. |
---|
| 161 | */ |
---|
[977c3de] | 162 | const elf::elf_sym& esym () const; |
---|
[ec24a37] | 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: |
---|
[977c3de] | 187 | |
---|
[90d8d43] | 188 | int index_; //< The symbol's index in the ELF file. |
---|
[977c3de] | 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. |
---|
[ec24a37] | 194 | }; |
---|
| 195 | |
---|
| 196 | /** |
---|
[977c3de] | 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. |
---|
[ec24a37] | 209 | */ |
---|
[544de91] | 210 | typedef std::map < std::string, symbol* > symtab; |
---|
| 211 | |
---|
[2e97351] | 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 | |
---|
[544de91] | 218 | /** |
---|
[fdb1fe6] | 219 | * A symbols contains a symbol table of global, weak and local symbols. |
---|
[544de91] | 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 | /** |
---|
[fdb1fe6] | 235 | * Add a global symbol. |
---|
[544de91] | 236 | */ |
---|
[fdb1fe6] | 237 | void add_global (symbol& sym); |
---|
[544de91] | 238 | |
---|
| 239 | /** |
---|
| 240 | * Add a weak symbol. |
---|
| 241 | */ |
---|
| 242 | void add_weak (symbol& sym); |
---|
| 243 | |
---|
| 244 | /** |
---|
[fdb1fe6] | 245 | * Add a local symbol. |
---|
[544de91] | 246 | */ |
---|
[fdb1fe6] | 247 | void add_local (symbol& sym); |
---|
| 248 | |
---|
| 249 | /** |
---|
| 250 | * Find a global symbol. |
---|
| 251 | */ |
---|
| 252 | symbol* find_global (const std::string& name); |
---|
[544de91] | 253 | |
---|
| 254 | /** |
---|
| 255 | * Find an weak symbol. |
---|
| 256 | */ |
---|
| 257 | symbol* find_weak (const std::string& name); |
---|
| 258 | |
---|
[fdb1fe6] | 259 | /** |
---|
| 260 | * Find an local symbol. |
---|
| 261 | */ |
---|
| 262 | symbol* find_local (const std::string& name); |
---|
| 263 | |
---|
[544de91] | 264 | /** |
---|
| 265 | * Return the size of the symbols loaded. |
---|
| 266 | */ |
---|
| 267 | size_t size () const; |
---|
| 268 | |
---|
| 269 | /** |
---|
[fdb1fe6] | 270 | * Return the globals symbol table. |
---|
[544de91] | 271 | */ |
---|
[fdb1fe6] | 272 | const symtab& globals () const; |
---|
[544de91] | 273 | |
---|
| 274 | /** |
---|
| 275 | * Return the weaks symbol table. |
---|
| 276 | */ |
---|
| 277 | const symtab& weaks () const; |
---|
| 278 | |
---|
[fdb1fe6] | 279 | /** |
---|
| 280 | * Return the locals symbol table. |
---|
| 281 | */ |
---|
| 282 | const symtab& locals () const; |
---|
| 283 | |
---|
[2e97351] | 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 | |
---|
[544de91] | 299 | private: |
---|
| 300 | |
---|
| 301 | /** |
---|
| 302 | * Cannot copy a table. |
---|
| 303 | */ |
---|
| 304 | table (const table& orig); |
---|
| 305 | |
---|
| 306 | /** |
---|
[fdb1fe6] | 307 | * A table of global symbols. |
---|
[544de91] | 308 | */ |
---|
[fdb1fe6] | 309 | symtab globals_; |
---|
[544de91] | 310 | |
---|
| 311 | /** |
---|
| 312 | * A table of weak symbols. |
---|
| 313 | */ |
---|
[fdb1fe6] | 314 | symtab weaks_; |
---|
| 315 | |
---|
| 316 | /** |
---|
| 317 | * A table of local symbols. |
---|
| 318 | */ |
---|
| 319 | symtab locals_; |
---|
[544de91] | 320 | }; |
---|
[ec24a37] | 321 | |
---|
| 322 | /** |
---|
[977c3de] | 323 | * Load a table from a buckey. |
---|
[ec24a37] | 324 | */ |
---|
[977c3de] | 325 | void load (bucket& bucket_, table& table_); |
---|
[ec24a37] | 326 | |
---|
[544de91] | 327 | /** |
---|
| 328 | * Load a table from a buckey. |
---|
| 329 | */ |
---|
| 330 | void load (bucket& bucket_, symtab& table_); |
---|
| 331 | |
---|
[ec24a37] | 332 | /** |
---|
[977c3de] | 333 | * Given a container of symbols return how many are referenced. |
---|
[ec24a37] | 334 | */ |
---|
[977c3de] | 335 | size_t referenced (pointers& symbols); |
---|
[ec24a37] | 336 | |
---|
| 337 | /** |
---|
| 338 | * Output the symbol table. |
---|
| 339 | */ |
---|
| 340 | void output (std::ostream& out, const table& symbols); |
---|
[544de91] | 341 | |
---|
| 342 | /** |
---|
| 343 | * Output the symbol table. |
---|
| 344 | */ |
---|
| 345 | void output (std::ostream& out, const symtab& symbols); |
---|
[ec24a37] | 346 | } |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | /** |
---|
| 350 | * Output stream operator. |
---|
| 351 | */ |
---|
[977c3de] | 352 | static inline std::ostream& operator<< (std::ostream& out, |
---|
| 353 | const rld::symbols::symbol& sym) { |
---|
[ec24a37] | 354 | sym.output (out); |
---|
| 355 | return out; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | #endif |
---|