ld/
[binutils.git] / gold / resolve.cc
blob6b8199cd8f2201fd87a46df01a9a99badc4a38b4
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 Sized_target<size, big_endian>* sized_target;
56 sized_target = object->sized_target SELECT_SIZE_ENDIAN_NAME (
57 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
58 sized_target->resolve(to, sym, object);
59 return;
62 // Build a little code for each symbol.
63 // Bit 0: 0 for global, 1 for weak.
64 // Bit 1: 0 for regular object, 1 for shared object
65 // Bits 2-3: 0 for normal, 1 for undefined, 2 for common
66 // This gives us values from 0 to 11:
68 enum
70 DEF = 0,
71 WEAK_DEF = 1,
72 DYN_DEF = 2,
73 DYN_WEAK_DEF = 3,
74 UNDEF = 4,
75 WEAK_UNDEF = 5,
76 DYN_UNDEF = 6,
77 DYN_WEAK_UNDEF = 7,
78 COMMON = 8,
79 WEAK_COMMON = 9,
80 DYN_COMMON = 10,
81 DYN_WEAK_COMMON = 11
84 int tobits;
85 switch (to->binding())
87 case elfcpp::STB_GLOBAL:
88 tobits = 0;
89 break;
91 case elfcpp::STB_WEAK:
92 tobits = 1;
93 break;
95 case elfcpp::STB_LOCAL:
96 // We should only see externally visible symbols in the symbol
97 // table.
98 abort();
100 default:
101 // Any target which wants to handle STB_LOOS, etc., needs to
102 // define a resolve method.
103 abort();
106 if (to->object() != NULL && to->object()->is_dynamic())
107 tobits |= (1 << 1);
109 switch (to->shnum())
111 case elfcpp::SHN_UNDEF:
112 tobits |= (1 << 2);
113 break;
115 case elfcpp::SHN_COMMON:
116 tobits |= (2 << 2);
117 break;
119 default:
120 if (to->type() == elfcpp::STT_COMMON)
121 tobits |= (2 << 2);
122 break;
125 int frombits;
126 switch (sym.get_st_bind())
128 case elfcpp::STB_GLOBAL:
129 frombits = 0;
130 break;
132 case elfcpp::STB_WEAK:
133 frombits = 1;
134 break;
136 case elfcpp::STB_LOCAL:
137 fprintf(stderr,
138 _("%s: %s: invalid STB_LOCAL symbol %s in external symbols\n"),
139 program_name, object->name().c_str(), to->name());
140 gold_exit(false);
142 default:
143 fprintf(stderr,
144 _("%s: %s: unsupported symbol binding %d for symbol %s\n"),
145 program_name, object->name().c_str(),
146 static_cast<int>(sym.get_st_bind()), to->name());
147 gold_exit(false);
150 if (object->is_dynamic())
152 frombits |= (1 << 1);
154 // Record that we've seen this symbol in a dynamic object.
155 to->set_in_dyn();
158 switch (sym.get_st_shndx())
160 case elfcpp::SHN_UNDEF:
161 frombits |= (1 << 2);
162 break;
164 case elfcpp::SHN_COMMON:
165 frombits |= (2 << 2);
166 break;
168 default:
169 if (sym.get_st_type() == elfcpp::STT_COMMON)
170 frombits |= (2 << 2);
171 break;
174 // FIXME: Warn if either but not both of TO and SYM are STT_TLS.
176 // We use a giant switch table for symbol resolution. This code is
177 // unwieldy, but: 1) it is efficient; 2) we definitely handle all
178 // cases; 3) it is easy to change the handling of a particular case.
179 // The alternative would be a series of conditionals, but it is easy
180 // to get the ordering wrong. This could also be done as a table,
181 // but that is no easier to understand than this large switch
182 // statement.
184 switch (tobits * 16 + frombits)
186 case DEF * 16 + DEF:
187 // Two definitions of the same symbol.
188 fprintf(stderr, "%s: %s: multiple definition of %s\n",
189 program_name, object->name().c_str(), to->name());
190 // FIXME: Report locations. Record that we have seen an error.
191 return;
193 case WEAK_DEF * 16 + DEF:
194 // We've seen a weak definition, and now we see a strong
195 // definition. In the original SVR4 linker, this was treated as
196 // a multiple definition error. In the Solaris linker and the
197 // GNU linker, a weak definition followed by a regular
198 // definition causes the weak definition to be overridden. We
199 // are currently compatible with the GNU linker. In the future
200 // we should add a target specific option to change this.
201 // FIXME.
202 to->override(sym, object);
203 return;
205 case DYN_DEF * 16 + DEF:
206 case DYN_WEAK_DEF * 16 + DEF:
207 // We've seen a definition in a dynamic object, and now we see a
208 // definition in a regular object. The definition in the
209 // regular object overrides the definition in the dynamic
210 // object.
211 to->override(sym, object);
212 return;
214 case UNDEF * 16 + DEF:
215 case WEAK_UNDEF * 16 + DEF:
216 case DYN_UNDEF * 16 + DEF:
217 case DYN_WEAK_UNDEF * 16 + DEF:
218 // We've seen an undefined reference, and now we see a
219 // definition. We use the definition.
220 to->override(sym, object);
221 return;
223 case COMMON * 16 + DEF:
224 case WEAK_COMMON * 16 + DEF:
225 case DYN_COMMON * 16 + DEF:
226 case DYN_WEAK_COMMON * 16 + DEF:
227 // We've seen a common symbol and now we see a definition. The
228 // definition overrides. FIXME: We should optionally issue a
229 // warning.
230 to->override(sym, object);
231 return;
233 case DEF * 16 + WEAK_DEF:
234 case WEAK_DEF * 16 + WEAK_DEF:
235 // We've seen a definition and now we see a weak definition. We
236 // ignore the new weak definition.
237 return;
239 case DYN_DEF * 16 + WEAK_DEF:
240 case DYN_WEAK_DEF * 16 + WEAK_DEF:
241 // We've seen a dynamic definition and now we see a regular weak
242 // definition. The regular weak definition overrides.
243 to->override(sym, object);
244 return;
246 case UNDEF * 16 + WEAK_DEF:
247 case WEAK_UNDEF * 16 + WEAK_DEF:
248 case DYN_UNDEF * 16 + WEAK_DEF:
249 case DYN_WEAK_UNDEF * 16 + WEAK_DEF:
250 // A weak definition of a currently undefined symbol.
251 to->override(sym, object);
252 return;
254 case COMMON * 16 + WEAK_DEF:
255 case WEAK_COMMON * 16 + WEAK_DEF:
256 // A weak definition does not override a common definition.
257 return;
259 case DYN_COMMON * 16 + WEAK_DEF:
260 case DYN_WEAK_COMMON * 16 + WEAK_DEF:
261 // A weak definition does override a definition in a dynamic
262 // object. FIXME: We should optionally issue a warning.
263 to->override(sym, object);
264 return;
266 case DEF * 16 + DYN_DEF:
267 case WEAK_DEF * 16 + DYN_DEF:
268 case DYN_DEF * 16 + DYN_DEF:
269 case DYN_WEAK_DEF * 16 + DYN_DEF:
270 // Ignore a dynamic definition if we already have a definition.
271 return;
273 case UNDEF * 16 + DYN_DEF:
274 case WEAK_UNDEF * 16 + DYN_DEF:
275 case DYN_UNDEF * 16 + DYN_DEF:
276 case DYN_WEAK_UNDEF * 16 + DYN_DEF:
277 // Use a dynamic definition if we have a reference.
278 to->override(sym, object);
279 return;
281 case COMMON * 16 + DYN_DEF:
282 case WEAK_COMMON * 16 + DYN_DEF:
283 case DYN_COMMON * 16 + DYN_DEF:
284 case DYN_WEAK_COMMON * 16 + DYN_DEF:
285 // Ignore a dynamic definition if we already have a common
286 // definition.
287 return;
289 case DEF * 16 + DYN_WEAK_DEF:
290 case WEAK_DEF * 16 + DYN_WEAK_DEF:
291 case DYN_DEF * 16 + DYN_WEAK_DEF:
292 case DYN_WEAK_DEF * 16 + DYN_WEAK_DEF:
293 // Ignore a weak dynamic definition if we already have a
294 // definition.
295 return;
297 case UNDEF * 16 + DYN_WEAK_DEF:
298 case WEAK_UNDEF * 16 + DYN_WEAK_DEF:
299 case DYN_UNDEF * 16 + DYN_WEAK_DEF:
300 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_DEF:
301 // Use a weak dynamic definition if we have a reference.
302 to->override(sym, object);
303 return;
305 case COMMON * 16 + DYN_WEAK_DEF:
306 case WEAK_COMMON * 16 + DYN_WEAK_DEF:
307 case DYN_COMMON * 16 + DYN_WEAK_DEF:
308 case DYN_WEAK_COMMON * 16 + DYN_WEAK_DEF:
309 // Ignore a weak dynamic definition if we already have a common
310 // definition.
311 return;
313 case DEF * 16 + UNDEF:
314 case WEAK_DEF * 16 + UNDEF:
315 case DYN_DEF * 16 + UNDEF:
316 case DYN_WEAK_DEF * 16 + UNDEF:
317 case UNDEF * 16 + UNDEF:
318 case WEAK_UNDEF * 16 + UNDEF:
319 case DYN_UNDEF * 16 + UNDEF:
320 case DYN_WEAK_UNDEF * 16 + UNDEF:
321 case COMMON * 16 + UNDEF:
322 case WEAK_COMMON * 16 + UNDEF:
323 case DYN_COMMON * 16 + UNDEF:
324 case DYN_WEAK_COMMON * 16 + UNDEF:
325 // A new undefined reference tells us nothing.
326 return;
328 case DEF * 16 + WEAK_UNDEF:
329 case WEAK_DEF * 16 + WEAK_UNDEF:
330 case DYN_DEF * 16 + WEAK_UNDEF:
331 case DYN_WEAK_DEF * 16 + WEAK_UNDEF:
332 case UNDEF * 16 + WEAK_UNDEF:
333 case WEAK_UNDEF * 16 + WEAK_UNDEF:
334 case DYN_UNDEF * 16 + WEAK_UNDEF:
335 case DYN_WEAK_UNDEF * 16 + WEAK_UNDEF:
336 case COMMON * 16 + WEAK_UNDEF:
337 case WEAK_COMMON * 16 + WEAK_UNDEF:
338 case DYN_COMMON * 16 + WEAK_UNDEF:
339 case DYN_WEAK_COMMON * 16 + WEAK_UNDEF:
340 // A new weak undefined reference tells us nothing.
341 return;
343 case DEF * 16 + DYN_UNDEF:
344 case WEAK_DEF * 16 + DYN_UNDEF:
345 case DYN_DEF * 16 + DYN_UNDEF:
346 case DYN_WEAK_DEF * 16 + DYN_UNDEF:
347 case UNDEF * 16 + DYN_UNDEF:
348 case WEAK_UNDEF * 16 + DYN_UNDEF:
349 case DYN_UNDEF * 16 + DYN_UNDEF:
350 case DYN_WEAK_UNDEF * 16 + DYN_UNDEF:
351 case COMMON * 16 + DYN_UNDEF:
352 case WEAK_COMMON * 16 + DYN_UNDEF:
353 case DYN_COMMON * 16 + DYN_UNDEF:
354 case DYN_WEAK_COMMON * 16 + DYN_UNDEF:
355 // A new dynamic undefined reference tells us nothing.
356 return;
358 case DEF * 16 + DYN_WEAK_UNDEF:
359 case WEAK_DEF * 16 + DYN_WEAK_UNDEF:
360 case DYN_DEF * 16 + DYN_WEAK_UNDEF:
361 case DYN_WEAK_DEF * 16 + DYN_WEAK_UNDEF:
362 case UNDEF * 16 + DYN_WEAK_UNDEF:
363 case WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
364 case DYN_UNDEF * 16 + DYN_WEAK_UNDEF:
365 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
366 case COMMON * 16 + DYN_WEAK_UNDEF:
367 case WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
368 case DYN_COMMON * 16 + DYN_WEAK_UNDEF:
369 case DYN_WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
370 // A new weak dynamic undefined reference tells us nothing.
371 return;
373 case DEF * 16 + COMMON:
374 // A common symbol does not override a definition.
375 return;
377 case WEAK_DEF * 16 + COMMON:
378 case DYN_DEF * 16 + COMMON:
379 case DYN_WEAK_DEF * 16 + COMMON:
380 // A common symbol does override a weak definition or a dynamic
381 // definition.
382 to->override(sym, object);
383 return;
385 case UNDEF * 16 + COMMON:
386 case WEAK_UNDEF * 16 + COMMON:
387 case DYN_UNDEF * 16 + COMMON:
388 case DYN_WEAK_UNDEF * 16 + COMMON:
389 // A common symbol is a definition for a reference.
390 to->override(sym, object);
391 return;
393 case COMMON * 16 + COMMON:
394 case WEAK_COMMON * 16 + COMMON:
395 case DYN_COMMON * 16 + COMMON:
396 case DYN_WEAK_COMMON * 16 + COMMON:
398 case DEF * 16 + WEAK_COMMON:
399 case WEAK_DEF * 16 + WEAK_COMMON:
400 case DYN_DEF * 16 + WEAK_COMMON:
401 case DYN_WEAK_DEF * 16 + WEAK_COMMON:
402 case UNDEF * 16 + WEAK_COMMON:
403 case WEAK_UNDEF * 16 + WEAK_COMMON:
404 case DYN_UNDEF * 16 + WEAK_COMMON:
405 case DYN_WEAK_UNDEF * 16 + WEAK_COMMON:
406 case COMMON * 16 + WEAK_COMMON:
407 case WEAK_COMMON * 16 + WEAK_COMMON:
408 case DYN_COMMON * 16 + WEAK_COMMON:
409 case DYN_WEAK_COMMON * 16 + WEAK_COMMON:
411 case DEF * 16 + DYN_COMMON:
412 case WEAK_DEF * 16 + DYN_COMMON:
413 case DYN_DEF * 16 + DYN_COMMON:
414 case DYN_WEAK_DEF * 16 + DYN_COMMON:
415 case UNDEF * 16 + DYN_COMMON:
416 case WEAK_UNDEF * 16 + DYN_COMMON:
417 case DYN_UNDEF * 16 + DYN_COMMON:
418 case DYN_WEAK_UNDEF * 16 + DYN_COMMON:
419 case COMMON * 16 + DYN_COMMON:
420 case WEAK_COMMON * 16 + DYN_COMMON:
421 case DYN_COMMON * 16 + DYN_COMMON:
422 case DYN_WEAK_COMMON * 16 + DYN_COMMON:
424 case DEF * 16 + DYN_WEAK_COMMON:
425 case WEAK_DEF * 16 + DYN_WEAK_COMMON:
426 case DYN_DEF * 16 + DYN_WEAK_COMMON:
427 case DYN_WEAK_DEF * 16 + DYN_WEAK_COMMON:
428 case UNDEF * 16 + DYN_WEAK_COMMON:
429 case WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
430 case DYN_UNDEF * 16 + DYN_WEAK_COMMON:
431 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
432 case COMMON * 16 + DYN_WEAK_COMMON:
433 case WEAK_COMMON * 16 + DYN_WEAK_COMMON:
434 case DYN_COMMON * 16 + DYN_WEAK_COMMON:
435 case DYN_WEAK_COMMON * 16 + DYN_WEAK_COMMON:
436 abort();
437 break;
439 default:
440 abort();
444 // Instantiate the templates we need. We could use the configure
445 // script to restrict this to only the ones needed for implemented
446 // targets.
448 template
449 void
450 Symbol_table::resolve<32, true>(
451 Sized_symbol<32>* to,
452 const elfcpp::Sym<32, true>& sym,
453 Object* object);
455 template
456 void
457 Symbol_table::resolve<32, false>(
458 Sized_symbol<32>* to,
459 const elfcpp::Sym<32, false>& sym,
460 Object* object);
462 template
463 void
464 Symbol_table::resolve<64, true>(
465 Sized_symbol<64>* to,
466 const elfcpp::Sym<64, true>& sym,
467 Object* object);
469 template
470 void
471 Symbol_table::resolve<64, false>(
472 Sized_symbol<64>* to,
473 const elfcpp::Sym<64, false>& sym,
474 Object* object);
476 } // End namespace gold.