Turn IFUNC symbols from shared libraries into normal FUNC symbols
[external/binutils.git] / gold / resolve.cc
1 // resolve.cc -- symbol resolution for gold
2
3 // Copyright (C) 2006-2015 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 "elfcpp.h"
26 #include "target.h"
27 #include "object.h"
28 #include "symtab.h"
29 #include "plugin.h"
30
31 namespace gold
32 {
33
34 // Symbol methods used in this file.
35
36 // This symbol is being overridden by another symbol whose version is
37 // VERSION.  Update the VERSION_ field accordingly.
38
39 inline void
40 Symbol::override_version(const char* version)
41 {
42   if (version == NULL)
43     {
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;
53     }
54   else
55     {
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;
63     }
64 }
65
66 // This symbol is being overidden by another symbol whose visibility
67 // is VISIBILITY.  Updated the VISIBILITY_ field accordingly.
68
69 inline void
70 Symbol::override_visibility(elfcpp::STV visibility)
71 {
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)
78     {
79       if (this->visibility_ == elfcpp::STV_DEFAULT)
80         this->visibility_ = visibility;
81       else if (this->visibility_ > visibility)
82         this->visibility_ = visibility;
83     }
84 }
85
86 // Override the fields in Symbol.
87
88 template<int size, bool big_endian>
89 void
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)
93 {
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)
101     {
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;
106       this->type_ = type;
107     }
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;
113   else
114     this->in_reg_ = true;
115 }
116
117 // Override the fields in Sized_symbol.
118
119 template<int size>
120 template<bool big_endian>
121 void
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)
125 {
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();
129 }
130
131 // Override TOSYM with symbol FROMSYM, defined in OBJECT, with version
132 // VERSION.  This handles all aliases of TOSYM.
133
134 template<int size, bool big_endian>
135 void
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)
140 {
141   tosym->override(fromsym, st_shndx, is_ordinary, object, version);
142   if (tosym->has_alias())
143     {
144       Symbol* sym = this->weak_aliases_[tosym];
145       gold_assert(sym != NULL);
146       Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
147       do
148         {
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);
153         }
154       while (ssym != tosym);
155     }
156 }
157
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.
163
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;
167
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;
171
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;
176
177 // This convenience function combines all the flags based on facts
178 // about the symbol.
179
180 static unsigned int
181 symbol_to_bits(elfcpp::STB binding, bool is_dynamic,
182                unsigned int shndx, bool is_ordinary)
183 {
184   unsigned int bits;
185
186   switch (binding)
187     {
188     case elfcpp::STB_GLOBAL:
189     case elfcpp::STB_GNU_UNIQUE:
190       bits = global_flag;
191       break;
192
193     case elfcpp::STB_WEAK:
194       bits = weak_flag;
195       break;
196
197     case elfcpp::STB_LOCAL:
198       // We should only see externally visible symbols in the symbol
199       // table.
200       gold_error(_("invalid STB_LOCAL symbol in external symbols"));
201       bits = global_flag;
202
203     default:
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));
207       bits = global_flag;
208     }
209
210   if (is_dynamic)
211     bits |= dynamic_flag;
212   else
213     bits |= regular_flag;
214
215   switch (shndx)
216     {
217     case elfcpp::SHN_UNDEF:
218       bits |= undef_flag;
219       break;
220
221     case elfcpp::SHN_COMMON:
222       if (!is_ordinary)
223         bits |= common_flag;
224       break;
225
226     default:
227       if (!is_ordinary && Symbol::is_common_shndx(shndx))
228         bits |= common_flag;
229       else
230         bits |= def_flag;
231       break;
232     }
233
234   return bits;
235 }
236
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.
245
246 template<int size, bool big_endian>
247 void
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)
254 {
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.
260   bool to_is_ordinary;
261   if (to->source() == Symbol::FROM_OBJECT
262       && to->object() == object
263       && is_ordinary
264       && to->is_defined()
265       && to->shndx(&to_is_ordinary) == st_shndx
266       && to_is_ordinary
267       && to->value() == sym.get_st_value())
268     return;
269
270   if (parameters->target().has_resolve())
271     {
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);
275       return;
276     }
277
278   if (!object->is_dynamic())
279     {
280       if (sym.get_st_type() == elfcpp::STT_COMMON
281           && (is_ordinary || !Symbol::is_common_shndx(st_shndx)))
282         {
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());
287           return;
288         }
289       // Record that we've seen this symbol in a regular object.
290       to->set_in_reg();
291     }
292   else if (st_shndx == elfcpp::SHN_UNDEF
293            && (to->visibility() == elfcpp::STV_HIDDEN
294                || to->visibility() == elfcpp::STV_INTERNAL))
295     {
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).
300       return;
301     }
302   else
303     {
304       // Record that we've seen this symbol in a dynamic object.
305       to->set_in_dyn();
306     }
307
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();
312
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)
318     {
319       Pluginobj* obj = to->object()->pluginobj();
320       if (obj != NULL
321           && parameters->options().plugins()->in_replacement_phase())
322         {
323           bool adjust_common = false;
324           typename Sized_symbol<size>::Size_type tosize = 0;
325           typename Sized_symbol<size>::Value_type tovalue = 0;
326           if (to->is_common()
327               && !is_ordinary && Symbol::is_common_shndx(st_shndx))
328             {
329               adjust_common = true;
330               tosize = to->symsize();
331               tovalue = to->value();
332             }
333           this->override(to, sym, st_shndx, is_ordinary, object, version);
334           if (adjust_common)
335             {
336               if (tosize > to->symsize())
337                 to->set_symsize(tosize);
338               if (tovalue > to->value())
339                 to->set_value(tovalue);
340             }
341           return;
342         }
343     }
344
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
359       && to_is_ordinary
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')
367     {
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);
374     }
375
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
379                           ? to->type()
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);
384
385   bool adjust_common_sizes;
386   bool adjust_dyndef;
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))
391     {
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)
396         {
397           if (tosize > to->symsize())
398             to->set_symsize(tosize);
399           if (tovalue > to->value())
400             to->set_value(tovalue);
401         }
402       if (adjust_dyndef)
403         {
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);
407         }
408     }
409   else
410     {
411       if (adjust_common_sizes)
412         {
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());
417         }
418       if (adjust_dyndef)
419         {
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());
423         }
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());
427     }
428
429   if (adjust_common_sizes && parameters->options().warn_common())
430     {
431       if (tosize > sym.get_st_size())
432         Symbol_table::report_resolve_problem(false,
433                                              _("common of '%s' overriding "
434                                                "smaller common"),
435                                              to, OBJECT, object);
436       else if (tosize < sym.get_st_size())
437         Symbol_table::report_resolve_problem(false,
438                                              _("common of '%s' overidden by "
439                                                "larger common"),
440                                              to, OBJECT, object);
441       else
442         Symbol_table::report_resolve_problem(false,
443                                              _("multiple common of '%s'"),
444                                              to, OBJECT, object);
445     }
446 }
447
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.
454
455 bool
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)
460 {
461   *adjust_common_sizes = false;
462   *adjust_dyndef = false;
463
464   unsigned int tobits;
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);
469   else
470     {
471       bool is_ordinary;
472       unsigned int shndx = to->shndx(&is_ordinary);
473       tobits = symbol_to_bits(to->binding(),
474                               to->object()->is_dynamic(),
475                               shndx,
476                               is_ordinary);
477     }
478
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 "
483                                            "and non-__thread"),
484                                          to, defined, object);
485
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
492   // statement.
493
494   // These are the values generated by the bit codes.
495   enum
496   {
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
509   };
510
511   switch (tobits * 16 + frombits)
512     {
513     case DEF * 16 + DEF:
514       // Two definitions of the same symbol.
515
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()))
521         return false;
522
523       if (!parameters->options().muldefs())
524         Symbol_table::report_resolve_problem(true,
525                                              _("multiple definition of '%s'"),
526                                              to, defined, object);
527       return false;
528
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.
537       // FIXME.
538       return true;
539
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
545       // object.
546       return true;
547
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.
554       return true;
555
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 "
565                                                "common"),
566                                              to, defined, object);
567       return true;
568
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.
573       return false;
574
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.
579       return true;
580
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.
586       return true;
587
588     case COMMON * 16 + WEAK_DEF:
589     case WEAK_COMMON * 16 + WEAK_DEF:
590       // A weak definition does not override a common definition.
591       return false;
592
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
596       // object.
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);
602       return true;
603
604     case DEF * 16 + DYN_DEF:
605     case WEAK_DEF * 16 + DYN_DEF:
606       // Ignore a dynamic definition if we already have a definition.
607       return false;
608
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
614       // default version.
615       if (to->object() == object
616           && to->version() == NULL
617           && is_default_version)
618         return true;
619       return false;
620
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.
625       return true;
626
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;
631       return true;
632
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
638       // definition.
639       return false;
640
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
646       // definition.
647       return false;
648
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;
653       return true;
654
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.
658       return true;
659
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;
664       return true;
665
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
671       // definition.
672       return false;
673
674     case DEF * 16 + UNDEF:
675     case WEAK_DEF * 16 + UNDEF:
676     case UNDEF * 16 + UNDEF:
677       // A new undefined reference tells us nothing.
678       return false;
679
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;
684       return false;
685
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.
690       return true;
691
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.
697       return false;
698
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.
710       return false;
711
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.
717       return true;
718
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;
723       return false;
724
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.
738       return false;
739
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.
753       return false;
754
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);
762       return false;
763
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
768       // definition.
769       return true;
770
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.
776       return true;
777
778     case COMMON * 16 + COMMON:
779       // Set the size to the maximum.
780       *adjust_common_sizes = true;
781       return false;
782
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.
786       return true;
787
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;
792       return true;
793
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
799       // definition.
800       return false;
801
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.
807       return true;
808
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
814       // symbol.
815       return false;
816
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
822       // definition.
823       return false;
824
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.
830       return true;
831
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;
838       return false;
839
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.
845       return false;
846
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.
852       return true;
853
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;
860       return false;
861
862     default:
863       gold_unreachable();
864     }
865 }
866
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
871 // found.
872
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.
876
877 void
878 Symbol_table::report_resolve_problem(bool is_error, const char* msg,
879                                      const Symbol* to, Defined defined,
880                                      Object* object)
881 {
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());
886
887   const char* objname;
888   switch (defined)
889     {
890     case OBJECT:
891       objname = object->name().c_str();
892       break;
893     case COPY:
894       objname = _("COPY reloc");
895       break;
896     case DEFSYM:
897     case UNDEFINED:
898       objname = _("command line");
899       break;
900     case SCRIPT:
901       objname = _("linker script");
902       break;
903     case PREDEFINED:
904     case INCREMENTAL_BASE:
905       objname = _("linker defined");
906       break;
907     default:
908       gold_unreachable();
909     }
910
911   if (is_error)
912     gold_error("%s: %s", objname, buf);
913   else
914     gold_warning("%s: %s", objname, buf);
915
916   delete[] buf;
917
918   if (to->source() == Symbol::FROM_OBJECT)
919     objname = to->object()->name().c_str();
920   else
921     objname = _("command line");
922   gold_info("%s: %s: previous definition here", program_name, objname);
923 }
924
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.
928
929 bool
930 Symbol_table::should_override_with_special(const Symbol* to,
931                                            elfcpp::STT fromtype,
932                                            Defined defined)
933 {
934   bool adjust_common_sizes;
935   bool adjust_dyn_def;
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);
941   return ret;
942 }
943
944 // Override symbol base with a special symbol.
945
946 void
947 Symbol::override_base_with_special(const Symbol* from)
948 {
949   bool same_name = this->name_ == from->name_;
950   gold_assert(same_name || this->has_alias());
951
952   // If we are overriding an undef, remember the original binding.
953   if (this->is_undefined())
954     this->set_undef_binding(this->binding_);
955
956   this->source_ = from->source_;
957   switch (from->source_)
958     {
959     case FROM_OBJECT:
960       this->u_.from_object = from->u_.from_object;
961       break;
962     case IN_OUTPUT_DATA:
963       this->u_.in_output_data = from->u_.in_output_data;
964       break;
965     case IN_OUTPUT_SEGMENT:
966       this->u_.in_output_segment = from->u_.in_output_segment;
967       break;
968     case IS_CONSTANT:
969     case IS_UNDEFINED:
970       break;
971     default:
972       gold_unreachable();
973       break;
974     }
975
976   if (same_name)
977     {
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
983       // script).
984       this->version_ = from->version_;
985     }
986   this->type_ = from->type_;
987   this->binding_ = from->binding_;
988   this->override_visibility(from->visibility_);
989   this->nonvis_ = from->nonvis_;
990
991   // Special symbols are always considered to be regular symbols.
992   this->in_reg_ = true;
993
994   if (from->needs_dynsym_entry_)
995     this->needs_dynsym_entry_ = true;
996   if (from->needs_dynsym_value_)
997     this->needs_dynsym_value_ = true;
998
999   this->is_predefined_ = from->is_predefined_;
1000
1001   // We shouldn't see these flags.  If we do, we need to handle them
1002   // somehow.
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_);
1008 }
1009
1010 // Override a symbol with a special symbol.
1011
1012 template<int size>
1013 void
1014 Sized_symbol<size>::override_with_special(const Sized_symbol<size>* from)
1015 {
1016   this->override_base_with_special(from);
1017   this->value_ = from->value_;
1018   this->symsize_ = from->symsize_;
1019 }
1020
1021 // Override TOSYM with the special symbol FROMSYM.  This handles all
1022 // aliases of TOSYM.
1023
1024 template<int size>
1025 void
1026 Symbol_table::override_with_special(Sized_symbol<size>* tosym,
1027                                     const Sized_symbol<size>* fromsym)
1028 {
1029   tosym->override_with_special(fromsym);
1030   if (tosym->has_alias())
1031     {
1032       Symbol* sym = this->weak_aliases_[tosym];
1033       gold_assert(sym != NULL);
1034       Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
1035       do
1036         {
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);
1041         }
1042       while (ssym != tosym);
1043     }
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);
1052 }
1053
1054 // Instantiate the templates we need.  We could use the configure
1055 // script to restrict this to only the ones needed for implemented
1056 // targets.
1057
1058 // We have to instantiate both big and little endian versions because
1059 // these are used by other templates that depends on size only.
1060
1061 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1062 template
1063 void
1064 Symbol_table::resolve<32, false>(
1065     Sized_symbol<32>* to,
1066     const elfcpp::Sym<32, false>& sym,
1067     unsigned int st_shndx,
1068     bool is_ordinary,
1069     unsigned int orig_st_shndx,
1070     Object* object,
1071     const char* version,
1072     bool is_default_version);
1073
1074 template
1075 void
1076 Symbol_table::resolve<32, true>(
1077     Sized_symbol<32>* to,
1078     const elfcpp::Sym<32, true>& sym,
1079     unsigned int st_shndx,
1080     bool is_ordinary,
1081     unsigned int orig_st_shndx,
1082     Object* object,
1083     const char* version,
1084     bool is_default_version);
1085 #endif
1086
1087 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1088 template
1089 void
1090 Symbol_table::resolve<64, false>(
1091     Sized_symbol<64>* to,
1092     const elfcpp::Sym<64, false>& sym,
1093     unsigned int st_shndx,
1094     bool is_ordinary,
1095     unsigned int orig_st_shndx,
1096     Object* object,
1097     const char* version,
1098     bool is_default_version);
1099
1100 template
1101 void
1102 Symbol_table::resolve<64, true>(
1103     Sized_symbol<64>* to,
1104     const elfcpp::Sym<64, true>& sym,
1105     unsigned int st_shndx,
1106     bool is_ordinary,
1107     unsigned int orig_st_shndx,
1108     Object* object,
1109     const char* version,
1110     bool is_default_version);
1111 #endif
1112
1113 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1114 template
1115 void
1116 Symbol_table::override_with_special<32>(Sized_symbol<32>*,
1117                                         const Sized_symbol<32>*);
1118 #endif
1119
1120 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1121 template
1122 void
1123 Symbol_table::override_with_special<64>(Sized_symbol<64>*,
1124                                         const Sized_symbol<64>*);
1125 #endif
1126
1127 } // End namespace gold.