Commit | Line | Data |
---|---|---|
14bfc3f5 ILT |
1 | // symtab.cc -- the gold symbol table |
2 | ||
6d03d481 | 3 | // Copyright 2006, 2007, 2008, 2009 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 | 32 | |
6d03d481 | 33 | #include "gc.h" |
14bfc3f5 | 34 | #include "object.h" |
70e654ba | 35 | #include "dwarf_reader.h" |
dbe717ef | 36 | #include "dynobj.h" |
75f65a3e | 37 | #include "output.h" |
61ba1cf9 | 38 | #include "target.h" |
645f8123 | 39 | #include "workqueue.h" |
14bfc3f5 | 40 | #include "symtab.h" |
c82fbeee | 41 | #include "demangle.h" // needed for --dynamic-list-cpp-new |
89fc3421 | 42 | #include "plugin.h" |
14bfc3f5 ILT |
43 | |
44 | namespace gold | |
45 | { | |
46 | ||
47 | // Class Symbol. | |
48 | ||
ead1e424 ILT |
49 | // Initialize fields in Symbol. This initializes everything except u_ |
50 | // and source_. | |
14bfc3f5 | 51 | |
14bfc3f5 | 52 | void |
ead1e424 ILT |
53 | Symbol::init_fields(const char* name, const char* version, |
54 | elfcpp::STT type, elfcpp::STB binding, | |
55 | elfcpp::STV visibility, unsigned char nonvis) | |
14bfc3f5 ILT |
56 | { |
57 | this->name_ = name; | |
58 | this->version_ = version; | |
c06b7b0b ILT |
59 | this->symtab_index_ = 0; |
60 | this->dynsym_index_ = 0; | |
0a65a3a7 | 61 | this->got_offsets_.init(); |
f4151f89 | 62 | this->plt_offset_ = 0; |
ead1e424 ILT |
63 | this->type_ = type; |
64 | this->binding_ = binding; | |
65 | this->visibility_ = visibility; | |
66 | this->nonvis_ = nonvis; | |
67 | this->is_target_special_ = false; | |
1564db8d ILT |
68 | this->is_def_ = false; |
69 | this->is_forwarder_ = false; | |
aeddab66 | 70 | this->has_alias_ = false; |
c06b7b0b | 71 | this->needs_dynsym_entry_ = false; |
008db82e | 72 | this->in_reg_ = false; |
ead1e424 | 73 | this->in_dyn_ = false; |
f4151f89 | 74 | this->has_plt_offset_ = false; |
f6ce93d6 | 75 | this->has_warning_ = false; |
46fe1623 | 76 | this->is_copied_from_dynobj_ = false; |
55a93433 | 77 | this->is_forced_local_ = false; |
d491d34e | 78 | this->is_ordinary_shndx_ = false; |
89fc3421 | 79 | this->in_real_elf_ = false; |
ead1e424 ILT |
80 | } |
81 | ||
a2b1aa12 ILT |
82 | // Return the demangled version of the symbol's name, but only |
83 | // if the --demangle flag was set. | |
84 | ||
85 | static std::string | |
86 | demangle(const char* name) | |
87 | { | |
086a1841 | 88 | if (!parameters->options().do_demangle()) |
ff541f30 ILT |
89 | return name; |
90 | ||
a2b1aa12 ILT |
91 | // cplus_demangle allocates memory for the result it returns, |
92 | // and returns NULL if the name is already demangled. | |
93 | char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS); | |
94 | if (demangled_name == NULL) | |
95 | return name; | |
96 | ||
97 | std::string retval(demangled_name); | |
98 | free(demangled_name); | |
99 | return retval; | |
100 | } | |
101 | ||
102 | std::string | |
103 | Symbol::demangled_name() const | |
104 | { | |
ff541f30 | 105 | return demangle(this->name()); |
a2b1aa12 ILT |
106 | } |
107 | ||
ead1e424 ILT |
108 | // Initialize the fields in the base class Symbol for SYM in OBJECT. |
109 | ||
110 | template<int size, bool big_endian> | |
111 | void | |
f3e9c5c5 ILT |
112 | Symbol::init_base_object(const char* name, const char* version, Object* object, |
113 | const elfcpp::Sym<size, big_endian>& sym, | |
114 | unsigned int st_shndx, bool is_ordinary) | |
ead1e424 ILT |
115 | { |
116 | this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(), | |
117 | sym.get_st_visibility(), sym.get_st_nonvis()); | |
118 | this->u_.from_object.object = object; | |
d491d34e ILT |
119 | this->u_.from_object.shndx = st_shndx; |
120 | this->is_ordinary_shndx_ = is_ordinary; | |
ead1e424 | 121 | this->source_ = FROM_OBJECT; |
008db82e | 122 | this->in_reg_ = !object->is_dynamic(); |
1564db8d | 123 | this->in_dyn_ = object->is_dynamic(); |
89fc3421 | 124 | this->in_real_elf_ = object->pluginobj() == NULL; |
14bfc3f5 ILT |
125 | } |
126 | ||
ead1e424 ILT |
127 | // Initialize the fields in the base class Symbol for a symbol defined |
128 | // in an Output_data. | |
129 | ||
130 | void | |
75517b77 ILT |
131 | Symbol::init_base_output_data(const char* name, const char* version, |
132 | Output_data* od, elfcpp::STT type, | |
133 | elfcpp::STB binding, elfcpp::STV visibility, | |
134 | unsigned char nonvis, bool offset_is_from_end) | |
ead1e424 | 135 | { |
75517b77 | 136 | this->init_fields(name, version, type, binding, visibility, nonvis); |
ead1e424 ILT |
137 | this->u_.in_output_data.output_data = od; |
138 | this->u_.in_output_data.offset_is_from_end = offset_is_from_end; | |
139 | this->source_ = IN_OUTPUT_DATA; | |
008db82e | 140 | this->in_reg_ = true; |
89fc3421 | 141 | this->in_real_elf_ = true; |
ead1e424 ILT |
142 | } |
143 | ||
144 | // Initialize the fields in the base class Symbol for a symbol defined | |
145 | // in an Output_segment. | |
146 | ||
147 | void | |
75517b77 ILT |
148 | Symbol::init_base_output_segment(const char* name, const char* version, |
149 | Output_segment* os, elfcpp::STT type, | |
150 | elfcpp::STB binding, elfcpp::STV visibility, | |
151 | unsigned char nonvis, | |
f3e9c5c5 | 152 | Segment_offset_base offset_base) |
ead1e424 | 153 | { |
75517b77 | 154 | this->init_fields(name, version, type, binding, visibility, nonvis); |
ead1e424 ILT |
155 | this->u_.in_output_segment.output_segment = os; |
156 | this->u_.in_output_segment.offset_base = offset_base; | |
157 | this->source_ = IN_OUTPUT_SEGMENT; | |
008db82e | 158 | this->in_reg_ = true; |
89fc3421 | 159 | this->in_real_elf_ = true; |
ead1e424 ILT |
160 | } |
161 | ||
162 | // Initialize the fields in the base class Symbol for a symbol defined | |
163 | // as a constant. | |
164 | ||
165 | void | |
75517b77 ILT |
166 | Symbol::init_base_constant(const char* name, const char* version, |
167 | elfcpp::STT type, elfcpp::STB binding, | |
168 | elfcpp::STV visibility, unsigned char nonvis) | |
f3e9c5c5 | 169 | { |
75517b77 | 170 | this->init_fields(name, version, type, binding, visibility, nonvis); |
f3e9c5c5 ILT |
171 | this->source_ = IS_CONSTANT; |
172 | this->in_reg_ = true; | |
89fc3421 | 173 | this->in_real_elf_ = true; |
f3e9c5c5 ILT |
174 | } |
175 | ||
176 | // Initialize the fields in the base class Symbol for an undefined | |
177 | // symbol. | |
178 | ||
179 | void | |
75517b77 ILT |
180 | Symbol::init_base_undefined(const char* name, const char* version, |
181 | elfcpp::STT type, elfcpp::STB binding, | |
182 | elfcpp::STV visibility, unsigned char nonvis) | |
ead1e424 | 183 | { |
75517b77 | 184 | this->init_fields(name, version, type, binding, visibility, nonvis); |
d7ab2a47 | 185 | this->dynsym_index_ = -1U; |
f3e9c5c5 | 186 | this->source_ = IS_UNDEFINED; |
008db82e | 187 | this->in_reg_ = true; |
89fc3421 | 188 | this->in_real_elf_ = true; |
ead1e424 ILT |
189 | } |
190 | ||
c7912668 ILT |
191 | // Allocate a common symbol in the base. |
192 | ||
193 | void | |
194 | Symbol::allocate_base_common(Output_data* od) | |
195 | { | |
196 | gold_assert(this->is_common()); | |
197 | this->source_ = IN_OUTPUT_DATA; | |
198 | this->u_.in_output_data.output_data = od; | |
199 | this->u_.in_output_data.offset_is_from_end = false; | |
200 | } | |
201 | ||
ead1e424 | 202 | // Initialize the fields in Sized_symbol for SYM in OBJECT. |
14bfc3f5 ILT |
203 | |
204 | template<int size> | |
205 | template<bool big_endian> | |
206 | void | |
f3e9c5c5 ILT |
207 | Sized_symbol<size>::init_object(const char* name, const char* version, |
208 | Object* object, | |
209 | const elfcpp::Sym<size, big_endian>& sym, | |
210 | unsigned int st_shndx, bool is_ordinary) | |
14bfc3f5 | 211 | { |
f3e9c5c5 | 212 | this->init_base_object(name, version, object, sym, st_shndx, is_ordinary); |
14bfc3f5 | 213 | this->value_ = sym.get_st_value(); |
ead1e424 ILT |
214 | this->symsize_ = sym.get_st_size(); |
215 | } | |
216 | ||
217 | // Initialize the fields in Sized_symbol for a symbol defined in an | |
218 | // Output_data. | |
219 | ||
220 | template<int size> | |
221 | void | |
75517b77 ILT |
222 | Sized_symbol<size>::init_output_data(const char* name, const char* version, |
223 | Output_data* od, Value_type value, | |
224 | Size_type symsize, elfcpp::STT type, | |
225 | elfcpp::STB binding, | |
f3e9c5c5 ILT |
226 | elfcpp::STV visibility, |
227 | unsigned char nonvis, | |
228 | bool offset_is_from_end) | |
ead1e424 | 229 | { |
75517b77 ILT |
230 | this->init_base_output_data(name, version, od, type, binding, visibility, |
231 | nonvis, offset_is_from_end); | |
ead1e424 ILT |
232 | this->value_ = value; |
233 | this->symsize_ = symsize; | |
234 | } | |
235 | ||
236 | // Initialize the fields in Sized_symbol for a symbol defined in an | |
237 | // Output_segment. | |
238 | ||
239 | template<int size> | |
240 | void | |
75517b77 ILT |
241 | Sized_symbol<size>::init_output_segment(const char* name, const char* version, |
242 | Output_segment* os, Value_type value, | |
243 | Size_type symsize, elfcpp::STT type, | |
244 | elfcpp::STB binding, | |
f3e9c5c5 ILT |
245 | elfcpp::STV visibility, |
246 | unsigned char nonvis, | |
247 | Segment_offset_base offset_base) | |
ead1e424 | 248 | { |
75517b77 ILT |
249 | this->init_base_output_segment(name, version, os, type, binding, visibility, |
250 | nonvis, offset_base); | |
ead1e424 ILT |
251 | this->value_ = value; |
252 | this->symsize_ = symsize; | |
253 | } | |
254 | ||
255 | // Initialize the fields in Sized_symbol for a symbol defined as a | |
256 | // constant. | |
257 | ||
258 | template<int size> | |
259 | void | |
75517b77 ILT |
260 | Sized_symbol<size>::init_constant(const char* name, const char* version, |
261 | Value_type value, Size_type symsize, | |
262 | elfcpp::STT type, elfcpp::STB binding, | |
263 | elfcpp::STV visibility, unsigned char nonvis) | |
ead1e424 | 264 | { |
75517b77 | 265 | this->init_base_constant(name, version, type, binding, visibility, nonvis); |
ead1e424 ILT |
266 | this->value_ = value; |
267 | this->symsize_ = symsize; | |
14bfc3f5 ILT |
268 | } |
269 | ||
f3e9c5c5 ILT |
270 | // Initialize the fields in Sized_symbol for an undefined symbol. |
271 | ||
272 | template<int size> | |
273 | void | |
75517b77 ILT |
274 | Sized_symbol<size>::init_undefined(const char* name, const char* version, |
275 | elfcpp::STT type, elfcpp::STB binding, | |
276 | elfcpp::STV visibility, unsigned char nonvis) | |
f3e9c5c5 | 277 | { |
75517b77 | 278 | this->init_base_undefined(name, version, type, binding, visibility, nonvis); |
f3e9c5c5 ILT |
279 | this->value_ = 0; |
280 | this->symsize_ = 0; | |
281 | } | |
282 | ||
8a5e3e08 ILT |
283 | // Return true if SHNDX represents a common symbol. |
284 | ||
285 | bool | |
286 | Symbol::is_common_shndx(unsigned int shndx) | |
287 | { | |
288 | return (shndx == elfcpp::SHN_COMMON | |
289 | || shndx == parameters->target().small_common_shndx() | |
290 | || shndx == parameters->target().large_common_shndx()); | |
291 | } | |
292 | ||
c7912668 ILT |
293 | // Allocate a common symbol. |
294 | ||
295 | template<int size> | |
296 | void | |
297 | Sized_symbol<size>::allocate_common(Output_data* od, Value_type value) | |
298 | { | |
299 | this->allocate_base_common(od); | |
300 | this->value_ = value; | |
301 | } | |
302 | ||
c82fbeee CS |
303 | // The ""'s around str ensure str is a string literal, so sizeof works. |
304 | #define strprefix(var, str) (strncmp(var, str, sizeof("" str "") - 1) == 0) | |
305 | ||
436ca963 ILT |
306 | // Return true if this symbol should be added to the dynamic symbol |
307 | // table. | |
308 | ||
309 | inline bool | |
310 | Symbol::should_add_dynsym_entry() const | |
311 | { | |
312 | // If the symbol is used by a dynamic relocation, we need to add it. | |
313 | if (this->needs_dynsym_entry()) | |
314 | return true; | |
315 | ||
6d03d481 ST |
316 | // If this symbol's section is not added, the symbol need not be added. |
317 | // The section may have been GCed. Note that export_dynamic is being | |
318 | // overridden here. This should not be done for shared objects. | |
319 | if (parameters->options().gc_sections() | |
320 | && !parameters->options().shared() | |
321 | && this->source() == Symbol::FROM_OBJECT | |
322 | && !this->object()->is_dynamic()) | |
323 | { | |
324 | Relobj* relobj = static_cast<Relobj*>(this->object()); | |
325 | bool is_ordinary; | |
326 | unsigned int shndx = this->shndx(&is_ordinary); | |
327 | if (is_ordinary && shndx != elfcpp::SHN_UNDEF | |
328 | && !relobj->is_section_included(shndx)) | |
329 | return false; | |
330 | } | |
331 | ||
55a93433 ILT |
332 | // If the symbol was forced local in a version script, do not add it. |
333 | if (this->is_forced_local()) | |
334 | return false; | |
335 | ||
c82fbeee CS |
336 | // If the symbol was forced dynamic in a --dynamic-list file, add it. |
337 | if (parameters->options().in_dynamic_list(this->name())) | |
338 | return true; | |
339 | ||
340 | // If dynamic-list-data was specified, add any STT_OBJECT. | |
341 | if (parameters->options().dynamic_list_data() | |
342 | && !this->is_from_dynobj() | |
343 | && this->type() == elfcpp::STT_OBJECT) | |
344 | return true; | |
345 | ||
346 | // If --dynamic-list-cpp-new was specified, add any new/delete symbol. | |
347 | // If --dynamic-list-cpp-typeinfo was specified, add any typeinfo symbols. | |
348 | if ((parameters->options().dynamic_list_cpp_new() | |
349 | || parameters->options().dynamic_list_cpp_typeinfo()) | |
350 | && !this->is_from_dynobj()) | |
351 | { | |
352 | // TODO(csilvers): We could probably figure out if we're an operator | |
353 | // new/delete or typeinfo without the need to demangle. | |
354 | char* demangled_name = cplus_demangle(this->name(), | |
355 | DMGL_ANSI | DMGL_PARAMS); | |
356 | if (demangled_name == NULL) | |
357 | { | |
358 | // Not a C++ symbol, so it can't satisfy these flags | |
359 | } | |
360 | else if (parameters->options().dynamic_list_cpp_new() | |
361 | && (strprefix(demangled_name, "operator new") | |
362 | || strprefix(demangled_name, "operator delete"))) | |
363 | { | |
364 | free(demangled_name); | |
365 | return true; | |
366 | } | |
367 | else if (parameters->options().dynamic_list_cpp_typeinfo() | |
368 | && (strprefix(demangled_name, "typeinfo name for") | |
369 | || strprefix(demangled_name, "typeinfo for"))) | |
370 | { | |
371 | free(demangled_name); | |
372 | return true; | |
373 | } | |
374 | else | |
375 | free(demangled_name); | |
376 | } | |
377 | ||
436ca963 ILT |
378 | // If exporting all symbols or building a shared library, |
379 | // and the symbol is defined in a regular object and is | |
380 | // externally visible, we need to add it. | |
8851ecca | 381 | if ((parameters->options().export_dynamic() || parameters->options().shared()) |
436ca963 ILT |
382 | && !this->is_from_dynobj() |
383 | && this->is_externally_visible()) | |
384 | return true; | |
385 | ||
386 | return false; | |
387 | } | |
388 | ||
b3b74ddc ILT |
389 | // Return true if the final value of this symbol is known at link |
390 | // time. | |
391 | ||
392 | bool | |
393 | Symbol::final_value_is_known() const | |
394 | { | |
395 | // If we are not generating an executable, then no final values are | |
396 | // known, since they will change at runtime. | |
374ad285 ILT |
397 | if (parameters->options().output_is_position_independent() |
398 | || parameters->options().relocatable()) | |
b3b74ddc ILT |
399 | return false; |
400 | ||
f3e9c5c5 ILT |
401 | // If the symbol is not from an object file, and is not undefined, |
402 | // then it is defined, and known. | |
b3b74ddc | 403 | if (this->source_ != FROM_OBJECT) |
f3e9c5c5 ILT |
404 | { |
405 | if (this->source_ != IS_UNDEFINED) | |
406 | return true; | |
407 | } | |
408 | else | |
409 | { | |
410 | // If the symbol is from a dynamic object, then the final value | |
411 | // is not known. | |
412 | if (this->object()->is_dynamic()) | |
413 | return false; | |
b3b74ddc | 414 | |
f3e9c5c5 ILT |
415 | // If the symbol is not undefined (it is defined or common), |
416 | // then the final value is known. | |
417 | if (!this->is_undefined()) | |
418 | return true; | |
419 | } | |
b3b74ddc ILT |
420 | |
421 | // If the symbol is undefined, then whether the final value is known | |
422 | // depends on whether we are doing a static link. If we are doing a | |
423 | // dynamic link, then the final value could be filled in at runtime. | |
424 | // This could reasonably be the case for a weak undefined symbol. | |
425 | return parameters->doing_static_link(); | |
426 | } | |
427 | ||
77e65537 | 428 | // Return the output section where this symbol is defined. |
a445fddf | 429 | |
77e65537 ILT |
430 | Output_section* |
431 | Symbol::output_section() const | |
a445fddf ILT |
432 | { |
433 | switch (this->source_) | |
434 | { | |
435 | case FROM_OBJECT: | |
77e65537 ILT |
436 | { |
437 | unsigned int shndx = this->u_.from_object.shndx; | |
d491d34e | 438 | if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_) |
77e65537 ILT |
439 | { |
440 | gold_assert(!this->u_.from_object.object->is_dynamic()); | |
89fc3421 | 441 | gold_assert(this->u_.from_object.object->pluginobj() == NULL); |
77e65537 | 442 | Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object); |
ef9beddf | 443 | return relobj->output_section(shndx); |
77e65537 ILT |
444 | } |
445 | return NULL; | |
446 | } | |
447 | ||
a445fddf | 448 | case IN_OUTPUT_DATA: |
77e65537 ILT |
449 | return this->u_.in_output_data.output_data->output_section(); |
450 | ||
a445fddf | 451 | case IN_OUTPUT_SEGMENT: |
f3e9c5c5 ILT |
452 | case IS_CONSTANT: |
453 | case IS_UNDEFINED: | |
77e65537 ILT |
454 | return NULL; |
455 | ||
456 | default: | |
457 | gold_unreachable(); | |
458 | } | |
459 | } | |
460 | ||
461 | // Set the symbol's output section. This is used for symbols defined | |
462 | // in scripts. This should only be called after the symbol table has | |
463 | // been finalized. | |
464 | ||
465 | void | |
466 | Symbol::set_output_section(Output_section* os) | |
467 | { | |
468 | switch (this->source_) | |
469 | { | |
470 | case FROM_OBJECT: | |
471 | case IN_OUTPUT_DATA: | |
472 | gold_assert(this->output_section() == os); | |
473 | break; | |
f3e9c5c5 | 474 | case IS_CONSTANT: |
77e65537 ILT |
475 | this->source_ = IN_OUTPUT_DATA; |
476 | this->u_.in_output_data.output_data = os; | |
477 | this->u_.in_output_data.offset_is_from_end = false; | |
478 | break; | |
479 | case IN_OUTPUT_SEGMENT: | |
f3e9c5c5 | 480 | case IS_UNDEFINED: |
a445fddf ILT |
481 | default: |
482 | gold_unreachable(); | |
483 | } | |
484 | } | |
485 | ||
14bfc3f5 ILT |
486 | // Class Symbol_table. |
487 | ||
09124467 ILT |
488 | Symbol_table::Symbol_table(unsigned int count, |
489 | const Version_script_info& version_script) | |
6d013333 | 490 | : saw_undefined_(0), offset_(0), table_(count), namepool_(), |
8a5e3e08 ILT |
491 | forwarders_(), commons_(), tls_commons_(), small_commons_(), |
492 | large_commons_(), forced_locals_(), warnings_(), | |
ef15dade | 493 | version_script_(version_script), gc_(NULL), icf_(NULL) |
14bfc3f5 | 494 | { |
6d013333 | 495 | namepool_.reserve(count); |
14bfc3f5 ILT |
496 | } |
497 | ||
498 | Symbol_table::~Symbol_table() | |
499 | { | |
500 | } | |
501 | ||
ad8f37d1 | 502 | // The hash function. The key values are Stringpool keys. |
14bfc3f5 | 503 | |
ad8f37d1 | 504 | inline size_t |
14bfc3f5 ILT |
505 | Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const |
506 | { | |
f0641a0b | 507 | return key.first ^ key.second; |
14bfc3f5 ILT |
508 | } |
509 | ||
ad8f37d1 ILT |
510 | // The symbol table key equality function. This is called with |
511 | // Stringpool keys. | |
14bfc3f5 | 512 | |
ad8f37d1 | 513 | inline bool |
14bfc3f5 ILT |
514 | Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1, |
515 | const Symbol_table_key& k2) const | |
516 | { | |
517 | return k1.first == k2.first && k1.second == k2.second; | |
518 | } | |
519 | ||
ef15dade ST |
520 | bool |
521 | Symbol_table::is_section_folded(Object* obj, unsigned int shndx) const | |
522 | { | |
032ce4e9 | 523 | return (parameters->options().icf_enabled() |
ef15dade ST |
524 | && this->icf_->is_section_folded(obj, shndx)); |
525 | } | |
526 | ||
6d03d481 ST |
527 | // For symbols that have been listed with -u option, add them to the |
528 | // work list to avoid gc'ing them. | |
529 | ||
530 | void | |
531 | Symbol_table::gc_mark_undef_symbols() | |
532 | { | |
533 | for (options::String_set::const_iterator p = | |
534 | parameters->options().undefined_begin(); | |
535 | p != parameters->options().undefined_end(); | |
536 | ++p) | |
537 | { | |
538 | const char* name = p->c_str(); | |
539 | Symbol* sym = this->lookup(name); | |
540 | gold_assert (sym != NULL); | |
541 | if (sym->source() == Symbol::FROM_OBJECT | |
542 | && !sym->object()->is_dynamic()) | |
543 | { | |
544 | Relobj* obj = static_cast<Relobj*>(sym->object()); | |
545 | bool is_ordinary; | |
546 | unsigned int shndx = sym->shndx(&is_ordinary); | |
547 | if (is_ordinary) | |
548 | { | |
549 | gold_assert(this->gc_ != NULL); | |
550 | this->gc_->worklist().push(Section_id(obj, shndx)); | |
551 | } | |
552 | } | |
553 | } | |
554 | } | |
555 | ||
556 | void | |
557 | Symbol_table::gc_mark_symbol_for_shlib(Symbol* sym) | |
558 | { | |
559 | if (!sym->is_from_dynobj() | |
560 | && sym->is_externally_visible()) | |
561 | { | |
562 | //Add the object and section to the work list. | |
563 | Relobj* obj = static_cast<Relobj*>(sym->object()); | |
564 | bool is_ordinary; | |
565 | unsigned int shndx = sym->shndx(&is_ordinary); | |
566 | if (is_ordinary && shndx != elfcpp::SHN_UNDEF) | |
567 | { | |
568 | gold_assert(this->gc_!= NULL); | |
569 | this->gc_->worklist().push(Section_id(obj, shndx)); | |
570 | } | |
571 | } | |
572 | } | |
573 | ||
574 | // When doing garbage collection, keep symbols that have been seen in | |
575 | // dynamic objects. | |
576 | inline void | |
577 | Symbol_table::gc_mark_dyn_syms(Symbol* sym) | |
578 | { | |
579 | if (sym->in_dyn() && sym->source() == Symbol::FROM_OBJECT | |
580 | && !sym->object()->is_dynamic()) | |
581 | { | |
582 | Relobj *obj = static_cast<Relobj*>(sym->object()); | |
583 | bool is_ordinary; | |
584 | unsigned int shndx = sym->shndx(&is_ordinary); | |
585 | if (is_ordinary && shndx != elfcpp::SHN_UNDEF) | |
586 | { | |
587 | gold_assert(this->gc_ != NULL); | |
588 | this->gc_->worklist().push(Section_id(obj, shndx)); | |
589 | } | |
590 | } | |
591 | } | |
592 | ||
dd8670e5 | 593 | // Make TO a symbol which forwards to FROM. |
14bfc3f5 ILT |
594 | |
595 | void | |
596 | Symbol_table::make_forwarder(Symbol* from, Symbol* to) | |
597 | { | |
a3ad94ed ILT |
598 | gold_assert(from != to); |
599 | gold_assert(!from->is_forwarder() && !to->is_forwarder()); | |
14bfc3f5 ILT |
600 | this->forwarders_[from] = to; |
601 | from->set_forwarder(); | |
602 | } | |
603 | ||
61ba1cf9 ILT |
604 | // Resolve the forwards from FROM, returning the real symbol. |
605 | ||
14bfc3f5 | 606 | Symbol* |
c06b7b0b | 607 | Symbol_table::resolve_forwards(const Symbol* from) const |
14bfc3f5 | 608 | { |
a3ad94ed | 609 | gold_assert(from->is_forwarder()); |
c06b7b0b | 610 | Unordered_map<const Symbol*, Symbol*>::const_iterator p = |
14bfc3f5 | 611 | this->forwarders_.find(from); |
a3ad94ed | 612 | gold_assert(p != this->forwarders_.end()); |
14bfc3f5 ILT |
613 | return p->second; |
614 | } | |
615 | ||
61ba1cf9 ILT |
616 | // Look up a symbol by name. |
617 | ||
618 | Symbol* | |
619 | Symbol_table::lookup(const char* name, const char* version) const | |
620 | { | |
f0641a0b ILT |
621 | Stringpool::Key name_key; |
622 | name = this->namepool_.find(name, &name_key); | |
61ba1cf9 ILT |
623 | if (name == NULL) |
624 | return NULL; | |
f0641a0b ILT |
625 | |
626 | Stringpool::Key version_key = 0; | |
61ba1cf9 ILT |
627 | if (version != NULL) |
628 | { | |
f0641a0b | 629 | version = this->namepool_.find(version, &version_key); |
61ba1cf9 ILT |
630 | if (version == NULL) |
631 | return NULL; | |
632 | } | |
633 | ||
f0641a0b | 634 | Symbol_table_key key(name_key, version_key); |
61ba1cf9 ILT |
635 | Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key); |
636 | if (p == this->table_.end()) | |
637 | return NULL; | |
638 | return p->second; | |
639 | } | |
640 | ||
14bfc3f5 ILT |
641 | // Resolve a Symbol with another Symbol. This is only used in the |
642 | // unusual case where there are references to both an unversioned | |
643 | // symbol and a symbol with a version, and we then discover that that | |
1564db8d ILT |
644 | // version is the default version. Because this is unusual, we do |
645 | // this the slow way, by converting back to an ELF symbol. | |
14bfc3f5 | 646 | |
1564db8d | 647 | template<int size, bool big_endian> |
14bfc3f5 | 648 | void |
95d14cd3 | 649 | Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from) |
14bfc3f5 | 650 | { |
1564db8d ILT |
651 | unsigned char buf[elfcpp::Elf_sizes<size>::sym_size]; |
652 | elfcpp::Sym_write<size, big_endian> esym(buf); | |
d491d34e | 653 | // We don't bother to set the st_name or the st_shndx field. |
1564db8d ILT |
654 | esym.put_st_value(from->value()); |
655 | esym.put_st_size(from->symsize()); | |
656 | esym.put_st_info(from->binding(), from->type()); | |
ead1e424 | 657 | esym.put_st_other(from->visibility(), from->nonvis()); |
d491d34e ILT |
658 | bool is_ordinary; |
659 | unsigned int shndx = from->shndx(&is_ordinary); | |
660 | this->resolve(to, esym.sym(), shndx, is_ordinary, shndx, from->object(), | |
95d14cd3 | 661 | from->version()); |
1ebd95fd ILT |
662 | if (from->in_reg()) |
663 | to->set_in_reg(); | |
664 | if (from->in_dyn()) | |
665 | to->set_in_dyn(); | |
6d03d481 ST |
666 | if (parameters->options().gc_sections()) |
667 | this->gc_mark_dyn_syms(to); | |
14bfc3f5 ILT |
668 | } |
669 | ||
0602e05a ILT |
670 | // Record that a symbol is forced to be local by a version script or |
671 | // by visibility. | |
55a93433 ILT |
672 | |
673 | void | |
674 | Symbol_table::force_local(Symbol* sym) | |
675 | { | |
676 | if (!sym->is_defined() && !sym->is_common()) | |
677 | return; | |
678 | if (sym->is_forced_local()) | |
679 | { | |
680 | // We already got this one. | |
681 | return; | |
682 | } | |
683 | sym->set_is_forced_local(); | |
684 | this->forced_locals_.push_back(sym); | |
685 | } | |
686 | ||
0864d551 ILT |
687 | // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This |
688 | // is only called for undefined symbols, when at least one --wrap | |
689 | // option was used. | |
690 | ||
691 | const char* | |
029ba973 | 692 | Symbol_table::wrap_symbol(const char* name, Stringpool::Key* name_key) |
0864d551 ILT |
693 | { |
694 | // For some targets, we need to ignore a specific character when | |
695 | // wrapping, and add it back later. | |
696 | char prefix = '\0'; | |
029ba973 | 697 | if (name[0] == parameters->target().wrap_char()) |
0864d551 ILT |
698 | { |
699 | prefix = name[0]; | |
700 | ++name; | |
701 | } | |
702 | ||
c5818ff1 | 703 | if (parameters->options().is_wrap(name)) |
0864d551 ILT |
704 | { |
705 | // Turn NAME into __wrap_NAME. | |
706 | std::string s; | |
707 | if (prefix != '\0') | |
708 | s += prefix; | |
709 | s += "__wrap_"; | |
710 | s += name; | |
711 | ||
712 | // This will give us both the old and new name in NAMEPOOL_, but | |
713 | // that is OK. Only the versions we need will wind up in the | |
714 | // real string table in the output file. | |
715 | return this->namepool_.add(s.c_str(), true, name_key); | |
716 | } | |
717 | ||
718 | const char* const real_prefix = "__real_"; | |
719 | const size_t real_prefix_length = strlen(real_prefix); | |
720 | if (strncmp(name, real_prefix, real_prefix_length) == 0 | |
c5818ff1 | 721 | && parameters->options().is_wrap(name + real_prefix_length)) |
0864d551 ILT |
722 | { |
723 | // Turn __real_NAME into NAME. | |
724 | std::string s; | |
725 | if (prefix != '\0') | |
726 | s += prefix; | |
727 | s += name + real_prefix_length; | |
728 | return this->namepool_.add(s.c_str(), true, name_key); | |
729 | } | |
730 | ||
731 | return name; | |
732 | } | |
733 | ||
8c500701 ILT |
734 | // This is called when we see a symbol NAME/VERSION, and the symbol |
735 | // already exists in the symbol table, and VERSION is marked as being | |
736 | // the default version. SYM is the NAME/VERSION symbol we just added. | |
737 | // DEFAULT_IS_NEW is true if this is the first time we have seen the | |
738 | // symbol NAME/NULL. PDEF points to the entry for NAME/NULL. | |
739 | ||
740 | template<int size, bool big_endian> | |
741 | void | |
742 | Symbol_table::define_default_version(Sized_symbol<size>* sym, | |
743 | bool default_is_new, | |
744 | Symbol_table_type::iterator pdef) | |
745 | { | |
746 | if (default_is_new) | |
747 | { | |
748 | // This is the first time we have seen NAME/NULL. Make | |
749 | // NAME/NULL point to NAME/VERSION, and mark SYM as the default | |
750 | // version. | |
751 | pdef->second = sym; | |
752 | sym->set_is_default(); | |
753 | } | |
754 | else if (pdef->second == sym) | |
755 | { | |
756 | // NAME/NULL already points to NAME/VERSION. Don't mark the | |
757 | // symbol as the default if it is not already the default. | |
758 | } | |
759 | else | |
760 | { | |
761 | // This is the unfortunate case where we already have entries | |
762 | // for both NAME/VERSION and NAME/NULL. We now see a symbol | |
763 | // NAME/VERSION where VERSION is the default version. We have | |
764 | // already resolved this new symbol with the existing | |
765 | // NAME/VERSION symbol. | |
766 | ||
767 | // It's possible that NAME/NULL and NAME/VERSION are both | |
768 | // defined in regular objects. This can only happen if one | |
769 | // object file defines foo and another defines foo@@ver. This | |
770 | // is somewhat obscure, but we call it a multiple definition | |
771 | // error. | |
772 | ||
773 | // It's possible that NAME/NULL actually has a version, in which | |
774 | // case it won't be the same as VERSION. This happens with | |
775 | // ver_test_7.so in the testsuite for the symbol t2_2. We see | |
776 | // t2_2@@VER2, so we define both t2_2/VER2 and t2_2/NULL. We | |
777 | // then see an unadorned t2_2 in an object file and give it | |
778 | // version VER1 from the version script. This looks like a | |
779 | // default definition for VER1, so it looks like we should merge | |
780 | // t2_2/NULL with t2_2/VER1. That doesn't make sense, but it's | |
781 | // not obvious that this is an error, either. So we just punt. | |
782 | ||
783 | // If one of the symbols has non-default visibility, and the | |
784 | // other is defined in a shared object, then they are different | |
785 | // symbols. | |
786 | ||
787 | // Otherwise, we just resolve the symbols as though they were | |
788 | // the same. | |
789 | ||
790 | if (pdef->second->version() != NULL) | |
791 | gold_assert(pdef->second->version() != sym->version()); | |
792 | else if (sym->visibility() != elfcpp::STV_DEFAULT | |
793 | && pdef->second->is_from_dynobj()) | |
794 | ; | |
795 | else if (pdef->second->visibility() != elfcpp::STV_DEFAULT | |
796 | && sym->is_from_dynobj()) | |
797 | ; | |
798 | else | |
799 | { | |
800 | const Sized_symbol<size>* symdef; | |
801 | symdef = this->get_sized_symbol<size>(pdef->second); | |
802 | Symbol_table::resolve<size, big_endian>(sym, symdef); | |
803 | this->make_forwarder(pdef->second, sym); | |
804 | pdef->second = sym; | |
805 | sym->set_is_default(); | |
806 | } | |
807 | } | |
808 | } | |
809 | ||
14bfc3f5 ILT |
810 | // Add one symbol from OBJECT to the symbol table. NAME is symbol |
811 | // name and VERSION is the version; both are canonicalized. DEF is | |
d491d34e ILT |
812 | // whether this is the default version. ST_SHNDX is the symbol's |
813 | // section index; IS_ORDINARY is whether this is a normal section | |
814 | // rather than a special code. | |
14bfc3f5 ILT |
815 | |
816 | // If DEF is true, then this is the definition of a default version of | |
817 | // a symbol. That means that any lookup of NAME/NULL and any lookup | |
818 | // of NAME/VERSION should always return the same symbol. This is | |
819 | // obvious for references, but in particular we want to do this for | |
820 | // definitions: overriding NAME/NULL should also override | |
821 | // NAME/VERSION. If we don't do that, it would be very hard to | |
822 | // override functions in a shared library which uses versioning. | |
823 | ||
824 | // We implement this by simply making both entries in the hash table | |
825 | // point to the same Symbol structure. That is easy enough if this is | |
826 | // the first time we see NAME/NULL or NAME/VERSION, but it is possible | |
827 | // that we have seen both already, in which case they will both have | |
828 | // independent entries in the symbol table. We can't simply change | |
829 | // the symbol table entry, because we have pointers to the entries | |
830 | // attached to the object files. So we mark the entry attached to the | |
831 | // object file as a forwarder, and record it in the forwarders_ map. | |
832 | // Note that entries in the hash table will never be marked as | |
833 | // forwarders. | |
70e654ba | 834 | // |
d491d34e ILT |
835 | // ORIG_ST_SHNDX and ST_SHNDX are almost always the same. |
836 | // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF | |
837 | // for a special section code. ST_SHNDX may be modified if the symbol | |
838 | // is defined in a section being discarded. | |
14bfc3f5 ILT |
839 | |
840 | template<int size, bool big_endian> | |
aeddab66 | 841 | Sized_symbol<size>* |
f6ce93d6 | 842 | Symbol_table::add_from_object(Object* object, |
14bfc3f5 | 843 | const char *name, |
f0641a0b ILT |
844 | Stringpool::Key name_key, |
845 | const char *version, | |
846 | Stringpool::Key version_key, | |
847 | bool def, | |
70e654ba | 848 | const elfcpp::Sym<size, big_endian>& sym, |
d491d34e ILT |
849 | unsigned int st_shndx, |
850 | bool is_ordinary, | |
851 | unsigned int orig_st_shndx) | |
14bfc3f5 | 852 | { |
c5818ff1 CC |
853 | // Print a message if this symbol is being traced. |
854 | if (parameters->options().is_trace_symbol(name)) | |
855 | { | |
d491d34e | 856 | if (orig_st_shndx == elfcpp::SHN_UNDEF) |
c5818ff1 CC |
857 | gold_info(_("%s: reference to %s"), object->name().c_str(), name); |
858 | else | |
859 | gold_info(_("%s: definition of %s"), object->name().c_str(), name); | |
860 | } | |
861 | ||
0864d551 ILT |
862 | // For an undefined symbol, we may need to adjust the name using |
863 | // --wrap. | |
d491d34e | 864 | if (orig_st_shndx == elfcpp::SHN_UNDEF |
c5818ff1 | 865 | && parameters->options().any_wrap()) |
0864d551 | 866 | { |
029ba973 | 867 | const char* wrap_name = this->wrap_symbol(name, &name_key); |
0864d551 ILT |
868 | if (wrap_name != name) |
869 | { | |
870 | // If we see a reference to malloc with version GLIBC_2.0, | |
871 | // and we turn it into a reference to __wrap_malloc, then we | |
872 | // discard the version number. Otherwise the user would be | |
873 | // required to specify the correct version for | |
874 | // __wrap_malloc. | |
875 | version = NULL; | |
876 | version_key = 0; | |
877 | name = wrap_name; | |
878 | } | |
879 | } | |
880 | ||
14bfc3f5 ILT |
881 | Symbol* const snull = NULL; |
882 | std::pair<typename Symbol_table_type::iterator, bool> ins = | |
f0641a0b ILT |
883 | this->table_.insert(std::make_pair(std::make_pair(name_key, version_key), |
884 | snull)); | |
14bfc3f5 ILT |
885 | |
886 | std::pair<typename Symbol_table_type::iterator, bool> insdef = | |
887 | std::make_pair(this->table_.end(), false); | |
888 | if (def) | |
889 | { | |
f0641a0b ILT |
890 | const Stringpool::Key vnull_key = 0; |
891 | insdef = this->table_.insert(std::make_pair(std::make_pair(name_key, | |
892 | vnull_key), | |
14bfc3f5 ILT |
893 | snull)); |
894 | } | |
895 | ||
896 | // ins.first: an iterator, which is a pointer to a pair. | |
897 | // ins.first->first: the key (a pair of name and version). | |
898 | // ins.first->second: the value (Symbol*). | |
899 | // ins.second: true if new entry was inserted, false if not. | |
900 | ||
1564db8d | 901 | Sized_symbol<size>* ret; |
ead1e424 ILT |
902 | bool was_undefined; |
903 | bool was_common; | |
14bfc3f5 ILT |
904 | if (!ins.second) |
905 | { | |
906 | // We already have an entry for NAME/VERSION. | |
7d1a9ebb | 907 | ret = this->get_sized_symbol<size>(ins.first->second); |
a3ad94ed | 908 | gold_assert(ret != NULL); |
ead1e424 ILT |
909 | |
910 | was_undefined = ret->is_undefined(); | |
911 | was_common = ret->is_common(); | |
912 | ||
d491d34e ILT |
913 | this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object, |
914 | version); | |
6d03d481 ST |
915 | if (parameters->options().gc_sections()) |
916 | this->gc_mark_dyn_syms(ret); | |
14bfc3f5 ILT |
917 | |
918 | if (def) | |
8c500701 ILT |
919 | this->define_default_version<size, big_endian>(ret, insdef.second, |
920 | insdef.first); | |
14bfc3f5 ILT |
921 | } |
922 | else | |
923 | { | |
924 | // This is the first time we have seen NAME/VERSION. | |
a3ad94ed | 925 | gold_assert(ins.first->second == NULL); |
ead1e424 | 926 | |
14bfc3f5 ILT |
927 | if (def && !insdef.second) |
928 | { | |
14b31740 ILT |
929 | // We already have an entry for NAME/NULL. If we override |
930 | // it, then change it to NAME/VERSION. | |
7d1a9ebb | 931 | ret = this->get_sized_symbol<size>(insdef.first->second); |
18e6b24e ILT |
932 | |
933 | was_undefined = ret->is_undefined(); | |
934 | was_common = ret->is_common(); | |
935 | ||
d491d34e ILT |
936 | this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object, |
937 | version); | |
6d03d481 ST |
938 | if (parameters->options().gc_sections()) |
939 | this->gc_mark_dyn_syms(ret); | |
14bfc3f5 ILT |
940 | ins.first->second = ret; |
941 | } | |
942 | else | |
943 | { | |
18e6b24e ILT |
944 | was_undefined = false; |
945 | was_common = false; | |
946 | ||
f6ce93d6 | 947 | Sized_target<size, big_endian>* target = |
029ba973 | 948 | parameters->sized_target<size, big_endian>(); |
1564db8d ILT |
949 | if (!target->has_make_symbol()) |
950 | ret = new Sized_symbol<size>(); | |
951 | else | |
14bfc3f5 | 952 | { |
1564db8d ILT |
953 | ret = target->make_symbol(); |
954 | if (ret == NULL) | |
14bfc3f5 ILT |
955 | { |
956 | // This means that we don't want a symbol table | |
957 | // entry after all. | |
958 | if (!def) | |
959 | this->table_.erase(ins.first); | |
960 | else | |
961 | { | |
962 | this->table_.erase(insdef.first); | |
963 | // Inserting insdef invalidated ins. | |
f0641a0b ILT |
964 | this->table_.erase(std::make_pair(name_key, |
965 | version_key)); | |
14bfc3f5 ILT |
966 | } |
967 | return NULL; | |
968 | } | |
969 | } | |
14bfc3f5 | 970 | |
f3e9c5c5 | 971 | ret->init_object(name, version, object, sym, st_shndx, is_ordinary); |
1564db8d | 972 | |
14bfc3f5 ILT |
973 | ins.first->second = ret; |
974 | if (def) | |
975 | { | |
976 | // This is the first time we have seen NAME/NULL. Point | |
977 | // it at the new entry for NAME/VERSION. | |
a3ad94ed | 978 | gold_assert(insdef.second); |
14bfc3f5 ILT |
979 | insdef.first->second = ret; |
980 | } | |
981 | } | |
8c500701 ILT |
982 | |
983 | if (def) | |
984 | ret->set_is_default(); | |
14bfc3f5 ILT |
985 | } |
986 | ||
ead1e424 ILT |
987 | // Record every time we see a new undefined symbol, to speed up |
988 | // archive groups. | |
989 | if (!was_undefined && ret->is_undefined()) | |
990 | ++this->saw_undefined_; | |
991 | ||
992 | // Keep track of common symbols, to speed up common symbol | |
993 | // allocation. | |
994 | if (!was_common && ret->is_common()) | |
155a0dd7 | 995 | { |
8a5e3e08 | 996 | if (ret->type() == elfcpp::STT_TLS) |
155a0dd7 | 997 | this->tls_commons_.push_back(ret); |
8a5e3e08 ILT |
998 | else if (!is_ordinary |
999 | && st_shndx == parameters->target().small_common_shndx()) | |
1000 | this->small_commons_.push_back(ret); | |
1001 | else if (!is_ordinary | |
1002 | && st_shndx == parameters->target().large_common_shndx()) | |
1003 | this->large_commons_.push_back(ret); | |
1004 | else | |
1005 | this->commons_.push_back(ret); | |
155a0dd7 | 1006 | } |
ead1e424 | 1007 | |
0602e05a ILT |
1008 | // If we're not doing a relocatable link, then any symbol with |
1009 | // hidden or internal visibility is local. | |
1010 | if ((ret->visibility() == elfcpp::STV_HIDDEN | |
1011 | || ret->visibility() == elfcpp::STV_INTERNAL) | |
1012 | && (ret->binding() == elfcpp::STB_GLOBAL | |
adcf2816 | 1013 | || ret->binding() == elfcpp::STB_GNU_UNIQUE |
0602e05a ILT |
1014 | || ret->binding() == elfcpp::STB_WEAK) |
1015 | && !parameters->options().relocatable()) | |
1016 | this->force_local(ret); | |
1017 | ||
14bfc3f5 ILT |
1018 | return ret; |
1019 | } | |
1020 | ||
f6ce93d6 | 1021 | // Add all the symbols in a relocatable object to the hash table. |
14bfc3f5 ILT |
1022 | |
1023 | template<int size, bool big_endian> | |
1024 | void | |
dbe717ef ILT |
1025 | Symbol_table::add_from_relobj( |
1026 | Sized_relobj<size, big_endian>* relobj, | |
f6ce93d6 | 1027 | const unsigned char* syms, |
14bfc3f5 | 1028 | size_t count, |
d491d34e | 1029 | size_t symndx_offset, |
14bfc3f5 ILT |
1030 | const char* sym_names, |
1031 | size_t sym_name_size, | |
92de84a6 ILT |
1032 | typename Sized_relobj<size, big_endian>::Symbols* sympointers, |
1033 | size_t *defined) | |
14bfc3f5 | 1034 | { |
92de84a6 ILT |
1035 | *defined = 0; |
1036 | ||
8851ecca | 1037 | gold_assert(size == parameters->target().get_size()); |
14bfc3f5 | 1038 | |
a783673b ILT |
1039 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
1040 | ||
88dd47ac ILT |
1041 | const bool just_symbols = relobj->just_symbols(); |
1042 | ||
f6ce93d6 | 1043 | const unsigned char* p = syms; |
a783673b | 1044 | for (size_t i = 0; i < count; ++i, p += sym_size) |
14bfc3f5 | 1045 | { |
92de84a6 ILT |
1046 | (*sympointers)[i] = NULL; |
1047 | ||
14bfc3f5 ILT |
1048 | elfcpp::Sym<size, big_endian> sym(p); |
1049 | ||
d491d34e | 1050 | unsigned int st_name = sym.get_st_name(); |
14bfc3f5 ILT |
1051 | if (st_name >= sym_name_size) |
1052 | { | |
75f2446e ILT |
1053 | relobj->error(_("bad global symbol name offset %u at %zu"), |
1054 | st_name, i); | |
1055 | continue; | |
14bfc3f5 ILT |
1056 | } |
1057 | ||
dbe717ef ILT |
1058 | const char* name = sym_names + st_name; |
1059 | ||
d491d34e ILT |
1060 | bool is_ordinary; |
1061 | unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset, | |
1062 | sym.get_st_shndx(), | |
1063 | &is_ordinary); | |
1064 | unsigned int orig_st_shndx = st_shndx; | |
1065 | if (!is_ordinary) | |
1066 | orig_st_shndx = elfcpp::SHN_UNDEF; | |
1067 | ||
92de84a6 ILT |
1068 | if (st_shndx != elfcpp::SHN_UNDEF) |
1069 | ++*defined; | |
1070 | ||
a783673b ILT |
1071 | // A symbol defined in a section which we are not including must |
1072 | // be treated as an undefined symbol. | |
a783673b | 1073 | if (st_shndx != elfcpp::SHN_UNDEF |
d491d34e | 1074 | && is_ordinary |
dbe717ef | 1075 | && !relobj->is_section_included(st_shndx)) |
d491d34e | 1076 | st_shndx = elfcpp::SHN_UNDEF; |
a783673b | 1077 | |
14bfc3f5 ILT |
1078 | // In an object file, an '@' in the name separates the symbol |
1079 | // name from the version name. If there are two '@' characters, | |
1080 | // this is the default version. | |
1081 | const char* ver = strchr(name, '@'); | |
057ead22 | 1082 | Stringpool::Key ver_key = 0; |
09124467 | 1083 | int namelen = 0; |
55a93433 | 1084 | // DEF: is the version default? LOCAL: is the symbol forced local? |
09124467 | 1085 | bool def = false; |
55a93433 | 1086 | bool local = false; |
09124467 ILT |
1087 | |
1088 | if (ver != NULL) | |
1089 | { | |
1090 | // The symbol name is of the form foo@VERSION or foo@@VERSION | |
1091 | namelen = ver - name; | |
1092 | ++ver; | |
1093 | if (*ver == '@') | |
1094 | { | |
1095 | def = true; | |
1096 | ++ver; | |
1097 | } | |
057ead22 | 1098 | ver = this->namepool_.add(ver, true, &ver_key); |
09124467 | 1099 | } |
5871526f ILT |
1100 | // We don't want to assign a version to an undefined symbol, |
1101 | // even if it is listed in the version script. FIXME: What | |
1102 | // about a common symbol? | |
057ead22 ILT |
1103 | else |
1104 | { | |
1105 | namelen = strlen(name); | |
1106 | if (!this->version_script_.empty() | |
1107 | && st_shndx != elfcpp::SHN_UNDEF) | |
1108 | { | |
1109 | // The symbol name did not have a version, but the | |
1110 | // version script may assign a version anyway. | |
1111 | std::string version; | |
1112 | if (this->version_script_.get_symbol_version(name, &version)) | |
1113 | { | |
1114 | // The version can be empty if the version script is | |
1115 | // only used to force some symbols to be local. | |
1116 | if (!version.empty()) | |
1117 | { | |
1118 | ver = this->namepool_.add_with_length(version.c_str(), | |
1119 | version.length(), | |
1120 | true, | |
1121 | &ver_key); | |
1122 | def = true; | |
1123 | } | |
1124 | } | |
1125 | else if (this->version_script_.symbol_is_local(name)) | |
1126 | local = true; | |
1127 | } | |
1128 | } | |
14bfc3f5 | 1129 | |
d491d34e ILT |
1130 | elfcpp::Sym<size, big_endian>* psym = &sym; |
1131 | unsigned char symbuf[sym_size]; | |
1132 | elfcpp::Sym<size, big_endian> sym2(symbuf); | |
88dd47ac ILT |
1133 | if (just_symbols) |
1134 | { | |
d491d34e | 1135 | memcpy(symbuf, p, sym_size); |
88dd47ac | 1136 | elfcpp::Sym_write<size, big_endian> sw(symbuf); |
d491d34e | 1137 | if (orig_st_shndx != elfcpp::SHN_UNDEF && is_ordinary) |
88dd47ac ILT |
1138 | { |
1139 | // Symbol values in object files are section relative. | |
1140 | // This is normally what we want, but since here we are | |
1141 | // converting the symbol to absolute we need to add the | |
1142 | // section address. The section address in an object | |
1143 | // file is normally zero, but people can use a linker | |
1144 | // script to change it. | |
d491d34e ILT |
1145 | sw.put_st_value(sym.get_st_value() |
1146 | + relobj->section_address(orig_st_shndx)); | |
88dd47ac | 1147 | } |
d491d34e ILT |
1148 | st_shndx = elfcpp::SHN_ABS; |
1149 | is_ordinary = false; | |
88dd47ac ILT |
1150 | psym = &sym2; |
1151 | } | |
1152 | ||
65514900 CC |
1153 | // Fix up visibility if object has no-export set. |
1154 | if (relobj->no_export()) | |
1155 | { | |
1156 | // We may have copied symbol already above. | |
1157 | if (psym != &sym2) | |
1158 | { | |
1159 | memcpy(symbuf, p, sym_size); | |
1160 | psym = &sym2; | |
1161 | } | |
1162 | ||
1163 | elfcpp::STV visibility = sym2.get_st_visibility(); | |
1164 | if (visibility == elfcpp::STV_DEFAULT | |
1165 | || visibility == elfcpp::STV_PROTECTED) | |
1166 | { | |
1167 | elfcpp::Sym_write<size, big_endian> sw(symbuf); | |
1168 | unsigned char nonvis = sym2.get_st_nonvis(); | |
1169 | sw.put_st_other(elfcpp::STV_HIDDEN, nonvis); | |
1170 | } | |
1171 | } | |
1172 | ||
057ead22 ILT |
1173 | Stringpool::Key name_key; |
1174 | name = this->namepool_.add_with_length(name, namelen, true, | |
1175 | &name_key); | |
1176 | ||
aeddab66 | 1177 | Sized_symbol<size>* res; |
057ead22 ILT |
1178 | res = this->add_from_object(relobj, name, name_key, ver, ver_key, |
1179 | def, *psym, st_shndx, is_ordinary, | |
1180 | orig_st_shndx); | |
6d03d481 ST |
1181 | |
1182 | // If building a shared library using garbage collection, do not | |
1183 | // treat externally visible symbols as garbage. | |
1184 | if (parameters->options().gc_sections() | |
1185 | && parameters->options().shared()) | |
1186 | this->gc_mark_symbol_for_shlib(res); | |
f0641a0b | 1187 | |
057ead22 ILT |
1188 | if (local) |
1189 | this->force_local(res); | |
14bfc3f5 | 1190 | |
730cdc88 | 1191 | (*sympointers)[i] = res; |
14bfc3f5 ILT |
1192 | } |
1193 | } | |
1194 | ||
89fc3421 CC |
1195 | // Add a symbol from a plugin-claimed file. |
1196 | ||
1197 | template<int size, bool big_endian> | |
1198 | Symbol* | |
1199 | Symbol_table::add_from_pluginobj( | |
1200 | Sized_pluginobj<size, big_endian>* obj, | |
1201 | const char* name, | |
1202 | const char* ver, | |
1203 | elfcpp::Sym<size, big_endian>* sym) | |
1204 | { | |
1205 | unsigned int st_shndx = sym->get_st_shndx(); | |
24998053 | 1206 | bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE; |
89fc3421 CC |
1207 | |
1208 | Stringpool::Key ver_key = 0; | |
1209 | bool def = false; | |
1210 | bool local = false; | |
1211 | ||
1212 | if (ver != NULL) | |
1213 | { | |
1214 | ver = this->namepool_.add(ver, true, &ver_key); | |
1215 | } | |
1216 | // We don't want to assign a version to an undefined symbol, | |
1217 | // even if it is listed in the version script. FIXME: What | |
1218 | // about a common symbol? | |
1219 | else | |
1220 | { | |
1221 | if (!this->version_script_.empty() | |
1222 | && st_shndx != elfcpp::SHN_UNDEF) | |
1223 | { | |
1224 | // The symbol name did not have a version, but the | |
1225 | // version script may assign a version anyway. | |
1226 | std::string version; | |
1227 | if (this->version_script_.get_symbol_version(name, &version)) | |
1228 | { | |
1229 | // The version can be empty if the version script is | |
1230 | // only used to force some symbols to be local. | |
1231 | if (!version.empty()) | |
1232 | { | |
1233 | ver = this->namepool_.add_with_length(version.c_str(), | |
1234 | version.length(), | |
1235 | true, | |
1236 | &ver_key); | |
1237 | def = true; | |
1238 | } | |
1239 | } | |
1240 | else if (this->version_script_.symbol_is_local(name)) | |
1241 | local = true; | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | Stringpool::Key name_key; | |
1246 | name = this->namepool_.add(name, true, &name_key); | |
1247 | ||
1248 | Sized_symbol<size>* res; | |
1249 | res = this->add_from_object(obj, name, name_key, ver, ver_key, | |
24998053 | 1250 | def, *sym, st_shndx, is_ordinary, st_shndx); |
89fc3421 CC |
1251 | |
1252 | if (local) | |
0602e05a | 1253 | this->force_local(res); |
89fc3421 CC |
1254 | |
1255 | return res; | |
1256 | } | |
1257 | ||
dbe717ef ILT |
1258 | // Add all the symbols in a dynamic object to the hash table. |
1259 | ||
1260 | template<int size, bool big_endian> | |
1261 | void | |
1262 | Symbol_table::add_from_dynobj( | |
1263 | Sized_dynobj<size, big_endian>* dynobj, | |
1264 | const unsigned char* syms, | |
1265 | size_t count, | |
1266 | const char* sym_names, | |
1267 | size_t sym_name_size, | |
1268 | const unsigned char* versym, | |
1269 | size_t versym_size, | |
92de84a6 ILT |
1270 | const std::vector<const char*>* version_map, |
1271 | typename Sized_relobj<size, big_endian>::Symbols* sympointers, | |
1272 | size_t* defined) | |
dbe717ef | 1273 | { |
92de84a6 ILT |
1274 | *defined = 0; |
1275 | ||
8851ecca | 1276 | gold_assert(size == parameters->target().get_size()); |
dbe717ef | 1277 | |
88dd47ac ILT |
1278 | if (dynobj->just_symbols()) |
1279 | { | |
1280 | gold_error(_("--just-symbols does not make sense with a shared object")); | |
1281 | return; | |
1282 | } | |
1283 | ||
dbe717ef ILT |
1284 | if (versym != NULL && versym_size / 2 < count) |
1285 | { | |
75f2446e ILT |
1286 | dynobj->error(_("too few symbol versions")); |
1287 | return; | |
dbe717ef ILT |
1288 | } |
1289 | ||
1290 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
1291 | ||
aeddab66 ILT |
1292 | // We keep a list of all STT_OBJECT symbols, so that we can resolve |
1293 | // weak aliases. This is necessary because if the dynamic object | |
1294 | // provides the same variable under two names, one of which is a | |
1295 | // weak definition, and the regular object refers to the weak | |
1296 | // definition, we have to put both the weak definition and the | |
1297 | // strong definition into the dynamic symbol table. Given a weak | |
1298 | // definition, the only way that we can find the corresponding | |
1299 | // strong definition, if any, is to search the symbol table. | |
1300 | std::vector<Sized_symbol<size>*> object_symbols; | |
1301 | ||
dbe717ef ILT |
1302 | const unsigned char* p = syms; |
1303 | const unsigned char* vs = versym; | |
1304 | for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2) | |
1305 | { | |
1306 | elfcpp::Sym<size, big_endian> sym(p); | |
1307 | ||
92de84a6 ILT |
1308 | if (sympointers != NULL) |
1309 | (*sympointers)[i] = NULL; | |
1310 | ||
65778909 ILT |
1311 | // Ignore symbols with local binding or that have |
1312 | // internal or hidden visibility. | |
1313 | if (sym.get_st_bind() == elfcpp::STB_LOCAL | |
1314 | || sym.get_st_visibility() == elfcpp::STV_INTERNAL | |
1315 | || sym.get_st_visibility() == elfcpp::STV_HIDDEN) | |
dbe717ef ILT |
1316 | continue; |
1317 | ||
8bdcdf2c ILT |
1318 | // A protected symbol in a shared library must be treated as a |
1319 | // normal symbol when viewed from outside the shared library. | |
1320 | // Implement this by overriding the visibility here. | |
1321 | elfcpp::Sym<size, big_endian>* psym = &sym; | |
1322 | unsigned char symbuf[sym_size]; | |
1323 | elfcpp::Sym<size, big_endian> sym2(symbuf); | |
1324 | if (sym.get_st_visibility() == elfcpp::STV_PROTECTED) | |
1325 | { | |
1326 | memcpy(symbuf, p, sym_size); | |
1327 | elfcpp::Sym_write<size, big_endian> sw(symbuf); | |
1328 | sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis()); | |
1329 | psym = &sym2; | |
1330 | } | |
1331 | ||
1332 | unsigned int st_name = psym->get_st_name(); | |
dbe717ef ILT |
1333 | if (st_name >= sym_name_size) |
1334 | { | |
75f2446e ILT |
1335 | dynobj->error(_("bad symbol name offset %u at %zu"), |
1336 | st_name, i); | |
1337 | continue; | |
dbe717ef ILT |
1338 | } |
1339 | ||
1340 | const char* name = sym_names + st_name; | |
1341 | ||
d491d34e | 1342 | bool is_ordinary; |
8bdcdf2c | 1343 | unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(), |
d491d34e ILT |
1344 | &is_ordinary); |
1345 | ||
92de84a6 ILT |
1346 | if (st_shndx != elfcpp::SHN_UNDEF) |
1347 | ++*defined; | |
1348 | ||
aeddab66 ILT |
1349 | Sized_symbol<size>* res; |
1350 | ||
dbe717ef ILT |
1351 | if (versym == NULL) |
1352 | { | |
1353 | Stringpool::Key name_key; | |
cfd73a4e | 1354 | name = this->namepool_.add(name, true, &name_key); |
aeddab66 | 1355 | res = this->add_from_object(dynobj, name, name_key, NULL, 0, |
8bdcdf2c | 1356 | false, *psym, st_shndx, is_ordinary, |
d491d34e | 1357 | st_shndx); |
dbe717ef | 1358 | } |
aeddab66 ILT |
1359 | else |
1360 | { | |
1361 | // Read the version information. | |
dbe717ef | 1362 | |
aeddab66 | 1363 | unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs); |
dbe717ef | 1364 | |
aeddab66 ILT |
1365 | bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0; |
1366 | v &= elfcpp::VERSYM_VERSION; | |
dbe717ef | 1367 | |
aeddab66 ILT |
1368 | // The Sun documentation says that V can be VER_NDX_LOCAL, |
1369 | // or VER_NDX_GLOBAL, or a version index. The meaning of | |
1370 | // VER_NDX_LOCAL is defined as "Symbol has local scope." | |
1371 | // The old GNU linker will happily generate VER_NDX_LOCAL | |
1372 | // for an undefined symbol. I don't know what the Sun | |
1373 | // linker will generate. | |
dbe717ef | 1374 | |
aeddab66 | 1375 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL) |
d491d34e | 1376 | && st_shndx != elfcpp::SHN_UNDEF) |
aeddab66 ILT |
1377 | { |
1378 | // This symbol should not be visible outside the object. | |
1379 | continue; | |
1380 | } | |
64707334 | 1381 | |
aeddab66 ILT |
1382 | // At this point we are definitely going to add this symbol. |
1383 | Stringpool::Key name_key; | |
1384 | name = this->namepool_.add(name, true, &name_key); | |
dbe717ef | 1385 | |
aeddab66 ILT |
1386 | if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL) |
1387 | || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL)) | |
1388 | { | |
1389 | // This symbol does not have a version. | |
1390 | res = this->add_from_object(dynobj, name, name_key, NULL, 0, | |
8bdcdf2c | 1391 | false, *psym, st_shndx, is_ordinary, |
d491d34e | 1392 | st_shndx); |
aeddab66 ILT |
1393 | } |
1394 | else | |
1395 | { | |
1396 | if (v >= version_map->size()) | |
1397 | { | |
1398 | dynobj->error(_("versym for symbol %zu out of range: %u"), | |
1399 | i, v); | |
1400 | continue; | |
1401 | } | |
dbe717ef | 1402 | |
aeddab66 ILT |
1403 | const char* version = (*version_map)[v]; |
1404 | if (version == NULL) | |
1405 | { | |
1406 | dynobj->error(_("versym for symbol %zu has no name: %u"), | |
1407 | i, v); | |
1408 | continue; | |
1409 | } | |
dbe717ef | 1410 | |
aeddab66 ILT |
1411 | Stringpool::Key version_key; |
1412 | version = this->namepool_.add(version, true, &version_key); | |
1413 | ||
1414 | // If this is an absolute symbol, and the version name | |
1415 | // and symbol name are the same, then this is the | |
1416 | // version definition symbol. These symbols exist to | |
1417 | // support using -u to pull in particular versions. We | |
1418 | // do not want to record a version for them. | |
d491d34e ILT |
1419 | if (st_shndx == elfcpp::SHN_ABS |
1420 | && !is_ordinary | |
aeddab66 ILT |
1421 | && name_key == version_key) |
1422 | res = this->add_from_object(dynobj, name, name_key, NULL, 0, | |
8bdcdf2c | 1423 | false, *psym, st_shndx, is_ordinary, |
d491d34e | 1424 | st_shndx); |
aeddab66 ILT |
1425 | else |
1426 | { | |
1427 | const bool def = (!hidden | |
d491d34e | 1428 | && st_shndx != elfcpp::SHN_UNDEF); |
aeddab66 | 1429 | res = this->add_from_object(dynobj, name, name_key, version, |
8bdcdf2c | 1430 | version_key, def, *psym, st_shndx, |
d491d34e | 1431 | is_ordinary, st_shndx); |
aeddab66 ILT |
1432 | } |
1433 | } | |
dbe717ef ILT |
1434 | } |
1435 | ||
99a37bfd | 1436 | // Note that it is possible that RES was overridden by an |
a4bb589a | 1437 | // earlier object, in which case it can't be aliased here. |
d491d34e ILT |
1438 | if (st_shndx != elfcpp::SHN_UNDEF |
1439 | && is_ordinary | |
8bdcdf2c | 1440 | && psym->get_st_type() == elfcpp::STT_OBJECT |
99a37bfd ILT |
1441 | && res->source() == Symbol::FROM_OBJECT |
1442 | && res->object() == dynobj) | |
aeddab66 | 1443 | object_symbols.push_back(res); |
92de84a6 ILT |
1444 | |
1445 | if (sympointers != NULL) | |
1446 | (*sympointers)[i] = res; | |
aeddab66 ILT |
1447 | } |
1448 | ||
1449 | this->record_weak_aliases(&object_symbols); | |
1450 | } | |
1451 | ||
1452 | // This is used to sort weak aliases. We sort them first by section | |
1453 | // index, then by offset, then by weak ahead of strong. | |
1454 | ||
1455 | template<int size> | |
1456 | class Weak_alias_sorter | |
1457 | { | |
1458 | public: | |
1459 | bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const; | |
1460 | }; | |
1461 | ||
1462 | template<int size> | |
1463 | bool | |
1464 | Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1, | |
1465 | const Sized_symbol<size>* s2) const | |
1466 | { | |
d491d34e ILT |
1467 | bool is_ordinary; |
1468 | unsigned int s1_shndx = s1->shndx(&is_ordinary); | |
1469 | gold_assert(is_ordinary); | |
1470 | unsigned int s2_shndx = s2->shndx(&is_ordinary); | |
1471 | gold_assert(is_ordinary); | |
1472 | if (s1_shndx != s2_shndx) | |
1473 | return s1_shndx < s2_shndx; | |
1474 | ||
aeddab66 ILT |
1475 | if (s1->value() != s2->value()) |
1476 | return s1->value() < s2->value(); | |
1477 | if (s1->binding() != s2->binding()) | |
1478 | { | |
1479 | if (s1->binding() == elfcpp::STB_WEAK) | |
1480 | return true; | |
1481 | if (s2->binding() == elfcpp::STB_WEAK) | |
1482 | return false; | |
1483 | } | |
1484 | return std::string(s1->name()) < std::string(s2->name()); | |
1485 | } | |
dbe717ef | 1486 | |
aeddab66 ILT |
1487 | // SYMBOLS is a list of object symbols from a dynamic object. Look |
1488 | // for any weak aliases, and record them so that if we add the weak | |
1489 | // alias to the dynamic symbol table, we also add the corresponding | |
1490 | // strong symbol. | |
dbe717ef | 1491 | |
aeddab66 ILT |
1492 | template<int size> |
1493 | void | |
1494 | Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols) | |
1495 | { | |
1496 | // Sort the vector by section index, then by offset, then by weak | |
1497 | // ahead of strong. | |
1498 | std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>()); | |
1499 | ||
1500 | // Walk through the vector. For each weak definition, record | |
1501 | // aliases. | |
1502 | for (typename std::vector<Sized_symbol<size>*>::const_iterator p = | |
1503 | symbols->begin(); | |
1504 | p != symbols->end(); | |
1505 | ++p) | |
1506 | { | |
1507 | if ((*p)->binding() != elfcpp::STB_WEAK) | |
1508 | continue; | |
1509 | ||
1510 | // Build a circular list of weak aliases. Each symbol points to | |
1511 | // the next one in the circular list. | |
1512 | ||
1513 | Sized_symbol<size>* from_sym = *p; | |
1514 | typename std::vector<Sized_symbol<size>*>::const_iterator q; | |
1515 | for (q = p + 1; q != symbols->end(); ++q) | |
dbe717ef | 1516 | { |
d491d34e ILT |
1517 | bool dummy; |
1518 | if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy) | |
aeddab66 ILT |
1519 | || (*q)->value() != from_sym->value()) |
1520 | break; | |
1521 | ||
1522 | this->weak_aliases_[from_sym] = *q; | |
1523 | from_sym->set_has_alias(); | |
1524 | from_sym = *q; | |
dbe717ef ILT |
1525 | } |
1526 | ||
aeddab66 ILT |
1527 | if (from_sym != *p) |
1528 | { | |
1529 | this->weak_aliases_[from_sym] = *p; | |
1530 | from_sym->set_has_alias(); | |
1531 | } | |
dbe717ef | 1532 | |
aeddab66 | 1533 | p = q - 1; |
dbe717ef ILT |
1534 | } |
1535 | } | |
1536 | ||
ead1e424 ILT |
1537 | // Create and return a specially defined symbol. If ONLY_IF_REF is |
1538 | // true, then only create the symbol if there is a reference to it. | |
86f2e683 | 1539 | // If this does not return NULL, it sets *POLDSYM to the existing |
8c500701 ILT |
1540 | // symbol if there is one. This sets *RESOLVE_OLDSYM if we should |
1541 | // resolve the newly created symbol to the old one. This | |
1542 | // canonicalizes *PNAME and *PVERSION. | |
ead1e424 ILT |
1543 | |
1544 | template<int size, bool big_endian> | |
1545 | Sized_symbol<size>* | |
9b07f471 ILT |
1546 | Symbol_table::define_special_symbol(const char** pname, const char** pversion, |
1547 | bool only_if_ref, | |
8c500701 ILT |
1548 | Sized_symbol<size>** poldsym, |
1549 | bool *resolve_oldsym) | |
ead1e424 | 1550 | { |
8c500701 | 1551 | *resolve_oldsym = false; |
ead1e424 | 1552 | |
55a93433 ILT |
1553 | // If the caller didn't give us a version, see if we get one from |
1554 | // the version script. | |
057ead22 | 1555 | std::string v; |
8c500701 | 1556 | bool is_default_version = false; |
55a93433 ILT |
1557 | if (*pversion == NULL) |
1558 | { | |
057ead22 ILT |
1559 | if (this->version_script_.get_symbol_version(*pname, &v)) |
1560 | { | |
1561 | if (!v.empty()) | |
1562 | *pversion = v.c_str(); | |
8c500701 ILT |
1563 | |
1564 | // If we get the version from a version script, then we are | |
1565 | // also the default version. | |
1566 | is_default_version = true; | |
057ead22 | 1567 | } |
55a93433 ILT |
1568 | } |
1569 | ||
8c500701 ILT |
1570 | Symbol* oldsym; |
1571 | Sized_symbol<size>* sym; | |
1572 | ||
1573 | bool add_to_table = false; | |
1574 | typename Symbol_table_type::iterator add_loc = this->table_.end(); | |
1575 | bool add_def_to_table = false; | |
1576 | typename Symbol_table_type::iterator add_def_loc = this->table_.end(); | |
1577 | ||
ead1e424 ILT |
1578 | if (only_if_ref) |
1579 | { | |
306d9ef0 | 1580 | oldsym = this->lookup(*pname, *pversion); |
8c500701 ILT |
1581 | if (oldsym == NULL && is_default_version) |
1582 | oldsym = this->lookup(*pname, NULL); | |
f6ce93d6 | 1583 | if (oldsym == NULL || !oldsym->is_undefined()) |
ead1e424 | 1584 | return NULL; |
306d9ef0 ILT |
1585 | |
1586 | *pname = oldsym->name(); | |
8c500701 ILT |
1587 | if (!is_default_version) |
1588 | *pversion = oldsym->version(); | |
ead1e424 ILT |
1589 | } |
1590 | else | |
1591 | { | |
14b31740 | 1592 | // Canonicalize NAME and VERSION. |
f0641a0b | 1593 | Stringpool::Key name_key; |
cfd73a4e | 1594 | *pname = this->namepool_.add(*pname, true, &name_key); |
ead1e424 | 1595 | |
14b31740 | 1596 | Stringpool::Key version_key = 0; |
306d9ef0 | 1597 | if (*pversion != NULL) |
cfd73a4e | 1598 | *pversion = this->namepool_.add(*pversion, true, &version_key); |
14b31740 | 1599 | |
ead1e424 | 1600 | Symbol* const snull = NULL; |
ead1e424 | 1601 | std::pair<typename Symbol_table_type::iterator, bool> ins = |
14b31740 ILT |
1602 | this->table_.insert(std::make_pair(std::make_pair(name_key, |
1603 | version_key), | |
ead1e424 ILT |
1604 | snull)); |
1605 | ||
8c500701 ILT |
1606 | std::pair<typename Symbol_table_type::iterator, bool> insdef = |
1607 | std::make_pair(this->table_.end(), false); | |
1608 | if (is_default_version) | |
1609 | { | |
1610 | const Stringpool::Key vnull = 0; | |
1611 | insdef = this->table_.insert(std::make_pair(std::make_pair(name_key, | |
1612 | vnull), | |
1613 | snull)); | |
1614 | } | |
1615 | ||
ead1e424 ILT |
1616 | if (!ins.second) |
1617 | { | |
14b31740 | 1618 | // We already have a symbol table entry for NAME/VERSION. |
ead1e424 | 1619 | oldsym = ins.first->second; |
a3ad94ed | 1620 | gold_assert(oldsym != NULL); |
8c500701 ILT |
1621 | |
1622 | if (is_default_version) | |
1623 | { | |
1624 | Sized_symbol<size>* soldsym = | |
1625 | this->get_sized_symbol<size>(oldsym); | |
1626 | this->define_default_version<size, big_endian>(soldsym, | |
1627 | insdef.second, | |
1628 | insdef.first); | |
1629 | } | |
ead1e424 ILT |
1630 | } |
1631 | else | |
1632 | { | |
1633 | // We haven't seen this symbol before. | |
a3ad94ed | 1634 | gold_assert(ins.first->second == NULL); |
8c500701 ILT |
1635 | |
1636 | add_to_table = true; | |
1637 | add_loc = ins.first; | |
1638 | ||
1639 | if (is_default_version && !insdef.second) | |
1640 | { | |
1641 | // We are adding NAME/VERSION, and it is the default | |
1642 | // version. We already have an entry for NAME/NULL. | |
1643 | oldsym = insdef.first->second; | |
1644 | *resolve_oldsym = true; | |
1645 | } | |
1646 | else | |
1647 | { | |
1648 | oldsym = NULL; | |
1649 | ||
1650 | if (is_default_version) | |
1651 | { | |
1652 | add_def_to_table = true; | |
1653 | add_def_loc = insdef.first; | |
1654 | } | |
1655 | } | |
ead1e424 ILT |
1656 | } |
1657 | } | |
1658 | ||
8851ecca ILT |
1659 | const Target& target = parameters->target(); |
1660 | if (!target.has_make_symbol()) | |
86f2e683 ILT |
1661 | sym = new Sized_symbol<size>(); |
1662 | else | |
ead1e424 | 1663 | { |
029ba973 ILT |
1664 | Sized_target<size, big_endian>* sized_target = |
1665 | parameters->sized_target<size, big_endian>(); | |
86f2e683 ILT |
1666 | sym = sized_target->make_symbol(); |
1667 | if (sym == NULL) | |
1668 | return NULL; | |
1669 | } | |
ead1e424 | 1670 | |
86f2e683 ILT |
1671 | if (add_to_table) |
1672 | add_loc->second = sym; | |
1673 | else | |
1674 | gold_assert(oldsym != NULL); | |
ead1e424 | 1675 | |
8c500701 ILT |
1676 | if (add_def_to_table) |
1677 | add_def_loc->second = sym; | |
1678 | ||
7d1a9ebb | 1679 | *poldsym = this->get_sized_symbol<size>(oldsym); |
ead1e424 ILT |
1680 | |
1681 | return sym; | |
1682 | } | |
1683 | ||
1684 | // Define a symbol based on an Output_data. | |
1685 | ||
14b31740 | 1686 | Symbol* |
9b07f471 ILT |
1687 | Symbol_table::define_in_output_data(const char* name, |
1688 | const char* version, | |
1689 | Output_data* od, | |
1690 | uint64_t value, | |
1691 | uint64_t symsize, | |
1692 | elfcpp::STT type, | |
1693 | elfcpp::STB binding, | |
ead1e424 ILT |
1694 | elfcpp::STV visibility, |
1695 | unsigned char nonvis, | |
1696 | bool offset_is_from_end, | |
1697 | bool only_if_ref) | |
1698 | { | |
8851ecca | 1699 | if (parameters->target().get_size() == 32) |
86f2e683 ILT |
1700 | { |
1701 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
9b07f471 | 1702 | return this->do_define_in_output_data<32>(name, version, od, |
86f2e683 ILT |
1703 | value, symsize, type, binding, |
1704 | visibility, nonvis, | |
1705 | offset_is_from_end, | |
1706 | only_if_ref); | |
1707 | #else | |
1708 | gold_unreachable(); | |
1709 | #endif | |
1710 | } | |
8851ecca | 1711 | else if (parameters->target().get_size() == 64) |
86f2e683 ILT |
1712 | { |
1713 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
9b07f471 | 1714 | return this->do_define_in_output_data<64>(name, version, od, |
86f2e683 ILT |
1715 | value, symsize, type, binding, |
1716 | visibility, nonvis, | |
1717 | offset_is_from_end, | |
1718 | only_if_ref); | |
1719 | #else | |
1720 | gold_unreachable(); | |
1721 | #endif | |
1722 | } | |
ead1e424 | 1723 | else |
a3ad94ed | 1724 | gold_unreachable(); |
ead1e424 ILT |
1725 | } |
1726 | ||
1727 | // Define a symbol in an Output_data, sized version. | |
1728 | ||
1729 | template<int size> | |
14b31740 | 1730 | Sized_symbol<size>* |
ead1e424 | 1731 | Symbol_table::do_define_in_output_data( |
ead1e424 | 1732 | const char* name, |
14b31740 | 1733 | const char* version, |
ead1e424 ILT |
1734 | Output_data* od, |
1735 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
1736 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
1737 | elfcpp::STT type, | |
1738 | elfcpp::STB binding, | |
1739 | elfcpp::STV visibility, | |
1740 | unsigned char nonvis, | |
1741 | bool offset_is_from_end, | |
1742 | bool only_if_ref) | |
1743 | { | |
1744 | Sized_symbol<size>* sym; | |
86f2e683 | 1745 | Sized_symbol<size>* oldsym; |
8c500701 | 1746 | bool resolve_oldsym; |
ead1e424 | 1747 | |
8851ecca | 1748 | if (parameters->target().is_big_endian()) |
193a53d9 ILT |
1749 | { |
1750 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) | |
7d1a9ebb | 1751 | sym = this->define_special_symbol<size, true>(&name, &version, |
8c500701 ILT |
1752 | only_if_ref, &oldsym, |
1753 | &resolve_oldsym); | |
193a53d9 ILT |
1754 | #else |
1755 | gold_unreachable(); | |
1756 | #endif | |
1757 | } | |
ead1e424 | 1758 | else |
193a53d9 ILT |
1759 | { |
1760 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) | |
7d1a9ebb | 1761 | sym = this->define_special_symbol<size, false>(&name, &version, |
8c500701 ILT |
1762 | only_if_ref, &oldsym, |
1763 | &resolve_oldsym); | |
193a53d9 ILT |
1764 | #else |
1765 | gold_unreachable(); | |
1766 | #endif | |
1767 | } | |
ead1e424 ILT |
1768 | |
1769 | if (sym == NULL) | |
14b31740 | 1770 | return NULL; |
ead1e424 | 1771 | |
75517b77 ILT |
1772 | sym->init_output_data(name, version, od, value, symsize, type, binding, |
1773 | visibility, nonvis, offset_is_from_end); | |
14b31740 | 1774 | |
e5756efb | 1775 | if (oldsym == NULL) |
55a93433 ILT |
1776 | { |
1777 | if (binding == elfcpp::STB_LOCAL | |
1778 | || this->version_script_.symbol_is_local(name)) | |
1779 | this->force_local(sym); | |
75517b77 ILT |
1780 | else if (version != NULL) |
1781 | sym->set_is_default(); | |
55a93433 ILT |
1782 | return sym; |
1783 | } | |
86f2e683 | 1784 | |
e5756efb ILT |
1785 | if (Symbol_table::should_override_with_special(oldsym)) |
1786 | this->override_with_special(oldsym, sym); | |
8c500701 ILT |
1787 | |
1788 | if (resolve_oldsym) | |
1789 | return sym; | |
1790 | else | |
1791 | { | |
1792 | delete sym; | |
1793 | return oldsym; | |
1794 | } | |
ead1e424 ILT |
1795 | } |
1796 | ||
1797 | // Define a symbol based on an Output_segment. | |
1798 | ||
14b31740 | 1799 | Symbol* |
9b07f471 | 1800 | Symbol_table::define_in_output_segment(const char* name, |
14b31740 | 1801 | const char* version, Output_segment* os, |
9b07f471 ILT |
1802 | uint64_t value, |
1803 | uint64_t symsize, | |
1804 | elfcpp::STT type, | |
1805 | elfcpp::STB binding, | |
ead1e424 ILT |
1806 | elfcpp::STV visibility, |
1807 | unsigned char nonvis, | |
1808 | Symbol::Segment_offset_base offset_base, | |
1809 | bool only_if_ref) | |
1810 | { | |
8851ecca | 1811 | if (parameters->target().get_size() == 32) |
86f2e683 ILT |
1812 | { |
1813 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
9b07f471 | 1814 | return this->do_define_in_output_segment<32>(name, version, os, |
86f2e683 ILT |
1815 | value, symsize, type, |
1816 | binding, visibility, nonvis, | |
1817 | offset_base, only_if_ref); | |
1818 | #else | |
1819 | gold_unreachable(); | |
1820 | #endif | |
1821 | } | |
8851ecca | 1822 | else if (parameters->target().get_size() == 64) |
86f2e683 ILT |
1823 | { |
1824 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
9b07f471 | 1825 | return this->do_define_in_output_segment<64>(name, version, os, |
86f2e683 ILT |
1826 | value, symsize, type, |
1827 | binding, visibility, nonvis, | |
1828 | offset_base, only_if_ref); | |
1829 | #else | |
1830 | gold_unreachable(); | |
1831 | #endif | |
1832 | } | |
ead1e424 | 1833 | else |
a3ad94ed | 1834 | gold_unreachable(); |
ead1e424 ILT |
1835 | } |
1836 | ||
1837 | // Define a symbol in an Output_segment, sized version. | |
1838 | ||
1839 | template<int size> | |
14b31740 | 1840 | Sized_symbol<size>* |
ead1e424 | 1841 | Symbol_table::do_define_in_output_segment( |
ead1e424 | 1842 | const char* name, |
14b31740 | 1843 | const char* version, |
ead1e424 ILT |
1844 | Output_segment* os, |
1845 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
1846 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
1847 | elfcpp::STT type, | |
1848 | elfcpp::STB binding, | |
1849 | elfcpp::STV visibility, | |
1850 | unsigned char nonvis, | |
1851 | Symbol::Segment_offset_base offset_base, | |
1852 | bool only_if_ref) | |
1853 | { | |
1854 | Sized_symbol<size>* sym; | |
86f2e683 | 1855 | Sized_symbol<size>* oldsym; |
8c500701 | 1856 | bool resolve_oldsym; |
ead1e424 | 1857 | |
8851ecca | 1858 | if (parameters->target().is_big_endian()) |
9025d29d ILT |
1859 | { |
1860 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) | |
7d1a9ebb | 1861 | sym = this->define_special_symbol<size, true>(&name, &version, |
8c500701 ILT |
1862 | only_if_ref, &oldsym, |
1863 | &resolve_oldsym); | |
9025d29d ILT |
1864 | #else |
1865 | gold_unreachable(); | |
1866 | #endif | |
1867 | } | |
ead1e424 | 1868 | else |
9025d29d ILT |
1869 | { |
1870 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) | |
7d1a9ebb | 1871 | sym = this->define_special_symbol<size, false>(&name, &version, |
8c500701 ILT |
1872 | only_if_ref, &oldsym, |
1873 | &resolve_oldsym); | |
9025d29d ILT |
1874 | #else |
1875 | gold_unreachable(); | |
1876 | #endif | |
1877 | } | |
ead1e424 ILT |
1878 | |
1879 | if (sym == NULL) | |
14b31740 | 1880 | return NULL; |
ead1e424 | 1881 | |
75517b77 | 1882 | sym->init_output_segment(name, version, os, value, symsize, type, binding, |
f3e9c5c5 | 1883 | visibility, nonvis, offset_base); |
14b31740 | 1884 | |
e5756efb | 1885 | if (oldsym == NULL) |
55a93433 ILT |
1886 | { |
1887 | if (binding == elfcpp::STB_LOCAL | |
1888 | || this->version_script_.symbol_is_local(name)) | |
1889 | this->force_local(sym); | |
75517b77 ILT |
1890 | else if (version != NULL) |
1891 | sym->set_is_default(); | |
55a93433 ILT |
1892 | return sym; |
1893 | } | |
86f2e683 | 1894 | |
e5756efb ILT |
1895 | if (Symbol_table::should_override_with_special(oldsym)) |
1896 | this->override_with_special(oldsym, sym); | |
8c500701 ILT |
1897 | |
1898 | if (resolve_oldsym) | |
1899 | return sym; | |
1900 | else | |
1901 | { | |
1902 | delete sym; | |
1903 | return oldsym; | |
1904 | } | |
ead1e424 ILT |
1905 | } |
1906 | ||
1907 | // Define a special symbol with a constant value. It is a multiple | |
1908 | // definition error if this symbol is already defined. | |
1909 | ||
14b31740 | 1910 | Symbol* |
9b07f471 ILT |
1911 | Symbol_table::define_as_constant(const char* name, |
1912 | const char* version, | |
1913 | uint64_t value, | |
1914 | uint64_t symsize, | |
1915 | elfcpp::STT type, | |
1916 | elfcpp::STB binding, | |
1917 | elfcpp::STV visibility, | |
1918 | unsigned char nonvis, | |
caa9d5d9 ILT |
1919 | bool only_if_ref, |
1920 | bool force_override) | |
ead1e424 | 1921 | { |
8851ecca | 1922 | if (parameters->target().get_size() == 32) |
86f2e683 ILT |
1923 | { |
1924 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
9b07f471 | 1925 | return this->do_define_as_constant<32>(name, version, value, |
86f2e683 | 1926 | symsize, type, binding, |
caa9d5d9 ILT |
1927 | visibility, nonvis, only_if_ref, |
1928 | force_override); | |
86f2e683 ILT |
1929 | #else |
1930 | gold_unreachable(); | |
1931 | #endif | |
1932 | } | |
8851ecca | 1933 | else if (parameters->target().get_size() == 64) |
86f2e683 ILT |
1934 | { |
1935 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
9b07f471 | 1936 | return this->do_define_as_constant<64>(name, version, value, |
86f2e683 | 1937 | symsize, type, binding, |
caa9d5d9 ILT |
1938 | visibility, nonvis, only_if_ref, |
1939 | force_override); | |
86f2e683 ILT |
1940 | #else |
1941 | gold_unreachable(); | |
1942 | #endif | |
1943 | } | |
ead1e424 | 1944 | else |
a3ad94ed | 1945 | gold_unreachable(); |
ead1e424 ILT |
1946 | } |
1947 | ||
1948 | // Define a symbol as a constant, sized version. | |
1949 | ||
1950 | template<int size> | |
14b31740 | 1951 | Sized_symbol<size>* |
ead1e424 | 1952 | Symbol_table::do_define_as_constant( |
ead1e424 | 1953 | const char* name, |
14b31740 | 1954 | const char* version, |
ead1e424 ILT |
1955 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
1956 | typename elfcpp::Elf_types<size>::Elf_WXword symsize, | |
1957 | elfcpp::STT type, | |
1958 | elfcpp::STB binding, | |
1959 | elfcpp::STV visibility, | |
1960 | unsigned char nonvis, | |
caa9d5d9 ILT |
1961 | bool only_if_ref, |
1962 | bool force_override) | |
ead1e424 ILT |
1963 | { |
1964 | Sized_symbol<size>* sym; | |
86f2e683 | 1965 | Sized_symbol<size>* oldsym; |
8c500701 | 1966 | bool resolve_oldsym; |
ead1e424 | 1967 | |
8851ecca | 1968 | if (parameters->target().is_big_endian()) |
9025d29d ILT |
1969 | { |
1970 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) | |
7d1a9ebb | 1971 | sym = this->define_special_symbol<size, true>(&name, &version, |
8c500701 ILT |
1972 | only_if_ref, &oldsym, |
1973 | &resolve_oldsym); | |
9025d29d ILT |
1974 | #else |
1975 | gold_unreachable(); | |
1976 | #endif | |
1977 | } | |
ead1e424 | 1978 | else |
9025d29d ILT |
1979 | { |
1980 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) | |
7d1a9ebb | 1981 | sym = this->define_special_symbol<size, false>(&name, &version, |
8c500701 ILT |
1982 | only_if_ref, &oldsym, |
1983 | &resolve_oldsym); | |
9025d29d ILT |
1984 | #else |
1985 | gold_unreachable(); | |
1986 | #endif | |
1987 | } | |
ead1e424 ILT |
1988 | |
1989 | if (sym == NULL) | |
14b31740 | 1990 | return NULL; |
ead1e424 | 1991 | |
75517b77 ILT |
1992 | sym->init_constant(name, version, value, symsize, type, binding, visibility, |
1993 | nonvis); | |
14b31740 | 1994 | |
e5756efb | 1995 | if (oldsym == NULL) |
55a93433 | 1996 | { |
686c8caf ILT |
1997 | // Version symbols are absolute symbols with name == version. |
1998 | // We don't want to force them to be local. | |
1999 | if ((version == NULL | |
2000 | || name != version | |
2001 | || value != 0) | |
2002 | && (binding == elfcpp::STB_LOCAL | |
2003 | || this->version_script_.symbol_is_local(name))) | |
55a93433 | 2004 | this->force_local(sym); |
75517b77 ILT |
2005 | else if (version != NULL |
2006 | && (name != version || value != 0)) | |
2007 | sym->set_is_default(); | |
55a93433 ILT |
2008 | return sym; |
2009 | } | |
86f2e683 | 2010 | |
caa9d5d9 | 2011 | if (force_override || Symbol_table::should_override_with_special(oldsym)) |
e5756efb | 2012 | this->override_with_special(oldsym, sym); |
8c500701 ILT |
2013 | |
2014 | if (resolve_oldsym) | |
2015 | return sym; | |
2016 | else | |
2017 | { | |
2018 | delete sym; | |
2019 | return oldsym; | |
2020 | } | |
ead1e424 ILT |
2021 | } |
2022 | ||
2023 | // Define a set of symbols in output sections. | |
2024 | ||
2025 | void | |
9b07f471 | 2026 | Symbol_table::define_symbols(const Layout* layout, int count, |
a445fddf ILT |
2027 | const Define_symbol_in_section* p, |
2028 | bool only_if_ref) | |
ead1e424 ILT |
2029 | { |
2030 | for (int i = 0; i < count; ++i, ++p) | |
2031 | { | |
2032 | Output_section* os = layout->find_output_section(p->output_section); | |
2033 | if (os != NULL) | |
9b07f471 | 2034 | this->define_in_output_data(p->name, NULL, os, p->value, |
14b31740 ILT |
2035 | p->size, p->type, p->binding, |
2036 | p->visibility, p->nonvis, | |
a445fddf ILT |
2037 | p->offset_is_from_end, |
2038 | only_if_ref || p->only_if_ref); | |
ead1e424 | 2039 | else |
9b07f471 | 2040 | this->define_as_constant(p->name, NULL, 0, p->size, p->type, |
ead1e424 | 2041 | p->binding, p->visibility, p->nonvis, |
caa9d5d9 ILT |
2042 | only_if_ref || p->only_if_ref, |
2043 | false); | |
ead1e424 ILT |
2044 | } |
2045 | } | |
2046 | ||
2047 | // Define a set of symbols in output segments. | |
2048 | ||
2049 | void | |
9b07f471 | 2050 | Symbol_table::define_symbols(const Layout* layout, int count, |
a445fddf ILT |
2051 | const Define_symbol_in_segment* p, |
2052 | bool only_if_ref) | |
ead1e424 ILT |
2053 | { |
2054 | for (int i = 0; i < count; ++i, ++p) | |
2055 | { | |
2056 | Output_segment* os = layout->find_output_segment(p->segment_type, | |
2057 | p->segment_flags_set, | |
2058 | p->segment_flags_clear); | |
2059 | if (os != NULL) | |
9b07f471 | 2060 | this->define_in_output_segment(p->name, NULL, os, p->value, |
14b31740 ILT |
2061 | p->size, p->type, p->binding, |
2062 | p->visibility, p->nonvis, | |
a445fddf ILT |
2063 | p->offset_base, |
2064 | only_if_ref || p->only_if_ref); | |
ead1e424 | 2065 | else |
9b07f471 | 2066 | this->define_as_constant(p->name, NULL, 0, p->size, p->type, |
ead1e424 | 2067 | p->binding, p->visibility, p->nonvis, |
caa9d5d9 ILT |
2068 | only_if_ref || p->only_if_ref, |
2069 | false); | |
ead1e424 ILT |
2070 | } |
2071 | } | |
2072 | ||
46fe1623 ILT |
2073 | // Define CSYM using a COPY reloc. POSD is the Output_data where the |
2074 | // symbol should be defined--typically a .dyn.bss section. VALUE is | |
2075 | // the offset within POSD. | |
2076 | ||
2077 | template<int size> | |
2078 | void | |
fe8718a4 | 2079 | Symbol_table::define_with_copy_reloc( |
fe8718a4 ILT |
2080 | Sized_symbol<size>* csym, |
2081 | Output_data* posd, | |
2082 | typename elfcpp::Elf_types<size>::Elf_Addr value) | |
46fe1623 ILT |
2083 | { |
2084 | gold_assert(csym->is_from_dynobj()); | |
2085 | gold_assert(!csym->is_copied_from_dynobj()); | |
2086 | Object* object = csym->object(); | |
2087 | gold_assert(object->is_dynamic()); | |
2088 | Dynobj* dynobj = static_cast<Dynobj*>(object); | |
2089 | ||
2090 | // Our copied variable has to override any variable in a shared | |
2091 | // library. | |
2092 | elfcpp::STB binding = csym->binding(); | |
2093 | if (binding == elfcpp::STB_WEAK) | |
2094 | binding = elfcpp::STB_GLOBAL; | |
2095 | ||
9b07f471 | 2096 | this->define_in_output_data(csym->name(), csym->version(), |
46fe1623 ILT |
2097 | posd, value, csym->symsize(), |
2098 | csym->type(), binding, | |
2099 | csym->visibility(), csym->nonvis(), | |
2100 | false, false); | |
2101 | ||
2102 | csym->set_is_copied_from_dynobj(); | |
2103 | csym->set_needs_dynsym_entry(); | |
2104 | ||
2105 | this->copied_symbol_dynobjs_[csym] = dynobj; | |
2106 | ||
2107 | // We have now defined all aliases, but we have not entered them all | |
2108 | // in the copied_symbol_dynobjs_ map. | |
2109 | if (csym->has_alias()) | |
2110 | { | |
2111 | Symbol* sym = csym; | |
2112 | while (true) | |
2113 | { | |
2114 | sym = this->weak_aliases_[sym]; | |
2115 | if (sym == csym) | |
2116 | break; | |
2117 | gold_assert(sym->output_data() == posd); | |
2118 | ||
2119 | sym->set_is_copied_from_dynobj(); | |
2120 | this->copied_symbol_dynobjs_[sym] = dynobj; | |
2121 | } | |
2122 | } | |
2123 | } | |
2124 | ||
2125 | // SYM is defined using a COPY reloc. Return the dynamic object where | |
2126 | // the original definition was found. | |
2127 | ||
2128 | Dynobj* | |
2129 | Symbol_table::get_copy_source(const Symbol* sym) const | |
2130 | { | |
2131 | gold_assert(sym->is_copied_from_dynobj()); | |
2132 | Copied_symbol_dynobjs::const_iterator p = | |
2133 | this->copied_symbol_dynobjs_.find(sym); | |
2134 | gold_assert(p != this->copied_symbol_dynobjs_.end()); | |
2135 | return p->second; | |
2136 | } | |
2137 | ||
f3e9c5c5 ILT |
2138 | // Add any undefined symbols named on the command line. |
2139 | ||
2140 | void | |
2141 | Symbol_table::add_undefined_symbols_from_command_line() | |
2142 | { | |
2143 | if (parameters->options().any_undefined()) | |
2144 | { | |
2145 | if (parameters->target().get_size() == 32) | |
2146 | { | |
5adf9721 | 2147 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
f3e9c5c5 ILT |
2148 | this->do_add_undefined_symbols_from_command_line<32>(); |
2149 | #else | |
2150 | gold_unreachable(); | |
2151 | #endif | |
2152 | } | |
2153 | else if (parameters->target().get_size() == 64) | |
2154 | { | |
2155 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
2156 | this->do_add_undefined_symbols_from_command_line<64>(); | |
2157 | #else | |
2158 | gold_unreachable(); | |
2159 | #endif | |
2160 | } | |
2161 | else | |
2162 | gold_unreachable(); | |
2163 | } | |
2164 | } | |
2165 | ||
2166 | template<int size> | |
2167 | void | |
2168 | Symbol_table::do_add_undefined_symbols_from_command_line() | |
2169 | { | |
2170 | for (options::String_set::const_iterator p = | |
2171 | parameters->options().undefined_begin(); | |
2172 | p != parameters->options().undefined_end(); | |
2173 | ++p) | |
2174 | { | |
2175 | const char* name = p->c_str(); | |
2176 | ||
2177 | if (this->lookup(name) != NULL) | |
2178 | continue; | |
2179 | ||
2180 | const char* version = NULL; | |
2181 | ||
2182 | Sized_symbol<size>* sym; | |
2183 | Sized_symbol<size>* oldsym; | |
8c500701 | 2184 | bool resolve_oldsym; |
f3e9c5c5 ILT |
2185 | if (parameters->target().is_big_endian()) |
2186 | { | |
2187 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG) | |
2188 | sym = this->define_special_symbol<size, true>(&name, &version, | |
8c500701 ILT |
2189 | false, &oldsym, |
2190 | &resolve_oldsym); | |
f3e9c5c5 ILT |
2191 | #else |
2192 | gold_unreachable(); | |
2193 | #endif | |
2194 | } | |
2195 | else | |
2196 | { | |
2197 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE) | |
2198 | sym = this->define_special_symbol<size, false>(&name, &version, | |
8c500701 ILT |
2199 | false, &oldsym, |
2200 | &resolve_oldsym); | |
f3e9c5c5 ILT |
2201 | #else |
2202 | gold_unreachable(); | |
2203 | #endif | |
2204 | } | |
2205 | ||
2206 | gold_assert(oldsym == NULL); | |
2207 | ||
75517b77 | 2208 | sym->init_undefined(name, version, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, |
f3e9c5c5 ILT |
2209 | elfcpp::STV_DEFAULT, 0); |
2210 | ++this->saw_undefined_; | |
2211 | } | |
2212 | } | |
2213 | ||
a3ad94ed ILT |
2214 | // Set the dynamic symbol indexes. INDEX is the index of the first |
2215 | // global dynamic symbol. Pointers to the symbols are stored into the | |
2216 | // vector SYMS. The names are added to DYNPOOL. This returns an | |
2217 | // updated dynamic symbol index. | |
2218 | ||
2219 | unsigned int | |
9b07f471 | 2220 | Symbol_table::set_dynsym_indexes(unsigned int index, |
a3ad94ed | 2221 | std::vector<Symbol*>* syms, |
14b31740 ILT |
2222 | Stringpool* dynpool, |
2223 | Versions* versions) | |
a3ad94ed ILT |
2224 | { |
2225 | for (Symbol_table_type::iterator p = this->table_.begin(); | |
2226 | p != this->table_.end(); | |
2227 | ++p) | |
2228 | { | |
2229 | Symbol* sym = p->second; | |
16649710 ILT |
2230 | |
2231 | // Note that SYM may already have a dynamic symbol index, since | |
2232 | // some symbols appear more than once in the symbol table, with | |
2233 | // and without a version. | |
2234 | ||
436ca963 | 2235 | if (!sym->should_add_dynsym_entry()) |
16649710 ILT |
2236 | sym->set_dynsym_index(-1U); |
2237 | else if (!sym->has_dynsym_index()) | |
a3ad94ed ILT |
2238 | { |
2239 | sym->set_dynsym_index(index); | |
2240 | ++index; | |
2241 | syms->push_back(sym); | |
cfd73a4e | 2242 | dynpool->add(sym->name(), false, NULL); |
14b31740 ILT |
2243 | |
2244 | // Record any version information. | |
09124467 ILT |
2245 | if (sym->version() != NULL) |
2246 | versions->record_version(this, dynpool, sym); | |
594c8e5e ILT |
2247 | |
2248 | // If the symbol is defined in a dynamic object and is | |
2249 | // referenced in a regular object, then mark the dynamic | |
2250 | // object as needed. This is used to implement --as-needed. | |
2251 | if (sym->is_from_dynobj() && sym->in_reg()) | |
2252 | sym->object()->set_is_needed(); | |
a3ad94ed ILT |
2253 | } |
2254 | } | |
2255 | ||
14b31740 ILT |
2256 | // Finish up the versions. In some cases this may add new dynamic |
2257 | // symbols. | |
9b07f471 | 2258 | index = versions->finalize(this, index, syms); |
14b31740 | 2259 | |
a3ad94ed ILT |
2260 | return index; |
2261 | } | |
2262 | ||
c06b7b0b | 2263 | // Set the final values for all the symbols. The index of the first |
55a93433 ILT |
2264 | // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the |
2265 | // file offset OFF. Add their names to POOL. Return the new file | |
2266 | // offset. Update *PLOCAL_SYMCOUNT if necessary. | |
54dc6425 | 2267 | |
75f65a3e | 2268 | off_t |
55a93433 ILT |
2269 | Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index, |
2270 | size_t dyncount, Stringpool* pool, | |
2271 | unsigned int *plocal_symcount) | |
54dc6425 | 2272 | { |
f6ce93d6 ILT |
2273 | off_t ret; |
2274 | ||
55a93433 ILT |
2275 | gold_assert(*plocal_symcount != 0); |
2276 | this->first_global_index_ = *plocal_symcount; | |
c06b7b0b | 2277 | |
16649710 ILT |
2278 | this->dynamic_offset_ = dynoff; |
2279 | this->first_dynamic_global_index_ = dyn_global_index; | |
2280 | this->dynamic_count_ = dyncount; | |
2281 | ||
8851ecca | 2282 | if (parameters->target().get_size() == 32) |
9025d29d ILT |
2283 | { |
2284 | #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE) | |
55a93433 | 2285 | ret = this->sized_finalize<32>(off, pool, plocal_symcount); |
9025d29d ILT |
2286 | #else |
2287 | gold_unreachable(); | |
2288 | #endif | |
2289 | } | |
8851ecca | 2290 | else if (parameters->target().get_size() == 64) |
9025d29d ILT |
2291 | { |
2292 | #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE) | |
55a93433 | 2293 | ret = this->sized_finalize<64>(off, pool, plocal_symcount); |
9025d29d ILT |
2294 | #else |
2295 | gold_unreachable(); | |
2296 | #endif | |
2297 | } | |
61ba1cf9 | 2298 | else |
a3ad94ed | 2299 | gold_unreachable(); |
f6ce93d6 ILT |
2300 | |
2301 | // Now that we have the final symbol table, we can reliably note | |
2302 | // which symbols should get warnings. | |
cb295612 | 2303 | this->warnings_.note_warnings(this); |
f6ce93d6 ILT |
2304 | |
2305 | return ret; | |
75f65a3e ILT |
2306 | } |
2307 | ||
55a93433 ILT |
2308 | // SYM is going into the symbol table at *PINDEX. Add the name to |
2309 | // POOL, update *PINDEX and *POFF. | |
2310 | ||
2311 | template<int size> | |
2312 | void | |
2313 | Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool, | |
2314 | unsigned int* pindex, off_t* poff) | |
2315 | { | |
2316 | sym->set_symtab_index(*pindex); | |
2317 | pool->add(sym->name(), false, NULL); | |
2318 | ++*pindex; | |
2319 | *poff += elfcpp::Elf_sizes<size>::sym_size; | |
2320 | } | |
2321 | ||
ead1e424 ILT |
2322 | // Set the final value for all the symbols. This is called after |
2323 | // Layout::finalize, so all the output sections have their final | |
2324 | // address. | |
75f65a3e ILT |
2325 | |
2326 | template<int size> | |
2327 | off_t | |
55a93433 ILT |
2328 | Symbol_table::sized_finalize(off_t off, Stringpool* pool, |
2329 | unsigned int* plocal_symcount) | |
75f65a3e | 2330 | { |
ead1e424 | 2331 | off = align_address(off, size >> 3); |
75f65a3e ILT |
2332 | this->offset_ = off; |
2333 | ||
55a93433 ILT |
2334 | unsigned int index = *plocal_symcount; |
2335 | const unsigned int orig_index = index; | |
c06b7b0b | 2336 | |
55a93433 ILT |
2337 | // First do all the symbols which have been forced to be local, as |
2338 | // they must appear before all global symbols. | |
2339 | for (Forced_locals::iterator p = this->forced_locals_.begin(); | |
2340 | p != this->forced_locals_.end(); | |
2341 | ++p) | |
2342 | { | |
2343 | Symbol* sym = *p; | |
2344 | gold_assert(sym->is_forced_local()); | |
2345 | if (this->sized_finalize_symbol<size>(sym)) | |
2346 | { | |
2347 | this->add_to_final_symtab<size>(sym, pool, &index, &off); | |
2348 | ++*plocal_symcount; | |
2349 | } | |
2350 | } | |
2351 | ||
2352 | // Now do all the remaining symbols. | |
c06b7b0b ILT |
2353 | for (Symbol_table_type::iterator p = this->table_.begin(); |
2354 | p != this->table_.end(); | |
2355 | ++p) | |
54dc6425 | 2356 | { |
55a93433 ILT |
2357 | Symbol* sym = p->second; |
2358 | if (this->sized_finalize_symbol<size>(sym)) | |
2359 | this->add_to_final_symtab<size>(sym, pool, &index, &off); | |
2360 | } | |
54dc6425 | 2361 | |
55a93433 | 2362 | this->output_count_ = index - orig_index; |
a3ad94ed | 2363 | |
55a93433 ILT |
2364 | return off; |
2365 | } | |
75f65a3e | 2366 | |
c0a62865 DK |
2367 | // Compute the final value of SYM and store status in location PSTATUS. |
2368 | // During relaxation, this may be called multiple times for a symbol to | |
2369 | // compute its would-be final value in each relaxation pass. | |
008db82e | 2370 | |
55a93433 | 2371 | template<int size> |
c0a62865 DK |
2372 | typename Sized_symbol<size>::Value_type |
2373 | Symbol_table::compute_final_value( | |
2374 | const Sized_symbol<size>* sym, | |
2375 | Compute_final_value_status* pstatus) const | |
55a93433 | 2376 | { |
ef9beddf | 2377 | typedef typename Sized_symbol<size>::Value_type Value_type; |
ef9beddf | 2378 | Value_type value; |
ead1e424 | 2379 | |
55a93433 ILT |
2380 | switch (sym->source()) |
2381 | { | |
2382 | case Symbol::FROM_OBJECT: | |
2383 | { | |
d491d34e ILT |
2384 | bool is_ordinary; |
2385 | unsigned int shndx = sym->shndx(&is_ordinary); | |
ead1e424 | 2386 | |
d491d34e | 2387 | if (!is_ordinary |
0dfbdef4 | 2388 | && shndx != elfcpp::SHN_ABS |
8a5e3e08 | 2389 | && !Symbol::is_common_shndx(shndx)) |
55a93433 | 2390 | { |
c0a62865 DK |
2391 | *pstatus = CFVS_UNSUPPORTED_SYMBOL_SECTION; |
2392 | return 0; | |
ead1e424 | 2393 | } |
ead1e424 | 2394 | |
55a93433 ILT |
2395 | Object* symobj = sym->object(); |
2396 | if (symobj->is_dynamic()) | |
ead1e424 | 2397 | { |
55a93433 ILT |
2398 | value = 0; |
2399 | shndx = elfcpp::SHN_UNDEF; | |
ead1e424 | 2400 | } |
89fc3421 CC |
2401 | else if (symobj->pluginobj() != NULL) |
2402 | { | |
2403 | value = 0; | |
2404 | shndx = elfcpp::SHN_UNDEF; | |
2405 | } | |
55a93433 ILT |
2406 | else if (shndx == elfcpp::SHN_UNDEF) |
2407 | value = 0; | |
d491d34e | 2408 | else if (!is_ordinary |
8a5e3e08 ILT |
2409 | && (shndx == elfcpp::SHN_ABS |
2410 | || Symbol::is_common_shndx(shndx))) | |
55a93433 ILT |
2411 | value = sym->value(); |
2412 | else | |
ead1e424 | 2413 | { |
55a93433 | 2414 | Relobj* relobj = static_cast<Relobj*>(symobj); |
ef9beddf | 2415 | Output_section* os = relobj->output_section(shndx); |
ef15dade | 2416 | uint64_t secoff64 = relobj->output_section_offset(shndx); |
55a93433 | 2417 | |
ef15dade ST |
2418 | if (this->is_section_folded(relobj, shndx)) |
2419 | { | |
2420 | gold_assert(os == NULL); | |
2421 | // Get the os of the section it is folded onto. | |
2422 | Section_id folded = this->icf_->get_folded_section(relobj, | |
2423 | shndx); | |
2424 | gold_assert(folded.first != NULL); | |
2425 | Relobj* folded_obj = reinterpret_cast<Relobj*>(folded.first); | |
2426 | os = folded_obj->output_section(folded.second); | |
2427 | gold_assert(os != NULL); | |
2428 | secoff64 = folded_obj->output_section_offset(folded.second); | |
2429 | } | |
2430 | ||
2431 | if (os == NULL) | |
ead1e424 | 2432 | { |
6d03d481 ST |
2433 | bool static_or_reloc = (parameters->doing_static_link() || |
2434 | parameters->options().relocatable()); | |
2435 | gold_assert(static_or_reloc || sym->dynsym_index() == -1U); | |
2436 | ||
c0a62865 DK |
2437 | *pstatus = CFVS_NO_OUTPUT_SECTION; |
2438 | return 0; | |
ead1e424 | 2439 | } |
55a93433 | 2440 | |
eff45813 CC |
2441 | if (secoff64 == -1ULL) |
2442 | { | |
2443 | // The section needs special handling (e.g., a merge section). | |
ef15dade | 2444 | |
eff45813 CC |
2445 | value = os->output_address(relobj, shndx, sym->value()); |
2446 | } | |
2447 | else | |
2448 | { | |
2449 | Value_type secoff = | |
2450 | convert_types<Value_type, uint64_t>(secoff64); | |
2451 | if (sym->type() == elfcpp::STT_TLS) | |
2452 | value = sym->value() + os->tls_offset() + secoff; | |
2453 | else | |
2454 | value = sym->value() + os->address() + secoff; | |
2455 | } | |
ead1e424 | 2456 | } |
55a93433 ILT |
2457 | } |
2458 | break; | |
2459 | ||
2460 | case Symbol::IN_OUTPUT_DATA: | |
2461 | { | |
2462 | Output_data* od = sym->output_data(); | |
155a0dd7 ILT |
2463 | value = sym->value(); |
2464 | if (sym->type() != elfcpp::STT_TLS) | |
2465 | value += od->address(); | |
2466 | else | |
2467 | { | |
2468 | Output_section* os = od->output_section(); | |
2469 | gold_assert(os != NULL); | |
2470 | value += os->tls_offset() + (od->address() - os->address()); | |
2471 | } | |
55a93433 ILT |
2472 | if (sym->offset_is_from_end()) |
2473 | value += od->data_size(); | |
2474 | } | |
2475 | break; | |
2476 | ||
2477 | case Symbol::IN_OUTPUT_SEGMENT: | |
2478 | { | |
2479 | Output_segment* os = sym->output_segment(); | |
edfbb029 CC |
2480 | value = sym->value(); |
2481 | if (sym->type() != elfcpp::STT_TLS) | |
2482 | value += os->vaddr(); | |
55a93433 ILT |
2483 | switch (sym->offset_base()) |
2484 | { | |
2485 | case Symbol::SEGMENT_START: | |
2486 | break; | |
2487 | case Symbol::SEGMENT_END: | |
2488 | value += os->memsz(); | |
2489 | break; | |
2490 | case Symbol::SEGMENT_BSS: | |
2491 | value += os->filesz(); | |
2492 | break; | |
2493 | default: | |
2494 | gold_unreachable(); | |
2495 | } | |
2496 | } | |
2497 | break; | |
ead1e424 | 2498 | |
f3e9c5c5 | 2499 | case Symbol::IS_CONSTANT: |
55a93433 ILT |
2500 | value = sym->value(); |
2501 | break; | |
ead1e424 | 2502 | |
f3e9c5c5 ILT |
2503 | case Symbol::IS_UNDEFINED: |
2504 | value = 0; | |
2505 | break; | |
2506 | ||
55a93433 ILT |
2507 | default: |
2508 | gold_unreachable(); | |
2509 | } | |
ead1e424 | 2510 | |
c0a62865 DK |
2511 | *pstatus = CFVS_OK; |
2512 | return value; | |
2513 | } | |
2514 | ||
2515 | // Finalize the symbol SYM. This returns true if the symbol should be | |
2516 | // added to the symbol table, false otherwise. | |
2517 | ||
2518 | template<int size> | |
2519 | bool | |
2520 | Symbol_table::sized_finalize_symbol(Symbol* unsized_sym) | |
2521 | { | |
2522 | typedef typename Sized_symbol<size>::Value_type Value_type; | |
2523 | ||
2524 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym); | |
2525 | ||
2526 | // The default version of a symbol may appear twice in the symbol | |
2527 | // table. We only need to finalize it once. | |
2528 | if (sym->has_symtab_index()) | |
2529 | return false; | |
2530 | ||
2531 | if (!sym->in_reg()) | |
2532 | { | |
2533 | gold_assert(!sym->has_symtab_index()); | |
2534 | sym->set_symtab_index(-1U); | |
2535 | gold_assert(sym->dynsym_index() == -1U); | |
2536 | return false; | |
2537 | } | |
2538 | ||
2539 | // Compute final symbol value. | |
2540 | Compute_final_value_status status; | |
2541 | Value_type value = this->compute_final_value(sym, &status); | |
2542 | ||
2543 | switch (status) | |
2544 | { | |
2545 | case CFVS_OK: | |
2546 | break; | |
2547 | case CFVS_UNSUPPORTED_SYMBOL_SECTION: | |
2548 | { | |
2549 | bool is_ordinary; | |
2550 | unsigned int shndx = sym->shndx(&is_ordinary); | |
2551 | gold_error(_("%s: unsupported symbol section 0x%x"), | |
2552 | sym->demangled_name().c_str(), shndx); | |
2553 | } | |
2554 | break; | |
2555 | case CFVS_NO_OUTPUT_SECTION: | |
2556 | sym->set_symtab_index(-1U); | |
2557 | return false; | |
2558 | default: | |
2559 | gold_unreachable(); | |
2560 | } | |
2561 | ||
55a93433 | 2562 | sym->set_value(value); |
9e2dcb77 | 2563 | |
8c604651 CS |
2564 | if (parameters->options().strip_all() |
2565 | || !parameters->options().should_retain_symbol(sym->name())) | |
55a93433 ILT |
2566 | { |
2567 | sym->set_symtab_index(-1U); | |
2568 | return false; | |
54dc6425 | 2569 | } |
75f65a3e | 2570 | |
55a93433 | 2571 | return true; |
54dc6425 ILT |
2572 | } |
2573 | ||
61ba1cf9 ILT |
2574 | // Write out the global symbols. |
2575 | ||
2576 | void | |
fd9d194f | 2577 | Symbol_table::write_globals(const Stringpool* sympool, |
d491d34e ILT |
2578 | const Stringpool* dynpool, |
2579 | Output_symtab_xindex* symtab_xindex, | |
2580 | Output_symtab_xindex* dynsym_xindex, | |
2581 | Output_file* of) const | |
61ba1cf9 | 2582 | { |
8851ecca | 2583 | switch (parameters->size_and_endianness()) |
61ba1cf9 | 2584 | { |
9025d29d | 2585 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca | 2586 | case Parameters::TARGET_32_LITTLE: |
fd9d194f | 2587 | this->sized_write_globals<32, false>(sympool, dynpool, symtab_xindex, |
d491d34e | 2588 | dynsym_xindex, of); |
8851ecca | 2589 | break; |
9025d29d | 2590 | #endif |
8851ecca ILT |
2591 | #ifdef HAVE_TARGET_32_BIG |
2592 | case Parameters::TARGET_32_BIG: | |
fd9d194f | 2593 | this->sized_write_globals<32, true>(sympool, dynpool, symtab_xindex, |
d491d34e | 2594 | dynsym_xindex, of); |
8851ecca | 2595 | break; |
9025d29d | 2596 | #endif |
9025d29d | 2597 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca | 2598 | case Parameters::TARGET_64_LITTLE: |
fd9d194f | 2599 | this->sized_write_globals<64, false>(sympool, dynpool, symtab_xindex, |
d491d34e | 2600 | dynsym_xindex, of); |
8851ecca | 2601 | break; |
9025d29d | 2602 | #endif |
8851ecca ILT |
2603 | #ifdef HAVE_TARGET_64_BIG |
2604 | case Parameters::TARGET_64_BIG: | |
fd9d194f | 2605 | this->sized_write_globals<64, true>(sympool, dynpool, symtab_xindex, |
d491d34e | 2606 | dynsym_xindex, of); |
8851ecca ILT |
2607 | break; |
2608 | #endif | |
2609 | default: | |
2610 | gold_unreachable(); | |
61ba1cf9 | 2611 | } |
61ba1cf9 ILT |
2612 | } |
2613 | ||
2614 | // Write out the global symbols. | |
2615 | ||
2616 | template<int size, bool big_endian> | |
2617 | void | |
fd9d194f | 2618 | Symbol_table::sized_write_globals(const Stringpool* sympool, |
16649710 | 2619 | const Stringpool* dynpool, |
d491d34e ILT |
2620 | Output_symtab_xindex* symtab_xindex, |
2621 | Output_symtab_xindex* dynsym_xindex, | |
61ba1cf9 ILT |
2622 | Output_file* of) const |
2623 | { | |
8851ecca | 2624 | const Target& target = parameters->target(); |
9a2d6984 | 2625 | |
61ba1cf9 | 2626 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
55a93433 ILT |
2627 | |
2628 | const unsigned int output_count = this->output_count_; | |
2629 | const section_size_type oview_size = output_count * sym_size; | |
2630 | const unsigned int first_global_index = this->first_global_index_; | |
5fe2a0f5 ILT |
2631 | unsigned char* psyms; |
2632 | if (this->offset_ == 0 || output_count == 0) | |
2633 | psyms = NULL; | |
2634 | else | |
2635 | psyms = of->get_output_view(this->offset_, oview_size); | |
16649710 | 2636 | |
55a93433 ILT |
2637 | const unsigned int dynamic_count = this->dynamic_count_; |
2638 | const section_size_type dynamic_size = dynamic_count * sym_size; | |
2639 | const unsigned int first_dynamic_global_index = | |
2640 | this->first_dynamic_global_index_; | |
16649710 | 2641 | unsigned char* dynamic_view; |
5fe2a0f5 | 2642 | if (this->dynamic_offset_ == 0 || dynamic_count == 0) |
16649710 ILT |
2643 | dynamic_view = NULL; |
2644 | else | |
2645 | dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size); | |
c06b7b0b | 2646 | |
61ba1cf9 ILT |
2647 | for (Symbol_table_type::const_iterator p = this->table_.begin(); |
2648 | p != this->table_.end(); | |
2649 | ++p) | |
2650 | { | |
2651 | Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second); | |
2652 | ||
9a2d6984 | 2653 | // Possibly warn about unresolved symbols in shared libraries. |
fd9d194f | 2654 | this->warn_about_undefined_dynobj_symbol(sym); |
e2827e5f | 2655 | |
a3ad94ed | 2656 | unsigned int sym_index = sym->symtab_index(); |
16649710 ILT |
2657 | unsigned int dynsym_index; |
2658 | if (dynamic_view == NULL) | |
2659 | dynsym_index = -1U; | |
2660 | else | |
2661 | dynsym_index = sym->dynsym_index(); | |
2662 | ||
2663 | if (sym_index == -1U && dynsym_index == -1U) | |
a3ad94ed ILT |
2664 | { |
2665 | // This symbol is not included in the output file. | |
2666 | continue; | |
2667 | } | |
16649710 | 2668 | |
ead1e424 | 2669 | unsigned int shndx; |
88dd47ac ILT |
2670 | typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value(); |
2671 | typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value; | |
ead1e424 ILT |
2672 | switch (sym->source()) |
2673 | { | |
2674 | case Symbol::FROM_OBJECT: | |
2675 | { | |
d491d34e ILT |
2676 | bool is_ordinary; |
2677 | unsigned int in_shndx = sym->shndx(&is_ordinary); | |
ead1e424 | 2678 | |
d491d34e | 2679 | if (!is_ordinary |
0dfbdef4 | 2680 | && in_shndx != elfcpp::SHN_ABS |
8a5e3e08 | 2681 | && !Symbol::is_common_shndx(in_shndx)) |
ead1e424 | 2682 | { |
75f2446e | 2683 | gold_error(_("%s: unsupported symbol section 0x%x"), |
a2b1aa12 | 2684 | sym->demangled_name().c_str(), in_shndx); |
75f2446e | 2685 | shndx = in_shndx; |
f6ce93d6 | 2686 | } |
ead1e424 ILT |
2687 | else |
2688 | { | |
75f2446e ILT |
2689 | Object* symobj = sym->object(); |
2690 | if (symobj->is_dynamic()) | |
2691 | { | |
2692 | if (sym->needs_dynsym_value()) | |
8851ecca | 2693 | dynsym_value = target.dynsym_value(sym); |
75f2446e ILT |
2694 | shndx = elfcpp::SHN_UNDEF; |
2695 | } | |
89fc3421 CC |
2696 | else if (symobj->pluginobj() != NULL) |
2697 | shndx = elfcpp::SHN_UNDEF; | |
75f2446e | 2698 | else if (in_shndx == elfcpp::SHN_UNDEF |
d491d34e ILT |
2699 | || (!is_ordinary |
2700 | && (in_shndx == elfcpp::SHN_ABS | |
8a5e3e08 | 2701 | || Symbol::is_common_shndx(in_shndx)))) |
75f2446e ILT |
2702 | shndx = in_shndx; |
2703 | else | |
2704 | { | |
2705 | Relobj* relobj = static_cast<Relobj*>(symobj); | |
ef9beddf | 2706 | Output_section* os = relobj->output_section(in_shndx); |
ef15dade ST |
2707 | if (this->is_section_folded(relobj, in_shndx)) |
2708 | { | |
2709 | // This global symbol must be written out even though | |
2710 | // it is folded. | |
2711 | // Get the os of the section it is folded onto. | |
2712 | Section_id folded = | |
2713 | this->icf_->get_folded_section(relobj, in_shndx); | |
2714 | gold_assert(folded.first !=NULL); | |
2715 | Relobj* folded_obj = | |
2716 | reinterpret_cast<Relobj*>(folded.first); | |
2717 | os = folded_obj->output_section(folded.second); | |
2718 | gold_assert(os != NULL); | |
2719 | } | |
75f2446e ILT |
2720 | gold_assert(os != NULL); |
2721 | shndx = os->out_shndx(); | |
88dd47ac | 2722 | |
d491d34e ILT |
2723 | if (shndx >= elfcpp::SHN_LORESERVE) |
2724 | { | |
2725 | if (sym_index != -1U) | |
2726 | symtab_xindex->add(sym_index, shndx); | |
2727 | if (dynsym_index != -1U) | |
2728 | dynsym_xindex->add(dynsym_index, shndx); | |
2729 | shndx = elfcpp::SHN_XINDEX; | |
2730 | } | |
2731 | ||
88dd47ac ILT |
2732 | // In object files symbol values are section |
2733 | // relative. | |
8851ecca | 2734 | if (parameters->options().relocatable()) |
88dd47ac | 2735 | sym_value -= os->address(); |
75f2446e | 2736 | } |
ead1e424 ILT |
2737 | } |
2738 | } | |
2739 | break; | |
2740 | ||
2741 | case Symbol::IN_OUTPUT_DATA: | |
2742 | shndx = sym->output_data()->out_shndx(); | |
d491d34e ILT |
2743 | if (shndx >= elfcpp::SHN_LORESERVE) |
2744 | { | |
2745 | if (sym_index != -1U) | |
2746 | symtab_xindex->add(sym_index, shndx); | |
2747 | if (dynsym_index != -1U) | |
2748 | dynsym_xindex->add(dynsym_index, shndx); | |
2749 | shndx = elfcpp::SHN_XINDEX; | |
2750 | } | |
ead1e424 ILT |
2751 | break; |
2752 | ||
2753 | case Symbol::IN_OUTPUT_SEGMENT: | |
2754 | shndx = elfcpp::SHN_ABS; | |
2755 | break; | |
2756 | ||
f3e9c5c5 | 2757 | case Symbol::IS_CONSTANT: |
ead1e424 ILT |
2758 | shndx = elfcpp::SHN_ABS; |
2759 | break; | |
2760 | ||
f3e9c5c5 ILT |
2761 | case Symbol::IS_UNDEFINED: |
2762 | shndx = elfcpp::SHN_UNDEF; | |
2763 | break; | |
2764 | ||
ead1e424 | 2765 | default: |
a3ad94ed | 2766 | gold_unreachable(); |
ead1e424 | 2767 | } |
61ba1cf9 | 2768 | |
16649710 ILT |
2769 | if (sym_index != -1U) |
2770 | { | |
55a93433 ILT |
2771 | sym_index -= first_global_index; |
2772 | gold_assert(sym_index < output_count); | |
2773 | unsigned char* ps = psyms + (sym_index * sym_size); | |
7d1a9ebb ILT |
2774 | this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx, |
2775 | sympool, ps); | |
16649710 | 2776 | } |
61ba1cf9 | 2777 | |
16649710 ILT |
2778 | if (dynsym_index != -1U) |
2779 | { | |
2780 | dynsym_index -= first_dynamic_global_index; | |
2781 | gold_assert(dynsym_index < dynamic_count); | |
2782 | unsigned char* pd = dynamic_view + (dynsym_index * sym_size); | |
7d1a9ebb ILT |
2783 | this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx, |
2784 | dynpool, pd); | |
16649710 | 2785 | } |
61ba1cf9 ILT |
2786 | } |
2787 | ||
c06b7b0b | 2788 | of->write_output_view(this->offset_, oview_size, psyms); |
16649710 ILT |
2789 | if (dynamic_view != NULL) |
2790 | of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view); | |
2791 | } | |
2792 | ||
2793 | // Write out the symbol SYM, in section SHNDX, to P. POOL is the | |
2794 | // strtab holding the name. | |
2795 | ||
2796 | template<int size, bool big_endian> | |
2797 | void | |
ab5c9e90 ILT |
2798 | Symbol_table::sized_write_symbol( |
2799 | Sized_symbol<size>* sym, | |
2800 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
2801 | unsigned int shndx, | |
2802 | const Stringpool* pool, | |
7d1a9ebb | 2803 | unsigned char* p) const |
16649710 ILT |
2804 | { |
2805 | elfcpp::Sym_write<size, big_endian> osym(p); | |
2806 | osym.put_st_name(pool->get_offset(sym->name())); | |
ab5c9e90 | 2807 | osym.put_st_value(value); |
58e54ac2 CD |
2808 | // Use a symbol size of zero for undefined symbols from shared libraries. |
2809 | if (shndx == elfcpp::SHN_UNDEF && sym->is_from_dynobj()) | |
2810 | osym.put_st_size(0); | |
2811 | else | |
2812 | osym.put_st_size(sym->symsize()); | |
53d7974c L |
2813 | elfcpp::STT type = sym->type(); |
2814 | // Turn IFUNC symbols from shared libraries into normal FUNC symbols. | |
2815 | if (type == elfcpp::STT_GNU_IFUNC | |
2816 | && sym->is_from_dynobj()) | |
2817 | type = elfcpp::STT_FUNC; | |
55a93433 ILT |
2818 | // A version script may have overridden the default binding. |
2819 | if (sym->is_forced_local()) | |
53d7974c | 2820 | osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, type)); |
55a93433 | 2821 | else |
53d7974c | 2822 | osym.put_st_info(elfcpp::elf_st_info(sym->binding(), type)); |
16649710 ILT |
2823 | osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis())); |
2824 | osym.put_st_shndx(shndx); | |
61ba1cf9 ILT |
2825 | } |
2826 | ||
9a2d6984 ILT |
2827 | // Check for unresolved symbols in shared libraries. This is |
2828 | // controlled by the --allow-shlib-undefined option. | |
2829 | ||
2830 | // We only warn about libraries for which we have seen all the | |
2831 | // DT_NEEDED entries. We don't try to track down DT_NEEDED entries | |
2832 | // which were not seen in this link. If we didn't see a DT_NEEDED | |
2833 | // entry, we aren't going to be able to reliably report whether the | |
2834 | // symbol is undefined. | |
2835 | ||
fd9d194f ILT |
2836 | // We also don't warn about libraries found in a system library |
2837 | // directory (e.g., /lib or /usr/lib); we assume that those libraries | |
2838 | // are OK. This heuristic avoids problems on GNU/Linux, in which -ldl | |
2839 | // can have undefined references satisfied by ld-linux.so. | |
9a2d6984 ILT |
2840 | |
2841 | inline void | |
fd9d194f | 2842 | Symbol_table::warn_about_undefined_dynobj_symbol(Symbol* sym) const |
9a2d6984 | 2843 | { |
d491d34e | 2844 | bool dummy; |
9a2d6984 ILT |
2845 | if (sym->source() == Symbol::FROM_OBJECT |
2846 | && sym->object()->is_dynamic() | |
d491d34e | 2847 | && sym->shndx(&dummy) == elfcpp::SHN_UNDEF |
9a2d6984 | 2848 | && sym->binding() != elfcpp::STB_WEAK |
8851ecca ILT |
2849 | && !parameters->options().allow_shlib_undefined() |
2850 | && !parameters->target().is_defined_by_abi(sym) | |
fd9d194f | 2851 | && !sym->object()->is_in_system_directory()) |
9a2d6984 ILT |
2852 | { |
2853 | // A very ugly cast. | |
2854 | Dynobj* dynobj = static_cast<Dynobj*>(sym->object()); | |
2855 | if (!dynobj->has_unknown_needed_entries()) | |
f073bbf7 | 2856 | gold_undefined_symbol(sym); |
9a2d6984 ILT |
2857 | } |
2858 | } | |
2859 | ||
a3ad94ed ILT |
2860 | // Write out a section symbol. Return the update offset. |
2861 | ||
2862 | void | |
9025d29d | 2863 | Symbol_table::write_section_symbol(const Output_section *os, |
d491d34e | 2864 | Output_symtab_xindex* symtab_xindex, |
a3ad94ed ILT |
2865 | Output_file* of, |
2866 | off_t offset) const | |
2867 | { | |
8851ecca | 2868 | switch (parameters->size_and_endianness()) |
a3ad94ed | 2869 | { |
9025d29d | 2870 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca | 2871 | case Parameters::TARGET_32_LITTLE: |
d491d34e ILT |
2872 | this->sized_write_section_symbol<32, false>(os, symtab_xindex, of, |
2873 | offset); | |
8851ecca | 2874 | break; |
9025d29d | 2875 | #endif |
8851ecca ILT |
2876 | #ifdef HAVE_TARGET_32_BIG |
2877 | case Parameters::TARGET_32_BIG: | |
d491d34e ILT |
2878 | this->sized_write_section_symbol<32, true>(os, symtab_xindex, of, |
2879 | offset); | |
8851ecca | 2880 | break; |
9025d29d | 2881 | #endif |
9025d29d | 2882 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca | 2883 | case Parameters::TARGET_64_LITTLE: |
d491d34e ILT |
2884 | this->sized_write_section_symbol<64, false>(os, symtab_xindex, of, |
2885 | offset); | |
8851ecca | 2886 | break; |
9025d29d | 2887 | #endif |
8851ecca ILT |
2888 | #ifdef HAVE_TARGET_64_BIG |
2889 | case Parameters::TARGET_64_BIG: | |
d491d34e ILT |
2890 | this->sized_write_section_symbol<64, true>(os, symtab_xindex, of, |
2891 | offset); | |
8851ecca ILT |
2892 | break; |
2893 | #endif | |
2894 | default: | |
2895 | gold_unreachable(); | |
a3ad94ed | 2896 | } |
a3ad94ed ILT |
2897 | } |
2898 | ||
2899 | // Write out a section symbol, specialized for size and endianness. | |
2900 | ||
2901 | template<int size, bool big_endian> | |
2902 | void | |
2903 | Symbol_table::sized_write_section_symbol(const Output_section* os, | |
d491d34e | 2904 | Output_symtab_xindex* symtab_xindex, |
a3ad94ed ILT |
2905 | Output_file* of, |
2906 | off_t offset) const | |
2907 | { | |
2908 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; | |
2909 | ||
2910 | unsigned char* pov = of->get_output_view(offset, sym_size); | |
2911 | ||
2912 | elfcpp::Sym_write<size, big_endian> osym(pov); | |
2913 | osym.put_st_name(0); | |
b4ecf66b ILT |
2914 | if (parameters->options().relocatable()) |
2915 | osym.put_st_value(0); | |
2916 | else | |
2917 | osym.put_st_value(os->address()); | |
a3ad94ed ILT |
2918 | osym.put_st_size(0); |
2919 | osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, | |
2920 | elfcpp::STT_SECTION)); | |
2921 | osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0)); | |
d491d34e ILT |
2922 | |
2923 | unsigned int shndx = os->out_shndx(); | |
2924 | if (shndx >= elfcpp::SHN_LORESERVE) | |
2925 | { | |
2926 | symtab_xindex->add(os->symtab_index(), shndx); | |
2927 | shndx = elfcpp::SHN_XINDEX; | |
2928 | } | |
2929 | osym.put_st_shndx(shndx); | |
a3ad94ed ILT |
2930 | |
2931 | of->write_output_view(offset, sym_size, pov); | |
2932 | } | |
2933 | ||
abaa3995 ILT |
2934 | // Print statistical information to stderr. This is used for --stats. |
2935 | ||
2936 | void | |
2937 | Symbol_table::print_stats() const | |
2938 | { | |
2939 | #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP) | |
2940 | fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"), | |
2941 | program_name, this->table_.size(), this->table_.bucket_count()); | |
2942 | #else | |
2943 | fprintf(stderr, _("%s: symbol table entries: %zu\n"), | |
2944 | program_name, this->table_.size()); | |
2945 | #endif | |
ad8f37d1 | 2946 | this->namepool_.print_stats("symbol table stringpool"); |
abaa3995 ILT |
2947 | } |
2948 | ||
ff541f30 ILT |
2949 | // We check for ODR violations by looking for symbols with the same |
2950 | // name for which the debugging information reports that they were | |
2951 | // defined in different source locations. When comparing the source | |
2952 | // location, we consider instances with the same base filename and | |
2953 | // line number to be the same. This is because different object | |
2954 | // files/shared libraries can include the same header file using | |
2955 | // different paths, and we don't want to report an ODR violation in | |
2956 | // that case. | |
2957 | ||
2958 | // This struct is used to compare line information, as returned by | |
7bf1f802 | 2959 | // Dwarf_line_info::one_addr2line. It implements a < comparison |
ff541f30 ILT |
2960 | // operator used with std::set. |
2961 | ||
2962 | struct Odr_violation_compare | |
2963 | { | |
2964 | bool | |
2965 | operator()(const std::string& s1, const std::string& s2) const | |
2966 | { | |
2967 | std::string::size_type pos1 = s1.rfind('/'); | |
2968 | std::string::size_type pos2 = s2.rfind('/'); | |
2969 | if (pos1 == std::string::npos | |
2970 | || pos2 == std::string::npos) | |
2971 | return s1 < s2; | |
2972 | return s1.compare(pos1, std::string::npos, | |
2973 | s2, pos2, std::string::npos) < 0; | |
2974 | } | |
2975 | }; | |
2976 | ||
70e654ba ILT |
2977 | // Check candidate_odr_violations_ to find symbols with the same name |
2978 | // but apparently different definitions (different source-file/line-no). | |
2979 | ||
2980 | void | |
17a1d0a9 ILT |
2981 | Symbol_table::detect_odr_violations(const Task* task, |
2982 | const char* output_file_name) const | |
70e654ba ILT |
2983 | { |
2984 | for (Odr_map::const_iterator it = candidate_odr_violations_.begin(); | |
2985 | it != candidate_odr_violations_.end(); | |
2986 | ++it) | |
2987 | { | |
2988 | const char* symbol_name = it->first; | |
2989 | // We use a sorted set so the output is deterministic. | |
ff541f30 | 2990 | std::set<std::string, Odr_violation_compare> line_nums; |
70e654ba | 2991 | |
b01c0a4a ILT |
2992 | for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator |
2993 | locs = it->second.begin(); | |
2994 | locs != it->second.end(); | |
2995 | ++locs) | |
70e654ba ILT |
2996 | { |
2997 | // We need to lock the object in order to read it. This | |
17a1d0a9 ILT |
2998 | // means that we have to run in a singleton Task. If we |
2999 | // want to run this in a general Task for better | |
3000 | // performance, we will need one Task for object, plus | |
3001 | // appropriate locking to ensure that we don't conflict with | |
e4e5049b CS |
3002 | // other uses of the object. Also note, one_addr2line is not |
3003 | // currently thread-safe. | |
17a1d0a9 | 3004 | Task_lock_obj<Object> tl(task, locs->object); |
e4e5049b | 3005 | // 16 is the size of the object-cache that one_addr2line should use. |
a55ce7fe | 3006 | std::string lineno = Dwarf_line_info::one_addr2line( |
e4e5049b | 3007 | locs->object, locs->shndx, locs->offset, 16); |
70e654ba ILT |
3008 | if (!lineno.empty()) |
3009 | line_nums.insert(lineno); | |
3010 | } | |
3011 | ||
3012 | if (line_nums.size() > 1) | |
3013 | { | |
dd8670e5 | 3014 | gold_warning(_("while linking %s: symbol '%s' defined in multiple " |
78f15696 | 3015 | "places (possible ODR violation):"), |
a2b1aa12 | 3016 | output_file_name, demangle(symbol_name).c_str()); |
70e654ba ILT |
3017 | for (std::set<std::string>::const_iterator it2 = line_nums.begin(); |
3018 | it2 != line_nums.end(); | |
3019 | ++it2) | |
3020 | fprintf(stderr, " %s\n", it2->c_str()); | |
3021 | } | |
3022 | } | |
e4e5049b CS |
3023 | // We only call one_addr2line() in this function, so we can clear its cache. |
3024 | Dwarf_line_info::clear_addr2line_cache(); | |
70e654ba ILT |
3025 | } |
3026 | ||
f6ce93d6 ILT |
3027 | // Warnings functions. |
3028 | ||
3029 | // Add a new warning. | |
3030 | ||
3031 | void | |
3032 | Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj, | |
cb295612 | 3033 | const std::string& warning) |
f6ce93d6 ILT |
3034 | { |
3035 | name = symtab->canonicalize_name(name); | |
cb295612 | 3036 | this->warnings_[name].set(obj, warning); |
f6ce93d6 ILT |
3037 | } |
3038 | ||
3039 | // Look through the warnings and mark the symbols for which we should | |
3040 | // warn. This is called during Layout::finalize when we know the | |
3041 | // sources for all the symbols. | |
3042 | ||
3043 | void | |
cb295612 | 3044 | Warnings::note_warnings(Symbol_table* symtab) |
f6ce93d6 ILT |
3045 | { |
3046 | for (Warning_table::iterator p = this->warnings_.begin(); | |
3047 | p != this->warnings_.end(); | |
3048 | ++p) | |
3049 | { | |
3050 | Symbol* sym = symtab->lookup(p->first, NULL); | |
3051 | if (sym != NULL | |
3052 | && sym->source() == Symbol::FROM_OBJECT | |
3053 | && sym->object() == p->second.object) | |
cb295612 | 3054 | sym->set_has_warning(); |
f6ce93d6 ILT |
3055 | } |
3056 | } | |
3057 | ||
3058 | // Issue a warning. This is called when we see a relocation against a | |
3059 | // symbol for which has a warning. | |
3060 | ||
75f2446e | 3061 | template<int size, bool big_endian> |
f6ce93d6 | 3062 | void |
75f2446e ILT |
3063 | Warnings::issue_warning(const Symbol* sym, |
3064 | const Relocate_info<size, big_endian>* relinfo, | |
3065 | size_t relnum, off_t reloffset) const | |
f6ce93d6 | 3066 | { |
a3ad94ed | 3067 | gold_assert(sym->has_warning()); |
f6ce93d6 | 3068 | Warning_table::const_iterator p = this->warnings_.find(sym->name()); |
a3ad94ed | 3069 | gold_assert(p != this->warnings_.end()); |
75f2446e ILT |
3070 | gold_warning_at_location(relinfo, relnum, reloffset, |
3071 | "%s", p->second.text.c_str()); | |
f6ce93d6 ILT |
3072 | } |
3073 | ||
14bfc3f5 ILT |
3074 | // Instantiate the templates we need. We could use the configure |
3075 | // script to restrict this to only the ones needed for implemented | |
3076 | // targets. | |
3077 | ||
c7912668 ILT |
3078 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
3079 | template | |
3080 | void | |
3081 | Sized_symbol<32>::allocate_common(Output_data*, Value_type); | |
3082 | #endif | |
3083 | ||
3084 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
3085 | template | |
3086 | void | |
3087 | Sized_symbol<64>::allocate_common(Output_data*, Value_type); | |
3088 | #endif | |
3089 | ||
193a53d9 | 3090 | #ifdef HAVE_TARGET_32_LITTLE |
14bfc3f5 ILT |
3091 | template |
3092 | void | |
193a53d9 ILT |
3093 | Symbol_table::add_from_relobj<32, false>( |
3094 | Sized_relobj<32, false>* relobj, | |
f6ce93d6 | 3095 | const unsigned char* syms, |
14bfc3f5 | 3096 | size_t count, |
d491d34e | 3097 | size_t symndx_offset, |
14bfc3f5 ILT |
3098 | const char* sym_names, |
3099 | size_t sym_name_size, | |
ae6dce4d | 3100 | Sized_relobj<32, false>::Symbols* sympointers, |
92de84a6 | 3101 | size_t* defined); |
193a53d9 | 3102 | #endif |
14bfc3f5 | 3103 | |
193a53d9 | 3104 | #ifdef HAVE_TARGET_32_BIG |
14bfc3f5 ILT |
3105 | template |
3106 | void | |
193a53d9 ILT |
3107 | Symbol_table::add_from_relobj<32, true>( |
3108 | Sized_relobj<32, true>* relobj, | |
f6ce93d6 | 3109 | const unsigned char* syms, |
14bfc3f5 | 3110 | size_t count, |
d491d34e | 3111 | size_t symndx_offset, |
14bfc3f5 ILT |
3112 | const char* sym_names, |
3113 | size_t sym_name_size, | |
ae6dce4d | 3114 | Sized_relobj<32, true>::Symbols* sympointers, |
92de84a6 | 3115 | size_t* defined); |
193a53d9 | 3116 | #endif |
14bfc3f5 | 3117 | |
193a53d9 | 3118 | #ifdef HAVE_TARGET_64_LITTLE |
14bfc3f5 ILT |
3119 | template |
3120 | void | |
193a53d9 ILT |
3121 | Symbol_table::add_from_relobj<64, false>( |
3122 | Sized_relobj<64, false>* relobj, | |
f6ce93d6 | 3123 | const unsigned char* syms, |
14bfc3f5 | 3124 | size_t count, |
d491d34e | 3125 | size_t symndx_offset, |
14bfc3f5 ILT |
3126 | const char* sym_names, |
3127 | size_t sym_name_size, | |
ae6dce4d | 3128 | Sized_relobj<64, false>::Symbols* sympointers, |
92de84a6 | 3129 | size_t* defined); |
193a53d9 | 3130 | #endif |
14bfc3f5 | 3131 | |
193a53d9 | 3132 | #ifdef HAVE_TARGET_64_BIG |
14bfc3f5 ILT |
3133 | template |
3134 | void | |
193a53d9 ILT |
3135 | Symbol_table::add_from_relobj<64, true>( |
3136 | Sized_relobj<64, true>* relobj, | |
f6ce93d6 | 3137 | const unsigned char* syms, |
14bfc3f5 | 3138 | size_t count, |
d491d34e | 3139 | size_t symndx_offset, |
14bfc3f5 ILT |
3140 | const char* sym_names, |
3141 | size_t sym_name_size, | |
ae6dce4d | 3142 | Sized_relobj<64, true>::Symbols* sympointers, |
92de84a6 | 3143 | size_t* defined); |
193a53d9 | 3144 | #endif |
14bfc3f5 | 3145 | |
89fc3421 CC |
3146 | #ifdef HAVE_TARGET_32_LITTLE |
3147 | template | |
3148 | Symbol* | |
3149 | Symbol_table::add_from_pluginobj<32, false>( | |
3150 | Sized_pluginobj<32, false>* obj, | |
3151 | const char* name, | |
3152 | const char* ver, | |
3153 | elfcpp::Sym<32, false>* sym); | |
3154 | #endif | |
3155 | ||
3156 | #ifdef HAVE_TARGET_32_BIG | |
3157 | template | |
3158 | Symbol* | |
3159 | Symbol_table::add_from_pluginobj<32, true>( | |
3160 | Sized_pluginobj<32, true>* obj, | |
3161 | const char* name, | |
3162 | const char* ver, | |
3163 | elfcpp::Sym<32, true>* sym); | |
3164 | #endif | |
3165 | ||
3166 | #ifdef HAVE_TARGET_64_LITTLE | |
3167 | template | |
3168 | Symbol* | |
3169 | Symbol_table::add_from_pluginobj<64, false>( | |
3170 | Sized_pluginobj<64, false>* obj, | |
3171 | const char* name, | |
3172 | const char* ver, | |
3173 | elfcpp::Sym<64, false>* sym); | |
3174 | #endif | |
3175 | ||
3176 | #ifdef HAVE_TARGET_64_BIG | |
3177 | template | |
3178 | Symbol* | |
3179 | Symbol_table::add_from_pluginobj<64, true>( | |
3180 | Sized_pluginobj<64, true>* obj, | |
3181 | const char* name, | |
3182 | const char* ver, | |
3183 | elfcpp::Sym<64, true>* sym); | |
3184 | #endif | |
3185 | ||
193a53d9 | 3186 | #ifdef HAVE_TARGET_32_LITTLE |
dbe717ef ILT |
3187 | template |
3188 | void | |
193a53d9 ILT |
3189 | Symbol_table::add_from_dynobj<32, false>( |
3190 | Sized_dynobj<32, false>* dynobj, | |
dbe717ef ILT |
3191 | const unsigned char* syms, |
3192 | size_t count, | |
3193 | const char* sym_names, | |
3194 | size_t sym_name_size, | |
3195 | const unsigned char* versym, | |
3196 | size_t versym_size, | |
92de84a6 ILT |
3197 | const std::vector<const char*>* version_map, |
3198 | Sized_relobj<32, false>::Symbols* sympointers, | |
3199 | size_t* defined); | |
193a53d9 | 3200 | #endif |
dbe717ef | 3201 | |
193a53d9 | 3202 | #ifdef HAVE_TARGET_32_BIG |
dbe717ef ILT |
3203 | template |
3204 | void | |
193a53d9 ILT |
3205 | Symbol_table::add_from_dynobj<32, true>( |
3206 | Sized_dynobj<32, true>* dynobj, | |
dbe717ef ILT |
3207 | const unsigned char* syms, |
3208 | size_t count, | |
3209 | const char* sym_names, | |
3210 | size_t sym_name_size, | |
3211 | const unsigned char* versym, | |
3212 | size_t versym_size, | |
92de84a6 ILT |
3213 | const std::vector<const char*>* version_map, |
3214 | Sized_relobj<32, true>::Symbols* sympointers, | |
3215 | size_t* defined); | |
193a53d9 | 3216 | #endif |
dbe717ef | 3217 | |
193a53d9 | 3218 | #ifdef HAVE_TARGET_64_LITTLE |
dbe717ef ILT |
3219 | template |
3220 | void | |
193a53d9 ILT |
3221 | Symbol_table::add_from_dynobj<64, false>( |
3222 | Sized_dynobj<64, false>* dynobj, | |
dbe717ef ILT |
3223 | const unsigned char* syms, |
3224 | size_t count, | |
3225 | const char* sym_names, | |
3226 | size_t sym_name_size, | |
3227 | const unsigned char* versym, | |
3228 | size_t versym_size, | |
92de84a6 ILT |
3229 | const std::vector<const char*>* version_map, |
3230 | Sized_relobj<64, false>::Symbols* sympointers, | |
3231 | size_t* defined); | |
193a53d9 | 3232 | #endif |
dbe717ef | 3233 | |
193a53d9 | 3234 | #ifdef HAVE_TARGET_64_BIG |
dbe717ef ILT |
3235 | template |
3236 | void | |
193a53d9 ILT |
3237 | Symbol_table::add_from_dynobj<64, true>( |
3238 | Sized_dynobj<64, true>* dynobj, | |
dbe717ef ILT |
3239 | const unsigned char* syms, |
3240 | size_t count, | |
3241 | const char* sym_names, | |
3242 | size_t sym_name_size, | |
3243 | const unsigned char* versym, | |
3244 | size_t versym_size, | |
92de84a6 ILT |
3245 | const std::vector<const char*>* version_map, |
3246 | Sized_relobj<64, true>::Symbols* sympointers, | |
3247 | size_t* defined); | |
193a53d9 | 3248 | #endif |
dbe717ef | 3249 | |
46fe1623 ILT |
3250 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
3251 | template | |
3252 | void | |
fe8718a4 | 3253 | Symbol_table::define_with_copy_reloc<32>( |
fe8718a4 ILT |
3254 | Sized_symbol<32>* sym, |
3255 | Output_data* posd, | |
3256 | elfcpp::Elf_types<32>::Elf_Addr value); | |
46fe1623 ILT |
3257 | #endif |
3258 | ||
3259 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
3260 | template | |
3261 | void | |
fe8718a4 | 3262 | Symbol_table::define_with_copy_reloc<64>( |
fe8718a4 ILT |
3263 | Sized_symbol<64>* sym, |
3264 | Output_data* posd, | |
3265 | elfcpp::Elf_types<64>::Elf_Addr value); | |
46fe1623 ILT |
3266 | #endif |
3267 | ||
75f2446e ILT |
3268 | #ifdef HAVE_TARGET_32_LITTLE |
3269 | template | |
3270 | void | |
3271 | Warnings::issue_warning<32, false>(const Symbol* sym, | |
3272 | const Relocate_info<32, false>* relinfo, | |
3273 | size_t relnum, off_t reloffset) const; | |
3274 | #endif | |
3275 | ||
3276 | #ifdef HAVE_TARGET_32_BIG | |
3277 | template | |
3278 | void | |
3279 | Warnings::issue_warning<32, true>(const Symbol* sym, | |
3280 | const Relocate_info<32, true>* relinfo, | |
3281 | size_t relnum, off_t reloffset) const; | |
3282 | #endif | |
3283 | ||
3284 | #ifdef HAVE_TARGET_64_LITTLE | |
3285 | template | |
3286 | void | |
3287 | Warnings::issue_warning<64, false>(const Symbol* sym, | |
3288 | const Relocate_info<64, false>* relinfo, | |
3289 | size_t relnum, off_t reloffset) const; | |
3290 | #endif | |
3291 | ||
3292 | #ifdef HAVE_TARGET_64_BIG | |
3293 | template | |
3294 | void | |
3295 | Warnings::issue_warning<64, true>(const Symbol* sym, | |
3296 | const Relocate_info<64, true>* relinfo, | |
3297 | size_t relnum, off_t reloffset) const; | |
3298 | #endif | |
3299 | ||
14bfc3f5 | 3300 | } // End namespace gold. |