]>
Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // symtab.cc -- the gold symbol table |
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 | ||
04bf7072 | 25 | #include <cstring> |
14bfc3f5 | 26 | #include <stdint.h> |
04bf7072 | 27 | #include <algorithm> |
70e654ba | 28 | #include <set> |
14bfc3f5 ILT |
29 | #include <string> |
30 | #include <utility> | |
a2b1aa12 | 31 | #include "demangle.h" |
14bfc3f5 ILT |
32 | |
33 | #include "object.h" | |
70e654ba | 34 | #include "dwarf_reader.h" |
dbe717ef | 35 | #include "dynobj.h" |
75f65a3e | 36 | #include "output.h" |
61ba1cf9 | 37 | #include "target.h" |
645f8123 | 38 | #include "workqueue.h" |
14bfc3f5 ILT |
39 | #include "symtab.h" |
40 | ||
41 | namespace gold | |
42 | { | |
43 | ||
44 | // Class Symbol. | |
45 | ||
ead1e424 ILT |
46 | // Initialize fields in Symbol. This initializes everything except u_ |
47 | // and source_. | |
14bfc3f5 | 48 | |
14bfc3f5 | 49 | void |
ead1e424 ILT |
50 | Symbol::init_fields(const char* name, const char* version, |
51 | elfcpp::STT type, elfcpp::STB binding, | |
52 | elfcpp::STV visibility, unsigned char nonvis) | |
14bfc3f5 ILT |
53 | { |
54 | this->name_ = name; | |
55 | this->version_ = version; | |
c06b7b0b ILT |
56 | this->symtab_index_ = 0; |
57 | this->dynsym_index_ = 0; | |
0a65a3a7 | 58 | this->got_offsets_.init(); |
f4151f89 | 59 | this->plt_offset_ = 0; |
ead1e424 ILT |
60 | this->type_ = type; |
61 | this->binding_ = binding; | |
62 | this->visibility_ = visibility; | |
63 | this->nonvis_ = nonvis; | |
64 | this->is_target_special_ = false; | |
1564db8d ILT |
65 | this->is_def_ = false; |
66 | this->is_forwarder_ = false; | |
aeddab66 | 67 | this->has_alias_ = false; |
c06b7b0b | 68 | this->needs_dynsym_entry_ = false; |
008db82e | 69 | this->in_reg_ = false; |
ead1e424 | 70 | this->in_dyn_ = false; |
f4151f89 | 71 | this->has_plt_offset_ = false; |
f6ce93d6 | 72 | this->has_warning_ = false; |
46fe1623 | 73 | this->is_copied_from_dynobj_ = false; |
55a93433 | 74 | this->is_forced_local_ = false; |
ead1e424 ILT |
75 | } |
76 | ||
a2b1aa12 ILT |
77 | // Return the demangled version of the symbol's name, but only |
78 | // if the --demangle flag was set. | |
79 | ||
80 | static std::string | |
81 | demangle(const char* name) | |
82 | { | |
086a1841 | 83 | if (!parameters->options().do_demangle()) |
ff541f30 ILT |
84 | return name; |
85 | ||
a2b1aa12 ILT |
86 | // cplus_demangle allocates memory for the result it returns, |
87 | // and returns NULL if the name is already demangled. | |
88 | char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS); | |
89 | if (demangled_name == NULL) | |
90 | return name; | |
91 | ||
92 | std::string retval(demangled_name); | |
93 | free(demangled_name); | |
94 | return retval; | |
95 | } | |
96 | ||
97 | std::string | |
98 | Symbol::demangled_name() const | |
99 | { | |
ff541f30 | 100 | return demangle(this->name()); |
a2b1aa12 ILT |
101 | } |
102 | ||
ead1e424 ILT |
103 | // Initialize the fields in the base class Symbol for SYM in OBJECT. |
104 | ||
105 | template<int size, bool big_endian> | |
106 | void | |
107 | Symbol::init_base(const char* name, const char* version, Object* object, | |
108 | const elfcpp::Sym<size, big_endian>& sym) | |
109 | { | |
110 | this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(), | |
111 | sym.get_st_visibility(), sym.get_st_nonvis()); | |
112 | this->u_.from_object.object = object; | |
113 | // FIXME: Handle SHN_XINDEX. | |
16649710 | 114 | this->u_.from_object.shndx = sym.get_st_shndx(); |
ead1e424 | 115 | this->source_ = FROM_OBJECT; |
008db82e | 116 | this->in_reg_ = !object->is_dynamic(); |
1564db8d | 117 | this->in_dyn_ = object->is_dynamic(); |
14bfc3f5 ILT |
118 | } |
119 | ||
ead1e424 ILT |
120 | // Initialize the fields in the base class Symbol for a symbol defined |
121 | // in an Output_data. | |
122 | ||
123 | void | |
124 | Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type, | |
125 | elfcpp::STB binding, elfcpp::STV visibility, | |
126 | unsigned char nonvis, bool offset_is_from_end) | |
127 | { | |
128 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
129 | this->u_.in_output_data.output_data = od; | |
130 | this->u_.in_output_data.offset_is_from_end = offset_is_from_end; | |
131 | this->source_ = IN_OUTPUT_DATA; | |
008db82e | 132 | this->in_reg_ = true; |
ead1e424 ILT |
133 | } |
134 | ||
135 | // Initialize the fields in the base class Symbol for a symbol defined | |
136 | // in an Output_segment. | |
137 | ||
138 | void | |
139 | Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type, | |
140 | elfcpp::STB binding, elfcpp::STV visibility, | |
141 | unsigned char nonvis, Segment_offset_base offset_base) | |
142 | { | |
143 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
144 | this->u_.in_output_segment.output_segment = os; | |
145 | this->u_.in_output_segment.offset_base = offset_base; | |
146 | this->source_ = IN_OUTPUT_SEGMENT; | |
008db82e | 147 | this->in_reg_ = true; |
ead1e424 ILT |
148 | } |
149 | ||
150 | // Initialize the fields in the base class Symbol for a symbol defined | |
151 | // as a constant. | |
152 | ||
153 | void | |
154 | Symbol::init_base(const char* name, elfcpp::STT type, | |
155 | elfcpp::STB binding, elfcpp::STV visibility, | |
156 | unsigned char nonvis) | |
157 | { | |
158 | this->init_fields(name, NULL, type, binding, visibility, nonvis); | |
159 | this->source_ = CONSTANT; | |
008db82e | 160 | this->in_reg_ = true; |
ead1e424 ILT |
161 | } |
162 | ||
c7912668 ILT |
163 | // Allocate a common symbol in the base. |
164 | ||
165 | void | |
166 | Symbol::allocate_base_common(Output_data* od) | |
167 | { | |
168 | gold_assert(this->is_common()); | |
169 | this->source_ = IN_OUTPUT_DATA; | |
170 | this->u_.in_output_data.output_data = od; | |
171 | this->u_.in_output_data.offset_is_from_end = false; | |
172 | } | |
173 | ||
ead1e424 | 174 | // Initialize the fields in Sized_symbol for SYM in OBJECT. |
14bfc3f5 ILT |
175 | |
176 | template<int size> | |
177 | template<bool big_endian> | |
178 | void | |
179 | Sized_symbol<size>::init(const char* name, const char* version, Object* object, | |
180 | const elfcpp::Sym<size, big_endian>& sym) | |
181 | { | |
182 | this->init_base(name, version, object, sym); | |
183 | this->value_ = sym.get_st_value(); | |
ead1e424 ILT |
184 | this->symsize_ = sym.get_st_size(); |
185 | } | |
186 | ||
187 | // Initialize the fields in Sized_symbol for a symbol defined in an | |
188 | // Output_data. | |
189 | ||
190 | template<int size> | |
191 | void | |
192 | Sized_symbol<size>::init(const char* name, Output_data* od, | |
193 | Value_type value, Size_type symsize, | |
194 | elfcpp::STT type, elfcpp::STB binding, | |
195 | elfcpp::STV visibility, unsigned char nonvis, | |
196 | bool offset_is_from_end) | |
197 | { | |
198 | this->init_base(name, od, type, binding, visibility, nonvis, | |
199 | offset_is_from_end); | |
200 | this->value_ = value; | |
201 | this->symsize_ = symsize; | |
202 | } | |
203 | ||
204 | // Initialize the fields in Sized_symbol for a symbol defined in an | |
205 | // Output_segment. | |
206 | ||
207 | template<int size> | |
208 | void | |
209 | Sized_symbol<size>::init(const char* name, Output_segment* os, | |
210 | Value_type value, Size_type symsize, | |
211 | elfcpp::STT type, elfcpp::STB binding, | |
212 | elfcpp::STV visibility, unsigned char nonvis, | |
213 | Segment_offset_base offset_base) | |
214 | { | |
215 | this->init_base(name, os, type, binding, visibility, nonvis, offset_base); | |
216 | this->value_ = value; | |
217 | this->symsize_ = symsize; | |
218 | } | |
219 | ||
220 | // Initialize the fields in Sized_symbol for a symbol defined as a | |
221 | // constant. | |
222 | ||
223 | template<int size> | |
224 | void | |
225 | Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize, | |
226 | elfcpp::STT type, elfcpp::STB binding, | |
227 | elfcpp::STV visibility, unsigned char nonvis) | |
228 | { | |
229 | this->init_base(name, type, binding, visibility, nonvis); | |
230 | this->value_ = value; | |
231 | this->symsize_ = symsize; | |
14bfc3f5 ILT |
232 | } |
233 | ||
c7912668 ILT |
234 | // Allocate a common symbol. |
235 | ||
236 | template<int size> | |
237 | void | |
238 | Sized_symbol<size>::allocate_common(Output_data* od, Value_type value) | |
239 | { | |
240 | this->allocate_base_common(od); | |
241 | this->value_ = value; | |
242 | } | |
243 | ||
436ca963 ILT |
244 | // Return true if this symbol should be added to the dynamic symbol |
245 | // table. | |
246 | ||
247 | inline bool | |
248 | Symbol::should_add_dynsym_entry() const | |
249 | { | |
250 | // If the symbol is used by a dynamic relocation, we need to add it. | |
251 | if (this->needs_dynsym_entry()) | |
252 | return true; | |
253 | ||
55a93433 ILT |
254 | // If the symbol was forced local in a version script, do not add it. |
255 | if (this->is_forced_local()) | |
256 | return false; | |
257 | ||
436ca963 ILT |
258 | // If exporting all symbols or building a shared library, |
259 | // and the symbol is defined in a regular object and is | |
260 | // externally visible, we need to add it. | |
8851ecca | 261 | if ((parameters->options().export_dynamic() || parameters->options().shared()) |
436ca963 ILT |
262 | && !this->is_from_dynobj() |
263 | && this->is_externally_visible()) | |
264 | return true; | |
265 | ||
266 | return false; | |
267 | } | |
268 | ||
b3b74ddc ILT |
269 | // Return true if the final value of this symbol is known at link |
270 | // time. | |
271 | ||
272 | bool | |
273 | Symbol::final_value_is_known() const | |
274 | { | |
275 | // If we are not generating an executable, then no final values are | |
276 | // known, since they will change at runtime. | |
8851ecca | 277 | if (parameters->options().shared() || parameters->options().relocatable()) |
b3b74ddc ILT |
278 | return false; |
279 | ||
280 | // If the symbol is not from an object file, then it is defined, and | |
281 | // known. | |
282 | if (this->source_ != FROM_OBJECT) | |
283 | return true; | |
284 | ||
285 | // If the symbol is from a dynamic object, then the final value is | |
286 | // not known. | |
287 | if (this->object()->is_dynamic()) | |
288 | return false; | |
289 | ||
290 | // If the symbol is not undefined (it is defined or common), then | |
291 | // the final value is known. | |
292 | if (!this->is_undefined()) | |
293 | return true; | |
294 | ||
295 | // If the symbol is undefined, then whether the final value is known | |
296 | // depends on whether we are doing a static link. If we are doing a | |
297 | // dynamic link, then the final value could be filled in at runtime. | |
298 | // This could reasonably be the case for a weak undefined symbol. | |
299 | return parameters->doing_static_link(); | |
300 | } | |
301 | ||
77e65537 | 302 | // Return the output section where this symbol is defined. |
a445fddf | 303 | |
77e65537 ILT |
304 | Output_section* |
305 | Symbol::output_section() const | |
a445fddf ILT |
306 | { |
307 | switch (this->source_) | |
308 | { | |
309 | case FROM_OBJECT: | |
77e65537 ILT |
310 | { |
311 | unsigned int shndx = this->u_.from_object.shndx; | |
312 | if (shndx != elfcpp::SHN_UNDEF && shndx < elfcpp::SHN_LORESERVE) | |
313 | { | |
314 | gold_assert(!this->u_.from_object.object->is_dynamic()); | |
315 | Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object); | |
316 | section_offset_type dummy; | |
317 | return relobj->output_section(shndx, &dummy); | |
318 | } | |
319 | return NULL; | |
320 | } | |
321 | ||
a445fddf | 322 | case IN_OUTPUT_DATA: |
77e65537 ILT |
323 | return this->u_.in_output_data.output_data->output_section(); |
324 | ||
a445fddf | 325 | case IN_OUTPUT_SEGMENT: |
a445fddf | 326 | case CONSTANT: |
77e65537 ILT |
327 | return NULL; |
328 | ||
329 | default: | |
330 | gold_unreachable(); | |
331 | } | |
332 | } | |
333 | ||
334 | // Set the symbol's output section. This is used for symbols defined | |
335 | // in scripts. This should only be called after the symbol table has | |
336 | // been finalized. | |
337 | ||
338 | void | |
339 | Symbol::set_output_section(Output_section* os) | |
340 | { | |
341 | switch (this->source_) | |
342 | { | |
343 | case FROM_OBJECT: | |
344 | case IN_OUTPUT_DATA: | |
345 | gold_assert(this->output_section() == os); | |
346 | break; | |
347 | case CONSTANT: | |
348 | this->source_ = IN_OUTPUT_DATA; | |
349 | this->u_.in_output_data.output_data = os; | |
350 | this->u_.in_output_data.offset_is_from_end = false; | |
351 | break; | |
352 | case IN_OUTPUT_SEGMENT: | |
a445fddf ILT |
353 | default: |
354 | gold_unreachable(); | |
355 | } | |
356 | } | |
357 | ||
14bfc3f5 ILT |
358 | // Class Symbol_table. |
359 | ||
09124467 ILT |
360 | Symbol_table::Symbol_table(unsigned int count, |
361 | const Version_script_info& version_script) | |
6d013333 | 362 | : saw_undefined_(0), offset_(0), table_(count), namepool_(), |
155a0dd7 | 363 | forwarders_(), commons_(), tls_commons_(), forced_locals_(), warnings_(), |
55a93433 | 364 | version_script_(version_script) |
14bfc3f5 | 365 | { |
6d013333 | 366 | namepool_.reserve(count); |
14bfc3f5 ILT |
367 | } |
368 | ||
369 | Symbol_table::~Symbol_table() | |
370 | { | |
371 | } | |
372 | ||
ad8f37d1 | 373 | // The hash function. The key values are Stringpool keys. |
14bfc3f5 | 374 | |
ad8f37d1 | 375 | inline size_t |
14bfc3f5 ILT |
376 | Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const |
377 | { | |
f0641a0b | 378 | return key.first ^ key.second; |
14bfc3f5 ILT |
379 | } |
380 | ||
ad8f37d1 ILT |
381 | // The symbol table key equality function. This is called with |
382 | // Stringpool keys. | |
14bfc3f5 | 383 | |
ad8f37d1 | 384 | inline bool |
14bfc3f5 ILT |
385 | Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1, |
386 | const Symbol_table_key& k2) const | |
387 | { | |
388 | return k1.first == k2.first && k1.second == k2.second; | |
389 | } | |
390 | ||
dd8670e5 | 391 | // Make TO a symbol which forwards to FROM. |
14bfc3f5 ILT |
392 | |
393 | void | |
394 | Symbol_table::make_forwarder(Symbol* from, Symbol* to) | |
395 | { | |
a3ad94ed ILT |
396 | gold_assert(from != to); |
397 | gold_assert(!from->is_forwarder() && !to->is_forwarder()); | |
14bfc3f5 ILT |
398 | this->forwarders_[from] = to; |
399 | from->set_forwarder(); | |
400 | } | |
401 | ||
61ba1cf9 ILT |
402 | // Resolve the forwards from FROM, returning the real symbol. |
403 | ||
14bfc3f5 | 404 | Symbol* |
c06b7b0b | 405 | Symbol_table::resolve_forwards(const Symbol* from) const |
14bfc3f5 | 406 | { |
a3ad94ed | 407 | gold_assert(from->is_forwarder()); |
c06b7b0b | 408 | Unordered_map<const Symbol*, Symbol*>::const_iterator p = |
14bfc3f5 | 409 | this->forwarders_.find(from); |
a3ad94ed | 410 | gold_assert(p != this->forwarders_.end()); |
14bfc3f5 ILT |
411 | return p->second; |
412 | } | |
413 | ||
61ba1cf9 ILT |
414 | // Look up a symbol by name. |
415 | ||
416 | Symbol* | |
417 | Symbol_table::lookup(const char* name, const char* version) const | |
418 | { | |
f0641a0b ILT |
419 | Stringpool::Key name_key; |
420 | name = this->namepool_.find(name, &name_key); | |
61ba1cf9 ILT |
421 | if (name == NULL) |
422 | return NULL; | |
f0641a0b ILT |
423 | |
424 | Stringpool::Key version_key = 0; | |
61ba1cf9 ILT |
425 | if (version != NULL) |
426 | { | |
f0641a0b | 427 | version = this->namepool_.find(version, &version_key); |
61ba1cf9 ILT |
428 | if (version == NULL) |
429 | return NULL; | |
430 | } | |
431 | ||
f0641a0b | 432 | Symbol_table_key key(name_key, version_key); |
61ba1cf9 ILT |
433 | Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key); |
434 | if (p == this->table_.end()) | |
435 | return NULL; | |
436 | return p->second; | |
437 | } | |
438 | ||
14bfc3f5 ILT |
439 | // Resolve a Symbol with another Symbol. This is only used in the |
440 | // unusual case where there are references to both an unversioned | |
441 | // symbol and a symbol with a version, and we then discover that that | |
1564db8d ILT |
442 | // version is the default version. Because this is unusual, we do |
443 | // this the slow way, by converting back to an ELF symbol. | |
14bfc3f5 | 444 | |
1564db8d | 445 | template<int size, bool big_endian> |
14bfc3f5 | 446 | void |
14b31740 | 447 | Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from, |
7d1a9ebb | 448 | const char* version) |
14bfc3f5 | 449 | { |
1564db8d ILT |
450 | unsigned char buf[elfcpp::Elf_sizes<size>::sym_size]; |
451 | elfcpp::Sym_write<size, big_endian> esym(buf); | |
452 | // We don't bother to set the st_name field. | |
453 | esym.put_st_value(from->value()); | |
454 | esym.put_st_size(from->symsize()); | |
455 | esym.put_st_info(from->binding(), from->type()); | |
ead1e424 | 456 | esym.put_st_other(from->visibility(), from->nonvis()); |
16649710 | 457 | esym.put_st_shndx(from->shndx()); |
70e654ba | 458 | this->resolve(to, esym.sym(), esym.sym(), from->object(), version); |
1ebd95fd ILT |
459 | if (from->in_reg()) |
460 | to->set_in_reg(); | |
461 | if (from->in_dyn()) | |
462 | to->set_in_dyn(); | |
14bfc3f5 ILT |
463 | } |
464 | ||
55a93433 ILT |
465 | // Record that a symbol is forced to be local by a version script. |
466 | ||
467 | void | |
468 | Symbol_table::force_local(Symbol* sym) | |
469 | { | |
470 | if (!sym->is_defined() && !sym->is_common()) | |
471 | return; | |
472 | if (sym->is_forced_local()) | |
473 | { | |
474 | // We already got this one. | |
475 | return; | |
476 | } | |
477 | sym->set_is_forced_local(); | |
478 | this->forced_locals_.push_back(sym); | |
479 | } | |
480 | ||
0864d551 ILT |
481 | // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This |
482 | // is only called for undefined symbols, when at least one --wrap | |
483 | // option was used. | |
484 | ||
485 | const char* | |
486 | Symbol_table::wrap_symbol(Object* object, const char* name, | |
487 | Stringpool::Key* name_key) | |
488 | { | |
489 | // For some targets, we need to ignore a specific character when | |
490 | // wrapping, and add it back later. | |
491 | char prefix = '\0'; | |
492 | if (name[0] == object->target()->wrap_char()) | |
493 | { | |
494 | prefix = name[0]; | |
495 | ++name; | |
496 | } | |
497 | ||
c5818ff1 | 498 | if (parameters->options().is_wrap(name)) |
0864d551 ILT |
499 | { |
500 | // Turn NAME into __wrap_NAME. | |
501 | std::string s; | |
502 | if (prefix != '\0') | |
503 | s += prefix; | |
504 | s += "__wrap_"; | |
505 | s += name; | |
506 | ||
507 | // This will give us both the old and new name in NAMEPOOL_, but | |
508 | // that is OK. Only the versions we need will wind up in the | |
509 | // real string table in the output file. | |
510 | return this->namepool_.add(s.c_str(), true, name_key); | |
511 | } | |
512 | ||
513 | const char* const real_prefix = "__real_"; | |
514 | const size_t real_prefix_length = strlen(real_prefix); | |
515 | if (strncmp(name, real_prefix, real_prefix_length) == 0 | |
c5818ff1 | 516 | && parameters->options().is_wrap(name + real_prefix_length)) |
0864d551 ILT |
517 | { |
518 | // Turn __real_NAME into NAME. | |
519 | std::string s; | |
520 | if (prefix != '\0') | |
521 | s += prefix; | |
522 | s += name + real_prefix_length; | |
523 | return this->namepool_.add(s.c_str(), true, name_key); | |
524 | } | |
525 | ||
526 | return name; | |
527 | } | |
528 | ||
14bfc3f5 ILT |
529 | // Add one symbol from OBJECT to the symbol table. NAME is symbol |
530 | // name and VERSION is the version; both are canonicalized. DEF is | |
531 | // whether this is the default version. | |
532 | ||
533 | // If DEF is true, then this is the definition of a default version of | |
534 | // a symbol. That means that any lookup of NAME/NULL and any lookup | |
535 | // of NAME/VERSION should always return the same symbol. This is | |
536 | // obvious for references, but in particular we want to do this for | |
537 | // definitions: overriding NAME/NULL should also override | |
538 | // NAME/VERSION. If we don't do that, it would be very hard to | |
539 | // override functions in a shared library which uses versioning. | |
540 | ||
541 | // We implement this by simply making both entries in the hash table | |
542 | // point to the same Symbol structure. That is easy enough if this is | |
543 | // the first time we see NAME/NULL or NAME/VERSION, but it is possible | |
544 | // that we have seen both already, in which case they will both have | |
545 | // independent entries in the symbol table. We can't simply change | |
546 | // the symbol table entry, because we have pointers to the entries | |
547 | // attached to the object files. So we mark the entry attached to the | |
548 | // object file as a forwarder, and record it in the forwarders_ map. | |
549 | // Note that entries in the hash table will never be marked as | |
550 | // forwarders. | |
70e654ba ILT |
551 | // |
552 | // SYM and ORIG_SYM are almost always the same. ORIG_SYM is the | |
553 | // symbol exactly as it existed in the input file. SYM is usually | |
554 | // that as well, but can be modified, for instance if we determine | |
555 | // it's in a to-be-discarded section. | |
14bfc3f5 ILT |
556 | |
557 | template<int size, bool big_endian> | |
aeddab66 | 558 | Sized_symbol<size>* |
f6ce93d6 | 559 | Symbol_table::add_from_object(Object* object, |
14bfc3f5 | 560 | const char *name, |
f0641a0b ILT |
561 | Stringpool::Key name_key, |
562 | const char *version, | |
563 | Stringpool::Key version_key, | |
564 | bool def, | |
70e654ba ILT |
565 | const elfcpp::Sym<size, big_endian>& sym, |
566 | const elfcpp::Sym<size, big_endian>& orig_sym) | |
14bfc3f5 | 567 | { |
c5818ff1 CC |
568 | // Print a message if this symbol is being traced. |
569 | if (parameters->options().is_trace_symbol(name)) | |
570 | { | |
571 | if (orig_sym.get_st_shndx() == elfcpp::SHN_UNDEF) | |
572 | gold_info(_("%s: reference to %s"), object->name().c_str(), name); | |
573 | else | |
574 | gold_info(_("%s: definition of %s"), object->name().c_str(), name); | |
575 | } | |
576 | ||
0864d551 ILT |
577 | // For an undefined symbol, we may need to adjust the name using |
578 | // --wrap. | |
579 | if (orig_sym.get_st_shndx() == elfcpp::SHN_UNDEF | |
c5818ff1 | 580 | && parameters->options().any_wrap()) |
0864d551 ILT |
581 | { |
582 | const char* wrap_name = this->wrap_symbol(object, name, &name_key); | |
583 | if (wrap_name != name) | |
584 | { | |
585 | // If we see a reference to malloc with version GLIBC_2.0, | |
586 | // and we turn it into a reference to __wrap_malloc, then we | |
587 | // discard the version number. Otherwise the user would be | |
588 | // required to specify the correct version for | |
589 | // __wrap_malloc. | |
590 | version = NULL; | |
591 | version_key = 0; | |
592 | name = wrap_name; | |
593 | } | |
594 | } | |
595 | ||
14bfc3f5 ILT |
596 | Symbol* const snull = NULL; |
597 | std::pair<typename Symbol_table_type::iterator, bool> ins = | |
f0641a0b ILT |
598 | this->table_.insert(std::make_pair(std::make_pair(name_key, version_key), |
599 | snull)); | |
14bfc3f5 ILT |
600 | |
601 | std::pair<typename Symbol_table_type::iterator, bool> insdef = | |
602 | std::make_pair(this->table_.end(), false); | |
603 | if (def) | |
604 | { | |
f0641a0b ILT |
605 | const Stringpool::Key vnull_key = 0; |
606 | insdef = this->table_.insert(std::make_pair(std::make_pair(name_key, | |
607 | vnull_key), | |
14bfc3f5 ILT |
608 | snull)); |
609 | } | |
610 | ||
611 | // ins.first: an iterator, which is a pointer to a pair. | |
612 | // ins.first->first: the key (a pair of name and version). | |
613 | // ins.first->second: the value (Symbol*). | |
614 | // ins.second: true if new entry was inserted, false if not. | |
615 | ||
1564db8d | 616 | Sized_symbol<size>* ret; |
ead1e424 ILT |
617 | bool was_undefined; |
618 | bool was_common; | |
14bfc3f5 ILT |
619 | if (!ins.second) |
620 | { | |
621 | // We already have an entry for NAME/VERSION. | |
7d1a9ebb | 622 | ret = this->get_sized_symbol<size>(ins.first->second); |
a3ad94ed | 623 | gold_assert(ret != NULL); |
ead1e424 ILT |
624 | |
625 | was_undefined = ret->is_undefined(); | |
626 | was_common = ret->is_common(); | |
627 | ||
70e654ba | 628 | this->resolve(ret, sym, orig_sym, object, version); |
14bfc3f5 ILT |
629 | |
630 | if (def) | |
631 | { | |
632 | if (insdef.second) | |
633 | { | |
634 | // This is the first time we have seen NAME/NULL. Make | |
635 | // NAME/NULL point to NAME/VERSION. | |
636 | insdef.first->second = ret; | |
637 | } | |
99f8faca ILT |
638 | else if (insdef.first->second != ret |
639 | && insdef.first->second->is_undefined()) | |
14bfc3f5 ILT |
640 | { |
641 | // This is the unfortunate case where we already have | |
99f8faca ILT |
642 | // entries for both NAME/VERSION and NAME/NULL. Note |
643 | // that we don't want to combine them if the existing | |
644 | // symbol is going to override the new one. FIXME: We | |
645 | // currently just test is_undefined, but this may not do | |
646 | // the right thing if the existing symbol is from a | |
647 | // shared library and the new one is from a regular | |
648 | // object. | |
649 | ||
274e99f9 | 650 | const Sized_symbol<size>* sym2; |
7d1a9ebb ILT |
651 | sym2 = this->get_sized_symbol<size>(insdef.first->second); |
652 | Symbol_table::resolve<size, big_endian>(ret, sym2, version); | |
14bfc3f5 ILT |
653 | this->make_forwarder(insdef.first->second, ret); |
654 | insdef.first->second = ret; | |
655 | } | |
479f6503 ILT |
656 | else |
657 | def = false; | |
14bfc3f5 ILT |
658 | } |
659 | } | |
660 | else | |
661 | { | |
662 | // This is the first time we have seen NAME/VERSION. | |
a3ad94ed | 663 | gold_assert(ins.first->second == NULL); |
ead1e424 | 664 | |
14bfc3f5 ILT |
665 | if (def && !insdef.second) |
666 | { | |
14b31740 ILT |
667 | // We already have an entry for NAME/NULL. If we override |
668 | // it, then change it to NAME/VERSION. | |
7d1a9ebb | 669 | ret = this->get_sized_symbol<size>(insdef.first->second); |
18e6b24e ILT |
670 | |
671 | was_undefined = ret->is_undefined(); | |
672 | was_common = ret->is_common(); | |
673 | ||
70e654ba | 674 | this->resolve(ret, sym, orig_sym, object, version); |
14bfc3f5 ILT |
675 | ins.first->second = ret; |
676 | } | |
677 | else | |
678 | { | |
18e6b24e ILT |
679 | was_undefined = false; |
680 | was_common = false; | |
681 | ||
f6ce93d6 | 682 | Sized_target<size, big_endian>* target = |
7d1a9ebb | 683 | object->sized_target<size, big_endian>(); |
1564db8d ILT |
684 | if (!target->has_make_symbol()) |
685 | ret = new Sized_symbol<size>(); | |
686 | else | |
14bfc3f5 | 687 | { |
1564db8d ILT |
688 | ret = target->make_symbol(); |
689 | if (ret == NULL) | |
14bfc3f5 ILT |
690 | { |
691 | // This means that we don't want a symbol table | |
692 | // entry after all. | |
693 | if (!def) | |
694 | this->table_.erase(ins.first); | |
695 | else | |
696 | { | |
697 | this->table_.erase(insdef.first); | |
698 | // Inserting insdef invalidated ins. | |
f0641a0b ILT |
699 | this->table_.erase(std::make_pair(name_key, |
700 | version_key)); | |
14bfc3f5 ILT |
701 | } |
702 | return NULL; | |
703 | } | |
704 | } | |
14bfc3f5 | 705 | |
1564db8d ILT |
706 | ret->init(name, version, object, sym); |
707 | ||
14bfc3f5 ILT |
708 | ins.first->second = ret; |
709 | if (def) | |
710 | { | |
711 | // This is the first time we have seen NAME/NULL. Point | |
712 | // it at the new entry for NAME/VERSION. | |
a3ad94ed | 713 | gold_assert(insdef.second); |
14bfc3f5 ILT |
714 | insdef.first->second = ret; |
715 | } | |
716 | } | |
717 | } | |
718 | ||
ead1e424 ILT |
719 | // Record every time we see a new undefined symbol, to speed up |
720 | // archive groups. | |
721 | if (!was_undefined && ret->is_undefined()) | |
722 | ++this->saw_undefined_; | |
723 | ||
724 | // Keep track of common symbols, to speed up common symbol | |
725 | // allocation. | |
726 | if (!was_common && ret->is_common()) | |
155a0dd7 ILT |
727 | { |
728 | if (ret->type() != elfcpp::STT_TLS) | |
729 | this->commons_.push_back(ret); | |
730 | else | |
731 | this->tls_commons_.push_back(ret); | |
732 | } | |
ead1e424 | 733 | |
be3e6201 ILT |
734 | if (def) |
735 | ret->set_is_default(); | |
14bfc3f5 ILT |
736 | return ret; |
737 | } | |
738 | ||
f6ce93d6 | 739 | // Add all the symbols in a relocatable object to the hash table. |
14bfc3f5 ILT |
740 | |
741 | template<int size, bool big_endian> | |
742 | void | |
dbe717ef ILT |
743 | Symbol_table::add_from_relobj( |
744 | Sized_relobj<size, big_endian>* relobj, | |
f6ce93d6 | 745 | const unsigned char* syms, |
14bfc3f5 ILT |
746 | size_t count, |
747 | const char* sym_names, | |
748 | size_t sym_name_size, | |
730cdc88 | 749 | typename Sized_relobj<size, big_endian>::Symbols* sympointers) |
14bfc3f5 | 750 | { |
9025d29d | 751 | gold_assert(size == relobj->target()->get_size()); |
8851ecca | 752 | gold_assert(size == parameters->target().get_size()); |
14bfc3f5 | 753 | |
a783673b ILT |
754 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
755 | ||
88dd47ac ILT |
756 | const bool just_symbols = relobj->just_symbols(); |
757 | ||
f6ce93d6 | 758 | const unsigned char* p = syms; |
a783673b | 759 | for (size_t i = 0; i < count; ++i, p += sym_size) |
14bfc3f5 ILT |
760 | { |
761 | elfcpp::Sym<size, big_endian> sym(p); | |
a783673b | 762 | elfcpp::Sym<size, big_endian>* psym = &sym; |
14bfc3f5 | 763 | |
a783673b | 764 | unsigned int st_name = psym->get_st_name(); |
14bfc3f5 ILT |
765 | if (st_name >= sym_name_size) |
766 | { | |
75f2446e ILT |
767 | relobj->error(_("bad global symbol name offset %u at %zu"), |
768 | st_name, i); | |
769 | continue; | |
14bfc3f5 ILT |
770 | } |
771 | ||
dbe717ef ILT |
772 | const char* name = sym_names + st_name; |
773 | ||
a783673b ILT |
774 | // A symbol defined in a section which we are not including must |
775 | // be treated as an undefined symbol. | |
776 | unsigned char symbuf[sym_size]; | |
777 | elfcpp::Sym<size, big_endian> sym2(symbuf); | |
778 | unsigned int st_shndx = psym->get_st_shndx(); | |
779 | if (st_shndx != elfcpp::SHN_UNDEF | |
780 | && st_shndx < elfcpp::SHN_LORESERVE | |
dbe717ef | 781 | && !relobj->is_section_included(st_shndx)) |
a783673b ILT |
782 | { |
783 | memcpy(symbuf, p, sym_size); | |
784 | elfcpp::Sym_write<size, big_endian> sw(symbuf); | |
785 | sw.put_st_shndx(elfcpp::SHN_UNDEF); | |
786 | psym = &sym2; | |
787 | } | |
788 | ||
14bfc3f5 ILT |
789 | // In an object file, an '@' in the name separates the symbol |
790 | // name from the version name. If there are two '@' characters, | |
791 | // this is the default version. | |
792 | const char* ver = strchr(name, '@'); | |
09124467 | 793 | int namelen = 0; |
55a93433 | 794 | // DEF: is the version default? LOCAL: is the symbol forced local? |
09124467 | 795 | bool def = false; |
55a93433 | 796 | bool local = false; |
09124467 ILT |
797 | |
798 | if (ver != NULL) | |
799 | { | |
800 | // The symbol name is of the form foo@VERSION or foo@@VERSION | |
801 | namelen = ver - name; | |
802 | ++ver; | |
803 | if (*ver == '@') | |
804 | { | |
805 | def = true; | |
806 | ++ver; | |
807 | } | |
808 | } | |
5871526f ILT |
809 | // We don't want to assign a version to an undefined symbol, |
810 | // even if it is listed in the version script. FIXME: What | |
811 | // about a common symbol? | |
812 | else if (!version_script_.empty() | |
813 | && psym->get_st_shndx() != elfcpp::SHN_UNDEF) | |
09124467 ILT |
814 | { |
815 | // The symbol name did not have a version, but | |
816 | // the version script may assign a version anyway. | |
817 | namelen = strlen(name); | |
818 | def = true; | |
55a93433 | 819 | // Check the global: entries from the version script. |
09124467 ILT |
820 | const std::string& version = |
821 | version_script_.get_symbol_version(name); | |
822 | if (!version.empty()) | |
823 | ver = version.c_str(); | |
55a93433 ILT |
824 | // Check the local: entries from the version script |
825 | if (version_script_.symbol_is_local(name)) | |
826 | local = true; | |
09124467 | 827 | } |
14bfc3f5 | 828 | |
88dd47ac ILT |
829 | if (just_symbols) |
830 | { | |
831 | if (psym != &sym2) | |
832 | memcpy(symbuf, p, sym_size); | |
833 | elfcpp::Sym_write<size, big_endian> sw(symbuf); | |
834 | sw.put_st_shndx(elfcpp::SHN_ABS); | |
835 | if (st_shndx != elfcpp::SHN_UNDEF | |
836 | && st_shndx < elfcpp::SHN_LORESERVE) | |
837 | { | |
838 | // Symbol values in object files are section relative. | |
839 | // This is normally what we want, but since here we are | |
840 | // converting the symbol to absolute we need to add the | |
841 | // section address. The section address in an object | |
842 | // file is normally zero, but people can use a linker | |
843 | // script to change it. | |
844 | sw.put_st_value(sym2.get_st_value() | |
845 | + relobj->section_address(st_shndx)); | |
846 | } | |
847 | psym = &sym2; | |
848 | } | |
849 | ||
aeddab66 | 850 | Sized_symbol<size>* res; |
14bfc3f5 ILT |
851 | if (ver == NULL) |
852 | { | |
f0641a0b | 853 | Stringpool::Key name_key; |
cfd73a4e | 854 | name = this->namepool_.add(name, true, &name_key); |
dbe717ef | 855 | res = this->add_from_object(relobj, name, name_key, NULL, 0, |
70e654ba | 856 | false, *psym, sym); |
55a93433 ILT |
857 | if (local) |
858 | this->force_local(res); | |
14bfc3f5 ILT |
859 | } |
860 | else | |
861 | { | |
f0641a0b | 862 | Stringpool::Key name_key; |
09124467 | 863 | name = this->namepool_.add_with_length(name, namelen, true, |
c0873094 | 864 | &name_key); |
f0641a0b | 865 | Stringpool::Key ver_key; |
cfd73a4e | 866 | ver = this->namepool_.add(ver, true, &ver_key); |
f0641a0b | 867 | |
dbe717ef | 868 | res = this->add_from_object(relobj, name, name_key, ver, ver_key, |
70e654ba | 869 | def, *psym, sym); |
14bfc3f5 ILT |
870 | } |
871 | ||
730cdc88 | 872 | (*sympointers)[i] = res; |
14bfc3f5 ILT |
873 | } |
874 | } | |
875 | ||
dbe717ef ILT |
876 | // Add all the symbols in a dynamic object to the hash table. |
877 | ||
878 | template<int size, bool big_endian> | |
879 | void | |
880 | Symbol_table::add_from_dynobj( | |
881 | Sized_dynobj<size, big_endian>* dynobj, | |
882 | const unsigned char* syms, | |
883 | size_t count, | |
884 | const char* sym_names, | |
885 | size_t sym_name_size, | |
886 | const unsigned char* versym, | |
887 | size_t versym_size, | |
888 | const std::vector<const char*>* version_map) | |
889 | { | |
9025d29d | 890 | gold_assert(size == dynobj->target()->get_size()); |
8851ecca | 891 | gold_assert(size == parameters->target().get_size()); |
dbe717ef | 892 | |
88dd47ac ILT |
893 | if (dynobj->just_symbols()) |
894 | { | |
895 | gold_error(_("--just-symbols does not make sense with a shared object")); | |
896 | return; | |
897 | } | |
898 | ||
dbe717ef ILT |
899 | if (versym != NULL && versym_size / 2 < count) |
900 | { | |
75f2446e ILT |
901 | dynobj->error(_("too few symbol versions")); |
902 | return; | |
dbe717ef ILT |
903 | } |
904 | ||
905 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
906 | ||
aeddab66 ILT |
907 | // We keep a list of all STT_OBJECT symbols, so that we can resolve |
908 | // weak aliases. This is necessary because if the dynamic object | |
909 | // provides the same variable under two names, one of which is a | |
910 | // weak definition, and the regular object refers to the weak | |
911 | // definition, we have to put both the weak definition and the | |
912 | // strong definition into the dynamic symbol table. Given a weak | |
913 | // definition, the only way that we can find the corresponding | |
914 | // strong definition, if any, is to search the symbol table. | |
915 | std::vector<Sized_symbol<size>*> object_symbols; | |
916 | ||
dbe717ef ILT |
917 | const unsigned char* p = syms; |
918 | const unsigned char* vs = versym; | |
919 | for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2) | |
920 | { | |
921 | elfcpp::Sym<size, big_endian> sym(p); | |
922 | ||
65778909 ILT |
923 | // Ignore symbols with local binding or that have |
924 | // internal or hidden visibility. | |
925 | if (sym.get_st_bind() == elfcpp::STB_LOCAL | |
926 | || sym.get_st_visibility() == elfcpp::STV_INTERNAL | |
927 | || sym.get_st_visibility() == elfcpp::STV_HIDDEN) | |
dbe717ef ILT |
928 | continue; |
929 | ||
930 | unsigned int st_name = sym.get_st_name(); | |
931 | if (st_name >= sym_name_size) | |
932 | { | |
75f2446e ILT |
933 | dynobj->error(_("bad symbol name offset %u at %zu"), |
934 | st_name, i); | |
935 | continue; | |
dbe717ef ILT |
936 | } |
937 | ||
938 | const char* name = sym_names + st_name; | |
939 | ||
aeddab66 ILT |
940 | Sized_symbol<size>* res; |
941 | ||
dbe717ef ILT |
942 | if (versym == NULL) |
943 | { | |
944 | Stringpool::Key name_key; | |
cfd73a4e | 945 | name = this->namepool_.add(name, true, &name_key); |
aeddab66 | 946 | res = this->add_from_object(dynobj, name, name_key, NULL, 0, |
70e654ba | 947 | false, sym, sym); |
dbe717ef | 948 | } |
aeddab66 ILT |
949 | else |
950 | { | |
951 | // Read the version information. | |
dbe717ef | 952 | |
aeddab66 | 953 | unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs); |
dbe717ef | 954 | |
aeddab66 ILT |
955 | bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0; |
956 | v &= elfcpp::VERSYM_VERSION; | |
dbe717ef | 957 | |
aeddab66 ILT |
958 | // The Sun documentation says that V can be VER_NDX_LOCAL, |
959 | // or VER_NDX_GLOBAL, or a version index. The meaning of | |
960 | // VER_NDX_LOCAL is defined as "Symbol has local scope." | |
961 | // The old GNU linker will happily generate VER_NDX_LOCAL | |
962 | // for an undefined symbol. I don't know what the Sun | |
963 | // linker will generate. | |
dbe717ef | 964 | |
aeddab66 ILT |
965 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL) |
966 | && sym.get_st_shndx() != elfcpp::SHN_UNDEF) | |
967 | { | |
968 | // This symbol should not be visible outside the object. | |
969 | continue; | |
970 | } | |
64707334 | 971 | |
aeddab66 ILT |
972 | // At this point we are definitely going to add this symbol. |
973 | Stringpool::Key name_key; | |
974 | name = this->namepool_.add(name, true, &name_key); | |
dbe717ef | 975 | |
aeddab66 ILT |
976 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL) |
977 | || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL)) | |
978 | { | |
979 | // This symbol does not have a version. | |
980 | res = this->add_from_object(dynobj, name, name_key, NULL, 0, | |
70e654ba | 981 | false, sym, sym); |
aeddab66 ILT |
982 | } |
983 | else | |
984 | { | |
985 | if (v >= version_map->size()) | |
986 | { | |
987 | dynobj->error(_("versym for symbol %zu out of range: %u"), | |
988 | i, v); | |
989 | continue; | |
990 | } | |
dbe717ef | 991 | |
aeddab66 ILT |
992 | const char* version = (*version_map)[v]; |
993 | if (version == NULL) | |
994 | { | |
995 | dynobj->error(_("versym for symbol %zu has no name: %u"), | |
996 | i, v); | |
997 | continue; | |
998 | } | |
dbe717ef | 999 | |
aeddab66 ILT |
1000 | Stringpool::Key version_key; |
1001 | version = this->namepool_.add(version, true, &version_key); | |
1002 | ||
1003 | // If this is an absolute symbol, and the version name | |
1004 | // and symbol name are the same, then this is the | |
1005 | // version definition symbol. These symbols exist to | |
1006 | // support using -u to pull in particular versions. We | |
1007 | // do not want to record a version for them. | |
1008 | if (sym.get_st_shndx() == elfcpp::SHN_ABS | |
1009 | && name_key == version_key) | |
1010 | res = this->add_from_object(dynobj, name, name_key, NULL, 0, | |
70e654ba | 1011 | false, sym, sym); |
aeddab66 ILT |
1012 | else |
1013 | { | |
1014 | const bool def = (!hidden | |
1015 | && (sym.get_st_shndx() | |
1016 | != elfcpp::SHN_UNDEF)); | |
1017 | res = this->add_from_object(dynobj, name, name_key, version, | |
70e654ba | 1018 | version_key, def, sym, sym); |
aeddab66 ILT |
1019 | } |
1020 | } | |
dbe717ef ILT |
1021 | } |
1022 | ||
99a37bfd | 1023 | // Note that it is possible that RES was overridden by an |
a4bb589a | 1024 | // earlier object, in which case it can't be aliased here. |
aeddab66 | 1025 | if (sym.get_st_shndx() != elfcpp::SHN_UNDEF |
99a37bfd ILT |
1026 | && sym.get_st_type() == elfcpp::STT_OBJECT |
1027 | && res->source() == Symbol::FROM_OBJECT | |
1028 | && res->object() == dynobj) | |
aeddab66 ILT |
1029 | object_symbols.push_back(res); |
1030 | } | |
1031 | ||
1032 | this->record_weak_aliases(&object_symbols); | |
1033 | } | |
1034 | ||
1035 | // This is used to sort weak aliases. We sort them first by section | |
1036 | // index, then by offset, then by weak ahead of strong. | |
1037 | ||
1038 | template<int size> | |
1039 | class Weak_alias_sorter | |
1040 | { | |
1041 | public: | |
1042 | bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const; | |
1043 | }; | |
1044 | ||
1045 | template<int size> | |
1046 | bool | |
1047 | Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1, | |
1048 | const Sized_symbol<size>* s2) const | |
1049 | { | |
1050 | if (s1->shndx() != s2->shndx()) | |
1051 | return s1->shndx() < s2->shndx(); | |
1052 | if (s1->value() != s2->value()) | |
1053 | return s1->value() < s2->value(); | |
1054 | if (s1->binding() != s2->binding()) | |
1055 | { | |
1056 | if (s1->binding() == elfcpp::STB_WEAK) | |
1057 | return true; | |
1058 | if (s2->binding() == elfcpp::STB_WEAK) | |
1059 | return false; | |
1060 | } | |
1061 | return std::string(s1->name()) < std::string(s2->name()); | |
1062 | } | |
dbe717ef | 1063 | |
aeddab66 ILT |
1064 | // SYMBOLS is a list of object symbols from a dynamic object. Look |
1065 | // for any weak aliases, and record them so that if we add the weak | |
1066 | // alias to the dynamic symbol table, we also add the corresponding | |
1067 | // strong symbol. | |
dbe717ef | 1068 | |
aeddab66 ILT |
1069 | template<int size> |
1070 | void | |
1071 | Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols) | |
1072 | { | |
1073 | // Sort the vector by section index, then by offset, then by weak | |
1074 | // ahead of strong. | |
1075 | std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>()); | |
1076 | ||
1077 | // Walk through the vector. For each weak definition, record | |
1078 | // aliases. | |
1079 | for (typename std::vector<Sized_symbol<size>*>::const_iterator p = | |
1080 | symbols->begin(); | |
1081 | p != symbols->end(); | |
1082 | ++p) | |
1083 | { | |
1084 | if ((*p)->binding() != elfcpp::STB_WEAK) | |
1085 | continue; | |
1086 | ||
1087 | // Build a circular list of weak aliases. Each symbol points to | |
1088 | // the next one in the circular list. | |
1089 | ||
1090 | Sized_symbol<size>* from_sym = *p; | |
1091 | typename std::vector<Sized_symbol<size>*>::const_iterator q; | |
1092 | for (q = p + 1; q != symbols->end(); ++q) | |
dbe717ef | 1093 | { |
aeddab66 ILT |
1094 | if ((*q)->shndx() != from_sym->shndx() |
1095 | || (*q)->value() != from_sym->value()) | |
1096 | break; | |
1097 | ||
1098 | this->weak_aliases_[from_sym] = *q; | |
1099 | from_sym->set_has_alias(); | |
1100 | from_sym = *q; | |
dbe717ef ILT |
1101 | } |
1102 | ||
aeddab66 ILT |
1103 | if (from_sym != *p) |
1104 | { | |
1105 | this->weak_aliases_[from_sym] = *p; | |
1106 | from_sym->set_has_alias(); | |
1107 | } | |
dbe717ef | 1108 | |
aeddab66 | 1109 | p = q - 1; |
dbe717ef ILT |
1110 | } |
1111 | } | |
1112 | ||
ead1e424 ILT |
1113 | // Create and return a specially defined symbol. If ONLY_IF_REF is |
1114 | // true, then only create the symbol if there is a reference to it. | |
86f2e683 | 1115 | // If this does not return NULL, it sets *POLDSYM to the existing |
306d9ef0 | 1116 | // symbol if there is one. This canonicalizes *PNAME and *PVERSION. |
ead1e424 ILT |
1117 | |
1118 | template<int size, bool big_endian> | |
1119 | Sized_symbol<size>* | |
9b07f471 ILT |
1120 | Symbol_table::define_special_symbol(const char** pname, const char** pversion, |
1121 | bool only_if_ref, | |
7d1a9ebb | 1122 | Sized_symbol<size>** poldsym) |
ead1e424 | 1123 | { |
ead1e424 ILT |
1124 | Symbol* oldsym; |
1125 | Sized_symbol<size>* sym; | |
86f2e683 ILT |
1126 | bool add_to_table = false; |
1127 | typename Symbol_table_type::iterator add_loc = this->table_.end(); | |
ead1e424 | 1128 | |
55a93433 ILT |
1129 | // If the caller didn't give us a version, see if we get one from |
1130 | // the version script. | |
1131 | if (*pversion == NULL) | |
1132 | { | |
1133 | const std::string& v(this->version_script_.get_symbol_version(*pname)); | |
1134 | if (!v.empty()) | |
1135 | *pversion = v.c_str(); | |
1136 | } | |
1137 | ||
ead1e424 ILT |
1138 | if (only_if_ref) |
1139 | { | |
306d9ef0 | 1140 | oldsym = this->lookup(*pname, *pversion); |
f6ce93d6 | 1141 | if (oldsym == NULL || !oldsym->is_undefined()) |
ead1e424 | 1142 | return NULL; |
306d9ef0 ILT |
1143 | |
1144 | *pname = oldsym->name(); | |
1145 | *pversion = oldsym->version(); | |
ead1e424 ILT |
1146 | } |
1147 | else | |
1148 | { | |
14b31740 | 1149 | // Canonicalize NAME and VERSION. |
f0641a0b | 1150 | Stringpool::Key name_key; |
cfd73a4e | 1151 | *pname = this->namepool_.add(*pname, true, &name_key); |
ead1e424 | 1152 | |
14b31740 | 1153 | Stringpool::Key version_key = 0; |
306d9ef0 | 1154 | if (*pversion != NULL) |
cfd73a4e | 1155 | *pversion = this->namepool_.add(*pversion, true, &version_key); |
14b31740 | 1156 | |
ead1e424 | 1157 | Symbol* const snull = NULL; |
ead1e424 | 1158 | std::pair<typename Symbol_table_type::iterator, bool> ins = |
14b31740 ILT |
1159 | this->table_.insert(std::make_pair(std::make_pair(name_key, |
1160 | version_key), | |
ead1e424 ILT |
1161 | snull)); |
1162 | ||
1163 | if (!ins.second) | |
1164 | { | |
14b31740 | 1165 | // We already have a symbol table entry for NAME/VERSION. |
ead1e424 | 1166 | oldsym = ins.first->second; |
a3ad94ed | 1167 | gold_assert(oldsym != NULL); |
ead1e424 ILT |
1168 | } |
1169 | else | |
1170 | { | |
1171 | // We haven't seen this symbol before. | |
a3ad94ed | 1172 | gold_assert(ins.first->second == NULL); |
86f2e683 ILT |
1173 | add_to_table = true; |
1174 | add_loc = ins.first; | |
ead1e424 ILT |
1175 | oldsym = NULL; |
1176 | } | |
1177 | } | |
1178 | ||
8851ecca ILT |
1179 | const Target& target = parameters->target(); |
1180 | if (!target.has_make_symbol()) | |
86f2e683 ILT |
1181 | sym = new Sized_symbol<size>(); |
1182 | else | |
ead1e424 | 1183 | { |
8851ecca ILT |
1184 | gold_assert(target.get_size() == size); |
1185 | gold_assert(target.is_big_endian() ? big_endian : !big_endian); | |
86f2e683 ILT |
1186 | typedef Sized_target<size, big_endian> My_target; |
1187 | const My_target* sized_target = | |
8851ecca | 1188 | static_cast<const My_target*>(&target); |
86f2e683 ILT |
1189 | sym = sized_target->make_symbol(); |
1190 | if (sym == NULL) | |
1191 | return NULL; | |
1192 | } | |
ead1e424 | 1193 | |
86f2e683 ILT |
1194 | if (add_to_table) |
1195 | add_loc->second = sym; | |
1196 | else | |
1197 | gold_assert(oldsym != NULL); | |
ead1e424 | 1198 | |
7d1a9ebb | 1199 | *poldsym = this->get_sized_symbol<size>(oldsym); |
ead1e424 ILT |
1200 | |
1201 | return sym; | |
1202 | } | |
1203 | ||
1204 | // Define a symbol based on an Output_data. | |
1205 | ||
14b31740 | 1206 | Symbol* |
9b07f471 ILT |
1207 | Symbol_table::define_in_output_data(const char* name, |
1208 | const char* version, | |
1209 | Output_data* od, | |
1210 | uint64_t value, | |
1211 | uint64_t symsize, | |
1212 | elfcpp::STT type, | |
1213 | elfcpp::STB binding, | |
ead1e424 ILT |
1214 | elfcpp::STV visibility, |
1215 | unsigned char nonvis, | |
1216 | bool offset_is_from_end, | |
1217 | bool only_if_ref) | |
1218 | { | |
8851ecca | 1219 | if (parameters->target().get_size() == 32) |
86f2e683 ILT |
1220 | { |
1221 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
9b07f471 | 1222 | return this->do_define_in_output_data<32>(name, version, od, |
86f2e683 ILT |
1223 | value, symsize, type, binding, |
1224 | visibility, nonvis, | |
1225 | offset_is_from_end, | |
1226 | only_if_ref); | |
1227 | #else | |
1228 | gold_unreachable(); | |
1229 | #endif | |
1230 | } | |
8851ecca | 1231 | else if (parameters->target().get_size() == 64) |
86f2e683 ILT |
1232 | { |
1233 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
9b07f471 | 1234 | return this->do_define_in_output_data<64>(name, version, od, |
86f2e683 ILT |
1235 | value, symsize, type, binding, |
1236 | visibility, nonvis, | |
1237 | offset_is_from_end, | |
1238 | only_if_ref); | |
1239 | #else | |
1240 | gold_unreachable(); | |
1241 | #endif | |
1242 | } | |
ead1e424 | 1243 | else |
a3ad94ed | 1244 | gold_unreachable(); |
ead1e424 ILT |
1245 | } |
1246 | ||
1247 | // Define a symbol in an Output_data, sized version. | |
1248 | ||
1249 | template<int size> | |
14b31740 | 1250 | Sized_symbol<size>* |
ead1e424 | 1251 | Symbol_table::do_define_in_output_data( |
ead1e424 | 1252 | const char* name, |
14b31740 | 1253 | const char* version, |
ead1e424 ILT |
1254 | Output_data* od, |
1255 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
1256 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
1257 | elfcpp::STT type, | |
1258 | elfcpp::STB binding, | |
1259 | elfcpp::STV visibility, | |
1260 | unsigned char nonvis, | |
1261 | bool offset_is_from_end, | |
1262 | bool only_if_ref) | |
1263 | { | |
1264 | Sized_symbol<size>* sym; | |
86f2e683 | 1265 | Sized_symbol<size>* oldsym; |
ead1e424 | 1266 | |
8851ecca | 1267 | if (parameters->target().is_big_endian()) |
193a53d9 ILT |
1268 | { |
1269 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) | |
7d1a9ebb ILT |
1270 | sym = this->define_special_symbol<size, true>(&name, &version, |
1271 | only_if_ref, &oldsym); | |
193a53d9 ILT |
1272 | #else |
1273 | gold_unreachable(); | |
1274 | #endif | |
1275 | } | |
ead1e424 | 1276 | else |
193a53d9 ILT |
1277 | { |
1278 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) | |
7d1a9ebb ILT |
1279 | sym = this->define_special_symbol<size, false>(&name, &version, |
1280 | only_if_ref, &oldsym); | |
193a53d9 ILT |
1281 | #else |
1282 | gold_unreachable(); | |
1283 | #endif | |
1284 | } | |
ead1e424 ILT |
1285 | |
1286 | if (sym == NULL) | |
14b31740 | 1287 | return NULL; |
ead1e424 | 1288 | |
d4f5281b | 1289 | gold_assert(version == NULL || oldsym != NULL); |
ead1e424 ILT |
1290 | sym->init(name, od, value, symsize, type, binding, visibility, nonvis, |
1291 | offset_is_from_end); | |
14b31740 | 1292 | |
e5756efb | 1293 | if (oldsym == NULL) |
55a93433 ILT |
1294 | { |
1295 | if (binding == elfcpp::STB_LOCAL | |
1296 | || this->version_script_.symbol_is_local(name)) | |
1297 | this->force_local(sym); | |
1298 | return sym; | |
1299 | } | |
86f2e683 | 1300 | |
e5756efb ILT |
1301 | if (Symbol_table::should_override_with_special(oldsym)) |
1302 | this->override_with_special(oldsym, sym); | |
1303 | delete sym; | |
1304 | return oldsym; | |
ead1e424 ILT |
1305 | } |
1306 | ||
1307 | // Define a symbol based on an Output_segment. | |
1308 | ||
14b31740 | 1309 | Symbol* |
9b07f471 | 1310 | Symbol_table::define_in_output_segment(const char* name, |
14b31740 | 1311 | const char* version, Output_segment* os, |
9b07f471 ILT |
1312 | uint64_t value, |
1313 | uint64_t symsize, | |
1314 | elfcpp::STT type, | |
1315 | elfcpp::STB binding, | |
ead1e424 ILT |
1316 | elfcpp::STV visibility, |
1317 | unsigned char nonvis, | |
1318 | Symbol::Segment_offset_base offset_base, | |
1319 | bool only_if_ref) | |
1320 | { | |
8851ecca | 1321 | if (parameters->target().get_size() == 32) |
86f2e683 ILT |
1322 | { |
1323 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
9b07f471 | 1324 | return this->do_define_in_output_segment<32>(name, version, os, |
86f2e683 ILT |
1325 | value, symsize, type, |
1326 | binding, visibility, nonvis, | |
1327 | offset_base, only_if_ref); | |
1328 | #else | |
1329 | gold_unreachable(); | |
1330 | #endif | |
1331 | } | |
8851ecca | 1332 | else if (parameters->target().get_size() == 64) |
86f2e683 ILT |
1333 | { |
1334 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
9b07f471 | 1335 | return this->do_define_in_output_segment<64>(name, version, os, |
86f2e683 ILT |
1336 | value, symsize, type, |
1337 | binding, visibility, nonvis, | |
1338 | offset_base, only_if_ref); | |
1339 | #else | |
1340 | gold_unreachable(); | |
1341 | #endif | |
1342 | } | |
ead1e424 | 1343 | else |
a3ad94ed | 1344 | gold_unreachable(); |
ead1e424 ILT |
1345 | } |
1346 | ||
1347 | // Define a symbol in an Output_segment, sized version. | |
1348 | ||
1349 | template<int size> | |
14b31740 | 1350 | Sized_symbol<size>* |
ead1e424 | 1351 | Symbol_table::do_define_in_output_segment( |
ead1e424 | 1352 | const char* name, |
14b31740 | 1353 | const char* version, |
ead1e424 ILT |
1354 | Output_segment* os, |
1355 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
1356 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
1357 | elfcpp::STT type, | |
1358 | elfcpp::STB binding, | |
1359 | elfcpp::STV visibility, | |
1360 | unsigned char nonvis, | |
1361 | Symbol::Segment_offset_base offset_base, | |
1362 | bool only_if_ref) | |
1363 | { | |
1364 | Sized_symbol<size>* sym; | |
86f2e683 | 1365 | Sized_symbol<size>* oldsym; |
ead1e424 | 1366 | |
8851ecca | 1367 | if (parameters->target().is_big_endian()) |
9025d29d ILT |
1368 | { |
1369 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) | |
7d1a9ebb ILT |
1370 | sym = this->define_special_symbol<size, true>(&name, &version, |
1371 | only_if_ref, &oldsym); | |
9025d29d ILT |
1372 | #else |
1373 | gold_unreachable(); | |
1374 | #endif | |
1375 | } | |
ead1e424 | 1376 | else |
9025d29d ILT |
1377 | { |
1378 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) | |
7d1a9ebb ILT |
1379 | sym = this->define_special_symbol<size, false>(&name, &version, |
1380 | only_if_ref, &oldsym); | |
9025d29d ILT |
1381 | #else |
1382 | gold_unreachable(); | |
1383 | #endif | |
1384 | } | |
ead1e424 ILT |
1385 | |
1386 | if (sym == NULL) | |
14b31740 | 1387 | return NULL; |
ead1e424 | 1388 | |
d4f5281b | 1389 | gold_assert(version == NULL || oldsym != NULL); |
ead1e424 ILT |
1390 | sym->init(name, os, value, symsize, type, binding, visibility, nonvis, |
1391 | offset_base); | |
14b31740 | 1392 | |
e5756efb | 1393 | if (oldsym == NULL) |
55a93433 ILT |
1394 | { |
1395 | if (binding == elfcpp::STB_LOCAL | |
1396 | || this->version_script_.symbol_is_local(name)) | |
1397 | this->force_local(sym); | |
1398 | return sym; | |
1399 | } | |
86f2e683 | 1400 | |
e5756efb ILT |
1401 | if (Symbol_table::should_override_with_special(oldsym)) |
1402 | this->override_with_special(oldsym, sym); | |
1403 | delete sym; | |
1404 | return oldsym; | |
ead1e424 ILT |
1405 | } |
1406 | ||
1407 | // Define a special symbol with a constant value. It is a multiple | |
1408 | // definition error if this symbol is already defined. | |
1409 | ||
14b31740 | 1410 | Symbol* |
9b07f471 ILT |
1411 | Symbol_table::define_as_constant(const char* name, |
1412 | const char* version, | |
1413 | uint64_t value, | |
1414 | uint64_t symsize, | |
1415 | elfcpp::STT type, | |
1416 | elfcpp::STB binding, | |
1417 | elfcpp::STV visibility, | |
1418 | unsigned char nonvis, | |
caa9d5d9 ILT |
1419 | bool only_if_ref, |
1420 | bool force_override) | |
ead1e424 | 1421 | { |
8851ecca | 1422 | if (parameters->target().get_size() == 32) |
86f2e683 ILT |
1423 | { |
1424 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
9b07f471 | 1425 | return this->do_define_as_constant<32>(name, version, value, |
86f2e683 | 1426 | symsize, type, binding, |
caa9d5d9 ILT |
1427 | visibility, nonvis, only_if_ref, |
1428 | force_override); | |
86f2e683 ILT |
1429 | #else |
1430 | gold_unreachable(); | |
1431 | #endif | |
1432 | } | |
8851ecca | 1433 | else if (parameters->target().get_size() == 64) |
86f2e683 ILT |
1434 | { |
1435 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
9b07f471 | 1436 | return this->do_define_as_constant<64>(name, version, value, |
86f2e683 | 1437 | symsize, type, binding, |
caa9d5d9 ILT |
1438 | visibility, nonvis, only_if_ref, |
1439 | force_override); | |
86f2e683 ILT |
1440 | #else |
1441 | gold_unreachable(); | |
1442 | #endif | |
1443 | } | |
ead1e424 | 1444 | else |
a3ad94ed | 1445 | gold_unreachable(); |
ead1e424 ILT |
1446 | } |
1447 | ||
1448 | // Define a symbol as a constant, sized version. | |
1449 | ||
1450 | template<int size> | |
14b31740 | 1451 | Sized_symbol<size>* |
ead1e424 | 1452 | Symbol_table::do_define_as_constant( |
ead1e424 | 1453 | const char* name, |
14b31740 | 1454 | const char* version, |
ead1e424 ILT |
1455 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
1456 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
1457 | elfcpp::STT type, | |
1458 | elfcpp::STB binding, | |
1459 | elfcpp::STV visibility, | |
1460 | unsigned char nonvis, | |
caa9d5d9 ILT |
1461 | bool only_if_ref, |
1462 | bool force_override) | |
ead1e424 ILT |
1463 | { |
1464 | Sized_symbol<size>* sym; | |
86f2e683 | 1465 | Sized_symbol<size>* oldsym; |
ead1e424 | 1466 | |
8851ecca | 1467 | if (parameters->target().is_big_endian()) |
9025d29d ILT |
1468 | { |
1469 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) | |
7d1a9ebb ILT |
1470 | sym = this->define_special_symbol<size, true>(&name, &version, |
1471 | only_if_ref, &oldsym); | |
9025d29d ILT |
1472 | #else |
1473 | gold_unreachable(); | |
1474 | #endif | |
1475 | } | |
ead1e424 | 1476 | else |
9025d29d ILT |
1477 | { |
1478 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) | |
7d1a9ebb ILT |
1479 | sym = this->define_special_symbol<size, false>(&name, &version, |
1480 | only_if_ref, &oldsym); | |
9025d29d ILT |
1481 | #else |
1482 | gold_unreachable(); | |
1483 | #endif | |
1484 | } | |
ead1e424 ILT |
1485 | |
1486 | if (sym == NULL) | |
14b31740 | 1487 | return NULL; |
ead1e424 | 1488 | |
09124467 | 1489 | gold_assert(version == NULL || version == name || oldsym != NULL); |
ead1e424 | 1490 | sym->init(name, value, symsize, type, binding, visibility, nonvis); |
14b31740 | 1491 | |
e5756efb | 1492 | if (oldsym == NULL) |
55a93433 | 1493 | { |
686c8caf ILT |
1494 | // Version symbols are absolute symbols with name == version. |
1495 | // We don't want to force them to be local. | |
1496 | if ((version == NULL | |
1497 | || name != version | |
1498 | || value != 0) | |
1499 | && (binding == elfcpp::STB_LOCAL | |
1500 | || this->version_script_.symbol_is_local(name))) | |
55a93433 ILT |
1501 | this->force_local(sym); |
1502 | return sym; | |
1503 | } | |
86f2e683 | 1504 | |
caa9d5d9 | 1505 | if (force_override || Symbol_table::should_override_with_special(oldsym)) |
e5756efb ILT |
1506 | this->override_with_special(oldsym, sym); |
1507 | delete sym; | |
1508 | return oldsym; | |
ead1e424 ILT |
1509 | } |
1510 | ||
1511 | // Define a set of symbols in output sections. | |
1512 | ||
1513 | void | |
9b07f471 | 1514 | Symbol_table::define_symbols(const Layout* layout, int count, |
a445fddf ILT |
1515 | const Define_symbol_in_section* p, |
1516 | bool only_if_ref) | |
ead1e424 ILT |
1517 | { |
1518 | for (int i = 0; i < count; ++i, ++p) | |
1519 | { | |
1520 | Output_section* os = layout->find_output_section(p->output_section); | |
1521 | if (os != NULL) | |
9b07f471 | 1522 | this->define_in_output_data(p->name, NULL, os, p->value, |
14b31740 ILT |
1523 | p->size, p->type, p->binding, |
1524 | p->visibility, p->nonvis, | |
a445fddf ILT |
1525 | p->offset_is_from_end, |
1526 | only_if_ref || p->only_if_ref); | |
ead1e424 | 1527 | else |
9b07f471 | 1528 | this->define_as_constant(p->name, NULL, 0, p->size, p->type, |
ead1e424 | 1529 | p->binding, p->visibility, p->nonvis, |
caa9d5d9 ILT |
1530 | only_if_ref || p->only_if_ref, |
1531 | false); | |
ead1e424 ILT |
1532 | } |
1533 | } | |
1534 | ||
1535 | // Define a set of symbols in output segments. | |
1536 | ||
1537 | void | |
9b07f471 | 1538 | Symbol_table::define_symbols(const Layout* layout, int count, |
a445fddf ILT |
1539 | const Define_symbol_in_segment* p, |
1540 | bool only_if_ref) | |
ead1e424 ILT |
1541 | { |
1542 | for (int i = 0; i < count; ++i, ++p) | |
1543 | { | |
1544 | Output_segment* os = layout->find_output_segment(p->segment_type, | |
1545 | p->segment_flags_set, | |
1546 | p->segment_flags_clear); | |
1547 | if (os != NULL) | |
9b07f471 | 1548 | this->define_in_output_segment(p->name, NULL, os, p->value, |
14b31740 ILT |
1549 | p->size, p->type, p->binding, |
1550 | p->visibility, p->nonvis, | |
a445fddf ILT |
1551 | p->offset_base, |
1552 | only_if_ref || p->only_if_ref); | |
ead1e424 | 1553 | else |
9b07f471 | 1554 | this->define_as_constant(p->name, NULL, 0, p->size, p->type, |
ead1e424 | 1555 | p->binding, p->visibility, p->nonvis, |
caa9d5d9 ILT |
1556 | only_if_ref || p->only_if_ref, |
1557 | false); | |
ead1e424 ILT |
1558 | } |
1559 | } | |
1560 | ||
46fe1623 ILT |
1561 | // Define CSYM using a COPY reloc. POSD is the Output_data where the |
1562 | // symbol should be defined--typically a .dyn.bss section. VALUE is | |
1563 | // the offset within POSD. | |
1564 | ||
1565 | template<int size> | |
1566 | void | |
fe8718a4 | 1567 | Symbol_table::define_with_copy_reloc( |
fe8718a4 ILT |
1568 | Sized_symbol<size>* csym, |
1569 | Output_data* posd, | |
1570 | typename elfcpp::Elf_types<size>::Elf_Addr value) | |
46fe1623 ILT |
1571 | { |
1572 | gold_assert(csym->is_from_dynobj()); | |
1573 | gold_assert(!csym->is_copied_from_dynobj()); | |
1574 | Object* object = csym->object(); | |
1575 | gold_assert(object->is_dynamic()); | |
1576 | Dynobj* dynobj = static_cast<Dynobj*>(object); | |
1577 | ||
1578 | // Our copied variable has to override any variable in a shared | |
1579 | // library. | |
1580 | elfcpp::STB binding = csym->binding(); | |
1581 | if (binding == elfcpp::STB_WEAK) | |
1582 | binding = elfcpp::STB_GLOBAL; | |
1583 | ||
9b07f471 | 1584 | this->define_in_output_data(csym->name(), csym->version(), |
46fe1623 ILT |
1585 | posd, value, csym->symsize(), |
1586 | csym->type(), binding, | |
1587 | csym->visibility(), csym->nonvis(), | |
1588 | false, false); | |
1589 | ||
1590 | csym->set_is_copied_from_dynobj(); | |
1591 | csym->set_needs_dynsym_entry(); | |
1592 | ||
1593 | this->copied_symbol_dynobjs_[csym] = dynobj; | |
1594 | ||
1595 | // We have now defined all aliases, but we have not entered them all | |
1596 | // in the copied_symbol_dynobjs_ map. | |
1597 | if (csym->has_alias()) | |
1598 | { | |
1599 | Symbol* sym = csym; | |
1600 | while (true) | |
1601 | { | |
1602 | sym = this->weak_aliases_[sym]; | |
1603 | if (sym == csym) | |
1604 | break; | |
1605 | gold_assert(sym->output_data() == posd); | |
1606 | ||
1607 | sym->set_is_copied_from_dynobj(); | |
1608 | this->copied_symbol_dynobjs_[sym] = dynobj; | |
1609 | } | |
1610 | } | |
1611 | } | |
1612 | ||
1613 | // SYM is defined using a COPY reloc. Return the dynamic object where | |
1614 | // the original definition was found. | |
1615 | ||
1616 | Dynobj* | |
1617 | Symbol_table::get_copy_source(const Symbol* sym) const | |
1618 | { | |
1619 | gold_assert(sym->is_copied_from_dynobj()); | |
1620 | Copied_symbol_dynobjs::const_iterator p = | |
1621 | this->copied_symbol_dynobjs_.find(sym); | |
1622 | gold_assert(p != this->copied_symbol_dynobjs_.end()); | |
1623 | return p->second; | |
1624 | } | |
1625 | ||
a3ad94ed ILT |
1626 | // Set the dynamic symbol indexes. INDEX is the index of the first |
1627 | // global dynamic symbol. Pointers to the symbols are stored into the | |
1628 | // vector SYMS. The names are added to DYNPOOL. This returns an | |
1629 | // updated dynamic symbol index. | |
1630 | ||
1631 | unsigned int | |
9b07f471 | 1632 | Symbol_table::set_dynsym_indexes(unsigned int index, |
a3ad94ed | 1633 | std::vector<Symbol*>* syms, |
14b31740 ILT |
1634 | Stringpool* dynpool, |
1635 | Versions* versions) | |
a3ad94ed ILT |
1636 | { |
1637 | for (Symbol_table_type::iterator p = this->table_.begin(); | |
1638 | p != this->table_.end(); | |
1639 | ++p) | |
1640 | { | |
1641 | Symbol* sym = p->second; | |
16649710 ILT |
1642 | |
1643 | // Note that SYM may already have a dynamic symbol index, since | |
1644 | // some symbols appear more than once in the symbol table, with | |
1645 | // and without a version. | |
1646 | ||
436ca963 | 1647 | if (!sym->should_add_dynsym_entry()) |
16649710 ILT |
1648 | sym->set_dynsym_index(-1U); |
1649 | else if (!sym->has_dynsym_index()) | |
a3ad94ed ILT |
1650 | { |
1651 | sym->set_dynsym_index(index); | |
1652 | ++index; | |
1653 | syms->push_back(sym); | |
cfd73a4e | 1654 | dynpool->add(sym->name(), false, NULL); |
14b31740 ILT |
1655 | |
1656 | // Record any version information. | |
09124467 ILT |
1657 | if (sym->version() != NULL) |
1658 | versions->record_version(this, dynpool, sym); | |
a3ad94ed ILT |
1659 | } |
1660 | } | |
1661 | ||
14b31740 ILT |
1662 | // Finish up the versions. In some cases this may add new dynamic |
1663 | // symbols. | |
9b07f471 | 1664 | index = versions->finalize(this, index, syms); |
14b31740 | 1665 | |
a3ad94ed ILT |
1666 | return index; |
1667 | } | |
1668 | ||
c06b7b0b | 1669 | // Set the final values for all the symbols. The index of the first |
55a93433 ILT |
1670 | // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the |
1671 | // file offset OFF. Add their names to POOL. Return the new file | |
1672 | // offset. Update *PLOCAL_SYMCOUNT if necessary. | |
54dc6425 | 1673 | |
75f65a3e | 1674 | off_t |
55a93433 ILT |
1675 | Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index, |
1676 | size_t dyncount, Stringpool* pool, | |
1677 | unsigned int *plocal_symcount) | |
54dc6425 | 1678 | { |
f6ce93d6 ILT |
1679 | off_t ret; |
1680 | ||
55a93433 ILT |
1681 | gold_assert(*plocal_symcount != 0); |
1682 | this->first_global_index_ = *plocal_symcount; | |
c06b7b0b | 1683 | |
16649710 ILT |
1684 | this->dynamic_offset_ = dynoff; |
1685 | this->first_dynamic_global_index_ = dyn_global_index; | |
1686 | this->dynamic_count_ = dyncount; | |
1687 | ||
8851ecca | 1688 | if (parameters->target().get_size() == 32) |
9025d29d ILT |
1689 | { |
1690 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE) | |
55a93433 | 1691 | ret = this->sized_finalize<32>(off, pool, plocal_symcount); |
9025d29d ILT |
1692 | #else |
1693 | gold_unreachable(); | |
1694 | #endif | |
1695 | } | |
8851ecca | 1696 | else if (parameters->target().get_size() == 64) |
9025d29d ILT |
1697 | { |
1698 | #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE) | |
55a93433 | 1699 | ret = this->sized_finalize<64>(off, pool, plocal_symcount); |
9025d29d ILT |
1700 | #else |
1701 | gold_unreachable(); | |
1702 | #endif | |
1703 | } | |
61ba1cf9 | 1704 | else |
a3ad94ed | 1705 | gold_unreachable(); |
f6ce93d6 ILT |
1706 | |
1707 | // Now that we have the final symbol table, we can reliably note | |
1708 | // which symbols should get warnings. | |
cb295612 | 1709 | this->warnings_.note_warnings(this); |
f6ce93d6 ILT |
1710 | |
1711 | return ret; | |
75f65a3e ILT |
1712 | } |
1713 | ||
55a93433 ILT |
1714 | // SYM is going into the symbol table at *PINDEX. Add the name to |
1715 | // POOL, update *PINDEX and *POFF. | |
1716 | ||
1717 | template<int size> | |
1718 | void | |
1719 | Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool, | |
1720 | unsigned int* pindex, off_t* poff) | |
1721 | { | |
1722 | sym->set_symtab_index(*pindex); | |
1723 | pool->add(sym->name(), false, NULL); | |
1724 | ++*pindex; | |
1725 | *poff += elfcpp::Elf_sizes<size>::sym_size; | |
1726 | } | |
1727 | ||
ead1e424 ILT |
1728 | // Set the final value for all the symbols. This is called after |
1729 | // Layout::finalize, so all the output sections have their final | |
1730 | // address. | |
75f65a3e ILT |
1731 | |
1732 | template<int size> | |
1733 | off_t | |
55a93433 ILT |
1734 | Symbol_table::sized_finalize(off_t off, Stringpool* pool, |
1735 | unsigned int* plocal_symcount) | |
75f65a3e | 1736 | { |
ead1e424 | 1737 | off = align_address(off, size >> 3); |
75f65a3e ILT |
1738 | this->offset_ = off; |
1739 | ||
55a93433 ILT |
1740 | unsigned int index = *plocal_symcount; |
1741 | const unsigned int orig_index = index; | |
c06b7b0b | 1742 | |
55a93433 ILT |
1743 | // First do all the symbols which have been forced to be local, as |
1744 | // they must appear before all global symbols. | |
1745 | for (Forced_locals::iterator p = this->forced_locals_.begin(); | |
1746 | p != this->forced_locals_.end(); | |
1747 | ++p) | |
1748 | { | |
1749 | Symbol* sym = *p; | |
1750 | gold_assert(sym->is_forced_local()); | |
1751 | if (this->sized_finalize_symbol<size>(sym)) | |
1752 | { | |
1753 | this->add_to_final_symtab<size>(sym, pool, &index, &off); | |
1754 | ++*plocal_symcount; | |
1755 | } | |
1756 | } | |
1757 | ||
1758 | // Now do all the remaining symbols. | |
c06b7b0b ILT |
1759 | for (Symbol_table_type::iterator p = this->table_.begin(); |
1760 | p != this->table_.end(); | |
1761 | ++p) | |
54dc6425 | 1762 | { |
55a93433 ILT |
1763 | Symbol* sym = p->second; |
1764 | if (this->sized_finalize_symbol<size>(sym)) | |
1765 | this->add_to_final_symtab<size>(sym, pool, &index, &off); | |
1766 | } | |
54dc6425 | 1767 | |
55a93433 | 1768 | this->output_count_ = index - orig_index; |
a3ad94ed | 1769 | |
55a93433 ILT |
1770 | return off; |
1771 | } | |
75f65a3e | 1772 | |
55a93433 ILT |
1773 | // Finalize the symbol SYM. This returns true if the symbol should be |
1774 | // added to the symbol table, false otherwise. | |
008db82e | 1775 | |
55a93433 ILT |
1776 | template<int size> |
1777 | bool | |
1778 | Symbol_table::sized_finalize_symbol(Symbol* unsized_sym) | |
1779 | { | |
1780 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym); | |
75f65a3e | 1781 | |
55a93433 ILT |
1782 | // The default version of a symbol may appear twice in the symbol |
1783 | // table. We only need to finalize it once. | |
1784 | if (sym->has_symtab_index()) | |
1785 | return false; | |
ead1e424 | 1786 | |
55a93433 ILT |
1787 | if (!sym->in_reg()) |
1788 | { | |
1789 | gold_assert(!sym->has_symtab_index()); | |
1790 | sym->set_symtab_index(-1U); | |
1791 | gold_assert(sym->dynsym_index() == -1U); | |
1792 | return false; | |
1793 | } | |
ead1e424 | 1794 | |
55a93433 | 1795 | typename Sized_symbol<size>::Value_type value; |
ead1e424 | 1796 | |
55a93433 ILT |
1797 | switch (sym->source()) |
1798 | { | |
1799 | case Symbol::FROM_OBJECT: | |
1800 | { | |
1801 | unsigned int shndx = sym->shndx(); | |
ead1e424 | 1802 | |
55a93433 ILT |
1803 | // FIXME: We need some target specific support here. |
1804 | if (shndx >= elfcpp::SHN_LORESERVE | |
0dfbdef4 ILT |
1805 | && shndx != elfcpp::SHN_ABS |
1806 | && shndx != elfcpp::SHN_COMMON) | |
55a93433 ILT |
1807 | { |
1808 | gold_error(_("%s: unsupported symbol section 0x%x"), | |
1809 | sym->demangled_name().c_str(), shndx); | |
1810 | shndx = elfcpp::SHN_UNDEF; | |
ead1e424 | 1811 | } |
ead1e424 | 1812 | |
55a93433 ILT |
1813 | Object* symobj = sym->object(); |
1814 | if (symobj->is_dynamic()) | |
ead1e424 | 1815 | { |
55a93433 ILT |
1816 | value = 0; |
1817 | shndx = elfcpp::SHN_UNDEF; | |
ead1e424 | 1818 | } |
55a93433 ILT |
1819 | else if (shndx == elfcpp::SHN_UNDEF) |
1820 | value = 0; | |
0dfbdef4 | 1821 | else if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON) |
55a93433 ILT |
1822 | value = sym->value(); |
1823 | else | |
ead1e424 | 1824 | { |
55a93433 ILT |
1825 | Relobj* relobj = static_cast<Relobj*>(symobj); |
1826 | section_offset_type secoff; | |
1827 | Output_section* os = relobj->output_section(shndx, &secoff); | |
1828 | ||
1829 | if (os == NULL) | |
ead1e424 | 1830 | { |
55a93433 ILT |
1831 | sym->set_symtab_index(-1U); |
1832 | gold_assert(sym->dynsym_index() == -1U); | |
1833 | return false; | |
ead1e424 | 1834 | } |
55a93433 ILT |
1835 | |
1836 | if (sym->type() == elfcpp::STT_TLS) | |
1837 | value = sym->value() + os->tls_offset() + secoff; | |
1838 | else | |
1839 | value = sym->value() + os->address() + secoff; | |
ead1e424 | 1840 | } |
55a93433 ILT |
1841 | } |
1842 | break; | |
1843 | ||
1844 | case Symbol::IN_OUTPUT_DATA: | |
1845 | { | |
1846 | Output_data* od = sym->output_data(); | |
155a0dd7 ILT |
1847 | value = sym->value(); |
1848 | if (sym->type() != elfcpp::STT_TLS) | |
1849 | value += od->address(); | |
1850 | else | |
1851 | { | |
1852 | Output_section* os = od->output_section(); | |
1853 | gold_assert(os != NULL); | |
1854 | value += os->tls_offset() + (od->address() - os->address()); | |
1855 | } | |
55a93433 ILT |
1856 | if (sym->offset_is_from_end()) |
1857 | value += od->data_size(); | |
1858 | } | |
1859 | break; | |
1860 | ||
1861 | case Symbol::IN_OUTPUT_SEGMENT: | |
1862 | { | |
1863 | Output_segment* os = sym->output_segment(); | |
edfbb029 CC |
1864 | value = sym->value(); |
1865 | if (sym->type() != elfcpp::STT_TLS) | |
1866 | value += os->vaddr(); | |
55a93433 ILT |
1867 | switch (sym->offset_base()) |
1868 | { | |
1869 | case Symbol::SEGMENT_START: | |
1870 | break; | |
1871 | case Symbol::SEGMENT_END: | |
1872 | value += os->memsz(); | |
1873 | break; | |
1874 | case Symbol::SEGMENT_BSS: | |
1875 | value += os->filesz(); | |
1876 | break; | |
1877 | default: | |
1878 | gold_unreachable(); | |
1879 | } | |
1880 | } | |
1881 | break; | |
ead1e424 | 1882 | |
55a93433 ILT |
1883 | case Symbol::CONSTANT: |
1884 | value = sym->value(); | |
1885 | break; | |
ead1e424 | 1886 | |
55a93433 ILT |
1887 | default: |
1888 | gold_unreachable(); | |
1889 | } | |
ead1e424 | 1890 | |
55a93433 | 1891 | sym->set_value(value); |
9e2dcb77 | 1892 | |
8851ecca | 1893 | if (parameters->options().strip_all()) |
55a93433 ILT |
1894 | { |
1895 | sym->set_symtab_index(-1U); | |
1896 | return false; | |
54dc6425 | 1897 | } |
75f65a3e | 1898 | |
55a93433 | 1899 | return true; |
54dc6425 ILT |
1900 | } |
1901 | ||
61ba1cf9 ILT |
1902 | // Write out the global symbols. |
1903 | ||
1904 | void | |
9a2d6984 ILT |
1905 | Symbol_table::write_globals(const Input_objects* input_objects, |
1906 | const Stringpool* sympool, | |
16649710 | 1907 | const Stringpool* dynpool, Output_file* of) const |
61ba1cf9 | 1908 | { |
8851ecca | 1909 | switch (parameters->size_and_endianness()) |
61ba1cf9 | 1910 | { |
9025d29d | 1911 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca ILT |
1912 | case Parameters::TARGET_32_LITTLE: |
1913 | this->sized_write_globals<32, false>(input_objects, sympool, | |
1914 | dynpool, of); | |
1915 | break; | |
9025d29d | 1916 | #endif |
8851ecca ILT |
1917 | #ifdef HAVE_TARGET_32_BIG |
1918 | case Parameters::TARGET_32_BIG: | |
1919 | this->sized_write_globals<32, true>(input_objects, sympool, | |
1920 | dynpool, of); | |
1921 | break; | |
9025d29d | 1922 | #endif |
9025d29d | 1923 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca ILT |
1924 | case Parameters::TARGET_64_LITTLE: |
1925 | this->sized_write_globals<64, false>(input_objects, sympool, | |
1926 | dynpool, of); | |
1927 | break; | |
9025d29d | 1928 | #endif |
8851ecca ILT |
1929 | #ifdef HAVE_TARGET_64_BIG |
1930 | case Parameters::TARGET_64_BIG: | |
1931 | this->sized_write_globals<64, true>(input_objects, sympool, | |
1932 | dynpool, of); | |
1933 | break; | |
1934 | #endif | |
1935 | default: | |
1936 | gold_unreachable(); | |
61ba1cf9 | 1937 | } |
61ba1cf9 ILT |
1938 | } |
1939 | ||
1940 | // Write out the global symbols. | |
1941 | ||
1942 | template<int size, bool big_endian> | |
1943 | void | |
9a2d6984 | 1944 | Symbol_table::sized_write_globals(const Input_objects* input_objects, |
61ba1cf9 | 1945 | const Stringpool* sympool, |
16649710 | 1946 | const Stringpool* dynpool, |
61ba1cf9 ILT |
1947 | Output_file* of) const |
1948 | { | |
8851ecca | 1949 | const Target& target = parameters->target(); |
9a2d6984 | 1950 | |
61ba1cf9 | 1951 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
55a93433 ILT |
1952 | |
1953 | const unsigned int output_count = this->output_count_; | |
1954 | const section_size_type oview_size = output_count * sym_size; | |
1955 | const unsigned int first_global_index = this->first_global_index_; | |
5fe2a0f5 ILT |
1956 | unsigned char* psyms; |
1957 | if (this->offset_ == 0 || output_count == 0) | |
1958 | psyms = NULL; | |
1959 | else | |
1960 | psyms = of->get_output_view(this->offset_, oview_size); | |
16649710 | 1961 | |
55a93433 ILT |
1962 | const unsigned int dynamic_count = this->dynamic_count_; |
1963 | const section_size_type dynamic_size = dynamic_count * sym_size; | |
1964 | const unsigned int first_dynamic_global_index = | |
1965 | this->first_dynamic_global_index_; | |
16649710 | 1966 | unsigned char* dynamic_view; |
5fe2a0f5 | 1967 | if (this->dynamic_offset_ == 0 || dynamic_count == 0) |
16649710 ILT |
1968 | dynamic_view = NULL; |
1969 | else | |
1970 | dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size); | |
c06b7b0b | 1971 | |
61ba1cf9 ILT |
1972 | for (Symbol_table_type::const_iterator p = this->table_.begin(); |
1973 | p != this->table_.end(); | |
1974 | ++p) | |
1975 | { | |
1976 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); | |
1977 | ||
9a2d6984 ILT |
1978 | // Possibly warn about unresolved symbols in shared libraries. |
1979 | this->warn_about_undefined_dynobj_symbol(input_objects, sym); | |
e2827e5f | 1980 | |
a3ad94ed | 1981 | unsigned int sym_index = sym->symtab_index(); |
16649710 ILT |
1982 | unsigned int dynsym_index; |
1983 | if (dynamic_view == NULL) | |
1984 | dynsym_index = -1U; | |
1985 | else | |
1986 | dynsym_index = sym->dynsym_index(); | |
1987 | ||
1988 | if (sym_index == -1U && dynsym_index == -1U) | |
a3ad94ed ILT |
1989 | { |
1990 | // This symbol is not included in the output file. | |
1991 | continue; | |
1992 | } | |
16649710 | 1993 | |
ead1e424 | 1994 | unsigned int shndx; |
88dd47ac ILT |
1995 | typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value(); |
1996 | typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value; | |
ead1e424 ILT |
1997 | switch (sym->source()) |
1998 | { | |
1999 | case Symbol::FROM_OBJECT: | |
2000 | { | |
16649710 | 2001 | unsigned int in_shndx = sym->shndx(); |
ead1e424 ILT |
2002 | |
2003 | // FIXME: We need some target specific support here. | |
16649710 | 2004 | if (in_shndx >= elfcpp::SHN_LORESERVE |
0dfbdef4 ILT |
2005 | && in_shndx != elfcpp::SHN_ABS |
2006 | && in_shndx != elfcpp::SHN_COMMON) | |
ead1e424 | 2007 | { |
75f2446e | 2008 | gold_error(_("%s: unsupported symbol section 0x%x"), |
a2b1aa12 | 2009 | sym->demangled_name().c_str(), in_shndx); |
75f2446e | 2010 | shndx = in_shndx; |
f6ce93d6 | 2011 | } |
ead1e424 ILT |
2012 | else |
2013 | { | |
75f2446e ILT |
2014 | Object* symobj = sym->object(); |
2015 | if (symobj->is_dynamic()) | |
2016 | { | |
2017 | if (sym->needs_dynsym_value()) | |
8851ecca | 2018 | dynsym_value = target.dynsym_value(sym); |
75f2446e ILT |
2019 | shndx = elfcpp::SHN_UNDEF; |
2020 | } | |
2021 | else if (in_shndx == elfcpp::SHN_UNDEF | |
0dfbdef4 ILT |
2022 | || in_shndx == elfcpp::SHN_ABS |
2023 | || in_shndx == elfcpp::SHN_COMMON) | |
75f2446e ILT |
2024 | shndx = in_shndx; |
2025 | else | |
2026 | { | |
2027 | Relobj* relobj = static_cast<Relobj*>(symobj); | |
8383303e | 2028 | section_offset_type secoff; |
75f2446e ILT |
2029 | Output_section* os = relobj->output_section(in_shndx, |
2030 | &secoff); | |
2031 | gold_assert(os != NULL); | |
2032 | shndx = os->out_shndx(); | |
88dd47ac ILT |
2033 | |
2034 | // In object files symbol values are section | |
2035 | // relative. | |
8851ecca | 2036 | if (parameters->options().relocatable()) |
88dd47ac | 2037 | sym_value -= os->address(); |
75f2446e | 2038 | } |
ead1e424 ILT |
2039 | } |
2040 | } | |
2041 | break; | |
2042 | ||
2043 | case Symbol::IN_OUTPUT_DATA: | |
2044 | shndx = sym->output_data()->out_shndx(); | |
2045 | break; | |
2046 | ||
2047 | case Symbol::IN_OUTPUT_SEGMENT: | |
2048 | shndx = elfcpp::SHN_ABS; | |
2049 | break; | |
2050 | ||
2051 | case Symbol::CONSTANT: | |
2052 | shndx = elfcpp::SHN_ABS; | |
2053 | break; | |
2054 | ||
2055 | default: | |
a3ad94ed | 2056 | gold_unreachable(); |
ead1e424 | 2057 | } |
61ba1cf9 | 2058 | |
16649710 ILT |
2059 | if (sym_index != -1U) |
2060 | { | |
55a93433 ILT |
2061 | sym_index -= first_global_index; |
2062 | gold_assert(sym_index < output_count); | |
2063 | unsigned char* ps = psyms + (sym_index * sym_size); | |
7d1a9ebb ILT |
2064 | this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx, |
2065 | sympool, ps); | |
16649710 | 2066 | } |
61ba1cf9 | 2067 | |
16649710 ILT |
2068 | if (dynsym_index != -1U) |
2069 | { | |
2070 | dynsym_index -= first_dynamic_global_index; | |
2071 | gold_assert(dynsym_index < dynamic_count); | |
2072 | unsigned char* pd = dynamic_view + (dynsym_index * sym_size); | |
7d1a9ebb ILT |
2073 | this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx, |
2074 | dynpool, pd); | |
16649710 | 2075 | } |
61ba1cf9 ILT |
2076 | } |
2077 | ||
c06b7b0b | 2078 | of->write_output_view(this->offset_, oview_size, psyms); |
16649710 ILT |
2079 | if (dynamic_view != NULL) |
2080 | of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view); | |
2081 | } | |
2082 | ||
2083 | // Write out the symbol SYM, in section SHNDX, to P. POOL is the | |
2084 | // strtab holding the name. | |
2085 | ||
2086 | template<int size, bool big_endian> | |
2087 | void | |
ab5c9e90 ILT |
2088 | Symbol_table::sized_write_symbol( |
2089 | Sized_symbol<size>* sym, | |
2090 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
2091 | unsigned int shndx, | |
2092 | const Stringpool* pool, | |
7d1a9ebb | 2093 | unsigned char* p) const |
16649710 ILT |
2094 | { |
2095 | elfcpp::Sym_write<size, big_endian> osym(p); | |
2096 | osym.put_st_name(pool->get_offset(sym->name())); | |
ab5c9e90 | 2097 | osym.put_st_value(value); |
16649710 | 2098 | osym.put_st_size(sym->symsize()); |
55a93433 ILT |
2099 | // A version script may have overridden the default binding. |
2100 | if (sym->is_forced_local()) | |
2101 | osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, sym->type())); | |
2102 | else | |
2103 | osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type())); | |
16649710 ILT |
2104 | osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis())); |
2105 | osym.put_st_shndx(shndx); | |
61ba1cf9 ILT |
2106 | } |
2107 | ||
9a2d6984 ILT |
2108 | // Check for unresolved symbols in shared libraries. This is |
2109 | // controlled by the --allow-shlib-undefined option. | |
2110 | ||
2111 | // We only warn about libraries for which we have seen all the | |
2112 | // DT_NEEDED entries. We don't try to track down DT_NEEDED entries | |
2113 | // which were not seen in this link. If we didn't see a DT_NEEDED | |
2114 | // entry, we aren't going to be able to reliably report whether the | |
2115 | // symbol is undefined. | |
2116 | ||
2117 | // We also don't warn about libraries found in the system library | |
2118 | // directory (the directory were we find libc.so); we assume that | |
2119 | // those libraries are OK. This heuristic avoids problems in | |
2120 | // GNU/Linux, in which -ldl can have undefined references satisfied by | |
2121 | // ld-linux.so. | |
2122 | ||
2123 | inline void | |
2124 | Symbol_table::warn_about_undefined_dynobj_symbol( | |
2125 | const Input_objects* input_objects, | |
2126 | Symbol* sym) const | |
2127 | { | |
2128 | if (sym->source() == Symbol::FROM_OBJECT | |
2129 | && sym->object()->is_dynamic() | |
2130 | && sym->shndx() == elfcpp::SHN_UNDEF | |
2131 | && sym->binding() != elfcpp::STB_WEAK | |
8851ecca ILT |
2132 | && !parameters->options().allow_shlib_undefined() |
2133 | && !parameters->target().is_defined_by_abi(sym) | |
9a2d6984 ILT |
2134 | && !input_objects->found_in_system_library_directory(sym->object())) |
2135 | { | |
2136 | // A very ugly cast. | |
2137 | Dynobj* dynobj = static_cast<Dynobj*>(sym->object()); | |
2138 | if (!dynobj->has_unknown_needed_entries()) | |
2139 | gold_error(_("%s: undefined reference to '%s'"), | |
a2b1aa12 ILT |
2140 | sym->object()->name().c_str(), |
2141 | sym->demangled_name().c_str()); | |
9a2d6984 ILT |
2142 | } |
2143 | } | |
2144 | ||
a3ad94ed ILT |
2145 | // Write out a section symbol. Return the update offset. |
2146 | ||
2147 | void | |
9025d29d | 2148 | Symbol_table::write_section_symbol(const Output_section *os, |
a3ad94ed ILT |
2149 | Output_file* of, |
2150 | off_t offset) const | |
2151 | { | |
8851ecca | 2152 | switch (parameters->size_and_endianness()) |
a3ad94ed | 2153 | { |
9025d29d | 2154 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca ILT |
2155 | case Parameters::TARGET_32_LITTLE: |
2156 | this->sized_write_section_symbol<32, false>(os, of, offset); | |
2157 | break; | |
9025d29d | 2158 | #endif |
8851ecca ILT |
2159 | #ifdef HAVE_TARGET_32_BIG |
2160 | case Parameters::TARGET_32_BIG: | |
2161 | this->sized_write_section_symbol<32, true>(os, of, offset); | |
2162 | break; | |
9025d29d | 2163 | #endif |
9025d29d | 2164 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca ILT |
2165 | case Parameters::TARGET_64_LITTLE: |
2166 | this->sized_write_section_symbol<64, false>(os, of, offset); | |
2167 | break; | |
9025d29d | 2168 | #endif |
8851ecca ILT |
2169 | #ifdef HAVE_TARGET_64_BIG |
2170 | case Parameters::TARGET_64_BIG: | |
2171 | this->sized_write_section_symbol<64, true>(os, of, offset); | |
2172 | break; | |
2173 | #endif | |
2174 | default: | |
2175 | gold_unreachable(); | |
a3ad94ed | 2176 | } |
a3ad94ed ILT |
2177 | } |
2178 | ||
2179 | // Write out a section symbol, specialized for size and endianness. | |
2180 | ||
2181 | template<int size, bool big_endian> | |
2182 | void | |
2183 | Symbol_table::sized_write_section_symbol(const Output_section* os, | |
2184 | Output_file* of, | |
2185 | off_t offset) const | |
2186 | { | |
2187 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
2188 | ||
2189 | unsigned char* pov = of->get_output_view(offset, sym_size); | |
2190 | ||
2191 | elfcpp::Sym_write<size, big_endian> osym(pov); | |
2192 | osym.put_st_name(0); | |
2193 | osym.put_st_value(os->address()); | |
2194 | osym.put_st_size(0); | |
2195 | osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, | |
2196 | elfcpp::STT_SECTION)); | |
2197 | osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0)); | |
2198 | osym.put_st_shndx(os->out_shndx()); | |
2199 | ||
2200 | of->write_output_view(offset, sym_size, pov); | |
2201 | } | |
2202 | ||
abaa3995 ILT |
2203 | // Print statistical information to stderr. This is used for --stats. |
2204 | ||
2205 | void | |
2206 | Symbol_table::print_stats() const | |
2207 | { | |
2208 | #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP) | |
2209 | fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"), | |
2210 | program_name, this->table_.size(), this->table_.bucket_count()); | |
2211 | #else | |
2212 | fprintf(stderr, _("%s: symbol table entries: %zu\n"), | |
2213 | program_name, this->table_.size()); | |
2214 | #endif | |
ad8f37d1 | 2215 | this->namepool_.print_stats("symbol table stringpool"); |
abaa3995 ILT |
2216 | } |
2217 | ||
ff541f30 ILT |
2218 | // We check for ODR violations by looking for symbols with the same |
2219 | // name for which the debugging information reports that they were | |
2220 | // defined in different source locations. When comparing the source | |
2221 | // location, we consider instances with the same base filename and | |
2222 | // line number to be the same. This is because different object | |
2223 | // files/shared libraries can include the same header file using | |
2224 | // different paths, and we don't want to report an ODR violation in | |
2225 | // that case. | |
2226 | ||
2227 | // This struct is used to compare line information, as returned by | |
7bf1f802 | 2228 | // Dwarf_line_info::one_addr2line. It implements a < comparison |
ff541f30 ILT |
2229 | // operator used with std::set. |
2230 | ||
2231 | struct Odr_violation_compare | |
2232 | { | |
2233 | bool | |
2234 | operator()(const std::string& s1, const std::string& s2) const | |
2235 | { | |
2236 | std::string::size_type pos1 = s1.rfind('/'); | |
2237 | std::string::size_type pos2 = s2.rfind('/'); | |
2238 | if (pos1 == std::string::npos | |
2239 | || pos2 == std::string::npos) | |
2240 | return s1 < s2; | |
2241 | return s1.compare(pos1, std::string::npos, | |
2242 | s2, pos2, std::string::npos) < 0; | |
2243 | } | |
2244 | }; | |
2245 | ||
70e654ba ILT |
2246 | // Check candidate_odr_violations_ to find symbols with the same name |
2247 | // but apparently different definitions (different source-file/line-no). | |
2248 | ||
2249 | void | |
17a1d0a9 ILT |
2250 | Symbol_table::detect_odr_violations(const Task* task, |
2251 | const char* output_file_name) const | |
70e654ba ILT |
2252 | { |
2253 | for (Odr_map::const_iterator it = candidate_odr_violations_.begin(); | |
2254 | it != candidate_odr_violations_.end(); | |
2255 | ++it) | |
2256 | { | |
2257 | const char* symbol_name = it->first; | |
2258 | // We use a sorted set so the output is deterministic. | |
ff541f30 | 2259 | std::set<std::string, Odr_violation_compare> line_nums; |
70e654ba | 2260 | |
b01c0a4a ILT |
2261 | for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator |
2262 | locs = it->second.begin(); | |
2263 | locs != it->second.end(); | |
2264 | ++locs) | |
70e654ba ILT |
2265 | { |
2266 | // We need to lock the object in order to read it. This | |
17a1d0a9 ILT |
2267 | // means that we have to run in a singleton Task. If we |
2268 | // want to run this in a general Task for better | |
2269 | // performance, we will need one Task for object, plus | |
2270 | // appropriate locking to ensure that we don't conflict with | |
2271 | // other uses of the object. | |
2272 | Task_lock_obj<Object> tl(task, locs->object); | |
a55ce7fe ILT |
2273 | std::string lineno = Dwarf_line_info::one_addr2line( |
2274 | locs->object, locs->shndx, locs->offset); | |
70e654ba ILT |
2275 | if (!lineno.empty()) |
2276 | line_nums.insert(lineno); | |
2277 | } | |
2278 | ||
2279 | if (line_nums.size() > 1) | |
2280 | { | |
dd8670e5 | 2281 | gold_warning(_("while linking %s: symbol '%s' defined in multiple " |
78f15696 | 2282 | "places (possible ODR violation):"), |
a2b1aa12 | 2283 | output_file_name, demangle(symbol_name).c_str()); |
70e654ba ILT |
2284 | for (std::set<std::string>::const_iterator it2 = line_nums.begin(); |
2285 | it2 != line_nums.end(); | |
2286 | ++it2) | |
2287 | fprintf(stderr, " %s\n", it2->c_str()); | |
2288 | } | |
2289 | } | |
2290 | } | |
2291 | ||
f6ce93d6 ILT |
2292 | // Warnings functions. |
2293 | ||
2294 | // Add a new warning. | |
2295 | ||
2296 | void | |
2297 | Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj, | |
cb295612 | 2298 | const std::string& warning) |
f6ce93d6 ILT |
2299 | { |
2300 | name = symtab->canonicalize_name(name); | |
cb295612 | 2301 | this->warnings_[name].set(obj, warning); |
f6ce93d6 ILT |
2302 | } |
2303 | ||
2304 | // Look through the warnings and mark the symbols for which we should | |
2305 | // warn. This is called during Layout::finalize when we know the | |
2306 | // sources for all the symbols. | |
2307 | ||
2308 | void | |
cb295612 | 2309 | Warnings::note_warnings(Symbol_table* symtab) |
f6ce93d6 ILT |
2310 | { |
2311 | for (Warning_table::iterator p = this->warnings_.begin(); | |
2312 | p != this->warnings_.end(); | |
2313 | ++p) | |
2314 | { | |
2315 | Symbol* sym = symtab->lookup(p->first, NULL); | |
2316 | if (sym != NULL | |
2317 | && sym->source() == Symbol::FROM_OBJECT | |
2318 | && sym->object() == p->second.object) | |
cb295612 | 2319 | sym->set_has_warning(); |
f6ce93d6 ILT |
2320 | } |
2321 | } | |
2322 | ||
2323 | // Issue a warning. This is called when we see a relocation against a | |
2324 | // symbol for which has a warning. | |
2325 | ||
75f2446e | 2326 | template<int size, bool big_endian> |
f6ce93d6 | 2327 | void |
75f2446e ILT |
2328 | Warnings::issue_warning(const Symbol* sym, |
2329 | const Relocate_info<size, big_endian>* relinfo, | |
2330 | size_t relnum, off_t reloffset) const | |
f6ce93d6 | 2331 | { |
a3ad94ed | 2332 | gold_assert(sym->has_warning()); |
f6ce93d6 | 2333 | Warning_table::const_iterator p = this->warnings_.find(sym->name()); |
a3ad94ed | 2334 | gold_assert(p != this->warnings_.end()); |
75f2446e ILT |
2335 | gold_warning_at_location(relinfo, relnum, reloffset, |
2336 | "%s", p->second.text.c_str()); | |
f6ce93d6 ILT |
2337 | } |
2338 | ||
14bfc3f5 ILT |
2339 | // Instantiate the templates we need. We could use the configure |
2340 | // script to restrict this to only the ones needed for implemented | |
2341 | // targets. | |
2342 | ||
c7912668 ILT |
2343 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
2344 | template | |
2345 | void | |
2346 | Sized_symbol<32>::allocate_common(Output_data*, Value_type); | |
2347 | #endif | |
2348 | ||
2349 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
2350 | template | |
2351 | void | |
2352 | Sized_symbol<64>::allocate_common(Output_data*, Value_type); | |
2353 | #endif | |
2354 | ||
193a53d9 | 2355 | #ifdef HAVE_TARGET_32_LITTLE |
14bfc3f5 ILT |
2356 | template |
2357 | void | |
193a53d9 ILT |
2358 | Symbol_table::add_from_relobj<32, false>( |
2359 | Sized_relobj<32, false>* relobj, | |
f6ce93d6 | 2360 | const unsigned char* syms, |
14bfc3f5 ILT |
2361 | size_t count, |
2362 | const char* sym_names, | |
2363 | size_t sym_name_size, | |
730cdc88 | 2364 | Sized_relobj<32, true>::Symbols* sympointers); |
193a53d9 | 2365 | #endif |
14bfc3f5 | 2366 | |
193a53d9 | 2367 | #ifdef HAVE_TARGET_32_BIG |
14bfc3f5 ILT |
2368 | template |
2369 | void | |
193a53d9 ILT |
2370 | Symbol_table::add_from_relobj<32, true>( |
2371 | Sized_relobj<32, true>* relobj, | |
f6ce93d6 | 2372 | const unsigned char* syms, |
14bfc3f5 ILT |
2373 | size_t count, |
2374 | const char* sym_names, | |
2375 | size_t sym_name_size, | |
730cdc88 | 2376 | Sized_relobj<32, false>::Symbols* sympointers); |
193a53d9 | 2377 | #endif |
14bfc3f5 | 2378 | |
193a53d9 | 2379 | #ifdef HAVE_TARGET_64_LITTLE |
14bfc3f5 ILT |
2380 | template |
2381 | void | |
193a53d9 ILT |
2382 | Symbol_table::add_from_relobj<64, false>( |
2383 | Sized_relobj<64, false>* relobj, | |
f6ce93d6 | 2384 | const unsigned char* syms, |
14bfc3f5 ILT |
2385 | size_t count, |
2386 | const char* sym_names, | |
2387 | size_t sym_name_size, | |
730cdc88 | 2388 | Sized_relobj<64, true>::Symbols* sympointers); |
193a53d9 | 2389 | #endif |
14bfc3f5 | 2390 | |
193a53d9 | 2391 | #ifdef HAVE_TARGET_64_BIG |
14bfc3f5 ILT |
2392 | template |
2393 | void | |
193a53d9 ILT |
2394 | Symbol_table::add_from_relobj<64, true>( |
2395 | Sized_relobj<64, true>* relobj, | |
f6ce93d6 | 2396 | const unsigned char* syms, |
14bfc3f5 ILT |
2397 | size_t count, |
2398 | const char* sym_names, | |
2399 | size_t sym_name_size, | |
730cdc88 | 2400 | Sized_relobj<64, false>::Symbols* sympointers); |
193a53d9 | 2401 | #endif |
14bfc3f5 | 2402 | |
193a53d9 | 2403 | #ifdef HAVE_TARGET_32_LITTLE |
dbe717ef ILT |
2404 | template |
2405 | void | |
193a53d9 ILT |
2406 | Symbol_table::add_from_dynobj<32, false>( |
2407 | Sized_dynobj<32, false>* dynobj, | |
dbe717ef ILT |
2408 | const unsigned char* syms, |
2409 | size_t count, | |
2410 | const char* sym_names, | |
2411 | size_t sym_name_size, | |
2412 | const unsigned char* versym, | |
2413 | size_t versym_size, | |
2414 | const std::vector<const char*>* version_map); | |
193a53d9 | 2415 | #endif |
dbe717ef | 2416 | |
193a53d9 | 2417 | #ifdef HAVE_TARGET_32_BIG |
dbe717ef ILT |
2418 | template |
2419 | void | |
193a53d9 ILT |
2420 | Symbol_table::add_from_dynobj<32, true>( |
2421 | Sized_dynobj<32, true>* dynobj, | |
dbe717ef ILT |
2422 | const unsigned char* syms, |
2423 | size_t count, | |
2424 | const char* sym_names, | |
2425 | size_t sym_name_size, | |
2426 | const unsigned char* versym, | |
2427 | size_t versym_size, | |
2428 | const std::vector<const char*>* version_map); | |
193a53d9 | 2429 | #endif |
dbe717ef | 2430 | |
193a53d9 | 2431 | #ifdef HAVE_TARGET_64_LITTLE |
dbe717ef ILT |
2432 | template |
2433 | void | |
193a53d9 ILT |
2434 | Symbol_table::add_from_dynobj<64, false>( |
2435 | Sized_dynobj<64, false>* dynobj, | |
dbe717ef ILT |
2436 | const unsigned char* syms, |
2437 | size_t count, | |
2438 | const char* sym_names, | |
2439 | size_t sym_name_size, | |
2440 | const unsigned char* versym, | |
2441 | size_t versym_size, | |
2442 | const std::vector<const char*>* version_map); | |
193a53d9 | 2443 | #endif |
dbe717ef | 2444 | |
193a53d9 | 2445 | #ifdef HAVE_TARGET_64_BIG |
dbe717ef ILT |
2446 | template |
2447 | void | |
193a53d9 ILT |
2448 | Symbol_table::add_from_dynobj<64, true>( |
2449 | Sized_dynobj<64, true>* dynobj, | |
dbe717ef ILT |
2450 | const unsigned char* syms, |
2451 | size_t count, | |
2452 | const char* sym_names, | |
2453 | size_t sym_name_size, | |
2454 | const unsigned char* versym, | |
2455 | size_t versym_size, | |
2456 | const std::vector<const char*>* version_map); | |
193a53d9 | 2457 | #endif |
dbe717ef | 2458 | |
46fe1623 ILT |
2459 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
2460 | template | |
2461 | void | |
fe8718a4 | 2462 | Symbol_table::define_with_copy_reloc<32>( |
fe8718a4 ILT |
2463 | Sized_symbol<32>* sym, |
2464 | Output_data* posd, | |
2465 | elfcpp::Elf_types<32>::Elf_Addr value); | |
46fe1623 ILT |
2466 | #endif |
2467 | ||
2468 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
2469 | template | |
2470 | void | |
fe8718a4 | 2471 | Symbol_table::define_with_copy_reloc<64>( |
fe8718a4 ILT |
2472 | Sized_symbol<64>* sym, |
2473 | Output_data* posd, | |
2474 | elfcpp::Elf_types<64>::Elf_Addr value); | |
46fe1623 ILT |
2475 | #endif |
2476 | ||
75f2446e ILT |
2477 | #ifdef HAVE_TARGET_32_LITTLE |
2478 | template | |
2479 | void | |
2480 | Warnings::issue_warning<32, false>(const Symbol* sym, | |
2481 | const Relocate_info<32, false>* relinfo, | |
2482 | size_t relnum, off_t reloffset) const; | |
2483 | #endif | |
2484 | ||
2485 | #ifdef HAVE_TARGET_32_BIG | |
2486 | template | |
2487 | void | |
2488 | Warnings::issue_warning<32, true>(const Symbol* sym, | |
2489 | const Relocate_info<32, true>* relinfo, | |
2490 | size_t relnum, off_t reloffset) const; | |
2491 | #endif | |
2492 | ||
2493 | #ifdef HAVE_TARGET_64_LITTLE | |
2494 | template | |
2495 | void | |
2496 | Warnings::issue_warning<64, false>(const Symbol* sym, | |
2497 | const Relocate_info<64, false>* relinfo, | |
2498 | size_t relnum, off_t reloffset) const; | |
2499 | #endif | |
2500 | ||
2501 | #ifdef HAVE_TARGET_64_BIG | |
2502 | template | |
2503 | void | |
2504 | Warnings::issue_warning<64, true>(const Symbol* sym, | |
2505 | const Relocate_info<64, true>* relinfo, | |
2506 | size_t relnum, off_t reloffset) const; | |
2507 | #endif | |
2508 | ||
14bfc3f5 | 2509 | } // End namespace gold. |