+ // Return true if this symbol can be preempted by a definition in
+ // another link unit.
+ bool
+ is_preemptible() const
+ {
+ // It doesn't make sense to ask whether a symbol defined in
+ // another object is preemptible.
+ gold_assert(!this->is_from_dynobj());
+
+ // It doesn't make sense to ask whether an undefined symbol
+ // is preemptible.
+ gold_assert(!this->is_undefined());
+
+ // If a symbol does not have default visibility, it can not be
+ // seen outside this link unit and therefore is not preemptible.
+ if (this->visibility_ != elfcpp::STV_DEFAULT)
+ return false;
+
+ // If this symbol has been forced to be a local symbol by a
+ // version script, then it is not visible outside this link unit
+ // and is not preemptible.
+ if (this->is_forced_local_)
+ return false;
+
+ // If we are not producing a shared library, then nothing is
+ // preemptible.
+ if (!parameters->options().shared())
+ return false;
+
+ // If the user used -Bsymbolic, then nothing is preemptible.
+ if (parameters->options().Bsymbolic())
+ return false;
+
+ // If the user used -Bsymbolic-functions, then functions are not
+ // preemptible. We explicitly check for not being STT_OBJECT,
+ // rather than for being STT_FUNC, because that is what the GNU
+ // linker does.
+ if (this->type() != elfcpp::STT_OBJECT
+ && parameters->options().Bsymbolic_functions())
+ return false;
+
+ // Otherwise the symbol is preemptible.
+ return true;
+ }
+
+ // Return true if this symbol is a function that needs a PLT entry.
+ bool
+ needs_plt_entry() const
+ {
+ // An undefined symbol from an executable does not need a PLT entry.
+ if (this->is_undefined() && !parameters->options().shared())
+ return false;
+
+ // An STT_GNU_IFUNC symbol always needs a PLT entry, even when
+ // doing a static link.
+ if (this->type() == elfcpp::STT_GNU_IFUNC)
+ return true;
+
+ // We only need a PLT entry for a function.
+ if (!this->is_func())
+ return false;
+
+ // If we're doing a static link or a -pie link, we don't create
+ // PLT entries.
+ if (parameters->doing_static_link()
+ || parameters->options().pie())
+ return false;
+
+ // We need a PLT entry if the function is defined in a dynamic
+ // object, or is undefined when building a shared object, or if it
+ // is subject to pre-emption.
+ return (this->is_from_dynobj()
+ || this->is_undefined()
+ || this->is_preemptible());
+ }
+
+ // When determining whether a reference to a symbol needs a dynamic
+ // relocation, we need to know several things about the reference.
+ // These flags may be or'ed together.
+ enum Reference_flags
+ {
+ // Reference to the symbol's absolute address.
+ ABSOLUTE_REF = 1,
+ // A non-PIC reference.
+ NON_PIC_REF = 2,
+ // A function call.
+ FUNCTION_CALL = 4
+ };
+
+ // Given a direct absolute or pc-relative static relocation against
+ // the global symbol, this function returns whether a dynamic relocation
+ // is needed.
+
+ bool
+ needs_dynamic_reloc(int flags) const
+ {
+ // No dynamic relocations in a static link!
+ if (parameters->doing_static_link())
+ return false;
+
+ // A reference to an undefined symbol from an executable should be
+ // statically resolved to 0, and does not need a dynamic relocation.
+ // This matches gnu ld behavior.
+ if (this->is_undefined() && !parameters->options().shared())
+ return false;
+
+ // A reference to an absolute symbol does not need a dynamic relocation.
+ if (this->is_absolute())
+ return false;
+
+ // An absolute reference within a position-independent output file
+ // will need a dynamic relocation.
+ if ((flags & ABSOLUTE_REF)
+ && parameters->options().output_is_position_independent())
+ return true;
+
+ // A function call that can branch to a local PLT entry does not need
+ // a dynamic relocation. A non-pic pc-relative function call in a
+ // shared library cannot use a PLT entry.
+ if ((flags & FUNCTION_CALL)
+ && this->has_plt_offset()
+ && !((flags & NON_PIC_REF)
+ && parameters->options().output_is_position_independent()))
+ return false;
+
+ // A reference to any PLT entry in a non-position-independent executable
+ // does not need a dynamic relocation.
+ if (!parameters->options().output_is_position_independent()
+ && this->has_plt_offset())
+ return false;
+
+ // A reference to a symbol defined in a dynamic object or to a
+ // symbol that is preemptible will need a dynamic relocation.
+ if (this->is_from_dynobj()
+ || this->is_undefined()
+ || this->is_preemptible())
+ return true;
+
+ // For all other cases, return FALSE.
+ return false;
+ }
+
+ // Whether we should use the PLT offset associated with a symbol for
+ // a relocation. IS_NON_PIC_REFERENCE is true if this is a non-PIC
+ // reloc--the same set of relocs for which we would pass NON_PIC_REF
+ // to the needs_dynamic_reloc function.
+
+ bool
+ use_plt_offset(bool is_non_pic_reference) const
+ {
+ // If the symbol doesn't have a PLT offset, then naturally we
+ // don't want to use it.
+ if (!this->has_plt_offset())
+ return false;
+
+ // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
+ if (this->type() == elfcpp::STT_GNU_IFUNC)
+ return true;
+
+ // If we are going to generate a dynamic relocation, then we will
+ // wind up using that, so no need to use the PLT entry.
+ if (this->needs_dynamic_reloc(FUNCTION_CALL
+ | (is_non_pic_reference
+ ? NON_PIC_REF
+ : 0)))
+ return false;
+
+ // If the symbol is from a dynamic object, we need to use the PLT
+ // entry.
+ if (this->is_from_dynobj())
+ return true;
+
+ // If we are generating a shared object, and this symbol is
+ // undefined or preemptible, we need to use the PLT entry.
+ if (parameters->options().shared()
+ && (this->is_undefined() || this->is_preemptible()))
+ return true;
+
+ // If this is a weak undefined symbol, we need to use the PLT
+ // entry; the symbol may be defined by a library loaded at
+ // runtime.
+ if (this->is_weak_undefined())
+ return true;
+
+ // Otherwise we can use the regular definition.
+ return false;
+ }
+
+ // Given a direct absolute static relocation against
+ // the global symbol, where a dynamic relocation is needed, this
+ // function returns whether a relative dynamic relocation can be used.
+ // The caller must determine separately whether the static relocation
+ // is compatible with a relative relocation.
+
+ bool
+ can_use_relative_reloc(bool is_function_call) const
+ {
+ // A function call that can branch to a local PLT entry can
+ // use a RELATIVE relocation.
+ if (is_function_call && this->has_plt_offset())
+ return true;
+
+ // A reference to a symbol defined in a dynamic object or to a
+ // symbol that is preemptible can not use a RELATIVE relocaiton.
+ if (this->is_from_dynobj()
+ || this->is_undefined()
+ || this->is_preemptible())
+ return false;
+
+ // For all other cases, return TRUE.
+ return true;
+ }
+
+ // Return the output section where this symbol is defined. Return
+ // NULL if the symbol has an absolute value.
+ Output_section*
+ output_section() const;
+
+ // Set the symbol's output section. This is used for symbols
+ // defined in scripts. This should only be called after the symbol
+ // table has been finalized.
+ void
+ set_output_section(Output_section*);
+