daily update
[binutils.git] / gold / resolve.cc
blobe5ca6e38c9991f3ef713fa5663bd966925b0e644
1 // resolve.cc -- symbol resolution for gold
3 #include "gold.h"
5 #include "elfcpp.h"
6 #include "target.h"
7 #include "object.h"
8 #include "symtab.h"
10 namespace gold
13 // Symbol methods used in this file.
15 // Override the fields in Symbol.
17 template<int size, bool big_endian>
18 void
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())
36 this->in_dyn_ = true;
37 else
38 this->in_reg_ = true;
41 // Override the fields in Sized_symbol.
43 template<int size>
44 template<bool big_endian>
45 void
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>
59 void
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);
71 return;
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:
80 enum
82 DEF = 0,
83 WEAK_DEF = 1,
84 DYN_DEF = 2,
85 DYN_WEAK_DEF = 3,
86 UNDEF = 4,
87 WEAK_UNDEF = 5,
88 DYN_UNDEF = 6,
89 DYN_WEAK_UNDEF = 7,
90 COMMON = 8,
91 WEAK_COMMON = 9,
92 DYN_COMMON = 10,
93 DYN_WEAK_COMMON = 11
96 int tobits;
97 switch (to->binding())
99 case elfcpp::STB_GLOBAL:
100 tobits = 0;
101 break;
103 case elfcpp::STB_WEAK:
104 tobits = 1;
105 break;
107 case elfcpp::STB_LOCAL:
108 // We should only see externally visible symbols in the symbol
109 // table.
110 gold_unreachable();
112 default:
113 // Any target which wants to handle STB_LOOS, etc., needs to
114 // define a resolve method.
115 gold_unreachable();
118 if (to->source() == Symbol::FROM_OBJECT
119 && to->object()->is_dynamic())
120 tobits |= (1 << 1);
122 switch (to->shndx())
124 case elfcpp::SHN_UNDEF:
125 tobits |= (1 << 2);
126 break;
128 case elfcpp::SHN_COMMON:
129 tobits |= (2 << 2);
130 break;
132 default:
133 if (to->type() == elfcpp::STT_COMMON)
134 tobits |= (2 << 2);
135 break;
138 int frombits;
139 switch (sym.get_st_bind())
141 case elfcpp::STB_GLOBAL:
142 frombits = 0;
143 break;
145 case elfcpp::STB_WEAK:
146 frombits = 1;
147 break;
149 case elfcpp::STB_LOCAL:
150 fprintf(stderr,
151 _("%s: %s: invalid STB_LOCAL symbol %s in external symbols\n"),
152 program_name, object->name().c_str(), to->name());
153 gold_exit(false);
155 default:
156 fprintf(stderr,
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());
160 gold_exit(false);
163 if (!object->is_dynamic())
165 // Record that we've seen this symbol in a regular object.
166 to->set_in_reg();
168 else
170 frombits |= (1 << 1);
172 // Record that we've seen this symbol in a dynamic object.
173 to->set_in_dyn();
176 switch (sym.get_st_shndx())
178 case elfcpp::SHN_UNDEF:
179 frombits |= (1 << 2);
180 break;
182 case elfcpp::SHN_COMMON:
183 frombits |= (2 << 2);
184 break;
186 default:
187 if (sym.get_st_type() == elfcpp::STT_COMMON)
188 frombits |= (2 << 2);
189 break;
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
200 // statement.
202 switch (tobits * 16 + frombits)
204 case DEF * 16 + DEF:
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.
209 return;
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.
219 // FIXME.
220 to->override(sym, object, version);
221 return;
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
228 // object.
229 to->override(sym, object, version);
230 return;
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);
239 return;
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
247 // warning.
248 to->override(sym, object, version);
249 return;
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.
255 return;
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);
262 return;
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);
270 return;
272 case COMMON * 16 + WEAK_DEF:
273 case WEAK_COMMON * 16 + WEAK_DEF:
274 // A weak definition does not override a common definition.
275 return;
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);
282 return;
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.
289 return;
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);
297 return;
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
304 // definition.
305 return;
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
312 // definition.
313 return;
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);
321 return;
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
328 // definition.
329 return;
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.
337 return;
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);
344 return;
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.
351 return;
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.
366 return;
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.
381 return;
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.
396 return;
398 case DEF * 16 + COMMON:
399 // A common symbol does not override a definition.
400 return;
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
406 // definition.
407 to->override(sym, object, version);
408 return;
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);
416 return;
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());
422 return;
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);
428 return;
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);
439 return;
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
446 // definition.
447 return;
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);
455 return;
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
462 // symbol.
463 return;
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
470 // definition.
471 return;
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);
479 return;
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());
488 return;
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.
495 return;
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);
503 return;
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());
512 return;
514 default:
515 gold_unreachable();
519 // Instantiate the templates we need. We could use the configure
520 // script to restrict this to only the ones needed for implemented
521 // targets.
523 #ifdef HAVE_TARGET_32_LITTLE
524 template
525 void
526 Symbol_table::resolve<32, false>(
527 Sized_symbol<32>* to,
528 const elfcpp::Sym<32, false>& sym,
529 Object* object,
530 const char* version);
531 #endif
533 #ifdef HAVE_TARGET_32_BIG
534 template
535 void
536 Symbol_table::resolve<32, true>(
537 Sized_symbol<32>* to,
538 const elfcpp::Sym<32, true>& sym,
539 Object* object,
540 const char* version);
541 #endif
543 #ifdef HAVE_TARGET_64_LITTLE
544 template
545 void
546 Symbol_table::resolve<64, false>(
547 Sized_symbol<64>* to,
548 const elfcpp::Sym<64, false>& sym,
549 Object* object,
550 const char* version);
551 #endif
553 #ifdef HAVE_TARGET_64_BIG
554 template
555 void
556 Symbol_table::resolve<64, true>(
557 Sized_symbol<64>* to,
558 const elfcpp::Sym<64, true>& sym,
559 Object* object,
560 const char* version);
561 #endif
563 } // End namespace gold.