daily update
[binutils.git] / gold / resolve.cc
blob1d874863d628588cace76341d8a6bbf8874a19bd
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();
37 // Override the fields in Sized_symbol.
39 template<int size>
40 template<bool big_endian>
41 void
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>
55 void
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);
67 return;
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:
76 enum
78 DEF = 0,
79 WEAK_DEF = 1,
80 DYN_DEF = 2,
81 DYN_WEAK_DEF = 3,
82 UNDEF = 4,
83 WEAK_UNDEF = 5,
84 DYN_UNDEF = 6,
85 DYN_WEAK_UNDEF = 7,
86 COMMON = 8,
87 WEAK_COMMON = 9,
88 DYN_COMMON = 10,
89 DYN_WEAK_COMMON = 11
92 int tobits;
93 switch (to->binding())
95 case elfcpp::STB_GLOBAL:
96 tobits = 0;
97 break;
99 case elfcpp::STB_WEAK:
100 tobits = 1;
101 break;
103 case elfcpp::STB_LOCAL:
104 // We should only see externally visible symbols in the symbol
105 // table.
106 gold_unreachable();
108 default:
109 // Any target which wants to handle STB_LOOS, etc., needs to
110 // define a resolve method.
111 gold_unreachable();
114 if (to->source() == Symbol::FROM_OBJECT
115 && to->object()->is_dynamic())
116 tobits |= (1 << 1);
118 switch (to->shndx())
120 case elfcpp::SHN_UNDEF:
121 tobits |= (1 << 2);
122 break;
124 case elfcpp::SHN_COMMON:
125 tobits |= (2 << 2);
126 break;
128 default:
129 if (to->type() == elfcpp::STT_COMMON)
130 tobits |= (2 << 2);
131 break;
134 int frombits;
135 switch (sym.get_st_bind())
137 case elfcpp::STB_GLOBAL:
138 frombits = 0;
139 break;
141 case elfcpp::STB_WEAK:
142 frombits = 1;
143 break;
145 case elfcpp::STB_LOCAL:
146 fprintf(stderr,
147 _("%s: %s: invalid STB_LOCAL symbol %s in external symbols\n"),
148 program_name, object->name().c_str(), to->name());
149 gold_exit(false);
151 default:
152 fprintf(stderr,
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());
156 gold_exit(false);
159 if (!object->is_dynamic())
161 // Record that we've seen this symbol in a regular object.
162 to->set_in_reg();
164 else
166 frombits |= (1 << 1);
168 // Record that we've seen this symbol in a dynamic object.
169 to->set_in_dyn();
172 switch (sym.get_st_shndx())
174 case elfcpp::SHN_UNDEF:
175 frombits |= (1 << 2);
176 break;
178 case elfcpp::SHN_COMMON:
179 frombits |= (2 << 2);
180 break;
182 default:
183 if (sym.get_st_type() == elfcpp::STT_COMMON)
184 frombits |= (2 << 2);
185 break;
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
205 // statement.
207 switch (tobits * 16 + frombits)
209 case DEF * 16 + DEF:
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.
214 return;
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.
224 // FIXME.
225 to->override(sym, object, version);
226 return;
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
233 // object.
234 to->override(sym, object, version);
235 return;
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);
244 return;
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
252 // warning.
253 to->override(sym, object, version);
254 return;
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.
260 return;
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);
267 return;
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);
275 return;
277 case COMMON * 16 + WEAK_DEF:
278 case WEAK_COMMON * 16 + WEAK_DEF:
279 // A weak definition does not override a common definition.
280 return;
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);
287 return;
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.
294 return;
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);
302 return;
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
309 // definition.
310 return;
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
317 // definition.
318 return;
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);
326 return;
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
333 // definition.
334 return;
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.
342 return;
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);
349 return;
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.
356 return;
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.
371 return;
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.
386 return;
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.
401 return;
403 case DEF * 16 + COMMON:
404 // A common symbol does not override a definition.
405 return;
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
411 // definition.
412 to->override(sym, object, version);
413 return;
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);
421 return;
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());
427 return;
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);
433 return;
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);
444 return;
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
451 // definition.
452 return;
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);
460 return;
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
467 // symbol.
468 return;
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
475 // definition.
476 return;
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);
484 return;
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());
493 return;
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.
500 return;
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);
508 return;
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());
517 return;
519 default:
520 gold_unreachable();
524 // Instantiate the templates we need. We could use the configure
525 // script to restrict this to only the ones needed for implemented
526 // targets.
528 template
529 void
530 Symbol_table::resolve<32, true>(
531 Sized_symbol<32>* to,
532 const elfcpp::Sym<32, true>& sym,
533 Object* object,
534 const char* version);
536 template
537 void
538 Symbol_table::resolve<32, false>(
539 Sized_symbol<32>* to,
540 const elfcpp::Sym<32, false>& sym,
541 Object* object,
542 const char* version);
544 template
545 void
546 Symbol_table::resolve<64, true>(
547 Sized_symbol<64>* to,
548 const elfcpp::Sym<64, true>& sym,
549 Object* object,
550 const char* version);
552 template
553 void
554 Symbol_table::resolve<64, false>(
555 Sized_symbol<64>* to,
556 const elfcpp::Sym<64, false>& sym,
557 Object* object,
558 const char* version);
560 } // End namespace gold.