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