This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
[gold patch committed] Add STT_GNU_IFUNC support
- From: Ian Lance Taylor <iant at google dot com>
- To: binutils at sourceware dot org
- Date: Thu, 19 Aug 2010 15:53:24 -0700
- Subject: [gold patch committed] Add STT_GNU_IFUNC support
This patch adds support for STT_GNU_IFUNC symbols to gold for the i386
and x86_64 targets. Committed to mainline.
I copied the test cases from glibc, though they are built differently.
I haven't looked at the other targets. It should be possible to add
IFUNC support for them following the general lines of the changes to
i386.cc and x86_64.cc.
Ian
2010-08-19 Ian Lance Taylor <iant@google.com>
PR 10893
* i386.cc (class Output_data_plt_i386): Update declarations.
Define Global_ifunc and Local_ifunc types. Add global_ifuncs_ and
local_ifuncs_ fields.
(Target_i386::do_plt_section_for_global): New function.
(Target_i386::do_plt_section_for_local): New function.
(Output_data_plt_i386::Output_data_plt_i386): Add symtab
parameter; change all callers. Initialize global_ifuncs_ and
local_ifuncs_. If doing a static link define __rel_iplt_start and
__rel_iplt_end.
(Output_data_plt_i386::add_entry): Handle IFUNC symbols.
(Output_data_plt_i386::add_local_ifunc_entry): New function.
(Output_data_plt_i386::do_write): Fix GOT entries for IFUNC
symbols.
(Target_i386::make_plt_section): New function, broken out of
make_plt_entry. Set sh_info field of .rel.plt to point to .plt.
(Target_i386::make_plt_entry): Call make_plt_section.
(Target_i386::make_local_ifunc_plt_entry): New function.
(Target_i386::Scan::reloc_needs_iplt_for_ifunc): New function.
(Target_i386::Scan::local): Handle IFUNC symbols. Add
R_386_IRELATIVE to switch.
(Target_i386::Scan::global): Likewise.
(Target_i386::Relocate::relocate): Likewise.
(Target_i386::Relocatable_size_for_reloc): Add R_386_IRELATIVE to
switch.
* x86_64.cc (class Output_data_plt_x86_64): Update declarations.
(Target_x86_64::do_plt_section_for_global): New function.
(Target_x86_64::do_plt_section_for_local): New function.
(Output_data_plt_x86_64::Output_data_plt_x86_64): Add symtab
parameter; change all callers. If doing a static link define
__rela_iplt_start and __rela_iplt_end.
(Output_data_plt_x86_64::add_entry): Handle IFUNC symbols.
(Output_data_plt_x86_64::add_local_ifunc_entry): New function.
(Target_x86_64::make_plt_section): Set sh_info field of .rel.plt
to point to .plt.
(Target_x86_64::make_local_ifunc_plt_entry): New function.
(Target_x86_64::Scan::check_non_pic): Add R_X86_64_IRELATIVE to
switch.
(Target_x86_64::Scan::reloc_needs_iplt_for_ifunc): New function.
(Target_x86_64::Scan::local): Handle IFUNC symbols. Add
R_X86_64_IRELATIVE to switch.
(Target_x86_64::Scan::global): Likewise.
(Target_x86_64::Relocate::relocate): Likewise.
(Target_x86_64::Relocatable_size_for_reloc): Add R_X86_64_IRELATIVE to
switch.
* target.h (class Target): Add plt_section_for_global and
plt_section_for_local functions. Add do_plt_section_for_global
and do_plt_section_for_local virtual functions.
* symtab.h (Symbol::needs_plt_entry): Handle IFUNC symbol. Add
clarifying comments.
(Symbol::use_plt_offset): Handle IFUNC symbol.
* object.cc (Sized_relobj::Sized_relobj): Initialize
local_plt_offsets_.
(Sized_relobj::local_has_plt_offset): New function.
(Sized_relobj::local_plt_offset): New function.
(Sized_relobj::set_local_plt_offset): New function.
(Sized_relobj::do_count): Handle IFUNC symbol.
* object.h (class Symbol_value): Add is_ifunc_symbol_ field. Take
a bit away from input_shndx_ field. Add set_is_func_symbol and
is_ifunc_symbol functions.
(class Sized_relobj): Update declarations. Remove Tls_got_entry
and Local_tls_got_offsets. Define Local_plt_offsets. Add
local_plt_offsets_ field.
(Sized_relobj::clear_local_symbols): Clear local_plt_offsets_.
* output.h (class Output_section_data): Add non-const
output_section function.
(class Output_data_got): Update declarations.
(class Output_data_got::Got_entry): Add use_plt_offset_ field.
Add use_plt_offset parameter to global and local constructors.
Change all callers. Change local_sym_index_ field to 31 bits.
Change GSYM_CODE and CONSTANT_CODE accordingly.
* output.cc (Output_data_reloc_base::do_adjust_output_section): If
doing a static link don't set sh_link field.
(Output_data_got::Got_entry::write): Use PLT offset if
appropriate.
(Output_data_got::add_global_plt): New function.
(Output_data_got::add_local_plt): New function.
* target-reloc.h (relocate_section): Handle IFUNC symbol.
* defstd.cc (in_section): Remove entries for __rel_iplt_start,
__rel_iplt_end, __rela_iplt_start, and __rela_iplt_end.
* configure.ac: Set IFUNC automake conditional for glibc >= 2.11.
* testsuite/Makefile.am: Add a bunch of IFUNC tests, all within
IFUNC conditional.
* testsuite/ifunc-sel.h: New file.
* testsuite/ifuncmain1.c: New file.
* testsuite/ifuncmain1vis.c: New file.
* testsuite/ifuncmod1.c: New file.
* testsuite/ifuncdep2.c: New file.
* testsuite/ifuncmain2.c: New file.
* testsuite/ifuncmain3.c: New file.
* testsuite/ifuncmod3.c: New file.
* testsuite/ifuncmain4.c: New file.
* testsuite/ifuncmain5.c: New file.
* testsuite/ifuncmod5.c: New file.
* testsuite/ifuncmain6pie.c: New file.
* testsuite/ifuncmod6.c: New file.
* testsuite/ifuncmain7.c: New file.
* configure, testsuite/Makefile.in: Rebuild.
Index: configure.ac
===================================================================
RCS file: /cvs/src/src/gold/configure.ac,v
retrieving revision 1.57
diff -p -u -r1.57 configure.ac
--- configure.ac 28 Jul 2010 21:30:13 -0000 1.57
+++ configure.ac 19 Aug 2010 22:49:20 -0000
@@ -359,6 +359,20 @@ if test "$gold_cv_c_random_seed" = "yes"
fi
AC_SUBST(RANDOM_SEED_CFLAGS)
+dnl On GNU/Linux ifunc is supported by the dynamic linker in glibc
+dnl 2.11 or later.
+AC_CACHE_CHECK([for glibc >= 2.11], [gold_cv_lib_glibc2_11],
+[AC_COMPILE_IFELSE([
+#include <features.h>
+#if !defined __GLIBC__
+error
+#elif __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 11)
+error
+#endif
+], [gold_cv_lib_glibc2_11=yes], [gold_cv_lib_glibc2_11=no])])
+
+AM_CONDITIONAL(IFUNC, test "$gold_cv_lib_glibc2_11" = "yes")
+
AM_BINUTILS_WARNINGS
WARN_CXXFLAGS=`echo ${WARN_CFLAGS} | sed -e 's/-Wstrict-prototypes//' -e 's/-Wmissing-prototypes//' -e 's/-Wshadow//'`
Index: defstd.cc
===================================================================
RCS file: /cvs/src/src/gold/defstd.cc,v
retrieving revision 1.8
diff -p -u -r1.8 defstd.cc
--- defstd.cc 7 Nov 2009 02:02:29 -0000 1.8
+++ defstd.cc 19 Aug 2010 22:49:20 -0000
@@ -1,6 +1,6 @@
// defstd.cc -- define standard symbols for gold.
-// Copyright 2006, 2007 Free Software Foundation, Inc.
+// Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.
// This file is part of gold.
@@ -109,54 +109,6 @@ const Define_symbol_in_section in_sectio
true // only_if_ref
},
{
- "__rel_iplt_start", // name
- ".rel.iplt", // output_section
- 0, // value
- 0, // size
- elfcpp::STT_NOTYPE, // type
- elfcpp::STB_GLOBAL, // binding
- elfcpp::STV_HIDDEN, // visibility
- 0, // nonvis
- false, // offset_is_from_end
- true // only_if_ref
- },
- {
- "__rel_iplt_end", // name
- ".rel.iplt", // output_section
- 0, // value
- 0, // size
- elfcpp::STT_NOTYPE, // type
- elfcpp::STB_GLOBAL, // binding
- elfcpp::STV_HIDDEN, // visibility
- 0, // nonvis
- true, // offset_is_from_end
- true // only_if_ref
- },
- {
- "__rela_iplt_start", // name
- ".rela.iplt", // output_section
- 0, // value
- 0, // size
- elfcpp::STT_NOTYPE, // type
- elfcpp::STB_GLOBAL, // binding
- elfcpp::STV_HIDDEN, // visibility
- 0, // nonvis
- false, // offset_is_from_end
- true // only_if_ref
- },
- {
- "__rela_iplt_end", // name
- ".rela.iplt", // output_section
- 0, // value
- 0, // size
- elfcpp::STT_NOTYPE, // type
- elfcpp::STB_GLOBAL, // binding
- elfcpp::STV_HIDDEN, // visibility
- 0, // nonvis
- true, // offset_is_from_end
- true // only_if_ref
- },
- {
"__stack", // name
".stack", // output_section
0, // value
Index: i386.cc
===================================================================
RCS file: /cvs/src/src/gold/i386.cc,v
retrieving revision 1.122
diff -p -u -r1.122 i386.cc
--- i386.cc 12 Aug 2010 22:14:59 -0000 1.122
+++ i386.cc 19 Aug 2010 22:49:20 -0000
@@ -45,7 +45,113 @@ namespace
using namespace gold;
-class Output_data_plt_i386;
+// A class to handle the PLT data.
+
+class Output_data_plt_i386 : public Output_section_data
+{
+ public:
+ typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
+
+ Output_data_plt_i386(Symbol_table*, Layout*, Output_data_space*);
+
+ // Add an entry to the PLT.
+ void
+ add_entry(Symbol* gsym);
+
+ // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
+ unsigned int
+ add_local_ifunc_entry(Sized_relobj<32, false>* relobj,
+ unsigned int local_sym_index);
+
+ // Return the .rel.plt section data.
+ Reloc_section*
+ rel_plt() const
+ { return this->rel_; }
+
+ // Return where the TLS_DESC relocations should go.
+ Reloc_section*
+ rel_tls_desc(Layout*);
+
+ // Return the number of PLT entries.
+ unsigned int
+ entry_count() const
+ { return this->count_; }
+
+ // Return the offset of the first non-reserved PLT entry.
+ static unsigned int
+ first_plt_entry_offset()
+ { return plt_entry_size; }
+
+ // Return the size of a PLT entry.
+ static unsigned int
+ get_plt_entry_size()
+ { return plt_entry_size; }
+
+ protected:
+ void
+ do_adjust_output_section(Output_section* os);
+
+ // Write to a map file.
+ void
+ do_print_to_mapfile(Mapfile* mapfile) const
+ { mapfile->print_output_data(this, _("** PLT")); }
+
+ private:
+ // The size of an entry in the PLT.
+ static const int plt_entry_size = 16;
+
+ // The first entry in the PLT for an executable.
+ static unsigned char exec_first_plt_entry[plt_entry_size];
+
+ // The first entry in the PLT for a shared object.
+ static unsigned char dyn_first_plt_entry[plt_entry_size];
+
+ // Other entries in the PLT for an executable.
+ static unsigned char exec_plt_entry[plt_entry_size];
+
+ // Other entries in the PLT for a shared object.
+ static unsigned char dyn_plt_entry[plt_entry_size];
+
+ // Set the final size.
+ void
+ set_final_data_size()
+ { this->set_data_size((this->count_ + 1) * plt_entry_size); }
+
+ // Write out the PLT data.
+ void
+ do_write(Output_file*);
+
+ // We keep a list of global STT_GNU_IFUNC symbols, each with its
+ // offset in the GOT.
+ struct Global_ifunc
+ {
+ Symbol* sym;
+ unsigned int got_offset;
+ };
+
+ // We keep a list of local STT_GNU_IFUNC symbols, each with its
+ // offset in the GOT.
+ struct Local_ifunc
+ {
+ Sized_relobj<32, false>* object;
+ unsigned int local_sym_index;
+ unsigned int got_offset;
+ };
+
+ // The reloc section.
+ Reloc_section* rel_;
+ // The TLS_DESC relocations, if necessary. These must follow the
+ // regular PLT relocs.
+ Reloc_section* tls_desc_rel_;
+ // The .got.plt section.
+ Output_data_space* got_plt_;
+ // The number of PLT entries.
+ unsigned int count_;
+ // Global STT_GNU_IFUNC symbols.
+ std::vector<Global_ifunc> global_ifuncs_;
+ // Local STT_GNU_IFUNC symbols.
+ std::vector<Local_ifunc> local_ifuncs_;
+};
// The i386 target class.
// TLS info comes from
@@ -172,6 +278,15 @@ class Target_i386 : public Target_freebs
return Target::do_is_local_label_name(name);
}
+ // Return the PLT section.
+ Output_data*
+ do_plt_section_for_global(const Symbol*) const
+ { return this->plt_section(); }
+
+ Output_data*
+ do_plt_section_for_local(const Relobj*, unsigned int) const
+ { return this->plt_section(); }
+
// Return whether SYM is call to a non-split function.
bool
do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
@@ -255,6 +370,9 @@ class Target_i386 : public Target_freebs
inline bool
possible_function_pointer_reloc(unsigned int r_type);
+ bool
+ reloc_needs_plt_for_ifunc(Sized_relobj<32, false>*, unsigned int r_type);
+
static void
unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
@@ -415,10 +533,20 @@ class Target_i386 : public Target_freebs
return this->got_tlsdesc_;
}
+ // Create the PLT section.
+ void
+ make_plt_section(Symbol_table* symtab, Layout* layout);
+
// Create a PLT entry for a global symbol.
void
make_plt_entry(Symbol_table*, Layout*, Symbol*);
+ // Create a PLT entry for a local STT_GNU_IFUNC symbol.
+ void
+ make_local_ifunc_plt_entry(Symbol_table*, Layout*,
+ Sized_relobj<32, false>* relobj,
+ unsigned int local_sym_index);
+
// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
void
define_tls_base_symbol(Symbol_table*, Layout*);
@@ -586,100 +714,38 @@ Target_i386::rel_dyn_section(Layout* lay
return this->rel_dyn_;
}
-// A class to handle the PLT data.
-
-class Output_data_plt_i386 : public Output_section_data
-{
- public:
- typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
-
- Output_data_plt_i386(Layout*, Output_data_space*);
-
- // Add an entry to the PLT.
- void
- add_entry(Symbol* gsym);
-
- // Return the .rel.plt section data.
- const Reloc_section*
- rel_plt() const
- { return this->rel_; }
-
- // Return where the TLS_DESC relocations should go.
- Reloc_section*
- rel_tls_desc(Layout*);
-
- // Return the number of PLT entries.
- unsigned int
- entry_count() const
- { return this->count_; }
-
- // Return the offset of the first non-reserved PLT entry.
- static unsigned int
- first_plt_entry_offset()
- { return plt_entry_size; }
-
- // Return the size of a PLT entry.
- static unsigned int
- get_plt_entry_size()
- { return plt_entry_size; }
-
- protected:
- void
- do_adjust_output_section(Output_section* os);
-
- // Write to a map file.
- void
- do_print_to_mapfile(Mapfile* mapfile) const
- { mapfile->print_output_data(this, _("** PLT")); }
-
- private:
- // The size of an entry in the PLT.
- static const int plt_entry_size = 16;
-
- // The first entry in the PLT for an executable.
- static unsigned char exec_first_plt_entry[plt_entry_size];
-
- // The first entry in the PLT for a shared object.
- static unsigned char dyn_first_plt_entry[plt_entry_size];
-
- // Other entries in the PLT for an executable.
- static unsigned char exec_plt_entry[plt_entry_size];
-
- // Other entries in the PLT for a shared object.
- static unsigned char dyn_plt_entry[plt_entry_size];
-
- // Set the final size.
- void
- set_final_data_size()
- { this->set_data_size((this->count_ + 1) * plt_entry_size); }
-
- // Write out the PLT data.
- void
- do_write(Output_file*);
-
- // The reloc section.
- Reloc_section* rel_;
- // The TLS_DESC relocations, if necessary. These must follow the
- // regular PLT relocs.
- Reloc_section* tls_desc_rel_;
- // The .got.plt section.
- Output_data_space* got_plt_;
- // The number of PLT entries.
- unsigned int count_;
-};
-
// Create the PLT section. The ordinary .got section is an argument,
// since we need to refer to the start. We also create our own .got
// section just for PLT entries.
-Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
+Output_data_plt_i386::Output_data_plt_i386(Symbol_table* symtab,
+ Layout* layout,
Output_data_space* got_plt)
- : Output_section_data(4), tls_desc_rel_(NULL), got_plt_(got_plt), count_(0)
+ : Output_section_data(4), tls_desc_rel_(NULL), got_plt_(got_plt), count_(0),
+ global_ifuncs_(), local_ifuncs_()
{
this->rel_ = new Reloc_section(false);
layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
elfcpp::SHF_ALLOC, this->rel_,
ORDER_DYNAMIC_PLT_RELOCS, false);
+
+ if (parameters->doing_static_link())
+ {
+ // A statically linked executable will only have a .rel.plt
+ // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
+ // symbols. The library will use these symbols to locate the
+ // IRELATIVE relocs at program startup time.
+ symtab->define_in_output_data("__rel_iplt_start", NULL,
+ Symbol_table::PREDEFINED,
+ this->rel_, 0, 0, elfcpp::STT_NOTYPE,
+ elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
+ 0, false, true);
+ symtab->define_in_output_data("__rel_iplt_end", NULL,
+ Symbol_table::PREDEFINED,
+ this->rel_, 0, 0, elfcpp::STT_NOTYPE,
+ elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
+ 0, true, true);
+ }
}
void
@@ -711,15 +777,58 @@ Output_data_plt_i386::add_entry(Symbol*
this->got_plt_->set_current_data_size(got_offset + 4);
// Every PLT entry needs a reloc.
- gsym->set_needs_dynsym_entry();
- this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
- got_offset);
+ if (gsym->type() == elfcpp::STT_GNU_IFUNC
+ && gsym->can_use_relative_reloc(false))
+ {
+ this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
+ this->got_plt_, got_offset);
+ struct Global_ifunc gi;
+ gi.sym = gsym;
+ gi.got_offset = got_offset;
+ this->global_ifuncs_.push_back(gi);
+ }
+ else
+ {
+ gsym->set_needs_dynsym_entry();
+ this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
+ got_offset);
+ }
// Note that we don't need to save the symbol. The contents of the
// PLT are independent of which symbols are used. The symbols only
// appear in the relocations.
}
+// Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
+// the PLT offset.
+
+unsigned int
+Output_data_plt_i386::add_local_ifunc_entry(Sized_relobj<32, false>* relobj,
+ unsigned int local_sym_index)
+{
+ unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
+ ++this->count_;
+
+ section_offset_type got_offset = this->got_plt_->current_data_size();
+
+ // Every PLT entry needs a GOT entry which points back to the PLT
+ // entry.
+ this->got_plt_->set_current_data_size(got_offset + 4);
+
+ // Every PLT entry needs a reloc.
+ this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
+ elfcpp::R_386_IRELATIVE,
+ this->got_plt_, got_offset);
+
+ struct Local_ifunc li;
+ li.object = relobj;
+ li.local_sym_index = local_sym_index;
+ li.got_offset = got_offset;
+ this->local_ifuncs_.push_back(li);
+
+ return plt_offset;
+}
+
// Return where the TLS_DESC relocations should go, creating it if
// necessary. These follow the JUMP_SLOT relocations.
@@ -858,6 +967,32 @@ Output_data_plt_i386::do_write(Output_fi
elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
}
+ // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
+ // the GOT to point to the actual symbol value, rather than point to
+ // the PLT entry. That will let the dynamic linker call the right
+ // function when resolving IRELATIVE relocations.
+ for (std::vector<Global_ifunc>::const_iterator p =
+ this->global_ifuncs_.begin();
+ p != this->global_ifuncs_.end();
+ ++p)
+ {
+ const Sized_symbol<32>* ssym =
+ static_cast<const Sized_symbol<32>*>(p->sym);
+ elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
+ ssym->value());
+ }
+
+ for (std::vector<Local_ifunc>::const_iterator p =
+ this->local_ifuncs_.begin();
+ p != this->local_ifuncs_.end();
+ ++p)
+ {
+ const Symbol_value<32>* psymval =
+ p->object->local_symbol(p->local_sym_index);
+ elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
+ psymval->value(p->object, 0));
+ }
+
gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
@@ -865,29 +1000,56 @@ Output_data_plt_i386::do_write(Output_fi
of->write_output_view(got_file_offset, got_size, got_view);
}
-// Create a PLT entry for a global symbol.
+// Create the PLT section.
void
-Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
+Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
{
- if (gsym->has_plt_offset())
- return;
-
if (this->plt_ == NULL)
{
// Create the GOT sections first.
this->got_section(symtab, layout);
- this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
+ this->plt_ = new Output_data_plt_i386(symtab, layout, this->got_plt_);
layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
(elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR),
this->plt_, ORDER_PLT, false);
+
+ // Make the sh_info field of .rel.plt point to .plt.
+ Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
+ rel_plt_os->set_info_section(this->plt_->output_section());
}
+}
+// Create a PLT entry for a global symbol.
+
+void
+Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
+{
+ if (gsym->has_plt_offset())
+ return;
+ if (this->plt_ == NULL)
+ this->make_plt_section(symtab, layout);
this->plt_->add_entry(gsym);
}
+// Make a PLT entry for a local STT_GNU_IFUNC symbol.
+
+void
+Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
+ Sized_relobj<32, false>* relobj,
+ unsigned int local_sym_index)
+{
+ if (relobj->local_has_plt_offset(local_sym_index))
+ return;
+ if (this->plt_ == NULL)
+ this->make_plt_section(symtab, layout);
+ unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
+ local_sym_index);
+ relobj->set_local_plt_offset(local_sym_index, plt_offset);
+}
+
// Return the number of entries in the PLT.
unsigned int
@@ -1035,6 +1197,75 @@ Target_i386::Scan::unsupported_reloc_loc
object->name().c_str(), r_type);
}
+// Return whether we need to make a PLT entry for a relocation of a
+// given type against a STT_GNU_IFUNC symbol.
+
+bool
+Target_i386::Scan::reloc_needs_plt_for_ifunc(Sized_relobj<32, false>* object,
+ unsigned int r_type)
+{
+ switch (r_type)
+ {
+ case elfcpp::R_386_NONE:
+ case elfcpp::R_386_GNU_VTINHERIT:
+ case elfcpp::R_386_GNU_VTENTRY:
+ return false;
+
+ case elfcpp::R_386_32:
+ case elfcpp::R_386_16:
+ case elfcpp::R_386_8:
+ case elfcpp::R_386_PC32:
+ case elfcpp::R_386_PC16:
+ case elfcpp::R_386_PC8:
+ case elfcpp::R_386_PLT32:
+ case elfcpp::R_386_GOTOFF:
+ case elfcpp::R_386_GOTPC:
+ case elfcpp::R_386_GOT32:
+ return true;
+
+ case elfcpp::R_386_COPY:
+ case elfcpp::R_386_GLOB_DAT:
+ case elfcpp::R_386_JUMP_SLOT:
+ case elfcpp::R_386_RELATIVE:
+ case elfcpp::R_386_IRELATIVE:
+ case elfcpp::R_386_TLS_TPOFF:
+ case elfcpp::R_386_TLS_DTPMOD32:
+ case elfcpp::R_386_TLS_DTPOFF32:
+ case elfcpp::R_386_TLS_TPOFF32:
+ case elfcpp::R_386_TLS_DESC:
+ // We will give an error later.
+ return false;
+
+ case elfcpp::R_386_TLS_GD:
+ case elfcpp::R_386_TLS_GOTDESC:
+ case elfcpp::R_386_TLS_DESC_CALL:
+ case elfcpp::R_386_TLS_LDM:
+ case elfcpp::R_386_TLS_LDO_32:
+ case elfcpp::R_386_TLS_IE:
+ case elfcpp::R_386_TLS_IE_32:
+ case elfcpp::R_386_TLS_GOTIE:
+ case elfcpp::R_386_TLS_LE:
+ case elfcpp::R_386_TLS_LE_32:
+ gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
+ object->name().c_str(), r_type);
+ return false;
+
+ case elfcpp::R_386_32PLT:
+ case elfcpp::R_386_TLS_GD_32:
+ case elfcpp::R_386_TLS_GD_PUSH:
+ case elfcpp::R_386_TLS_GD_CALL:
+ case elfcpp::R_386_TLS_GD_POP:
+ case elfcpp::R_386_TLS_LDM_32:
+ case elfcpp::R_386_TLS_LDM_PUSH:
+ case elfcpp::R_386_TLS_LDM_CALL:
+ case elfcpp::R_386_TLS_LDM_POP:
+ case elfcpp::R_386_USED_BY_INTEL_200:
+ default:
+ // We will give an error later.
+ return false;
+ }
+}
+
// Scan a relocation for a local symbol.
inline void
@@ -1048,6 +1279,14 @@ Target_i386::Scan::local(Symbol_table* s
unsigned int r_type,
const elfcpp::Sym<32, false>& lsym)
{
+ // A local STT_GNU_IFUNC symbol may require a PLT entry.
+ if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
+ && this->reloc_needs_plt_for_ifunc(object, r_type))
+ {
+ unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
+ target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
+ }
+
switch (r_type)
{
case elfcpp::R_386_NONE:
@@ -1066,9 +1305,9 @@ Target_i386::Scan::local(Symbol_table* s
{
Reloc_section* rel_dyn = target->rel_dyn_section(layout);
unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
- rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
- output_section, data_shndx,
- reloc.get_r_offset());
+ rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
+ output_section, data_shndx,
+ reloc.get_r_offset());
}
break;
@@ -1125,17 +1364,27 @@ Target_i386::Scan::local(Symbol_table* s
// The symbol requires a GOT entry.
Output_data_got<32, false>* got = target->got_section(symtab, layout);
unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
- if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
+
+ // For a STT_GNU_IFUNC symbol we want the PLT offset. That
+ // lets function pointers compare correctly with shared
+ // libraries. Otherwise we would need an IRELATIVE reloc.
+ bool is_new;
+ if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
+ is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
+ else
+ is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
+ if (is_new)
{
// If we are generating a shared object, we need to add a
// dynamic RELATIVE relocation for this symbol's GOT entry.
if (parameters->options().output_is_position_independent())
{
Reloc_section* rel_dyn = target->rel_dyn_section(layout);
- unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
- rel_dyn->add_local_relative(
- object, r_sym, elfcpp::R_386_RELATIVE, got,
- object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
+ unsigned int got_offset =
+ object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
+ rel_dyn->add_local_relative(object, r_sym,
+ elfcpp::R_386_RELATIVE,
+ got, got_offset);
}
}
}
@@ -1147,6 +1396,7 @@ Target_i386::Scan::local(Symbol_table* s
case elfcpp::R_386_GLOB_DAT:
case elfcpp::R_386_JUMP_SLOT:
case elfcpp::R_386_RELATIVE:
+ case elfcpp::R_386_IRELATIVE:
case elfcpp::R_386_TLS_TPOFF:
case elfcpp::R_386_TLS_DTPMOD32:
case elfcpp::R_386_TLS_DTPOFF32:
@@ -1394,6 +1644,11 @@ Target_i386::Scan::global(Symbol_table*
unsigned int r_type,
Symbol* gsym)
{
+ // A STT_GNU_IFUNC symbol may require a PLT entry.
+ if (gsym->type() == elfcpp::STT_GNU_IFUNC
+ && this->reloc_needs_plt_for_ifunc(object, r_type))
+ target->make_plt_entry(symtab, layout, gsym);
+
switch (r_type)
{
case elfcpp::R_386_NONE:
@@ -1424,13 +1679,31 @@ Target_i386::Scan::global(Symbol_table*
target->copy_reloc(symtab, layout, object,
data_shndx, output_section, gsym, reloc);
}
+ else if (r_type == elfcpp::R_386_32
+ && gsym->type() == elfcpp::STT_GNU_IFUNC
+ && gsym->can_use_relative_reloc(false)
+ && !gsym->is_from_dynobj()
+ && !gsym->is_undefined()
+ && !gsym->is_preemptible())
+ {
+ // Use an IRELATIVE reloc for a locally defined
+ // STT_GNU_IFUNC symbol. This makes a function
+ // address in a PIE executable match the address in a
+ // shared library that it links against.
+ Reloc_section* rel_dyn = target->rel_dyn_section(layout);
+ rel_dyn->add_symbolless_global_addend(gsym,
+ elfcpp::R_386_IRELATIVE,
+ output_section,
+ object, data_shndx,
+ reloc.get_r_offset());
+ }
else if (r_type == elfcpp::R_386_32
&& gsym->can_use_relative_reloc(false))
{
Reloc_section* rel_dyn = target->rel_dyn_section(layout);
- rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
- output_section, object,
- data_shndx, reloc.get_r_offset());
+ rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
+ output_section, object,
+ data_shndx, reloc.get_r_offset());
}
else
{
@@ -1485,7 +1758,13 @@ Target_i386::Scan::global(Symbol_table*
// The symbol requires a GOT entry.
Output_data_got<32, false>* got = target->got_section(symtab, layout);
if (gsym->final_value_is_known())
- got->add_global(gsym, GOT_TYPE_STANDARD);
+ {
+ // For a STT_GNU_IFUNC symbol we want the PLT address.
+ if (gsym->type() == elfcpp::STT_GNU_IFUNC)
+ got->add_global_plt(gsym, GOT_TYPE_STANDARD);
+ else
+ got->add_global(gsym, GOT_TYPE_STANDARD);
+ }
else
{
// If this symbol is not fully resolved, we need to add a
@@ -1493,15 +1772,34 @@ Target_i386::Scan::global(Symbol_table*
Reloc_section* rel_dyn = target->rel_dyn_section(layout);
if (gsym->is_from_dynobj()
|| gsym->is_undefined()
- || gsym->is_preemptible())
+ || gsym->is_preemptible()
+ || (gsym->type() == elfcpp::STT_GNU_IFUNC
+ && parameters->options().output_is_position_independent()))
got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
rel_dyn, elfcpp::R_386_GLOB_DAT);
else
{
- if (got->add_global(gsym, GOT_TYPE_STANDARD))
- rel_dyn->add_global_relative(
- gsym, elfcpp::R_386_RELATIVE, got,
- gsym->got_offset(GOT_TYPE_STANDARD));
+ // For a STT_GNU_IFUNC symbol we want to write the PLT
+ // offset into the GOT, so that function pointer
+ // comparisons work correctly.
+ bool is_new;
+ if (gsym->type() != elfcpp::STT_GNU_IFUNC)
+ is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
+ else
+ {
+ is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
+ // Tell the dynamic linker to use the PLT address
+ // when resolving relocations.
+ if (gsym->is_from_dynobj()
+ && !parameters->options().shared())
+ gsym->set_needs_dynsym_value();
+ }
+ if (is_new)
+ {
+ unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
+ rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
+ got, got_off);
+ }
}
}
}
@@ -1534,6 +1832,7 @@ Target_i386::Scan::global(Symbol_table*
case elfcpp::R_386_GLOB_DAT:
case elfcpp::R_386_JUMP_SLOT:
case elfcpp::R_386_RELATIVE:
+ case elfcpp::R_386_IRELATIVE:
case elfcpp::R_386_TLS_TPOFF:
case elfcpp::R_386_TLS_DTPMOD32:
case elfcpp::R_386_TLS_DTPOFF32:
@@ -1866,19 +2165,41 @@ Target_i386::Relocate::relocate(const Re
}
}
+ const Sized_relobj<32, false>* object = relinfo->object;
+
// Pick the value to use for symbols defined in shared objects.
Symbol_value<32> symval;
if (gsym != NULL
- && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
- || r_type == elfcpp::R_386_PC16
- || r_type == elfcpp::R_386_PC32))
+ && gsym->type() == elfcpp::STT_GNU_IFUNC
+ && r_type == elfcpp::R_386_32
+ && gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF)
+ && gsym->can_use_relative_reloc(false)
+ && !gsym->is_from_dynobj()
+ && !gsym->is_undefined()
+ && !gsym->is_preemptible())
+ {
+ // In this case we are generating a R_386_IRELATIVE reloc. We
+ // want to use the real value of the symbol, not the PLT offset.
+ }
+ else if (gsym != NULL
+ && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
+ || r_type == elfcpp::R_386_PC16
+ || r_type == elfcpp::R_386_PC32))
{
symval.set_output_value(target->plt_section()->address()
+ gsym->plt_offset());
psymval = &symval;
}
-
- const Sized_relobj<32, false>* object = relinfo->object;
+ else if (gsym == NULL && psymval->is_ifunc_symbol())
+ {
+ unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
+ if (object->local_has_plt_offset(r_sym))
+ {
+ symval.set_output_value(target->plt_section()->address()
+ + object->local_plt_offset(r_sym));
+ psymval = &symval;
+ }
+ }
// Get the GOT offset if needed.
// The GOT pointer points to the end of the GOT section.
@@ -2001,6 +2322,7 @@ Target_i386::Relocate::relocate(const Re
case elfcpp::R_386_GLOB_DAT:
case elfcpp::R_386_JUMP_SLOT:
case elfcpp::R_386_RELATIVE:
+ case elfcpp::R_386_IRELATIVE:
// These are outstanding tls relocs, which are unexpected when
// linking.
case elfcpp::R_386_TLS_TPOFF:
@@ -2713,6 +3035,7 @@ Target_i386::Relocatable_size_for_reloc:
case elfcpp::R_386_GLOB_DAT:
case elfcpp::R_386_JUMP_SLOT:
case elfcpp::R_386_RELATIVE:
+ case elfcpp::R_386_IRELATIVE:
case elfcpp::R_386_TLS_TPOFF:
case elfcpp::R_386_TLS_DTPMOD32:
case elfcpp::R_386_TLS_DTPOFF32:
Index: object.cc
===================================================================
RCS file: /cvs/src/src/gold/object.cc,v
retrieving revision 1.128
diff -p -u -r1.128 object.cc
--- object.cc 12 Aug 2010 22:01:11 -0000 1.128
+++ object.cc 19 Aug 2010 22:49:21 -0000
@@ -391,6 +391,7 @@ Sized_relobj<size, big_endian>::Sized_re
local_dynsym_offset_(0),
local_values_(),
local_got_offsets_(),
+ local_plt_offsets_(),
kept_comdat_sections_(),
has_eh_frame_(false),
discarded_eh_frame_shndx_(-1U),
@@ -1690,6 +1691,41 @@ Sized_relobj<size, big_endian>::do_shoul
return Archive::SHOULD_INCLUDE_UNKNOWN;
}
+// Return whether the local symbol SYMNDX has a PLT offset.
+
+template<int size, bool big_endian>
+bool
+Sized_relobj<size, big_endian>::local_has_plt_offset(unsigned int symndx) const
+{
+ typename Local_plt_offsets::const_iterator p =
+ this->local_plt_offsets_.find(symndx);
+ return p != this->local_plt_offsets_.end();
+}
+
+// Get the PLT offset of a local symbol.
+
+template<int size, bool big_endian>
+unsigned int
+Sized_relobj<size, big_endian>::local_plt_offset(unsigned int symndx) const
+{
+ typename Local_plt_offsets::const_iterator p =
+ this->local_plt_offsets_.find(symndx);
+ gold_assert(p != this->local_plt_offsets_.end());
+ return p->second;
+}
+
+// Set the PLT offset of a local symbol.
+
+template<int size, bool big_endian>
+void
+Sized_relobj<size, big_endian>::set_local_plt_offset(unsigned int symndx,
+ unsigned int plt_offset)
+{
+ std::pair<typename Local_plt_offsets::iterator, bool> ins =
+ this->local_plt_offsets_.insert(std::make_pair(symndx, plt_offset));
+ gold_assert(ins.second);
+}
+
// First pass over the local symbols. Here we add their names to
// *POOL and *DYNPOOL, and we store the symbol value in
// THIS->LOCAL_VALUES_. This function is always called from a
@@ -1756,6 +1792,8 @@ Sized_relobj<size, big_endian>::do_count
lv.set_is_section_symbol();
else if (sym.get_st_type() == elfcpp::STT_TLS)
lv.set_is_tls_symbol();
+ else if (sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
+ lv.set_is_ifunc_symbol();
// Save the input symbol value for use in do_finalize_local_symbols().
lv.set_input_value(sym.get_st_value());
@@ -2065,7 +2103,6 @@ Sized_relobj<size, big_endian>::do_secti
return shdr.get_sh_entsize();
}
-
// Write out the local symbols.
template<int size, bool big_endian>
Index: object.h
===================================================================
RCS file: /cvs/src/src/gold/object.h,v
retrieving revision 1.100
diff -p -u -r1.100 object.h
--- object.h 12 Aug 2010 22:15:00 -0000 1.100
+++ object.h 19 Aug 2010 22:49:21 -0000
@@ -39,6 +39,7 @@ class General_options;
class Task;
class Cref;
class Layout;
+class Output_data;
class Output_section;
class Output_file;
class Output_symtab_xindex;
@@ -1151,7 +1152,7 @@ class Symbol_value
Symbol_value()
: output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
is_ordinary_shndx_(false), is_section_symbol_(false),
- is_tls_symbol_(false), has_output_value_(true)
+ is_tls_symbol_(false), is_ifunc_symbol_(false), has_output_value_(true)
{ this->u_.value = 0; }
// Get the value of this symbol. OBJECT is the object in which this
@@ -1364,11 +1365,21 @@ class Symbol_value
set_is_tls_symbol()
{ this->is_tls_symbol_ = true; }
- // Return TRUE if this is a TLS symbol.
+ // Return true if this is a TLS symbol.
bool
is_tls_symbol() const
{ return this->is_tls_symbol_; }
+ // Record that this is an IFUNC symbol.
+ void
+ set_is_ifunc_symbol()
+ { this->is_ifunc_symbol_ = true; }
+
+ // Return true if this is an IFUNC symbol.
+ bool
+ is_ifunc_symbol() const
+ { return this->is_ifunc_symbol_; }
+
private:
// The index of this local symbol in the output symbol table. This
// will be 0 if no value has been assigned yet, and the symbol may
@@ -1381,7 +1392,7 @@ class Symbol_value
unsigned int output_dynsym_index_;
// The section index in the input file in which this symbol is
// defined.
- unsigned int input_shndx_ : 28;
+ unsigned int input_shndx_ : 27;
// Whether the section index is an ordinary index, not a special
// value.
bool is_ordinary_shndx_ : 1;
@@ -1389,6 +1400,8 @@ class Symbol_value
bool is_section_symbol_ : 1;
// Whether this is a STT_TLS symbol.
bool is_tls_symbol_ : 1;
+ // Whether this is a STT_GNU_IFUNC symbol.
+ bool is_ifunc_symbol_ : 1;
// Whether this symbol has a value for the output file. This is
// normally set to true during Layout::finalize, by
// finalize_local_symbols. It will be false for a section symbol in
@@ -1692,6 +1705,19 @@ class Sized_relobj : public Relobj
return p->second;
}
+ // Return whether the local symbol SYMNDX has a PLT offset.
+ bool
+ local_has_plt_offset(unsigned int symndx) const;
+
+ // Return the PLT offset for a local symbol. It is an error to call
+ // this if it doesn't have one.
+ unsigned int
+ local_plt_offset(unsigned int symndx) const;
+
+ // Set the PLT offset of the local symbol SYMNDX.
+ void
+ set_local_plt_offset(unsigned int symndx, unsigned int plt_offset);
+
// Get the offset of input section SHNDX within its output section.
// This is -1 if the input section requires a special mapping, such
// as a merge section. The output section can be found in the
@@ -2108,6 +2134,7 @@ class Sized_relobj : public Relobj
{
this->local_values_.clear();
this->local_got_offsets_.clear();
+ this->local_plt_offsets_.clear();
}
// Record a mapping from discarded section SHNDX to the corresponding
@@ -2139,20 +2166,8 @@ class Sized_relobj : public Relobj
// for tp-relative offsets for TLS symbols.
typedef Unordered_map<unsigned int, Got_offset_list*> Local_got_offsets;
- // The TLS GOT offsets of local symbols. The map stores the offsets
- // for either a single GOT entry that holds the module index of a TLS
- // symbol, or a pair of GOT entries containing the module index and
- // dtv-relative offset.
- struct Tls_got_entry
- {
- Tls_got_entry(int got_offset, bool have_pair)
- : got_offset_(got_offset),
- have_pair_(have_pair)
- { }
- int got_offset_;
- bool have_pair_;
- };
- typedef Unordered_map<unsigned int, Tls_got_entry> Local_tls_got_offsets;
+ // The PLT offsets of local symbols.
+ typedef Unordered_map<unsigned int, unsigned int> Local_plt_offsets;
// Saved information for sections whose layout was deferred.
struct Deferred_layout
@@ -2197,6 +2212,8 @@ class Sized_relobj : public Relobj
// GOT offsets for local non-TLS symbols, and tp-relative offsets
// for TLS symbols, indexed by symbol number.
Local_got_offsets local_got_offsets_;
+ // PLT offsets for local symbols.
+ Local_plt_offsets local_plt_offsets_;
// For each input section, the offset of the input section in its
// output section. This is INVALID_ADDRESS if the input section requires a
// special mapping.
Index: output.cc
===================================================================
RCS file: /cvs/src/src/gold/output.cc,v
retrieving revision 1.132
diff -p -u -r1.132 output.cc
--- output.cc 3 Aug 2010 14:07:13 -0000 1.132
+++ output.cc 19 Aug 2010 22:49:22 -0000
@@ -1,6 +1,6 @@
// output.cc -- manage the output file for gold
-// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+// Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.
// This file is part of gold.
@@ -1133,10 +1133,17 @@ Output_data_reloc_base<sh_type, dynamic,
os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
else
gold_unreachable();
- if (dynamic)
- os->set_should_link_to_dynsym();
- else
+
+ // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
+ // static link. The backends will generate a dynamic reloc section
+ // to hold this. In that case we don't want to link to the dynsym
+ // section, because there isn't one.
+ if (!dynamic)
os->set_should_link_to_symtab();
+ else if (parameters->doing_static_link())
+ ;
+ else
+ os->set_should_link_to_dynsym();
}
// Write out relocation data.
@@ -1262,12 +1269,18 @@ Output_data_got<size, big_endian>::Got_e
// link-time value, which will be relocated dynamically by a
// RELATIVE relocation.
Symbol* gsym = this->u_.gsym;
- Sized_symbol<size>* sgsym;
- // This cast is a bit ugly. We don't want to put a
- // virtual method in Symbol, because we want Symbol to be
- // as small as possible.
- sgsym = static_cast<Sized_symbol<size>*>(gsym);
- val = sgsym->value();
+ if (this->use_plt_offset_ && gsym->has_plt_offset())
+ val = (parameters->target().plt_section_for_global(gsym)->address()
+ + gsym->plt_offset());
+ else
+ {
+ Sized_symbol<size>* sgsym;
+ // This cast is a bit ugly. We don't want to put a
+ // virtual method in Symbol, because we want Symbol to be
+ // as small as possible.
+ sgsym = static_cast<Sized_symbol<size>*>(gsym);
+ val = sgsym->value();
+ }
}
break;
@@ -1277,9 +1290,17 @@ Output_data_got<size, big_endian>::Got_e
default:
{
+ const Sized_relobj<size, big_endian>* object = this->u_.object;
const unsigned int lsi = this->local_sym_index_;
- const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
- val = symval->value(this->u_.object, 0);
+ const Symbol_value<size>* symval = object->local_symbol(lsi);
+ if (!this->use_plt_offset_)
+ val = symval->value(this->u_.object, 0);
+ else
+ {
+ const Output_data* plt =
+ parameters->target().plt_section_for_local(object, lsi);
+ val = plt->address() + object->local_plt_offset(lsi);
+ }
}
break;
}
@@ -1302,7 +1323,23 @@ Output_data_got<size, big_endian>::add_g
if (gsym->has_got_offset(got_type))
return false;
- this->entries_.push_back(Got_entry(gsym));
+ this->entries_.push_back(Got_entry(gsym, false));
+ this->set_got_size();
+ gsym->set_got_offset(got_type, this->last_got_offset());
+ return true;
+}
+
+// Like add_global, but use the PLT offset.
+
+template<int size, bool big_endian>
+bool
+Output_data_got<size, big_endian>::add_global_plt(Symbol* gsym,
+ unsigned int got_type)
+{
+ if (gsym->has_got_offset(got_type))
+ return false;
+
+ this->entries_.push_back(Got_entry(gsym, true));
this->set_got_size();
gsym->set_got_offset(got_type, this->last_got_offset());
return true;
@@ -1310,6 +1347,7 @@ Output_data_got<size, big_endian>::add_g
// Add an entry for a global symbol to the GOT, and add a dynamic
// relocation of type R_TYPE for the GOT entry.
+
template<int size, bool big_endian>
void
Output_data_got<size, big_endian>::add_global_with_rel(
@@ -1417,7 +1455,25 @@ Output_data_got<size, big_endian>::add_l
if (object->local_has_got_offset(symndx, got_type))
return false;
- this->entries_.push_back(Got_entry(object, symndx));
+ this->entries_.push_back(Got_entry(object, symndx, false));
+ this->set_got_size();
+ object->set_local_got_offset(symndx, got_type, this->last_got_offset());
+ return true;
+}
+
+// Like add_local, but use the PLT offset.
+
+template<int size, bool big_endian>
+bool
+Output_data_got<size, big_endian>::add_local_plt(
+ Sized_relobj<size, big_endian>* object,
+ unsigned int symndx,
+ unsigned int got_type)
+{
+ if (object->local_has_got_offset(symndx, got_type))
+ return false;
+
+ this->entries_.push_back(Got_entry(object, symndx, true));
this->set_got_size();
object->set_local_got_offset(symndx, got_type, this->last_got_offset());
return true;
@@ -1425,6 +1481,7 @@ Output_data_got<size, big_endian>::add_l
// Add an entry for a local symbol to the GOT, and add a dynamic
// relocation of type R_TYPE for the GOT entry.
+
template<int size, bool big_endian>
void
Output_data_got<size, big_endian>::add_local_with_rel(
@@ -1486,7 +1543,7 @@ Output_data_got<size, big_endian>::add_l
Output_section* os = object->output_section(shndx);
rel_dyn->add_output_section(os, r_type_1, this, got_offset);
- this->entries_.push_back(Got_entry(object, symndx));
+ this->entries_.push_back(Got_entry(object, symndx, false));
if (r_type_2 != 0)
{
got_offset = this->last_got_offset();
@@ -1516,7 +1573,7 @@ Output_data_got<size, big_endian>::add_l
Output_section* os = object->output_section(shndx);
rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
- this->entries_.push_back(Got_entry(object, symndx));
+ this->entries_.push_back(Got_entry(object, symndx, false));
if (r_type_2 != 0)
{
got_offset = this->last_got_offset();
Index: output.h
===================================================================
RCS file: /cvs/src/src/gold/output.h,v
retrieving revision 1.111
diff -p -u -r1.111 output.h
--- output.h 3 Aug 2010 14:07:13 -0000 1.111
+++ output.h 19 Aug 2010 22:49:22 -0000
@@ -1,6 +1,6 @@
// output.h -- manage the output file for gold -*- C++ -*-
-// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+// Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.
// This file is part of gold.
@@ -605,6 +605,10 @@ class Output_section_data : public Outpu
{ }
// Return the output section.
+ Output_section*
+ output_section()
+ { return this->output_section_; }
+
const Output_section*
output_section() const
{ return this->output_section_; }
@@ -1913,6 +1917,11 @@ class Output_data_got : public Output_se
bool
add_global(Symbol* gsym, unsigned int got_type);
+ // Like add_global, but use the PLT offset of the global symbol if
+ // it has one.
+ bool
+ add_global_plt(Symbol* gsym, unsigned int got_type);
+
// Add an entry for a global symbol to the GOT, and add a dynamic
// relocation of type R_TYPE for the GOT entry.
void
@@ -1942,6 +1951,12 @@ class Output_data_got : public Output_se
add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
unsigned int got_type);
+ // Like add_local, but use the PLT offset of the local symbol if it
+ // has one.
+ bool
+ add_local_plt(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
+ unsigned int got_type);
+
// Add an entry for a local symbol to the GOT, and add a dynamic
// relocation of type R_TYPE for the GOT entry.
void
@@ -1995,28 +2010,29 @@ class Output_data_got : public Output_se
public:
// Create a zero entry.
Got_entry()
- : local_sym_index_(CONSTANT_CODE)
+ : local_sym_index_(CONSTANT_CODE), use_plt_offset_(false)
{ this->u_.constant = 0; }
// Create a global symbol entry.
- explicit Got_entry(Symbol* gsym)
- : local_sym_index_(GSYM_CODE)
+ Got_entry(Symbol* gsym, bool use_plt_offset)
+ : local_sym_index_(GSYM_CODE), use_plt_offset_(use_plt_offset)
{ this->u_.gsym = gsym; }
// Create a local symbol entry.
Got_entry(Sized_relobj<size, big_endian>* object,
- unsigned int local_sym_index)
- : local_sym_index_(local_sym_index)
+ unsigned int local_sym_index, bool use_plt_offset)
+ : local_sym_index_(local_sym_index), use_plt_offset_(use_plt_offset)
{
gold_assert(local_sym_index != GSYM_CODE
- && local_sym_index != CONSTANT_CODE);
+ && local_sym_index != CONSTANT_CODE
+ && local_sym_index == this->local_sym_index_);
this->u_.object = object;
}
// Create a constant entry. The constant is a host value--it will
// be swapped, if necessary, when it is written out.
explicit Got_entry(Valtype constant)
- : local_sym_index_(CONSTANT_CODE)
+ : local_sym_index_(CONSTANT_CODE), use_plt_offset_(false)
{ this->u_.constant = constant; }
// Write the GOT entry to an output view.
@@ -2026,8 +2042,8 @@ class Output_data_got : public Output_se
private:
enum
{
- GSYM_CODE = -1U,
- CONSTANT_CODE = -2U
+ GSYM_CODE = 0x7fffffff,
+ CONSTANT_CODE = 0x7ffffffe
};
union
@@ -2041,7 +2057,9 @@ class Output_data_got : public Output_se
} u_;
// For a local symbol, the local symbol index. This is GSYM_CODE
// for a global symbol, or CONSTANT_CODE for a constant.
- unsigned int local_sym_index_;
+ unsigned int local_sym_index_ : 31;
+ // Whether to use the PLT offset of the symbol if it has one.
+ bool use_plt_offset_ : 1;
};
typedef std::vector<Got_entry> Got_entries;
Index: symtab.h
===================================================================
RCS file: /cvs/src/src/gold/symtab.h,v
retrieving revision 1.111
diff -p -u -r1.111 symtab.h
--- symtab.h 12 Aug 2010 22:15:00 -0000 1.111
+++ symtab.h 19 Aug 2010 22:49:23 -0000
@@ -582,9 +582,6 @@ class Symbol
}
// Return true if this symbol is a function that needs a PLT entry.
- // If the symbol is defined in a dynamic object or if it is subject
- // to pre-emption, we need to make a PLT entry. If we're doing a
- // static link or a -pie link, we don't create PLT entries.
bool
needs_plt_entry() const
{
@@ -592,12 +589,27 @@ class Symbol
if (this->is_undefined() && !parameters->options().shared())
return false;
- return (!parameters->doing_static_link()
- && !parameters->options().pie()
- && this->is_func()
- && (this->is_from_dynobj()
- || this->is_undefined()
- || this->is_preemptible()));
+ // 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
@@ -678,6 +690,10 @@ class Symbol
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
Index: target-reloc.h
===================================================================
RCS file: /cvs/src/src/gold/target-reloc.h,v
retrieving revision 1.43
diff -p -u -r1.43 target-reloc.h
--- target-reloc.h 10 Jun 2010 17:20:27 -0000 1.43
+++ target-reloc.h 19 Aug 2010 22:49:23 -0000
@@ -284,6 +284,10 @@ relocate_section(
else
symval.set_no_output_symtab_entry();
symval.set_output_value(sym->value());
+ if (gsym->type() == elfcpp::STT_TLS)
+ symval.set_is_tls_symbol();
+ else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
+ symval.set_is_ifunc_symbol();
psymval = &symval;
is_defined_in_discarded_section =
Index: target.h
===================================================================
RCS file: /cvs/src/src/gold/target.h,v
retrieving revision 1.50
diff -p -u -r1.50 target.h
--- target.h 12 Aug 2010 22:15:00 -0000 1.50
+++ target.h 19 Aug 2010 22:49:23 -0000
@@ -53,6 +53,7 @@ class Symbol;
template<int size>
class Sized_symbol;
class Symbol_table;
+class Output_data;
class Output_section;
class Input_objects;
@@ -261,6 +262,18 @@ class Target
reloc_addend(void* arg, unsigned int type, uint64_t addend) const
{ return this->do_reloc_addend(arg, type, addend); }
+ // Return the PLT section to use for a global symbol. This is used
+ // for STT_GNU_IFUNC symbols.
+ Output_data*
+ plt_section_for_global(const Symbol* sym) const
+ { return this->do_plt_section_for_global(sym); }
+
+ // Return the PLT section to use for a local symbol. This is used
+ // for STT_GNU_IFUNC symbols.
+ Output_data*
+ plt_section_for_local(const Relobj* object, unsigned int symndx) const
+ { return this->do_plt_section_for_local(object, symndx); }
+
// Return true if a reference to SYM from a reloc of type R_TYPE
// means that the current function may call an object compiled
// without -fsplit-stack. SYM is known to be defined in an object
@@ -462,6 +475,16 @@ class Target
do_reloc_addend(void*, unsigned int, uint64_t) const
{ gold_unreachable(); }
+ // Virtual functions that must be overridden by a target that uses
+ // STT_GNU_IFUNC symbols.
+ virtual Output_data*
+ do_plt_section_for_global(const Symbol*) const
+ { gold_unreachable(); }
+
+ virtual Output_data*
+ do_plt_section_for_local(const Relobj*, unsigned int) const
+ { gold_unreachable(); }
+
// Virtual function which may be overridden by the child class. The
// default implementation is that any function not defined by the
// ABI is a call to a non-split function.
Index: x86_64.cc
===================================================================
RCS file: /cvs/src/src/gold/x86_64.cc,v
retrieving revision 1.114
diff -p -u -r1.114 x86_64.cc
--- x86_64.cc 12 Aug 2010 22:15:00 -0000 1.114
+++ x86_64.cc 19 Aug 2010 22:49:24 -0000
@@ -46,7 +46,115 @@ namespace
using namespace gold;
-class Output_data_plt_x86_64;
+// A class to handle the PLT data.
+
+class Output_data_plt_x86_64 : public Output_section_data
+{
+ public:
+ typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
+
+ Output_data_plt_x86_64(Symbol_table*, Layout*, Output_data_got<64, false>*,
+ Output_data_space*);
+
+ // Add an entry to the PLT.
+ void
+ add_entry(Symbol* gsym);
+
+ // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
+ unsigned int
+ add_local_ifunc_entry(Sized_relobj<64, false>* relobj,
+ unsigned int local_sym_index);
+
+ // Add the reserved TLSDESC_PLT entry to the PLT.
+ void
+ reserve_tlsdesc_entry(unsigned int got_offset)
+ { this->tlsdesc_got_offset_ = got_offset; }
+
+ // Return true if a TLSDESC_PLT entry has been reserved.
+ bool
+ has_tlsdesc_entry() const
+ { return this->tlsdesc_got_offset_ != -1U; }
+
+ // Return the GOT offset for the reserved TLSDESC_PLT entry.
+ unsigned int
+ get_tlsdesc_got_offset() const
+ { return this->tlsdesc_got_offset_; }
+
+ // Return the offset of the reserved TLSDESC_PLT entry.
+ unsigned int
+ get_tlsdesc_plt_offset() const
+ { return (this->count_ + 1) * plt_entry_size; }
+
+ // Return the .rela.plt section data.
+ Reloc_section*
+ rela_plt()
+ { return this->rel_; }
+
+ // Return where the TLSDESC relocations should go.
+ Reloc_section*
+ rela_tlsdesc(Layout*);
+
+ // Return the number of PLT entries.
+ unsigned int
+ entry_count() const
+ { return this->count_; }
+
+ // Return the offset of the first non-reserved PLT entry.
+ static unsigned int
+ first_plt_entry_offset()
+ { return plt_entry_size; }
+
+ // Return the size of a PLT entry.
+ static unsigned int
+ get_plt_entry_size()
+ { return plt_entry_size; }
+
+ protected:
+ void
+ do_adjust_output_section(Output_section* os);
+
+ // Write to a map file.
+ void
+ do_print_to_mapfile(Mapfile* mapfile) const
+ { mapfile->print_output_data(this, _("** PLT")); }
+
+ private:
+ // The size of an entry in the PLT.
+ static const int plt_entry_size = 16;
+
+ // The first entry in the PLT.
+ // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
+ // procedure linkage table for both programs and shared objects."
+ static unsigned char first_plt_entry[plt_entry_size];
+
+ // Other entries in the PLT for an executable.
+ static unsigned char plt_entry[plt_entry_size];
+
+ // The reserved TLSDESC entry in the PLT for an executable.
+ static unsigned char tlsdesc_plt_entry[plt_entry_size];
+
+ // Set the final size.
+ void
+ set_final_data_size();
+
+ // Write out the PLT data.
+ void
+ do_write(Output_file*);
+
+ // The reloc section.
+ Reloc_section* rel_;
+ // The TLSDESC relocs, if necessary. These must follow the regular
+ // PLT relocs.
+ Reloc_section* tlsdesc_rel_;
+ // The .got section.
+ Output_data_got<64, false>* got_;
+ // The .got.plt section.
+ Output_data_space* got_plt_;
+ // The number of PLT entries.
+ unsigned int count_;
+ // Offset of the reserved TLSDESC_GOT entry when needed.
+ unsigned int tlsdesc_got_offset_;
+};
// The x86_64 target class.
// See the ABI at
@@ -188,6 +296,15 @@ class Target_x86_64 : public Target_free
uint64_t
do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
+ // Return the PLT section.
+ Output_data*
+ do_plt_section_for_global(const Symbol*) const
+ { return this->plt_section(); }
+
+ Output_data*
+ do_plt_section_for_local(const Relobj*, unsigned int) const
+ { return this->plt_section(); }
+
// Adjust -fstack-split code which calls non-stack-split code.
void
do_calls_non_split(Relobj* object, unsigned int shndx,
@@ -291,6 +408,9 @@ class Target_x86_64 : public Target_free
inline bool
possible_function_pointer_reloc(unsigned int r_type);
+ bool
+ reloc_needs_plt_for_ifunc(Sized_relobj<64, false>*, unsigned int r_type);
+
// Whether we have issued an error about a non-PIC compilation.
bool issued_non_pic_error_;
};
@@ -441,6 +561,12 @@ class Target_x86_64 : public Target_free
void
make_plt_entry(Symbol_table*, Layout*, Symbol*);
+ // Create a PLT entry for a local STT_GNU_IFUNC symbol.
+ void
+ make_local_ifunc_plt_entry(Symbol_table*, Layout*,
+ Sized_relobj<64, false>* relobj,
+ unsigned int local_sym_index);
+
// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
void
define_tls_base_symbol(Symbol_table*, Layout*);
@@ -641,116 +767,12 @@ Target_x86_64::rela_dyn_section(Layout*
return this->rela_dyn_;
}
-// A class to handle the PLT data.
-
-class Output_data_plt_x86_64 : public Output_section_data
-{
- public:
- typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
-
- Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
- Output_data_space*);
-
- // Add an entry to the PLT.
- void
- add_entry(Symbol* gsym);
-
- // Add the reserved TLSDESC_PLT entry to the PLT.
- void
- reserve_tlsdesc_entry(unsigned int got_offset)
- { this->tlsdesc_got_offset_ = got_offset; }
-
- // Return true if a TLSDESC_PLT entry has been reserved.
- bool
- has_tlsdesc_entry() const
- { return this->tlsdesc_got_offset_ != -1U; }
-
- // Return the GOT offset for the reserved TLSDESC_PLT entry.
- unsigned int
- get_tlsdesc_got_offset() const
- { return this->tlsdesc_got_offset_; }
-
- // Return the offset of the reserved TLSDESC_PLT entry.
- unsigned int
- get_tlsdesc_plt_offset() const
- { return (this->count_ + 1) * plt_entry_size; }
-
- // Return the .rela.plt section data.
- const Reloc_section*
- rela_plt() const
- { return this->rel_; }
-
- // Return where the TLSDESC relocations should go.
- Reloc_section*
- rela_tlsdesc(Layout*);
-
- // Return the number of PLT entries.
- unsigned int
- entry_count() const
- { return this->count_; }
-
- // Return the offset of the first non-reserved PLT entry.
- static unsigned int
- first_plt_entry_offset()
- { return plt_entry_size; }
-
- // Return the size of a PLT entry.
- static unsigned int
- get_plt_entry_size()
- { return plt_entry_size; }
-
- protected:
- void
- do_adjust_output_section(Output_section* os);
-
- // Write to a map file.
- void
- do_print_to_mapfile(Mapfile* mapfile) const
- { mapfile->print_output_data(this, _("** PLT")); }
-
- private:
- // The size of an entry in the PLT.
- static const int plt_entry_size = 16;
-
- // The first entry in the PLT.
- // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
- // procedure linkage table for both programs and shared objects."
- static unsigned char first_plt_entry[plt_entry_size];
-
- // Other entries in the PLT for an executable.
- static unsigned char plt_entry[plt_entry_size];
-
- // The reserved TLSDESC entry in the PLT for an executable.
- static unsigned char tlsdesc_plt_entry[plt_entry_size];
-
- // Set the final size.
- void
- set_final_data_size();
-
- // Write out the PLT data.
- void
- do_write(Output_file*);
-
- // The reloc section.
- Reloc_section* rel_;
- // The TLSDESC relocs, if necessary. These must follow the regular
- // PLT relocs.
- Reloc_section* tlsdesc_rel_;
- // The .got section.
- Output_data_got<64, false>* got_;
- // The .got.plt section.
- Output_data_space* got_plt_;
- // The number of PLT entries.
- unsigned int count_;
- // Offset of the reserved TLSDESC_GOT entry when needed.
- unsigned int tlsdesc_got_offset_;
-};
-
// Create the PLT section. The ordinary .got section is an argument,
// since we need to refer to the start. We also create our own .got
// section just for PLT entries.
-Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
+Output_data_plt_x86_64::Output_data_plt_x86_64(Symbol_table* symtab,
+ Layout* layout,
Output_data_got<64, false>* got,
Output_data_space* got_plt)
: Output_section_data(8), tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
@@ -760,6 +782,24 @@ Output_data_plt_x86_64::Output_data_plt_
layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
elfcpp::SHF_ALLOC, this->rel_,
ORDER_DYNAMIC_PLT_RELOCS, false);
+
+ if (parameters->doing_static_link())
+ {
+ // A statically linked executable will only have a .rela.plt
+ // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
+ // symbols. The library will use these symbols to locate the
+ // IRELATIVE relocs at program startup time.
+ symtab->define_in_output_data("__rela_iplt_start", NULL,
+ Symbol_table::PREDEFINED,
+ this->rel_, 0, 0, elfcpp::STT_NOTYPE,
+ elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
+ 0, false, true);
+ symtab->define_in_output_data("__rela_iplt_end", NULL,
+ Symbol_table::PREDEFINED,
+ this->rel_, 0, 0, elfcpp::STT_NOTYPE,
+ elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
+ 0, true, true);
+ }
}
void
@@ -789,15 +829,46 @@ Output_data_plt_x86_64::add_entry(Symbol
this->got_plt_->set_current_data_size(got_offset + 8);
// Every PLT entry needs a reloc.
- gsym->set_needs_dynsym_entry();
- this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
- got_offset, 0);
+ if (gsym->type() == elfcpp::STT_GNU_IFUNC
+ && gsym->can_use_relative_reloc(false))
+ this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
+ this->got_plt_, got_offset, 0);
+ else
+ {
+ gsym->set_needs_dynsym_entry();
+ this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
+ got_offset, 0);
+ }
// Note that we don't need to save the symbol. The contents of the
// PLT are independent of which symbols are used. The symbols only
// appear in the relocations.
}
+// Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
+// the PLT offset.
+
+unsigned int
+Output_data_plt_x86_64::add_local_ifunc_entry(Sized_relobj<64, false>* relobj,
+ unsigned int local_sym_index)
+{
+ unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
+ ++this->count_;
+
+ section_offset_type got_offset = this->got_plt_->current_data_size();
+
+ // Every PLT entry needs a GOT entry which points back to the PLT
+ // entry.
+ this->got_plt_->set_current_data_size(got_offset + 8);
+
+ // Every PLT entry needs a reloc.
+ this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
+ elfcpp::R_X86_64_IRELATIVE,
+ this->got_plt_, got_offset, 0);
+
+ return plt_offset;
+}
+
// Return where the TLSDESC relocations should go, creating it if
// necessary. These follow the JUMP_SLOT relocations.
@@ -968,12 +1039,16 @@ Target_x86_64::make_plt_section(Symbol_t
// Create the GOT sections first.
this->got_section(symtab, layout);
- this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
+ this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
this->got_plt_);
layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
(elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR),
this->plt_, ORDER_PLT, false);
+
+ // Make the sh_info field of .rela.plt point to .plt.
+ Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
+ rela_plt_os->set_info_section(this->plt_->output_section());
}
}
@@ -1000,6 +1075,22 @@ Target_x86_64::make_plt_entry(Symbol_tab
this->plt_->add_entry(gsym);
}
+// Make a PLT entry for a local STT_GNU_IFUNC symbol.
+
+void
+Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
+ Sized_relobj<64, false>* relobj,
+ unsigned int local_sym_index)
+{
+ if (relobj->local_has_plt_offset(local_sym_index))
+ return;
+ if (this->plt_ == NULL)
+ this->make_plt_section(symtab, layout);
+ unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
+ local_sym_index);
+ relobj->set_local_plt_offset(local_sym_index, plt_offset);
+}
+
// Return the number of entries in the PLT.
unsigned int
@@ -1172,6 +1263,7 @@ Target_x86_64::Scan::check_non_pic(Relob
{
// These are the relocation types supported by glibc for x86_64.
case elfcpp::R_X86_64_RELATIVE:
+ case elfcpp::R_X86_64_IRELATIVE:
case elfcpp::R_X86_64_GLOB_DAT:
case elfcpp::R_X86_64_JUMP_SLOT:
case elfcpp::R_X86_64_DTPMOD64:
@@ -1200,6 +1292,72 @@ Target_x86_64::Scan::check_non_pic(Relob
}
}
+// Return whether we need to make a PLT entry for a relocation of the
+// given type against a STT_GNU_IFUNC symbol.
+
+bool
+Target_x86_64::Scan::reloc_needs_plt_for_ifunc(Sized_relobj<64, false>* object,
+ unsigned int r_type)
+{
+ switch (r_type)
+ {
+ case elfcpp::R_X86_64_NONE:
+ case elfcpp::R_X86_64_GNU_VTINHERIT:
+ case elfcpp::R_X86_64_GNU_VTENTRY:
+ return false;
+
+ case elfcpp::R_X86_64_64:
+ case elfcpp::R_X86_64_32:
+ case elfcpp::R_X86_64_32S:
+ case elfcpp::R_X86_64_16:
+ case elfcpp::R_X86_64_8:
+ case elfcpp::R_X86_64_PC64:
+ case elfcpp::R_X86_64_PC32:
+ case elfcpp::R_X86_64_PC16:
+ case elfcpp::R_X86_64_PC8:
+ case elfcpp::R_X86_64_PLT32:
+ case elfcpp::R_X86_64_GOTPC32:
+ case elfcpp::R_X86_64_GOTOFF64:
+ case elfcpp::R_X86_64_GOTPC64:
+ case elfcpp::R_X86_64_PLTOFF64:
+ case elfcpp::R_X86_64_GOT64:
+ case elfcpp::R_X86_64_GOT32:
+ case elfcpp::R_X86_64_GOTPCREL64:
+ case elfcpp::R_X86_64_GOTPCREL:
+ case elfcpp::R_X86_64_GOTPLT64:
+ return true;
+
+ case elfcpp::R_X86_64_COPY:
+ case elfcpp::R_X86_64_GLOB_DAT:
+ case elfcpp::R_X86_64_JUMP_SLOT:
+ case elfcpp::R_X86_64_RELATIVE:
+ case elfcpp::R_X86_64_IRELATIVE:
+ case elfcpp::R_X86_64_TPOFF64:
+ case elfcpp::R_X86_64_DTPMOD64:
+ case elfcpp::R_X86_64_TLSDESC:
+ // We will give an error later.
+ return false;
+
+ case elfcpp::R_X86_64_TLSGD:
+ case elfcpp::R_X86_64_GOTPC32_TLSDESC:
+ case elfcpp::R_X86_64_TLSDESC_CALL:
+ case elfcpp::R_X86_64_TLSLD:
+ case elfcpp::R_X86_64_DTPOFF32:
+ case elfcpp::R_X86_64_DTPOFF64:
+ case elfcpp::R_X86_64_GOTTPOFF:
+ case elfcpp::R_X86_64_TPOFF32:
+ gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
+ object->name().c_str(), r_type);
+ return false;
+
+ case elfcpp::R_X86_64_SIZE32:
+ case elfcpp::R_X86_64_SIZE64:
+ default:
+ // We will give an error later.
+ return false;
+ }
+}
+
// Scan a relocation for a local symbol.
inline void
@@ -1213,6 +1371,14 @@ Target_x86_64::Scan::local(Symbol_table*
unsigned int r_type,
const elfcpp::Sym<64, false>& lsym)
{
+ // A local STT_GNU_IFUNC symbol may require a PLT entry.
+ if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
+ && this->reloc_needs_plt_for_ifunc(object, r_type))
+ {
+ unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
+ target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
+ }
+
switch (r_type)
{
case elfcpp::R_X86_64_NONE:
@@ -1231,11 +1397,11 @@ Target_x86_64::Scan::local(Symbol_table*
{
unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
Reloc_section* rela_dyn = target->rela_dyn_section(layout);
- rela_dyn->add_local_relative(object, r_sym,
- elfcpp::R_X86_64_RELATIVE,
- output_section, data_shndx,
- reloc.get_r_offset(),
- reloc.get_r_addend());
+ rela_dyn->add_local_relative(object, r_sym,
+ elfcpp::R_X86_64_RELATIVE,
+ output_section, data_shndx,
+ reloc.get_r_offset(),
+ reloc.get_r_addend());
}
break;
@@ -1306,7 +1472,16 @@ Target_x86_64::Scan::local(Symbol_table*
// The symbol requires a GOT entry.
Output_data_got<64, false>* got = target->got_section(symtab, layout);
unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
- if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
+
+ // For a STT_GNU_IFUNC symbol we want the PLT offset. That
+ // lets function pointers compare correctly with shared
+ // libraries. Otherwise we would need an IRELATIVE reloc.
+ bool is_new;
+ if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
+ is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
+ else
+ is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
+ if (is_new)
{
// If we are generating a shared object, we need to add a
// dynamic relocation for this symbol's GOT entry.
@@ -1315,9 +1490,13 @@ Target_x86_64::Scan::local(Symbol_table*
Reloc_section* rela_dyn = target->rela_dyn_section(layout);
// R_X86_64_RELATIVE assumes a 64-bit relocation.
if (r_type != elfcpp::R_X86_64_GOT32)
- rela_dyn->add_local_relative(
- object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
- object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
+ {
+ unsigned int got_offset =
+ object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
+ rela_dyn->add_local_relative(object, r_sym,
+ elfcpp::R_X86_64_RELATIVE,
+ got, got_offset, 0);
+ }
else
{
this->check_non_pic(object, r_type);
@@ -1338,6 +1517,7 @@ Target_x86_64::Scan::local(Symbol_table*
case elfcpp::R_X86_64_GLOB_DAT:
case elfcpp::R_X86_64_JUMP_SLOT:
case elfcpp::R_X86_64_RELATIVE:
+ case elfcpp::R_X86_64_IRELATIVE:
// These are outstanding tls relocs, which are unexpected when linking
case elfcpp::R_X86_64_TPOFF64:
case elfcpp::R_X86_64_DTPMOD64:
@@ -1571,6 +1751,11 @@ Target_x86_64::Scan::global(Symbol_table
unsigned int r_type,
Symbol* gsym)
{
+ // A STT_GNU_IFUNC symbol may require a PLT entry.
+ if (gsym->type() == elfcpp::STT_GNU_IFUNC
+ && this->reloc_needs_plt_for_ifunc(object, r_type))
+ target->make_plt_entry(symtab, layout, gsym);
+
switch (r_type)
{
case elfcpp::R_X86_64_NONE:
@@ -1603,14 +1788,34 @@ Target_x86_64::Scan::global(Symbol_table
target->copy_reloc(symtab, layout, object,
data_shndx, output_section, gsym, reloc);
}
+ else if (r_type == elfcpp::R_X86_64_64
+ && gsym->type() == elfcpp::STT_GNU_IFUNC
+ && gsym->can_use_relative_reloc(false)
+ && !gsym->is_from_dynobj()
+ && !gsym->is_undefined()
+ && !gsym->is_preemptible())
+ {
+ // Use an IRELATIVE reloc for a locally defined
+ // STT_GNU_IFUNC symbol. This makes a function
+ // address in a PIE executable match the address in a
+ // shared library that it links against.
+ Reloc_section* rela_dyn = target->rela_dyn_section(layout);
+ unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
+ rela_dyn->add_symbolless_global_addend(gsym, r_type,
+ output_section, object,
+ data_shndx,
+ reloc.get_r_offset(),
+ reloc.get_r_addend());
+ }
else if (r_type == elfcpp::R_X86_64_64
&& gsym->can_use_relative_reloc(false))
{
Reloc_section* rela_dyn = target->rela_dyn_section(layout);
- rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
- output_section, object,
- data_shndx, reloc.get_r_offset(),
- reloc.get_r_addend());
+ rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
+ output_section, object,
+ data_shndx,
+ reloc.get_r_offset(),
+ reloc.get_r_addend());
}
else
{
@@ -1664,23 +1869,49 @@ Target_x86_64::Scan::global(Symbol_table
// The symbol requires a GOT entry.
Output_data_got<64, false>* got = target->got_section(symtab, layout);
if (gsym->final_value_is_known())
- got->add_global(gsym, GOT_TYPE_STANDARD);
+ {
+ // For a STT_GNU_IFUNC symbol we want the PLT address.
+ if (gsym->type() == elfcpp::STT_GNU_IFUNC)
+ got->add_global_plt(gsym, GOT_TYPE_STANDARD);
+ else
+ got->add_global(gsym, GOT_TYPE_STANDARD);
+ }
else
{
// If this symbol is not fully resolved, we need to add a
// dynamic relocation for it.
Reloc_section* rela_dyn = target->rela_dyn_section(layout);
- if (gsym->is_from_dynobj()
- || gsym->is_undefined()
- || gsym->is_preemptible())
+ if (gsym->is_from_dynobj()
+ || gsym->is_undefined()
+ || gsym->is_preemptible()
+ || (gsym->type() == elfcpp::STT_GNU_IFUNC
+ && parameters->options().output_is_position_independent()))
got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
elfcpp::R_X86_64_GLOB_DAT);
else
{
- if (got->add_global(gsym, GOT_TYPE_STANDARD))
- rela_dyn->add_global_relative(
- gsym, elfcpp::R_X86_64_RELATIVE, got,
- gsym->got_offset(GOT_TYPE_STANDARD), 0);
+ // For a STT_GNU_IFUNC symbol we want to write the PLT
+ // offset into the GOT, so that function pointer
+ // comparisons work correctly.
+ bool is_new;
+ if (gsym->type() != elfcpp::STT_GNU_IFUNC)
+ is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
+ else
+ {
+ is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
+ // Tell the dynamic linker to use the PLT address
+ // when resolving relocations.
+ if (gsym->is_from_dynobj()
+ && !parameters->options().shared())
+ gsym->set_needs_dynsym_value();
+ }
+ if (is_new)
+ {
+ unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
+ rela_dyn->add_global_relative(gsym,
+ elfcpp::R_X86_64_RELATIVE,
+ got, got_off, 0);
+ }
}
}
// For GOTPLT64, we also need a PLT entry (but only if the
@@ -1723,6 +1954,7 @@ Target_x86_64::Scan::global(Symbol_table
case elfcpp::R_X86_64_GLOB_DAT:
case elfcpp::R_X86_64_JUMP_SLOT:
case elfcpp::R_X86_64_RELATIVE:
+ case elfcpp::R_X86_64_IRELATIVE:
// These are outstanding tls relocs, which are unexpected when linking
case elfcpp::R_X86_64_TPOFF64:
case elfcpp::R_X86_64_DTPMOD64:
@@ -2007,7 +2239,9 @@ Target_x86_64::Relocate::relocate(const
}
}
- // Pick the value to use for symbols defined in shared objects.
+ const Sized_relobj<64, false>* object = relinfo->object;
+
+ // Pick the value to use for symbols defined in the PLT.
Symbol_value<64> symval;
if (gsym != NULL
&& gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
@@ -2019,8 +2253,17 @@ Target_x86_64::Relocate::relocate(const
+ gsym->plt_offset());
psymval = &symval;
}
+ else if (gsym == NULL && psymval->is_ifunc_symbol())
+ {
+ unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
+ if (object->local_has_plt_offset(r_sym))
+ {
+ symval.set_output_value(target->plt_section()->address()
+ + object->local_plt_offset(r_sym));
+ psymval = &symval;
+ }
+ }
- const Sized_relobj<64, false>* object = relinfo->object;
const elfcpp::Elf_Xword addend = rela.get_r_addend();
// Get the GOT offset if needed.
@@ -2196,6 +2439,7 @@ Target_x86_64::Relocate::relocate(const
case elfcpp::R_X86_64_GLOB_DAT:
case elfcpp::R_X86_64_JUMP_SLOT:
case elfcpp::R_X86_64_RELATIVE:
+ case elfcpp::R_X86_64_IRELATIVE:
// These are outstanding tls relocs, which are unexpected when linking
case elfcpp::R_X86_64_TPOFF64:
case elfcpp::R_X86_64_DTPMOD64:
@@ -2789,6 +3033,7 @@ Target_x86_64::Relocatable_size_for_relo
case elfcpp::R_X86_64_GLOB_DAT:
case elfcpp::R_X86_64_JUMP_SLOT:
case elfcpp::R_X86_64_RELATIVE:
+ case elfcpp::R_X86_64_IRELATIVE:
// These are outstanding tls relocs, which are unexpected when linking
case elfcpp::R_X86_64_TPOFF64:
case elfcpp::R_X86_64_DTPMOD64:
Index: testsuite/Makefile.am
===================================================================
RCS file: /cvs/src/src/gold/testsuite/Makefile.am,v
retrieving revision 1.140
diff -p -u -r1.140 Makefile.am
--- testsuite/Makefile.am 13 Aug 2010 00:18:19 -0000 1.140
+++ testsuite/Makefile.am 19 Aug 2010 22:49:24 -0000
@@ -1530,6 +1530,208 @@ no_version_test.o: no_version_test.c
no_version_test.stdout: libno_version_test.so
$(TEST_OBJDUMP) -h $< > $@
+# Test STT_GNU_IFUNC symbols.
+if IFUNC
+
+ifuncmod1.o: ifuncmod1.c
+ $(COMPILE) -c -fpic -o $@ $<
+ifuncmod1.so: ifuncmod1.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -shared ifuncmod1.o
+
+ifuncdep1.o: ifuncmod1.c
+ $(COMPILE) -c -o $@ $<
+
+ifuncmain1pic.o: ifuncmain1.c
+ $(COMPILE) -c -fpic -o $@ $<
+ifuncmain1pie.o: ifuncmain1.c
+ $(COMPILE) -c -fpie -o $@ $<
+
+check_PROGRAMS += ifuncmain1static
+ifuncmain1static_SOURCES = ifuncmain1.c
+ifuncmain1static_DEPENDENCIES = gcctestdir/ld ifuncdep1.o
+ifuncmain1static_LDFLAGS = -Bgcctestdir/ -static
+ifuncmain1static_LDADD = ifuncdep1.o
+
+check_PROGRAMS += ifuncmain1picstatic
+ifuncmain1picstatic: ifuncmain1pic.o ifuncmod1.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -static ifuncmain1pic.o ifuncmod1.o
+
+check_PROGRAMS += ifuncmain1
+ifuncmain1_SOURCES = ifuncmain1.c
+ifuncmain1_DEPENDENCIES = gcctestdir/ld ifuncmod1.so
+ifuncmain1_LDFLAGS = -Bgcctestdir/ -Wl,-R,.
+ifuncmain1_LDADD = ifuncmod1.so
+
+check_PROGRAMS += ifuncmain1pic
+ifuncmain1pic: ifuncmain1pic.o ifuncmod1.so gcctestdir/ld
+ $(LINK) -Bgcctestdir/ ifuncmain1pic.o ifuncmod1.so -Wl,-R,.
+
+check_PROGRAMS += ifuncmain1vis
+ifuncmain1vis_SOURCES = ifuncmain1vis.c
+ifuncmain1vis_DEPENDENCIES = gcctestdir/ld ifuncmod1.so
+ifuncmain1vis_LDFLAGS = -Bgcctestdir/ -Wl,-R,.
+ifuncmain1vis_LDADD = ifuncmod1.so
+
+check_PROGRAMS += ifuncmain1vispic
+ifuncmain1vispic.o: ifuncmain1vis.c
+ $(COMPILE) -c -fpic -o $@ $<
+ifuncmain1vispic: ifuncmain1vispic.o ifuncmod1.so gcctestdir/ld
+ $(LINK) -Bgcctestdir/ ifuncmain1pic.o ifuncmod1.so -Wl,-R,.
+
+check_PROGRAMS += ifuncmain1staticpic
+ifuncmain1staticpic: ifuncmain1pic.o ifuncmod1.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ ifuncmain1pic.o ifuncmod1.o
+
+check_PROGRAMS += ifuncmain1pie
+ifuncmain1pie: ifuncmain1pie.o ifuncmod1.so gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -pie ifuncmain1pie.o ifuncmod1.so -Wl,-R,.
+
+check_PROGRAMS += ifuncmain1vispie
+ifuncmain1vispie.o: ifuncmain1vis.c
+ $(COMPILE) -c -fpie -o $@ $<
+ifuncmain1vispie: ifuncmain1vispie.o ifuncmod1.so gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -pie ifuncmain1vispie.o ifuncmod1.so -Wl,-R,.
+
+check_PROGRAMS += ifuncmain1staticpie
+ifuncmain1staticpie: ifuncmain1pie.o ifuncmod1.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -pie ifuncmain1pie.o ifuncmod1.o
+
+ifuncmain2pic.o: ifuncmain2.c
+ $(COMPILE) -c -fpic -o $@ $<
+
+ifuncdep2pic.o: ifuncdep2.c
+ $(COMPILE) -c -fpic -o $@ $<
+
+check_PROGRAMS += ifuncmain2static
+ifuncmain2static_SOURCES = ifuncmain2.c ifuncdep2.c
+ifuncmain2static_DEPENDENCIES = gcctestdir/ld
+ifuncmain2static_LDFLAGS = -Bgcctestdir/ -static
+
+check_PROGRAMS += ifuncmain2picstatic
+ifuncmain2picstatic: ifuncmain2pic.o ifuncdep2pic.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -static ifuncmain2pic.o ifuncdep2pic.o
+
+check_PROGRAMS += ifuncmain2
+ifuncmain2_SOURCES = ifuncmain2.c ifuncdep2.c
+ifuncmain2_DEPENDENCIES = gcctestdir/ld
+ifuncmain2_LDFLAGS = -Bgcctestdir/
+
+check_PROGRAMS += ifuncmain2pic
+ifuncmain2pic: ifuncmain2pic.o ifuncdep2pic.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ ifuncmain2pic.o ifuncdep2pic.o
+
+ifuncmod3.o: ifuncmod3.c
+ $(COMPILE) -c -fpic -o $@ $<
+ifuncmod3.so: ifuncmod3.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -shared ifuncmod3.o
+
+check_PROGRAMS += ifuncmain3
+ifuncmain3_SOURCES = ifuncmain3.c
+ifuncmain3_DEPENDENCIES = gcctestdir/ld ifuncmod3.so
+ifuncmain3_LDFLAGS = -Bgcctestdir/ -Wl,--export-dynamic -Wl,-R,.
+ifuncmain3_LDADD = -ldl
+
+ifuncmain4pic.o: ifuncmain4.c
+ $(COMPILE) -c -fpic -o $@ $<
+
+check_PROGRAMS += ifuncmain4static
+ifuncmain4static_SOURCES = ifuncmain4.c
+ifuncmain4static_DEPENDENCIES = gcctestdir/ld
+ifuncmain4static_LDFLAGS = -Bgcctestdir/ -static
+
+check_PROGRAMS += ifuncmain4picstatic
+ifuncmain4picstatic: ifuncmain4pic.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -static ifuncmain4pic.o
+
+check_PROGRAMS += ifuncmain4
+ifuncmain4_SOURCES = ifuncmain4.c
+ifuncmain4_DEPENDENCIES = gcctestdir/ld
+ifuncmain4_LDFLAGS = -Bgcctestdir/
+
+ifuncmain5pic.o: ifuncmain5.c
+ $(COMPILE) -c -fpic -o $@ $<
+
+ifuncmain5pie.o: ifuncmain5.c
+ $(COMPILE) -c -fpie -o $@ $<
+
+ifuncmod5.o: ifuncmod5.c
+ $(COMPILE) -c -fpic -o $@ $<
+ifuncmod5.so: ifuncmod5.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -shared ifuncmod5.o
+
+ifuncdep5.o: ifuncmod5.c
+ $(COMPILE) -c -o $@ $<
+
+check_PROGRAMS += ifuncmain5static
+ifuncmain5static_SOURCES = ifuncmain5.c
+ifuncmain5static_DEPENDENCIES = gcctestdir/ld ifuncdep5.o
+ifuncmain5static_LDFLAGS = -Bgcctestdir/ -static
+ifuncmain5static_LDADD = ifuncdep5.o
+
+check_PROGRAMS += ifuncmain5picstatic
+ifuncmain5picstatic: ifuncmain5pic.o ifuncmod5.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -static ifuncmain5pic.o ifuncmod5.o
+
+check_PROGRAMS += ifuncmain5
+ifuncmain5_SOURCES = ifuncmain5.c
+ifuncmain5_DEPENDENCIES = gcctestdir/ld ifuncmod5.so
+ifuncmain5_LDFLAGS = -Bgcctestdir/ -Wl,-R,.
+ifuncmain5_LDADD = ifuncmod5.so
+
+check_PROGRAMS += ifuncmain5pic
+ifuncmain5pic: ifuncmain5pic.o ifuncmod5.so gcctestdir/ld
+ $(LINK) -Bgcctestdir/ ifuncmain5pic.o ifuncmod5.so -Wl,-R,.
+
+check_PROGRAMS += ifuncmain5staticpic
+ifuncmain5staticpic: ifuncmain5pic.o ifuncmod5.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ ifuncmain5pic.o ifuncmod5.o
+
+check_PROGRAMS += ifuncmain5pie
+ifuncmain5pie: ifuncmain5pie.o ifuncmod5.so gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -pie ifuncmain5pie.o ifuncmod5.so -Wl,-R,.
+
+ifuncmain6pie.o: ifuncmain6pie.c
+ $(COMPILE) -c -fpie -o $@ $<
+
+ifuncmod6.o: ifuncmod6.c
+ $(COMPILE) -c -fpic -o $@ $<
+ifuncmod6.so: ifuncmod6.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -shared ifuncmod6.o
+
+check_PROGRAMS += ifuncmain6pie
+ifuncmain6pie: ifuncmain6pie.o ifuncmod6.so gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -pie ifuncmain6pie.o ifuncmod6.so -Wl,-R,.
+
+ifuncmain7pic.o: ifuncmain7.c
+ $(COMPILE) -c -fpic -o $@ $<
+
+ifuncmain7pie.o: ifuncmain7.c
+ $(COMPILE) -c -fpie -o $@ $<
+
+check_PROGRAMS += ifuncmain7static
+ifuncmain7static_SOURCES = ifuncmain7.c
+ifuncmain7static_DEPENDENCIES = gcctestdir/ld
+ifuncmain7static_LDFLAGS = -Bgcctestdir/ -static
+
+check_PROGRAMS += ifuncmain7picstatic
+ifuncmain7picstatic: ifuncmain7pic.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -static ifuncmain7pic.o
+
+check_PROGRAMS += ifuncmain7
+ifuncmain7_SOURCES = ifuncmain7.c
+ifuncmain7_DEPENDENCIES = gcctestdir/ld
+ifuncmain7_LDFLAGS = -Bgcctestdir/
+
+check_PROGRAMS += ifuncmain7pic
+ifuncmain7pic: ifuncmain7pic.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ ifuncmain7pic.o
+
+check_PROGRAMS += ifuncmain7pie
+ifuncmain7pie: ifuncmain7pie.o gcctestdir/ld
+ $(LINK) -Bgcctestdir/ -pie ifuncmain7pie.o
+
+endif IFUNC
+
# Test that strong reference to a weak symbol in a DSO remains strong.
check_SCRIPTS += strong_ref_weak_def.sh
check_DATA += strong_ref_weak_def.stdout
Index: testsuite/ifunc-sel.h
===================================================================
RCS file: testsuite/ifunc-sel.h
diff -N testsuite/ifunc-sel.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifunc-sel.h 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,26 @@
+/* Used by the elf ifunc tests. */
+#ifndef ELF_IFUNC_SEL_H
+#define ELF_IFUNC_SEL_H 1
+
+extern int global;
+
+static inline void *
+ifunc_sel (int (*f1) (void), int (*f2) (void), int (*f3) (void))
+{
+ switch (global)
+ {
+ case 1:
+ return f1;
+ case -1:
+ return f2;
+ default:
+ return f3;
+ }
+}
+
+static inline void *
+ifunc_one (int (*f1) (void))
+{
+ return f1;
+}
+#endif
Index: testsuite/ifuncdep2.c
===================================================================
RCS file: testsuite/ifuncdep2.c
diff -N testsuite/ifuncdep2.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncdep2.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,50 @@
+/* Test 3 STT_GNU_IFUNC symbols. */
+
+#include "ifunc-sel.h"
+
+int global __attribute__ ((visibility ("protected"))) = -1;
+
+static int
+one (void)
+{
+ return 1;
+}
+
+static int
+minus_one (void)
+{
+ return -1;
+}
+
+static int
+zero (void)
+{
+ return 0;
+}
+
+void * foo1_ifunc (void) __asm__ ("foo1");
+__asm__(".type foo1, %gnu_indirect_function");
+
+void *
+foo1_ifunc (void)
+{
+ return ifunc_sel (one, minus_one, zero);
+}
+
+void * foo2_ifunc (void) __asm__ ("foo2");
+__asm__(".type foo2, %gnu_indirect_function");
+
+void *
+foo2_ifunc (void)
+{
+ return ifunc_sel (minus_one, one, zero);
+}
+
+void * foo3_ifunc (void) __asm__ ("foo3");
+__asm__(".type foo3, %gnu_indirect_function");
+
+void *
+foo3_ifunc (void)
+{
+ return ifunc_sel (one, zero, minus_one);
+}
Index: testsuite/ifuncmain1.c
===================================================================
RCS file: testsuite/ifuncmain1.c
diff -N testsuite/ifuncmain1.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncmain1.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,64 @@
+/* Test STT_GNU_IFUNC symbols:
+
+ 1. Direct function call.
+ 2. Function pointer.
+ 3. Visibility without override.
+ */
+
+#include <stdlib.h>
+
+int ret_foo;
+int ret_foo_hidden;
+int ret_foo_protected;
+
+extern int foo (void);
+extern int foo_protected (void);
+
+#ifndef FOO_P
+typedef int (*foo_p) (void);
+#endif
+
+foo_p foo_ptr = foo;
+foo_p foo_procted_ptr = foo_protected;
+
+extern foo_p get_foo_p (void);
+extern foo_p get_foo_hidden_p (void);
+extern foo_p get_foo_protected_p (void);
+
+int
+main (void)
+{
+ foo_p p;
+
+ if (foo_ptr != foo)
+ abort ();
+ if (foo () != -1)
+ abort ();
+ if ((*foo_ptr) () != -1)
+ abort ();
+
+ if (foo_procted_ptr != foo_protected)
+ abort ();
+ if (foo_protected () != 0)
+ abort ();
+ if ((*foo_procted_ptr) () != 0)
+ abort ();
+
+ p = get_foo_p ();
+ if (p != foo)
+ abort ();
+ if (ret_foo != -1 || (*p) () != ret_foo)
+ abort ();
+
+ p = get_foo_hidden_p ();
+ if (ret_foo_hidden != 1 || (*p) () != ret_foo_hidden)
+ abort ();
+
+ p = get_foo_protected_p ();
+ if (p != foo_protected)
+ abort ();
+ if (ret_foo_protected != 0 || (*p) () != ret_foo_protected)
+ abort ();
+
+ return 0;
+}
Index: testsuite/ifuncmain1vis.c
===================================================================
RCS file: testsuite/ifuncmain1vis.c
diff -N testsuite/ifuncmain1vis.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncmain1vis.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,89 @@
+/* Test STT_GNU_IFUNC symbols:
+
+ 1. Direct function call.
+ 2. Function pointer.
+ 3. Visibility with override.
+ */
+
+#include <stdlib.h>
+
+int __attribute__ ((noinline)) foo_hidden (void);
+
+int ret_foo;
+int ret_foo_hidden;
+int ret_foo_protected;
+
+extern int foo (void);
+extern int foo_protected (void);
+
+#ifndef FOO_P
+typedef int (*foo_p) (void);
+#endif
+
+foo_p foo_ptr = foo;
+foo_p foo_procted_ptr = foo_protected;
+
+extern foo_p get_foo_p (void);
+extern foo_p get_foo_hidden_p (void);
+extern foo_p get_foo_protected_p (void);
+
+int
+__attribute__ ((noinline))
+foo (void)
+{
+ return -30;
+}
+
+int
+__attribute__ ((noinline))
+foo_hidden (void)
+{
+ return -20;
+}
+
+int
+__attribute__ ((noinline))
+foo_protected (void)
+{
+ return -40;
+}
+
+int
+main (void)
+{
+ foo_p p;
+
+ if (foo_ptr != foo)
+ abort ();
+ if ((*foo_ptr) () != -30)
+ abort ();
+
+ if (foo_procted_ptr != foo_protected)
+ abort ();
+ if ((*foo_procted_ptr) () != -40)
+ abort ();
+
+ p = get_foo_p ();
+ if (p != foo)
+ abort ();
+ if (foo () != -30)
+ abort ();
+ if (ret_foo != -30 || (*p) () != ret_foo)
+ abort ();
+
+ p = get_foo_hidden_p ();
+ if (foo_hidden () != -20)
+ abort ();
+ if (ret_foo_hidden != 1 || (*p) () != ret_foo_hidden)
+ abort ();
+
+ p = get_foo_protected_p ();
+ if (p == foo_protected)
+ abort ();
+ if (foo_protected () != -40)
+ abort ();
+ if (ret_foo_protected != 0 || (*p) () != ret_foo_protected)
+ abort ();
+
+ return 0;
+}
Index: testsuite/ifuncmain2.c
===================================================================
RCS file: testsuite/ifuncmain2.c
diff -N testsuite/ifuncmain2.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncmain2.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,14 @@
+/* Test calling one STT_GNU_IFUNC function with 3 different
+ STT_GNU_IFUNC definitions. */
+
+#include <stdlib.h>
+
+extern int foo1 (void);
+
+int
+main (void)
+{
+ if (foo1 () != -1)
+ abort ();
+ return 0;
+}
Index: testsuite/ifuncmain3.c
===================================================================
RCS file: testsuite/ifuncmain3.c
diff -N testsuite/ifuncmain3.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncmain3.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,133 @@
+/* Test STT_GNU_IFUNC symbols with dlopen:
+
+ 1. Direct function call.
+ 2. Function pointer.
+ 3. Visibility with override.
+ */
+
+#include <dlfcn.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+extern int __attribute__ ((noinline)) foo (void);
+extern int __attribute__ ((noinline)) foo_hidden (void);
+extern int __attribute__ ((noinline)) foo_protected (void);
+
+typedef int (*foo_p) (void);
+
+int
+__attribute__ ((noinline))
+foo (void)
+{
+ return -30;
+}
+
+int
+__attribute__ ((noinline))
+foo_hidden (void)
+{
+ return -20;
+}
+
+int
+__attribute__ ((noinline))
+foo_protected (void)
+{
+ return -40;
+}
+
+int
+main (void)
+{
+ foo_p p;
+ foo_p (*f) (void);
+ int *ret;
+
+ void *h = dlopen ("ifuncmod3.so", RTLD_LAZY);
+ if (h == NULL)
+ {
+ printf ("cannot load: %s\n", dlerror ());
+ return 1;
+ }
+
+ p = dlsym (h, "foo");
+ if (p == NULL)
+ {
+ printf ("symbol not found: %s\n", dlerror ());
+ return 1;
+ }
+ if ((*p) () != -1)
+ abort ();
+
+ f = dlsym (h, "get_foo_p");
+ if (f == NULL)
+ {
+ printf ("symbol not found: %s\n", dlerror ());
+ return 1;
+ }
+
+ ret = dlsym (h, "ret_foo");
+ if (ret == NULL)
+ {
+ printf ("symbol not found: %s\n", dlerror ());
+ return 1;
+ }
+
+ p = (*f) ();
+ if (p != foo)
+ abort ();
+ if (foo () != -30)
+ abort ();
+ if (*ret != -30 || (*p) () != *ret)
+ abort ();
+
+ f = dlsym (h, "get_foo_hidden_p");
+ if (f == NULL)
+ {
+ printf ("symbol not found: %s\n", dlerror ());
+ return 1;
+ }
+
+ ret = dlsym (h, "ret_foo_hidden");
+ if (ret == NULL)
+ {
+ printf ("symbol not found: %s\n", dlerror ());
+ return 1;
+ }
+
+ p = (*f) ();
+ if (foo_hidden () != -20)
+ abort ();
+ if (*ret != 1 || (*p) () != *ret)
+ abort ();
+
+ f = dlsym (h, "get_foo_protected_p");
+ if (f == NULL)
+ {
+ printf ("symbol not found: %s\n", dlerror ());
+ return 1;
+ }
+
+ ret = dlsym (h, "ret_foo_protected");
+ if (ret == NULL)
+ {
+ printf ("symbol not found: %s\n", dlerror ());
+ return 1;
+ }
+
+ p = (*f) ();
+ if (p == foo_protected)
+ abort ();
+ if (foo_protected () != -40)
+ abort ();
+ if (*ret != 0 || (*p) () != *ret)
+ abort ();
+
+ if (dlclose (h) != 0)
+ {
+ printf ("cannot close: %s\n", dlerror ());
+ return 1;
+ }
+
+ return 0;
+}
Index: testsuite/ifuncmain4.c
===================================================================
RCS file: testsuite/ifuncmain4.c
diff -N testsuite/ifuncmain4.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncmain4.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,4 @@
+/* Test STT_GNU_IFUNC symbols in a single source file. */
+
+#include "ifuncmod1.c"
+#include "ifuncmain1.c"
Index: testsuite/ifuncmain5.c
===================================================================
RCS file: testsuite/ifuncmain5.c
diff -N testsuite/ifuncmain5.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncmain5.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,41 @@
+/* Test STT_GNU_IFUNC symbols with dynamic function pointer only. */
+
+#include <stdlib.h>
+
+extern int foo (void);
+extern int foo_protected (void);
+
+typedef int (*foo_p) (void);
+
+extern foo_p __attribute__ ((noinline)) get_foo (void);
+extern foo_p __attribute__ ((noinline)) get_foo_protected (void);
+
+foo_p
+__attribute__ ((noinline))
+get_foo (void)
+{
+ return foo;
+}
+
+foo_p
+__attribute__ ((noinline))
+get_foo_protected (void)
+{
+ return foo_protected;
+}
+
+int
+main (void)
+{
+ foo_p p;
+
+ p = get_foo ();
+ if ((*p) () != -1)
+ abort ();
+
+ p = get_foo_protected ();
+ if ((*p) () != 0)
+ abort ();
+
+ return 0;
+}
Index: testsuite/ifuncmain6pie.c
===================================================================
RCS file: testsuite/ifuncmain6pie.c
diff -N testsuite/ifuncmain6pie.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncmain6pie.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,64 @@
+/* Test STT_GNU_IFUNC symbols in PIE:
+
+ 1. Direct function call.
+ 2. Function pointer.
+ 3. Reference from a shared library.
+ */
+
+#include <stdlib.h>
+#include "ifunc-sel.h"
+
+typedef int (*foo_p) (void);
+extern foo_p foo_ptr;
+
+static int
+one (void)
+{
+ return -30;
+}
+
+void * foo_ifunc (void) __asm__ ("foo");
+__asm__(".type foo, %gnu_indirect_function");
+
+void *
+foo_ifunc (void)
+{
+ return ifunc_one (one);
+}
+
+extern int foo (void);
+extern foo_p get_foo (void);
+extern foo_p get_foo_p (void);
+
+foo_p my_foo_ptr = foo;
+
+int
+main (void)
+{
+ foo_p p;
+
+ p = get_foo ();
+ if (p != foo)
+ abort ();
+ if ((*p) () != -30)
+ abort ();
+
+ p = get_foo_p ();
+ if (p != foo)
+ abort ();
+ if ((*p) () != -30)
+ abort ();
+
+ if (foo_ptr != foo)
+ abort ();
+ if (my_foo_ptr != foo)
+ abort ();
+ if ((*foo_ptr) () != -30)
+ abort ();
+ if ((*my_foo_ptr) () != -30)
+ abort ();
+ if (foo () != -30)
+ abort ();
+
+ return 0;
+}
Index: testsuite/ifuncmain7.c
===================================================================
RCS file: testsuite/ifuncmain7.c
diff -N testsuite/ifuncmain7.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncmain7.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,74 @@
+/* Test local STT_GNU_IFUNC symbols:
+
+ 1. Direct function call.
+ 2. Function pointer.
+ */
+
+#include <stdlib.h>
+#include "ifunc-sel.h"
+
+extern int foo (void);
+
+static int
+one (void)
+{
+ return -30;
+}
+
+static void * foo_ifunc (void) __asm__ ("foo");
+__asm__(".type foo, %gnu_indirect_function");
+
+static void *
+__attribute__ ((used))
+foo_ifunc (void)
+{
+ return ifunc_one (one);
+}
+
+typedef int (*foo_p) (void);
+
+extern foo_p __attribute__ ((noinline)) get_foo_p (void);
+extern foo_p __attribute__ ((noinline)) get_foo (void);
+
+foo_p foo_ptr = foo;
+
+foo_p
+__attribute__ ((noinline))
+get_foo_p (void)
+{
+ return foo_ptr;
+}
+
+foo_p
+__attribute__ ((noinline))
+get_foo (void)
+{
+ return foo;
+}
+
+int
+main (void)
+{
+ foo_p p;
+
+ p = get_foo ();
+ if (p != foo)
+ abort ();
+ if ((*p) () != -30)
+ abort ();
+
+ p = get_foo_p ();
+ if (p != foo)
+ abort ();
+ if ((*p) () != -30)
+ abort ();
+
+ if (foo_ptr != foo)
+ abort ();
+ if ((*foo_ptr) () != -30)
+ abort ();
+ if (foo () != -30)
+ abort ();
+
+ return 0;
+}
Index: testsuite/ifuncmod1.c
===================================================================
RCS file: testsuite/ifuncmod1.c
diff -N testsuite/ifuncmod1.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncmod1.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,95 @@
+/* Test STT_GNU_IFUNC symbols:
+
+ 1. Direct function call.
+ 2. Function pointer.
+ 3. Visibility.
+ */
+#include "ifunc-sel.h"
+
+int global __attribute__ ((visibility ("protected"))) = -1;
+
+static int
+one (void)
+{
+ return 1;
+}
+
+static int
+minus_one (void)
+{
+ return -1;
+}
+
+static int
+zero (void)
+{
+ return 0;
+}
+
+void * foo_ifunc (void) __asm__ ("foo");
+__asm__(".type foo, %gnu_indirect_function");
+
+void *
+foo_ifunc (void)
+{
+ return ifunc_sel (one, minus_one, zero);
+}
+
+void * foo_hidden_ifunc (void) __asm__ ("foo_hidden");
+__asm__(".type foo_hidden, %gnu_indirect_function");
+
+void *
+foo_hidden_ifunc (void)
+{
+ return ifunc_sel (minus_one, one, zero);
+}
+
+void * foo_protected_ifunc (void) __asm__ ("foo_protected");
+__asm__(".type foo_protected, %gnu_indirect_function");
+
+void *
+foo_protected_ifunc (void)
+{
+ return ifunc_sel (one, zero, minus_one);
+}
+
+/* Test hidden indirect function. */
+__asm__(".hidden foo_hidden");
+
+/* Test protected indirect function. */
+__asm__(".protected foo_protected");
+
+extern int foo (void);
+extern int foo_hidden (void);
+extern int foo_protected (void);
+extern int ret_foo;
+extern int ret_foo_hidden;
+extern int ret_foo_protected;
+
+#define FOO_P
+typedef int (*foo_p) (void);
+
+extern foo_p get_foo_p (void);
+extern foo_p get_foo_hidden_p (void);
+extern foo_p get_foo_protected_p (void);
+
+foo_p
+get_foo_p (void)
+{
+ ret_foo = foo ();
+ return foo;
+}
+
+foo_p
+get_foo_hidden_p (void)
+{
+ ret_foo_hidden = foo_hidden ();
+ return foo_hidden;
+}
+
+foo_p
+get_foo_protected_p (void)
+{
+ ret_foo_protected = foo_protected ();
+ return foo_protected;
+}
Index: testsuite/ifuncmod3.c
===================================================================
RCS file: testsuite/ifuncmod3.c
diff -N testsuite/ifuncmod3.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncmod3.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,7 @@
+/* Test STT_GNU_IFUNC symbols with dlopen. */
+
+#include "ifuncmod1.c"
+
+int ret_foo;
+int ret_foo_hidden;
+int ret_foo_protected;
Index: testsuite/ifuncmod5.c
===================================================================
RCS file: testsuite/ifuncmod5.c
diff -N testsuite/ifuncmod5.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncmod5.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,55 @@
+/* Test STT_GNU_IFUNC symbols without direct function call. */
+#include "ifunc-sel.h"
+
+int global __attribute__ ((visibility ("protected"))) = -1;
+
+static int
+one (void)
+{
+ return 1;
+}
+
+static int
+minus_one (void)
+{
+ return -1;
+}
+
+static int
+zero (void)
+{
+ return 0;
+}
+
+void * foo_ifunc (void) __asm__ ("foo");
+__asm__(".type foo, %gnu_indirect_function");
+
+void *
+foo_ifunc (void)
+{
+ return ifunc_sel (one, minus_one, zero);
+}
+
+void * foo_hidden_ifunc (void) __asm__ ("foo_hidden");
+__asm__(".type foo_hidden, %gnu_indirect_function");
+
+void *
+foo_hidden_ifunc (void)
+{
+ return ifunc_sel (minus_one, one, zero);
+}
+
+void * foo_protected_ifunc (void) __asm__ ("foo_protected");
+__asm__(".type foo_protected, %gnu_indirect_function");
+
+void *
+foo_protected_ifunc (void)
+{
+ return ifunc_sel (one, zero, minus_one);
+}
+
+/* Test hidden indirect function. */
+__asm__(".hidden foo_hidden");
+
+/* Test protected indirect function. */
+__asm__(".protected foo_protected");
Index: testsuite/ifuncmod6.c
===================================================================
RCS file: testsuite/ifuncmod6.c
diff -N testsuite/ifuncmod6.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/ifuncmod6.c 19 Aug 2010 22:49:24 -0000
@@ -0,0 +1,22 @@
+/* Test STT_GNU_IFUNC symbol reference in a shared library. */
+
+extern int foo (void);
+
+typedef int (*foo_p) (void);
+
+extern foo_p get_foo_p (void);
+extern foo_p get_foo (void);
+
+foo_p foo_ptr = foo;
+
+foo_p
+get_foo_p (void)
+{
+ return foo_ptr;
+}
+
+foo_p
+get_foo (void)
+{
+ return foo;
+}