1 // resolve.cc -- symbol resolution for gold
13 // Symbol methods used in this file.
15 // Override the fields in Symbol.
17 template<int size
, bool big_endian
>
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())
41 // Override the fields in Sized_symbol.
44 template<bool big_endian
>
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
>
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
);
93 if (!object
->is_dynamic())
95 // Record that we've seen this symbol in a regular object.
100 // Record that we've seen this symbol in a dynamic object.
104 unsigned int frombits
;
105 switch (sym
.get_st_bind())
107 case elfcpp::STB_GLOBAL
:
108 frombits
= global_flag
;
111 case elfcpp::STB_WEAK
:
112 frombits
= weak_flag
;
115 case elfcpp::STB_LOCAL
:
117 _("%s: %s: invalid STB_LOCAL symbol %s in external symbols\n"),
118 program_name
, object
->name().c_str(), to
->name());
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());
129 if (!object
->is_dynamic())
130 frombits
|= regular_flag
;
132 frombits
|= dynamic_flag
;
134 switch (sym
.get_st_shndx())
136 case elfcpp::SHN_UNDEF
:
137 frombits
|= undef_flag
;
140 case elfcpp::SHN_COMMON
:
141 frombits
|= common_flag
;
145 if (sym
.get_st_type() == elfcpp::STT_COMMON
)
146 frombits
|= common_flag
;
148 frombits
|= def_flag
;
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
);
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.
177 Symbol_table::should_override(const Symbol
* to
, unsigned int frombits
,
178 bool* adjust_common_sizes
)
180 *adjust_common_sizes
= false;
183 switch (to
->binding())
185 case elfcpp::STB_GLOBAL
:
186 tobits
= global_flag
;
189 case elfcpp::STB_WEAK
:
193 case elfcpp::STB_LOCAL
:
194 // We should only see externally visible symbols in the symbol
199 // Any target which wants to handle STB_LOOS, etc., needs to
200 // define a resolve method.
204 if (to
->source() == Symbol::FROM_OBJECT
205 && to
->object()->is_dynamic())
206 tobits
|= dynamic_flag
;
208 tobits
|= regular_flag
;
212 case elfcpp::SHN_UNDEF
:
213 tobits
|= undef_flag
;
216 case elfcpp::SHN_COMMON
:
217 tobits
|= common_flag
;
221 if (to
->type() == elfcpp::STT_COMMON
)
222 tobits
|= common_flag
;
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
238 // These are the values generated by the bit codes.
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
)
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.
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.
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
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.
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
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.
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.
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.
319 case COMMON
* 16 + WEAK_DEF
:
320 case WEAK_COMMON
* 16 + WEAK_DEF
:
321 // A weak definition does not override a common definition.
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.
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.
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.
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
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
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.
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
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.
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.
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.
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.
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.
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.
441 case DEF
* 16 + COMMON
:
442 // A common symbol does not override a definition.
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
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.
459 case COMMON
* 16 + COMMON
:
460 // Set the size to the maximum.
461 *adjust_common_sizes
= true;
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.
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;
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
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.
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
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
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.
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;
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.
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.
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;
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.
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
);
562 // Override symbol base with a special symbol.
565 Symbol::override_base_with_special(const Symbol
* from
)
567 this->source_
= from
->source_
;
568 switch (from
->source_
)
571 this->u_
.from_object
= from
->u_
.from_object
;
574 this->u_
.in_output_data
= from
->u_
.in_output_data
;
576 case IN_OUTPUT_SEGMENT
:
577 this->u_
.in_output_segment
= from
->u_
.in_output_segment
;
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.
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
616 #ifdef HAVE_TARGET_32_LITTLE
619 Symbol_table::resolve
<32, false>(
620 Sized_symbol
<32>* to
,
621 const elfcpp::Sym
<32, false>& sym
,
623 const char* version
);
626 #ifdef HAVE_TARGET_32_BIG
629 Symbol_table::resolve
<32, true>(
630 Sized_symbol
<32>* to
,
631 const elfcpp::Sym
<32, true>& sym
,
633 const char* version
);
636 #ifdef HAVE_TARGET_64_LITTLE
639 Symbol_table::resolve
<64, false>(
640 Sized_symbol
<64>* to
,
641 const elfcpp::Sym
<64, false>& sym
,
643 const char* version
);
646 #ifdef HAVE_TARGET_64_BIG
649 Symbol_table::resolve
<64, true>(
650 Sized_symbol
<64>* to
,
651 const elfcpp::Sym
<64, true>& sym
,
653 const char* version
);
656 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
659 Sized_symbol
<32>::override_with_special(const Sized_symbol
<32>*);
662 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
665 Sized_symbol
<64>::override_with_special(const Sized_symbol
<64>*);
668 } // End namespace gold.