daily update
[binutils.git] / gold / resolve.cc
blob599aad46d13a3a84b438ce47d15fdf91c3e62c7f
1 // resolve.cc -- symbol resolution for gold
3 #include "gold.h"
5 #include "elfcpp.h"
6 #include "target.h"
7 #include "object.h"
8 #include "symtab.h"
10 namespace gold
13 // Symbol methods used in this file.
15 // Override the fields in Symbol.
17 template<int size, bool big_endian>
18 void
19 Symbol::override_base(const elfcpp::Sym<size, big_endian>& sym,
20 Object* object, const char* version)
22 gold_assert(this->source_ == FROM_OBJECT);
23 this->u_.from_object.object = object;
24 if (version != NULL && this->version() != version)
26 gold_assert(this->version() == NULL);
27 this->version_ = version;
29 // FIXME: Handle SHN_XINDEX.
30 this->u_.from_object.shndx = sym.get_st_shndx();
31 this->type_ = sym.get_st_type();
32 this->binding_ = sym.get_st_bind();
33 this->visibility_ = sym.get_st_visibility();
34 this->nonvis_ = sym.get_st_nonvis();
35 if (object->is_dynamic())
36 this->in_dyn_ = true;
37 else
38 this->in_reg_ = true;
41 // Override the fields in Sized_symbol.
43 template<int size>
44 template<bool big_endian>
45 void
46 Sized_symbol<size>::override(const elfcpp::Sym<size, big_endian>& sym,
47 Object* object, const char* version)
49 this->override_base(sym, object, version);
50 this->value_ = sym.get_st_value();
51 this->symsize_ = sym.get_st_size();
54 // The resolve functions build a little code for each symbol.
55 // Bit 0: 0 for global, 1 for weak.
56 // Bit 1: 0 for regular object, 1 for shared object
57 // Bits 2-3: 0 for normal, 1 for undefined, 2 for common
58 // This gives us values from 0 to 11.
60 static const int global_or_weak_shift = 0;
61 static const unsigned int global_flag = 0 << global_or_weak_shift;
62 static const unsigned int weak_flag = 1 << global_or_weak_shift;
64 static const int regular_or_dynamic_shift = 1;
65 static const unsigned int regular_flag = 0 << regular_or_dynamic_shift;
66 static const unsigned int dynamic_flag = 1 << regular_or_dynamic_shift;
68 static const int def_undef_or_common_shift = 2;
69 static const unsigned int def_flag = 0 << def_undef_or_common_shift;
70 static const unsigned int undef_flag = 1 << def_undef_or_common_shift;
71 static const unsigned int common_flag = 2 << def_undef_or_common_shift;
73 // Resolve a symbol. This is called the second and subsequent times
74 // we see a symbol. TO is the pre-existing symbol. SYM is the new
75 // symbol, seen in OBJECT. VERSION of the version of SYM.
77 template<int size, bool big_endian>
78 void
79 Symbol_table::resolve(Sized_symbol<size>* to,
80 const elfcpp::Sym<size, big_endian>& sym,
81 Object* object, const char* version)
83 if (object->target()->has_resolve())
85 Sized_target<size, big_endian>* sized_target;
86 sized_target = object->sized_target
87 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
88 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
89 sized_target->resolve(to, sym, object, version);
90 return;
93 if (!object->is_dynamic())
95 // Record that we've seen this symbol in a regular object.
96 to->set_in_reg();
98 else
100 // Record that we've seen this symbol in a dynamic object.
101 to->set_in_dyn();
104 unsigned int frombits;
105 switch (sym.get_st_bind())
107 case elfcpp::STB_GLOBAL:
108 frombits = global_flag;
109 break;
111 case elfcpp::STB_WEAK:
112 frombits = weak_flag;
113 break;
115 case elfcpp::STB_LOCAL:
116 fprintf(stderr,
117 _("%s: %s: invalid STB_LOCAL symbol %s in external symbols\n"),
118 program_name, object->name().c_str(), to->name());
119 gold_exit(false);
121 default:
122 fprintf(stderr,
123 _("%s: %s: unsupported symbol binding %d for symbol %s\n"),
124 program_name, object->name().c_str(),
125 static_cast<int>(sym.get_st_bind()), to->name());
126 gold_exit(false);
129 if (!object->is_dynamic())
130 frombits |= regular_flag;
131 else
132 frombits |= dynamic_flag;
134 switch (sym.get_st_shndx())
136 case elfcpp::SHN_UNDEF:
137 frombits |= undef_flag;
138 break;
140 case elfcpp::SHN_COMMON:
141 frombits |= common_flag;
142 break;
144 default:
145 if (sym.get_st_type() == elfcpp::STT_COMMON)
146 frombits |= common_flag;
147 else
148 frombits |= def_flag;
149 break;
152 bool adjust_common_sizes;
153 if (Symbol_table::should_override(to, frombits, &adjust_common_sizes))
155 typename Sized_symbol<size>::Size_type tosize = to->symsize();
157 to->override(sym, object, version);
159 if (adjust_common_sizes && tosize > to->symsize())
160 to->set_symsize(tosize);
162 else
164 if (adjust_common_sizes && sym.get_st_size() > to->symsize())
165 to->set_symsize(sym.get_st_size());
169 // Handle the core of symbol resolution. This is called with the
170 // existing symbol, TO, and a bitflag describing the new symbol. This
171 // returns true if we should override the existing symbol with the new
172 // one, and returns false otherwise. It sets *ADJUST_COMMON_SIZES to
173 // true if we should set the symbol size to the maximum of the TO and
174 // FROM sizes. It handles error conditions.
176 bool
177 Symbol_table::should_override(const Symbol* to, unsigned int frombits,
178 bool* adjust_common_sizes)
180 *adjust_common_sizes = false;
182 unsigned int tobits;
183 switch (to->binding())
185 case elfcpp::STB_GLOBAL:
186 tobits = global_flag;
187 break;
189 case elfcpp::STB_WEAK:
190 tobits = weak_flag;
191 break;
193 case elfcpp::STB_LOCAL:
194 // We should only see externally visible symbols in the symbol
195 // table.
196 gold_unreachable();
198 default:
199 // Any target which wants to handle STB_LOOS, etc., needs to
200 // define a resolve method.
201 gold_unreachable();
204 if (to->source() == Symbol::FROM_OBJECT
205 && to->object()->is_dynamic())
206 tobits |= dynamic_flag;
207 else
208 tobits |= regular_flag;
210 switch (to->shndx())
212 case elfcpp::SHN_UNDEF:
213 tobits |= undef_flag;
214 break;
216 case elfcpp::SHN_COMMON:
217 tobits |= common_flag;
218 break;
220 default:
221 if (to->type() == elfcpp::STT_COMMON)
222 tobits |= common_flag;
223 else
224 tobits |= def_flag;
225 break;
228 // FIXME: Warn if either but not both of TO and SYM are STT_TLS.
230 // We use a giant switch table for symbol resolution. This code is
231 // unwieldy, but: 1) it is efficient; 2) we definitely handle all
232 // cases; 3) it is easy to change the handling of a particular case.
233 // The alternative would be a series of conditionals, but it is easy
234 // to get the ordering wrong. This could also be done as a table,
235 // but that is no easier to understand than this large switch
236 // statement.
238 // These are the values generated by the bit codes.
239 enum
241 DEF = global_flag | regular_flag | def_flag,
242 WEAK_DEF = weak_flag | regular_flag | def_flag,
243 DYN_DEF = global_flag | dynamic_flag | def_flag,
244 DYN_WEAK_DEF = weak_flag | dynamic_flag | def_flag,
245 UNDEF = global_flag | regular_flag | undef_flag,
246 WEAK_UNDEF = weak_flag | regular_flag | undef_flag,
247 DYN_UNDEF = global_flag | dynamic_flag | undef_flag,
248 DYN_WEAK_UNDEF = weak_flag | dynamic_flag | undef_flag,
249 COMMON = global_flag | regular_flag | common_flag,
250 WEAK_COMMON = weak_flag | regular_flag | common_flag,
251 DYN_COMMON = global_flag | dynamic_flag | common_flag,
252 DYN_WEAK_COMMON = weak_flag | dynamic_flag | common_flag
255 switch (tobits * 16 + frombits)
257 case DEF * 16 + DEF:
258 // Two definitions of the same symbol.
259 fprintf(stderr, "%s: multiple definition of %s\n",
260 program_name, to->name());
261 // FIXME: Report locations. Record that we have seen an error.
262 return false;
264 case WEAK_DEF * 16 + DEF:
265 // We've seen a weak definition, and now we see a strong
266 // definition. In the original SVR4 linker, this was treated as
267 // a multiple definition error. In the Solaris linker and the
268 // GNU linker, a weak definition followed by a regular
269 // definition causes the weak definition to be overridden. We
270 // are currently compatible with the GNU linker. In the future
271 // we should add a target specific option to change this.
272 // FIXME.
273 return true;
275 case DYN_DEF * 16 + DEF:
276 case DYN_WEAK_DEF * 16 + DEF:
277 // We've seen a definition in a dynamic object, and now we see a
278 // definition in a regular object. The definition in the
279 // regular object overrides the definition in the dynamic
280 // object.
281 return true;
283 case UNDEF * 16 + DEF:
284 case WEAK_UNDEF * 16 + DEF:
285 case DYN_UNDEF * 16 + DEF:
286 case DYN_WEAK_UNDEF * 16 + DEF:
287 // We've seen an undefined reference, and now we see a
288 // definition. We use the definition.
289 return true;
291 case COMMON * 16 + DEF:
292 case WEAK_COMMON * 16 + DEF:
293 case DYN_COMMON * 16 + DEF:
294 case DYN_WEAK_COMMON * 16 + DEF:
295 // We've seen a common symbol and now we see a definition. The
296 // definition overrides. FIXME: We should optionally issue, version a
297 // warning.
298 return true;
300 case DEF * 16 + WEAK_DEF:
301 case WEAK_DEF * 16 + WEAK_DEF:
302 // We've seen a definition and now we see a weak definition. We
303 // ignore the new weak definition.
304 return false;
306 case DYN_DEF * 16 + WEAK_DEF:
307 case DYN_WEAK_DEF * 16 + WEAK_DEF:
308 // We've seen a dynamic definition and now we see a regular weak
309 // definition. The regular weak definition overrides.
310 return true;
312 case UNDEF * 16 + WEAK_DEF:
313 case WEAK_UNDEF * 16 + WEAK_DEF:
314 case DYN_UNDEF * 16 + WEAK_DEF:
315 case DYN_WEAK_UNDEF * 16 + WEAK_DEF:
316 // A weak definition of a currently undefined symbol.
317 return true;
319 case COMMON * 16 + WEAK_DEF:
320 case WEAK_COMMON * 16 + WEAK_DEF:
321 // A weak definition does not override a common definition.
322 return false;
324 case DYN_COMMON * 16 + WEAK_DEF:
325 case DYN_WEAK_COMMON * 16 + WEAK_DEF:
326 // A weak definition does override a definition in a dynamic
327 // object. FIXME: We should optionally issue a warning.
328 return true;
330 case DEF * 16 + DYN_DEF:
331 case WEAK_DEF * 16 + DYN_DEF:
332 case DYN_DEF * 16 + DYN_DEF:
333 case DYN_WEAK_DEF * 16 + DYN_DEF:
334 // Ignore a dynamic definition if we already have a definition.
335 return false;
337 case UNDEF * 16 + DYN_DEF:
338 case WEAK_UNDEF * 16 + DYN_DEF:
339 case DYN_UNDEF * 16 + DYN_DEF:
340 case DYN_WEAK_UNDEF * 16 + DYN_DEF:
341 // Use a dynamic definition if we have a reference.
342 return true;
344 case COMMON * 16 + DYN_DEF:
345 case WEAK_COMMON * 16 + DYN_DEF:
346 case DYN_COMMON * 16 + DYN_DEF:
347 case DYN_WEAK_COMMON * 16 + DYN_DEF:
348 // Ignore a dynamic definition if we already have a common
349 // definition.
350 return false;
352 case DEF * 16 + DYN_WEAK_DEF:
353 case WEAK_DEF * 16 + DYN_WEAK_DEF:
354 case DYN_DEF * 16 + DYN_WEAK_DEF:
355 case DYN_WEAK_DEF * 16 + DYN_WEAK_DEF:
356 // Ignore a weak dynamic definition if we already have a
357 // definition.
358 return false;
360 case UNDEF * 16 + DYN_WEAK_DEF:
361 case WEAK_UNDEF * 16 + DYN_WEAK_DEF:
362 case DYN_UNDEF * 16 + DYN_WEAK_DEF:
363 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_DEF:
364 // Use a weak dynamic definition if we have a reference.
365 return true;
367 case COMMON * 16 + DYN_WEAK_DEF:
368 case WEAK_COMMON * 16 + DYN_WEAK_DEF:
369 case DYN_COMMON * 16 + DYN_WEAK_DEF:
370 case DYN_WEAK_COMMON * 16 + DYN_WEAK_DEF:
371 // Ignore a weak dynamic definition if we already have a common
372 // definition.
373 return false;
375 case DEF * 16 + UNDEF:
376 case WEAK_DEF * 16 + UNDEF:
377 case DYN_DEF * 16 + UNDEF:
378 case DYN_WEAK_DEF * 16 + UNDEF:
379 case UNDEF * 16 + UNDEF:
380 // A new undefined reference tells us nothing.
381 return false;
383 case WEAK_UNDEF * 16 + UNDEF:
384 case DYN_UNDEF * 16 + UNDEF:
385 case DYN_WEAK_UNDEF * 16 + UNDEF:
386 // A strong undef overrides a dynamic or weak undef.
387 return true;
389 case COMMON * 16 + UNDEF:
390 case WEAK_COMMON * 16 + UNDEF:
391 case DYN_COMMON * 16 + UNDEF:
392 case DYN_WEAK_COMMON * 16 + UNDEF:
393 // A new undefined reference tells us nothing.
394 return false;
396 case DEF * 16 + WEAK_UNDEF:
397 case WEAK_DEF * 16 + WEAK_UNDEF:
398 case DYN_DEF * 16 + WEAK_UNDEF:
399 case DYN_WEAK_DEF * 16 + WEAK_UNDEF:
400 case UNDEF * 16 + WEAK_UNDEF:
401 case WEAK_UNDEF * 16 + WEAK_UNDEF:
402 case DYN_UNDEF * 16 + WEAK_UNDEF:
403 case DYN_WEAK_UNDEF * 16 + WEAK_UNDEF:
404 case COMMON * 16 + WEAK_UNDEF:
405 case WEAK_COMMON * 16 + WEAK_UNDEF:
406 case DYN_COMMON * 16 + WEAK_UNDEF:
407 case DYN_WEAK_COMMON * 16 + WEAK_UNDEF:
408 // A new weak undefined reference tells us nothing.
409 return false;
411 case DEF * 16 + DYN_UNDEF:
412 case WEAK_DEF * 16 + DYN_UNDEF:
413 case DYN_DEF * 16 + DYN_UNDEF:
414 case DYN_WEAK_DEF * 16 + DYN_UNDEF:
415 case UNDEF * 16 + DYN_UNDEF:
416 case WEAK_UNDEF * 16 + DYN_UNDEF:
417 case DYN_UNDEF * 16 + DYN_UNDEF:
418 case DYN_WEAK_UNDEF * 16 + DYN_UNDEF:
419 case COMMON * 16 + DYN_UNDEF:
420 case WEAK_COMMON * 16 + DYN_UNDEF:
421 case DYN_COMMON * 16 + DYN_UNDEF:
422 case DYN_WEAK_COMMON * 16 + DYN_UNDEF:
423 // A new dynamic undefined reference tells us nothing.
424 return false;
426 case DEF * 16 + DYN_WEAK_UNDEF:
427 case WEAK_DEF * 16 + DYN_WEAK_UNDEF:
428 case DYN_DEF * 16 + DYN_WEAK_UNDEF:
429 case DYN_WEAK_DEF * 16 + DYN_WEAK_UNDEF:
430 case UNDEF * 16 + DYN_WEAK_UNDEF:
431 case WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
432 case DYN_UNDEF * 16 + DYN_WEAK_UNDEF:
433 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
434 case COMMON * 16 + DYN_WEAK_UNDEF:
435 case WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
436 case DYN_COMMON * 16 + DYN_WEAK_UNDEF:
437 case DYN_WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
438 // A new weak dynamic undefined reference tells us nothing.
439 return false;
441 case DEF * 16 + COMMON:
442 // A common symbol does not override a definition.
443 return false;
445 case WEAK_DEF * 16 + COMMON:
446 case DYN_DEF * 16 + COMMON:
447 case DYN_WEAK_DEF * 16 + COMMON:
448 // A common symbol does override a weak definition or a dynamic
449 // definition.
450 return true;
452 case UNDEF * 16 + COMMON:
453 case WEAK_UNDEF * 16 + COMMON:
454 case DYN_UNDEF * 16 + COMMON:
455 case DYN_WEAK_UNDEF * 16 + COMMON:
456 // A common symbol is a definition for a reference.
457 return true;
459 case COMMON * 16 + COMMON:
460 // Set the size to the maximum.
461 *adjust_common_sizes = true;
462 return false;
464 case WEAK_COMMON * 16 + COMMON:
465 // I'm not sure just what a weak common symbol means, but
466 // presumably it can be overridden by a regular common symbol.
467 return true;
469 case DYN_COMMON * 16 + COMMON:
470 case DYN_WEAK_COMMON * 16 + COMMON:
471 // Use the real common symbol, but adjust the size if necessary.
472 *adjust_common_sizes = true;
473 return true;
475 case DEF * 16 + WEAK_COMMON:
476 case WEAK_DEF * 16 + WEAK_COMMON:
477 case DYN_DEF * 16 + WEAK_COMMON:
478 case DYN_WEAK_DEF * 16 + WEAK_COMMON:
479 // Whatever a weak common symbol is, it won't override a
480 // definition.
481 return false;
483 case UNDEF * 16 + WEAK_COMMON:
484 case WEAK_UNDEF * 16 + WEAK_COMMON:
485 case DYN_UNDEF * 16 + WEAK_COMMON:
486 case DYN_WEAK_UNDEF * 16 + WEAK_COMMON:
487 // A weak common symbol is better than an undefined symbol.
488 return true;
490 case COMMON * 16 + WEAK_COMMON:
491 case WEAK_COMMON * 16 + WEAK_COMMON:
492 case DYN_COMMON * 16 + WEAK_COMMON:
493 case DYN_WEAK_COMMON * 16 + WEAK_COMMON:
494 // Ignore a weak common symbol in the presence of a real common
495 // symbol.
496 return false;
498 case DEF * 16 + DYN_COMMON:
499 case WEAK_DEF * 16 + DYN_COMMON:
500 case DYN_DEF * 16 + DYN_COMMON:
501 case DYN_WEAK_DEF * 16 + DYN_COMMON:
502 // Ignore a dynamic common symbol in the presence of a
503 // definition.
504 return false;
506 case UNDEF * 16 + DYN_COMMON:
507 case WEAK_UNDEF * 16 + DYN_COMMON:
508 case DYN_UNDEF * 16 + DYN_COMMON:
509 case DYN_WEAK_UNDEF * 16 + DYN_COMMON:
510 // A dynamic common symbol is a definition of sorts.
511 return true;
513 case COMMON * 16 + DYN_COMMON:
514 case WEAK_COMMON * 16 + DYN_COMMON:
515 case DYN_COMMON * 16 + DYN_COMMON:
516 case DYN_WEAK_COMMON * 16 + DYN_COMMON:
517 // Set the size to the maximum.
518 *adjust_common_sizes = true;
519 return false;
521 case DEF * 16 + DYN_WEAK_COMMON:
522 case WEAK_DEF * 16 + DYN_WEAK_COMMON:
523 case DYN_DEF * 16 + DYN_WEAK_COMMON:
524 case DYN_WEAK_DEF * 16 + DYN_WEAK_COMMON:
525 // A common symbol is ignored in the face of a definition.
526 return false;
528 case UNDEF * 16 + DYN_WEAK_COMMON:
529 case WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
530 case DYN_UNDEF * 16 + DYN_WEAK_COMMON:
531 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
532 // I guess a weak common symbol is better than a definition.
533 return true;
535 case COMMON * 16 + DYN_WEAK_COMMON:
536 case WEAK_COMMON * 16 + DYN_WEAK_COMMON:
537 case DYN_COMMON * 16 + DYN_WEAK_COMMON:
538 case DYN_WEAK_COMMON * 16 + DYN_WEAK_COMMON:
539 // Set the size to the maximum.
540 *adjust_common_sizes = true;
541 return false;
543 default:
544 gold_unreachable();
548 // A special case of should_override which is only called for a strong
549 // defined symbol from a regular object file. This is used when
550 // defining special symbols.
552 bool
553 Symbol_table::should_override_with_special(const Symbol* to)
555 bool adjust_common_sizes;
556 unsigned int frombits = global_flag | regular_flag | def_flag;
557 bool ret = Symbol_table::should_override(to, frombits, &adjust_common_sizes);
558 gold_assert(!adjust_common_sizes);
559 return ret;
562 // Override symbol base with a special symbol.
564 void
565 Symbol::override_base_with_special(const Symbol* from)
567 this->source_ = from->source_;
568 switch (from->source_)
570 case FROM_OBJECT:
571 this->u_.from_object = from->u_.from_object;
572 break;
573 case IN_OUTPUT_DATA:
574 this->u_.in_output_data = from->u_.in_output_data;
575 break;
576 case IN_OUTPUT_SEGMENT:
577 this->u_.in_output_segment = from->u_.in_output_segment;
578 break;
579 case CONSTANT:
580 break;
581 default:
582 gold_unreachable();
583 break;
586 if (from->version_ != NULL && this->version_ != from->version_)
588 gold_assert(this->version_ == NULL);
589 this->version_ = from->version_;
592 this->type_ = from->type_;
593 this->binding_ = from->binding_;
594 this->visibility_ = from->visibility_;
595 this->nonvis_ = from->nonvis_;
597 // Special symbols are always considered to be regular symbols.
598 this->in_reg_ = true;
601 // Override a symbol with a special symbol.
603 template<int size>
604 void
605 Sized_symbol<size>::override_with_special(const Sized_symbol<size>* from)
607 this->override_base_with_special(from);
608 this->value_ = from->value_;
609 this->symsize_ = from->symsize_;
612 // Instantiate the templates we need. We could use the configure
613 // script to restrict this to only the ones needed for implemented
614 // targets.
616 #ifdef HAVE_TARGET_32_LITTLE
617 template
618 void
619 Symbol_table::resolve<32, false>(
620 Sized_symbol<32>* to,
621 const elfcpp::Sym<32, false>& sym,
622 Object* object,
623 const char* version);
624 #endif
626 #ifdef HAVE_TARGET_32_BIG
627 template
628 void
629 Symbol_table::resolve<32, true>(
630 Sized_symbol<32>* to,
631 const elfcpp::Sym<32, true>& sym,
632 Object* object,
633 const char* version);
634 #endif
636 #ifdef HAVE_TARGET_64_LITTLE
637 template
638 void
639 Symbol_table::resolve<64, false>(
640 Sized_symbol<64>* to,
641 const elfcpp::Sym<64, false>& sym,
642 Object* object,
643 const char* version);
644 #endif
646 #ifdef HAVE_TARGET_64_BIG
647 template
648 void
649 Symbol_table::resolve<64, true>(
650 Sized_symbol<64>* to,
651 const elfcpp::Sym<64, true>& sym,
652 Object* object,
653 const char* version);
654 #endif
656 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
657 template
658 void
659 Sized_symbol<32>::override_with_special(const Sized_symbol<32>*);
660 #endif
662 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
663 template
664 void
665 Sized_symbol<64>::override_with_special(const Sized_symbol<64>*);
666 #endif
668 } // End namespace gold.