Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // symtab.h -- the gold symbol table -*- C++ -*- |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
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 | ||
bae7f79e ILT |
23 | // Symbol_table |
24 | // The symbol table. | |
25 | ||
bae7f79e ILT |
26 | #include <string> |
27 | #include <utility> | |
ead1e424 | 28 | #include <vector> |
bae7f79e ILT |
29 | |
30 | #include "elfcpp.h" | |
7e1edb90 | 31 | #include "parameters.h" |
14bfc3f5 | 32 | #include "stringpool.h" |
f6ce93d6 | 33 | #include "object.h" |
bae7f79e ILT |
34 | |
35 | #ifndef GOLD_SYMTAB_H | |
36 | #define GOLD_SYMTAB_H | |
37 | ||
38 | namespace gold | |
39 | { | |
40 | ||
14bfc3f5 | 41 | class Object; |
f6ce93d6 | 42 | class Relobj; |
dbe717ef ILT |
43 | template<int size, bool big_endian> |
44 | class Sized_relobj; | |
f6ce93d6 | 45 | class Dynobj; |
dbe717ef ILT |
46 | template<int size, bool big_endian> |
47 | class Sized_dynobj; | |
14b31740 | 48 | class Versions; |
9a2d6984 | 49 | class Input_objects; |
ead1e424 | 50 | class Output_data; |
a3ad94ed | 51 | class Output_section; |
ead1e424 | 52 | class Output_segment; |
61ba1cf9 ILT |
53 | class Output_file; |
54 | class Target; | |
14bfc3f5 | 55 | |
14bfc3f5 ILT |
56 | // The base class of an entry in the symbol table. The symbol table |
57 | // can have a lot of entries, so we don't want this class to big. | |
58 | // Size dependent fields can be found in the template class | |
59 | // Sized_symbol. Targets may support their own derived classes. | |
bae7f79e | 60 | |
bae7f79e ILT |
61 | class Symbol |
62 | { | |
63 | public: | |
ead1e424 ILT |
64 | // Because we want the class to be small, we don't use any virtual |
65 | // functions. But because symbols can be defined in different | |
66 | // places, we need to classify them. This enum is the different | |
67 | // sources of symbols we support. | |
68 | enum Source | |
69 | { | |
f6ce93d6 ILT |
70 | // Symbol defined in a relocatable or dynamic input file--this is |
71 | // the most common case. | |
ead1e424 ILT |
72 | FROM_OBJECT, |
73 | // Symbol defined in an Output_data, a special section created by | |
74 | // the target. | |
75 | IN_OUTPUT_DATA, | |
76 | // Symbol defined in an Output_segment, with no associated | |
77 | // section. | |
78 | IN_OUTPUT_SEGMENT, | |
79 | // Symbol value is constant. | |
80 | CONSTANT | |
81 | }; | |
82 | ||
83 | // When the source is IN_OUTPUT_SEGMENT, we need to describe what | |
84 | // the offset means. | |
85 | enum Segment_offset_base | |
86 | { | |
87 | // From the start of the segment. | |
88 | SEGMENT_START, | |
89 | // From the end of the segment. | |
90 | SEGMENT_END, | |
91 | // From the filesz of the segment--i.e., after the loaded bytes | |
92 | // but before the bytes which are allocated but zeroed. | |
93 | SEGMENT_BSS | |
94 | }; | |
95 | ||
14bfc3f5 ILT |
96 | // Return the symbol name. |
97 | const char* | |
98 | name() const | |
99 | { return this->name_; } | |
100 | ||
101 | // Return the symbol version. This will return NULL for an | |
102 | // unversioned symbol. | |
103 | const char* | |
104 | version() const | |
105 | { return this->version_; } | |
106 | ||
ead1e424 ILT |
107 | // Return the symbol source. |
108 | Source | |
109 | source() const | |
110 | { return this->source_; } | |
111 | ||
14bfc3f5 ILT |
112 | // Return the object with which this symbol is associated. |
113 | Object* | |
114 | object() const | |
ead1e424 | 115 | { |
a3ad94ed | 116 | gold_assert(this->source_ == FROM_OBJECT); |
ead1e424 ILT |
117 | return this->u_.from_object.object; |
118 | } | |
119 | ||
f6ce93d6 ILT |
120 | // Return the index of the section in the input relocatable or |
121 | // dynamic object file. | |
ead1e424 | 122 | unsigned int |
16649710 | 123 | shndx() const |
ead1e424 | 124 | { |
a3ad94ed | 125 | gold_assert(this->source_ == FROM_OBJECT); |
16649710 | 126 | return this->u_.from_object.shndx; |
ead1e424 ILT |
127 | } |
128 | ||
129 | // Return the output data section with which this symbol is | |
130 | // associated, if the symbol was specially defined with respect to | |
131 | // an output data section. | |
132 | Output_data* | |
133 | output_data() const | |
134 | { | |
a3ad94ed | 135 | gold_assert(this->source_ == IN_OUTPUT_DATA); |
ead1e424 ILT |
136 | return this->u_.in_output_data.output_data; |
137 | } | |
138 | ||
139 | // If this symbol was defined with respect to an output data | |
140 | // section, return whether the value is an offset from end. | |
141 | bool | |
142 | offset_is_from_end() const | |
143 | { | |
a3ad94ed | 144 | gold_assert(this->source_ == IN_OUTPUT_DATA); |
ead1e424 ILT |
145 | return this->u_.in_output_data.offset_is_from_end; |
146 | } | |
147 | ||
148 | // Return the output segment with which this symbol is associated, | |
149 | // if the symbol was specially defined with respect to an output | |
150 | // segment. | |
151 | Output_segment* | |
152 | output_segment() const | |
153 | { | |
a3ad94ed | 154 | gold_assert(this->source_ == IN_OUTPUT_SEGMENT); |
ead1e424 ILT |
155 | return this->u_.in_output_segment.output_segment; |
156 | } | |
157 | ||
158 | // If this symbol was defined with respect to an output segment, | |
159 | // return the offset base. | |
160 | Segment_offset_base | |
161 | offset_base() const | |
162 | { | |
a3ad94ed | 163 | gold_assert(this->source_ == IN_OUTPUT_SEGMENT); |
ead1e424 ILT |
164 | return this->u_.in_output_segment.offset_base; |
165 | } | |
14bfc3f5 ILT |
166 | |
167 | // Return the symbol binding. | |
168 | elfcpp::STB | |
169 | binding() const | |
170 | { return this->binding_; } | |
171 | ||
1564db8d ILT |
172 | // Return the symbol type. |
173 | elfcpp::STT | |
174 | type() const | |
175 | { return this->type_; } | |
176 | ||
177 | // Return the symbol visibility. | |
178 | elfcpp::STV | |
179 | visibility() const | |
180 | { return this->visibility_; } | |
181 | ||
182 | // Return the non-visibility part of the st_other field. | |
183 | unsigned char | |
ead1e424 ILT |
184 | nonvis() const |
185 | { return this->nonvis_; } | |
14bfc3f5 | 186 | |
1564db8d ILT |
187 | // Return whether this symbol is a forwarder. This will never be |
188 | // true of a symbol found in the hash table, but may be true of | |
189 | // symbol pointers attached to object files. | |
190 | bool | |
191 | is_forwarder() const | |
192 | { return this->is_forwarder_; } | |
193 | ||
194 | // Mark this symbol as a forwarder. | |
195 | void | |
196 | set_forwarder() | |
197 | { this->is_forwarder_ = true; } | |
198 | ||
aeddab66 ILT |
199 | // Return whether this symbol has an alias in the weak aliases table |
200 | // in Symbol_table. | |
201 | bool | |
202 | has_alias() const | |
203 | { return this->has_alias_; } | |
204 | ||
205 | // Mark this symbol as having an alias. | |
206 | void | |
207 | set_has_alias() | |
208 | { this->has_alias_ = true; } | |
209 | ||
c06b7b0b ILT |
210 | // Return whether this symbol needs an entry in the dynamic symbol |
211 | // table. | |
212 | bool | |
213 | needs_dynsym_entry() const | |
429c1569 ILT |
214 | { |
215 | return (this->needs_dynsym_entry_ | |
216 | || (this->in_reg() && this->in_dyn())); | |
217 | } | |
c06b7b0b ILT |
218 | |
219 | // Mark this symbol as needing an entry in the dynamic symbol table. | |
220 | void | |
221 | set_needs_dynsym_entry() | |
222 | { this->needs_dynsym_entry_ = true; } | |
223 | ||
436ca963 ILT |
224 | // Return whether this symbol should be added to the dynamic symbol |
225 | // table. | |
226 | bool | |
227 | should_add_dynsym_entry() const; | |
228 | ||
008db82e ILT |
229 | // Return whether this symbol has been seen in a regular object. |
230 | bool | |
231 | in_reg() const | |
232 | { return this->in_reg_; } | |
233 | ||
234 | // Mark this symbol as having been seen in a regular object. | |
235 | void | |
236 | set_in_reg() | |
237 | { this->in_reg_ = true; } | |
238 | ||
1ebd95fd ILT |
239 | // Return whether this symbol has been seen in a dynamic object. |
240 | bool | |
241 | in_dyn() const | |
242 | { return this->in_dyn_; } | |
243 | ||
f6ce93d6 | 244 | // Mark this symbol as having been seen in a dynamic object. |
1564db8d ILT |
245 | void |
246 | set_in_dyn() | |
247 | { this->in_dyn_ = true; } | |
248 | ||
c06b7b0b ILT |
249 | // Return the index of this symbol in the output file symbol table. |
250 | // A value of -1U means that this symbol is not going into the | |
251 | // output file. This starts out as zero, and is set to a non-zero | |
252 | // value by Symbol_table::finalize. It is an error to ask for the | |
253 | // symbol table index before it has been set. | |
254 | unsigned int | |
255 | symtab_index() const | |
256 | { | |
a3ad94ed | 257 | gold_assert(this->symtab_index_ != 0); |
c06b7b0b ILT |
258 | return this->symtab_index_; |
259 | } | |
260 | ||
261 | // Set the index of the symbol in the output file symbol table. | |
262 | void | |
263 | set_symtab_index(unsigned int index) | |
264 | { | |
a3ad94ed | 265 | gold_assert(index != 0); |
c06b7b0b ILT |
266 | this->symtab_index_ = index; |
267 | } | |
268 | ||
a3ad94ed ILT |
269 | // Return whether this symbol already has an index in the output |
270 | // file symbol table. | |
271 | bool | |
272 | has_symtab_index() const | |
273 | { return this->symtab_index_ != 0; } | |
274 | ||
c06b7b0b ILT |
275 | // Return the index of this symbol in the dynamic symbol table. A |
276 | // value of -1U means that this symbol is not going into the dynamic | |
277 | // symbol table. This starts out as zero, and is set to a non-zero | |
278 | // during Layout::finalize. It is an error to ask for the dynamic | |
279 | // symbol table index before it has been set. | |
280 | unsigned int | |
281 | dynsym_index() const | |
282 | { | |
a3ad94ed | 283 | gold_assert(this->dynsym_index_ != 0); |
c06b7b0b ILT |
284 | return this->dynsym_index_; |
285 | } | |
286 | ||
287 | // Set the index of the symbol in the dynamic symbol table. | |
288 | void | |
289 | set_dynsym_index(unsigned int index) | |
290 | { | |
a3ad94ed | 291 | gold_assert(index != 0); |
c06b7b0b ILT |
292 | this->dynsym_index_ = index; |
293 | } | |
294 | ||
16649710 ILT |
295 | // Return whether this symbol already has an index in the dynamic |
296 | // symbol table. | |
297 | bool | |
298 | has_dynsym_index() const | |
299 | { return this->dynsym_index_ != 0; } | |
300 | ||
ead1e424 | 301 | // Return whether this symbol has an entry in the GOT section. |
92e059d8 | 302 | bool |
ead1e424 ILT |
303 | has_got_offset() const |
304 | { return this->has_got_offset_; } | |
305 | ||
306 | // Return the offset into the GOT section of this symbol. | |
307 | unsigned int | |
308 | got_offset() const | |
309 | { | |
a3ad94ed | 310 | gold_assert(this->has_got_offset()); |
ead1e424 ILT |
311 | return this->got_offset_; |
312 | } | |
313 | ||
314 | // Set the GOT offset of this symbol. | |
315 | void | |
316 | set_got_offset(unsigned int got_offset) | |
317 | { | |
318 | this->has_got_offset_ = true; | |
319 | this->got_offset_ = got_offset; | |
320 | } | |
321 | ||
a3ad94ed | 322 | // Return whether this symbol has an entry in the PLT section. |
ead1e424 | 323 | bool |
a3ad94ed ILT |
324 | has_plt_offset() const |
325 | { return this->has_plt_offset_; } | |
326 | ||
327 | // Return the offset into the PLT section of this symbol. | |
328 | unsigned int | |
329 | plt_offset() const | |
330 | { | |
331 | gold_assert(this->has_plt_offset()); | |
332 | return this->plt_offset_; | |
333 | } | |
334 | ||
335 | // Set the PLT offset of this symbol. | |
336 | void | |
337 | set_plt_offset(unsigned int plt_offset) | |
338 | { | |
339 | this->has_plt_offset_ = true; | |
340 | this->plt_offset_ = plt_offset; | |
341 | } | |
342 | ||
ab5c9e90 ILT |
343 | // Return whether this dynamic symbol needs a special value in the |
344 | // dynamic symbol table. | |
345 | bool | |
346 | needs_dynsym_value() const | |
347 | { return this->needs_dynsym_value_; } | |
348 | ||
349 | // Set that this dynamic symbol needs a special value in the dynamic | |
350 | // symbol table. | |
351 | void | |
352 | set_needs_dynsym_value() | |
353 | { | |
354 | gold_assert(this->object()->is_dynamic()); | |
355 | this->needs_dynsym_value_ = true; | |
356 | } | |
357 | ||
a3ad94ed ILT |
358 | // Return true if the final value of this symbol is known at link |
359 | // time. | |
360 | bool | |
b3b74ddc | 361 | final_value_is_known() const; |
ead1e424 | 362 | |
f6ce93d6 ILT |
363 | // Return whether this is a defined symbol (not undefined or |
364 | // common). | |
365 | bool | |
366 | is_defined() const | |
367 | { | |
368 | return (this->source_ != FROM_OBJECT | |
16649710 ILT |
369 | || (this->shndx() != elfcpp::SHN_UNDEF |
370 | && this->shndx() != elfcpp::SHN_COMMON)); | |
a3ad94ed ILT |
371 | } |
372 | ||
14b31740 | 373 | // Return true if this symbol is from a dynamic object. |
a3ad94ed | 374 | bool |
14b31740 | 375 | is_from_dynobj() const |
a3ad94ed | 376 | { |
14b31740 | 377 | return this->source_ == FROM_OBJECT && this->object()->is_dynamic(); |
f6ce93d6 ILT |
378 | } |
379 | ||
ead1e424 ILT |
380 | // Return whether this is an undefined symbol. |
381 | bool | |
382 | is_undefined() const | |
383 | { | |
16649710 | 384 | return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_UNDEF; |
ead1e424 ILT |
385 | } |
386 | ||
387 | // Return whether this is a common symbol. | |
388 | bool | |
389 | is_common() const | |
390 | { | |
f6ce93d6 | 391 | return (this->source_ == FROM_OBJECT |
16649710 | 392 | && (this->shndx() == elfcpp::SHN_COMMON |
f6ce93d6 | 393 | || this->type_ == elfcpp::STT_COMMON)); |
ead1e424 | 394 | } |
92e059d8 | 395 | |
a6badf5a ILT |
396 | // Return whether this symbol can be seen outside this object. |
397 | bool | |
398 | is_externally_visible() const | |
399 | { | |
400 | return (this->visibility_ == elfcpp::STV_DEFAULT | |
401 | || this->visibility_ == elfcpp::STV_PROTECTED); | |
402 | } | |
403 | ||
436ca963 ILT |
404 | // Return true if this symbol can be preempted by a definition in |
405 | // another link unit. | |
406 | bool | |
407 | is_preemptible() const | |
408 | { | |
386c048c ILT |
409 | // It doesn't make sense to ask whether a symbol defined in |
410 | // another object is preemptible. | |
411 | gold_assert(!this->is_from_dynobj()); | |
412 | ||
436ca963 ILT |
413 | return (this->visibility_ != elfcpp::STV_INTERNAL |
414 | && this->visibility_ != elfcpp::STV_HIDDEN | |
51b08ebe | 415 | && this->visibility_ != elfcpp::STV_PROTECTED |
d61c6bd4 | 416 | && parameters->output_is_shared() |
51b08ebe | 417 | && !parameters->symbolic()); |
436ca963 ILT |
418 | } |
419 | ||
d61c6bd4 ILT |
420 | // Return true if this symbol is a function that needs a PLT entry. |
421 | // If the symbol is defined in a dynamic object or if it is subject | |
422 | // to pre-emption, we need to make a PLT entry. | |
423 | bool | |
424 | needs_plt_entry() const | |
425 | { | |
426 | return (this->type() == elfcpp::STT_FUNC | |
427 | && (this->is_from_dynobj() || this->is_preemptible())); | |
428 | } | |
429 | ||
430 | // Given a direct absolute or pc-relative static relocation against | |
431 | // the global symbol, this function returns whether a dynamic relocation | |
432 | // is needed. | |
433 | ||
434 | bool | |
435 | needs_dynamic_reloc(bool is_absolute_ref, bool is_function_call) const | |
436 | { | |
437 | // An absolute reference within a position-independent output file | |
438 | // will need a dynamic relocaion. | |
439 | if (is_absolute_ref && parameters->output_is_position_independent()) | |
440 | return true; | |
441 | ||
442 | // A function call that can branch to a local PLT entry does not need | |
443 | // a dynamic relocation. | |
444 | if (is_function_call && this->has_plt_offset()) | |
445 | return false; | |
446 | ||
447 | // A reference to any PLT entry in a non-position-independent executable | |
448 | // does not need a dynamic relocation. | |
449 | if (!parameters->output_is_position_independent() | |
450 | && this->has_plt_offset()) | |
451 | return false; | |
452 | ||
453 | // A reference to a symbol defined in a dynamic object or to a | |
454 | // symbol that is preemptible will need a dynamic relocation. | |
455 | if (this->is_from_dynobj() || this->is_preemptible()) | |
456 | return true; | |
457 | ||
458 | // For all other cases, return FALSE. | |
459 | return false; | |
460 | } | |
461 | ||
462 | // Given a direct absolute static relocation against | |
463 | // the global symbol, where a dynamic relocation is needed, this | |
464 | // function returns whether a relative dynamic relocation can be used. | |
465 | // The caller must determine separately whether the static relocation | |
466 | // is compatible with a relative relocation. | |
467 | ||
468 | bool | |
469 | can_use_relative_reloc(bool is_function_call) const | |
470 | { | |
471 | // A function call that can branch to a local PLT entry can | |
472 | // use a RELATIVE relocation. | |
473 | if (is_function_call && this->has_plt_offset()) | |
474 | return true; | |
475 | ||
476 | // A reference to a symbol defined in a dynamic object or to a | |
477 | // symbol that is preemptible can not use a RELATIVE relocaiton. | |
478 | if (this->is_from_dynobj() || this->is_preemptible()) | |
479 | return false; | |
480 | ||
481 | // For all other cases, return TRUE. | |
482 | return true; | |
483 | } | |
484 | ||
f6ce93d6 ILT |
485 | // Return whether there should be a warning for references to this |
486 | // symbol. | |
487 | bool | |
488 | has_warning() const | |
489 | { return this->has_warning_; } | |
490 | ||
491 | // Mark this symbol as having a warning. | |
492 | void | |
493 | set_has_warning() | |
494 | { this->has_warning_ = true; } | |
495 | ||
46fe1623 ILT |
496 | // Return whether this symbol is defined by a COPY reloc from a |
497 | // dynamic object. | |
498 | bool | |
499 | is_copied_from_dynobj() const | |
500 | { return this->is_copied_from_dynobj_; } | |
501 | ||
502 | // Mark this symbol as defined by a COPY reloc. | |
503 | void | |
504 | set_is_copied_from_dynobj() | |
505 | { this->is_copied_from_dynobj_ = true; } | |
506 | ||
d61c6bd4 ILT |
507 | // Mark this symbol as needing its value written to the GOT even when |
508 | // the value is subject to dynamic relocation (e.g., when the target | |
509 | // uses a RELATIVE relocation for the GOT entry). | |
510 | void | |
511 | set_needs_value_in_got() | |
512 | { this->needs_value_in_got_ = true; } | |
513 | ||
514 | // Return whether this symbol needs its value written to the GOT even | |
515 | // when the value is subject to dynamic relocation. | |
516 | bool | |
517 | needs_value_in_got() const | |
518 | { return this->needs_value_in_got_; } | |
519 | ||
14bfc3f5 ILT |
520 | protected: |
521 | // Instances of this class should always be created at a specific | |
522 | // size. | |
523 | Symbol() | |
f6ce93d6 | 524 | { memset(this, 0, sizeof *this); } |
14bfc3f5 | 525 | |
ead1e424 ILT |
526 | // Initialize the general fields. |
527 | void | |
528 | init_fields(const char* name, const char* version, | |
529 | elfcpp::STT type, elfcpp::STB binding, | |
530 | elfcpp::STV visibility, unsigned char nonvis); | |
531 | ||
14bfc3f5 ILT |
532 | // Initialize fields from an ELF symbol in OBJECT. |
533 | template<int size, bool big_endian> | |
534 | void | |
535 | init_base(const char *name, const char* version, Object* object, | |
536 | const elfcpp::Sym<size, big_endian>&); | |
bae7f79e | 537 | |
ead1e424 ILT |
538 | // Initialize fields for an Output_data. |
539 | void | |
540 | init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB, | |
541 | elfcpp::STV, unsigned char nonvis, bool offset_is_from_end); | |
542 | ||
543 | // Initialize fields for an Output_segment. | |
544 | void | |
545 | init_base(const char* name, Output_segment* os, elfcpp::STT type, | |
546 | elfcpp::STB binding, elfcpp::STV visibility, | |
547 | unsigned char nonvis, Segment_offset_base offset_base); | |
548 | ||
549 | // Initialize fields for a constant. | |
550 | void | |
551 | init_base(const char* name, elfcpp::STT type, elfcpp::STB binding, | |
552 | elfcpp::STV visibility, unsigned char nonvis); | |
553 | ||
1564db8d ILT |
554 | // Override existing symbol. |
555 | template<int size, bool big_endian> | |
556 | void | |
14b31740 ILT |
557 | override_base(const elfcpp::Sym<size, big_endian>&, Object* object, |
558 | const char* version); | |
1564db8d | 559 | |
86f2e683 ILT |
560 | // Override existing symbol with a special symbol. |
561 | void | |
562 | override_base_with_special(const Symbol* from); | |
563 | ||
bae7f79e | 564 | private: |
14bfc3f5 ILT |
565 | Symbol(const Symbol&); |
566 | Symbol& operator=(const Symbol&); | |
567 | ||
568 | // Symbol name (expected to point into a Stringpool). | |
569 | const char* name_; | |
570 | // Symbol version (expected to point into a Stringpool). This may | |
571 | // be NULL. | |
bae7f79e | 572 | const char* version_; |
ead1e424 ILT |
573 | |
574 | union | |
575 | { | |
576 | // This struct is used if SOURCE_ == FROM_OBJECT. | |
577 | struct | |
578 | { | |
579 | // Object in which symbol is defined, or in which it was first | |
580 | // seen. | |
581 | Object* object; | |
582 | // Section number in object_ in which symbol is defined. | |
16649710 | 583 | unsigned int shndx; |
ead1e424 ILT |
584 | } from_object; |
585 | ||
586 | // This struct is used if SOURCE_ == IN_OUTPUT_DATA. | |
587 | struct | |
588 | { | |
589 | // Output_data in which symbol is defined. Before | |
590 | // Layout::finalize the symbol's value is an offset within the | |
591 | // Output_data. | |
592 | Output_data* output_data; | |
593 | // True if the offset is from the end, false if the offset is | |
594 | // from the beginning. | |
595 | bool offset_is_from_end; | |
596 | } in_output_data; | |
597 | ||
598 | // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT. | |
599 | struct | |
600 | { | |
601 | // Output_segment in which the symbol is defined. Before | |
602 | // Layout::finalize the symbol's value is an offset. | |
603 | Output_segment* output_segment; | |
604 | // The base to use for the offset before Layout::finalize. | |
605 | Segment_offset_base offset_base; | |
606 | } in_output_segment; | |
607 | } u_; | |
608 | ||
c06b7b0b ILT |
609 | // The index of this symbol in the output file. If the symbol is |
610 | // not going into the output file, this value is -1U. This field | |
611 | // starts as always holding zero. It is set to a non-zero value by | |
612 | // Symbol_table::finalize. | |
613 | unsigned int symtab_index_; | |
614 | ||
615 | // The index of this symbol in the dynamic symbol table. If the | |
616 | // symbol is not going into the dynamic symbol table, this value is | |
617 | // -1U. This field starts as always holding zero. It is set to a | |
618 | // non-zero value during Layout::finalize. | |
619 | unsigned int dynsym_index_; | |
620 | ||
ead1e424 | 621 | // If this symbol has an entry in the GOT section (has_got_offset_ |
c06b7b0b | 622 | // is true), this is the offset from the start of the GOT section. |
ead1e424 | 623 | unsigned int got_offset_; |
c06b7b0b | 624 | |
a3ad94ed ILT |
625 | // If this symbol has an entry in the PLT section (has_plt_offset_ |
626 | // is true), then this is the offset from the start of the PLT | |
627 | // section. | |
628 | unsigned int plt_offset_; | |
629 | ||
14bfc3f5 | 630 | // Symbol type. |
bae7f79e | 631 | elfcpp::STT type_ : 4; |
14bfc3f5 | 632 | // Symbol binding. |
bae7f79e | 633 | elfcpp::STB binding_ : 4; |
14bfc3f5 ILT |
634 | // Symbol visibility. |
635 | elfcpp::STV visibility_ : 2; | |
636 | // Rest of symbol st_other field. | |
ead1e424 ILT |
637 | unsigned int nonvis_ : 6; |
638 | // The type of symbol. | |
f6ce93d6 | 639 | Source source_ : 3; |
14bfc3f5 ILT |
640 | // True if this symbol always requires special target-specific |
641 | // handling. | |
ead1e424 | 642 | bool is_target_special_ : 1; |
14bfc3f5 | 643 | // True if this is the default version of the symbol. |
1564db8d | 644 | bool is_def_ : 1; |
14bfc3f5 ILT |
645 | // True if this symbol really forwards to another symbol. This is |
646 | // used when we discover after the fact that two different entries | |
647 | // in the hash table really refer to the same symbol. This will | |
648 | // never be set for a symbol found in the hash table, but may be set | |
649 | // for a symbol found in the list of symbols attached to an Object. | |
650 | // It forwards to the symbol found in the forwarders_ map of | |
651 | // Symbol_table. | |
1564db8d | 652 | bool is_forwarder_ : 1; |
aeddab66 ILT |
653 | // True if the symbol has an alias in the weak_aliases table in |
654 | // Symbol_table. | |
655 | bool has_alias_ : 1; | |
c06b7b0b ILT |
656 | // True if this symbol needs to be in the dynamic symbol table. |
657 | bool needs_dynsym_entry_ : 1; | |
008db82e ILT |
658 | // True if we've seen this symbol in a regular object. |
659 | bool in_reg_ : 1; | |
1564db8d ILT |
660 | // True if we've seen this symbol in a dynamic object. |
661 | bool in_dyn_ : 1; | |
ead1e424 ILT |
662 | // True if the symbol has an entry in the GOT section. |
663 | bool has_got_offset_ : 1; | |
a3ad94ed ILT |
664 | // True if the symbol has an entry in the PLT section. |
665 | bool has_plt_offset_ : 1; | |
ab5c9e90 ILT |
666 | // True if this is a dynamic symbol which needs a special value in |
667 | // the dynamic symbol table. | |
668 | bool needs_dynsym_value_ : 1; | |
f6ce93d6 ILT |
669 | // True if there is a warning for this symbol. |
670 | bool has_warning_ : 1; | |
46fe1623 ILT |
671 | // True if we are using a COPY reloc for this symbol, so that the |
672 | // real definition lives in a dynamic object. | |
673 | bool is_copied_from_dynobj_ : 1; | |
d61c6bd4 ILT |
674 | // True if the static value should be written to the GOT even |
675 | // when the final value is subject to dynamic relocation. | |
676 | bool needs_value_in_got_ : 1; | |
bae7f79e ILT |
677 | }; |
678 | ||
14bfc3f5 ILT |
679 | // The parts of a symbol which are size specific. Using a template |
680 | // derived class like this helps us use less space on a 32-bit system. | |
bae7f79e ILT |
681 | |
682 | template<int size> | |
14bfc3f5 ILT |
683 | class Sized_symbol : public Symbol |
684 | { | |
685 | public: | |
1564db8d ILT |
686 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type; |
687 | typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type; | |
688 | ||
14bfc3f5 ILT |
689 | Sized_symbol() |
690 | { } | |
691 | ||
692 | // Initialize fields from an ELF symbol in OBJECT. | |
693 | template<bool big_endian> | |
694 | void | |
695 | init(const char *name, const char* version, Object* object, | |
696 | const elfcpp::Sym<size, big_endian>&); | |
697 | ||
ead1e424 ILT |
698 | // Initialize fields for an Output_data. |
699 | void | |
700 | init(const char* name, Output_data*, Value_type value, Size_type symsize, | |
701 | elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis, | |
702 | bool offset_is_from_end); | |
703 | ||
704 | // Initialize fields for an Output_segment. | |
705 | void | |
706 | init(const char* name, Output_segment*, Value_type value, Size_type symsize, | |
707 | elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis, | |
708 | Segment_offset_base offset_base); | |
709 | ||
710 | // Initialize fields for a constant. | |
711 | void | |
712 | init(const char* name, Value_type value, Size_type symsize, | |
713 | elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis); | |
714 | ||
1564db8d ILT |
715 | // Override existing symbol. |
716 | template<bool big_endian> | |
717 | void | |
14b31740 ILT |
718 | override(const elfcpp::Sym<size, big_endian>&, Object* object, |
719 | const char* version); | |
1564db8d | 720 | |
86f2e683 ILT |
721 | // Override existing symbol with a special symbol. |
722 | void | |
723 | override_with_special(const Sized_symbol<size>*); | |
724 | ||
1564db8d ILT |
725 | // Return the symbol's value. |
726 | Value_type | |
727 | value() const | |
728 | { return this->value_; } | |
729 | ||
730 | // Return the symbol's size (we can't call this 'size' because that | |
731 | // is a template parameter). | |
732 | Size_type | |
733 | symsize() const | |
ead1e424 ILT |
734 | { return this->symsize_; } |
735 | ||
736 | // Set the symbol size. This is used when resolving common symbols. | |
737 | void | |
738 | set_symsize(Size_type symsize) | |
739 | { this->symsize_ = symsize; } | |
1564db8d | 740 | |
75f65a3e ILT |
741 | // Set the symbol value. This is called when we store the final |
742 | // values of the symbols into the symbol table. | |
743 | void | |
744 | set_value(Value_type value) | |
745 | { this->value_ = value; } | |
746 | ||
14bfc3f5 ILT |
747 | private: |
748 | Sized_symbol(const Sized_symbol&); | |
749 | Sized_symbol& operator=(const Sized_symbol&); | |
750 | ||
ead1e424 ILT |
751 | // Symbol value. Before Layout::finalize this is the offset in the |
752 | // input section. This is set to the final value during | |
753 | // Layout::finalize. | |
1564db8d | 754 | Value_type value_; |
14bfc3f5 | 755 | // Symbol size. |
ead1e424 ILT |
756 | Size_type symsize_; |
757 | }; | |
758 | ||
759 | // A struct describing a symbol defined by the linker, where the value | |
760 | // of the symbol is defined based on an output section. This is used | |
761 | // for symbols defined by the linker, like "_init_array_start". | |
762 | ||
763 | struct Define_symbol_in_section | |
764 | { | |
765 | // The symbol name. | |
766 | const char* name; | |
767 | // The name of the output section with which this symbol should be | |
768 | // associated. If there is no output section with that name, the | |
769 | // symbol will be defined as zero. | |
770 | const char* output_section; | |
771 | // The offset of the symbol within the output section. This is an | |
772 | // offset from the start of the output section, unless start_at_end | |
773 | // is true, in which case this is an offset from the end of the | |
774 | // output section. | |
775 | uint64_t value; | |
776 | // The size of the symbol. | |
777 | uint64_t size; | |
778 | // The symbol type. | |
779 | elfcpp::STT type; | |
780 | // The symbol binding. | |
781 | elfcpp::STB binding; | |
782 | // The symbol visibility. | |
783 | elfcpp::STV visibility; | |
784 | // The rest of the st_other field. | |
785 | unsigned char nonvis; | |
786 | // If true, the value field is an offset from the end of the output | |
787 | // section. | |
788 | bool offset_is_from_end; | |
789 | // If true, this symbol is defined only if we see a reference to it. | |
790 | bool only_if_ref; | |
791 | }; | |
792 | ||
793 | // A struct describing a symbol defined by the linker, where the value | |
794 | // of the symbol is defined based on a segment. This is used for | |
795 | // symbols defined by the linker, like "_end". We describe the | |
796 | // segment with which the symbol should be associated by its | |
797 | // characteristics. If no segment meets these characteristics, the | |
798 | // symbol will be defined as zero. If there is more than one segment | |
799 | // which meets these characteristics, we will use the first one. | |
800 | ||
801 | struct Define_symbol_in_segment | |
802 | { | |
803 | // The symbol name. | |
804 | const char* name; | |
805 | // The segment type where the symbol should be defined, typically | |
806 | // PT_LOAD. | |
807 | elfcpp::PT segment_type; | |
808 | // Bitmask of segment flags which must be set. | |
809 | elfcpp::PF segment_flags_set; | |
810 | // Bitmask of segment flags which must be clear. | |
811 | elfcpp::PF segment_flags_clear; | |
812 | // The offset of the symbol within the segment. The offset is | |
813 | // calculated from the position set by offset_base. | |
814 | uint64_t value; | |
815 | // The size of the symbol. | |
816 | uint64_t size; | |
817 | // The symbol type. | |
818 | elfcpp::STT type; | |
819 | // The symbol binding. | |
820 | elfcpp::STB binding; | |
821 | // The symbol visibility. | |
822 | elfcpp::STV visibility; | |
823 | // The rest of the st_other field. | |
824 | unsigned char nonvis; | |
825 | // The base from which we compute the offset. | |
826 | Symbol::Segment_offset_base offset_base; | |
827 | // If true, this symbol is defined only if we see a reference to it. | |
828 | bool only_if_ref; | |
14bfc3f5 ILT |
829 | }; |
830 | ||
f6ce93d6 ILT |
831 | // This class manages warnings. Warnings are a GNU extension. When |
832 | // we see a section named .gnu.warning.SYM in an object file, and if | |
833 | // we wind using the definition of SYM from that object file, then we | |
834 | // will issue a warning for any relocation against SYM from a | |
835 | // different object file. The text of the warning is the contents of | |
836 | // the section. This is not precisely the definition used by the old | |
837 | // GNU linker; the old GNU linker treated an occurrence of | |
838 | // .gnu.warning.SYM as defining a warning symbol. A warning symbol | |
839 | // would trigger a warning on any reference. However, it was | |
840 | // inconsistent in that a warning in a dynamic object only triggered | |
841 | // if there was no definition in a regular object. This linker is | |
842 | // different in that we only issue a warning if we use the symbol | |
843 | // definition from the same object file as the warning section. | |
844 | ||
845 | class Warnings | |
846 | { | |
847 | public: | |
848 | Warnings() | |
849 | : warnings_() | |
850 | { } | |
851 | ||
852 | // Add a warning for symbol NAME in section SHNDX in object OBJ. | |
853 | void | |
854 | add_warning(Symbol_table* symtab, const char* name, Object* obj, | |
855 | unsigned int shndx); | |
856 | ||
857 | // For each symbol for which we should give a warning, make a note | |
858 | // on the symbol. | |
859 | void | |
860 | note_warnings(Symbol_table* symtab); | |
861 | ||
75f2446e ILT |
862 | // Issue a warning for a reference to SYM at RELINFO's location. |
863 | template<int size, bool big_endian> | |
f6ce93d6 | 864 | void |
75f2446e ILT |
865 | issue_warning(const Symbol* sym, const Relocate_info<size, big_endian>*, |
866 | size_t relnum, off_t reloffset) const; | |
f6ce93d6 ILT |
867 | |
868 | private: | |
869 | Warnings(const Warnings&); | |
870 | Warnings& operator=(const Warnings&); | |
871 | ||
872 | // What we need to know to get the warning text. | |
873 | struct Warning_location | |
874 | { | |
875 | // The object the warning is in. | |
876 | Object* object; | |
877 | // The index of the warning section. | |
878 | unsigned int shndx; | |
879 | // The warning text if we have already loaded it. | |
880 | std::string text; | |
881 | ||
882 | Warning_location() | |
883 | : object(NULL), shndx(0), text() | |
884 | { } | |
885 | ||
886 | void | |
887 | set(Object* o, unsigned int s) | |
888 | { | |
889 | this->object = o; | |
890 | this->shndx = s; | |
891 | } | |
892 | ||
893 | void | |
894 | set_text(const char* t, off_t l) | |
895 | { this->text.assign(t, l); } | |
896 | }; | |
897 | ||
898 | // A mapping from warning symbol names (canonicalized in | |
70e654ba | 899 | // Symbol_table's namepool_ field) to warning information. |
f6ce93d6 ILT |
900 | typedef Unordered_map<const char*, Warning_location> Warning_table; |
901 | ||
902 | Warning_table warnings_; | |
903 | }; | |
904 | ||
14bfc3f5 ILT |
905 | // The main linker symbol table. |
906 | ||
bae7f79e ILT |
907 | class Symbol_table |
908 | { | |
909 | public: | |
910 | Symbol_table(); | |
911 | ||
1564db8d | 912 | ~Symbol_table(); |
bae7f79e | 913 | |
dbe717ef | 914 | // Add COUNT external symbols from the relocatable object RELOBJ to |
f6ce93d6 ILT |
915 | // the symbol table. SYMS is the symbols, SYM_NAMES is their names, |
916 | // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to | |
917 | // point to the symbols in the symbol table. | |
14bfc3f5 ILT |
918 | template<int size, bool big_endian> |
919 | void | |
dbe717ef ILT |
920 | add_from_relobj(Sized_relobj<size, big_endian>* relobj, |
921 | const unsigned char* syms, size_t count, | |
922 | const char* sym_names, size_t sym_name_size, | |
730cdc88 | 923 | typename Sized_relobj<size, big_endian>::Symbols*); |
14bfc3f5 | 924 | |
dbe717ef ILT |
925 | // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the |
926 | // symbol table. SYMS is the symbols. SYM_NAMES is their names. | |
927 | // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are | |
928 | // symbol version data. | |
929 | template<int size, bool big_endian> | |
930 | void | |
931 | add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj, | |
932 | const unsigned char* syms, size_t count, | |
933 | const char* sym_names, size_t sym_name_size, | |
934 | const unsigned char* versym, size_t versym_size, | |
935 | const std::vector<const char*>*); | |
936 | ||
ead1e424 ILT |
937 | // Define a special symbol based on an Output_data. It is a |
938 | // multiple definition error if this symbol is already defined. | |
14b31740 ILT |
939 | Symbol* |
940 | define_in_output_data(const Target*, const char* name, const char* version, | |
941 | Output_data*, uint64_t value, uint64_t symsize, | |
ead1e424 ILT |
942 | elfcpp::STT type, elfcpp::STB binding, |
943 | elfcpp::STV visibility, unsigned char nonvis, | |
944 | bool offset_is_from_end, bool only_if_ref); | |
945 | ||
946 | // Define a special symbol based on an Output_segment. It is a | |
947 | // multiple definition error if this symbol is already defined. | |
14b31740 ILT |
948 | Symbol* |
949 | define_in_output_segment(const Target*, const char* name, | |
950 | const char* version, Output_segment*, | |
ead1e424 ILT |
951 | uint64_t value, uint64_t symsize, |
952 | elfcpp::STT type, elfcpp::STB binding, | |
953 | elfcpp::STV visibility, unsigned char nonvis, | |
954 | Symbol::Segment_offset_base, bool only_if_ref); | |
955 | ||
956 | // Define a special symbol with a constant value. It is a multiple | |
957 | // definition error if this symbol is already defined. | |
14b31740 ILT |
958 | Symbol* |
959 | define_as_constant(const Target*, const char* name, const char* version, | |
960 | uint64_t value, uint64_t symsize, elfcpp::STT type, | |
961 | elfcpp::STB binding, elfcpp::STV visibility, | |
962 | unsigned char nonvis, bool only_if_ref); | |
ead1e424 ILT |
963 | |
964 | // Define a set of symbols in output sections. | |
965 | void | |
14b31740 | 966 | define_symbols(const Layout*, const Target*, int count, |
ead1e424 ILT |
967 | const Define_symbol_in_section*); |
968 | ||
969 | // Define a set of symbols in output segments. | |
970 | void | |
14b31740 | 971 | define_symbols(const Layout*, const Target*, int count, |
70e654ba | 972 | const Define_symbol_in_segment*); |
ead1e424 | 973 | |
46fe1623 ILT |
974 | // Define SYM using a COPY reloc. POSD is the Output_data where the |
975 | // symbol should be defined--typically a .dyn.bss section. VALUE is | |
976 | // the offset within POSD. | |
977 | template<int size> | |
978 | void | |
979 | define_with_copy_reloc(const Target*, Sized_symbol<size>* sym, | |
980 | Output_data* posd, uint64_t value); | |
981 | ||
61ba1cf9 ILT |
982 | // Look up a symbol. |
983 | Symbol* | |
984 | lookup(const char*, const char* version = NULL) const; | |
985 | ||
14bfc3f5 | 986 | // Return the real symbol associated with the forwarder symbol FROM. |
bae7f79e | 987 | Symbol* |
c06b7b0b | 988 | resolve_forwards(const Symbol* from) const; |
bae7f79e | 989 | |
1564db8d ILT |
990 | // Return the sized version of a symbol in this table. |
991 | template<int size> | |
992 | Sized_symbol<size>* | |
5482377d | 993 | get_sized_symbol(Symbol* ACCEPT_SIZE) const; |
1564db8d ILT |
994 | |
995 | template<int size> | |
996 | const Sized_symbol<size>* | |
5482377d | 997 | get_sized_symbol(const Symbol* ACCEPT_SIZE) const; |
54dc6425 | 998 | |
ead1e424 ILT |
999 | // Return the count of undefined symbols seen. |
1000 | int | |
1001 | saw_undefined() const | |
1002 | { return this->saw_undefined_; } | |
1003 | ||
1004 | // Allocate the common symbols | |
1005 | void | |
1006 | allocate_commons(const General_options&, Layout*); | |
1007 | ||
f6ce93d6 ILT |
1008 | // Add a warning for symbol NAME in section SHNDX in object OBJ. |
1009 | void | |
1010 | add_warning(const char* name, Object* obj, unsigned int shndx) | |
1011 | { this->warnings_.add_warning(this, name, obj, shndx); } | |
1012 | ||
1013 | // Canonicalize a symbol name for use in the hash table. | |
1014 | const char* | |
1015 | canonicalize_name(const char* name) | |
cfd73a4e | 1016 | { return this->namepool_.add(name, true, NULL); } |
f6ce93d6 ILT |
1017 | |
1018 | // Possibly issue a warning for a reference to SYM at LOCATION which | |
1019 | // is in OBJ. | |
75f2446e | 1020 | template<int size, bool big_endian> |
f6ce93d6 | 1021 | void |
75f2446e ILT |
1022 | issue_warning(const Symbol* sym, |
1023 | const Relocate_info<size, big_endian>* relinfo, | |
1024 | size_t relnum, off_t reloffset) const | |
1025 | { this->warnings_.issue_warning(sym, relinfo, relnum, reloffset); } | |
f6ce93d6 | 1026 | |
70e654ba ILT |
1027 | // Check candidate_odr_violations_ to find symbols with the same name |
1028 | // but apparently different definitions (different source-file/line-no). | |
1029 | void | |
78f15696 | 1030 | detect_odr_violations(const char* output_file_name) const; |
70e654ba | 1031 | |
46fe1623 ILT |
1032 | // SYM is defined using a COPY reloc. Return the dynamic object |
1033 | // where the original definition was found. | |
1034 | Dynobj* | |
1035 | get_copy_source(const Symbol* sym) const; | |
1036 | ||
a3ad94ed ILT |
1037 | // Set the dynamic symbol indexes. INDEX is the index of the first |
1038 | // global dynamic symbol. Pointers to the symbols are stored into | |
1039 | // the vector. The names are stored into the Stringpool. This | |
1040 | // returns an updated dynamic symbol index. | |
1041 | unsigned int | |
35cdfc9a | 1042 | set_dynsym_indexes(const Target*, unsigned int index, |
14b31740 | 1043 | std::vector<Symbol*>*, Stringpool*, Versions*); |
a3ad94ed | 1044 | |
75f65a3e | 1045 | // Finalize the symbol table after we have set the final addresses |
c06b7b0b ILT |
1046 | // of all the input sections. This sets the final symbol indexes, |
1047 | // values and adds the names to *POOL. INDEX is the index of the | |
16649710 ILT |
1048 | // first global symbol. OFF is the file offset of the global symbol |
1049 | // table, DYNOFF is the offset of the globals in the dynamic symbol | |
1050 | // table, DYN_GLOBAL_INDEX is the index of the first global dynamic | |
1051 | // symbol, and DYNCOUNT is the number of global dynamic symbols. | |
1052 | // This records the parameters, and returns the new file offset. | |
75f65a3e | 1053 | off_t |
16649710 ILT |
1054 | finalize(unsigned int index, off_t off, off_t dynoff, |
1055 | size_t dyn_global_index, size_t dyncount, Stringpool* pool); | |
1564db8d | 1056 | |
61ba1cf9 ILT |
1057 | // Write out the global symbols. |
1058 | void | |
9a2d6984 | 1059 | write_globals(const Input_objects*, const Stringpool*, const Stringpool*, |
16649710 | 1060 | Output_file*) const; |
61ba1cf9 | 1061 | |
a3ad94ed ILT |
1062 | // Write out a section symbol. Return the updated offset. |
1063 | void | |
9025d29d | 1064 | write_section_symbol(const Output_section*, Output_file*, off_t) const; |
a3ad94ed | 1065 | |
bae7f79e ILT |
1066 | private: |
1067 | Symbol_table(const Symbol_table&); | |
1068 | Symbol_table& operator=(const Symbol_table&); | |
1069 | ||
14bfc3f5 ILT |
1070 | // Make FROM a forwarder symbol to TO. |
1071 | void | |
1072 | make_forwarder(Symbol* from, Symbol* to); | |
1073 | ||
1074 | // Add a symbol. | |
1075 | template<int size, bool big_endian> | |
aeddab66 | 1076 | Sized_symbol<size>* |
f0641a0b ILT |
1077 | add_from_object(Object*, const char *name, Stringpool::Key name_key, |
1078 | const char *version, Stringpool::Key version_key, | |
70e654ba ILT |
1079 | bool def, const elfcpp::Sym<size, big_endian>& sym, |
1080 | const elfcpp::Sym<size, big_endian>& orig_sym); | |
14bfc3f5 ILT |
1081 | |
1082 | // Resolve symbols. | |
1083 | template<int size, bool big_endian> | |
aeddab66 | 1084 | void |
1564db8d ILT |
1085 | resolve(Sized_symbol<size>* to, |
1086 | const elfcpp::Sym<size, big_endian>& sym, | |
70e654ba | 1087 | const elfcpp::Sym<size, big_endian>& orig_sym, |
14b31740 | 1088 | Object*, const char* version); |
14bfc3f5 | 1089 | |
1564db8d | 1090 | template<int size, bool big_endian> |
aeddab66 | 1091 | void |
14b31740 ILT |
1092 | resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from, |
1093 | const char* version ACCEPT_SIZE_ENDIAN); | |
1094 | ||
86f2e683 ILT |
1095 | // Whether we should override a symbol, based on flags in |
1096 | // resolve.cc. | |
1097 | static bool | |
d20222a1 | 1098 | should_override(const Symbol*, unsigned int, Object*, bool*); |
86f2e683 | 1099 | |
aeddab66 ILT |
1100 | // Override a symbol. |
1101 | template<int size, bool big_endian> | |
1102 | void | |
1103 | override(Sized_symbol<size>* tosym, | |
1104 | const elfcpp::Sym<size, big_endian>& fromsym, | |
1105 | Object* object, const char* version); | |
1106 | ||
86f2e683 ILT |
1107 | // Whether we should override a symbol with a special symbol which |
1108 | // is automatically defined by the linker. | |
1109 | static bool | |
1110 | should_override_with_special(const Symbol*); | |
1111 | ||
aeddab66 ILT |
1112 | // Override a symbol with a special symbol. |
1113 | template<int size> | |
1114 | void | |
1115 | override_with_special(Sized_symbol<size>* tosym, | |
1116 | const Sized_symbol<size>* fromsym); | |
1117 | ||
1118 | // Record all weak alias sets for a dynamic object. | |
1119 | template<int size> | |
1120 | void | |
1121 | record_weak_aliases(std::vector<Sized_symbol<size>*>*); | |
1122 | ||
14b31740 ILT |
1123 | // Define a special symbol. |
1124 | template<int size, bool big_endian> | |
1125 | Sized_symbol<size>* | |
306d9ef0 ILT |
1126 | define_special_symbol(const Target* target, const char** pname, |
1127 | const char** pversion, bool only_if_ref, | |
86f2e683 | 1128 | Sized_symbol<size>** poldsym ACCEPT_SIZE_ENDIAN); |
14bfc3f5 | 1129 | |
ead1e424 ILT |
1130 | // Define a symbol in an Output_data, sized version. |
1131 | template<int size> | |
14b31740 ILT |
1132 | Sized_symbol<size>* |
1133 | do_define_in_output_data(const Target*, const char* name, | |
1134 | const char* version, Output_data*, | |
ead1e424 ILT |
1135 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
1136 | typename elfcpp::Elf_types<size>::Elf_WXword ssize, | |
1137 | elfcpp::STT type, elfcpp::STB binding, | |
1138 | elfcpp::STV visibility, unsigned char nonvis, | |
1139 | bool offset_is_from_end, bool only_if_ref); | |
1140 | ||
1141 | // Define a symbol in an Output_segment, sized version. | |
1142 | template<int size> | |
14b31740 | 1143 | Sized_symbol<size>* |
ead1e424 | 1144 | do_define_in_output_segment( |
14b31740 | 1145 | const Target*, const char* name, const char* version, Output_segment* os, |
ead1e424 ILT |
1146 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
1147 | typename elfcpp::Elf_types<size>::Elf_WXword ssize, | |
1148 | elfcpp::STT type, elfcpp::STB binding, | |
1149 | elfcpp::STV visibility, unsigned char nonvis, | |
1150 | Symbol::Segment_offset_base offset_base, bool only_if_ref); | |
1151 | ||
1152 | // Define a symbol as a constant, sized version. | |
1153 | template<int size> | |
14b31740 | 1154 | Sized_symbol<size>* |
ead1e424 | 1155 | do_define_as_constant( |
14b31740 | 1156 | const Target*, const char* name, const char* version, |
ead1e424 ILT |
1157 | typename elfcpp::Elf_types<size>::Elf_Addr value, |
1158 | typename elfcpp::Elf_types<size>::Elf_WXword ssize, | |
1159 | elfcpp::STT type, elfcpp::STB binding, | |
1160 | elfcpp::STV visibility, unsigned char nonvis, | |
1161 | bool only_if_ref); | |
1162 | ||
1163 | // Allocate the common symbols, sized version. | |
1164 | template<int size> | |
1165 | void | |
1166 | do_allocate_commons(const General_options&, Layout*); | |
1167 | ||
70e654ba ILT |
1168 | // Implement detect_odr_violations. |
1169 | template<int size, bool big_endian> | |
1170 | void | |
1171 | sized_detect_odr_violations() const; | |
1172 | ||
75f65a3e ILT |
1173 | // Finalize symbols specialized for size. |
1174 | template<int size> | |
1175 | off_t | |
c06b7b0b | 1176 | sized_finalize(unsigned int, off_t, Stringpool*); |
75f65a3e | 1177 | |
61ba1cf9 ILT |
1178 | // Write globals specialized for size and endianness. |
1179 | template<int size, bool big_endian> | |
1180 | void | |
9a2d6984 ILT |
1181 | sized_write_globals(const Input_objects*, const Stringpool*, |
1182 | const Stringpool*, Output_file*) const; | |
16649710 ILT |
1183 | |
1184 | // Write out a symbol to P. | |
1185 | template<int size, bool big_endian> | |
1186 | void | |
ab5c9e90 ILT |
1187 | sized_write_symbol(Sized_symbol<size>*, |
1188 | typename elfcpp::Elf_types<size>::Elf_Addr value, | |
1189 | unsigned int shndx, | |
6a469986 ILT |
1190 | const Stringpool*, unsigned char* p |
1191 | ACCEPT_SIZE_ENDIAN) const; | |
61ba1cf9 | 1192 | |
9a2d6984 ILT |
1193 | // Possibly warn about an undefined symbol from a dynamic object. |
1194 | void | |
1195 | warn_about_undefined_dynobj_symbol(const Input_objects*, Symbol*) const; | |
1196 | ||
a3ad94ed ILT |
1197 | // Write out a section symbol, specialized for size and endianness. |
1198 | template<int size, bool big_endian> | |
1199 | void | |
1200 | sized_write_section_symbol(const Output_section*, Output_file*, off_t) const; | |
1201 | ||
54dc6425 ILT |
1202 | // The type of the symbol hash table. |
1203 | ||
f0641a0b | 1204 | typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key; |
14bfc3f5 ILT |
1205 | |
1206 | struct Symbol_table_hash | |
1207 | { | |
1208 | size_t | |
1209 | operator()(const Symbol_table_key&) const; | |
1210 | }; | |
1211 | ||
1212 | struct Symbol_table_eq | |
1213 | { | |
1214 | bool | |
1215 | operator()(const Symbol_table_key&, const Symbol_table_key&) const; | |
1216 | }; | |
1217 | ||
1218 | typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash, | |
1219 | Symbol_table_eq> Symbol_table_type; | |
1220 | ||
ead1e424 | 1221 | // The type of the list of common symbols. |
ead1e424 ILT |
1222 | typedef std::vector<Symbol*> Commons_type; |
1223 | ||
46fe1623 ILT |
1224 | // A map from symbols with COPY relocs to the dynamic objects where |
1225 | // they are defined. | |
1226 | typedef Unordered_map<const Symbol*, Dynobj*> Copied_symbol_dynobjs; | |
1227 | ||
70e654ba ILT |
1228 | // A map from symbol name (as a pointer into the namepool) to all |
1229 | // the locations the symbols is (weakly) defined (and certain other | |
1230 | // conditions are met). This map will be used later to detect | |
1231 | // possible One Definition Rule (ODR) violations. | |
1232 | struct Symbol_location | |
1233 | { | |
1234 | Object* object; // Object where the symbol is defined. | |
1235 | unsigned int shndx; // Section-in-object where the symbol is defined. | |
1236 | off_t offset; // Offset-in-section where the symbol is defined. | |
1237 | bool operator==(const Symbol_location& that) const | |
1238 | { | |
1239 | return (this->object == that.object | |
1240 | && this->shndx == that.shndx | |
1241 | && this->offset == that.offset); | |
1242 | } | |
1243 | }; | |
1244 | ||
1245 | struct Symbol_location_hash | |
1246 | { | |
1247 | size_t operator()(const Symbol_location& loc) const | |
1248 | { return reinterpret_cast<uintptr_t>(loc.object) ^ loc.offset ^ loc.shndx; } | |
1249 | }; | |
1250 | ||
1251 | typedef Unordered_map<const char*, | |
1252 | Unordered_set<Symbol_location, Symbol_location_hash> > | |
1253 | Odr_map; | |
1254 | ||
ead1e424 ILT |
1255 | // We increment this every time we see a new undefined symbol, for |
1256 | // use in archive groups. | |
1257 | int saw_undefined_; | |
c06b7b0b ILT |
1258 | // The index of the first global symbol in the output file. |
1259 | unsigned int first_global_index_; | |
75f65a3e ILT |
1260 | // The file offset within the output symtab section where we should |
1261 | // write the table. | |
1262 | off_t offset_; | |
61ba1cf9 ILT |
1263 | // The number of global symbols we want to write out. |
1264 | size_t output_count_; | |
16649710 ILT |
1265 | // The file offset of the global dynamic symbols, or 0 if none. |
1266 | off_t dynamic_offset_; | |
16649710 ILT |
1267 | // The index of the first global dynamic symbol. |
1268 | unsigned int first_dynamic_global_index_; | |
16649710 ILT |
1269 | // The number of global dynamic symbols, or 0 if none. |
1270 | off_t dynamic_count_; | |
54dc6425 | 1271 | // The symbol hash table. |
14bfc3f5 | 1272 | Symbol_table_type table_; |
54dc6425 ILT |
1273 | // A pool of symbol names. This is used for all global symbols. |
1274 | // Entries in the hash table point into this pool. | |
14bfc3f5 | 1275 | Stringpool namepool_; |
14bfc3f5 | 1276 | // Forwarding symbols. |
c06b7b0b | 1277 | Unordered_map<const Symbol*, Symbol*> forwarders_; |
aeddab66 ILT |
1278 | // Weak aliases. A symbol in this list points to the next alias. |
1279 | // The aliases point to each other in a circular list. | |
1280 | Unordered_map<Symbol*, Symbol*> weak_aliases_; | |
ead1e424 ILT |
1281 | // We don't expect there to be very many common symbols, so we keep |
1282 | // a list of them. When we find a common symbol we add it to this | |
1283 | // list. It is possible that by the time we process the list the | |
1284 | // symbol is no longer a common symbol. It may also have become a | |
1285 | // forwarder. | |
1286 | Commons_type commons_; | |
f6ce93d6 ILT |
1287 | // Manage symbol warnings. |
1288 | Warnings warnings_; | |
70e654ba ILT |
1289 | // Manage potential One Definition Rule (ODR) violations. |
1290 | Odr_map candidate_odr_violations_; | |
1291 | ||
46fe1623 ILT |
1292 | // When we emit a COPY reloc for a symbol, we define it in an |
1293 | // Output_data. When it's time to emit version information for it, | |
1294 | // we need to know the dynamic object in which we found the original | |
1295 | // definition. This maps symbols with COPY relocs to the dynamic | |
1296 | // object where they were defined. | |
1297 | Copied_symbol_dynobjs copied_symbol_dynobjs_; | |
bae7f79e ILT |
1298 | }; |
1299 | ||
1564db8d ILT |
1300 | // We inline get_sized_symbol for efficiency. |
1301 | ||
1302 | template<int size> | |
1303 | Sized_symbol<size>* | |
5482377d | 1304 | Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const |
1564db8d | 1305 | { |
9025d29d | 1306 | gold_assert(size == parameters->get_size()); |
1564db8d ILT |
1307 | return static_cast<Sized_symbol<size>*>(sym); |
1308 | } | |
1309 | ||
1310 | template<int size> | |
1311 | const Sized_symbol<size>* | |
5482377d | 1312 | Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const |
1564db8d | 1313 | { |
9025d29d | 1314 | gold_assert(size == parameters->get_size()); |
1564db8d ILT |
1315 | return static_cast<const Sized_symbol<size>*>(sym); |
1316 | } | |
1317 | ||
bae7f79e ILT |
1318 | } // End namespace gold. |
1319 | ||
1320 | #endif // !defined(GOLD_SYMTAB_H) |