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 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
);
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:
85 switch (to
->binding())
87 case elfcpp::STB_GLOBAL
:
91 case elfcpp::STB_WEAK
:
95 case elfcpp::STB_LOCAL
:
96 // We should only see externally visible symbols in the symbol
101 // Any target which wants to handle STB_LOOS, etc., needs to
102 // define a resolve method.
106 if (to
->object() != NULL
&& to
->object()->is_dynamic())
111 case elfcpp::SHN_UNDEF
:
115 case elfcpp::SHN_COMMON
:
120 if (to
->type() == elfcpp::STT_COMMON
)
126 switch (sym
.get_st_bind())
128 case elfcpp::STB_GLOBAL
:
132 case elfcpp::STB_WEAK
:
136 case elfcpp::STB_LOCAL
:
138 _("%s: %s: invalid STB_LOCAL symbol %s in external symbols\n"),
139 program_name
, object
->name().c_str(), to
->name());
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());
150 if (object
->is_dynamic())
152 frombits
|= (1 << 1);
154 // Record that we've seen this symbol in a dynamic object.
158 switch (sym
.get_st_shndx())
160 case elfcpp::SHN_UNDEF
:
161 frombits
|= (1 << 2);
164 case elfcpp::SHN_COMMON
:
165 frombits
|= (2 << 2);
169 if (sym
.get_st_type() == elfcpp::STT_COMMON
)
170 frombits
|= (2 << 2);
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
184 switch (tobits
* 16 + frombits
)
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.
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.
202 to
->override(sym
, object
);
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
211 to
->override(sym
, object
);
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
);
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
230 to
->override(sym
, object
);
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.
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
);
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
);
254 case COMMON
* 16 + WEAK_DEF
:
255 case WEAK_COMMON
* 16 + WEAK_DEF
:
256 // A weak definition does not override a common definition.
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
);
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.
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
);
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
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
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
);
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
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.
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.
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.
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.
373 case DEF
* 16 + COMMON
:
374 // A common symbol does not override a definition.
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
382 to
->override(sym
, object
);
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
);
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
:
444 // Instantiate the templates we need. We could use the configure
445 // script to restrict this to only the ones needed for implemented
450 Symbol_table::resolve
<32, true>(
451 Sized_symbol
<32>* to
,
452 const elfcpp::Sym
<32, true>& sym
,
457 Symbol_table::resolve
<32, false>(
458 Sized_symbol
<32>* to
,
459 const elfcpp::Sym
<32, false>& sym
,
464 Symbol_table::resolve
<64, true>(
465 Sized_symbol
<64>* to
,
466 const elfcpp::Sym
<64, true>& sym
,
471 Symbol_table::resolve
<64, false>(
472 Sized_symbol
<64>* to
,
473 const elfcpp::Sym
<64, false>& sym
,
476 } // End namespace gold.