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 assert(this->source_
== FROM_OBJECT
);
23 this->u_
.from_object
.object
= object
;
24 // FIXME: Handle SHN_XINDEX.
25 this->u_
.from_object
.shnum
= 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
->object() != NULL
&& to
->object()->is_dynamic())
114 case elfcpp::SHN_UNDEF
:
118 case elfcpp::SHN_COMMON
:
123 if (to
->type() == elfcpp::STT_COMMON
)
129 switch (sym
.get_st_bind())
131 case elfcpp::STB_GLOBAL
:
135 case elfcpp::STB_WEAK
:
139 case elfcpp::STB_LOCAL
:
141 _("%s: %s: invalid STB_LOCAL symbol %s in external symbols\n"),
142 program_name
, object
->name().c_str(), to
->name());
147 _("%s: %s: unsupported symbol binding %d for symbol %s\n"),
148 program_name
, object
->name().c_str(),
149 static_cast<int>(sym
.get_st_bind()), to
->name());
153 if (object
->is_dynamic())
155 frombits
|= (1 << 1);
157 // Record that we've seen this symbol in a dynamic object.
161 switch (sym
.get_st_shndx())
163 case elfcpp::SHN_UNDEF
:
164 frombits
|= (1 << 2);
167 case elfcpp::SHN_COMMON
:
168 frombits
|= (2 << 2);
172 if (sym
.get_st_type() == elfcpp::STT_COMMON
)
173 frombits
|= (2 << 2);
177 // FIXME: Warn if either but not both of TO and SYM are STT_TLS.
179 // We use a giant switch table for symbol resolution. This code is
180 // unwieldy, but: 1) it is efficient; 2) we definitely handle all
181 // cases; 3) it is easy to change the handling of a particular case.
182 // The alternative would be a series of conditionals, but it is easy
183 // to get the ordering wrong. This could also be done as a table,
184 // but that is no easier to understand than this large switch
187 switch (tobits
* 16 + frombits
)
190 // Two definitions of the same symbol.
191 fprintf(stderr
, "%s: %s: multiple definition of %s\n",
192 program_name
, object
->name().c_str(), to
->name());
193 // FIXME: Report locations. Record that we have seen an error.
196 case WEAK_DEF
* 16 + DEF
:
197 // We've seen a weak definition, and now we see a strong
198 // definition. In the original SVR4 linker, this was treated as
199 // a multiple definition error. In the Solaris linker and the
200 // GNU linker, a weak definition followed by a regular
201 // definition causes the weak definition to be overridden. We
202 // are currently compatible with the GNU linker. In the future
203 // we should add a target specific option to change this.
205 to
->override(sym
, object
);
208 case DYN_DEF
* 16 + DEF
:
209 case DYN_WEAK_DEF
* 16 + DEF
:
210 // We've seen a definition in a dynamic object, and now we see a
211 // definition in a regular object. The definition in the
212 // regular object overrides the definition in the dynamic
214 to
->override(sym
, object
);
217 case UNDEF
* 16 + DEF
:
218 case WEAK_UNDEF
* 16 + DEF
:
219 case DYN_UNDEF
* 16 + DEF
:
220 case DYN_WEAK_UNDEF
* 16 + DEF
:
221 // We've seen an undefined reference, and now we see a
222 // definition. We use the definition.
223 to
->override(sym
, object
);
226 case COMMON
* 16 + DEF
:
227 case WEAK_COMMON
* 16 + DEF
:
228 case DYN_COMMON
* 16 + DEF
:
229 case DYN_WEAK_COMMON
* 16 + DEF
:
230 // We've seen a common symbol and now we see a definition. The
231 // definition overrides. FIXME: We should optionally issue a
233 to
->override(sym
, object
);
236 case DEF
* 16 + WEAK_DEF
:
237 case WEAK_DEF
* 16 + WEAK_DEF
:
238 // We've seen a definition and now we see a weak definition. We
239 // ignore the new weak definition.
242 case DYN_DEF
* 16 + WEAK_DEF
:
243 case DYN_WEAK_DEF
* 16 + WEAK_DEF
:
244 // We've seen a dynamic definition and now we see a regular weak
245 // definition. The regular weak definition overrides.
246 to
->override(sym
, object
);
249 case UNDEF
* 16 + WEAK_DEF
:
250 case WEAK_UNDEF
* 16 + WEAK_DEF
:
251 case DYN_UNDEF
* 16 + WEAK_DEF
:
252 case DYN_WEAK_UNDEF
* 16 + WEAK_DEF
:
253 // A weak definition of a currently undefined symbol.
254 to
->override(sym
, object
);
257 case COMMON
* 16 + WEAK_DEF
:
258 case WEAK_COMMON
* 16 + WEAK_DEF
:
259 // A weak definition does not override a common definition.
262 case DYN_COMMON
* 16 + WEAK_DEF
:
263 case DYN_WEAK_COMMON
* 16 + WEAK_DEF
:
264 // A weak definition does override a definition in a dynamic
265 // object. FIXME: We should optionally issue a warning.
266 to
->override(sym
, object
);
269 case DEF
* 16 + DYN_DEF
:
270 case WEAK_DEF
* 16 + DYN_DEF
:
271 case DYN_DEF
* 16 + DYN_DEF
:
272 case DYN_WEAK_DEF
* 16 + DYN_DEF
:
273 // Ignore a dynamic definition if we already have a definition.
276 case UNDEF
* 16 + DYN_DEF
:
277 case WEAK_UNDEF
* 16 + DYN_DEF
:
278 case DYN_UNDEF
* 16 + DYN_DEF
:
279 case DYN_WEAK_UNDEF
* 16 + DYN_DEF
:
280 // Use a dynamic definition if we have a reference.
281 to
->override(sym
, object
);
284 case COMMON
* 16 + DYN_DEF
:
285 case WEAK_COMMON
* 16 + DYN_DEF
:
286 case DYN_COMMON
* 16 + DYN_DEF
:
287 case DYN_WEAK_COMMON
* 16 + DYN_DEF
:
288 // Ignore a dynamic definition if we already have a common
292 case DEF
* 16 + DYN_WEAK_DEF
:
293 case WEAK_DEF
* 16 + DYN_WEAK_DEF
:
294 case DYN_DEF
* 16 + DYN_WEAK_DEF
:
295 case DYN_WEAK_DEF
* 16 + DYN_WEAK_DEF
:
296 // Ignore a weak dynamic definition if we already have a
300 case UNDEF
* 16 + DYN_WEAK_DEF
:
301 case WEAK_UNDEF
* 16 + DYN_WEAK_DEF
:
302 case DYN_UNDEF
* 16 + DYN_WEAK_DEF
:
303 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_DEF
:
304 // Use a weak dynamic definition if we have a reference.
305 to
->override(sym
, object
);
308 case COMMON
* 16 + DYN_WEAK_DEF
:
309 case WEAK_COMMON
* 16 + DYN_WEAK_DEF
:
310 case DYN_COMMON
* 16 + DYN_WEAK_DEF
:
311 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_DEF
:
312 // Ignore a weak dynamic definition if we already have a common
316 case DEF
* 16 + UNDEF
:
317 case WEAK_DEF
* 16 + UNDEF
:
318 case DYN_DEF
* 16 + UNDEF
:
319 case DYN_WEAK_DEF
* 16 + UNDEF
:
320 case UNDEF
* 16 + UNDEF
:
321 // A new undefined reference tells us nothing.
324 case WEAK_UNDEF
* 16 + UNDEF
:
325 case DYN_UNDEF
* 16 + UNDEF
:
326 case DYN_WEAK_UNDEF
* 16 + UNDEF
:
327 // A strong undef overrides a dynamic or weak undef.
328 to
->override(sym
, object
);
331 case COMMON
* 16 + UNDEF
:
332 case WEAK_COMMON
* 16 + UNDEF
:
333 case DYN_COMMON
* 16 + UNDEF
:
334 case DYN_WEAK_COMMON
* 16 + UNDEF
:
335 // A new undefined reference tells us nothing.
338 case DEF
* 16 + WEAK_UNDEF
:
339 case WEAK_DEF
* 16 + WEAK_UNDEF
:
340 case DYN_DEF
* 16 + WEAK_UNDEF
:
341 case DYN_WEAK_DEF
* 16 + WEAK_UNDEF
:
342 case UNDEF
* 16 + WEAK_UNDEF
:
343 case WEAK_UNDEF
* 16 + WEAK_UNDEF
:
344 case DYN_UNDEF
* 16 + WEAK_UNDEF
:
345 case DYN_WEAK_UNDEF
* 16 + WEAK_UNDEF
:
346 case COMMON
* 16 + WEAK_UNDEF
:
347 case WEAK_COMMON
* 16 + WEAK_UNDEF
:
348 case DYN_COMMON
* 16 + WEAK_UNDEF
:
349 case DYN_WEAK_COMMON
* 16 + WEAK_UNDEF
:
350 // A new weak undefined reference tells us nothing.
353 case DEF
* 16 + DYN_UNDEF
:
354 case WEAK_DEF
* 16 + DYN_UNDEF
:
355 case DYN_DEF
* 16 + DYN_UNDEF
:
356 case DYN_WEAK_DEF
* 16 + DYN_UNDEF
:
357 case UNDEF
* 16 + DYN_UNDEF
:
358 case WEAK_UNDEF
* 16 + DYN_UNDEF
:
359 case DYN_UNDEF
* 16 + DYN_UNDEF
:
360 case DYN_WEAK_UNDEF
* 16 + DYN_UNDEF
:
361 case COMMON
* 16 + DYN_UNDEF
:
362 case WEAK_COMMON
* 16 + DYN_UNDEF
:
363 case DYN_COMMON
* 16 + DYN_UNDEF
:
364 case DYN_WEAK_COMMON
* 16 + DYN_UNDEF
:
365 // A new dynamic undefined reference tells us nothing.
368 case DEF
* 16 + DYN_WEAK_UNDEF
:
369 case WEAK_DEF
* 16 + DYN_WEAK_UNDEF
:
370 case DYN_DEF
* 16 + DYN_WEAK_UNDEF
:
371 case DYN_WEAK_DEF
* 16 + DYN_WEAK_UNDEF
:
372 case UNDEF
* 16 + DYN_WEAK_UNDEF
:
373 case WEAK_UNDEF
* 16 + DYN_WEAK_UNDEF
:
374 case DYN_UNDEF
* 16 + DYN_WEAK_UNDEF
:
375 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_UNDEF
:
376 case COMMON
* 16 + DYN_WEAK_UNDEF
:
377 case WEAK_COMMON
* 16 + DYN_WEAK_UNDEF
:
378 case DYN_COMMON
* 16 + DYN_WEAK_UNDEF
:
379 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_UNDEF
:
380 // A new weak dynamic undefined reference tells us nothing.
383 case DEF
* 16 + COMMON
:
384 // A common symbol does not override a definition.
387 case WEAK_DEF
* 16 + COMMON
:
388 case DYN_DEF
* 16 + COMMON
:
389 case DYN_WEAK_DEF
* 16 + COMMON
:
390 // A common symbol does override a weak definition or a dynamic
392 to
->override(sym
, object
);
395 case UNDEF
* 16 + COMMON
:
396 case WEAK_UNDEF
* 16 + COMMON
:
397 case DYN_UNDEF
* 16 + COMMON
:
398 case DYN_WEAK_UNDEF
* 16 + COMMON
:
399 // A common symbol is a definition for a reference.
400 to
->override(sym
, object
);
403 case COMMON
* 16 + COMMON
:
404 // Set the size to the maximum.
405 if (sym
.get_st_size() > to
->symsize())
406 to
->set_symsize(sym
.get_st_size());
409 case WEAK_COMMON
* 16 + COMMON
:
410 // I'm not sure just what a weak common symbol means, but
411 // presumably it can be overridden by a regular common symbol.
412 to
->override(sym
, object
);
415 case DYN_COMMON
* 16 + COMMON
:
416 case DYN_WEAK_COMMON
* 16 + COMMON
:
418 // Use the real common symbol, but adjust the size if necessary.
419 typename Sized_symbol
<size
>::Size_type symsize
= to
->symsize();
420 to
->override(sym
, object
);
421 if (to
->symsize() < symsize
)
422 to
->set_symsize(symsize
);
426 case DEF
* 16 + WEAK_COMMON
:
427 case WEAK_DEF
* 16 + WEAK_COMMON
:
428 case DYN_DEF
* 16 + WEAK_COMMON
:
429 case DYN_WEAK_DEF
* 16 + WEAK_COMMON
:
430 // Whatever a weak common symbol is, it won't override a
434 case UNDEF
* 16 + WEAK_COMMON
:
435 case WEAK_UNDEF
* 16 + WEAK_COMMON
:
436 case DYN_UNDEF
* 16 + WEAK_COMMON
:
437 case DYN_WEAK_UNDEF
* 16 + WEAK_COMMON
:
438 // A weak common symbol is better than an undefined symbol.
439 to
->override(sym
, object
);
442 case COMMON
* 16 + WEAK_COMMON
:
443 case WEAK_COMMON
* 16 + WEAK_COMMON
:
444 case DYN_COMMON
* 16 + WEAK_COMMON
:
445 case DYN_WEAK_COMMON
* 16 + WEAK_COMMON
:
446 // Ignore a weak common symbol in the presence of a real common
450 case DEF
* 16 + DYN_COMMON
:
451 case WEAK_DEF
* 16 + DYN_COMMON
:
452 case DYN_DEF
* 16 + DYN_COMMON
:
453 case DYN_WEAK_DEF
* 16 + DYN_COMMON
:
454 // Ignore a dynamic common symbol in the presence of a
458 case UNDEF
* 16 + DYN_COMMON
:
459 case WEAK_UNDEF
* 16 + DYN_COMMON
:
460 case DYN_UNDEF
* 16 + DYN_COMMON
:
461 case DYN_WEAK_UNDEF
* 16 + DYN_COMMON
:
462 // A dynamic common symbol is a definition of sorts.
463 to
->override(sym
, object
);
466 case COMMON
* 16 + DYN_COMMON
:
467 case WEAK_COMMON
* 16 + DYN_COMMON
:
468 case DYN_COMMON
* 16 + DYN_COMMON
:
469 case DYN_WEAK_COMMON
* 16 + DYN_COMMON
:
470 // Set the size to the maximum.
471 if (sym
.get_st_size() > to
->symsize())
472 to
->set_symsize(sym
.get_st_size());
475 case DEF
* 16 + DYN_WEAK_COMMON
:
476 case WEAK_DEF
* 16 + DYN_WEAK_COMMON
:
477 case DYN_DEF
* 16 + DYN_WEAK_COMMON
:
478 case DYN_WEAK_DEF
* 16 + DYN_WEAK_COMMON
:
479 // A common symbol is ignored in the face of a definition.
482 case UNDEF
* 16 + DYN_WEAK_COMMON
:
483 case WEAK_UNDEF
* 16 + DYN_WEAK_COMMON
:
484 case DYN_UNDEF
* 16 + DYN_WEAK_COMMON
:
485 case DYN_WEAK_UNDEF
* 16 + DYN_WEAK_COMMON
:
486 // I guess a weak common symbol is better than a definition.
487 to
->override(sym
, object
);
490 case COMMON
* 16 + DYN_WEAK_COMMON
:
491 case WEAK_COMMON
* 16 + DYN_WEAK_COMMON
:
492 case DYN_COMMON
* 16 + DYN_WEAK_COMMON
:
493 case DYN_WEAK_COMMON
* 16 + DYN_WEAK_COMMON
:
494 // Set the size to the maximum.
495 if (sym
.get_st_size() > to
->symsize())
496 to
->set_symsize(sym
.get_st_size());
504 // Instantiate the templates we need. We could use the configure
505 // script to restrict this to only the ones needed for implemented
510 Symbol_table::resolve
<32, true>(
511 Sized_symbol
<32>* to
,
512 const elfcpp::Sym
<32, true>& sym
,
517 Symbol_table::resolve
<32, false>(
518 Sized_symbol
<32>* to
,
519 const elfcpp::Sym
<32, false>& sym
,
524 Symbol_table::resolve
<64, true>(
525 Sized_symbol
<64>* to
,
526 const elfcpp::Sym
<64, true>& sym
,
531 Symbol_table::resolve
<64, false>(
532 Sized_symbol
<64>* to
,
533 const elfcpp::Sym
<64, false>& sym
,
536 } // End namespace gold.