1 // resolve.cc -- symbol resolution for gold
3 // Copyright (C) 2006-2015 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.
34 // Symbol methods used in this file.
36 // This symbol is being overridden by another symbol whose version is
37 // VERSION. Update the VERSION_ field accordingly.
40 Symbol::override_version(const char* version
)
44 // This is the case where this symbol is NAME/VERSION, and the
45 // version was not marked as hidden. That makes it the default
46 // version, so we create NAME/NULL. Later we see another symbol
47 // NAME/NULL, and that symbol is overriding this one. In this
48 // case, since NAME/VERSION is the default, we make NAME/NULL
49 // override NAME/VERSION as well. They are already the same
50 // Symbol structure. Setting the VERSION_ field to NULL ensures
51 // that it will be output with the correct, empty, version.
52 this->version_
= version
;
56 // This is the case where this symbol is NAME/VERSION_ONE, and
57 // now we see NAME/VERSION_TWO, and NAME/VERSION_TWO is
58 // overriding NAME. If VERSION_ONE and VERSION_TWO are
59 // different, then this can only happen when VERSION_ONE is NULL
60 // and VERSION_TWO is not hidden.
61 gold_assert(this->version_
== version
|| this->version_
== NULL
);
62 this->version_
= version
;
66 // This symbol is being overidden by another symbol whose visibility
67 // is VISIBILITY. Updated the VISIBILITY_ field accordingly.
70 Symbol::override_visibility(elfcpp::STV visibility
)
72 // The rule for combining visibility is that we always choose the
73 // most constrained visibility. In order of increasing constraint,
74 // visibility goes PROTECTED, HIDDEN, INTERNAL. This is the reverse
75 // of the numeric values, so the effect is that we always want the
76 // smallest non-zero value.
77 if (visibility
!= elfcpp::STV_DEFAULT
)
79 if (this->visibility_
== elfcpp::STV_DEFAULT
)
80 this->visibility_
= visibility
;
81 else if (this->visibility_
> visibility
)
82 this->visibility_
= visibility
;
86 // Override the fields in Symbol.
88 template<int size
, bool big_endian
>
90 Symbol::override_base(const elfcpp::Sym
<size
, big_endian
>& sym
,
91 unsigned int st_shndx
, bool is_ordinary
,
92 Object
* object
, const char* version
)
94 gold_assert(this->source_
== FROM_OBJECT
);
95 this->u_
.from_object
.object
= object
;
96 this->override_version(version
);
97 this->u_
.from_object
.shndx
= st_shndx
;
98 this->is_ordinary_shndx_
= is_ordinary
;
99 // Don't override st_type from plugin placeholder symbols.
100 if (object
->pluginobj() == NULL
)
102 // Turn IFUNC symbols from shared libraries into normal FUNC symbols.
103 elfcpp::STT type
= sym
.get_st_type();
104 if (object
->is_dynamic() && type
== elfcpp::STT_GNU_IFUNC
)
105 type
= elfcpp::STT_FUNC
;
108 this->binding_
= sym
.get_st_bind();
109 this->override_visibility(sym
.get_st_visibility());
110 this->nonvis_
= sym
.get_st_nonvis();
111 if (object
->is_dynamic())
112 this->in_dyn_
= true;
114 this->in_reg_
= true;
117 // Override the fields in Sized_symbol.
120 template<bool big_endian
>
122 Sized_symbol
<size
>::override(const elfcpp::Sym
<size
, big_endian
>& sym
,
123 unsigned st_shndx
, bool is_ordinary
,
124 Object
* object
, const char* version
)
126 this->override_base(sym
, st_shndx
, is_ordinary
, object
, version
);
127 this->value_
= sym
.get_st_value();
128 this->symsize_
= sym
.get_st_size();
131 // Override TOSYM with symbol FROMSYM, defined in OBJECT, with version
132 // VERSION. This handles all aliases of TOSYM.
134 template<int size
, bool big_endian
>
136 Symbol_table::override(Sized_symbol
<size
>* tosym
,
137 const elfcpp::Sym
<size
, big_endian
>& fromsym
,
138 unsigned int st_shndx
, bool is_ordinary
,
139 Object
* object
, const char* version
)
141 tosym
->override(fromsym
, st_shndx
, is_ordinary
, object
, version
);
142 if (tosym
->has_alias())
144 Symbol
* sym
= this->weak_aliases_
[tosym
];
145 gold_assert(sym
!= NULL
);
146 Sized_symbol
<size
>* ssym
= this->get_sized_symbol
<size
>(sym
);
149 ssym
->override(fromsym
, st_shndx
, is_ordinary
, object
, version
);
150 sym
= this->weak_aliases_
[ssym
];
151 gold_assert(sym
!= NULL
);
152 ssym
= this->get_sized_symbol
<size
>(sym
);
154 while (ssym
!= tosym
);
158 // The resolve functions build a little code for each symbol.
159 // Bit 0: 0 for global, 1 for weak.
160 // Bit 1: 0 for regular object, 1 for shared object
161 // Bits 2-3: 0 for normal, 1 for undefined, 2 for common
162 // This gives us values from 0 to 11.
164 static const int global_or_weak_shift
= 0;
165 static const unsigned int global_flag
= 0 << global_or_weak_shift
;
166 static const unsigned int weak_flag
= 1 << global_or_weak_shift
;
168 static const int regular_or_dynamic_shift
= 1;
169 static const unsigned int regular_flag
= 0 << regular_or_dynamic_shift
;
170 static const unsigned int dynamic_flag
= 1 << regular_or_dynamic_shift
;
172 static const int def_undef_or_common_shift
= 2;
173 static const unsigned int def_flag
= 0 << def_undef_or_common_shift
;
174 static const unsigned int undef_flag
= 1 << def_undef_or_common_shift
;
175 static const unsigned int common_flag
= 2 << def_undef_or_common_shift
;
177 // This convenience function combines all the flags based on facts
181 symbol_to_bits(elfcpp::STB binding
, bool is_dynamic
,
182 unsigned int shndx
, bool is_ordinary
)
188 case elfcpp::STB_GLOBAL
:
189 case elfcpp::STB_GNU_UNIQUE
:
193 case elfcpp::STB_WEAK
:
197 case elfcpp::STB_LOCAL
:
198 // We should only see externally visible symbols in the symbol
200 gold_error(_("invalid STB_LOCAL symbol in external symbols"));
204 // Any target which wants to handle STB_LOOS, etc., needs to
205 // define a resolve method.
206 gold_error(_("unsupported symbol binding %d"), static_cast<int>(binding
));
211 bits
|= dynamic_flag
;
213 bits
|= regular_flag
;
217 case elfcpp::SHN_UNDEF
:
221 case elfcpp::SHN_COMMON
:
227 if (!is_ordinary
&& Symbol::is_common_shndx(shndx
))
237 // Resolve a symbol. This is called the second and subsequent times
238 // we see a symbol. TO is the pre-existing symbol. ST_SHNDX is the
239 // section index for SYM, possibly adjusted for many sections.
240 // IS_ORDINARY is whether ST_SHNDX is a normal section index rather
241 // than a special code. ORIG_ST_SHNDX is the original section index,
242 // before any munging because of discarded sections, except that all
243 // non-ordinary section indexes are mapped to SHN_UNDEF. VERSION is
244 // the version of SYM.
246 template<int size
, bool big_endian
>
248 Symbol_table::resolve(Sized_symbol
<size
>* to
,
249 const elfcpp::Sym
<size
, big_endian
>& sym
,
250 unsigned int st_shndx
, bool is_ordinary
,
251 unsigned int orig_st_shndx
,
252 Object
* object
, const char* version
,
253 bool is_default_version
)
255 // It's possible for a symbol to be defined in an object file
256 // using .symver to give it a version, and for there to also be
257 // a linker script giving that symbol the same version. We
258 // don't want to give a multiple-definition error for this
259 // harmless redefinition.
261 if (to
->source() == Symbol::FROM_OBJECT
262 && to
->object() == object
265 && to
->shndx(&to_is_ordinary
) == st_shndx
267 && to
->value() == sym
.get_st_value())
270 if (parameters
->target().has_resolve())
272 Sized_target
<size
, big_endian
>* sized_target
;
273 sized_target
= parameters
->sized_target
<size
, big_endian
>();
274 sized_target
->resolve(to
, sym
, object
, version
);
278 if (!object
->is_dynamic())
280 if (sym
.get_st_type() == elfcpp::STT_COMMON
281 && (is_ordinary
|| !Symbol::is_common_shndx(st_shndx
)))
283 gold_warning(_("STT_COMMON symbol '%s' in %s "
284 "is not in a common section"),
285 to
->demangled_name().c_str(),
286 to
->object()->name().c_str());
289 // Record that we've seen this symbol in a regular object.
292 else if (st_shndx
== elfcpp::SHN_UNDEF
293 && (to
->visibility() == elfcpp::STV_HIDDEN
294 || to
->visibility() == elfcpp::STV_INTERNAL
))
296 // The symbol is hidden, so a reference from a shared object
297 // cannot bind to it. We tried issuing a warning in this case,
298 // but that produces false positives when the symbol is
299 // actually resolved in a different shared object (PR 15574).
304 // Record that we've seen this symbol in a dynamic object.
308 // Record if we've seen this symbol in a real ELF object (i.e., the
309 // symbol is referenced from outside the world known to the plugin).
310 if (object
->pluginobj() == NULL
&& !object
->is_dynamic())
311 to
->set_in_real_elf();
313 // If we're processing replacement files, allow new symbols to override
314 // the placeholders from the plugin objects.
315 // Treat common symbols specially since it is possible that an ELF
316 // file increased the size of the alignment.
317 if (to
->source() == Symbol::FROM_OBJECT
)
319 Pluginobj
* obj
= to
->object()->pluginobj();
321 && parameters
->options().plugins()->in_replacement_phase())
323 bool adjust_common
= false;
324 typename Sized_symbol
<size
>::Size_type tosize
= 0;
325 typename Sized_symbol
<size
>::Value_type tovalue
= 0;
327 && !is_ordinary
&& Symbol::is_common_shndx(st_shndx
))
329 adjust_common
= true;
330 tosize
= to
->symsize();
331 tovalue
= to
->value();
333 this->override(to
, sym
, st_shndx
, is_ordinary
, object
, version
);
336 if (tosize
> to
->symsize())
337 to
->set_symsize(tosize
);
338 if (tovalue
> to
->value())
339 to
->set_value(tovalue
);
345 // A new weak undefined reference, merging with an old weak
346 // reference, could be a One Definition Rule (ODR) violation --
347 // especially if the types or sizes of the references differ. We'll
348 // store such pairs and look them up later to make sure they
349 // actually refer to the same lines of code. We also check
350 // combinations of weak and strong, which might occur if one case is
351 // inline and the other is not. (Note: not all ODR violations can
352 // be found this way, and not everything this finds is an ODR
353 // violation. But it's helpful to warn about.)
354 if (parameters
->options().detect_odr_violations()
355 && (sym
.get_st_bind() == elfcpp::STB_WEAK
356 || to
->binding() == elfcpp::STB_WEAK
)
357 && orig_st_shndx
!= elfcpp::SHN_UNDEF
358 && to
->shndx(&to_is_ordinary
) != elfcpp::SHN_UNDEF
360 && sym
.get_st_size() != 0 // Ignore weird 0-sized symbols.
361 && to
->symsize() != 0
362 && (sym
.get_st_type() != to
->type()
363 || sym
.get_st_size() != to
->symsize())
364 // C does not have a concept of ODR, so we only need to do this
365 // on C++ symbols. These have (mangled) names starting with _Z.
366 && to
->name()[0] == '_' && to
->name()[1] == 'Z')
368 Symbol_location fromloc
369 = { object
, orig_st_shndx
, static_cast<off_t
>(sym
.get_st_value()) };
370 Symbol_location toloc
= { to
->object(), to
->shndx(&to_is_ordinary
),
371 static_cast<off_t
>(to
->value()) };
372 this->candidate_odr_violations_
[to
->name()].insert(fromloc
);
373 this->candidate_odr_violations_
[to
->name()].insert(toloc
);
376 // Plugins don't provide a symbol type, so adopt the existing type
377 // if the FROM symbol is from a plugin.
378 elfcpp::STT fromtype
= (object
->pluginobj() != NULL
380 : sym
.get_st_type());
381 unsigned int frombits
= symbol_to_bits(sym
.get_st_bind(),
382 object
->is_dynamic(),
383 st_shndx
, is_ordinary
);
385 bool adjust_common_sizes
;
387 typename Sized_symbol
<size
>::Size_type tosize
= to
->symsize();
388 if (Symbol_table::should_override(to
, frombits
, fromtype
, OBJECT
,
389 object
, &adjust_common_sizes
,
390 &adjust_dyndef
, is_default_version
))
392 elfcpp::STB tobinding
= to
->binding();
393 typename Sized_symbol
<size
>::Value_type tovalue
= to
->value();
394 this->override(to
, sym
, st_shndx
, is_ordinary
, object
, version
);
395 if (adjust_common_sizes
)
397 if (tosize
> to
->symsize())
398 to
->set_symsize(tosize
);
399 if (tovalue
> to
->value())
400 to
->set_value(tovalue
);
404 // We are overriding an UNDEF or WEAK UNDEF with a DYN DEF.
405 // Remember which kind of UNDEF it was for future reference.
406 to
->set_undef_binding(tobinding
);
411 if (adjust_common_sizes
)
413 if (sym
.get_st_size() > tosize
)
414 to
->set_symsize(sym
.get_st_size());
415 if (sym
.get_st_value() > to
->value())
416 to
->set_value(sym
.get_st_value());
420 // We are keeping a DYN DEF after seeing an UNDEF or WEAK UNDEF.
421 // Remember which kind of UNDEF it was.
422 to
->set_undef_binding(sym
.get_st_bind());
424 // The ELF ABI says that even for a reference to a symbol we
425 // merge the visibility.
426 to
->override_visibility(sym
.get_st_visibility());
429 if (adjust_common_sizes
&& parameters
->options().warn_common())
431 if (tosize
> sym
.get_st_size())
432 Symbol_table::report_resolve_problem(false,
433 _("common of '%s' overriding "
436 else if (tosize
< sym
.get_st_size())
437 Symbol_table::report_resolve_problem(false,
438 _("common of '%s' overidden by "
442 Symbol_table::report_resolve_problem(false,
443 _("multiple common of '%s'"),
448 // Handle the core of symbol resolution. This is called with the
449 // existing symbol, TO, and a bitflag describing the new symbol. This
450 // returns true if we should override the existing symbol with the new
451 // one, and returns false otherwise. It sets *ADJUST_COMMON_SIZES to
452 // true if we should set the symbol size to the maximum of the TO and
453 // FROM sizes. It handles error conditions.
456 Symbol_table::should_override(const Symbol
* to
, unsigned int frombits
,
457 elfcpp::STT fromtype
, Defined defined
,
458 Object
* object
, bool* adjust_common_sizes
,
459 bool* adjust_dyndef
, bool is_default_version
)
461 *adjust_common_sizes
= false;
462 *adjust_dyndef
= false;
465 if (to
->source() == Symbol::IS_UNDEFINED
)
466 tobits
= symbol_to_bits(to
->binding(), false, elfcpp::SHN_UNDEF
, true);
467 else if (to
->source() != Symbol::FROM_OBJECT
)
468 tobits
= symbol_to_bits(to
->binding(), false, elfcpp::SHN_ABS
, false);
472 unsigned int shndx
= to
->shndx(&is_ordinary
);
473 tobits
= symbol_to_bits(to
->binding(),
474 to
->object()->is_dynamic(),
479 if ((to
->type() == elfcpp::STT_TLS
) ^ (fromtype
== elfcpp::STT_TLS
)
480 && !to
->is_placeholder())
481 Symbol_table::report_resolve_problem(true,
482 _("symbol '%s' used as both __thread "
484 to
, defined
, object
);
486 // We use a giant switch table for symbol resolution. This code is
487 // unwieldy, but: 1) it is efficient; 2) we definitely handle all
488 // cases; 3) it is easy to change the handling of a particular case.
489 // The alternative would be a series of conditionals, but it is easy
490 // to get the ordering wrong. This could also be done as a table,
491 // but that is no easier to understand than this large switch
494 // These are the values generated by the bit codes.
497 DEF
= global_flag
| regular_flag
| def_flag
,
498 WEAK_DEF
= weak_flag
| regular_flag
| def_flag
,
499 DYN_DEF
= global_flag
| dynamic_flag
| def_flag
,
500 DYN_WEAK_DEF
= weak_flag
| dynamic_flag
| def_flag
,
501 UNDEF
= global_flag
| regular_flag
| undef_flag
,
502 WEAK_UNDEF
= weak_flag
| regular_flag
| undef_flag
,
503 DYN_UNDEF
= global_flag
| dynamic_flag
| undef_flag
,
504 DYN_WEAK_UNDEF
= weak_flag
| dynamic_flag
| undef_flag
,
505 COMMON
= global_flag
| regular_flag
| common_flag
,
506 WEAK_COMMON
= weak_flag
| regular_flag
| common_flag
,
507 DYN_COMMON
= global_flag
| dynamic_flag
| common_flag
,
508 DYN_WEAK_COMMON
= weak_flag
| dynamic_flag
| common_flag
511 switch (tobits
* 16 + frombits
)
514 // Two definitions of the same symbol.
516 // If either symbol is defined by an object included using
517 // --just-symbols, then don't warn. This is for compatibility
518 // with the GNU linker. FIXME: This is a hack.
519 if ((to
->source() == Symbol::FROM_OBJECT
&& to
->object()->just_symbols())
520 || (object
!= NULL
&& object
->just_symbols()))
523 if (!parameters
->options().muldefs())
524 Symbol_table::report_resolve_problem(true,
525 _("multiple definition of '%s'"),
526 to
, defined
, object
);
529 case WEAK_DEF
* 16 + DEF
:
530 // We've seen a weak definition, and now we see a strong
531 // definition. In the original SVR4 linker, this was treated as
532 // a multiple definition error. In the Solaris linker and the
533 // GNU linker, a weak definition followed by a regular
534 // definition causes the weak definition to be overridden. We
535 // are currently compatible with the GNU linker. In the future
536 // we should add a target specific option to change this.
540 case DYN_DEF
* 16 + DEF
:
541 case DYN_WEAK_DEF
* 16 + DEF
:
542 // We've seen a definition in a dynamic object, and now we see a
543 // definition in a regular object. The definition in the
544 // regular object overrides the definition in the dynamic
548 case UNDEF
* 16 + DEF
:
549 case WEAK_UNDEF
* 16 + DEF
:
550 case DYN_UNDEF
* 16 + DEF
:
551 case DYN_WEAK_UNDEF
* 16 + DEF
:
552 // We've seen an undefined reference, and now we see a
553 // definition. We use the definition.
556 case COMMON
* 16 + DEF
:
557 case WEAK_COMMON
* 16 + DEF
:
558 case DYN_COMMON
* 16 + DEF
:
559 case DYN_WEAK_COMMON
* 16 + DEF
:
560 // We've seen a common symbol and now we see a definition. The
561 // definition overrides.
562 if (parameters
->options().warn_common())
563 Symbol_table::report_resolve_problem(false,
564 _("definition of '%s' overriding "
566 to
, defined
, object
);
569 case DEF
* 16 + WEAK_DEF
:
570 case WEAK_DEF
* 16 + WEAK_DEF
:
571 // We've seen a definition and now we see a weak definition. We
572 // ignore the new weak definition.
575 case DYN_DEF
* 16 + WEAK_DEF
:
576 case DYN_WEAK_DEF
* 16 + WEAK_DEF
:
577 // We've seen a dynamic definition and now we see a regular weak
578 // definition. The regular weak definition overrides.
581 case UNDEF
* 16 + WEAK_DEF
:
582 case WEAK_UNDEF
* 16 + WEAK_DEF
:
583 case DYN_UNDEF
* 16 + WEAK_DEF
:
584 case DYN_WEAK_UNDEF
* 16 + WEAK_DEF
:
585 // A weak definition of a currently undefined symbol.
588 case COMMON
* 16 + WEAK_DEF
:
589 case WEAK_COMMON
* 16 + WEAK_DEF
:
590 // A weak definition does not override a common definition.
593 case DYN_COMMON
* 16 + WEAK_DEF
:
594 case DYN_WEAK_COMMON
* 16 + WEAK_DEF
:
595 // A weak definition does override a definition in a dynamic
597 if (parameters
->options().warn_common())
598 Symbol_table::report_resolve_problem(false,
599 _("definition of '%s' overriding "
600 "dynamic common definition"),
601 to
, defined
, object
);
604 case DEF
* 16 + DYN_DEF
:
605 case WEAK_DEF
* 16 + DYN_DEF
:
606 // Ignore a dynamic definition if we already have a definition.
609 case DYN_DEF
* 16 + DYN_DEF
:
610 case DYN_WEAK_DEF
* 16 + DYN_DEF
:
611 // Ignore a dynamic definition if we already have a definition,
612 // unless the existing definition is an unversioned definition
613 // in the same dynamic object, and the new definition is a
615 if (to
->object() == object
616 && to
->version() == NULL
617 && is_default_version
)
621 case UNDEF
* 16 + DYN_DEF
:
622 case DYN_UNDEF
* 16 + DYN_DEF
:
623 case DYN_WEAK_UNDEF
* 16 + DYN_DEF
:
624 // Use a dynamic definition if we have a reference.
627 case WEAK_UNDEF
* 16 + DYN_DEF
:
628 // When overriding a weak undef by a dynamic definition,
629 // we need to remember that the original undef was weak.
630 *adjust_dyndef
= true;
633 case COMMON
* 16 + DYN_DEF
:
634 case WEAK_COMMON
* 16 + DYN_DEF
:
635 case DYN_COMMON
* 16 + DYN_DEF
:
636 case DYN_WEAK_COMMON
* 16 + DYN_DEF
:
637 // Ignore a dynamic definition if we already have a common
641 case DEF
* 16 + DYN_WEAK_DEF
:
642 case WEAK_DEF
* 16 + DYN_WEAK_DEF
:
643 case DYN_DEF
* 16 + DYN_WEAK_DEF
:
644 case DYN_WEAK_DEF
* 16 + DYN_WEAK_DEF
:
645 // Ignore a weak dynamic definition if we already have a
649 case UNDEF
* 16 + DYN_WEAK_DEF
:
650 // When overriding an undef by a dynamic weak definition,
651 // we need to remember that the original undef was not weak.
652 *adjust_dyndef
= true;
655 case DYN_UNDEF
* 16 + DYN_WEAK_DEF
:
656 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_DEF
:
657 // Use a weak dynamic definition if we have a reference.
660 case WEAK_UNDEF
* 16 + DYN_WEAK_DEF
:
661 // When overriding a weak undef by a dynamic definition,
662 // we need to remember that the original undef was weak.
663 *adjust_dyndef
= true;
666 case COMMON
* 16 + DYN_WEAK_DEF
:
667 case WEAK_COMMON
* 16 + DYN_WEAK_DEF
:
668 case DYN_COMMON
* 16 + DYN_WEAK_DEF
:
669 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_DEF
:
670 // Ignore a weak dynamic definition if we already have a common
674 case DEF
* 16 + UNDEF
:
675 case WEAK_DEF
* 16 + UNDEF
:
676 case UNDEF
* 16 + UNDEF
:
677 // A new undefined reference tells us nothing.
680 case DYN_DEF
* 16 + UNDEF
:
681 case DYN_WEAK_DEF
* 16 + UNDEF
:
682 // For a dynamic def, we need to remember which kind of undef we see.
683 *adjust_dyndef
= true;
686 case WEAK_UNDEF
* 16 + UNDEF
:
687 case DYN_UNDEF
* 16 + UNDEF
:
688 case DYN_WEAK_UNDEF
* 16 + UNDEF
:
689 // A strong undef overrides a dynamic or weak undef.
692 case COMMON
* 16 + UNDEF
:
693 case WEAK_COMMON
* 16 + UNDEF
:
694 case DYN_COMMON
* 16 + UNDEF
:
695 case DYN_WEAK_COMMON
* 16 + UNDEF
:
696 // A new undefined reference tells us nothing.
699 case DEF
* 16 + WEAK_UNDEF
:
700 case WEAK_DEF
* 16 + WEAK_UNDEF
:
701 case UNDEF
* 16 + WEAK_UNDEF
:
702 case WEAK_UNDEF
* 16 + WEAK_UNDEF
:
703 case DYN_UNDEF
* 16 + WEAK_UNDEF
:
704 case COMMON
* 16 + WEAK_UNDEF
:
705 case WEAK_COMMON
* 16 + WEAK_UNDEF
:
706 case DYN_COMMON
* 16 + WEAK_UNDEF
:
707 case DYN_WEAK_COMMON
* 16 + WEAK_UNDEF
:
708 // A new weak undefined reference tells us nothing unless the
709 // exisiting symbol is a dynamic weak reference.
712 case DYN_WEAK_UNDEF
* 16 + WEAK_UNDEF
:
713 // A new weak reference overrides an existing dynamic weak reference.
714 // This is necessary because a dynamic weak reference remembers
715 // the old binding, which may not be weak. If we keeps the existing
716 // dynamic weak reference, the weakness may be dropped in the output.
719 case DYN_DEF
* 16 + WEAK_UNDEF
:
720 case DYN_WEAK_DEF
* 16 + WEAK_UNDEF
:
721 // For a dynamic def, we need to remember which kind of undef we see.
722 *adjust_dyndef
= true;
725 case DEF
* 16 + DYN_UNDEF
:
726 case WEAK_DEF
* 16 + DYN_UNDEF
:
727 case DYN_DEF
* 16 + DYN_UNDEF
:
728 case DYN_WEAK_DEF
* 16 + DYN_UNDEF
:
729 case UNDEF
* 16 + DYN_UNDEF
:
730 case WEAK_UNDEF
* 16 + DYN_UNDEF
:
731 case DYN_UNDEF
* 16 + DYN_UNDEF
:
732 case DYN_WEAK_UNDEF
* 16 + DYN_UNDEF
:
733 case COMMON
* 16 + DYN_UNDEF
:
734 case WEAK_COMMON
* 16 + DYN_UNDEF
:
735 case DYN_COMMON
* 16 + DYN_UNDEF
:
736 case DYN_WEAK_COMMON
* 16 + DYN_UNDEF
:
737 // A new dynamic undefined reference tells us nothing.
740 case DEF
* 16 + DYN_WEAK_UNDEF
:
741 case WEAK_DEF
* 16 + DYN_WEAK_UNDEF
:
742 case DYN_DEF
* 16 + DYN_WEAK_UNDEF
:
743 case DYN_WEAK_DEF
* 16 + DYN_WEAK_UNDEF
:
744 case UNDEF
* 16 + DYN_WEAK_UNDEF
:
745 case WEAK_UNDEF
* 16 + DYN_WEAK_UNDEF
:
746 case DYN_UNDEF
* 16 + DYN_WEAK_UNDEF
:
747 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_UNDEF
:
748 case COMMON
* 16 + DYN_WEAK_UNDEF
:
749 case WEAK_COMMON
* 16 + DYN_WEAK_UNDEF
:
750 case DYN_COMMON
* 16 + DYN_WEAK_UNDEF
:
751 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_UNDEF
:
752 // A new weak dynamic undefined reference tells us nothing.
755 case DEF
* 16 + COMMON
:
756 // A common symbol does not override a definition.
757 if (parameters
->options().warn_common())
758 Symbol_table::report_resolve_problem(false,
759 _("common '%s' overridden by "
760 "previous definition"),
761 to
, defined
, object
);
764 case WEAK_DEF
* 16 + COMMON
:
765 case DYN_DEF
* 16 + COMMON
:
766 case DYN_WEAK_DEF
* 16 + COMMON
:
767 // A common symbol does override a weak definition or a dynamic
771 case UNDEF
* 16 + COMMON
:
772 case WEAK_UNDEF
* 16 + COMMON
:
773 case DYN_UNDEF
* 16 + COMMON
:
774 case DYN_WEAK_UNDEF
* 16 + COMMON
:
775 // A common symbol is a definition for a reference.
778 case COMMON
* 16 + COMMON
:
779 // Set the size to the maximum.
780 *adjust_common_sizes
= true;
783 case WEAK_COMMON
* 16 + COMMON
:
784 // I'm not sure just what a weak common symbol means, but
785 // presumably it can be overridden by a regular common symbol.
788 case DYN_COMMON
* 16 + COMMON
:
789 case DYN_WEAK_COMMON
* 16 + COMMON
:
790 // Use the real common symbol, but adjust the size if necessary.
791 *adjust_common_sizes
= true;
794 case DEF
* 16 + WEAK_COMMON
:
795 case WEAK_DEF
* 16 + WEAK_COMMON
:
796 case DYN_DEF
* 16 + WEAK_COMMON
:
797 case DYN_WEAK_DEF
* 16 + WEAK_COMMON
:
798 // Whatever a weak common symbol is, it won't override a
802 case UNDEF
* 16 + WEAK_COMMON
:
803 case WEAK_UNDEF
* 16 + WEAK_COMMON
:
804 case DYN_UNDEF
* 16 + WEAK_COMMON
:
805 case DYN_WEAK_UNDEF
* 16 + WEAK_COMMON
:
806 // A weak common symbol is better than an undefined symbol.
809 case COMMON
* 16 + WEAK_COMMON
:
810 case WEAK_COMMON
* 16 + WEAK_COMMON
:
811 case DYN_COMMON
* 16 + WEAK_COMMON
:
812 case DYN_WEAK_COMMON
* 16 + WEAK_COMMON
:
813 // Ignore a weak common symbol in the presence of a real common
817 case DEF
* 16 + DYN_COMMON
:
818 case WEAK_DEF
* 16 + DYN_COMMON
:
819 case DYN_DEF
* 16 + DYN_COMMON
:
820 case DYN_WEAK_DEF
* 16 + DYN_COMMON
:
821 // Ignore a dynamic common symbol in the presence of a
825 case UNDEF
* 16 + DYN_COMMON
:
826 case WEAK_UNDEF
* 16 + DYN_COMMON
:
827 case DYN_UNDEF
* 16 + DYN_COMMON
:
828 case DYN_WEAK_UNDEF
* 16 + DYN_COMMON
:
829 // A dynamic common symbol is a definition of sorts.
832 case COMMON
* 16 + DYN_COMMON
:
833 case WEAK_COMMON
* 16 + DYN_COMMON
:
834 case DYN_COMMON
* 16 + DYN_COMMON
:
835 case DYN_WEAK_COMMON
* 16 + DYN_COMMON
:
836 // Set the size to the maximum.
837 *adjust_common_sizes
= true;
840 case DEF
* 16 + DYN_WEAK_COMMON
:
841 case WEAK_DEF
* 16 + DYN_WEAK_COMMON
:
842 case DYN_DEF
* 16 + DYN_WEAK_COMMON
:
843 case DYN_WEAK_DEF
* 16 + DYN_WEAK_COMMON
:
844 // A common symbol is ignored in the face of a definition.
847 case UNDEF
* 16 + DYN_WEAK_COMMON
:
848 case WEAK_UNDEF
* 16 + DYN_WEAK_COMMON
:
849 case DYN_UNDEF
* 16 + DYN_WEAK_COMMON
:
850 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_COMMON
:
851 // I guess a weak common symbol is better than a definition.
854 case COMMON
* 16 + DYN_WEAK_COMMON
:
855 case WEAK_COMMON
* 16 + DYN_WEAK_COMMON
:
856 case DYN_COMMON
* 16 + DYN_WEAK_COMMON
:
857 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_COMMON
:
858 // Set the size to the maximum.
859 *adjust_common_sizes
= true;
867 // Issue an error or warning due to symbol resolution. IS_ERROR
868 // indicates an error rather than a warning. MSG is the error
869 // message; it is expected to have a %s for the symbol name. TO is
870 // the existing symbol. DEFINED/OBJECT is where the new symbol was
873 // FIXME: We should have better location information here. When the
874 // symbol is defined, we should be able to pull the location from the
875 // debug info if there is any.
878 Symbol_table::report_resolve_problem(bool is_error
, const char* msg
,
879 const Symbol
* to
, Defined defined
,
882 std::string
demangled(to
->demangled_name());
883 size_t len
= strlen(msg
) + demangled
.length() + 10;
884 char* buf
= new char[len
];
885 snprintf(buf
, len
, msg
, demangled
.c_str());
891 objname
= object
->name().c_str();
894 objname
= _("COPY reloc");
898 objname
= _("command line");
901 objname
= _("linker script");
904 case INCREMENTAL_BASE
:
905 objname
= _("linker defined");
912 gold_error("%s: %s", objname
, buf
);
914 gold_warning("%s: %s", objname
, buf
);
918 if (to
->source() == Symbol::FROM_OBJECT
)
919 objname
= to
->object()->name().c_str();
921 objname
= _("command line");
922 gold_info("%s: %s: previous definition here", program_name
, objname
);
925 // A special case of should_override which is only called for a strong
926 // defined symbol from a regular object file. This is used when
927 // defining special symbols.
930 Symbol_table::should_override_with_special(const Symbol
* to
,
931 elfcpp::STT fromtype
,
934 bool adjust_common_sizes
;
936 unsigned int frombits
= global_flag
| regular_flag
| def_flag
;
937 bool ret
= Symbol_table::should_override(to
, frombits
, fromtype
, defined
,
938 NULL
, &adjust_common_sizes
,
939 &adjust_dyn_def
, false);
940 gold_assert(!adjust_common_sizes
&& !adjust_dyn_def
);
944 // Override symbol base with a special symbol.
947 Symbol::override_base_with_special(const Symbol
* from
)
949 bool same_name
= this->name_
== from
->name_
;
950 gold_assert(same_name
|| this->has_alias());
952 // If we are overriding an undef, remember the original binding.
953 if (this->is_undefined())
954 this->set_undef_binding(this->binding_
);
956 this->source_
= from
->source_
;
957 switch (from
->source_
)
960 this->u_
.from_object
= from
->u_
.from_object
;
963 this->u_
.in_output_data
= from
->u_
.in_output_data
;
965 case IN_OUTPUT_SEGMENT
:
966 this->u_
.in_output_segment
= from
->u_
.in_output_segment
;
978 // When overriding a versioned symbol with a special symbol, we
979 // may be changing the version. This will happen if we see a
980 // special symbol such as "_end" defined in a shared object with
981 // one version (from a version script), but we want to define it
982 // here with a different version (from a different version
984 this->version_
= from
->version_
;
986 this->type_
= from
->type_
;
987 this->binding_
= from
->binding_
;
988 this->override_visibility(from
->visibility_
);
989 this->nonvis_
= from
->nonvis_
;
991 // Special symbols are always considered to be regular symbols.
992 this->in_reg_
= true;
994 if (from
->needs_dynsym_entry_
)
995 this->needs_dynsym_entry_
= true;
996 if (from
->needs_dynsym_value_
)
997 this->needs_dynsym_value_
= true;
999 this->is_predefined_
= from
->is_predefined_
;
1001 // We shouldn't see these flags. If we do, we need to handle them
1003 gold_assert(!from
->is_forwarder_
);
1004 gold_assert(!from
->has_plt_offset());
1005 gold_assert(!from
->has_warning_
);
1006 gold_assert(!from
->is_copied_from_dynobj_
);
1007 gold_assert(!from
->is_forced_local_
);
1010 // Override a symbol with a special symbol.
1014 Sized_symbol
<size
>::override_with_special(const Sized_symbol
<size
>* from
)
1016 this->override_base_with_special(from
);
1017 this->value_
= from
->value_
;
1018 this->symsize_
= from
->symsize_
;
1021 // Override TOSYM with the special symbol FROMSYM. This handles all
1022 // aliases of TOSYM.
1026 Symbol_table::override_with_special(Sized_symbol
<size
>* tosym
,
1027 const Sized_symbol
<size
>* fromsym
)
1029 tosym
->override_with_special(fromsym
);
1030 if (tosym
->has_alias())
1032 Symbol
* sym
= this->weak_aliases_
[tosym
];
1033 gold_assert(sym
!= NULL
);
1034 Sized_symbol
<size
>* ssym
= this->get_sized_symbol
<size
>(sym
);
1037 ssym
->override_with_special(fromsym
);
1038 sym
= this->weak_aliases_
[ssym
];
1039 gold_assert(sym
!= NULL
);
1040 ssym
= this->get_sized_symbol
<size
>(sym
);
1042 while (ssym
!= tosym
);
1044 if (tosym
->binding() == elfcpp::STB_LOCAL
1045 || ((tosym
->visibility() == elfcpp::STV_HIDDEN
1046 || tosym
->visibility() == elfcpp::STV_INTERNAL
)
1047 && (tosym
->binding() == elfcpp::STB_GLOBAL
1048 || tosym
->binding() == elfcpp::STB_GNU_UNIQUE
1049 || tosym
->binding() == elfcpp::STB_WEAK
)
1050 && !parameters
->options().relocatable()))
1051 this->force_local(tosym
);
1054 // Instantiate the templates we need. We could use the configure
1055 // script to restrict this to only the ones needed for implemented
1058 // We have to instantiate both big and little endian versions because
1059 // these are used by other templates that depends on size only.
1061 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1064 Symbol_table::resolve
<32, false>(
1065 Sized_symbol
<32>* to
,
1066 const elfcpp::Sym
<32, false>& sym
,
1067 unsigned int st_shndx
,
1069 unsigned int orig_st_shndx
,
1071 const char* version
,
1072 bool is_default_version
);
1076 Symbol_table::resolve
<32, true>(
1077 Sized_symbol
<32>* to
,
1078 const elfcpp::Sym
<32, true>& sym
,
1079 unsigned int st_shndx
,
1081 unsigned int orig_st_shndx
,
1083 const char* version
,
1084 bool is_default_version
);
1087 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1090 Symbol_table::resolve
<64, false>(
1091 Sized_symbol
<64>* to
,
1092 const elfcpp::Sym
<64, false>& sym
,
1093 unsigned int st_shndx
,
1095 unsigned int orig_st_shndx
,
1097 const char* version
,
1098 bool is_default_version
);
1102 Symbol_table::resolve
<64, true>(
1103 Sized_symbol
<64>* to
,
1104 const elfcpp::Sym
<64, true>& sym
,
1105 unsigned int st_shndx
,
1107 unsigned int orig_st_shndx
,
1109 const char* version
,
1110 bool is_default_version
);
1113 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1116 Symbol_table::override_with_special
<32>(Sized_symbol
<32>*,
1117 const Sized_symbol
<32>*);
1120 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1123 Symbol_table::override_with_special
<64>(Sized_symbol
<64>*,
1124 const Sized_symbol
<64>*);
1127 } // End namespace gold.