1 // resolve.cc -- symbol resolution for gold
2 
3 // Copyright (C) 2006-2016 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     this->type_ = sym.get_st_type();
102   this->binding_ = sym.get_st_bind();
103   this->override_visibility(sym.get_st_visibility());
104   this->nonvis_ = sym.get_st_nonvis();
105   if (object->is_dynamic())
106     this->in_dyn_ = true;
107   else
108     this->in_reg_ = true;
109 }
110 
111 // Override the fields in Sized_symbol.
112 
113 template<int size>
114 template<bool big_endian>
115 void
116 Sized_symbol<size>::override(const elfcpp::Sym<size, big_endian>& sym,
117 			     unsigned st_shndx, bool is_ordinary,
118 			     Object* object, const char* version)
119 {
120   this->override_base(sym, st_shndx, is_ordinary, object, version);
121   this->value_ = sym.get_st_value();
122   this->symsize_ = sym.get_st_size();
123 }
124 
125 // Override TOSYM with symbol FROMSYM, defined in OBJECT, with version
126 // VERSION.  This handles all aliases of TOSYM.
127 
128 template<int size, bool big_endian>
129 void
130 Symbol_table::override(Sized_symbol<size>* tosym,
131 		       const elfcpp::Sym<size, big_endian>& fromsym,
132 		       unsigned int st_shndx, bool is_ordinary,
133 		       Object* object, const char* version)
134 {
135   tosym->override(fromsym, st_shndx, is_ordinary, object, version);
136   if (tosym->has_alias())
137     {
138       Symbol* sym = this->weak_aliases_[tosym];
139       gold_assert(sym != NULL);
140       Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
141       do
142 	{
143 	  ssym->override(fromsym, st_shndx, is_ordinary, object, version);
144 	  sym = this->weak_aliases_[ssym];
145 	  gold_assert(sym != NULL);
146 	  ssym = this->get_sized_symbol<size>(sym);
147 	}
148       while (ssym != tosym);
149     }
150 }
151 
152 // The resolve functions build a little code for each symbol.
153 // Bit 0: 0 for global, 1 for weak.
154 // Bit 1: 0 for regular object, 1 for shared object
155 // Bits 2-3: 0 for normal, 1 for undefined, 2 for common
156 // This gives us values from 0 to 11.
157 
158 static const int global_or_weak_shift = 0;
159 static const unsigned int global_flag = 0 << global_or_weak_shift;
160 static const unsigned int weak_flag = 1 << global_or_weak_shift;
161 
162 static const int regular_or_dynamic_shift = 1;
163 static const unsigned int regular_flag = 0 << regular_or_dynamic_shift;
164 static const unsigned int dynamic_flag = 1 << regular_or_dynamic_shift;
165 
166 static const int def_undef_or_common_shift = 2;
167 static const unsigned int def_flag = 0 << def_undef_or_common_shift;
168 static const unsigned int undef_flag = 1 << def_undef_or_common_shift;
169 static const unsigned int common_flag = 2 << def_undef_or_common_shift;
170 
171 // This convenience function combines all the flags based on facts
172 // about the symbol.
173 
174 static unsigned int
175 symbol_to_bits(elfcpp::STB binding, bool is_dynamic,
176 	       unsigned int shndx, bool is_ordinary)
177 {
178   unsigned int bits;
179 
180   switch (binding)
181     {
182     case elfcpp::STB_GLOBAL:
183     case elfcpp::STB_GNU_UNIQUE:
184       bits = global_flag;
185       break;
186 
187     case elfcpp::STB_WEAK:
188       bits = weak_flag;
189       break;
190 
191     case elfcpp::STB_LOCAL:
192       // We should only see externally visible symbols in the symbol
193       // table.
194       gold_error(_("invalid STB_LOCAL symbol in external symbols"));
195       bits = global_flag;
196 
197     default:
198       // Any target which wants to handle STB_LOOS, etc., needs to
199       // define a resolve method.
200       gold_error(_("unsupported symbol binding %d"), static_cast<int>(binding));
201       bits = global_flag;
202     }
203 
204   if (is_dynamic)
205     bits |= dynamic_flag;
206   else
207     bits |= regular_flag;
208 
209   switch (shndx)
210     {
211     case elfcpp::SHN_UNDEF:
212       bits |= undef_flag;
213       break;
214 
215     case elfcpp::SHN_COMMON:
216       if (!is_ordinary)
217 	bits |= common_flag;
218       break;
219 
220     default:
221       if (!is_ordinary && Symbol::is_common_shndx(shndx))
222 	bits |= common_flag;
223       else
224         bits |= def_flag;
225       break;
226     }
227 
228   return bits;
229 }
230 
231 // Resolve a symbol.  This is called the second and subsequent times
232 // we see a symbol.  TO is the pre-existing symbol.  ST_SHNDX is the
233 // section index for SYM, possibly adjusted for many sections.
234 // IS_ORDINARY is whether ST_SHNDX is a normal section index rather
235 // than a special code.  ORIG_ST_SHNDX is the original section index,
236 // before any munging because of discarded sections, except that all
237 // non-ordinary section indexes are mapped to SHN_UNDEF.  VERSION is
238 // the version of SYM.
239 
240 template<int size, bool big_endian>
241 void
242 Symbol_table::resolve(Sized_symbol<size>* to,
243 		      const elfcpp::Sym<size, big_endian>& sym,
244 		      unsigned int st_shndx, bool is_ordinary,
245 		      unsigned int orig_st_shndx,
246 		      Object* object, const char* version,
247 		      bool is_default_version)
248 {
249   // It's possible for a symbol to be defined in an object file
250   // using .symver to give it a version, and for there to also be
251   // a linker script giving that symbol the same version.  We
252   // don't want to give a multiple-definition error for this
253   // harmless redefinition.
254   bool to_is_ordinary;
255   if (to->source() == Symbol::FROM_OBJECT
256       && to->object() == object
257       && is_ordinary
258       && to->is_defined()
259       && to->shndx(&to_is_ordinary) == st_shndx
260       && to_is_ordinary
261       && to->value() == sym.get_st_value())
262     return;
263 
264   if (parameters->target().has_resolve())
265     {
266       Sized_target<size, big_endian>* sized_target;
267       sized_target = parameters->sized_target<size, big_endian>();
268       sized_target->resolve(to, sym, object, version);
269       return;
270     }
271 
272   if (!object->is_dynamic())
273     {
274       if (sym.get_st_type() == elfcpp::STT_COMMON
275 	  && (is_ordinary || !Symbol::is_common_shndx(st_shndx)))
276 	{
277 	  gold_warning(_("STT_COMMON symbol '%s' in %s "
278 			 "is not in a common section"),
279 		       to->demangled_name().c_str(),
280 		       to->object()->name().c_str());
281 	  return;
282 	}
283       // Record that we've seen this symbol in a regular object.
284       to->set_in_reg();
285     }
286   else if (st_shndx == elfcpp::SHN_UNDEF
287            && (to->visibility() == elfcpp::STV_HIDDEN
288                || to->visibility() == elfcpp::STV_INTERNAL))
289     {
290       // The symbol is hidden, so a reference from a shared object
291       // cannot bind to it.  We tried issuing a warning in this case,
292       // but that produces false positives when the symbol is
293       // actually resolved in a different shared object (PR 15574).
294       return;
295     }
296   else
297     {
298       // Record that we've seen this symbol in a dynamic object.
299       to->set_in_dyn();
300     }
301 
302   // Record if we've seen this symbol in a real ELF object (i.e., the
303   // symbol is referenced from outside the world known to the plugin).
304   if (object->pluginobj() == NULL && !object->is_dynamic())
305     to->set_in_real_elf();
306 
307   // If we're processing replacement files, allow new symbols to override
308   // the placeholders from the plugin objects.
309   // Treat common symbols specially since it is possible that an ELF
310   // file increased the size of the alignment.
311   if (to->source() == Symbol::FROM_OBJECT)
312     {
313       Pluginobj* obj = to->object()->pluginobj();
314       if (obj != NULL
315           && parameters->options().plugins()->in_replacement_phase())
316         {
317 	  bool adjust_common = false;
318 	  typename Sized_symbol<size>::Size_type tosize = 0;
319 	  typename Sized_symbol<size>::Value_type tovalue = 0;
320 	  if (to->is_common()
321 	      && !is_ordinary && Symbol::is_common_shndx(st_shndx))
322 	    {
323 	      adjust_common = true;
324 	      tosize = to->symsize();
325 	      tovalue = to->value();
326 	    }
327 	  this->override(to, sym, st_shndx, is_ordinary, object, version);
328 	  if (adjust_common)
329 	    {
330 	      if (tosize > to->symsize())
331 		to->set_symsize(tosize);
332 	      if (tovalue > to->value())
333 		to->set_value(tovalue);
334 	    }
335 	  return;
336         }
337     }
338 
339   // A new weak undefined reference, merging with an old weak
340   // reference, could be a One Definition Rule (ODR) violation --
341   // especially if the types or sizes of the references differ.  We'll
342   // store such pairs and look them up later to make sure they
343   // actually refer to the same lines of code.  We also check
344   // combinations of weak and strong, which might occur if one case is
345   // inline and the other is not.  (Note: not all ODR violations can
346   // be found this way, and not everything this finds is an ODR
347   // violation.  But it's helpful to warn about.)
348   if (parameters->options().detect_odr_violations()
349       && (sym.get_st_bind() == elfcpp::STB_WEAK
350 	  || to->binding() == elfcpp::STB_WEAK)
351       && orig_st_shndx != elfcpp::SHN_UNDEF
352       && to->shndx(&to_is_ordinary) != elfcpp::SHN_UNDEF
353       && to_is_ordinary
354       && sym.get_st_size() != 0    // Ignore weird 0-sized symbols.
355       && to->symsize() != 0
356       && (sym.get_st_type() != to->type()
357           || sym.get_st_size() != to->symsize())
358       // C does not have a concept of ODR, so we only need to do this
359       // on C++ symbols.  These have (mangled) names starting with _Z.
360       && to->name()[0] == '_' && to->name()[1] == 'Z')
361     {
362       Symbol_location fromloc
363           = { object, orig_st_shndx, static_cast<off_t>(sym.get_st_value()) };
364       Symbol_location toloc = { to->object(), to->shndx(&to_is_ordinary),
365 				static_cast<off_t>(to->value()) };
366       this->candidate_odr_violations_[to->name()].insert(fromloc);
367       this->candidate_odr_violations_[to->name()].insert(toloc);
368     }
369 
370   // Plugins don't provide a symbol type, so adopt the existing type
371   // if the FROM symbol is from a plugin.
372   elfcpp::STT fromtype = (object->pluginobj() != NULL
373 			  ? to->type()
374 			  : sym.get_st_type());
375   unsigned int frombits = symbol_to_bits(sym.get_st_bind(),
376                                          object->is_dynamic(),
377 					 st_shndx, is_ordinary);
378 
379   bool adjust_common_sizes;
380   bool adjust_dyndef;
381   typename Sized_symbol<size>::Size_type tosize = to->symsize();
382   if (Symbol_table::should_override(to, frombits, fromtype, OBJECT,
383 				    object, &adjust_common_sizes,
384 				    &adjust_dyndef, is_default_version))
385     {
386       elfcpp::STB tobinding = to->binding();
387       typename Sized_symbol<size>::Value_type tovalue = to->value();
388       this->override(to, sym, st_shndx, is_ordinary, object, version);
389       if (adjust_common_sizes)
390 	{
391 	  if (tosize > to->symsize())
392 	    to->set_symsize(tosize);
393 	  if (tovalue > to->value())
394 	    to->set_value(tovalue);
395 	}
396       if (adjust_dyndef)
397 	{
398 	  // We are overriding an UNDEF or WEAK UNDEF with a DYN DEF.
399 	  // Remember which kind of UNDEF it was for future reference.
400 	  to->set_undef_binding(tobinding);
401 	}
402     }
403   else
404     {
405       if (adjust_common_sizes)
406 	{
407 	  if (sym.get_st_size() > tosize)
408 	    to->set_symsize(sym.get_st_size());
409 	  if (sym.get_st_value() > to->value())
410 	    to->set_value(sym.get_st_value());
411 	}
412       if (adjust_dyndef)
413 	{
414 	  // We are keeping a DYN DEF after seeing an UNDEF or WEAK UNDEF.
415 	  // Remember which kind of UNDEF it was.
416 	  to->set_undef_binding(sym.get_st_bind());
417 	}
418       // The ELF ABI says that even for a reference to a symbol we
419       // merge the visibility.
420       to->override_visibility(sym.get_st_visibility());
421     }
422 
423   if (adjust_common_sizes && parameters->options().warn_common())
424     {
425       if (tosize > sym.get_st_size())
426 	Symbol_table::report_resolve_problem(false,
427 					     _("common of '%s' overriding "
428 					       "smaller common"),
429 					     to, OBJECT, object);
430       else if (tosize < sym.get_st_size())
431 	Symbol_table::report_resolve_problem(false,
432 					     _("common of '%s' overidden by "
433 					       "larger common"),
434 					     to, OBJECT, object);
435       else
436 	Symbol_table::report_resolve_problem(false,
437 					     _("multiple common of '%s'"),
438 					     to, OBJECT, object);
439     }
440 }
441 
442 // Handle the core of symbol resolution.  This is called with the
443 // existing symbol, TO, and a bitflag describing the new symbol.  This
444 // returns true if we should override the existing symbol with the new
445 // one, and returns false otherwise.  It sets *ADJUST_COMMON_SIZES to
446 // true if we should set the symbol size to the maximum of the TO and
447 // FROM sizes.  It handles error conditions.
448 
449 bool
450 Symbol_table::should_override(const Symbol* to, unsigned int frombits,
451 			      elfcpp::STT fromtype, Defined defined,
452 			      Object* object, bool* adjust_common_sizes,
453 			      bool* adjust_dyndef, bool is_default_version)
454 {
455   *adjust_common_sizes = false;
456   *adjust_dyndef = false;
457 
458   unsigned int tobits;
459   if (to->source() == Symbol::IS_UNDEFINED)
460     tobits = symbol_to_bits(to->binding(), false, elfcpp::SHN_UNDEF, true);
461   else if (to->source() != Symbol::FROM_OBJECT)
462     tobits = symbol_to_bits(to->binding(), false, elfcpp::SHN_ABS, false);
463   else
464     {
465       bool is_ordinary;
466       unsigned int shndx = to->shndx(&is_ordinary);
467       tobits = symbol_to_bits(to->binding(),
468 			      to->object()->is_dynamic(),
469 			      shndx,
470 			      is_ordinary);
471     }
472 
473   if ((to->type() == elfcpp::STT_TLS) ^ (fromtype == elfcpp::STT_TLS)
474       && !to->is_placeholder())
475     Symbol_table::report_resolve_problem(true,
476 					 _("symbol '%s' used as both __thread "
477 					   "and non-__thread"),
478 					 to, defined, object);
479 
480   // We use a giant switch table for symbol resolution.  This code is
481   // unwieldy, but: 1) it is efficient; 2) we definitely handle all
482   // cases; 3) it is easy to change the handling of a particular case.
483   // The alternative would be a series of conditionals, but it is easy
484   // to get the ordering wrong.  This could also be done as a table,
485   // but that is no easier to understand than this large switch
486   // statement.
487 
488   // These are the values generated by the bit codes.
489   enum
490   {
491     DEF =              global_flag | regular_flag | def_flag,
492     WEAK_DEF =         weak_flag   | regular_flag | def_flag,
493     DYN_DEF =          global_flag | dynamic_flag | def_flag,
494     DYN_WEAK_DEF =     weak_flag   | dynamic_flag | def_flag,
495     UNDEF =            global_flag | regular_flag | undef_flag,
496     WEAK_UNDEF =       weak_flag   | regular_flag | undef_flag,
497     DYN_UNDEF =        global_flag | dynamic_flag | undef_flag,
498     DYN_WEAK_UNDEF =   weak_flag   | dynamic_flag | undef_flag,
499     COMMON =           global_flag | regular_flag | common_flag,
500     WEAK_COMMON =      weak_flag   | regular_flag | common_flag,
501     DYN_COMMON =       global_flag | dynamic_flag | common_flag,
502     DYN_WEAK_COMMON =  weak_flag   | dynamic_flag | common_flag
503   };
504 
505   switch (tobits * 16 + frombits)
506     {
507     case DEF * 16 + DEF:
508       // Two definitions of the same symbol.
509 
510       // If either symbol is defined by an object included using
511       // --just-symbols, then don't warn.  This is for compatibility
512       // with the GNU linker.  FIXME: This is a hack.
513       if ((to->source() == Symbol::FROM_OBJECT && to->object()->just_symbols())
514           || (object != NULL && object->just_symbols()))
515         return false;
516 
517       if (!parameters->options().muldefs())
518 	Symbol_table::report_resolve_problem(true,
519 					     _("multiple definition of '%s'"),
520 					     to, defined, object);
521       return false;
522 
523     case WEAK_DEF * 16 + DEF:
524       // We've seen a weak definition, and now we see a strong
525       // definition.  In the original SVR4 linker, this was treated as
526       // a multiple definition error.  In the Solaris linker and the
527       // GNU linker, a weak definition followed by a regular
528       // definition causes the weak definition to be overridden.  We
529       // are currently compatible with the GNU linker.  In the future
530       // we should add a target specific option to change this.
531       // FIXME.
532       return true;
533 
534     case DYN_DEF * 16 + DEF:
535     case DYN_WEAK_DEF * 16 + DEF:
536       // We've seen a definition in a dynamic object, and now we see a
537       // definition in a regular object.  The definition in the
538       // regular object overrides the definition in the dynamic
539       // object.
540       return true;
541 
542     case UNDEF * 16 + DEF:
543     case WEAK_UNDEF * 16 + DEF:
544     case DYN_UNDEF * 16 + DEF:
545     case DYN_WEAK_UNDEF * 16 + DEF:
546       // We've seen an undefined reference, and now we see a
547       // definition.  We use the definition.
548       return true;
549 
550     case COMMON * 16 + DEF:
551     case WEAK_COMMON * 16 + DEF:
552     case DYN_COMMON * 16 + DEF:
553     case DYN_WEAK_COMMON * 16 + DEF:
554       // We've seen a common symbol and now we see a definition.  The
555       // definition overrides.
556       if (parameters->options().warn_common())
557 	Symbol_table::report_resolve_problem(false,
558 					     _("definition of '%s' overriding "
559 					       "common"),
560 					     to, defined, object);
561       return true;
562 
563     case DEF * 16 + WEAK_DEF:
564     case WEAK_DEF * 16 + WEAK_DEF:
565       // We've seen a definition and now we see a weak definition.  We
566       // ignore the new weak definition.
567       return false;
568 
569     case DYN_DEF * 16 + WEAK_DEF:
570     case DYN_WEAK_DEF * 16 + WEAK_DEF:
571       // We've seen a dynamic definition and now we see a regular weak
572       // definition.  The regular weak definition overrides.
573       return true;
574 
575     case UNDEF * 16 + WEAK_DEF:
576     case WEAK_UNDEF * 16 + WEAK_DEF:
577     case DYN_UNDEF * 16 + WEAK_DEF:
578     case DYN_WEAK_UNDEF * 16 + WEAK_DEF:
579       // A weak definition of a currently undefined symbol.
580       return true;
581 
582     case COMMON * 16 + WEAK_DEF:
583     case WEAK_COMMON * 16 + WEAK_DEF:
584       // A weak definition does not override a common definition.
585       return false;
586 
587     case DYN_COMMON * 16 + WEAK_DEF:
588     case DYN_WEAK_COMMON * 16 + WEAK_DEF:
589       // A weak definition does override a definition in a dynamic
590       // object.
591       if (parameters->options().warn_common())
592 	Symbol_table::report_resolve_problem(false,
593 					     _("definition of '%s' overriding "
594 					       "dynamic common definition"),
595 					     to, defined, object);
596       return true;
597 
598     case DEF * 16 + DYN_DEF:
599     case WEAK_DEF * 16 + DYN_DEF:
600       // Ignore a dynamic definition if we already have a definition.
601       return false;
602 
603     case DYN_DEF * 16 + DYN_DEF:
604     case DYN_WEAK_DEF * 16 + DYN_DEF:
605       // Ignore a dynamic definition if we already have a definition,
606       // unless the existing definition is an unversioned definition
607       // in the same dynamic object, and the new definition is a
608       // default version.
609       if (to->object() == object
610           && to->version() == NULL
611           && is_default_version)
612         return true;
613       return false;
614 
615     case UNDEF * 16 + DYN_DEF:
616     case DYN_UNDEF * 16 + DYN_DEF:
617     case DYN_WEAK_UNDEF * 16 + DYN_DEF:
618       // Use a dynamic definition if we have a reference.
619       return true;
620 
621     case WEAK_UNDEF * 16 + DYN_DEF:
622       // When overriding a weak undef by a dynamic definition,
623       // we need to remember that the original undef was weak.
624       *adjust_dyndef = true;
625       return true;
626 
627     case COMMON * 16 + DYN_DEF:
628     case WEAK_COMMON * 16 + DYN_DEF:
629     case DYN_COMMON * 16 + DYN_DEF:
630     case DYN_WEAK_COMMON * 16 + DYN_DEF:
631       // Ignore a dynamic definition if we already have a common
632       // definition.
633       return false;
634 
635     case DEF * 16 + DYN_WEAK_DEF:
636     case WEAK_DEF * 16 + DYN_WEAK_DEF:
637     case DYN_DEF * 16 + DYN_WEAK_DEF:
638     case DYN_WEAK_DEF * 16 + DYN_WEAK_DEF:
639       // Ignore a weak dynamic definition if we already have a
640       // definition.
641       return false;
642 
643     case UNDEF * 16 + DYN_WEAK_DEF:
644       // When overriding an undef by a dynamic weak definition,
645       // we need to remember that the original undef was not weak.
646       *adjust_dyndef = true;
647       return true;
648 
649     case DYN_UNDEF * 16 + DYN_WEAK_DEF:
650     case DYN_WEAK_UNDEF * 16 + DYN_WEAK_DEF:
651       // Use a weak dynamic definition if we have a reference.
652       return true;
653 
654     case WEAK_UNDEF * 16 + DYN_WEAK_DEF:
655       // When overriding a weak undef by a dynamic definition,
656       // we need to remember that the original undef was weak.
657       *adjust_dyndef = true;
658       return true;
659 
660     case COMMON * 16 + DYN_WEAK_DEF:
661     case WEAK_COMMON * 16 + DYN_WEAK_DEF:
662     case DYN_COMMON * 16 + DYN_WEAK_DEF:
663     case DYN_WEAK_COMMON * 16 + DYN_WEAK_DEF:
664       // Ignore a weak dynamic definition if we already have a common
665       // definition.
666       return false;
667 
668     case DEF * 16 + UNDEF:
669     case WEAK_DEF * 16 + UNDEF:
670     case UNDEF * 16 + UNDEF:
671       // A new undefined reference tells us nothing.
672       return false;
673 
674     case DYN_DEF * 16 + UNDEF:
675     case DYN_WEAK_DEF * 16 + UNDEF:
676       // For a dynamic def, we need to remember which kind of undef we see.
677       *adjust_dyndef = true;
678       return false;
679 
680     case WEAK_UNDEF * 16 + UNDEF:
681     case DYN_UNDEF * 16 + UNDEF:
682     case DYN_WEAK_UNDEF * 16 + UNDEF:
683       // A strong undef overrides a dynamic or weak undef.
684       return true;
685 
686     case COMMON * 16 + UNDEF:
687     case WEAK_COMMON * 16 + UNDEF:
688     case DYN_COMMON * 16 + UNDEF:
689     case DYN_WEAK_COMMON * 16 + UNDEF:
690       // A new undefined reference tells us nothing.
691       return false;
692 
693     case DEF * 16 + WEAK_UNDEF:
694     case WEAK_DEF * 16 + WEAK_UNDEF:
695     case UNDEF * 16 + WEAK_UNDEF:
696     case WEAK_UNDEF * 16 + WEAK_UNDEF:
697     case DYN_UNDEF * 16 + WEAK_UNDEF:
698     case COMMON * 16 + WEAK_UNDEF:
699     case WEAK_COMMON * 16 + WEAK_UNDEF:
700     case DYN_COMMON * 16 + WEAK_UNDEF:
701     case DYN_WEAK_COMMON * 16 + WEAK_UNDEF:
702       // A new weak undefined reference tells us nothing unless the
703       // exisiting symbol is a dynamic weak reference.
704       return false;
705 
706     case DYN_WEAK_UNDEF * 16 + WEAK_UNDEF:
707       // A new weak reference overrides an existing dynamic weak reference.
708       // This is necessary because a dynamic weak reference remembers
709       // the old binding, which may not be weak.  If we keeps the existing
710       // dynamic weak reference, the weakness may be dropped in the output.
711       return true;
712 
713     case DYN_DEF * 16 + WEAK_UNDEF:
714     case DYN_WEAK_DEF * 16 + WEAK_UNDEF:
715       // For a dynamic def, we need to remember which kind of undef we see.
716       *adjust_dyndef = true;
717       return false;
718 
719     case DEF * 16 + DYN_UNDEF:
720     case WEAK_DEF * 16 + DYN_UNDEF:
721     case DYN_DEF * 16 + DYN_UNDEF:
722     case DYN_WEAK_DEF * 16 + DYN_UNDEF:
723     case UNDEF * 16 + DYN_UNDEF:
724     case WEAK_UNDEF * 16 + DYN_UNDEF:
725     case DYN_UNDEF * 16 + DYN_UNDEF:
726     case DYN_WEAK_UNDEF * 16 + DYN_UNDEF:
727     case COMMON * 16 + DYN_UNDEF:
728     case WEAK_COMMON * 16 + DYN_UNDEF:
729     case DYN_COMMON * 16 + DYN_UNDEF:
730     case DYN_WEAK_COMMON * 16 + DYN_UNDEF:
731       // A new dynamic undefined reference tells us nothing.
732       return false;
733 
734     case DEF * 16 + DYN_WEAK_UNDEF:
735     case WEAK_DEF * 16 + DYN_WEAK_UNDEF:
736     case DYN_DEF * 16 + DYN_WEAK_UNDEF:
737     case DYN_WEAK_DEF * 16 + DYN_WEAK_UNDEF:
738     case UNDEF * 16 + DYN_WEAK_UNDEF:
739     case WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
740     case DYN_UNDEF * 16 + DYN_WEAK_UNDEF:
741     case DYN_WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
742     case COMMON * 16 + DYN_WEAK_UNDEF:
743     case WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
744     case DYN_COMMON * 16 + DYN_WEAK_UNDEF:
745     case DYN_WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
746       // A new weak dynamic undefined reference tells us nothing.
747       return false;
748 
749     case DEF * 16 + COMMON:
750       // A common symbol does not override a definition.
751       if (parameters->options().warn_common())
752 	Symbol_table::report_resolve_problem(false,
753 					     _("common '%s' overridden by "
754 					       "previous definition"),
755 					     to, defined, object);
756       return false;
757 
758     case WEAK_DEF * 16 + COMMON:
759     case DYN_DEF * 16 + COMMON:
760     case DYN_WEAK_DEF * 16 + COMMON:
761       // A common symbol does override a weak definition or a dynamic
762       // definition.
763       return true;
764 
765     case UNDEF * 16 + COMMON:
766     case WEAK_UNDEF * 16 + COMMON:
767     case DYN_UNDEF * 16 + COMMON:
768     case DYN_WEAK_UNDEF * 16 + COMMON:
769       // A common symbol is a definition for a reference.
770       return true;
771 
772     case COMMON * 16 + COMMON:
773       // Set the size to the maximum.
774       *adjust_common_sizes = true;
775       return false;
776 
777     case WEAK_COMMON * 16 + COMMON:
778       // I'm not sure just what a weak common symbol means, but
779       // presumably it can be overridden by a regular common symbol.
780       return true;
781 
782     case DYN_COMMON * 16 + COMMON:
783     case DYN_WEAK_COMMON * 16 + COMMON:
784       // Use the real common symbol, but adjust the size if necessary.
785       *adjust_common_sizes = true;
786       return true;
787 
788     case DEF * 16 + WEAK_COMMON:
789     case WEAK_DEF * 16 + WEAK_COMMON:
790     case DYN_DEF * 16 + WEAK_COMMON:
791     case DYN_WEAK_DEF * 16 + WEAK_COMMON:
792       // Whatever a weak common symbol is, it won't override a
793       // definition.
794       return false;
795 
796     case UNDEF * 16 + WEAK_COMMON:
797     case WEAK_UNDEF * 16 + WEAK_COMMON:
798     case DYN_UNDEF * 16 + WEAK_COMMON:
799     case DYN_WEAK_UNDEF * 16 + WEAK_COMMON:
800       // A weak common symbol is better than an undefined symbol.
801       return true;
802 
803     case COMMON * 16 + WEAK_COMMON:
804     case WEAK_COMMON * 16 + WEAK_COMMON:
805     case DYN_COMMON * 16 + WEAK_COMMON:
806     case DYN_WEAK_COMMON * 16 + WEAK_COMMON:
807       // Ignore a weak common symbol in the presence of a real common
808       // symbol.
809       return false;
810 
811     case DEF * 16 + DYN_COMMON:
812     case WEAK_DEF * 16 + DYN_COMMON:
813     case DYN_DEF * 16 + DYN_COMMON:
814     case DYN_WEAK_DEF * 16 + DYN_COMMON:
815       // Ignore a dynamic common symbol in the presence of a
816       // definition.
817       return false;
818 
819     case UNDEF * 16 + DYN_COMMON:
820     case WEAK_UNDEF * 16 + DYN_COMMON:
821     case DYN_UNDEF * 16 + DYN_COMMON:
822     case DYN_WEAK_UNDEF * 16 + DYN_COMMON:
823       // A dynamic common symbol is a definition of sorts.
824       return true;
825 
826     case COMMON * 16 + DYN_COMMON:
827     case WEAK_COMMON * 16 + DYN_COMMON:
828     case DYN_COMMON * 16 + DYN_COMMON:
829     case DYN_WEAK_COMMON * 16 + DYN_COMMON:
830       // Set the size to the maximum.
831       *adjust_common_sizes = true;
832       return false;
833 
834     case DEF * 16 + DYN_WEAK_COMMON:
835     case WEAK_DEF * 16 + DYN_WEAK_COMMON:
836     case DYN_DEF * 16 + DYN_WEAK_COMMON:
837     case DYN_WEAK_DEF * 16 + DYN_WEAK_COMMON:
838       // A common symbol is ignored in the face of a definition.
839       return false;
840 
841     case UNDEF * 16 + DYN_WEAK_COMMON:
842     case WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
843     case DYN_UNDEF * 16 + DYN_WEAK_COMMON:
844     case DYN_WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
845       // I guess a weak common symbol is better than a definition.
846       return true;
847 
848     case COMMON * 16 + DYN_WEAK_COMMON:
849     case WEAK_COMMON * 16 + DYN_WEAK_COMMON:
850     case DYN_COMMON * 16 + DYN_WEAK_COMMON:
851     case DYN_WEAK_COMMON * 16 + DYN_WEAK_COMMON:
852       // Set the size to the maximum.
853       *adjust_common_sizes = true;
854       return false;
855 
856     default:
857       gold_unreachable();
858     }
859 }
860 
861 // Issue an error or warning due to symbol resolution.  IS_ERROR
862 // indicates an error rather than a warning.  MSG is the error
863 // message; it is expected to have a %s for the symbol name.  TO is
864 // the existing symbol.  DEFINED/OBJECT is where the new symbol was
865 // found.
866 
867 // FIXME: We should have better location information here.  When the
868 // symbol is defined, we should be able to pull the location from the
869 // debug info if there is any.
870 
871 void
872 Symbol_table::report_resolve_problem(bool is_error, const char* msg,
873 				     const Symbol* to, Defined defined,
874 				     Object* object)
875 {
876   std::string demangled(to->demangled_name());
877   size_t len = strlen(msg) + demangled.length() + 10;
878   char* buf = new char[len];
879   snprintf(buf, len, msg, demangled.c_str());
880 
881   const char* objname;
882   switch (defined)
883     {
884     case OBJECT:
885       objname = object->name().c_str();
886       break;
887     case COPY:
888       objname = _("COPY reloc");
889       break;
890     case DEFSYM:
891     case UNDEFINED:
892       objname = _("command line");
893       break;
894     case SCRIPT:
895       objname = _("linker script");
896       break;
897     case PREDEFINED:
898     case INCREMENTAL_BASE:
899       objname = _("linker defined");
900       break;
901     default:
902       gold_unreachable();
903     }
904 
905   if (is_error)
906     gold_error("%s: %s", objname, buf);
907   else
908     gold_warning("%s: %s", objname, buf);
909 
910   delete[] buf;
911 
912   if (to->source() == Symbol::FROM_OBJECT)
913     objname = to->object()->name().c_str();
914   else
915     objname = _("command line");
916   gold_info("%s: %s: previous definition here", program_name, objname);
917 }
918 
919 // A special case of should_override which is only called for a strong
920 // defined symbol from a regular object file.  This is used when
921 // defining special symbols.
922 
923 bool
924 Symbol_table::should_override_with_special(const Symbol* to,
925 					   elfcpp::STT fromtype,
926 					   Defined defined)
927 {
928   bool adjust_common_sizes;
929   bool adjust_dyn_def;
930   unsigned int frombits = global_flag | regular_flag | def_flag;
931   bool ret = Symbol_table::should_override(to, frombits, fromtype, defined,
932 					   NULL, &adjust_common_sizes,
933 					   &adjust_dyn_def, false);
934   gold_assert(!adjust_common_sizes && !adjust_dyn_def);
935   return ret;
936 }
937 
938 // Override symbol base with a special symbol.
939 
940 void
941 Symbol::override_base_with_special(const Symbol* from)
942 {
943   bool same_name = this->name_ == from->name_;
944   gold_assert(same_name || this->has_alias());
945 
946   // If we are overriding an undef, remember the original binding.
947   if (this->is_undefined())
948     this->set_undef_binding(this->binding_);
949 
950   this->source_ = from->source_;
951   switch (from->source_)
952     {
953     case FROM_OBJECT:
954       this->u_.from_object = from->u_.from_object;
955       break;
956     case IN_OUTPUT_DATA:
957       this->u_.in_output_data = from->u_.in_output_data;
958       break;
959     case IN_OUTPUT_SEGMENT:
960       this->u_.in_output_segment = from->u_.in_output_segment;
961       break;
962     case IS_CONSTANT:
963     case IS_UNDEFINED:
964       break;
965     default:
966       gold_unreachable();
967       break;
968     }
969 
970   if (same_name)
971     {
972       // When overriding a versioned symbol with a special symbol, we
973       // may be changing the version.  This will happen if we see a
974       // special symbol such as "_end" defined in a shared object with
975       // one version (from a version script), but we want to define it
976       // here with a different version (from a different version
977       // script).
978       this->version_ = from->version_;
979     }
980   this->type_ = from->type_;
981   this->binding_ = from->binding_;
982   this->override_visibility(from->visibility_);
983   this->nonvis_ = from->nonvis_;
984 
985   // Special symbols are always considered to be regular symbols.
986   this->in_reg_ = true;
987 
988   if (from->needs_dynsym_entry_)
989     this->needs_dynsym_entry_ = true;
990   if (from->needs_dynsym_value_)
991     this->needs_dynsym_value_ = true;
992 
993   this->is_predefined_ = from->is_predefined_;
994 
995   // We shouldn't see these flags.  If we do, we need to handle them
996   // somehow.
997   gold_assert(!from->is_forwarder_);
998   gold_assert(!from->has_plt_offset());
999   gold_assert(!from->has_warning_);
1000   gold_assert(!from->is_copied_from_dynobj_);
1001   gold_assert(!from->is_forced_local_);
1002 }
1003 
1004 // Override a symbol with a special symbol.
1005 
1006 template<int size>
1007 void
1008 Sized_symbol<size>::override_with_special(const Sized_symbol<size>* from)
1009 {
1010   this->override_base_with_special(from);
1011   this->value_ = from->value_;
1012   this->symsize_ = from->symsize_;
1013 }
1014 
1015 // Override TOSYM with the special symbol FROMSYM.  This handles all
1016 // aliases of TOSYM.
1017 
1018 template<int size>
1019 void
1020 Symbol_table::override_with_special(Sized_symbol<size>* tosym,
1021 				    const Sized_symbol<size>* fromsym)
1022 {
1023   tosym->override_with_special(fromsym);
1024   if (tosym->has_alias())
1025     {
1026       Symbol* sym = this->weak_aliases_[tosym];
1027       gold_assert(sym != NULL);
1028       Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
1029       do
1030 	{
1031 	  ssym->override_with_special(fromsym);
1032 	  sym = this->weak_aliases_[ssym];
1033 	  gold_assert(sym != NULL);
1034 	  ssym = this->get_sized_symbol<size>(sym);
1035 	}
1036       while (ssym != tosym);
1037     }
1038   if (tosym->binding() == elfcpp::STB_LOCAL
1039       || ((tosym->visibility() == elfcpp::STV_HIDDEN
1040 	   || tosym->visibility() == elfcpp::STV_INTERNAL)
1041 	  && (tosym->binding() == elfcpp::STB_GLOBAL
1042 	      || tosym->binding() == elfcpp::STB_GNU_UNIQUE
1043 	      || tosym->binding() == elfcpp::STB_WEAK)
1044 	  && !parameters->options().relocatable()))
1045     this->force_local(tosym);
1046 }
1047 
1048 // Instantiate the templates we need.  We could use the configure
1049 // script to restrict this to only the ones needed for implemented
1050 // targets.
1051 
1052 // We have to instantiate both big and little endian versions because
1053 // these are used by other templates that depends on size only.
1054 
1055 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1056 template
1057 void
1058 Symbol_table::resolve<32, false>(
1059     Sized_symbol<32>* to,
1060     const elfcpp::Sym<32, false>& sym,
1061     unsigned int st_shndx,
1062     bool is_ordinary,
1063     unsigned int orig_st_shndx,
1064     Object* object,
1065     const char* version,
1066     bool is_default_version);
1067 
1068 template
1069 void
1070 Symbol_table::resolve<32, true>(
1071     Sized_symbol<32>* to,
1072     const elfcpp::Sym<32, true>& sym,
1073     unsigned int st_shndx,
1074     bool is_ordinary,
1075     unsigned int orig_st_shndx,
1076     Object* object,
1077     const char* version,
1078     bool is_default_version);
1079 #endif
1080 
1081 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1082 template
1083 void
1084 Symbol_table::resolve<64, false>(
1085     Sized_symbol<64>* to,
1086     const elfcpp::Sym<64, false>& sym,
1087     unsigned int st_shndx,
1088     bool is_ordinary,
1089     unsigned int orig_st_shndx,
1090     Object* object,
1091     const char* version,
1092     bool is_default_version);
1093 
1094 template
1095 void
1096 Symbol_table::resolve<64, true>(
1097     Sized_symbol<64>* to,
1098     const elfcpp::Sym<64, true>& sym,
1099     unsigned int st_shndx,
1100     bool is_ordinary,
1101     unsigned int orig_st_shndx,
1102     Object* object,
1103     const char* version,
1104     bool is_default_version);
1105 #endif
1106 
1107 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1108 template
1109 void
1110 Symbol_table::override_with_special<32>(Sized_symbol<32>*,
1111 					const Sized_symbol<32>*);
1112 #endif
1113 
1114 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1115 template
1116 void
1117 Symbol_table::override_with_special<64>(Sized_symbol<64>*,
1118 					const Sized_symbol<64>*);
1119 #endif
1120 
1121 } // End namespace gold.
1122