1// symtab.cc -- the gold symbol table 2 3// Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4// Written by Ian Lance Taylor <iant@google.com>. 5 6// This file is part of gold. 7 8// This program is free software; you can redistribute it and/or modify 9// it under the terms of the GNU General Public License as published by 10// the Free Software Foundation; either version 3 of the License, or 11// (at your option) any later version. 12 13// This program is distributed in the hope that it will be useful, 14// but WITHOUT ANY WARRANTY; without even the implied warranty of 15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16// GNU General Public License for more details. 17 18// You should have received a copy of the GNU General Public License 19// along with this program; if not, write to the Free Software 20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21// MA 02110-1301, USA. 22 23#include "gold.h" 24 25#include <cstring> 26#include <stdint.h> 27#include <algorithm> 28#include <set> 29#include <string> 30#include <utility> 31#include "demangle.h" 32 33#include "gc.h" 34#include "object.h" 35#include "dwarf_reader.h" 36#include "dynobj.h" 37#include "output.h" 38#include "target.h" 39#include "workqueue.h" 40#include "symtab.h" 41#include "script.h" 42#include "plugin.h" 43 44namespace gold 45{ 46 47// Class Symbol. 48 49// Initialize fields in Symbol. This initializes everything except u_ 50// and source_. 51 52void 53Symbol::init_fields(const char* name, const char* version, 54 elfcpp::STT type, elfcpp::STB binding, 55 elfcpp::STV visibility, unsigned char nonvis) 56{ 57 this->name_ = name; 58 this->version_ = version; 59 this->symtab_index_ = 0; 60 this->dynsym_index_ = 0; 61 this->got_offsets_.init(); 62 this->plt_offset_ = -1U; 63 this->type_ = type; 64 this->binding_ = binding; 65 this->visibility_ = visibility; 66 this->nonvis_ = nonvis; 67 this->is_def_ = false; 68 this->is_forwarder_ = false; 69 this->has_alias_ = false; 70 this->needs_dynsym_entry_ = false; 71 this->in_reg_ = false; 72 this->in_dyn_ = false; 73 this->has_warning_ = false; 74 this->is_copied_from_dynobj_ = false; 75 this->is_forced_local_ = false; 76 this->is_ordinary_shndx_ = false; 77 this->in_real_elf_ = false; 78 this->is_defined_in_discarded_section_ = false; 79 this->undef_binding_set_ = false; 80 this->undef_binding_weak_ = false; 81} 82 83// Return the demangled version of the symbol's name, but only 84// if the --demangle flag was set. 85 86static std::string 87demangle(const char* name) 88{ 89 if (!parameters->options().do_demangle()) 90 return name; 91 92 // cplus_demangle allocates memory for the result it returns, 93 // and returns NULL if the name is already demangled. 94 char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS); 95 if (demangled_name == NULL) 96 return name; 97 98 std::string retval(demangled_name); 99 free(demangled_name); 100 return retval; 101} 102 103std::string 104Symbol::demangled_name() const 105{ 106 return demangle(this->name()); 107} 108 109// Initialize the fields in the base class Symbol for SYM in OBJECT. 110 111template<int size, bool big_endian> 112void 113Symbol::init_base_object(const char* name, const char* version, Object* object, 114 const elfcpp::Sym<size, big_endian>& sym, 115 unsigned int st_shndx, bool is_ordinary) 116{ 117 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(), 118 sym.get_st_visibility(), sym.get_st_nonvis()); 119 this->u_.from_object.object = object; 120 this->u_.from_object.shndx = st_shndx; 121 this->is_ordinary_shndx_ = is_ordinary; 122 this->source_ = FROM_OBJECT; 123 this->in_reg_ = !object->is_dynamic(); 124 this->in_dyn_ = object->is_dynamic(); 125 this->in_real_elf_ = object->pluginobj() == NULL; 126} 127 128// Initialize the fields in the base class Symbol for a symbol defined 129// in an Output_data. 130 131void 132Symbol::init_base_output_data(const char* name, const char* version, 133 Output_data* od, elfcpp::STT type, 134 elfcpp::STB binding, elfcpp::STV visibility, 135 unsigned char nonvis, bool offset_is_from_end) 136{ 137 this->init_fields(name, version, type, binding, visibility, nonvis); 138 this->u_.in_output_data.output_data = od; 139 this->u_.in_output_data.offset_is_from_end = offset_is_from_end; 140 this->source_ = IN_OUTPUT_DATA; 141 this->in_reg_ = true; 142 this->in_real_elf_ = true; 143} 144 145// Initialize the fields in the base class Symbol for a symbol defined 146// in an Output_segment. 147 148void 149Symbol::init_base_output_segment(const char* name, const char* version, 150 Output_segment* os, elfcpp::STT type, 151 elfcpp::STB binding, elfcpp::STV visibility, 152 unsigned char nonvis, 153 Segment_offset_base offset_base) 154{ 155 this->init_fields(name, version, type, binding, visibility, nonvis); 156 this->u_.in_output_segment.output_segment = os; 157 this->u_.in_output_segment.offset_base = offset_base; 158 this->source_ = IN_OUTPUT_SEGMENT; 159 this->in_reg_ = true; 160 this->in_real_elf_ = true; 161} 162 163// Initialize the fields in the base class Symbol for a symbol defined 164// as a constant. 165 166void 167Symbol::init_base_constant(const char* name, const char* version, 168 elfcpp::STT type, elfcpp::STB binding, 169 elfcpp::STV visibility, unsigned char nonvis) 170{ 171 this->init_fields(name, version, type, binding, visibility, nonvis); 172 this->source_ = IS_CONSTANT; 173 this->in_reg_ = true; 174 this->in_real_elf_ = true; 175} 176 177// Initialize the fields in the base class Symbol for an undefined 178// symbol. 179 180void 181Symbol::init_base_undefined(const char* name, const char* version, 182 elfcpp::STT type, elfcpp::STB binding, 183 elfcpp::STV visibility, unsigned char nonvis) 184{ 185 this->init_fields(name, version, type, binding, visibility, nonvis); 186 this->dynsym_index_ = -1U; 187 this->source_ = IS_UNDEFINED; 188 this->in_reg_ = true; 189 this->in_real_elf_ = true; 190} 191 192// Allocate a common symbol in the base. 193 194void 195Symbol::allocate_base_common(Output_data* od) 196{ 197 gold_assert(this->is_common()); 198 this->source_ = IN_OUTPUT_DATA; 199 this->u_.in_output_data.output_data = od; 200 this->u_.in_output_data.offset_is_from_end = false; 201} 202 203// Initialize the fields in Sized_symbol for SYM in OBJECT. 204 205template<int size> 206template<bool big_endian> 207void 208Sized_symbol<size>::init_object(const char* name, const char* version, 209 Object* object, 210 const elfcpp::Sym<size, big_endian>& sym, 211 unsigned int st_shndx, bool is_ordinary) 212{ 213 this->init_base_object(name, version, object, sym, st_shndx, is_ordinary); 214 this->value_ = sym.get_st_value(); 215 this->symsize_ = sym.get_st_size(); 216} 217 218// Initialize the fields in Sized_symbol for a symbol defined in an 219// Output_data. 220 221template<int size> 222void 223Sized_symbol<size>::init_output_data(const char* name, const char* version, 224 Output_data* od, Value_type value, 225 Size_type symsize, elfcpp::STT type, 226 elfcpp::STB binding, 227 elfcpp::STV visibility, 228 unsigned char nonvis, 229 bool offset_is_from_end) 230{ 231 this->init_base_output_data(name, version, od, type, binding, visibility, 232 nonvis, offset_is_from_end); 233 this->value_ = value; 234 this->symsize_ = symsize; 235} 236 237// Initialize the fields in Sized_symbol for a symbol defined in an 238// Output_segment. 239 240template<int size> 241void 242Sized_symbol<size>::init_output_segment(const char* name, const char* version, 243 Output_segment* os, Value_type value, 244 Size_type symsize, elfcpp::STT type, 245 elfcpp::STB binding, 246 elfcpp::STV visibility, 247 unsigned char nonvis, 248 Segment_offset_base offset_base) 249{ 250 this->init_base_output_segment(name, version, os, type, binding, visibility, 251 nonvis, offset_base); 252 this->value_ = value; 253 this->symsize_ = symsize; 254} 255 256// Initialize the fields in Sized_symbol for a symbol defined as a 257// constant. 258 259template<int size> 260void 261Sized_symbol<size>::init_constant(const char* name, const char* version, 262 Value_type value, Size_type symsize, 263 elfcpp::STT type, elfcpp::STB binding, 264 elfcpp::STV visibility, unsigned char nonvis) 265{ 266 this->init_base_constant(name, version, type, binding, visibility, nonvis); 267 this->value_ = value; 268 this->symsize_ = symsize; 269} 270 271// Initialize the fields in Sized_symbol for an undefined symbol. 272 273template<int size> 274void 275Sized_symbol<size>::init_undefined(const char* name, const char* version, 276 elfcpp::STT type, elfcpp::STB binding, 277 elfcpp::STV visibility, unsigned char nonvis) 278{ 279 this->init_base_undefined(name, version, type, binding, visibility, nonvis); 280 this->value_ = 0; 281 this->symsize_ = 0; 282} 283 284// Return true if SHNDX represents a common symbol. 285 286bool 287Symbol::is_common_shndx(unsigned int shndx) 288{ 289 return (shndx == elfcpp::SHN_COMMON 290 || shndx == parameters->target().small_common_shndx() 291 || shndx == parameters->target().large_common_shndx()); 292} 293 294// Allocate a common symbol. 295 296template<int size> 297void 298Sized_symbol<size>::allocate_common(Output_data* od, Value_type value) 299{ 300 this->allocate_base_common(od); 301 this->value_ = value; 302} 303 304// The ""'s around str ensure str is a string literal, so sizeof works. 305#define strprefix(var, str) (strncmp(var, str, sizeof("" str "") - 1) == 0) 306 307// Return true if this symbol should be added to the dynamic symbol 308// table. 309 310inline bool 311Symbol::should_add_dynsym_entry(Symbol_table* symtab) const 312{ 313 // If the symbol is used by a dynamic relocation, we need to add it. 314 if (this->needs_dynsym_entry()) 315 return true; 316 317 // If this symbol's section is not added, the symbol need not be added. 318 // The section may have been GCed. Note that export_dynamic is being 319 // overridden here. This should not be done for shared objects. 320 if (parameters->options().gc_sections() 321 && !parameters->options().shared() 322 && this->source() == Symbol::FROM_OBJECT 323 && !this->object()->is_dynamic()) 324 { 325 Relobj* relobj = static_cast<Relobj*>(this->object()); 326 bool is_ordinary; 327 unsigned int shndx = this->shndx(&is_ordinary); 328 if (is_ordinary && shndx != elfcpp::SHN_UNDEF 329 && !relobj->is_section_included(shndx) 330 && !symtab->is_section_folded(relobj, shndx)) 331 return false; 332 } 333 334 // If the symbol was forced local in a version script, do not add it. 335 if (this->is_forced_local()) 336 return false; 337 338 // If the symbol was forced dynamic in a --dynamic-list file, add it. 339 if (parameters->options().in_dynamic_list(this->name())) 340 return true; 341 342 // If dynamic-list-data was specified, add any STT_OBJECT. 343 if (parameters->options().dynamic_list_data() 344 && !this->is_from_dynobj() 345 && this->type() == elfcpp::STT_OBJECT) 346 return true; 347 348 // If --dynamic-list-cpp-new was specified, add any new/delete symbol. 349 // If --dynamic-list-cpp-typeinfo was specified, add any typeinfo symbols. 350 if ((parameters->options().dynamic_list_cpp_new() 351 || parameters->options().dynamic_list_cpp_typeinfo()) 352 && !this->is_from_dynobj()) 353 { 354 // TODO(csilvers): We could probably figure out if we're an operator 355 // new/delete or typeinfo without the need to demangle. 356 char* demangled_name = cplus_demangle(this->name(), 357 DMGL_ANSI | DMGL_PARAMS); 358 if (demangled_name == NULL) 359 { 360 // Not a C++ symbol, so it can't satisfy these flags 361 } 362 else if (parameters->options().dynamic_list_cpp_new() 363 && (strprefix(demangled_name, "operator new") 364 || strprefix(demangled_name, "operator delete"))) 365 { 366 free(demangled_name); 367 return true; 368 } 369 else if (parameters->options().dynamic_list_cpp_typeinfo() 370 && (strprefix(demangled_name, "typeinfo name for") 371 || strprefix(demangled_name, "typeinfo for"))) 372 { 373 free(demangled_name); 374 return true; 375 } 376 else 377 free(demangled_name); 378 } 379 380 // If exporting all symbols or building a shared library, 381 // and the symbol is defined in a regular object and is 382 // externally visible, we need to add it. 383 if ((parameters->options().export_dynamic() || parameters->options().shared()) 384 && !this->is_from_dynobj() 385 && this->is_externally_visible()) 386 return true; 387 388 return false; 389} 390 391// Return true if the final value of this symbol is known at link 392// time. 393 394bool 395Symbol::final_value_is_known() const 396{ 397 // If we are not generating an executable, then no final values are 398 // known, since they will change at runtime. 399 if (parameters->options().output_is_position_independent() 400 || parameters->options().relocatable()) 401 return false; 402 403 // If the symbol is not from an object file, and is not undefined, 404 // then it is defined, and known. 405 if (this->source_ != FROM_OBJECT) 406 { 407 if (this->source_ != IS_UNDEFINED) 408 return true; 409 } 410 else 411 { 412 // If the symbol is from a dynamic object, then the final value 413 // is not known. 414 if (this->object()->is_dynamic()) 415 return false; 416 417 // If the symbol is not undefined (it is defined or common), 418 // then the final value is known. 419 if (!this->is_undefined()) 420 return true; 421 } 422 423 // If the symbol is undefined, then whether the final value is known 424 // depends on whether we are doing a static link. If we are doing a 425 // dynamic link, then the final value could be filled in at runtime. 426 // This could reasonably be the case for a weak undefined symbol. 427 return parameters->doing_static_link(); 428} 429 430// Return the output section where this symbol is defined. 431 432Output_section* 433Symbol::output_section() const 434{ 435 switch (this->source_) 436 { 437 case FROM_OBJECT: 438 { 439 unsigned int shndx = this->u_.from_object.shndx; 440 if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_) 441 { 442 gold_assert(!this->u_.from_object.object->is_dynamic()); 443 gold_assert(this->u_.from_object.object->pluginobj() == NULL); 444 Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object); 445 return relobj->output_section(shndx); 446 } 447 return NULL; 448 } 449 450 case IN_OUTPUT_DATA: 451 return this->u_.in_output_data.output_data->output_section(); 452 453 case IN_OUTPUT_SEGMENT: 454 case IS_CONSTANT: 455 case IS_UNDEFINED: 456 return NULL; 457 458 default: 459 gold_unreachable(); 460 } 461} 462 463// Set the symbol's output section. This is used for symbols defined 464// in scripts. This should only be called after the symbol table has 465// been finalized. 466 467void 468Symbol::set_output_section(Output_section* os) 469{ 470 switch (this->source_) 471 { 472 case FROM_OBJECT: 473 case IN_OUTPUT_DATA: 474 gold_assert(this->output_section() == os); 475 break; 476 case IS_CONSTANT: 477 this->source_ = IN_OUTPUT_DATA; 478 this->u_.in_output_data.output_data = os; 479 this->u_.in_output_data.offset_is_from_end = false; 480 break; 481 case IN_OUTPUT_SEGMENT: 482 case IS_UNDEFINED: 483 default: 484 gold_unreachable(); 485 } 486} 487 488// Class Symbol_table. 489 490Symbol_table::Symbol_table(unsigned int count, 491 const Version_script_info& version_script) 492 : saw_undefined_(0), offset_(0), table_(count), namepool_(), 493 forwarders_(), commons_(), tls_commons_(), small_commons_(), 494 large_commons_(), forced_locals_(), warnings_(), 495 version_script_(version_script), gc_(NULL), icf_(NULL) 496{ 497 namepool_.reserve(count); 498} 499 500Symbol_table::~Symbol_table() 501{ 502} 503 504// The symbol table key equality function. This is called with 505// Stringpool keys. 506 507inline bool 508Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1, 509 const Symbol_table_key& k2) const 510{ 511 return k1.first == k2.first && k1.second == k2.second; 512} 513 514bool 515Symbol_table::is_section_folded(Object* obj, unsigned int shndx) const 516{ 517 return (parameters->options().icf_enabled() 518 && this->icf_->is_section_folded(obj, shndx)); 519} 520 521// For symbols that have been listed with -u option, add them to the 522// work list to avoid gc'ing them. 523 524void 525Symbol_table::gc_mark_undef_symbols(Layout* layout) 526{ 527 for (options::String_set::const_iterator p = 528 parameters->options().undefined_begin(); 529 p != parameters->options().undefined_end(); 530 ++p) 531 { 532 const char* name = p->c_str(); 533 Symbol* sym = this->lookup(name); 534 gold_assert(sym != NULL); 535 if (sym->source() == Symbol::FROM_OBJECT 536 && !sym->object()->is_dynamic()) 537 { 538 Relobj* obj = static_cast<Relobj*>(sym->object()); 539 bool is_ordinary; 540 unsigned int shndx = sym->shndx(&is_ordinary); 541 if (is_ordinary) 542 { 543 gold_assert(this->gc_ != NULL); 544 this->gc_->worklist().push(Section_id(obj, shndx)); 545 } 546 } 547 } 548 549 for (Script_options::referenced_const_iterator p = 550 layout->script_options()->referenced_begin(); 551 p != layout->script_options()->referenced_end(); 552 ++p) 553 { 554 Symbol* sym = this->lookup(p->c_str()); 555 gold_assert(sym != NULL); 556 if (sym->source() == Symbol::FROM_OBJECT 557 && !sym->object()->is_dynamic()) 558 { 559 Relobj* obj = static_cast<Relobj*>(sym->object()); 560 bool is_ordinary; 561 unsigned int shndx = sym->shndx(&is_ordinary); 562 if (is_ordinary) 563 { 564 gold_assert(this->gc_ != NULL); 565 this->gc_->worklist().push(Section_id(obj, shndx)); 566 } 567 } 568 } 569} 570 571void 572Symbol_table::gc_mark_symbol_for_shlib(Symbol* sym) 573{ 574 if (!sym->is_from_dynobj() 575 && sym->is_externally_visible()) 576 { 577 //Add the object and section to the work list. 578 Relobj* obj = static_cast<Relobj*>(sym->object()); 579 bool is_ordinary; 580 unsigned int shndx = sym->shndx(&is_ordinary); 581 if (is_ordinary && shndx != elfcpp::SHN_UNDEF) 582 { 583 gold_assert(this->gc_!= NULL); 584 this->gc_->worklist().push(Section_id(obj, shndx)); 585 } 586 } 587} 588 589// When doing garbage collection, keep symbols that have been seen in 590// dynamic objects. 591inline void 592Symbol_table::gc_mark_dyn_syms(Symbol* sym) 593{ 594 if (sym->in_dyn() && sym->source() == Symbol::FROM_OBJECT 595 && !sym->object()->is_dynamic()) 596 { 597 Relobj* obj = static_cast<Relobj*>(sym->object()); 598 bool is_ordinary; 599 unsigned int shndx = sym->shndx(&is_ordinary); 600 if (is_ordinary && shndx != elfcpp::SHN_UNDEF) 601 { 602 gold_assert(this->gc_ != NULL); 603 this->gc_->worklist().push(Section_id(obj, shndx)); 604 } 605 } 606} 607 608// Make TO a symbol which forwards to FROM. 609 610void 611Symbol_table::make_forwarder(Symbol* from, Symbol* to) 612{ 613 gold_assert(from != to); 614 gold_assert(!from->is_forwarder() && !to->is_forwarder()); 615 this->forwarders_[from] = to; 616 from->set_forwarder(); 617} 618 619// Resolve the forwards from FROM, returning the real symbol. 620 621Symbol* 622Symbol_table::resolve_forwards(const Symbol* from) const 623{ 624 gold_assert(from->is_forwarder()); 625 Unordered_map<const Symbol*, Symbol*>::const_iterator p = 626 this->forwarders_.find(from); 627 gold_assert(p != this->forwarders_.end()); 628 return p->second; 629} 630 631// Look up a symbol by name. 632 633Symbol* 634Symbol_table::lookup(const char* name, const char* version) const 635{ 636 Stringpool::Key name_key; 637 name = this->namepool_.find(name, &name_key); 638 if (name == NULL) 639 return NULL; 640 641 Stringpool::Key version_key = 0; 642 if (version != NULL) 643 { 644 version = this->namepool_.find(version, &version_key); 645 if (version == NULL) 646 return NULL; 647 } 648 649 Symbol_table_key key(name_key, version_key); 650 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key); 651 if (p == this->table_.end()) 652 return NULL; 653 return p->second; 654} 655 656// Resolve a Symbol with another Symbol. This is only used in the 657// unusual case where there are references to both an unversioned 658// symbol and a symbol with a version, and we then discover that that 659// version is the default version. Because this is unusual, we do 660// this the slow way, by converting back to an ELF symbol. 661 662template<int size, bool big_endian> 663void 664Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from) 665{ 666 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size]; 667 elfcpp::Sym_write<size, big_endian> esym(buf); 668 // We don't bother to set the st_name or the st_shndx field. 669 esym.put_st_value(from->value()); 670 esym.put_st_size(from->symsize()); 671 esym.put_st_info(from->binding(), from->type()); 672 esym.put_st_other(from->visibility(), from->nonvis()); 673 bool is_ordinary; 674 unsigned int shndx = from->shndx(&is_ordinary); 675 this->resolve(to, esym.sym(), shndx, is_ordinary, shndx, from->object(), 676 from->version()); 677 if (from->in_reg()) 678 to->set_in_reg(); 679 if (from->in_dyn()) 680 to->set_in_dyn(); 681 if (parameters->options().gc_sections()) 682 this->gc_mark_dyn_syms(to); 683} 684 685// Record that a symbol is forced to be local by a version script or 686// by visibility. 687 688void 689Symbol_table::force_local(Symbol* sym) 690{ 691 if (!sym->is_defined() && !sym->is_common()) 692 return; 693 if (sym->is_forced_local()) 694 { 695 // We already got this one. 696 return; 697 } 698 sym->set_is_forced_local(); 699 this->forced_locals_.push_back(sym); 700} 701 702// Adjust NAME for wrapping, and update *NAME_KEY if necessary. This 703// is only called for undefined symbols, when at least one --wrap 704// option was used. 705 706const char* 707Symbol_table::wrap_symbol(const char* name, Stringpool::Key* name_key) 708{ 709 // For some targets, we need to ignore a specific character when 710 // wrapping, and add it back later. 711 char prefix = '\0'; 712 if (name[0] == parameters->target().wrap_char()) 713 { 714 prefix = name[0]; 715 ++name; 716 } 717 718 if (parameters->options().is_wrap(name)) 719 { 720 // Turn NAME into __wrap_NAME. 721 std::string s; 722 if (prefix != '\0') 723 s += prefix; 724 s += "__wrap_"; 725 s += name; 726 727 // This will give us both the old and new name in NAMEPOOL_, but 728 // that is OK. Only the versions we need will wind up in the 729 // real string table in the output file. 730 return this->namepool_.add(s.c_str(), true, name_key); 731 } 732 733 const char* const real_prefix = "__real_"; 734 const size_t real_prefix_length = strlen(real_prefix); 735 if (strncmp(name, real_prefix, real_prefix_length) == 0 736 && parameters->options().is_wrap(name + real_prefix_length)) 737 { 738 // Turn __real_NAME into NAME. 739 std::string s; 740 if (prefix != '\0') 741 s += prefix; 742 s += name + real_prefix_length; 743 return this->namepool_.add(s.c_str(), true, name_key); 744 } 745 746 return name; 747} 748 749// This is called when we see a symbol NAME/VERSION, and the symbol 750// already exists in the symbol table, and VERSION is marked as being 751// the default version. SYM is the NAME/VERSION symbol we just added. 752// DEFAULT_IS_NEW is true if this is the first time we have seen the 753// symbol NAME/NULL. PDEF points to the entry for NAME/NULL. 754 755template<int size, bool big_endian> 756void 757Symbol_table::define_default_version(Sized_symbol<size>* sym, 758 bool default_is_new, 759 Symbol_table_type::iterator pdef) 760{ 761 if (default_is_new) 762 { 763 // This is the first time we have seen NAME/NULL. Make 764 // NAME/NULL point to NAME/VERSION, and mark SYM as the default 765 // version. 766 pdef->second = sym; 767 sym->set_is_default(); 768 } 769 else if (pdef->second == sym) 770 { 771 // NAME/NULL already points to NAME/VERSION. Don't mark the 772 // symbol as the default if it is not already the default. 773 } 774 else 775 { 776 // This is the unfortunate case where we already have entries 777 // for both NAME/VERSION and NAME/NULL. We now see a symbol 778 // NAME/VERSION where VERSION is the default version. We have 779 // already resolved this new symbol with the existing 780 // NAME/VERSION symbol. 781 782 // It's possible that NAME/NULL and NAME/VERSION are both 783 // defined in regular objects. This can only happen if one 784 // object file defines foo and another defines foo@@ver. This 785 // is somewhat obscure, but we call it a multiple definition 786 // error. 787 788 // It's possible that NAME/NULL actually has a version, in which 789 // case it won't be the same as VERSION. This happens with 790 // ver_test_7.so in the testsuite for the symbol t2_2. We see 791 // t2_2@@VER2, so we define both t2_2/VER2 and t2_2/NULL. We 792 // then see an unadorned t2_2 in an object file and give it 793 // version VER1 from the version script. This looks like a 794 // default definition for VER1, so it looks like we should merge 795 // t2_2/NULL with t2_2/VER1. That doesn't make sense, but it's 796 // not obvious that this is an error, either. So we just punt. 797 798 // If one of the symbols has non-default visibility, and the 799 // other is defined in a shared object, then they are different 800 // symbols. 801 802 // Otherwise, we just resolve the symbols as though they were 803 // the same. 804 805 if (pdef->second->version() != NULL) 806 gold_assert(pdef->second->version() != sym->version()); 807 else if (sym->visibility() != elfcpp::STV_DEFAULT 808 && pdef->second->is_from_dynobj()) 809 ; 810 else if (pdef->second->visibility() != elfcpp::STV_DEFAULT 811 && sym->is_from_dynobj()) 812 ; 813 else 814 { 815 const Sized_symbol<size>* symdef; 816 symdef = this->get_sized_symbol<size>(pdef->second); 817 Symbol_table::resolve<size, big_endian>(sym, symdef); 818 this->make_forwarder(pdef->second, sym); 819 pdef->second = sym; 820 sym->set_is_default(); 821 } 822 } 823} 824 825// Add one symbol from OBJECT to the symbol table. NAME is symbol 826// name and VERSION is the version; both are canonicalized. DEF is 827// whether this is the default version. ST_SHNDX is the symbol's 828// section index; IS_ORDINARY is whether this is a normal section 829// rather than a special code. 830 831// If IS_DEFAULT_VERSION is true, then this is the definition of a 832// default version of a symbol. That means that any lookup of 833// NAME/NULL and any lookup of NAME/VERSION should always return the 834// same symbol. This is obvious for references, but in particular we 835// want to do this for definitions: overriding NAME/NULL should also 836// override NAME/VERSION. If we don't do that, it would be very hard 837// to override functions in a shared library which uses versioning. 838 839// We implement this by simply making both entries in the hash table 840// point to the same Symbol structure. That is easy enough if this is 841// the first time we see NAME/NULL or NAME/VERSION, but it is possible 842// that we have seen both already, in which case they will both have 843// independent entries in the symbol table. We can't simply change 844// the symbol table entry, because we have pointers to the entries 845// attached to the object files. So we mark the entry attached to the 846// object file as a forwarder, and record it in the forwarders_ map. 847// Note that entries in the hash table will never be marked as 848// forwarders. 849// 850// ORIG_ST_SHNDX and ST_SHNDX are almost always the same. 851// ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF 852// for a special section code. ST_SHNDX may be modified if the symbol 853// is defined in a section being discarded. 854 855template<int size, bool big_endian> 856Sized_symbol<size>* 857Symbol_table::add_from_object(Object* object, 858 const char* name, 859 Stringpool::Key name_key, 860 const char* version, 861 Stringpool::Key version_key, 862 bool is_default_version, 863 const elfcpp::Sym<size, big_endian>& sym, 864 unsigned int st_shndx, 865 bool is_ordinary, 866 unsigned int orig_st_shndx) 867{ 868 // Print a message if this symbol is being traced. 869 if (parameters->options().is_trace_symbol(name)) 870 { 871 if (orig_st_shndx == elfcpp::SHN_UNDEF) 872 gold_info(_("%s: reference to %s"), object->name().c_str(), name); 873 else 874 gold_info(_("%s: definition of %s"), object->name().c_str(), name); 875 } 876 877 // For an undefined symbol, we may need to adjust the name using 878 // --wrap. 879 if (orig_st_shndx == elfcpp::SHN_UNDEF 880 && parameters->options().any_wrap()) 881 { 882 const char* wrap_name = this->wrap_symbol(name, &name_key); 883 if (wrap_name != name) 884 { 885 // If we see a reference to malloc with version GLIBC_2.0, 886 // and we turn it into a reference to __wrap_malloc, then we 887 // discard the version number. Otherwise the user would be 888 // required to specify the correct version for 889 // __wrap_malloc. 890 version = NULL; 891 version_key = 0; 892 name = wrap_name; 893 } 894 } 895 896 Symbol* const snull = NULL; 897 std::pair<typename Symbol_table_type::iterator, bool> ins = 898 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key), 899 snull)); 900 901 std::pair<typename Symbol_table_type::iterator, bool> insdefault = 902 std::make_pair(this->table_.end(), false); 903 if (is_default_version) 904 { 905 const Stringpool::Key vnull_key = 0; 906 insdefault = this->table_.insert(std::make_pair(std::make_pair(name_key, 907 vnull_key), 908 snull)); 909 } 910 911 // ins.first: an iterator, which is a pointer to a pair. 912 // ins.first->first: the key (a pair of name and version). 913 // ins.first->second: the value (Symbol*). 914 // ins.second: true if new entry was inserted, false if not. 915 916 Sized_symbol<size>* ret; 917 bool was_undefined; 918 bool was_common; 919 if (!ins.second) 920 { 921 // We already have an entry for NAME/VERSION. 922 ret = this->get_sized_symbol<size>(ins.first->second); 923 gold_assert(ret != NULL); 924 925 was_undefined = ret->is_undefined(); 926 was_common = ret->is_common(); 927 928 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object, 929 version); 930 if (parameters->options().gc_sections()) 931 this->gc_mark_dyn_syms(ret); 932 933 if (is_default_version) 934 this->define_default_version<size, big_endian>(ret, insdefault.second, 935 insdefault.first); 936 } 937 else 938 { 939 // This is the first time we have seen NAME/VERSION. 940 gold_assert(ins.first->second == NULL); 941 942 if (is_default_version && !insdefault.second) 943 { 944 // We already have an entry for NAME/NULL. If we override 945 // it, then change it to NAME/VERSION. 946 ret = this->get_sized_symbol<size>(insdefault.first->second); 947 948 was_undefined = ret->is_undefined(); 949 was_common = ret->is_common(); 950 951 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object, 952 version); 953 if (parameters->options().gc_sections()) 954 this->gc_mark_dyn_syms(ret); 955 ins.first->second = ret; 956 } 957 else 958 { 959 was_undefined = false; 960 was_common = false; 961 962 Sized_target<size, big_endian>* target = 963 parameters->sized_target<size, big_endian>(); 964 if (!target->has_make_symbol()) 965 ret = new Sized_symbol<size>(); 966 else 967 { 968 ret = target->make_symbol(); 969 if (ret == NULL) 970 { 971 // This means that we don't want a symbol table 972 // entry after all. 973 if (!is_default_version) 974 this->table_.erase(ins.first); 975 else 976 { 977 this->table_.erase(insdefault.first); 978 // Inserting INSDEFAULT invalidated INS. 979 this->table_.erase(std::make_pair(name_key, 980 version_key)); 981 } 982 return NULL; 983 } 984 } 985 986 ret->init_object(name, version, object, sym, st_shndx, is_ordinary); 987 988 ins.first->second = ret; 989 if (is_default_version) 990 { 991 // This is the first time we have seen NAME/NULL. Point 992 // it at the new entry for NAME/VERSION. 993 gold_assert(insdefault.second); 994 insdefault.first->second = ret; 995 } 996 } 997 998 if (is_default_version) 999 ret->set_is_default(); 1000 } 1001 1002 // Record every time we see a new undefined symbol, to speed up 1003 // archive groups. 1004 if (!was_undefined && ret->is_undefined()) 1005 { 1006 ++this->saw_undefined_; 1007 if (parameters->options().has_plugins()) 1008 parameters->options().plugins()->new_undefined_symbol(ret); 1009 } 1010 1011 // Keep track of common symbols, to speed up common symbol 1012 // allocation. 1013 if (!was_common && ret->is_common()) 1014 { 1015 if (ret->type() == elfcpp::STT_TLS) 1016 this->tls_commons_.push_back(ret); 1017 else if (!is_ordinary 1018 && st_shndx == parameters->target().small_common_shndx()) 1019 this->small_commons_.push_back(ret); 1020 else if (!is_ordinary 1021 && st_shndx == parameters->target().large_common_shndx()) 1022 this->large_commons_.push_back(ret); 1023 else 1024 this->commons_.push_back(ret); 1025 } 1026 1027 // If we're not doing a relocatable link, then any symbol with 1028 // hidden or internal visibility is local. 1029 if ((ret->visibility() == elfcpp::STV_HIDDEN 1030 || ret->visibility() == elfcpp::STV_INTERNAL) 1031 && (ret->binding() == elfcpp::STB_GLOBAL 1032 || ret->binding() == elfcpp::STB_GNU_UNIQUE 1033 || ret->binding() == elfcpp::STB_WEAK) 1034 && !parameters->options().relocatable()) 1035 this->force_local(ret); 1036 1037 return ret; 1038} 1039 1040// Add all the symbols in a relocatable object to the hash table. 1041 1042template<int size, bool big_endian> 1043void 1044Symbol_table::add_from_relobj( 1045 Sized_relobj<size, big_endian>* relobj, 1046 const unsigned char* syms, 1047 size_t count, 1048 size_t symndx_offset, 1049 const char* sym_names, 1050 size_t sym_name_size, 1051 typename Sized_relobj<size, big_endian>::Symbols* sympointers, 1052 size_t* defined) 1053{ 1054 *defined = 0; 1055 1056 gold_assert(size == parameters->target().get_size()); 1057 1058 const int sym_size = elfcpp::Elf_sizes<size>::sym_size; 1059 1060 const bool just_symbols = relobj->just_symbols(); 1061 1062 const unsigned char* p = syms; 1063 for (size_t i = 0; i < count; ++i, p += sym_size) 1064 { 1065 (*sympointers)[i] = NULL; 1066 1067 elfcpp::Sym<size, big_endian> sym(p); 1068 1069 unsigned int st_name = sym.get_st_name(); 1070 if (st_name >= sym_name_size) 1071 { 1072 relobj->error(_("bad global symbol name offset %u at %zu"), 1073 st_name, i); 1074 continue; 1075 } 1076 1077 const char* name = sym_names + st_name; 1078 1079 bool is_ordinary; 1080 unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset, 1081 sym.get_st_shndx(), 1082 &is_ordinary); 1083 unsigned int orig_st_shndx = st_shndx; 1084 if (!is_ordinary) 1085 orig_st_shndx = elfcpp::SHN_UNDEF; 1086 1087 if (st_shndx != elfcpp::SHN_UNDEF) 1088 ++*defined; 1089 1090 // A symbol defined in a section which we are not including must 1091 // be treated as an undefined symbol. 1092 bool is_defined_in_discarded_section = false; 1093 if (st_shndx != elfcpp::SHN_UNDEF 1094 && is_ordinary 1095 && !relobj->is_section_included(st_shndx) 1096 && !this->is_section_folded(relobj, st_shndx)) 1097 { 1098 st_shndx = elfcpp::SHN_UNDEF; 1099 is_defined_in_discarded_section = true; 1100 } 1101 1102 // In an object file, an '@' in the name separates the symbol 1103 // name from the version name. If there are two '@' characters, 1104 // this is the default version. 1105 const char* ver = strchr(name, '@'); 1106 Stringpool::Key ver_key = 0; 1107 int namelen = 0; 1108 // IS_DEFAULT_VERSION: is the version default? 1109 // IS_FORCED_LOCAL: is the symbol forced local? 1110 bool is_default_version = false; 1111 bool is_forced_local = false; 1112 1113 if (ver != NULL) 1114 { 1115 // The symbol name is of the form foo@VERSION or foo@@VERSION 1116 namelen = ver - name; 1117 ++ver; 1118 if (*ver == '@') 1119 { 1120 is_default_version = true; 1121 ++ver; 1122 } 1123 ver = this->namepool_.add(ver, true, &ver_key); 1124 } 1125 // We don't want to assign a version to an undefined symbol, 1126 // even if it is listed in the version script. FIXME: What 1127 // about a common symbol? 1128 else 1129 { 1130 namelen = strlen(name); 1131 if (!this->version_script_.empty() 1132 && st_shndx != elfcpp::SHN_UNDEF) 1133 { 1134 // The symbol name did not have a version, but the 1135 // version script may assign a version anyway. 1136 std::string version; 1137 bool is_global; 1138 if (this->version_script_.get_symbol_version(name, &version, 1139 &is_global)) 1140 { 1141 if (!is_global) 1142 is_forced_local = true; 1143 else if (!version.empty()) 1144 { 1145 ver = this->namepool_.add_with_length(version.c_str(), 1146 version.length(), 1147 true, 1148 &ver_key); 1149 is_default_version = true; 1150 } 1151 } 1152 } 1153 } 1154 1155 elfcpp::Sym<size, big_endian>* psym = &sym; 1156 unsigned char symbuf[sym_size]; 1157 elfcpp::Sym<size, big_endian> sym2(symbuf); 1158 if (just_symbols) 1159 { 1160 memcpy(symbuf, p, sym_size); 1161 elfcpp::Sym_write<size, big_endian> sw(symbuf); 1162 if (orig_st_shndx != elfcpp::SHN_UNDEF && is_ordinary) 1163 { 1164 // Symbol values in object files are section relative. 1165 // This is normally what we want, but since here we are 1166 // converting the symbol to absolute we need to add the 1167 // section address. The section address in an object 1168 // file is normally zero, but people can use a linker 1169 // script to change it. 1170 sw.put_st_value(sym.get_st_value() 1171 + relobj->section_address(orig_st_shndx)); 1172 } 1173 st_shndx = elfcpp::SHN_ABS; 1174 is_ordinary = false; 1175 psym = &sym2; 1176 } 1177 1178 // Fix up visibility if object has no-export set. 1179 if (relobj->no_export() 1180 && (orig_st_shndx != elfcpp::SHN_UNDEF || !is_ordinary)) 1181 { 1182 // We may have copied symbol already above. 1183 if (psym != &sym2) 1184 { 1185 memcpy(symbuf, p, sym_size); 1186 psym = &sym2; 1187 } 1188 1189 elfcpp::STV visibility = sym2.get_st_visibility(); 1190 if (visibility == elfcpp::STV_DEFAULT 1191 || visibility == elfcpp::STV_PROTECTED) 1192 { 1193 elfcpp::Sym_write<size, big_endian> sw(symbuf); 1194 unsigned char nonvis = sym2.get_st_nonvis(); 1195 sw.put_st_other(elfcpp::STV_HIDDEN, nonvis); 1196 } 1197 } 1198 1199 Stringpool::Key name_key; 1200 name = this->namepool_.add_with_length(name, namelen, true, 1201 &name_key); 1202 1203 Sized_symbol<size>* res; 1204 res = this->add_from_object(relobj, name, name_key, ver, ver_key, 1205 is_default_version, *psym, st_shndx, 1206 is_ordinary, orig_st_shndx); 1207 1208 // If building a shared library using garbage collection, do not 1209 // treat externally visible symbols as garbage. 1210 if (parameters->options().gc_sections() 1211 && parameters->options().shared()) 1212 this->gc_mark_symbol_for_shlib(res); 1213 1214 if (is_forced_local) 1215 this->force_local(res); 1216 1217 if (is_defined_in_discarded_section) 1218 res->set_is_defined_in_discarded_section(); 1219 1220 (*sympointers)[i] = res; 1221 } 1222} 1223 1224// Add a symbol from a plugin-claimed file. 1225 1226template<int size, bool big_endian> 1227Symbol* 1228Symbol_table::add_from_pluginobj( 1229 Sized_pluginobj<size, big_endian>* obj, 1230 const char* name, 1231 const char* ver, 1232 elfcpp::Sym<size, big_endian>* sym) 1233{ 1234 unsigned int st_shndx = sym->get_st_shndx(); 1235 bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE; 1236 1237 Stringpool::Key ver_key = 0; 1238 bool is_default_version = false; 1239 bool is_forced_local = false; 1240 1241 if (ver != NULL) 1242 { 1243 ver = this->namepool_.add(ver, true, &ver_key); 1244 } 1245 // We don't want to assign a version to an undefined symbol, 1246 // even if it is listed in the version script. FIXME: What 1247 // about a common symbol? 1248 else 1249 { 1250 if (!this->version_script_.empty() 1251 && st_shndx != elfcpp::SHN_UNDEF) 1252 { 1253 // The symbol name did not have a version, but the 1254 // version script may assign a version anyway. 1255 std::string version; 1256 bool is_global; 1257 if (this->version_script_.get_symbol_version(name, &version, 1258 &is_global)) 1259 { 1260 if (!is_global) 1261 is_forced_local = true; 1262 else if (!version.empty()) 1263 { 1264 ver = this->namepool_.add_with_length(version.c_str(), 1265 version.length(), 1266 true, 1267 &ver_key); 1268 is_default_version = true; 1269 } 1270 } 1271 } 1272 } 1273 1274 Stringpool::Key name_key; 1275 name = this->namepool_.add(name, true, &name_key); 1276 1277 Sized_symbol<size>* res; 1278 res = this->add_from_object(obj, name, name_key, ver, ver_key, 1279 is_default_version, *sym, st_shndx, 1280 is_ordinary, st_shndx); 1281 1282 if (is_forced_local) 1283 this->force_local(res); 1284 1285 return res; 1286} 1287 1288// Add all the symbols in a dynamic object to the hash table. 1289 1290template<int size, bool big_endian> 1291void 1292Symbol_table::add_from_dynobj( 1293 Sized_dynobj<size, big_endian>* dynobj, 1294 const unsigned char* syms, 1295 size_t count, 1296 const char* sym_names, 1297 size_t sym_name_size, 1298 const unsigned char* versym, 1299 size_t versym_size, 1300 const std::vector<const char*>* version_map, 1301 typename Sized_relobj<size, big_endian>::Symbols* sympointers, 1302 size_t* defined) 1303{ 1304 *defined = 0; 1305 1306 gold_assert(size == parameters->target().get_size()); 1307 1308 if (dynobj->just_symbols()) 1309 { 1310 gold_error(_("--just-symbols does not make sense with a shared object")); 1311 return; 1312 } 1313 1314 if (versym != NULL && versym_size / 2 < count) 1315 { 1316 dynobj->error(_("too few symbol versions")); 1317 return; 1318 } 1319 1320 const int sym_size = elfcpp::Elf_sizes<size>::sym_size; 1321 1322 // We keep a list of all STT_OBJECT symbols, so that we can resolve 1323 // weak aliases. This is necessary because if the dynamic object 1324 // provides the same variable under two names, one of which is a 1325 // weak definition, and the regular object refers to the weak 1326 // definition, we have to put both the weak definition and the 1327 // strong definition into the dynamic symbol table. Given a weak 1328 // definition, the only way that we can find the corresponding 1329 // strong definition, if any, is to search the symbol table. 1330 std::vector<Sized_symbol<size>*> object_symbols; 1331 1332 const unsigned char* p = syms; 1333 const unsigned char* vs = versym; 1334 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2) 1335 { 1336 elfcpp::Sym<size, big_endian> sym(p); 1337 1338 if (sympointers != NULL) 1339 (*sympointers)[i] = NULL; 1340 1341 // Ignore symbols with local binding or that have 1342 // internal or hidden visibility. 1343 if (sym.get_st_bind() == elfcpp::STB_LOCAL 1344 || sym.get_st_visibility() == elfcpp::STV_INTERNAL 1345 || sym.get_st_visibility() == elfcpp::STV_HIDDEN) 1346 continue; 1347 1348 // A protected symbol in a shared library must be treated as a 1349 // normal symbol when viewed from outside the shared library. 1350 // Implement this by overriding the visibility here. 1351 elfcpp::Sym<size, big_endian>* psym = &sym; 1352 unsigned char symbuf[sym_size]; 1353 elfcpp::Sym<size, big_endian> sym2(symbuf); 1354 if (sym.get_st_visibility() == elfcpp::STV_PROTECTED) 1355 { 1356 memcpy(symbuf, p, sym_size); 1357 elfcpp::Sym_write<size, big_endian> sw(symbuf); 1358 sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis()); 1359 psym = &sym2; 1360 } 1361 1362 unsigned int st_name = psym->get_st_name(); 1363 if (st_name >= sym_name_size) 1364 { 1365 dynobj->error(_("bad symbol name offset %u at %zu"), 1366 st_name, i); 1367 continue; 1368 } 1369 1370 const char* name = sym_names + st_name; 1371 1372 bool is_ordinary; 1373 unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(), 1374 &is_ordinary); 1375 1376 if (st_shndx != elfcpp::SHN_UNDEF) 1377 ++*defined; 1378 1379 Sized_symbol<size>* res; 1380 1381 if (versym == NULL) 1382 { 1383 Stringpool::Key name_key; 1384 name = this->namepool_.add(name, true, &name_key); 1385 res = this->add_from_object(dynobj, name, name_key, NULL, 0, 1386 false, *psym, st_shndx, is_ordinary, 1387 st_shndx); 1388 } 1389 else 1390 { 1391 // Read the version information. 1392 1393 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs); 1394 1395 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0; 1396 v &= elfcpp::VERSYM_VERSION; 1397 1398 // The Sun documentation says that V can be VER_NDX_LOCAL, 1399 // or VER_NDX_GLOBAL, or a version index. The meaning of 1400 // VER_NDX_LOCAL is defined as "Symbol has local scope." 1401 // The old GNU linker will happily generate VER_NDX_LOCAL 1402 // for an undefined symbol. I don't know what the Sun 1403 // linker will generate. 1404 1405 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL) 1406 && st_shndx != elfcpp::SHN_UNDEF) 1407 { 1408 // This symbol should not be visible outside the object. 1409 continue; 1410 } 1411 1412 // At this point we are definitely going to add this symbol. 1413 Stringpool::Key name_key; 1414 name = this->namepool_.add(name, true, &name_key); 1415 1416 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL) 1417 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL)) 1418 { 1419 // This symbol does not have a version. 1420 res = this->add_from_object(dynobj, name, name_key, NULL, 0, 1421 false, *psym, st_shndx, is_ordinary, 1422 st_shndx); 1423 } 1424 else 1425 { 1426 if (v >= version_map->size()) 1427 { 1428 dynobj->error(_("versym for symbol %zu out of range: %u"), 1429 i, v); 1430 continue; 1431 } 1432 1433 const char* version = (*version_map)[v]; 1434 if (version == NULL) 1435 { 1436 dynobj->error(_("versym for symbol %zu has no name: %u"), 1437 i, v); 1438 continue; 1439 } 1440 1441 Stringpool::Key version_key; 1442 version = this->namepool_.add(version, true, &version_key); 1443 1444 // If this is an absolute symbol, and the version name 1445 // and symbol name are the same, then this is the 1446 // version definition symbol. These symbols exist to 1447 // support using -u to pull in particular versions. We 1448 // do not want to record a version for them. 1449 if (st_shndx == elfcpp::SHN_ABS 1450 && !is_ordinary 1451 && name_key == version_key) 1452 res = this->add_from_object(dynobj, name, name_key, NULL, 0, 1453 false, *psym, st_shndx, is_ordinary, 1454 st_shndx); 1455 else 1456 { 1457 const bool is_default_version = 1458 !hidden && st_shndx != elfcpp::SHN_UNDEF; 1459 res = this->add_from_object(dynobj, name, name_key, version, 1460 version_key, is_default_version, 1461 *psym, st_shndx, 1462 is_ordinary, st_shndx); 1463 } 1464 } 1465 } 1466 1467 // Note that it is possible that RES was overridden by an 1468 // earlier object, in which case it can't be aliased here. 1469 if (st_shndx != elfcpp::SHN_UNDEF 1470 && is_ordinary 1471 && psym->get_st_type() == elfcpp::STT_OBJECT 1472 && res->source() == Symbol::FROM_OBJECT 1473 && res->object() == dynobj) 1474 object_symbols.push_back(res); 1475 1476 if (sympointers != NULL) 1477 (*sympointers)[i] = res; 1478 } 1479 1480 this->record_weak_aliases(&object_symbols); 1481} 1482 1483// This is used to sort weak aliases. We sort them first by section 1484// index, then by offset, then by weak ahead of strong. 1485 1486template<int size> 1487class Weak_alias_sorter 1488{ 1489 public: 1490 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const; 1491}; 1492 1493template<int size> 1494bool 1495Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1, 1496 const Sized_symbol<size>* s2) const 1497{ 1498 bool is_ordinary; 1499 unsigned int s1_shndx = s1->shndx(&is_ordinary); 1500 gold_assert(is_ordinary); 1501 unsigned int s2_shndx = s2->shndx(&is_ordinary); 1502 gold_assert(is_ordinary); 1503 if (s1_shndx != s2_shndx) 1504 return s1_shndx < s2_shndx; 1505 1506 if (s1->value() != s2->value()) 1507 return s1->value() < s2->value(); 1508 if (s1->binding() != s2->binding()) 1509 { 1510 if (s1->binding() == elfcpp::STB_WEAK) 1511 return true; 1512 if (s2->binding() == elfcpp::STB_WEAK) 1513 return false; 1514 } 1515 return std::string(s1->name()) < std::string(s2->name()); 1516} 1517 1518// SYMBOLS is a list of object symbols from a dynamic object. Look 1519// for any weak aliases, and record them so that if we add the weak 1520// alias to the dynamic symbol table, we also add the corresponding 1521// strong symbol. 1522 1523template<int size> 1524void 1525Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols) 1526{ 1527 // Sort the vector by section index, then by offset, then by weak 1528 // ahead of strong. 1529 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>()); 1530 1531 // Walk through the vector. For each weak definition, record 1532 // aliases. 1533 for (typename std::vector<Sized_symbol<size>*>::const_iterator p = 1534 symbols->begin(); 1535 p != symbols->end(); 1536 ++p) 1537 { 1538 if ((*p)->binding() != elfcpp::STB_WEAK) 1539 continue; 1540 1541 // Build a circular list of weak aliases. Each symbol points to 1542 // the next one in the circular list. 1543 1544 Sized_symbol<size>* from_sym = *p; 1545 typename std::vector<Sized_symbol<size>*>::const_iterator q; 1546 for (q = p + 1; q != symbols->end(); ++q) 1547 { 1548 bool dummy; 1549 if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy) 1550 || (*q)->value() != from_sym->value()) 1551 break; 1552 1553 this->weak_aliases_[from_sym] = *q; 1554 from_sym->set_has_alias(); 1555 from_sym = *q; 1556 } 1557 1558 if (from_sym != *p) 1559 { 1560 this->weak_aliases_[from_sym] = *p; 1561 from_sym->set_has_alias(); 1562 } 1563 1564 p = q - 1; 1565 } 1566} 1567 1568// Create and return a specially defined symbol. If ONLY_IF_REF is 1569// true, then only create the symbol if there is a reference to it. 1570// If this does not return NULL, it sets *POLDSYM to the existing 1571// symbol if there is one. This sets *RESOLVE_OLDSYM if we should 1572// resolve the newly created symbol to the old one. This 1573// canonicalizes *PNAME and *PVERSION. 1574 1575template<int size, bool big_endian> 1576Sized_symbol<size>* 1577Symbol_table::define_special_symbol(const char** pname, const char** pversion, 1578 bool only_if_ref, 1579 Sized_symbol<size>** poldsym, 1580 bool* resolve_oldsym) 1581{ 1582 *resolve_oldsym = false; 1583 1584 // If the caller didn't give us a version, see if we get one from 1585 // the version script. 1586 std::string v; 1587 bool is_default_version = false; 1588 if (*pversion == NULL) 1589 { 1590 bool is_global; 1591 if (this->version_script_.get_symbol_version(*pname, &v, &is_global)) 1592 { 1593 if (is_global && !v.empty()) 1594 { 1595 *pversion = v.c_str(); 1596 // If we get the version from a version script, then we 1597 // are also the default version. 1598 is_default_version = true; 1599 } 1600 } 1601 } 1602 1603 Symbol* oldsym; 1604 Sized_symbol<size>* sym; 1605 1606 bool add_to_table = false; 1607 typename Symbol_table_type::iterator add_loc = this->table_.end(); 1608 bool add_def_to_table = false; 1609 typename Symbol_table_type::iterator add_def_loc = this->table_.end(); 1610 1611 if (only_if_ref) 1612 { 1613 oldsym = this->lookup(*pname, *pversion); 1614 if (oldsym == NULL && is_default_version) 1615 oldsym = this->lookup(*pname, NULL); 1616 if (oldsym == NULL || !oldsym->is_undefined()) 1617 return NULL; 1618 1619 *pname = oldsym->name(); 1620 if (!is_default_version) 1621 *pversion = oldsym->version(); 1622 } 1623 else 1624 { 1625 // Canonicalize NAME and VERSION. 1626 Stringpool::Key name_key; 1627 *pname = this->namepool_.add(*pname, true, &name_key); 1628 1629 Stringpool::Key version_key = 0; 1630 if (*pversion != NULL) 1631 *pversion = this->namepool_.add(*pversion, true, &version_key); 1632 1633 Symbol* const snull = NULL; 1634 std::pair<typename Symbol_table_type::iterator, bool> ins = 1635 this->table_.insert(std::make_pair(std::make_pair(name_key, 1636 version_key), 1637 snull)); 1638 1639 std::pair<typename Symbol_table_type::iterator, bool> insdefault = 1640 std::make_pair(this->table_.end(), false); 1641 if (is_default_version) 1642 { 1643 const Stringpool::Key vnull = 0; 1644 insdefault = 1645 this->table_.insert(std::make_pair(std::make_pair(name_key, 1646 vnull), 1647 snull)); 1648 } 1649 1650 if (!ins.second) 1651 { 1652 // We already have a symbol table entry for NAME/VERSION. 1653 oldsym = ins.first->second; 1654 gold_assert(oldsym != NULL); 1655 1656 if (is_default_version) 1657 { 1658 Sized_symbol<size>* soldsym = 1659 this->get_sized_symbol<size>(oldsym); 1660 this->define_default_version<size, big_endian>(soldsym, 1661 insdefault.second, 1662 insdefault.first); 1663 } 1664 } 1665 else 1666 { 1667 // We haven't seen this symbol before. 1668 gold_assert(ins.first->second == NULL); 1669 1670 add_to_table = true; 1671 add_loc = ins.first; 1672 1673 if (is_default_version && !insdefault.second) 1674 { 1675 // We are adding NAME/VERSION, and it is the default 1676 // version. We already have an entry for NAME/NULL. 1677 oldsym = insdefault.first->second; 1678 *resolve_oldsym = true; 1679 } 1680 else 1681 { 1682 oldsym = NULL; 1683 1684 if (is_default_version) 1685 { 1686 add_def_to_table = true; 1687 add_def_loc = insdefault.first; 1688 } 1689 } 1690 } 1691 } 1692 1693 const Target& target = parameters->target(); 1694 if (!target.has_make_symbol()) 1695 sym = new Sized_symbol<size>(); 1696 else 1697 { 1698 Sized_target<size, big_endian>* sized_target = 1699 parameters->sized_target<size, big_endian>(); 1700 sym = sized_target->make_symbol(); 1701 if (sym == NULL) 1702 return NULL; 1703 } 1704 1705 if (add_to_table) 1706 add_loc->second = sym; 1707 else 1708 gold_assert(oldsym != NULL); 1709 1710 if (add_def_to_table) 1711 add_def_loc->second = sym; 1712 1713 *poldsym = this->get_sized_symbol<size>(oldsym); 1714 1715 return sym; 1716} 1717 1718// Define a symbol based on an Output_data. 1719 1720Symbol* 1721Symbol_table::define_in_output_data(const char* name, 1722 const char* version, 1723 Defined defined, 1724 Output_data* od, 1725 uint64_t value, 1726 uint64_t symsize, 1727 elfcpp::STT type, 1728 elfcpp::STB binding, 1729 elfcpp::STV visibility, 1730 unsigned char nonvis, 1731 bool offset_is_from_end, 1732 bool only_if_ref) 1733{ 1734 if (parameters->target().get_size() == 32) 1735 { 1736#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) 1737 return this->do_define_in_output_data<32>(name, version, defined, od, 1738 value, symsize, type, binding, 1739 visibility, nonvis, 1740 offset_is_from_end, 1741 only_if_ref); 1742#else 1743 gold_unreachable(); 1744#endif 1745 } 1746 else if (parameters->target().get_size() == 64) 1747 { 1748#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) 1749 return this->do_define_in_output_data<64>(name, version, defined, od, 1750 value, symsize, type, binding, 1751 visibility, nonvis, 1752 offset_is_from_end, 1753 only_if_ref); 1754#else 1755 gold_unreachable(); 1756#endif 1757 } 1758 else 1759 gold_unreachable(); 1760} 1761 1762// Define a symbol in an Output_data, sized version. 1763 1764template<int size> 1765Sized_symbol<size>* 1766Symbol_table::do_define_in_output_data( 1767 const char* name, 1768 const char* version, 1769 Defined defined, 1770 Output_data* od, 1771 typename elfcpp::Elf_types<size>::Elf_Addr value, 1772 typename elfcpp::Elf_types<size>::Elf_WXword symsize, 1773 elfcpp::STT type, 1774 elfcpp::STB binding, 1775 elfcpp::STV visibility, 1776 unsigned char nonvis, 1777 bool offset_is_from_end, 1778 bool only_if_ref) 1779{ 1780 Sized_symbol<size>* sym; 1781 Sized_symbol<size>* oldsym; 1782 bool resolve_oldsym; 1783 1784 if (parameters->target().is_big_endian()) 1785 { 1786#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) 1787 sym = this->define_special_symbol<size, true>(&name, &version, 1788 only_if_ref, &oldsym, 1789 &resolve_oldsym); 1790#else 1791 gold_unreachable(); 1792#endif 1793 } 1794 else 1795 { 1796#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) 1797 sym = this->define_special_symbol<size, false>(&name, &version, 1798 only_if_ref, &oldsym, 1799 &resolve_oldsym); 1800#else 1801 gold_unreachable(); 1802#endif 1803 } 1804 1805 if (sym == NULL) 1806 return NULL; 1807 1808 sym->init_output_data(name, version, od, value, symsize, type, binding, 1809 visibility, nonvis, offset_is_from_end); 1810 1811 if (oldsym == NULL) 1812 { 1813 if (binding == elfcpp::STB_LOCAL 1814 || this->version_script_.symbol_is_local(name)) 1815 this->force_local(sym); 1816 else if (version != NULL) 1817 sym->set_is_default(); 1818 return sym; 1819 } 1820 1821 if (Symbol_table::should_override_with_special(oldsym, defined)) 1822 this->override_with_special(oldsym, sym); 1823 1824 if (resolve_oldsym) 1825 return sym; 1826 else 1827 { 1828 delete sym; 1829 return oldsym; 1830 } 1831} 1832 1833// Define a symbol based on an Output_segment. 1834 1835Symbol* 1836Symbol_table::define_in_output_segment(const char* name, 1837 const char* version, 1838 Defined defined, 1839 Output_segment* os, 1840 uint64_t value, 1841 uint64_t symsize, 1842 elfcpp::STT type, 1843 elfcpp::STB binding, 1844 elfcpp::STV visibility, 1845 unsigned char nonvis, 1846 Symbol::Segment_offset_base offset_base, 1847 bool only_if_ref) 1848{ 1849 if (parameters->target().get_size() == 32) 1850 { 1851#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) 1852 return this->do_define_in_output_segment<32>(name, version, defined, os, 1853 value, symsize, type, 1854 binding, visibility, nonvis, 1855 offset_base, only_if_ref); 1856#else 1857 gold_unreachable(); 1858#endif 1859 } 1860 else if (parameters->target().get_size() == 64) 1861 { 1862#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) 1863 return this->do_define_in_output_segment<64>(name, version, defined, os, 1864 value, symsize, type, 1865 binding, visibility, nonvis, 1866 offset_base, only_if_ref); 1867#else 1868 gold_unreachable(); 1869#endif 1870 } 1871 else 1872 gold_unreachable(); 1873} 1874 1875// Define a symbol in an Output_segment, sized version. 1876 1877template<int size> 1878Sized_symbol<size>* 1879Symbol_table::do_define_in_output_segment( 1880 const char* name, 1881 const char* version, 1882 Defined defined, 1883 Output_segment* os, 1884 typename elfcpp::Elf_types<size>::Elf_Addr value, 1885 typename elfcpp::Elf_types<size>::Elf_WXword symsize, 1886 elfcpp::STT type, 1887 elfcpp::STB binding, 1888 elfcpp::STV visibility, 1889 unsigned char nonvis, 1890 Symbol::Segment_offset_base offset_base, 1891 bool only_if_ref) 1892{ 1893 Sized_symbol<size>* sym; 1894 Sized_symbol<size>* oldsym; 1895 bool resolve_oldsym; 1896 1897 if (parameters->target().is_big_endian()) 1898 { 1899#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) 1900 sym = this->define_special_symbol<size, true>(&name, &version, 1901 only_if_ref, &oldsym, 1902 &resolve_oldsym); 1903#else 1904 gold_unreachable(); 1905#endif 1906 } 1907 else 1908 { 1909#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) 1910 sym = this->define_special_symbol<size, false>(&name, &version, 1911 only_if_ref, &oldsym, 1912 &resolve_oldsym); 1913#else 1914 gold_unreachable(); 1915#endif 1916 } 1917 1918 if (sym == NULL) 1919 return NULL; 1920 1921 sym->init_output_segment(name, version, os, value, symsize, type, binding, 1922 visibility, nonvis, offset_base); 1923 1924 if (oldsym == NULL) 1925 { 1926 if (binding == elfcpp::STB_LOCAL 1927 || this->version_script_.symbol_is_local(name)) 1928 this->force_local(sym); 1929 else if (version != NULL) 1930 sym->set_is_default(); 1931 return sym; 1932 } 1933 1934 if (Symbol_table::should_override_with_special(oldsym, defined)) 1935 this->override_with_special(oldsym, sym); 1936 1937 if (resolve_oldsym) 1938 return sym; 1939 else 1940 { 1941 delete sym; 1942 return oldsym; 1943 } 1944} 1945 1946// Define a special symbol with a constant value. It is a multiple 1947// definition error if this symbol is already defined. 1948 1949Symbol* 1950Symbol_table::define_as_constant(const char* name, 1951 const char* version, 1952 Defined defined, 1953 uint64_t value, 1954 uint64_t symsize, 1955 elfcpp::STT type, 1956 elfcpp::STB binding, 1957 elfcpp::STV visibility, 1958 unsigned char nonvis, 1959 bool only_if_ref, 1960 bool force_override) 1961{ 1962 if (parameters->target().get_size() == 32) 1963 { 1964#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) 1965 return this->do_define_as_constant<32>(name, version, defined, value, 1966 symsize, type, binding, 1967 visibility, nonvis, only_if_ref, 1968 force_override); 1969#else 1970 gold_unreachable(); 1971#endif 1972 } 1973 else if (parameters->target().get_size() == 64) 1974 { 1975#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) 1976 return this->do_define_as_constant<64>(name, version, defined, value, 1977 symsize, type, binding, 1978 visibility, nonvis, only_if_ref, 1979 force_override); 1980#else 1981 gold_unreachable(); 1982#endif 1983 } 1984 else 1985 gold_unreachable(); 1986} 1987 1988// Define a symbol as a constant, sized version. 1989 1990template<int size> 1991Sized_symbol<size>* 1992Symbol_table::do_define_as_constant( 1993 const char* name, 1994 const char* version, 1995 Defined defined, 1996 typename elfcpp::Elf_types<size>::Elf_Addr value, 1997 typename elfcpp::Elf_types<size>::Elf_WXword symsize, 1998 elfcpp::STT type, 1999 elfcpp::STB binding, 2000 elfcpp::STV visibility, 2001 unsigned char nonvis, 2002 bool only_if_ref, 2003 bool force_override) 2004{ 2005 Sized_symbol<size>* sym; 2006 Sized_symbol<size>* oldsym; 2007 bool resolve_oldsym; 2008 2009 if (parameters->target().is_big_endian()) 2010 { 2011#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) 2012 sym = this->define_special_symbol<size, true>(&name, &version, 2013 only_if_ref, &oldsym, 2014 &resolve_oldsym); 2015#else 2016 gold_unreachable(); 2017#endif 2018 } 2019 else 2020 { 2021#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) 2022 sym = this->define_special_symbol<size, false>(&name, &version, 2023 only_if_ref, &oldsym, 2024 &resolve_oldsym); 2025#else 2026 gold_unreachable(); 2027#endif 2028 } 2029 2030 if (sym == NULL) 2031 return NULL; 2032 2033 sym->init_constant(name, version, value, symsize, type, binding, visibility, 2034 nonvis); 2035 2036 if (oldsym == NULL) 2037 { 2038 // Version symbols are absolute symbols with name == version. 2039 // We don't want to force them to be local. 2040 if ((version == NULL 2041 || name != version 2042 || value != 0) 2043 && (binding == elfcpp::STB_LOCAL 2044 || this->version_script_.symbol_is_local(name))) 2045 this->force_local(sym); 2046 else if (version != NULL 2047 && (name != version || value != 0)) 2048 sym->set_is_default(); 2049 return sym; 2050 } 2051 2052 if (force_override 2053 || Symbol_table::should_override_with_special(oldsym, defined)) 2054 this->override_with_special(oldsym, sym); 2055 2056 if (resolve_oldsym) 2057 return sym; 2058 else 2059 { 2060 delete sym; 2061 return oldsym; 2062 } 2063} 2064 2065// Define a set of symbols in output sections. 2066 2067void 2068Symbol_table::define_symbols(const Layout* layout, int count, 2069 const Define_symbol_in_section* p, 2070 bool only_if_ref) 2071{ 2072 for (int i = 0; i < count; ++i, ++p) 2073 { 2074 Output_section* os = layout->find_output_section(p->output_section); 2075 if (os != NULL) 2076 this->define_in_output_data(p->name, NULL, PREDEFINED, os, p->value, 2077 p->size, p->type, p->binding, 2078 p->visibility, p->nonvis, 2079 p->offset_is_from_end, 2080 only_if_ref || p->only_if_ref); 2081 else 2082 this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size, 2083 p->type, p->binding, p->visibility, p->nonvis, 2084 only_if_ref || p->only_if_ref, 2085 false); 2086 } 2087} 2088 2089// Define a set of symbols in output segments. 2090 2091void 2092Symbol_table::define_symbols(const Layout* layout, int count, 2093 const Define_symbol_in_segment* p, 2094 bool only_if_ref) 2095{ 2096 for (int i = 0; i < count; ++i, ++p) 2097 { 2098 Output_segment* os = layout->find_output_segment(p->segment_type, 2099 p->segment_flags_set, 2100 p->segment_flags_clear); 2101 if (os != NULL) 2102 this->define_in_output_segment(p->name, NULL, PREDEFINED, os, p->value, 2103 p->size, p->type, p->binding, 2104 p->visibility, p->nonvis, 2105 p->offset_base, 2106 only_if_ref || p->only_if_ref); 2107 else 2108 this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size, 2109 p->type, p->binding, p->visibility, p->nonvis, 2110 only_if_ref || p->only_if_ref, 2111 false); 2112 } 2113} 2114 2115// Define CSYM using a COPY reloc. POSD is the Output_data where the 2116// symbol should be defined--typically a .dyn.bss section. VALUE is 2117// the offset within POSD. 2118 2119template<int size> 2120void 2121Symbol_table::define_with_copy_reloc( 2122 Sized_symbol<size>* csym, 2123 Output_data* posd, 2124 typename elfcpp::Elf_types<size>::Elf_Addr value) 2125{ 2126 gold_assert(csym->is_from_dynobj()); 2127 gold_assert(!csym->is_copied_from_dynobj()); 2128 Object* object = csym->object(); 2129 gold_assert(object->is_dynamic()); 2130 Dynobj* dynobj = static_cast<Dynobj*>(object); 2131 2132 // Our copied variable has to override any variable in a shared 2133 // library. 2134 elfcpp::STB binding = csym->binding(); 2135 if (binding == elfcpp::STB_WEAK) 2136 binding = elfcpp::STB_GLOBAL; 2137 2138 this->define_in_output_data(csym->name(), csym->version(), COPY, 2139 posd, value, csym->symsize(), 2140 csym->type(), binding, 2141 csym->visibility(), csym->nonvis(), 2142 false, false); 2143 2144 csym->set_is_copied_from_dynobj(); 2145 csym->set_needs_dynsym_entry(); 2146 2147 this->copied_symbol_dynobjs_[csym] = dynobj; 2148 2149 // We have now defined all aliases, but we have not entered them all 2150 // in the copied_symbol_dynobjs_ map. 2151 if (csym->has_alias()) 2152 { 2153 Symbol* sym = csym; 2154 while (true) 2155 { 2156 sym = this->weak_aliases_[sym]; 2157 if (sym == csym) 2158 break; 2159 gold_assert(sym->output_data() == posd); 2160 2161 sym->set_is_copied_from_dynobj(); 2162 this->copied_symbol_dynobjs_[sym] = dynobj; 2163 } 2164 } 2165} 2166 2167// SYM is defined using a COPY reloc. Return the dynamic object where 2168// the original definition was found. 2169 2170Dynobj* 2171Symbol_table::get_copy_source(const Symbol* sym) const 2172{ 2173 gold_assert(sym->is_copied_from_dynobj()); 2174 Copied_symbol_dynobjs::const_iterator p = 2175 this->copied_symbol_dynobjs_.find(sym); 2176 gold_assert(p != this->copied_symbol_dynobjs_.end()); 2177 return p->second; 2178} 2179 2180// Add any undefined symbols named on the command line. 2181 2182void 2183Symbol_table::add_undefined_symbols_from_command_line(Layout* layout) 2184{ 2185 if (parameters->options().any_undefined() 2186 || layout->script_options()->any_unreferenced()) 2187 { 2188 if (parameters->target().get_size() == 32) 2189 { 2190#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) 2191 this->do_add_undefined_symbols_from_command_line<32>(layout); 2192#else 2193 gold_unreachable(); 2194#endif 2195 } 2196 else if (parameters->target().get_size() == 64) 2197 { 2198#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) 2199 this->do_add_undefined_symbols_from_command_line<64>(layout); 2200#else 2201 gold_unreachable(); 2202#endif 2203 } 2204 else 2205 gold_unreachable(); 2206 } 2207} 2208 2209template<int size> 2210void 2211Symbol_table::do_add_undefined_symbols_from_command_line(Layout* layout) 2212{ 2213 for (options::String_set::const_iterator p = 2214 parameters->options().undefined_begin(); 2215 p != parameters->options().undefined_end(); 2216 ++p) 2217 this->add_undefined_symbol_from_command_line<size>(p->c_str()); 2218 2219 for (Script_options::referenced_const_iterator p = 2220 layout->script_options()->referenced_begin(); 2221 p != layout->script_options()->referenced_end(); 2222 ++p) 2223 this->add_undefined_symbol_from_command_line<size>(p->c_str()); 2224} 2225 2226template<int size> 2227void 2228Symbol_table::add_undefined_symbol_from_command_line(const char* name) 2229{ 2230 if (this->lookup(name) != NULL) 2231 return; 2232 2233 const char* version = NULL; 2234 2235 Sized_symbol<size>* sym; 2236 Sized_symbol<size>* oldsym; 2237 bool resolve_oldsym; 2238 if (parameters->target().is_big_endian()) 2239 { 2240#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) 2241 sym = this->define_special_symbol<size, true>(&name, &version, 2242 false, &oldsym, 2243 &resolve_oldsym); 2244#else 2245 gold_unreachable(); 2246#endif 2247 } 2248 else 2249 { 2250#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) 2251 sym = this->define_special_symbol<size, false>(&name, &version, 2252 false, &oldsym, 2253 &resolve_oldsym); 2254#else 2255 gold_unreachable(); 2256#endif 2257 } 2258 2259 gold_assert(oldsym == NULL); 2260 2261 sym->init_undefined(name, version, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, 2262 elfcpp::STV_DEFAULT, 0); 2263 ++this->saw_undefined_; 2264} 2265 2266// Set the dynamic symbol indexes. INDEX is the index of the first 2267// global dynamic symbol. Pointers to the symbols are stored into the 2268// vector SYMS. The names are added to DYNPOOL. This returns an 2269// updated dynamic symbol index. 2270 2271unsigned int 2272Symbol_table::set_dynsym_indexes(unsigned int index, 2273 std::vector<Symbol*>* syms, 2274 Stringpool* dynpool, 2275 Versions* versions) 2276{ 2277 for (Symbol_table_type::iterator p = this->table_.begin(); 2278 p != this->table_.end(); 2279 ++p) 2280 { 2281 Symbol* sym = p->second; 2282 2283 // Note that SYM may already have a dynamic symbol index, since 2284 // some symbols appear more than once in the symbol table, with 2285 // and without a version. 2286 2287 if (!sym->should_add_dynsym_entry(this)) 2288 sym->set_dynsym_index(-1U); 2289 else if (!sym->has_dynsym_index()) 2290 { 2291 sym->set_dynsym_index(index); 2292 ++index; 2293 syms->push_back(sym); 2294 dynpool->add(sym->name(), false, NULL); 2295 2296 // Record any version information. 2297 if (sym->version() != NULL) 2298 versions->record_version(this, dynpool, sym); 2299 2300 // If the symbol is defined in a dynamic object and is 2301 // referenced in a regular object, then mark the dynamic 2302 // object as needed. This is used to implement --as-needed. 2303 if (sym->is_from_dynobj() && sym->in_reg()) 2304 sym->object()->set_is_needed(); 2305 } 2306 } 2307 2308 // Finish up the versions. In some cases this may add new dynamic 2309 // symbols. 2310 index = versions->finalize(this, index, syms); 2311 2312 return index; 2313} 2314 2315// Set the final values for all the symbols. The index of the first 2316// global symbol in the output file is *PLOCAL_SYMCOUNT. Record the 2317// file offset OFF. Add their names to POOL. Return the new file 2318// offset. Update *PLOCAL_SYMCOUNT if necessary. 2319 2320off_t 2321Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index, 2322 size_t dyncount, Stringpool* pool, 2323 unsigned int* plocal_symcount) 2324{ 2325 off_t ret; 2326 2327 gold_assert(*plocal_symcount != 0); 2328 this->first_global_index_ = *plocal_symcount; 2329 2330 this->dynamic_offset_ = dynoff; 2331 this->first_dynamic_global_index_ = dyn_global_index; 2332 this->dynamic_count_ = dyncount; 2333 2334 if (parameters->target().get_size() == 32) 2335 { 2336#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE) 2337 ret = this->sized_finalize<32>(off, pool, plocal_symcount); 2338#else 2339 gold_unreachable(); 2340#endif 2341 } 2342 else if (parameters->target().get_size() == 64) 2343 { 2344#if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE) 2345 ret = this->sized_finalize<64>(off, pool, plocal_symcount); 2346#else 2347 gold_unreachable(); 2348#endif 2349 } 2350 else 2351 gold_unreachable(); 2352 2353 // Now that we have the final symbol table, we can reliably note 2354 // which symbols should get warnings. 2355 this->warnings_.note_warnings(this); 2356 2357 return ret; 2358} 2359 2360// SYM is going into the symbol table at *PINDEX. Add the name to 2361// POOL, update *PINDEX and *POFF. 2362 2363template<int size> 2364void 2365Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool, 2366 unsigned int* pindex, off_t* poff) 2367{ 2368 sym->set_symtab_index(*pindex); 2369 pool->add(sym->name(), false, NULL); 2370 ++*pindex; 2371 *poff += elfcpp::Elf_sizes<size>::sym_size; 2372} 2373 2374// Set the final value for all the symbols. This is called after 2375// Layout::finalize, so all the output sections have their final 2376// address. 2377 2378template<int size> 2379off_t 2380Symbol_table::sized_finalize(off_t off, Stringpool* pool, 2381 unsigned int* plocal_symcount) 2382{ 2383 off = align_address(off, size >> 3); 2384 this->offset_ = off; 2385 2386 unsigned int index = *plocal_symcount; 2387 const unsigned int orig_index = index; 2388 2389 // First do all the symbols which have been forced to be local, as 2390 // they must appear before all global symbols. 2391 for (Forced_locals::iterator p = this->forced_locals_.begin(); 2392 p != this->forced_locals_.end(); 2393 ++p) 2394 { 2395 Symbol* sym = *p; 2396 gold_assert(sym->is_forced_local()); 2397 if (this->sized_finalize_symbol<size>(sym)) 2398 { 2399 this->add_to_final_symtab<size>(sym, pool, &index, &off); 2400 ++*plocal_symcount; 2401 } 2402 } 2403 2404 // Now do all the remaining symbols. 2405 for (Symbol_table_type::iterator p = this->table_.begin(); 2406 p != this->table_.end(); 2407 ++p) 2408 { 2409 Symbol* sym = p->second; 2410 if (this->sized_finalize_symbol<size>(sym)) 2411 this->add_to_final_symtab<size>(sym, pool, &index, &off); 2412 } 2413 2414 this->output_count_ = index - orig_index; 2415 2416 return off; 2417} 2418 2419// Compute the final value of SYM and store status in location PSTATUS. 2420// During relaxation, this may be called multiple times for a symbol to 2421// compute its would-be final value in each relaxation pass. 2422 2423template<int size> 2424typename Sized_symbol<size>::Value_type 2425Symbol_table::compute_final_value( 2426 const Sized_symbol<size>* sym, 2427 Compute_final_value_status* pstatus) const 2428{ 2429 typedef typename Sized_symbol<size>::Value_type Value_type; 2430 Value_type value; 2431 2432 switch (sym->source()) 2433 { 2434 case Symbol::FROM_OBJECT: 2435 { 2436 bool is_ordinary; 2437 unsigned int shndx = sym->shndx(&is_ordinary); 2438 2439 if (!is_ordinary 2440 && shndx != elfcpp::SHN_ABS 2441 && !Symbol::is_common_shndx(shndx)) 2442 { 2443 *pstatus = CFVS_UNSUPPORTED_SYMBOL_SECTION; 2444 return 0; 2445 } 2446 2447 Object* symobj = sym->object(); 2448 if (symobj->is_dynamic()) 2449 { 2450 value = 0; 2451 shndx = elfcpp::SHN_UNDEF; 2452 } 2453 else if (symobj->pluginobj() != NULL) 2454 { 2455 value = 0; 2456 shndx = elfcpp::SHN_UNDEF; 2457 } 2458 else if (shndx == elfcpp::SHN_UNDEF) 2459 value = 0; 2460 else if (!is_ordinary 2461 && (shndx == elfcpp::SHN_ABS 2462 || Symbol::is_common_shndx(shndx))) 2463 value = sym->value(); 2464 else 2465 { 2466 Relobj* relobj = static_cast<Relobj*>(symobj); 2467 Output_section* os = relobj->output_section(shndx); 2468 2469 if (this->is_section_folded(relobj, shndx)) 2470 { 2471 gold_assert(os == NULL); 2472 // Get the os of the section it is folded onto. 2473 Section_id folded = this->icf_->get_folded_section(relobj, 2474 shndx); 2475 gold_assert(folded.first != NULL); 2476 Relobj* folded_obj = reinterpret_cast<Relobj*>(folded.first); 2477 unsigned folded_shndx = folded.second; 2478 2479 os = folded_obj->output_section(folded_shndx); 2480 gold_assert(os != NULL); 2481 2482 // Replace (relobj, shndx) with canonical ICF input section. 2483 shndx = folded_shndx; 2484 relobj = folded_obj; 2485 } 2486 2487 uint64_t secoff64 = relobj->output_section_offset(shndx); 2488 if (os == NULL) 2489 { 2490 bool static_or_reloc = (parameters->doing_static_link() || 2491 parameters->options().relocatable()); 2492 gold_assert(static_or_reloc || sym->dynsym_index() == -1U); 2493 2494 *pstatus = CFVS_NO_OUTPUT_SECTION; 2495 return 0; 2496 } 2497 2498 if (secoff64 == -1ULL) 2499 { 2500 // The section needs special handling (e.g., a merge section). 2501 2502 value = os->output_address(relobj, shndx, sym->value()); 2503 } 2504 else 2505 { 2506 Value_type secoff = 2507 convert_types<Value_type, uint64_t>(secoff64); 2508 if (sym->type() == elfcpp::STT_TLS) 2509 value = sym->value() + os->tls_offset() + secoff; 2510 else 2511 value = sym->value() + os->address() + secoff; 2512 } 2513 } 2514 } 2515 break; 2516 2517 case Symbol::IN_OUTPUT_DATA: 2518 { 2519 Output_data* od = sym->output_data(); 2520 value = sym->value(); 2521 if (sym->type() != elfcpp::STT_TLS) 2522 value += od->address(); 2523 else 2524 { 2525 Output_section* os = od->output_section(); 2526 gold_assert(os != NULL); 2527 value += os->tls_offset() + (od->address() - os->address()); 2528 } 2529 if (sym->offset_is_from_end()) 2530 value += od->data_size(); 2531 } 2532 break; 2533 2534 case Symbol::IN_OUTPUT_SEGMENT: 2535 { 2536 Output_segment* os = sym->output_segment(); 2537 value = sym->value(); 2538 if (sym->type() != elfcpp::STT_TLS) 2539 value += os->vaddr(); 2540 switch (sym->offset_base()) 2541 { 2542 case Symbol::SEGMENT_START: 2543 break; 2544 case Symbol::SEGMENT_END: 2545 value += os->memsz(); 2546 break; 2547 case Symbol::SEGMENT_BSS: 2548 value += os->filesz(); 2549 break; 2550 default: 2551 gold_unreachable(); 2552 } 2553 } 2554 break; 2555 2556 case Symbol::IS_CONSTANT: 2557 value = sym->value(); 2558 break; 2559 2560 case Symbol::IS_UNDEFINED: 2561 value = 0; 2562 break; 2563 2564 default: 2565 gold_unreachable(); 2566 } 2567 2568 *pstatus = CFVS_OK; 2569 return value; 2570} 2571 2572// Finalize the symbol SYM. This returns true if the symbol should be 2573// added to the symbol table, false otherwise. 2574 2575template<int size> 2576bool 2577Symbol_table::sized_finalize_symbol(Symbol* unsized_sym) 2578{ 2579 typedef typename Sized_symbol<size>::Value_type Value_type; 2580 2581 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym); 2582 2583 // The default version of a symbol may appear twice in the symbol 2584 // table. We only need to finalize it once. 2585 if (sym->has_symtab_index()) 2586 return false; 2587 2588 if (!sym->in_reg()) 2589 { 2590 gold_assert(!sym->has_symtab_index()); 2591 sym->set_symtab_index(-1U); 2592 gold_assert(sym->dynsym_index() == -1U); 2593 return false; 2594 } 2595 2596 // Compute final symbol value. 2597 Compute_final_value_status status; 2598 Value_type value = this->compute_final_value(sym, &status); 2599 2600 switch (status) 2601 { 2602 case CFVS_OK: 2603 break; 2604 case CFVS_UNSUPPORTED_SYMBOL_SECTION: 2605 { 2606 bool is_ordinary; 2607 unsigned int shndx = sym->shndx(&is_ordinary); 2608 gold_error(_("%s: unsupported symbol section 0x%x"), 2609 sym->demangled_name().c_str(), shndx); 2610 } 2611 break; 2612 case CFVS_NO_OUTPUT_SECTION: 2613 sym->set_symtab_index(-1U); 2614 return false; 2615 default: 2616 gold_unreachable(); 2617 } 2618 2619 sym->set_value(value); 2620 2621 if (parameters->options().strip_all() 2622 || !parameters->options().should_retain_symbol(sym->name())) 2623 { 2624 sym->set_symtab_index(-1U); 2625 return false; 2626 } 2627 2628 return true; 2629} 2630 2631// Write out the global symbols. 2632 2633void 2634Symbol_table::write_globals(const Stringpool* sympool, 2635 const Stringpool* dynpool, 2636 Output_symtab_xindex* symtab_xindex, 2637 Output_symtab_xindex* dynsym_xindex, 2638 Output_file* of) const 2639{ 2640 switch (parameters->size_and_endianness()) 2641 { 2642#ifdef HAVE_TARGET_32_LITTLE 2643 case Parameters::TARGET_32_LITTLE: 2644 this->sized_write_globals<32, false>(sympool, dynpool, symtab_xindex, 2645 dynsym_xindex, of); 2646 break; 2647#endif 2648#ifdef HAVE_TARGET_32_BIG 2649 case Parameters::TARGET_32_BIG: 2650 this->sized_write_globals<32, true>(sympool, dynpool, symtab_xindex, 2651 dynsym_xindex, of); 2652 break; 2653#endif 2654#ifdef HAVE_TARGET_64_LITTLE 2655 case Parameters::TARGET_64_LITTLE: 2656 this->sized_write_globals<64, false>(sympool, dynpool, symtab_xindex, 2657 dynsym_xindex, of); 2658 break; 2659#endif 2660#ifdef HAVE_TARGET_64_BIG 2661 case Parameters::TARGET_64_BIG: 2662 this->sized_write_globals<64, true>(sympool, dynpool, symtab_xindex, 2663 dynsym_xindex, of); 2664 break; 2665#endif 2666 default: 2667 gold_unreachable(); 2668 } 2669} 2670 2671// Write out the global symbols. 2672 2673template<int size, bool big_endian> 2674void 2675Symbol_table::sized_write_globals(const Stringpool* sympool, 2676 const Stringpool* dynpool, 2677 Output_symtab_xindex* symtab_xindex, 2678 Output_symtab_xindex* dynsym_xindex, 2679 Output_file* of) const 2680{ 2681 const Target& target = parameters->target(); 2682 2683 const int sym_size = elfcpp::Elf_sizes<size>::sym_size; 2684 2685 const unsigned int output_count = this->output_count_; 2686 const section_size_type oview_size = output_count * sym_size; 2687 const unsigned int first_global_index = this->first_global_index_; 2688 unsigned char* psyms; 2689 if (this->offset_ == 0 || output_count == 0) 2690 psyms = NULL; 2691 else 2692 psyms = of->get_output_view(this->offset_, oview_size); 2693 2694 const unsigned int dynamic_count = this->dynamic_count_; 2695 const section_size_type dynamic_size = dynamic_count * sym_size; 2696 const unsigned int first_dynamic_global_index = 2697 this->first_dynamic_global_index_; 2698 unsigned char* dynamic_view; 2699 if (this->dynamic_offset_ == 0 || dynamic_count == 0) 2700 dynamic_view = NULL; 2701 else 2702 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size); 2703 2704 for (Symbol_table_type::const_iterator p = this->table_.begin(); 2705 p != this->table_.end(); 2706 ++p) 2707 { 2708 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); 2709 2710 // Possibly warn about unresolved symbols in shared libraries. 2711 this->warn_about_undefined_dynobj_symbol(sym); 2712 2713 unsigned int sym_index = sym->symtab_index(); 2714 unsigned int dynsym_index; 2715 if (dynamic_view == NULL) 2716 dynsym_index = -1U; 2717 else 2718 dynsym_index = sym->dynsym_index(); 2719 2720 if (sym_index == -1U && dynsym_index == -1U) 2721 { 2722 // This symbol is not included in the output file. 2723 continue; 2724 } 2725 2726 unsigned int shndx; 2727 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value(); 2728 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value; 2729 elfcpp::STB binding = sym->binding(); 2730 switch (sym->source()) 2731 { 2732 case Symbol::FROM_OBJECT: 2733 { 2734 bool is_ordinary; 2735 unsigned int in_shndx = sym->shndx(&is_ordinary); 2736 2737 if (!is_ordinary 2738 && in_shndx != elfcpp::SHN_ABS 2739 && !Symbol::is_common_shndx(in_shndx)) 2740 { 2741 gold_error(_("%s: unsupported symbol section 0x%x"), 2742 sym->demangled_name().c_str(), in_shndx); 2743 shndx = in_shndx; 2744 } 2745 else 2746 { 2747 Object* symobj = sym->object(); 2748 if (symobj->is_dynamic()) 2749 { 2750 if (sym->needs_dynsym_value()) 2751 dynsym_value = target.dynsym_value(sym); 2752 shndx = elfcpp::SHN_UNDEF; 2753 if (sym->is_undef_binding_weak()) 2754 binding = elfcpp::STB_WEAK; 2755 else 2756 binding = elfcpp::STB_GLOBAL; 2757 } 2758 else if (symobj->pluginobj() != NULL) 2759 shndx = elfcpp::SHN_UNDEF; 2760 else if (in_shndx == elfcpp::SHN_UNDEF 2761 || (!is_ordinary 2762 && (in_shndx == elfcpp::SHN_ABS 2763 || Symbol::is_common_shndx(in_shndx)))) 2764 shndx = in_shndx; 2765 else 2766 { 2767 Relobj* relobj = static_cast<Relobj*>(symobj); 2768 Output_section* os = relobj->output_section(in_shndx); 2769 if (this->is_section_folded(relobj, in_shndx)) 2770 { 2771 // This global symbol must be written out even though 2772 // it is folded. 2773 // Get the os of the section it is folded onto. 2774 Section_id folded = 2775 this->icf_->get_folded_section(relobj, in_shndx); 2776 gold_assert(folded.first !=NULL); 2777 Relobj* folded_obj = 2778 reinterpret_cast<Relobj*>(folded.first); 2779 os = folded_obj->output_section(folded.second); 2780 gold_assert(os != NULL); 2781 } 2782 gold_assert(os != NULL); 2783 shndx = os->out_shndx(); 2784 2785 if (shndx >= elfcpp::SHN_LORESERVE) 2786 { 2787 if (sym_index != -1U) 2788 symtab_xindex->add(sym_index, shndx); 2789 if (dynsym_index != -1U) 2790 dynsym_xindex->add(dynsym_index, shndx); 2791 shndx = elfcpp::SHN_XINDEX; 2792 } 2793 2794 // In object files symbol values are section 2795 // relative. 2796 if (parameters->options().relocatable()) 2797 sym_value -= os->address(); 2798 } 2799 } 2800 } 2801 break; 2802 2803 case Symbol::IN_OUTPUT_DATA: 2804 shndx = sym->output_data()->out_shndx(); 2805 if (shndx >= elfcpp::SHN_LORESERVE) 2806 { 2807 if (sym_index != -1U) 2808 symtab_xindex->add(sym_index, shndx); 2809 if (dynsym_index != -1U) 2810 dynsym_xindex->add(dynsym_index, shndx); 2811 shndx = elfcpp::SHN_XINDEX; 2812 } 2813 break; 2814 2815 case Symbol::IN_OUTPUT_SEGMENT: 2816 shndx = elfcpp::SHN_ABS; 2817 break; 2818 2819 case Symbol::IS_CONSTANT: 2820 shndx = elfcpp::SHN_ABS; 2821 break; 2822 2823 case Symbol::IS_UNDEFINED: 2824 shndx = elfcpp::SHN_UNDEF; 2825 break; 2826 2827 default: 2828 gold_unreachable(); 2829 } 2830 2831 if (sym_index != -1U) 2832 { 2833 sym_index -= first_global_index; 2834 gold_assert(sym_index < output_count); 2835 unsigned char* ps = psyms + (sym_index * sym_size); 2836 this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx, 2837 binding, sympool, ps); 2838 } 2839 2840 if (dynsym_index != -1U) 2841 { 2842 dynsym_index -= first_dynamic_global_index; 2843 gold_assert(dynsym_index < dynamic_count); 2844 unsigned char* pd = dynamic_view + (dynsym_index * sym_size); 2845 this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx, 2846 binding, dynpool, pd); 2847 } 2848 } 2849 2850 of->write_output_view(this->offset_, oview_size, psyms); 2851 if (dynamic_view != NULL) 2852 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view); 2853} 2854 2855// Write out the symbol SYM, in section SHNDX, to P. POOL is the 2856// strtab holding the name. 2857 2858template<int size, bool big_endian> 2859void 2860Symbol_table::sized_write_symbol( 2861 Sized_symbol<size>* sym, 2862 typename elfcpp::Elf_types<size>::Elf_Addr value, 2863 unsigned int shndx, 2864 elfcpp::STB binding, 2865 const Stringpool* pool, 2866 unsigned char* p) const 2867{ 2868 elfcpp::Sym_write<size, big_endian> osym(p); 2869 osym.put_st_name(pool->get_offset(sym->name())); 2870 osym.put_st_value(value); 2871 // Use a symbol size of zero for undefined symbols from shared libraries. 2872 if (shndx == elfcpp::SHN_UNDEF && sym->is_from_dynobj()) 2873 osym.put_st_size(0); 2874 else 2875 osym.put_st_size(sym->symsize()); 2876 elfcpp::STT type = sym->type(); 2877 // Turn IFUNC symbols from shared libraries into normal FUNC symbols. 2878 if (type == elfcpp::STT_GNU_IFUNC 2879 && sym->is_from_dynobj()) 2880 type = elfcpp::STT_FUNC; 2881 // A version script may have overridden the default binding. 2882 if (sym->is_forced_local()) 2883 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, type)); 2884 else 2885 osym.put_st_info(elfcpp::elf_st_info(binding, type)); 2886 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis())); 2887 osym.put_st_shndx(shndx); 2888} 2889 2890// Check for unresolved symbols in shared libraries. This is 2891// controlled by the --allow-shlib-undefined option. 2892 2893// We only warn about libraries for which we have seen all the 2894// DT_NEEDED entries. We don't try to track down DT_NEEDED entries 2895// which were not seen in this link. If we didn't see a DT_NEEDED 2896// entry, we aren't going to be able to reliably report whether the 2897// symbol is undefined. 2898 2899// We also don't warn about libraries found in a system library 2900// directory (e.g., /lib or /usr/lib); we assume that those libraries 2901// are OK. This heuristic avoids problems on GNU/Linux, in which -ldl 2902// can have undefined references satisfied by ld-linux.so. 2903 2904inline void 2905Symbol_table::warn_about_undefined_dynobj_symbol(Symbol* sym) const 2906{ 2907 bool dummy; 2908 if (sym->source() == Symbol::FROM_OBJECT 2909 && sym->object()->is_dynamic() 2910 && sym->shndx(&dummy) == elfcpp::SHN_UNDEF 2911 && sym->binding() != elfcpp::STB_WEAK 2912 && !parameters->options().allow_shlib_undefined() 2913 && !parameters->target().is_defined_by_abi(sym) 2914 && !sym->object()->is_in_system_directory()) 2915 { 2916 // A very ugly cast. 2917 Dynobj* dynobj = static_cast<Dynobj*>(sym->object()); 2918 if (!dynobj->has_unknown_needed_entries()) 2919 gold_undefined_symbol(sym); 2920 } 2921} 2922 2923// Write out a section symbol. Return the update offset. 2924 2925void 2926Symbol_table::write_section_symbol(const Output_section* os, 2927 Output_symtab_xindex* symtab_xindex, 2928 Output_file* of, 2929 off_t offset) const 2930{ 2931 switch (parameters->size_and_endianness()) 2932 { 2933#ifdef HAVE_TARGET_32_LITTLE 2934 case Parameters::TARGET_32_LITTLE: 2935 this->sized_write_section_symbol<32, false>(os, symtab_xindex, of, 2936 offset); 2937 break; 2938#endif 2939#ifdef HAVE_TARGET_32_BIG 2940 case Parameters::TARGET_32_BIG: 2941 this->sized_write_section_symbol<32, true>(os, symtab_xindex, of, 2942 offset); 2943 break; 2944#endif 2945#ifdef HAVE_TARGET_64_LITTLE 2946 case Parameters::TARGET_64_LITTLE: 2947 this->sized_write_section_symbol<64, false>(os, symtab_xindex, of, 2948 offset); 2949 break; 2950#endif 2951#ifdef HAVE_TARGET_64_BIG 2952 case Parameters::TARGET_64_BIG: 2953 this->sized_write_section_symbol<64, true>(os, symtab_xindex, of, 2954 offset); 2955 break; 2956#endif 2957 default: 2958 gold_unreachable(); 2959 } 2960} 2961 2962// Write out a section symbol, specialized for size and endianness. 2963 2964template<int size, bool big_endian> 2965void 2966Symbol_table::sized_write_section_symbol(const Output_section* os, 2967 Output_symtab_xindex* symtab_xindex, 2968 Output_file* of, 2969 off_t offset) const 2970{ 2971 const int sym_size = elfcpp::Elf_sizes<size>::sym_size; 2972 2973 unsigned char* pov = of->get_output_view(offset, sym_size); 2974 2975 elfcpp::Sym_write<size, big_endian> osym(pov); 2976 osym.put_st_name(0); 2977 if (parameters->options().relocatable()) 2978 osym.put_st_value(0); 2979 else 2980 osym.put_st_value(os->address()); 2981 osym.put_st_size(0); 2982 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, 2983 elfcpp::STT_SECTION)); 2984 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0)); 2985 2986 unsigned int shndx = os->out_shndx(); 2987 if (shndx >= elfcpp::SHN_LORESERVE) 2988 { 2989 symtab_xindex->add(os->symtab_index(), shndx); 2990 shndx = elfcpp::SHN_XINDEX; 2991 } 2992 osym.put_st_shndx(shndx); 2993 2994 of->write_output_view(offset, sym_size, pov); 2995} 2996 2997// Print statistical information to stderr. This is used for --stats. 2998 2999void 3000Symbol_table::print_stats() const 3001{ 3002#if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP) 3003 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"), 3004 program_name, this->table_.size(), this->table_.bucket_count()); 3005#else 3006 fprintf(stderr, _("%s: symbol table entries: %zu\n"), 3007 program_name, this->table_.size()); 3008#endif 3009 this->namepool_.print_stats("symbol table stringpool"); 3010} 3011 3012// We check for ODR violations by looking for symbols with the same 3013// name for which the debugging information reports that they were 3014// defined in different source locations. When comparing the source 3015// location, we consider instances with the same base filename to be 3016// the same. This is because different object files/shared libraries 3017// can include the same header file using different paths, and 3018// different optimization settings can make the line number appear to 3019// be a couple lines off, and we don't want to report an ODR violation 3020// in those cases. 3021 3022// This struct is used to compare line information, as returned by 3023// Dwarf_line_info::one_addr2line. It implements a < comparison 3024// operator used with std::set. 3025 3026struct Odr_violation_compare 3027{ 3028 bool 3029 operator()(const std::string& s1, const std::string& s2) const 3030 { 3031 // Inputs should be of the form "dirname/filename:linenum" where 3032 // "dirname/" is optional. We want to compare just the filename. 3033 3034 // Find the last '/' and ':' in each string. 3035 std::string::size_type s1begin = s1.rfind('/'); 3036 std::string::size_type s2begin = s2.rfind('/'); 3037 std::string::size_type s1end = s1.rfind(':'); 3038 std::string::size_type s2end = s2.rfind(':'); 3039 // If there was no '/' in a string, start at the beginning. 3040 if (s1begin == std::string::npos) 3041 s1begin = 0; 3042 if (s2begin == std::string::npos) 3043 s2begin = 0; 3044 // If the ':' appeared in the directory name, compare to the end 3045 // of the string. 3046 if (s1end < s1begin) 3047 s1end = s1.size(); 3048 if (s2end < s2begin) 3049 s2end = s2.size(); 3050 // Compare takes lengths, not end indices. 3051 return s1.compare(s1begin, s1end - s1begin, 3052 s2, s2begin, s2end - s2begin) < 0; 3053 } 3054}; 3055 3056// Check candidate_odr_violations_ to find symbols with the same name 3057// but apparently different definitions (different source-file/line-no). 3058 3059void 3060Symbol_table::detect_odr_violations(const Task* task, 3061 const char* output_file_name) const 3062{ 3063 for (Odr_map::const_iterator it = candidate_odr_violations_.begin(); 3064 it != candidate_odr_violations_.end(); 3065 ++it) 3066 { 3067 const char* symbol_name = it->first; 3068 // Maps from symbol location to a sample object file we found 3069 // that location in. We use a sorted map so the location order 3070 // is deterministic, but we only store an arbitrary object file 3071 // to avoid copying lots of names. 3072 std::map<std::string, std::string, Odr_violation_compare> line_nums; 3073 3074 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator 3075 locs = it->second.begin(); 3076 locs != it->second.end(); 3077 ++locs) 3078 { 3079 // We need to lock the object in order to read it. This 3080 // means that we have to run in a singleton Task. If we 3081 // want to run this in a general Task for better 3082 // performance, we will need one Task for object, plus 3083 // appropriate locking to ensure that we don't conflict with 3084 // other uses of the object. Also note, one_addr2line is not 3085 // currently thread-safe. 3086 Task_lock_obj<Object> tl(task, locs->object); 3087 // 16 is the size of the object-cache that one_addr2line should use. 3088 std::string lineno = Dwarf_line_info::one_addr2line( 3089 locs->object, locs->shndx, locs->offset, 16); 3090 if (!lineno.empty()) 3091 { 3092 std::string& sample_object = line_nums[lineno]; 3093 if (sample_object.empty()) 3094 sample_object = locs->object->name(); 3095 } 3096 } 3097 3098 if (line_nums.size() > 1) 3099 { 3100 gold_warning(_("while linking %s: symbol '%s' defined in multiple " 3101 "places (possible ODR violation):"), 3102 output_file_name, demangle(symbol_name).c_str()); 3103 for (std::map<std::string, std::string>::const_iterator it2 = 3104 line_nums.begin(); 3105 it2 != line_nums.end(); 3106 ++it2) 3107 fprintf(stderr, _(" %s from %s\n"), 3108 it2->first.c_str(), it2->second.c_str()); 3109 } 3110 } 3111 // We only call one_addr2line() in this function, so we can clear its cache. 3112 Dwarf_line_info::clear_addr2line_cache(); 3113} 3114 3115// Warnings functions. 3116 3117// Add a new warning. 3118 3119void 3120Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj, 3121 const std::string& warning) 3122{ 3123 name = symtab->canonicalize_name(name); 3124 this->warnings_[name].set(obj, warning); 3125} 3126 3127// Look through the warnings and mark the symbols for which we should 3128// warn. This is called during Layout::finalize when we know the 3129// sources for all the symbols. 3130 3131void 3132Warnings::note_warnings(Symbol_table* symtab) 3133{ 3134 for (Warning_table::iterator p = this->warnings_.begin(); 3135 p != this->warnings_.end(); 3136 ++p) 3137 { 3138 Symbol* sym = symtab->lookup(p->first, NULL); 3139 if (sym != NULL 3140 && sym->source() == Symbol::FROM_OBJECT 3141 && sym->object() == p->second.object) 3142 sym->set_has_warning(); 3143 } 3144} 3145 3146// Issue a warning. This is called when we see a relocation against a 3147// symbol for which has a warning. 3148 3149template<int size, bool big_endian> 3150void 3151Warnings::issue_warning(const Symbol* sym, 3152 const Relocate_info<size, big_endian>* relinfo, 3153 size_t relnum, off_t reloffset) const 3154{ 3155 gold_assert(sym->has_warning()); 3156 Warning_table::const_iterator p = this->warnings_.find(sym->name()); 3157 gold_assert(p != this->warnings_.end()); 3158 gold_warning_at_location(relinfo, relnum, reloffset, 3159 "%s", p->second.text.c_str()); 3160} 3161 3162// Instantiate the templates we need. We could use the configure 3163// script to restrict this to only the ones needed for implemented 3164// targets. 3165 3166#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) 3167template 3168void 3169Sized_symbol<32>::allocate_common(Output_data*, Value_type); 3170#endif 3171 3172#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) 3173template 3174void 3175Sized_symbol<64>::allocate_common(Output_data*, Value_type); 3176#endif 3177 3178#ifdef HAVE_TARGET_32_LITTLE 3179template 3180void 3181Symbol_table::add_from_relobj<32, false>( 3182 Sized_relobj<32, false>* relobj, 3183 const unsigned char* syms, 3184 size_t count, 3185 size_t symndx_offset, 3186 const char* sym_names, 3187 size_t sym_name_size, 3188 Sized_relobj<32, false>::Symbols* sympointers, 3189 size_t* defined); 3190#endif 3191 3192#ifdef HAVE_TARGET_32_BIG 3193template 3194void 3195Symbol_table::add_from_relobj<32, true>( 3196 Sized_relobj<32, true>* relobj, 3197 const unsigned char* syms, 3198 size_t count, 3199 size_t symndx_offset, 3200 const char* sym_names, 3201 size_t sym_name_size, 3202 Sized_relobj<32, true>::Symbols* sympointers, 3203 size_t* defined); 3204#endif 3205 3206#ifdef HAVE_TARGET_64_LITTLE 3207template 3208void 3209Symbol_table::add_from_relobj<64, false>( 3210 Sized_relobj<64, false>* relobj, 3211 const unsigned char* syms, 3212 size_t count, 3213 size_t symndx_offset, 3214 const char* sym_names, 3215 size_t sym_name_size, 3216 Sized_relobj<64, false>::Symbols* sympointers, 3217 size_t* defined); 3218#endif 3219 3220#ifdef HAVE_TARGET_64_BIG 3221template 3222void 3223Symbol_table::add_from_relobj<64, true>( 3224 Sized_relobj<64, true>* relobj, 3225 const unsigned char* syms, 3226 size_t count, 3227 size_t symndx_offset, 3228 const char* sym_names, 3229 size_t sym_name_size, 3230 Sized_relobj<64, true>::Symbols* sympointers, 3231 size_t* defined); 3232#endif 3233 3234#ifdef HAVE_TARGET_32_LITTLE 3235template 3236Symbol* 3237Symbol_table::add_from_pluginobj<32, false>( 3238 Sized_pluginobj<32, false>* obj, 3239 const char* name, 3240 const char* ver, 3241 elfcpp::Sym<32, false>* sym); 3242#endif 3243 3244#ifdef HAVE_TARGET_32_BIG 3245template 3246Symbol* 3247Symbol_table::add_from_pluginobj<32, true>( 3248 Sized_pluginobj<32, true>* obj, 3249 const char* name, 3250 const char* ver, 3251 elfcpp::Sym<32, true>* sym); 3252#endif 3253 3254#ifdef HAVE_TARGET_64_LITTLE 3255template 3256Symbol* 3257Symbol_table::add_from_pluginobj<64, false>( 3258 Sized_pluginobj<64, false>* obj, 3259 const char* name, 3260 const char* ver, 3261 elfcpp::Sym<64, false>* sym); 3262#endif 3263 3264#ifdef HAVE_TARGET_64_BIG 3265template 3266Symbol* 3267Symbol_table::add_from_pluginobj<64, true>( 3268 Sized_pluginobj<64, true>* obj, 3269 const char* name, 3270 const char* ver, 3271 elfcpp::Sym<64, true>* sym); 3272#endif 3273 3274#ifdef HAVE_TARGET_32_LITTLE 3275template 3276void 3277Symbol_table::add_from_dynobj<32, false>( 3278 Sized_dynobj<32, false>* dynobj, 3279 const unsigned char* syms, 3280 size_t count, 3281 const char* sym_names, 3282 size_t sym_name_size, 3283 const unsigned char* versym, 3284 size_t versym_size, 3285 const std::vector<const char*>* version_map, 3286 Sized_relobj<32, false>::Symbols* sympointers, 3287 size_t* defined); 3288#endif 3289 3290#ifdef HAVE_TARGET_32_BIG 3291template 3292void 3293Symbol_table::add_from_dynobj<32, true>( 3294 Sized_dynobj<32, true>* dynobj, 3295 const unsigned char* syms, 3296 size_t count, 3297 const char* sym_names, 3298 size_t sym_name_size, 3299 const unsigned char* versym, 3300 size_t versym_size, 3301 const std::vector<const char*>* version_map, 3302 Sized_relobj<32, true>::Symbols* sympointers, 3303 size_t* defined); 3304#endif 3305 3306#ifdef HAVE_TARGET_64_LITTLE 3307template 3308void 3309Symbol_table::add_from_dynobj<64, false>( 3310 Sized_dynobj<64, false>* dynobj, 3311 const unsigned char* syms, 3312 size_t count, 3313 const char* sym_names, 3314 size_t sym_name_size, 3315 const unsigned char* versym, 3316 size_t versym_size, 3317 const std::vector<const char*>* version_map, 3318 Sized_relobj<64, false>::Symbols* sympointers, 3319 size_t* defined); 3320#endif 3321 3322#ifdef HAVE_TARGET_64_BIG 3323template 3324void 3325Symbol_table::add_from_dynobj<64, true>( 3326 Sized_dynobj<64, true>* dynobj, 3327 const unsigned char* syms, 3328 size_t count, 3329 const char* sym_names, 3330 size_t sym_name_size, 3331 const unsigned char* versym, 3332 size_t versym_size, 3333 const std::vector<const char*>* version_map, 3334 Sized_relobj<64, true>::Symbols* sympointers, 3335 size_t* defined); 3336#endif 3337 3338#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) 3339template 3340void 3341Symbol_table::define_with_copy_reloc<32>( 3342 Sized_symbol<32>* sym, 3343 Output_data* posd, 3344 elfcpp::Elf_types<32>::Elf_Addr value); 3345#endif 3346 3347#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) 3348template 3349void 3350Symbol_table::define_with_copy_reloc<64>( 3351 Sized_symbol<64>* sym, 3352 Output_data* posd, 3353 elfcpp::Elf_types<64>::Elf_Addr value); 3354#endif 3355 3356#ifdef HAVE_TARGET_32_LITTLE 3357template 3358void 3359Warnings::issue_warning<32, false>(const Symbol* sym, 3360 const Relocate_info<32, false>* relinfo, 3361 size_t relnum, off_t reloffset) const; 3362#endif 3363 3364#ifdef HAVE_TARGET_32_BIG 3365template 3366void 3367Warnings::issue_warning<32, true>(const Symbol* sym, 3368 const Relocate_info<32, true>* relinfo, 3369 size_t relnum, off_t reloffset) const; 3370#endif 3371 3372#ifdef HAVE_TARGET_64_LITTLE 3373template 3374void 3375Warnings::issue_warning<64, false>(const Symbol* sym, 3376 const Relocate_info<64, false>* relinfo, 3377 size_t relnum, off_t reloffset) const; 3378#endif 3379 3380#ifdef HAVE_TARGET_64_BIG 3381template 3382void 3383Warnings::issue_warning<64, true>(const Symbol* sym, 3384 const Relocate_info<64, true>* relinfo, 3385 size_t relnum, off_t reloffset) const; 3386#endif 3387 3388} // End namespace gold. 3389