2006-09-08 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gold / resolve.cc
blob4a9b35567f749051a55c4d1a49b9212ee968df8a
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)
22 this->object_ = object;
23 this->shnum_ = sym.get_st_shndx(); // FIXME: Handle SHN_XINDEX.
24 this->type_ = sym.get_st_type();
25 this->binding_ = sym.get_st_bind();
26 this->visibility_ = sym.get_st_visibility();
27 this->other_ = sym.get_st_nonvis();
30 // Override the fields in Sized_symbol.
32 template<int size>
33 template<bool big_endian>
34 void
35 Sized_symbol<size>::override(const elfcpp::Sym<size, big_endian>& sym,
36 Object* object)
38 this->override_base(sym, object);
39 this->value_ = sym.get_st_value();
40 this->size_ = sym.get_st_size();
43 // Resolve a symbol. This is called the second and subsequent times
44 // we see a symbol. TO is the pre-existing symbol. SYM is the new
45 // symbol, seen in OBJECT.
47 template<int size, bool big_endian>
48 void
49 Symbol_table::resolve(Sized_symbol<size>* to,
50 const elfcpp::Sym<size, big_endian>& sym,
51 Object* object)
53 if (object->target()->has_resolve())
55 object->sized_target<size, big_endian>()->resolve(to, sym, object);
56 return;
59 // Build a little code for each symbol.
60 // Bit 0: 0 for global, 1 for weak.
61 // Bit 1: 0 for regular object, 1 for shared object
62 // Bits 2-3: 0 for normal, 1 for undefined, 2 for common
63 // This gives us values from 0 to 11:
65 enum
67 DEF = 0,
68 WEAK_DEF = 1,
69 DYN_DEF = 2,
70 DYN_WEAK_DEF = 3,
71 UNDEF = 4,
72 WEAK_UNDEF = 5,
73 DYN_UNDEF = 6,
74 DYN_WEAK_UNDEF = 7,
75 COMMON = 8,
76 WEAK_COMMON = 9,
77 DYN_COMMON = 10,
78 DYN_WEAK_COMMON = 11
81 int tobits;
82 switch (to->binding())
84 case elfcpp::STB_GLOBAL:
85 tobits = 0;
86 break;
88 case elfcpp::STB_WEAK:
89 tobits = 1;
90 break;
92 case elfcpp::STB_LOCAL:
93 // We should only see externally visible symbols in the symbol
94 // table.
95 abort();
97 default:
98 // Any target which wants to handle STB_LOOS, etc., needs to
99 // define a resolve method.
100 abort();
103 if (to->object() != NULL && to->object()->is_dynamic())
104 tobits |= (1 << 1);
106 switch (to->shnum())
108 case elfcpp::SHN_UNDEF:
109 tobits |= (1 << 2);
110 break;
112 case elfcpp::SHN_COMMON:
113 tobits |= (2 << 2);
114 break;
116 default:
117 if (to->type() == elfcpp::STT_COMMON)
118 tobits |= (2 << 2);
119 break;
122 int frombits;
123 switch (sym.get_st_bind())
125 case elfcpp::STB_GLOBAL:
126 frombits = 0;
127 break;
129 case elfcpp::STB_WEAK:
130 frombits = 1;
131 break;
133 case elfcpp::STB_LOCAL:
134 fprintf(stderr,
135 _("%s: %s: invalid STB_LOCAL symbol %s in external symbols\n"),
136 program_name, object->name().c_str(), to->name());
137 gold_exit(false);
139 default:
140 fprintf(stderr,
141 _("%s: %s: unsupported symbol binding %d for symbol %s\n"),
142 program_name, object->name().c_str(),
143 static_cast<int>(sym.get_st_bind()), to->name());
144 gold_exit(false);
147 if (object->is_dynamic())
149 frombits |= (1 << 1);
151 // Record that we've seen this symbol in a dynamic object.
152 to->set_in_dyn();
155 switch (sym.get_st_shndx())
157 case elfcpp::SHN_UNDEF:
158 frombits |= (1 << 2);
159 break;
161 case elfcpp::SHN_COMMON:
162 frombits |= (2 << 2);
163 break;
165 default:
166 if (sym.get_st_type() == elfcpp::STT_COMMON)
167 frombits |= (2 << 2);
168 break;
171 // FIXME: Warn if either but not both of TO and SYM are STT_TLS.
173 // We use a giant switch table for symbol resolution. This code is
174 // unwieldy, but: 1) it is efficient; 2) we definitely handle all
175 // cases; 3) it is easy to change the handling of a particular case.
176 // The alternative would be a series of conditionals, but it is easy
177 // to get the ordering wrong. This could also be done as a table,
178 // but that is no easier to understand than this large switch
179 // statement.
181 switch (tobits * 16 + frombits)
183 case DEF * 16 + DEF:
184 // Two definitions of the same symbol.
185 fprintf(stderr, "%s: %s: multiple definition of %s\n",
186 program_name, object->name().c_str(), to->name());
187 // FIXME: Report locations. Record that we have seen an error.
188 return;
190 case WEAK_DEF * 16 + DEF:
191 // We've seen a weak definition, and now we see a strong
192 // definition. In the original SVR4 linker, this was treated as
193 // a multiple definition error. In the Solaris linker and the
194 // GNU linker, a weak definition followed by a regular
195 // definition causes the weak definition to be overridden. We
196 // are currently compatible with the GNU linker. In the future
197 // we should add a target specific option to change this.
198 // FIXME.
199 to->override(sym, object);
200 return;
202 case DYN_DEF * 16 + DEF:
203 case DYN_WEAK_DEF * 16 + DEF:
204 // We've seen a definition in a dynamic object, and now we see a
205 // definition in a regular object. The definition in the
206 // regular object overrides the definition in the dynamic
207 // object.
208 to->override(sym, object);
209 return;
211 case UNDEF * 16 + DEF:
212 case WEAK_UNDEF * 16 + DEF:
213 case DYN_UNDEF * 16 + DEF:
214 case DYN_WEAK_UNDEF * 16 + DEF:
215 // We've seen an undefined reference, and now we see a
216 // definition. We use the definition.
217 to->override(sym, object);
218 return;
220 case COMMON * 16 + DEF:
221 case WEAK_COMMON * 16 + DEF:
222 case DYN_COMMON * 16 + DEF:
223 case DYN_WEAK_COMMON * 16 + DEF:
224 // We've seen a common symbol and now we see a definition. The
225 // definition overrides. FIXME: We should optionally issue a
226 // warning.
227 to->override(sym, object);
228 return;
230 case DEF * 16 + WEAK_DEF:
231 case WEAK_DEF * 16 + WEAK_DEF:
232 // We've seen a definition and now we see a weak definition. We
233 // ignore the new weak definition.
234 return;
236 case DYN_DEF * 16 + WEAK_DEF:
237 case DYN_WEAK_DEF * 16 + WEAK_DEF:
238 // We've seen a dynamic definition and now we see a regular weak
239 // definition. The regular weak definition overrides.
240 to->override(sym, object);
241 return;
243 case UNDEF * 16 + WEAK_DEF:
244 case WEAK_UNDEF * 16 + WEAK_DEF:
245 case DYN_UNDEF * 16 + WEAK_DEF:
246 case DYN_WEAK_UNDEF * 16 + WEAK_DEF:
247 // A weak definition of a currently undefined symbol.
248 to->override(sym, object);
249 return;
251 case COMMON * 16 + WEAK_DEF:
252 case WEAK_COMMON * 16 + WEAK_DEF:
253 // A weak definition does not override a common definition.
254 return;
256 case DYN_COMMON * 16 + WEAK_DEF:
257 case DYN_WEAK_COMMON * 16 + WEAK_DEF:
258 // A weak definition does override a definition in a dynamic
259 // object. FIXME: We should optionally issue a warning.
260 to->override(sym, object);
261 return;
263 case DEF * 16 + DYN_DEF:
264 case WEAK_DEF * 16 + DYN_DEF:
265 case DYN_DEF * 16 + DYN_DEF:
266 case DYN_WEAK_DEF * 16 + DYN_DEF:
267 // Ignore a dynamic definition if we already have a definition.
268 return;
270 case UNDEF * 16 + DYN_DEF:
271 case WEAK_UNDEF * 16 + DYN_DEF:
272 case DYN_UNDEF * 16 + DYN_DEF:
273 case DYN_WEAK_UNDEF * 16 + DYN_DEF:
274 // Use a dynamic definition if we have a reference.
275 to->override(sym, object);
276 return;
278 case COMMON * 16 + DYN_DEF:
279 case WEAK_COMMON * 16 + DYN_DEF:
280 case DYN_COMMON * 16 + DYN_DEF:
281 case DYN_WEAK_COMMON * 16 + DYN_DEF:
282 // Ignore a dynamic definition if we already have a common
283 // definition.
284 return;
286 case DEF * 16 + DYN_WEAK_DEF:
287 case WEAK_DEF * 16 + DYN_WEAK_DEF:
288 case DYN_DEF * 16 + DYN_WEAK_DEF:
289 case DYN_WEAK_DEF * 16 + DYN_WEAK_DEF:
290 // Ignore a weak dynamic definition if we already have a
291 // definition.
292 return;
294 case UNDEF * 16 + DYN_WEAK_DEF:
295 case WEAK_UNDEF * 16 + DYN_WEAK_DEF:
296 case DYN_UNDEF * 16 + DYN_WEAK_DEF:
297 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_DEF:
298 // Use a weak dynamic definition if we have a reference.
299 to->override(sym, object);
300 return;
302 case COMMON * 16 + DYN_WEAK_DEF:
303 case WEAK_COMMON * 16 + DYN_WEAK_DEF:
304 case DYN_COMMON * 16 + DYN_WEAK_DEF:
305 case DYN_WEAK_COMMON * 16 + DYN_WEAK_DEF:
306 // Ignore a weak dynamic definition if we already have a common
307 // definition.
308 return;
310 case DEF * 16 + UNDEF:
311 case WEAK_DEF * 16 + UNDEF:
312 case DYN_DEF * 16 + UNDEF:
313 case DYN_WEAK_DEF * 16 + UNDEF:
314 case UNDEF * 16 + UNDEF:
315 case WEAK_UNDEF * 16 + UNDEF:
316 case DYN_UNDEF * 16 + UNDEF:
317 case DYN_WEAK_UNDEF * 16 + UNDEF:
318 case COMMON * 16 + UNDEF:
319 case WEAK_COMMON * 16 + UNDEF:
320 case DYN_COMMON * 16 + UNDEF:
321 case DYN_WEAK_COMMON * 16 + UNDEF:
322 // A new undefined reference tells us nothing.
323 return;
325 case DEF * 16 + WEAK_UNDEF:
326 case WEAK_DEF * 16 + WEAK_UNDEF:
327 case DYN_DEF * 16 + WEAK_UNDEF:
328 case DYN_WEAK_DEF * 16 + WEAK_UNDEF:
329 case UNDEF * 16 + WEAK_UNDEF:
330 case WEAK_UNDEF * 16 + WEAK_UNDEF:
331 case DYN_UNDEF * 16 + WEAK_UNDEF:
332 case DYN_WEAK_UNDEF * 16 + WEAK_UNDEF:
333 case COMMON * 16 + WEAK_UNDEF:
334 case WEAK_COMMON * 16 + WEAK_UNDEF:
335 case DYN_COMMON * 16 + WEAK_UNDEF:
336 case DYN_WEAK_COMMON * 16 + WEAK_UNDEF:
337 // A new weak undefined reference tells us nothing.
338 return;
340 case DEF * 16 + DYN_UNDEF:
341 case WEAK_DEF * 16 + DYN_UNDEF:
342 case DYN_DEF * 16 + DYN_UNDEF:
343 case DYN_WEAK_DEF * 16 + DYN_UNDEF:
344 case UNDEF * 16 + DYN_UNDEF:
345 case WEAK_UNDEF * 16 + DYN_UNDEF:
346 case DYN_UNDEF * 16 + DYN_UNDEF:
347 case DYN_WEAK_UNDEF * 16 + DYN_UNDEF:
348 case COMMON * 16 + DYN_UNDEF:
349 case WEAK_COMMON * 16 + DYN_UNDEF:
350 case DYN_COMMON * 16 + DYN_UNDEF:
351 case DYN_WEAK_COMMON * 16 + DYN_UNDEF:
352 // A new dynamic undefined reference tells us nothing.
353 return;
355 case DEF * 16 + DYN_WEAK_UNDEF:
356 case WEAK_DEF * 16 + DYN_WEAK_UNDEF:
357 case DYN_DEF * 16 + DYN_WEAK_UNDEF:
358 case DYN_WEAK_DEF * 16 + DYN_WEAK_UNDEF:
359 case UNDEF * 16 + DYN_WEAK_UNDEF:
360 case WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
361 case DYN_UNDEF * 16 + DYN_WEAK_UNDEF:
362 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
363 case COMMON * 16 + DYN_WEAK_UNDEF:
364 case WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
365 case DYN_COMMON * 16 + DYN_WEAK_UNDEF:
366 case DYN_WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
367 // A new weak dynamic undefined reference tells us nothing.
368 return;
370 case DEF * 16 + COMMON:
371 // A common symbol does not override a definition.
372 return;
374 case WEAK_DEF * 16 + COMMON:
375 case DYN_DEF * 16 + COMMON:
376 case DYN_WEAK_DEF * 16 + COMMON:
377 // A common symbol does override a weak definition or a dynamic
378 // definition.
379 to->override(sym, object);
380 return;
382 case UNDEF * 16 + COMMON:
383 case WEAK_UNDEF * 16 + COMMON:
384 case DYN_UNDEF * 16 + COMMON:
385 case DYN_WEAK_UNDEF * 16 + COMMON:
386 // A common symbol is a definition for a reference.
387 to->override(sym, object);
388 return;
390 case COMMON * 16 + COMMON:
391 case WEAK_COMMON * 16 + COMMON:
392 case DYN_COMMON * 16 + COMMON:
393 case DYN_WEAK_COMMON * 16 + COMMON:
395 case DEF * 16 + WEAK_COMMON:
396 case WEAK_DEF * 16 + WEAK_COMMON:
397 case DYN_DEF * 16 + WEAK_COMMON:
398 case DYN_WEAK_DEF * 16 + WEAK_COMMON:
399 case UNDEF * 16 + WEAK_COMMON:
400 case WEAK_UNDEF * 16 + WEAK_COMMON:
401 case DYN_UNDEF * 16 + WEAK_COMMON:
402 case DYN_WEAK_UNDEF * 16 + WEAK_COMMON:
403 case COMMON * 16 + WEAK_COMMON:
404 case WEAK_COMMON * 16 + WEAK_COMMON:
405 case DYN_COMMON * 16 + WEAK_COMMON:
406 case DYN_WEAK_COMMON * 16 + WEAK_COMMON:
408 case DEF * 16 + DYN_COMMON:
409 case WEAK_DEF * 16 + DYN_COMMON:
410 case DYN_DEF * 16 + DYN_COMMON:
411 case DYN_WEAK_DEF * 16 + DYN_COMMON:
412 case UNDEF * 16 + DYN_COMMON:
413 case WEAK_UNDEF * 16 + DYN_COMMON:
414 case DYN_UNDEF * 16 + DYN_COMMON:
415 case DYN_WEAK_UNDEF * 16 + DYN_COMMON:
416 case COMMON * 16 + DYN_COMMON:
417 case WEAK_COMMON * 16 + DYN_COMMON:
418 case DYN_COMMON * 16 + DYN_COMMON:
419 case DYN_WEAK_COMMON * 16 + DYN_COMMON:
421 case DEF * 16 + DYN_WEAK_COMMON:
422 case WEAK_DEF * 16 + DYN_WEAK_COMMON:
423 case DYN_DEF * 16 + DYN_WEAK_COMMON:
424 case DYN_WEAK_DEF * 16 + DYN_WEAK_COMMON:
425 case UNDEF * 16 + DYN_WEAK_COMMON:
426 case WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
427 case DYN_UNDEF * 16 + DYN_WEAK_COMMON:
428 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
429 case COMMON * 16 + DYN_WEAK_COMMON:
430 case WEAK_COMMON * 16 + DYN_WEAK_COMMON:
431 case DYN_COMMON * 16 + DYN_WEAK_COMMON:
432 case DYN_WEAK_COMMON * 16 + DYN_WEAK_COMMON:
433 abort();
434 break;
436 default:
437 abort();
441 // Instantiate the templates we need. We could use the configure
442 // script to restrict this to only the ones needed for implemented
443 // targets.
445 template
446 void
447 Symbol_table::resolve<32, true>(
448 Sized_symbol<32>* to,
449 const elfcpp::Sym<32, true>& sym,
450 Object* object);
452 template
453 void
454 Symbol_table::resolve<32, false>(
455 Sized_symbol<32>* to,
456 const elfcpp::Sym<32, false>& sym,
457 Object* object);
459 template
460 void
461 Symbol_table::resolve<64, true>(
462 Sized_symbol<64>* to,
463 const elfcpp::Sym<64, true>& sym,
464 Object* object);
466 template
467 void
468 Symbol_table::resolve<64, false>(
469 Sized_symbol<64>* to,
470 const elfcpp::Sym<64, false>& sym,
471 Object* object);
473 } // End namespace gold.