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
,
22 gold_assert(this->source_
== FROM_OBJECT
);
23 this->u_
.from_object
.object
= object
;
24 // FIXME: Handle SHN_XINDEX.
25 this->u_
.from_object
.shndx
= sym
.get_st_shndx();
26 this->type_
= sym
.get_st_type();
27 this->binding_
= sym
.get_st_bind();
28 this->visibility_
= sym
.get_st_visibility();
29 this->nonvis_
= sym
.get_st_nonvis();
32 // Override the fields in Sized_symbol.
35 template<bool big_endian
>
37 Sized_symbol
<size
>::override(const elfcpp::Sym
<size
, big_endian
>& sym
,
40 this->override_base(sym
, object
);
41 this->value_
= sym
.get_st_value();
42 this->symsize_
= sym
.get_st_size();
45 // Resolve a symbol. This is called the second and subsequent times
46 // we see a symbol. TO is the pre-existing symbol. SYM is the new
47 // symbol, seen in OBJECT.
49 template<int size
, bool big_endian
>
51 Symbol_table::resolve(Sized_symbol
<size
>* to
,
52 const elfcpp::Sym
<size
, big_endian
>& sym
,
55 if (object
->target()->has_resolve())
57 Sized_target
<size
, big_endian
>* sized_target
;
58 sized_target
= object
->sized_target
59 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
60 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
61 sized_target
->resolve(to
, sym
, object
);
65 // Build a little code for each symbol.
66 // Bit 0: 0 for global, 1 for weak.
67 // Bit 1: 0 for regular object, 1 for shared object
68 // Bits 2-3: 0 for normal, 1 for undefined, 2 for common
69 // This gives us values from 0 to 11:
88 switch (to
->binding())
90 case elfcpp::STB_GLOBAL
:
94 case elfcpp::STB_WEAK
:
98 case elfcpp::STB_LOCAL
:
99 // We should only see externally visible symbols in the symbol
104 // Any target which wants to handle STB_LOOS, etc., needs to
105 // define a resolve method.
109 if (to
->source() == Symbol::FROM_OBJECT
110 && to
->object()->is_dynamic())
115 case elfcpp::SHN_UNDEF
:
119 case elfcpp::SHN_COMMON
:
124 if (to
->type() == elfcpp::STT_COMMON
)
130 switch (sym
.get_st_bind())
132 case elfcpp::STB_GLOBAL
:
136 case elfcpp::STB_WEAK
:
140 case elfcpp::STB_LOCAL
:
142 _("%s: %s: invalid STB_LOCAL symbol %s in external symbols\n"),
143 program_name
, object
->name().c_str(), to
->name());
148 _("%s: %s: unsupported symbol binding %d for symbol %s\n"),
149 program_name
, object
->name().c_str(),
150 static_cast<int>(sym
.get_st_bind()), to
->name());
154 if (object
->is_dynamic())
156 frombits
|= (1 << 1);
158 // Record that we've seen this symbol in a dynamic object.
162 switch (sym
.get_st_shndx())
164 case elfcpp::SHN_UNDEF
:
165 frombits
|= (1 << 2);
168 case elfcpp::SHN_COMMON
:
169 frombits
|= (2 << 2);
173 if (sym
.get_st_type() == elfcpp::STT_COMMON
)
174 frombits
|= (2 << 2);
178 if ((tobits
& (1 << 1)) != (frombits
& (1 << 1)))
180 // This symbol is seen in both a dynamic object and a regular
181 // object. That means that we need the symbol to go into the
182 // dynamic symbol table, so that the dynamic linker can use the
183 // regular symbol to override or define the dynamic symbol.
184 to
->set_needs_dynsym_entry();
187 // FIXME: Warn if either but not both of TO and SYM are STT_TLS.
189 // We use a giant switch table for symbol resolution. This code is
190 // unwieldy, but: 1) it is efficient; 2) we definitely handle all
191 // cases; 3) it is easy to change the handling of a particular case.
192 // The alternative would be a series of conditionals, but it is easy
193 // to get the ordering wrong. This could also be done as a table,
194 // but that is no easier to understand than this large switch
197 switch (tobits
* 16 + frombits
)
200 // Two definitions of the same symbol.
201 fprintf(stderr
, "%s: %s: multiple definition of %s\n",
202 program_name
, object
->name().c_str(), to
->name());
203 // FIXME: Report locations. Record that we have seen an error.
206 case WEAK_DEF
* 16 + DEF
:
207 // We've seen a weak definition, and now we see a strong
208 // definition. In the original SVR4 linker, this was treated as
209 // a multiple definition error. In the Solaris linker and the
210 // GNU linker, a weak definition followed by a regular
211 // definition causes the weak definition to be overridden. We
212 // are currently compatible with the GNU linker. In the future
213 // we should add a target specific option to change this.
215 to
->override(sym
, object
);
218 case DYN_DEF
* 16 + DEF
:
219 case DYN_WEAK_DEF
* 16 + DEF
:
220 // We've seen a definition in a dynamic object, and now we see a
221 // definition in a regular object. The definition in the
222 // regular object overrides the definition in the dynamic
224 to
->override(sym
, object
);
227 case UNDEF
* 16 + DEF
:
228 case WEAK_UNDEF
* 16 + DEF
:
229 case DYN_UNDEF
* 16 + DEF
:
230 case DYN_WEAK_UNDEF
* 16 + DEF
:
231 // We've seen an undefined reference, and now we see a
232 // definition. We use the definition.
233 to
->override(sym
, object
);
236 case COMMON
* 16 + DEF
:
237 case WEAK_COMMON
* 16 + DEF
:
238 case DYN_COMMON
* 16 + DEF
:
239 case DYN_WEAK_COMMON
* 16 + DEF
:
240 // We've seen a common symbol and now we see a definition. The
241 // definition overrides. FIXME: We should optionally issue a
243 to
->override(sym
, object
);
246 case DEF
* 16 + WEAK_DEF
:
247 case WEAK_DEF
* 16 + WEAK_DEF
:
248 // We've seen a definition and now we see a weak definition. We
249 // ignore the new weak definition.
252 case DYN_DEF
* 16 + WEAK_DEF
:
253 case DYN_WEAK_DEF
* 16 + WEAK_DEF
:
254 // We've seen a dynamic definition and now we see a regular weak
255 // definition. The regular weak definition overrides.
256 to
->override(sym
, object
);
259 case UNDEF
* 16 + WEAK_DEF
:
260 case WEAK_UNDEF
* 16 + WEAK_DEF
:
261 case DYN_UNDEF
* 16 + WEAK_DEF
:
262 case DYN_WEAK_UNDEF
* 16 + WEAK_DEF
:
263 // A weak definition of a currently undefined symbol.
264 to
->override(sym
, object
);
267 case COMMON
* 16 + WEAK_DEF
:
268 case WEAK_COMMON
* 16 + WEAK_DEF
:
269 // A weak definition does not override a common definition.
272 case DYN_COMMON
* 16 + WEAK_DEF
:
273 case DYN_WEAK_COMMON
* 16 + WEAK_DEF
:
274 // A weak definition does override a definition in a dynamic
275 // object. FIXME: We should optionally issue a warning.
276 to
->override(sym
, object
);
279 case DEF
* 16 + DYN_DEF
:
280 case WEAK_DEF
* 16 + DYN_DEF
:
281 case DYN_DEF
* 16 + DYN_DEF
:
282 case DYN_WEAK_DEF
* 16 + DYN_DEF
:
283 // Ignore a dynamic definition if we already have a definition.
286 case UNDEF
* 16 + DYN_DEF
:
287 case WEAK_UNDEF
* 16 + DYN_DEF
:
288 case DYN_UNDEF
* 16 + DYN_DEF
:
289 case DYN_WEAK_UNDEF
* 16 + DYN_DEF
:
290 // Use a dynamic definition if we have a reference.
291 to
->override(sym
, object
);
294 case COMMON
* 16 + DYN_DEF
:
295 case WEAK_COMMON
* 16 + DYN_DEF
:
296 case DYN_COMMON
* 16 + DYN_DEF
:
297 case DYN_WEAK_COMMON
* 16 + DYN_DEF
:
298 // Ignore a dynamic definition if we already have a common
302 case DEF
* 16 + DYN_WEAK_DEF
:
303 case WEAK_DEF
* 16 + DYN_WEAK_DEF
:
304 case DYN_DEF
* 16 + DYN_WEAK_DEF
:
305 case DYN_WEAK_DEF
* 16 + DYN_WEAK_DEF
:
306 // Ignore a weak dynamic definition if we already have a
310 case UNDEF
* 16 + DYN_WEAK_DEF
:
311 case WEAK_UNDEF
* 16 + DYN_WEAK_DEF
:
312 case DYN_UNDEF
* 16 + DYN_WEAK_DEF
:
313 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_DEF
:
314 // Use a weak dynamic definition if we have a reference.
315 to
->override(sym
, object
);
318 case COMMON
* 16 + DYN_WEAK_DEF
:
319 case WEAK_COMMON
* 16 + DYN_WEAK_DEF
:
320 case DYN_COMMON
* 16 + DYN_WEAK_DEF
:
321 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_DEF
:
322 // Ignore a weak dynamic definition if we already have a common
326 case DEF
* 16 + UNDEF
:
327 case WEAK_DEF
* 16 + UNDEF
:
328 case DYN_DEF
* 16 + UNDEF
:
329 case DYN_WEAK_DEF
* 16 + UNDEF
:
330 case UNDEF
* 16 + UNDEF
:
331 // A new undefined reference tells us nothing.
334 case WEAK_UNDEF
* 16 + UNDEF
:
335 case DYN_UNDEF
* 16 + UNDEF
:
336 case DYN_WEAK_UNDEF
* 16 + UNDEF
:
337 // A strong undef overrides a dynamic or weak undef.
338 to
->override(sym
, object
);
341 case COMMON
* 16 + UNDEF
:
342 case WEAK_COMMON
* 16 + UNDEF
:
343 case DYN_COMMON
* 16 + UNDEF
:
344 case DYN_WEAK_COMMON
* 16 + UNDEF
:
345 // A new undefined reference tells us nothing.
348 case DEF
* 16 + WEAK_UNDEF
:
349 case WEAK_DEF
* 16 + WEAK_UNDEF
:
350 case DYN_DEF
* 16 + WEAK_UNDEF
:
351 case DYN_WEAK_DEF
* 16 + WEAK_UNDEF
:
352 case UNDEF
* 16 + WEAK_UNDEF
:
353 case WEAK_UNDEF
* 16 + WEAK_UNDEF
:
354 case DYN_UNDEF
* 16 + WEAK_UNDEF
:
355 case DYN_WEAK_UNDEF
* 16 + WEAK_UNDEF
:
356 case COMMON
* 16 + WEAK_UNDEF
:
357 case WEAK_COMMON
* 16 + WEAK_UNDEF
:
358 case DYN_COMMON
* 16 + WEAK_UNDEF
:
359 case DYN_WEAK_COMMON
* 16 + WEAK_UNDEF
:
360 // A new weak undefined reference tells us nothing.
363 case DEF
* 16 + DYN_UNDEF
:
364 case WEAK_DEF
* 16 + DYN_UNDEF
:
365 case DYN_DEF
* 16 + DYN_UNDEF
:
366 case DYN_WEAK_DEF
* 16 + DYN_UNDEF
:
367 case UNDEF
* 16 + DYN_UNDEF
:
368 case WEAK_UNDEF
* 16 + DYN_UNDEF
:
369 case DYN_UNDEF
* 16 + DYN_UNDEF
:
370 case DYN_WEAK_UNDEF
* 16 + DYN_UNDEF
:
371 case COMMON
* 16 + DYN_UNDEF
:
372 case WEAK_COMMON
* 16 + DYN_UNDEF
:
373 case DYN_COMMON
* 16 + DYN_UNDEF
:
374 case DYN_WEAK_COMMON
* 16 + DYN_UNDEF
:
375 // A new dynamic undefined reference tells us nothing.
378 case DEF
* 16 + DYN_WEAK_UNDEF
:
379 case WEAK_DEF
* 16 + DYN_WEAK_UNDEF
:
380 case DYN_DEF
* 16 + DYN_WEAK_UNDEF
:
381 case DYN_WEAK_DEF
* 16 + DYN_WEAK_UNDEF
:
382 case UNDEF
* 16 + DYN_WEAK_UNDEF
:
383 case WEAK_UNDEF
* 16 + DYN_WEAK_UNDEF
:
384 case DYN_UNDEF
* 16 + DYN_WEAK_UNDEF
:
385 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_UNDEF
:
386 case COMMON
* 16 + DYN_WEAK_UNDEF
:
387 case WEAK_COMMON
* 16 + DYN_WEAK_UNDEF
:
388 case DYN_COMMON
* 16 + DYN_WEAK_UNDEF
:
389 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_UNDEF
:
390 // A new weak dynamic undefined reference tells us nothing.
393 case DEF
* 16 + COMMON
:
394 // A common symbol does not override a definition.
397 case WEAK_DEF
* 16 + COMMON
:
398 case DYN_DEF
* 16 + COMMON
:
399 case DYN_WEAK_DEF
* 16 + COMMON
:
400 // A common symbol does override a weak definition or a dynamic
402 to
->override(sym
, object
);
405 case UNDEF
* 16 + COMMON
:
406 case WEAK_UNDEF
* 16 + COMMON
:
407 case DYN_UNDEF
* 16 + COMMON
:
408 case DYN_WEAK_UNDEF
* 16 + COMMON
:
409 // A common symbol is a definition for a reference.
410 to
->override(sym
, object
);
413 case COMMON
* 16 + COMMON
:
414 // Set the size to the maximum.
415 if (sym
.get_st_size() > to
->symsize())
416 to
->set_symsize(sym
.get_st_size());
419 case WEAK_COMMON
* 16 + COMMON
:
420 // I'm not sure just what a weak common symbol means, but
421 // presumably it can be overridden by a regular common symbol.
422 to
->override(sym
, object
);
425 case DYN_COMMON
* 16 + COMMON
:
426 case DYN_WEAK_COMMON
* 16 + COMMON
:
428 // Use the real common symbol, but adjust the size if necessary.
429 typename Sized_symbol
<size
>::Size_type symsize
= to
->symsize();
430 to
->override(sym
, object
);
431 if (to
->symsize() < symsize
)
432 to
->set_symsize(symsize
);
436 case DEF
* 16 + WEAK_COMMON
:
437 case WEAK_DEF
* 16 + WEAK_COMMON
:
438 case DYN_DEF
* 16 + WEAK_COMMON
:
439 case DYN_WEAK_DEF
* 16 + WEAK_COMMON
:
440 // Whatever a weak common symbol is, it won't override a
444 case UNDEF
* 16 + WEAK_COMMON
:
445 case WEAK_UNDEF
* 16 + WEAK_COMMON
:
446 case DYN_UNDEF
* 16 + WEAK_COMMON
:
447 case DYN_WEAK_UNDEF
* 16 + WEAK_COMMON
:
448 // A weak common symbol is better than an undefined symbol.
449 to
->override(sym
, object
);
452 case COMMON
* 16 + WEAK_COMMON
:
453 case WEAK_COMMON
* 16 + WEAK_COMMON
:
454 case DYN_COMMON
* 16 + WEAK_COMMON
:
455 case DYN_WEAK_COMMON
* 16 + WEAK_COMMON
:
456 // Ignore a weak common symbol in the presence of a real common
460 case DEF
* 16 + DYN_COMMON
:
461 case WEAK_DEF
* 16 + DYN_COMMON
:
462 case DYN_DEF
* 16 + DYN_COMMON
:
463 case DYN_WEAK_DEF
* 16 + DYN_COMMON
:
464 // Ignore a dynamic common symbol in the presence of a
468 case UNDEF
* 16 + DYN_COMMON
:
469 case WEAK_UNDEF
* 16 + DYN_COMMON
:
470 case DYN_UNDEF
* 16 + DYN_COMMON
:
471 case DYN_WEAK_UNDEF
* 16 + DYN_COMMON
:
472 // A dynamic common symbol is a definition of sorts.
473 to
->override(sym
, object
);
476 case COMMON
* 16 + DYN_COMMON
:
477 case WEAK_COMMON
* 16 + DYN_COMMON
:
478 case DYN_COMMON
* 16 + DYN_COMMON
:
479 case DYN_WEAK_COMMON
* 16 + DYN_COMMON
:
480 // Set the size to the maximum.
481 if (sym
.get_st_size() > to
->symsize())
482 to
->set_symsize(sym
.get_st_size());
485 case DEF
* 16 + DYN_WEAK_COMMON
:
486 case WEAK_DEF
* 16 + DYN_WEAK_COMMON
:
487 case DYN_DEF
* 16 + DYN_WEAK_COMMON
:
488 case DYN_WEAK_DEF
* 16 + DYN_WEAK_COMMON
:
489 // A common symbol is ignored in the face of a definition.
492 case UNDEF
* 16 + DYN_WEAK_COMMON
:
493 case WEAK_UNDEF
* 16 + DYN_WEAK_COMMON
:
494 case DYN_UNDEF
* 16 + DYN_WEAK_COMMON
:
495 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_COMMON
:
496 // I guess a weak common symbol is better than a definition.
497 to
->override(sym
, object
);
500 case COMMON
* 16 + DYN_WEAK_COMMON
:
501 case WEAK_COMMON
* 16 + DYN_WEAK_COMMON
:
502 case DYN_COMMON
* 16 + DYN_WEAK_COMMON
:
503 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_COMMON
:
504 // Set the size to the maximum.
505 if (sym
.get_st_size() > to
->symsize())
506 to
->set_symsize(sym
.get_st_size());
514 // Instantiate the templates we need. We could use the configure
515 // script to restrict this to only the ones needed for implemented
520 Symbol_table::resolve
<32, true>(
521 Sized_symbol
<32>* to
,
522 const elfcpp::Sym
<32, true>& sym
,
527 Symbol_table::resolve
<32, false>(
528 Sized_symbol
<32>* to
,
529 const elfcpp::Sym
<32, false>& sym
,
534 Symbol_table::resolve
<64, true>(
535 Sized_symbol
<64>* to
,
536 const elfcpp::Sym
<64, true>& sym
,
541 Symbol_table::resolve
<64, false>(
542 Sized_symbol
<64>* to
,
543 const elfcpp::Sym
<64, false>& sym
,
546 } // End namespace gold.