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