| 1 | // symtab.cc -- the gold symbol table |
| 2 | |
| 3 | // Copyright 2006, 2007, 2008 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 "object.h" |
| 34 | #include "dwarf_reader.h" |
| 35 | #include "dynobj.h" |
| 36 | #include "output.h" |
| 37 | #include "target.h" |
| 38 | #include "workqueue.h" |
| 39 | #include "symtab.h" |
| 40 | |
| 41 | namespace gold |
| 42 | { |
| 43 | |
| 44 | // Class Symbol. |
| 45 | |
| 46 | // Initialize fields in Symbol. This initializes everything except u_ |
| 47 | // and source_. |
| 48 | |
| 49 | void |
| 50 | Symbol::init_fields(const char* name, const char* version, |
| 51 | elfcpp::STT type, elfcpp::STB binding, |
| 52 | elfcpp::STV visibility, unsigned char nonvis) |
| 53 | { |
| 54 | this->name_ = name; |
| 55 | this->version_ = version; |
| 56 | this->symtab_index_ = 0; |
| 57 | this->dynsym_index_ = 0; |
| 58 | this->got_offsets_.init(); |
| 59 | this->plt_offset_ = 0; |
| 60 | this->type_ = type; |
| 61 | this->binding_ = binding; |
| 62 | this->visibility_ = visibility; |
| 63 | this->nonvis_ = nonvis; |
| 64 | this->is_target_special_ = false; |
| 65 | this->is_def_ = false; |
| 66 | this->is_forwarder_ = false; |
| 67 | this->has_alias_ = false; |
| 68 | this->needs_dynsym_entry_ = false; |
| 69 | this->in_reg_ = false; |
| 70 | this->in_dyn_ = false; |
| 71 | this->has_plt_offset_ = false; |
| 72 | this->has_warning_ = false; |
| 73 | this->is_copied_from_dynobj_ = false; |
| 74 | this->is_forced_local_ = false; |
| 75 | this->is_ordinary_shndx_ = false; |
| 76 | } |
| 77 | |
| 78 | // Return the demangled version of the symbol's name, but only |
| 79 | // if the --demangle flag was set. |
| 80 | |
| 81 | static std::string |
| 82 | demangle(const char* name) |
| 83 | { |
| 84 | if (!parameters->options().do_demangle()) |
| 85 | return name; |
| 86 | |
| 87 | // cplus_demangle allocates memory for the result it returns, |
| 88 | // and returns NULL if the name is already demangled. |
| 89 | char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS); |
| 90 | if (demangled_name == NULL) |
| 91 | return name; |
| 92 | |
| 93 | std::string retval(demangled_name); |
| 94 | free(demangled_name); |
| 95 | return retval; |
| 96 | } |
| 97 | |
| 98 | std::string |
| 99 | Symbol::demangled_name() const |
| 100 | { |
| 101 | return demangle(this->name()); |
| 102 | } |
| 103 | |
| 104 | // Initialize the fields in the base class Symbol for SYM in OBJECT. |
| 105 | |
| 106 | template<int size, bool big_endian> |
| 107 | void |
| 108 | Symbol::init_base_object(const char* name, const char* version, Object* object, |
| 109 | const elfcpp::Sym<size, big_endian>& sym, |
| 110 | unsigned int st_shndx, bool is_ordinary) |
| 111 | { |
| 112 | this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(), |
| 113 | sym.get_st_visibility(), sym.get_st_nonvis()); |
| 114 | this->u_.from_object.object = object; |
| 115 | this->u_.from_object.shndx = st_shndx; |
| 116 | this->is_ordinary_shndx_ = is_ordinary; |
| 117 | this->source_ = FROM_OBJECT; |
| 118 | this->in_reg_ = !object->is_dynamic(); |
| 119 | this->in_dyn_ = object->is_dynamic(); |
| 120 | } |
| 121 | |
| 122 | // Initialize the fields in the base class Symbol for a symbol defined |
| 123 | // in an Output_data. |
| 124 | |
| 125 | void |
| 126 | Symbol::init_base_output_data(const char* name, const char* version, |
| 127 | Output_data* od, elfcpp::STT type, |
| 128 | elfcpp::STB binding, elfcpp::STV visibility, |
| 129 | unsigned char nonvis, bool offset_is_from_end) |
| 130 | { |
| 131 | this->init_fields(name, version, type, binding, visibility, nonvis); |
| 132 | this->u_.in_output_data.output_data = od; |
| 133 | this->u_.in_output_data.offset_is_from_end = offset_is_from_end; |
| 134 | this->source_ = IN_OUTPUT_DATA; |
| 135 | this->in_reg_ = true; |
| 136 | } |
| 137 | |
| 138 | // Initialize the fields in the base class Symbol for a symbol defined |
| 139 | // in an Output_segment. |
| 140 | |
| 141 | void |
| 142 | Symbol::init_base_output_segment(const char* name, const char* version, |
| 143 | Output_segment* os, elfcpp::STT type, |
| 144 | elfcpp::STB binding, elfcpp::STV visibility, |
| 145 | unsigned char nonvis, |
| 146 | Segment_offset_base offset_base) |
| 147 | { |
| 148 | this->init_fields(name, version, type, binding, visibility, nonvis); |
| 149 | this->u_.in_output_segment.output_segment = os; |
| 150 | this->u_.in_output_segment.offset_base = offset_base; |
| 151 | this->source_ = IN_OUTPUT_SEGMENT; |
| 152 | this->in_reg_ = true; |
| 153 | } |
| 154 | |
| 155 | // Initialize the fields in the base class Symbol for a symbol defined |
| 156 | // as a constant. |
| 157 | |
| 158 | void |
| 159 | Symbol::init_base_constant(const char* name, const char* version, |
| 160 | elfcpp::STT type, elfcpp::STB binding, |
| 161 | elfcpp::STV visibility, unsigned char nonvis) |
| 162 | { |
| 163 | this->init_fields(name, version, type, binding, visibility, nonvis); |
| 164 | this->source_ = IS_CONSTANT; |
| 165 | this->in_reg_ = true; |
| 166 | } |
| 167 | |
| 168 | // Initialize the fields in the base class Symbol for an undefined |
| 169 | // symbol. |
| 170 | |
| 171 | void |
| 172 | Symbol::init_base_undefined(const char* name, const char* version, |
| 173 | elfcpp::STT type, elfcpp::STB binding, |
| 174 | elfcpp::STV visibility, unsigned char nonvis) |
| 175 | { |
| 176 | this->init_fields(name, version, type, binding, visibility, nonvis); |
| 177 | this->source_ = IS_UNDEFINED; |
| 178 | this->in_reg_ = true; |
| 179 | } |
| 180 | |
| 181 | // Allocate a common symbol in the base. |
| 182 | |
| 183 | void |
| 184 | Symbol::allocate_base_common(Output_data* od) |
| 185 | { |
| 186 | gold_assert(this->is_common()); |
| 187 | this->source_ = IN_OUTPUT_DATA; |
| 188 | this->u_.in_output_data.output_data = od; |
| 189 | this->u_.in_output_data.offset_is_from_end = false; |
| 190 | } |
| 191 | |
| 192 | // Initialize the fields in Sized_symbol for SYM in OBJECT. |
| 193 | |
| 194 | template<int size> |
| 195 | template<bool big_endian> |
| 196 | void |
| 197 | Sized_symbol<size>::init_object(const char* name, const char* version, |
| 198 | Object* object, |
| 199 | const elfcpp::Sym<size, big_endian>& sym, |
| 200 | unsigned int st_shndx, bool is_ordinary) |
| 201 | { |
| 202 | this->init_base_object(name, version, object, sym, st_shndx, is_ordinary); |
| 203 | this->value_ = sym.get_st_value(); |
| 204 | this->symsize_ = sym.get_st_size(); |
| 205 | } |
| 206 | |
| 207 | // Initialize the fields in Sized_symbol for a symbol defined in an |
| 208 | // Output_data. |
| 209 | |
| 210 | template<int size> |
| 211 | void |
| 212 | Sized_symbol<size>::init_output_data(const char* name, const char* version, |
| 213 | Output_data* od, Value_type value, |
| 214 | Size_type symsize, elfcpp::STT type, |
| 215 | elfcpp::STB binding, |
| 216 | elfcpp::STV visibility, |
| 217 | unsigned char nonvis, |
| 218 | bool offset_is_from_end) |
| 219 | { |
| 220 | this->init_base_output_data(name, version, od, type, binding, visibility, |
| 221 | nonvis, offset_is_from_end); |
| 222 | this->value_ = value; |
| 223 | this->symsize_ = symsize; |
| 224 | } |
| 225 | |
| 226 | // Initialize the fields in Sized_symbol for a symbol defined in an |
| 227 | // Output_segment. |
| 228 | |
| 229 | template<int size> |
| 230 | void |
| 231 | Sized_symbol<size>::init_output_segment(const char* name, const char* version, |
| 232 | Output_segment* os, Value_type value, |
| 233 | Size_type symsize, elfcpp::STT type, |
| 234 | elfcpp::STB binding, |
| 235 | elfcpp::STV visibility, |
| 236 | unsigned char nonvis, |
| 237 | Segment_offset_base offset_base) |
| 238 | { |
| 239 | this->init_base_output_segment(name, version, os, type, binding, visibility, |
| 240 | nonvis, offset_base); |
| 241 | this->value_ = value; |
| 242 | this->symsize_ = symsize; |
| 243 | } |
| 244 | |
| 245 | // Initialize the fields in Sized_symbol for a symbol defined as a |
| 246 | // constant. |
| 247 | |
| 248 | template<int size> |
| 249 | void |
| 250 | Sized_symbol<size>::init_constant(const char* name, const char* version, |
| 251 | Value_type value, Size_type symsize, |
| 252 | elfcpp::STT type, elfcpp::STB binding, |
| 253 | elfcpp::STV visibility, unsigned char nonvis) |
| 254 | { |
| 255 | this->init_base_constant(name, version, type, binding, visibility, nonvis); |
| 256 | this->value_ = value; |
| 257 | this->symsize_ = symsize; |
| 258 | } |
| 259 | |
| 260 | // Initialize the fields in Sized_symbol for an undefined symbol. |
| 261 | |
| 262 | template<int size> |
| 263 | void |
| 264 | Sized_symbol<size>::init_undefined(const char* name, const char* version, |
| 265 | elfcpp::STT type, elfcpp::STB binding, |
| 266 | elfcpp::STV visibility, unsigned char nonvis) |
| 267 | { |
| 268 | this->init_base_undefined(name, version, type, binding, visibility, nonvis); |
| 269 | this->value_ = 0; |
| 270 | this->symsize_ = 0; |
| 271 | } |
| 272 | |
| 273 | // Allocate a common symbol. |
| 274 | |
| 275 | template<int size> |
| 276 | void |
| 277 | Sized_symbol<size>::allocate_common(Output_data* od, Value_type value) |
| 278 | { |
| 279 | this->allocate_base_common(od); |
| 280 | this->value_ = value; |
| 281 | } |
| 282 | |
| 283 | // Return true if this symbol should be added to the dynamic symbol |
| 284 | // table. |
| 285 | |
| 286 | inline bool |
| 287 | Symbol::should_add_dynsym_entry() const |
| 288 | { |
| 289 | // If the symbol is used by a dynamic relocation, we need to add it. |
| 290 | if (this->needs_dynsym_entry()) |
| 291 | return true; |
| 292 | |
| 293 | // If the symbol was forced local in a version script, do not add it. |
| 294 | if (this->is_forced_local()) |
| 295 | return false; |
| 296 | |
| 297 | // If exporting all symbols or building a shared library, |
| 298 | // and the symbol is defined in a regular object and is |
| 299 | // externally visible, we need to add it. |
| 300 | if ((parameters->options().export_dynamic() || parameters->options().shared()) |
| 301 | && !this->is_from_dynobj() |
| 302 | && this->is_externally_visible()) |
| 303 | return true; |
| 304 | |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | // Return true if the final value of this symbol is known at link |
| 309 | // time. |
| 310 | |
| 311 | bool |
| 312 | Symbol::final_value_is_known() const |
| 313 | { |
| 314 | // If we are not generating an executable, then no final values are |
| 315 | // known, since they will change at runtime. |
| 316 | if (parameters->options().shared() || parameters->options().relocatable()) |
| 317 | return false; |
| 318 | |
| 319 | // If the symbol is not from an object file, and is not undefined, |
| 320 | // then it is defined, and known. |
| 321 | if (this->source_ != FROM_OBJECT) |
| 322 | { |
| 323 | if (this->source_ != IS_UNDEFINED) |
| 324 | return true; |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | // If the symbol is from a dynamic object, then the final value |
| 329 | // is not known. |
| 330 | if (this->object()->is_dynamic()) |
| 331 | return false; |
| 332 | |
| 333 | // If the symbol is not undefined (it is defined or common), |
| 334 | // then the final value is known. |
| 335 | if (!this->is_undefined()) |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | // If the symbol is undefined, then whether the final value is known |
| 340 | // depends on whether we are doing a static link. If we are doing a |
| 341 | // dynamic link, then the final value could be filled in at runtime. |
| 342 | // This could reasonably be the case for a weak undefined symbol. |
| 343 | return parameters->doing_static_link(); |
| 344 | } |
| 345 | |
| 346 | // Return the output section where this symbol is defined. |
| 347 | |
| 348 | Output_section* |
| 349 | Symbol::output_section() const |
| 350 | { |
| 351 | switch (this->source_) |
| 352 | { |
| 353 | case FROM_OBJECT: |
| 354 | { |
| 355 | unsigned int shndx = this->u_.from_object.shndx; |
| 356 | if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_) |
| 357 | { |
| 358 | gold_assert(!this->u_.from_object.object->is_dynamic()); |
| 359 | Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object); |
| 360 | section_offset_type dummy; |
| 361 | return relobj->output_section(shndx, &dummy); |
| 362 | } |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | case IN_OUTPUT_DATA: |
| 367 | return this->u_.in_output_data.output_data->output_section(); |
| 368 | |
| 369 | case IN_OUTPUT_SEGMENT: |
| 370 | case IS_CONSTANT: |
| 371 | case IS_UNDEFINED: |
| 372 | return NULL; |
| 373 | |
| 374 | default: |
| 375 | gold_unreachable(); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // Set the symbol's output section. This is used for symbols defined |
| 380 | // in scripts. This should only be called after the symbol table has |
| 381 | // been finalized. |
| 382 | |
| 383 | void |
| 384 | Symbol::set_output_section(Output_section* os) |
| 385 | { |
| 386 | switch (this->source_) |
| 387 | { |
| 388 | case FROM_OBJECT: |
| 389 | case IN_OUTPUT_DATA: |
| 390 | gold_assert(this->output_section() == os); |
| 391 | break; |
| 392 | case IS_CONSTANT: |
| 393 | this->source_ = IN_OUTPUT_DATA; |
| 394 | this->u_.in_output_data.output_data = os; |
| 395 | this->u_.in_output_data.offset_is_from_end = false; |
| 396 | break; |
| 397 | case IN_OUTPUT_SEGMENT: |
| 398 | case IS_UNDEFINED: |
| 399 | default: |
| 400 | gold_unreachable(); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // Class Symbol_table. |
| 405 | |
| 406 | Symbol_table::Symbol_table(unsigned int count, |
| 407 | const Version_script_info& version_script) |
| 408 | : saw_undefined_(0), offset_(0), table_(count), namepool_(), |
| 409 | forwarders_(), commons_(), tls_commons_(), forced_locals_(), warnings_(), |
| 410 | version_script_(version_script) |
| 411 | { |
| 412 | namepool_.reserve(count); |
| 413 | } |
| 414 | |
| 415 | Symbol_table::~Symbol_table() |
| 416 | { |
| 417 | } |
| 418 | |
| 419 | // The hash function. The key values are Stringpool keys. |
| 420 | |
| 421 | inline size_t |
| 422 | Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const |
| 423 | { |
| 424 | return key.first ^ key.second; |
| 425 | } |
| 426 | |
| 427 | // The symbol table key equality function. This is called with |
| 428 | // Stringpool keys. |
| 429 | |
| 430 | inline bool |
| 431 | Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1, |
| 432 | const Symbol_table_key& k2) const |
| 433 | { |
| 434 | return k1.first == k2.first && k1.second == k2.second; |
| 435 | } |
| 436 | |
| 437 | // Make TO a symbol which forwards to FROM. |
| 438 | |
| 439 | void |
| 440 | Symbol_table::make_forwarder(Symbol* from, Symbol* to) |
| 441 | { |
| 442 | gold_assert(from != to); |
| 443 | gold_assert(!from->is_forwarder() && !to->is_forwarder()); |
| 444 | this->forwarders_[from] = to; |
| 445 | from->set_forwarder(); |
| 446 | } |
| 447 | |
| 448 | // Resolve the forwards from FROM, returning the real symbol. |
| 449 | |
| 450 | Symbol* |
| 451 | Symbol_table::resolve_forwards(const Symbol* from) const |
| 452 | { |
| 453 | gold_assert(from->is_forwarder()); |
| 454 | Unordered_map<const Symbol*, Symbol*>::const_iterator p = |
| 455 | this->forwarders_.find(from); |
| 456 | gold_assert(p != this->forwarders_.end()); |
| 457 | return p->second; |
| 458 | } |
| 459 | |
| 460 | // Look up a symbol by name. |
| 461 | |
| 462 | Symbol* |
| 463 | Symbol_table::lookup(const char* name, const char* version) const |
| 464 | { |
| 465 | Stringpool::Key name_key; |
| 466 | name = this->namepool_.find(name, &name_key); |
| 467 | if (name == NULL) |
| 468 | return NULL; |
| 469 | |
| 470 | Stringpool::Key version_key = 0; |
| 471 | if (version != NULL) |
| 472 | { |
| 473 | version = this->namepool_.find(version, &version_key); |
| 474 | if (version == NULL) |
| 475 | return NULL; |
| 476 | } |
| 477 | |
| 478 | Symbol_table_key key(name_key, version_key); |
| 479 | Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key); |
| 480 | if (p == this->table_.end()) |
| 481 | return NULL; |
| 482 | return p->second; |
| 483 | } |
| 484 | |
| 485 | // Resolve a Symbol with another Symbol. This is only used in the |
| 486 | // unusual case where there are references to both an unversioned |
| 487 | // symbol and a symbol with a version, and we then discover that that |
| 488 | // version is the default version. Because this is unusual, we do |
| 489 | // this the slow way, by converting back to an ELF symbol. |
| 490 | |
| 491 | template<int size, bool big_endian> |
| 492 | void |
| 493 | Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from, |
| 494 | const char* version) |
| 495 | { |
| 496 | unsigned char buf[elfcpp::Elf_sizes<size>::sym_size]; |
| 497 | elfcpp::Sym_write<size, big_endian> esym(buf); |
| 498 | // We don't bother to set the st_name or the st_shndx field. |
| 499 | esym.put_st_value(from->value()); |
| 500 | esym.put_st_size(from->symsize()); |
| 501 | esym.put_st_info(from->binding(), from->type()); |
| 502 | esym.put_st_other(from->visibility(), from->nonvis()); |
| 503 | bool is_ordinary; |
| 504 | unsigned int shndx = from->shndx(&is_ordinary); |
| 505 | this->resolve(to, esym.sym(), shndx, is_ordinary, shndx, from->object(), |
| 506 | version); |
| 507 | if (from->in_reg()) |
| 508 | to->set_in_reg(); |
| 509 | if (from->in_dyn()) |
| 510 | to->set_in_dyn(); |
| 511 | } |
| 512 | |
| 513 | // Record that a symbol is forced to be local by a version script. |
| 514 | |
| 515 | void |
| 516 | Symbol_table::force_local(Symbol* sym) |
| 517 | { |
| 518 | if (!sym->is_defined() && !sym->is_common()) |
| 519 | return; |
| 520 | if (sym->is_forced_local()) |
| 521 | { |
| 522 | // We already got this one. |
| 523 | return; |
| 524 | } |
| 525 | sym->set_is_forced_local(); |
| 526 | this->forced_locals_.push_back(sym); |
| 527 | } |
| 528 | |
| 529 | // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This |
| 530 | // is only called for undefined symbols, when at least one --wrap |
| 531 | // option was used. |
| 532 | |
| 533 | const char* |
| 534 | Symbol_table::wrap_symbol(Object* object, const char* name, |
| 535 | Stringpool::Key* name_key) |
| 536 | { |
| 537 | // For some targets, we need to ignore a specific character when |
| 538 | // wrapping, and add it back later. |
| 539 | char prefix = '\0'; |
| 540 | if (name[0] == object->target()->wrap_char()) |
| 541 | { |
| 542 | prefix = name[0]; |
| 543 | ++name; |
| 544 | } |
| 545 | |
| 546 | if (parameters->options().is_wrap(name)) |
| 547 | { |
| 548 | // Turn NAME into __wrap_NAME. |
| 549 | std::string s; |
| 550 | if (prefix != '\0') |
| 551 | s += prefix; |
| 552 | s += "__wrap_"; |
| 553 | s += name; |
| 554 | |
| 555 | // This will give us both the old and new name in NAMEPOOL_, but |
| 556 | // that is OK. Only the versions we need will wind up in the |
| 557 | // real string table in the output file. |
| 558 | return this->namepool_.add(s.c_str(), true, name_key); |
| 559 | } |
| 560 | |
| 561 | const char* const real_prefix = "__real_"; |
| 562 | const size_t real_prefix_length = strlen(real_prefix); |
| 563 | if (strncmp(name, real_prefix, real_prefix_length) == 0 |
| 564 | && parameters->options().is_wrap(name + real_prefix_length)) |
| 565 | { |
| 566 | // Turn __real_NAME into NAME. |
| 567 | std::string s; |
| 568 | if (prefix != '\0') |
| 569 | s += prefix; |
| 570 | s += name + real_prefix_length; |
| 571 | return this->namepool_.add(s.c_str(), true, name_key); |
| 572 | } |
| 573 | |
| 574 | return name; |
| 575 | } |
| 576 | |
| 577 | // Add one symbol from OBJECT to the symbol table. NAME is symbol |
| 578 | // name and VERSION is the version; both are canonicalized. DEF is |
| 579 | // whether this is the default version. ST_SHNDX is the symbol's |
| 580 | // section index; IS_ORDINARY is whether this is a normal section |
| 581 | // rather than a special code. |
| 582 | |
| 583 | // If DEF is true, then this is the definition of a default version of |
| 584 | // a symbol. That means that any lookup of NAME/NULL and any lookup |
| 585 | // of NAME/VERSION should always return the same symbol. This is |
| 586 | // obvious for references, but in particular we want to do this for |
| 587 | // definitions: overriding NAME/NULL should also override |
| 588 | // NAME/VERSION. If we don't do that, it would be very hard to |
| 589 | // override functions in a shared library which uses versioning. |
| 590 | |
| 591 | // We implement this by simply making both entries in the hash table |
| 592 | // point to the same Symbol structure. That is easy enough if this is |
| 593 | // the first time we see NAME/NULL or NAME/VERSION, but it is possible |
| 594 | // that we have seen both already, in which case they will both have |
| 595 | // independent entries in the symbol table. We can't simply change |
| 596 | // the symbol table entry, because we have pointers to the entries |
| 597 | // attached to the object files. So we mark the entry attached to the |
| 598 | // object file as a forwarder, and record it in the forwarders_ map. |
| 599 | // Note that entries in the hash table will never be marked as |
| 600 | // forwarders. |
| 601 | // |
| 602 | // ORIG_ST_SHNDX and ST_SHNDX are almost always the same. |
| 603 | // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF |
| 604 | // for a special section code. ST_SHNDX may be modified if the symbol |
| 605 | // is defined in a section being discarded. |
| 606 | |
| 607 | template<int size, bool big_endian> |
| 608 | Sized_symbol<size>* |
| 609 | Symbol_table::add_from_object(Object* object, |
| 610 | const char *name, |
| 611 | Stringpool::Key name_key, |
| 612 | const char *version, |
| 613 | Stringpool::Key version_key, |
| 614 | bool def, |
| 615 | const elfcpp::Sym<size, big_endian>& sym, |
| 616 | unsigned int st_shndx, |
| 617 | bool is_ordinary, |
| 618 | unsigned int orig_st_shndx) |
| 619 | { |
| 620 | // Print a message if this symbol is being traced. |
| 621 | if (parameters->options().is_trace_symbol(name)) |
| 622 | { |
| 623 | if (orig_st_shndx == elfcpp::SHN_UNDEF) |
| 624 | gold_info(_("%s: reference to %s"), object->name().c_str(), name); |
| 625 | else |
| 626 | gold_info(_("%s: definition of %s"), object->name().c_str(), name); |
| 627 | } |
| 628 | |
| 629 | // For an undefined symbol, we may need to adjust the name using |
| 630 | // --wrap. |
| 631 | if (orig_st_shndx == elfcpp::SHN_UNDEF |
| 632 | && parameters->options().any_wrap()) |
| 633 | { |
| 634 | const char* wrap_name = this->wrap_symbol(object, name, &name_key); |
| 635 | if (wrap_name != name) |
| 636 | { |
| 637 | // If we see a reference to malloc with version GLIBC_2.0, |
| 638 | // and we turn it into a reference to __wrap_malloc, then we |
| 639 | // discard the version number. Otherwise the user would be |
| 640 | // required to specify the correct version for |
| 641 | // __wrap_malloc. |
| 642 | version = NULL; |
| 643 | version_key = 0; |
| 644 | name = wrap_name; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | Symbol* const snull = NULL; |
| 649 | std::pair<typename Symbol_table_type::iterator, bool> ins = |
| 650 | this->table_.insert(std::make_pair(std::make_pair(name_key, version_key), |
| 651 | snull)); |
| 652 | |
| 653 | std::pair<typename Symbol_table_type::iterator, bool> insdef = |
| 654 | std::make_pair(this->table_.end(), false); |
| 655 | if (def) |
| 656 | { |
| 657 | const Stringpool::Key vnull_key = 0; |
| 658 | insdef = this->table_.insert(std::make_pair(std::make_pair(name_key, |
| 659 | vnull_key), |
| 660 | snull)); |
| 661 | } |
| 662 | |
| 663 | // ins.first: an iterator, which is a pointer to a pair. |
| 664 | // ins.first->first: the key (a pair of name and version). |
| 665 | // ins.first->second: the value (Symbol*). |
| 666 | // ins.second: true if new entry was inserted, false if not. |
| 667 | |
| 668 | Sized_symbol<size>* ret; |
| 669 | bool was_undefined; |
| 670 | bool was_common; |
| 671 | if (!ins.second) |
| 672 | { |
| 673 | // We already have an entry for NAME/VERSION. |
| 674 | ret = this->get_sized_symbol<size>(ins.first->second); |
| 675 | gold_assert(ret != NULL); |
| 676 | |
| 677 | was_undefined = ret->is_undefined(); |
| 678 | was_common = ret->is_common(); |
| 679 | |
| 680 | this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object, |
| 681 | version); |
| 682 | |
| 683 | if (def) |
| 684 | { |
| 685 | if (insdef.second) |
| 686 | { |
| 687 | // This is the first time we have seen NAME/NULL. Make |
| 688 | // NAME/NULL point to NAME/VERSION. |
| 689 | insdef.first->second = ret; |
| 690 | } |
| 691 | else if (insdef.first->second != ret |
| 692 | && insdef.first->second->is_undefined()) |
| 693 | { |
| 694 | // This is the unfortunate case where we already have |
| 695 | // entries for both NAME/VERSION and NAME/NULL. Note |
| 696 | // that we don't want to combine them if the existing |
| 697 | // symbol is going to override the new one. FIXME: We |
| 698 | // currently just test is_undefined, but this may not do |
| 699 | // the right thing if the existing symbol is from a |
| 700 | // shared library and the new one is from a regular |
| 701 | // object. |
| 702 | |
| 703 | const Sized_symbol<size>* sym2; |
| 704 | sym2 = this->get_sized_symbol<size>(insdef.first->second); |
| 705 | Symbol_table::resolve<size, big_endian>(ret, sym2, version); |
| 706 | this->make_forwarder(insdef.first->second, ret); |
| 707 | insdef.first->second = ret; |
| 708 | } |
| 709 | else |
| 710 | def = false; |
| 711 | } |
| 712 | } |
| 713 | else |
| 714 | { |
| 715 | // This is the first time we have seen NAME/VERSION. |
| 716 | gold_assert(ins.first->second == NULL); |
| 717 | |
| 718 | if (def && !insdef.second) |
| 719 | { |
| 720 | // We already have an entry for NAME/NULL. If we override |
| 721 | // it, then change it to NAME/VERSION. |
| 722 | ret = this->get_sized_symbol<size>(insdef.first->second); |
| 723 | |
| 724 | was_undefined = ret->is_undefined(); |
| 725 | was_common = ret->is_common(); |
| 726 | |
| 727 | this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object, |
| 728 | version); |
| 729 | ins.first->second = ret; |
| 730 | } |
| 731 | else |
| 732 | { |
| 733 | was_undefined = false; |
| 734 | was_common = false; |
| 735 | |
| 736 | Sized_target<size, big_endian>* target = |
| 737 | object->sized_target<size, big_endian>(); |
| 738 | if (!target->has_make_symbol()) |
| 739 | ret = new Sized_symbol<size>(); |
| 740 | else |
| 741 | { |
| 742 | ret = target->make_symbol(); |
| 743 | if (ret == NULL) |
| 744 | { |
| 745 | // This means that we don't want a symbol table |
| 746 | // entry after all. |
| 747 | if (!def) |
| 748 | this->table_.erase(ins.first); |
| 749 | else |
| 750 | { |
| 751 | this->table_.erase(insdef.first); |
| 752 | // Inserting insdef invalidated ins. |
| 753 | this->table_.erase(std::make_pair(name_key, |
| 754 | version_key)); |
| 755 | } |
| 756 | return NULL; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | ret->init_object(name, version, object, sym, st_shndx, is_ordinary); |
| 761 | |
| 762 | ins.first->second = ret; |
| 763 | if (def) |
| 764 | { |
| 765 | // This is the first time we have seen NAME/NULL. Point |
| 766 | // it at the new entry for NAME/VERSION. |
| 767 | gold_assert(insdef.second); |
| 768 | insdef.first->second = ret; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | // Record every time we see a new undefined symbol, to speed up |
| 774 | // archive groups. |
| 775 | if (!was_undefined && ret->is_undefined()) |
| 776 | ++this->saw_undefined_; |
| 777 | |
| 778 | // Keep track of common symbols, to speed up common symbol |
| 779 | // allocation. |
| 780 | if (!was_common && ret->is_common()) |
| 781 | { |
| 782 | if (ret->type() != elfcpp::STT_TLS) |
| 783 | this->commons_.push_back(ret); |
| 784 | else |
| 785 | this->tls_commons_.push_back(ret); |
| 786 | } |
| 787 | |
| 788 | if (def) |
| 789 | ret->set_is_default(); |
| 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | // Add all the symbols in a relocatable object to the hash table. |
| 794 | |
| 795 | template<int size, bool big_endian> |
| 796 | void |
| 797 | Symbol_table::add_from_relobj( |
| 798 | Sized_relobj<size, big_endian>* relobj, |
| 799 | const unsigned char* syms, |
| 800 | size_t count, |
| 801 | size_t symndx_offset, |
| 802 | const char* sym_names, |
| 803 | size_t sym_name_size, |
| 804 | typename Sized_relobj<size, big_endian>::Symbols* sympointers) |
| 805 | { |
| 806 | gold_assert(size == relobj->target()->get_size()); |
| 807 | gold_assert(size == parameters->target().get_size()); |
| 808 | |
| 809 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 810 | |
| 811 | const bool just_symbols = relobj->just_symbols(); |
| 812 | |
| 813 | const unsigned char* p = syms; |
| 814 | for (size_t i = 0; i < count; ++i, p += sym_size) |
| 815 | { |
| 816 | elfcpp::Sym<size, big_endian> sym(p); |
| 817 | |
| 818 | unsigned int st_name = sym.get_st_name(); |
| 819 | if (st_name >= sym_name_size) |
| 820 | { |
| 821 | relobj->error(_("bad global symbol name offset %u at %zu"), |
| 822 | st_name, i); |
| 823 | continue; |
| 824 | } |
| 825 | |
| 826 | const char* name = sym_names + st_name; |
| 827 | |
| 828 | bool is_ordinary; |
| 829 | unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset, |
| 830 | sym.get_st_shndx(), |
| 831 | &is_ordinary); |
| 832 | unsigned int orig_st_shndx = st_shndx; |
| 833 | if (!is_ordinary) |
| 834 | orig_st_shndx = elfcpp::SHN_UNDEF; |
| 835 | |
| 836 | // A symbol defined in a section which we are not including must |
| 837 | // be treated as an undefined symbol. |
| 838 | if (st_shndx != elfcpp::SHN_UNDEF |
| 839 | && is_ordinary |
| 840 | && !relobj->is_section_included(st_shndx)) |
| 841 | st_shndx = elfcpp::SHN_UNDEF; |
| 842 | |
| 843 | // In an object file, an '@' in the name separates the symbol |
| 844 | // name from the version name. If there are two '@' characters, |
| 845 | // this is the default version. |
| 846 | const char* ver = strchr(name, '@'); |
| 847 | int namelen = 0; |
| 848 | // DEF: is the version default? LOCAL: is the symbol forced local? |
| 849 | bool def = false; |
| 850 | bool local = false; |
| 851 | |
| 852 | if (ver != NULL) |
| 853 | { |
| 854 | // The symbol name is of the form foo@VERSION or foo@@VERSION |
| 855 | namelen = ver - name; |
| 856 | ++ver; |
| 857 | if (*ver == '@') |
| 858 | { |
| 859 | def = true; |
| 860 | ++ver; |
| 861 | } |
| 862 | } |
| 863 | // We don't want to assign a version to an undefined symbol, |
| 864 | // even if it is listed in the version script. FIXME: What |
| 865 | // about a common symbol? |
| 866 | else if (!version_script_.empty() |
| 867 | && st_shndx != elfcpp::SHN_UNDEF) |
| 868 | { |
| 869 | // The symbol name did not have a version, but |
| 870 | // the version script may assign a version anyway. |
| 871 | namelen = strlen(name); |
| 872 | def = true; |
| 873 | // Check the global: entries from the version script. |
| 874 | const std::string& version = |
| 875 | version_script_.get_symbol_version(name); |
| 876 | if (!version.empty()) |
| 877 | ver = version.c_str(); |
| 878 | // Check the local: entries from the version script |
| 879 | if (version_script_.symbol_is_local(name)) |
| 880 | local = true; |
| 881 | } |
| 882 | |
| 883 | elfcpp::Sym<size, big_endian>* psym = &sym; |
| 884 | unsigned char symbuf[sym_size]; |
| 885 | elfcpp::Sym<size, big_endian> sym2(symbuf); |
| 886 | if (just_symbols) |
| 887 | { |
| 888 | memcpy(symbuf, p, sym_size); |
| 889 | elfcpp::Sym_write<size, big_endian> sw(symbuf); |
| 890 | if (orig_st_shndx != elfcpp::SHN_UNDEF && is_ordinary) |
| 891 | { |
| 892 | // Symbol values in object files are section relative. |
| 893 | // This is normally what we want, but since here we are |
| 894 | // converting the symbol to absolute we need to add the |
| 895 | // section address. The section address in an object |
| 896 | // file is normally zero, but people can use a linker |
| 897 | // script to change it. |
| 898 | sw.put_st_value(sym.get_st_value() |
| 899 | + relobj->section_address(orig_st_shndx)); |
| 900 | } |
| 901 | st_shndx = elfcpp::SHN_ABS; |
| 902 | is_ordinary = false; |
| 903 | psym = &sym2; |
| 904 | } |
| 905 | |
| 906 | Sized_symbol<size>* res; |
| 907 | if (ver == NULL) |
| 908 | { |
| 909 | Stringpool::Key name_key; |
| 910 | name = this->namepool_.add(name, true, &name_key); |
| 911 | res = this->add_from_object(relobj, name, name_key, NULL, 0, |
| 912 | false, *psym, st_shndx, is_ordinary, |
| 913 | orig_st_shndx); |
| 914 | if (local) |
| 915 | this->force_local(res); |
| 916 | } |
| 917 | else |
| 918 | { |
| 919 | Stringpool::Key name_key; |
| 920 | name = this->namepool_.add_with_length(name, namelen, true, |
| 921 | &name_key); |
| 922 | Stringpool::Key ver_key; |
| 923 | ver = this->namepool_.add(ver, true, &ver_key); |
| 924 | |
| 925 | res = this->add_from_object(relobj, name, name_key, ver, ver_key, |
| 926 | def, *psym, st_shndx, is_ordinary, |
| 927 | orig_st_shndx); |
| 928 | } |
| 929 | |
| 930 | (*sympointers)[i] = res; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | // Add all the symbols in a dynamic object to the hash table. |
| 935 | |
| 936 | template<int size, bool big_endian> |
| 937 | void |
| 938 | Symbol_table::add_from_dynobj( |
| 939 | Sized_dynobj<size, big_endian>* dynobj, |
| 940 | const unsigned char* syms, |
| 941 | size_t count, |
| 942 | const char* sym_names, |
| 943 | size_t sym_name_size, |
| 944 | const unsigned char* versym, |
| 945 | size_t versym_size, |
| 946 | const std::vector<const char*>* version_map) |
| 947 | { |
| 948 | gold_assert(size == dynobj->target()->get_size()); |
| 949 | gold_assert(size == parameters->target().get_size()); |
| 950 | |
| 951 | if (dynobj->just_symbols()) |
| 952 | { |
| 953 | gold_error(_("--just-symbols does not make sense with a shared object")); |
| 954 | return; |
| 955 | } |
| 956 | |
| 957 | if (versym != NULL && versym_size / 2 < count) |
| 958 | { |
| 959 | dynobj->error(_("too few symbol versions")); |
| 960 | return; |
| 961 | } |
| 962 | |
| 963 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 964 | |
| 965 | // We keep a list of all STT_OBJECT symbols, so that we can resolve |
| 966 | // weak aliases. This is necessary because if the dynamic object |
| 967 | // provides the same variable under two names, one of which is a |
| 968 | // weak definition, and the regular object refers to the weak |
| 969 | // definition, we have to put both the weak definition and the |
| 970 | // strong definition into the dynamic symbol table. Given a weak |
| 971 | // definition, the only way that we can find the corresponding |
| 972 | // strong definition, if any, is to search the symbol table. |
| 973 | std::vector<Sized_symbol<size>*> object_symbols; |
| 974 | |
| 975 | const unsigned char* p = syms; |
| 976 | const unsigned char* vs = versym; |
| 977 | for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2) |
| 978 | { |
| 979 | elfcpp::Sym<size, big_endian> sym(p); |
| 980 | |
| 981 | // Ignore symbols with local binding or that have |
| 982 | // internal or hidden visibility. |
| 983 | if (sym.get_st_bind() == elfcpp::STB_LOCAL |
| 984 | || sym.get_st_visibility() == elfcpp::STV_INTERNAL |
| 985 | || sym.get_st_visibility() == elfcpp::STV_HIDDEN) |
| 986 | continue; |
| 987 | |
| 988 | // A protected symbol in a shared library must be treated as a |
| 989 | // normal symbol when viewed from outside the shared library. |
| 990 | // Implement this by overriding the visibility here. |
| 991 | elfcpp::Sym<size, big_endian>* psym = &sym; |
| 992 | unsigned char symbuf[sym_size]; |
| 993 | elfcpp::Sym<size, big_endian> sym2(symbuf); |
| 994 | if (sym.get_st_visibility() == elfcpp::STV_PROTECTED) |
| 995 | { |
| 996 | memcpy(symbuf, p, sym_size); |
| 997 | elfcpp::Sym_write<size, big_endian> sw(symbuf); |
| 998 | sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis()); |
| 999 | psym = &sym2; |
| 1000 | } |
| 1001 | |
| 1002 | unsigned int st_name = psym->get_st_name(); |
| 1003 | if (st_name >= sym_name_size) |
| 1004 | { |
| 1005 | dynobj->error(_("bad symbol name offset %u at %zu"), |
| 1006 | st_name, i); |
| 1007 | continue; |
| 1008 | } |
| 1009 | |
| 1010 | const char* name = sym_names + st_name; |
| 1011 | |
| 1012 | bool is_ordinary; |
| 1013 | unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(), |
| 1014 | &is_ordinary); |
| 1015 | |
| 1016 | Sized_symbol<size>* res; |
| 1017 | |
| 1018 | if (versym == NULL) |
| 1019 | { |
| 1020 | Stringpool::Key name_key; |
| 1021 | name = this->namepool_.add(name, true, &name_key); |
| 1022 | res = this->add_from_object(dynobj, name, name_key, NULL, 0, |
| 1023 | false, *psym, st_shndx, is_ordinary, |
| 1024 | st_shndx); |
| 1025 | } |
| 1026 | else |
| 1027 | { |
| 1028 | // Read the version information. |
| 1029 | |
| 1030 | unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs); |
| 1031 | |
| 1032 | bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0; |
| 1033 | v &= elfcpp::VERSYM_VERSION; |
| 1034 | |
| 1035 | // The Sun documentation says that V can be VER_NDX_LOCAL, |
| 1036 | // or VER_NDX_GLOBAL, or a version index. The meaning of |
| 1037 | // VER_NDX_LOCAL is defined as "Symbol has local scope." |
| 1038 | // The old GNU linker will happily generate VER_NDX_LOCAL |
| 1039 | // for an undefined symbol. I don't know what the Sun |
| 1040 | // linker will generate. |
| 1041 | |
| 1042 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL) |
| 1043 | && st_shndx != elfcpp::SHN_UNDEF) |
| 1044 | { |
| 1045 | // This symbol should not be visible outside the object. |
| 1046 | continue; |
| 1047 | } |
| 1048 | |
| 1049 | // At this point we are definitely going to add this symbol. |
| 1050 | Stringpool::Key name_key; |
| 1051 | name = this->namepool_.add(name, true, &name_key); |
| 1052 | |
| 1053 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL) |
| 1054 | || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL)) |
| 1055 | { |
| 1056 | // This symbol does not have a version. |
| 1057 | res = this->add_from_object(dynobj, name, name_key, NULL, 0, |
| 1058 | false, *psym, st_shndx, is_ordinary, |
| 1059 | st_shndx); |
| 1060 | } |
| 1061 | else |
| 1062 | { |
| 1063 | if (v >= version_map->size()) |
| 1064 | { |
| 1065 | dynobj->error(_("versym for symbol %zu out of range: %u"), |
| 1066 | i, v); |
| 1067 | continue; |
| 1068 | } |
| 1069 | |
| 1070 | const char* version = (*version_map)[v]; |
| 1071 | if (version == NULL) |
| 1072 | { |
| 1073 | dynobj->error(_("versym for symbol %zu has no name: %u"), |
| 1074 | i, v); |
| 1075 | continue; |
| 1076 | } |
| 1077 | |
| 1078 | Stringpool::Key version_key; |
| 1079 | version = this->namepool_.add(version, true, &version_key); |
| 1080 | |
| 1081 | // If this is an absolute symbol, and the version name |
| 1082 | // and symbol name are the same, then this is the |
| 1083 | // version definition symbol. These symbols exist to |
| 1084 | // support using -u to pull in particular versions. We |
| 1085 | // do not want to record a version for them. |
| 1086 | if (st_shndx == elfcpp::SHN_ABS |
| 1087 | && !is_ordinary |
| 1088 | && name_key == version_key) |
| 1089 | res = this->add_from_object(dynobj, name, name_key, NULL, 0, |
| 1090 | false, *psym, st_shndx, is_ordinary, |
| 1091 | st_shndx); |
| 1092 | else |
| 1093 | { |
| 1094 | const bool def = (!hidden |
| 1095 | && st_shndx != elfcpp::SHN_UNDEF); |
| 1096 | res = this->add_from_object(dynobj, name, name_key, version, |
| 1097 | version_key, def, *psym, st_shndx, |
| 1098 | is_ordinary, st_shndx); |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | // Note that it is possible that RES was overridden by an |
| 1104 | // earlier object, in which case it can't be aliased here. |
| 1105 | if (st_shndx != elfcpp::SHN_UNDEF |
| 1106 | && is_ordinary |
| 1107 | && psym->get_st_type() == elfcpp::STT_OBJECT |
| 1108 | && res->source() == Symbol::FROM_OBJECT |
| 1109 | && res->object() == dynobj) |
| 1110 | object_symbols.push_back(res); |
| 1111 | } |
| 1112 | |
| 1113 | this->record_weak_aliases(&object_symbols); |
| 1114 | } |
| 1115 | |
| 1116 | // This is used to sort weak aliases. We sort them first by section |
| 1117 | // index, then by offset, then by weak ahead of strong. |
| 1118 | |
| 1119 | template<int size> |
| 1120 | class Weak_alias_sorter |
| 1121 | { |
| 1122 | public: |
| 1123 | bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const; |
| 1124 | }; |
| 1125 | |
| 1126 | template<int size> |
| 1127 | bool |
| 1128 | Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1, |
| 1129 | const Sized_symbol<size>* s2) const |
| 1130 | { |
| 1131 | bool is_ordinary; |
| 1132 | unsigned int s1_shndx = s1->shndx(&is_ordinary); |
| 1133 | gold_assert(is_ordinary); |
| 1134 | unsigned int s2_shndx = s2->shndx(&is_ordinary); |
| 1135 | gold_assert(is_ordinary); |
| 1136 | if (s1_shndx != s2_shndx) |
| 1137 | return s1_shndx < s2_shndx; |
| 1138 | |
| 1139 | if (s1->value() != s2->value()) |
| 1140 | return s1->value() < s2->value(); |
| 1141 | if (s1->binding() != s2->binding()) |
| 1142 | { |
| 1143 | if (s1->binding() == elfcpp::STB_WEAK) |
| 1144 | return true; |
| 1145 | if (s2->binding() == elfcpp::STB_WEAK) |
| 1146 | return false; |
| 1147 | } |
| 1148 | return std::string(s1->name()) < std::string(s2->name()); |
| 1149 | } |
| 1150 | |
| 1151 | // SYMBOLS is a list of object symbols from a dynamic object. Look |
| 1152 | // for any weak aliases, and record them so that if we add the weak |
| 1153 | // alias to the dynamic symbol table, we also add the corresponding |
| 1154 | // strong symbol. |
| 1155 | |
| 1156 | template<int size> |
| 1157 | void |
| 1158 | Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols) |
| 1159 | { |
| 1160 | // Sort the vector by section index, then by offset, then by weak |
| 1161 | // ahead of strong. |
| 1162 | std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>()); |
| 1163 | |
| 1164 | // Walk through the vector. For each weak definition, record |
| 1165 | // aliases. |
| 1166 | for (typename std::vector<Sized_symbol<size>*>::const_iterator p = |
| 1167 | symbols->begin(); |
| 1168 | p != symbols->end(); |
| 1169 | ++p) |
| 1170 | { |
| 1171 | if ((*p)->binding() != elfcpp::STB_WEAK) |
| 1172 | continue; |
| 1173 | |
| 1174 | // Build a circular list of weak aliases. Each symbol points to |
| 1175 | // the next one in the circular list. |
| 1176 | |
| 1177 | Sized_symbol<size>* from_sym = *p; |
| 1178 | typename std::vector<Sized_symbol<size>*>::const_iterator q; |
| 1179 | for (q = p + 1; q != symbols->end(); ++q) |
| 1180 | { |
| 1181 | bool dummy; |
| 1182 | if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy) |
| 1183 | || (*q)->value() != from_sym->value()) |
| 1184 | break; |
| 1185 | |
| 1186 | this->weak_aliases_[from_sym] = *q; |
| 1187 | from_sym->set_has_alias(); |
| 1188 | from_sym = *q; |
| 1189 | } |
| 1190 | |
| 1191 | if (from_sym != *p) |
| 1192 | { |
| 1193 | this->weak_aliases_[from_sym] = *p; |
| 1194 | from_sym->set_has_alias(); |
| 1195 | } |
| 1196 | |
| 1197 | p = q - 1; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | // Create and return a specially defined symbol. If ONLY_IF_REF is |
| 1202 | // true, then only create the symbol if there is a reference to it. |
| 1203 | // If this does not return NULL, it sets *POLDSYM to the existing |
| 1204 | // symbol if there is one. This canonicalizes *PNAME and *PVERSION. |
| 1205 | |
| 1206 | template<int size, bool big_endian> |
| 1207 | Sized_symbol<size>* |
| 1208 | Symbol_table::define_special_symbol(const char** pname, const char** pversion, |
| 1209 | bool only_if_ref, |
| 1210 | Sized_symbol<size>** poldsym) |
| 1211 | { |
| 1212 | Symbol* oldsym; |
| 1213 | Sized_symbol<size>* sym; |
| 1214 | bool add_to_table = false; |
| 1215 | typename Symbol_table_type::iterator add_loc = this->table_.end(); |
| 1216 | |
| 1217 | // If the caller didn't give us a version, see if we get one from |
| 1218 | // the version script. |
| 1219 | if (*pversion == NULL) |
| 1220 | { |
| 1221 | const std::string& v(this->version_script_.get_symbol_version(*pname)); |
| 1222 | if (!v.empty()) |
| 1223 | *pversion = v.c_str(); |
| 1224 | } |
| 1225 | |
| 1226 | if (only_if_ref) |
| 1227 | { |
| 1228 | oldsym = this->lookup(*pname, *pversion); |
| 1229 | if (oldsym == NULL || !oldsym->is_undefined()) |
| 1230 | return NULL; |
| 1231 | |
| 1232 | *pname = oldsym->name(); |
| 1233 | *pversion = oldsym->version(); |
| 1234 | } |
| 1235 | else |
| 1236 | { |
| 1237 | // Canonicalize NAME and VERSION. |
| 1238 | Stringpool::Key name_key; |
| 1239 | *pname = this->namepool_.add(*pname, true, &name_key); |
| 1240 | |
| 1241 | Stringpool::Key version_key = 0; |
| 1242 | if (*pversion != NULL) |
| 1243 | *pversion = this->namepool_.add(*pversion, true, &version_key); |
| 1244 | |
| 1245 | Symbol* const snull = NULL; |
| 1246 | std::pair<typename Symbol_table_type::iterator, bool> ins = |
| 1247 | this->table_.insert(std::make_pair(std::make_pair(name_key, |
| 1248 | version_key), |
| 1249 | snull)); |
| 1250 | |
| 1251 | if (!ins.second) |
| 1252 | { |
| 1253 | // We already have a symbol table entry for NAME/VERSION. |
| 1254 | oldsym = ins.first->second; |
| 1255 | gold_assert(oldsym != NULL); |
| 1256 | } |
| 1257 | else |
| 1258 | { |
| 1259 | // We haven't seen this symbol before. |
| 1260 | gold_assert(ins.first->second == NULL); |
| 1261 | add_to_table = true; |
| 1262 | add_loc = ins.first; |
| 1263 | oldsym = NULL; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | const Target& target = parameters->target(); |
| 1268 | if (!target.has_make_symbol()) |
| 1269 | sym = new Sized_symbol<size>(); |
| 1270 | else |
| 1271 | { |
| 1272 | gold_assert(target.get_size() == size); |
| 1273 | gold_assert(target.is_big_endian() ? big_endian : !big_endian); |
| 1274 | typedef Sized_target<size, big_endian> My_target; |
| 1275 | const My_target* sized_target = |
| 1276 | static_cast<const My_target*>(&target); |
| 1277 | sym = sized_target->make_symbol(); |
| 1278 | if (sym == NULL) |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | |
| 1282 | if (add_to_table) |
| 1283 | add_loc->second = sym; |
| 1284 | else |
| 1285 | gold_assert(oldsym != NULL); |
| 1286 | |
| 1287 | *poldsym = this->get_sized_symbol<size>(oldsym); |
| 1288 | |
| 1289 | return sym; |
| 1290 | } |
| 1291 | |
| 1292 | // Define a symbol based on an Output_data. |
| 1293 | |
| 1294 | Symbol* |
| 1295 | Symbol_table::define_in_output_data(const char* name, |
| 1296 | const char* version, |
| 1297 | Output_data* od, |
| 1298 | uint64_t value, |
| 1299 | uint64_t symsize, |
| 1300 | elfcpp::STT type, |
| 1301 | elfcpp::STB binding, |
| 1302 | elfcpp::STV visibility, |
| 1303 | unsigned char nonvis, |
| 1304 | bool offset_is_from_end, |
| 1305 | bool only_if_ref) |
| 1306 | { |
| 1307 | if (parameters->target().get_size() == 32) |
| 1308 | { |
| 1309 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
| 1310 | return this->do_define_in_output_data<32>(name, version, od, |
| 1311 | value, symsize, type, binding, |
| 1312 | visibility, nonvis, |
| 1313 | offset_is_from_end, |
| 1314 | only_if_ref); |
| 1315 | #else |
| 1316 | gold_unreachable(); |
| 1317 | #endif |
| 1318 | } |
| 1319 | else if (parameters->target().get_size() == 64) |
| 1320 | { |
| 1321 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) |
| 1322 | return this->do_define_in_output_data<64>(name, version, od, |
| 1323 | value, symsize, type, binding, |
| 1324 | visibility, nonvis, |
| 1325 | offset_is_from_end, |
| 1326 | only_if_ref); |
| 1327 | #else |
| 1328 | gold_unreachable(); |
| 1329 | #endif |
| 1330 | } |
| 1331 | else |
| 1332 | gold_unreachable(); |
| 1333 | } |
| 1334 | |
| 1335 | // Define a symbol in an Output_data, sized version. |
| 1336 | |
| 1337 | template<int size> |
| 1338 | Sized_symbol<size>* |
| 1339 | Symbol_table::do_define_in_output_data( |
| 1340 | const char* name, |
| 1341 | const char* version, |
| 1342 | Output_data* od, |
| 1343 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
| 1344 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, |
| 1345 | elfcpp::STT type, |
| 1346 | elfcpp::STB binding, |
| 1347 | elfcpp::STV visibility, |
| 1348 | unsigned char nonvis, |
| 1349 | bool offset_is_from_end, |
| 1350 | bool only_if_ref) |
| 1351 | { |
| 1352 | Sized_symbol<size>* sym; |
| 1353 | Sized_symbol<size>* oldsym; |
| 1354 | |
| 1355 | if (parameters->target().is_big_endian()) |
| 1356 | { |
| 1357 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) |
| 1358 | sym = this->define_special_symbol<size, true>(&name, &version, |
| 1359 | only_if_ref, &oldsym); |
| 1360 | #else |
| 1361 | gold_unreachable(); |
| 1362 | #endif |
| 1363 | } |
| 1364 | else |
| 1365 | { |
| 1366 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) |
| 1367 | sym = this->define_special_symbol<size, false>(&name, &version, |
| 1368 | only_if_ref, &oldsym); |
| 1369 | #else |
| 1370 | gold_unreachable(); |
| 1371 | #endif |
| 1372 | } |
| 1373 | |
| 1374 | if (sym == NULL) |
| 1375 | return NULL; |
| 1376 | |
| 1377 | sym->init_output_data(name, version, od, value, symsize, type, binding, |
| 1378 | visibility, nonvis, offset_is_from_end); |
| 1379 | |
| 1380 | if (oldsym == NULL) |
| 1381 | { |
| 1382 | if (binding == elfcpp::STB_LOCAL |
| 1383 | || this->version_script_.symbol_is_local(name)) |
| 1384 | this->force_local(sym); |
| 1385 | else if (version != NULL) |
| 1386 | sym->set_is_default(); |
| 1387 | return sym; |
| 1388 | } |
| 1389 | |
| 1390 | if (Symbol_table::should_override_with_special(oldsym)) |
| 1391 | this->override_with_special(oldsym, sym); |
| 1392 | delete sym; |
| 1393 | return oldsym; |
| 1394 | } |
| 1395 | |
| 1396 | // Define a symbol based on an Output_segment. |
| 1397 | |
| 1398 | Symbol* |
| 1399 | Symbol_table::define_in_output_segment(const char* name, |
| 1400 | const char* version, Output_segment* os, |
| 1401 | uint64_t value, |
| 1402 | uint64_t symsize, |
| 1403 | elfcpp::STT type, |
| 1404 | elfcpp::STB binding, |
| 1405 | elfcpp::STV visibility, |
| 1406 | unsigned char nonvis, |
| 1407 | Symbol::Segment_offset_base offset_base, |
| 1408 | bool only_if_ref) |
| 1409 | { |
| 1410 | if (parameters->target().get_size() == 32) |
| 1411 | { |
| 1412 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
| 1413 | return this->do_define_in_output_segment<32>(name, version, os, |
| 1414 | value, symsize, type, |
| 1415 | binding, visibility, nonvis, |
| 1416 | offset_base, only_if_ref); |
| 1417 | #else |
| 1418 | gold_unreachable(); |
| 1419 | #endif |
| 1420 | } |
| 1421 | else if (parameters->target().get_size() == 64) |
| 1422 | { |
| 1423 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) |
| 1424 | return this->do_define_in_output_segment<64>(name, version, os, |
| 1425 | value, symsize, type, |
| 1426 | binding, visibility, nonvis, |
| 1427 | offset_base, only_if_ref); |
| 1428 | #else |
| 1429 | gold_unreachable(); |
| 1430 | #endif |
| 1431 | } |
| 1432 | else |
| 1433 | gold_unreachable(); |
| 1434 | } |
| 1435 | |
| 1436 | // Define a symbol in an Output_segment, sized version. |
| 1437 | |
| 1438 | template<int size> |
| 1439 | Sized_symbol<size>* |
| 1440 | Symbol_table::do_define_in_output_segment( |
| 1441 | const char* name, |
| 1442 | const char* version, |
| 1443 | Output_segment* os, |
| 1444 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
| 1445 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, |
| 1446 | elfcpp::STT type, |
| 1447 | elfcpp::STB binding, |
| 1448 | elfcpp::STV visibility, |
| 1449 | unsigned char nonvis, |
| 1450 | Symbol::Segment_offset_base offset_base, |
| 1451 | bool only_if_ref) |
| 1452 | { |
| 1453 | Sized_symbol<size>* sym; |
| 1454 | Sized_symbol<size>* oldsym; |
| 1455 | |
| 1456 | if (parameters->target().is_big_endian()) |
| 1457 | { |
| 1458 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) |
| 1459 | sym = this->define_special_symbol<size, true>(&name, &version, |
| 1460 | only_if_ref, &oldsym); |
| 1461 | #else |
| 1462 | gold_unreachable(); |
| 1463 | #endif |
| 1464 | } |
| 1465 | else |
| 1466 | { |
| 1467 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) |
| 1468 | sym = this->define_special_symbol<size, false>(&name, &version, |
| 1469 | only_if_ref, &oldsym); |
| 1470 | #else |
| 1471 | gold_unreachable(); |
| 1472 | #endif |
| 1473 | } |
| 1474 | |
| 1475 | if (sym == NULL) |
| 1476 | return NULL; |
| 1477 | |
| 1478 | sym->init_output_segment(name, version, os, value, symsize, type, binding, |
| 1479 | visibility, nonvis, offset_base); |
| 1480 | |
| 1481 | if (oldsym == NULL) |
| 1482 | { |
| 1483 | if (binding == elfcpp::STB_LOCAL |
| 1484 | || this->version_script_.symbol_is_local(name)) |
| 1485 | this->force_local(sym); |
| 1486 | else if (version != NULL) |
| 1487 | sym->set_is_default(); |
| 1488 | return sym; |
| 1489 | } |
| 1490 | |
| 1491 | if (Symbol_table::should_override_with_special(oldsym)) |
| 1492 | this->override_with_special(oldsym, sym); |
| 1493 | delete sym; |
| 1494 | return oldsym; |
| 1495 | } |
| 1496 | |
| 1497 | // Define a special symbol with a constant value. It is a multiple |
| 1498 | // definition error if this symbol is already defined. |
| 1499 | |
| 1500 | Symbol* |
| 1501 | Symbol_table::define_as_constant(const char* name, |
| 1502 | const char* version, |
| 1503 | uint64_t value, |
| 1504 | uint64_t symsize, |
| 1505 | elfcpp::STT type, |
| 1506 | elfcpp::STB binding, |
| 1507 | elfcpp::STV visibility, |
| 1508 | unsigned char nonvis, |
| 1509 | bool only_if_ref, |
| 1510 | bool force_override) |
| 1511 | { |
| 1512 | if (parameters->target().get_size() == 32) |
| 1513 | { |
| 1514 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
| 1515 | return this->do_define_as_constant<32>(name, version, value, |
| 1516 | symsize, type, binding, |
| 1517 | visibility, nonvis, only_if_ref, |
| 1518 | force_override); |
| 1519 | #else |
| 1520 | gold_unreachable(); |
| 1521 | #endif |
| 1522 | } |
| 1523 | else if (parameters->target().get_size() == 64) |
| 1524 | { |
| 1525 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) |
| 1526 | return this->do_define_as_constant<64>(name, version, value, |
| 1527 | symsize, type, binding, |
| 1528 | visibility, nonvis, only_if_ref, |
| 1529 | force_override); |
| 1530 | #else |
| 1531 | gold_unreachable(); |
| 1532 | #endif |
| 1533 | } |
| 1534 | else |
| 1535 | gold_unreachable(); |
| 1536 | } |
| 1537 | |
| 1538 | // Define a symbol as a constant, sized version. |
| 1539 | |
| 1540 | template<int size> |
| 1541 | Sized_symbol<size>* |
| 1542 | Symbol_table::do_define_as_constant( |
| 1543 | const char* name, |
| 1544 | const char* version, |
| 1545 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
| 1546 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, |
| 1547 | elfcpp::STT type, |
| 1548 | elfcpp::STB binding, |
| 1549 | elfcpp::STV visibility, |
| 1550 | unsigned char nonvis, |
| 1551 | bool only_if_ref, |
| 1552 | bool force_override) |
| 1553 | { |
| 1554 | Sized_symbol<size>* sym; |
| 1555 | Sized_symbol<size>* oldsym; |
| 1556 | |
| 1557 | if (parameters->target().is_big_endian()) |
| 1558 | { |
| 1559 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) |
| 1560 | sym = this->define_special_symbol<size, true>(&name, &version, |
| 1561 | only_if_ref, &oldsym); |
| 1562 | #else |
| 1563 | gold_unreachable(); |
| 1564 | #endif |
| 1565 | } |
| 1566 | else |
| 1567 | { |
| 1568 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) |
| 1569 | sym = this->define_special_symbol<size, false>(&name, &version, |
| 1570 | only_if_ref, &oldsym); |
| 1571 | #else |
| 1572 | gold_unreachable(); |
| 1573 | #endif |
| 1574 | } |
| 1575 | |
| 1576 | if (sym == NULL) |
| 1577 | return NULL; |
| 1578 | |
| 1579 | sym->init_constant(name, version, value, symsize, type, binding, visibility, |
| 1580 | nonvis); |
| 1581 | |
| 1582 | if (oldsym == NULL) |
| 1583 | { |
| 1584 | // Version symbols are absolute symbols with name == version. |
| 1585 | // We don't want to force them to be local. |
| 1586 | if ((version == NULL |
| 1587 | || name != version |
| 1588 | || value != 0) |
| 1589 | && (binding == elfcpp::STB_LOCAL |
| 1590 | || this->version_script_.symbol_is_local(name))) |
| 1591 | this->force_local(sym); |
| 1592 | else if (version != NULL |
| 1593 | && (name != version || value != 0)) |
| 1594 | sym->set_is_default(); |
| 1595 | return sym; |
| 1596 | } |
| 1597 | |
| 1598 | if (force_override || Symbol_table::should_override_with_special(oldsym)) |
| 1599 | this->override_with_special(oldsym, sym); |
| 1600 | delete sym; |
| 1601 | return oldsym; |
| 1602 | } |
| 1603 | |
| 1604 | // Define a set of symbols in output sections. |
| 1605 | |
| 1606 | void |
| 1607 | Symbol_table::define_symbols(const Layout* layout, int count, |
| 1608 | const Define_symbol_in_section* p, |
| 1609 | bool only_if_ref) |
| 1610 | { |
| 1611 | for (int i = 0; i < count; ++i, ++p) |
| 1612 | { |
| 1613 | Output_section* os = layout->find_output_section(p->output_section); |
| 1614 | if (os != NULL) |
| 1615 | this->define_in_output_data(p->name, NULL, os, p->value, |
| 1616 | p->size, p->type, p->binding, |
| 1617 | p->visibility, p->nonvis, |
| 1618 | p->offset_is_from_end, |
| 1619 | only_if_ref || p->only_if_ref); |
| 1620 | else |
| 1621 | this->define_as_constant(p->name, NULL, 0, p->size, p->type, |
| 1622 | p->binding, p->visibility, p->nonvis, |
| 1623 | only_if_ref || p->only_if_ref, |
| 1624 | false); |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | // Define a set of symbols in output segments. |
| 1629 | |
| 1630 | void |
| 1631 | Symbol_table::define_symbols(const Layout* layout, int count, |
| 1632 | const Define_symbol_in_segment* p, |
| 1633 | bool only_if_ref) |
| 1634 | { |
| 1635 | for (int i = 0; i < count; ++i, ++p) |
| 1636 | { |
| 1637 | Output_segment* os = layout->find_output_segment(p->segment_type, |
| 1638 | p->segment_flags_set, |
| 1639 | p->segment_flags_clear); |
| 1640 | if (os != NULL) |
| 1641 | this->define_in_output_segment(p->name, NULL, os, p->value, |
| 1642 | p->size, p->type, p->binding, |
| 1643 | p->visibility, p->nonvis, |
| 1644 | p->offset_base, |
| 1645 | only_if_ref || p->only_if_ref); |
| 1646 | else |
| 1647 | this->define_as_constant(p->name, NULL, 0, p->size, p->type, |
| 1648 | p->binding, p->visibility, p->nonvis, |
| 1649 | only_if_ref || p->only_if_ref, |
| 1650 | false); |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | // Define CSYM using a COPY reloc. POSD is the Output_data where the |
| 1655 | // symbol should be defined--typically a .dyn.bss section. VALUE is |
| 1656 | // the offset within POSD. |
| 1657 | |
| 1658 | template<int size> |
| 1659 | void |
| 1660 | Symbol_table::define_with_copy_reloc( |
| 1661 | Sized_symbol<size>* csym, |
| 1662 | Output_data* posd, |
| 1663 | typename elfcpp::Elf_types<size>::Elf_Addr value) |
| 1664 | { |
| 1665 | gold_assert(csym->is_from_dynobj()); |
| 1666 | gold_assert(!csym->is_copied_from_dynobj()); |
| 1667 | Object* object = csym->object(); |
| 1668 | gold_assert(object->is_dynamic()); |
| 1669 | Dynobj* dynobj = static_cast<Dynobj*>(object); |
| 1670 | |
| 1671 | // Our copied variable has to override any variable in a shared |
| 1672 | // library. |
| 1673 | elfcpp::STB binding = csym->binding(); |
| 1674 | if (binding == elfcpp::STB_WEAK) |
| 1675 | binding = elfcpp::STB_GLOBAL; |
| 1676 | |
| 1677 | this->define_in_output_data(csym->name(), csym->version(), |
| 1678 | posd, value, csym->symsize(), |
| 1679 | csym->type(), binding, |
| 1680 | csym->visibility(), csym->nonvis(), |
| 1681 | false, false); |
| 1682 | |
| 1683 | csym->set_is_copied_from_dynobj(); |
| 1684 | csym->set_needs_dynsym_entry(); |
| 1685 | |
| 1686 | this->copied_symbol_dynobjs_[csym] = dynobj; |
| 1687 | |
| 1688 | // We have now defined all aliases, but we have not entered them all |
| 1689 | // in the copied_symbol_dynobjs_ map. |
| 1690 | if (csym->has_alias()) |
| 1691 | { |
| 1692 | Symbol* sym = csym; |
| 1693 | while (true) |
| 1694 | { |
| 1695 | sym = this->weak_aliases_[sym]; |
| 1696 | if (sym == csym) |
| 1697 | break; |
| 1698 | gold_assert(sym->output_data() == posd); |
| 1699 | |
| 1700 | sym->set_is_copied_from_dynobj(); |
| 1701 | this->copied_symbol_dynobjs_[sym] = dynobj; |
| 1702 | } |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | // SYM is defined using a COPY reloc. Return the dynamic object where |
| 1707 | // the original definition was found. |
| 1708 | |
| 1709 | Dynobj* |
| 1710 | Symbol_table::get_copy_source(const Symbol* sym) const |
| 1711 | { |
| 1712 | gold_assert(sym->is_copied_from_dynobj()); |
| 1713 | Copied_symbol_dynobjs::const_iterator p = |
| 1714 | this->copied_symbol_dynobjs_.find(sym); |
| 1715 | gold_assert(p != this->copied_symbol_dynobjs_.end()); |
| 1716 | return p->second; |
| 1717 | } |
| 1718 | |
| 1719 | // Add any undefined symbols named on the command line. |
| 1720 | |
| 1721 | void |
| 1722 | Symbol_table::add_undefined_symbols_from_command_line() |
| 1723 | { |
| 1724 | if (parameters->options().any_undefined()) |
| 1725 | { |
| 1726 | if (parameters->target().get_size() == 32) |
| 1727 | { |
| 1728 | #if defined(HAVE_TARGET_32_LITTL) || defined(HAVE_TARGET_32_BIG) |
| 1729 | this->do_add_undefined_symbols_from_command_line<32>(); |
| 1730 | #else |
| 1731 | gold_unreachable(); |
| 1732 | #endif |
| 1733 | } |
| 1734 | else if (parameters->target().get_size() == 64) |
| 1735 | { |
| 1736 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) |
| 1737 | this->do_add_undefined_symbols_from_command_line<64>(); |
| 1738 | #else |
| 1739 | gold_unreachable(); |
| 1740 | #endif |
| 1741 | } |
| 1742 | else |
| 1743 | gold_unreachable(); |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | template<int size> |
| 1748 | void |
| 1749 | Symbol_table::do_add_undefined_symbols_from_command_line() |
| 1750 | { |
| 1751 | for (options::String_set::const_iterator p = |
| 1752 | parameters->options().undefined_begin(); |
| 1753 | p != parameters->options().undefined_end(); |
| 1754 | ++p) |
| 1755 | { |
| 1756 | const char* name = p->c_str(); |
| 1757 | |
| 1758 | if (this->lookup(name) != NULL) |
| 1759 | continue; |
| 1760 | |
| 1761 | const char* version = NULL; |
| 1762 | |
| 1763 | Sized_symbol<size>* sym; |
| 1764 | Sized_symbol<size>* oldsym; |
| 1765 | if (parameters->target().is_big_endian()) |
| 1766 | { |
| 1767 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) |
| 1768 | sym = this->define_special_symbol<size, true>(&name, &version, |
| 1769 | false, &oldsym); |
| 1770 | #else |
| 1771 | gold_unreachable(); |
| 1772 | #endif |
| 1773 | } |
| 1774 | else |
| 1775 | { |
| 1776 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) |
| 1777 | sym = this->define_special_symbol<size, false>(&name, &version, |
| 1778 | false, &oldsym); |
| 1779 | #else |
| 1780 | gold_unreachable(); |
| 1781 | #endif |
| 1782 | } |
| 1783 | |
| 1784 | gold_assert(oldsym == NULL); |
| 1785 | |
| 1786 | sym->init_undefined(name, version, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, |
| 1787 | elfcpp::STV_DEFAULT, 0); |
| 1788 | ++this->saw_undefined_; |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | // Set the dynamic symbol indexes. INDEX is the index of the first |
| 1793 | // global dynamic symbol. Pointers to the symbols are stored into the |
| 1794 | // vector SYMS. The names are added to DYNPOOL. This returns an |
| 1795 | // updated dynamic symbol index. |
| 1796 | |
| 1797 | unsigned int |
| 1798 | Symbol_table::set_dynsym_indexes(unsigned int index, |
| 1799 | std::vector<Symbol*>* syms, |
| 1800 | Stringpool* dynpool, |
| 1801 | Versions* versions) |
| 1802 | { |
| 1803 | for (Symbol_table_type::iterator p = this->table_.begin(); |
| 1804 | p != this->table_.end(); |
| 1805 | ++p) |
| 1806 | { |
| 1807 | Symbol* sym = p->second; |
| 1808 | |
| 1809 | // Note that SYM may already have a dynamic symbol index, since |
| 1810 | // some symbols appear more than once in the symbol table, with |
| 1811 | // and without a version. |
| 1812 | |
| 1813 | if (!sym->should_add_dynsym_entry()) |
| 1814 | sym->set_dynsym_index(-1U); |
| 1815 | else if (!sym->has_dynsym_index()) |
| 1816 | { |
| 1817 | sym->set_dynsym_index(index); |
| 1818 | ++index; |
| 1819 | syms->push_back(sym); |
| 1820 | dynpool->add(sym->name(), false, NULL); |
| 1821 | |
| 1822 | // Record any version information. |
| 1823 | if (sym->version() != NULL) |
| 1824 | versions->record_version(this, dynpool, sym); |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | // Finish up the versions. In some cases this may add new dynamic |
| 1829 | // symbols. |
| 1830 | index = versions->finalize(this, index, syms); |
| 1831 | |
| 1832 | return index; |
| 1833 | } |
| 1834 | |
| 1835 | // Set the final values for all the symbols. The index of the first |
| 1836 | // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the |
| 1837 | // file offset OFF. Add their names to POOL. Return the new file |
| 1838 | // offset. Update *PLOCAL_SYMCOUNT if necessary. |
| 1839 | |
| 1840 | off_t |
| 1841 | Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index, |
| 1842 | size_t dyncount, Stringpool* pool, |
| 1843 | unsigned int *plocal_symcount) |
| 1844 | { |
| 1845 | off_t ret; |
| 1846 | |
| 1847 | gold_assert(*plocal_symcount != 0); |
| 1848 | this->first_global_index_ = *plocal_symcount; |
| 1849 | |
| 1850 | this->dynamic_offset_ = dynoff; |
| 1851 | this->first_dynamic_global_index_ = dyn_global_index; |
| 1852 | this->dynamic_count_ = dyncount; |
| 1853 | |
| 1854 | if (parameters->target().get_size() == 32) |
| 1855 | { |
| 1856 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE) |
| 1857 | ret = this->sized_finalize<32>(off, pool, plocal_symcount); |
| 1858 | #else |
| 1859 | gold_unreachable(); |
| 1860 | #endif |
| 1861 | } |
| 1862 | else if (parameters->target().get_size() == 64) |
| 1863 | { |
| 1864 | #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE) |
| 1865 | ret = this->sized_finalize<64>(off, pool, plocal_symcount); |
| 1866 | #else |
| 1867 | gold_unreachable(); |
| 1868 | #endif |
| 1869 | } |
| 1870 | else |
| 1871 | gold_unreachable(); |
| 1872 | |
| 1873 | // Now that we have the final symbol table, we can reliably note |
| 1874 | // which symbols should get warnings. |
| 1875 | this->warnings_.note_warnings(this); |
| 1876 | |
| 1877 | return ret; |
| 1878 | } |
| 1879 | |
| 1880 | // SYM is going into the symbol table at *PINDEX. Add the name to |
| 1881 | // POOL, update *PINDEX and *POFF. |
| 1882 | |
| 1883 | template<int size> |
| 1884 | void |
| 1885 | Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool, |
| 1886 | unsigned int* pindex, off_t* poff) |
| 1887 | { |
| 1888 | sym->set_symtab_index(*pindex); |
| 1889 | pool->add(sym->name(), false, NULL); |
| 1890 | ++*pindex; |
| 1891 | *poff += elfcpp::Elf_sizes<size>::sym_size; |
| 1892 | } |
| 1893 | |
| 1894 | // Set the final value for all the symbols. This is called after |
| 1895 | // Layout::finalize, so all the output sections have their final |
| 1896 | // address. |
| 1897 | |
| 1898 | template<int size> |
| 1899 | off_t |
| 1900 | Symbol_table::sized_finalize(off_t off, Stringpool* pool, |
| 1901 | unsigned int* plocal_symcount) |
| 1902 | { |
| 1903 | off = align_address(off, size >> 3); |
| 1904 | this->offset_ = off; |
| 1905 | |
| 1906 | unsigned int index = *plocal_symcount; |
| 1907 | const unsigned int orig_index = index; |
| 1908 | |
| 1909 | // First do all the symbols which have been forced to be local, as |
| 1910 | // they must appear before all global symbols. |
| 1911 | for (Forced_locals::iterator p = this->forced_locals_.begin(); |
| 1912 | p != this->forced_locals_.end(); |
| 1913 | ++p) |
| 1914 | { |
| 1915 | Symbol* sym = *p; |
| 1916 | gold_assert(sym->is_forced_local()); |
| 1917 | if (this->sized_finalize_symbol<size>(sym)) |
| 1918 | { |
| 1919 | this->add_to_final_symtab<size>(sym, pool, &index, &off); |
| 1920 | ++*plocal_symcount; |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | // Now do all the remaining symbols. |
| 1925 | for (Symbol_table_type::iterator p = this->table_.begin(); |
| 1926 | p != this->table_.end(); |
| 1927 | ++p) |
| 1928 | { |
| 1929 | Symbol* sym = p->second; |
| 1930 | if (this->sized_finalize_symbol<size>(sym)) |
| 1931 | this->add_to_final_symtab<size>(sym, pool, &index, &off); |
| 1932 | } |
| 1933 | |
| 1934 | this->output_count_ = index - orig_index; |
| 1935 | |
| 1936 | return off; |
| 1937 | } |
| 1938 | |
| 1939 | // Finalize the symbol SYM. This returns true if the symbol should be |
| 1940 | // added to the symbol table, false otherwise. |
| 1941 | |
| 1942 | template<int size> |
| 1943 | bool |
| 1944 | Symbol_table::sized_finalize_symbol(Symbol* unsized_sym) |
| 1945 | { |
| 1946 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym); |
| 1947 | |
| 1948 | // The default version of a symbol may appear twice in the symbol |
| 1949 | // table. We only need to finalize it once. |
| 1950 | if (sym->has_symtab_index()) |
| 1951 | return false; |
| 1952 | |
| 1953 | if (!sym->in_reg()) |
| 1954 | { |
| 1955 | gold_assert(!sym->has_symtab_index()); |
| 1956 | sym->set_symtab_index(-1U); |
| 1957 | gold_assert(sym->dynsym_index() == -1U); |
| 1958 | return false; |
| 1959 | } |
| 1960 | |
| 1961 | typename Sized_symbol<size>::Value_type value; |
| 1962 | |
| 1963 | switch (sym->source()) |
| 1964 | { |
| 1965 | case Symbol::FROM_OBJECT: |
| 1966 | { |
| 1967 | bool is_ordinary; |
| 1968 | unsigned int shndx = sym->shndx(&is_ordinary); |
| 1969 | |
| 1970 | // FIXME: We need some target specific support here. |
| 1971 | if (!is_ordinary |
| 1972 | && shndx != elfcpp::SHN_ABS |
| 1973 | && shndx != elfcpp::SHN_COMMON) |
| 1974 | { |
| 1975 | gold_error(_("%s: unsupported symbol section 0x%x"), |
| 1976 | sym->demangled_name().c_str(), shndx); |
| 1977 | shndx = elfcpp::SHN_UNDEF; |
| 1978 | } |
| 1979 | |
| 1980 | Object* symobj = sym->object(); |
| 1981 | if (symobj->is_dynamic()) |
| 1982 | { |
| 1983 | value = 0; |
| 1984 | shndx = elfcpp::SHN_UNDEF; |
| 1985 | } |
| 1986 | else if (shndx == elfcpp::SHN_UNDEF) |
| 1987 | value = 0; |
| 1988 | else if (!is_ordinary |
| 1989 | && (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)) |
| 1990 | value = sym->value(); |
| 1991 | else |
| 1992 | { |
| 1993 | Relobj* relobj = static_cast<Relobj*>(symobj); |
| 1994 | section_offset_type secoff; |
| 1995 | Output_section* os = relobj->output_section(shndx, &secoff); |
| 1996 | |
| 1997 | if (os == NULL) |
| 1998 | { |
| 1999 | sym->set_symtab_index(-1U); |
| 2000 | gold_assert(sym->dynsym_index() == -1U); |
| 2001 | return false; |
| 2002 | } |
| 2003 | |
| 2004 | if (sym->type() == elfcpp::STT_TLS) |
| 2005 | value = sym->value() + os->tls_offset() + secoff; |
| 2006 | else |
| 2007 | value = sym->value() + os->address() + secoff; |
| 2008 | } |
| 2009 | } |
| 2010 | break; |
| 2011 | |
| 2012 | case Symbol::IN_OUTPUT_DATA: |
| 2013 | { |
| 2014 | Output_data* od = sym->output_data(); |
| 2015 | value = sym->value(); |
| 2016 | if (sym->type() != elfcpp::STT_TLS) |
| 2017 | value += od->address(); |
| 2018 | else |
| 2019 | { |
| 2020 | Output_section* os = od->output_section(); |
| 2021 | gold_assert(os != NULL); |
| 2022 | value += os->tls_offset() + (od->address() - os->address()); |
| 2023 | } |
| 2024 | if (sym->offset_is_from_end()) |
| 2025 | value += od->data_size(); |
| 2026 | } |
| 2027 | break; |
| 2028 | |
| 2029 | case Symbol::IN_OUTPUT_SEGMENT: |
| 2030 | { |
| 2031 | Output_segment* os = sym->output_segment(); |
| 2032 | value = sym->value(); |
| 2033 | if (sym->type() != elfcpp::STT_TLS) |
| 2034 | value += os->vaddr(); |
| 2035 | switch (sym->offset_base()) |
| 2036 | { |
| 2037 | case Symbol::SEGMENT_START: |
| 2038 | break; |
| 2039 | case Symbol::SEGMENT_END: |
| 2040 | value += os->memsz(); |
| 2041 | break; |
| 2042 | case Symbol::SEGMENT_BSS: |
| 2043 | value += os->filesz(); |
| 2044 | break; |
| 2045 | default: |
| 2046 | gold_unreachable(); |
| 2047 | } |
| 2048 | } |
| 2049 | break; |
| 2050 | |
| 2051 | case Symbol::IS_CONSTANT: |
| 2052 | value = sym->value(); |
| 2053 | break; |
| 2054 | |
| 2055 | case Symbol::IS_UNDEFINED: |
| 2056 | value = 0; |
| 2057 | break; |
| 2058 | |
| 2059 | default: |
| 2060 | gold_unreachable(); |
| 2061 | } |
| 2062 | |
| 2063 | sym->set_value(value); |
| 2064 | |
| 2065 | if (parameters->options().strip_all()) |
| 2066 | { |
| 2067 | sym->set_symtab_index(-1U); |
| 2068 | return false; |
| 2069 | } |
| 2070 | |
| 2071 | return true; |
| 2072 | } |
| 2073 | |
| 2074 | // Write out the global symbols. |
| 2075 | |
| 2076 | void |
| 2077 | Symbol_table::write_globals(const Input_objects* input_objects, |
| 2078 | const Stringpool* sympool, |
| 2079 | const Stringpool* dynpool, |
| 2080 | Output_symtab_xindex* symtab_xindex, |
| 2081 | Output_symtab_xindex* dynsym_xindex, |
| 2082 | Output_file* of) const |
| 2083 | { |
| 2084 | switch (parameters->size_and_endianness()) |
| 2085 | { |
| 2086 | #ifdef HAVE_TARGET_32_LITTLE |
| 2087 | case Parameters::TARGET_32_LITTLE: |
| 2088 | this->sized_write_globals<32, false>(input_objects, sympool, |
| 2089 | dynpool, symtab_xindex, |
| 2090 | dynsym_xindex, of); |
| 2091 | break; |
| 2092 | #endif |
| 2093 | #ifdef HAVE_TARGET_32_BIG |
| 2094 | case Parameters::TARGET_32_BIG: |
| 2095 | this->sized_write_globals<32, true>(input_objects, sympool, |
| 2096 | dynpool, symtab_xindex, |
| 2097 | dynsym_xindex, of); |
| 2098 | break; |
| 2099 | #endif |
| 2100 | #ifdef HAVE_TARGET_64_LITTLE |
| 2101 | case Parameters::TARGET_64_LITTLE: |
| 2102 | this->sized_write_globals<64, false>(input_objects, sympool, |
| 2103 | dynpool, symtab_xindex, |
| 2104 | dynsym_xindex, of); |
| 2105 | break; |
| 2106 | #endif |
| 2107 | #ifdef HAVE_TARGET_64_BIG |
| 2108 | case Parameters::TARGET_64_BIG: |
| 2109 | this->sized_write_globals<64, true>(input_objects, sympool, |
| 2110 | dynpool, symtab_xindex, |
| 2111 | dynsym_xindex, of); |
| 2112 | break; |
| 2113 | #endif |
| 2114 | default: |
| 2115 | gold_unreachable(); |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | // Write out the global symbols. |
| 2120 | |
| 2121 | template<int size, bool big_endian> |
| 2122 | void |
| 2123 | Symbol_table::sized_write_globals(const Input_objects* input_objects, |
| 2124 | const Stringpool* sympool, |
| 2125 | const Stringpool* dynpool, |
| 2126 | Output_symtab_xindex* symtab_xindex, |
| 2127 | Output_symtab_xindex* dynsym_xindex, |
| 2128 | Output_file* of) const |
| 2129 | { |
| 2130 | const Target& target = parameters->target(); |
| 2131 | |
| 2132 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 2133 | |
| 2134 | const unsigned int output_count = this->output_count_; |
| 2135 | const section_size_type oview_size = output_count * sym_size; |
| 2136 | const unsigned int first_global_index = this->first_global_index_; |
| 2137 | unsigned char* psyms; |
| 2138 | if (this->offset_ == 0 || output_count == 0) |
| 2139 | psyms = NULL; |
| 2140 | else |
| 2141 | psyms = of->get_output_view(this->offset_, oview_size); |
| 2142 | |
| 2143 | const unsigned int dynamic_count = this->dynamic_count_; |
| 2144 | const section_size_type dynamic_size = dynamic_count * sym_size; |
| 2145 | const unsigned int first_dynamic_global_index = |
| 2146 | this->first_dynamic_global_index_; |
| 2147 | unsigned char* dynamic_view; |
| 2148 | if (this->dynamic_offset_ == 0 || dynamic_count == 0) |
| 2149 | dynamic_view = NULL; |
| 2150 | else |
| 2151 | dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size); |
| 2152 | |
| 2153 | for (Symbol_table_type::const_iterator p = this->table_.begin(); |
| 2154 | p != this->table_.end(); |
| 2155 | ++p) |
| 2156 | { |
| 2157 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); |
| 2158 | |
| 2159 | // Possibly warn about unresolved symbols in shared libraries. |
| 2160 | this->warn_about_undefined_dynobj_symbol(input_objects, sym); |
| 2161 | |
| 2162 | unsigned int sym_index = sym->symtab_index(); |
| 2163 | unsigned int dynsym_index; |
| 2164 | if (dynamic_view == NULL) |
| 2165 | dynsym_index = -1U; |
| 2166 | else |
| 2167 | dynsym_index = sym->dynsym_index(); |
| 2168 | |
| 2169 | if (sym_index == -1U && dynsym_index == -1U) |
| 2170 | { |
| 2171 | // This symbol is not included in the output file. |
| 2172 | continue; |
| 2173 | } |
| 2174 | |
| 2175 | unsigned int shndx; |
| 2176 | typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value(); |
| 2177 | typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value; |
| 2178 | switch (sym->source()) |
| 2179 | { |
| 2180 | case Symbol::FROM_OBJECT: |
| 2181 | { |
| 2182 | bool is_ordinary; |
| 2183 | unsigned int in_shndx = sym->shndx(&is_ordinary); |
| 2184 | |
| 2185 | // FIXME: We need some target specific support here. |
| 2186 | if (!is_ordinary |
| 2187 | && in_shndx != elfcpp::SHN_ABS |
| 2188 | && in_shndx != elfcpp::SHN_COMMON) |
| 2189 | { |
| 2190 | gold_error(_("%s: unsupported symbol section 0x%x"), |
| 2191 | sym->demangled_name().c_str(), in_shndx); |
| 2192 | shndx = in_shndx; |
| 2193 | } |
| 2194 | else |
| 2195 | { |
| 2196 | Object* symobj = sym->object(); |
| 2197 | if (symobj->is_dynamic()) |
| 2198 | { |
| 2199 | if (sym->needs_dynsym_value()) |
| 2200 | dynsym_value = target.dynsym_value(sym); |
| 2201 | shndx = elfcpp::SHN_UNDEF; |
| 2202 | } |
| 2203 | else if (in_shndx == elfcpp::SHN_UNDEF |
| 2204 | || (!is_ordinary |
| 2205 | && (in_shndx == elfcpp::SHN_ABS |
| 2206 | || in_shndx == elfcpp::SHN_COMMON))) |
| 2207 | shndx = in_shndx; |
| 2208 | else |
| 2209 | { |
| 2210 | Relobj* relobj = static_cast<Relobj*>(symobj); |
| 2211 | section_offset_type secoff; |
| 2212 | Output_section* os = relobj->output_section(in_shndx, |
| 2213 | &secoff); |
| 2214 | gold_assert(os != NULL); |
| 2215 | shndx = os->out_shndx(); |
| 2216 | |
| 2217 | if (shndx >= elfcpp::SHN_LORESERVE) |
| 2218 | { |
| 2219 | if (sym_index != -1U) |
| 2220 | symtab_xindex->add(sym_index, shndx); |
| 2221 | if (dynsym_index != -1U) |
| 2222 | dynsym_xindex->add(dynsym_index, shndx); |
| 2223 | shndx = elfcpp::SHN_XINDEX; |
| 2224 | } |
| 2225 | |
| 2226 | // In object files symbol values are section |
| 2227 | // relative. |
| 2228 | if (parameters->options().relocatable()) |
| 2229 | sym_value -= os->address(); |
| 2230 | } |
| 2231 | } |
| 2232 | } |
| 2233 | break; |
| 2234 | |
| 2235 | case Symbol::IN_OUTPUT_DATA: |
| 2236 | shndx = sym->output_data()->out_shndx(); |
| 2237 | if (shndx >= elfcpp::SHN_LORESERVE) |
| 2238 | { |
| 2239 | if (sym_index != -1U) |
| 2240 | symtab_xindex->add(sym_index, shndx); |
| 2241 | if (dynsym_index != -1U) |
| 2242 | dynsym_xindex->add(dynsym_index, shndx); |
| 2243 | shndx = elfcpp::SHN_XINDEX; |
| 2244 | } |
| 2245 | break; |
| 2246 | |
| 2247 | case Symbol::IN_OUTPUT_SEGMENT: |
| 2248 | shndx = elfcpp::SHN_ABS; |
| 2249 | break; |
| 2250 | |
| 2251 | case Symbol::IS_CONSTANT: |
| 2252 | shndx = elfcpp::SHN_ABS; |
| 2253 | break; |
| 2254 | |
| 2255 | case Symbol::IS_UNDEFINED: |
| 2256 | shndx = elfcpp::SHN_UNDEF; |
| 2257 | break; |
| 2258 | |
| 2259 | default: |
| 2260 | gold_unreachable(); |
| 2261 | } |
| 2262 | |
| 2263 | if (sym_index != -1U) |
| 2264 | { |
| 2265 | sym_index -= first_global_index; |
| 2266 | gold_assert(sym_index < output_count); |
| 2267 | unsigned char* ps = psyms + (sym_index * sym_size); |
| 2268 | this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx, |
| 2269 | sympool, ps); |
| 2270 | } |
| 2271 | |
| 2272 | if (dynsym_index != -1U) |
| 2273 | { |
| 2274 | dynsym_index -= first_dynamic_global_index; |
| 2275 | gold_assert(dynsym_index < dynamic_count); |
| 2276 | unsigned char* pd = dynamic_view + (dynsym_index * sym_size); |
| 2277 | this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx, |
| 2278 | dynpool, pd); |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | of->write_output_view(this->offset_, oview_size, psyms); |
| 2283 | if (dynamic_view != NULL) |
| 2284 | of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view); |
| 2285 | } |
| 2286 | |
| 2287 | // Write out the symbol SYM, in section SHNDX, to P. POOL is the |
| 2288 | // strtab holding the name. |
| 2289 | |
| 2290 | template<int size, bool big_endian> |
| 2291 | void |
| 2292 | Symbol_table::sized_write_symbol( |
| 2293 | Sized_symbol<size>* sym, |
| 2294 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
| 2295 | unsigned int shndx, |
| 2296 | const Stringpool* pool, |
| 2297 | unsigned char* p) const |
| 2298 | { |
| 2299 | elfcpp::Sym_write<size, big_endian> osym(p); |
| 2300 | osym.put_st_name(pool->get_offset(sym->name())); |
| 2301 | osym.put_st_value(value); |
| 2302 | osym.put_st_size(sym->symsize()); |
| 2303 | // A version script may have overridden the default binding. |
| 2304 | if (sym->is_forced_local()) |
| 2305 | osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, sym->type())); |
| 2306 | else |
| 2307 | osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type())); |
| 2308 | osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis())); |
| 2309 | osym.put_st_shndx(shndx); |
| 2310 | } |
| 2311 | |
| 2312 | // Check for unresolved symbols in shared libraries. This is |
| 2313 | // controlled by the --allow-shlib-undefined option. |
| 2314 | |
| 2315 | // We only warn about libraries for which we have seen all the |
| 2316 | // DT_NEEDED entries. We don't try to track down DT_NEEDED entries |
| 2317 | // which were not seen in this link. If we didn't see a DT_NEEDED |
| 2318 | // entry, we aren't going to be able to reliably report whether the |
| 2319 | // symbol is undefined. |
| 2320 | |
| 2321 | // We also don't warn about libraries found in the system library |
| 2322 | // directory (the directory were we find libc.so); we assume that |
| 2323 | // those libraries are OK. This heuristic avoids problems in |
| 2324 | // GNU/Linux, in which -ldl can have undefined references satisfied by |
| 2325 | // ld-linux.so. |
| 2326 | |
| 2327 | inline void |
| 2328 | Symbol_table::warn_about_undefined_dynobj_symbol( |
| 2329 | const Input_objects* input_objects, |
| 2330 | Symbol* sym) const |
| 2331 | { |
| 2332 | bool dummy; |
| 2333 | if (sym->source() == Symbol::FROM_OBJECT |
| 2334 | && sym->object()->is_dynamic() |
| 2335 | && sym->shndx(&dummy) == elfcpp::SHN_UNDEF |
| 2336 | && sym->binding() != elfcpp::STB_WEAK |
| 2337 | && !parameters->options().allow_shlib_undefined() |
| 2338 | && !parameters->target().is_defined_by_abi(sym) |
| 2339 | && !input_objects->found_in_system_library_directory(sym->object())) |
| 2340 | { |
| 2341 | // A very ugly cast. |
| 2342 | Dynobj* dynobj = static_cast<Dynobj*>(sym->object()); |
| 2343 | if (!dynobj->has_unknown_needed_entries()) |
| 2344 | gold_error(_("%s: undefined reference to '%s'"), |
| 2345 | sym->object()->name().c_str(), |
| 2346 | sym->demangled_name().c_str()); |
| 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | // Write out a section symbol. Return the update offset. |
| 2351 | |
| 2352 | void |
| 2353 | Symbol_table::write_section_symbol(const Output_section *os, |
| 2354 | Output_symtab_xindex* symtab_xindex, |
| 2355 | Output_file* of, |
| 2356 | off_t offset) const |
| 2357 | { |
| 2358 | switch (parameters->size_and_endianness()) |
| 2359 | { |
| 2360 | #ifdef HAVE_TARGET_32_LITTLE |
| 2361 | case Parameters::TARGET_32_LITTLE: |
| 2362 | this->sized_write_section_symbol<32, false>(os, symtab_xindex, of, |
| 2363 | offset); |
| 2364 | break; |
| 2365 | #endif |
| 2366 | #ifdef HAVE_TARGET_32_BIG |
| 2367 | case Parameters::TARGET_32_BIG: |
| 2368 | this->sized_write_section_symbol<32, true>(os, symtab_xindex, of, |
| 2369 | offset); |
| 2370 | break; |
| 2371 | #endif |
| 2372 | #ifdef HAVE_TARGET_64_LITTLE |
| 2373 | case Parameters::TARGET_64_LITTLE: |
| 2374 | this->sized_write_section_symbol<64, false>(os, symtab_xindex, of, |
| 2375 | offset); |
| 2376 | break; |
| 2377 | #endif |
| 2378 | #ifdef HAVE_TARGET_64_BIG |
| 2379 | case Parameters::TARGET_64_BIG: |
| 2380 | this->sized_write_section_symbol<64, true>(os, symtab_xindex, of, |
| 2381 | offset); |
| 2382 | break; |
| 2383 | #endif |
| 2384 | default: |
| 2385 | gold_unreachable(); |
| 2386 | } |
| 2387 | } |
| 2388 | |
| 2389 | // Write out a section symbol, specialized for size and endianness. |
| 2390 | |
| 2391 | template<int size, bool big_endian> |
| 2392 | void |
| 2393 | Symbol_table::sized_write_section_symbol(const Output_section* os, |
| 2394 | Output_symtab_xindex* symtab_xindex, |
| 2395 | Output_file* of, |
| 2396 | off_t offset) const |
| 2397 | { |
| 2398 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 2399 | |
| 2400 | unsigned char* pov = of->get_output_view(offset, sym_size); |
| 2401 | |
| 2402 | elfcpp::Sym_write<size, big_endian> osym(pov); |
| 2403 | osym.put_st_name(0); |
| 2404 | osym.put_st_value(os->address()); |
| 2405 | osym.put_st_size(0); |
| 2406 | osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, |
| 2407 | elfcpp::STT_SECTION)); |
| 2408 | osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0)); |
| 2409 | |
| 2410 | unsigned int shndx = os->out_shndx(); |
| 2411 | if (shndx >= elfcpp::SHN_LORESERVE) |
| 2412 | { |
| 2413 | symtab_xindex->add(os->symtab_index(), shndx); |
| 2414 | shndx = elfcpp::SHN_XINDEX; |
| 2415 | } |
| 2416 | osym.put_st_shndx(shndx); |
| 2417 | |
| 2418 | of->write_output_view(offset, sym_size, pov); |
| 2419 | } |
| 2420 | |
| 2421 | // Print statistical information to stderr. This is used for --stats. |
| 2422 | |
| 2423 | void |
| 2424 | Symbol_table::print_stats() const |
| 2425 | { |
| 2426 | #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP) |
| 2427 | fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"), |
| 2428 | program_name, this->table_.size(), this->table_.bucket_count()); |
| 2429 | #else |
| 2430 | fprintf(stderr, _("%s: symbol table entries: %zu\n"), |
| 2431 | program_name, this->table_.size()); |
| 2432 | #endif |
| 2433 | this->namepool_.print_stats("symbol table stringpool"); |
| 2434 | } |
| 2435 | |
| 2436 | // We check for ODR violations by looking for symbols with the same |
| 2437 | // name for which the debugging information reports that they were |
| 2438 | // defined in different source locations. When comparing the source |
| 2439 | // location, we consider instances with the same base filename and |
| 2440 | // line number to be the same. This is because different object |
| 2441 | // files/shared libraries can include the same header file using |
| 2442 | // different paths, and we don't want to report an ODR violation in |
| 2443 | // that case. |
| 2444 | |
| 2445 | // This struct is used to compare line information, as returned by |
| 2446 | // Dwarf_line_info::one_addr2line. It implements a < comparison |
| 2447 | // operator used with std::set. |
| 2448 | |
| 2449 | struct Odr_violation_compare |
| 2450 | { |
| 2451 | bool |
| 2452 | operator()(const std::string& s1, const std::string& s2) const |
| 2453 | { |
| 2454 | std::string::size_type pos1 = s1.rfind('/'); |
| 2455 | std::string::size_type pos2 = s2.rfind('/'); |
| 2456 | if (pos1 == std::string::npos |
| 2457 | || pos2 == std::string::npos) |
| 2458 | return s1 < s2; |
| 2459 | return s1.compare(pos1, std::string::npos, |
| 2460 | s2, pos2, std::string::npos) < 0; |
| 2461 | } |
| 2462 | }; |
| 2463 | |
| 2464 | // Check candidate_odr_violations_ to find symbols with the same name |
| 2465 | // but apparently different definitions (different source-file/line-no). |
| 2466 | |
| 2467 | void |
| 2468 | Symbol_table::detect_odr_violations(const Task* task, |
| 2469 | const char* output_file_name) const |
| 2470 | { |
| 2471 | for (Odr_map::const_iterator it = candidate_odr_violations_.begin(); |
| 2472 | it != candidate_odr_violations_.end(); |
| 2473 | ++it) |
| 2474 | { |
| 2475 | const char* symbol_name = it->first; |
| 2476 | // We use a sorted set so the output is deterministic. |
| 2477 | std::set<std::string, Odr_violation_compare> line_nums; |
| 2478 | |
| 2479 | for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator |
| 2480 | locs = it->second.begin(); |
| 2481 | locs != it->second.end(); |
| 2482 | ++locs) |
| 2483 | { |
| 2484 | // We need to lock the object in order to read it. This |
| 2485 | // means that we have to run in a singleton Task. If we |
| 2486 | // want to run this in a general Task for better |
| 2487 | // performance, we will need one Task for object, plus |
| 2488 | // appropriate locking to ensure that we don't conflict with |
| 2489 | // other uses of the object. Also note, one_addr2line is not |
| 2490 | // currently thread-safe. |
| 2491 | Task_lock_obj<Object> tl(task, locs->object); |
| 2492 | // 16 is the size of the object-cache that one_addr2line should use. |
| 2493 | std::string lineno = Dwarf_line_info::one_addr2line( |
| 2494 | locs->object, locs->shndx, locs->offset, 16); |
| 2495 | if (!lineno.empty()) |
| 2496 | line_nums.insert(lineno); |
| 2497 | } |
| 2498 | |
| 2499 | if (line_nums.size() > 1) |
| 2500 | { |
| 2501 | gold_warning(_("while linking %s: symbol '%s' defined in multiple " |
| 2502 | "places (possible ODR violation):"), |
| 2503 | output_file_name, demangle(symbol_name).c_str()); |
| 2504 | for (std::set<std::string>::const_iterator it2 = line_nums.begin(); |
| 2505 | it2 != line_nums.end(); |
| 2506 | ++it2) |
| 2507 | fprintf(stderr, " %s\n", it2->c_str()); |
| 2508 | } |
| 2509 | } |
| 2510 | // We only call one_addr2line() in this function, so we can clear its cache. |
| 2511 | Dwarf_line_info::clear_addr2line_cache(); |
| 2512 | } |
| 2513 | |
| 2514 | // Warnings functions. |
| 2515 | |
| 2516 | // Add a new warning. |
| 2517 | |
| 2518 | void |
| 2519 | Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj, |
| 2520 | const std::string& warning) |
| 2521 | { |
| 2522 | name = symtab->canonicalize_name(name); |
| 2523 | this->warnings_[name].set(obj, warning); |
| 2524 | } |
| 2525 | |
| 2526 | // Look through the warnings and mark the symbols for which we should |
| 2527 | // warn. This is called during Layout::finalize when we know the |
| 2528 | // sources for all the symbols. |
| 2529 | |
| 2530 | void |
| 2531 | Warnings::note_warnings(Symbol_table* symtab) |
| 2532 | { |
| 2533 | for (Warning_table::iterator p = this->warnings_.begin(); |
| 2534 | p != this->warnings_.end(); |
| 2535 | ++p) |
| 2536 | { |
| 2537 | Symbol* sym = symtab->lookup(p->first, NULL); |
| 2538 | if (sym != NULL |
| 2539 | && sym->source() == Symbol::FROM_OBJECT |
| 2540 | && sym->object() == p->second.object) |
| 2541 | sym->set_has_warning(); |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | // Issue a warning. This is called when we see a relocation against a |
| 2546 | // symbol for which has a warning. |
| 2547 | |
| 2548 | template<int size, bool big_endian> |
| 2549 | void |
| 2550 | Warnings::issue_warning(const Symbol* sym, |
| 2551 | const Relocate_info<size, big_endian>* relinfo, |
| 2552 | size_t relnum, off_t reloffset) const |
| 2553 | { |
| 2554 | gold_assert(sym->has_warning()); |
| 2555 | Warning_table::const_iterator p = this->warnings_.find(sym->name()); |
| 2556 | gold_assert(p != this->warnings_.end()); |
| 2557 | gold_warning_at_location(relinfo, relnum, reloffset, |
| 2558 | "%s", p->second.text.c_str()); |
| 2559 | } |
| 2560 | |
| 2561 | // Instantiate the templates we need. We could use the configure |
| 2562 | // script to restrict this to only the ones needed for implemented |
| 2563 | // targets. |
| 2564 | |
| 2565 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
| 2566 | template |
| 2567 | void |
| 2568 | Sized_symbol<32>::allocate_common(Output_data*, Value_type); |
| 2569 | #endif |
| 2570 | |
| 2571 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) |
| 2572 | template |
| 2573 | void |
| 2574 | Sized_symbol<64>::allocate_common(Output_data*, Value_type); |
| 2575 | #endif |
| 2576 | |
| 2577 | #ifdef HAVE_TARGET_32_LITTLE |
| 2578 | template |
| 2579 | void |
| 2580 | Symbol_table::add_from_relobj<32, false>( |
| 2581 | Sized_relobj<32, false>* relobj, |
| 2582 | const unsigned char* syms, |
| 2583 | size_t count, |
| 2584 | size_t symndx_offset, |
| 2585 | const char* sym_names, |
| 2586 | size_t sym_name_size, |
| 2587 | Sized_relobj<32, true>::Symbols* sympointers); |
| 2588 | #endif |
| 2589 | |
| 2590 | #ifdef HAVE_TARGET_32_BIG |
| 2591 | template |
| 2592 | void |
| 2593 | Symbol_table::add_from_relobj<32, true>( |
| 2594 | Sized_relobj<32, true>* relobj, |
| 2595 | const unsigned char* syms, |
| 2596 | size_t count, |
| 2597 | size_t symndx_offset, |
| 2598 | const char* sym_names, |
| 2599 | size_t sym_name_size, |
| 2600 | Sized_relobj<32, false>::Symbols* sympointers); |
| 2601 | #endif |
| 2602 | |
| 2603 | #ifdef HAVE_TARGET_64_LITTLE |
| 2604 | template |
| 2605 | void |
| 2606 | Symbol_table::add_from_relobj<64, false>( |
| 2607 | Sized_relobj<64, false>* relobj, |
| 2608 | const unsigned char* syms, |
| 2609 | size_t count, |
| 2610 | size_t symndx_offset, |
| 2611 | const char* sym_names, |
| 2612 | size_t sym_name_size, |
| 2613 | Sized_relobj<64, true>::Symbols* sympointers); |
| 2614 | #endif |
| 2615 | |
| 2616 | #ifdef HAVE_TARGET_64_BIG |
| 2617 | template |
| 2618 | void |
| 2619 | Symbol_table::add_from_relobj<64, true>( |
| 2620 | Sized_relobj<64, true>* relobj, |
| 2621 | const unsigned char* syms, |
| 2622 | size_t count, |
| 2623 | size_t symndx_offset, |
| 2624 | const char* sym_names, |
| 2625 | size_t sym_name_size, |
| 2626 | Sized_relobj<64, false>::Symbols* sympointers); |
| 2627 | #endif |
| 2628 | |
| 2629 | #ifdef HAVE_TARGET_32_LITTLE |
| 2630 | template |
| 2631 | void |
| 2632 | Symbol_table::add_from_dynobj<32, false>( |
| 2633 | Sized_dynobj<32, false>* dynobj, |
| 2634 | const unsigned char* syms, |
| 2635 | size_t count, |
| 2636 | const char* sym_names, |
| 2637 | size_t sym_name_size, |
| 2638 | const unsigned char* versym, |
| 2639 | size_t versym_size, |
| 2640 | const std::vector<const char*>* version_map); |
| 2641 | #endif |
| 2642 | |
| 2643 | #ifdef HAVE_TARGET_32_BIG |
| 2644 | template |
| 2645 | void |
| 2646 | Symbol_table::add_from_dynobj<32, true>( |
| 2647 | Sized_dynobj<32, true>* dynobj, |
| 2648 | const unsigned char* syms, |
| 2649 | size_t count, |
| 2650 | const char* sym_names, |
| 2651 | size_t sym_name_size, |
| 2652 | const unsigned char* versym, |
| 2653 | size_t versym_size, |
| 2654 | const std::vector<const char*>* version_map); |
| 2655 | #endif |
| 2656 | |
| 2657 | #ifdef HAVE_TARGET_64_LITTLE |
| 2658 | template |
| 2659 | void |
| 2660 | Symbol_table::add_from_dynobj<64, false>( |
| 2661 | Sized_dynobj<64, false>* dynobj, |
| 2662 | const unsigned char* syms, |
| 2663 | size_t count, |
| 2664 | const char* sym_names, |
| 2665 | size_t sym_name_size, |
| 2666 | const unsigned char* versym, |
| 2667 | size_t versym_size, |
| 2668 | const std::vector<const char*>* version_map); |
| 2669 | #endif |
| 2670 | |
| 2671 | #ifdef HAVE_TARGET_64_BIG |
| 2672 | template |
| 2673 | void |
| 2674 | Symbol_table::add_from_dynobj<64, true>( |
| 2675 | Sized_dynobj<64, true>* dynobj, |
| 2676 | const unsigned char* syms, |
| 2677 | size_t count, |
| 2678 | const char* sym_names, |
| 2679 | size_t sym_name_size, |
| 2680 | const unsigned char* versym, |
| 2681 | size_t versym_size, |
| 2682 | const std::vector<const char*>* version_map); |
| 2683 | #endif |
| 2684 | |
| 2685 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
| 2686 | template |
| 2687 | void |
| 2688 | Symbol_table::define_with_copy_reloc<32>( |
| 2689 | Sized_symbol<32>* sym, |
| 2690 | Output_data* posd, |
| 2691 | elfcpp::Elf_types<32>::Elf_Addr value); |
| 2692 | #endif |
| 2693 | |
| 2694 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) |
| 2695 | template |
| 2696 | void |
| 2697 | Symbol_table::define_with_copy_reloc<64>( |
| 2698 | Sized_symbol<64>* sym, |
| 2699 | Output_data* posd, |
| 2700 | elfcpp::Elf_types<64>::Elf_Addr value); |
| 2701 | #endif |
| 2702 | |
| 2703 | #ifdef HAVE_TARGET_32_LITTLE |
| 2704 | template |
| 2705 | void |
| 2706 | Warnings::issue_warning<32, false>(const Symbol* sym, |
| 2707 | const Relocate_info<32, false>* relinfo, |
| 2708 | size_t relnum, off_t reloffset) const; |
| 2709 | #endif |
| 2710 | |
| 2711 | #ifdef HAVE_TARGET_32_BIG |
| 2712 | template |
| 2713 | void |
| 2714 | Warnings::issue_warning<32, true>(const Symbol* sym, |
| 2715 | const Relocate_info<32, true>* relinfo, |
| 2716 | size_t relnum, off_t reloffset) const; |
| 2717 | #endif |
| 2718 | |
| 2719 | #ifdef HAVE_TARGET_64_LITTLE |
| 2720 | template |
| 2721 | void |
| 2722 | Warnings::issue_warning<64, false>(const Symbol* sym, |
| 2723 | const Relocate_info<64, false>* relinfo, |
| 2724 | size_t relnum, off_t reloffset) const; |
| 2725 | #endif |
| 2726 | |
| 2727 | #ifdef HAVE_TARGET_64_BIG |
| 2728 | template |
| 2729 | void |
| 2730 | Warnings::issue_warning<64, true>(const Symbol* sym, |
| 2731 | const Relocate_info<64, true>* relinfo, |
| 2732 | size_t relnum, off_t reloffset) const; |
| 2733 | #endif |
| 2734 | |
| 2735 | } // End namespace gold. |