1 // resolve.cc -- symbol resolution for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
33 // Symbol methods used in this file.
35 // Override the fields in Symbol.
37 template<int size, bool big_endian>
39 Symbol::override_base(const elfcpp::Sym<size, big_endian>& sym,
40 Object* object, const char* version)
42 gold_assert(this->source_ == FROM_OBJECT);
43 this->u_.from_object.object = object;
44 if (version != NULL && this->version() != version)
46 gold_assert(this->version() == NULL);
47 this->version_ = version;
49 // FIXME: Handle SHN_XINDEX.
50 this->u_.from_object.shndx = sym.get_st_shndx();
51 this->type_ = sym.get_st_type();
52 this->binding_ = sym.get_st_bind();
53 this->visibility_ = sym.get_st_visibility();
54 this->nonvis_ = sym.get_st_nonvis();
55 if (object->is_dynamic())
61 // Override the fields in Sized_symbol.
64 template<bool big_endian>
66 Sized_symbol<size>::override(const elfcpp::Sym<size, big_endian>& sym,
67 Object* object, const char* version)
69 this->override_base(sym, object, version);
70 this->value_ = sym.get_st_value();
71 this->symsize_ = sym.get_st_size();
74 // The resolve functions build a little code for each symbol.
75 // Bit 0: 0 for global, 1 for weak.
76 // Bit 1: 0 for regular object, 1 for shared object
77 // Bits 2-3: 0 for normal, 1 for undefined, 2 for common
78 // This gives us values from 0 to 11.
80 static const int global_or_weak_shift = 0;
81 static const unsigned int global_flag = 0 << global_or_weak_shift;
82 static const unsigned int weak_flag = 1 << global_or_weak_shift;
84 static const int regular_or_dynamic_shift = 1;
85 static const unsigned int regular_flag = 0 << regular_or_dynamic_shift;
86 static const unsigned int dynamic_flag = 1 << regular_or_dynamic_shift;
88 static const int def_undef_or_common_shift = 2;
89 static const unsigned int def_flag = 0 << def_undef_or_common_shift;
90 static const unsigned int undef_flag = 1 << def_undef_or_common_shift;
91 static const unsigned int common_flag = 2 << def_undef_or_common_shift;
93 // Resolve a symbol. This is called the second and subsequent times
94 // we see a symbol. TO is the pre-existing symbol. SYM is the new
95 // symbol, seen in OBJECT. VERSION of the version of SYM.
97 template<int size, bool big_endian>
99 Symbol_table::resolve(Sized_symbol<size>* to,
100 const elfcpp::Sym<size, big_endian>& sym,
101 Object* object, const char* version)
103 if (object->target()->has_resolve())
105 Sized_target<size, big_endian>* sized_target;
106 sized_target = object->sized_target
107 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
108 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
109 sized_target->resolve(to, sym, object, version);
113 if (!object->is_dynamic())
115 // Record that we've seen this symbol in a regular object.
120 // Record that we've seen this symbol in a dynamic object.
124 unsigned int frombits;
125 switch (sym.get_st_bind())
127 case elfcpp::STB_GLOBAL:
128 frombits = global_flag;
131 case elfcpp::STB_WEAK:
132 frombits = weak_flag;
135 case elfcpp::STB_LOCAL:
136 gold_error(_("%s: invalid STB_LOCAL symbol %s in external symbols"),
137 object->name().c_str(), to->name());
138 frombits = global_flag;
142 gold_error(_("%s: unsupported symbol binding %d for symbol %s"),
143 object->name().c_str(),
144 static_cast<int>(sym.get_st_bind()), to->name());
145 frombits = global_flag;
149 if (!object->is_dynamic())
150 frombits |= regular_flag;
152 frombits |= dynamic_flag;
154 switch (sym.get_st_shndx())
156 case elfcpp::SHN_UNDEF:
157 frombits |= undef_flag;
160 case elfcpp::SHN_COMMON:
161 frombits |= common_flag;
165 if (sym.get_st_type() == elfcpp::STT_COMMON)
166 frombits |= common_flag;
168 frombits |= def_flag;
172 bool adjust_common_sizes;
173 if (Symbol_table::should_override(to, frombits, object,
174 &adjust_common_sizes))
176 typename Sized_symbol<size>::Size_type tosize = to->symsize();
178 to->override(sym, object, version);
180 if (adjust_common_sizes && tosize > to->symsize())
181 to->set_symsize(tosize);
185 if (adjust_common_sizes && sym.get_st_size() > to->symsize())
186 to->set_symsize(sym.get_st_size());
190 // Handle the core of symbol resolution. This is called with the
191 // existing symbol, TO, and a bitflag describing the new symbol. This
192 // returns true if we should override the existing symbol with the new
193 // one, and returns false otherwise. It sets *ADJUST_COMMON_SIZES to
194 // true if we should set the symbol size to the maximum of the TO and
195 // FROM sizes. It handles error conditions.
198 Symbol_table::should_override(const Symbol* to, unsigned int frombits,
199 Object* object, bool* adjust_common_sizes)
201 *adjust_common_sizes = false;
204 switch (to->binding())
206 case elfcpp::STB_GLOBAL:
207 tobits = global_flag;
210 case elfcpp::STB_WEAK:
214 case elfcpp::STB_LOCAL:
215 // We should only see externally visible symbols in the symbol
220 // Any target which wants to handle STB_LOOS, etc., needs to
221 // define a resolve method.
225 if (to->source() == Symbol::FROM_OBJECT
226 && to->object()->is_dynamic())
227 tobits |= dynamic_flag;
229 tobits |= regular_flag;
233 case elfcpp::SHN_UNDEF:
234 tobits |= undef_flag;
237 case elfcpp::SHN_COMMON:
238 tobits |= common_flag;
242 if (to->type() == elfcpp::STT_COMMON)
243 tobits |= common_flag;
249 // FIXME: Warn if either but not both of TO and SYM are STT_TLS.
251 // We use a giant switch table for symbol resolution. This code is
252 // unwieldy, but: 1) it is efficient; 2) we definitely handle all
253 // cases; 3) it is easy to change the handling of a particular case.
254 // The alternative would be a series of conditionals, but it is easy
255 // to get the ordering wrong. This could also be done as a table,
256 // but that is no easier to understand than this large switch
259 // These are the values generated by the bit codes.
262 DEF = global_flag | regular_flag | def_flag,
263 WEAK_DEF = weak_flag | regular_flag | def_flag,
264 DYN_DEF = global_flag | dynamic_flag | def_flag,
265 DYN_WEAK_DEF = weak_flag | dynamic_flag | def_flag,
266 UNDEF = global_flag | regular_flag | undef_flag,
267 WEAK_UNDEF = weak_flag | regular_flag | undef_flag,
268 DYN_UNDEF = global_flag | dynamic_flag | undef_flag,
269 DYN_WEAK_UNDEF = weak_flag | dynamic_flag | undef_flag,
270 COMMON = global_flag | regular_flag | common_flag,
271 WEAK_COMMON = weak_flag | regular_flag | common_flag,
272 DYN_COMMON = global_flag | dynamic_flag | common_flag,
273 DYN_WEAK_COMMON = weak_flag | dynamic_flag | common_flag
276 switch (tobits * 16 + frombits)
279 // Two definitions of the same symbol.
280 // FIXME: Do a better job of reporting locations.
281 gold_error(_("%s: multiple definition of %s"),
282 object != NULL ? object->name().c_str() : _("command line"),
284 gold_error(_("%s: previous definition here"),
285 (to->source() == Symbol::FROM_OBJECT
286 ? to->object()->name().c_str()
287 : _("command line")));
290 case WEAK_DEF * 16 + DEF:
291 // We've seen a weak definition, and now we see a strong
292 // definition. In the original SVR4 linker, this was treated as
293 // a multiple definition error. In the Solaris linker and the
294 // GNU linker, a weak definition followed by a regular
295 // definition causes the weak definition to be overridden. We
296 // are currently compatible with the GNU linker. In the future
297 // we should add a target specific option to change this.
301 case DYN_DEF * 16 + DEF:
302 case DYN_WEAK_DEF * 16 + DEF:
303 // We've seen a definition in a dynamic object, and now we see a
304 // definition in a regular object. The definition in the
305 // regular object overrides the definition in the dynamic
309 case UNDEF * 16 + DEF:
310 case WEAK_UNDEF * 16 + DEF:
311 case DYN_UNDEF * 16 + DEF:
312 case DYN_WEAK_UNDEF * 16 + DEF:
313 // We've seen an undefined reference, and now we see a
314 // definition. We use the definition.
317 case COMMON * 16 + DEF:
318 case WEAK_COMMON * 16 + DEF:
319 case DYN_COMMON * 16 + DEF:
320 case DYN_WEAK_COMMON * 16 + DEF:
321 // We've seen a common symbol and now we see a definition. The
322 // definition overrides. FIXME: We should optionally issue, version a
326 case DEF * 16 + WEAK_DEF:
327 case WEAK_DEF * 16 + WEAK_DEF:
328 // We've seen a definition and now we see a weak definition. We
329 // ignore the new weak definition.
332 case DYN_DEF * 16 + WEAK_DEF:
333 case DYN_WEAK_DEF * 16 + WEAK_DEF:
334 // We've seen a dynamic definition and now we see a regular weak
335 // definition. The regular weak definition overrides.
338 case UNDEF * 16 + WEAK_DEF:
339 case WEAK_UNDEF * 16 + WEAK_DEF:
340 case DYN_UNDEF * 16 + WEAK_DEF:
341 case DYN_WEAK_UNDEF * 16 + WEAK_DEF:
342 // A weak definition of a currently undefined symbol.
345 case COMMON * 16 + WEAK_DEF:
346 case WEAK_COMMON * 16 + WEAK_DEF:
347 // A weak definition does not override a common definition.
350 case DYN_COMMON * 16 + WEAK_DEF:
351 case DYN_WEAK_COMMON * 16 + WEAK_DEF:
352 // A weak definition does override a definition in a dynamic
353 // object. FIXME: We should optionally issue a warning.
356 case DEF * 16 + DYN_DEF:
357 case WEAK_DEF * 16 + DYN_DEF:
358 case DYN_DEF * 16 + DYN_DEF:
359 case DYN_WEAK_DEF * 16 + DYN_DEF:
360 // Ignore a dynamic definition if we already have a definition.
363 case UNDEF * 16 + DYN_DEF:
364 case WEAK_UNDEF * 16 + DYN_DEF:
365 case DYN_UNDEF * 16 + DYN_DEF:
366 case DYN_WEAK_UNDEF * 16 + DYN_DEF:
367 // Use a dynamic definition if we have a reference.
370 case COMMON * 16 + DYN_DEF:
371 case WEAK_COMMON * 16 + DYN_DEF:
372 case DYN_COMMON * 16 + DYN_DEF:
373 case DYN_WEAK_COMMON * 16 + DYN_DEF:
374 // Ignore a dynamic definition if we already have a common
378 case DEF * 16 + DYN_WEAK_DEF:
379 case WEAK_DEF * 16 + DYN_WEAK_DEF:
380 case DYN_DEF * 16 + DYN_WEAK_DEF:
381 case DYN_WEAK_DEF * 16 + DYN_WEAK_DEF:
382 // Ignore a weak dynamic definition if we already have a
386 case UNDEF * 16 + DYN_WEAK_DEF:
387 case WEAK_UNDEF * 16 + DYN_WEAK_DEF:
388 case DYN_UNDEF * 16 + DYN_WEAK_DEF:
389 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_DEF:
390 // Use a weak dynamic definition if we have a reference.
393 case COMMON * 16 + DYN_WEAK_DEF:
394 case WEAK_COMMON * 16 + DYN_WEAK_DEF:
395 case DYN_COMMON * 16 + DYN_WEAK_DEF:
396 case DYN_WEAK_COMMON * 16 + DYN_WEAK_DEF:
397 // Ignore a weak dynamic definition if we already have a common
401 case DEF * 16 + UNDEF:
402 case WEAK_DEF * 16 + UNDEF:
403 case DYN_DEF * 16 + UNDEF:
404 case DYN_WEAK_DEF * 16 + UNDEF:
405 case UNDEF * 16 + UNDEF:
406 // A new undefined reference tells us nothing.
409 case WEAK_UNDEF * 16 + UNDEF:
410 case DYN_UNDEF * 16 + UNDEF:
411 case DYN_WEAK_UNDEF * 16 + UNDEF:
412 // A strong undef overrides a dynamic or weak undef.
415 case COMMON * 16 + UNDEF:
416 case WEAK_COMMON * 16 + UNDEF:
417 case DYN_COMMON * 16 + UNDEF:
418 case DYN_WEAK_COMMON * 16 + UNDEF:
419 // A new undefined reference tells us nothing.
422 case DEF * 16 + WEAK_UNDEF:
423 case WEAK_DEF * 16 + WEAK_UNDEF:
424 case DYN_DEF * 16 + WEAK_UNDEF:
425 case DYN_WEAK_DEF * 16 + WEAK_UNDEF:
426 case UNDEF * 16 + WEAK_UNDEF:
427 case WEAK_UNDEF * 16 + WEAK_UNDEF:
428 case DYN_UNDEF * 16 + WEAK_UNDEF:
429 case DYN_WEAK_UNDEF * 16 + WEAK_UNDEF:
430 case COMMON * 16 + WEAK_UNDEF:
431 case WEAK_COMMON * 16 + WEAK_UNDEF:
432 case DYN_COMMON * 16 + WEAK_UNDEF:
433 case DYN_WEAK_COMMON * 16 + WEAK_UNDEF:
434 // A new weak undefined reference tells us nothing.
437 case DEF * 16 + DYN_UNDEF:
438 case WEAK_DEF * 16 + DYN_UNDEF:
439 case DYN_DEF * 16 + DYN_UNDEF:
440 case DYN_WEAK_DEF * 16 + DYN_UNDEF:
441 case UNDEF * 16 + DYN_UNDEF:
442 case WEAK_UNDEF * 16 + DYN_UNDEF:
443 case DYN_UNDEF * 16 + DYN_UNDEF:
444 case DYN_WEAK_UNDEF * 16 + DYN_UNDEF:
445 case COMMON * 16 + DYN_UNDEF:
446 case WEAK_COMMON * 16 + DYN_UNDEF:
447 case DYN_COMMON * 16 + DYN_UNDEF:
448 case DYN_WEAK_COMMON * 16 + DYN_UNDEF:
449 // A new dynamic undefined reference tells us nothing.
452 case DEF * 16 + DYN_WEAK_UNDEF:
453 case WEAK_DEF * 16 + DYN_WEAK_UNDEF:
454 case DYN_DEF * 16 + DYN_WEAK_UNDEF:
455 case DYN_WEAK_DEF * 16 + DYN_WEAK_UNDEF:
456 case UNDEF * 16 + DYN_WEAK_UNDEF:
457 case WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
458 case DYN_UNDEF * 16 + DYN_WEAK_UNDEF:
459 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
460 case COMMON * 16 + DYN_WEAK_UNDEF:
461 case WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
462 case DYN_COMMON * 16 + DYN_WEAK_UNDEF:
463 case DYN_WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
464 // A new weak dynamic undefined reference tells us nothing.
467 case DEF * 16 + COMMON:
468 // A common symbol does not override a definition.
471 case WEAK_DEF * 16 + COMMON:
472 case DYN_DEF * 16 + COMMON:
473 case DYN_WEAK_DEF * 16 + COMMON:
474 // A common symbol does override a weak definition or a dynamic
478 case UNDEF * 16 + COMMON:
479 case WEAK_UNDEF * 16 + COMMON:
480 case DYN_UNDEF * 16 + COMMON:
481 case DYN_WEAK_UNDEF * 16 + COMMON:
482 // A common symbol is a definition for a reference.
485 case COMMON * 16 + COMMON:
486 // Set the size to the maximum.
487 *adjust_common_sizes = true;
490 case WEAK_COMMON * 16 + COMMON:
491 // I'm not sure just what a weak common symbol means, but
492 // presumably it can be overridden by a regular common symbol.
495 case DYN_COMMON * 16 + COMMON:
496 case DYN_WEAK_COMMON * 16 + COMMON:
497 // Use the real common symbol, but adjust the size if necessary.
498 *adjust_common_sizes = true;
501 case DEF * 16 + WEAK_COMMON:
502 case WEAK_DEF * 16 + WEAK_COMMON:
503 case DYN_DEF * 16 + WEAK_COMMON:
504 case DYN_WEAK_DEF * 16 + WEAK_COMMON:
505 // Whatever a weak common symbol is, it won't override a
509 case UNDEF * 16 + WEAK_COMMON:
510 case WEAK_UNDEF * 16 + WEAK_COMMON:
511 case DYN_UNDEF * 16 + WEAK_COMMON:
512 case DYN_WEAK_UNDEF * 16 + WEAK_COMMON:
513 // A weak common symbol is better than an undefined symbol.
516 case COMMON * 16 + WEAK_COMMON:
517 case WEAK_COMMON * 16 + WEAK_COMMON:
518 case DYN_COMMON * 16 + WEAK_COMMON:
519 case DYN_WEAK_COMMON * 16 + WEAK_COMMON:
520 // Ignore a weak common symbol in the presence of a real common
524 case DEF * 16 + DYN_COMMON:
525 case WEAK_DEF * 16 + DYN_COMMON:
526 case DYN_DEF * 16 + DYN_COMMON:
527 case DYN_WEAK_DEF * 16 + DYN_COMMON:
528 // Ignore a dynamic common symbol in the presence of a
532 case UNDEF * 16 + DYN_COMMON:
533 case WEAK_UNDEF * 16 + DYN_COMMON:
534 case DYN_UNDEF * 16 + DYN_COMMON:
535 case DYN_WEAK_UNDEF * 16 + DYN_COMMON:
536 // A dynamic common symbol is a definition of sorts.
539 case COMMON * 16 + DYN_COMMON:
540 case WEAK_COMMON * 16 + DYN_COMMON:
541 case DYN_COMMON * 16 + DYN_COMMON:
542 case DYN_WEAK_COMMON * 16 + DYN_COMMON:
543 // Set the size to the maximum.
544 *adjust_common_sizes = true;
547 case DEF * 16 + DYN_WEAK_COMMON:
548 case WEAK_DEF * 16 + DYN_WEAK_COMMON:
549 case DYN_DEF * 16 + DYN_WEAK_COMMON:
550 case DYN_WEAK_DEF * 16 + DYN_WEAK_COMMON:
551 // A common symbol is ignored in the face of a definition.
554 case UNDEF * 16 + DYN_WEAK_COMMON:
555 case WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
556 case DYN_UNDEF * 16 + DYN_WEAK_COMMON:
557 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
558 // I guess a weak common symbol is better than a definition.
561 case COMMON * 16 + DYN_WEAK_COMMON:
562 case WEAK_COMMON * 16 + DYN_WEAK_COMMON:
563 case DYN_COMMON * 16 + DYN_WEAK_COMMON:
564 case DYN_WEAK_COMMON * 16 + DYN_WEAK_COMMON:
565 // Set the size to the maximum.
566 *adjust_common_sizes = true;
574 // A special case of should_override which is only called for a strong
575 // defined symbol from a regular object file. This is used when
576 // defining special symbols.
579 Symbol_table::should_override_with_special(const Symbol* to)
581 bool adjust_common_sizes;
582 unsigned int frombits = global_flag | regular_flag | def_flag;
583 bool ret = Symbol_table::should_override(to, frombits, NULL,
584 &adjust_common_sizes);
585 gold_assert(!adjust_common_sizes);
589 // Override symbol base with a special symbol.
592 Symbol::override_base_with_special(const Symbol* from)
594 this->source_ = from->source_;
595 switch (from->source_)
598 this->u_.from_object = from->u_.from_object;
601 this->u_.in_output_data = from->u_.in_output_data;
603 case IN_OUTPUT_SEGMENT:
604 this->u_.in_output_segment = from->u_.in_output_segment;
613 if (from->version_ != NULL && this->version_ != from->version_)
615 gold_assert(this->version_ == NULL);
616 this->version_ = from->version_;
619 this->type_ = from->type_;
620 this->binding_ = from->binding_;
621 this->visibility_ = from->visibility_;
622 this->nonvis_ = from->nonvis_;
624 // Special symbols are always considered to be regular symbols.
625 this->in_reg_ = true;
628 // Override a symbol with a special symbol.
632 Sized_symbol<size>::override_with_special(const Sized_symbol<size>* from)
634 this->override_base_with_special(from);
635 this->value_ = from->value_;
636 this->symsize_ = from->symsize_;
639 // Instantiate the templates we need. We could use the configure
640 // script to restrict this to only the ones needed for implemented
643 #ifdef HAVE_TARGET_32_LITTLE
646 Symbol_table::resolve<32, false>(
647 Sized_symbol<32>* to,
648 const elfcpp::Sym<32, false>& sym,
650 const char* version);
653 #ifdef HAVE_TARGET_32_BIG
656 Symbol_table::resolve<32, true>(
657 Sized_symbol<32>* to,
658 const elfcpp::Sym<32, true>& sym,
660 const char* version);
663 #ifdef HAVE_TARGET_64_LITTLE
666 Symbol_table::resolve<64, false>(
667 Sized_symbol<64>* to,
668 const elfcpp::Sym<64, false>& sym,
670 const char* version);
673 #ifdef HAVE_TARGET_64_BIG
676 Symbol_table::resolve<64, true>(
677 Sized_symbol<64>* to,
678 const elfcpp::Sym<64, true>& sym,
680 const char* version);
683 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
686 Sized_symbol<32>::override_with_special(const Sized_symbol<32>*);
689 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
692 Sized_symbol<64>::override_with_special(const Sized_symbol<64>*);
695 } // End namespace gold.