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 if ((tobits & (1 << 1)) != (frombits & (1 << 1)))
194 // This symbol is seen in both a dynamic object and a regular
195 // object. That means that we need the symbol to go into the
196 // dynamic symbol table, so that the dynamic linker can use the
197 // regular symbol to override or define the dynamic symbol.
198 to->set_needs_dynsym_entry();
201 // FIXME: Warn if either but not both of TO and SYM are STT_TLS.
203 // We use a giant switch table for symbol resolution. This code is
204 // unwieldy, but: 1) it is efficient; 2) we definitely handle all
205 // cases; 3) it is easy to change the handling of a particular case.
206 // The alternative would be a series of conditionals, but it is easy
207 // to get the ordering wrong. This could also be done as a table,
208 // but that is no easier to understand than this large switch
211 switch (tobits * 16 + frombits)
214 // Two definitions of the same symbol.
215 fprintf(stderr, "%s: %s: multiple definition of %s\n",
216 program_name, object->name().c_str(), to->name());
217 // FIXME: Report locations. Record that we have seen an error.
220 case WEAK_DEF * 16 + DEF:
221 // We've seen a weak definition, and now we see a strong
222 // definition. In the original SVR4 linker, this was treated as
223 // a multiple definition error. In the Solaris linker and the
224 // GNU linker, a weak definition followed by a regular
225 // definition causes the weak definition to be overridden. We
226 // are currently compatible with the GNU linker. In the future
227 // we should add a target specific option to change this.
229 to->override(sym, object, version);
232 case DYN_DEF * 16 + DEF:
233 case DYN_WEAK_DEF * 16 + DEF:
234 // We've seen a definition in a dynamic object, and now we see a
235 // definition in a regular object. The definition in the
236 // regular object overrides the definition in the dynamic
238 to->override(sym, object, version);
241 case UNDEF * 16 + DEF:
242 case WEAK_UNDEF * 16 + DEF:
243 case DYN_UNDEF * 16 + DEF:
244 case DYN_WEAK_UNDEF * 16 + DEF:
245 // We've seen an undefined reference, and now we see a
246 // definition. We use the definition.
247 to->override(sym, object, version);
250 case COMMON * 16 + DEF:
251 case WEAK_COMMON * 16 + DEF:
252 case DYN_COMMON * 16 + DEF:
253 case DYN_WEAK_COMMON * 16 + DEF:
254 // We've seen a common symbol and now we see a definition. The
255 // definition overrides. FIXME: We should optionally issue, version a
257 to->override(sym, object, version);
260 case DEF * 16 + WEAK_DEF:
261 case WEAK_DEF * 16 + WEAK_DEF:
262 // We've seen a definition and now we see a weak definition. We
263 // ignore the new weak definition.
266 case DYN_DEF * 16 + WEAK_DEF:
267 case DYN_WEAK_DEF * 16 + WEAK_DEF:
268 // We've seen a dynamic definition and now we see a regular weak
269 // definition. The regular weak definition overrides.
270 to->override(sym, object, version);
273 case UNDEF * 16 + WEAK_DEF:
274 case WEAK_UNDEF * 16 + WEAK_DEF:
275 case DYN_UNDEF * 16 + WEAK_DEF:
276 case DYN_WEAK_UNDEF * 16 + WEAK_DEF:
277 // A weak definition of a currently undefined symbol.
278 to->override(sym, object, version);
281 case COMMON * 16 + WEAK_DEF:
282 case WEAK_COMMON * 16 + WEAK_DEF:
283 // A weak definition does not override a common definition.
286 case DYN_COMMON * 16 + WEAK_DEF:
287 case DYN_WEAK_COMMON * 16 + WEAK_DEF:
288 // A weak definition does override a definition in a dynamic
289 // object. FIXME: We should optionally issue a warning.
290 to->override(sym, object, version);
293 case DEF * 16 + DYN_DEF:
294 case WEAK_DEF * 16 + DYN_DEF:
295 case DYN_DEF * 16 + DYN_DEF:
296 case DYN_WEAK_DEF * 16 + DYN_DEF:
297 // Ignore a dynamic definition if we already have a definition.
300 case UNDEF * 16 + DYN_DEF:
301 case WEAK_UNDEF * 16 + DYN_DEF:
302 case DYN_UNDEF * 16 + DYN_DEF:
303 case DYN_WEAK_UNDEF * 16 + DYN_DEF:
304 // Use a dynamic definition if we have a reference.
305 to->override(sym, object, version);
308 case COMMON * 16 + DYN_DEF:
309 case WEAK_COMMON * 16 + DYN_DEF:
310 case DYN_COMMON * 16 + DYN_DEF:
311 case DYN_WEAK_COMMON * 16 + DYN_DEF:
312 // Ignore a dynamic definition if we already have a common
316 case DEF * 16 + DYN_WEAK_DEF:
317 case WEAK_DEF * 16 + DYN_WEAK_DEF:
318 case DYN_DEF * 16 + DYN_WEAK_DEF:
319 case DYN_WEAK_DEF * 16 + DYN_WEAK_DEF:
320 // Ignore a weak dynamic definition if we already have a
324 case UNDEF * 16 + DYN_WEAK_DEF:
325 case WEAK_UNDEF * 16 + DYN_WEAK_DEF:
326 case DYN_UNDEF * 16 + DYN_WEAK_DEF:
327 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_DEF:
328 // Use a weak dynamic definition if we have a reference.
329 to->override(sym, object, version);
332 case COMMON * 16 + DYN_WEAK_DEF:
333 case WEAK_COMMON * 16 + DYN_WEAK_DEF:
334 case DYN_COMMON * 16 + DYN_WEAK_DEF:
335 case DYN_WEAK_COMMON * 16 + DYN_WEAK_DEF:
336 // Ignore a weak dynamic definition if we already have a common
340 case DEF * 16 + UNDEF:
341 case WEAK_DEF * 16 + UNDEF:
342 case DYN_DEF * 16 + UNDEF:
343 case DYN_WEAK_DEF * 16 + UNDEF:
344 case UNDEF * 16 + UNDEF:
345 // A new undefined reference tells us nothing.
348 case WEAK_UNDEF * 16 + UNDEF:
349 case DYN_UNDEF * 16 + UNDEF:
350 case DYN_WEAK_UNDEF * 16 + UNDEF:
351 // A strong undef overrides a dynamic or weak undef.
352 to->override(sym, object, version);
355 case COMMON * 16 + UNDEF:
356 case WEAK_COMMON * 16 + UNDEF:
357 case DYN_COMMON * 16 + UNDEF:
358 case DYN_WEAK_COMMON * 16 + UNDEF:
359 // A new undefined reference tells us nothing.
362 case DEF * 16 + WEAK_UNDEF:
363 case WEAK_DEF * 16 + WEAK_UNDEF:
364 case DYN_DEF * 16 + WEAK_UNDEF:
365 case DYN_WEAK_DEF * 16 + WEAK_UNDEF:
366 case UNDEF * 16 + WEAK_UNDEF:
367 case WEAK_UNDEF * 16 + WEAK_UNDEF:
368 case DYN_UNDEF * 16 + WEAK_UNDEF:
369 case DYN_WEAK_UNDEF * 16 + WEAK_UNDEF:
370 case COMMON * 16 + WEAK_UNDEF:
371 case WEAK_COMMON * 16 + WEAK_UNDEF:
372 case DYN_COMMON * 16 + WEAK_UNDEF:
373 case DYN_WEAK_COMMON * 16 + WEAK_UNDEF:
374 // A new weak undefined reference tells us nothing.
377 case DEF * 16 + DYN_UNDEF:
378 case WEAK_DEF * 16 + DYN_UNDEF:
379 case DYN_DEF * 16 + DYN_UNDEF:
380 case DYN_WEAK_DEF * 16 + DYN_UNDEF:
381 case UNDEF * 16 + DYN_UNDEF:
382 case WEAK_UNDEF * 16 + DYN_UNDEF:
383 case DYN_UNDEF * 16 + DYN_UNDEF:
384 case DYN_WEAK_UNDEF * 16 + DYN_UNDEF:
385 case COMMON * 16 + DYN_UNDEF:
386 case WEAK_COMMON * 16 + DYN_UNDEF:
387 case DYN_COMMON * 16 + DYN_UNDEF:
388 case DYN_WEAK_COMMON * 16 + DYN_UNDEF:
389 // A new dynamic undefined reference tells us nothing.
392 case DEF * 16 + DYN_WEAK_UNDEF:
393 case WEAK_DEF * 16 + DYN_WEAK_UNDEF:
394 case DYN_DEF * 16 + DYN_WEAK_UNDEF:
395 case DYN_WEAK_DEF * 16 + DYN_WEAK_UNDEF:
396 case UNDEF * 16 + DYN_WEAK_UNDEF:
397 case WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
398 case DYN_UNDEF * 16 + DYN_WEAK_UNDEF:
399 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
400 case COMMON * 16 + DYN_WEAK_UNDEF:
401 case WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
402 case DYN_COMMON * 16 + DYN_WEAK_UNDEF:
403 case DYN_WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
404 // A new weak dynamic undefined reference tells us nothing.
407 case DEF * 16 + COMMON:
408 // A common symbol does not override a definition.
411 case WEAK_DEF * 16 + COMMON:
412 case DYN_DEF * 16 + COMMON:
413 case DYN_WEAK_DEF * 16 + COMMON:
414 // A common symbol does override a weak definition or a dynamic
416 to->override(sym, object, version);
419 case UNDEF * 16 + COMMON:
420 case WEAK_UNDEF * 16 + COMMON:
421 case DYN_UNDEF * 16 + COMMON:
422 case DYN_WEAK_UNDEF * 16 + COMMON:
423 // A common symbol is a definition for a reference.
424 to->override(sym, object, version);
427 case COMMON * 16 + COMMON:
428 // Set the size to the maximum.
429 if (sym.get_st_size() > to->symsize())
430 to->set_symsize(sym.get_st_size());
433 case WEAK_COMMON * 16 + COMMON:
434 // I'm not sure just what a weak common symbol means, but
435 // presumably it can be overridden by a regular common symbol.
436 to->override(sym, object, version);
439 case DYN_COMMON * 16 + COMMON:
440 case DYN_WEAK_COMMON * 16 + COMMON:
442 // Use the real common symbol, but adjust the size if necessary.
443 typename Sized_symbol<size>::Size_type symsize = to->symsize();
444 to->override(sym, object, version);
445 if (to->symsize() < symsize)
446 to->set_symsize(symsize);
450 case DEF * 16 + WEAK_COMMON:
451 case WEAK_DEF * 16 + WEAK_COMMON:
452 case DYN_DEF * 16 + WEAK_COMMON:
453 case DYN_WEAK_DEF * 16 + WEAK_COMMON:
454 // Whatever a weak common symbol is, it won't override a
458 case UNDEF * 16 + WEAK_COMMON:
459 case WEAK_UNDEF * 16 + WEAK_COMMON:
460 case DYN_UNDEF * 16 + WEAK_COMMON:
461 case DYN_WEAK_UNDEF * 16 + WEAK_COMMON:
462 // A weak common symbol is better than an undefined symbol.
463 to->override(sym, object, version);
466 case COMMON * 16 + WEAK_COMMON:
467 case WEAK_COMMON * 16 + WEAK_COMMON:
468 case DYN_COMMON * 16 + WEAK_COMMON:
469 case DYN_WEAK_COMMON * 16 + WEAK_COMMON:
470 // Ignore a weak common symbol in the presence of a real common
474 case DEF * 16 + DYN_COMMON:
475 case WEAK_DEF * 16 + DYN_COMMON:
476 case DYN_DEF * 16 + DYN_COMMON:
477 case DYN_WEAK_DEF * 16 + DYN_COMMON:
478 // Ignore a dynamic common symbol in the presence of a
482 case UNDEF * 16 + DYN_COMMON:
483 case WEAK_UNDEF * 16 + DYN_COMMON:
484 case DYN_UNDEF * 16 + DYN_COMMON:
485 case DYN_WEAK_UNDEF * 16 + DYN_COMMON:
486 // A dynamic common symbol is a definition of sorts.
487 to->override(sym, object, version);
490 case COMMON * 16 + DYN_COMMON:
491 case WEAK_COMMON * 16 + DYN_COMMON:
492 case DYN_COMMON * 16 + DYN_COMMON:
493 case DYN_WEAK_COMMON * 16 + DYN_COMMON:
494 // Set the size to the maximum.
495 if (sym.get_st_size() > to->symsize())
496 to->set_symsize(sym.get_st_size());
499 case DEF * 16 + DYN_WEAK_COMMON:
500 case WEAK_DEF * 16 + DYN_WEAK_COMMON:
501 case DYN_DEF * 16 + DYN_WEAK_COMMON:
502 case DYN_WEAK_DEF * 16 + DYN_WEAK_COMMON:
503 // A common symbol is ignored in the face of a definition.
506 case UNDEF * 16 + DYN_WEAK_COMMON:
507 case WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
508 case DYN_UNDEF * 16 + DYN_WEAK_COMMON:
509 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
510 // I guess a weak common symbol is better than a definition.
511 to->override(sym, object, version);
514 case COMMON * 16 + DYN_WEAK_COMMON:
515 case WEAK_COMMON * 16 + DYN_WEAK_COMMON:
516 case DYN_COMMON * 16 + DYN_WEAK_COMMON:
517 case DYN_WEAK_COMMON * 16 + DYN_WEAK_COMMON:
518 // Set the size to the maximum.
519 if (sym.get_st_size() > to->symsize())
520 to->set_symsize(sym.get_st_size());
528 // Instantiate the templates we need. We could use the configure
529 // script to restrict this to only the ones needed for implemented
534 Symbol_table::resolve<32, true>(
535 Sized_symbol<32>* to,
536 const elfcpp::Sym<32, true>& sym,
538 const char* version);
542 Symbol_table::resolve<32, false>(
543 Sized_symbol<32>* to,
544 const elfcpp::Sym<32, false>& sym,
546 const char* version);
550 Symbol_table::resolve<64, true>(
551 Sized_symbol<64>* to,
552 const elfcpp::Sym<64, true>& sym,
554 const char* version);
558 Symbol_table::resolve<64, false>(
559 Sized_symbol<64>* to,
560 const elfcpp::Sym<64, false>& sym,
562 const char* version);
564 } // End namespace gold.