| 1 | // symtab.cc -- the gold symbol table |
| 2 | |
| 3 | #include "gold.h" |
| 4 | |
| 5 | #include <stdint.h> |
| 6 | #include <string> |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "object.h" |
| 10 | #include "dynobj.h" |
| 11 | #include "output.h" |
| 12 | #include "target.h" |
| 13 | #include "workqueue.h" |
| 14 | #include "symtab.h" |
| 15 | |
| 16 | namespace gold |
| 17 | { |
| 18 | |
| 19 | // Class Symbol. |
| 20 | |
| 21 | // Initialize fields in Symbol. This initializes everything except u_ |
| 22 | // and source_. |
| 23 | |
| 24 | void |
| 25 | Symbol::init_fields(const char* name, const char* version, |
| 26 | elfcpp::STT type, elfcpp::STB binding, |
| 27 | elfcpp::STV visibility, unsigned char nonvis) |
| 28 | { |
| 29 | this->name_ = name; |
| 30 | this->version_ = version; |
| 31 | this->symtab_index_ = 0; |
| 32 | this->dynsym_index_ = 0; |
| 33 | this->got_offset_ = 0; |
| 34 | this->type_ = type; |
| 35 | this->binding_ = binding; |
| 36 | this->visibility_ = visibility; |
| 37 | this->nonvis_ = nonvis; |
| 38 | this->is_target_special_ = false; |
| 39 | this->is_def_ = false; |
| 40 | this->is_forwarder_ = false; |
| 41 | this->needs_dynsym_entry_ = false; |
| 42 | this->in_reg_ = false; |
| 43 | this->in_dyn_ = false; |
| 44 | this->has_got_offset_ = false; |
| 45 | this->has_warning_ = false; |
| 46 | } |
| 47 | |
| 48 | // Initialize the fields in the base class Symbol for SYM in OBJECT. |
| 49 | |
| 50 | template<int size, bool big_endian> |
| 51 | void |
| 52 | Symbol::init_base(const char* name, const char* version, Object* object, |
| 53 | const elfcpp::Sym<size, big_endian>& sym) |
| 54 | { |
| 55 | this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(), |
| 56 | sym.get_st_visibility(), sym.get_st_nonvis()); |
| 57 | this->u_.from_object.object = object; |
| 58 | // FIXME: Handle SHN_XINDEX. |
| 59 | this->u_.from_object.shndx = sym.get_st_shndx(); |
| 60 | this->source_ = FROM_OBJECT; |
| 61 | this->in_reg_ = !object->is_dynamic(); |
| 62 | this->in_dyn_ = object->is_dynamic(); |
| 63 | } |
| 64 | |
| 65 | // Initialize the fields in the base class Symbol for a symbol defined |
| 66 | // in an Output_data. |
| 67 | |
| 68 | void |
| 69 | Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type, |
| 70 | elfcpp::STB binding, elfcpp::STV visibility, |
| 71 | unsigned char nonvis, bool offset_is_from_end) |
| 72 | { |
| 73 | this->init_fields(name, NULL, type, binding, visibility, nonvis); |
| 74 | this->u_.in_output_data.output_data = od; |
| 75 | this->u_.in_output_data.offset_is_from_end = offset_is_from_end; |
| 76 | this->source_ = IN_OUTPUT_DATA; |
| 77 | this->in_reg_ = true; |
| 78 | } |
| 79 | |
| 80 | // Initialize the fields in the base class Symbol for a symbol defined |
| 81 | // in an Output_segment. |
| 82 | |
| 83 | void |
| 84 | Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type, |
| 85 | elfcpp::STB binding, elfcpp::STV visibility, |
| 86 | unsigned char nonvis, Segment_offset_base offset_base) |
| 87 | { |
| 88 | this->init_fields(name, NULL, type, binding, visibility, nonvis); |
| 89 | this->u_.in_output_segment.output_segment = os; |
| 90 | this->u_.in_output_segment.offset_base = offset_base; |
| 91 | this->source_ = IN_OUTPUT_SEGMENT; |
| 92 | this->in_reg_ = true; |
| 93 | } |
| 94 | |
| 95 | // Initialize the fields in the base class Symbol for a symbol defined |
| 96 | // as a constant. |
| 97 | |
| 98 | void |
| 99 | Symbol::init_base(const char* name, elfcpp::STT type, |
| 100 | elfcpp::STB binding, elfcpp::STV visibility, |
| 101 | unsigned char nonvis) |
| 102 | { |
| 103 | this->init_fields(name, NULL, type, binding, visibility, nonvis); |
| 104 | this->source_ = CONSTANT; |
| 105 | this->in_reg_ = true; |
| 106 | } |
| 107 | |
| 108 | // Initialize the fields in Sized_symbol for SYM in OBJECT. |
| 109 | |
| 110 | template<int size> |
| 111 | template<bool big_endian> |
| 112 | void |
| 113 | Sized_symbol<size>::init(const char* name, const char* version, Object* object, |
| 114 | const elfcpp::Sym<size, big_endian>& sym) |
| 115 | { |
| 116 | this->init_base(name, version, object, sym); |
| 117 | this->value_ = sym.get_st_value(); |
| 118 | this->symsize_ = sym.get_st_size(); |
| 119 | } |
| 120 | |
| 121 | // Initialize the fields in Sized_symbol for a symbol defined in an |
| 122 | // Output_data. |
| 123 | |
| 124 | template<int size> |
| 125 | void |
| 126 | Sized_symbol<size>::init(const char* name, Output_data* od, |
| 127 | Value_type value, Size_type symsize, |
| 128 | elfcpp::STT type, elfcpp::STB binding, |
| 129 | elfcpp::STV visibility, unsigned char nonvis, |
| 130 | bool offset_is_from_end) |
| 131 | { |
| 132 | this->init_base(name, od, type, binding, visibility, nonvis, |
| 133 | offset_is_from_end); |
| 134 | this->value_ = value; |
| 135 | this->symsize_ = symsize; |
| 136 | } |
| 137 | |
| 138 | // Initialize the fields in Sized_symbol for a symbol defined in an |
| 139 | // Output_segment. |
| 140 | |
| 141 | template<int size> |
| 142 | void |
| 143 | Sized_symbol<size>::init(const char* name, Output_segment* os, |
| 144 | Value_type value, Size_type symsize, |
| 145 | elfcpp::STT type, elfcpp::STB binding, |
| 146 | elfcpp::STV visibility, unsigned char nonvis, |
| 147 | Segment_offset_base offset_base) |
| 148 | { |
| 149 | this->init_base(name, os, type, binding, visibility, nonvis, offset_base); |
| 150 | this->value_ = value; |
| 151 | this->symsize_ = symsize; |
| 152 | } |
| 153 | |
| 154 | // Initialize the fields in Sized_symbol for a symbol defined as a |
| 155 | // constant. |
| 156 | |
| 157 | template<int size> |
| 158 | void |
| 159 | Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize, |
| 160 | elfcpp::STT type, elfcpp::STB binding, |
| 161 | elfcpp::STV visibility, unsigned char nonvis) |
| 162 | { |
| 163 | this->init_base(name, type, binding, visibility, nonvis); |
| 164 | this->value_ = value; |
| 165 | this->symsize_ = symsize; |
| 166 | } |
| 167 | |
| 168 | // Class Symbol_table. |
| 169 | |
| 170 | Symbol_table::Symbol_table() |
| 171 | : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(), |
| 172 | forwarders_(), commons_(), warnings_() |
| 173 | { |
| 174 | } |
| 175 | |
| 176 | Symbol_table::~Symbol_table() |
| 177 | { |
| 178 | } |
| 179 | |
| 180 | // The hash function. The key is always canonicalized, so we use a |
| 181 | // simple combination of the pointers. |
| 182 | |
| 183 | size_t |
| 184 | Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const |
| 185 | { |
| 186 | return key.first ^ key.second; |
| 187 | } |
| 188 | |
| 189 | // The symbol table key equality function. This is only called with |
| 190 | // canonicalized name and version strings, so we can use pointer |
| 191 | // comparison. |
| 192 | |
| 193 | bool |
| 194 | Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1, |
| 195 | const Symbol_table_key& k2) const |
| 196 | { |
| 197 | return k1.first == k2.first && k1.second == k2.second; |
| 198 | } |
| 199 | |
| 200 | // Make TO a symbol which forwards to FROM. |
| 201 | |
| 202 | void |
| 203 | Symbol_table::make_forwarder(Symbol* from, Symbol* to) |
| 204 | { |
| 205 | gold_assert(from != to); |
| 206 | gold_assert(!from->is_forwarder() && !to->is_forwarder()); |
| 207 | this->forwarders_[from] = to; |
| 208 | from->set_forwarder(); |
| 209 | } |
| 210 | |
| 211 | // Resolve the forwards from FROM, returning the real symbol. |
| 212 | |
| 213 | Symbol* |
| 214 | Symbol_table::resolve_forwards(const Symbol* from) const |
| 215 | { |
| 216 | gold_assert(from->is_forwarder()); |
| 217 | Unordered_map<const Symbol*, Symbol*>::const_iterator p = |
| 218 | this->forwarders_.find(from); |
| 219 | gold_assert(p != this->forwarders_.end()); |
| 220 | return p->second; |
| 221 | } |
| 222 | |
| 223 | // Look up a symbol by name. |
| 224 | |
| 225 | Symbol* |
| 226 | Symbol_table::lookup(const char* name, const char* version) const |
| 227 | { |
| 228 | Stringpool::Key name_key; |
| 229 | name = this->namepool_.find(name, &name_key); |
| 230 | if (name == NULL) |
| 231 | return NULL; |
| 232 | |
| 233 | Stringpool::Key version_key = 0; |
| 234 | if (version != NULL) |
| 235 | { |
| 236 | version = this->namepool_.find(version, &version_key); |
| 237 | if (version == NULL) |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | Symbol_table_key key(name_key, version_key); |
| 242 | Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key); |
| 243 | if (p == this->table_.end()) |
| 244 | return NULL; |
| 245 | return p->second; |
| 246 | } |
| 247 | |
| 248 | // Resolve a Symbol with another Symbol. This is only used in the |
| 249 | // unusual case where there are references to both an unversioned |
| 250 | // symbol and a symbol with a version, and we then discover that that |
| 251 | // version is the default version. Because this is unusual, we do |
| 252 | // this the slow way, by converting back to an ELF symbol. |
| 253 | |
| 254 | template<int size, bool big_endian> |
| 255 | void |
| 256 | Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from, |
| 257 | const char* version ACCEPT_SIZE_ENDIAN) |
| 258 | { |
| 259 | unsigned char buf[elfcpp::Elf_sizes<size>::sym_size]; |
| 260 | elfcpp::Sym_write<size, big_endian> esym(buf); |
| 261 | // We don't bother to set the st_name field. |
| 262 | esym.put_st_value(from->value()); |
| 263 | esym.put_st_size(from->symsize()); |
| 264 | esym.put_st_info(from->binding(), from->type()); |
| 265 | esym.put_st_other(from->visibility(), from->nonvis()); |
| 266 | esym.put_st_shndx(from->shndx()); |
| 267 | Symbol_table::resolve(to, esym.sym(), from->object(), version); |
| 268 | } |
| 269 | |
| 270 | // Add one symbol from OBJECT to the symbol table. NAME is symbol |
| 271 | // name and VERSION is the version; both are canonicalized. DEF is |
| 272 | // whether this is the default version. |
| 273 | |
| 274 | // If DEF is true, then this is the definition of a default version of |
| 275 | // a symbol. That means that any lookup of NAME/NULL and any lookup |
| 276 | // of NAME/VERSION should always return the same symbol. This is |
| 277 | // obvious for references, but in particular we want to do this for |
| 278 | // definitions: overriding NAME/NULL should also override |
| 279 | // NAME/VERSION. If we don't do that, it would be very hard to |
| 280 | // override functions in a shared library which uses versioning. |
| 281 | |
| 282 | // We implement this by simply making both entries in the hash table |
| 283 | // point to the same Symbol structure. That is easy enough if this is |
| 284 | // the first time we see NAME/NULL or NAME/VERSION, but it is possible |
| 285 | // that we have seen both already, in which case they will both have |
| 286 | // independent entries in the symbol table. We can't simply change |
| 287 | // the symbol table entry, because we have pointers to the entries |
| 288 | // attached to the object files. So we mark the entry attached to the |
| 289 | // object file as a forwarder, and record it in the forwarders_ map. |
| 290 | // Note that entries in the hash table will never be marked as |
| 291 | // forwarders. |
| 292 | |
| 293 | template<int size, bool big_endian> |
| 294 | Symbol* |
| 295 | Symbol_table::add_from_object(Object* object, |
| 296 | const char *name, |
| 297 | Stringpool::Key name_key, |
| 298 | const char *version, |
| 299 | Stringpool::Key version_key, |
| 300 | bool def, |
| 301 | const elfcpp::Sym<size, big_endian>& sym) |
| 302 | { |
| 303 | Symbol* const snull = NULL; |
| 304 | std::pair<typename Symbol_table_type::iterator, bool> ins = |
| 305 | this->table_.insert(std::make_pair(std::make_pair(name_key, version_key), |
| 306 | snull)); |
| 307 | |
| 308 | std::pair<typename Symbol_table_type::iterator, bool> insdef = |
| 309 | std::make_pair(this->table_.end(), false); |
| 310 | if (def) |
| 311 | { |
| 312 | const Stringpool::Key vnull_key = 0; |
| 313 | insdef = this->table_.insert(std::make_pair(std::make_pair(name_key, |
| 314 | vnull_key), |
| 315 | snull)); |
| 316 | } |
| 317 | |
| 318 | // ins.first: an iterator, which is a pointer to a pair. |
| 319 | // ins.first->first: the key (a pair of name and version). |
| 320 | // ins.first->second: the value (Symbol*). |
| 321 | // ins.second: true if new entry was inserted, false if not. |
| 322 | |
| 323 | Sized_symbol<size>* ret; |
| 324 | bool was_undefined; |
| 325 | bool was_common; |
| 326 | if (!ins.second) |
| 327 | { |
| 328 | // We already have an entry for NAME/VERSION. |
| 329 | ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second |
| 330 | SELECT_SIZE(size)); |
| 331 | gold_assert(ret != NULL); |
| 332 | |
| 333 | was_undefined = ret->is_undefined(); |
| 334 | was_common = ret->is_common(); |
| 335 | |
| 336 | Symbol_table::resolve(ret, sym, object, version); |
| 337 | |
| 338 | if (def) |
| 339 | { |
| 340 | if (insdef.second) |
| 341 | { |
| 342 | // This is the first time we have seen NAME/NULL. Make |
| 343 | // NAME/NULL point to NAME/VERSION. |
| 344 | insdef.first->second = ret; |
| 345 | } |
| 346 | else if (insdef.first->second != ret) |
| 347 | { |
| 348 | // This is the unfortunate case where we already have |
| 349 | // entries for both NAME/VERSION and NAME/NULL. |
| 350 | const Sized_symbol<size>* sym2; |
| 351 | sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) ( |
| 352 | insdef.first->second |
| 353 | SELECT_SIZE(size)); |
| 354 | Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
| 355 | ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian)); |
| 356 | this->make_forwarder(insdef.first->second, ret); |
| 357 | insdef.first->second = ret; |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | // This is the first time we have seen NAME/VERSION. |
| 364 | gold_assert(ins.first->second == NULL); |
| 365 | |
| 366 | was_undefined = false; |
| 367 | was_common = false; |
| 368 | |
| 369 | if (def && !insdef.second) |
| 370 | { |
| 371 | // We already have an entry for NAME/NULL. If we override |
| 372 | // it, then change it to NAME/VERSION. |
| 373 | ret = this->get_sized_symbol SELECT_SIZE_NAME(size) ( |
| 374 | insdef.first->second |
| 375 | SELECT_SIZE(size)); |
| 376 | Symbol_table::resolve(ret, sym, object, version); |
| 377 | ins.first->second = ret; |
| 378 | } |
| 379 | else |
| 380 | { |
| 381 | Sized_target<size, big_endian>* target = |
| 382 | object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
| 383 | SELECT_SIZE_ENDIAN_ONLY(size, big_endian)); |
| 384 | if (!target->has_make_symbol()) |
| 385 | ret = new Sized_symbol<size>(); |
| 386 | else |
| 387 | { |
| 388 | ret = target->make_symbol(); |
| 389 | if (ret == NULL) |
| 390 | { |
| 391 | // This means that we don't want a symbol table |
| 392 | // entry after all. |
| 393 | if (!def) |
| 394 | this->table_.erase(ins.first); |
| 395 | else |
| 396 | { |
| 397 | this->table_.erase(insdef.first); |
| 398 | // Inserting insdef invalidated ins. |
| 399 | this->table_.erase(std::make_pair(name_key, |
| 400 | version_key)); |
| 401 | } |
| 402 | return NULL; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | ret->init(name, version, object, sym); |
| 407 | |
| 408 | ins.first->second = ret; |
| 409 | if (def) |
| 410 | { |
| 411 | // This is the first time we have seen NAME/NULL. Point |
| 412 | // it at the new entry for NAME/VERSION. |
| 413 | gold_assert(insdef.second); |
| 414 | insdef.first->second = ret; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Record every time we see a new undefined symbol, to speed up |
| 420 | // archive groups. |
| 421 | if (!was_undefined && ret->is_undefined()) |
| 422 | ++this->saw_undefined_; |
| 423 | |
| 424 | // Keep track of common symbols, to speed up common symbol |
| 425 | // allocation. |
| 426 | if (!was_common && ret->is_common()) |
| 427 | this->commons_.push_back(ret); |
| 428 | |
| 429 | return ret; |
| 430 | } |
| 431 | |
| 432 | // Add all the symbols in a relocatable object to the hash table. |
| 433 | |
| 434 | template<int size, bool big_endian> |
| 435 | void |
| 436 | Symbol_table::add_from_relobj( |
| 437 | Sized_relobj<size, big_endian>* relobj, |
| 438 | const unsigned char* syms, |
| 439 | size_t count, |
| 440 | const char* sym_names, |
| 441 | size_t sym_name_size, |
| 442 | Symbol** sympointers) |
| 443 | { |
| 444 | // We take the size from the first object we see. |
| 445 | if (this->get_size() == 0) |
| 446 | this->set_size(size); |
| 447 | |
| 448 | if (size != this->get_size() || size != relobj->target()->get_size()) |
| 449 | { |
| 450 | fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"), |
| 451 | program_name, relobj->name().c_str()); |
| 452 | gold_exit(false); |
| 453 | } |
| 454 | |
| 455 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 456 | |
| 457 | const unsigned char* p = syms; |
| 458 | for (size_t i = 0; i < count; ++i, p += sym_size) |
| 459 | { |
| 460 | elfcpp::Sym<size, big_endian> sym(p); |
| 461 | elfcpp::Sym<size, big_endian>* psym = &sym; |
| 462 | |
| 463 | unsigned int st_name = psym->get_st_name(); |
| 464 | if (st_name >= sym_name_size) |
| 465 | { |
| 466 | fprintf(stderr, |
| 467 | _("%s: %s: bad global symbol name offset %u at %lu\n"), |
| 468 | program_name, relobj->name().c_str(), st_name, |
| 469 | static_cast<unsigned long>(i)); |
| 470 | gold_exit(false); |
| 471 | } |
| 472 | |
| 473 | const char* name = sym_names + st_name; |
| 474 | |
| 475 | // A symbol defined in a section which we are not including must |
| 476 | // be treated as an undefined symbol. |
| 477 | unsigned char symbuf[sym_size]; |
| 478 | elfcpp::Sym<size, big_endian> sym2(symbuf); |
| 479 | unsigned int st_shndx = psym->get_st_shndx(); |
| 480 | if (st_shndx != elfcpp::SHN_UNDEF |
| 481 | && st_shndx < elfcpp::SHN_LORESERVE |
| 482 | && !relobj->is_section_included(st_shndx)) |
| 483 | { |
| 484 | memcpy(symbuf, p, sym_size); |
| 485 | elfcpp::Sym_write<size, big_endian> sw(symbuf); |
| 486 | sw.put_st_shndx(elfcpp::SHN_UNDEF); |
| 487 | psym = &sym2; |
| 488 | } |
| 489 | |
| 490 | // In an object file, an '@' in the name separates the symbol |
| 491 | // name from the version name. If there are two '@' characters, |
| 492 | // this is the default version. |
| 493 | const char* ver = strchr(name, '@'); |
| 494 | |
| 495 | Symbol* res; |
| 496 | if (ver == NULL) |
| 497 | { |
| 498 | Stringpool::Key name_key; |
| 499 | name = this->namepool_.add(name, &name_key); |
| 500 | res = this->add_from_object(relobj, name, name_key, NULL, 0, |
| 501 | false, *psym); |
| 502 | } |
| 503 | else |
| 504 | { |
| 505 | Stringpool::Key name_key; |
| 506 | name = this->namepool_.add(name, ver - name, &name_key); |
| 507 | |
| 508 | bool def = false; |
| 509 | ++ver; |
| 510 | if (*ver == '@') |
| 511 | { |
| 512 | def = true; |
| 513 | ++ver; |
| 514 | } |
| 515 | |
| 516 | Stringpool::Key ver_key; |
| 517 | ver = this->namepool_.add(ver, &ver_key); |
| 518 | |
| 519 | res = this->add_from_object(relobj, name, name_key, ver, ver_key, |
| 520 | def, *psym); |
| 521 | } |
| 522 | |
| 523 | *sympointers++ = res; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // Add all the symbols in a dynamic object to the hash table. |
| 528 | |
| 529 | template<int size, bool big_endian> |
| 530 | void |
| 531 | Symbol_table::add_from_dynobj( |
| 532 | Sized_dynobj<size, big_endian>* dynobj, |
| 533 | const unsigned char* syms, |
| 534 | size_t count, |
| 535 | const char* sym_names, |
| 536 | size_t sym_name_size, |
| 537 | const unsigned char* versym, |
| 538 | size_t versym_size, |
| 539 | const std::vector<const char*>* version_map) |
| 540 | { |
| 541 | // We take the size from the first object we see. |
| 542 | if (this->get_size() == 0) |
| 543 | this->set_size(size); |
| 544 | |
| 545 | if (size != this->get_size() || size != dynobj->target()->get_size()) |
| 546 | { |
| 547 | fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"), |
| 548 | program_name, dynobj->name().c_str()); |
| 549 | gold_exit(false); |
| 550 | } |
| 551 | |
| 552 | if (versym != NULL && versym_size / 2 < count) |
| 553 | { |
| 554 | fprintf(stderr, _("%s: %s: too few symbol versions\n"), |
| 555 | program_name, dynobj->name().c_str()); |
| 556 | gold_exit(false); |
| 557 | } |
| 558 | |
| 559 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 560 | |
| 561 | const unsigned char* p = syms; |
| 562 | const unsigned char* vs = versym; |
| 563 | for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2) |
| 564 | { |
| 565 | elfcpp::Sym<size, big_endian> sym(p); |
| 566 | |
| 567 | // Ignore symbols with local binding. |
| 568 | if (sym.get_st_bind() == elfcpp::STB_LOCAL) |
| 569 | continue; |
| 570 | |
| 571 | unsigned int st_name = sym.get_st_name(); |
| 572 | if (st_name >= sym_name_size) |
| 573 | { |
| 574 | fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"), |
| 575 | program_name, dynobj->name().c_str(), st_name, |
| 576 | static_cast<unsigned long>(i)); |
| 577 | gold_exit(false); |
| 578 | } |
| 579 | |
| 580 | const char* name = sym_names + st_name; |
| 581 | |
| 582 | if (versym == NULL) |
| 583 | { |
| 584 | Stringpool::Key name_key; |
| 585 | name = this->namepool_.add(name, &name_key); |
| 586 | this->add_from_object(dynobj, name, name_key, NULL, 0, |
| 587 | false, sym); |
| 588 | continue; |
| 589 | } |
| 590 | |
| 591 | // Read the version information. |
| 592 | |
| 593 | unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs); |
| 594 | |
| 595 | bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0; |
| 596 | v &= elfcpp::VERSYM_VERSION; |
| 597 | |
| 598 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)) |
| 599 | { |
| 600 | // This symbol should not be visible outside the object. |
| 601 | continue; |
| 602 | } |
| 603 | |
| 604 | // At this point we are definitely going to add this symbol. |
| 605 | Stringpool::Key name_key; |
| 606 | name = this->namepool_.add(name, &name_key); |
| 607 | |
| 608 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL)) |
| 609 | { |
| 610 | // This symbol does not have a version. |
| 611 | this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym); |
| 612 | continue; |
| 613 | } |
| 614 | |
| 615 | if (v >= version_map->size()) |
| 616 | { |
| 617 | fprintf(stderr, |
| 618 | _("%s: %s: versym for symbol %zu out of range: %u\n"), |
| 619 | program_name, dynobj->name().c_str(), i, v); |
| 620 | gold_exit(false); |
| 621 | } |
| 622 | |
| 623 | const char* version = (*version_map)[v]; |
| 624 | if (version == NULL) |
| 625 | { |
| 626 | fprintf(stderr, _("%s: %s: versym for symbol %zu has no name: %u\n"), |
| 627 | program_name, dynobj->name().c_str(), i, v); |
| 628 | gold_exit(false); |
| 629 | } |
| 630 | |
| 631 | Stringpool::Key version_key; |
| 632 | version = this->namepool_.add(version, &version_key); |
| 633 | |
| 634 | // If this is an absolute symbol, and the version name and |
| 635 | // symbol name are the same, then this is the version definition |
| 636 | // symbol. These symbols exist to support using -u to pull in |
| 637 | // particular versions. We do not want to record a version for |
| 638 | // them. |
| 639 | if (sym.get_st_shndx() == elfcpp::SHN_ABS && name_key == version_key) |
| 640 | { |
| 641 | this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym); |
| 642 | continue; |
| 643 | } |
| 644 | |
| 645 | const bool def = !hidden && sym.get_st_shndx() != elfcpp::SHN_UNDEF; |
| 646 | |
| 647 | this->add_from_object(dynobj, name, name_key, version, version_key, |
| 648 | def, sym); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | // Create and return a specially defined symbol. If ONLY_IF_REF is |
| 653 | // true, then only create the symbol if there is a reference to it. |
| 654 | |
| 655 | template<int size, bool big_endian> |
| 656 | Sized_symbol<size>* |
| 657 | Symbol_table::define_special_symbol(const Target* target, const char* name, |
| 658 | const char* version, bool only_if_ref |
| 659 | ACCEPT_SIZE_ENDIAN) |
| 660 | { |
| 661 | gold_assert(this->size_ == size); |
| 662 | |
| 663 | Symbol* oldsym; |
| 664 | Sized_symbol<size>* sym; |
| 665 | |
| 666 | if (only_if_ref) |
| 667 | { |
| 668 | oldsym = this->lookup(name, version); |
| 669 | if (oldsym == NULL || !oldsym->is_undefined()) |
| 670 | return NULL; |
| 671 | sym = NULL; |
| 672 | |
| 673 | // Canonicalize NAME and VERSION. |
| 674 | name = oldsym->name(); |
| 675 | version = oldsym->version(); |
| 676 | } |
| 677 | else |
| 678 | { |
| 679 | // Canonicalize NAME and VERSION. |
| 680 | Stringpool::Key name_key; |
| 681 | name = this->namepool_.add(name, &name_key); |
| 682 | |
| 683 | Stringpool::Key version_key = 0; |
| 684 | if (version != NULL) |
| 685 | version = this->namepool_.add(version, &version_key); |
| 686 | |
| 687 | Symbol* const snull = NULL; |
| 688 | std::pair<typename Symbol_table_type::iterator, bool> ins = |
| 689 | this->table_.insert(std::make_pair(std::make_pair(name_key, |
| 690 | version_key), |
| 691 | snull)); |
| 692 | |
| 693 | if (!ins.second) |
| 694 | { |
| 695 | // We already have a symbol table entry for NAME/VERSION. |
| 696 | oldsym = ins.first->second; |
| 697 | gold_assert(oldsym != NULL); |
| 698 | sym = NULL; |
| 699 | } |
| 700 | else |
| 701 | { |
| 702 | // We haven't seen this symbol before. |
| 703 | gold_assert(ins.first->second == NULL); |
| 704 | |
| 705 | if (!target->has_make_symbol()) |
| 706 | sym = new Sized_symbol<size>(); |
| 707 | else |
| 708 | { |
| 709 | gold_assert(target->get_size() == size); |
| 710 | gold_assert(target->is_big_endian() ? big_endian : !big_endian); |
| 711 | typedef Sized_target<size, big_endian> My_target; |
| 712 | const My_target* sized_target = |
| 713 | static_cast<const My_target*>(target); |
| 714 | sym = sized_target->make_symbol(); |
| 715 | if (sym == NULL) |
| 716 | return NULL; |
| 717 | } |
| 718 | |
| 719 | ins.first->second = sym; |
| 720 | oldsym = NULL; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | if (oldsym != NULL) |
| 725 | { |
| 726 | gold_assert(sym == NULL); |
| 727 | |
| 728 | sym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym |
| 729 | SELECT_SIZE(size)); |
| 730 | gold_assert(sym->source() == Symbol::FROM_OBJECT); |
| 731 | const int old_shndx = sym->shndx(); |
| 732 | if (old_shndx != elfcpp::SHN_UNDEF |
| 733 | && old_shndx != elfcpp::SHN_COMMON |
| 734 | && !sym->object()->is_dynamic()) |
| 735 | { |
| 736 | fprintf(stderr, "%s: linker defined: multiple definition of %s\n", |
| 737 | program_name, name); |
| 738 | // FIXME: Report old location. Record that we have seen an |
| 739 | // error. |
| 740 | return NULL; |
| 741 | } |
| 742 | |
| 743 | // Our new definition is going to override the old reference. |
| 744 | } |
| 745 | |
| 746 | return sym; |
| 747 | } |
| 748 | |
| 749 | // Define a symbol based on an Output_data. |
| 750 | |
| 751 | Symbol* |
| 752 | Symbol_table::define_in_output_data(const Target* target, const char* name, |
| 753 | const char* version, Output_data* od, |
| 754 | uint64_t value, uint64_t symsize, |
| 755 | elfcpp::STT type, elfcpp::STB binding, |
| 756 | elfcpp::STV visibility, |
| 757 | unsigned char nonvis, |
| 758 | bool offset_is_from_end, |
| 759 | bool only_if_ref) |
| 760 | { |
| 761 | gold_assert(target->get_size() == this->size_); |
| 762 | if (this->size_ == 32) |
| 763 | return this->do_define_in_output_data<32>(target, name, version, od, value, |
| 764 | symsize, type, binding, |
| 765 | visibility, nonvis, |
| 766 | offset_is_from_end, only_if_ref); |
| 767 | else if (this->size_ == 64) |
| 768 | return this->do_define_in_output_data<64>(target, name, version, od, value, |
| 769 | symsize, type, binding, |
| 770 | visibility, nonvis, |
| 771 | offset_is_from_end, only_if_ref); |
| 772 | else |
| 773 | gold_unreachable(); |
| 774 | } |
| 775 | |
| 776 | // Define a symbol in an Output_data, sized version. |
| 777 | |
| 778 | template<int size> |
| 779 | Sized_symbol<size>* |
| 780 | Symbol_table::do_define_in_output_data( |
| 781 | const Target* target, |
| 782 | const char* name, |
| 783 | const char* version, |
| 784 | Output_data* od, |
| 785 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
| 786 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, |
| 787 | elfcpp::STT type, |
| 788 | elfcpp::STB binding, |
| 789 | elfcpp::STV visibility, |
| 790 | unsigned char nonvis, |
| 791 | bool offset_is_from_end, |
| 792 | bool only_if_ref) |
| 793 | { |
| 794 | Sized_symbol<size>* sym; |
| 795 | |
| 796 | if (target->is_big_endian()) |
| 797 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( |
| 798 | target, name, version, only_if_ref |
| 799 | SELECT_SIZE_ENDIAN(size, true)); |
| 800 | else |
| 801 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( |
| 802 | target, name, version, only_if_ref |
| 803 | SELECT_SIZE_ENDIAN(size, false)); |
| 804 | |
| 805 | if (sym == NULL) |
| 806 | return NULL; |
| 807 | |
| 808 | sym->init(name, od, value, symsize, type, binding, visibility, nonvis, |
| 809 | offset_is_from_end); |
| 810 | |
| 811 | return sym; |
| 812 | } |
| 813 | |
| 814 | // Define a symbol based on an Output_segment. |
| 815 | |
| 816 | Symbol* |
| 817 | Symbol_table::define_in_output_segment(const Target* target, const char* name, |
| 818 | const char* version, Output_segment* os, |
| 819 | uint64_t value, uint64_t symsize, |
| 820 | elfcpp::STT type, elfcpp::STB binding, |
| 821 | elfcpp::STV visibility, |
| 822 | unsigned char nonvis, |
| 823 | Symbol::Segment_offset_base offset_base, |
| 824 | bool only_if_ref) |
| 825 | { |
| 826 | gold_assert(target->get_size() == this->size_); |
| 827 | if (this->size_ == 32) |
| 828 | return this->do_define_in_output_segment<32>(target, name, version, os, |
| 829 | value, symsize, type, binding, |
| 830 | visibility, nonvis, |
| 831 | offset_base, only_if_ref); |
| 832 | else if (this->size_ == 64) |
| 833 | return this->do_define_in_output_segment<64>(target, name, version, os, |
| 834 | value, symsize, type, binding, |
| 835 | visibility, nonvis, |
| 836 | offset_base, only_if_ref); |
| 837 | else |
| 838 | gold_unreachable(); |
| 839 | } |
| 840 | |
| 841 | // Define a symbol in an Output_segment, sized version. |
| 842 | |
| 843 | template<int size> |
| 844 | Sized_symbol<size>* |
| 845 | Symbol_table::do_define_in_output_segment( |
| 846 | const Target* target, |
| 847 | const char* name, |
| 848 | const char* version, |
| 849 | Output_segment* os, |
| 850 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
| 851 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, |
| 852 | elfcpp::STT type, |
| 853 | elfcpp::STB binding, |
| 854 | elfcpp::STV visibility, |
| 855 | unsigned char nonvis, |
| 856 | Symbol::Segment_offset_base offset_base, |
| 857 | bool only_if_ref) |
| 858 | { |
| 859 | Sized_symbol<size>* sym; |
| 860 | |
| 861 | if (target->is_big_endian()) |
| 862 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( |
| 863 | target, name, version, only_if_ref |
| 864 | SELECT_SIZE_ENDIAN(size, true)); |
| 865 | else |
| 866 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( |
| 867 | target, name, version, only_if_ref |
| 868 | SELECT_SIZE_ENDIAN(size, false)); |
| 869 | |
| 870 | if (sym == NULL) |
| 871 | return NULL; |
| 872 | |
| 873 | sym->init(name, os, value, symsize, type, binding, visibility, nonvis, |
| 874 | offset_base); |
| 875 | |
| 876 | return sym; |
| 877 | } |
| 878 | |
| 879 | // Define a special symbol with a constant value. It is a multiple |
| 880 | // definition error if this symbol is already defined. |
| 881 | |
| 882 | Symbol* |
| 883 | Symbol_table::define_as_constant(const Target* target, const char* name, |
| 884 | const char* version, uint64_t value, |
| 885 | uint64_t symsize, elfcpp::STT type, |
| 886 | elfcpp::STB binding, elfcpp::STV visibility, |
| 887 | unsigned char nonvis, bool only_if_ref) |
| 888 | { |
| 889 | gold_assert(target->get_size() == this->size_); |
| 890 | if (this->size_ == 32) |
| 891 | return this->do_define_as_constant<32>(target, name, version, value, |
| 892 | symsize, type, binding, visibility, |
| 893 | nonvis, only_if_ref); |
| 894 | else if (this->size_ == 64) |
| 895 | return this->do_define_as_constant<64>(target, name, version, value, |
| 896 | symsize, type, binding, visibility, |
| 897 | nonvis, only_if_ref); |
| 898 | else |
| 899 | gold_unreachable(); |
| 900 | } |
| 901 | |
| 902 | // Define a symbol as a constant, sized version. |
| 903 | |
| 904 | template<int size> |
| 905 | Sized_symbol<size>* |
| 906 | Symbol_table::do_define_as_constant( |
| 907 | const Target* target, |
| 908 | const char* name, |
| 909 | const char* version, |
| 910 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
| 911 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, |
| 912 | elfcpp::STT type, |
| 913 | elfcpp::STB binding, |
| 914 | elfcpp::STV visibility, |
| 915 | unsigned char nonvis, |
| 916 | bool only_if_ref) |
| 917 | { |
| 918 | Sized_symbol<size>* sym; |
| 919 | |
| 920 | if (target->is_big_endian()) |
| 921 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) ( |
| 922 | target, name, version, only_if_ref |
| 923 | SELECT_SIZE_ENDIAN(size, true)); |
| 924 | else |
| 925 | sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) ( |
| 926 | target, name, version, only_if_ref |
| 927 | SELECT_SIZE_ENDIAN(size, false)); |
| 928 | |
| 929 | if (sym == NULL) |
| 930 | return NULL; |
| 931 | |
| 932 | sym->init(name, value, symsize, type, binding, visibility, nonvis); |
| 933 | |
| 934 | return sym; |
| 935 | } |
| 936 | |
| 937 | // Define a set of symbols in output sections. |
| 938 | |
| 939 | void |
| 940 | Symbol_table::define_symbols(const Layout* layout, const Target* target, |
| 941 | int count, const Define_symbol_in_section* p) |
| 942 | { |
| 943 | for (int i = 0; i < count; ++i, ++p) |
| 944 | { |
| 945 | Output_section* os = layout->find_output_section(p->output_section); |
| 946 | if (os != NULL) |
| 947 | this->define_in_output_data(target, p->name, NULL, os, p->value, |
| 948 | p->size, p->type, p->binding, |
| 949 | p->visibility, p->nonvis, |
| 950 | p->offset_is_from_end, p->only_if_ref); |
| 951 | else |
| 952 | this->define_as_constant(target, p->name, NULL, 0, p->size, p->type, |
| 953 | p->binding, p->visibility, p->nonvis, |
| 954 | p->only_if_ref); |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | // Define a set of symbols in output segments. |
| 959 | |
| 960 | void |
| 961 | Symbol_table::define_symbols(const Layout* layout, const Target* target, |
| 962 | int count, const Define_symbol_in_segment* p) |
| 963 | { |
| 964 | for (int i = 0; i < count; ++i, ++p) |
| 965 | { |
| 966 | Output_segment* os = layout->find_output_segment(p->segment_type, |
| 967 | p->segment_flags_set, |
| 968 | p->segment_flags_clear); |
| 969 | if (os != NULL) |
| 970 | this->define_in_output_segment(target, p->name, NULL, os, p->value, |
| 971 | p->size, p->type, p->binding, |
| 972 | p->visibility, p->nonvis, |
| 973 | p->offset_base, p->only_if_ref); |
| 974 | else |
| 975 | this->define_as_constant(target, p->name, NULL, 0, p->size, p->type, |
| 976 | p->binding, p->visibility, p->nonvis, |
| 977 | p->only_if_ref); |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | // Set the dynamic symbol indexes. INDEX is the index of the first |
| 982 | // global dynamic symbol. Pointers to the symbols are stored into the |
| 983 | // vector SYMS. The names are added to DYNPOOL. This returns an |
| 984 | // updated dynamic symbol index. |
| 985 | |
| 986 | unsigned int |
| 987 | Symbol_table::set_dynsym_indexes(const General_options* options, |
| 988 | const Target* target, |
| 989 | unsigned int index, |
| 990 | std::vector<Symbol*>* syms, |
| 991 | Stringpool* dynpool, |
| 992 | Versions* versions) |
| 993 | { |
| 994 | for (Symbol_table_type::iterator p = this->table_.begin(); |
| 995 | p != this->table_.end(); |
| 996 | ++p) |
| 997 | { |
| 998 | Symbol* sym = p->second; |
| 999 | |
| 1000 | // Note that SYM may already have a dynamic symbol index, since |
| 1001 | // some symbols appear more than once in the symbol table, with |
| 1002 | // and without a version. |
| 1003 | |
| 1004 | if (!sym->needs_dynsym_entry()) |
| 1005 | sym->set_dynsym_index(-1U); |
| 1006 | else if (!sym->has_dynsym_index()) |
| 1007 | { |
| 1008 | sym->set_dynsym_index(index); |
| 1009 | ++index; |
| 1010 | syms->push_back(sym); |
| 1011 | dynpool->add(sym->name(), NULL); |
| 1012 | |
| 1013 | // Record any version information. |
| 1014 | if (sym->version() != NULL) |
| 1015 | versions->record_version(options, dynpool, sym); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | // Finish up the versions. In some cases this may add new dynamic |
| 1020 | // symbols. |
| 1021 | index = versions->finalize(target, this, index, syms); |
| 1022 | |
| 1023 | return index; |
| 1024 | } |
| 1025 | |
| 1026 | // Set the final values for all the symbols. The index of the first |
| 1027 | // global symbol in the output file is INDEX. Record the file offset |
| 1028 | // OFF. Add their names to POOL. Return the new file offset. |
| 1029 | |
| 1030 | off_t |
| 1031 | Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff, |
| 1032 | size_t dyn_global_index, size_t dyncount, |
| 1033 | Stringpool* pool) |
| 1034 | { |
| 1035 | off_t ret; |
| 1036 | |
| 1037 | gold_assert(index != 0); |
| 1038 | this->first_global_index_ = index; |
| 1039 | |
| 1040 | this->dynamic_offset_ = dynoff; |
| 1041 | this->first_dynamic_global_index_ = dyn_global_index; |
| 1042 | this->dynamic_count_ = dyncount; |
| 1043 | |
| 1044 | if (this->size_ == 32) |
| 1045 | ret = this->sized_finalize<32>(index, off, pool); |
| 1046 | else if (this->size_ == 64) |
| 1047 | ret = this->sized_finalize<64>(index, off, pool); |
| 1048 | else |
| 1049 | gold_unreachable(); |
| 1050 | |
| 1051 | // Now that we have the final symbol table, we can reliably note |
| 1052 | // which symbols should get warnings. |
| 1053 | this->warnings_.note_warnings(this); |
| 1054 | |
| 1055 | return ret; |
| 1056 | } |
| 1057 | |
| 1058 | // Set the final value for all the symbols. This is called after |
| 1059 | // Layout::finalize, so all the output sections have their final |
| 1060 | // address. |
| 1061 | |
| 1062 | template<int size> |
| 1063 | off_t |
| 1064 | Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool) |
| 1065 | { |
| 1066 | off = align_address(off, size >> 3); |
| 1067 | this->offset_ = off; |
| 1068 | |
| 1069 | size_t orig_index = index; |
| 1070 | |
| 1071 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 1072 | for (Symbol_table_type::iterator p = this->table_.begin(); |
| 1073 | p != this->table_.end(); |
| 1074 | ++p) |
| 1075 | { |
| 1076 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); |
| 1077 | |
| 1078 | // FIXME: Here we need to decide which symbols should go into |
| 1079 | // the output file, based on --strip. |
| 1080 | |
| 1081 | // The default version of a symbol may appear twice in the |
| 1082 | // symbol table. We only need to finalize it once. |
| 1083 | if (sym->has_symtab_index()) |
| 1084 | continue; |
| 1085 | |
| 1086 | if (!sym->in_reg()) |
| 1087 | { |
| 1088 | gold_assert(!sym->has_symtab_index()); |
| 1089 | sym->set_symtab_index(-1U); |
| 1090 | gold_assert(sym->dynsym_index() == -1U); |
| 1091 | continue; |
| 1092 | } |
| 1093 | |
| 1094 | typename Sized_symbol<size>::Value_type value; |
| 1095 | |
| 1096 | switch (sym->source()) |
| 1097 | { |
| 1098 | case Symbol::FROM_OBJECT: |
| 1099 | { |
| 1100 | unsigned int shndx = sym->shndx(); |
| 1101 | |
| 1102 | // FIXME: We need some target specific support here. |
| 1103 | if (shndx >= elfcpp::SHN_LORESERVE |
| 1104 | && shndx != elfcpp::SHN_ABS) |
| 1105 | { |
| 1106 | fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"), |
| 1107 | program_name, sym->name(), shndx); |
| 1108 | gold_exit(false); |
| 1109 | } |
| 1110 | |
| 1111 | Object* symobj = sym->object(); |
| 1112 | if (symobj->is_dynamic()) |
| 1113 | { |
| 1114 | value = 0; |
| 1115 | shndx = elfcpp::SHN_UNDEF; |
| 1116 | } |
| 1117 | else if (shndx == elfcpp::SHN_UNDEF) |
| 1118 | value = 0; |
| 1119 | else if (shndx == elfcpp::SHN_ABS) |
| 1120 | value = sym->value(); |
| 1121 | else |
| 1122 | { |
| 1123 | Relobj* relobj = static_cast<Relobj*>(symobj); |
| 1124 | off_t secoff; |
| 1125 | Output_section* os = relobj->output_section(shndx, &secoff); |
| 1126 | |
| 1127 | if (os == NULL) |
| 1128 | { |
| 1129 | sym->set_symtab_index(-1U); |
| 1130 | gold_assert(sym->dynsym_index() == -1U); |
| 1131 | continue; |
| 1132 | } |
| 1133 | |
| 1134 | value = sym->value() + os->address() + secoff; |
| 1135 | } |
| 1136 | } |
| 1137 | break; |
| 1138 | |
| 1139 | case Symbol::IN_OUTPUT_DATA: |
| 1140 | { |
| 1141 | Output_data* od = sym->output_data(); |
| 1142 | value = sym->value() + od->address(); |
| 1143 | if (sym->offset_is_from_end()) |
| 1144 | value += od->data_size(); |
| 1145 | } |
| 1146 | break; |
| 1147 | |
| 1148 | case Symbol::IN_OUTPUT_SEGMENT: |
| 1149 | { |
| 1150 | Output_segment* os = sym->output_segment(); |
| 1151 | value = sym->value() + os->vaddr(); |
| 1152 | switch (sym->offset_base()) |
| 1153 | { |
| 1154 | case Symbol::SEGMENT_START: |
| 1155 | break; |
| 1156 | case Symbol::SEGMENT_END: |
| 1157 | value += os->memsz(); |
| 1158 | break; |
| 1159 | case Symbol::SEGMENT_BSS: |
| 1160 | value += os->filesz(); |
| 1161 | break; |
| 1162 | default: |
| 1163 | gold_unreachable(); |
| 1164 | } |
| 1165 | } |
| 1166 | break; |
| 1167 | |
| 1168 | case Symbol::CONSTANT: |
| 1169 | value = sym->value(); |
| 1170 | break; |
| 1171 | |
| 1172 | default: |
| 1173 | gold_unreachable(); |
| 1174 | } |
| 1175 | |
| 1176 | sym->set_value(value); |
| 1177 | sym->set_symtab_index(index); |
| 1178 | pool->add(sym->name(), NULL); |
| 1179 | ++index; |
| 1180 | off += sym_size; |
| 1181 | } |
| 1182 | |
| 1183 | this->output_count_ = index - orig_index; |
| 1184 | |
| 1185 | return off; |
| 1186 | } |
| 1187 | |
| 1188 | // Write out the global symbols. |
| 1189 | |
| 1190 | void |
| 1191 | Symbol_table::write_globals(const Target* target, const Stringpool* sympool, |
| 1192 | const Stringpool* dynpool, Output_file* of) const |
| 1193 | { |
| 1194 | if (this->size_ == 32) |
| 1195 | { |
| 1196 | if (target->is_big_endian()) |
| 1197 | this->sized_write_globals<32, true>(target, sympool, dynpool, of); |
| 1198 | else |
| 1199 | this->sized_write_globals<32, false>(target, sympool, dynpool, of); |
| 1200 | } |
| 1201 | else if (this->size_ == 64) |
| 1202 | { |
| 1203 | if (target->is_big_endian()) |
| 1204 | this->sized_write_globals<64, true>(target, sympool, dynpool, of); |
| 1205 | else |
| 1206 | this->sized_write_globals<64, false>(target, sympool, dynpool, of); |
| 1207 | } |
| 1208 | else |
| 1209 | gold_unreachable(); |
| 1210 | } |
| 1211 | |
| 1212 | // Write out the global symbols. |
| 1213 | |
| 1214 | template<int size, bool big_endian> |
| 1215 | void |
| 1216 | Symbol_table::sized_write_globals(const Target*, |
| 1217 | const Stringpool* sympool, |
| 1218 | const Stringpool* dynpool, |
| 1219 | Output_file* of) const |
| 1220 | { |
| 1221 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 1222 | unsigned int index = this->first_global_index_; |
| 1223 | const off_t oview_size = this->output_count_ * sym_size; |
| 1224 | unsigned char* const psyms = of->get_output_view(this->offset_, oview_size); |
| 1225 | |
| 1226 | unsigned int dynamic_count = this->dynamic_count_; |
| 1227 | off_t dynamic_size = dynamic_count * sym_size; |
| 1228 | unsigned int first_dynamic_global_index = this->first_dynamic_global_index_; |
| 1229 | unsigned char* dynamic_view; |
| 1230 | if (this->dynamic_offset_ == 0) |
| 1231 | dynamic_view = NULL; |
| 1232 | else |
| 1233 | dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size); |
| 1234 | |
| 1235 | unsigned char* ps = psyms; |
| 1236 | for (Symbol_table_type::const_iterator p = this->table_.begin(); |
| 1237 | p != this->table_.end(); |
| 1238 | ++p) |
| 1239 | { |
| 1240 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); |
| 1241 | |
| 1242 | unsigned int sym_index = sym->symtab_index(); |
| 1243 | unsigned int dynsym_index; |
| 1244 | if (dynamic_view == NULL) |
| 1245 | dynsym_index = -1U; |
| 1246 | else |
| 1247 | dynsym_index = sym->dynsym_index(); |
| 1248 | |
| 1249 | if (sym_index == -1U && dynsym_index == -1U) |
| 1250 | { |
| 1251 | // This symbol is not included in the output file. |
| 1252 | continue; |
| 1253 | } |
| 1254 | |
| 1255 | if (sym_index == index) |
| 1256 | ++index; |
| 1257 | else if (sym_index != -1U) |
| 1258 | { |
| 1259 | // We have already seen this symbol, because it has a |
| 1260 | // default version. |
| 1261 | gold_assert(sym_index < index); |
| 1262 | if (dynsym_index == -1U) |
| 1263 | continue; |
| 1264 | sym_index = -1U; |
| 1265 | } |
| 1266 | |
| 1267 | unsigned int shndx; |
| 1268 | switch (sym->source()) |
| 1269 | { |
| 1270 | case Symbol::FROM_OBJECT: |
| 1271 | { |
| 1272 | unsigned int in_shndx = sym->shndx(); |
| 1273 | |
| 1274 | // FIXME: We need some target specific support here. |
| 1275 | if (in_shndx >= elfcpp::SHN_LORESERVE |
| 1276 | && in_shndx != elfcpp::SHN_ABS) |
| 1277 | { |
| 1278 | fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"), |
| 1279 | program_name, sym->name(), in_shndx); |
| 1280 | gold_exit(false); |
| 1281 | } |
| 1282 | |
| 1283 | Object* symobj = sym->object(); |
| 1284 | if (symobj->is_dynamic()) |
| 1285 | { |
| 1286 | // FIXME. |
| 1287 | shndx = elfcpp::SHN_UNDEF; |
| 1288 | } |
| 1289 | else if (in_shndx == elfcpp::SHN_UNDEF |
| 1290 | || in_shndx == elfcpp::SHN_ABS) |
| 1291 | shndx = in_shndx; |
| 1292 | else |
| 1293 | { |
| 1294 | Relobj* relobj = static_cast<Relobj*>(symobj); |
| 1295 | off_t secoff; |
| 1296 | Output_section* os = relobj->output_section(in_shndx, &secoff); |
| 1297 | gold_assert(os != NULL); |
| 1298 | shndx = os->out_shndx(); |
| 1299 | } |
| 1300 | } |
| 1301 | break; |
| 1302 | |
| 1303 | case Symbol::IN_OUTPUT_DATA: |
| 1304 | shndx = sym->output_data()->out_shndx(); |
| 1305 | break; |
| 1306 | |
| 1307 | case Symbol::IN_OUTPUT_SEGMENT: |
| 1308 | shndx = elfcpp::SHN_ABS; |
| 1309 | break; |
| 1310 | |
| 1311 | case Symbol::CONSTANT: |
| 1312 | shndx = elfcpp::SHN_ABS; |
| 1313 | break; |
| 1314 | |
| 1315 | default: |
| 1316 | gold_unreachable(); |
| 1317 | } |
| 1318 | |
| 1319 | if (sym_index != -1U) |
| 1320 | { |
| 1321 | this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
| 1322 | sym, shndx, sympool, ps |
| 1323 | SELECT_SIZE_ENDIAN(size, big_endian)); |
| 1324 | ps += sym_size; |
| 1325 | } |
| 1326 | |
| 1327 | if (dynsym_index != -1U) |
| 1328 | { |
| 1329 | dynsym_index -= first_dynamic_global_index; |
| 1330 | gold_assert(dynsym_index < dynamic_count); |
| 1331 | unsigned char* pd = dynamic_view + (dynsym_index * sym_size); |
| 1332 | this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
| 1333 | sym, shndx, dynpool, pd |
| 1334 | SELECT_SIZE_ENDIAN(size, big_endian)); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | gold_assert(ps - psyms == oview_size); |
| 1339 | |
| 1340 | of->write_output_view(this->offset_, oview_size, psyms); |
| 1341 | if (dynamic_view != NULL) |
| 1342 | of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view); |
| 1343 | } |
| 1344 | |
| 1345 | // Write out the symbol SYM, in section SHNDX, to P. POOL is the |
| 1346 | // strtab holding the name. |
| 1347 | |
| 1348 | template<int size, bool big_endian> |
| 1349 | void |
| 1350 | Symbol_table::sized_write_symbol(Sized_symbol<size>* sym, |
| 1351 | unsigned int shndx, |
| 1352 | const Stringpool* pool, |
| 1353 | unsigned char* p |
| 1354 | ACCEPT_SIZE_ENDIAN) const |
| 1355 | { |
| 1356 | elfcpp::Sym_write<size, big_endian> osym(p); |
| 1357 | osym.put_st_name(pool->get_offset(sym->name())); |
| 1358 | osym.put_st_value(sym->value()); |
| 1359 | osym.put_st_size(sym->symsize()); |
| 1360 | osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type())); |
| 1361 | osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis())); |
| 1362 | osym.put_st_shndx(shndx); |
| 1363 | } |
| 1364 | |
| 1365 | // Write out a section symbol. Return the update offset. |
| 1366 | |
| 1367 | void |
| 1368 | Symbol_table::write_section_symbol(const Target* target, |
| 1369 | const Output_section *os, |
| 1370 | Output_file* of, |
| 1371 | off_t offset) const |
| 1372 | { |
| 1373 | if (this->size_ == 32) |
| 1374 | { |
| 1375 | if (target->is_big_endian()) |
| 1376 | this->sized_write_section_symbol<32, true>(os, of, offset); |
| 1377 | else |
| 1378 | this->sized_write_section_symbol<32, false>(os, of, offset); |
| 1379 | } |
| 1380 | else if (this->size_ == 64) |
| 1381 | { |
| 1382 | if (target->is_big_endian()) |
| 1383 | this->sized_write_section_symbol<64, true>(os, of, offset); |
| 1384 | else |
| 1385 | this->sized_write_section_symbol<64, false>(os, of, offset); |
| 1386 | } |
| 1387 | else |
| 1388 | gold_unreachable(); |
| 1389 | } |
| 1390 | |
| 1391 | // Write out a section symbol, specialized for size and endianness. |
| 1392 | |
| 1393 | template<int size, bool big_endian> |
| 1394 | void |
| 1395 | Symbol_table::sized_write_section_symbol(const Output_section* os, |
| 1396 | Output_file* of, |
| 1397 | off_t offset) const |
| 1398 | { |
| 1399 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 1400 | |
| 1401 | unsigned char* pov = of->get_output_view(offset, sym_size); |
| 1402 | |
| 1403 | elfcpp::Sym_write<size, big_endian> osym(pov); |
| 1404 | osym.put_st_name(0); |
| 1405 | osym.put_st_value(os->address()); |
| 1406 | osym.put_st_size(0); |
| 1407 | osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, |
| 1408 | elfcpp::STT_SECTION)); |
| 1409 | osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0)); |
| 1410 | osym.put_st_shndx(os->out_shndx()); |
| 1411 | |
| 1412 | of->write_output_view(offset, sym_size, pov); |
| 1413 | } |
| 1414 | |
| 1415 | // Warnings functions. |
| 1416 | |
| 1417 | // Add a new warning. |
| 1418 | |
| 1419 | void |
| 1420 | Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj, |
| 1421 | unsigned int shndx) |
| 1422 | { |
| 1423 | name = symtab->canonicalize_name(name); |
| 1424 | this->warnings_[name].set(obj, shndx); |
| 1425 | } |
| 1426 | |
| 1427 | // Look through the warnings and mark the symbols for which we should |
| 1428 | // warn. This is called during Layout::finalize when we know the |
| 1429 | // sources for all the symbols. |
| 1430 | |
| 1431 | void |
| 1432 | Warnings::note_warnings(Symbol_table* symtab) |
| 1433 | { |
| 1434 | for (Warning_table::iterator p = this->warnings_.begin(); |
| 1435 | p != this->warnings_.end(); |
| 1436 | ++p) |
| 1437 | { |
| 1438 | Symbol* sym = symtab->lookup(p->first, NULL); |
| 1439 | if (sym != NULL |
| 1440 | && sym->source() == Symbol::FROM_OBJECT |
| 1441 | && sym->object() == p->second.object) |
| 1442 | { |
| 1443 | sym->set_has_warning(); |
| 1444 | |
| 1445 | // Read the section contents to get the warning text. It |
| 1446 | // would be nicer if we only did this if we have to actually |
| 1447 | // issue a warning. Unfortunately, warnings are issued as |
| 1448 | // we relocate sections. That means that we can not lock |
| 1449 | // the object then, as we might try to issue the same |
| 1450 | // warning multiple times simultaneously. |
| 1451 | { |
| 1452 | Task_locker_obj<Object> tl(*p->second.object); |
| 1453 | const unsigned char* c; |
| 1454 | off_t len; |
| 1455 | c = p->second.object->section_contents(p->second.shndx, &len); |
| 1456 | p->second.set_text(reinterpret_cast<const char*>(c), len); |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | } |
| 1461 | |
| 1462 | // Issue a warning. This is called when we see a relocation against a |
| 1463 | // symbol for which has a warning. |
| 1464 | |
| 1465 | void |
| 1466 | Warnings::issue_warning(const Symbol* sym, const std::string& location) const |
| 1467 | { |
| 1468 | gold_assert(sym->has_warning()); |
| 1469 | Warning_table::const_iterator p = this->warnings_.find(sym->name()); |
| 1470 | gold_assert(p != this->warnings_.end()); |
| 1471 | fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(), |
| 1472 | p->second.text.c_str()); |
| 1473 | } |
| 1474 | |
| 1475 | // Instantiate the templates we need. We could use the configure |
| 1476 | // script to restrict this to only the ones needed for implemented |
| 1477 | // targets. |
| 1478 | |
| 1479 | template |
| 1480 | void |
| 1481 | Symbol_table::add_from_relobj<32, true>( |
| 1482 | Sized_relobj<32, true>* relobj, |
| 1483 | const unsigned char* syms, |
| 1484 | size_t count, |
| 1485 | const char* sym_names, |
| 1486 | size_t sym_name_size, |
| 1487 | Symbol** sympointers); |
| 1488 | |
| 1489 | template |
| 1490 | void |
| 1491 | Symbol_table::add_from_relobj<32, false>( |
| 1492 | Sized_relobj<32, false>* relobj, |
| 1493 | const unsigned char* syms, |
| 1494 | size_t count, |
| 1495 | const char* sym_names, |
| 1496 | size_t sym_name_size, |
| 1497 | Symbol** sympointers); |
| 1498 | |
| 1499 | template |
| 1500 | void |
| 1501 | Symbol_table::add_from_relobj<64, true>( |
| 1502 | Sized_relobj<64, true>* relobj, |
| 1503 | const unsigned char* syms, |
| 1504 | size_t count, |
| 1505 | const char* sym_names, |
| 1506 | size_t sym_name_size, |
| 1507 | Symbol** sympointers); |
| 1508 | |
| 1509 | template |
| 1510 | void |
| 1511 | Symbol_table::add_from_relobj<64, false>( |
| 1512 | Sized_relobj<64, false>* relobj, |
| 1513 | const unsigned char* syms, |
| 1514 | size_t count, |
| 1515 | const char* sym_names, |
| 1516 | size_t sym_name_size, |
| 1517 | Symbol** sympointers); |
| 1518 | |
| 1519 | template |
| 1520 | void |
| 1521 | Symbol_table::add_from_dynobj<32, true>( |
| 1522 | Sized_dynobj<32, true>* dynobj, |
| 1523 | const unsigned char* syms, |
| 1524 | size_t count, |
| 1525 | const char* sym_names, |
| 1526 | size_t sym_name_size, |
| 1527 | const unsigned char* versym, |
| 1528 | size_t versym_size, |
| 1529 | const std::vector<const char*>* version_map); |
| 1530 | |
| 1531 | template |
| 1532 | void |
| 1533 | Symbol_table::add_from_dynobj<32, false>( |
| 1534 | Sized_dynobj<32, false>* dynobj, |
| 1535 | const unsigned char* syms, |
| 1536 | size_t count, |
| 1537 | const char* sym_names, |
| 1538 | size_t sym_name_size, |
| 1539 | const unsigned char* versym, |
| 1540 | size_t versym_size, |
| 1541 | const std::vector<const char*>* version_map); |
| 1542 | |
| 1543 | template |
| 1544 | void |
| 1545 | Symbol_table::add_from_dynobj<64, true>( |
| 1546 | Sized_dynobj<64, true>* dynobj, |
| 1547 | const unsigned char* syms, |
| 1548 | size_t count, |
| 1549 | const char* sym_names, |
| 1550 | size_t sym_name_size, |
| 1551 | const unsigned char* versym, |
| 1552 | size_t versym_size, |
| 1553 | const std::vector<const char*>* version_map); |
| 1554 | |
| 1555 | template |
| 1556 | void |
| 1557 | Symbol_table::add_from_dynobj<64, false>( |
| 1558 | Sized_dynobj<64, false>* dynobj, |
| 1559 | const unsigned char* syms, |
| 1560 | size_t count, |
| 1561 | const char* sym_names, |
| 1562 | size_t sym_name_size, |
| 1563 | const unsigned char* versym, |
| 1564 | size_t versym_size, |
| 1565 | const std::vector<const char*>* version_map); |
| 1566 | |
| 1567 | } // End namespace gold. |