PR 9918
[deliverable/binutils-gdb.git] / gold / i386.cc
index 917ec0b64ac4566192042c704e6425ffc1483b25..3beacaa28cae2ea1326ed94875a8e50eeffe75ef 100644 (file)
@@ -1,6 +1,6 @@
 // i386.cc -- i386 target support for gold.
 
-// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
+// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
 // Written by Ian Lance Taylor <iant@google.com>.
 
 // This file is part of gold.
@@ -59,9 +59,25 @@ class Target_i386 : public Sized_target<32, false>
     : Sized_target<32, false>(&i386_info),
       got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
       copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
-      got_mod_index_offset_(-1U)
+      got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
   { }
 
+  // Process the relocations to determine unreferenced sections for 
+  // garbage collection.
+  void
+  gc_process_relocs(const General_options& options,
+                    Symbol_table* symtab,
+                    Layout* layout,
+                    Sized_relobj<32, false>* object,
+                    unsigned int data_shndx,
+                    unsigned int sh_type,
+                    const unsigned char* prelocs,
+                    size_t reloc_count,
+                    Output_section* output_section,
+                    bool needs_special_offset_handling,
+                    size_t local_symbol_count,
+                    const unsigned char* plocal_symbols);
+
   // Scan the relocations to look for symbol adjustments.
   void
   scan_relocs(const General_options& options,
@@ -135,7 +151,7 @@ class Target_i386 : public Sized_target<32, false>
 
   // Return whether SYM is defined by the ABI.
   bool
-  do_is_defined_by_abi(Symbol* sym) const
+  do_is_defined_by_abi(const Symbol* sym) const
   { return strcmp(sym->name(), "___tls_get_addr") == 0; }
 
   // Return the size of the GOT section.
@@ -198,13 +214,14 @@ class Target_i386 : public Sized_target<32, false>
     inline bool
     should_apply_static_reloc(const Sized_symbol<32>* gsym,
                               int ref_flags,
-                              bool is_32bit);
+                              bool is_32bit,
+                             Output_section* output_section);
 
     // Do a relocation.  Return false if the caller should not issue
     // any warnings about this relocation.
     inline bool
-    relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
-            const elfcpp::Rel<32, false>&,
+    relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
+            size_t relnum, const elfcpp::Rel<32, false>&,
             unsigned int r_type, const Sized_symbol<32>*,
             const Symbol_value<32>*,
             unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
@@ -323,6 +340,10 @@ class Target_i386 : public Sized_target<32, false>
   void
   make_plt_entry(Symbol_table*, Layout*, Symbol*);
 
+  // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
+  void
+  define_tls_base_symbol(Symbol_table*, Layout*);
+
   // Create a GOT entry for the TLS module index.
   unsigned int
   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
@@ -353,7 +374,8 @@ class Target_i386 : public Sized_target<32, false>
 
   // Add a potential copy relocation.
   void
-  copy_reloc(Symbol_table* symtab, Layout* layout, Relobj* object,
+  copy_reloc(Symbol_table* symtab, Layout* layout,
+             Sized_relobj<32, false>* object,
             unsigned int shndx, Output_section* output_section,
             Symbol* sym, const elfcpp::Rel<32, false>& reloc)
   {
@@ -391,6 +413,8 @@ class Target_i386 : public Sized_target<32, false>
   Output_data_space* dynbss_;
   // Offset of the GOT entry for the TLS module index.
   unsigned int got_mod_index_offset_;
+  // True if the _TLS_MODULE_BASE_ symbol has been defined.
+  bool tls_base_symbol_defined_;
 };
 
 const Target::Target_info Target_i386::i386_info =
@@ -420,18 +444,23 @@ Target_i386::got_section(Symbol_table* symtab, Layout* layout)
 
       this->got_ = new Output_data_got<32, false>();
 
-      layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
-                                     elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
-                                     this->got_);
+      Output_section* os;
+      os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
+                                          (elfcpp::SHF_ALLOC
+                                           | elfcpp::SHF_WRITE),
+                                          this->got_);
+      os->set_is_relro();
 
       // The old GNU linker creates a .got.plt section.  We just
       // create another set of data in the .got section.  Note that we
       // always create a PLT if we create a GOT, although the PLT
       // might be empty.
-      this->got_plt_ = new Output_data_space(4);
-      layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
-                                     elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
-                                     this->got_plt_);
+      this->got_plt_ = new Output_data_space(4, "** GOT PLT");
+      os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
+                                          (elfcpp::SHF_ALLOC
+                                           | elfcpp::SHF_WRITE),
+                                          this->got_plt_);
+      os->set_is_relro();
 
       // The first three entries are reserved.
       this->got_plt_->set_current_data_size(3 * 4);
@@ -456,7 +485,7 @@ Target_i386::rel_dyn_section(Layout* layout)
   if (this->rel_dyn_ == NULL)
     {
       gold_assert(layout != NULL);
-      this->rel_dyn_ = new Reloc_section();
+      this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
                                      elfcpp::SHF_ALLOC, this->rel_dyn_);
     }
@@ -485,6 +514,11 @@ class Output_data_plt_i386 : public Output_section_data
   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;
@@ -526,7 +560,7 @@ Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
                                           Output_data_space* got_plt)
   : Output_section_data(4), got_plt_(got_plt), count_(0)
 {
-  this->rel_ = new Reloc_section();
+  this->rel_ = new Reloc_section(false);
   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
                                  elfcpp::SHF_ALLOC, this->rel_);
 }
@@ -719,6 +753,31 @@ Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
   this->plt_->add_entry(gsym);
 }
 
+// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
+
+void
+Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
+{
+  if (this->tls_base_symbol_defined_)
+    return;
+
+  Output_segment* tls_segment = layout->tls_segment();
+  if (tls_segment != NULL)
+    {
+      bool is_exec = parameters->options().output_is_executable();
+      symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
+                                      tls_segment, 0, 0,
+                                      elfcpp::STT_TLS,
+                                      elfcpp::STB_LOCAL,
+                                      elfcpp::STV_HIDDEN, 0,
+                                      (is_exec
+                                       ? Symbol::SEGMENT_END
+                                       : Symbol::SEGMENT_START),
+                                      true);
+    }
+  this->tls_base_symbol_defined_ = true;
+}
+
 // Create a GOT entry for the TLS module index.
 
 unsigned int
@@ -854,18 +913,24 @@ Target_i386::Scan::local(const General_options&,
       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());
           if (lsym.get_st_type() != elfcpp::STT_SECTION)
-            {
-              unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
-              rel_dyn->add_local(object, r_sym, r_type, output_section,
-                                 data_shndx, reloc.get_r_offset());
-            }
+           rel_dyn->add_local(object, r_sym, r_type, output_section,
+                              data_shndx, reloc.get_r_offset());
           else
             {
               gold_assert(lsym.get_st_value() == 0);
-              rel_dyn->add_local_section(object, lsym.get_st_shndx(),
-                                         r_type, output_section,
-                                         data_shndx, reloc.get_r_offset());
+             unsigned int shndx = lsym.get_st_shndx();
+             bool is_ordinary;
+             shndx = object->adjust_sym_shndx(r_sym, shndx,
+                                              &is_ordinary);
+             if (!is_ordinary)
+               object->error(_("section symbol %u has bad shndx %u"),
+                             r_sym, shndx);
+             else
+               rel_dyn->add_local_section(object, shndx,
+                                          r_type, output_section,
+                                          data_shndx, reloc.get_r_offset());
             }
         }
       break;
@@ -948,28 +1013,41 @@ Target_i386::Scan::local(const General_options&,
                 Output_data_got<32, false>* got
                     = target->got_section(symtab, layout);
                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
-                got->add_local_pair_with_rel(object, r_sym, 
-                                             lsym.get_st_shndx(),
-                                             GOT_TYPE_TLS_PAIR,
-                                             target->rel_dyn_section(layout),
-                                             elfcpp::R_386_TLS_DTPMOD32, 0);
+               unsigned int shndx = lsym.get_st_shndx();
+               bool is_ordinary;
+               shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
+               if (!is_ordinary)
+                 object->error(_("local symbol %u has bad shndx %u"),
+                             r_sym, shndx);
+                else
+                 got->add_local_pair_with_rel(object, r_sym, shndx,
+                                              GOT_TYPE_TLS_PAIR,
+                                              target->rel_dyn_section(layout),
+                                              elfcpp::R_386_TLS_DTPMOD32, 0);
              }
            else if (optimized_type != tls::TLSOPT_TO_LE)
              unsupported_reloc_local(object, r_type);
            break;
 
          case elfcpp::R_386_TLS_GOTDESC:     // Global-dynamic (from ~oliva)
+           target->define_tls_base_symbol(symtab, layout);
             if (optimized_type == tls::TLSOPT_NONE)
               {
                 // Create a double GOT entry with an R_386_TLS_DESC reloc.
                 Output_data_got<32, false>* got
                     = target->got_section(symtab, layout);
                 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
-                got->add_local_pair_with_rel(object, r_sym, 
-                                             lsym.get_st_shndx(),
-                                             GOT_TYPE_TLS_DESC,
-                                             target->rel_dyn_section(layout),
-                                             elfcpp::R_386_TLS_DESC, 0);
+               unsigned int shndx = lsym.get_st_shndx();
+               bool is_ordinary;
+               shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
+               if (!is_ordinary)
+                 object->error(_("local symbol %u has bad shndx %u"),
+                             r_sym, shndx);
+                else
+                 got->add_local_pair_with_rel(object, r_sym, shndx,
+                                              GOT_TYPE_TLS_DESC,
+                                              target->rel_dyn_section(layout),
+                                              elfcpp::R_386_TLS_DESC, 0);
               }
             else if (optimized_type != tls::TLSOPT_TO_LE)
               unsupported_reloc_local(object, r_type);
@@ -1285,6 +1363,7 @@ Target_i386::Scan::global(const General_options&,
            break;
 
          case elfcpp::R_386_TLS_GOTDESC:     // Global-dynamic (~oliva url)
+           target->define_tls_base_symbol(symtab, layout);
             if (optimized_type == tls::TLSOPT_NONE)
               {
                 // Create a double GOT entry with an R_386_TLS_DESC reloc.
@@ -1394,6 +1473,38 @@ Target_i386::Scan::global(const General_options&,
     }
 }
 
+// Process relocations for gc.
+
+void
+Target_i386::gc_process_relocs(const General_options& options,
+                               Symbol_table* symtab,
+                               Layout* layout,
+                               Sized_relobj<32, false>* object,
+                               unsigned int data_shndx,
+                               unsigned int,
+                               const unsigned char* prelocs,
+                               size_t reloc_count,
+                               Output_section* output_section,
+                               bool needs_special_offset_handling,
+                               size_t local_symbol_count,
+                               const unsigned char* plocal_symbols)
+{
+  gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
+                         Target_i386::Scan>(
+    options,
+    symtab,
+    layout,
+    this,
+    object,
+    data_shndx,
+    prelocs,
+    reloc_count,
+    output_section,
+    needs_special_offset_handling,
+    local_symbol_count,
+    plocal_symbols);
+}
+
 // Scan relocations for a section.
 
 void
@@ -1485,8 +1596,15 @@ Target_i386::do_finalize_sections(Layout* layout)
 inline bool
 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
                                                  int ref_flags,
-                                                 bool is_32bit)
+                                                 bool is_32bit,
+                                                Output_section* output_section)
 {
+  // If the output section is not allocated, then we didn't call
+  // scan_relocs, we didn't create a dynamic reloc, and we must apply
+  // the reloc here.
+  if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
+    return true;
+
   // For local symbols, we will have created a non-RELATIVE dynamic
   // relocation only if (a) the output is position independent,
   // (b) the relocation is absolute (not pc- or segment-relative), and
@@ -1512,6 +1630,7 @@ Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
 inline bool
 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
                                Target_i386* target,
+                               Output_section *output_section,
                                size_t relnum,
                                const elfcpp::Rel<32, false>& rel,
                                unsigned int r_type,
@@ -1523,7 +1642,8 @@ Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
 {
   if (this->skip_call_tls_get_addr_)
     {
-      if (r_type != elfcpp::R_386_PLT32
+      if ((r_type != elfcpp::R_386_PLT32
+           && r_type != elfcpp::R_386_PC32)
          || gsym == NULL
          || strcmp(gsym->name(), "___tls_get_addr") != 0)
        gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
@@ -1537,15 +1657,10 @@ Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
 
   // Pick the value to use for symbols defined in shared objects.
   Symbol_value<32> symval;
-  bool is_nonpic = (r_type == elfcpp::R_386_PC8
-                    || r_type == elfcpp::R_386_PC16
-                    || r_type == elfcpp::R_386_PC32);
   if (gsym != NULL
-      && (gsym->is_from_dynobj()
-          || (parameters->options().shared()
-              && (gsym->is_undefined() || gsym->is_preemptible())))
-      && gsym->has_plt_offset()
-      && (!is_nonpic || !parameters->options().shared()))
+      && 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());
@@ -1591,7 +1706,8 @@ Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
       break;
 
     case elfcpp::R_386_32:
-      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true))
+      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
+                                   output_section))
         Relocate_functions<32, false>::rel32(view, object, psymval);
       break;
 
@@ -1600,13 +1716,14 @@ Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
         int ref_flags = Symbol::NON_PIC_REF;
         if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
           ref_flags |= Symbol::FUNCTION_CALL;
-        if (should_apply_static_reloc(gsym, ref_flags, true))
+        if (should_apply_static_reloc(gsym, ref_flags, true, output_section))
           Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
       }
       break;
 
     case elfcpp::R_386_16:
-      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
+      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
+                                   output_section))
         Relocate_functions<32, false>::rel16(view, object, psymval);
       break;
 
@@ -1615,13 +1732,14 @@ Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
         int ref_flags = Symbol::NON_PIC_REF;
         if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
           ref_flags |= Symbol::FUNCTION_CALL;
-        if (should_apply_static_reloc(gsym, ref_flags, false))
-          Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
+        if (should_apply_static_reloc(gsym, ref_flags, false, output_section))
+          Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
       }
       break;
 
     case elfcpp::R_386_8:
-      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
+      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
+                                   output_section))
         Relocate_functions<32, false>::rel8(view, object, psymval);
       break;
 
@@ -1630,8 +1748,9 @@ Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
         int ref_flags = Symbol::NON_PIC_REF;
         if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
           ref_flags |= Symbol::FUNCTION_CALL;
-        if (should_apply_static_reloc(gsym, ref_flags, false))
-          Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
+        if (should_apply_static_reloc(gsym, ref_flags, false,
+                                     output_section))
+          Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
       }
       break;
 
@@ -1796,6 +1915,7 @@ Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
 
     case elfcpp::R_386_TLS_GOTDESC:      // Global-dynamic (from ~oliva url)
     case elfcpp::R_386_TLS_DESC_CALL:
+      this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
       if (optimized_type == tls::TLSOPT_TO_LE)
         {
          gold_assert(tls_segment != NULL);
@@ -1880,7 +2000,8 @@ Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
       // This reloc can appear in debugging sections, in which case we
       // won't see the TLS_LDM reloc.  The local_dynamic_type field
       // tells us this.
-      if (optimized_type == tls::TLSOPT_TO_LE)
+      if (optimized_type == tls::TLSOPT_TO_LE
+          && this->local_dynamic_type_ != LOCAL_DYNAMIC_NONE)
        {
           gold_assert(tls_segment != NULL);
           value -= tls_segment->memsz();
This page took 0.029625 seconds and 4 git commands to generate.