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