1 // resolve.cc -- symbol resolution for gold
13 // Symbol methods used in this file.
15 // Override the fields in Symbol.
17 template<int size
, bool big_endian
>
19 Symbol::override_base(const elfcpp::Sym
<size
, big_endian
>& sym
,
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.
33 template<bool big_endian
>
35 Sized_symbol
<size
>::override(const elfcpp::Sym
<size
, big_endian
>& sym
,
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
>
49 Symbol_table::resolve(Sized_symbol
<size
>* to
,
50 const elfcpp::Sym
<size
, big_endian
>& sym
,
53 if (object
->target()->has_resolve())
55 object
->sized_target
<size
, big_endian
>()->resolve(to
, sym
, object
);
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:
82 switch (to
->binding())
84 case elfcpp::STB_GLOBAL
:
88 case elfcpp::STB_WEAK
:
92 case elfcpp::STB_LOCAL
:
93 // We should only see externally visible symbols in the symbol
98 // Any target which wants to handle STB_LOOS, etc., needs to
99 // define a resolve method.
103 if (to
->object() != NULL
&& to
->object()->is_dynamic())
108 case elfcpp::SHN_UNDEF
:
112 case elfcpp::SHN_COMMON
:
117 if (to
->type() == elfcpp::STT_COMMON
)
123 switch (sym
.get_st_bind())
125 case elfcpp::STB_GLOBAL
:
129 case elfcpp::STB_WEAK
:
133 case elfcpp::STB_LOCAL
:
135 _("%s: %s: invalid STB_LOCAL symbol %s in external symbols\n"),
136 program_name
, object
->name().c_str(), to
->name());
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());
147 if (object
->is_dynamic())
149 frombits
|= (1 << 1);
151 // Record that we've seen this symbol in a dynamic object.
155 switch (sym
.get_st_shndx())
157 case elfcpp::SHN_UNDEF
:
158 frombits
|= (1 << 2);
161 case elfcpp::SHN_COMMON
:
162 frombits
|= (2 << 2);
166 if (sym
.get_st_type() == elfcpp::STT_COMMON
)
167 frombits
|= (2 << 2);
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
181 switch (tobits
* 16 + frombits
)
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.
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.
199 to
->override(sym
, object
);
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
208 to
->override(sym
, object
);
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
);
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
227 to
->override(sym
, object
);
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.
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
);
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
);
251 case COMMON
* 16 + WEAK_DEF
:
252 case WEAK_COMMON
* 16 + WEAK_DEF
:
253 // A weak definition does not override a common definition.
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
);
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.
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
);
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
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
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
);
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
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.
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.
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.
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.
370 case DEF
* 16 + COMMON
:
371 // A common symbol does not override a definition.
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
379 to
->override(sym
, object
);
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
);
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
:
441 // Instantiate the templates we need. We could use the configure
442 // script to restrict this to only the ones needed for implemented
447 Symbol_table::resolve
<32, true>(
448 Sized_symbol
<32>* to
,
449 const elfcpp::Sym
<32, true>& sym
,
454 Symbol_table::resolve
<32, false>(
455 Sized_symbol
<32>* to
,
456 const elfcpp::Sym
<32, false>& sym
,
461 Symbol_table::resolve
<64, true>(
462 Sized_symbol
<64>* to
,
463 const elfcpp::Sym
<64, true>& sym
,
468 Symbol_table::resolve
<64, false>(
469 Sized_symbol
<64>* to
,
470 const elfcpp::Sym
<64, false>& sym
,
473 } // End namespace gold.