Set in_dyn_ and in_reg_ when overriding a symbol.
[external/binutils.git] / gold / resolve.cc
1 // resolve.cc -- symbol resolution for gold
2
3 #include "gold.h"
4
5 #include "elfcpp.h"
6 #include "target.h"
7 #include "object.h"
8 #include "symtab.h"
9
10 namespace gold
11 {
12
13 // Symbol methods used in this file.
14
15 // Override the fields in Symbol.
16
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)
21 {
22   gold_assert(this->source_ == FROM_OBJECT);
23   this->u_.from_object.object = object;
24   if (version != NULL && this->version() != version)
25     {
26       gold_assert(this->version() == NULL);
27       this->version_ = version;
28     }
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;
39 }
40
41 // Override the fields in Sized_symbol.
42
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)
48 {
49   this->override_base(sym, object, version);
50   this->value_ = sym.get_st_value();
51   this->symsize_ = sym.get_st_size();
52 }
53
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.
57
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)
63 {
64   if (object->target()->has_resolve())
65     {
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;
72     }
73
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:
79
80   enum
81   {
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
94   };
95
96   int tobits;
97   switch (to->binding())
98     {
99     case elfcpp::STB_GLOBAL:
100       tobits = 0;
101       break;
102
103     case elfcpp::STB_WEAK:
104       tobits = 1;
105       break;
106
107     case elfcpp::STB_LOCAL:
108       // We should only see externally visible symbols in the symbol
109       // table.
110       gold_unreachable();
111
112     default:
113       // Any target which wants to handle STB_LOOS, etc., needs to
114       // define a resolve method.
115       gold_unreachable();
116     }
117
118   if (to->source() == Symbol::FROM_OBJECT
119       && to->object()->is_dynamic())
120     tobits |= (1 << 1);
121
122   switch (to->shndx())
123     {
124     case elfcpp::SHN_UNDEF:
125       tobits |= (1 << 2);
126       break;
127
128     case elfcpp::SHN_COMMON:
129       tobits |= (2 << 2);
130       break;
131
132     default:
133       if (to->type() == elfcpp::STT_COMMON)
134         tobits |= (2 << 2);
135       break;
136     }
137
138   int frombits;
139   switch (sym.get_st_bind())
140     {
141     case elfcpp::STB_GLOBAL:
142       frombits = 0;
143       break;
144
145     case elfcpp::STB_WEAK:
146       frombits = 1;
147       break;
148
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);
154
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);
161     }
162
163   if (!object->is_dynamic())
164     {
165       // Record that we've seen this symbol in a regular object.
166       to->set_in_reg();
167     }
168   else
169     {
170       frombits |= (1 << 1);
171
172       // Record that we've seen this symbol in a dynamic object.
173       to->set_in_dyn();
174     }
175
176   switch (sym.get_st_shndx())
177     {
178     case elfcpp::SHN_UNDEF:
179       frombits |= (1 << 2);
180       break;
181
182     case elfcpp::SHN_COMMON:
183       frombits |= (2 << 2);
184       break;
185
186     default:
187       if (sym.get_st_type() == elfcpp::STT_COMMON)
188         frombits |= (2 << 2);
189       break;
190     }
191
192   if ((tobits & (1 << 1)) != (frombits & (1 << 1)))
193     {
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();
199     }
200
201   // FIXME: Warn if either but not both of TO and SYM are STT_TLS.
202
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
209   // statement.
210
211   switch (tobits * 16 + frombits)
212     {
213     case DEF * 16 + DEF:
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.
218       return;
219
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.
228       // FIXME.
229       to->override(sym, object, version);
230       return;
231
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
237       // object.
238       to->override(sym, object, version);
239       return;
240
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);
248       return;
249
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
256       // warning.
257       to->override(sym, object, version);
258       return;
259
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.
264       return;
265
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);
271       return;
272
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);
279       return;
280
281     case COMMON * 16 + WEAK_DEF:
282     case WEAK_COMMON * 16 + WEAK_DEF:
283       // A weak definition does not override a common definition.
284       return;
285
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);
291       return;
292
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.
298       return;
299
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);
306       return;
307
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
313       // definition.
314       return;
315
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
321       // definition.
322       return;
323
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);
330       return;
331
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
337       // definition.
338       return;
339
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.
346       return;
347
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);
353       return;
354
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.
360       return;
361
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.
375       return;
376
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.
390       return;
391
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.
405       return;
406
407     case DEF * 16 + COMMON:
408       // A common symbol does not override a definition.
409       return;
410
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
415       // definition.
416       to->override(sym, object, version);
417       return;
418
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);
425       return;
426
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());
431       return;
432
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);
437       return;
438
439     case DYN_COMMON * 16 + COMMON:
440     case DYN_WEAK_COMMON * 16 + COMMON:
441       {
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);
447       }
448       return;
449
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
455       // definition.
456       return;
457
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);
464       return;
465
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
471       // symbol.
472       return;
473
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
479       // definition.
480       return;
481
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);
488       return;
489
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());
497       return;
498
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.
504       return;
505
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);
512       return;
513
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());
521       return;
522
523     default:
524       gold_unreachable();
525     }
526 }
527
528 // Instantiate the templates we need.  We could use the configure
529 // script to restrict this to only the ones needed for implemented
530 // targets.
531
532 template
533 void
534 Symbol_table::resolve<32, true>(
535     Sized_symbol<32>* to,
536     const elfcpp::Sym<32, true>& sym,
537     Object* object,
538     const char* version);
539
540 template
541 void
542 Symbol_table::resolve<32, false>(
543     Sized_symbol<32>* to,
544     const elfcpp::Sym<32, false>& sym,
545     Object* object,
546     const char* version);
547
548 template
549 void
550 Symbol_table::resolve<64, true>(
551     Sized_symbol<64>* to,
552     const elfcpp::Sym<64, true>& sym,
553     Object* object,
554     const char* version);
555
556 template
557 void
558 Symbol_table::resolve<64, false>(
559     Sized_symbol<64>* to,
560     const elfcpp::Sym<64, false>& sym,
561     Object* object,
562     const char* version);
563
564 } // End namespace gold.