PR gdb/12659:
[binutils-gdb.git] / gold / incremental.h
index dc8ef33e05853dfd0ab03c72757dbf2d8492ed89..0edb190885d9904fc5c4802987a2a4f81edcb612 100644 (file)
@@ -1,6 +1,6 @@
 // inremental.h -- incremental linking support for gold   -*- C++ -*-
 
-// Copyright 2009, 2010 Free Software Foundation, Inc.
+// Copyright 2009, 2010, 2011 Free Software Foundation, Inc.
 // Written by Mikolaj Zalewski <mikolajz@google.com>.
 
 // This file is part of gold.
@@ -46,7 +46,6 @@ class Incremental_inputs;
 class Incremental_binary;
 class Incremental_library;
 class Object;
-class Script_info;
 
 // Incremental input type as stored in .gnu_incremental_inputs.
 
@@ -68,6 +67,25 @@ enum Incremental_input_flags
   INCREMENTAL_INPUT_AS_NEEDED = 0x4000
 };
 
+// Symbol flags for the incremental symbol table.
+// These flags are stored in the top two bits of
+// the symbol index field.
+
+enum Incremental_shlib_symbol_flags
+{
+  // Symbol is defined in this library.
+  INCREMENTAL_SHLIB_SYM_DEF = 2,
+  // Symbol is defined in this library, with a COPY relocation.
+  INCREMENTAL_SHLIB_SYM_COPY = 3
+};
+
+static const int INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT = 30;
+
+// Return TRUE if a section of type SH_TYPE can be updated in place
+// during an incremental update.
+bool
+can_incremental_update(unsigned int sh_type);
+
 // Create an Incremental_binary object for FILE. Returns NULL is this is not
 // possible, e.g. FILE is not an ELF file or has an unsupported target.
 
@@ -245,7 +263,13 @@ class Script_info
 {
  public:
   Script_info(const std::string& filename)
-    : filename_(filename), incremental_script_entry_(NULL)
+    : filename_(filename), input_file_index_(0),
+      incremental_script_entry_(NULL)
+  { }
+
+  Script_info(const std::string& filename, unsigned int input_file_index)
+    : filename_(filename), input_file_index_(input_file_index),
+      incremental_script_entry_(NULL)
   { }
 
   // Store a pointer to the incremental information for this script.
@@ -258,6 +282,11 @@ class Script_info
   filename() const
   { return this->filename_; }
 
+  // Return the input file index.
+  unsigned int
+  input_file_index() const
+  { return this->input_file_index_; }
+
   // Return the pointer to the incremental information for this script.
   Incremental_script_entry*
   incremental_info() const
@@ -265,6 +294,7 @@ class Script_info
 
  private:
   const std::string filename_;
+  unsigned int input_file_index_;
   Incremental_script_entry* incremental_script_entry_;
 };
 
@@ -325,7 +355,7 @@ class Incremental_object_entry : public Incremental_input_entry
   Incremental_object_entry(Stringpool::Key filename_key, Object* obj,
                           unsigned int arg_serial, Timespec mtime)
     : Incremental_input_entry(filename_key, arg_serial, mtime), obj_(obj),
-      is_member_(false), sections_()
+      is_member_(false), sections_(), groups_()
   { this->sections_.reserve(obj->shnum()); }
 
   // Get the object.
@@ -368,6 +398,21 @@ class Incremental_object_entry : public Incremental_input_entry
   get_input_section_size(unsigned int n) const
   { return this->sections_[n].sh_size_; }
 
+  // Add a kept COMDAT group.
+  void
+  add_comdat_group(Stringpool::Key signature_key)
+  { this->groups_.push_back(signature_key); }
+
+  // Return the number of COMDAT groups.
+  unsigned int
+  get_comdat_group_count() const
+  { return this->groups_.size(); }
+
+  // Return the stringpool key for the signature of the Nth comdat group.
+  Stringpool::Key
+  get_comdat_signature_key(unsigned int n) const
+  { return this->groups_[n]; }
+
  protected:
   virtual Incremental_input_type
   do_type() const
@@ -400,6 +445,9 @@ class Incremental_object_entry : public Incremental_input_entry
     off_t sh_size_;
   };
   std::vector<Input_section> sections_;
+
+  // COMDAT groups.
+  std::vector<Stringpool::Key> groups_;
 };
 
 // Class for recording shared library input files.
@@ -546,6 +594,10 @@ class Incremental_inputs
   report_input_section(Object* obj, unsigned int shndx, const char* name,
                       off_t sh_size);
 
+  // Record a kept COMDAT group belonging to object file OBJ.
+  void
+  report_comdat_group(Object* obj, const char* name);
+
   // Record the info for input script SCRIPT.
   void
   report_script(Script_info* script, unsigned int arg_serial,
@@ -814,7 +866,7 @@ class Incremental_inputs_reader
                  || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
 
       unsigned int section_count = this->get_input_section_count();
-      return (this->info_offset_ + 24
+      return (this->info_offset_ + 28
              + section_count * input_section_entry_size
              + symndx * 20);
     }
@@ -869,6 +921,16 @@ class Incremental_inputs_reader
       return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 20);
     }
 
+    // Return the COMDAT group count -- for objects only.
+    unsigned int
+    get_comdat_group_count() const
+    {
+      gold_assert(this->type() == INCREMENTAL_INPUT_OBJECT
+                 || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
+
+      return Swap32::readval(this->inputs_->p_ + this->info_offset_ + 24);
+    }
+
     // Return the object count -- for scripts only.
     unsigned int
     get_object_count() const
@@ -939,7 +1001,7 @@ class Incremental_inputs_reader
     {
       Input_section_info info;
       const unsigned char* p = (this->inputs_->p_
-                               + this->info_offset_ + 24
+                               + this->info_offset_ + 28
                                + n * input_section_entry_size);
       unsigned int name_offset = Swap32::readval(p);
       info.name = this->inputs_->get_string(name_offset);
@@ -957,25 +1019,56 @@ class Incremental_inputs_reader
                  || this->type() == INCREMENTAL_INPUT_ARCHIVE_MEMBER);
       unsigned int section_count = this->get_input_section_count();
       const unsigned char* p = (this->inputs_->p_
-                               + this->info_offset_ + 24
+                               + this->info_offset_ + 28
                                + section_count * input_section_entry_size
                                + n * 20);
       return Incremental_global_symbol_reader<big_endian>(p);
     }
 
+    // Return the signature of the Nth comdat group -- for objects only.
+    const char*
+    get_comdat_group_signature(unsigned int n) const
+    {
+      unsigned int section_count = this->get_input_section_count();
+      unsigned int symbol_count = this->get_global_symbol_count();
+      const unsigned char* p = (this->inputs_->p_
+                               + this->info_offset_ + 28
+                               + section_count * input_section_entry_size
+                               + symbol_count * 20
+                               + n * 4);
+      unsigned int name_offset = Swap32::readval(p);
+      return this->inputs_->get_string(name_offset);
+    }
+
     // Return the output symbol index for the Nth global symbol -- for shared
     // libraries only.  Sets *IS_DEF to TRUE if the symbol is defined in this
-    // input file.
+    // input file.  Sets *IS_COPY to TRUE if the symbol was copied from this
+    // input file with a COPY relocation.
     unsigned int
-    get_output_symbol_index(unsigned int n, bool* is_def)
+    get_output_symbol_index(unsigned int n, bool* is_def, bool* is_copy)
     {
       gold_assert(this->type() == INCREMENTAL_INPUT_SHARED_LIBRARY);
       const unsigned char* p = (this->inputs_->p_
                                + this->info_offset_ + 8
                                + n * 4);
       unsigned int output_symndx = Swap32::readval(p);
-      *is_def = (output_symndx & (1U << 31)) != 0;
-      return output_symndx & ((1U << 31) - 1);
+      unsigned int flags = output_symndx >> INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT;
+      output_symndx &= ((1U << INCREMENTAL_SHLIB_SYM_FLAGS_SHIFT) - 1);
+      switch (flags)
+       {
+         case INCREMENTAL_SHLIB_SYM_DEF:
+           *is_def = true;
+           *is_copy = false;
+           break;
+         case INCREMENTAL_SHLIB_SYM_COPY:
+           *is_def = true;
+           *is_copy = true;
+           break;
+         default:
+           *is_def = false;
+           *is_copy = false;
+       }
+      return output_symndx;
     }
 
    private:
@@ -1326,12 +1419,12 @@ class Incremental_binary
 
   // Return an Incremental_library for the given input file.
   Incremental_library*
-  get_library(unsigned int n)
+  get_library(unsigned int n) const
   { return this->library_map_[n]; }
 
   // Return a Script_info for the given input file.
   Script_info*
-  get_script_info(unsigned int n)
+  get_script_info(unsigned int n) const
   { return this->script_map_[n]; }
 
   // Initialize the layout of the output file based on the existing
@@ -1350,6 +1443,11 @@ class Incremental_binary
   process_got_plt(Symbol_table* symtab, Layout* layout)
   { this->do_process_got_plt(symtab, layout); }
 
+  // Emit COPY relocations from the existing output file.
+  void
+  emit_copy_relocs(Symbol_table* symtab)
+  { this->do_emit_copy_relocs(symtab); }
+
   // Apply incremental relocations for symbols whose values have changed.
   void
   apply_incremental_relocs(const Symbol_table* symtab, Layout* layout,
@@ -1432,6 +1530,10 @@ class Incremental_binary
   virtual void
   do_process_got_plt(Symbol_table* symtab, Layout* layout) = 0;
 
+  // Emit COPY relocations from the existing output file.
+  virtual void
+  do_emit_copy_relocs(Symbol_table* symtab) = 0;
+
   // Apply incremental relocations for symbols whose values have changed.
   virtual void
   do_apply_incremental_relocs(const Symbol_table*, Layout*, Output_file*) = 0;
@@ -1467,9 +1569,9 @@ class Sized_incremental_binary : public Incremental_binary
                            const elfcpp::Ehdr<size, big_endian>& ehdr,
                            Target* target)
     : Incremental_binary(output, target), elf_file_(this, ehdr),
-      input_objects_(), section_map_(), symbol_map_(), main_symtab_loc_(),
-      main_strtab_loc_(), has_incremental_info_(false), inputs_reader_(),
-      symtab_reader_(), relocs_reader_(), got_plt_reader_(),
+      input_objects_(), section_map_(), symbol_map_(), copy_relocs_(),
+      main_symtab_loc_(), main_strtab_loc_(), has_incremental_info_(false),
+      inputs_reader_(), symtab_reader_(), relocs_reader_(), got_plt_reader_(),
       input_entry_readers_()
   { this->setup_readers(); }
 
@@ -1511,6 +1613,11 @@ class Sized_incremental_binary : public Incremental_binary
   global_symbol(unsigned int symndx) const
   { return this->symbol_map_[symndx]; }
 
+  // Add a COPY relocation for a global symbol.
+  void
+  add_copy_reloc(Symbol* gsym, Output_section* os, off_t offset)
+  { this->copy_relocs_.push_back(Copy_reloc(gsym, os, offset)); }
+
   // Readers for the incremental info sections.
 
   const Incremental_inputs_reader<size, big_endian>&
@@ -1559,6 +1666,10 @@ class Sized_incremental_binary : public Incremental_binary
   virtual void
   do_process_got_plt(Symbol_table* symtab, Layout* layout);
 
+  // Emit COPY relocations from the existing output file.
+  virtual void
+  do_emit_copy_relocs(Symbol_table* symtab);
+
   // Apply incremental relocations for symbols whose values have changed.
   virtual void
   do_apply_incremental_relocs(const Symbol_table* symtab, Layout* layout,
@@ -1617,6 +1728,22 @@ class Sized_incremental_binary : public Incremental_binary
   }
 
  private:
+  // List of symbols that need COPY relocations.
+  struct Copy_reloc
+  {
+    Copy_reloc(Symbol* sym, Output_section* os, off_t off)
+      : symbol(sym), output_section(os), offset(off)
+    { }
+
+    // The global symbol to copy.
+    Symbol* symbol;
+    // The output section into which the symbol was copied.
+    Output_section* output_section;
+    // The offset within that output section.
+    off_t offset;
+  };
+  typedef std::vector<Copy_reloc> Copy_relocs;
+
   bool
   find_incremental_inputs_sections(unsigned int* p_inputs_shndx,
                                   unsigned int* p_symtab_shndx,
@@ -1640,6 +1767,9 @@ class Sized_incremental_binary : public Incremental_binary
   // Map global symbols from the input file to the symbol table.
   std::vector<Symbol*> symbol_map_;
 
+  // List of symbols that need COPY relocations.
+  Copy_relocs copy_relocs_;
+
   // Locations of the main symbol table and symbol string table.
   Location main_symtab_loc_;
   Location main_strtab_loc_;
@@ -1794,6 +1924,15 @@ class Sized_relobj_incr : public Sized_relobj<size, big_endian>
   do_get_global_symbols() const
   { return &this->symbols_; }
 
+  // Return the value of a local symbol.
+  uint64_t
+  do_local_symbol_value(unsigned int, uint64_t) const
+  { gold_unreachable(); }
+
+  unsigned int
+  do_local_plt_offset(unsigned int) const
+  { gold_unreachable(); }
+
   // Return the number of local symbols.
   unsigned int
   do_local_symbol_count() const
@@ -1866,6 +2005,8 @@ class Sized_relobj_incr : public Sized_relobj<size, big_endian>
   unsigned int local_dynsym_offset_;
   // The entries in the symbol table for the external symbols.
   Symbols symbols_;
+  // Number of symbols defined in object file itself.
+  size_t defined_count_;
   // The offset of the first incremental relocation for this object.
   unsigned int incr_reloc_offset_;
   // The number of incremental relocations for this object.
@@ -1997,6 +2138,8 @@ class Sized_incr_dynobj : public Dynobj
   Input_entry_reader input_reader_;
   // The entries in the symbol table for the external symbols.
   Symbols symbols_;
+  // Number of symbols defined in object file itself.
+  size_t defined_count_;
 };
 
 // Allocate an incremental object of the appropriate size and endianness.