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();
37 // Override the fields in Sized_symbol.
40 template<bool big_endian
>
42 Sized_symbol
<size
>::override(const elfcpp::Sym
<size
, big_endian
>& sym
,
43 Object
* object
, const char* version
)
45 this->override_base(sym
, object
, version
);
46 this->value_
= sym
.get_st_value();
47 this->symsize_
= sym
.get_st_size();
50 // Resolve a symbol. This is called the second and subsequent times
51 // we see a symbol. TO is the pre-existing symbol. SYM is the new
52 // symbol, seen in OBJECT. VERSION of the version of SYM.
54 template<int size
, bool big_endian
>
56 Symbol_table::resolve(Sized_symbol
<size
>* to
,
57 const elfcpp::Sym
<size
, big_endian
>& sym
,
58 Object
* object
, const char* version
)
60 if (object
->target()->has_resolve())
62 Sized_target
<size
, big_endian
>* sized_target
;
63 sized_target
= object
->sized_target
64 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
65 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
66 sized_target
->resolve(to
, sym
, object
, version
);
70 // Build a little code for each symbol.
71 // Bit 0: 0 for global, 1 for weak.
72 // Bit 1: 0 for regular object, 1 for shared object
73 // Bits 2-3: 0 for normal, 1 for undefined, 2 for common
74 // This gives us values from 0 to 11:
93 switch (to
->binding())
95 case elfcpp::STB_GLOBAL
:
99 case elfcpp::STB_WEAK
:
103 case elfcpp::STB_LOCAL
:
104 // We should only see externally visible symbols in the symbol
109 // Any target which wants to handle STB_LOOS, etc., needs to
110 // define a resolve method.
114 if (to
->source() == Symbol::FROM_OBJECT
115 && to
->object()->is_dynamic())
120 case elfcpp::SHN_UNDEF
:
124 case elfcpp::SHN_COMMON
:
129 if (to
->type() == elfcpp::STT_COMMON
)
135 switch (sym
.get_st_bind())
137 case elfcpp::STB_GLOBAL
:
141 case elfcpp::STB_WEAK
:
145 case elfcpp::STB_LOCAL
:
147 _("%s: %s: invalid STB_LOCAL symbol %s in external symbols\n"),
148 program_name
, object
->name().c_str(), to
->name());
153 _("%s: %s: unsupported symbol binding %d for symbol %s\n"),
154 program_name
, object
->name().c_str(),
155 static_cast<int>(sym
.get_st_bind()), to
->name());
159 if (!object
->is_dynamic())
161 // Record that we've seen this symbol in a regular object.
166 frombits
|= (1 << 1);
168 // Record that we've seen this symbol in a dynamic object.
172 switch (sym
.get_st_shndx())
174 case elfcpp::SHN_UNDEF
:
175 frombits
|= (1 << 2);
178 case elfcpp::SHN_COMMON
:
179 frombits
|= (2 << 2);
183 if (sym
.get_st_type() == elfcpp::STT_COMMON
)
184 frombits
|= (2 << 2);
188 if ((tobits
& (1 << 1)) != (frombits
& (1 << 1)))
190 // This symbol is seen in both a dynamic object and a regular
191 // object. That means that we need the symbol to go into the
192 // dynamic symbol table, so that the dynamic linker can use the
193 // regular symbol to override or define the dynamic symbol.
194 to
->set_needs_dynsym_entry();
197 // FIXME: Warn if either but not both of TO and SYM are STT_TLS.
199 // We use a giant switch table for symbol resolution. This code is
200 // unwieldy, but: 1) it is efficient; 2) we definitely handle all
201 // cases; 3) it is easy to change the handling of a particular case.
202 // The alternative would be a series of conditionals, but it is easy
203 // to get the ordering wrong. This could also be done as a table,
204 // but that is no easier to understand than this large switch
207 switch (tobits
* 16 + frombits
)
210 // Two definitions of the same symbol.
211 fprintf(stderr
, "%s: %s: multiple definition of %s\n",
212 program_name
, object
->name().c_str(), to
->name());
213 // FIXME: Report locations. Record that we have seen an error.
216 case WEAK_DEF
* 16 + DEF
:
217 // We've seen a weak definition, and now we see a strong
218 // definition. In the original SVR4 linker, this was treated as
219 // a multiple definition error. In the Solaris linker and the
220 // GNU linker, a weak definition followed by a regular
221 // definition causes the weak definition to be overridden. We
222 // are currently compatible with the GNU linker. In the future
223 // we should add a target specific option to change this.
225 to
->override(sym
, object
, version
);
228 case DYN_DEF
* 16 + DEF
:
229 case DYN_WEAK_DEF
* 16 + DEF
:
230 // We've seen a definition in a dynamic object, and now we see a
231 // definition in a regular object. The definition in the
232 // regular object overrides the definition in the dynamic
234 to
->override(sym
, object
, version
);
237 case UNDEF
* 16 + DEF
:
238 case WEAK_UNDEF
* 16 + DEF
:
239 case DYN_UNDEF
* 16 + DEF
:
240 case DYN_WEAK_UNDEF
* 16 + DEF
:
241 // We've seen an undefined reference, and now we see a
242 // definition. We use the definition.
243 to
->override(sym
, object
, version
);
246 case COMMON
* 16 + DEF
:
247 case WEAK_COMMON
* 16 + DEF
:
248 case DYN_COMMON
* 16 + DEF
:
249 case DYN_WEAK_COMMON
* 16 + DEF
:
250 // We've seen a common symbol and now we see a definition. The
251 // definition overrides. FIXME: We should optionally issue, version a
253 to
->override(sym
, object
, version
);
256 case DEF
* 16 + WEAK_DEF
:
257 case WEAK_DEF
* 16 + WEAK_DEF
:
258 // We've seen a definition and now we see a weak definition. We
259 // ignore the new weak definition.
262 case DYN_DEF
* 16 + WEAK_DEF
:
263 case DYN_WEAK_DEF
* 16 + WEAK_DEF
:
264 // We've seen a dynamic definition and now we see a regular weak
265 // definition. The regular weak definition overrides.
266 to
->override(sym
, object
, version
);
269 case UNDEF
* 16 + WEAK_DEF
:
270 case WEAK_UNDEF
* 16 + WEAK_DEF
:
271 case DYN_UNDEF
* 16 + WEAK_DEF
:
272 case DYN_WEAK_UNDEF
* 16 + WEAK_DEF
:
273 // A weak definition of a currently undefined symbol.
274 to
->override(sym
, object
, version
);
277 case COMMON
* 16 + WEAK_DEF
:
278 case WEAK_COMMON
* 16 + WEAK_DEF
:
279 // A weak definition does not override a common definition.
282 case DYN_COMMON
* 16 + WEAK_DEF
:
283 case DYN_WEAK_COMMON
* 16 + WEAK_DEF
:
284 // A weak definition does override a definition in a dynamic
285 // object. FIXME: We should optionally issue a warning.
286 to
->override(sym
, object
, version
);
289 case DEF
* 16 + DYN_DEF
:
290 case WEAK_DEF
* 16 + DYN_DEF
:
291 case DYN_DEF
* 16 + DYN_DEF
:
292 case DYN_WEAK_DEF
* 16 + DYN_DEF
:
293 // Ignore a dynamic definition if we already have a definition.
296 case UNDEF
* 16 + DYN_DEF
:
297 case WEAK_UNDEF
* 16 + DYN_DEF
:
298 case DYN_UNDEF
* 16 + DYN_DEF
:
299 case DYN_WEAK_UNDEF
* 16 + DYN_DEF
:
300 // Use a dynamic definition if we have a reference.
301 to
->override(sym
, object
, version
);
304 case COMMON
* 16 + DYN_DEF
:
305 case WEAK_COMMON
* 16 + DYN_DEF
:
306 case DYN_COMMON
* 16 + DYN_DEF
:
307 case DYN_WEAK_COMMON
* 16 + DYN_DEF
:
308 // Ignore a dynamic definition if we already have a common
312 case DEF
* 16 + DYN_WEAK_DEF
:
313 case WEAK_DEF
* 16 + DYN_WEAK_DEF
:
314 case DYN_DEF
* 16 + DYN_WEAK_DEF
:
315 case DYN_WEAK_DEF
* 16 + DYN_WEAK_DEF
:
316 // Ignore a weak dynamic definition if we already have a
320 case UNDEF
* 16 + DYN_WEAK_DEF
:
321 case WEAK_UNDEF
* 16 + DYN_WEAK_DEF
:
322 case DYN_UNDEF
* 16 + DYN_WEAK_DEF
:
323 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_DEF
:
324 // Use a weak dynamic definition if we have a reference.
325 to
->override(sym
, object
, version
);
328 case COMMON
* 16 + DYN_WEAK_DEF
:
329 case WEAK_COMMON
* 16 + DYN_WEAK_DEF
:
330 case DYN_COMMON
* 16 + DYN_WEAK_DEF
:
331 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_DEF
:
332 // Ignore a weak dynamic definition if we already have a common
336 case DEF
* 16 + UNDEF
:
337 case WEAK_DEF
* 16 + UNDEF
:
338 case DYN_DEF
* 16 + UNDEF
:
339 case DYN_WEAK_DEF
* 16 + UNDEF
:
340 case UNDEF
* 16 + UNDEF
:
341 // A new undefined reference tells us nothing.
344 case WEAK_UNDEF
* 16 + UNDEF
:
345 case DYN_UNDEF
* 16 + UNDEF
:
346 case DYN_WEAK_UNDEF
* 16 + UNDEF
:
347 // A strong undef overrides a dynamic or weak undef.
348 to
->override(sym
, object
, version
);
351 case COMMON
* 16 + UNDEF
:
352 case WEAK_COMMON
* 16 + UNDEF
:
353 case DYN_COMMON
* 16 + UNDEF
:
354 case DYN_WEAK_COMMON
* 16 + UNDEF
:
355 // A new undefined reference tells us nothing.
358 case DEF
* 16 + WEAK_UNDEF
:
359 case WEAK_DEF
* 16 + WEAK_UNDEF
:
360 case DYN_DEF
* 16 + WEAK_UNDEF
:
361 case DYN_WEAK_DEF
* 16 + WEAK_UNDEF
:
362 case UNDEF
* 16 + WEAK_UNDEF
:
363 case WEAK_UNDEF
* 16 + WEAK_UNDEF
:
364 case DYN_UNDEF
* 16 + WEAK_UNDEF
:
365 case DYN_WEAK_UNDEF
* 16 + WEAK_UNDEF
:
366 case COMMON
* 16 + WEAK_UNDEF
:
367 case WEAK_COMMON
* 16 + WEAK_UNDEF
:
368 case DYN_COMMON
* 16 + WEAK_UNDEF
:
369 case DYN_WEAK_COMMON
* 16 + WEAK_UNDEF
:
370 // A new weak undefined reference tells us nothing.
373 case DEF
* 16 + DYN_UNDEF
:
374 case WEAK_DEF
* 16 + DYN_UNDEF
:
375 case DYN_DEF
* 16 + DYN_UNDEF
:
376 case DYN_WEAK_DEF
* 16 + DYN_UNDEF
:
377 case UNDEF
* 16 + DYN_UNDEF
:
378 case WEAK_UNDEF
* 16 + DYN_UNDEF
:
379 case DYN_UNDEF
* 16 + DYN_UNDEF
:
380 case DYN_WEAK_UNDEF
* 16 + DYN_UNDEF
:
381 case COMMON
* 16 + DYN_UNDEF
:
382 case WEAK_COMMON
* 16 + DYN_UNDEF
:
383 case DYN_COMMON
* 16 + DYN_UNDEF
:
384 case DYN_WEAK_COMMON
* 16 + DYN_UNDEF
:
385 // A new dynamic undefined reference tells us nothing.
388 case DEF
* 16 + DYN_WEAK_UNDEF
:
389 case WEAK_DEF
* 16 + DYN_WEAK_UNDEF
:
390 case DYN_DEF
* 16 + DYN_WEAK_UNDEF
:
391 case DYN_WEAK_DEF
* 16 + DYN_WEAK_UNDEF
:
392 case UNDEF
* 16 + DYN_WEAK_UNDEF
:
393 case WEAK_UNDEF
* 16 + DYN_WEAK_UNDEF
:
394 case DYN_UNDEF
* 16 + DYN_WEAK_UNDEF
:
395 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_UNDEF
:
396 case COMMON
* 16 + DYN_WEAK_UNDEF
:
397 case WEAK_COMMON
* 16 + DYN_WEAK_UNDEF
:
398 case DYN_COMMON
* 16 + DYN_WEAK_UNDEF
:
399 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_UNDEF
:
400 // A new weak dynamic undefined reference tells us nothing.
403 case DEF
* 16 + COMMON
:
404 // A common symbol does not override a definition.
407 case WEAK_DEF
* 16 + COMMON
:
408 case DYN_DEF
* 16 + COMMON
:
409 case DYN_WEAK_DEF
* 16 + COMMON
:
410 // A common symbol does override a weak definition or a dynamic
412 to
->override(sym
, object
, version
);
415 case UNDEF
* 16 + COMMON
:
416 case WEAK_UNDEF
* 16 + COMMON
:
417 case DYN_UNDEF
* 16 + COMMON
:
418 case DYN_WEAK_UNDEF
* 16 + COMMON
:
419 // A common symbol is a definition for a reference.
420 to
->override(sym
, object
, version
);
423 case COMMON
* 16 + COMMON
:
424 // Set the size to the maximum.
425 if (sym
.get_st_size() > to
->symsize())
426 to
->set_symsize(sym
.get_st_size());
429 case WEAK_COMMON
* 16 + COMMON
:
430 // I'm not sure just what a weak common symbol means, but
431 // presumably it can be overridden by a regular common symbol.
432 to
->override(sym
, object
, version
);
435 case DYN_COMMON
* 16 + COMMON
:
436 case DYN_WEAK_COMMON
* 16 + COMMON
:
438 // Use the real common symbol, but adjust the size if necessary.
439 typename Sized_symbol
<size
>::Size_type symsize
= to
->symsize();
440 to
->override(sym
, object
, version
);
441 if (to
->symsize() < symsize
)
442 to
->set_symsize(symsize
);
446 case DEF
* 16 + WEAK_COMMON
:
447 case WEAK_DEF
* 16 + WEAK_COMMON
:
448 case DYN_DEF
* 16 + WEAK_COMMON
:
449 case DYN_WEAK_DEF
* 16 + WEAK_COMMON
:
450 // Whatever a weak common symbol is, it won't override a
454 case UNDEF
* 16 + WEAK_COMMON
:
455 case WEAK_UNDEF
* 16 + WEAK_COMMON
:
456 case DYN_UNDEF
* 16 + WEAK_COMMON
:
457 case DYN_WEAK_UNDEF
* 16 + WEAK_COMMON
:
458 // A weak common symbol is better than an undefined symbol.
459 to
->override(sym
, object
, version
);
462 case COMMON
* 16 + WEAK_COMMON
:
463 case WEAK_COMMON
* 16 + WEAK_COMMON
:
464 case DYN_COMMON
* 16 + WEAK_COMMON
:
465 case DYN_WEAK_COMMON
* 16 + WEAK_COMMON
:
466 // Ignore a weak common symbol in the presence of a real common
470 case DEF
* 16 + DYN_COMMON
:
471 case WEAK_DEF
* 16 + DYN_COMMON
:
472 case DYN_DEF
* 16 + DYN_COMMON
:
473 case DYN_WEAK_DEF
* 16 + DYN_COMMON
:
474 // Ignore a dynamic common symbol in the presence of a
478 case UNDEF
* 16 + DYN_COMMON
:
479 case WEAK_UNDEF
* 16 + DYN_COMMON
:
480 case DYN_UNDEF
* 16 + DYN_COMMON
:
481 case DYN_WEAK_UNDEF
* 16 + DYN_COMMON
:
482 // A dynamic common symbol is a definition of sorts.
483 to
->override(sym
, object
, version
);
486 case COMMON
* 16 + DYN_COMMON
:
487 case WEAK_COMMON
* 16 + DYN_COMMON
:
488 case DYN_COMMON
* 16 + DYN_COMMON
:
489 case DYN_WEAK_COMMON
* 16 + DYN_COMMON
:
490 // Set the size to the maximum.
491 if (sym
.get_st_size() > to
->symsize())
492 to
->set_symsize(sym
.get_st_size());
495 case DEF
* 16 + DYN_WEAK_COMMON
:
496 case WEAK_DEF
* 16 + DYN_WEAK_COMMON
:
497 case DYN_DEF
* 16 + DYN_WEAK_COMMON
:
498 case DYN_WEAK_DEF
* 16 + DYN_WEAK_COMMON
:
499 // A common symbol is ignored in the face of a definition.
502 case UNDEF
* 16 + DYN_WEAK_COMMON
:
503 case WEAK_UNDEF
* 16 + DYN_WEAK_COMMON
:
504 case DYN_UNDEF
* 16 + DYN_WEAK_COMMON
:
505 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_COMMON
:
506 // I guess a weak common symbol is better than a definition.
507 to
->override(sym
, object
, version
);
510 case COMMON
* 16 + DYN_WEAK_COMMON
:
511 case WEAK_COMMON
* 16 + DYN_WEAK_COMMON
:
512 case DYN_COMMON
* 16 + DYN_WEAK_COMMON
:
513 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_COMMON
:
514 // Set the size to the maximum.
515 if (sym
.get_st_size() > to
->symsize())
516 to
->set_symsize(sym
.get_st_size());
524 // Instantiate the templates we need. We could use the configure
525 // script to restrict this to only the ones needed for implemented
530 Symbol_table::resolve
<32, true>(
531 Sized_symbol
<32>* to
,
532 const elfcpp::Sym
<32, true>& sym
,
534 const char* version
);
538 Symbol_table::resolve
<32, false>(
539 Sized_symbol
<32>* to
,
540 const elfcpp::Sym
<32, false>& sym
,
542 const char* version
);
546 Symbol_table::resolve
<64, true>(
547 Sized_symbol
<64>* to
,
548 const elfcpp::Sym
<64, true>& sym
,
550 const char* version
);
554 Symbol_table::resolve
<64, false>(
555 Sized_symbol
<64>* to
,
556 const elfcpp::Sym
<64, false>& sym
,
558 const char* version
);
560 } // End namespace gold.