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