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