missed from last commit
[deliverable/binutils-gdb.git] / gold / resolve.cc
CommitLineData
14bfc3f5
ILT
1// resolve.cc -- symbol resolution for gold
2
e5756efb 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
14bfc3f5
ILT
23#include "gold.h"
24
25#include "elfcpp.h"
26#include "target.h"
27#include "object.h"
28#include "symtab.h"
29
30namespace gold
31{
32
1564db8d
ILT
33// Symbol methods used in this file.
34
35// Override the fields in Symbol.
36
37template<int size, bool big_endian>
38void
39Symbol::override_base(const elfcpp::Sym<size, big_endian>& sym,
d491d34e 40 unsigned int st_shndx, bool is_ordinary,
14b31740 41 Object* object, const char* version)
1564db8d 42{
a3ad94ed 43 gold_assert(this->source_ == FROM_OBJECT);
ead1e424 44 this->u_.from_object.object = object;
14b31740
ILT
45 if (version != NULL && this->version() != version)
46 {
47 gold_assert(this->version() == NULL);
48 this->version_ = version;
49 }
d491d34e
ILT
50 this->u_.from_object.shndx = st_shndx;
51 this->is_ordinary_shndx_ = is_ordinary;
1564db8d
ILT
52 this->type_ = sym.get_st_type();
53 this->binding_ = sym.get_st_bind();
54 this->visibility_ = sym.get_st_visibility();
ead1e424 55 this->nonvis_ = sym.get_st_nonvis();
0d4f1889
ILT
56 if (object->is_dynamic())
57 this->in_dyn_ = true;
58 else
59 this->in_reg_ = true;
1564db8d
ILT
60}
61
62// Override the fields in Sized_symbol.
63
64template<int size>
65template<bool big_endian>
66void
67Sized_symbol<size>::override(const elfcpp::Sym<size, big_endian>& sym,
d491d34e 68 unsigned st_shndx, bool is_ordinary,
14b31740 69 Object* object, const char* version)
1564db8d 70{
d491d34e 71 this->override_base(sym, st_shndx, is_ordinary, object, version);
1564db8d 72 this->value_ = sym.get_st_value();
ead1e424 73 this->symsize_ = sym.get_st_size();
1564db8d
ILT
74}
75
aeddab66
ILT
76// Override TOSYM with symbol FROMSYM, defined in OBJECT, with version
77// VERSION. This handles all aliases of TOSYM.
78
79template<int size, bool big_endian>
80void
81Symbol_table::override(Sized_symbol<size>* tosym,
82 const elfcpp::Sym<size, big_endian>& fromsym,
d491d34e 83 unsigned int st_shndx, bool is_ordinary,
aeddab66
ILT
84 Object* object, const char* version)
85{
d491d34e 86 tosym->override(fromsym, st_shndx, is_ordinary, object, version);
aeddab66
ILT
87 if (tosym->has_alias())
88 {
89 Symbol* sym = this->weak_aliases_[tosym];
90 gold_assert(sym != NULL);
7d1a9ebb 91 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
aeddab66
ILT
92 do
93 {
d491d34e 94 ssym->override(fromsym, st_shndx, is_ordinary, object, version);
aeddab66
ILT
95 sym = this->weak_aliases_[ssym];
96 gold_assert(sym != NULL);
7d1a9ebb 97 ssym = this->get_sized_symbol<size>(sym);
aeddab66
ILT
98 }
99 while (ssym != tosym);
100 }
101}
102
86f2e683
ILT
103// The resolve functions build a little code for each symbol.
104// Bit 0: 0 for global, 1 for weak.
105// Bit 1: 0 for regular object, 1 for shared object
106// Bits 2-3: 0 for normal, 1 for undefined, 2 for common
107// This gives us values from 0 to 11.
108
109static const int global_or_weak_shift = 0;
110static const unsigned int global_flag = 0 << global_or_weak_shift;
111static const unsigned int weak_flag = 1 << global_or_weak_shift;
112
113static const int regular_or_dynamic_shift = 1;
114static const unsigned int regular_flag = 0 << regular_or_dynamic_shift;
115static const unsigned int dynamic_flag = 1 << regular_or_dynamic_shift;
116
117static const int def_undef_or_common_shift = 2;
118static const unsigned int def_flag = 0 << def_undef_or_common_shift;
119static const unsigned int undef_flag = 1 << def_undef_or_common_shift;
120static const unsigned int common_flag = 2 << def_undef_or_common_shift;
121
70e654ba
ILT
122// This convenience function combines all the flags based on facts
123// about the symbol.
124
125static unsigned int
126symbol_to_bits(elfcpp::STB binding, bool is_dynamic,
d491d34e 127 unsigned int shndx, bool is_ordinary, elfcpp::STT type)
70e654ba
ILT
128{
129 unsigned int bits;
130
131 switch (binding)
132 {
133 case elfcpp::STB_GLOBAL:
134 bits = global_flag;
135 break;
136
137 case elfcpp::STB_WEAK:
138 bits = weak_flag;
139 break;
140
141 case elfcpp::STB_LOCAL:
142 // We should only see externally visible symbols in the symbol
143 // table.
144 gold_error(_("invalid STB_LOCAL symbol in external symbols"));
145 bits = global_flag;
146
147 default:
148 // Any target which wants to handle STB_LOOS, etc., needs to
149 // define a resolve method.
150 gold_error(_("unsupported symbol binding"));
151 bits = global_flag;
152 }
153
154 if (is_dynamic)
155 bits |= dynamic_flag;
156 else
157 bits |= regular_flag;
158
159 switch (shndx)
160 {
161 case elfcpp::SHN_UNDEF:
162 bits |= undef_flag;
163 break;
164
165 case elfcpp::SHN_COMMON:
d491d34e
ILT
166 if (!is_ordinary)
167 bits |= common_flag;
70e654ba
ILT
168 break;
169
170 default:
171 if (type == elfcpp::STT_COMMON)
172 bits |= common_flag;
173 else
174 bits |= def_flag;
175 break;
176 }
177
178 return bits;
179}
180
14bfc3f5 181// Resolve a symbol. This is called the second and subsequent times
d491d34e
ILT
182// we see a symbol. TO is the pre-existing symbol. ST_SHNDX is the
183// section index for SYM, possibly adjusted for many sections.
184// IS_ORDINARY is whether ST_SHNDX is a normal section index rather
185// than a special code. ORIG_ST_SHNDX is the original section index,
186// before any munging because of discarded sections, except that all
187// non-ordinary section indexes are mapped to SHN_UNDEF. VERSION of
188// the version of SYM.
14bfc3f5
ILT
189
190template<int size, bool big_endian>
191void
1564db8d 192Symbol_table::resolve(Sized_symbol<size>* to,
14bfc3f5 193 const elfcpp::Sym<size, big_endian>& sym,
d491d34e
ILT
194 unsigned int st_shndx, bool is_ordinary,
195 unsigned int orig_st_shndx,
14b31740 196 Object* object, const char* version)
14bfc3f5
ILT
197{
198 if (object->target()->has_resolve())
199 {
274e99f9 200 Sized_target<size, big_endian>* sized_target;
7d1a9ebb 201 sized_target = object->sized_target<size, big_endian>();
14b31740 202 sized_target->resolve(to, sym, object, version);
14bfc3f5
ILT
203 return;
204 }
205
86f2e683
ILT
206 if (!object->is_dynamic())
207 {
208 // Record that we've seen this symbol in a regular object.
209 to->set_in_reg();
210 }
211 else
212 {
213 // Record that we've seen this symbol in a dynamic object.
214 to->set_in_dyn();
215 }
14bfc3f5 216
70e654ba
ILT
217 unsigned int frombits = symbol_to_bits(sym.get_st_bind(),
218 object->is_dynamic(),
d491d34e 219 st_shndx, is_ordinary,
70e654ba 220 sym.get_st_type());
14bfc3f5 221
86f2e683 222 bool adjust_common_sizes;
d20222a1
ILT
223 if (Symbol_table::should_override(to, frombits, object,
224 &adjust_common_sizes))
86f2e683
ILT
225 {
226 typename Sized_symbol<size>::Size_type tosize = to->symsize();
227
d491d34e 228 this->override(to, sym, st_shndx, is_ordinary, object, version);
86f2e683
ILT
229
230 if (adjust_common_sizes && tosize > to->symsize())
231 to->set_symsize(tosize);
232 }
233 else
234 {
235 if (adjust_common_sizes && sym.get_st_size() > to->symsize())
236 to->set_symsize(sym.get_st_size());
237 }
70e654ba
ILT
238
239 // A new weak undefined reference, merging with an old weak
240 // reference, could be a One Definition Rule (ODR) violation --
241 // especially if the types or sizes of the references differ. We'll
242 // store such pairs and look them up later to make sure they
243 // actually refer to the same lines of code. (Note: not all ODR
244 // violations can be found this way, and not everything this finds
245 // is an ODR violation. But it's helpful to warn about.)
d491d34e 246 bool to_is_ordinary;
8851ecca 247 if (parameters->options().detect_odr_violations()
d491d34e 248 && sym.get_st_bind() == elfcpp::STB_WEAK
70e654ba 249 && to->binding() == elfcpp::STB_WEAK
d491d34e
ILT
250 && orig_st_shndx != elfcpp::SHN_UNDEF
251 && to->shndx(&to_is_ordinary) != elfcpp::SHN_UNDEF
252 && to_is_ordinary
253 && sym.get_st_size() != 0 // Ignore weird 0-sized symbols.
70e654ba 254 && to->symsize() != 0
d491d34e
ILT
255 && (sym.get_st_type() != to->type()
256 || sym.get_st_size() != to->symsize())
70e654ba
ILT
257 // C does not have a concept of ODR, so we only need to do this
258 // on C++ symbols. These have (mangled) names starting with _Z.
259 && to->name()[0] == '_' && to->name()[1] == 'Z')
260 {
a2b1aa12 261 Symbol_location fromloc
d491d34e
ILT
262 = { object, orig_st_shndx, sym.get_st_value() };
263 Symbol_location toloc = { to->object(), to->shndx(&to_is_ordinary),
264 to->value() };
a2b1aa12
ILT
265 this->candidate_odr_violations_[to->name()].insert(fromloc);
266 this->candidate_odr_violations_[to->name()].insert(toloc);
70e654ba 267 }
86f2e683
ILT
268}
269
270// Handle the core of symbol resolution. This is called with the
271// existing symbol, TO, and a bitflag describing the new symbol. This
272// returns true if we should override the existing symbol with the new
273// one, and returns false otherwise. It sets *ADJUST_COMMON_SIZES to
274// true if we should set the symbol size to the maximum of the TO and
275// FROM sizes. It handles error conditions.
276
277bool
278Symbol_table::should_override(const Symbol* to, unsigned int frombits,
d20222a1 279 Object* object, bool* adjust_common_sizes)
86f2e683
ILT
280{
281 *adjust_common_sizes = false;
282
e5756efb 283 unsigned int tobits;
d491d34e
ILT
284 if (to->source() != Symbol::FROM_OBJECT)
285 tobits = symbol_to_bits(to->binding(), false, elfcpp::SHN_ABS, false,
e5756efb
ILT
286 to->type());
287 else
d491d34e
ILT
288 {
289 bool is_ordinary;
290 unsigned int shndx = to->shndx(&is_ordinary);
291 tobits = symbol_to_bits(to->binding(),
292 to->object()->is_dynamic(),
293 shndx,
294 is_ordinary,
295 to->type());
296 }
14bfc3f5 297
1564db8d
ILT
298 // FIXME: Warn if either but not both of TO and SYM are STT_TLS.
299
14bfc3f5
ILT
300 // We use a giant switch table for symbol resolution. This code is
301 // unwieldy, but: 1) it is efficient; 2) we definitely handle all
302 // cases; 3) it is easy to change the handling of a particular case.
303 // The alternative would be a series of conditionals, but it is easy
304 // to get the ordering wrong. This could also be done as a table,
305 // but that is no easier to understand than this large switch
306 // statement.
307
86f2e683
ILT
308 // These are the values generated by the bit codes.
309 enum
310 {
311 DEF = global_flag | regular_flag | def_flag,
312 WEAK_DEF = weak_flag | regular_flag | def_flag,
313 DYN_DEF = global_flag | dynamic_flag | def_flag,
314 DYN_WEAK_DEF = weak_flag | dynamic_flag | def_flag,
315 UNDEF = global_flag | regular_flag | undef_flag,
316 WEAK_UNDEF = weak_flag | regular_flag | undef_flag,
317 DYN_UNDEF = global_flag | dynamic_flag | undef_flag,
318 DYN_WEAK_UNDEF = weak_flag | dynamic_flag | undef_flag,
319 COMMON = global_flag | regular_flag | common_flag,
320 WEAK_COMMON = weak_flag | regular_flag | common_flag,
321 DYN_COMMON = global_flag | dynamic_flag | common_flag,
322 DYN_WEAK_COMMON = weak_flag | dynamic_flag | common_flag
323 };
324
14bfc3f5
ILT
325 switch (tobits * 16 + frombits)
326 {
327 case DEF * 16 + DEF:
12e14209 328 // Two definitions of the same symbol.
878405a8
ILT
329
330 // If either symbol is defined by an object included using
331 // --just-symbols, then don't warn. This is for compatibility
332 // with the GNU linker. FIXME: This is a hack.
333 if ((to->source() == Symbol::FROM_OBJECT && to->object()->just_symbols())
334 || object->just_symbols())
335 return false;
336
d20222a1
ILT
337 // FIXME: Do a better job of reporting locations.
338 gold_error(_("%s: multiple definition of %s"),
339 object != NULL ? object->name().c_str() : _("command line"),
a2b1aa12 340 to->demangled_name().c_str());
d20222a1
ILT
341 gold_error(_("%s: previous definition here"),
342 (to->source() == Symbol::FROM_OBJECT
343 ? to->object()->name().c_str()
344 : _("command line")));
86f2e683 345 return false;
14bfc3f5
ILT
346
347 case WEAK_DEF * 16 + DEF:
1564db8d
ILT
348 // We've seen a weak definition, and now we see a strong
349 // definition. In the original SVR4 linker, this was treated as
350 // a multiple definition error. In the Solaris linker and the
351 // GNU linker, a weak definition followed by a regular
352 // definition causes the weak definition to be overridden. We
353 // are currently compatible with the GNU linker. In the future
354 // we should add a target specific option to change this.
355 // FIXME.
86f2e683 356 return true;
14bfc3f5
ILT
357
358 case DYN_DEF * 16 + DEF:
359 case DYN_WEAK_DEF * 16 + DEF:
1564db8d
ILT
360 // We've seen a definition in a dynamic object, and now we see a
361 // definition in a regular object. The definition in the
362 // regular object overrides the definition in the dynamic
363 // object.
86f2e683 364 return true;
1564db8d 365
14bfc3f5
ILT
366 case UNDEF * 16 + DEF:
367 case WEAK_UNDEF * 16 + DEF:
368 case DYN_UNDEF * 16 + DEF:
369 case DYN_WEAK_UNDEF * 16 + DEF:
1564db8d
ILT
370 // We've seen an undefined reference, and now we see a
371 // definition. We use the definition.
86f2e683 372 return true;
1564db8d 373
14bfc3f5
ILT
374 case COMMON * 16 + DEF:
375 case WEAK_COMMON * 16 + DEF:
376 case DYN_COMMON * 16 + DEF:
377 case DYN_WEAK_COMMON * 16 + DEF:
1564db8d 378 // We've seen a common symbol and now we see a definition. The
14b31740 379 // definition overrides. FIXME: We should optionally issue, version a
1564db8d 380 // warning.
86f2e683 381 return true;
14bfc3f5
ILT
382
383 case DEF * 16 + WEAK_DEF:
384 case WEAK_DEF * 16 + WEAK_DEF:
1564db8d
ILT
385 // We've seen a definition and now we see a weak definition. We
386 // ignore the new weak definition.
86f2e683 387 return false;
1564db8d 388
14bfc3f5
ILT
389 case DYN_DEF * 16 + WEAK_DEF:
390 case DYN_WEAK_DEF * 16 + WEAK_DEF:
1564db8d
ILT
391 // We've seen a dynamic definition and now we see a regular weak
392 // definition. The regular weak definition overrides.
86f2e683 393 return true;
1564db8d 394
14bfc3f5
ILT
395 case UNDEF * 16 + WEAK_DEF:
396 case WEAK_UNDEF * 16 + WEAK_DEF:
397 case DYN_UNDEF * 16 + WEAK_DEF:
398 case DYN_WEAK_UNDEF * 16 + WEAK_DEF:
1564db8d 399 // A weak definition of a currently undefined symbol.
86f2e683 400 return true;
1564db8d 401
14bfc3f5
ILT
402 case COMMON * 16 + WEAK_DEF:
403 case WEAK_COMMON * 16 + WEAK_DEF:
1564db8d 404 // A weak definition does not override a common definition.
86f2e683 405 return false;
1564db8d 406
14bfc3f5
ILT
407 case DYN_COMMON * 16 + WEAK_DEF:
408 case DYN_WEAK_COMMON * 16 + WEAK_DEF:
1564db8d
ILT
409 // A weak definition does override a definition in a dynamic
410 // object. FIXME: We should optionally issue a warning.
86f2e683 411 return true;
14bfc3f5
ILT
412
413 case DEF * 16 + DYN_DEF:
414 case WEAK_DEF * 16 + DYN_DEF:
415 case DYN_DEF * 16 + DYN_DEF:
416 case DYN_WEAK_DEF * 16 + DYN_DEF:
1564db8d 417 // Ignore a dynamic definition if we already have a definition.
86f2e683 418 return false;
1564db8d 419
14bfc3f5
ILT
420 case UNDEF * 16 + DYN_DEF:
421 case WEAK_UNDEF * 16 + DYN_DEF:
422 case DYN_UNDEF * 16 + DYN_DEF:
423 case DYN_WEAK_UNDEF * 16 + DYN_DEF:
1564db8d 424 // Use a dynamic definition if we have a reference.
86f2e683 425 return true;
1564db8d 426
14bfc3f5
ILT
427 case COMMON * 16 + DYN_DEF:
428 case WEAK_COMMON * 16 + DYN_DEF:
429 case DYN_COMMON * 16 + DYN_DEF:
430 case DYN_WEAK_COMMON * 16 + DYN_DEF:
1564db8d
ILT
431 // Ignore a dynamic definition if we already have a common
432 // definition.
86f2e683 433 return false;
14bfc3f5
ILT
434
435 case DEF * 16 + DYN_WEAK_DEF:
436 case WEAK_DEF * 16 + DYN_WEAK_DEF:
437 case DYN_DEF * 16 + DYN_WEAK_DEF:
438 case DYN_WEAK_DEF * 16 + DYN_WEAK_DEF:
1564db8d
ILT
439 // Ignore a weak dynamic definition if we already have a
440 // definition.
86f2e683 441 return false;
1564db8d 442
14bfc3f5
ILT
443 case UNDEF * 16 + DYN_WEAK_DEF:
444 case WEAK_UNDEF * 16 + DYN_WEAK_DEF:
445 case DYN_UNDEF * 16 + DYN_WEAK_DEF:
446 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_DEF:
1564db8d 447 // Use a weak dynamic definition if we have a reference.
86f2e683 448 return true;
1564db8d 449
14bfc3f5
ILT
450 case COMMON * 16 + DYN_WEAK_DEF:
451 case WEAK_COMMON * 16 + DYN_WEAK_DEF:
452 case DYN_COMMON * 16 + DYN_WEAK_DEF:
453 case DYN_WEAK_COMMON * 16 + DYN_WEAK_DEF:
1564db8d
ILT
454 // Ignore a weak dynamic definition if we already have a common
455 // definition.
86f2e683 456 return false;
14bfc3f5
ILT
457
458 case DEF * 16 + UNDEF:
459 case WEAK_DEF * 16 + UNDEF:
460 case DYN_DEF * 16 + UNDEF:
461 case DYN_WEAK_DEF * 16 + UNDEF:
462 case UNDEF * 16 + UNDEF:
ead1e424 463 // A new undefined reference tells us nothing.
86f2e683 464 return false;
ead1e424 465
14bfc3f5
ILT
466 case WEAK_UNDEF * 16 + UNDEF:
467 case DYN_UNDEF * 16 + UNDEF:
468 case DYN_WEAK_UNDEF * 16 + UNDEF:
ead1e424 469 // A strong undef overrides a dynamic or weak undef.
86f2e683 470 return true;
ead1e424 471
14bfc3f5
ILT
472 case COMMON * 16 + UNDEF:
473 case WEAK_COMMON * 16 + UNDEF:
474 case DYN_COMMON * 16 + UNDEF:
475 case DYN_WEAK_COMMON * 16 + UNDEF:
1564db8d 476 // A new undefined reference tells us nothing.
86f2e683 477 return false;
14bfc3f5
ILT
478
479 case DEF * 16 + WEAK_UNDEF:
480 case WEAK_DEF * 16 + WEAK_UNDEF:
481 case DYN_DEF * 16 + WEAK_UNDEF:
482 case DYN_WEAK_DEF * 16 + WEAK_UNDEF:
483 case UNDEF * 16 + WEAK_UNDEF:
484 case WEAK_UNDEF * 16 + WEAK_UNDEF:
485 case DYN_UNDEF * 16 + WEAK_UNDEF:
486 case DYN_WEAK_UNDEF * 16 + WEAK_UNDEF:
487 case COMMON * 16 + WEAK_UNDEF:
488 case WEAK_COMMON * 16 + WEAK_UNDEF:
489 case DYN_COMMON * 16 + WEAK_UNDEF:
490 case DYN_WEAK_COMMON * 16 + WEAK_UNDEF:
1564db8d 491 // A new weak undefined reference tells us nothing.
86f2e683 492 return false;
14bfc3f5
ILT
493
494 case DEF * 16 + DYN_UNDEF:
495 case WEAK_DEF * 16 + DYN_UNDEF:
496 case DYN_DEF * 16 + DYN_UNDEF:
497 case DYN_WEAK_DEF * 16 + DYN_UNDEF:
498 case UNDEF * 16 + DYN_UNDEF:
499 case WEAK_UNDEF * 16 + DYN_UNDEF:
500 case DYN_UNDEF * 16 + DYN_UNDEF:
501 case DYN_WEAK_UNDEF * 16 + DYN_UNDEF:
502 case COMMON * 16 + DYN_UNDEF:
503 case WEAK_COMMON * 16 + DYN_UNDEF:
504 case DYN_COMMON * 16 + DYN_UNDEF:
505 case DYN_WEAK_COMMON * 16 + DYN_UNDEF:
1564db8d 506 // A new dynamic undefined reference tells us nothing.
86f2e683 507 return false;
14bfc3f5
ILT
508
509 case DEF * 16 + DYN_WEAK_UNDEF:
510 case WEAK_DEF * 16 + DYN_WEAK_UNDEF:
511 case DYN_DEF * 16 + DYN_WEAK_UNDEF:
512 case DYN_WEAK_DEF * 16 + DYN_WEAK_UNDEF:
513 case UNDEF * 16 + DYN_WEAK_UNDEF:
514 case WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
515 case DYN_UNDEF * 16 + DYN_WEAK_UNDEF:
516 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_UNDEF:
517 case COMMON * 16 + DYN_WEAK_UNDEF:
518 case WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
519 case DYN_COMMON * 16 + DYN_WEAK_UNDEF:
520 case DYN_WEAK_COMMON * 16 + DYN_WEAK_UNDEF:
1564db8d 521 // A new weak dynamic undefined reference tells us nothing.
86f2e683 522 return false;
14bfc3f5
ILT
523
524 case DEF * 16 + COMMON:
1564db8d 525 // A common symbol does not override a definition.
86f2e683 526 return false;
1564db8d 527
14bfc3f5
ILT
528 case WEAK_DEF * 16 + COMMON:
529 case DYN_DEF * 16 + COMMON:
530 case DYN_WEAK_DEF * 16 + COMMON:
1564db8d
ILT
531 // A common symbol does override a weak definition or a dynamic
532 // definition.
86f2e683 533 return true;
1564db8d 534
14bfc3f5
ILT
535 case UNDEF * 16 + COMMON:
536 case WEAK_UNDEF * 16 + COMMON:
537 case DYN_UNDEF * 16 + COMMON:
538 case DYN_WEAK_UNDEF * 16 + COMMON:
1564db8d 539 // A common symbol is a definition for a reference.
86f2e683 540 return true;
1564db8d 541
14bfc3f5 542 case COMMON * 16 + COMMON:
ead1e424 543 // Set the size to the maximum.
86f2e683
ILT
544 *adjust_common_sizes = true;
545 return false;
ead1e424 546
14bfc3f5 547 case WEAK_COMMON * 16 + COMMON:
ead1e424
ILT
548 // I'm not sure just what a weak common symbol means, but
549 // presumably it can be overridden by a regular common symbol.
86f2e683 550 return true;
ead1e424 551
14bfc3f5
ILT
552 case DYN_COMMON * 16 + COMMON:
553 case DYN_WEAK_COMMON * 16 + COMMON:
86f2e683
ILT
554 // Use the real common symbol, but adjust the size if necessary.
555 *adjust_common_sizes = true;
556 return true;
14bfc3f5
ILT
557
558 case DEF * 16 + WEAK_COMMON:
559 case WEAK_DEF * 16 + WEAK_COMMON:
560 case DYN_DEF * 16 + WEAK_COMMON:
561 case DYN_WEAK_DEF * 16 + WEAK_COMMON:
ead1e424
ILT
562 // Whatever a weak common symbol is, it won't override a
563 // definition.
86f2e683 564 return false;
ead1e424 565
14bfc3f5
ILT
566 case UNDEF * 16 + WEAK_COMMON:
567 case WEAK_UNDEF * 16 + WEAK_COMMON:
568 case DYN_UNDEF * 16 + WEAK_COMMON:
569 case DYN_WEAK_UNDEF * 16 + WEAK_COMMON:
ead1e424 570 // A weak common symbol is better than an undefined symbol.
86f2e683 571 return true;
ead1e424 572
14bfc3f5
ILT
573 case COMMON * 16 + WEAK_COMMON:
574 case WEAK_COMMON * 16 + WEAK_COMMON:
575 case DYN_COMMON * 16 + WEAK_COMMON:
576 case DYN_WEAK_COMMON * 16 + WEAK_COMMON:
ead1e424
ILT
577 // Ignore a weak common symbol in the presence of a real common
578 // symbol.
86f2e683 579 return false;
14bfc3f5
ILT
580
581 case DEF * 16 + DYN_COMMON:
582 case WEAK_DEF * 16 + DYN_COMMON:
583 case DYN_DEF * 16 + DYN_COMMON:
584 case DYN_WEAK_DEF * 16 + DYN_COMMON:
ead1e424
ILT
585 // Ignore a dynamic common symbol in the presence of a
586 // definition.
86f2e683 587 return false;
ead1e424 588
14bfc3f5
ILT
589 case UNDEF * 16 + DYN_COMMON:
590 case WEAK_UNDEF * 16 + DYN_COMMON:
591 case DYN_UNDEF * 16 + DYN_COMMON:
592 case DYN_WEAK_UNDEF * 16 + DYN_COMMON:
ead1e424 593 // A dynamic common symbol is a definition of sorts.
86f2e683 594 return true;
ead1e424 595
14bfc3f5
ILT
596 case COMMON * 16 + DYN_COMMON:
597 case WEAK_COMMON * 16 + DYN_COMMON:
598 case DYN_COMMON * 16 + DYN_COMMON:
599 case DYN_WEAK_COMMON * 16 + DYN_COMMON:
ead1e424 600 // Set the size to the maximum.
86f2e683
ILT
601 *adjust_common_sizes = true;
602 return false;
14bfc3f5
ILT
603
604 case DEF * 16 + DYN_WEAK_COMMON:
605 case WEAK_DEF * 16 + DYN_WEAK_COMMON:
606 case DYN_DEF * 16 + DYN_WEAK_COMMON:
607 case DYN_WEAK_DEF * 16 + DYN_WEAK_COMMON:
ead1e424 608 // A common symbol is ignored in the face of a definition.
86f2e683 609 return false;
ead1e424 610
14bfc3f5
ILT
611 case UNDEF * 16 + DYN_WEAK_COMMON:
612 case WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
613 case DYN_UNDEF * 16 + DYN_WEAK_COMMON:
614 case DYN_WEAK_UNDEF * 16 + DYN_WEAK_COMMON:
ead1e424 615 // I guess a weak common symbol is better than a definition.
86f2e683 616 return true;
ead1e424 617
14bfc3f5
ILT
618 case COMMON * 16 + DYN_WEAK_COMMON:
619 case WEAK_COMMON * 16 + DYN_WEAK_COMMON:
620 case DYN_COMMON * 16 + DYN_WEAK_COMMON:
621 case DYN_WEAK_COMMON * 16 + DYN_WEAK_COMMON:
ead1e424 622 // Set the size to the maximum.
86f2e683
ILT
623 *adjust_common_sizes = true;
624 return false;
1564db8d
ILT
625
626 default:
a3ad94ed 627 gold_unreachable();
14bfc3f5
ILT
628 }
629}
630
86f2e683
ILT
631// A special case of should_override which is only called for a strong
632// defined symbol from a regular object file. This is used when
633// defining special symbols.
634
635bool
636Symbol_table::should_override_with_special(const Symbol* to)
637{
638 bool adjust_common_sizes;
639 unsigned int frombits = global_flag | regular_flag | def_flag;
d20222a1
ILT
640 bool ret = Symbol_table::should_override(to, frombits, NULL,
641 &adjust_common_sizes);
86f2e683
ILT
642 gold_assert(!adjust_common_sizes);
643 return ret;
644}
645
646// Override symbol base with a special symbol.
647
648void
649Symbol::override_base_with_special(const Symbol* from)
650{
46fe1623
ILT
651 gold_assert(this->name_ == from->name_ || this->has_alias());
652
86f2e683
ILT
653 this->source_ = from->source_;
654 switch (from->source_)
655 {
656 case FROM_OBJECT:
657 this->u_.from_object = from->u_.from_object;
658 break;
659 case IN_OUTPUT_DATA:
660 this->u_.in_output_data = from->u_.in_output_data;
661 break;
662 case IN_OUTPUT_SEGMENT:
663 this->u_.in_output_segment = from->u_.in_output_segment;
664 break;
665 case CONSTANT:
666 break;
667 default:
668 gold_unreachable();
669 break;
670 }
671
672 if (from->version_ != NULL && this->version_ != from->version_)
673 {
674 gold_assert(this->version_ == NULL);
675 this->version_ = from->version_;
676 }
677
678 this->type_ = from->type_;
679 this->binding_ = from->binding_;
680 this->visibility_ = from->visibility_;
681 this->nonvis_ = from->nonvis_;
682
683 // Special symbols are always considered to be regular symbols.
684 this->in_reg_ = true;
46fe1623
ILT
685
686 if (from->needs_dynsym_entry_)
687 this->needs_dynsym_entry_ = true;
688 if (from->needs_dynsym_value_)
689 this->needs_dynsym_value_ = true;
690
691 // We shouldn't see these flags. If we do, we need to handle them
692 // somehow.
693 gold_assert(!from->is_target_special_ || this->is_target_special_);
694 gold_assert(!from->is_forwarder_);
46fe1623
ILT
695 gold_assert(!from->has_plt_offset_);
696 gold_assert(!from->has_warning_);
697 gold_assert(!from->is_copied_from_dynobj_);
55a93433 698 gold_assert(!from->is_forced_local_);
86f2e683
ILT
699}
700
701// Override a symbol with a special symbol.
702
703template<int size>
704void
705Sized_symbol<size>::override_with_special(const Sized_symbol<size>* from)
706{
707 this->override_base_with_special(from);
708 this->value_ = from->value_;
709 this->symsize_ = from->symsize_;
710}
711
aeddab66
ILT
712// Override TOSYM with the special symbol FROMSYM. This handles all
713// aliases of TOSYM.
714
715template<int size>
716void
717Symbol_table::override_with_special(Sized_symbol<size>* tosym,
718 const Sized_symbol<size>* fromsym)
719{
720 tosym->override_with_special(fromsym);
721 if (tosym->has_alias())
722 {
723 Symbol* sym = this->weak_aliases_[tosym];
724 gold_assert(sym != NULL);
7d1a9ebb 725 Sized_symbol<size>* ssym = this->get_sized_symbol<size>(sym);
aeddab66
ILT
726 do
727 {
728 ssym->override_with_special(fromsym);
729 sym = this->weak_aliases_[ssym];
730 gold_assert(sym != NULL);
7d1a9ebb 731 ssym = this->get_sized_symbol<size>(sym);
aeddab66
ILT
732 }
733 while (ssym != tosym);
734 }
55a93433
ILT
735 if (tosym->binding() == elfcpp::STB_LOCAL)
736 this->force_local(tosym);
aeddab66
ILT
737}
738
14bfc3f5
ILT
739// Instantiate the templates we need. We could use the configure
740// script to restrict this to only the ones needed for implemented
741// targets.
742
193a53d9 743#ifdef HAVE_TARGET_32_LITTLE
14bfc3f5
ILT
744template
745void
193a53d9 746Symbol_table::resolve<32, false>(
1564db8d 747 Sized_symbol<32>* to,
193a53d9 748 const elfcpp::Sym<32, false>& sym,
d491d34e
ILT
749 unsigned int st_shndx,
750 bool is_ordinary,
751 unsigned int orig_st_shndx,
14b31740
ILT
752 Object* object,
753 const char* version);
193a53d9 754#endif
14bfc3f5 755
193a53d9 756#ifdef HAVE_TARGET_32_BIG
14bfc3f5
ILT
757template
758void
193a53d9 759Symbol_table::resolve<32, true>(
1564db8d 760 Sized_symbol<32>* to,
193a53d9 761 const elfcpp::Sym<32, true>& sym,
d491d34e
ILT
762 unsigned int st_shndx,
763 bool is_ordinary,
764 unsigned int orig_st_shndx,
14b31740
ILT
765 Object* object,
766 const char* version);
193a53d9 767#endif
14bfc3f5 768
193a53d9 769#ifdef HAVE_TARGET_64_LITTLE
14bfc3f5
ILT
770template
771void
193a53d9 772Symbol_table::resolve<64, false>(
1564db8d 773 Sized_symbol<64>* to,
193a53d9 774 const elfcpp::Sym<64, false>& sym,
d491d34e
ILT
775 unsigned int st_shndx,
776 bool is_ordinary,
777 unsigned int orig_st_shndx,
14b31740
ILT
778 Object* object,
779 const char* version);
193a53d9 780#endif
14bfc3f5 781
193a53d9 782#ifdef HAVE_TARGET_64_BIG
14bfc3f5
ILT
783template
784void
193a53d9 785Symbol_table::resolve<64, true>(
1564db8d 786 Sized_symbol<64>* to,
193a53d9 787 const elfcpp::Sym<64, true>& sym,
d491d34e
ILT
788 unsigned int st_shndx,
789 bool is_ordinary,
790 unsigned int orig_st_shndx,
14b31740
ILT
791 Object* object,
792 const char* version);
193a53d9 793#endif
14bfc3f5 794
86f2e683
ILT
795#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
796template
797void
aeddab66
ILT
798Symbol_table::override_with_special<32>(Sized_symbol<32>*,
799 const Sized_symbol<32>*);
86f2e683
ILT
800#endif
801
802#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
803template
804void
aeddab66
ILT
805Symbol_table::override_with_special<64>(Sized_symbol<64>*,
806 const Sized_symbol<64>*);
86f2e683
ILT
807#endif
808
14bfc3f5 809} // End namespace gold.
This page took 0.12346 seconds and 4 git commands to generate.