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 // Resolve a symbol. This is called the second and subsequent times
55 // we see a symbol. TO is the pre-existing symbol. SYM is the new
56 // symbol, seen in OBJECT. VERSION of the version of SYM.
58 template<int size
, bool big_endian
>
60 Symbol_table::resolve(Sized_symbol
<size
>* to
,
61 const elfcpp::Sym
<size
, big_endian
>& sym
,
62 Object
* object
, const char* version
)
64 if (object
->target()->has_resolve())
66 Sized_target
<size
, big_endian
>* sized_target
;
67 sized_target
= object
->sized_target
68 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
69 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
70 sized_target
->resolve(to
, sym
, object
, version
);
74 // Build a little code for each symbol.
75 // Bit 0: 0 for global, 1 for weak.
76 // Bit 1: 0 for regular object, 1 for shared object
77 // Bits 2-3: 0 for normal, 1 for undefined, 2 for common
78 // This gives us values from 0 to 11:
97 switch (to
->binding())
99 case elfcpp::STB_GLOBAL
:
103 case elfcpp::STB_WEAK
:
107 case elfcpp::STB_LOCAL
:
108 // We should only see externally visible symbols in the symbol
113 // Any target which wants to handle STB_LOOS, etc., needs to
114 // define a resolve method.
118 if (to
->source() == Symbol::FROM_OBJECT
119 && to
->object()->is_dynamic())
124 case elfcpp::SHN_UNDEF
:
128 case elfcpp::SHN_COMMON
:
133 if (to
->type() == elfcpp::STT_COMMON
)
139 switch (sym
.get_st_bind())
141 case elfcpp::STB_GLOBAL
:
145 case elfcpp::STB_WEAK
:
149 case elfcpp::STB_LOCAL
:
151 _("%s: %s: invalid STB_LOCAL symbol %s in external symbols\n"),
152 program_name
, object
->name().c_str(), to
->name());
157 _("%s: %s: unsupported symbol binding %d for symbol %s\n"),
158 program_name
, object
->name().c_str(),
159 static_cast<int>(sym
.get_st_bind()), to
->name());
163 if (!object
->is_dynamic())
165 // Record that we've seen this symbol in a regular object.
170 frombits
|= (1 << 1);
172 // Record that we've seen this symbol in a dynamic object.
176 switch (sym
.get_st_shndx())
178 case elfcpp::SHN_UNDEF
:
179 frombits
|= (1 << 2);
182 case elfcpp::SHN_COMMON
:
183 frombits
|= (2 << 2);
187 if (sym
.get_st_type() == elfcpp::STT_COMMON
)
188 frombits
|= (2 << 2);
192 // FIXME: Warn if either but not both of TO and SYM are STT_TLS.
194 // We use a giant switch table for symbol resolution. This code is
195 // unwieldy, but: 1) it is efficient; 2) we definitely handle all
196 // cases; 3) it is easy to change the handling of a particular case.
197 // The alternative would be a series of conditionals, but it is easy
198 // to get the ordering wrong. This could also be done as a table,
199 // but that is no easier to understand than this large switch
202 switch (tobits
* 16 + frombits
)
205 // Two definitions of the same symbol.
206 fprintf(stderr
, "%s: %s: multiple definition of %s\n",
207 program_name
, object
->name().c_str(), to
->name());
208 // FIXME: Report locations. Record that we have seen an error.
211 case WEAK_DEF
* 16 + DEF
:
212 // We've seen a weak definition, and now we see a strong
213 // definition. In the original SVR4 linker, this was treated as
214 // a multiple definition error. In the Solaris linker and the
215 // GNU linker, a weak definition followed by a regular
216 // definition causes the weak definition to be overridden. We
217 // are currently compatible with the GNU linker. In the future
218 // we should add a target specific option to change this.
220 to
->override(sym
, object
, version
);
223 case DYN_DEF
* 16 + DEF
:
224 case DYN_WEAK_DEF
* 16 + DEF
:
225 // We've seen a definition in a dynamic object, and now we see a
226 // definition in a regular object. The definition in the
227 // regular object overrides the definition in the dynamic
229 to
->override(sym
, object
, version
);
232 case UNDEF
* 16 + DEF
:
233 case WEAK_UNDEF
* 16 + DEF
:
234 case DYN_UNDEF
* 16 + DEF
:
235 case DYN_WEAK_UNDEF
* 16 + DEF
:
236 // We've seen an undefined reference, and now we see a
237 // definition. We use the definition.
238 to
->override(sym
, object
, version
);
241 case COMMON
* 16 + DEF
:
242 case WEAK_COMMON
* 16 + DEF
:
243 case DYN_COMMON
* 16 + DEF
:
244 case DYN_WEAK_COMMON
* 16 + DEF
:
245 // We've seen a common symbol and now we see a definition. The
246 // definition overrides. FIXME: We should optionally issue, version a
248 to
->override(sym
, object
, version
);
251 case DEF
* 16 + WEAK_DEF
:
252 case WEAK_DEF
* 16 + WEAK_DEF
:
253 // We've seen a definition and now we see a weak definition. We
254 // ignore the new weak definition.
257 case DYN_DEF
* 16 + WEAK_DEF
:
258 case DYN_WEAK_DEF
* 16 + WEAK_DEF
:
259 // We've seen a dynamic definition and now we see a regular weak
260 // definition. The regular weak definition overrides.
261 to
->override(sym
, object
, version
);
264 case UNDEF
* 16 + WEAK_DEF
:
265 case WEAK_UNDEF
* 16 + WEAK_DEF
:
266 case DYN_UNDEF
* 16 + WEAK_DEF
:
267 case DYN_WEAK_UNDEF
* 16 + WEAK_DEF
:
268 // A weak definition of a currently undefined symbol.
269 to
->override(sym
, object
, version
);
272 case COMMON
* 16 + WEAK_DEF
:
273 case WEAK_COMMON
* 16 + WEAK_DEF
:
274 // A weak definition does not override a common definition.
277 case DYN_COMMON
* 16 + WEAK_DEF
:
278 case DYN_WEAK_COMMON
* 16 + WEAK_DEF
:
279 // A weak definition does override a definition in a dynamic
280 // object. FIXME: We should optionally issue a warning.
281 to
->override(sym
, object
, version
);
284 case DEF
* 16 + DYN_DEF
:
285 case WEAK_DEF
* 16 + DYN_DEF
:
286 case DYN_DEF
* 16 + DYN_DEF
:
287 case DYN_WEAK_DEF
* 16 + DYN_DEF
:
288 // Ignore a dynamic definition if we already have a definition.
291 case UNDEF
* 16 + DYN_DEF
:
292 case WEAK_UNDEF
* 16 + DYN_DEF
:
293 case DYN_UNDEF
* 16 + DYN_DEF
:
294 case DYN_WEAK_UNDEF
* 16 + DYN_DEF
:
295 // Use a dynamic definition if we have a reference.
296 to
->override(sym
, object
, version
);
299 case COMMON
* 16 + DYN_DEF
:
300 case WEAK_COMMON
* 16 + DYN_DEF
:
301 case DYN_COMMON
* 16 + DYN_DEF
:
302 case DYN_WEAK_COMMON
* 16 + DYN_DEF
:
303 // Ignore a dynamic definition if we already have a common
307 case DEF
* 16 + DYN_WEAK_DEF
:
308 case WEAK_DEF
* 16 + DYN_WEAK_DEF
:
309 case DYN_DEF
* 16 + DYN_WEAK_DEF
:
310 case DYN_WEAK_DEF
* 16 + DYN_WEAK_DEF
:
311 // Ignore a weak dynamic definition if we already have a
315 case UNDEF
* 16 + DYN_WEAK_DEF
:
316 case WEAK_UNDEF
* 16 + DYN_WEAK_DEF
:
317 case DYN_UNDEF
* 16 + DYN_WEAK_DEF
:
318 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_DEF
:
319 // Use a weak dynamic definition if we have a reference.
320 to
->override(sym
, object
, version
);
323 case COMMON
* 16 + DYN_WEAK_DEF
:
324 case WEAK_COMMON
* 16 + DYN_WEAK_DEF
:
325 case DYN_COMMON
* 16 + DYN_WEAK_DEF
:
326 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_DEF
:
327 // Ignore a weak dynamic definition if we already have a common
331 case DEF
* 16 + UNDEF
:
332 case WEAK_DEF
* 16 + UNDEF
:
333 case DYN_DEF
* 16 + UNDEF
:
334 case DYN_WEAK_DEF
* 16 + UNDEF
:
335 case UNDEF
* 16 + UNDEF
:
336 // A new undefined reference tells us nothing.
339 case WEAK_UNDEF
* 16 + UNDEF
:
340 case DYN_UNDEF
* 16 + UNDEF
:
341 case DYN_WEAK_UNDEF
* 16 + UNDEF
:
342 // A strong undef overrides a dynamic or weak undef.
343 to
->override(sym
, object
, version
);
346 case COMMON
* 16 + UNDEF
:
347 case WEAK_COMMON
* 16 + UNDEF
:
348 case DYN_COMMON
* 16 + UNDEF
:
349 case DYN_WEAK_COMMON
* 16 + UNDEF
:
350 // A new undefined reference tells us nothing.
353 case DEF
* 16 + WEAK_UNDEF
:
354 case WEAK_DEF
* 16 + WEAK_UNDEF
:
355 case DYN_DEF
* 16 + WEAK_UNDEF
:
356 case DYN_WEAK_DEF
* 16 + WEAK_UNDEF
:
357 case UNDEF
* 16 + WEAK_UNDEF
:
358 case WEAK_UNDEF
* 16 + WEAK_UNDEF
:
359 case DYN_UNDEF
* 16 + WEAK_UNDEF
:
360 case DYN_WEAK_UNDEF
* 16 + WEAK_UNDEF
:
361 case COMMON
* 16 + WEAK_UNDEF
:
362 case WEAK_COMMON
* 16 + WEAK_UNDEF
:
363 case DYN_COMMON
* 16 + WEAK_UNDEF
:
364 case DYN_WEAK_COMMON
* 16 + WEAK_UNDEF
:
365 // A new weak undefined reference tells us nothing.
368 case DEF
* 16 + DYN_UNDEF
:
369 case WEAK_DEF
* 16 + DYN_UNDEF
:
370 case DYN_DEF
* 16 + DYN_UNDEF
:
371 case DYN_WEAK_DEF
* 16 + DYN_UNDEF
:
372 case UNDEF
* 16 + DYN_UNDEF
:
373 case WEAK_UNDEF
* 16 + DYN_UNDEF
:
374 case DYN_UNDEF
* 16 + DYN_UNDEF
:
375 case DYN_WEAK_UNDEF
* 16 + DYN_UNDEF
:
376 case COMMON
* 16 + DYN_UNDEF
:
377 case WEAK_COMMON
* 16 + DYN_UNDEF
:
378 case DYN_COMMON
* 16 + DYN_UNDEF
:
379 case DYN_WEAK_COMMON
* 16 + DYN_UNDEF
:
380 // A new dynamic undefined reference tells us nothing.
383 case DEF
* 16 + DYN_WEAK_UNDEF
:
384 case WEAK_DEF
* 16 + DYN_WEAK_UNDEF
:
385 case DYN_DEF
* 16 + DYN_WEAK_UNDEF
:
386 case DYN_WEAK_DEF
* 16 + DYN_WEAK_UNDEF
:
387 case UNDEF
* 16 + DYN_WEAK_UNDEF
:
388 case WEAK_UNDEF
* 16 + DYN_WEAK_UNDEF
:
389 case DYN_UNDEF
* 16 + DYN_WEAK_UNDEF
:
390 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_UNDEF
:
391 case COMMON
* 16 + DYN_WEAK_UNDEF
:
392 case WEAK_COMMON
* 16 + DYN_WEAK_UNDEF
:
393 case DYN_COMMON
* 16 + DYN_WEAK_UNDEF
:
394 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_UNDEF
:
395 // A new weak dynamic undefined reference tells us nothing.
398 case DEF
* 16 + COMMON
:
399 // A common symbol does not override a definition.
402 case WEAK_DEF
* 16 + COMMON
:
403 case DYN_DEF
* 16 + COMMON
:
404 case DYN_WEAK_DEF
* 16 + COMMON
:
405 // A common symbol does override a weak definition or a dynamic
407 to
->override(sym
, object
, version
);
410 case UNDEF
* 16 + COMMON
:
411 case WEAK_UNDEF
* 16 + COMMON
:
412 case DYN_UNDEF
* 16 + COMMON
:
413 case DYN_WEAK_UNDEF
* 16 + COMMON
:
414 // A common symbol is a definition for a reference.
415 to
->override(sym
, object
, version
);
418 case COMMON
* 16 + COMMON
:
419 // Set the size to the maximum.
420 if (sym
.get_st_size() > to
->symsize())
421 to
->set_symsize(sym
.get_st_size());
424 case WEAK_COMMON
* 16 + COMMON
:
425 // I'm not sure just what a weak common symbol means, but
426 // presumably it can be overridden by a regular common symbol.
427 to
->override(sym
, object
, version
);
430 case DYN_COMMON
* 16 + COMMON
:
431 case DYN_WEAK_COMMON
* 16 + COMMON
:
433 // Use the real common symbol, but adjust the size if necessary.
434 typename Sized_symbol
<size
>::Size_type symsize
= to
->symsize();
435 to
->override(sym
, object
, version
);
436 if (to
->symsize() < symsize
)
437 to
->set_symsize(symsize
);
441 case DEF
* 16 + WEAK_COMMON
:
442 case WEAK_DEF
* 16 + WEAK_COMMON
:
443 case DYN_DEF
* 16 + WEAK_COMMON
:
444 case DYN_WEAK_DEF
* 16 + WEAK_COMMON
:
445 // Whatever a weak common symbol is, it won't override a
449 case UNDEF
* 16 + WEAK_COMMON
:
450 case WEAK_UNDEF
* 16 + WEAK_COMMON
:
451 case DYN_UNDEF
* 16 + WEAK_COMMON
:
452 case DYN_WEAK_UNDEF
* 16 + WEAK_COMMON
:
453 // A weak common symbol is better than an undefined symbol.
454 to
->override(sym
, object
, version
);
457 case COMMON
* 16 + WEAK_COMMON
:
458 case WEAK_COMMON
* 16 + WEAK_COMMON
:
459 case DYN_COMMON
* 16 + WEAK_COMMON
:
460 case DYN_WEAK_COMMON
* 16 + WEAK_COMMON
:
461 // Ignore a weak common symbol in the presence of a real common
465 case DEF
* 16 + DYN_COMMON
:
466 case WEAK_DEF
* 16 + DYN_COMMON
:
467 case DYN_DEF
* 16 + DYN_COMMON
:
468 case DYN_WEAK_DEF
* 16 + DYN_COMMON
:
469 // Ignore a dynamic common symbol in the presence of a
473 case UNDEF
* 16 + DYN_COMMON
:
474 case WEAK_UNDEF
* 16 + DYN_COMMON
:
475 case DYN_UNDEF
* 16 + DYN_COMMON
:
476 case DYN_WEAK_UNDEF
* 16 + DYN_COMMON
:
477 // A dynamic common symbol is a definition of sorts.
478 to
->override(sym
, object
, version
);
481 case COMMON
* 16 + DYN_COMMON
:
482 case WEAK_COMMON
* 16 + DYN_COMMON
:
483 case DYN_COMMON
* 16 + DYN_COMMON
:
484 case DYN_WEAK_COMMON
* 16 + DYN_COMMON
:
485 // Set the size to the maximum.
486 if (sym
.get_st_size() > to
->symsize())
487 to
->set_symsize(sym
.get_st_size());
490 case DEF
* 16 + DYN_WEAK_COMMON
:
491 case WEAK_DEF
* 16 + DYN_WEAK_COMMON
:
492 case DYN_DEF
* 16 + DYN_WEAK_COMMON
:
493 case DYN_WEAK_DEF
* 16 + DYN_WEAK_COMMON
:
494 // A common symbol is ignored in the face of a definition.
497 case UNDEF
* 16 + DYN_WEAK_COMMON
:
498 case WEAK_UNDEF
* 16 + DYN_WEAK_COMMON
:
499 case DYN_UNDEF
* 16 + DYN_WEAK_COMMON
:
500 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_COMMON
:
501 // I guess a weak common symbol is better than a definition.
502 to
->override(sym
, object
, version
);
505 case COMMON
* 16 + DYN_WEAK_COMMON
:
506 case WEAK_COMMON
* 16 + DYN_WEAK_COMMON
:
507 case DYN_COMMON
* 16 + DYN_WEAK_COMMON
:
508 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_COMMON
:
509 // Set the size to the maximum.
510 if (sym
.get_st_size() > to
->symsize())
511 to
->set_symsize(sym
.get_st_size());
519 // Instantiate the templates we need. We could use the configure
520 // script to restrict this to only the ones needed for implemented
523 #ifdef HAVE_TARGET_32_LITTLE
526 Symbol_table::resolve
<32, false>(
527 Sized_symbol
<32>* to
,
528 const elfcpp::Sym
<32, false>& sym
,
530 const char* version
);
533 #ifdef HAVE_TARGET_32_BIG
536 Symbol_table::resolve
<32, true>(
537 Sized_symbol
<32>* to
,
538 const elfcpp::Sym
<32, true>& sym
,
540 const char* version
);
543 #ifdef HAVE_TARGET_64_LITTLE
546 Symbol_table::resolve
<64, false>(
547 Sized_symbol
<64>* to
,
548 const elfcpp::Sym
<64, false>& sym
,
550 const char* version
);
553 #ifdef HAVE_TARGET_64_BIG
556 Symbol_table::resolve
<64, true>(
557 Sized_symbol
<64>* to
,
558 const elfcpp::Sym
<64, true>& sym
,
560 const char* version
);
563 } // End namespace gold.